Move fontTools.ttLib.importXML to fontTools.misc.xmlReader
Better matches xmlWriter.
This commit is contained in:
parent
38fdae6342
commit
3ebfea491e
@ -9,9 +9,9 @@ class TTXParseError(Exception): pass
|
|||||||
BUFSIZE = 0x4000
|
BUFSIZE = 0x4000
|
||||||
|
|
||||||
|
|
||||||
class ExpatParser:
|
class XMLReader:
|
||||||
|
|
||||||
def __init__(self, ttFont, fileName, progress=None, quiet=False):
|
def __init__(self, fileName, ttFont, progress=None, quiet=False):
|
||||||
self.ttFont = ttFont
|
self.ttFont = ttFont
|
||||||
self.fileName = fileName
|
self.fileName = fileName
|
||||||
self.progress = progress
|
self.progress = progress
|
||||||
@ -20,18 +20,21 @@ class ExpatParser:
|
|||||||
self.contentStack = []
|
self.contentStack = []
|
||||||
self.stackSize = 0
|
self.stackSize = 0
|
||||||
|
|
||||||
def parse(self):
|
def read(self):
|
||||||
|
if self.progress:
|
||||||
|
import stat
|
||||||
|
self.progress.set(0, os.stat(fileName)[stat.ST_SIZE] / 100 or 1)
|
||||||
file = open(self.fileName)
|
file = open(self.fileName)
|
||||||
self.parseFile(file)
|
self._parseFile(file)
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
def parseFile(self, file):
|
def _parseFile(self, file):
|
||||||
from xml.parsers.expat import ParserCreate
|
from xml.parsers.expat import ParserCreate
|
||||||
parser = ParserCreate()
|
parser = ParserCreate()
|
||||||
parser.returns_unicode = 0
|
parser.returns_unicode = 0
|
||||||
parser.StartElementHandler = self.startElementHandler
|
parser.StartElementHandler = self._startElementHandler
|
||||||
parser.EndElementHandler = self.endElementHandler
|
parser.EndElementHandler = self._endElementHandler
|
||||||
parser.CharacterDataHandler = self.characterDataHandler
|
parser.CharacterDataHandler = self._characterDataHandler
|
||||||
|
|
||||||
pos = 0
|
pos = 0
|
||||||
while 1:
|
while 1:
|
||||||
@ -44,7 +47,7 @@ class ExpatParser:
|
|||||||
self.progress.set(pos / 100)
|
self.progress.set(pos / 100)
|
||||||
parser.Parse(chunk, 0)
|
parser.Parse(chunk, 0)
|
||||||
|
|
||||||
def startElementHandler(self, name, attrs):
|
def _startElementHandler(self, name, attrs):
|
||||||
stackSize = self.stackSize
|
stackSize = self.stackSize
|
||||||
self.stackSize = stackSize + 1
|
self.stackSize = stackSize + 1
|
||||||
if not stackSize:
|
if not stackSize:
|
||||||
@ -60,7 +63,8 @@ class ExpatParser:
|
|||||||
subFile = attrs.get("src")
|
subFile = attrs.get("src")
|
||||||
if subFile is not None:
|
if subFile is not None:
|
||||||
subFile = os.path.join(os.path.dirname(self.fileName), subFile)
|
subFile = os.path.join(os.path.dirname(self.fileName), subFile)
|
||||||
importXML(self.ttFont, subFile, self.progress)
|
subReader = XMLReader(subFile, self.ttFont, self.progress, self.quiet)
|
||||||
|
subReader.read()
|
||||||
self.contentStack.append([])
|
self.contentStack.append([])
|
||||||
return
|
return
|
||||||
tag = ttLib.xmlToTag(name)
|
tag = ttLib.xmlToTag(name)
|
||||||
@ -96,11 +100,11 @@ class ExpatParser:
|
|||||||
self.contentStack[-1].append((name, attrs, list))
|
self.contentStack[-1].append((name, attrs, list))
|
||||||
self.contentStack.append(list)
|
self.contentStack.append(list)
|
||||||
|
|
||||||
def characterDataHandler(self, data):
|
def _characterDataHandler(self, data):
|
||||||
if self.stackSize > 1:
|
if self.stackSize > 1:
|
||||||
self.contentStack[-1].append(data)
|
self.contentStack[-1].append(data)
|
||||||
|
|
||||||
def endElementHandler(self, name):
|
def _endElementHandler(self, name):
|
||||||
self.stackSize = self.stackSize - 1
|
self.stackSize = self.stackSize - 1
|
||||||
del self.contentStack[-1]
|
del self.contentStack[-1]
|
||||||
if self.stackSize == 1:
|
if self.stackSize == 1:
|
||||||
@ -124,14 +128,3 @@ class ProgressPrinter:
|
|||||||
def setLabel(self, text):
|
def setLabel(self, text):
|
||||||
print text
|
print text
|
||||||
|
|
||||||
|
|
||||||
def importXML(ttFont, fileName, progress=None, quiet=True):
|
|
||||||
"""Import a TTX file (an XML-based text format), so as to recreate
|
|
||||||
a font object.
|
|
||||||
"""
|
|
||||||
if progress:
|
|
||||||
import stat
|
|
||||||
progress.set(0, os.stat(fileName)[stat.ST_SIZE] / 100 or 1)
|
|
||||||
p = ExpatParser(ttFont, fileName, progress, quiet)
|
|
||||||
p.parse()
|
|
||||||
|
|
@ -324,8 +324,11 @@ class TTFont:
|
|||||||
# contain the table which was originally used to extract the
|
# contain the table which was originally used to extract the
|
||||||
# glyph names from (ie. 'post', 'cmap' or 'CFF ').
|
# glyph names from (ie. 'post', 'cmap' or 'CFF ').
|
||||||
self.getGlyphOrder()
|
self.getGlyphOrder()
|
||||||
import xmlImport
|
|
||||||
xmlImport.importXML(self, file, progress, quiet)
|
from fontTools.misc import xmlReader
|
||||||
|
|
||||||
|
reader = xmlReader.XMLReader(file, self, progress, quiet)
|
||||||
|
reader.read()
|
||||||
|
|
||||||
def isLoaded(self, tag):
|
def isLoaded(self, tag):
|
||||||
"""Return true if the table identified by 'tag' has been
|
"""Return true if the table identified by 'tag' has been
|
||||||
|
Loading…
x
Reference in New Issue
Block a user