本文整理汇总了Python中fontTools.subset.Subsetter方法的典型用法代码示例。如果您正苦于以下问题:Python subset.Subsetter方法的具体用法?Python subset.Subsetter怎么用?Python subset.Subsetter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fontTools.subset
的用法示例。
在下文中一共展示了subset.Subsetter方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: subset_font_cmap
# 需要导入模块: from fontTools import subset [as 别名]
# 或者: from fontTools.subset import Subsetter [as 别名]
def subset_font_cmap(srcname, dstname, exclude=None, include=None, bump_version=True):
opt = _DEFAULT_OPTIONS
font = subset.load_font(srcname, opt)
target_charset = set(font_data.get_cmap(font).keys())
if include is not None:
target_charset &= include
if exclude is not None:
target_charset -= exclude
subsetter = subset.Subsetter(options=opt)
subsetter.populate(unicodes=target_charset)
subsetter.subset(font)
if bump_version:
# assume version string has 'uh' if unhinted, else hinted.
revision, version_string = swat_license.get_bumped_version(font)
font["head"].fontRevision = revision
font_data.set_name_record(font, _VERSION_ID, version_string)
subset.save_font(font, dstname, opt)
示例2: test_timing_publishes_parts
# 需要导入模块: from fontTools import subset [as 别名]
# 或者: from fontTools.subset import Subsetter [as 别名]
def test_timing_publishes_parts(self):
_, fontpath = self.compile_font(self.getpath("TestTTF-Regular.ttx"), ".ttf")
subsetpath = self.temp_path(".ttf")
options = subset.Options()
options.timing = True
subsetter = subset.Subsetter(options)
subsetter.populate(text='ABC')
font = TTFont(fontpath)
with CapturingLogHandler('fontTools.subset.timer', logging.DEBUG) as captor:
captor.logger.propagate = False
subsetter.subset(font)
logs = captor.records
captor.logger.propagate = True
self.assertTrue(len(logs) > 5)
self.assertEqual(len(logs), len([l for l in logs if 'msg' in l.args and 'time' in l.args]))
# Look for a few things we know should happen
self.assertTrue(filter(lambda l: l.args['msg'] == "load 'cmap'", logs))
self.assertTrue(filter(lambda l: l.args['msg'] == "subset 'cmap'", logs))
self.assertTrue(filter(lambda l: l.args['msg'] == "subset 'glyf'", logs))
示例3: decompress
# 需要导入模块: from fontTools import subset [as 别名]
# 或者: from fontTools.subset import Subsetter [as 别名]
def decompress(ttFont, **kwargs):
""" Use the FontTools Subsetter to desubroutinize the font's CFF table.
Any keyword arguments are passed on as options to the Subsetter.
Skip if the font contains no subroutines.
"""
if not has_subrs(ttFont):
log.debug('No subroutines found; skip decompress')
return
from fontTools import subset
# The FontTools subsetter modifies many tables by default; here
# we only want to desubroutinize, so we run the subsetter on a
# temporary copy and extract the resulting CFF table from it
make_temp = kwargs.pop('make_temp', True)
if make_temp:
from io import BytesIO
from fontTools.ttLib import TTFont, newTable
stream = BytesIO()
ttFont.save(stream, reorderTables=None)
stream.flush()
stream.seek(0)
tmpfont = TTFont(stream)
else:
tmpfont = ttFont # run subsetter on the original font
options = subset.Options(**kwargs)
options.desubroutinize = True
options.notdef_outline = True
subsetter = subset.Subsetter(options=options)
subsetter.populate(glyphs=tmpfont.getGlyphOrder())
subsetter.subset(tmpfont)
if make_temp:
# copy modified CFF table to original font
data = tmpfont['CFF '].compile(tmpfont)
table = newTable('CFF ')
table.decompile(data, ttFont)
ttFont['CFF '] = table
tmpfont.close()
示例4: closeGlyphsOverGSUB
# 需要导入模块: from fontTools import subset [as 别名]
# 或者: from fontTools.subset import Subsetter [as 别名]
def closeGlyphsOverGSUB(gsub, glyphs):
""" Use the FontTools subsetter to perform a closure over the GSUB table
given the initial `glyphs` (set of glyph names, str). Update the set
in-place adding all the glyph names that can be reached via GSUB
substitutions from this initial set.
"""
subsetter = subset.Subsetter()
subsetter.glyphs = glyphs
gsub.closure_glyphs(subsetter)
示例5: subset_font
# 需要导入模块: from fontTools import subset [as 别名]
# 或者: from fontTools.subset import Subsetter [as 别名]
def subset_font(source_file, target_file, include=None, exclude=None, options=None):
"""Subsets a font file.
Subsets a font file based on a specified character set. If only include is
specified, only characters from that set would be included in the output
font. If only exclude is specified, all characters except those in that
set will be included. If neither is specified, the character set will
remain the same, but inaccessible glyphs will be removed.
Args:
source_file: Input file name.
target_file: Output file name
include: The list of characters to include from the source font.
exclude: The list of characters to exclude from the source font.
options: A dictionary listing which options should be different from the
default.
Raises:
NotImplementedError: Both include and exclude were specified.
"""
opt = subset.Options()
opt.name_IDs = ["*"]
opt.name_legacy = True
opt.name_languages = ["*"]
opt.layout_features = ["*"]
opt.notdef_outline = True
opt.recalc_bounds = True
opt.recalc_timestamp = True
opt.canonical_order = True
opt.drop_tables = ["+TTFA"]
if options is not None:
for name, value in options.items():
setattr(opt, name, value)
if include is not None:
if exclude is not None:
raise NotImplementedError(
"Subset cannot include and exclude a set at the same time."
)
target_charset = include
else:
if exclude is None:
exclude = []
source_charset = coverage.character_set(source_file)
target_charset = source_charset - set(exclude)
font = subset.load_font(source_file, opt)
subsetter = subset.Subsetter(options=opt)
subsetter.populate(unicodes=target_charset)
subsetter.subset(font)
subset.save_font(font, target_file, opt)
示例6: makeKit
# 需要导入模块: from fontTools import subset [as 别名]
# 或者: from fontTools.subset import Subsetter [as 别名]
def makeKit(font_path):
# put the result into a directory named file_name.kit
dest_dir = os.path.splitext(font_path)[0] + '.kit'
if os.path.isdir(dest_dir):
print 'FAILURE: dest %s already exists' % dest_dir
return False
os.makedirs(dest_dir)
print 'Making a kit for %s in %s' % (font_path, dest_dir)
# crack open the font
# equivalent pyftsubset /tmp/Lobster-Regular.ttf --unicodes='*' --obfuscate_names
options = subset.Options()
with contextlib.closing(subset.load_font(font_path, options)) as font:
unicodes = []
for t in font['cmap'].tables:
if t.isUnicode():
unicodes.extend(t.cmap.keys())
options.unicodes = unicodes
# mangle 'name' so the font can't be installed
options.obfuscate_names
# apply our subsetting, most notably trashing 'name'
subsetter = subset.Subsetter(options=options)
subsetter.populate()
# write [ot]tf, woff, and woff2 editions with 'name' mangled
font_name_noext = os.path.splitext(os.path.basename(font_path))[0]
font_ext = os.path.splitext(os.path.basename(font_path))[1]
for fmt in [font_ext, '.woff', '.woff2']:
dest_file = os.path.join(dest_dir, font_name_noext + fmt)
options.flavor = None
if fmt.startswith('.woff'):
options.flavor = fmt[1:]
print 'Writing %s' % dest_file
with open(dest_file, 'wb') as f:
subset.save_font(font, f, options)
# write a sample somewhat (no early Android, IE) bulletproof css
dest_file = os.path.join(dest_dir, 'bulletproof.css')
os2 = font['OS/2']
font_style = 'normal'
if os2.fsSelection & 1:
font_style = 'italic'
with open(dest_file, 'w') as f:
f.write("@font-face {\n")
f.write(" font-family: '%s';\n" % font_name_noext)
f.write(" font-style: %s;\n" % font_style)
f.write(" font-weight: %d;\n" % os2.usWeightClass)
f.write(" src:\n")
f.write(" url('./%s.woff2') format('woff2'),\n" % font_name_noext)
f.write(" url('./%s.woff') format('woff'),\n" % font_name_noext)
if font_ext == '.otf':
f.write(" url('./%s.otf') format('opentype')" % font_name_noext)
else:
f.write(" url('./%s.ttf') format('truetype')" % font_name_noext)
f.write(";\n")
f.write("}\n")
return True
示例7: subset_otf_from_ufo
# 需要导入模块: from fontTools import subset [as 别名]
# 或者: from fontTools.subset import Subsetter [as 别名]
def subset_otf_from_ufo(self, otf_path, ufo):
"""Subset a font using "Keep Glyphs" custom parameter and export flags as set
by glyphsLib.
"Export Glyphs" and "Remove Glyphs" are currently not supported:
https://github.com/googlei18n/glyphsLib/issues/295.
"""
from fontTools import subset
# we must exclude from the final UFO glyphOrder all the glyphs that were not
# exported to OTF because included in 'public.skipExportGlyphs'
skip_export_glyphs = set(ufo.lib.get("public.skipExportGlyphs", ()))
exported_glyphs = dict.fromkeys(
g for g in ufo.keys() if g not in skip_export_glyphs
)
ufo_order = makeOfficialGlyphOrder(exported_glyphs, glyphOrder=ufo.glyphOrder)
# ufo2ft always inserts a ".notdef" glyph as the first glyph
if ".notdef" not in exported_glyphs:
ufo_order.insert(0, ".notdef")
ot_order = TTFont(otf_path).getGlyphOrder()
assert ot_order[0] == ".notdef"
assert len(ufo_order) == len(ot_order)
for key in (KEEP_GLYPHS_NEW_KEY, KEEP_GLYPHS_OLD_KEY):
keep_glyphs_list = ufo.lib.get(key)
if keep_glyphs_list is not None:
keep_glyphs = set(keep_glyphs_list)
break
else:
keep_glyphs = None
include = []
for source_name, binary_name in zip(ufo_order, ot_order):
if keep_glyphs and source_name not in keep_glyphs:
continue
if source_name in ufo:
exported = ufo[source_name].lib.get(GLYPH_EXPORT_KEY, True)
if not exported:
continue
include.append(binary_name)
# copied from nototools.subset
opt = subset.Options()
opt.name_IDs = ["*"]
opt.name_legacy = True
opt.name_languages = ["*"]
opt.layout_features = ["*"]
opt.notdef_outline = True
opt.recalc_bounds = True
opt.recalc_timestamp = True
opt.canonical_order = True
opt.glyph_names = True
font = subset.load_font(otf_path, opt, lazy=False)
subsetter = subset.Subsetter(options=opt)
subsetter.populate(glyphs=include)
subsetter.subset(font)
subset.save_font(font, otf_path, opt)