1

I want to convert Asciidoctor files into PDF files using Gradle and the AsciidoctorPdf plugin. To be able to customize the resulting PDF, I define a custom theme. As base font I use the EB-Garamond font from Google.

My theme references the default theme, which references the bundled Noto Serif font: https://github.com/asciidoctor/asciidoctor-pdf/blob/main/data/themes/default-theme.yml

The search path for the bundled fonts is provided by GEM_FONTS_DIR keyword:

Font paths can be absolute or relative. Absolute paths are used as is. Relative font paths are resolved from the font search path. You can also use the GEM_FONTS_DIR keyword to refer to the location of the bundled fonts.

https://docs.asciidoctor.org/pdf-converter/latest/theme/custom-font/#declare

I set the relative path to the directory containing the EB-Garamond font in the extension of AsciidoctorPdf :

tasks.asciidoctorPdf {
    setFontsDirs(listOf("fonts"))
}

Now when I run the build, I get an error message:

No such file or directory - notoserif-regular-subset.ttf not found in <absolute-path>/play-books/fonts
Exception in thread "main" org.asciidoctor.gradle.remote.AsciidoctorRemoteExecutionException: Error running Asciidoctor whilst attempting to process <absolute-path>/cocinero.adoc using backend pdf

I suspect that by calling setFontsDirs, the search path for fonts is completely reset and thus the path to the bundled fonts is also lost.

How can I pass the path to my own font directory to AsciidoctorPdf without losing the path to the bundled fonts?

build.gradle.kts:

plugins {
    id("org.asciidoctor.jvm.convert")
    id("org.asciidoctor.jvm.pdf")
}

tasks.asciidoctorPdf {
    dependsOn("asciidoctor")

    setFontsDirs(listOf("fonts"))
    setTheme("play-book")
}

pdfThemes {
    local("play-book") {
        themeDir = projectDir
    }
}

tasks.build {
    dependsOn("asciidoctor")
    dependsOn("asciidoctorPdf")
}

settings.gradle.kts:

pluginManagement {
    repositories {
        gradlePluginPortal()
        mavenCentral()
    }

    plugins {
        val versions = object {
            val asciidoctor = "3.3.2"
        }

        id("org.asciidoctor.jvm.convert") version versions.asciidoctor
        id("org.asciidoctor.jvm.pdf") version versions.asciidoctor
    }
}

dependencyResolutionManagement {
    repositories {
        mavenCentral()
    }
}

rootProject.name = "cartel-deutsch"

include("play-books")

play-book-theme.yml:

extends: default

font:
  catalog:
    merge: true
    EBGaramond:
      normal: EB_Garamond/static/EBGaramond-Regular.ttf
      italic: EB_Garamond/static/EBGaramond-Italic.ttf
      bold: EB_Garamond/static/EBGaramond-Bold.ttf
      bold_italic: EB_Garamond/static/EBGaramond-BoldItalic.ttf

page:
  size: A5
  margin: [ 8mm, 8mm, 12mm, 8mm ]

base:
  font-family: EBGaramond

heading:
  font-family: Helvetica
Olaf
  • 3,786
  • 4
  • 25
  • 38

1 Answers1

0
tasks.asciidoctorPdf {
    setFontsDirs(listOf("fonts"))
}

I suspect that by calling setFontsDirs, the search path for fonts is completely reset and thus the path to the bundled fonts is also lost.

This is correct, compare the implementation 1:

    void setFontsDirs(Iterable<Object> paths) {
        this.fontDirs.clear()
        this.fontDirs.addAll(paths)
    }

How can I pass the path to my own font directory to AsciidoctorPdf without losing the path to the bundled fonts?

Two forms, the first is cheating and the second an educated guess.

  • cheating 1st: don't. use absolute paths to your fonts when you merge: true.

  • educated guess 2nd: (not working at time of answer) setFontsDirs(listOf("fonts", "GEM_FONTS_DIR")) where the GEM_FONTS_DIR is this token verbatim 2.

  • fallback 3rd: There is the other option to also list the default fonts in the YAML and use the GEM_FONTS_DIR prefix there, see Redeclaring the bundle fonts in a custom theme in Extending the font catalog 3.


So educated guess was not good enough, Olaf comments:

The second suggested solution did not work. GEM_FONTS_DIR is not recognized as a keyword, but as a relative path:

No such file or directory - notoserif-regular-subset.ttf not found in /play-books/fonts:/play-books/GEM_FONTS_DIR


  1. /groovy/org/asciidoctor/gradle/jvm/pdf/AsciidoctorPdfTask.groovy#L101-L111

  2. Configuring the font search path, specifically this:

    To include the location of the bundled fonts in the search, include the GEM_FONTS_DIR token in the list:

    $ asciidoctor-pdf -a pdf-theme=basic-theme.yml -a pdf-fontsdir="path/to/fonts;GEM_FONTS_DIR" document.adoc
    
  3. Extending the font catalog

hakre
  • 193,403
  • 52
  • 435
  • 836
  • The second suggested solution did not work. GEM_FONTS_DIR is not recognized as a keyword, but as a relative path: No such file or directory - notoserif-regular-subset.ttf not found in /play-books/fonts:/play-books/GEM_FONTS_DIR – Olaf Apr 21 '23 at 16:24
  • Regarding the first proposal, absoulte paths are not a solution because the project will be built in different environments. – Olaf Apr 21 '23 at 16:26
  • Maybe you can gain it from the environment as the parameter (variable) named `GEM_FONTS_DIR`? It looks a bit like this could be ... and if not, perhaps look it up and provide as absolute path. – hakre Apr 21 '23 at 16:28
  • And btw. is it really a relative path you use there `"fonts"`? I can't precisely say why, but I wouldn't do that, perhaps again only for trouble-shooting reasons. And when everything work, I'd build back then. – hakre Apr 21 '23 at 16:33
  • 1
    @Olaf: There is the other option to also list the default fonts in the YAML and use the GEM_FONTS_DIR prefix there, see _Redeclaring the bundle fonts in a custom theme_ in _Extending the font catalog_. It is perhaps a bit awkward, but maybe better than creating symbolic links when setting up the environments (which could also work). YMMV. – hakre Apr 21 '23 at 16:43
  • Your suggestion works. I have repeated the font declarations of the default theme. Doesn't feel really good though. But it works and that counts. – Olaf Apr 21 '23 at 17:12
  • @Olaf: I've added it as the third option to the answer. It is perhaps worth to ask for GEM_FONTS_DIR support for the gradle plugin. – hakre Apr 21 '23 at 17:25