使用 itext 调整 OpenType 字体字距

发布于 2024-12-11 16:22:28 字数 1131 浏览 0 评论 0原文

我正在使用 itext 和 ColdFusion (java) 将文本字符串写入 PDF 文档。我有需要使用的 trueType 和 openType 字体。 Truetype 字体似乎工作正常,但字距调整未用于任何以 .otf 结尾的字体文件。下面的代码在 Airstream (OpenType) 中写入“文本的第 1 行”,但缺少“T”和“e”之间的字距调整。当在其他程序中使用相同的字体时,它会进行字距调整。我也下载了较新版本的itext,但字距调整仍然不起作用。有谁知道如何在 itext 中使用 otf 字体进行字距调整?

<cfscript>
pdfContentByte = createObject("java","com.lowagie.text.pdf.PdfContentByte");
BaseFont= createObject("java","com.lowagie.text.pdf.BaseFont");
bf = BaseFont.createFont("c:\windows\fonts\AirstreamITCStd.otf", "" , BaseFont.EMBEDDED);
document = createobject("java","com.lowagie.text.Document").init();
fileOutput = createObject("java","java.io.FileOutputStream").init("c:\inetpub\test.pdf");
writer = createobject("java","com.lowagie.text.pdf.PdfWriter").getInstance(document,fileOutput);
document.open();    
cb = writer.getDirectContent(); 
cb.beginText();
cb.setFontAndSize(bf, 72);
cb.showTextAlignedKerned(PdfContentByte.ALIGN_LEFT,"Line 1 of Text",0,72,0);
cb.endText();
document.close();

bf.hasKernPairs(); //returns NO
bf.getClass().getName(); //returns "com.lowagie.text.pdf.TrueTypeFont"
</cfscript>

I am using itext and ColdFusion (java) to write text strings to a PDF document. I have both trueType and openType fonts that I need to use. Truetype fonts seem to be working correctly, but the kerning is not being used for any font file ending in .otf. The code below writes "Line 1 of Text" in Airstream (OpenType) but the kerning between "T" and "e" is missing. When the same font is used in other programs, it has kerning. I downloaded a newer version of itext also, but the kerning still did not work. Does anyone know how to get kerning to work with otf fonts in itext?

<cfscript>
pdfContentByte = createObject("java","com.lowagie.text.pdf.PdfContentByte");
BaseFont= createObject("java","com.lowagie.text.pdf.BaseFont");
bf = BaseFont.createFont("c:\windows\fonts\AirstreamITCStd.otf", "" , BaseFont.EMBEDDED);
document = createobject("java","com.lowagie.text.Document").init();
fileOutput = createObject("java","java.io.FileOutputStream").init("c:\inetpub\test.pdf");
writer = createobject("java","com.lowagie.text.pdf.PdfWriter").getInstance(document,fileOutput);
document.open();    
cb = writer.getDirectContent(); 
cb.beginText();
cb.setFontAndSize(bf, 72);
cb.showTextAlignedKerned(PdfContentByte.ALIGN_LEFT,"Line 1 of Text",0,72,0);
cb.endText();
document.close();

bf.hasKernPairs(); //returns NO
bf.getClass().getName(); //returns "com.lowagie.text.pdf.TrueTypeFont"
</cfscript>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独行侠 2024-12-18 16:22:28

根据所谓的规范:http://www.microsoft.com/typography/otspec/kern。嗯
“kern”表不支持包含 CFF 轮廓的 OpenType™ 字体,必须使用“GPOS”OpenType 布局表。

我检查了源代码,IText 实现仅检查 truetype 字体的字距,根本不读取 GPOS 表,因此内部字距必须为空,并且 hasKernPairs 必须返回 false。

所以,有两种方法可以解决:

  • 摆脱你使用的 otf:)
  • 通过读取 GPosition 表来修补 truetypefont
  • 等我,我正在处理 cff 内容,但 PDF 是我的可选:) 但不是排除这个可能性:)

according the socalled spec: http://www.microsoft.com/typography/otspec/kern.htm
OpenType™ fonts containing CFF outlines are not supported by the 'kern' table and must use the 'GPOS' OpenType Layout table.

I checked out the source, IText implementation only check the kern for truetype font, not read GPOS table at all, so the internal kernings must be empty, and the hasKernPairs must return false.

So, there have 2 way to solove:

  • get rid of the otf you used:)
  • patch the truetypefont by reading the GPosition table
  • wait for me, I'm processing the cff content, but PDF is optional of ever of my:) but not exclude the possibility:)
柠檬色的秋千 2024-12-18 16:22:28

Have a look at this thread about How to use Open Type Fonts in Java.
Here is stated that otf is not supported by java (not even with iText). Otf support depends on sdk version and OS.

Alternatively you could use FontForge which converts otf to ttf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文