Skip to content

Commit

Permalink
feat: re-add forced features build
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Oct 20, 2024
1 parent cc2552a commit 9395d7a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 36 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ VENV = . $(VENV_DIR)/bin/activate;

define build-font
@$(VENV) python $(SCRIPTS_DIR)/font.py \
--config "sources/family_config.yaml" \
build $(1)
endef

Expand Down
34 changes: 11 additions & 23 deletions scripts/font.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#!/usr/bin/env python3
"""Lilex helper entrypoint"""
import os
import sys
from argparse import BooleanOptionalAction
from typing import TypedDict

import yaml
from arrrgs import arg, command, global_args, run
from glyphsLib import GSFont
from liblilex import (
FontFormat,
GlyphsFont,
OpenTypeFeatures,
build_family,
force_features,
generate_spacers,
render_ligatures,
)
Expand All @@ -22,7 +21,9 @@
OUT_DIR = "build"

global_args(
arg("--config", "-c", default="family_config.yaml", help="Font config file")
arg("--config", "-c", default="sources/family_config.yaml", help="Font config file"),
arg("--features", "-o", default=None,
help="OpenType features that will be forced to be enabled")
)

AppConfig = TypedDict("AppConfig", {
Expand Down Expand Up @@ -60,6 +61,7 @@ def generate(args, config: AppConfig):

@command(
arg("formats", nargs="*", help="Format list", default=['ttf', 'variable']),
arg("--output", "-o", default=OUT_DIR, help="Output directory"),
arg("--store_temp", "-s", action=BooleanOptionalAction,
help="Not to delete the temporary folder after build")
)
Expand All @@ -77,26 +79,6 @@ async def build(args, config: AppConfig):
await build_family(fonts, config["output"], formats)
print("🟢 Font binaries successfully built")

# def generate_calt(font: GlyphsFont) -> GSFeature:
# glyphs = font.ligatures()
# code = render_ligatures(glyphs) + read_files(f"{FEATURES_DIR}/calt")
# return GSFeature("calt", code)

def move_to_calt(font: GSFont, features: list[str]):
for fea in features:
if fea not in font.features:
print(f"Unknown feature: '{fea}'")
print(font.features)
sys.exit(1)
# Move the code of the feature to the calt,
# which is executed in most cases
feature = font.features[fea]
feature.disabled = True
font.features["calt"].code += "\n" + feature.code
# Remove feature from aalt
aalt = font.features["aalt"]
aalt.code = aalt.code.replace(f"feature {fea};\n", "")

def load_font(args):
with open(args.config, mode="r", encoding="utf-8") as file:
config_file = yaml.safe_load(file)
Expand All @@ -115,6 +97,12 @@ def load_font(args):
"calt": render_ligatures(font.ligatures()),
}
)
if args.features is not None:
forced = args.features.split(",")
forced = map(lambda x: x.strip(), forced)
forced = filter(lambda x: len(x) > 0, forced)
forced = list(forced)
feats = force_features(feats, forced)
generate_spacers(font.ligatures(), font.file.glyphs)
font.set_classes(cls)
font.set_features(feats)
Expand Down
7 changes: 6 additions & 1 deletion scripts/liblilex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
"""Lilex font library"""
from .build import FontFormat, build_family
from .features import OpenTypeFeatures, generate_spacers, render_ligatures
from .features import (
OpenTypeFeatures,
force_features,
generate_spacers,
render_ligatures,
)
from .glyphs_font import GlyphsFont
1 change: 1 addition & 0 deletions scripts/liblilex/features/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""OpenType features utils"""
from .features_loader import OpenTypeFeatures
from .ligatures import render_ligatures
from .mappers import force_features
from .name import feature_prefix, name_from_code
from .spacers import generate_spacers
from .tpl import NAME_TPL
22 changes: 11 additions & 11 deletions scripts/liblilex/features/features_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,21 @@ def items(
data: dict[str, str] = None
) -> tuple[list[GSFeature], list[GSClass]]:
"""Returns a lists of features and classes"""
feat_map: dict[str, str] = {}
for name, feature in self._features.items():
if name in ignore_features:
fea_map: dict[str, str] = {}
for fea_name, feature in self._features.items():
if fea_name in ignore_features:
print(f"Ignoring '{fea_name}'")
continue
fea_name = name
if "/" in name:
fea_name = name.split("/")[0]
if fea_name not in feat_map:
if "/" in fea_name:
fea_name = fea_name.split("/")[0]
if fea_name not in fea_map:
if data is not None and fea_name in data:
feat_map[fea_name] = data[fea_name] + "\n" + feature.code
fea_map[fea_name] = data[fea_name] + "\n" + feature.code
else:
feat_map[fea_name] = feature.code
fea_map[fea_name] = feature.code
else:
feat_map[fea_name] += "\n" + feature.code
fea_map[fea_name] += "\n" + feature.code
feats = []
for name, code in feat_map.items():
for name, code in fea_map.items():
feats.append(GSFeature(name, code))
return feats, self._classes
27 changes: 27 additions & 0 deletions scripts/liblilex/features/mappers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""OpenType feature mappers"""
import sys

from glyphsLib import GSFeature

FeatureDict = dict[str, GSFeature]

def to_feature_dict(features: list[GSFeature]) -> FeatureDict:
return {fea.name: fea for fea in features}

def from_feature_dict(feature_dict: FeatureDict) -> list[GSFeature]:
return list(feature_dict.values())

def force_features(features: list[GSFeature], forced: list[str]):
fea_dict = to_feature_dict(features)
for fea in forced:
if fea not in fea_dict:
print(f"Unknown feature: '{fea}'")
sys.exit(1)
# Move the code of the feature to the calt,
# which is executed in most cases
fea_dict[fea].disabled = True
fea_dict["calt"].code += "\n" + fea_dict[fea].code
# Remove feature from aalt
aalt = fea_dict["aalt"]
aalt.code = aalt.code.replace(f"feature {fea};\n", "")
return from_feature_dict(fea_dict)

0 comments on commit 9395d7a

Please sign in to comment.