From b7db9ecef93e743286685df57e75dd30acc9282e Mon Sep 17 00:00:00 2001 From: Just van Rossum Date: Sat, 2 Dec 2023 20:02:11 +0100 Subject: [PATCH] Prepend U+ to code points in .csv, but still allow 'bare' hex values when reading --- src/fontra/backends/fontra.py | 17 +++- .../fonts/MutatorSans.fontra/glyph-info.csv | 92 +++++++++---------- 2 files changed, 61 insertions(+), 48 deletions(-) diff --git a/src/fontra/backends/fontra.py b/src/fontra/backends/fontra.py index 3509934ae..e5801f682 100644 --- a/src/fontra/backends/fontra.py +++ b/src/fontra/backends/fontra.py @@ -110,7 +110,7 @@ def _readGlyphInfo(self): for row in reader: glyphName, *rest = row if rest: - codePoints = [int(cp, 16) for cp in rest[0].split(",") if cp] + codePoints = _parseCodePoints(rest[0]) else: codePoints = [] self.glyphMap[glyphName] = codePoints @@ -120,7 +120,7 @@ def _writeGlyphInfo(self): writer = csv.writer(file, delimiter=";") writer.writerow(["glyph name", "code points"]) for glyphName, codePoints in sorted(self.glyphMap.items()): - codePoints = ",".join(f"{cp:04X}" for cp in codePoints) + codePoints = ",".join(f"U+{cp:04X}" for cp in codePoints) writer.writerow([glyphName, codePoints]) def _readFontData(self): @@ -144,6 +144,19 @@ def getGlyphFilePath(self, glyphName): return self.glyphsDir / (stringToFileName(glyphName) + ".json") +def _parseCodePoints(cell): + codePoints = [] + cell = cell.strip() + if cell: + for s in cell.split(","): + s = s.strip() + # U+ should become mandatory, but for now let's be lenient + if s.startswith("U+"): + s = s[2:] + codePoints.append(int(s, 16)) + return codePoints + + def serializeGlyph(glyph, glyphName=None): glyph = glyph.convertToPaths() jsonGlyph = unstructure(glyph) diff --git a/test-common/fonts/MutatorSans.fontra/glyph-info.csv b/test-common/fonts/MutatorSans.fontra/glyph-info.csv index d3d62adc7..7d67ab47b 100644 --- a/test-common/fonts/MutatorSans.fontra/glyph-info.csv +++ b/test-common/fonts/MutatorSans.fontra/glyph-info.csv @@ -1,55 +1,55 @@ glyph name;code points -A;0041,0061 -Aacute;00C1,00E1 -Adieresis;00C4,00E4 -B;0042,0062 -C;0043,0063 -D;0044,0064 -E;0045,0065 -F;0046,0066 -G;0047,0067 -H;0048,0068 -I;0049,0069 +A;U+0041,U+0061 +Aacute;U+00C1,U+00E1 +Adieresis;U+00C4,U+00E4 +B;U+0042,U+0062 +C;U+0043,U+0063 +D;U+0044,U+0064 +E;U+0045,U+0065 +F;U+0046,U+0066 +G;U+0047,U+0067 +H;U+0048,U+0068 +I;U+0049,U+0069 I.narrow; IJ; -J;004A,006A +J;U+004A,U+006A J.narrow; -K;004B,006B -L;004C,006C -M;004D,006D -N;004E,006E -O;004F,006F -P;0050,0070 -Q;0051,0071 -R;0052,0072 +K;U+004B,U+006B +L;U+004C,U+006C +M;U+004D,U+006D +N;U+004E,U+006E +O;U+004F,U+006F +P;U+0050,U+0070 +Q;U+0051,U+0071 +R;U+0052,U+0072 R.alt; -S;0053,0073 +S;U+0053,U+0073 S.closed; -T;0054,0074 -U;0055,0075 -V;0056,0076 -W;0057,0077 -X;0058,0078 -Y;0059,0079 -Z;005A,007A -acute;00B4 -arrowdown;2193 -arrowleft;2190 -arrowright;2192 -arrowup;2191 -colon;003A -comma;002C -dieresis;00A8 -dot;27D1 +T;U+0054,U+0074 +U;U+0055,U+0075 +V;U+0056,U+0076 +W;U+0057,U+0077 +X;U+0058,U+0078 +Y;U+0059,U+0079 +Z;U+005A,U+007A +acute;U+00B4 +arrowdown;U+2193 +arrowleft;U+2190 +arrowright;U+2192 +arrowup;U+2191 +colon;U+003A +comma;U+002C +dieresis;U+00A8 +dot;U+27D1 em; nestedcomponents; nlitest; -period;002E -quotedblbase;201E -quotedblleft;201C -quotedblright;201D -quotesinglbase;201A -semicolon;003B -space;0020 -varcotest1;E000 -varcotest2;E001 +period;U+002E +quotedblbase;U+201E +quotedblleft;U+201C +quotedblright;U+201D +quotesinglbase;U+201A +semicolon;U+003B +space;U+0020 +varcotest1;U+E000 +varcotest2;U+E001