#!/usr/bin/env python2 import parser import sys import os def hlineto(positions): assert(len(positions) > 0) while len(positions) > 0: for item in rlineto([positions[0], 0]): yield item if len(positions) >= 2: for item in rlineto([0, positions[1]]): yield item positions = positions[2:] def vlineto(positions): assert(len(positions) > 0) while len(positions) > 0: for item in rlineto([0, positions[0]]): yield item if len(positions) >= 2: for item in rlineto([positions[1], 0]): yield item positions = positions[2:] def rlineto(args): while len(args) > 0: yield "l %s %s " % (args[0], -args[1]) rel_update_position(args[0], -args[1]) args = args[2:] B_first = True position = [0, 0] def rel_update_position(x, y): global position position[0] += x position[1] += y return (x,y) def rmoveto(arguments): global B_first global position if B_first: B_first = False else: yield "z " # close path yield "M %s %s " % (position[0], position[1]) # go back to where you were before closing the path. yield "m %s %s " % tuple(rel_update_position(arguments[0], -arguments[1])) def vmoveto(arguments): assert(len(arguments) == 1) for item in rmoveto([0, arguments[0]]): yield item def hmoveto(arguments): assert(len(arguments) == 1) for item in rmoveto([arguments[0], 1]): yield item def hhcurveto(curves): # starts and ends horizontally if len(curves) % 2 != 0: dy1 = curves[0] # first curve curves = curves[1 : ] else: dy1 = 0 for i in range(len(curves) // 4): curve = curves[i * 4 : (i + 1) * 4] yield curveto(curve[0], dy1, curve[1], curve[2], curve[3], 0) def rrcurveto(args): while len(args) > 0: a = args[:6] yield curveto(a[0], a[1], a[2], a[3], a[4], a[5]) args = args[6:] def hvcurveto(curves): if (len(curves) % 2): dxl = curves[-1] curves = curves[:-1] else: dxl = 0 # first bezier tangent = horizontal. # ends vertical. # hvcurveto: dx1 dx2 dy2 dy3 yield curveto(curves[0], 0, curves[1], curves[2], dxl, curves[3]) curves = curves[4:] if len(curves) > 0: for item in vhcurveto(curves): yield item # helper: def curveto(dx1, dy1, dx2, dy2, dx3, dy3): # http://yytex.googlecode.com/svn/trunk/src/c32/dejis.c # rrcurveto: dx1 dy1 dx2 dy2 dx3 dy3 # hvcurveto: dx1 dx2 dy2 dy3 # vhcurveto: dy1 dx2 dy2 dx3 rel_update_position(dx3 + dx2 + dx1, -(dy3 + dy2 + dy1)) # FIXME return "c %s %s %s %s %s %s " % (dx1, -dy1, dx2 + dx1, -(dy2 + dy1), dx3 + dx2 + dx1, -(dy3 + dy2 + dy1)) #return "c %s %s %s %s %s %s " % (dx1, dy1, dx2, dy2, dx3, dy3) def vhcurveto(curves): if len(curves) % 2: dyl = curves[-1] curves = curves[:-1] else: dyl = 0 # first bezier tangent = horizontal. # ends vertical. curve = curves[: 4] yield curveto(0, curve[0], curve[1], curve[2], curve[3], dyl) curves = curves[4:] if len(curves) > 0: for item in hvcurveto(curves): yield item def vvcurveto(curves): assert(len(curves) >= 4) if len(curves) % 4: dx1 = curves[0] curves = curves[1:] else: dx1 = 0 for i in range(0, len(curves), 4): curve = curves[i : i + 4] yield curveto(dx1, curve[0], curve[1], curve[2], 0, curve[3]) dx1 = 0 def rlinecurve(curves): lines = curves[:-6] for item in rlineto(lines): yield item for item in rrcurveto(curves[-6:]): yield item def process(text): for n in text.split("/"): if n == "": continue glyph_index = Charset_decoder[n] #code = ord(c) #print >>sys.stderr, "process code", code, "font", font_name #glyph_index = code # encoding.get_glyph_for_code(code) operations = char_strings_index.entries[glyph_index].operations for operator, arguments in operations: #print operator if operator in ["hstem", "vstem", "hstemhm", "vstemhm", "hintmask", "cntrmask"]: # hinting continue if operator == "rmoveto": for item in rmoveto(arguments): yield item elif operator == "vmoveto": for item in vmoveto(arguments): yield item elif operator == "hmoveto": for item in hmoveto(arguments): yield item elif operator == "rlineto": for item in rlineto(arguments): yield item elif operator == "hhcurveto": for item in hhcurveto(arguments): yield item elif operator == "hvcurveto": for item in hvcurveto(arguments): yield item elif operator == "vhcurveto": for item in vhcurveto(arguments): yield item elif operator == "vvcurveto": for item in vvcurveto(arguments): yield item elif operator == "vlineto": for item in vlineto(arguments): yield item elif operator == "rrcurveto": for item in rrcurveto(arguments): yield item elif operator == "hlineto": for item in hlineto(arguments): yield item elif operator == "rcurveline": num_curves = len(arguments) // 6 for i in range(num_curves): for item in rrcurveto(arguments[i * 6 : (i + 1) * 6]): yield item for item in rlineto([arguments[-2], arguments[-1]]): yield item elif operator == "rlinecurve": for item in rlinecurve(arguments): yield item elif operator == "endchar": yield "z " # close path break else: pass # print operator assert(len(sys.argv) == 4) font_name = sys.argv[1] f = open(font_name, "rb") encoding, char_strings_index, Charset_decoder = parser.parse(f) text = sys.argv[-2] if not os.path.exists("rendered"): os.mkdir("rendered") if not os.path.exists("rendered/%s" % font_name): os.mkdir("rendered/%s" % font_name) if not os.path.exists("rendered/%s/%d" % (font_name, int(sys.argv[-1]))): os.mkdir("rendered/%s/%d" % (font_name, int(sys.argv[-1]))) output_name = "rendered/%s/%d/SVG" % (font_name, int(sys.argv[-1])) output_font_name = "rendered/%s/%d/font" % (font_name, int(sys.argv[-1])) os.spawnvp(os.P_WAIT, "cp", ["cp", "--", font_name, output_font_name]) output_input_name = "rendered/%s/%d/input" % (font_name, int(sys.argv[-1])) ff = open(output_input_name, "wb") ff.write(text) ff.close() output_file = open(output_name, "w") output_file.write(""" ") output_file.close()