ImageFont 检测丢失的字形(Python Pillow)
ImageFont detect missing glyph (Python Pillow)
这是一个简短的example。
from PIL import ImageFont, ImageDraw
draw = ImageDraw.Draw(image)
# use a bitmap font
font = ImageFont.load("arial.pil")
draw.text((10, 10), "hello", font=font)
# use a truetype font
font = ImageFont.truetype("arial.ttf", 15)
draw.text((10, 25), "world", font=font)
I want to know if the font is missing any glyphs from the rendered text.
当我尝试渲染一个缺失的字形时,我得到一个空方块。
draw.text((10, 10), chr(1234), font=font)
- 如何以编程方式确定缺失的字形?
- 如何在 ttf 文件中列出可用的字形?
两道题几乎一模一样
I would prefer using Pillow to determine what I want.
Other modules from PyPI are welcome as well.
有'fontTools'包,其中包含一个python class 用于读取和查询TrueType 字体。这样的事情是可能的
from fontTools.ttLib import TTFont
f = TTFont('/path/to/font/arial.ttf')
print(f.getGlyphOrder()) # a list of the glyphs in the order
# they appear
print(f.getReversedGlyphMap() # mapping from glyph names to Id
id = f.getGlyphID('Euro') # The internal code for the Euro character,
# Raises attribute error if the character
# isn't present.
从字符到字形的映射通常很复杂,并且在字体的 cmap table 中定义。这是一个二进制部分,但可以用
检查
f.getTableData('cmap')
一个字体可以有多个cmap table。 freetype interface也可以读取ttf文件。可以使用 freetype 尝试渲染字符,并查看结果:这会很慢。
import freetype as ft
face = ft.Face('arial.ttf')
face.set_size(25*32)
face.load_char(‽)
bitmap = face.glyph.bitmap.buffer
if bitmap == [255, 255, 255, 255, 0, 255, 255, 0, 255, 255, 0, 255, 255, 0, 255, 255, 0, 255, 255, 255, 255]:
print("No interobang in the font")
这是一个简短的example。
from PIL import ImageFont, ImageDraw
draw = ImageDraw.Draw(image)
# use a bitmap font
font = ImageFont.load("arial.pil")
draw.text((10, 10), "hello", font=font)
# use a truetype font
font = ImageFont.truetype("arial.ttf", 15)
draw.text((10, 25), "world", font=font)
I want to know if the font is missing any glyphs from the rendered text.
当我尝试渲染一个缺失的字形时,我得到一个空方块。
draw.text((10, 10), chr(1234), font=font)
- 如何以编程方式确定缺失的字形?
- 如何在 ttf 文件中列出可用的字形?
两道题几乎一模一样
I would prefer using Pillow to determine what I want. Other modules from PyPI are welcome as well.
有'fontTools'包,其中包含一个python class 用于读取和查询TrueType 字体。这样的事情是可能的
from fontTools.ttLib import TTFont
f = TTFont('/path/to/font/arial.ttf')
print(f.getGlyphOrder()) # a list of the glyphs in the order
# they appear
print(f.getReversedGlyphMap() # mapping from glyph names to Id
id = f.getGlyphID('Euro') # The internal code for the Euro character,
# Raises attribute error if the character
# isn't present.
从字符到字形的映射通常很复杂,并且在字体的 cmap table 中定义。这是一个二进制部分,但可以用
检查f.getTableData('cmap')
一个字体可以有多个cmap table。 freetype interface也可以读取ttf文件。可以使用 freetype 尝试渲染字符,并查看结果:这会很慢。
import freetype as ft
face = ft.Face('arial.ttf')
face.set_size(25*32)
face.load_char(‽)
bitmap = face.glyph.bitmap.buffer
if bitmap == [255, 255, 255, 255, 0, 255, 255, 0, 255, 255, 0, 255, 255, 0, 255, 255, 0, 255, 255, 255, 255]:
print("No interobang in the font")