import fontforge
import psMat
import sys

if len(sys.argv) > 2 and sys.argv[1] == "-u":
    svgs = sys.argv[2:]
    uni = True
else:
    svgs = sys.argv[1:]
    uni = False

# create empty font
font = fontforge.font()

# set font names
font.fontname = "RichStyle"
font.fullname = "RichStyle"
font.familyname = "RichStyle"
font.version = "0.5"
font.copyright = "RichStyle Font\n(c) 2012 - 2014 Mohammad Anas Ramadan\nhttp://richstyle.org\nLicensed under Ubuntu Font License 1.0\n\nHTML5 Technology Icons\n(c) The World Wide Web Consortium (W3C)\nhttp://www.w3.org/html/logo/\nLicensed under Creative Commons Attribution (CC BY) 3.0.\n\nThe Noun Project and Contributers\n(c) 2010 -2012 The Noun Project\nhttp://thenounproject.com\nLicensed under Creative Commons Attribution (CC BY) 3.0.\n\nThe company logos in this font are copyright of their respective owners.\n\nMany thanks also go to Offset Media for its icons.\nhttp://offsetmedia.co.uk\n\nLast but not least, many thanks go to Khaled Hosny for his great support.\nhttp://khaledhosny.org"

def centerGlyph(glyph):
    # center horizontally
    sb = (glyph.right_side_bearing + glyph.left_side_bearing)/2
    if sb < 0:
        sb = 0
    glyph.right_side_bearing = glyph.left_side_bearing = sb

    # center vertically
    xmin = glyph.boundingBox()[1]
    xmax = glyph.boundingBox()[-1]
    transform = xmax - ((xmax - xmin)/2) - 300
    glyph.transform(psMat.translate(0, -transform))

# import svgs
for svg in svgs:
    if "." in svg:
        if uni:
            svg = svg.decode("utf8")
            code = ord(u"%s" %svg.split(".")[0])
            name = fontforge.nameFromUnicode(code)
        else:
            name = svg.split(".")[0]
            code = fontforge.unicodeFromName(name)
    else:
        print >> sys.stderr, "files must have an extension of their type"
        sys.exit(1)

    # create a new glyph with the code point i
    glyph = font.createChar(code, name)

    # import svg file into it
    glyph.importOutlines(svg)

    centerGlyph(glyph)

    glyph.simplify()

font.generate("RichStyle.otf") # OpenType
font.generate("RichStyle.ttf") # TrueType
font.generate("RichStyle.woff") # WOFF
font.generate("RichStyle.svg") # SVG
font.save("RichStyle.sfd") # SFD
