How to read glyph from .xml file

62 views
Skip to first unread message

h.huse...@gmail.com

unread,
Mar 3, 2020, 2:31:29 AM3/3/20
to fonttools
Hi, how can i read glyph from file like:

<?xml version='1.0' encoding='UTF-8'?>
<glyph name="kaf" format="2">
<outline>
<contour>
<point x="0.0" y="1411.0" type="line"/>
<point x="0.0" y="1487.0" type="line"/>
<point x="1.0" y="1488.0"/>
<point x="2.0" y="1490.0"/>
<point x="2.0" y="1491.0" type="curve"/>
<point x="16.0" y="1526.0" type="line" smooth="yes"/>
<point x="101.0" y="1719.0"/>
<point x="516.0" y="1543.0"/>
<point x="523.0" y="1459.0" type="curve"/>
<point x="504.0" y="1450.0"/>
<point x="469.0" y="1444.0"/>
<point x="443.0" y="1436.0" type="curve" smooth="yes"/>
<point x="411.0" y="1426.0"/>
<point x="397.0" y="1420.0"/>
<point x="369.0" y="1406.0" type="curve" smooth="yes"/>
<point x="75.0" y="1259.0"/>
<point x="497.0" y="952.0"/>
<point x="627.0" y="865.0" type="curve" smooth="yes"/>
<point x="691.0" y="822.0"/>
<point x="828.0" y="719.0"/>
<point x="889.0" y="754.0" type="curve" smooth="yes"/>
<point x="937.0" y="780.0"/>
<point x="979.0" y="980.0"/>
<point x="997.0" y="1046.0" type="curve" smooth="yes"/>
<point x="1031.0" y="1168.0"/>
<point x="1126.0" y="1355.0"/>
<point x="1207.0" y="1437.0" type="curve" smooth="yes"/>
<point x="1361.0" y="1593.0"/>
<point x="1401.0" y="1599.0"/>
<point x="1582.0" y="1640.0" type="curve"/>
<point x="1598.0" y="1645.0" type="line"/>
<point x="1729.0" y="1645.0" type="line"/>
<point x="1730.0" y="1644.0"/>
<point x="1733.0" y="1642.0"/>
<point x="1734.0" y="1643.0" type="curve"/>
<point x="1781.0" y="1636.0" type="line" smooth="yes"/>
<point x="1803.0" y="1632.0"/>
<point x="1818.0" y="1628.0"/>
<point x="1838.0" y="1623.0" type="curve" smooth="yes"/>
<point x="1934.0" y="1600.0"/>
<point x="2042.0" y="1555.0"/>
<point x="2125.0" y="1505.0" type="curve" smooth="yes"/>
<point x="2259.0" y="1423.0"/>
<point x="2388.0" y="1315.0"/>
<point x="2482.0" y="1193.0" type="curve" smooth="yes"/>
<point x="2618.0" y="1019.0"/>
<point x="2647.0" y="970.0"/>
<point x="2718.0" y="752.0" type="curve" smooth="yes"/>
<point x="2729.0" y="717.0"/>
<point x="2738.0" y="683.0"/>
<point x="2747.0" y="651.0" type="curve" smooth="yes"/>
<point x="2752.0" y="634.0"/>
<point x="2754.0" y="621.0"/>
<point x="2755.0" y="609.0" type="curve"/>
<point x="2788.0" y="600.0"/>
<point x="2848.0" y="597.0"/>
<point x="2880.0" y="593.0" type="curve"/>
<point x="2880.0" y="262.0" type="line"/>
<point x="2849.0" y="264.0"/>
<point x="2813.0" y="268.0"/>
<point x="2783.0" y="262.0" type="curve"/>
<point x="2777.0" y="260.0"/>
<point x="2770.0" y="260.0"/>
<point x="2763.0" y="258.0" type="curve"/>
<point x="2763.0" y="256.0" type="line"/>
<point x="2734.0" y="195.0" type="line"/>
<point x="2693.0" y="131.0"/>
<point x="2629.0" y="66.0"/>
<point x="2557.0" y="34.0" type="curve" smooth="yes"/>
<point x="2444.0" y="-16.0"/>
<point x="2358.0" y="-10.0"/>
<point x="2247.0" y="46.0" type="curve" smooth="yes"/>
<point x="2099.0" y="120.0"/>
<point x="1987.0" y="255.0"/>
<point x="1968.0" y="423.0" type="curve" smooth="yes"/>
<point x="1937.0" y="706.0"/>
<point x="2304.0" y="638.0"/>
<point x="2555.0" y="638.0" type="curve"/>
<point x="2538.0" y="712.0"/>
<point x="2490.0" y="804.0"/>
<point x="2452.0" y="858.0" type="curve" smooth="yes"/>
<point x="2319.0" y="1047.0"/>
<point x="2107.0" y="1178.0"/>
<point x="1878.0" y="1235.0" type="curve" smooth="yes"/>
<point x="1411.0" y="1352.0"/>
<point x="1221.0" y="1135.0"/>
<point x="1135.0" y="770.0" type="curve" smooth="yes"/>
<point x="1100.0" y="622.0"/>
<point x="1109.0" y="425.0"/>
<point x="1029.0" y="400.0" type="curve" smooth="yes"/>
<point x="838.0" y="340.0"/>
<point x="559.0" y="599.0"/>
<point x="429.0" y="729.0" type="curve" smooth="yes"/>
<point x="402.0" y="756.0"/>
<point x="374.0" y="781.0"/>
<point x="350.0" y="808.0" type="curve" smooth="yes"/>
<point x="278.0" y="896.0" type="line" smooth="yes"/>
<point x="181.0" y="1020.0"/>
<point x="97.0" y="1142.0"/>
<point x="37.0" y="1289.0" type="curve"/>
</contour>
</outline>
</glyph>

