Ticket #6: test.py

File test.py, 1.4 kB (added by erik, 3 years ago)
Line 
1
2 def glyphNameToFileName(glyphName, glyphSet):
3         """Default algorithm for making a file name out of a glyph name.
4         This one has limited support for case insensitive file systems:
5         it assumes glyph names are not case sensitive apart from the first
6         character:
7                 'a'     -> 'a.glif'
8                 'A'     -> 'A_.glif'
9                 'A.alt' -> 'A_.alt.glif'
10                 'A.Alt' -> 'A_.Alt.glif'  # this one would cause problems
11         """
12         if glyphName.startswith("."):
13                 # some OSes consider filenames such as .notdef "hidden"
14                 glyphName = "_" + glyphName[1:]
15         parts = glyphName.split(".")
16         if parts[0].find("_")!=-1:
17                 # it is a compound name, check the separate parts
18                 bits = []
19                 for p in parts[0].split("_"):
20                         if p != p.lower():
21                                 bits.append(p+"_")
22                                 continue
23                         bits.append(p)
24                 parts[0] = "_".join(bits)
25         else:
26                 # it is a single name
27                 if parts[0] != parts[0].lower():
28                         parts[0] += "_"
29         for i in range(1, len(parts)):
30                 # resolve additional, period separated parts, like alt / Alt
31                 if parts[i] != parts[i].lower():
32                         parts[i] += "_"
33         return ".".join(parts) + ".glif"
34
35
36 names = [
37         ('.notdef', '_notdef.glif'),
38         ('a', 'a.glif'),
39         ('A', 'A_.glif'),
40         ('A.alt', 'A_.alt.glif'),
41         ('A.Alt', 'A_.Alt_.glif'),
42         ('T_H', "T__H_.glif"),
43         ("T_h", "T__h.glif"),
44         ("t_h", "t_h.glif"),
45         ('F_F_I', "F__F__I_.glif"),
46         ('f_f_i', "f_f_i.glif")
47         ]
48
49
50 for name, want in names:
51         filename = glyphNameToFileName(name, None)
52         print name, "\t", filename
53         assert filename == want