如何从 Android 中的 TTF 文件读取字距调整对 table

How to read kerning pairs table from TTF file in Android

我目前正在 Canvas 上绘制文本,同时使用从 TTF 文件加载的外部(非标准)字体。我想为我显示的文本启用字距调整。

我想知道是否有可能使用 Android API.

从字体中读取字距调整对

What I want to know is if there is a possibility to read kerning pairs from typeface using Android API.

没有 public API 可以从 TTF 文件中读取字距调整对。但是,我从 Apache FOP and you can read the kerning pairs using this library.

中提取了相关代码

用法示例:

TTFFile file = TTFFile.open(getAssets().open("fonts/font.ttf"));
Map<Integer, Map<Integer, Integer>> kerning = file.getKerning();

您还可以检索其他元数据。示例:

TTFFile ttfFile = TTFFile.open(new File("/system/fonts/Roboto-Regular.ttf"));

String name = ttfFile.getFullName();             // "Roboto Regular"
String family = ttfFile.getSubFamilyName();      // "Regular"
int fontWeight = ttfFile.getWeightClass();       // 400
String copyright = ttfFile.getCopyrightNotice(); // "Font data copyright Google 2014"

I want to enable kerning for the text I am displaying.

参见:

How to adjust text kerning in Android TextView?

setLetterSpacing(float)

我愿意在 Windows 上使用标准 Java 使用 the parser described above。如果有人想这样做,需要使用 Rectangle 而不是 Rect。这只是一个小的转换。我还删除了目录 jaredrummler 因为它有点太长了(不过我在文件的开头保留了版权注释)。但是在这个解析器中有两个 TTFFile classes。此代码:

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();

只有导入正确的 class 文件才有效:

import com.fontreader.truetype.TTFFile;

最后,代码可以运行,但返回的字距调整对不起作用,您使用以下路径转换:

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 恢复的长度:

double interchar = fontsize * 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], tx[i], 0f);
    lens[i] = glyphs[i].getBounds2D().getWidth() + interchar;
}

为了清楚起见,我在 Graphics2D 中使用 fill 显示字形,并使用上面的长度进行翻译,并按照上面的建议添加到库 Apache FOP 返回的字距调整位移中,但结果很糟糕。 fontsize 是本次讨论中建议的标准 1000,乘以字体大小后 interchar 结果为 75。所有这一切似乎都是正确的,但我的手动字距调整看起来比使用 ttf 文件中的字距调整要好得多。

有没有受过这个库培训的人能够告诉我们应该如何使用这些字距调整对?

很抱歉稍微偏离了原来的问题,但这可能会使信息完整,因为一旦阅读了字距调整对,如何在 Windows 或 Android 上正确使用它们?