我可以使用PIL获取两个字符的字距值吗?
有没有办法用PIL这个库获取字体中两个字符之间的字距值?
3 个回答
0
有一个叫做kern-dump的工具,它可以提取字体文件中所有字形对的间距值。
4
答案是,正如Ignacio所说,不可以。
这部分是比较严肃的回答。接下来,我们来点完全不同的内容:
你可以用一些方法来大致估算(不是那个,因为仅仅使用PIL你无法知道字体的设计em大小)的字距调整值,像这样:
import ImageFont
SAMPLE_SIZE= 128 # should provide sufficient resolution for kerning values
def get_a_kerning_value(font_descriptor, char1, char2):
"""See if there is some kerning value defined for a pair of characters
Return the kerning value as an approximated percentage of the row height."""
imagefont= ImageFont.truetype(font_descriptor, SAMPLE_SIZE)
width1= imagefont.getsize(char1)[0]
width2= imagefont.getsize(char2)[0]
widths, height= imagefont.getsize(char1 + char2)
return (widths - (width1 + width2))/float(height)
2
不,PIL只能用来显示文字,而不能获取文字的度量信息。你需要使用合适的Windows API函数,或者使用FreeType库(不过目前没有稳定的Python接口)来获取字距信息。