balsamine.2014-2015
clone your own copy | download snapshot

Snapshots | iceberg

Inside this repository

ufo-clean.py
text/x-python

Download raw (5.1 KB)

#!/usr/bin/env python
#-*- coding: utf-8 -*-

from xml.dom.minidom import parse as parseXml
import sys
import glob
import fontforge


letters = ["!", """, "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", "&#59;", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "", "€", "", "‚", "ƒ", "„", "…", "†", "‡", "ˆ", "‰", "Š", "‹", "Œ", "", "Ž", "", "", "‘", "’", "“", "”", "•", "–", "—", "˜", "™", "š", "›", "œ", "", "ž", "Ÿ", " ", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "­", "®", "¯", "°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "€", "Œ", "œ", "…", "–", "—", "‘", "’", "“", "”", "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ", "Ⅼ", "Ⅽ", "Ⅾ", "Ⅿ", "←", "↑", "→", "↓", "⓪", "①", "②", "③", "④", "⑤", "⑥", "⑦", "⑧", "⑨", "♩", "♪", "♫", "♬", "♭", "♮", "♯", "′", "″", "∢"]

STROKE_FONT = "%s-stroke.ufo" % sys.argv[1].replace(" ", "-")
GLYPH_DIR = "%s/glyphs/" % STROKE_FONT

for letter in letters:
    letter = letter.split("/")[-1].replace(".svg", "")
    char = fontforge.unicodeFromName(letter)

    if char == -1:
        char = letter.replace("&#", "").replace(";", "")
        letter = fontforge.nameFromUnicode(int(char))
    print "letter: %s" % letter
    print "char: %s" % char


    # Gets the XML of the glyph
    try: 
        letter = letter.replace("&#", "").replace(";", "")
        letter = fontforge.nameFromUnicode(int(char))
        # In UFO, capital characters have an underscore in their name: "A" -> "A_.glif"
        if letter[0].isupper(): 
            if len(letter) == 1:
                letter = letter + "_"
            elif len(letter) == 2:
                letter = letter[0] + "_" + letter[1] + "_"
            else:
                letter = letter[0] + "_" + letter[1:]
            #letter = letter.lower()
        if letter[0:3] == "uni": 
            continue
            #letter = "uni" + letter[3:].upper() + "_"



        glyph = parseXml("%s%s.glif" % (GLYPH_DIR, letter))
        # Gets contours descriptions of the glyph
        contours = glyph.getElementsByTagName("contour")

        for contour in contours:
            try:
                # Checks type of first node of the countour
                type= contour.childNodes[1].attributes["type"].value
                if type == "move":
                    # Should be ok already
                    pass
                elif type == "line":
                    # Changing type "line" to "move"
                    contour.childNodes[1].attributes["type"].value = "move"
                elif type == "curve":
                    # Putting curve point at the end of the contour
                    contour.appendChild(contour.childNodes[1])
                    # Changing first "line" to "move"
                    contour.childNodes[2].attributes["type"].value = "move"
            except KeyError:
                print "Did not work."
                pass


        # Saves XML
        f = open("%s%s.glif" % (GLYPH_DIR, letter), "w")
        f.write(glyph.toxml())
        f.close()
    except IOError:
        # In case it still doesn't work, it passes. i.e: €
        print "%s did not work." % char
        pass