| 1 |
"""ttLib: Interfacing with TrueType fonts. |
| 2 |
|
| 3 |
This package contains routines for converting between fontTools objects |
| 4 |
(representing TrueType/OpenType fonts) and fontFeatures. This particular |
| 5 |
module is mainly concerned with getting information out of binary OTF/TTF |
| 6 |
fonts and into fontFeatures.""" |
| 7 |
from collections import OrderedDict |
| 8 |
|
| 9 |
from .GDEFUnparser import GDEFUnparser |
| 10 |
from .GSUBUnparser import GSUBUnparser |
| 11 |
from .GPOSUnparser import GPOSUnparser |
| 12 |
|
| 13 |
|
| 14 |
def unparseLanguageSystems(tables): |
| 15 |
"""Build a set of script / language pairs from a GSUB/GPOS table. |
| 16 |
|
| 17 |
Args: |
| 18 |
tables: A list of ``fontTools.ttLib.tables.G_S_U_B_.table_G_S_U_B_`` / |
| 19 |
``fontTools.ttLib.tables.G_P_O_S_.table_G_P_O_S_`` objects. |
| 20 |
|
| 21 |
Returns an ordered dictionary whose keys are four-character script tags and |
| 22 |
whose values are a set of four-character language tags. |
| 23 |
""" |
| 24 |
scripts = OrderedDict() |
| 25 |
for table in tables: |
| 26 |
if not table.table.ScriptList: |
| 27 |
continue |
| 28 |
for scriptRecord in table.table.ScriptList.ScriptRecord: |
| 29 |
scriptTag = scriptRecord.ScriptTag |
| 30 |
languages = scripts.get(scriptTag, []) |
| 31 |
script = scriptRecord.Script |
| 32 |
items = [] |
| 33 |
if script.DefaultLangSys is not None: |
| 34 |
items.append(("dflt", script.DefaultLangSys)) |
| 35 |
items += [(l.LangSysTag, l.LangSys) for l in script.LangSysRecord] |
| 36 |
languages = set([i[0] for i in items]) |
| 37 |
|
| 38 |
if languages and scriptTag not in scripts: |
| 39 |
scripts[scriptTag] = languages |
| 40 |
|
| 41 |
return scripts |
| 42 |
|
| 43 |
|
| 44 |
def unparse(font, do_gdef=False, doLookups=True, config={}): |
| 45 |
"""Convert a binary OpenType font into a fontFeatures object |
| 46 |
|
| 47 |
Args: |
| 48 |
font: A ``TTFont`` object. |
| 49 |
do_gdef: Boolean. Whether the GDEF table should also be read. |
| 50 |
doLookups: Whether the lookups should be read, or just the script/language |
| 51 |
information and top-level features. |
| 52 |
config: A dictionary of glyph class and routine names. |
| 53 |
""" |
| 54 |
gsub_gpos = [font[tableTag] for tableTag in ("GSUB", "GPOS") if tableTag in font] |
| 55 |
from fontFeatures import FontFeatures |
| 56 |
|
| 57 |
ff = FontFeatures() |
| 58 |
|
| 59 |
languageSystems = unparseLanguageSystems(gsub_gpos) |
| 60 |
|
| 61 |
if "GSUB" in font: |
| 62 |
GSUBUnparser( |
| 63 |
font["GSUB"], ff, languageSystems, font=font, config=config |
| 64 |
).unparse(doLookups=doLookups) |
| 65 |
|
| 66 |
if "GPOS" in font: |
| 67 |
GPOSUnparser( |
| 68 |
font["GPOS"], ff, languageSystems, font=font, config=config |
| 69 |
).unparse(doLookups=doLookups) |
| 70 |
|
| 71 |
if "GDEF" in font and do_gdef: |
| 72 |
GDEFUnparser(font["GDEF"], ff).unparse() |
| 73 |
|
| 74 |
return ff |