names.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. """Helpers for instantiating name table records."""
  2. from contextlib import contextmanager
  3. from copy import deepcopy
  4. from enum import IntEnum
  5. import re
  6. class NameID(IntEnum):
  7. FAMILY_NAME = 1
  8. SUBFAMILY_NAME = 2
  9. UNIQUE_FONT_IDENTIFIER = 3
  10. FULL_FONT_NAME = 4
  11. VERSION_STRING = 5
  12. POSTSCRIPT_NAME = 6
  13. TYPOGRAPHIC_FAMILY_NAME = 16
  14. TYPOGRAPHIC_SUBFAMILY_NAME = 17
  15. VARIATIONS_POSTSCRIPT_NAME_PREFIX = 25
  16. ELIDABLE_AXIS_VALUE_NAME = 2
  17. def getVariationNameIDs(varfont):
  18. used = []
  19. if "fvar" in varfont:
  20. fvar = varfont["fvar"]
  21. for axis in fvar.axes:
  22. used.append(axis.axisNameID)
  23. for instance in fvar.instances:
  24. used.append(instance.subfamilyNameID)
  25. if instance.postscriptNameID != 0xFFFF:
  26. used.append(instance.postscriptNameID)
  27. if "STAT" in varfont:
  28. stat = varfont["STAT"].table
  29. for axis in stat.DesignAxisRecord.Axis if stat.DesignAxisRecord else ():
  30. used.append(axis.AxisNameID)
  31. for value in stat.AxisValueArray.AxisValue if stat.AxisValueArray else ():
  32. used.append(value.ValueNameID)
  33. elidedFallbackNameID = getattr(stat, "ElidedFallbackNameID", None)
  34. if elidedFallbackNameID is not None:
  35. used.append(elidedFallbackNameID)
  36. # nameIDs <= 255 are reserved by OT spec so we don't touch them
  37. return {nameID for nameID in used if nameID > 255}
  38. @contextmanager
  39. def pruningUnusedNames(varfont):
  40. from . import log
  41. origNameIDs = getVariationNameIDs(varfont)
  42. yield
  43. log.info("Pruning name table")
  44. exclude = origNameIDs - getVariationNameIDs(varfont)
  45. varfont["name"].names[:] = [
  46. record for record in varfont["name"].names if record.nameID not in exclude
  47. ]
  48. if "ltag" in varfont:
  49. # Drop the whole 'ltag' table if all the language-dependent Unicode name
  50. # records that reference it have been dropped.
  51. # TODO: Only prune unused ltag tags, renumerating langIDs accordingly.
  52. # Note ltag can also be used by feat or morx tables, so check those too.
  53. if not any(
  54. record
  55. for record in varfont["name"].names
  56. if record.platformID == 0 and record.langID != 0xFFFF
  57. ):
  58. del varfont["ltag"]
  59. def updateNameTable(varfont, axisLimits):
  60. """Update instatiated variable font's name table using STAT AxisValues.
  61. Raises ValueError if the STAT table is missing or an Axis Value table is
  62. missing for requested axis locations.
  63. First, collect all STAT AxisValues that match the new default axis locations
  64. (excluding "elided" ones); concatenate the strings in design axis order,
  65. while giving priority to "synthetic" values (Format 4), to form the
  66. typographic subfamily name associated with the new default instance.
  67. Finally, update all related records in the name table, making sure that
  68. legacy family/sub-family names conform to the the R/I/B/BI (Regular, Italic,
  69. Bold, Bold Italic) naming model.
  70. Example: Updating a partial variable font:
  71. | >>> ttFont = TTFont("OpenSans[wdth,wght].ttf")
  72. | >>> updateNameTable(ttFont, {"wght": (400, 900), "wdth": 75})
  73. The name table records will be updated in the following manner:
  74. NameID 1 familyName: "Open Sans" --> "Open Sans Condensed"
  75. NameID 2 subFamilyName: "Regular" --> "Regular"
  76. NameID 3 Unique font identifier: "3.000;GOOG;OpenSans-Regular" --> \
  77. "3.000;GOOG;OpenSans-Condensed"
  78. NameID 4 Full font name: "Open Sans Regular" --> "Open Sans Condensed"
  79. NameID 6 PostScript name: "OpenSans-Regular" --> "OpenSans-Condensed"
  80. NameID 16 Typographic Family name: None --> "Open Sans"
  81. NameID 17 Typographic Subfamily name: None --> "Condensed"
  82. References:
  83. https://docs.microsoft.com/en-us/typography/opentype/spec/stat
  84. https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-ids
  85. """
  86. from . import AxisLimits, axisValuesFromAxisLimits
  87. if "STAT" not in varfont:
  88. raise ValueError("Cannot update name table since there is no STAT table.")
  89. stat = varfont["STAT"].table
  90. if not stat.AxisValueArray:
  91. raise ValueError("Cannot update name table since there are no STAT Axis Values")
  92. fvar = varfont["fvar"]
  93. # The updated name table will reflect the new 'zero origin' of the font.
  94. # If we're instantiating a partial font, we will populate the unpinned
  95. # axes with their default axis values from fvar.
  96. axisLimits = AxisLimits(axisLimits).limitAxesAndPopulateDefaults(varfont)
  97. partialDefaults = axisLimits.defaultLocation()
  98. fvarDefaults = {a.axisTag: a.defaultValue for a in fvar.axes}
  99. defaultAxisCoords = AxisLimits({**fvarDefaults, **partialDefaults})
  100. assert all(v.minimum == v.maximum for v in defaultAxisCoords.values())
  101. axisValueTables = axisValuesFromAxisLimits(stat, defaultAxisCoords)
  102. checkAxisValuesExist(stat, axisValueTables, defaultAxisCoords.pinnedLocation())
  103. # ignore "elidable" axis values, should be omitted in application font menus.
  104. axisValueTables = [
  105. v for v in axisValueTables if not v.Flags & ELIDABLE_AXIS_VALUE_NAME
  106. ]
  107. axisValueTables = _sortAxisValues(axisValueTables)
  108. _updateNameRecords(varfont, axisValueTables)
  109. def checkAxisValuesExist(stat, axisValues, axisCoords):
  110. seen = set()
  111. designAxes = stat.DesignAxisRecord.Axis
  112. hasValues = set()
  113. for value in stat.AxisValueArray.AxisValue:
  114. if value.Format in (1, 2, 3):
  115. hasValues.add(designAxes[value.AxisIndex].AxisTag)
  116. elif value.Format == 4:
  117. for rec in value.AxisValueRecord:
  118. hasValues.add(designAxes[rec.AxisIndex].AxisTag)
  119. for axisValueTable in axisValues:
  120. axisValueFormat = axisValueTable.Format
  121. if axisValueTable.Format in (1, 2, 3):
  122. axisTag = designAxes[axisValueTable.AxisIndex].AxisTag
  123. if axisValueFormat == 2:
  124. axisValue = axisValueTable.NominalValue
  125. else:
  126. axisValue = axisValueTable.Value
  127. if axisTag in axisCoords and axisValue == axisCoords[axisTag]:
  128. seen.add(axisTag)
  129. elif axisValueTable.Format == 4:
  130. for rec in axisValueTable.AxisValueRecord:
  131. axisTag = designAxes[rec.AxisIndex].AxisTag
  132. if axisTag in axisCoords and rec.Value == axisCoords[axisTag]:
  133. seen.add(axisTag)
  134. missingAxes = (set(axisCoords) - seen) & hasValues
  135. if missingAxes:
  136. missing = ", ".join(f"'{i}': {axisCoords[i]}" for i in missingAxes)
  137. raise ValueError(f"Cannot find Axis Values {{{missing}}}")
  138. def _sortAxisValues(axisValues):
  139. # Sort by axis index, remove duplicates and ensure that format 4 AxisValues
  140. # are dominant.
  141. # The MS Spec states: "if a format 1, format 2 or format 3 table has a
  142. # (nominal) value used in a format 4 table that also has values for
  143. # other axes, the format 4 table, being the more specific match, is used",
  144. # https://docs.microsoft.com/en-us/typography/opentype/spec/stat#axis-value-table-format-4
  145. results = []
  146. seenAxes = set()
  147. # Sort format 4 axes so the tables with the most AxisValueRecords are first
  148. format4 = sorted(
  149. [v for v in axisValues if v.Format == 4],
  150. key=lambda v: len(v.AxisValueRecord),
  151. reverse=True,
  152. )
  153. for val in format4:
  154. axisIndexes = set(r.AxisIndex for r in val.AxisValueRecord)
  155. minIndex = min(axisIndexes)
  156. if not seenAxes & axisIndexes:
  157. seenAxes |= axisIndexes
  158. results.append((minIndex, val))
  159. for val in axisValues:
  160. if val in format4:
  161. continue
  162. axisIndex = val.AxisIndex
  163. if axisIndex not in seenAxes:
  164. seenAxes.add(axisIndex)
  165. results.append((axisIndex, val))
  166. return [axisValue for _, axisValue in sorted(results)]
  167. def _updateNameRecords(varfont, axisValues):
  168. # Update nametable based on the axisValues using the R/I/B/BI model.
  169. nametable = varfont["name"]
  170. stat = varfont["STAT"].table
  171. axisValueNameIDs = [a.ValueNameID for a in axisValues]
  172. ribbiNameIDs = [n for n in axisValueNameIDs if _isRibbi(nametable, n)]
  173. nonRibbiNameIDs = [n for n in axisValueNameIDs if n not in ribbiNameIDs]
  174. elidedNameID = stat.ElidedFallbackNameID
  175. elidedNameIsRibbi = _isRibbi(nametable, elidedNameID)
  176. getName = nametable.getName
  177. platforms = set((r.platformID, r.platEncID, r.langID) for r in nametable.names)
  178. for platform in platforms:
  179. if not all(getName(i, *platform) for i in (1, 2, elidedNameID)):
  180. # Since no family name and subfamily name records were found,
  181. # we cannot update this set of name Records.
  182. continue
  183. subFamilyName = " ".join(
  184. getName(n, *platform).toUnicode() for n in ribbiNameIDs
  185. )
  186. if nonRibbiNameIDs:
  187. typoSubFamilyName = " ".join(
  188. getName(n, *platform).toUnicode() for n in axisValueNameIDs
  189. )
  190. else:
  191. typoSubFamilyName = None
  192. # If neither subFamilyName and typographic SubFamilyName exist,
  193. # we will use the STAT's elidedFallbackName
  194. if not typoSubFamilyName and not subFamilyName:
  195. if elidedNameIsRibbi:
  196. subFamilyName = getName(elidedNameID, *platform).toUnicode()
  197. else:
  198. typoSubFamilyName = getName(elidedNameID, *platform).toUnicode()
  199. familyNameSuffix = " ".join(
  200. getName(n, *platform).toUnicode() for n in nonRibbiNameIDs
  201. )
  202. _updateNameTableStyleRecords(
  203. varfont,
  204. familyNameSuffix,
  205. subFamilyName,
  206. typoSubFamilyName,
  207. *platform,
  208. )
  209. def _isRibbi(nametable, nameID):
  210. englishRecord = nametable.getName(nameID, 3, 1, 0x409)
  211. return (
  212. True
  213. if englishRecord is not None
  214. and englishRecord.toUnicode() in ("Regular", "Italic", "Bold", "Bold Italic")
  215. else False
  216. )
  217. def _updateNameTableStyleRecords(
  218. varfont,
  219. familyNameSuffix,
  220. subFamilyName,
  221. typoSubFamilyName,
  222. platformID=3,
  223. platEncID=1,
  224. langID=0x409,
  225. ):
  226. # TODO (Marc F) It may be nice to make this part a standalone
  227. # font renamer in the future.
  228. nametable = varfont["name"]
  229. platform = (platformID, platEncID, langID)
  230. currentFamilyName = nametable.getName(
  231. NameID.TYPOGRAPHIC_FAMILY_NAME, *platform
  232. ) or nametable.getName(NameID.FAMILY_NAME, *platform)
  233. currentStyleName = nametable.getName(
  234. NameID.TYPOGRAPHIC_SUBFAMILY_NAME, *platform
  235. ) or nametable.getName(NameID.SUBFAMILY_NAME, *platform)
  236. if not all([currentFamilyName, currentStyleName]):
  237. raise ValueError(f"Missing required NameIDs 1 and 2 for platform {platform}")
  238. currentFamilyName = currentFamilyName.toUnicode()
  239. currentStyleName = currentStyleName.toUnicode()
  240. nameIDs = {
  241. NameID.FAMILY_NAME: currentFamilyName,
  242. NameID.SUBFAMILY_NAME: subFamilyName or "Regular",
  243. }
  244. if typoSubFamilyName:
  245. nameIDs[NameID.FAMILY_NAME] = f"{currentFamilyName} {familyNameSuffix}".strip()
  246. nameIDs[NameID.TYPOGRAPHIC_FAMILY_NAME] = currentFamilyName
  247. nameIDs[NameID.TYPOGRAPHIC_SUBFAMILY_NAME] = typoSubFamilyName
  248. else:
  249. # Remove previous Typographic Family and SubFamily names since they're
  250. # no longer required
  251. for nameID in (
  252. NameID.TYPOGRAPHIC_FAMILY_NAME,
  253. NameID.TYPOGRAPHIC_SUBFAMILY_NAME,
  254. ):
  255. nametable.removeNames(nameID=nameID)
  256. newFamilyName = (
  257. nameIDs.get(NameID.TYPOGRAPHIC_FAMILY_NAME) or nameIDs[NameID.FAMILY_NAME]
  258. )
  259. newStyleName = (
  260. nameIDs.get(NameID.TYPOGRAPHIC_SUBFAMILY_NAME) or nameIDs[NameID.SUBFAMILY_NAME]
  261. )
  262. nameIDs[NameID.FULL_FONT_NAME] = f"{newFamilyName} {newStyleName}"
  263. nameIDs[NameID.POSTSCRIPT_NAME] = _updatePSNameRecord(
  264. varfont, newFamilyName, newStyleName, platform
  265. )
  266. uniqueID = _updateUniqueIdNameRecord(varfont, nameIDs, platform)
  267. if uniqueID:
  268. nameIDs[NameID.UNIQUE_FONT_IDENTIFIER] = uniqueID
  269. for nameID, string in nameIDs.items():
  270. assert string, nameID
  271. nametable.setName(string, nameID, *platform)
  272. if "fvar" not in varfont:
  273. nametable.removeNames(NameID.VARIATIONS_POSTSCRIPT_NAME_PREFIX)
  274. def _updatePSNameRecord(varfont, familyName, styleName, platform):
  275. # Implementation based on Adobe Technical Note #5902 :
  276. # https://wwwimages2.adobe.com/content/dam/acom/en/devnet/font/pdfs/5902.AdobePSNameGeneration.pdf
  277. nametable = varfont["name"]
  278. family_prefix = nametable.getName(
  279. NameID.VARIATIONS_POSTSCRIPT_NAME_PREFIX, *platform
  280. )
  281. if family_prefix:
  282. family_prefix = family_prefix.toUnicode()
  283. else:
  284. family_prefix = familyName
  285. psName = f"{family_prefix}-{styleName}"
  286. # Remove any characters other than uppercase Latin letters, lowercase
  287. # Latin letters, digits and hyphens.
  288. psName = re.sub(r"[^A-Za-z0-9-]", r"", psName)
  289. if len(psName) > 127:
  290. # Abbreviating the stylename so it fits within 127 characters whilst
  291. # conforming to every vendor's specification is too complex. Instead
  292. # we simply truncate the psname and add the required "..."
  293. return f"{psName[:124]}..."
  294. return psName
  295. def _updateUniqueIdNameRecord(varfont, nameIDs, platform):
  296. nametable = varfont["name"]
  297. currentRecord = nametable.getName(NameID.UNIQUE_FONT_IDENTIFIER, *platform)
  298. if not currentRecord:
  299. return None
  300. # Check if full name and postscript name are a substring of currentRecord
  301. for nameID in (NameID.FULL_FONT_NAME, NameID.POSTSCRIPT_NAME):
  302. nameRecord = nametable.getName(nameID, *platform)
  303. if not nameRecord:
  304. continue
  305. if nameRecord.toUnicode() in currentRecord.toUnicode():
  306. return currentRecord.toUnicode().replace(
  307. nameRecord.toUnicode(), nameIDs[nameRecord.nameID]
  308. )
  309. # Create a new string since we couldn't find any substrings.
  310. fontVersion = _fontVersion(varfont, platform)
  311. achVendID = varfont["OS/2"].achVendID
  312. # Remove non-ASCII characers and trailing spaces
  313. vendor = re.sub(r"[^\x00-\x7F]", "", achVendID).strip()
  314. psName = nameIDs[NameID.POSTSCRIPT_NAME]
  315. return f"{fontVersion};{vendor};{psName}"
  316. def _fontVersion(font, platform=(3, 1, 0x409)):
  317. nameRecord = font["name"].getName(NameID.VERSION_STRING, *platform)
  318. if nameRecord is None:
  319. return f'{font["head"].fontRevision:.3f}'
  320. # "Version 1.101; ttfautohint (v1.8.1.43-b0c9)" --> "1.101"
  321. # Also works fine with inputs "Version 1.101" or "1.101" etc
  322. versionNumber = nameRecord.toUnicode().split(";")[0]
  323. return versionNumber.lstrip("Version ").strip()