Skip to content

Commit

Permalink
font-patcher: Sanitize output filenames
Browse files Browse the repository at this point in the history
[why]
The filename is determined by the font (family) name. The font name
might include characters that are forbidden to use in filenames.

[how]
Filter output filesnames and prevent any character that is likely
forbidden under Windows.
Also prevent control characters.

Translate Windows path separators to posix.

Fixes: #632

Signed-off-by: Fini Jastrow <[email protected]>
  • Loading branch information
Finii committed Dec 22, 2022
1 parent 52cf6f9 commit 5c5c51e
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions font-patcher
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from __future__ import absolute_import, print_function, unicode_literals

# Change the script version when you edit this script:
script_version = "3.2.1"
script_version = "3.2.2"

version = "2.3.0-RC"
projectName = "Nerd Fonts"
Expand Down Expand Up @@ -322,15 +322,19 @@ class font_patcher:
if not sourceFont.layers[l].is_background:
layer = l
break
outfile = os.path.normpath(os.path.join(self.args.outputdir, sourceFont.familyname + ".ttc"))
outfile = os.path.normpath(os.path.join(
sanitize_filename(self.args.outputdir, True),
sanitize_filename(sourceFont.familyname) + ".ttc"))
# the `PfEd-comments` flag is required for Fontforge to save '.comment' and '.fontlog'.
sourceFonts[0].generateTtc(outfile, sourceFonts[1:], flags=(str('opentype'), str('PfEd-comments')), layer=layer)
message = "\nGenerated: {} fonts in '{}'".format(len(sourceFonts), outfile)
else:
fontname = sourceFont.fullname
if not fontname:
fontname = sourceFont.cidfontname
outfile = os.path.normpath(os.path.join(self.args.outputdir, fontname + self.args.extension))
outfile = os.path.normpath(os.path.join(
sanitize_filename(self.args.outputdir, True),
sanitize_filename(fontname) + self.args.extension))
# the `PfEd-comments` flag is required for Fontforge to save '.comment' and '.fontlog'.
bitmaps = str()
if len(self.sourceFont.bitmapSizes):
Expand Down Expand Up @@ -1209,6 +1213,20 @@ def make_sure_path_exists(path):
if exception.errno != errno.EEXIST:
raise

def sanitize_filename(filename, allow_dirs = False):
""" Enforces to not use forbitten characters in a filename/path. """
if filename == '.' and not allow_dirs:
return '_'
trans = filename.maketrans('<>:"|?*', '_______')
for i in range(0x00, 0x20):
trans[i] = ord('_')
if not allow_dirs:
trans[ord('/')] = ord('_')
trans[ord('\\')] = ord('_')
else:
trans[ord('\\')] = ord('/') # We use posix paths
return filename.translate(trans)

def get_multiglyph_boundingBox(glyphs, destGlyph = None):
""" Returns dict of the dimensions of multiple glyphs combined(, as if they are copied into destGlyph) """
# If destGlyph is given the glyph(s) are first copied over into that
Expand Down

0 comments on commit 5c5c51e

Please sign in to comment.