This repository has been archived on 2025-10-02. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
inter-font/misc/pylib/robofab/tools/glyphNameSchemes.py
Rasmus Andersson 8234b62ab7 Speeds up font compilation by around 200%
Cython is used to compile some hot paths into native Python extensions.
These hot paths were identified through running ufocompile with the hotshot
profiler and then converting file by file to Cython, starting with the "hottest"
paths and continuing until returns were deminishing. This means that only a few
Python files were converted to Cython.

Closes #23
Closes #20 (really this time)
2017-09-04 11:12:34 -04:00

41 lines
1.3 KiB
Python
Executable file

"""A separate module for glyphname to filename functions.
glyphNameToShortFileName() generates a non-clashing filename for systems with
filename-length limitations.
"""
MAXLEN = 31
def glyphNameToShortFileName(glyphName, glyphSet):
"""Alternative glyphname to filename function.
Features a garuanteed maximum filename for really long glyphnames, and clash testing.
- all non-ascii characters are converted to "_" (underscore), including "."
- all glyphnames which are too long are truncated and a hash is added at the end
- the hash is generated from the whole glyphname
- finally, the candidate glyphname is checked against the contents.plist
and a incrementing number is added at the end if there is a clash.
"""
import binascii, struct, string
ext = ".glif"
ok = string.ascii_letters + string.digits + " _"
h = binascii.hexlify(struct.pack(">l", binascii.crc32(glyphName)))
n = ''
for c in glyphName:
if c in ok:
if c != c.lower():
n += c + "_"
else:
n += c
else:
n += "_"
if len(n + ext) < MAXLEN:
return n + ext
count = 0
candidate = n[:MAXLEN - len(h + ext)] + h + ext
if glyphSet is not None:
names = glyphSet.getReverseContents()
while candidate.lower() in names:
candidate = n[:MAXLEN - len(h + ext + str(count))] + h + str(count) + ext
count += 1
return candidate