Skip to content
This repository has been archived by the owner on Jan 28, 2019. It is now read-only.

add the default xml declaration in writeGlyphToString #157

Merged
merged 4 commits into from
Jul 10, 2018

Conversation

anthrotype
Copy link
Member

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

@@ -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)
Copy link
Member Author

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.

Copy link
Collaborator

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

Copy link
Member Author

@anthrotype anthrotype Jul 10, 2018

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:

/~https://github.com/lxml/lxml/blob/0ef938692ea222d1b15939e0356739b895e883dd/src/lxml/etree.pyx#L3318

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.

@anthrotype
Copy link
Member Author

/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 anthrotype merged commit 586b83c into unified-font-object:master Jul 10, 2018
@anthrotype anthrotype deleted the xml-declaration branch July 10, 2018 11:47
@benkiel
Copy link
Collaborator

benkiel commented Jul 10, 2018

@anthrotype this is breaking fontParts so something is amiss withe the xml declaration

@anthrotype
Copy link
Member Author

i'll fix that

@anthrotype
Copy link
Member Author

anthrotype commented Jul 10, 2018

fixed with 4cb26a3

i'll cut a bugfix release once the bots are green.

@anthrotype
Copy link
Member Author

oops wrong link, 4cb26a3

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants