16
16
from io import BytesIO , open
17
17
from warnings import warn
18
18
from collections import OrderedDict
19
- from fontTools .misc .py23 import tobytes , unicode
19
+ from fontTools .misc .py23 import basestring , unicode
20
20
from ufoLib .plistlib import PlistWriter , readPlist , writePlist
21
21
from ufoLib .plistFromETree import readPlistFromTree
22
22
from ufoLib .pointPen import AbstractPointPen , PointToSegmentPen
23
23
from ufoLib .filenames import userNameToFileName
24
24
from ufoLib .validators import isDictEnough , genericTypeValidator , colorValidator ,\
25
25
guidelinesValidator , anchorsValidator , identifierValidator , imageValidator , glyphLibValidator
26
26
27
- try :
28
- basestring
29
- except NameError :
30
- basestring = str
31
-
32
- try :
33
- unicode
34
- except NameError :
35
- unicode = str
36
-
37
27
from lxml import etree
38
28
39
29
@@ -388,7 +378,7 @@ def writeGlyph(self, glyphName, glyphObject=None, drawPointsFunc=None, formatVer
388
378
if validate is None :
389
379
validate = self ._validateWrite
390
380
self ._purgeCachedGLIF (glyphName )
391
- data = writeGlyphToString (glyphName , glyphObject , drawPointsFunc , formatVersion = formatVersion , validate = validate )
381
+ data = _writeGlyphToBytes (glyphName , glyphObject , drawPointsFunc , formatVersion = formatVersion , validate = validate )
392
382
fileName = self .contents .get (glyphName )
393
383
if fileName is None :
394
384
if self ._existingFileNames is None :
@@ -407,7 +397,7 @@ def writeGlyph(self, glyphName, glyphObject=None, drawPointsFunc=None, formatVer
407
397
if data == oldData :
408
398
return
409
399
with open (path , "wb" ) as f :
410
- f .write (tobytes ( data , encoding = "utf-8" ) )
400
+ f .write (data )
411
401
412
402
def deleteGlyph (self , glyphName ):
413
403
"""Permanently delete the glyph from the glyph set on disk. Will
@@ -560,34 +550,10 @@ def readGlyphFromString(aString, glyphObject=None, pointPen=None, formatVersions
560
550
_readGlyphFromTree (tree , glyphObject , pointPen , formatVersions = formatVersions , validate = validate )
561
551
562
552
563
- def writeGlyphToString (glyphName , glyphObject = None , drawPointsFunc = None , formatVersion = 2 , validate = True ):
564
- """
565
- Return .glif data for a glyph as a UTF-8 encoded string.
566
- The 'glyphObject' argument can be any kind of object (even None);
567
- the writeGlyphToString() method will attempt to get the following
568
- attributes from it:
569
- "width" the advance width of the glyph
570
- "height" the advance height of the glyph
571
- "unicodes" a list of unicode values for this glyph
572
- "note" a string
573
- "lib" a dictionary containing custom data
574
- "image" a dictionary containing image data
575
- "guidelines" a list of guideline data dictionaries
576
- "anchors" a list of anchor data dictionaries
577
-
578
- All attributes are optional: if 'glyphObject' doesn't
579
- have the attribute, it will simply be skipped.
580
-
581
- To write outline data to the .glif file, writeGlyphToString() needs
582
- a function (any callable object actually) that will take one
583
- argument: an object that conforms to the PointPen protocol.
584
- The function will be called by writeGlyphToString(); it has to call the
585
- proper PointPen methods to transfer the outline to the .glif file.
586
-
587
- The GLIF format version can be specified with the formatVersion argument.
588
-
589
- ``validate`` will validate the written data. It is set to ``True`` by default.
590
- """
553
+ def _writeGlyphToBytes (
554
+ glyphName , glyphObject = None , drawPointsFunc = None , writer = None ,
555
+ formatVersion = 2 , validate = True ):
556
+ """Return .glif data for a glyph as a UTF-8 encoded bytes string."""
591
557
# start
592
558
if validate and not isinstance (glyphName , basestring ):
593
559
raise GlifLibError ("The glyph name is not properly formatted." )
@@ -627,9 +593,49 @@ def writeGlyphToString(glyphName, glyphObject=None, drawPointsFunc=None, formatV
627
593
if getattr (glyphObject , "lib" , None ):
628
594
_writeLib (glyphObject , root , validate )
629
595
# return the text
630
- tree = etree .ElementTree (root )
631
- text = etree .tostring (root , encoding = unicode , pretty_print = True )
632
- return text
596
+ data = etree .tostring (
597
+ root , encoding = "utf-8" , xml_declaration = True , pretty_print = True
598
+ )
599
+ return data
600
+
601
+
602
+ def writeGlyphToString (glyphName , glyphObject = None , drawPointsFunc = None , formatVersion = 2 , validate = True ):
603
+ """
604
+ Return .glif data for a glyph as a Unicode string (`unicode` in py2, `str`
605
+ in py3). The XML declaration's encoding is always set to "UTF-8".
606
+ The 'glyphObject' argument can be any kind of object (even None);
607
+ the writeGlyphToString() method will attempt to get the following
608
+ attributes from it:
609
+ "width" the advance width of the glyph
610
+ "height" the advance height of the glyph
611
+ "unicodes" a list of unicode values for this glyph
612
+ "note" a string
613
+ "lib" a dictionary containing custom data
614
+ "image" a dictionary containing image data
615
+ "guidelines" a list of guideline data dictionaries
616
+ "anchors" a list of anchor data dictionaries
617
+
618
+ All attributes are optional: if 'glyphObject' doesn't
619
+ have the attribute, it will simply be skipped.
620
+
621
+ To write outline data to the .glif file, writeGlyphToString() needs
622
+ a function (any callable object actually) that will take one
623
+ argument: an object that conforms to the PointPen protocol.
624
+ The function will be called by writeGlyphToString(); it has to call the
625
+ proper PointPen methods to transfer the outline to the .glif file.
626
+
627
+ The GLIF format version can be specified with the formatVersion argument.
628
+
629
+ ``validate`` will validate the written data. It is set to ``True`` by default.
630
+ """
631
+ data = _writeGlyphToBytes (
632
+ glyphName ,
633
+ glyphObject = glyphObject ,
634
+ drawPointsFunc = drawPointsFunc ,
635
+ formatVersion = formatVersion ,
636
+ validate = validate ,
637
+ )
638
+ return data .decode ("utf-8" )
633
639
634
640
635
641
def _writeAdvance (glyphObject , element , validate ):
0 commit comments