Bahman Eslami

unread,
Mar 3, 2020, 5:28:54 AM3/3/20
to fonttools
Hey,

Depends on what is your environment. Your destination glyph object should have a point pen. Here is a RoboFont example:

```
from fontTools.ufoLib.glifLib import writeGlyphToString, readGlyphFromString, Glyph
dummyGlyph = Glyph() succes = False try: # try to read the xml readGlyphFromString(possibleGlifXML, dummyGlyph, dummyGlyph.getPointPen()) succes = True except Exception as e: # if not valid fail print(f"Not valid glif xml!\nerror: {e}\ndata: \"{possibleGlifXML}\"")
```

I took this from Frederik in this post in RoboFont forums:

Bahman

Max Ilinov

unread,
Mar 3, 2020, 5:37:30 AM3/3/20
to fonttools
Also in Robofont you can just copy this code select any glyph and paste it — it will be drawn.
If you paste it in Glyph View window, it will be drawn into current layer.

вторник, 3 марта 2020 г., 10:31:29 UTC+3 пользователь h.hus...@gmail.com написал:

h.huse...@gmail.com

unread,
Mar 3, 2020, 8:50:28 AM3/3/20
to fonttools
Thank you for your reply. Actually I want to build a font. I'm using example code from https://github.com/fonttools/fonttools/blob/51bff6cf6a08976c831aa5c9e8c901fac7e61dea/Lib/fontTools/fontBuilder.py#L26 . I have ~500 glif files like above. So I want to build the font using these glif files. I just delete line 64 from example code and add this code:
with open("kaf.glif", "r", encoding="utf-8") as f:
xmlData = f.read()
glyph = Glyph("kaf", "b")
try:
readGlyphFromString(xmlData, glyph, glyph.getPointPen())
except Exception as e:
print(f"Not valid glif xml!\nerror: {e}\ndata: \"{xmlData}\"")

but it did not work properly. AttributeError: 'Glyph' object has no attribute 'recalcBounds'

3 Mart 2020 Salı 13:28:54 UTC+3 tarihinde Bahman Eslami yazdı:

h.huse...@gmail.com

unread,
Mar 3, 2020, 8:59:30 AM3/3/20
to fonttools
Thank you for your reply. I know the Robofont but its quite expensive and also 30 day trail not enough for me 

3 Mart 2020 Salı 13:37:30 UTC+3 tarihinde Max Ilinov yazdı:
Reply all
Reply to author
Forward
0 new messages