__init__.py 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. """
  2. Module for dealing with 'gvar'-style font variations, also known as run-time
  3. interpolation.
  4. The ideas here are very similar to MutatorMath. There is even code to read
  5. MutatorMath .designspace files in the varLib.designspace module.
  6. For now, if you run this file on a designspace file, it tries to find
  7. ttf-interpolatable files for the masters and build a variable-font from
  8. them. Such ttf-interpolatable and designspace files can be generated from
  9. a Glyphs source, eg., using noto-source as an example:
  10. .. code-block:: sh
  11. $ fontmake -o ttf-interpolatable -g NotoSansArabic-MM.glyphs
  12. Then you can make a variable-font this way:
  13. .. code-block:: sh
  14. $ fonttools varLib master_ufo/NotoSansArabic.designspace
  15. API *will* change in near future.
  16. """
  17. from typing import List
  18. from fontTools.misc.vector import Vector
  19. from fontTools.misc.roundTools import noRound, otRound
  20. from fontTools.misc.fixedTools import floatToFixed as fl2fi
  21. from fontTools.misc.textTools import Tag, tostr
  22. from fontTools.ttLib import TTFont, newTable
  23. from fontTools.ttLib.tables._f_v_a_r import Axis, NamedInstance
  24. from fontTools.ttLib.tables._g_l_y_f import (
  25. GlyphCoordinates,
  26. dropImpliedOnCurvePoints,
  27. USE_MY_METRICS,
  28. )
  29. from fontTools.ttLib.tables.ttProgram import Program
  30. from fontTools.ttLib.tables.TupleVariation import TupleVariation
  31. from fontTools.ttLib.tables import otTables as ot
  32. from fontTools.ttLib.tables.otBase import OTTableWriter
  33. from fontTools.varLib import builder, models, varStore
  34. from fontTools.varLib.merger import VariationMerger, COLRVariationMerger
  35. from fontTools.varLib.mvar import MVAR_ENTRIES
  36. from fontTools.varLib.iup import iup_delta_optimize
  37. from fontTools.varLib.featureVars import addFeatureVariations
  38. from fontTools.designspaceLib import DesignSpaceDocument, InstanceDescriptor
  39. from fontTools.designspaceLib.split import splitInterpolable, splitVariableFonts
  40. from fontTools.varLib.stat import buildVFStatTable
  41. from fontTools.colorLib.builder import buildColrV1
  42. from fontTools.colorLib.unbuilder import unbuildColrV1
  43. from functools import partial
  44. from collections import OrderedDict, defaultdict, namedtuple
  45. import os.path
  46. import logging
  47. from copy import deepcopy
  48. from pprint import pformat
  49. from re import fullmatch
  50. from .errors import VarLibError, VarLibValidationError
  51. log = logging.getLogger("fontTools.varLib")
  52. # This is a lib key for the designspace document. The value should be
  53. # a comma-separated list of OpenType feature tag(s), to be used as the
  54. # FeatureVariations feature.
  55. # If present, the DesignSpace <rules processing="..."> flag is ignored.
  56. FEAVAR_FEATURETAG_LIB_KEY = "com.github.fonttools.varLib.featureVarsFeatureTag"
  57. #
  58. # Creation routines
  59. #
  60. def _add_fvar(font, axes, instances: List[InstanceDescriptor]):
  61. """
  62. Add 'fvar' table to font.
  63. axes is an ordered dictionary of DesignspaceAxis objects.
  64. instances is list of dictionary objects with 'location', 'stylename',
  65. and possibly 'postscriptfontname' entries.
  66. """
  67. assert axes
  68. assert isinstance(axes, OrderedDict)
  69. log.info("Generating fvar")
  70. fvar = newTable("fvar")
  71. nameTable = font["name"]
  72. # if there are not currently any mac names don't add them here, that's inconsistent
  73. # https://github.com/fonttools/fonttools/issues/683
  74. macNames = any(nr.platformID == 1 for nr in getattr(nameTable, "names", ()))
  75. # we have all the best ways to express mac names
  76. platforms = ((3, 1, 0x409),)
  77. if macNames:
  78. platforms = ((1, 0, 0),) + platforms
  79. for a in axes.values():
  80. axis = Axis()
  81. axis.axisTag = Tag(a.tag)
  82. # TODO Skip axes that have no variation.
  83. axis.minValue, axis.defaultValue, axis.maxValue = (
  84. a.minimum,
  85. a.default,
  86. a.maximum,
  87. )
  88. axis.axisNameID = nameTable.addMultilingualName(
  89. a.labelNames, font, minNameID=256, mac=macNames
  90. )
  91. axis.flags = int(a.hidden)
  92. fvar.axes.append(axis)
  93. default_coordinates = {axis.axisTag: axis.defaultValue for axis in fvar.axes}
  94. for instance in instances:
  95. # Filter out discrete axis locations
  96. coordinates = {
  97. name: value for name, value in instance.location.items() if name in axes
  98. }
  99. if "en" not in instance.localisedStyleName:
  100. if not instance.styleName:
  101. raise VarLibValidationError(
  102. f"Instance at location '{coordinates}' must have a default English "
  103. "style name ('stylename' attribute on the instance element or a "
  104. "stylename element with an 'xml:lang=\"en\"' attribute)."
  105. )
  106. localisedStyleName = dict(instance.localisedStyleName)
  107. localisedStyleName["en"] = tostr(instance.styleName)
  108. else:
  109. localisedStyleName = instance.localisedStyleName
  110. psname = instance.postScriptFontName
  111. inst = NamedInstance()
  112. inst.coordinates = {
  113. axes[k].tag: axes[k].map_backward(v) for k, v in coordinates.items()
  114. }
  115. subfamilyNameID = nameTable.findMultilingualName(
  116. localisedStyleName, windows=True, mac=macNames
  117. )
  118. if subfamilyNameID in {2, 17} and inst.coordinates == default_coordinates:
  119. # Instances can only reuse an existing name ID 2 or 17 if they are at the
  120. # default location across all axes, see:
  121. # https://github.com/fonttools/fonttools/issues/3825.
  122. inst.subfamilyNameID = subfamilyNameID
  123. else:
  124. inst.subfamilyNameID = nameTable.addMultilingualName(
  125. localisedStyleName, windows=True, mac=macNames, minNameID=256
  126. )
  127. if psname is not None:
  128. psname = tostr(psname)
  129. inst.postscriptNameID = nameTable.addName(psname, platforms=platforms)
  130. fvar.instances.append(inst)
  131. assert "fvar" not in font
  132. font["fvar"] = fvar
  133. return fvar
  134. def _add_avar(font, axes, mappings, axisTags):
  135. """
  136. Add 'avar' table to font.
  137. axes is an ordered dictionary of AxisDescriptor objects.
  138. """
  139. assert axes
  140. assert isinstance(axes, OrderedDict)
  141. log.info("Generating avar")
  142. avar = newTable("avar")
  143. interesting = False
  144. vals_triples = {}
  145. for axis in axes.values():
  146. # Currently, some rasterizers require that the default value maps
  147. # (-1 to -1, 0 to 0, and 1 to 1) be present for all the segment
  148. # maps, even when the default normalization mapping for the axis
  149. # was not modified.
  150. # https://github.com/googlei18n/fontmake/issues/295
  151. # https://github.com/fonttools/fonttools/issues/1011
  152. # TODO(anthrotype) revert this (and 19c4b37) when issue is fixed
  153. curve = avar.segments[axis.tag] = {-1.0: -1.0, 0.0: 0.0, 1.0: 1.0}
  154. keys_triple = (axis.minimum, axis.default, axis.maximum)
  155. vals_triple = tuple(axis.map_forward(v) for v in keys_triple)
  156. vals_triples[axis.tag] = vals_triple
  157. if not axis.map:
  158. continue
  159. items = sorted(axis.map)
  160. keys = [item[0] for item in items]
  161. vals = [item[1] for item in items]
  162. # Current avar requirements. We don't have to enforce
  163. # these on the designer and can deduce some ourselves,
  164. # but for now just enforce them.
  165. if axis.minimum != min(keys):
  166. raise VarLibValidationError(
  167. f"Axis '{axis.name}': there must be a mapping for the axis minimum "
  168. f"value {axis.minimum} and it must be the lowest input mapping value."
  169. )
  170. if axis.maximum != max(keys):
  171. raise VarLibValidationError(
  172. f"Axis '{axis.name}': there must be a mapping for the axis maximum "
  173. f"value {axis.maximum} and it must be the highest input mapping value."
  174. )
  175. if axis.default not in keys:
  176. raise VarLibValidationError(
  177. f"Axis '{axis.name}': there must be a mapping for the axis default "
  178. f"value {axis.default}."
  179. )
  180. # No duplicate input values (output values can be >= their preceeding value).
  181. if len(set(keys)) != len(keys):
  182. raise VarLibValidationError(
  183. f"Axis '{axis.name}': All axis mapping input='...' values must be "
  184. "unique, but we found duplicates."
  185. )
  186. # Ascending values
  187. if sorted(vals) != vals:
  188. raise VarLibValidationError(
  189. f"Axis '{axis.name}': mapping output values must be in ascending order."
  190. )
  191. keys = [models.normalizeValue(v, keys_triple) for v in keys]
  192. vals = [models.normalizeValue(v, vals_triple) for v in vals]
  193. if all(k == v for k, v in zip(keys, vals)):
  194. continue
  195. interesting = True
  196. curve.update(zip(keys, vals))
  197. assert 0.0 in curve and curve[0.0] == 0.0
  198. assert -1.0 not in curve or curve[-1.0] == -1.0
  199. assert +1.0 not in curve or curve[+1.0] == +1.0
  200. # curve.update({-1.0: -1.0, 0.0: 0.0, 1.0: 1.0})
  201. if mappings:
  202. interesting = True
  203. inputLocations = [
  204. {
  205. axes[name].tag: models.normalizeValue(v, vals_triples[axes[name].tag])
  206. for name, v in mapping.inputLocation.items()
  207. }
  208. for mapping in mappings
  209. ]
  210. outputLocations = [
  211. {
  212. axes[name].tag: models.normalizeValue(v, vals_triples[axes[name].tag])
  213. for name, v in mapping.outputLocation.items()
  214. }
  215. for mapping in mappings
  216. ]
  217. assert len(inputLocations) == len(outputLocations)
  218. # If base-master is missing, insert it at zero location.
  219. if not any(all(v == 0 for k, v in loc.items()) for loc in inputLocations):
  220. inputLocations.insert(0, {})
  221. outputLocations.insert(0, {})
  222. model = models.VariationModel(inputLocations, axisTags)
  223. storeBuilder = varStore.OnlineVarStoreBuilder(axisTags)
  224. storeBuilder.setModel(model)
  225. varIdxes = {}
  226. for tag in axisTags:
  227. masterValues = []
  228. for vo, vi in zip(outputLocations, inputLocations):
  229. if tag not in vo:
  230. masterValues.append(0)
  231. continue
  232. v = vo[tag] - vi.get(tag, 0)
  233. masterValues.append(fl2fi(v, 14))
  234. varIdxes[tag] = storeBuilder.storeMasters(masterValues)[1]
  235. store = storeBuilder.finish()
  236. optimized = store.optimize()
  237. varIdxes = {axis: optimized[value] for axis, value in varIdxes.items()}
  238. varIdxMap = builder.buildDeltaSetIndexMap(varIdxes[t] for t in axisTags)
  239. avar.majorVersion = 2
  240. avar.table = ot.avar()
  241. avar.table.VarIdxMap = varIdxMap
  242. avar.table.VarStore = store
  243. assert "avar" not in font
  244. if not interesting:
  245. log.info("No need for avar")
  246. avar = None
  247. else:
  248. font["avar"] = avar
  249. return avar
  250. def _add_stat(font):
  251. # Note: this function only gets called by old code that calls `build()`
  252. # directly. Newer code that wants to benefit from STAT data from the
  253. # designspace should call `build_many()`
  254. if "STAT" in font:
  255. return
  256. from ..otlLib.builder import buildStatTable
  257. fvarTable = font["fvar"]
  258. axes = [dict(tag=a.axisTag, name=a.axisNameID) for a in fvarTable.axes]
  259. buildStatTable(font, axes)
  260. _MasterData = namedtuple("_MasterData", ["glyf", "hMetrics", "vMetrics"])
  261. def _add_gvar(font, masterModel, master_ttfs, tolerance=0.5, optimize=True):
  262. if tolerance < 0:
  263. raise ValueError("`tolerance` must be a positive number.")
  264. log.info("Generating gvar")
  265. assert "gvar" not in font
  266. gvar = font["gvar"] = newTable("gvar")
  267. glyf = font["glyf"]
  268. defaultMasterIndex = masterModel.reverseMapping[0]
  269. master_datas = [
  270. _MasterData(
  271. m["glyf"], m["hmtx"].metrics, getattr(m.get("vmtx"), "metrics", None)
  272. )
  273. for m in master_ttfs
  274. ]
  275. for glyph in font.getGlyphOrder():
  276. log.debug("building gvar for glyph '%s'", glyph)
  277. allData = [
  278. m.glyf._getCoordinatesAndControls(glyph, m.hMetrics, m.vMetrics)
  279. for m in master_datas
  280. ]
  281. if allData[defaultMasterIndex][1].numberOfContours != 0:
  282. # If the default master is not empty, interpret empty non-default masters
  283. # as missing glyphs from a sparse master
  284. allData = [
  285. d if d is not None and d[1].numberOfContours != 0 else None
  286. for d in allData
  287. ]
  288. model, allData = masterModel.getSubModel(allData)
  289. allCoords = [d[0] for d in allData]
  290. allControls = [d[1] for d in allData]
  291. control = allControls[0]
  292. if not models.allEqual(allControls):
  293. log.warning("glyph %s has incompatible masters; skipping" % glyph)
  294. continue
  295. del allControls
  296. # Update gvar
  297. gvar.variations[glyph] = []
  298. deltas = model.getDeltas(
  299. allCoords, round=partial(GlyphCoordinates.__round__, round=round)
  300. )
  301. supports = model.supports
  302. assert len(deltas) == len(supports)
  303. # Prepare for IUP optimization
  304. origCoords = deltas[0]
  305. endPts = control.endPts
  306. for i, (delta, support) in enumerate(zip(deltas[1:], supports[1:])):
  307. if all(v == 0 for v in delta.array):
  308. continue
  309. var = TupleVariation(support, delta)
  310. if optimize:
  311. delta_opt = iup_delta_optimize(
  312. delta, origCoords, endPts, tolerance=tolerance
  313. )
  314. if None in delta_opt:
  315. # Use "optimized" version only if smaller...
  316. var_opt = TupleVariation(support, delta_opt)
  317. axis_tags = sorted(
  318. support.keys()
  319. ) # Shouldn't matter that this is different from fvar...?
  320. tupleData, auxData = var.compile(axis_tags)
  321. unoptimized_len = len(tupleData) + len(auxData)
  322. tupleData, auxData = var_opt.compile(axis_tags)
  323. optimized_len = len(tupleData) + len(auxData)
  324. if optimized_len < unoptimized_len:
  325. var = var_opt
  326. gvar.variations[glyph].append(var)
  327. def _remove_TTHinting(font):
  328. for tag in ("cvar", "cvt ", "fpgm", "prep"):
  329. if tag in font:
  330. del font[tag]
  331. maxp = font["maxp"]
  332. for attr in (
  333. "maxTwilightPoints",
  334. "maxStorage",
  335. "maxFunctionDefs",
  336. "maxInstructionDefs",
  337. "maxStackElements",
  338. "maxSizeOfInstructions",
  339. ):
  340. setattr(maxp, attr, 0)
  341. maxp.maxZones = 1
  342. font["glyf"].removeHinting()
  343. # TODO: Modify gasp table to deactivate gridfitting for all ranges?
  344. def _merge_TTHinting(font, masterModel, master_ttfs):
  345. log.info("Merging TT hinting")
  346. assert "cvar" not in font
  347. # Check that the existing hinting is compatible
  348. # fpgm and prep table
  349. for tag in ("fpgm", "prep"):
  350. all_pgms = [m[tag].program for m in master_ttfs if tag in m]
  351. if not all_pgms:
  352. continue
  353. font_pgm = getattr(font.get(tag), "program", None)
  354. if any(pgm != font_pgm for pgm in all_pgms):
  355. log.warning(
  356. "Masters have incompatible %s tables, hinting is discarded." % tag
  357. )
  358. _remove_TTHinting(font)
  359. return
  360. # glyf table
  361. font_glyf = font["glyf"]
  362. master_glyfs = [m["glyf"] for m in master_ttfs]
  363. for name, glyph in font_glyf.glyphs.items():
  364. all_pgms = [getattr(glyf.get(name), "program", None) for glyf in master_glyfs]
  365. if not any(all_pgms):
  366. continue
  367. glyph.expand(font_glyf)
  368. font_pgm = getattr(glyph, "program", None)
  369. if any(pgm != font_pgm for pgm in all_pgms if pgm):
  370. log.warning(
  371. "Masters have incompatible glyph programs in glyph '%s', hinting is discarded."
  372. % name
  373. )
  374. # TODO Only drop hinting from this glyph.
  375. _remove_TTHinting(font)
  376. return
  377. # cvt table
  378. all_cvs = [Vector(m["cvt "].values) if "cvt " in m else None for m in master_ttfs]
  379. nonNone_cvs = models.nonNone(all_cvs)
  380. if not nonNone_cvs:
  381. # There is no cvt table to make a cvar table from, we're done here.
  382. return
  383. if not models.allEqual(len(c) for c in nonNone_cvs):
  384. log.warning("Masters have incompatible cvt tables, hinting is discarded.")
  385. _remove_TTHinting(font)
  386. return
  387. variations = []
  388. deltas, supports = masterModel.getDeltasAndSupports(
  389. all_cvs, round=round
  390. ) # builtin round calls into Vector.__round__, which uses builtin round as we like
  391. for i, (delta, support) in enumerate(zip(deltas[1:], supports[1:])):
  392. if all(v == 0 for v in delta):
  393. continue
  394. var = TupleVariation(support, delta)
  395. variations.append(var)
  396. # We can build the cvar table now.
  397. if variations:
  398. cvar = font["cvar"] = newTable("cvar")
  399. cvar.version = 1
  400. cvar.variations = variations
  401. def _has_inconsistent_use_my_metrics_flag(
  402. master_glyf, glyph_name, flagged_components, expected_num_components
  403. ) -> bool:
  404. master_glyph = master_glyf.get(glyph_name)
  405. # 'sparse' glyph master doesn't contribute. Besides when components don't match
  406. # the VF build is going to fail anyway, so be lenient here.
  407. if (
  408. master_glyph is not None
  409. and master_glyph.isComposite()
  410. and len(master_glyph.components) == expected_num_components
  411. ):
  412. for i, base_glyph in flagged_components:
  413. comp = master_glyph.components[i]
  414. if comp.glyphName != base_glyph:
  415. break
  416. if not (comp.flags & USE_MY_METRICS):
  417. return True
  418. return False
  419. def _unset_inconsistent_use_my_metrics_flags(vf, master_fonts):
  420. """Clear USE_MY_METRICS on composite components if inconsistent across masters.
  421. If a composite glyph's component has USE_MY_METRICS set differently among
  422. the masters, the flag is removed from the variable font's glyf table so that
  423. advance widths are not determined by that single component's phantom points.
  424. """
  425. glyf = vf["glyf"]
  426. master_glyfs = [m["glyf"] for m in master_fonts if "glyf" in m]
  427. if not master_glyfs:
  428. # Should not happen: at least the base master (as copied into vf) has glyf
  429. return
  430. for glyph_name in glyf.keys():
  431. glyph = glyf[glyph_name]
  432. if not glyph.isComposite():
  433. continue
  434. # collect indices of component(s) that carry the USE_MY_METRICS flag.
  435. # This is supposed to be 1 component per composite, but you never know.
  436. flagged_components = [
  437. (i, comp.glyphName)
  438. for i, comp in enumerate(glyph.components)
  439. if (comp.flags & USE_MY_METRICS)
  440. ]
  441. if not flagged_components:
  442. # Nothing to fix
  443. continue
  444. # Verify that for all master glyf tables that contribute this glyph, the
  445. # corresponding component (same glyphName and index) also carries USE_MY_METRICS
  446. # and unset the flag if not.
  447. expected_num_components = len(glyph.components)
  448. if any(
  449. _has_inconsistent_use_my_metrics_flag(
  450. master_glyf, glyph_name, flagged_components, expected_num_components
  451. )
  452. for master_glyf in master_glyfs
  453. ):
  454. comp_names = [name for _, name in flagged_components]
  455. log.info(
  456. "Composite glyph '%s' has inconsistent USE_MY_METRICS flags across "
  457. "masters; clearing the flag on component%s %s",
  458. glyph_name,
  459. "s" if len(comp_names) > 1 else "",
  460. comp_names if len(comp_names) > 1 else comp_names[0],
  461. )
  462. for i, _ in flagged_components:
  463. glyph.components[i].flags &= ~USE_MY_METRICS
  464. _MetricsFields = namedtuple(
  465. "_MetricsFields",
  466. [
  467. "tableTag",
  468. "metricsTag",
  469. "sb1",
  470. "sb2",
  471. "advMapping",
  472. "vOrigMapping",
  473. "phantomIndex",
  474. ],
  475. )
  476. HVAR_FIELDS = _MetricsFields(
  477. tableTag="HVAR",
  478. metricsTag="hmtx",
  479. sb1="LsbMap",
  480. sb2="RsbMap",
  481. advMapping="AdvWidthMap",
  482. vOrigMapping=None,
  483. phantomIndex=0,
  484. )
  485. VVAR_FIELDS = _MetricsFields(
  486. tableTag="VVAR",
  487. metricsTag="vmtx",
  488. sb1="TsbMap",
  489. sb2="BsbMap",
  490. advMapping="AdvHeightMap",
  491. vOrigMapping="VOrgMap",
  492. phantomIndex=1,
  493. )
  494. def _add_HVAR(font, masterModel, master_ttfs, axisTags):
  495. getAdvanceMetrics = partial(
  496. _get_advance_metrics, font, masterModel, master_ttfs, axisTags, HVAR_FIELDS
  497. )
  498. _add_VHVAR(font, axisTags, HVAR_FIELDS, getAdvanceMetrics)
  499. def _add_VVAR(font, masterModel, master_ttfs, axisTags):
  500. getAdvanceMetrics = partial(
  501. _get_advance_metrics, font, masterModel, master_ttfs, axisTags, VVAR_FIELDS
  502. )
  503. _add_VHVAR(font, axisTags, VVAR_FIELDS, getAdvanceMetrics)
  504. def _add_VHVAR(font, axisTags, tableFields, getAdvanceMetrics):
  505. tableTag = tableFields.tableTag
  506. assert tableTag not in font
  507. glyphOrder = font.getGlyphOrder()
  508. log.info("Generating " + tableTag)
  509. VHVAR = newTable(tableTag)
  510. tableClass = getattr(ot, tableTag)
  511. vhvar = VHVAR.table = tableClass()
  512. vhvar.Version = 0x00010000
  513. vhAdvanceDeltasAndSupports, vOrigDeltasAndSupports = getAdvanceMetrics()
  514. if vOrigDeltasAndSupports:
  515. singleModel = False
  516. else:
  517. singleModel = models.allEqual(
  518. id(v[1]) for v in vhAdvanceDeltasAndSupports.values()
  519. )
  520. directStore = None
  521. if singleModel:
  522. # Build direct mapping
  523. supports = next(iter(vhAdvanceDeltasAndSupports.values()))[1][1:]
  524. varTupleList = builder.buildVarRegionList(supports, axisTags)
  525. varTupleIndexes = list(range(len(supports)))
  526. varData = builder.buildVarData(varTupleIndexes, [], optimize=False)
  527. for glyphName in glyphOrder:
  528. varData.addItem(vhAdvanceDeltasAndSupports[glyphName][0], round=noRound)
  529. varData.optimize()
  530. directStore = builder.buildVarStore(varTupleList, [varData])
  531. # remove unused regions from VarRegionList
  532. directStore.prune_regions()
  533. # Build optimized indirect mapping
  534. storeBuilder = varStore.OnlineVarStoreBuilder(axisTags)
  535. advMapping = {}
  536. for glyphName in glyphOrder:
  537. deltas, supports = vhAdvanceDeltasAndSupports[glyphName]
  538. storeBuilder.setSupports(supports)
  539. advMapping[glyphName] = storeBuilder.storeDeltas(deltas, round=noRound)
  540. if vOrigDeltasAndSupports:
  541. vOrigMap = {}
  542. for glyphName in glyphOrder:
  543. deltas, supports = vOrigDeltasAndSupports[glyphName]
  544. storeBuilder.setSupports(supports)
  545. vOrigMap[glyphName] = storeBuilder.storeDeltas(deltas, round=noRound)
  546. indirectStore = storeBuilder.finish()
  547. mapping2 = indirectStore.optimize(use_NO_VARIATION_INDEX=False)
  548. advMapping = [mapping2[advMapping[g]] for g in glyphOrder]
  549. advanceMapping = builder.buildVarIdxMap(advMapping, glyphOrder)
  550. if vOrigDeltasAndSupports:
  551. vOrigMap = [mapping2[vOrigMap[g]] for g in glyphOrder]
  552. useDirect = False
  553. vOrigMapping = None
  554. if directStore:
  555. # Compile both, see which is more compact
  556. writer = OTTableWriter()
  557. directStore.compile(writer, font)
  558. directSize = len(writer.getAllData())
  559. writer = OTTableWriter()
  560. indirectStore.compile(writer, font)
  561. advanceMapping.compile(writer, font)
  562. indirectSize = len(writer.getAllData())
  563. useDirect = directSize < indirectSize
  564. if useDirect:
  565. metricsStore = directStore
  566. advanceMapping = None
  567. else:
  568. metricsStore = indirectStore
  569. if vOrigDeltasAndSupports:
  570. vOrigMapping = builder.buildVarIdxMap(vOrigMap, glyphOrder)
  571. vhvar.VarStore = metricsStore
  572. setattr(vhvar, tableFields.advMapping, advanceMapping)
  573. if vOrigMapping is not None:
  574. setattr(vhvar, tableFields.vOrigMapping, vOrigMapping)
  575. setattr(vhvar, tableFields.sb1, None)
  576. setattr(vhvar, tableFields.sb2, None)
  577. font[tableTag] = VHVAR
  578. return
  579. def _get_advance_metrics(font, masterModel, master_ttfs, axisTags, tableFields):
  580. tableTag = tableFields.tableTag
  581. glyphOrder = font.getGlyphOrder()
  582. # Build list of source font advance widths for each glyph
  583. metricsTag = tableFields.metricsTag
  584. advMetricses = [m[metricsTag].metrics for m in master_ttfs]
  585. # Build list of source font vertical origin coords for each glyph
  586. if tableTag == "VVAR" and "VORG" in master_ttfs[0]:
  587. vOrigMetricses = [m["VORG"].VOriginRecords for m in master_ttfs]
  588. defaultYOrigs = [m["VORG"].defaultVertOriginY for m in master_ttfs]
  589. vOrigMetricses = list(zip(vOrigMetricses, defaultYOrigs))
  590. else:
  591. vOrigMetricses = None
  592. vhAdvanceDeltasAndSupports = {}
  593. vOrigDeltasAndSupports = {}
  594. # HACK: we treat width 65535 as a sentinel value to signal that a glyph
  595. # from a non-default master should not participate in computing {H,V}VAR,
  596. # as if it were missing. Allows to variate other glyph-related data independently
  597. # from glyph metrics
  598. sparse_advance = 0xFFFF
  599. for glyph in glyphOrder:
  600. vhAdvances = [
  601. (
  602. metrics[glyph][0]
  603. if glyph in metrics and metrics[glyph][0] != sparse_advance
  604. else None
  605. )
  606. for metrics in advMetricses
  607. ]
  608. vhAdvanceDeltasAndSupports[glyph] = masterModel.getDeltasAndSupports(
  609. vhAdvances, round=round
  610. )
  611. if vOrigMetricses:
  612. for glyph in glyphOrder:
  613. # We need to supply a vOrigs tuple with non-None default values
  614. # for each glyph. vOrigMetricses contains values only for those
  615. # glyphs which have a non-default vOrig.
  616. vOrigs = [
  617. metrics[glyph] if glyph in metrics else defaultVOrig
  618. for metrics, defaultVOrig in vOrigMetricses
  619. ]
  620. vOrigDeltasAndSupports[glyph] = masterModel.getDeltasAndSupports(
  621. vOrigs, round=round
  622. )
  623. return vhAdvanceDeltasAndSupports, vOrigDeltasAndSupports
  624. def _add_MVAR(font, masterModel, master_ttfs, axisTags):
  625. log.info("Generating MVAR")
  626. store_builder = varStore.OnlineVarStoreBuilder(axisTags)
  627. records = []
  628. lastTableTag = None
  629. fontTable = None
  630. tables = None
  631. # HACK: we need to special-case post.underlineThickness and .underlinePosition
  632. # and unilaterally/arbitrarily define a sentinel value to distinguish the case
  633. # when a post table is present in a given master simply because that's where
  634. # the glyph names in TrueType must be stored, but the underline values are not
  635. # meant to be used for building MVAR's deltas. The value of -0x8000 (-36768)
  636. # the minimum FWord (int16) value, was chosen for its unlikelyhood to appear
  637. # in real-world underline position/thickness values.
  638. specialTags = {"unds": -0x8000, "undo": -0x8000}
  639. for tag, (tableTag, itemName) in sorted(MVAR_ENTRIES.items(), key=lambda kv: kv[1]):
  640. # For each tag, fetch the associated table from all fonts (or not when we are
  641. # still looking at a tag from the same tables) and set up the variation model
  642. # for them.
  643. if tableTag != lastTableTag:
  644. tables = fontTable = None
  645. if tableTag in font:
  646. fontTable = font[tableTag]
  647. tables = []
  648. for master in master_ttfs:
  649. if tableTag not in master or (
  650. tag in specialTags
  651. and getattr(master[tableTag], itemName) == specialTags[tag]
  652. ):
  653. tables.append(None)
  654. else:
  655. tables.append(master[tableTag])
  656. model, tables = masterModel.getSubModel(tables)
  657. store_builder.setModel(model)
  658. lastTableTag = tableTag
  659. if tables is None: # Tag not applicable to the master font.
  660. continue
  661. # TODO support gasp entries
  662. master_values = [getattr(table, itemName) for table in tables]
  663. if models.allEqual(master_values):
  664. base, varIdx = master_values[0], None
  665. else:
  666. base, varIdx = store_builder.storeMasters(master_values)
  667. setattr(fontTable, itemName, base)
  668. if varIdx is None:
  669. continue
  670. log.info(" %s: %s.%s %s", tag, tableTag, itemName, master_values)
  671. rec = ot.MetricsValueRecord()
  672. rec.ValueTag = tag
  673. rec.VarIdx = varIdx
  674. records.append(rec)
  675. assert "MVAR" not in font
  676. if records:
  677. store = store_builder.finish()
  678. # Optimize
  679. mapping = store.optimize()
  680. for rec in records:
  681. rec.VarIdx = mapping[rec.VarIdx]
  682. MVAR = font["MVAR"] = newTable("MVAR")
  683. mvar = MVAR.table = ot.MVAR()
  684. mvar.Version = 0x00010000
  685. mvar.Reserved = 0
  686. mvar.VarStore = store
  687. # XXX these should not be hard-coded but computed automatically
  688. mvar.ValueRecordSize = 8
  689. mvar.ValueRecordCount = len(records)
  690. mvar.ValueRecord = sorted(records, key=lambda r: r.ValueTag)
  691. def _add_BASE(font, masterModel, master_ttfs, axisTags):
  692. log.info("Generating BASE")
  693. merger = VariationMerger(masterModel, axisTags, font)
  694. merger.mergeTables(font, master_ttfs, ["BASE"])
  695. store = merger.store_builder.finish()
  696. if not store:
  697. return
  698. base = font["BASE"].table
  699. assert base.Version == 0x00010000
  700. base.Version = 0x00010001
  701. base.VarStore = store
  702. def _merge_OTL(font, model, master_fonts, axisTags):
  703. otl_tags = ["GSUB", "GDEF", "GPOS"]
  704. if not any(tag in font for tag in otl_tags):
  705. return
  706. log.info("Merging OpenType Layout tables")
  707. merger = VariationMerger(model, axisTags, font)
  708. merger.mergeTables(font, master_fonts, otl_tags)
  709. store = merger.store_builder.finish()
  710. if not store:
  711. return
  712. try:
  713. GDEF = font["GDEF"].table
  714. assert GDEF.Version <= 0x00010002
  715. except KeyError:
  716. font["GDEF"] = newTable("GDEF")
  717. GDEFTable = font["GDEF"] = newTable("GDEF")
  718. GDEF = GDEFTable.table = ot.GDEF()
  719. GDEF.GlyphClassDef = None
  720. GDEF.AttachList = None
  721. GDEF.LigCaretList = None
  722. GDEF.MarkAttachClassDef = None
  723. GDEF.MarkGlyphSetsDef = None
  724. GDEF.Version = 0x00010003
  725. GDEF.VarStore = store
  726. # Optimize
  727. varidx_map = store.optimize()
  728. GDEF.remap_device_varidxes(varidx_map)
  729. if "GPOS" in font:
  730. font["GPOS"].table.remap_device_varidxes(varidx_map)
  731. def _add_GSUB_feature_variations(
  732. font, axes, internal_axis_supports, rules, featureTags
  733. ):
  734. def normalize(name, value):
  735. return models.normalizeLocation({name: value}, internal_axis_supports)[name]
  736. log.info("Generating GSUB FeatureVariations")
  737. axis_tags = {name: axis.tag for name, axis in axes.items()}
  738. conditional_subs = []
  739. for rule in rules:
  740. region = []
  741. for conditions in rule.conditionSets:
  742. space = {}
  743. for condition in conditions:
  744. axis_name = condition["name"]
  745. if condition["minimum"] is not None:
  746. minimum = normalize(axis_name, condition["minimum"])
  747. else:
  748. minimum = -1.0
  749. if condition["maximum"] is not None:
  750. maximum = normalize(axis_name, condition["maximum"])
  751. else:
  752. maximum = 1.0
  753. tag = axis_tags[axis_name]
  754. space[tag] = (minimum, maximum)
  755. region.append(space)
  756. subs = {k: v for k, v in rule.subs}
  757. conditional_subs.append((region, subs))
  758. addFeatureVariations(font, conditional_subs, featureTags)
  759. _DesignSpaceData = namedtuple(
  760. "_DesignSpaceData",
  761. [
  762. "axes",
  763. "axisMappings",
  764. "internal_axis_supports",
  765. "base_idx",
  766. "normalized_master_locs",
  767. "masters",
  768. "instances",
  769. "rules",
  770. "rulesProcessingLast",
  771. "lib",
  772. ],
  773. )
  774. def _add_CFF2(varFont, model, master_fonts):
  775. from .cff import merge_region_fonts
  776. glyphOrder = varFont.getGlyphOrder()
  777. if "CFF2" not in varFont:
  778. from fontTools.cffLib.CFFToCFF2 import convertCFFToCFF2
  779. convertCFFToCFF2(varFont)
  780. ordered_fonts_list = model.reorderMasters(master_fonts, model.reverseMapping)
  781. # re-ordering the master list simplifies building the CFF2 data item lists.
  782. merge_region_fonts(varFont, model, ordered_fonts_list, glyphOrder)
  783. def _add_COLR(font, model, master_fonts, axisTags, colr_layer_reuse=True):
  784. merger = COLRVariationMerger(
  785. model, axisTags, font, allowLayerReuse=colr_layer_reuse
  786. )
  787. merger.mergeTables(font, master_fonts)
  788. store = merger.store_builder.finish()
  789. colr = font["COLR"].table
  790. if store:
  791. mapping = store.optimize()
  792. colr.VarStore = store
  793. varIdxes = [mapping[v] for v in merger.varIdxes]
  794. colr.VarIndexMap = builder.buildDeltaSetIndexMap(varIdxes)
  795. def load_designspace(designspace, log_enabled=True, *, require_sources=True):
  796. # TODO: remove this and always assume 'designspace' is a DesignSpaceDocument,
  797. # never a file path, as that's already handled by caller
  798. if hasattr(designspace, "sources"): # Assume a DesignspaceDocument
  799. ds = designspace
  800. else: # Assume a file path
  801. ds = DesignSpaceDocument.fromfile(designspace)
  802. masters = ds.sources
  803. if require_sources and not masters:
  804. raise VarLibValidationError("Designspace must have at least one source.")
  805. instances = ds.instances
  806. # TODO: Use fontTools.designspaceLib.tagForAxisName instead.
  807. standard_axis_map = OrderedDict(
  808. [
  809. ("weight", ("wght", {"en": "Weight"})),
  810. ("width", ("wdth", {"en": "Width"})),
  811. ("slant", ("slnt", {"en": "Slant"})),
  812. ("optical", ("opsz", {"en": "Optical Size"})),
  813. ("italic", ("ital", {"en": "Italic"})),
  814. ]
  815. )
  816. # Setup axes
  817. if not ds.axes:
  818. raise VarLibValidationError(f"Designspace must have at least one axis.")
  819. axes = OrderedDict()
  820. for axis_index, axis in enumerate(ds.axes):
  821. axis_name = axis.name
  822. if not axis_name:
  823. if not axis.tag:
  824. raise VarLibValidationError(f"Axis at index {axis_index} needs a tag.")
  825. axis_name = axis.name = axis.tag
  826. if axis_name in standard_axis_map:
  827. if axis.tag is None:
  828. axis.tag = standard_axis_map[axis_name][0]
  829. if not axis.labelNames:
  830. axis.labelNames.update(standard_axis_map[axis_name][1])
  831. else:
  832. if not axis.tag:
  833. raise VarLibValidationError(f"Axis at index {axis_index} needs a tag.")
  834. if not axis.labelNames:
  835. axis.labelNames["en"] = tostr(axis_name)
  836. axes[axis_name] = axis
  837. if log_enabled:
  838. log.info("Axes:\n%s", pformat([axis.asdict() for axis in axes.values()]))
  839. axisMappings = ds.axisMappings
  840. if axisMappings and log_enabled:
  841. log.info("Mappings:\n%s", pformat(axisMappings))
  842. # Check all master and instance locations are valid and fill in defaults
  843. for obj in masters + instances:
  844. obj_name = obj.name or obj.styleName or ""
  845. loc = obj.getFullDesignLocation(ds)
  846. obj.designLocation = loc
  847. if loc is None:
  848. raise VarLibValidationError(
  849. f"Source or instance '{obj_name}' has no location."
  850. )
  851. for axis_name in loc.keys():
  852. if axis_name not in axes:
  853. raise VarLibValidationError(
  854. f"Location axis '{axis_name}' unknown for '{obj_name}'."
  855. )
  856. for axis_name, axis in axes.items():
  857. v = axis.map_backward(loc[axis_name])
  858. if not (axis.minimum <= v <= axis.maximum):
  859. raise VarLibValidationError(
  860. f"Source or instance '{obj_name}' has out-of-range location "
  861. f"for axis '{axis_name}': is mapped to {v} but must be in "
  862. f"mapped range [{axis.minimum}..{axis.maximum}] (NOTE: all "
  863. "values are in user-space)."
  864. )
  865. # Normalize master locations
  866. internal_master_locs = [o.getFullDesignLocation(ds) for o in masters]
  867. if log_enabled:
  868. log.info("Internal master locations:\n%s", pformat(internal_master_locs))
  869. # TODO This mapping should ideally be moved closer to logic in _add_fvar/avar
  870. internal_axis_supports = {}
  871. for axis in axes.values():
  872. triple = (axis.minimum, axis.default, axis.maximum)
  873. internal_axis_supports[axis.name] = [axis.map_forward(v) for v in triple]
  874. if log_enabled:
  875. log.info("Internal axis supports:\n%s", pformat(internal_axis_supports))
  876. normalized_master_locs = [
  877. models.normalizeLocation(m, internal_axis_supports)
  878. for m in internal_master_locs
  879. ]
  880. if log_enabled:
  881. log.info("Normalized master locations:\n%s", pformat(normalized_master_locs))
  882. # Find base master
  883. base_idx = None
  884. for i, m in enumerate(normalized_master_locs):
  885. if all(v == 0 for v in m.values()):
  886. if base_idx is not None:
  887. raise VarLibValidationError(
  888. "More than one base master found in Designspace."
  889. )
  890. base_idx = i
  891. if require_sources and base_idx is None:
  892. raise VarLibValidationError(
  893. "Base master not found; no master at default location?"
  894. )
  895. if log_enabled:
  896. log.info("Index of base master: %s", base_idx)
  897. return _DesignSpaceData(
  898. axes,
  899. axisMappings,
  900. internal_axis_supports,
  901. base_idx,
  902. normalized_master_locs,
  903. masters,
  904. instances,
  905. ds.rules,
  906. ds.rulesProcessingLast,
  907. ds.lib,
  908. )
  909. # https://docs.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass
  910. WDTH_VALUE_TO_OS2_WIDTH_CLASS = {
  911. 50: 1,
  912. 62.5: 2,
  913. 75: 3,
  914. 87.5: 4,
  915. 100: 5,
  916. 112.5: 6,
  917. 125: 7,
  918. 150: 8,
  919. 200: 9,
  920. }
  921. def set_default_weight_width_slant(font, location):
  922. if "OS/2" in font:
  923. if "wght" in location:
  924. weight_class = otRound(max(1, min(location["wght"], 1000)))
  925. if font["OS/2"].usWeightClass != weight_class:
  926. log.info("Setting OS/2.usWeightClass = %s", weight_class)
  927. font["OS/2"].usWeightClass = weight_class
  928. if "wdth" in location:
  929. # map 'wdth' axis (50..200) to OS/2.usWidthClass (1..9), rounding to closest
  930. widthValue = min(max(location["wdth"], 50), 200)
  931. widthClass = otRound(
  932. models.piecewiseLinearMap(widthValue, WDTH_VALUE_TO_OS2_WIDTH_CLASS)
  933. )
  934. if font["OS/2"].usWidthClass != widthClass:
  935. log.info("Setting OS/2.usWidthClass = %s", widthClass)
  936. font["OS/2"].usWidthClass = widthClass
  937. if "slnt" in location and "post" in font:
  938. italicAngle = max(-90, min(location["slnt"], 90))
  939. if font["post"].italicAngle != italicAngle:
  940. log.info("Setting post.italicAngle = %s", italicAngle)
  941. font["post"].italicAngle = italicAngle
  942. def drop_implied_oncurve_points(*masters: TTFont) -> int:
  943. """Drop impliable on-curve points from all the simple glyphs in masters.
  944. In TrueType glyf outlines, on-curve points can be implied when they are located
  945. exactly at the midpoint of the line connecting two consecutive off-curve points.
  946. The input masters' glyf tables are assumed to contain same-named glyphs that are
  947. interpolatable. Oncurve points are only dropped if they can be implied for all
  948. the masters. The fonts are modified in-place.
  949. Args:
  950. masters: The TTFont(s) to modify
  951. Returns:
  952. The total number of points that were dropped if any.
  953. Reference:
  954. https://developer.apple.com/fonts/TrueType-Reference-Manual/RM01/Chap1.html
  955. """
  956. count = 0
  957. glyph_masters = defaultdict(list)
  958. # multiple DS source may point to the same TTFont object and we want to
  959. # avoid processing the same glyph twice as they are modified in-place
  960. for font in {id(m): m for m in masters}.values():
  961. glyf = font["glyf"]
  962. for glyphName in glyf.keys():
  963. glyph_masters[glyphName].append(glyf[glyphName])
  964. count = 0
  965. for glyphName, glyphs in glyph_masters.items():
  966. try:
  967. dropped = dropImpliedOnCurvePoints(*glyphs)
  968. except ValueError as e:
  969. # we don't fail for incompatible glyphs in _add_gvar so we shouldn't here
  970. log.warning("Failed to drop implied oncurves for %r: %s", glyphName, e)
  971. else:
  972. count += len(dropped)
  973. return count
  974. def build_many(
  975. designspace: DesignSpaceDocument,
  976. master_finder=lambda s: s,
  977. exclude=[],
  978. optimize=True,
  979. skip_vf=lambda vf_name: False,
  980. colr_layer_reuse=True,
  981. drop_implied_oncurves=False,
  982. ):
  983. """
  984. Build variable fonts from a designspace file, version 5 which can define
  985. several VFs, or version 4 which has implicitly one VF covering the whole doc.
  986. If master_finder is set, it should be a callable that takes master
  987. filename as found in designspace file and map it to master font
  988. binary as to be opened (eg. .ttf or .otf).
  989. skip_vf can be used to skip building some of the variable fonts defined in
  990. the input designspace. It's a predicate that takes as argument the name
  991. of the variable font and returns `bool`.
  992. Always returns a Dict[str, TTFont] keyed by VariableFontDescriptor.name
  993. """
  994. res = {}
  995. # varLib.build (used further below) by default only builds an incomplete 'STAT'
  996. # with an empty AxisValueArray--unless the VF inherited 'STAT' from its base master.
  997. # Designspace version 5 can also be used to define 'STAT' labels or customize
  998. # axes ordering, etc. To avoid overwriting a pre-existing 'STAT' or redoing the
  999. # same work twice, here we check if designspace contains any 'STAT' info before
  1000. # proceeding to call buildVFStatTable for each VF.
  1001. # https://github.com/fonttools/fonttools/pull/3024
  1002. # https://github.com/fonttools/fonttools/issues/3045
  1003. doBuildStatFromDSv5 = (
  1004. "STAT" not in exclude
  1005. and designspace.formatTuple >= (5, 0)
  1006. and (
  1007. any(a.axisLabels or a.axisOrdering is not None for a in designspace.axes)
  1008. or designspace.locationLabels
  1009. )
  1010. )
  1011. for _location, subDoc in splitInterpolable(designspace):
  1012. for name, vfDoc in splitVariableFonts(subDoc):
  1013. if skip_vf(name):
  1014. log.debug(f"Skipping variable TTF font: {name}")
  1015. continue
  1016. vf = build(
  1017. vfDoc,
  1018. master_finder,
  1019. exclude=exclude,
  1020. optimize=optimize,
  1021. colr_layer_reuse=colr_layer_reuse,
  1022. drop_implied_oncurves=drop_implied_oncurves,
  1023. )[0]
  1024. if doBuildStatFromDSv5:
  1025. buildVFStatTable(vf, designspace, name)
  1026. res[name] = vf
  1027. return res
  1028. def build(
  1029. designspace,
  1030. master_finder=lambda s: s,
  1031. exclude=[],
  1032. optimize=True,
  1033. colr_layer_reuse=True,
  1034. drop_implied_oncurves=False,
  1035. ):
  1036. """
  1037. Build variation font from a designspace file.
  1038. If master_finder is set, it should be a callable that takes master
  1039. filename as found in designspace file and map it to master font
  1040. binary as to be opened (eg. .ttf or .otf).
  1041. """
  1042. if hasattr(designspace, "sources"): # Assume a DesignspaceDocument
  1043. pass
  1044. else: # Assume a file path
  1045. designspace = DesignSpaceDocument.fromfile(designspace)
  1046. ds = load_designspace(designspace)
  1047. log.info("Building variable font")
  1048. log.info("Loading master fonts")
  1049. master_fonts = load_masters(designspace, master_finder)
  1050. # TODO: 'master_ttfs' is unused except for return value, remove later
  1051. master_ttfs = []
  1052. for master in master_fonts:
  1053. try:
  1054. master_ttfs.append(master.reader.file.name)
  1055. except AttributeError:
  1056. master_ttfs.append(None) # in-memory fonts have no path
  1057. if drop_implied_oncurves and "glyf" in master_fonts[ds.base_idx]:
  1058. drop_count = drop_implied_oncurve_points(*master_fonts)
  1059. log.info(
  1060. "Dropped %s on-curve points from simple glyphs in the 'glyf' table",
  1061. drop_count,
  1062. )
  1063. # Copy the base master to work from it
  1064. vf = deepcopy(master_fonts[ds.base_idx])
  1065. if "DSIG" in vf:
  1066. del vf["DSIG"]
  1067. # Clear USE_MY_METRICS composite flags if set inconsistently across masters.
  1068. if "glyf" in vf:
  1069. _unset_inconsistent_use_my_metrics_flags(vf, master_fonts)
  1070. # TODO append masters as named-instances as well; needs .designspace change.
  1071. fvar = _add_fvar(vf, ds.axes, ds.instances)
  1072. if "STAT" not in exclude:
  1073. _add_stat(vf)
  1074. # Map from axis names to axis tags...
  1075. normalized_master_locs = [
  1076. {ds.axes[k].tag: v for k, v in loc.items()} for loc in ds.normalized_master_locs
  1077. ]
  1078. # From here on, we use fvar axes only
  1079. axisTags = [axis.axisTag for axis in fvar.axes]
  1080. # Assume single-model for now.
  1081. model = models.VariationModel(normalized_master_locs, axisOrder=axisTags)
  1082. assert 0 == model.mapping[ds.base_idx]
  1083. log.info("Building variations tables")
  1084. if "avar" not in exclude:
  1085. _add_avar(vf, ds.axes, ds.axisMappings, axisTags)
  1086. if "BASE" not in exclude and "BASE" in vf:
  1087. _add_BASE(vf, model, master_fonts, axisTags)
  1088. if "MVAR" not in exclude:
  1089. _add_MVAR(vf, model, master_fonts, axisTags)
  1090. if "HVAR" not in exclude:
  1091. _add_HVAR(vf, model, master_fonts, axisTags)
  1092. if "VVAR" not in exclude and "vmtx" in vf:
  1093. _add_VVAR(vf, model, master_fonts, axisTags)
  1094. if "GDEF" not in exclude or "GPOS" not in exclude:
  1095. _merge_OTL(vf, model, master_fonts, axisTags)
  1096. if "gvar" not in exclude and "glyf" in vf:
  1097. _add_gvar(vf, model, master_fonts, optimize=optimize)
  1098. if "cvar" not in exclude and "glyf" in vf:
  1099. _merge_TTHinting(vf, model, master_fonts)
  1100. if "GSUB" not in exclude and ds.rules:
  1101. featureTags = _feature_variations_tags(ds)
  1102. _add_GSUB_feature_variations(
  1103. vf, ds.axes, ds.internal_axis_supports, ds.rules, featureTags
  1104. )
  1105. if "CFF2" not in exclude and ("CFF " in vf or "CFF2" in vf):
  1106. _add_CFF2(vf, model, master_fonts)
  1107. if "post" in vf:
  1108. # set 'post' to format 2 to keep the glyph names dropped from CFF2
  1109. post = vf["post"]
  1110. if post.formatType != 2.0:
  1111. post.formatType = 2.0
  1112. post.extraNames = []
  1113. post.mapping = {}
  1114. if "COLR" not in exclude and "COLR" in vf and vf["COLR"].version > 0:
  1115. _add_COLR(vf, model, master_fonts, axisTags, colr_layer_reuse)
  1116. set_default_weight_width_slant(
  1117. vf, location={axis.axisTag: axis.defaultValue for axis in vf["fvar"].axes}
  1118. )
  1119. for tag in exclude:
  1120. if tag in vf:
  1121. del vf[tag]
  1122. # TODO: Only return vf for 4.0+, the rest is unused.
  1123. return vf, model, master_ttfs
  1124. def _open_font(path, master_finder=lambda s: s):
  1125. # load TTFont masters from given 'path': this can be either a .TTX or an
  1126. # OpenType binary font; or if neither of these, try use the 'master_finder'
  1127. # callable to resolve the path to a valid .TTX or OpenType font binary.
  1128. from fontTools.ttx import guessFileType
  1129. master_path = os.path.normpath(path)
  1130. tp = guessFileType(master_path)
  1131. if tp is None:
  1132. # not an OpenType binary/ttx, fall back to the master finder.
  1133. master_path = master_finder(master_path)
  1134. tp = guessFileType(master_path)
  1135. if tp in ("TTX", "OTX"):
  1136. font = TTFont()
  1137. font.importXML(master_path)
  1138. elif tp in ("TTF", "OTF", "WOFF", "WOFF2"):
  1139. font = TTFont(master_path)
  1140. else:
  1141. raise VarLibValidationError("Invalid master path: %r" % master_path)
  1142. return font
  1143. def load_masters(designspace, master_finder=lambda s: s):
  1144. """Ensure that all SourceDescriptor.font attributes have an appropriate TTFont
  1145. object loaded, or else open TTFont objects from the SourceDescriptor.path
  1146. attributes.
  1147. The paths can point to either an OpenType font, a TTX file, or a UFO. In the
  1148. latter case, use the provided master_finder callable to map from UFO paths to
  1149. the respective master font binaries (e.g. .ttf, .otf or .ttx).
  1150. Return list of master TTFont objects in the same order they are listed in the
  1151. DesignSpaceDocument.
  1152. """
  1153. for master in designspace.sources:
  1154. # If a SourceDescriptor has a layer name, demand that the compiled TTFont
  1155. # be supplied by the caller. This spares us from modifying MasterFinder.
  1156. if master.layerName and master.font is None:
  1157. raise VarLibValidationError(
  1158. f"Designspace source '{master.name or '<Unknown>'}' specified a "
  1159. "layer name but lacks the required TTFont object in the 'font' "
  1160. "attribute."
  1161. )
  1162. return designspace.loadSourceFonts(_open_font, master_finder=master_finder)
  1163. class MasterFinder(object):
  1164. def __init__(self, template):
  1165. self.template = template
  1166. def __call__(self, src_path):
  1167. fullname = os.path.abspath(src_path)
  1168. dirname, basename = os.path.split(fullname)
  1169. stem, ext = os.path.splitext(basename)
  1170. path = self.template.format(
  1171. fullname=fullname,
  1172. dirname=dirname,
  1173. basename=basename,
  1174. stem=stem,
  1175. ext=ext,
  1176. )
  1177. return os.path.normpath(path)
  1178. def _feature_variations_tags(ds):
  1179. raw_tags = ds.lib.get(
  1180. FEAVAR_FEATURETAG_LIB_KEY,
  1181. "rclt" if ds.rulesProcessingLast else "rvrn",
  1182. )
  1183. return sorted({t.strip() for t in raw_tags.split(",")})
  1184. def addGSUBFeatureVariations(vf, designspace, featureTags=(), *, log_enabled=False):
  1185. """Add GSUB FeatureVariations table to variable font, based on DesignSpace rules.
  1186. Args:
  1187. vf: A TTFont object representing the variable font.
  1188. designspace: A DesignSpaceDocument object.
  1189. featureTags: Optional feature tag(s) to use for the FeatureVariations records.
  1190. If unset, the key 'com.github.fonttools.varLib.featureVarsFeatureTag' is
  1191. looked up in the DS <lib> and used; otherwise the default is 'rclt' if
  1192. the <rules processing="last"> attribute is set, else 'rvrn'.
  1193. See <https://fonttools.readthedocs.io/en/latest/designspaceLib/xml.html#rules-element>
  1194. log_enabled: If True, log info about DS axes and sources. Default is False, as
  1195. the same info may have already been logged as part of varLib.build.
  1196. """
  1197. ds = load_designspace(designspace, log_enabled=log_enabled)
  1198. if not ds.rules:
  1199. return
  1200. if not featureTags:
  1201. featureTags = _feature_variations_tags(ds)
  1202. _add_GSUB_feature_variations(
  1203. vf, ds.axes, ds.internal_axis_supports, ds.rules, featureTags
  1204. )
  1205. def main(args=None):
  1206. """Build variable fonts from a designspace file and masters"""
  1207. from argparse import ArgumentParser
  1208. from fontTools import configLogger
  1209. parser = ArgumentParser(prog="varLib", description=main.__doc__)
  1210. parser.add_argument("designspace")
  1211. output_group = parser.add_mutually_exclusive_group()
  1212. output_group.add_argument(
  1213. "-o", metavar="OUTPUTFILE", dest="outfile", default=None, help="output file"
  1214. )
  1215. output_group.add_argument(
  1216. "-d",
  1217. "--output-dir",
  1218. metavar="OUTPUTDIR",
  1219. default=None,
  1220. help="output dir (default: same as input designspace file)",
  1221. )
  1222. parser.add_argument(
  1223. "-x",
  1224. metavar="TAG",
  1225. dest="exclude",
  1226. action="append",
  1227. default=[],
  1228. help="exclude table",
  1229. )
  1230. parser.add_argument(
  1231. "--disable-iup",
  1232. dest="optimize",
  1233. action="store_false",
  1234. help="do not perform IUP optimization",
  1235. )
  1236. parser.add_argument(
  1237. "--no-colr-layer-reuse",
  1238. dest="colr_layer_reuse",
  1239. action="store_false",
  1240. help="do not rebuild variable COLR table to optimize COLR layer reuse",
  1241. )
  1242. parser.add_argument(
  1243. "--drop-implied-oncurves",
  1244. action="store_true",
  1245. help=(
  1246. "drop on-curve points that can be implied when exactly in the middle of "
  1247. "two off-curve points (only applies to TrueType fonts)"
  1248. ),
  1249. )
  1250. parser.add_argument(
  1251. "--master-finder",
  1252. default="master_ttf_interpolatable/{stem}.ttf",
  1253. help=(
  1254. "templated string used for finding binary font "
  1255. "files given the source file names defined in the "
  1256. "designspace document. The following special strings "
  1257. "are defined: {fullname} is the absolute source file "
  1258. "name; {basename} is the file name without its "
  1259. "directory; {stem} is the basename without the file "
  1260. "extension; {ext} is the source file extension; "
  1261. "{dirname} is the directory of the absolute file "
  1262. 'name. The default value is "%(default)s".'
  1263. ),
  1264. )
  1265. parser.add_argument(
  1266. "--variable-fonts",
  1267. default=".*",
  1268. metavar="VF_NAME",
  1269. help=(
  1270. "Filter the list of variable fonts produced from the input "
  1271. "Designspace v5 file. By default all listed variable fonts are "
  1272. "generated. To generate a specific variable font (or variable fonts) "
  1273. 'that match a given "name" attribute, you can pass as argument '
  1274. "the full name or a regular expression. E.g.: --variable-fonts "
  1275. '"MyFontVF_WeightOnly"; or --variable-fonts "MyFontVFItalic_.*".'
  1276. ),
  1277. )
  1278. logging_group = parser.add_mutually_exclusive_group(required=False)
  1279. logging_group.add_argument(
  1280. "-v", "--verbose", action="store_true", help="Run more verbosely."
  1281. )
  1282. logging_group.add_argument(
  1283. "-q", "--quiet", action="store_true", help="Turn verbosity off."
  1284. )
  1285. options = parser.parse_args(args)
  1286. configLogger(
  1287. level=("DEBUG" if options.verbose else "ERROR" if options.quiet else "INFO")
  1288. )
  1289. designspace_filename = options.designspace
  1290. designspace = DesignSpaceDocument.fromfile(designspace_filename)
  1291. vf_descriptors = designspace.getVariableFonts()
  1292. if not vf_descriptors:
  1293. parser.error(f"No variable fonts in given designspace {designspace.path!r}")
  1294. vfs_to_build = []
  1295. for vf in vf_descriptors:
  1296. # Skip variable fonts that do not match the user's inclusion regex if given.
  1297. if not fullmatch(options.variable_fonts, vf.name):
  1298. continue
  1299. vfs_to_build.append(vf)
  1300. if not vfs_to_build:
  1301. parser.error(f"No variable fonts matching {options.variable_fonts!r}")
  1302. if options.outfile is not None and len(vfs_to_build) > 1:
  1303. parser.error(
  1304. "can't specify -o because there are multiple VFs to build; "
  1305. "use --output-dir, or select a single VF with --variable-fonts"
  1306. )
  1307. output_dir = options.output_dir
  1308. if output_dir is None:
  1309. output_dir = os.path.dirname(designspace_filename)
  1310. vf_name_to_output_path = {}
  1311. if len(vfs_to_build) == 1 and options.outfile is not None:
  1312. vf_name_to_output_path[vfs_to_build[0].name] = options.outfile
  1313. else:
  1314. for vf in vfs_to_build:
  1315. if vf.filename is not None:
  1316. # Only use basename to prevent path traversal attacks
  1317. filename = os.path.basename(vf.filename)
  1318. else:
  1319. filename = vf.name + ".{ext}"
  1320. vf_name_to_output_path[vf.name] = os.path.join(output_dir, filename)
  1321. finder = MasterFinder(options.master_finder)
  1322. vfs = build_many(
  1323. designspace,
  1324. finder,
  1325. exclude=options.exclude,
  1326. optimize=options.optimize,
  1327. colr_layer_reuse=options.colr_layer_reuse,
  1328. drop_implied_oncurves=options.drop_implied_oncurves,
  1329. )
  1330. for vf_name, vf in vfs.items():
  1331. ext = "otf" if vf.sfntVersion == "OTTO" else "ttf"
  1332. output_path = vf_name_to_output_path[vf_name].format(ext=ext)
  1333. output_dir = os.path.dirname(output_path)
  1334. if output_dir:
  1335. os.makedirs(output_dir, exist_ok=True)
  1336. log.info("Saving variation font %s", output_path)
  1337. vf.save(output_path)
  1338. if __name__ == "__main__":
  1339. import sys
  1340. if len(sys.argv) > 1:
  1341. sys.exit(main())
  1342. import doctest
  1343. sys.exit(doctest.testmod().failed)