本文整理汇总了Python中fontTools.designspaceLib.DesignSpaceDocument.fromfile方法的典型用法代码示例。如果您正苦于以下问题:Python DesignSpaceDocument.fromfile方法的具体用法?Python DesignSpaceDocument.fromfile怎么用?Python DesignSpaceDocument.fromfile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fontTools.designspaceLib.DesignSpaceDocument
的用法示例。
在下文中一共展示了DesignSpaceDocument.fromfile方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_varlib_build_from_ttx_paths
# 需要导入模块: from fontTools.designspaceLib import DesignSpaceDocument [as 别名]
# 或者: from fontTools.designspaceLib.DesignSpaceDocument import fromfile [as 别名]
def test_varlib_build_from_ttx_paths(self):
ds_path = self.get_test_input("Build.designspace")
ttx_dir = self.get_test_input("master_ttx_interpolatable_ttf")
expected_ttx_path = self.get_test_output("BuildMain.ttx")
ds = DesignSpaceDocument.fromfile(ds_path)
for source in ds.sources:
source.path = os.path.join(
ttx_dir, os.path.basename(source.filename).replace(".ufo", ".ttx")
)
ds.updatePaths()
varfont, _, _ = build(ds)
varfont = reload_font(varfont)
tables = [table_tag for table_tag in varfont.keys() if table_tag != "head"]
self.expect_ttx(varfont, expected_ttx_path, tables)
示例2: test_varlib_build_sparse_CFF2
# 需要导入模块: from fontTools.designspaceLib import DesignSpaceDocument [as 别名]
# 或者: from fontTools.designspaceLib.DesignSpaceDocument import fromfile [as 别名]
def test_varlib_build_sparse_CFF2(self):
ds_path = self.get_test_input('TestSparseCFF2VF.designspace')
ttx_dir = self.get_test_input("master_sparse_cff2")
expected_ttx_path = self.get_test_output("TestSparseCFF2VF.ttx")
self.temp_dir()
for path in self.get_file_list(ttx_dir, '.ttx', 'MasterSet_Kanji-'):
self.compile_font(path, ".otf", self.tempdir)
ds = DesignSpaceDocument.fromfile(ds_path)
for source in ds.sources:
source.path = os.path.join(
self.tempdir, os.path.basename(source.filename).replace(".ufo", ".otf")
)
ds.updatePaths()
varfont, _, _ = build(ds)
varfont = reload_font(varfont)
tables = ["fvar", "CFF2"]
self.expect_ttx(varfont, expected_ttx_path, tables)
示例3: test_varlib_build_from_ds_object_in_memory_ttfonts
# 需要导入模块: from fontTools.designspaceLib import DesignSpaceDocument [as 别名]
# 或者: from fontTools.designspaceLib.DesignSpaceDocument import fromfile [as 别名]
def test_varlib_build_from_ds_object_in_memory_ttfonts(self):
ds_path = self.get_test_input("Build.designspace")
ttx_dir = self.get_test_input("master_ttx_interpolatable_ttf")
expected_ttx_path = self.get_test_output("BuildMain.ttx")
self.temp_dir()
for path in self.get_file_list(ttx_dir, '.ttx', 'TestFamily-'):
self.compile_font(path, ".ttf", self.tempdir)
ds = DesignSpaceDocument.fromfile(ds_path)
for source in ds.sources:
filename = os.path.join(
self.tempdir, os.path.basename(source.filename).replace(".ufo", ".ttf")
)
source.font = TTFont(
filename, recalcBBoxes=False, recalcTimestamp=False, lazy=True
)
source.filename = None # Make sure no file path gets into build()
varfont, _, _ = build(ds)
varfont = reload_font(varfont)
tables = [table_tag for table_tag in varfont.keys() if table_tag != "head"]
self.expect_ttx(varfont, expected_ttx_path, tables)
示例4: test_varlib_build_VVAR_CFF2
# 需要导入模块: from fontTools.designspaceLib import DesignSpaceDocument [as 别名]
# 或者: from fontTools.designspaceLib.DesignSpaceDocument import fromfile [as 别名]
def test_varlib_build_VVAR_CFF2(self):
ds_path = self.get_test_input('TestVVAR.designspace')
ttx_dir = self.get_test_input("master_vvar_cff2")
expected_ttx_name = 'TestVVAR'
suffix = '.otf'
self.temp_dir()
for path in self.get_file_list(ttx_dir, '.ttx', 'TestVVAR'):
font, savepath = self.compile_font(path, suffix, self.tempdir)
ds = DesignSpaceDocument.fromfile(ds_path)
for source in ds.sources:
source.path = os.path.join(
self.tempdir, os.path.basename(source.filename).replace(".ufo", suffix)
)
ds.updatePaths()
varfont, _, _ = build(ds)
varfont = reload_font(varfont)
expected_ttx_path = self.get_test_output(expected_ttx_name + '.ttx')
tables = ["VVAR"]
self.expect_ttx(varfont, expected_ttx_path, tables)
self.check_ttx_dump(varfont, expected_ttx_path, tables, suffix)
示例5: load_designspace
# 需要导入模块: from fontTools.designspaceLib import DesignSpaceDocument [as 别名]
# 或者: from fontTools.designspaceLib.DesignSpaceDocument import fromfile [as 别名]
def load_designspace(designspace_filename):
ds = DesignSpaceDocument.fromfile(designspace_filename)
masters = ds.sources
if not masters:
raise VarLibError("no sources found in .designspace")
instances = ds.instances
standard_axis_map = OrderedDict([
('weight', ('wght', {'en':'Weight'})),
('width', ('wdth', {'en':'Width'})),
('slant', ('slnt', {'en':'Slant'})),
('optical', ('opsz', {'en':'Optical Size'})),
])
# Setup axes
axes = OrderedDict()
for axis in ds.axes:
axis_name = axis.name
if not axis_name:
assert axis.tag is not None
axis_name = axis.name = axis.tag
if axis_name in standard_axis_map:
if axis.tag is None:
axis.tag = standard_axis_map[axis_name][0]
if not axis.labelNames:
axis.labelNames.update(standard_axis_map[axis_name][1])
else:
assert axis.tag is not None
if not axis.labelNames:
axis.labelNames["en"] = axis_name
axes[axis_name] = axis
log.info("Axes:\n%s", pformat([axis.asdict() for axis in axes.values()]))
# Check all master and instance locations are valid and fill in defaults
for obj in masters+instances:
obj_name = obj.name or obj.styleName or ''
loc = obj.location
for axis_name in loc.keys():
assert axis_name in axes, "Location axis '%s' unknown for '%s'." % (axis_name, obj_name)
for axis_name,axis in axes.items():
if axis_name not in loc:
loc[axis_name] = axis.default
else:
v = axis.map_backward(loc[axis_name])
assert axis.minimum <= v <= axis.maximum, "Location for axis '%s' (mapped to %s) out of range for '%s' [%s..%s]" % (axis_name, v, obj_name, axis.minimum, axis.maximum)
# Normalize master locations
internal_master_locs = [o.location for o in masters]
log.info("Internal master locations:\n%s", pformat(internal_master_locs))
# TODO This mapping should ideally be moved closer to logic in _add_fvar/avar
internal_axis_supports = {}
for axis in axes.values():
triple = (axis.minimum, axis.default, axis.maximum)
internal_axis_supports[axis.name] = [axis.map_forward(v) for v in triple]
log.info("Internal axis supports:\n%s", pformat(internal_axis_supports))
normalized_master_locs = [models.normalizeLocation(m, internal_axis_supports) for m in internal_master_locs]
log.info("Normalized master locations:\n%s", pformat(normalized_master_locs))
# Find base master
base_idx = None
for i,m in enumerate(normalized_master_locs):
if all(v == 0 for v in m.values()):
assert base_idx is None
base_idx = i
assert base_idx is not None, "Base master not found; no master at default location?"
log.info("Index of base master: %s", base_idx)
return _DesignSpaceData(
axes,
internal_axis_supports,
base_idx,
normalized_master_locs,
masters,
instances,
ds.rules,
)
示例6: build
# 需要导入模块: from fontTools.designspaceLib import DesignSpaceDocument [as 别名]
# 或者: from fontTools.designspaceLib.DesignSpaceDocument import fromfile [as 别名]
def build(designspace, master_finder=lambda s:s, exclude=[], optimize=True):
"""
Build variation font from a designspace file.
If master_finder is set, it should be a callable that takes master
filename as found in designspace file and map it to master font
binary as to be opened (eg. .ttf or .otf).
"""
if hasattr(designspace, "sources"): # Assume a DesignspaceDocument
pass
else: # Assume a file path
designspace = DesignSpaceDocument.fromfile(designspace)
ds = load_designspace(designspace)
log.info("Building variable font")
log.info("Loading master fonts")
master_fonts = load_masters(designspace, master_finder)
# TODO: 'master_ttfs' is unused except for return value, remove later
master_ttfs = []
for master in master_fonts:
try:
master_ttfs.append(master.reader.file.name)
except AttributeError:
master_ttfs.append(None) # in-memory fonts have no path
# Copy the base master to work from it
vf = deepcopy(master_fonts[ds.base_idx])
# TODO append masters as named-instances as well; needs .designspace change.
fvar = _add_fvar(vf, ds.axes, ds.instances)
if 'STAT' not in exclude:
_add_stat(vf, ds.axes)
if 'avar' not in exclude:
_add_avar(vf, ds.axes)
# Map from axis names to axis tags...
normalized_master_locs = [
{ds.axes[k].tag: v for k,v in loc.items()} for loc in ds.normalized_master_locs
]
# From here on, we use fvar axes only
axisTags = [axis.axisTag for axis in fvar.axes]
# Assume single-model for now.
model = models.VariationModel(normalized_master_locs, axisOrder=axisTags)
assert 0 == model.mapping[ds.base_idx]
log.info("Building variations tables")
if 'MVAR' not in exclude:
_add_MVAR(vf, model, master_fonts, axisTags)
if 'HVAR' not in exclude:
_add_HVAR(vf, model, master_fonts, axisTags)
if 'GDEF' not in exclude or 'GPOS' not in exclude:
_merge_OTL(vf, model, master_fonts, axisTags)
if 'gvar' not in exclude and 'glyf' in vf:
_add_gvar(vf, model, master_fonts, optimize=optimize)
if 'cvar' not in exclude and 'glyf' in vf:
_merge_TTHinting(vf, model, master_fonts)
if 'GSUB' not in exclude and ds.rules:
_add_GSUB_feature_variations(vf, ds.axes, ds.internal_axis_supports, ds.rules)
if 'CFF2' not in exclude and 'CFF ' in vf:
_add_CFF2(vf, model, master_fonts)
for tag in exclude:
if tag in vf:
del vf[tag]
# TODO: Only return vf for 4.0+, the rest is unused.
return vf, model, master_ttfs
示例7: test_varlib_build_sparse_masters_MVAR
# 需要导入模块: from fontTools.designspaceLib import DesignSpaceDocument [as 别名]
# 或者: from fontTools.designspaceLib.DesignSpaceDocument import fromfile [as 别名]
def test_varlib_build_sparse_masters_MVAR(self):
import fontTools.varLib.mvar
ds_path = self.get_test_input("SparseMasters.designspace")
ds = DesignSpaceDocument.fromfile(ds_path)
load_masters(ds)
# Trigger MVAR generation so varLib is forced to create deltas with a
# sparse master inbetween.
font_0_os2 = ds.sources[0].font["OS/2"]
font_0_os2.sTypoAscender = 1
font_0_os2.sTypoDescender = 1
font_0_os2.sTypoLineGap = 1
font_0_os2.usWinAscent = 1
font_0_os2.usWinDescent = 1
font_0_os2.sxHeight = 1
font_0_os2.sCapHeight = 1
font_0_os2.ySubscriptXSize = 1
font_0_os2.ySubscriptYSize = 1
font_0_os2.ySubscriptXOffset = 1
font_0_os2.ySubscriptYOffset = 1
font_0_os2.ySuperscriptXSize = 1
font_0_os2.ySuperscriptYSize = 1
font_0_os2.ySuperscriptXOffset = 1
font_0_os2.ySuperscriptYOffset = 1
font_0_os2.yStrikeoutSize = 1
font_0_os2.yStrikeoutPosition = 1
font_0_vhea = newTable("vhea")
font_0_vhea.ascent = 1
font_0_vhea.descent = 1
font_0_vhea.lineGap = 1
font_0_vhea.caretSlopeRise = 1
font_0_vhea.caretSlopeRun = 1
font_0_vhea.caretOffset = 1
ds.sources[0].font["vhea"] = font_0_vhea
font_0_hhea = ds.sources[0].font["hhea"]
font_0_hhea.caretSlopeRise = 1
font_0_hhea.caretSlopeRun = 1
font_0_hhea.caretOffset = 1
font_0_post = ds.sources[0].font["post"]
font_0_post.underlineThickness = 1
font_0_post.underlinePosition = 1
font_2_os2 = ds.sources[2].font["OS/2"]
font_2_os2.sTypoAscender = 800
font_2_os2.sTypoDescender = 800
font_2_os2.sTypoLineGap = 800
font_2_os2.usWinAscent = 800
font_2_os2.usWinDescent = 800
font_2_os2.sxHeight = 800
font_2_os2.sCapHeight = 800
font_2_os2.ySubscriptXSize = 800
font_2_os2.ySubscriptYSize = 800
font_2_os2.ySubscriptXOffset = 800
font_2_os2.ySubscriptYOffset = 800
font_2_os2.ySuperscriptXSize = 800
font_2_os2.ySuperscriptYSize = 800
font_2_os2.ySuperscriptXOffset = 800
font_2_os2.ySuperscriptYOffset = 800
font_2_os2.yStrikeoutSize = 800
font_2_os2.yStrikeoutPosition = 800
font_2_vhea = newTable("vhea")
font_2_vhea.ascent = 800
font_2_vhea.descent = 800
font_2_vhea.lineGap = 800
font_2_vhea.caretSlopeRise = 800
font_2_vhea.caretSlopeRun = 800
font_2_vhea.caretOffset = 800
ds.sources[2].font["vhea"] = font_2_vhea
font_2_hhea = ds.sources[2].font["hhea"]
font_2_hhea.caretSlopeRise = 800
font_2_hhea.caretSlopeRun = 800
font_2_hhea.caretOffset = 800
font_2_post = ds.sources[2].font["post"]
font_2_post.underlineThickness = 800
font_2_post.underlinePosition = 800
varfont, _, _ = build(ds)
mvar_tags = [vr.ValueTag for vr in varfont["MVAR"].table.ValueRecord]
assert all(tag in mvar_tags for tag in fontTools.varLib.mvar.MVAR_ENTRIES)