首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Android中的TTF文件中读取kerning对表

如何从Android中的TTF文件中读取kerning对表
EN

Stack Overflow用户
提问于 2016-01-26 09:19:57
回答 2查看 2.2K关注 0票数 8

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

我想知道的是,是否有可能使用Android从字体中读取kerning对。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-16 03:50:06

我想知道的是,是否有可能使用Android从字体中读取kerning对。

没有公共API可以从TTF文件中读取kerning对。但是,我从Apache FOP中提取了相关的代码,您可以使用这个图书馆读取内核对。

示例用法:

代码语言:javascript
复制
TTFFile file = TTFFile.open(getAssets().open("fonts/font.ttf"));
Map<Integer, Map<Integer, Integer>> kerning = file.getKerning();

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

代码语言:javascript
复制
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"

我想要为我所显示的文本启用kerning。

见:

如何在Android TextView中调整文本角化?

浮子(SetLetterSpacing)

票数 9
EN

Stack Overflow用户

发布于 2020-11-09 19:51:06

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

代码语言:javascript
复制
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();

只有当您导入正确的类文件时才有效:

代码语言:javascript
复制
import com.fontreader.truetype.TTFFile;

最后,代码可以工作,但是返回的不能使用您使用以下方法转换的路径工作:

代码语言:javascript
复制
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();
    } 
}

透镜在以下代码中恢复的长度:

代码语言:javascript
复制
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;
}

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

,有谁受过这个库的训练,能告诉我们应该如何使用这些kerning对吗?

抱歉,稍微偏离了原来的问题,但这可能会完成信息,因为一旦一个人阅读了角化对,如何正确地使用他们在Windows或Android?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35010436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档