这个问题是关于在Java中恢复字形字体信息,它与此处发布的问题有关。有关更多详细信息,请查看问题和答案。
那里建议使用Apache FOP库直接从 Truetype 文件中恢复字距调整对,因为 Java 不提供此信息。然后我将库移植到 Windows 并使用以下代码恢复字距调整对:
TTFFile file;
File ttf = new File("C:\\Windows\\Fonts\\calibri.ttf" );
try { file = TTFFile.open(ttf); }
catch (IOException e) {e.printStackTrace(); }
Map<Integer, Map<Integer, Integer>> kerning = file.getKerning();
最后,该库可以工作,但返回的字距调整对不适用于使用下面的函数在Path2D.Float中检索到的字形以及紧随其后显示的代码片段:
void vectorize(Path2D.Float path, String s) {
PathIterator pIter;
FontRenderContext frc = new FontRenderContext(null,true,true);
GlyphVector gv;
Shape glyph;
gv = font.createGlyphVector(frc, s);
glyph = gv.getGlyphOutline(0);
pIter = glyph.getPathIterator(null);
while (!pIter.isDone()) {
switch(pIter.currentSegment(points)) {
case PathIterator.SEG_MOVETO:
path.moveTo(points[0], points[1]);
break;
case PathIterator.SEG_LINETO :
path.lineTo(points[0], points[1]);
break;
case PathIterator.SEG_QUADTO :
path.quadTo(points[0], points[1], points[2], points[3]);
break;
case PathIterator.SEG_CUBICTO :
path.curveTo(points[0], points[1], points[2], points[3], points[4], points[5]);
break;
case PathIterator.SEG_CLOSE :
path.closePath();
}
pIter.next();
}
}
字形长度被检索到阵列lens中:
Font font = new Font("Calibri", Font.PLAIN, 1000);
double interchar = 1000. * 0.075;
int size = '}' - ' ' + 1;
Path2D.Float[] glyphs = new Path2D.Float[size];
double[] lens = new double[size];
String chars[] = new String[size];
int i; char c;
char[] s = { '0' };
for (i = 0, c = ' '; c <= '}'; c++, i++) { s[0] = c; chars[i] = new String(s); }
for (i = 0; i < size; i++) {
vectorize(glyphs[i] = new Path2D.Float(), chars[i]); // function shown above
lens[i] = glyphs[i].getBounds2D().getWidth() + interchar;
}
为了清楚起见,我使用 Graphics2D 的填充显示字形,并使用上面添加到 Apache FOP 库返回的字距偏移的长度进行翻译,但结果很糟糕。字体大小为标准 1000,如该讨论中所建议的那样,而interchar结果为 75。所有这些似乎都是正确的,但我的手动字距调整对看起来比使用 TTF 文件中的字距调整对好得多。
在这个库或 Truetype 字体中是否有知识渊博的人能够告诉我们应该如何使用这些字距调整对?
是否有必要直接从 TTF 文件访问字形,而不是如上所示使用 Java 字体管理?如果是,如何?