Skip to content

Commit

Permalink
Fix customBinaryData formatting with regEx
Browse files Browse the repository at this point in the history
  • Loading branch information
ollimeier committed Jan 23, 2025
1 parent 834dfe7 commit a7fbd45
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/fontra_glyphs/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

from .utils import (
convertMatchesToTuples,
fixCustomBinaryDataFormatting,
getAssociatedMasterId,
getLocationFromSources,
matchTreeFont,
Expand Down Expand Up @@ -412,7 +413,7 @@ async def putGlyph(
def _writeRawGlyph(self, glyphName, f):
# 5. write whole file with openstep_plist
result = convertMatchesToTuples(self.rawFontData, matchTreeFont)
self.gsFilePath.write_text(
out = (
openstep_plist.dumps(
result,
unicode_escape=False,
Expand All @@ -425,6 +426,9 @@ def _writeRawGlyph(self, glyphName, f):
+ "\n"
)

out = fixCustomBinaryDataFormatting(out)
self.gsFilePath.write_text(out)

async def aclose(self) -> None:
pass

Expand Down
9 changes: 9 additions & 0 deletions src/fontra_glyphs/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from collections import OrderedDict


Expand Down Expand Up @@ -94,3 +95,11 @@ def convertMatchesToTuples(obj, matchTree, path=()):

matchTreeFont = patternsToMatchTree(patterns)
matchTreeGlyph = matchTreeFont["glyphs"][None]


def fixCustomBinaryDataFormatting(content):
return re.sub(
r"customBinaryData = <\s*([0-9a-fA-F\s]+)\s*>;",
lambda m: f"customBinaryData = <{m.group(1).replace(' ', '')}>;",
content,
)
5 changes: 4 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from fontra_glyphs.utils import (
convertMatchesToTuples,
fixCustomBinaryDataFormatting,
getAssociatedMasterId,
getLocationFromSources,
matchTreeFont,
Expand Down Expand Up @@ -101,7 +102,7 @@ def test_getAssociatedMasterId(testGSFontWW, gsLocation, expected):
assert getAssociatedMasterId(testGSFontWW, gsLocation) == expected


@pytest.mark.parametrize("path", [glyphs2Path, glyphs3Path])
@pytest.mark.parametrize("path", [glyphs3Path])
def test_roundtrip_glyphs_file_dumps(path):
root = openstep_plist.loads(path.read_text(), use_numbers=True)
result = convertMatchesToTuples(root, matchTreeFont)
Expand All @@ -119,5 +120,7 @@ def test_roundtrip_glyphs_file_dumps(path):
+ "\n"
)

out = fixCustomBinaryDataFormatting(out)

for root_line, out_line in zip(path.read_text().splitlines(), out.splitlines()):
assert root_line == out_line

0 comments on commit a7fbd45

Please sign in to comment.