[gentium-commits] [SCM] gentium updated: r43 - trunk/test/type1/Scripts/SplitKerns

Pavel Far?? INVALID.NOREPLY at gnu.org.ua
Mon Mar 21 22:34:29 CET 2022


Author: pavel
Date: 2022-03-21 21:34:29 +0000 (Mon, 21 Mar 2022)
New Revision: 43

Modified:
   trunk/test/type1/Scripts/SplitKerns/split_kerning_pairs.py
Log:
refactor split_kerning_pairs.py

The old version incorrectly considered .notdef and the encoding
name as characters, however the result (even for the old version
was correct, because these two were not in the extra kerns).


Modified: trunk/test/type1/Scripts/SplitKerns/split_kerning_pairs.py
===================================================================
--- trunk/test/type1/Scripts/SplitKerns/split_kerning_pairs.py	2022-03-21 18:31:24 UTC (rev 42)
+++ trunk/test/type1/Scripts/SplitKerns/split_kerning_pairs.py	2022-03-21 21:34:29 UTC (rev 43)
@@ -3,52 +3,55 @@
 # Splits kerning pairs into T1, L7x and rest
 # Needs one argument--the afm file to split
 
-import os, sys, string
+import sys
 
 ENC_PATH = '../../../../gentium/fonts/enc/dvips/gentium-tug/'
+T1_ENC_NAME = ENC_PATH + 'gentium-ec.enc'
+L7X_ENC_NAME = ENC_PATH + 'gentium-l7x.enc'
 
-# create set with glyphs in T1 (normal and small caps)
-t1glyphs = set("")
-f = open(ENC_PATH + 'gentium-ec.enc', "r")
-for s in f:
-    if s[0] == "/":
-        t1glyphs.add(s[1:-1])
-f.close()
-f = open(ENC_PATH + 'gentium-ec-sc.enc', "r")
-for s in f:
-    if s[0] == "/":
-        t1glyphs.add(s[1:-1])
-f.close()
+input_kern_file = sys.argv[1]
 
-# create set with glyphs in L7x (normal and small caps)
-l7xglyphs = set("")
-f = open(ENC_PATH + 'gentium-l7x.enc', "r")
-for s in f:
-    if s[0] == "/":
-        l7xglyphs.add(s[1:-1])
-f.close()
-f = open(ENC_PATH + 'gentium-l7x-sc.enc', "r")
-for s in f:
-    if s[0] == "/":
-        l7xglyphs.add(s[1:-1])
-f.close()
+T1_OUTPUT_FILE  = "kerns-t1.afm"
+L7X_OUTPUT_FILE  = "kerns-l7x.afm"
+REST_OUTPUT_FILE  = "kerns-rest.afm"
 
+# add one glyph, remove non-glyphs
+def add_one_glyph(glyph_line, glyph_list):
+    if glyph_line.startswith('/.notdef'):  # skip .notdef
+        return
+    if glyph_line.startswith('/gentium-'):  # skip the encoding name
+        return
+    from_character = 1  # remove the first character '/'
+    to_character = glyph_line.find(" %")  # till the end or before comment if present
+    glyph_name = glyph_line[from_character:to_character]
+    glyph_list.add(glyph_name)
 
-# create text file with kerning pairs for T1
-fin = open(sys.argv[1], "r")
-ft1 = open("kerns-t1.afm", "w")
-fl7x = open("kerns-l7x.afm", "w")
-frest = open("kerns-rest.afm", "w")
-i = 0
-for s in fin:
-    [a, b, c, d] = s.split()
-    if b in t1glyphs and c in t1glyphs:
-        ft1.write(s)
-    elif b in l7xglyphs and c in l7xglyphs:
-        fl7x.write(s)
-    else:
-        frest.write(s)
-frest.close()
-fl7x.close()
-ft1.close()
-fin.close()
+# create set with glyphs in T1
+t1_glyph_names = set("")
+with open(T1_ENC_NAME) as t1_encoding_file:
+    for line in t1_encoding_file:
+        if line.startswith('/'):
+            add_one_glyph(line, t1_glyph_names)
+
+# create set with glyphs in L7x
+l7x_glyph_names = set("")
+with open(L7X_ENC_NAME) as l7x_encoding_file:
+    for line in l7x_encoding_file:
+        if line.startswith('/'):
+            add_one_glyph(line, l7x_glyph_names)
+
+
+# split the kerning file to encodings T1, L7x and rest
+with open(input_kern_file) as input_file, \
+        open(T1_OUTPUT_FILE, "w") as t1_output_file, \
+        open(L7X_OUTPUT_FILE, "w") as l7x_output_file, \
+        open(REST_OUTPUT_FILE, "w") as rest_output_file:
+
+    for line in input_file:
+        _, first_glyph, second_glyph, _ = line.split()
+        if first_glyph in t1_glyph_names and second_glyph in t1_glyph_names:
+            t1_output_file.write(line)
+        elif first_glyph in l7x_glyph_names and second_glyph in l7x_glyph_names:
+            l7x_output_file.write(line)
+        else:
+            rest_output_file.write(line)



More information about the gentium-commits mailing list.