Updates based upon review feedback for glyf to individual files; include table name in contentOnly file; Remove unnecessary parameters from toXML - leaving only the splitGlyphs optional additional parameter

This commit is contained in:
Bill Amidei 2017-11-23 07:17:35 -08:00
parent 11bfd82854
commit de94613e08
5 changed files with 47 additions and 30 deletions

View File

@ -76,10 +76,12 @@ class XMLReader(object):
def _startElementHandler(self, name, attrs):
if self.stackSize == 1 and self.contentOnly:
# We already know the table we're parsing, move to
# We already know the table we're parsing, skip
# parsing the table tag and continue to
# stack '2' which begins parsing content
self.contentStack.append([])
self.stackSize = 2
return
stackSize = self.stackSize
self.stackSize = stackSize + 1
subFile = attrs.get("src")

View File

@ -300,7 +300,7 @@ class TTFont(object):
if progress:
progress.set(i)
tag = tables[i]
if splitTables or splitGlyphs:
if splitTables or (splitGlyphs and (tag == 'glyf')):
tablePath = fileNameTemplate % tagToIdentifier(tag)
else:
tablePath = None
@ -314,8 +314,7 @@ class TTFont(object):
writer.newline()
else:
tableWriter = writer
self._tableToXML(tableWriter, tag, progress, tablePath, ttLibVersion=version,
idlefunc=idlefunc, newlinestr=newlinestr, splitGlyphs=splitGlyphs)
self._tableToXML(tableWriter, tag, progress, splitGlyphs=splitGlyphs)
if splitTables:
tableWriter.endtag("ttFont")
tableWriter.newline()
@ -329,8 +328,7 @@ class TTFont(object):
if not hasattr(fileOrPath, "write") and fileOrPath != "-":
writer.close()
def _tableToXML(self, writer, tag, progress, tablePath, ttLibVersion=None,
idlefunc=None, newlinestr=None, splitGlyphs=None, quiet=None):
def _tableToXML(self, writer, tag, progress, splitGlyphs=False, quiet=None):
if quiet is not None:
deprecateArgument("quiet", "configure logging instead")
if tag in self:
@ -352,9 +350,9 @@ class TTFont(object):
attrs['raw'] = True
writer.begintag(xmlTag, **attrs)
writer.newline()
if tag in ("glyf"):
table.toXML(writer, self, progress, tablePath, ttLibVersion, idlefunc, newlinestr, splitGlyphs)
elif tag in ("CFF "):
if tag == "glyf":
table.toXML(writer, self, progress, splitGlyphs)
elif tag == "CFF ":
table.toXML(writer, self, progress)
else:
table.toXML(writer, self)
@ -894,17 +892,9 @@ def nameToIdentifier(name):
return ident
def tagToIdentifier(tag):
"""Convert a table tag to a valid (but UGLY) python identifier,
as well as a filename that's guaranteed to be unique even on a
caseless file system. Each character is mapped to two characters.
Lowercase letters get an underscore before the letter, uppercase
letters get an underscore after the letter. Trailing spaces are
trimmed. Illegal characters are escaped as two hex bytes. If the
result starts with a number (as the result of a hex escape), an
extra underscore is prepended. Examples:
'glyf' -> '_g_l_y_f'
'cvt ' -> '_c_v_t'
'OS/2' -> 'O_S_2f_2'
"""This performs the same conversion which nameToIdentifiier does
with the additional assertion that the source tag is 4 characters
long which is criteria for a valid tag name.
"""
if tag == "GlyphOrder":
return tag

View File

@ -5,6 +5,7 @@ from collections import namedtuple
from fontTools.misc.py23 import *
from fontTools.misc import sstruct
from fontTools import ttLib
from fontTools import version
from fontTools.misc.textTools import safeEval, pad
from fontTools.misc.arrayTools import calcBounds, calcIntBounds, pointInRect
from fontTools.misc.bezierTools import calcQuadraticBounds
@ -22,6 +23,11 @@ from fontTools.ttLib import nameToIdentifier
log = logging.getLogger(__name__)
# We compute the version the same as is computed in ttlib/__init__
# so that we can write 'ttLibVersion' attribute of the glyf TTX files
# when glyf is written to separate files.
version = ".".join(version.split('.')[:2])
#
# The Apple and MS rasterizers behave differently for
# scaled composite components: one does scale first and then translate
@ -112,8 +118,7 @@ class table__g_l_y_f(DefaultTable.DefaultTable):
ttFont['maxp'].numGlyphs = len(self.glyphs)
return data
def toXML(self, writer, ttFont, progress=None, tablePath=None,
ttLibVersion=None, idlefunc=None, newlinestr=None, splitGlyphs=None):
def toXML(self, writer, ttFont, progress=None, splitGlyphs=False):
writer.newline()
glyphNames = ttFont.getGlyphNames()
@ -131,12 +136,15 @@ class table__g_l_y_f(DefaultTable.DefaultTable):
glyph = self[glyphName]
if glyph.numberOfContours:
if splitGlyphs:
path, ext = os.path.splitext(tablePath)
path, ext = os.path.splitext(writer.file.name)
fileNameTemplate = path + ".%s" + ext
glyphPath = fileNameTemplate % nameToIdentifier(glyphName)
glyphWriter = xmlWriter.XMLWriter(glyphPath, idlefunc=idlefunc,
newlinestr=newlinestr)
glyphWriter.begintag("ttFont", ttLibVersion=ttLibVersion)
glyphWriter = xmlWriter.XMLWriter(glyphPath, idlefunc=writer.idlefunc,
newlinestr=writer.newlinestr)
glyphWriter.begintag("ttFont", ttLibVersion=version)
glyphWriter.newline()
glyphWriter.newline()
glyphWriter.begintag("glyf")
glyphWriter.newline()
glyphWriter.newline()
writer.simpletag("TTGlyph", src=os.path.basename(glyphPath))
@ -155,6 +163,8 @@ class table__g_l_y_f(DefaultTable.DefaultTable):
glyphWriter.endtag('TTGlyph')
glyphWriter.newline()
if splitGlyphs:
glyphWriter.endtag("glyf")
glyphWriter.newline()
glyphWriter.endtag("ttFont")
glyphWriter.newline()
glyphWriter.close()

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<ttFont ttLibVersion="3.15">
<TTGlyph name=".notdef" xMin="65" yMin="-60" xMax="495" yMax="770">
<contour>
<pt x="65" y="770" on="1"/>
<pt x="95" y="770" on="1"/>
<pt x="95" y="-60" on="1"/>
<pt x="65" y="-60" on="1"/>
</contour>
<contour.../>
</TTGlyph>
</ttFont>

View File

@ -153,9 +153,11 @@ class TestXMLReader(unittest.TestCase):
subFileData = (
'<ttFont ttLibVersion="3.15">'
'<namerecord nameID="%s" platformID="%s" platEncID="1" langID="%s">'
'%s'
'</namerecord>'
'<name>'
'<namerecord nameID="%s" platformID="%s" platEncID="1" langID="%s">'
'%s'
'</namerecord>'
'</name>'
'</ttFont>'
)%(expectedNameID, expectedPlatform, expectedLangId, expectedContent)
tmp.write(subFileData.encode("utf-8"))