如何使用Python获取1200英寸的truetype字体字符的宽度?

twn*_*ale 3 python truetype

我可以使用PIL获得字符的高度和宽度(以像素为单位)(请参见下文),但是(除非我没有记错)像素大小取决于屏幕的DPI,后者可能会有所不同。相反,我想做的是以绝对单位(例如英寸)或1200英寸(“单词完美单位”)为单位来计算字符的宽度。

>>> # Getting pixels width with PIL
>>> font = ImageFont.truetype('/blah/Fonts/times.ttf' , 12)
>>> font.getsize('a')
(5, 14)
Run Code Online (Sandbox Code Playgroud)

我想要这样做的原因是创建一个用于包装二进制Word Perfect文档的自动换行功能。Word Perfect要求在整个文本的有效点插入软换行代码,否则文件将损坏且无法打开。问题是在何处添加它们以用于可变宽度字体。

我意识到我不完全了解像素与屏幕分辨率和字体大小之间的关系。我要解决所有这些错误吗?

usr*_*301 5

原始文本宽度通常以印刷者的点为单位进行计算,但是由于出于字体定义目的而将点定义为1/72英寸,因此您可以轻松地将其转换为任何其他单位。

要获得字符的设计宽度(以em单位表示),您需要访问字体的低级数据。最简单的方法是to pip install fonttools,它可以在最低的字体定义级别上运行所有内容。

安装了fontTools后,您可以:

  1. 加载字体数据–这需要实际字体文件的路径;
  2. 字符宽度存储为字形宽度,这意味着您必须检索“字符到字形”映射;这是在cmap字体表中:

    一种。cmap为您的字体加载。最有用的是Unicode映射-字体可能包含其他字体。b。加载字体的字形集。这是该字体中字形的名称列表。

  3. 然后,对于每个Unicode字符,首先查找其名称,然后使用该名称以设计单位检索其宽度。

  4. 不要忘记,“设计单位”是基于字体的整体“设计宽度”。这可以是一个标准值1000(对于Type 1字体2048通常),(对于TrueType字体通常)或任何其他值。

这导致此功能:

from fontTools.ttLib import TTFont
from fontTools.ttLib.tables._c_m_a_p import CmapSubtable

font = TTFont('/Library/Fonts/Arial.ttf')
cmap = font['cmap']
t = cmap.getcmap(3,1).cmap
s = font.getGlyphSet()
units_per_em = font['head'].unitsPerEm

def getTextWidth(text,pointSize):
    total = 0
    for c in text:
        if ord(c) in t and t[ord(c)] in s:
            total += s[t[ord(c)]].width
        else:
            total += s['.notdef'].width
    total = total*float(pointSize)/units_per_em;
    return total

text = 'This is a test'

width = getTextWidth(text,12)

print ('Text: "%s"' % text)
print ('Width in points: %f' % width)
print ('Width in inches: %f' % (width/72))
print ('Width in cm: %f' % (width*2.54/72))
print ('Width in WP Units: %f' % (width*1200/72))
Run Code Online (Sandbox Code Playgroud)

结果是:

from fontTools.ttLib import TTFont
from fontTools.ttLib.tables._c_m_a_p import CmapSubtable

font = TTFont('/Library/Fonts/Arial.ttf')
cmap = font['cmap']
t = cmap.getcmap(3,1).cmap
s = font.getGlyphSet()
units_per_em = font['head'].unitsPerEm

def getTextWidth(text,pointSize):
    total = 0
    for c in text:
        if ord(c) in t and t[ord(c)] in s:
            total += s[t[ord(c)]].width
        else:
            total += s['.notdef'].width
    total = total*float(pointSize)/units_per_em;
    return total

text = 'This is a test'

width = getTextWidth(text,12)

print ('Text: "%s"' % text)
print ('Width in points: %f' % width)
print ('Width in inches: %f' % (width/72))
print ('Width in cm: %f' % (width*2.54/72))
print ('Width in WP Units: %f' % (width*1200/72))
Run Code Online (Sandbox Code Playgroud)

与Adobe InDesign的报告进行比较时是正确的。(请注意,此处不应用每个字符的字距调整!这将需要更多代码。)

字体中未定义的字符会被静默忽略,并且通常.notdef会使用字形的宽度。如果您希望将此报告为错误,请删除if该功能中的测试。

演员要float在功能上getTextWidth是如此的这个作品下的Python 2.7和3.5两种,但请注意,如果您使用Python 2.7和更大的价值Unicode字符(非纯ASCII),你需要的功能重写正确使用UTF8字符。