我目前正在画布上绘制文本,同时使用外部(非标准)字体,从TTF文件加载。我想要为我所显示的文本启用kerning。
我想知道的是,是否有可能使用Android从字体中读取kerning对。
发布于 2016-02-16 03:50:06
我想知道的是,是否有可能使用Android从字体中读取kerning对。
没有公共API可以从TTF文件中读取kerning对。但是,我从Apache FOP中提取了相关的代码,您可以使用这个图书馆读取内核对。
示例用法:
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"我想要为我所显示的文本启用kerning。
见:
浮子(SetLetterSpacing)
发布于 2020-11-09 19:51:06
我愿意在Windows上使用标准的上述解析器。如果有人想这样做,就需要使用矩形而不是Rect。这只是一个小转变。我还删除了目录jaredrummler,因为它太长了(不过,我在文件开头保留了版权注释)。但是在这个解析器中有两个TTFFile类。此代码:
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();只有当您导入正确的类文件时才有效:
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();
}
}透镜在以下代码中恢复的长度:
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?
https://stackoverflow.com/questions/35010436
复制相似问题