我必须使用包含字距调整信息的给定字体来计算给定文本的水平宽度。
字形名称和宽度以及他的字距对信息应该可以通过文本字符的 unicode 值访问,如下所示:
// GLYPHS[ unicode ] = [ "name", width ];
// KERNS [ unicode ] = { "nextGlyphName" : horizontalAdjustment };
// = { GLYPHS[ unicode ][ 0 ] : horizontalAdjustment };
因此,如果您有一个如下所示的文件,其中包含这些数据对象,您可以将其复制并粘贴到您的源代码中,或者在运行时打开并导入它们(每个字体非常长的文件的第一页):
// Family Name
// 'Times New Roman'
// EM size
// '2048'
// is_quadratic
// '1'
//
// GLYPHS[ unicode ] = [ "name", width ];
// KERNS [ unicode ] = { "nextGlyphName" : horizontalAdjustment };
// = { GLYPHS[ unicode ][ 0 ] : horizontalAdjustment };
GLYPHS = {};
KERNS = {};
GLYPHS[ 32 ] = [ "space", 512 ];
KERNS [ 32 ] = {
"Upsilondieresis" : -76,
"Upsilon" : -76,
"Tau" : -37,
"Lambda" : -113,
"Delta" : -113,
"Alpha" : -113,
"Alphatonos" : -113,
"Y" : -76,
"W" : -37,
"V" : -37,
"T" : -37,
"A" : -113
};
GLYPHS[ 33 ] = [ "exclam", 682 ];
GLYPHS[ 34 ] = [ "quotedbl", 836 ];
GLYPHS[ 35 ] = [ "numbersign", 1024 ];
GLYPHS[ 36 ] = [ "dollar", 1024 ];
GLYPHS[ 37 ] = [ "percent", 1706 ];
GLYPHS[ 38 ] = [ "ampersand", 1593 ];
GLYPHS[ 39 ] = [ "quotesingle", 369 ];
GLYPHS[ 40 ] = [ "parenleft", 682 ];
GLYPHS[ 41 ] = [ "parenright", 682 ];
GLYPHS[ 42 ] = [ "asterisk", 1024 ];
GLYPHS[ 43 ] = [ "plus", 1155 ];
GLYPHS[ 44 ] = [ "comma", 512 ];
GLYPHS[ 45 ] = [ "hyphen", 682 ];
GLYPHS[ 46 ] = [ "period", 512 ];
GLYPHS[ 47 ] = [ "slash", 569 ];
GLYPHS[ 48 ] = [ "zero", 1024 ];
GLYPHS[ 49 ] = [ "one", 1024 ];
KERNS [ 49 ] = {
"one" : -76
};
GLYPHS[ 50 ] = [ "two", 1024 ];
GLYPHS[ 51 ] = [ "three", 1024 ];
GLYPHS[ 52 ] = [ "four", 1024 ];
GLYPHS[ 53 ] = [ "five", 1024 ];
GLYPHS[ 54 ] = [ "six", 1024 ];
GLYPHS[ 55 ] = [ "seven", 1024 ];
GLYPHS[ 56 ] = [ "eight", 1024 ];
GLYPHS[ 57 ] = [ "nine", 1024 ];
GLYPHS[ 58 ] = [ "colon", 569 ];
GLYPHS[ 59 ] = [ "semicolon", 569 ];
GLYPHS[ 60 ] = [ "less", 1155 ];
GLYPHS[ 61 ] = [ "equal", 1155 ];
GLYPHS[ 62 ] = [ "greater", 1155 ];
GLYPHS[ 63 ] = [ "question", 909 ];
GLYPHS[ 64 ] = [ "at", 1886 ];
GLYPHS[ 65 ] = [ "A", 1479 ];
KERNS [ 65 ] = {
"quoteright" : -227,
"y" : -188,
"w" : -188,
"v" : -152,
"Y" : -188,
"W" : -164,
"V" : -264,
"T" : -227,
"space" : -113
};
GLYPHS[ 66 ] = [ "B", 1366 ];
GLYPHS[ 67 ] = [ "C", 1366 ];
但请不要忘记检查特殊 KERNS 对象的存在,然后再尝试像这样访问它:
if ( !( KERNS[ glUnicode ] == undefined ) ) {
if ( !( KERNS[ glUnicode ][ GLYPHS[ nextGlyphUnicode ][ 0 ] ] == undefined ) ) {
...
然后你可以像这样访问它:
kernWidth = KERNS[ glUnicode ][ GLYPHS[ nextGlyphUnicode ][ 0 ] ];
如果您有以下“Text”作为示例,“T”(glUnicode == 84)的 kernWidth,后跟“e”(nextGlyphUnicode == 101)将为您提供以下结果:
kernWidth == -143
您可以使用 GLYPHS 和 KERNS 数据对象创建上述文件,并使用以下脚本在fontforge
“嵌入式”python 2.7 解释器中运行良好。这个脚本是为windows机器设计的,所以你必须首先调整你的路径!
# This files name is 'Kernings.py'
# run these two commands in the fontforge "embedded" python interpreter (ffpython.exe)
# >>> script = open( "Scripts\\Kernings.py", "r" )
# >>> exec script
import fontforge
fontFilenames = [
"arial.ttf",
"arialbd.ttf",
"ariali.ttf",
"arialbi.ttf",
"ARIALN.TTF",
"ARIALNB.TTF",
"ARIALNI.TTF",
"ARIALNBI.TTF",
"calibri.ttf",
"calibrib.ttf",
"calibrii.ttf",
"calibriz.ttf",
"cambria.ttc",
"cambriab.ttf",
"cambriai.ttf",
"cambriaz.ttf",
"times.ttf",
"timesbd.ttf",
"timesi.ttf",
"timesbi.ttf",
"verdana.ttf",
"verdanab.ttf",
"verdanai.ttf",
"verdanaz.ttf"
]
for actFontFile in fontFilenames :
print( "c:\\windows\\fonts\\" + actFontFile )
out = open( "Scripts\\Kern_" + actFontFile[ : len( actFontFile ) - 4 ] + "_json.txt", "w" )
font = fontforge.open( "c:\\windows\\fonts\\" + actFontFile )
out.write(
"// Family Name\n// '" + font.familyname + "'\n"
+ "// EM size\n// '" + str( font.em ) + "'\n"
+ "// is_quadratic\n// '" + str( font.is_quadratic ) + "'\n"
+ "//\n"
+ '// GLYPHS[ unicode ] = [ "name", width ];\n'
+ '// KERNS [ unicode ] = { "nextGlyphName" : horizontalAdjustment };\n'
+ "// = { GLYPHS[ unicode ][ 0 ] : horizontalAdjustment };\n"
+ "GLYPHS = {};\n"
+ "KERNS = {};\n\n"
)
glyphIdIterator = font.__iter__()
for glyphName in glyphIdIterator :
if font[ glyphName ].unicode >=0 :
kerningStrings = []
outstring = ( "GLYPHS[ "
+ str( font[ glyphName ].unicode ).rjust( 5 ) + " ] = [ \""
+ glyphName + "\","
+ str( font[ glyphName ].width ).rjust( 5 ) + " ];\n"
)
subs = font[ glyphName ].getPosSub("*")
if len( subs ):
for sub in subs:
if len( sub ):
for subsub in sub:
if str( subsub ).lower().find( "'kern'" ) >=0:
kerningStrings.append(
(" \"" + str( sub[ 2 ] ) + "\"").ljust( 20 )
+ ":" + str( sub[ 5 ] ).rjust( 6 )
)
break
if ( len( kerningStrings ) ) :
outstring = outstring + ( "KERNS [ "
+ str( font[ glyphName ].unicode ).rjust( 5 ) + " ] = {" )
for kerningString in kerningStrings :
outstring = outstring + "\n" + kerningString + ","
outstring = outstring.rstrip( "," )
outstring = outstring + "\n};\n"
out.write( outstring )
out.close()
font.close()
#
# file 'Kernings.py' ends here
我希望,这会有所帮助。非常感谢您的关注,
理查德