voltToFea.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. """\
  2. MS VOLT ``.vtp`` to AFDKO ``.fea`` OpenType Layout converter.
  3. Usage
  4. -----
  5. To convert a VTP project file:
  6. .. code-block:: sh
  7. $ fonttools voltLib.voltToFea input.vtp output.fea
  8. It is also possible convert font files with `TSIV` table (as saved from Volt),
  9. in this case the glyph names used in the Volt project will be mapped to the
  10. actual glyph names in the font files when written to the feature file:
  11. .. code-block:: sh
  12. $ fonttools voltLib.voltToFea input.ttf output.fea
  13. The ``--quiet`` option can be used to suppress warnings.
  14. The ``--traceback`` can be used to get Python traceback in case of exceptions,
  15. instead of suppressing the traceback.
  16. Limitations
  17. -----------
  18. * Not all VOLT features are supported, the script will error if it it
  19. encounters something it does not understand. Please report an issue if this
  20. happens.
  21. * AFDKO feature file syntax for mark positioning is awkward and does not allow
  22. setting the mark coverage. It also defines mark anchors globally, as a result
  23. some mark positioning lookups might cover many marks than what was in the VOLT
  24. file. This should not be an issue in practice, but if it is then the only way
  25. is to modify the VOLT file or the generated feature file manually to use unique
  26. mark anchors for each lookup.
  27. * VOLT allows subtable breaks in any lookup type, but AFDKO feature file
  28. implementations vary in their support; currently AFDKO’s makeOTF supports
  29. subtable breaks in pair positioning lookups only, while FontTools’ feaLib
  30. support it for most substitution lookups and only some positioning lookups.
  31. """
  32. import logging
  33. import re
  34. from io import StringIO
  35. from graphlib import TopologicalSorter
  36. from fontTools.feaLib import ast
  37. from fontTools.ttLib import TTFont, TTLibError
  38. from fontTools.voltLib import ast as VAst
  39. from fontTools.voltLib.parser import Parser as VoltParser
  40. log = logging.getLogger("fontTools.voltLib.voltToFea")
  41. TABLES = ["GDEF", "GSUB", "GPOS"]
  42. def _flatten_group(group):
  43. ret = []
  44. if isinstance(group, (tuple, list)):
  45. for item in group:
  46. ret.extend(_flatten_group(item))
  47. elif hasattr(group, "enum"):
  48. ret.extend(_flatten_group(group.enum))
  49. else:
  50. ret.append(group)
  51. return ret
  52. # Topologically sort of group definitions to ensure that all groups are defined
  53. # before they are referenced. This is necessary because FEA requires it but
  54. # VOLT does not, see below.
  55. def sort_groups(groups):
  56. group_map = {group.name.lower(): group for group in groups}
  57. graph = {
  58. group.name.lower(): [
  59. x.group.lower()
  60. for x in _flatten_group(group)
  61. if isinstance(x, VAst.GroupName)
  62. ]
  63. for group in groups
  64. }
  65. sorter = TopologicalSorter(graph)
  66. return [group_map[name] for name in sorter.static_order()]
  67. class Lookup(ast.LookupBlock):
  68. def __init__(self, name, use_extension=False, location=None):
  69. super().__init__(name, use_extension, location)
  70. self.chained = []
  71. class VoltToFea:
  72. _NOT_LOOKUP_NAME_RE = re.compile(r"[^A-Za-z_0-9.]")
  73. _NOT_CLASS_NAME_RE = re.compile(r"[^A-Za-z_0-9.\-]")
  74. def __init__(self, file_or_path, font=None):
  75. if isinstance(file_or_path, VAst.VoltFile):
  76. self._doc, self._file_or_path = file_or_path, None
  77. else:
  78. self._doc, self._file_or_path = None, file_or_path
  79. self._font = font
  80. self._glyph_map = {}
  81. self._glyph_order = None
  82. self._gdef = {}
  83. self._glyphclasses = {}
  84. self._features = {}
  85. self._lookups = {}
  86. self._marks = set()
  87. self._ligatures = {}
  88. self._markclasses = {}
  89. self._anchors = {}
  90. self._settings = {}
  91. self._lookup_names = {}
  92. self._class_names = {}
  93. def _lookupName(self, name):
  94. if name not in self._lookup_names:
  95. res = self._NOT_LOOKUP_NAME_RE.sub("_", name)
  96. while res in self._lookup_names.values():
  97. res += "_"
  98. self._lookup_names[name] = res
  99. return self._lookup_names[name]
  100. def _className(self, name):
  101. if name not in self._class_names:
  102. res = self._NOT_CLASS_NAME_RE.sub("_", name)
  103. while res in self._class_names.values():
  104. res += "_"
  105. self._class_names[name] = res
  106. return self._class_names[name]
  107. def _collectStatements(self, doc, tables, ignore_unsupported_settings=False):
  108. # Collect glyph difinitions first, as we need them to map VOLT glyph names to font glyph name.
  109. for statement in doc.statements:
  110. if isinstance(statement, VAst.GlyphDefinition):
  111. self._glyphDefinition(statement)
  112. # Collect and sort group definitions first, to make sure a group
  113. # definition that references other groups comes after them since VOLT
  114. # does not enforce such ordering, and feature file require it.
  115. groups = [s for s in doc.statements if isinstance(s, VAst.GroupDefinition)]
  116. for group in sort_groups(groups):
  117. self._groupDefinition(group)
  118. for statement in doc.statements:
  119. if isinstance(statement, VAst.AnchorDefinition):
  120. if "GPOS" in tables:
  121. self._anchorDefinition(statement)
  122. elif isinstance(statement, VAst.SettingDefinition):
  123. self._settingDefinition(statement, ignore_unsupported_settings)
  124. elif isinstance(statement, (VAst.GlyphDefinition, VAst.GroupDefinition)):
  125. pass # Handled above
  126. elif isinstance(statement, VAst.ScriptDefinition):
  127. self._scriptDefinition(statement)
  128. elif not isinstance(statement, VAst.LookupDefinition):
  129. raise NotImplementedError(statement)
  130. # Lookup definitions need to be handled last as they reference glyph
  131. # and mark classes that might be defined after them.
  132. for statement in doc.statements:
  133. if isinstance(statement, VAst.LookupDefinition):
  134. if statement.pos and "GPOS" not in tables:
  135. continue
  136. if statement.sub and "GSUB" not in tables:
  137. continue
  138. self._lookupDefinition(statement)
  139. def _buildFeatureFile(self, tables):
  140. doc = ast.FeatureFile()
  141. statements = doc.statements
  142. if self._glyphclasses:
  143. statements.append(ast.Comment("# Glyph classes"))
  144. statements.extend(self._glyphclasses.values())
  145. if self._markclasses:
  146. statements.append(ast.Comment("\n# Mark classes"))
  147. statements.extend(c[1] for c in sorted(self._markclasses.items()))
  148. if self._lookups:
  149. statements.append(ast.Comment("\n# Lookups"))
  150. for lookup in self._lookups.values():
  151. statements.extend(lookup.chained)
  152. statements.append(lookup)
  153. # Prune features
  154. features = self._features.copy()
  155. for feature_tag in features:
  156. scripts = features[feature_tag]
  157. for script_tag in scripts:
  158. langs = scripts[script_tag]
  159. for language_tag in langs:
  160. langs[language_tag] = [
  161. l for l in langs[language_tag] if l.lower() in self._lookups
  162. ]
  163. scripts[script_tag] = {t: l for t, l in langs.items() if l}
  164. features[feature_tag] = {t: s for t, s in scripts.items() if s}
  165. features = {t: f for t, f in features.items() if f}
  166. if features:
  167. statements.append(ast.Comment("# Features"))
  168. for feature_tag, scripts in features.items():
  169. feature = ast.FeatureBlock(feature_tag)
  170. script_tags = sorted(scripts, key=lambda k: 0 if k == "DFLT" else 1)
  171. if feature_tag == "aalt" and len(script_tags) > 1:
  172. log.warning(
  173. "FEA syntax does not allow script statements in 'aalt' feature, "
  174. "so only lookups from the first script will be included."
  175. )
  176. script_tags = script_tags[:1]
  177. for script_tag in script_tags:
  178. if feature_tag != "aalt":
  179. feature.statements.append(ast.ScriptStatement(script_tag))
  180. language_tags = sorted(
  181. scripts[script_tag],
  182. key=lambda k: 0 if k == "dflt" else 1,
  183. )
  184. if feature_tag == "aalt" and len(language_tags) > 1:
  185. log.warning(
  186. "FEA syntax does not allow language statements in 'aalt' feature, "
  187. "so only lookups from the first language will be included."
  188. )
  189. language_tags = language_tags[:1]
  190. for language_tag in language_tags:
  191. if feature_tag != "aalt":
  192. include_default = True if language_tag == "dflt" else False
  193. feature.statements.append(
  194. ast.LanguageStatement(
  195. language_tag.ljust(4),
  196. include_default=include_default,
  197. )
  198. )
  199. for name in scripts[script_tag][language_tag]:
  200. lookup = self._lookups[name.lower()]
  201. lookupref = ast.LookupReferenceStatement(lookup)
  202. feature.statements.append(lookupref)
  203. statements.append(feature)
  204. if self._gdef and "GDEF" in tables:
  205. classes = []
  206. for name in ("BASE", "MARK", "LIGATURE", "COMPONENT"):
  207. if name in self._gdef:
  208. classname = "GDEF_" + name.lower()
  209. glyphclass = ast.GlyphClassDefinition(classname, self._gdef[name])
  210. statements.append(glyphclass)
  211. classes.append(ast.GlyphClassName(glyphclass))
  212. else:
  213. classes.append(None)
  214. gdef = ast.TableBlock("GDEF")
  215. gdef.statements.append(ast.GlyphClassDefStatement(*classes))
  216. statements.append(gdef)
  217. return doc
  218. def convert(self, tables=None, ignore_unsupported_settings=False):
  219. if self._doc is None:
  220. self._doc = VoltParser(self._file_or_path).parse()
  221. doc = self._doc
  222. if tables is None:
  223. tables = TABLES
  224. if self._font is not None:
  225. self._glyph_order = self._font.getGlyphOrder()
  226. self._collectStatements(doc, tables, ignore_unsupported_settings)
  227. fea = self._buildFeatureFile(tables)
  228. return fea.asFea()
  229. def _glyphName(self, glyph):
  230. try:
  231. name = glyph.glyph
  232. except AttributeError:
  233. name = glyph
  234. return ast.GlyphName(self._glyph_map.get(name, name))
  235. def _groupName(self, group):
  236. try:
  237. name = group.group
  238. except AttributeError:
  239. name = group
  240. return ast.GlyphClassName(self._glyphclasses[name.lower()])
  241. def _glyphSet(self, item):
  242. return [
  243. (self._glyphName(x) if isinstance(x, (str, VAst.GlyphName)) else x)
  244. for x in item.glyphSet()
  245. ]
  246. def _coverage(self, coverage, flatten=False):
  247. items = []
  248. for item in coverage:
  249. if isinstance(item, VAst.GlyphName):
  250. items.append(self._glyphName(item))
  251. elif isinstance(item, VAst.GroupName):
  252. items.append(self._groupName(item))
  253. elif isinstance(item, VAst.Enum):
  254. item = self._coverage(item.enum, flatten=True)
  255. if flatten:
  256. items.extend(item)
  257. else:
  258. items.append(ast.GlyphClass(item))
  259. elif isinstance(item, VAst.Range):
  260. item = self._glyphSet(item)
  261. if flatten:
  262. items.extend(item)
  263. else:
  264. items.append(ast.GlyphClass(item))
  265. else:
  266. raise NotImplementedError(item)
  267. return items
  268. def _context(self, context):
  269. out = []
  270. for item in context:
  271. coverage = self._coverage(item, flatten=True)
  272. if len(coverage) > 1:
  273. coverage = ast.GlyphClass(coverage)
  274. else:
  275. coverage = coverage[0]
  276. out.append(coverage)
  277. return out
  278. def _groupDefinition(self, group):
  279. name = self._className(group.name)
  280. glyphs = self._coverage(group.enum.enum, flatten=True)
  281. glyphclass = ast.GlyphClass(glyphs)
  282. classdef = ast.GlyphClassDefinition(name, glyphclass)
  283. self._glyphclasses[group.name.lower()] = classdef
  284. def _glyphDefinition(self, glyph):
  285. try:
  286. self._glyph_map[glyph.name] = self._glyph_order[glyph.id]
  287. except TypeError:
  288. pass
  289. if glyph.type in ("BASE", "MARK", "LIGATURE", "COMPONENT"):
  290. if glyph.type not in self._gdef:
  291. self._gdef[glyph.type] = ast.GlyphClass()
  292. self._gdef[glyph.type].glyphs.append(self._glyphName(glyph.name))
  293. if glyph.type == "MARK":
  294. self._marks.add(glyph.name)
  295. elif glyph.type == "LIGATURE":
  296. self._ligatures[glyph.name] = glyph.components
  297. def _scriptDefinition(self, script):
  298. stag = script.tag
  299. for lang in script.langs:
  300. ltag = lang.tag
  301. for feature in lang.features:
  302. lookups = {l.split("\\")[0]: True for l in feature.lookups}
  303. ftag = feature.tag
  304. if ftag not in self._features:
  305. self._features[ftag] = {}
  306. if stag not in self._features[ftag]:
  307. self._features[ftag][stag] = {}
  308. assert ltag not in self._features[ftag][stag]
  309. self._features[ftag][stag][ltag] = lookups.keys()
  310. def _settingDefinition(self, setting, ignore_unsupported=False):
  311. if setting.name.startswith("COMPILER_"):
  312. self._settings[setting.name] = setting.value
  313. elif not ignore_unsupported:
  314. log.warning(f"Unsupported setting ignored: {setting.name}")
  315. def _adjustment(self, adjustment):
  316. adv, dx, dy, adv_adjust_by, dx_adjust_by, dy_adjust_by = adjustment
  317. adv_device = adv_adjust_by and adv_adjust_by.items() or None
  318. dx_device = dx_adjust_by and dx_adjust_by.items() or None
  319. dy_device = dy_adjust_by and dy_adjust_by.items() or None
  320. return ast.ValueRecord(
  321. xPlacement=dx,
  322. yPlacement=dy,
  323. xAdvance=adv,
  324. xPlaDevice=dx_device,
  325. yPlaDevice=dy_device,
  326. xAdvDevice=adv_device,
  327. )
  328. def _anchor(self, adjustment):
  329. adv, dx, dy, adv_adjust_by, dx_adjust_by, dy_adjust_by = adjustment
  330. assert not adv_adjust_by
  331. dx_device = dx_adjust_by and dx_adjust_by.items() or None
  332. dy_device = dy_adjust_by and dy_adjust_by.items() or None
  333. return ast.Anchor(
  334. dx or 0,
  335. dy or 0,
  336. xDeviceTable=dx_device or None,
  337. yDeviceTable=dy_device or None,
  338. )
  339. def _anchorDefinition(self, anchordef):
  340. anchorname = anchordef.name
  341. glyphname = anchordef.glyph_name
  342. anchor = self._anchor(anchordef.pos)
  343. if glyphname not in self._anchors:
  344. self._anchors[glyphname] = {}
  345. if anchorname.startswith("MARK_"):
  346. anchorname = anchorname[:5] + anchorname[5:].lower()
  347. else:
  348. anchorname = anchorname.lower()
  349. if anchorname not in self._anchors[glyphname]:
  350. self._anchors[glyphname][anchorname] = {}
  351. self._anchors[glyphname][anchorname][anchordef.component] = anchor
  352. def _gposLookup(self, lookup, fealookup):
  353. statements = fealookup.statements
  354. pos = lookup.pos
  355. if isinstance(pos, VAst.PositionAdjustPairDefinition):
  356. for (idx1, idx2), (pos1, pos2) in pos.adjust_pair.items():
  357. coverage_1 = pos.coverages_1[idx1 - 1]
  358. coverage_2 = pos.coverages_2[idx2 - 1]
  359. # If not both are groups, use “enum pos” otherwise makeotf will
  360. # fail.
  361. enumerated = False
  362. for item in coverage_1 + coverage_2:
  363. if not isinstance(item, VAst.GroupName):
  364. enumerated = True
  365. glyphs1 = self._coverage(coverage_1)
  366. glyphs2 = self._coverage(coverage_2)
  367. record1 = self._adjustment(pos1)
  368. record2 = self._adjustment(pos2)
  369. assert len(glyphs1) == 1
  370. assert len(glyphs2) == 1
  371. statements.append(
  372. ast.PairPosStatement(
  373. glyphs1[0], record1, glyphs2[0], record2, enumerated=enumerated
  374. )
  375. )
  376. elif isinstance(pos, VAst.PositionAdjustSingleDefinition):
  377. for a, b in pos.adjust_single:
  378. glyphs = self._coverage(a)
  379. record = self._adjustment(b)
  380. assert len(glyphs) == 1
  381. statements.append(
  382. ast.SinglePosStatement([(glyphs[0], record)], [], [], False)
  383. )
  384. elif isinstance(pos, VAst.PositionAttachDefinition):
  385. anchors = {}
  386. allmarks = set()
  387. for coverage, anchorname in pos.coverage_to:
  388. # In feature files mark classes are global, but in VOLT they
  389. # are defined per-lookup. If we output mark class definitions
  390. # for all marks that use a given anchor, we might end up with a
  391. # mark used in two different classes in the same lookup, which
  392. # is causes feature file compilation error.
  393. # At the expense of uglier feature code, we make the mark class
  394. # name by appending the current lookup name not the anchor
  395. # name, and output mark class definitions only for marks used
  396. # in this lookup.
  397. classname = self._className(f"{anchorname}.{lookup.name}")
  398. markclass = ast.MarkClass(classname)
  399. # Anchor names are case-insensitive in VOLT
  400. anchorname = anchorname.lower()
  401. # We might still end in marks used in two different anchor
  402. # classes, so we filter out already used marks.
  403. marks = set()
  404. for mark in coverage:
  405. marks.update(mark.glyphSet())
  406. if not marks.isdisjoint(allmarks):
  407. marks.difference_update(allmarks)
  408. if not marks:
  409. continue
  410. allmarks.update(marks)
  411. for glyphname in marks:
  412. glyph = self._glyphName(glyphname)
  413. anchor = self._anchors[glyphname][f"MARK_{anchorname}"][1]
  414. markdef = ast.MarkClassDefinition(markclass, anchor, glyph)
  415. self._markclasses[(glyphname, classname)] = markdef
  416. for base in pos.coverage:
  417. for name in base.glyphSet():
  418. if name not in anchors:
  419. anchors[name] = []
  420. if (anchorname, classname) not in anchors[name]:
  421. anchors[name].append((anchorname, classname))
  422. is_ligature = all(n in self._ligatures for n in anchors)
  423. is_mark = all(n in self._marks for n in anchors)
  424. for name in anchors:
  425. components = 1
  426. if is_ligature:
  427. components = self._ligatures[name]
  428. marks = [[] for _ in range(components)]
  429. for mark, classname in anchors[name]:
  430. markclass = ast.MarkClass(classname)
  431. for component in range(1, components + 1):
  432. if component in self._anchors[name][mark]:
  433. anchor = self._anchors[name][mark][component]
  434. marks[component - 1].append((anchor, markclass))
  435. base = self._glyphName(name)
  436. if is_mark:
  437. mark = ast.MarkMarkPosStatement(base, marks[0])
  438. elif is_ligature:
  439. mark = ast.MarkLigPosStatement(base, marks)
  440. else:
  441. mark = ast.MarkBasePosStatement(base, marks[0])
  442. statements.append(mark)
  443. elif isinstance(pos, VAst.PositionAttachCursiveDefinition):
  444. # Collect enter and exit glyphs
  445. enter_coverage = []
  446. for coverage in pos.coverages_enter:
  447. for base in coverage:
  448. for name in base.glyphSet():
  449. enter_coverage.append(name)
  450. exit_coverage = []
  451. for coverage in pos.coverages_exit:
  452. for base in coverage:
  453. for name in base.glyphSet():
  454. exit_coverage.append(name)
  455. # Write enter anchors, also check if the glyph has exit anchor and
  456. # write it, too.
  457. for name in enter_coverage:
  458. glyph = self._glyphName(name)
  459. entry = self._anchors[name]["entry"][1]
  460. exit = None
  461. if name in exit_coverage:
  462. exit = self._anchors[name]["exit"][1]
  463. exit_coverage.pop(exit_coverage.index(name))
  464. statements.append(ast.CursivePosStatement(glyph, entry, exit))
  465. # Write any remaining exit anchors.
  466. for name in exit_coverage:
  467. glyph = self._glyphName(name)
  468. exit = self._anchors[name]["exit"][1]
  469. statements.append(ast.CursivePosStatement(glyph, None, exit))
  470. else:
  471. raise NotImplementedError(pos)
  472. def _gposContextLookup(self, lookup, prefix, suffix, ignore, fealookup, chained):
  473. statements = fealookup.statements
  474. pos = lookup.pos
  475. if isinstance(pos, VAst.PositionAdjustPairDefinition):
  476. for (idx1, idx2), (pos1, pos2) in pos.adjust_pair.items():
  477. glyphs1 = self._coverage(pos.coverages_1[idx1 - 1])
  478. glyphs2 = self._coverage(pos.coverages_2[idx2 - 1])
  479. assert len(glyphs1) == 1
  480. assert len(glyphs2) == 1
  481. glyphs = (glyphs1[0], glyphs2[0])
  482. if ignore:
  483. statement = ast.IgnorePosStatement([(prefix, glyphs, suffix)])
  484. else:
  485. statement = ast.ChainContextPosStatement(
  486. prefix, glyphs, suffix, [chained, chained]
  487. )
  488. statements.append(statement)
  489. elif isinstance(pos, VAst.PositionAdjustSingleDefinition):
  490. glyphs = [ast.GlyphClass()]
  491. for a, _ in pos.adjust_single:
  492. glyphs[0].extend(self._coverage(a, flatten=True))
  493. if ignore:
  494. statement = ast.IgnorePosStatement([(prefix, glyphs, suffix)])
  495. else:
  496. statement = ast.ChainContextPosStatement(
  497. prefix, glyphs, suffix, [chained]
  498. )
  499. statements.append(statement)
  500. elif isinstance(pos, VAst.PositionAttachDefinition):
  501. glyphs = [ast.GlyphClass()]
  502. for coverage, _ in pos.coverage_to:
  503. glyphs[0].extend(self._coverage(coverage, flatten=True))
  504. if ignore:
  505. statement = ast.IgnorePosStatement([(prefix, glyphs, suffix)])
  506. else:
  507. statement = ast.ChainContextPosStatement(
  508. prefix, glyphs, suffix, [chained]
  509. )
  510. statements.append(statement)
  511. else:
  512. raise NotImplementedError(pos)
  513. def _gsubLookup(self, lookup, fealookup):
  514. statements = fealookup.statements
  515. sub = lookup.sub
  516. # Alternate substitutions are represented by adding multiple
  517. # substitutions for the same glyph, so we need to collect them into one
  518. # to many mapping.
  519. if isinstance(sub, VAst.SubstitutionAlternateDefinition):
  520. alternates = {}
  521. for key, val in sub.mapping.items():
  522. if not key or not val:
  523. path, line, column = sub.location
  524. log.warning(f"{path}:{line}:{column}: Ignoring empty substitution")
  525. continue
  526. glyphs = self._coverage(key)
  527. replacements = self._coverage(val)
  528. assert len(glyphs) == 1
  529. for src_glyph, repl_glyph in zip(
  530. glyphs[0].glyphSet(), replacements[0].glyphSet()
  531. ):
  532. alternates.setdefault(str(self._glyphName(src_glyph)), []).append(
  533. str(self._glyphName(repl_glyph))
  534. )
  535. for glyph, replacements in alternates.items():
  536. statement = ast.AlternateSubstStatement(
  537. [], glyph, [], ast.GlyphClass(replacements)
  538. )
  539. statements.append(statement)
  540. return
  541. for key, val in sub.mapping.items():
  542. if not key or not val:
  543. path, line, column = sub.location
  544. log.warning(f"{path}:{line}:{column}: Ignoring empty substitution")
  545. continue
  546. glyphs = self._coverage(key)
  547. replacements = self._coverage(val)
  548. if isinstance(sub, VAst.SubstitutionSingleDefinition):
  549. assert len(glyphs) == 1
  550. assert len(replacements) == 1
  551. statements.append(
  552. ast.SingleSubstStatement(glyphs, replacements, [], [], False)
  553. )
  554. elif isinstance(sub, VAst.SubstitutionReverseChainingSingleDefinition):
  555. # This is handled in gsubContextLookup()
  556. pass
  557. elif isinstance(sub, VAst.SubstitutionMultipleDefinition):
  558. assert len(glyphs) == 1
  559. statements.append(
  560. ast.MultipleSubstStatement([], glyphs[0], [], replacements)
  561. )
  562. elif isinstance(sub, VAst.SubstitutionLigatureDefinition):
  563. assert len(replacements) == 1
  564. statement = ast.LigatureSubstStatement(
  565. [], glyphs, [], replacements[0], False
  566. )
  567. # If any of the input glyphs is a group, we need to
  568. # explode the substitution into multiple ligature substitutions
  569. # since feature file syntax does not support classes in
  570. # ligature substitutions.
  571. n = max(len(x.glyphSet()) for x in glyphs)
  572. if n > 1:
  573. # All input should either be groups of the same length or single glyphs
  574. assert all(len(x.glyphSet()) in (n, 1) for x in glyphs)
  575. glyphs = [x.glyphSet() for x in glyphs]
  576. glyphs = [([x[0]] * n if len(x) == 1 else x) for x in glyphs]
  577. # In this case ligature replacements must be a group of the same length
  578. # as the input groups, or a single glyph. VOLT
  579. # allows the replacement glyphs to be longer and truncates them.
  580. # So well allow that and zip() below will do the truncation
  581. # for us.
  582. replacement = replacements[0].glyphSet()
  583. if len(replacement) == 1:
  584. replacement = [replacement[0]] * n
  585. assert len(replacement) >= n
  586. # Add the unexploded statement commented out for reference.
  587. statements.append(ast.Comment(f"# {statement}"))
  588. for zipped in zip(*glyphs, replacement):
  589. zipped = [self._glyphName(x) for x in zipped]
  590. statements.append(
  591. ast.LigatureSubstStatement(
  592. [], zipped[:-1], [], zipped[-1], False
  593. )
  594. )
  595. else:
  596. statements.append(statement)
  597. else:
  598. raise NotImplementedError(sub)
  599. def _gsubContextLookup(self, lookup, prefix, suffix, ignore, fealookup, chained):
  600. statements = fealookup.statements
  601. sub = lookup.sub
  602. if isinstance(sub, VAst.SubstitutionReverseChainingSingleDefinition):
  603. # Reverse substitutions is a special case, it can’t use chained lookups.
  604. for key, val in sub.mapping.items():
  605. if not key or not val:
  606. path, line, column = sub.location
  607. log.warning(f"{path}:{line}:{column}: Ignoring empty substitution")
  608. continue
  609. glyphs = self._coverage(key)
  610. replacements = self._coverage(val)
  611. statements.append(
  612. ast.ReverseChainSingleSubstStatement(
  613. prefix, suffix, glyphs, replacements
  614. )
  615. )
  616. fealookup.chained = []
  617. return
  618. if not isinstance(
  619. sub,
  620. (
  621. VAst.SubstitutionSingleDefinition,
  622. VAst.SubstitutionMultipleDefinition,
  623. VAst.SubstitutionLigatureDefinition,
  624. VAst.SubstitutionAlternateDefinition,
  625. ),
  626. ):
  627. raise NotImplementedError(type(sub))
  628. glyphs = []
  629. for key, val in sub.mapping.items():
  630. if not key or not val:
  631. path, line, column = sub.location
  632. log.warning(f"{path}:{line}:{column}: Ignoring empty substitution")
  633. continue
  634. glyphs.extend(self._coverage(key, flatten=True))
  635. if len(glyphs) > 1:
  636. glyphs = [ast.GlyphClass(glyphs)]
  637. if ignore:
  638. statements.append(ast.IgnoreSubstStatement([(prefix, glyphs, suffix)]))
  639. else:
  640. statements.append(
  641. ast.ChainContextSubstStatement(prefix, glyphs, suffix, [chained])
  642. )
  643. def _lookupDefinition(self, lookup):
  644. mark_attachement = None
  645. mark_filtering = None
  646. flags = 0
  647. if lookup.direction == "RTL":
  648. flags |= 1
  649. if not lookup.process_base:
  650. flags |= 2
  651. # FIXME: Does VOLT support this?
  652. # if not lookup.process_ligatures:
  653. # flags |= 4
  654. if not lookup.process_marks:
  655. flags |= 8
  656. elif isinstance(lookup.process_marks, str):
  657. mark_attachement = self._groupName(lookup.process_marks)
  658. elif lookup.mark_glyph_set is not None:
  659. mark_filtering = self._groupName(lookup.mark_glyph_set)
  660. lookupflags = None
  661. if flags or mark_attachement is not None or mark_filtering is not None:
  662. lookupflags = ast.LookupFlagStatement(
  663. flags, mark_attachement, mark_filtering
  664. )
  665. use_extension = False
  666. if self._settings.get("COMPILER_USEEXTENSIONLOOKUPS"):
  667. use_extension = True
  668. if "\\" in lookup.name:
  669. # Merge sub lookups as subtables (lookups named “base\sub”),
  670. # makeotf/feaLib will issue a warning and ignore the subtable
  671. # statement if it is not a pairpos lookup, though.
  672. name = lookup.name.split("\\")[0]
  673. if name.lower() not in self._lookups:
  674. fealookup = Lookup(
  675. self._lookupName(name),
  676. use_extension=use_extension,
  677. )
  678. if lookupflags is not None:
  679. fealookup.statements.append(lookupflags)
  680. fealookup.statements.append(ast.Comment("# " + lookup.name))
  681. else:
  682. fealookup = self._lookups[name.lower()]
  683. fealookup.statements.append(ast.SubtableStatement())
  684. fealookup.statements.append(ast.Comment("# " + lookup.name))
  685. self._lookups[name.lower()] = fealookup
  686. else:
  687. fealookup = Lookup(
  688. self._lookupName(lookup.name),
  689. use_extension=use_extension,
  690. )
  691. if lookupflags is not None:
  692. fealookup.statements.append(lookupflags)
  693. self._lookups[lookup.name.lower()] = fealookup
  694. if lookup.comments is not None:
  695. fealookup.statements.append(ast.Comment("# " + lookup.comments))
  696. contexts = []
  697. for context in lookup.context:
  698. prefix = self._context(context.left)
  699. suffix = self._context(context.right)
  700. ignore = context.ex_or_in == "EXCEPT_CONTEXT"
  701. contexts.append([prefix, suffix, ignore])
  702. # It seems that VOLT will create contextual substitution using
  703. # only the input if there is no other contexts in this lookup.
  704. if ignore and len(lookup.context) == 1:
  705. contexts.append([[], [], False])
  706. if contexts:
  707. chained = ast.LookupBlock(
  708. self._lookupName(lookup.name + " chained"),
  709. use_extension=use_extension,
  710. )
  711. fealookup.chained.append(chained)
  712. if lookup.sub is not None:
  713. self._gsubLookup(lookup, chained)
  714. elif lookup.pos is not None:
  715. self._gposLookup(lookup, chained)
  716. for prefix, suffix, ignore in contexts:
  717. if lookup.sub is not None:
  718. self._gsubContextLookup(
  719. lookup, prefix, suffix, ignore, fealookup, chained
  720. )
  721. elif lookup.pos is not None:
  722. self._gposContextLookup(
  723. lookup, prefix, suffix, ignore, fealookup, chained
  724. )
  725. else:
  726. if lookup.sub is not None:
  727. self._gsubLookup(lookup, fealookup)
  728. elif lookup.pos is not None:
  729. self._gposLookup(lookup, fealookup)
  730. def main(args=None):
  731. """Convert MS VOLT to AFDKO feature files."""
  732. import argparse
  733. from pathlib import Path
  734. from fontTools import configLogger
  735. parser = argparse.ArgumentParser(
  736. "fonttools voltLib.voltToFea", description=main.__doc__
  737. )
  738. parser.add_argument(
  739. "input", metavar="INPUT", type=Path, help="input font/VTP file to process"
  740. )
  741. parser.add_argument(
  742. "featurefile", metavar="OUTPUT", type=Path, help="output feature file"
  743. )
  744. parser.add_argument(
  745. "-t",
  746. "--table",
  747. action="append",
  748. choices=TABLES,
  749. dest="tables",
  750. help="List of tables to write, by default all tables are written",
  751. )
  752. parser.add_argument(
  753. "-q", "--quiet", action="store_true", help="Suppress non-error messages"
  754. )
  755. parser.add_argument(
  756. "--traceback", action="store_true", help="Don’t catch exceptions"
  757. )
  758. options = parser.parse_args(args)
  759. configLogger(level=("ERROR" if options.quiet else "INFO"))
  760. file_or_path = options.input
  761. font = None
  762. try:
  763. font = TTFont(file_or_path)
  764. if "TSIV" in font:
  765. file_or_path = StringIO(font["TSIV"].data.decode("utf-8"))
  766. else:
  767. log.error('"TSIV" table is missing, font was not saved from VOLT?')
  768. return 1
  769. except TTLibError:
  770. pass
  771. converter = VoltToFea(file_or_path, font)
  772. try:
  773. fea = converter.convert(options.tables)
  774. except NotImplementedError as e:
  775. if options.traceback:
  776. raise
  777. location = getattr(e.args[0], "location", None)
  778. message = f'"{e}" is not supported'
  779. if location:
  780. path, line, column = location
  781. log.error(f"{path}:{line}:{column}: {message}")
  782. else:
  783. log.error(message)
  784. return 1
  785. with open(options.featurefile, "w") as feafile:
  786. feafile.write(fea)
  787. if __name__ == "__main__":
  788. import sys
  789. sys.exit(main())