| 1 |
# from fontFeatures import Attachment, Routine, Positioning, ValueRecord |
| 2 |
import logging |
| 3 |
from fontTools.feaLib.builder import addOpenTypeFeaturesFromString |
| 4 |
from fontTools.ttLib import newTable |
| 5 |
from fontTools.ttLib.tables import otBase, otTables |
| 6 |
|
| 7 |
logger = logging.getLogger(__name__) |
| 8 |
|
| 9 |
|
| 10 |
def build_all_features(font, ttFont): |
| 11 |
logger.info("Generating opentype features") |
| 12 |
|
| 13 |
# build_cursive(font) |
| 14 |
# build_mark_mkmk(font) |
| 15 |
# build_mark_mkmk(font, "mkmk") |
| 16 |
# build_kern(font) |
| 17 |
|
| 18 |
features = font.features.to_fea() |
| 19 |
logger.info("Compiling opentype features") |
| 20 |
addOpenTypeFeaturesFromString(ttFont, features) |
| 21 |
add_gdef_classdef(font, ttFont) |
| 22 |
|
| 23 |
|
| 24 |
CATMAP = {"base": 1, "ligature": 2, "mark": 3, "component": 4} |
| 25 |
|
| 26 |
|
| 27 |
def add_gdef_classdef(font, ttFont): |
| 28 |
if not "GDEF" in ttFont: |
| 29 |
ttFont["GDEF"] = newTable("GDEF") |
| 30 |
gdef = otTables.GDEF() |
| 31 |
gdef.Version = 0x00010000 |
| 32 |
ttFont["GDEF"].table = gdef |
| 33 |
else: |
| 34 |
gdef = ttFont["GDEF"].table |
| 35 |
if not hasattr(gdef, "GlyphClassDef"): |
| 36 |
gdef.GlyphClassDef = otTables.GlyphClassDef() |
| 37 |
gdef.GlyphClassDef.classDefs = {} |
| 38 |
classdeftable = gdef.GlyphClassDef.classDefs |
| 39 |
if not classdeftable: |
| 40 |
for glyph in font.glyphs: |
| 41 |
if glyph.category in CATMAP: |
| 42 |
classdeftable[glyph.name] = CATMAP[glyph.category] |
| 43 |
|
| 44 |
|
| 45 |
def build_kern(font): |
| 46 |
def _expand_class(c): |
| 47 |
if c[0] != "@": |
| 48 |
return [c] |
| 49 |
if c[1:] not in font.features.namedClasses: |
| 50 |
logging.info("Attempted to use undefined kerning class %s" % c) |
| 51 |
return None |
| 52 |
return [x for x in font.features.namedClasses[c[1:]] if font.glyphs[x].exported] |
| 53 |
|
| 54 |
kernroutine = Routine() |
| 55 |
for (left, right), kern in font._all_kerning.items(): |
| 56 |
left, right = _expand_class(left), _expand_class(right) |
| 57 |
if left is None or right is None: |
| 58 |
continue |
| 59 |
kernroutine.rules.append( |
| 60 |
Positioning( |
| 61 |
[left, right], |
| 62 |
[ValueRecord(xAdvance=kern), ValueRecord()], |
| 63 |
) |
| 64 |
) |
| 65 |
font.features.addFeature("kern", [kernroutine]) |
| 66 |
|
| 67 |
|
| 68 |
def build_cursive(font): |
| 69 |
anchors = font._all_anchors |
| 70 |
if "entry" in anchors and "exit" in anchors: |
| 71 |
attach = Attachment( |
| 72 |
"entry", "exit", anchors["entry"], anchors["exit"], flags=(0x8 | 0x1) |
| 73 |
) |
| 74 |
r = Routine( |
| 75 |
rules=[attach], |
| 76 |
) |
| 77 |
font.features.addFeature("curs", [r]) |
| 78 |
|
| 79 |
|
| 80 |
def build_mark_mkmk(font, which="mark", strict=False): |
| 81 |
# Find matching pairs of foo/_foo anchors |
| 82 |
anchors = font._all_anchors |
| 83 |
r = Routine(rules=[]) |
| 84 |
if which == "mark": |
| 85 |
basecategory = "base" |
| 86 |
else: |
| 87 |
basecategory = "mark" |
| 88 |
for baseanchor in anchors: |
| 89 |
markanchor = "_" + baseanchor |
| 90 |
if markanchor not in anchors: |
| 91 |
continue |
| 92 |
# Filter glyphs to those which are baseanchors |
| 93 |
bases = { |
| 94 |
k: v |
| 95 |
for k, v in anchors[baseanchor].items() |
| 96 |
if font.glyphs[k].exported and (font.glyphs[k].category == basecategory) |
| 97 |
} |
| 98 |
marks = { |
| 99 |
k: v |
| 100 |
for k, v in anchors[markanchor].items() |
| 101 |
if font.glyphs[k].exported |
| 102 |
and (not strict or font.glyphs[k].category == "mark") |
| 103 |
} |
| 104 |
if not (bases and marks): |
| 105 |
continue |
| 106 |
attach = Attachment(baseanchor, markanchor, bases, marks) |
| 107 |
attach.fontfeatures = font.features # THIS IS A TERRIBLE HACK |
| 108 |
r.rules.append(attach) |
| 109 |
if r.rules: |
| 110 |
font.features.addFeature(which, [r]) |