访问字体,字距的Java2D信息
题
小背景..
这一切运作良好,除了一件事:缺少字距。诚然,这是没有必要有作为文本看起来很好,因为它是,但它会提高质量,如果我有访问字体字距信息。
而且问题是..
时有可能获得使用普通的Java字距信息的方式,将在Windows,Linux和MacOS X是便携式?回来时,我写了我简单看了看身边的TextRenderer
,但无法找到这样的方式..
一种可能的解决方案
如果没有在纯Java这样的方式,我想用写一个单独的工具 的Freetype 即可。作为其特征页中列出:
FreeType 2的提供的信息 往往是无法从其他 相似的字体引擎,例如字距 距离下,字形名称,垂直 度量等
该工具会共同字符字距对存储到我的文字渲染器加载和使用的文件。所以这可能是什么,如果你们不拿出一个更好的选择,我会做的。 :)
解决方案
我知道的是,只有图书馆阅读字距调整信息“somwhat”正确的 iText的和从Apache防。
http://www.1t3xt.info/api /com/lowagie/text/pdf/BaseFont.html HTTP:/ /svn.apache.org/viewvc/xmlgraphics/fop/tags/fop-0_95/src/java/org/apache/fop/fonts/ (一的svn链接,因为似乎没有网上API)
其他提示
与Java SE 6开始,Java可以提供字距信息,该字体提供它。这是默认关闭,并且可以导通这样的:
Map<TextAttribute, Object> textAttributes = new HashMap<TextAttribute, Object>();
textAttributes.put(TextAttribute.FAMILY, "Arial");
textAttributes.put(TextAttribute.SIZE, 25f);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
这个论坛线程包含关于这一主题的更详细的讨论:
这是我在寻找字距信息和我的javascript中的离线提供它的方式,我在这里读了这个问题,也和因为没有答案,我搜索进一步上,并在年底,我得到这样的:
其可通过该字形的Unicode来索引两个JavaScript对象:
GLYPHS = {};
KERNS = {};
和它们设置是这样的:
// GLYPHS[ unicode ] = [ "name", width ];
// KERNS [ unicode ] = { "nextGlyphName" : horizontalAdjustment };
// = { GLYPHS[ unicode ][ 0 ] : horizontalAdjustment };
所以,如果我有我的“文本”的字符串,我通过它去逐个字符,用他的统一访问字形名和宽度是这样的:
glUnicode = "Text".charCodeAt( strIndex ); // "T" == 84
glName = GLYPHS[ glUnicode ][ 0 ];
glWidth = GLYPHS[ glUnicode ][ 1 ];
要访问字距调整值,我们来看看在下一字符的Unicode值,其是这样的:
nextGlyphUnicode = "Text".charCodeAt( strIndex + 1 ); // "e" == 101
和如果下列对象存在,那么该语句会给你字距宽度(你必须首先检查是否存在这样的:
if ( !( KERNS[ glUnicode ] == undefined ) ) {
if ( !( KERNS[ glUnicode ][ GLYPHS[ nextGlyphUnicode ][ 0 ] ] == undefined ) ) {
...
):
kernWidth = KERNS[ glUnicode ][ GLYPHS[ nextGlyphUnicode ][ 0 ] ];
在这个例子中,对于kernWidth将 “e” 之后的 “T” 将是
kernWidth == -143
我想,这就是你要找的,对不对? 通过字符的Unicode值和以下字符的Unicode值访问的所有信息。 非常简单和非常好的。
因此,我创建的文件为每个字体和所述第一页面看起来像这样:
// 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 ];
您可以复制的内容中的每个文件,你需要到您的源代码中或在运行时读取它拥有的对象availlable。
和文件可以与运行fontforge
的“嵌入式”的Python 2.7解释器中细下面的脚本来创建。
这个脚本是专为Windows机器,所以你必须先适应你的路!
#
# 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
krnStrLen = len( kerningStrings )
if ( krnStrLen ) :
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()
我希望这可以帮助。 非常感谢您对我们的关注,
理查德