__init__.py 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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. var.optimize(origCoords, endPts, tolerance=tolerance)
  312. gvar.variations[glyph].append(var)
  313. def _remove_TTHinting(font):
  314. for tag in ("cvar", "cvt ", "fpgm", "prep"):
  315. if tag in font:
  316. del font[tag]
  317. maxp = font["maxp"]
  318. for attr in (
  319. "maxTwilightPoints",
  320. "maxStorage",
  321. "maxFunctionDefs",
  322. "maxInstructionDefs",
  323. "maxStackElements",
  324. "maxSizeOfInstructions",
  325. ):
  326. setattr(maxp, attr, 0)
  327. maxp.maxZones = 1
  328. font["glyf"].removeHinting()
  329. # TODO: Modify gasp table to deactivate gridfitting for all ranges?
  330. def _merge_TTHinting(font, masterModel, master_ttfs):
  331. log.info("Merging TT hinting")
  332. assert "cvar" not in font
  333. # Check that the existing hinting is compatible
  334. # fpgm and prep table
  335. for tag in ("fpgm", "prep"):
  336. all_pgms = [m[tag].program for m in master_ttfs if tag in m]
  337. if not all_pgms:
  338. continue
  339. font_pgm = getattr(font.get(tag), "program", None)
  340. if any(pgm != font_pgm for pgm in all_pgms):
  341. log.warning(
  342. "Masters have incompatible %s tables, hinting is discarded." % tag
  343. )
  344. _remove_TTHinting(font)
  345. return
  346. # glyf table
  347. font_glyf = font["glyf"]
  348. master_glyfs = [m["glyf"] for m in master_ttfs]
  349. for name, glyph in font_glyf.glyphs.items():
  350. all_pgms = [getattr(glyf.get(name), "program", None) for glyf in master_glyfs]
  351. if not any(all_pgms):
  352. continue
  353. glyph.expand(font_glyf)
  354. font_pgm = getattr(glyph, "program", None)
  355. if any(pgm != font_pgm for pgm in all_pgms if pgm):
  356. log.warning(
  357. "Masters have incompatible glyph programs in glyph '%s', hinting is discarded."
  358. % name
  359. )
  360. # TODO Only drop hinting from this glyph.
  361. _remove_TTHinting(font)
  362. return
  363. # cvt table
  364. all_cvs = [Vector(m["cvt "].values) if "cvt " in m else None for m in master_ttfs]
  365. nonNone_cvs = models.nonNone(all_cvs)
  366. if not nonNone_cvs:
  367. # There is no cvt table to make a cvar table from, we're done here.
  368. return
  369. if not models.allEqual(len(c) for c in nonNone_cvs):
  370. log.warning("Masters have incompatible cvt tables, hinting is discarded.")
  371. _remove_TTHinting(font)
  372. return
  373. variations = []
  374. deltas, supports = masterModel.getDeltasAndSupports(
  375. all_cvs, round=round
  376. ) # builtin round calls into Vector.__round__, which uses builtin round as we like
  377. for i, (delta, support) in enumerate(zip(deltas[1:], supports[1:])):
  378. if all(v == 0 for v in delta):
  379. continue
  380. var = TupleVariation(support, delta)
  381. variations.append(var)
  382. # We can build the cvar table now.
  383. if variations:
  384. cvar = font["cvar"] = newTable("cvar")
  385. cvar.version = 1
  386. cvar.variations = variations
  387. def _has_inconsistent_use_my_metrics_flag(
  388. master_glyf, glyph_name, flagged_components, expected_num_components
  389. ) -> bool:
  390. master_glyph = master_glyf.get(glyph_name)
  391. # 'sparse' glyph master doesn't contribute. Besides when components don't match
  392. # the VF build is going to fail anyway, so be lenient here.
  393. if (
  394. master_glyph is not None
  395. and master_glyph.isComposite()
  396. and len(master_glyph.components) == expected_num_components
  397. ):
  398. for i, base_glyph in flagged_components:
  399. comp = master_glyph.components[i]
  400. if comp.glyphName != base_glyph:
  401. break
  402. if not (comp.flags & USE_MY_METRICS):
  403. return True
  404. return False
  405. def _unset_inconsistent_use_my_metrics_flags(vf, master_fonts):
  406. """Clear USE_MY_METRICS on composite components if inconsistent across masters.
  407. If a composite glyph's component has USE_MY_METRICS set differently among
  408. the masters, the flag is removed from the variable font's glyf table so that
  409. advance widths are not determined by that single component's phantom points.
  410. """
  411. glyf = vf["glyf"]
  412. master_glyfs = [m["glyf"] for m in master_fonts if "glyf" in m]
  413. if not master_glyfs:
  414. # Should not happen: at least the base master (as copied into vf) has glyf
  415. return
  416. for glyph_name in glyf.keys():
  417. glyph = glyf[glyph_name]
  418. if not glyph.isComposite():
  419. continue
  420. # collect indices of component(s) that carry the USE_MY_METRICS flag.
  421. # This is supposed to be 1 component per composite, but you never know.
  422. flagged_components = [
  423. (i, comp.glyphName)
  424. for i, comp in enumerate(glyph.components)
  425. if (comp.flags & USE_MY_METRICS)
  426. ]
  427. if not flagged_components:
  428. # Nothing to fix
  429. continue
  430. # Verify that for all master glyf tables that contribute this glyph, the
  431. # corresponding component (same glyphName and index) also carries USE_MY_METRICS
  432. # and unset the flag if not.
  433. expected_num_components = len(glyph.components)
  434. if any(
  435. _has_inconsistent_use_my_metrics_flag(
  436. master_glyf, glyph_name, flagged_components, expected_num_components
  437. )
  438. for master_glyf in master_glyfs
  439. ):
  440. comp_names = [name for _, name in flagged_components]
  441. log.info(
  442. "Composite glyph '%s' has inconsistent USE_MY_METRICS flags across "
  443. "masters; clearing the flag on component%s %s",
  444. glyph_name,
  445. "s" if len(comp_names) > 1 else "",
  446. comp_names if len(comp_names) > 1 else comp_names[0],
  447. )
  448. for i, _ in flagged_components:
  449. glyph.components[i].flags &= ~USE_MY_METRICS
  450. _MetricsFields = namedtuple(
  451. "_MetricsFields",
  452. [
  453. "tableTag",
  454. "metricsTag",
  455. "sb1",
  456. "sb2",
  457. "advMapping",
  458. "vOrigMapping",
  459. "phantomIndex",
  460. ],
  461. )
  462. HVAR_FIELDS = _MetricsFields(
  463. tableTag="HVAR",
  464. metricsTag="hmtx",
  465. sb1="LsbMap",
  466. sb2="RsbMap",
  467. advMapping="AdvWidthMap",
  468. vOrigMapping=None,
  469. phantomIndex=0,
  470. )
  471. VVAR_FIELDS = _MetricsFields(
  472. tableTag="VVAR",
  473. metricsTag="vmtx",
  474. sb1="TsbMap",
  475. sb2="BsbMap",
  476. advMapping="AdvHeightMap",
  477. vOrigMapping="VOrgMap",
  478. phantomIndex=1,
  479. )
  480. def _add_HVAR(font, masterModel, master_ttfs, axisTags):
  481. getAdvanceMetrics = partial(
  482. _get_advance_metrics, font, masterModel, master_ttfs, axisTags, HVAR_FIELDS
  483. )
  484. _add_VHVAR(font, axisTags, HVAR_FIELDS, getAdvanceMetrics)
  485. def _add_VVAR(font, masterModel, master_ttfs, axisTags):
  486. getAdvanceMetrics = partial(
  487. _get_advance_metrics, font, masterModel, master_ttfs, axisTags, VVAR_FIELDS
  488. )
  489. _add_VHVAR(font, axisTags, VVAR_FIELDS, getAdvanceMetrics)
  490. def _add_VHVAR(font, axisTags, tableFields, getAdvanceMetrics):
  491. tableTag = tableFields.tableTag
  492. assert tableTag not in font
  493. glyphOrder = font.getGlyphOrder()
  494. log.info("Generating " + tableTag)
  495. VHVAR = newTable(tableTag)
  496. tableClass = getattr(ot, tableTag)
  497. vhvar = VHVAR.table = tableClass()
  498. vhvar.Version = 0x00010000
  499. vhAdvanceDeltasAndSupports, vOrigDeltasAndSupports = getAdvanceMetrics()
  500. if vOrigDeltasAndSupports:
  501. singleModel = False
  502. else:
  503. singleModel = models.allEqual(
  504. id(v[1]) for v in vhAdvanceDeltasAndSupports.values()
  505. )
  506. directStore = None
  507. if singleModel:
  508. # Build direct mapping
  509. supports = next(iter(vhAdvanceDeltasAndSupports.values()))[1][1:]
  510. varTupleList = builder.buildVarRegionList(supports, axisTags)
  511. varTupleIndexes = list(range(len(supports)))
  512. varData = builder.buildVarData(varTupleIndexes, [], optimize=False)
  513. for glyphName in glyphOrder:
  514. varData.addItem(vhAdvanceDeltasAndSupports[glyphName][0], round=noRound)
  515. varData.optimize()
  516. directStore = builder.buildVarStore(varTupleList, [varData])
  517. # remove unused regions from VarRegionList
  518. directStore.prune_regions()
  519. # Build optimized indirect mapping
  520. storeBuilder = varStore.OnlineVarStoreBuilder(axisTags)
  521. advMapping = {}
  522. for glyphName in glyphOrder:
  523. deltas, supports = vhAdvanceDeltasAndSupports[glyphName]
  524. storeBuilder.setSupports(supports)
  525. advMapping[glyphName] = storeBuilder.storeDeltas(deltas, round=noRound)
  526. if vOrigDeltasAndSupports:
  527. vOrigMap = {}
  528. for glyphName in glyphOrder:
  529. deltas, supports = vOrigDeltasAndSupports[glyphName]
  530. storeBuilder.setSupports(supports)
  531. vOrigMap[glyphName] = storeBuilder.storeDeltas(deltas, round=noRound)
  532. indirectStore = storeBuilder.finish()
  533. mapping2 = indirectStore.optimize(use_NO_VARIATION_INDEX=False)
  534. advMapping = [mapping2[advMapping[g]] for g in glyphOrder]
  535. advanceMapping = builder.buildVarIdxMap(advMapping, glyphOrder)
  536. if vOrigDeltasAndSupports:
  537. vOrigMap = [mapping2[vOrigMap[g]] for g in glyphOrder]
  538. useDirect = False
  539. vOrigMapping = None
  540. if directStore:
  541. # Compile both, see which is more compact
  542. writer = OTTableWriter()
  543. directStore.compile(writer, font)
  544. directSize = len(writer.getAllData())
  545. writer = OTTableWriter()
  546. indirectStore.compile(writer, font)
  547. advanceMapping.compile(writer, font)
  548. indirectSize = len(writer.getAllData())
  549. useDirect = directSize < indirectSize
  550. if useDirect:
  551. metricsStore = directStore
  552. advanceMapping = None
  553. else:
  554. metricsStore = indirectStore
  555. if vOrigDeltasAndSupports:
  556. vOrigMapping = builder.buildVarIdxMap(vOrigMap, glyphOrder)
  557. vhvar.VarStore = metricsStore
  558. setattr(vhvar, tableFields.advMapping, advanceMapping)
  559. if vOrigMapping is not None:
  560. setattr(vhvar, tableFields.vOrigMapping, vOrigMapping)
  561. setattr(vhvar, tableFields.sb1, None)
  562. setattr(vhvar, tableFields.sb2, None)
  563. font[tableTag] = VHVAR
  564. return
  565. def _get_advance_metrics(font, masterModel, master_ttfs, axisTags, tableFields):
  566. tableTag = tableFields.tableTag
  567. glyphOrder = font.getGlyphOrder()
  568. # Build list of source font advance widths for each glyph
  569. metricsTag = tableFields.metricsTag
  570. advMetricses = [m[metricsTag].metrics for m in master_ttfs]
  571. # Build list of source font vertical origin coords for each glyph
  572. if tableTag == "VVAR" and "VORG" in master_ttfs[0]:
  573. vOrigMetricses = [m["VORG"].VOriginRecords for m in master_ttfs]
  574. defaultYOrigs = [m["VORG"].defaultVertOriginY for m in master_ttfs]
  575. vOrigMetricses = list(zip(vOrigMetricses, defaultYOrigs))
  576. else:
  577. vOrigMetricses = None
  578. vhAdvanceDeltasAndSupports = {}
  579. vOrigDeltasAndSupports = {}
  580. # HACK: we treat width 65535 as a sentinel value to signal that a glyph
  581. # from a non-default master should not participate in computing {H,V}VAR,
  582. # as if it were missing. Allows to variate other glyph-related data independently
  583. # from glyph metrics
  584. sparse_advance = 0xFFFF
  585. for glyph in glyphOrder:
  586. vhAdvances = [
  587. (
  588. metrics[glyph][0]
  589. if glyph in metrics and metrics[glyph][0] != sparse_advance
  590. else None
  591. )
  592. for metrics in advMetricses
  593. ]
  594. vhAdvanceDeltasAndSupports[glyph] = masterModel.getDeltasAndSupports(
  595. vhAdvances, round=round
  596. )
  597. if vOrigMetricses:
  598. for glyph in glyphOrder:
  599. # We need to supply a vOrigs tuple with non-None default values
  600. # for each glyph. vOrigMetricses contains values only for those
  601. # glyphs which have a non-default vOrig.
  602. vOrigs = [
  603. metrics[glyph] if glyph in metrics else defaultVOrig
  604. for metrics, defaultVOrig in vOrigMetricses
  605. ]
  606. vOrigDeltasAndSupports[glyph] = masterModel.getDeltasAndSupports(
  607. vOrigs, round=round
  608. )
  609. return vhAdvanceDeltasAndSupports, vOrigDeltasAndSupports
  610. def _add_MVAR(font, masterModel, master_ttfs, axisTags):
  611. log.info("Generating MVAR")
  612. store_builder = varStore.OnlineVarStoreBuilder(axisTags)
  613. records = []
  614. lastTableTag = None
  615. fontTable = None
  616. tables = None
  617. # HACK: we need to special-case post.underlineThickness and .underlinePosition
  618. # and unilaterally/arbitrarily define a sentinel value to distinguish the case
  619. # when a post table is present in a given master simply because that's where
  620. # the glyph names in TrueType must be stored, but the underline values are not
  621. # meant to be used for building MVAR's deltas. The value of -0x8000 (-32768)
  622. # the minimum FWord (int16) value, was chosen for its unlikelyhood to appear
  623. # in real-world underline position/thickness values.
  624. specialTags = {"unds": -0x8000, "undo": -0x8000}
  625. for tag, (tableTag, itemName) in sorted(MVAR_ENTRIES.items(), key=lambda kv: kv[1]):
  626. # For each tag, fetch the associated table from all fonts (or not when we are
  627. # still looking at a tag from the same tables) and set up the variation model
  628. # for them.
  629. if tableTag != lastTableTag:
  630. tables = fontTable = None
  631. if tableTag in font:
  632. fontTable = font[tableTag]
  633. tables = []
  634. for master in master_ttfs:
  635. if tableTag not in master or (
  636. tag in specialTags
  637. and getattr(master[tableTag], itemName) == specialTags[tag]
  638. ):
  639. tables.append(None)
  640. else:
  641. tables.append(master[tableTag])
  642. model, tables = masterModel.getSubModel(tables)
  643. store_builder.setModel(model)
  644. lastTableTag = tableTag
  645. if tables is None: # Tag not applicable to the master font.
  646. continue
  647. # TODO support gasp entries
  648. master_values = [getattr(table, itemName) for table in tables]
  649. if models.allEqual(master_values):
  650. base, varIdx = master_values[0], None
  651. else:
  652. base, varIdx = store_builder.storeMasters(master_values)
  653. setattr(fontTable, itemName, base)
  654. if varIdx is None:
  655. continue
  656. log.info(" %s: %s.%s %s", tag, tableTag, itemName, master_values)
  657. rec = ot.MetricsValueRecord()
  658. rec.ValueTag = tag
  659. rec.VarIdx = varIdx
  660. records.append(rec)
  661. assert "MVAR" not in font
  662. if records:
  663. store = store_builder.finish()
  664. # Optimize
  665. mapping = store.optimize()
  666. for rec in records:
  667. rec.VarIdx = mapping[rec.VarIdx]
  668. MVAR = font["MVAR"] = newTable("MVAR")
  669. mvar = MVAR.table = ot.MVAR()
  670. mvar.Version = 0x00010000
  671. mvar.Reserved = 0
  672. mvar.VarStore = store
  673. # XXX these should not be hard-coded but computed automatically
  674. mvar.ValueRecordSize = 8
  675. mvar.ValueRecordCount = len(records)
  676. mvar.ValueRecord = sorted(records, key=lambda r: r.ValueTag)
  677. def _add_BASE(font, masterModel, master_ttfs, axisTags):
  678. log.info("Generating BASE")
  679. merger = VariationMerger(masterModel, axisTags, font)
  680. merger.mergeTables(font, master_ttfs, ["BASE"])
  681. store = merger.store_builder.finish()
  682. if not store:
  683. return
  684. base = font["BASE"].table
  685. assert base.Version == 0x00010000
  686. base.Version = 0x00010001
  687. base.VarStore = store
  688. def _merge_OTL(font, model, master_fonts, axisTags):
  689. otl_tags = ["GSUB", "GDEF", "GPOS"]
  690. if not any(tag in font for tag in otl_tags):
  691. return
  692. log.info("Merging OpenType Layout tables")
  693. merger = VariationMerger(model, axisTags, font)
  694. merger.mergeTables(font, master_fonts, otl_tags)
  695. store = merger.store_builder.finish()
  696. if not store:
  697. return
  698. try:
  699. GDEF = font["GDEF"].table
  700. assert GDEF.Version <= 0x00010002
  701. except KeyError:
  702. GDEFTable = font["GDEF"] = newTable("GDEF")
  703. GDEF = GDEFTable.table = ot.GDEF()
  704. GDEF.GlyphClassDef = None
  705. GDEF.AttachList = None
  706. GDEF.LigCaretList = None
  707. GDEF.MarkAttachClassDef = None
  708. GDEF.MarkGlyphSetsDef = None
  709. GDEF.Version = 0x00010003
  710. GDEF.VarStore = store
  711. # Optimize
  712. varidx_map = store.optimize()
  713. GDEF.remap_device_varidxes(varidx_map)
  714. if "GPOS" in font:
  715. font["GPOS"].table.remap_device_varidxes(varidx_map)
  716. def _add_GSUB_feature_variations(
  717. font, axes, internal_axis_supports, rules, featureTags
  718. ):
  719. def normalize(name, value):
  720. return models.normalizeLocation({name: value}, internal_axis_supports)[name]
  721. log.info("Generating GSUB FeatureVariations")
  722. axis_tags = {name: axis.tag for name, axis in axes.items()}
  723. conditional_subs = []
  724. for rule in rules:
  725. region = []
  726. for conditions in rule.conditionSets:
  727. space = {}
  728. for condition in conditions:
  729. axis_name = condition["name"]
  730. if condition["minimum"] is not None:
  731. minimum = normalize(axis_name, condition["minimum"])
  732. else:
  733. minimum = -1.0
  734. if condition["maximum"] is not None:
  735. maximum = normalize(axis_name, condition["maximum"])
  736. else:
  737. maximum = 1.0
  738. tag = axis_tags[axis_name]
  739. space[tag] = (minimum, maximum)
  740. region.append(space)
  741. subs = {k: v for k, v in rule.subs}
  742. conditional_subs.append((region, subs))
  743. addFeatureVariations(font, conditional_subs, featureTags)
  744. _DesignSpaceData = namedtuple(
  745. "_DesignSpaceData",
  746. [
  747. "axes",
  748. "axisMappings",
  749. "internal_axis_supports",
  750. "base_idx",
  751. "normalized_master_locs",
  752. "masters",
  753. "instances",
  754. "rules",
  755. "rulesProcessingLast",
  756. "lib",
  757. ],
  758. )
  759. def _add_CFF2(varFont, model, master_fonts):
  760. from .cff import merge_region_fonts
  761. glyphOrder = varFont.getGlyphOrder()
  762. if "CFF2" not in varFont:
  763. from fontTools.cffLib.CFFToCFF2 import convertCFFToCFF2
  764. convertCFFToCFF2(varFont)
  765. ordered_fonts_list = model.reorderMasters(master_fonts, model.reverseMapping)
  766. # re-ordering the master list simplifies building the CFF2 data item lists.
  767. merge_region_fonts(varFont, model, ordered_fonts_list, glyphOrder)
  768. def _add_COLR(font, model, master_fonts, axisTags, colr_layer_reuse=True):
  769. merger = COLRVariationMerger(
  770. model, axisTags, font, allowLayerReuse=colr_layer_reuse
  771. )
  772. merger.mergeTables(font, master_fonts)
  773. store = merger.store_builder.finish()
  774. colr = font["COLR"].table
  775. if store:
  776. mapping = store.optimize()
  777. colr.VarStore = store
  778. varIdxes = [mapping[v] for v in merger.varIdxes]
  779. colr.VarIndexMap = builder.buildDeltaSetIndexMap(varIdxes)
  780. def load_designspace(designspace, log_enabled=True, *, require_sources=True):
  781. # TODO: remove this and always assume 'designspace' is a DesignSpaceDocument,
  782. # never a file path, as that's already handled by caller
  783. if hasattr(designspace, "sources"): # Assume a DesignspaceDocument
  784. ds = designspace
  785. else: # Assume a file path
  786. ds = DesignSpaceDocument.fromfile(designspace)
  787. masters = ds.sources
  788. if require_sources and not masters:
  789. raise VarLibValidationError("Designspace must have at least one source.")
  790. instances = ds.instances
  791. # TODO: Use fontTools.designspaceLib.tagForAxisName instead.
  792. standard_axis_map = OrderedDict(
  793. [
  794. ("weight", ("wght", {"en": "Weight"})),
  795. ("width", ("wdth", {"en": "Width"})),
  796. ("slant", ("slnt", {"en": "Slant"})),
  797. ("optical", ("opsz", {"en": "Optical Size"})),
  798. ("italic", ("ital", {"en": "Italic"})),
  799. ]
  800. )
  801. # Setup axes
  802. if not ds.axes:
  803. raise VarLibValidationError(f"Designspace must have at least one axis.")
  804. axes = OrderedDict()
  805. for axis_index, axis in enumerate(ds.axes):
  806. axis_name = axis.name
  807. if not axis_name:
  808. if not axis.tag:
  809. raise VarLibValidationError(f"Axis at index {axis_index} needs a tag.")
  810. axis_name = axis.name = axis.tag
  811. if axis_name in standard_axis_map:
  812. if axis.tag is None:
  813. axis.tag = standard_axis_map[axis_name][0]
  814. if not axis.labelNames:
  815. axis.labelNames.update(standard_axis_map[axis_name][1])
  816. else:
  817. if not axis.tag:
  818. raise VarLibValidationError(f"Axis at index {axis_index} needs a tag.")
  819. if not axis.labelNames:
  820. axis.labelNames["en"] = tostr(axis_name)
  821. axes[axis_name] = axis
  822. if log_enabled:
  823. log.info("Axes:\n%s", pformat([axis.asdict() for axis in axes.values()]))
  824. axisMappings = ds.axisMappings
  825. if axisMappings and log_enabled:
  826. log.info("Mappings:\n%s", pformat(axisMappings))
  827. # Check all master and instance locations are valid and fill in defaults
  828. for obj in masters + instances:
  829. obj_name = obj.name or obj.styleName or ""
  830. loc = obj.getFullDesignLocation(ds)
  831. obj.designLocation = loc
  832. if loc is None:
  833. raise VarLibValidationError(
  834. f"Source or instance '{obj_name}' has no location."
  835. )
  836. for axis_name in loc.keys():
  837. if axis_name not in axes:
  838. raise VarLibValidationError(
  839. f"Location axis '{axis_name}' unknown for '{obj_name}'."
  840. )
  841. for axis_name, axis in axes.items():
  842. v = axis.map_backward(loc[axis_name])
  843. if not (axis.minimum <= v <= axis.maximum):
  844. raise VarLibValidationError(
  845. f"Source or instance '{obj_name}' has out-of-range location "
  846. f"for axis '{axis_name}': is mapped to {v} but must be in "
  847. f"mapped range [{axis.minimum}..{axis.maximum}] (NOTE: all "
  848. "values are in user-space)."
  849. )
  850. # Normalize master locations
  851. internal_master_locs = [o.getFullDesignLocation(ds) for o in masters]
  852. if log_enabled:
  853. log.info("Internal master locations:\n%s", pformat(internal_master_locs))
  854. # TODO This mapping should ideally be moved closer to logic in _add_fvar/avar
  855. internal_axis_supports = {}
  856. for axis in axes.values():
  857. triple = (axis.minimum, axis.default, axis.maximum)
  858. internal_axis_supports[axis.name] = [axis.map_forward(v) for v in triple]
  859. if log_enabled:
  860. log.info("Internal axis supports:\n%s", pformat(internal_axis_supports))
  861. normalized_master_locs = [
  862. models.normalizeLocation(m, internal_axis_supports)
  863. for m in internal_master_locs
  864. ]
  865. if log_enabled:
  866. log.info("Normalized master locations:\n%s", pformat(normalized_master_locs))
  867. # Find base master
  868. base_idx = None
  869. for i, m in enumerate(normalized_master_locs):
  870. if all(v == 0 for v in m.values()):
  871. if base_idx is not None:
  872. raise VarLibValidationError(
  873. "More than one base master found in Designspace."
  874. )
  875. base_idx = i
  876. if require_sources and base_idx is None:
  877. raise VarLibValidationError(
  878. "Base master not found; no master at default location?"
  879. )
  880. if log_enabled:
  881. log.info("Index of base master: %s", base_idx)
  882. return _DesignSpaceData(
  883. axes,
  884. axisMappings,
  885. internal_axis_supports,
  886. base_idx,
  887. normalized_master_locs,
  888. masters,
  889. instances,
  890. ds.rules,
  891. ds.rulesProcessingLast,
  892. ds.lib,
  893. )
  894. # https://docs.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass
  895. WDTH_VALUE_TO_OS2_WIDTH_CLASS = {
  896. 50: 1,
  897. 62.5: 2,
  898. 75: 3,
  899. 87.5: 4,
  900. 100: 5,
  901. 112.5: 6,
  902. 125: 7,
  903. 150: 8,
  904. 200: 9,
  905. }
  906. def set_default_weight_width_slant(font, location):
  907. if "OS/2" in font:
  908. if "wght" in location:
  909. weight_class = otRound(max(1, min(location["wght"], 1000)))
  910. if font["OS/2"].usWeightClass != weight_class:
  911. log.info("Setting OS/2.usWeightClass = %s", weight_class)
  912. font["OS/2"].usWeightClass = weight_class
  913. if "wdth" in location:
  914. # map 'wdth' axis (50..200) to OS/2.usWidthClass (1..9), rounding to closest
  915. widthValue = min(max(location["wdth"], 50), 200)
  916. widthClass = otRound(
  917. models.piecewiseLinearMap(widthValue, WDTH_VALUE_TO_OS2_WIDTH_CLASS)
  918. )
  919. if font["OS/2"].usWidthClass != widthClass:
  920. log.info("Setting OS/2.usWidthClass = %s", widthClass)
  921. font["OS/2"].usWidthClass = widthClass
  922. if "slnt" in location and "post" in font:
  923. italicAngle = max(-90, min(location["slnt"], 90))
  924. if font["post"].italicAngle != italicAngle:
  925. log.info("Setting post.italicAngle = %s", italicAngle)
  926. font["post"].italicAngle = italicAngle
  927. def drop_implied_oncurve_points(*masters: TTFont) -> int:
  928. """Drop impliable on-curve points from all the simple glyphs in masters.
  929. In TrueType glyf outlines, on-curve points can be implied when they are located
  930. exactly at the midpoint of the line connecting two consecutive off-curve points.
  931. The input masters' glyf tables are assumed to contain same-named glyphs that are
  932. interpolatable. Oncurve points are only dropped if they can be implied for all
  933. the masters. The fonts are modified in-place.
  934. Args:
  935. masters: The TTFont(s) to modify
  936. Returns:
  937. The total number of points that were dropped if any.
  938. Reference:
  939. https://developer.apple.com/fonts/TrueType-Reference-Manual/RM01/Chap1.html
  940. """
  941. glyph_masters = defaultdict(list)
  942. # multiple DS source may point to the same TTFont object and we want to
  943. # avoid processing the same glyph twice as they are modified in-place
  944. for font in {id(m): m for m in masters}.values():
  945. glyf = font["glyf"]
  946. for glyphName in glyf.keys():
  947. glyph_masters[glyphName].append(glyf[glyphName])
  948. count = 0
  949. for glyphName, glyphs in glyph_masters.items():
  950. try:
  951. dropped = dropImpliedOnCurvePoints(*glyphs)
  952. except ValueError as e:
  953. # we don't fail for incompatible glyphs in _add_gvar so we shouldn't here
  954. log.warning("Failed to drop implied oncurves for %r: %s", glyphName, e)
  955. else:
  956. count += len(dropped)
  957. return count
  958. def build_many(
  959. designspace: DesignSpaceDocument,
  960. master_finder=lambda s: s,
  961. exclude=[],
  962. optimize=True,
  963. skip_vf=lambda vf_name: False,
  964. colr_layer_reuse=True,
  965. drop_implied_oncurves=False,
  966. ):
  967. """
  968. Build variable fonts from a designspace file, version 5 which can define
  969. several VFs, or version 4 which has implicitly one VF covering the whole doc.
  970. If master_finder is set, it should be a callable that takes master
  971. filename as found in designspace file and map it to master font
  972. binary as to be opened (eg. .ttf or .otf).
  973. skip_vf can be used to skip building some of the variable fonts defined in
  974. the input designspace. It's a predicate that takes as argument the name
  975. of the variable font and returns `bool`.
  976. Always returns a Dict[str, TTFont] keyed by VariableFontDescriptor.name
  977. """
  978. res = {}
  979. # varLib.build (used further below) by default only builds an incomplete 'STAT'
  980. # with an empty AxisValueArray--unless the VF inherited 'STAT' from its base master.
  981. # Designspace version 5 can also be used to define 'STAT' labels or customize
  982. # axes ordering, etc. To avoid overwriting a pre-existing 'STAT' or redoing the
  983. # same work twice, here we check if designspace contains any 'STAT' info before
  984. # proceeding to call buildVFStatTable for each VF.
  985. # https://github.com/fonttools/fonttools/pull/3024
  986. # https://github.com/fonttools/fonttools/issues/3045
  987. doBuildStatFromDSv5 = (
  988. "STAT" not in exclude
  989. and designspace.formatTuple >= (5, 0)
  990. and (
  991. any(a.axisLabels or a.axisOrdering is not None for a in designspace.axes)
  992. or designspace.locationLabels
  993. )
  994. )
  995. for _location, subDoc in splitInterpolable(designspace):
  996. for name, vfDoc in splitVariableFonts(subDoc):
  997. if skip_vf(name):
  998. log.debug(f"Skipping variable TTF font: {name}")
  999. continue
  1000. vf = build(
  1001. vfDoc,
  1002. master_finder,
  1003. exclude=exclude,
  1004. optimize=optimize,
  1005. colr_layer_reuse=colr_layer_reuse,
  1006. drop_implied_oncurves=drop_implied_oncurves,
  1007. )[0]
  1008. if doBuildStatFromDSv5:
  1009. buildVFStatTable(vf, designspace, name)
  1010. res[name] = vf
  1011. return res
  1012. def build(
  1013. designspace,
  1014. master_finder=lambda s: s,
  1015. exclude=[],
  1016. optimize=True,
  1017. colr_layer_reuse=True,
  1018. drop_implied_oncurves=False,
  1019. ):
  1020. """
  1021. Build variation font from a designspace file.
  1022. If master_finder is set, it should be a callable that takes master
  1023. filename as found in designspace file and map it to master font
  1024. binary as to be opened (eg. .ttf or .otf).
  1025. """
  1026. if hasattr(designspace, "sources"): # Assume a DesignspaceDocument
  1027. pass
  1028. else: # Assume a file path
  1029. designspace = DesignSpaceDocument.fromfile(designspace)
  1030. ds = load_designspace(designspace)
  1031. log.info("Building variable font")
  1032. log.info("Loading master fonts")
  1033. master_fonts = load_masters(designspace, master_finder)
  1034. # TODO: 'master_ttfs' is unused except for return value, remove later
  1035. master_ttfs = []
  1036. for master in master_fonts:
  1037. try:
  1038. master_ttfs.append(master.reader.file.name)
  1039. except AttributeError:
  1040. master_ttfs.append(None) # in-memory fonts have no path
  1041. if drop_implied_oncurves and "glyf" in master_fonts[ds.base_idx]:
  1042. drop_count = drop_implied_oncurve_points(*master_fonts)
  1043. log.info(
  1044. "Dropped %s on-curve points from simple glyphs in the 'glyf' table",
  1045. drop_count,
  1046. )
  1047. # Copy the base master to work from it
  1048. vf = deepcopy(master_fonts[ds.base_idx])
  1049. if "DSIG" in vf:
  1050. del vf["DSIG"]
  1051. # Clear USE_MY_METRICS composite flags if set inconsistently across masters.
  1052. if "glyf" in vf:
  1053. _unset_inconsistent_use_my_metrics_flags(vf, master_fonts)
  1054. # TODO append masters as named-instances as well; needs .designspace change.
  1055. fvar = _add_fvar(vf, ds.axes, ds.instances)
  1056. if "STAT" not in exclude:
  1057. _add_stat(vf)
  1058. # Map from axis names to axis tags...
  1059. normalized_master_locs = [
  1060. {ds.axes[k].tag: v for k, v in loc.items()} for loc in ds.normalized_master_locs
  1061. ]
  1062. # From here on, we use fvar axes only
  1063. axisTags = [axis.axisTag for axis in fvar.axes]
  1064. # Assume single-model for now.
  1065. model = models.VariationModel(normalized_master_locs, axisOrder=axisTags)
  1066. assert 0 == model.mapping[ds.base_idx]
  1067. log.info("Building variations tables")
  1068. if "avar" not in exclude:
  1069. _add_avar(vf, ds.axes, ds.axisMappings, axisTags)
  1070. if "BASE" not in exclude and "BASE" in vf:
  1071. _add_BASE(vf, model, master_fonts, axisTags)
  1072. if "MVAR" not in exclude:
  1073. _add_MVAR(vf, model, master_fonts, axisTags)
  1074. if "HVAR" not in exclude:
  1075. _add_HVAR(vf, model, master_fonts, axisTags)
  1076. if "VVAR" not in exclude and "vmtx" in vf:
  1077. _add_VVAR(vf, model, master_fonts, axisTags)
  1078. if "GDEF" not in exclude or "GPOS" not in exclude:
  1079. _merge_OTL(vf, model, master_fonts, axisTags)
  1080. if "gvar" not in exclude and "glyf" in vf:
  1081. _add_gvar(vf, model, master_fonts, optimize=optimize)
  1082. if "cvar" not in exclude and "glyf" in vf:
  1083. _merge_TTHinting(vf, model, master_fonts)
  1084. if "GSUB" not in exclude and ds.rules:
  1085. featureTags = _feature_variations_tags(ds)
  1086. _add_GSUB_feature_variations(
  1087. vf, ds.axes, ds.internal_axis_supports, ds.rules, featureTags
  1088. )
  1089. if "CFF2" not in exclude and ("CFF " in vf or "CFF2" in vf):
  1090. _add_CFF2(vf, model, master_fonts)
  1091. if "post" in vf:
  1092. # set 'post' to format 2 to keep the glyph names dropped from CFF2
  1093. post = vf["post"]
  1094. if post.formatType != 2.0:
  1095. post.formatType = 2.0
  1096. post.extraNames = []
  1097. post.mapping = {}
  1098. if "COLR" not in exclude and "COLR" in vf and vf["COLR"].version > 0:
  1099. _add_COLR(vf, model, master_fonts, axisTags, colr_layer_reuse)
  1100. set_default_weight_width_slant(
  1101. vf, location={axis.axisTag: axis.defaultValue for axis in vf["fvar"].axes}
  1102. )
  1103. for tag in exclude:
  1104. if tag in vf:
  1105. del vf[tag]
  1106. # TODO: Only return vf for 4.0+, the rest is unused.
  1107. return vf, model, master_ttfs
  1108. def _open_font(path, master_finder=lambda s: s):
  1109. # load TTFont masters from given 'path': this can be either a .TTX or an
  1110. # OpenType binary font; or if neither of these, try use the 'master_finder'
  1111. # callable to resolve the path to a valid .TTX or OpenType font binary.
  1112. from fontTools.ttx import guessFileType
  1113. master_path = os.path.normpath(path)
  1114. tp = guessFileType(master_path)
  1115. if tp is None:
  1116. # not an OpenType binary/ttx, fall back to the master finder.
  1117. master_path = master_finder(master_path)
  1118. tp = guessFileType(master_path)
  1119. if tp in ("TTX", "OTX"):
  1120. font = TTFont()
  1121. font.importXML(master_path)
  1122. elif tp in ("TTF", "OTF", "WOFF", "WOFF2"):
  1123. font = TTFont(master_path)
  1124. else:
  1125. raise VarLibValidationError("Invalid master path: %r" % master_path)
  1126. return font
  1127. def load_masters(designspace, master_finder=lambda s: s):
  1128. """Ensure that all SourceDescriptor.font attributes have an appropriate TTFont
  1129. object loaded, or else open TTFont objects from the SourceDescriptor.path
  1130. attributes.
  1131. The paths can point to either an OpenType font, a TTX file, or a UFO. In the
  1132. latter case, use the provided master_finder callable to map from UFO paths to
  1133. the respective master font binaries (e.g. .ttf, .otf or .ttx).
  1134. Return list of master TTFont objects in the same order they are listed in the
  1135. DesignSpaceDocument.
  1136. """
  1137. for master in designspace.sources:
  1138. # If a SourceDescriptor has a layer name, demand that the compiled TTFont
  1139. # be supplied by the caller. This spares us from modifying MasterFinder.
  1140. if master.layerName and master.font is None:
  1141. raise VarLibValidationError(
  1142. f"Designspace source '{master.name or '<Unknown>'}' specified a "
  1143. "layer name but lacks the required TTFont object in the 'font' "
  1144. "attribute."
  1145. )
  1146. return designspace.loadSourceFonts(_open_font, master_finder=master_finder)
  1147. class MasterFinder(object):
  1148. def __init__(self, template):
  1149. self.template = template
  1150. def __call__(self, src_path):
  1151. fullname = os.path.abspath(src_path)
  1152. dirname, basename = os.path.split(fullname)
  1153. stem, ext = os.path.splitext(basename)
  1154. path = self.template.format(
  1155. fullname=fullname,
  1156. dirname=dirname,
  1157. basename=basename,
  1158. stem=stem,
  1159. ext=ext,
  1160. )
  1161. return os.path.normpath(path)
  1162. def _feature_variations_tags(ds):
  1163. raw_tags = ds.lib.get(
  1164. FEAVAR_FEATURETAG_LIB_KEY,
  1165. "rclt" if ds.rulesProcessingLast else "rvrn",
  1166. )
  1167. return sorted({t.strip() for t in raw_tags.split(",")})
  1168. def addGSUBFeatureVariations(vf, designspace, featureTags=(), *, log_enabled=False):
  1169. """Add GSUB FeatureVariations table to variable font, based on DesignSpace rules.
  1170. Args:
  1171. vf: A TTFont object representing the variable font.
  1172. designspace: A DesignSpaceDocument object.
  1173. featureTags: Optional feature tag(s) to use for the FeatureVariations records.
  1174. If unset, the key 'com.github.fonttools.varLib.featureVarsFeatureTag' is
  1175. looked up in the DS <lib> and used; otherwise the default is 'rclt' if
  1176. the <rules processing="last"> attribute is set, else 'rvrn'.
  1177. See <https://fonttools.readthedocs.io/en/latest/designspaceLib/xml.html#rules-element>
  1178. log_enabled: If True, log info about DS axes and sources. Default is False, as
  1179. the same info may have already been logged as part of varLib.build.
  1180. """
  1181. ds = load_designspace(designspace, log_enabled=log_enabled)
  1182. if not ds.rules:
  1183. return
  1184. if not featureTags:
  1185. featureTags = _feature_variations_tags(ds)
  1186. _add_GSUB_feature_variations(
  1187. vf, ds.axes, ds.internal_axis_supports, ds.rules, featureTags
  1188. )
  1189. def main(args=None):
  1190. """Build variable fonts from a designspace file and masters"""
  1191. from argparse import ArgumentParser
  1192. from fontTools import configLogger
  1193. parser = ArgumentParser(prog="varLib", description=main.__doc__)
  1194. parser.add_argument("designspace")
  1195. output_group = parser.add_mutually_exclusive_group()
  1196. output_group.add_argument(
  1197. "-o", metavar="OUTPUTFILE", dest="outfile", default=None, help="output file"
  1198. )
  1199. output_group.add_argument(
  1200. "-d",
  1201. "--output-dir",
  1202. metavar="OUTPUTDIR",
  1203. default=None,
  1204. help="output dir (default: same as input designspace file)",
  1205. )
  1206. parser.add_argument(
  1207. "-x",
  1208. metavar="TAG",
  1209. dest="exclude",
  1210. action="append",
  1211. default=[],
  1212. help="exclude table",
  1213. )
  1214. parser.add_argument(
  1215. "--disable-iup",
  1216. dest="optimize",
  1217. action="store_false",
  1218. help="do not perform IUP optimization",
  1219. )
  1220. parser.add_argument(
  1221. "--no-colr-layer-reuse",
  1222. dest="colr_layer_reuse",
  1223. action="store_false",
  1224. help="do not rebuild variable COLR table to optimize COLR layer reuse",
  1225. )
  1226. parser.add_argument(
  1227. "--drop-implied-oncurves",
  1228. action="store_true",
  1229. help=(
  1230. "drop on-curve points that can be implied when exactly in the middle of "
  1231. "two off-curve points (only applies to TrueType fonts)"
  1232. ),
  1233. )
  1234. parser.add_argument(
  1235. "--master-finder",
  1236. default="master_ttf_interpolatable/{stem}.ttf",
  1237. help=(
  1238. "templated string used for finding binary font "
  1239. "files given the source file names defined in the "
  1240. "designspace document. The following special strings "
  1241. "are defined: {fullname} is the absolute source file "
  1242. "name; {basename} is the file name without its "
  1243. "directory; {stem} is the basename without the file "
  1244. "extension; {ext} is the source file extension; "
  1245. "{dirname} is the directory of the absolute file "
  1246. 'name. The default value is "%(default)s".'
  1247. ),
  1248. )
  1249. parser.add_argument(
  1250. "--variable-fonts",
  1251. default=".*",
  1252. metavar="VF_NAME",
  1253. help=(
  1254. "Filter the list of variable fonts produced from the input "
  1255. "Designspace v5 file. By default all listed variable fonts are "
  1256. "generated. To generate a specific variable font (or variable fonts) "
  1257. 'that match a given "name" attribute, you can pass as argument '
  1258. "the full name or a regular expression. E.g.: --variable-fonts "
  1259. '"MyFontVF_WeightOnly"; or --variable-fonts "MyFontVFItalic_.*".'
  1260. ),
  1261. )
  1262. logging_group = parser.add_mutually_exclusive_group(required=False)
  1263. logging_group.add_argument(
  1264. "-v", "--verbose", action="store_true", help="Run more verbosely."
  1265. )
  1266. logging_group.add_argument(
  1267. "-q", "--quiet", action="store_true", help="Turn verbosity off."
  1268. )
  1269. options = parser.parse_args(args)
  1270. configLogger(
  1271. level=("DEBUG" if options.verbose else "ERROR" if options.quiet else "INFO")
  1272. )
  1273. designspace_filename = options.designspace
  1274. designspace = DesignSpaceDocument.fromfile(designspace_filename)
  1275. vf_descriptors = designspace.getVariableFonts()
  1276. if not vf_descriptors:
  1277. parser.error(f"No variable fonts in given designspace {designspace.path!r}")
  1278. vfs_to_build = []
  1279. for vf in vf_descriptors:
  1280. # Skip variable fonts that do not match the user's inclusion regex if given.
  1281. if not fullmatch(options.variable_fonts, vf.name):
  1282. continue
  1283. vfs_to_build.append(vf)
  1284. if not vfs_to_build:
  1285. parser.error(f"No variable fonts matching {options.variable_fonts!r}")
  1286. if options.outfile is not None and len(vfs_to_build) > 1:
  1287. parser.error(
  1288. "can't specify -o because there are multiple VFs to build; "
  1289. "use --output-dir, or select a single VF with --variable-fonts"
  1290. )
  1291. output_dir = options.output_dir
  1292. if output_dir is None:
  1293. output_dir = os.path.dirname(designspace_filename)
  1294. vf_name_to_output_path = {}
  1295. if len(vfs_to_build) == 1 and options.outfile is not None:
  1296. vf_name_to_output_path[vfs_to_build[0].name] = options.outfile
  1297. else:
  1298. for vf in vfs_to_build:
  1299. if vf.filename is not None:
  1300. # Only use basename to prevent path traversal attacks
  1301. filename = os.path.basename(vf.filename)
  1302. else:
  1303. filename = vf.name + ".{ext}"
  1304. vf_name_to_output_path[vf.name] = os.path.join(output_dir, filename)
  1305. vf_names_to_build = {vf.name for vf in vfs_to_build}
  1306. finder = MasterFinder(options.master_finder)
  1307. vfs = build_many(
  1308. designspace,
  1309. finder,
  1310. exclude=options.exclude,
  1311. optimize=options.optimize,
  1312. skip_vf=lambda name: name not in vf_names_to_build,
  1313. colr_layer_reuse=options.colr_layer_reuse,
  1314. drop_implied_oncurves=options.drop_implied_oncurves,
  1315. )
  1316. for vf_name, vf in vfs.items():
  1317. ext = "otf" if vf.sfntVersion == "OTTO" else "ttf"
  1318. output_path = vf_name_to_output_path[vf_name].format(ext=ext)
  1319. output_dir = os.path.dirname(output_path)
  1320. if output_dir:
  1321. os.makedirs(output_dir, exist_ok=True)
  1322. log.info("Saving variation font %s", output_path)
  1323. vf.save(output_path)
  1324. if __name__ == "__main__":
  1325. import sys
  1326. if len(sys.argv) > 1:
  1327. sys.exit(main())
  1328. import doctest
  1329. sys.exit(doctest.testmod().failed)