如何使用从TTF文件提取的字距调整对在Java中将字形正确显示为Path2D?

问题描述

该问题与在 Java 中恢复字形字体信息有关,它与question posted here.有关。有关更多详细信息,请检查问题和解答。

由于Java不提供此信息,因此建议使用Apache FOP库直接从Truetype文件中恢复字距对。然后,我将库移植到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[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中的 fill 显示字形,并按照建议的方式使用上述长度添加到Apache FOP库返回的字距置换中来翻译字形,但结果令人震惊。正如该讨论中所建议的,字体大小是标准的1000,而 interchar 结果是75。所有这些似乎都是正确的,但我的手动字距调整对看起来比使用字距调整好得多对从TTF文件。

该库或Truetype字体中是否有任何知识丰富的人士能够告诉我们我们应该如何使用这些字距调整对?

是否需要直接从TTF文件访问字形,而不是如上所述使用Java字体管理?如果是,请如何?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)