Strip name table records before writing to XML

They will be stripped reading back anyone, so any surrounding
whitespace will be lost.  At least this way diffs of ttx files
are cleaner.
This commit is contained in:
Behdad Esfahbod 2013-11-28 18:48:15 -05:00
parent ca80208a15
commit 1edfe57656
2 changed files with 12 additions and 16 deletions

View File

@ -41,33 +41,29 @@ class XMLWriter:
"""Writes text in a CDATA section."""
self._writeraw("<![CDATA[" + string + "]]>")
def writeutf16be(self, data):
"""Writes a UTF-16 bytes() sequence into the XML
as native Unicode. When this is read in xmlReader,
the original bytes can be recovered by encoding to
'utf-16-be'."""
self._writeraw(escape(data.decode('utf-16-be')))
def write8bit(self, data):
def write8bit(self, data, strip=False):
"""Writes a bytes() sequence into the XML, escaping
non-ASCII bytes. When this is read in xmlReader,
the original bytes can be recovered by encoding to
'latin-1'."""
self._writeraw(escape8bit(data))
self._writeraw(escape8bit(data), strip=strip)
def write16bit(self, data):
self._writeraw(escape16bit(data))
def write16bit(self, data, strip=False):
self._writeraw(escape16bit(data), strip=strip)
def write_noindent(self, string):
"""Writes text without indentation."""
self._writeraw(escape(string), indent=False)
def _writeraw(self, data, indent=True):
def _writeraw(self, data, indent=True, strip=False):
"""Writes bytes, possibly indented."""
if indent and self.needindent:
self.file.write(self.indentlevel * self.indentwhite)
self.needindent = 0
self.file.write(tostr(data, encoding="utf-8"))
s = tostr(data, encoding="utf-8")
if (strip):
s = s.strip()
self.file.write(s)
def newline(self):
self.file.write("\n")

View File

@ -102,11 +102,11 @@ class NameRecord:
if len(self.string) % 2:
# no, shouldn't happen, but some of the Apple
# tools cause this anyway :-(
writer.write16bit(self.string + b"\0")
writer.write16bit(self.string + b"\0", strip=True)
else:
writer.write16bit(self.string)
writer.write16bit(self.string, strip=True)
else:
writer.write8bit(self.string)
writer.write8bit(self.string, strip=True)
writer.newline()
writer.endtag("namerecord")
writer.newline()