Skip to content

Commit

Permalink
[sample] Improve feature parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledhosny committed Nov 3, 2024
1 parent e74b5c7 commit 7b9c7c4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
31 changes: 23 additions & 8 deletions Lib/alifTools/sample/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,39 @@ def _parse_features(text: str) -> dict[str, list[list[int]]]:
return {}
features = {}
for feature in text.split(","):
value = 1
start = 0
end = -1
value = None
start = None
end = None

if feature[0] == "-":
value = 0
if feature[0] in ("+", "-"):
feature = feature[1:]
tag = feature
if "[" in feature:
tag, extra = feature.split("[")
if "=" in extra:
extra, value = extra.split("=")
if "=" in tag:
tag, value = tag.split("=")
if "[" in tag:
tag, extra = tag.split("[")
if extra[-1] == "]":
extra = extra[:-1]
start = end = extra
if ":" in extra:
start, end = extra.split(":")
features[tag] = [[int(value), int(start), int(end)]]
print(tag, value, start, end)
if value is None:
value = 1
if start is None or start == "":
start = 0
if end is None or end == "":
end = 0xFFFFFFFF
features.setdefault(tag, []).append(
[int(start), int(end), bool(int(value))]
)
for tag, value in features.items():
if value == [[0, 0xFFFFFFFF, 1]]:
features[tag] = True
if value == [[0, 0xFFFFFFFF, 0]]:
features[tag] = False
return features

def shape(
Expand Down
27 changes: 27 additions & 0 deletions tests/sample_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
from alifTools.sample import Font


HHB_FEATURE_GLOBAL_END = 0xFFFFFFFF


@pytest.mark.parametrize(
"text,features",
[
("aalt", {"aalt": True}),
("+aalt,liga", {"aalt": True, "liga": True}),
("aalt,liga,+dlig", {"aalt": True, "liga": True, "dlig": True}),
("-aalt", {"aalt": False}),
("aalt=0,liga", {"aalt": False, "liga": True}),
("aalt=1,liga=0", {"aalt": True, "liga": False}),
("aalt,-liga,dlig", {"aalt": True, "liga": False, "dlig": True}),
("aalt[2:4]", {"aalt": [[2, 4, True]]}),
("aalt[2:4]=1", {"aalt": [[2, 4, True]]}),
("aalt[2:4]=0", {"aalt": [[2, 4, False]]}),
("-aalt[2:4]", {"aalt": [[2, 4, False]]}),
("aalt[2:]", {"aalt": [[2, HHB_FEATURE_GLOBAL_END, True]]}),
("-aalt[2:]", {"aalt": [[2, HHB_FEATURE_GLOBAL_END, False]]}),
],
)
def test_parse_feature(text, features):
assert Font._parse_features(text) == features

0 comments on commit 7b9c7c4

Please sign in to comment.