1
1
mirror of https://github.com/i-tu/Hasklig.git synced 2024-09-11 10:36:46 +03:00

Make addSVGtable to python 2/3 compatible

This commit is contained in:
Curzy 2017-09-13 21:10:14 +09:00
parent 3592ef6fdc
commit ac902f44b0

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python
from __future__ import print_function
"""
Adds an SVG table to a TTF or OTF font.
@ -12,7 +13,7 @@ import re
try:
from fontTools import ttLib, version
except ImportError:
print >> sys.stderr, "ERROR: FontTools Python module is not installed."
print("ERROR: FontTools Python module is not installed.", file=sys.stderr)
sys.exit(1)
TABLE_TAG = 'SVG '
@ -34,9 +35,9 @@ def readFile(filePath):
def setIDvalue(data, gid):
id = reIDvalue.search(data)
if id:
newData = re.sub(id.group(1), 'id="glyph%s"' % gid, data)
newData = re.sub(id.group(1), 'id="glyph{}"'.format(gid), data)
else:
newData = re.sub('<svg', '<svg id="glyph%s"' % gid, data)
newData = re.sub('<svg', '<svg id="glyph{}"'.format(gid), data)
return newData
@ -67,7 +68,12 @@ def processFontFile(fontFilePath, svgFilePathsList):
try:
gid = font.getGlyphID(gName)
except KeyError:
print >> sys.stderr, "ERROR: Could not find a glyph named %s in the font %s." % (gName, os.path.split(fontFilePath)[1])
print(
"ERROR: Could not find a glyph named {} in the font {}.".format(
gName, os.path.split(fontFilePath)[1]
),
file=sys.stderr
)
continue
svgItemsList = []
@ -83,7 +89,11 @@ def processFontFile(fontFilePath, svgFilePathsList):
# don't do any changes to the source OTF/TTF font if there's no SVG data
if not svgDocsDict:
print >> sys.stderr, "ERROR: Could not find any artwork files that can be added to the font."
print(
"ERROR: Could not find any artwork files "
"that can be added to the font.",
file=sys.stderr
)
return
svgDocsList = [svgDocsDict[index] for index in sorted(svgDocsDict.keys())]
@ -98,7 +108,8 @@ def processFontFile(fontFilePath, svgFilePathsList):
# https://github.com/behdad/fonttools/issues/302
folderPath, fontFileName = os.path.split(fontFilePath)
fileNameNoExtension, fileExtension = os.path.splitext(fontFileName)
newFontFilePath = os.path.join(folderPath, "%s%s%s" % ('.', fileNameNoExtension, fileExtension))
newFontFilePath = os.path.join(folderPath, "{}{}{}".format(
'.', fileNameNoExtension, fileExtension))
font.save(newFontFilePath)
font.close()
@ -106,7 +117,10 @@ def processFontFile(fontFilePath, svgFilePathsList):
os.remove(fontFilePath)
os.rename(newFontFilePath, fontFilePath)
print >> sys.stdout, "SVG table successfully added to %s" % fontFilePath
print(
"SVG table successfully added to {}".format(fontFilePath),
file=sys.stderr
)
def validateSVGfiles(svgFilePathsList):
@ -128,7 +142,10 @@ def validateSVGfiles(svgFilePathsList):
# find <svg> blob
svg = reSVGelement.search(data)
if not svg:
print "WARNING: Could not find <svg> element in the file. Skiping %s" % (filePath)
print(
"WARNING: Could not find <svg> element in the file. "
"Skiping {}".format(filePath)
)
continue
validatedPaths.append(filePath)
@ -156,27 +173,35 @@ def run():
# Font file path
if os.path.isfile(fontFilePath):
if getFontFormat(fontFilePath) not in ["OTF", "TTF"]:
print >> sys.stderr, "ERROR: The path is not a valid OTF or TTF font."
print("ERROR: The path is not a valid OTF or TTF font.",
file=sys.stderr)
return
else:
print >> sys.stderr, "ERROR: The path to the font is invalid."
print("ERROR: The path to the font is invalid.",
file=sys.stderr)
return
# SVG folder path
if os.path.isdir(svgFolderPath):
svgFilePathsList = []
for dirName, subdirList, fileList in os.walk(svgFolderPath): # Support nested folders
for dirName, subdirList, fileList in os.walk(
svgFolderPath): # Support nested folders
for file in fileList:
svgFilePathsList.append(os.path.join(dirName, file)) # Assemble the full paths, not just file names
svgFilePathsList.append(os.path.join(dirName,
file)) # Assemble the full paths, not just file names
else:
print >> sys.stderr, "ERROR: The path to the folder containing the SVG files is invalid."
print(
"ERROR: The path to the folder "
"containing the SVG files is invalid.",
file=sys.stderr
)
return
# validate the SVGs
svgFilePathsList = validateSVGfiles(svgFilePathsList)
if not svgFilePathsList:
print >> sys.stderr, "WARNING: No SVG files were found."
print("WARNING: No SVG files were found.", file=sys.stderr)
return
processFontFile(fontFilePath, svgFilePathsList)
@ -184,6 +209,8 @@ def run():
if __name__ == "__main__":
if len(sys.argv) != 3:
print "To run this script type:\n python %s <path to input OTF/TTF file> <path to folder tree containing SVG files>" % sys.argv[0]
print("To run this script type:\n "
"python {} <path to input OTF/TTF file> "
"<path to folder tree containing SVG files>".format(sys.argv[0]))
else:
run()