Skip to content

Instantly share code, notes, and snippets.

@sovichet
Last active January 7, 2024 06:58
Show Gist options
  • Select an option

  • Save sovichet/607ee6a4c4085882e762884eeccf01ae to your computer and use it in GitHub Desktop.

Select an option

Save sovichet/607ee6a4c4085882e762884eeccf01ae to your computer and use it in GitHub Desktop.
Extract custom glyphdata from selected glyphs
import os.path
import xml.etree.ElementTree as ET
'''
Use this script in Glyphs Macro window to extract the glyph info from the
selected glyphs, and saved as a custom XML file.
'''
root = ET.Element("glyphData")
root.set("format", "2")
sortInt = 0
sortPrefix = "my"
for layer in Font.selectedLayers:
glyph = layer.parent
if glyph.export:
ge = ET.SubElement(root, "glyph")
ge.set("name", glyph.name)
ge.set("script", glyph.script)
ge.set("sortName", "{0}{1:04d}".format(sortPrefix, sortInt))
if glyph.unicode:
ge.set("unicode", glyph.unicode)
if glyph.glyphInfo and glyph.glyphInfo.desc:
ge.set("description", glyph.glyphInfo.desc)
if glyph.glyphInfo and glyph.glyphInfo.anchors:
ge.set("anchors", ', '.join(glyph.glyphInfo.anchors))
if glyph.glyphInfo and glyph.glyphInfo.accents:
ge.set("marks", ', '.join(glyph.glyphInfo.accents))
if glyph.glyphInfo and glyph.glyphInfo.components:
l = []
for c in glyph.glyphInfo.components:
l.append(c.name)
ge.set("decompose", ', '.join(l))
if glyph.category:
ge.set("category", glyph.category)
if glyph.subCategory:
ge.set("subCategory", glyph.subCategory)
if glyph.productionName:
ge.set("production", glyph.productionName)
sortInt = sortInt + 2
tree = ET.ElementTree(root)
ET.indent(tree, space="\t", level=0)
with open(os.path.expanduser("~/Desktop/GlyphData_extract.xml"), "wb") as f:
header = '''<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE glyphData [
<!ELEMENT glyphData (glyph)+>
<!ATTLIST glyphData
format CDATA #IMPLIED>
<!ELEMENT glyph EMPTY>
<!ATTLIST glyph
unicode CDATA #IMPLIED
unicodeLegacy CDATA #IMPLIED
name CDATA #REQUIRED
sortName CDATA #IMPLIED
sortNameKeep CDATA #IMPLIED
category CDATA #REQUIRED
subCategory CDATA #IMPLIED
case CDATA #IMPLIED
direction CDATA #IMPLIED
script CDATA #IMPLIED
description CDATA #IMPLIED
production CDATA #IMPLIED
altNames CDATA #IMPLIED
decompose CDATA #IMPLIED
anchors CDATA #IMPLIED
marks CDATA #IMPLIED>
]>
'''
f.write(bytes(header, "utf-8"))
tree.write(f)
print("XML file is exported.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment