-
Notifications
You must be signed in to change notification settings - Fork 19
add the default xml declaration in writeGlyphToString #157
add the default xml declaration in writeGlyphToString #157
Conversation
Lib/ufoLib/glifLib.py
Outdated
@@ -628,7 +628,10 @@ def writeGlyphToString(glyphName, glyphObject=None, drawPointsFunc=None, formatV | |||
_writeLib(glyphObject, root, validate) | |||
# return the text | |||
tree = etree.ElementTree(root) | |||
text = etree.tostring(root, encoding=unicode, pretty_print=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by the way, this was also doubly wrong, because if anything it should have been encoding="unicode"
(as a string), not the built-in unicode
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not according to the lxml doc I read, but on vacation right now so can’t dig the reference up, if it works the other way, great
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benkiel you’re right, you can also pass the unicode function to the encoding argument:
I was misled by the fact that in the official docs I linked earlier there is no mention of that, only of “unicode” as a string.
Nevertheless, we need to restore the xml declaration, otherwise attempting to parse the xml with another parser different from xml might not work properly.
/cc @miguelsousa |
the writeGlyphToString will still return a unicode string, like it's always done. However, in the writeGlyph method we can call a private _writeGlyphToBytes and write UTF-8 bytes directly instead of decoding them, then re-encoding them as we write to the file.
@anthrotype this is breaking fontParts so something is amiss withe the xml declaration |
i'll fix that |
fixed with 4cb26a3 i'll cut a bugfix release once the bots are green. |
oops wrong link, 4cb26a3 |
This is a regression from ufoLib v2.1.1, see
adobe-type-tools/afdko#462 (comment)
https://travis-ci.org/adobe-type-tools/afdko/jobs/401751859#L8427
when we were using the fonttools XMLWriter the xml declaration was always included. When using lxml with encoding="unicode" there is no default xml declaration.
https://lxml.de/parsing.html#python-unicode-strings