[gentium-commits] [SCM] gentium updated: r44 - trunk/test/type1/Scripts/MakeKerningPairs

Pavel Far?? INVALID.NOREPLY at gnu.org.ua
Tue Mar 22 14:35:03 CET 2022


Author: pavel
Date: 2022-03-22 13:35:02 +0000 (Tue, 22 Mar 2022)
New Revision: 44

Modified:
   trunk/test/type1/Scripts/MakeKerningPairs/make_kerning_pairs.py
Log:
refactor make_kerning_pairs.py

change extension for small caps from '.SC' to '.sc'
generated files now end with '\n' instead of space


Modified: trunk/test/type1/Scripts/MakeKerningPairs/make_kerning_pairs.py
===================================================================
--- trunk/test/type1/Scripts/MakeKerningPairs/make_kerning_pairs.py	2022-03-21 21:34:29 UTC (rev 43)
+++ trunk/test/type1/Scripts/MakeKerningPairs/make_kerning_pairs.py	2022-03-22 13:35:02 UTC (rev 44)
@@ -9,55 +9,57 @@
 # that contain characters in UTF-8 for testing the kerning
 # pairs
 
-import os, sys, string, codecs
+import sys
+import textwrap
 
-# create dictionary with glyphs accessible by PostScript names
-glyphs = {}
-f = open('glyphlist.txt', "r")
-for s in f:
-    if s[0] != "#":
-        [k, v] = s.strip().split(";")
-        if len(v) == 4: # just values 0000--FFFF
-            glyphs[k] = chr(int(v, 16))
-f.close()
+ADOBE_GLYPH_LIST = 'glyphlist.txt'
 
-def glyphtounichar(name):
+input_kerning_name = sys.argv[1]
+
+OUTPUT_REGULAR_NAME = "kerntest.txt"
+OUTPUT_SMALLCAPS_NAME = "kerntest-sc.txt"
+LINE_LENGTH = 74
+SMALLCAPS_EXTENSION = '.sc'
+
+def hexa_string_to_char(s):
+    return chr(int(s, 16))
+
+def glyph_name_to_char(name):
     if name.find("uni") == 0:
-        return chr(int(name[3:], 16))
+        return hexa_string_to_char(name[3:])
     else:
-        return glyphs[name]
+        return glyph_list[name]
 
-# create text file with kerning pairs in utf8 for testing
-fin = open(sys.argv[1], "r")
-fout = codecs.open("kerntest.txt", "w", "utf_8")
-# no small caps
-i = 0
-for s in fin:
-    if s.find(".SC") < 0:
-        [a, b, c, d] = s.split()
-        fout.write(glyphtounichar(b) + glyphtounichar(c))
-        i = i + 1
-        if i == 25:
-            fout.write("\n")
-            i = 0
-        else:
-            fout.write(" ")
-fout.close()
+def write_kerning_pairs_to_file(file_name, kerning_pairs):
+    with open(file_name, 'w') as output_file:
+        for line in textwrap.wrap(kerning_pairs, LINE_LENGTH):
+            output_file.write(line.strip() + '\n')
 
+# create dictionary with glyph_list accessible by PostScript names
+glyph_list = {}
+with open(ADOBE_GLYPH_LIST) as glyph_list_file:
+    for line in glyph_list_file:
+        if line.startswith('#'):  # skip comment
+            continue
+        glyph_name, glyph_hexa_value = line.strip().split(';')
+        if len(glyph_hexa_value) > 4:  # skip some hebrew and arabic
+            continue                   # e.g.: 'daletholam;05D3 05B9'
+        glyph_list[glyph_name] = hexa_string_to_char(glyph_hexa_value)
 
-# create text file with kerning pairs for small caps in utf8 for testing
-fin.seek(0)
-fout = codecs.open("kerntest-sc.txt", "w", "utf_8")
-i = 0
-for s in fin:
-    if s.find(".SC") >= 0:
-        [a, b, c, d] = s.replace(".SC", "").split()
-        fout.write(glyphtounichar(b) + glyphtounichar(c))
-        i = i + 1
-        if i == 25:
-            fout.write("\n")
-            i = 0
+
+# Generate strings with kerning pairs
+kerning_pairs_regular = ''
+kerning_pairs_smallcaps = ''
+with open(input_kerning_name) as input_kerning_file:
+    for line in input_kerning_file:
+        _, first_glyph_name, second_glyph_name, _ = line.replace(SMALLCAPS_EXTENSION,'').split()
+        first_char = glyph_name_to_char(first_glyph_name)
+        second_char = glyph_name_to_char(second_glyph_name)
+        if line.find (SMALLCAPS_EXTENSION) > 0:
+            kerning_pairs_smallcaps += first_char + second_char + ' '
         else:
-            fout.write(" ")
-fout.close()
-fin.close()
+            kerning_pairs_regular += first_char + second_char + ' '
+
+# Generate files with kerning pairs
+write_kerning_pairs_to_file(OUTPUT_REGULAR_NAME, kerning_pairs_regular)
+write_kerning_pairs_to_file(OUTPUT_SMALLCAPS_NAME, kerning_pairs_smallcaps)



More information about the gentium-commits mailing list.