| 1 |
import logging |
| 2 |
from typing import List |
| 3 |
|
| 4 |
from babelfont.Font import Font |
| 5 |
from babelfont.Glyph import GlyphList |
| 6 |
|
| 7 |
logger = logging.getLogger(__name__) |
| 8 |
|
| 9 |
|
| 10 |
def _replaceFeature(font: Font, value: str): |
| 11 |
tag, code = value.split(";", 1) |
| 12 |
logger.info(f"Replacing feature '{tag}'") |
| 13 |
font.features.features = [ |
| 14 |
(key, code if key == tag else value) for key, value in font.features.features |
| 15 |
] |
| 16 |
|
| 17 |
|
| 18 |
def _removeFeatures(font: Font, value: List[str]): |
| 19 |
logger.info(f"Removing features '{', '.join(value)}'") |
| 20 |
font.features.features = [ |
| 21 |
(key, value) for key, value in font.features.features if key not in value |
| 22 |
] |
| 23 |
|
| 24 |
|
| 25 |
def _removeGlyphs(font: Font, value: List[str]): |
| 26 |
logger.info(f"Removing glyphs") |
| 27 |
# for glyph in value: |
| 28 |
# font.glyphs.pop(glyph) |
| 29 |
|
| 30 |
|
| 31 |
def _decomposeGlyphs(font: Font, value: List[str]): |
| 32 |
logger.info(f"Decomposing glyphs") |
| 33 |
for glyph in font.glyphs: |
| 34 |
if glyph.name in value: |
| 35 |
# logger.info(f"Decomposing " + glyph.name) |
| 36 |
for layer in glyph.layers: |
| 37 |
layer.decompose() |
| 38 |
|
| 39 |
|
| 40 |
def _renameGlyphs(font: Font, value: List[str]): |
| 41 |
logger.info(f"Renaming glyphs") |
| 42 |
for mapping in value: |
| 43 |
oldname, newname = mapping.split("=", 1) |
| 44 |
if newname in font.glyphs and oldname in font.glyphs: |
| 45 |
# Swap any unicode mappings |
| 46 |
(font.glyphs[oldname].codepoints, font.glyphs[newname].codepoints) = ( |
| 47 |
font.glyphs[newname].codepoints, |
| 48 |
font.glyphs[oldname].codepoints, |
| 49 |
) |
| 50 |
font.glyphs[newname].name = oldname |
| 51 |
font.glyphs[oldname].name = newname |
| 52 |
font.glyphs = GlyphList({glyph.name: glyph for glyph in font.glyphs}) |
| 53 |
|
| 54 |
|
| 55 |
CUSTOM_PARAMETER_APPLIER = { |
| 56 |
"Replace Feature": _replaceFeature, |
| 57 |
"Remove Features": _removeFeatures, |
| 58 |
"Remove Glyphs": _removeGlyphs, |
| 59 |
"Decompose Glyphs": _decomposeGlyphs, |
| 60 |
"Rename Glyphs": _renameGlyphs, |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
def apply_custom_parameters(font: Font, args=None): |
| 65 |
if "instance" in args: |
| 66 |
root = None |
| 67 |
for instance in font.instances: |
| 68 |
short_instance_name = instance.name.get_default() |
| 69 |
long_instance_name = ( |
| 70 |
instance.familyName.get_default() + " " + short_instance_name |
| 71 |
) |
| 72 |
if args["instance"] in (short_instance_name, long_instance_name): |
| 73 |
root = instance._formatspecific.get("com.glyphsapp", {}) |
| 74 |
break |
| 75 |
if not root: |
| 76 |
raise ValueError(f"Instance '{args['instance']}' not found") |
| 77 |
else: |
| 78 |
root = font._formatspecific.get("com.glyphsapp", {}) |
| 79 |
|
| 80 |
for cp in root.get("customParameters", []): |
| 81 |
name = cp.get("name") |
| 82 |
value = cp.get("value") |
| 83 |
if name in CUSTOM_PARAMETER_APPLIER: |
| 84 |
CUSTOM_PARAMETER_APPLIER[name](font, value) |