root/applicationScripts/FontLab/Production/GlyphOrder-Set.py

Revision 667, 3.0 kB (checked in by tal, 1 year ago)
Some scripts that help with glyph order and Unicode values in all open fonts.
Line 
1 # Glyph Order: Set
2 """Set encoding in all open fonts."""
3
4 import os
5 from robofab.world import AllFonts
6 import dialogKit
7 from FL import *
8 import fl_cmd
9
10 # ---------------
11 # Encoding Search
12 # ---------------
13
14 resolutionOrder = [
15     "/Library/Application Support/FontLab/Encoding",
16     "/Library/Application Support/FontLab/Studio 5/Encoding",
17     os.path.join(os.getenv("HOME"), "Library", "Application Support", "FontLab", "Encoding"),
18     os.path.join(os.getenv("HOME"), "Library", "Application Support", "FontLab", "Studio 5", "Encoding"),
19 ]
20
21 def findEncodings():
22     encodings = {}
23     for path in resolutionOrder:
24         if not os.path.exists(path):
25             continue
26         for encodingPath in runFolder(path):
27             name = skimEncoding(encodingPath)
28             encodings[name] = encodingPath
29     return encodings
30
31 def runFolder(path):
32     found = []
33     for fileName in os.listdir(path):
34         p = os.path.join(path, fileName)
35         if os.path.isdir(p):
36             found += runFolder(p)
37         elif os.path.splitext(fileName)[-1].lower() == ".enc":
38             found.append(p)
39     return found
40
41 def skimEncoding(path):
42     f = open(path, "rb")
43     text = f.read()
44     f.close()
45     text = text.replace("\r", "\n")
46     lines = [line for line in text.splitlines() if line]
47     # get the name and the group
48     name = None
49     group = None
50     if lines[0].startswith("%%FONTLAB ENCODING: "):
51         index, name = lines[0].split(";")
52         name = name.strip()
53         index = index.split(" ")[-1].replace(";", "")
54         lines = lines[1:]
55     if lines[0].startswith("%%GROUP:"):
56         group = lines[0].replace("%%GROUP:", "").strip()
57     # compile the name
58     fullName = None
59     if group is not None:
60         if name is not None:
61             fullName = group + "-" + name
62         else:
63             fullName = group + "-<undefined>"
64     else:
65         if name is None:
66             fullName = "<undefined>"
67         else:
68             fullName = name
69     return fullName
70
71
72 # ---------
73 # Interface
74 # ---------
75
76 class BatchEncodingDialog(object):
77
78     def __init__(self):
79         self.encodings = findEncodings().items()
80         self.encodings.sort()
81         names = [name for name, path in self.encodings]
82
83         self.w = dialogKit.ModalDialog((500, 300), "Set Encoding in All Fonts", okCallback=self.okCallback)
84         self.w.l = dialogKit.List((12, 12, -12, -60), names)
85         self.w.open()
86
87     def okCallback(self, sender):
88         selection = self.w.l.getSelection()
89         if not selection:
90             print "Error: No encoding selected!"
91             return
92         fonts = AllFonts()
93         if not fonts:
94             print "Error: No fonts!"
95             return
96         print "Setting Encoding..."
97         name, path = self.encodings[selection[0]]
98         for font in fonts:
99             print os.path.basename(font.path)
100             fl.ifont = font.fontIndex
101             naked = font.naked()
102             encObject = naked.encoding
103             encObject.Load(path)
104             font.update()
105             fl.CallCommand(fl_cmd.FontSortByCodepage)
106
107 BatchEncodingDialog()
Note: See TracBrowser for help on using the browser.