def test_skip_export_glyphs_designspace(self, FontClass): # Designspace has a public.skipExportGlyphs lib key excluding "b" and "d". designspace = designspaceLib.DesignSpaceDocument.fromfile( getpath("IncompatibleMasters/IncompatibleMasters.designspace")) for source in designspace.sources: source.font = FontClass( getpath(os.path.join("IncompatibleMasters", source.filename))) ufo2ft.compileInterpolatableTTFsFromDS(designspace, inplace=True) for source in designspace.sources: assert source.font.getGlyphOrder() == [ ".notdef", "a", "c", "e", "f" ] gpos_table = source.font["GPOS"].table assert gpos_table.LookupList.Lookup[0].SubTable[ 0].Coverage.glyphs == [ "a", "e", "f", ] glyphs = source.font["glyf"].glyphs for g in glyphs.values(): g.expand(source.font["glyf"]) assert glyphs["a"].numberOfContours == 1 assert not hasattr(glyphs["a"], "components") assert glyphs["c"].numberOfContours == 6 assert not hasattr(glyphs["c"], "components") assert glyphs["e"].numberOfContours == 13 assert not hasattr(glyphs["e"], "components") assert glyphs["f"].isComposite()
def empty_varfont(designspace_path): designspace = fontTools.designspaceLib.DesignSpaceDocument.fromfile( designspace_path) for source in designspace.sources: source.font = testutil.empty_UFO(source.styleName) ufo2ft.compileInterpolatableTTFsFromDS(designspace, inplace=True) varfont, _, _ = fontTools.varLib.build(designspace) return varfont
def _build_interpolatable_masters( self, designspace, ttf, use_production_names=None, reverse_direction=True, conversion_error=None, feature_writers=None, cff_round_tolerance=None, **kwargs, ): designspace = self._load_designspace_sources(designspace) if ttf: return ufo2ft.compileInterpolatableTTFsFromDS( designspace, useProductionNames=use_production_names, reverseDirection=reverse_direction, cubicConversionError=conversion_error, featureWriters=feature_writers, inplace=True, ) else: return ufo2ft.compileInterpolatableOTFsFromDS( designspace, useProductionNames=use_production_names, roundTolerance=cff_round_tolerance, featureWriters=feature_writers, inplace=True, )
def test_custom_layer_compilation_interpolatable_from_ds(designspace, inplace): result = compileInterpolatableTTFsFromDS(designspace, inplace=inplace) assert (designspace is result) == inplace master_ttfs = [s.font for s in result.sources] assert master_ttfs[0].getGlyphOrder() == [ ".notdef", "a", "e", "s", "dotabovecomb", "edotabove", ] assert master_ttfs[1].getGlyphOrder() == [".notdef", "e"] assert master_ttfs[2].getGlyphOrder() == [ ".notdef", "a", "e", "s", "dotabovecomb", "edotabove", ] sparse_tables = [ tag for tag in master_ttfs[1].keys() if tag != "GlyphOrder" ] assert SPARSE_TTF_MASTER_TABLES.issuperset(sparse_tables) # sentinel value used by varLib to ignore the post table for this sparse # master when building the MVAR table assert master_ttfs[1]["post"].underlinePosition == -0x8000 assert master_ttfs[1]["post"].underlineThickness == -0x8000
def designSpaceAndMti2VF(family): dspath, folder = getFile(".designspace", family) designSpace = openDesignSpace(dspath) mti_source = os.path.join(folder, family + ".plist") mti_paths = readThePlist(open(mti_source, "rb")) masters = designSpace.loadSourceFonts(Font) for master in masters: key = master.info.familyName.replace( " ", "") + "-" + master.info.styleName.replace(" ", "") for table, path in mti_paths[key].items(): with open(os.path.join(folder, path), "rb") as mti_: ufo_path = ("com.github.googlei18n.ufo2ft.mtiFeatures/%s.mti" % table.strip()) master.data[ufo_path] = mti_.read() # If we have MTI sources, any Adobe feature files derived from # the Glyphs file should be ignored. We clear it here because # it only contains junk information anyway. master.features.text = "" font, _, _ = varLib.build( compileInterpolatableTTFsFromDS( designSpace, featureCompilerClass=MtiFeatureCompiler)) destination = folder + "/fonts/VAR" if not os.path.exists(destination): os.makedirs(destination) print("\t" + family + " Variable Font generated\n") font.save(os.path.join(destination, family + "-VF.ttf"))
def generate_variable_font( designspace_path: Path, stylespace_path: Path, additional_locations=None) -> fontTools.ttLib.TTFont: designspace = fontTools.designspaceLib.DesignSpaceDocument.fromfile( designspace_path) for source in designspace.sources: source.font = empty_UFO(source.styleName) ufo2ft.compileInterpolatableTTFsFromDS(designspace, inplace=True) varfont, _, _ = fontTools.varLib.build(designspace) stylespace = statmake.classes.Stylespace.from_file(stylespace_path) if additional_locations is None: additional_locations = designspace.lib.get( "org.statmake.additionalLocations", {}) statmake.lib.apply_stylespace_to_variable_font(stylespace, varfont, additional_locations) return reload_font(varfont)
def designSpace2Var(self): ds = self.designSpace family = os.path.basename(self.familyPath) print("\n>>> Load the {} designspace".format(family)) print(" Load " + family + " files") ds.loadSourceFonts(Font) print(" Start to build Variable Tables") feature_Writers = [KernFeatureWriter(mode="append"), MarkFeatureWriter] font, _, _ = varLib.build(compileInterpolatableTTFsFromDS( ds, featureWriters=feature_Writers), optimize=False) font.save(os.path.join(self.destination, family + "-VF.ttf")) print(" " + family + " Variable Font generated\n")
def designSpace2Var(family): print(">>> Load the {} designspace".format(family)) path, folder = getFile(".designspace", family) designSpace = openDesignSpace(path) print(" Load " + family + " files") designSpace.loadSourceFonts(Font) print(" Start to build Variable Tables") feature_Writers = [KernFeatureWriter(mode="append"), MarkFeatureWriter] font, _, _ = varLib.build(compileInterpolatableTTFsFromDS( designSpace, featureWriters=feature_Writers), optimize=False) destination = folder + "/fonts/VAR" if not os.path.exists(destination): os.makedirs(destination) font.save(os.path.join(destination, family + "-VF.ttf")) print(" " + family + " Variable Font generated\n")
def makeVarFont(self): (minimum, maximum), tag2name = self.findRegularBoldDefaultLocation() # check if defautl exist otherTags = [ a.tag for a in self.designSpaceDocument.axes if a.tag != "wght" ] print("\tLoad {}.designspace".format(self.familyName)) self.designSpaceDocument.loadSourceFonts(defcon.Font) vfont, _, _ = varLib.build(compileInterpolatableTTFsFromDS( self.designSpaceDocument, featureWriters=[ KernFeatureWriter(mode="append"), MarkFeatureWriter() ]), optimize=False) fullfontsFolder = os.path.join(self.dirpath, "fonts/VAR") if not os.path.exists(fullfontsFolder): os.makedirs(fullfontsFolder) path = os.path.join(fullfontsFolder, self.familyName + "-VF.ttf") vfont.save(path) vfont = TTFont(path) tags = {"wght": (minimum, maximum)} if len(otherTags) == 0: slimft = instancer.instantiateVariableFont(vfont, tags) else: for t in otherTags: tags[t] = None slimft = instancer.instantiateVariableFont(vfont, tags) for namerecord in slimft['name'].names: namerecord.string = namerecord.toUnicode() if namerecord.nameID == 3: unicID = namerecord.string.split(";") newID = "4.444" + ";" + unicID[1] + ";" + unicID[2] print("\tTagging the font as a 'slim' one:", newID) namerecord.string = newID if namerecord.nameID == 5: namerecord.string = "Version 4.444" print("\tSaving " + self.familyName + "Slim-VF.ttf\n") slimFontFolder = os.path.join(fullfontsFolder + "/SlimVF") if not os.path.exists(slimFontFolder): os.makedirs(slimFontFolder) slimft.save( os.path.join(slimFontFolder, "%sSlim-VF.ttf" % self.familyName))
def makeVarFont(self, mti=False): self.designSpaceDocument.loadSourceFonts(Font) print("\tStart to build Variable Tables") self.vfont, _, _ = varLib.build(compileInterpolatableTTFsFromDS( self.designSpaceDocument, featureCompilerClass=MtiFeatureCompiler, featureWriters=None), optimize=False) if self.makeUIVersion is False: path = os.path.join(self.destination, self.familyName + "-VF.ttf") print(path) self.vfont.save(path) print("\t" + self.familyName + " Variable Font generated\n") else: path = os.path.join(self.destination, self.familyName + "UI-VF.ttf") vfontUI = self.renamer_() vfontUI.save(path) print("\t" + self.familyName + "UI Variable Font generated\n")
def _build_interpolatable_masters(self, designspace, ttf, use_production_names=None, reverse_direction=True, conversion_error=None, feature_writers=None, cff_round_tolerance=None, **kwargs): if hasattr(designspace, "__fspath__"): ds_path = designspace.__fspath__() if isinstance(designspace, basestring): ds_path = designspace else: # reload designspace from its path so we have a new copy # that can be modified in-place. ds_path = designspace.path if ds_path is not None: designspace = designspaceLib.DesignSpaceDocument.fromfile(ds_path) self._load_designspace_sources(designspace) if ttf: return ufo2ft.compileInterpolatableTTFsFromDS( designspace, useProductionNames=use_production_names, reverseDirection=reverse_direction, cubicConversionError=conversion_error, featureWriters=feature_writers, inplace=True, ) else: return ufo2ft.compileInterpolatableOTFsFromDS( designspace, useProductionNames=use_production_names, roundTolerance=cff_round_tolerance, featureWriters=feature_writers, inplace=True, )
def test_compilation_from_ds_missing_source_font(designspace): designspace.sources[0].font = None with pytest.raises(AttributeError, match="missing required 'font'"): compileInterpolatableTTFsFromDS(designspace)
) parser.add_argument("output_path", type=Path, help="The variable TTF output path.") args = parser.parse_args() designspace_path = args.designspace_path.resolve() stylespace_path = args.stylespace_path.resolve() output_path = args.output_path.resolve() # 1. Load Designspace and filter out instances that are marked as non-exportable. designspace = fontTools.designspaceLib.DesignSpaceDocument.fromfile(designspace_path) for source in designspace.sources: source.font = ufoLib2.Font.open(designspace_path.parent / source.filename) designspace.instances = [ s for s in designspace.instances if s.lib.get("com.schriftgestaltung.export", True) ] # 2. Compile interpolatable master TTFs. ufo2ft.compileInterpolatableTTFsFromDS(designspace, inplace=True) # 3. Combine masters into a variable TTF. varfont, _, _ = fontTools.varLib.build(designspace) # 4. Generate STAT table. stylespace = statmake.classes.Stylespace.from_file(stylespace_path) statmake.lib.apply_stylespace_to_variable_font(stylespace, varfont, {}) varfont.save(output_path)