如何在 Apache FOP 中自动创建斜体/粗体字体?

Sea*_*ene 2 java pdf fonts apache-fop

假设我正在使用 Apache FOP 创建 PDF 文件。PDF 中的所有字符均应为Times New Roman Italic. 由于我的系统中没有这种字体,Apache FOP将尝试在/data/fonts中查找该字体,该字体由fop.xconf设置。

<directory>/data/fonts</directory>
Run Code Online (Sandbox Code Playgroud)

但是,/data/fonts 中只有一个文件,即Times New Roman Regular.ttf. 是的,不Times New Roman Italic.ttf,不Times New Roman Bold.ttf

ls /data/fonts 
'Times New Roman Regular.ttf'
Run Code Online (Sandbox Code Playgroud)

在这种情况下,Apache FOP 报告它找不到Times New Roman Italic字体,并且它回退到默认字体,该字体也不Times New Roman是斜体。

我可以通过放入 /data/fonts 来解决问题Times New Roman Italic.ttf,但是如果我没有该字体的特定斜体/粗体版本怎么办?据我所知,有些字体根本没有特定的粗体/斜体版本。Apache FOP 是否可以自动从常规字体创建斜体/粗体样式,即使结果不如特定的斜体/粗体字体?

lfu*_*ini 5

如果您使用的是 FOP 2.2 版,则有一个配置选项可以自动创建模拟斜体和粗体字形:simulate-style

style此示例将和 的四种组合映射weight相同的TrueType 字体,并在需要时对字形进行粗体化和倾斜:

<font kerning="yes" embed-url="Times.ttf" simulate-style="true">
  <font-triplet name="Times" style="normal" weight="normal"/>
  <font-triplet name="Times" style="italic" weight="normal"/>
  <font-triplet name="Times" style="normal" weight="bold"/>
  <font-triplet name="Times" style="italic" weight="bold"/>
</font>
Run Code Online (Sandbox Code Playgroud)

请注意,注册所有不同的font-triplet元素很重要;如果只有<font-triplet name="Times" style="normal" weight="normal"/>字体替换机制,将首先将粗体、斜体和粗斜体映射到注册的普通变体,然后FOP将模拟样式(什么也不做)。