def interpolate_layout(designspace_filename, loc, master_finder=lambda s: s, mapped=False): """ Interpolate GPOS from a designspace file and location. 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 mapped is False (default), then location is mapped using the map element of the axes in designspace file. If mapped is True, it is assumed that location is in designspace's internal space and no mapping is performed. """ axes, internal_axis_supports, base_idx, normalized_master_locs, masters, instances = load_designspace( designspace_filename) log.info("Building interpolated font") log.info("Loading master fonts") basedir = os.path.dirname(designspace_filename) master_ttfs = [ master_finder(os.path.join(basedir, m['filename'])) for m in masters ] master_fonts = [TTFont(ttf_path) for ttf_path in master_ttfs] #font = master_fonts[base_idx] font = TTFont(master_ttfs[base_idx]) log.info("Location: %s", pformat(loc)) if not mapped: loc = {name: axes[name].map_forward(v) for name, v in loc.items()} log.info("Internal location: %s", pformat(loc)) loc = models.normalizeLocation(loc, internal_axis_supports) log.info("Normalized location: %s", pformat(loc)) # Assume single-model for now. model = models.VariationModel(normalized_master_locs) assert 0 == model.mapping[base_idx] merger = InstancerMerger(font, model, loc) log.info("Building interpolated tables") merger.mergeTables(font, master_fonts, ['GPOS']) return font
def interpolate_layout(designspace, loc, master_finder=lambda s: s, mapped=False): """ Interpolate GPOS from a designspace file and location. 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 mapped is False (default), then location is mapped using the map element of the axes in designspace file. If mapped is True, it is assumed that location is in designspace's internal space and no mapping is performed. """ if hasattr(designspace, "sources"): # Assume a DesignspaceDocument pass else: # Assume a file path from fontTools.designspaceLib import DesignSpaceDocument designspace = DesignSpaceDocument.fromfile(designspace) ds = load_designspace(designspace) log.info("Building interpolated font") log.info("Loading master fonts") master_fonts = load_masters(designspace, master_finder) font = deepcopy(master_fonts[ds.base_idx]) log.info("Location: %s", pformat(loc)) if not mapped: loc = {name: ds.axes[name].map_forward(v) for name, v in loc.items()} log.info("Internal location: %s", pformat(loc)) loc = models.normalizeLocation(loc, ds.internal_axis_supports) log.info("Normalized location: %s", pformat(loc)) # Assume single-model for now. model = models.VariationModel(ds.normalized_master_locs) assert 0 == model.mapping[ds.base_idx] merger = InstancerMerger(font, model, loc) log.info("Building interpolated tables") # TODO GSUB/GDEF merger.mergeTables(font, master_fonts, ['GPOS']) return font
def interpolate_layout(designspace_filename, loc, master_finder=lambda s:s, mapped=False): """ Interpolate GPOS from a designspace file and location. 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 mapped is False (default), then location is mapped using the map element of the axes in designspace file. If mapped is True, it is assumed that location is in designspace's internal space and no mapping is performed. """ ds = load_designspace(designspace_filename) log.info("Building interpolated font") log.info("Loading master fonts") basedir = os.path.dirname(designspace_filename) master_ttfs = [ master_finder(os.path.join(basedir, m.filename)) for m in ds.masters ] master_fonts = [TTFont(ttf_path) for ttf_path in master_ttfs] #font = master_fonts[ds.base_idx] font = TTFont(master_ttfs[ds.base_idx]) log.info("Location: %s", pformat(loc)) if not mapped: loc = {name: ds.axes[name].map_forward(v) for name,v in loc.items()} log.info("Internal location: %s", pformat(loc)) loc = models.normalizeLocation(loc, ds.internal_axis_supports) log.info("Normalized location: %s", pformat(loc)) # Assume single-model for now. model = models.VariationModel(ds.normalized_master_locs) assert 0 == model.mapping[ds.base_idx] merger = InstancerMerger(font, model, loc) log.info("Building interpolated tables") # TODO GSUB/GDEF merger.mergeTables(font, master_fonts, ['GPOS']) return font
def interpolate_layout(designspace_filename, loc, finder): ds = designspace.load(designspace_filename) axes = ds['axes'] if 'axes' in ds else [] if 'sources' not in ds or not ds['sources']: raise VarLibError("no 'sources' defined in .designspace") masters = ds['sources'] base_idx = None for i,m in enumerate(masters): if 'info' in m and m['info']['copy']: assert base_idx is None base_idx = i assert base_idx is not None, "Cannot find 'base' master; Add <info> element to one of the masters in the .designspace document." from pprint import pprint print("Index of base master:", base_idx) print("Building variable font") print("Loading master fonts") basedir = os.path.dirname(designspace_filename) master_ttfs = [finder(os.path.join(basedir, m['filename'])) for m in masters] master_fonts = [TTFont(ttf_path) for ttf_path in master_ttfs] #font = master_fonts[base_idx] font = TTFont(master_ttfs[base_idx]) master_locs = [o['location'] for o in masters] axis_names = set(master_locs[0].keys()) assert all(axis_names == set(m.keys()) for m in master_locs) # Set up axes axes_dict = {} if axes: # the designspace file loaded had an <axes> element for axis in axes: default = axis['default'] lower = axis['minimum'] upper = axis['maximum'] name = axis['name'] axes_dict[name] = (lower, default, upper) else: for tag in axis_names: default = master_locs[base_idx][tag] lower = min(m[tag] for m in master_locs) upper = max(m[tag] for m in master_locs) if default == lower == upper: continue axes_dict[tag] = (lower, default, upper) print("Axes:") pprint(axes_dict) print("Location:", loc) print("Master locations:") pprint(master_locs) # Normalize locations loc = models.normalizeLocation(loc, axes_dict) master_locs = [models.normalizeLocation(m, axes_dict) for m in master_locs] print("Normalized location:", loc) print("Normalized master locations:") pprint(master_locs) # Assume single-model for now. model = models.VariationModel(master_locs) assert 0 == model.mapping[base_idx] merger = InstancerMerger(font, model, loc) print("Building variations tables") merger.mergeTables(font, master_fonts, axes_dict, base_idx, ['GPOS']) return font