简体   繁体   English

用Java渲染字体:java.awt.Font行为不正确

[英]Rendering fonts in Java: java.awt.Font doesn't behave correctly

What I've encountered so far is what I can think of a "bug" in Oracle's font rendering libraries in Java. 到目前为止,我能想到的是Oracle Java字体渲染库中的“错误”。 In certain fonts (like fonts embedded in PDF files), some irregular character codes are used and they have glyphs associated to them. 在某些字体(如嵌入PDF文件中的字体)中,使用了一些不规则的字符代码,并且它们具有与它们相关的字形。

When trying to get glyph vector using java.awt.Font.createGlyphVector method in such cases (character codes 9, 10 and 13 which belong to tab, carriage return and new line) the correct glyph is not returned, but it works for everything else. 在这种情况下(使用属于制表符,回车符和换行符的字符代码9、10和13)尝试使用java.awt.Font.createGlyphVector方法获取字形向量时,不会返回正确的字形,但它适用于其他所有情况。

I tracked the issue to the CMap implementation in Java, that has a code similar to this: 我将问题跟踪到Java中的CMap实现,该实现具有类似于以下代码:

         char getGlyph(int charCode) {
             if (charCode < 256) {
                 if (charCode < 0x0010) {
                     switch (charCode) {
                     case 0x0009:
                     case 0x000a:
                     case 0x000d: return CharToGlyphMapper.INVISIBLE_GLYPH_ID;
                     }
                 }
                 return (char)(0xff & cmap[charCode]);
             } else {
                 return 0;
             }
         }

So, my question is: 所以,我的问题是:

  • IS this actually a bug? 这实际上是一个错误吗?
  • Does anyone know how to work around this? 有谁知道如何解决这个问题?
  • Is there a way to extract CMAP data from true type fonts, so that I can map characters (code points) to glyph codes? 有没有办法从真正的字体中提取CMAP数据,以便我可以将字符(代码点)映射到字形代码?

I'm sure you've figured out the problem by now, but for those who stumble upon this page in the future, I would recommend the sfntly library . 我敢肯定您现在已经解决了问题,但是对于那些将来偶然发现此页面的人,我建议使用sfntly库 It is a free java library by google that allows you to edit sfnt based fonts that is very easy to work with. 它是Google提供的免费Java库,可让您编辑非常容易使用的基于sfnt的字体。 If you solved your problem another way, please enlighten us with an update. 如果您以其他方式解决了问题,请通过更新来启发我们。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM