Don't assume anything about the structure of GLIF that is not part of the spec.
git-svn-id: http://svn.robofab.com/trunk@234 b5fa9d6c-a76f-4ffd-b3cb-f825fc41095c
This commit is contained in:
parent
9ae34cc649
commit
62d81b2b9e
@ -536,28 +536,36 @@ def _fetchGlyphName(glyphPath):
|
|||||||
|
|
||||||
def _fetchUnicodes(text):
|
def _fetchUnicodes(text):
|
||||||
# Given GLIF text, get a list of all unicode values from the XML data.
|
# Given GLIF text, get a list of all unicode values from the XML data.
|
||||||
# NOTE: this assumes .glif files written by glifLib, since
|
parser = _FetchUnicodesParser(text)
|
||||||
# we simply stop parsing as soon as we see anything else than
|
return parser.unicodes
|
||||||
# <glyph>, <advance> or <unicode>. glifLib always writes those
|
|
||||||
# elements in that order, before anything else.
|
class _FetchUnicodesParser(object):
|
||||||
|
|
||||||
|
def __init__(self, text):
|
||||||
from xml.parsers.expat import ParserCreate
|
from xml.parsers.expat import ParserCreate
|
||||||
|
self.unicodes = []
|
||||||
|
self._elementStack = []
|
||||||
|
parser = ParserCreate()
|
||||||
|
parser.returns_unicode = 0 # XXX, Don't remember why. It sucks, though.
|
||||||
|
parser.StartElementHandler = self.startElementHandler
|
||||||
|
parser.EndElementHandler = self.endElementHandler
|
||||||
|
parser.Parse(text)
|
||||||
|
|
||||||
unicodes = []
|
def startElementHandler(self, name, attrs):
|
||||||
def _startElementHandler(tagName, attrs, _unicodes=unicodes):
|
if name == "unicode" and len(self._elementStack) == 1 and self._elementStack[0] == "glyph":
|
||||||
if tagName == "unicode":
|
value = attrs.get("hex")
|
||||||
_unicodes.append(int(attrs["hex"], 16))
|
if value is None:
|
||||||
elif tagName not in ("glyph", "advance"):
|
raise GlifLibError("The required hex attribute not defined in a unicode element.")
|
||||||
raise _DoneParsing()
|
|
||||||
|
|
||||||
p = ParserCreate()
|
|
||||||
p.StartElementHandler = _startElementHandler
|
|
||||||
p.returns_unicode = True
|
|
||||||
f = StringIO(text)
|
|
||||||
try:
|
try:
|
||||||
p.ParseFile(f)
|
value = int(value, 16)
|
||||||
except _DoneParsing:
|
except ValueError:
|
||||||
pass
|
raise GlifLibError("The value for the hex attribute defined in a unicode element is not a valid value.")
|
||||||
return unicodes
|
self.unicodes.append(value)
|
||||||
|
self._elementStack.append(name)
|
||||||
|
|
||||||
|
def endElementHandler(self, name):
|
||||||
|
other = self._elementStack.pop(-1)
|
||||||
|
assert other == name
|
||||||
|
|
||||||
|
|
||||||
def buildOutline_Format0(pen, xmlNodes):
|
def buildOutline_Format0(pen, xmlNodes):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user