hershey.py revision 7f0dcdb9b962d8f8023044f93db690c20582683b
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico# Copyright 2011, Windell H. Oskay, www.evilmadscientist.com
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico#
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico# This program is free software; you can redistribute it and/or modify
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler# it under the terms of the GNU General Public License as published by
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler# the Free Software Foundation; either version 2 of the License, or
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler# (at your option) any later version.
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler#
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler# This program is distributed in the hope that it will be useful,
4d761440b84b78f6af14bb66f2fd52b69641bfd2Hannes Hochreiner# but WITHOUT ANY WARRANTY; without even the implied warranty of
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico# GNU General Public License for more details.
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico#
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico# You should have received a copy of the GNU General Public License
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico# along with this program; if not, write to the Free Software
24a9a5b95f13f29873daec7e0be9ab1f74b90916Hannes Hochreiner# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andlerimport hersheydata #data file w/ Hershey font data
4d761440b84b78f6af14bb66f2fd52b69641bfd2Hannes Hochreinerimport inkex
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNicoimport simplestyle
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNicoDebug = False
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNicodef draw_svg_text(char, face, offset, vertoffset, parent):
24a9a5b95f13f29873daec7e0be9ab1f74b90916Hannes Hochreiner style = { 'stroke': '#000000', 'fill': 'none' }
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico pathString = face[char]
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler splitString = pathString.split()
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler midpoint = offset - int(splitString[0])
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler pathString = pathString[pathString.find("M"):] #portion after first move
6be2794aa8debd2d12c023ec766fa5473e81dfd4JazzyNico trans = 'translate(' + str(midpoint) + ',' + str(vertoffset) + ')'
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler text_attribs = {'style':simplestyle.formatStyle(style), 'd':pathString, 'transform':trans}
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler inkex.etree.SubElement(parent, inkex.addNS('path','svg'), text_attribs)
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler return midpoint + int(splitString[1]) #new offset value
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andlerclass Hershey( inkex.Effect ):
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler def __init__( self ):
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler inkex.Effect.__init__( self )
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler self.OptionParser.add_option( "--tab", #NOTE: value is not used.
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler action="store", type="string",
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler dest="tab", default="splash",
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler help="The active tab when Apply was pressed" )
1b1107ddff011fb4b14c466805d0f3b75a1bae3cJosh Andler self.OptionParser.add_option( "--text",
action="store", type="string",
dest="text", default="Hershey Text for Inkscape",
help="The input text to render")
self.OptionParser.add_option( "--action",
action="store", type="string",
dest="action", default="render",
help="The active option when Apply was pressed" )
self.OptionParser.add_option( "--fontface",
action="store", type="string",
dest="fontface", default="rowmans",
help="The selected font face when Apply was pressed" )
def effect( self ):
# Embed text in group to make manipulation easier:
g_attribs = {inkex.addNS('label','inkscape'):'Hershey Text' }
g = inkex.etree.SubElement(self.current_layer, 'g', g_attribs)
font = eval('hersheydata.' + str(self.options.fontface))
clearfont = hersheydata.futural
#Baseline: modernized roman simplex from JHF distribution.
w = 0 #Initial spacing offset
spacing = 3 # spacing between letters
if self.options.action == "render":
#evaluate text string
letterVals = [ord(q) - 32 for q in self.options.text]
for q in letterVals:
if (q < 0) or (q > 95):
w += 2*spacing
else:
w = draw_svg_text(q, font, w, 0, g)
else:
#Generate glyph table
wmax = 0;
for p in range(0,10):
w = 0
v = spacing * (15*p - 67 )
for q in range(0,10):
r = p*10 + q
if (r < 0) or (r > 95):
w += 5*spacing
else:
w = draw_svg_text(r, clearfont, w, v, g)
w = draw_svg_text(r, font, w, v, g)
w += 5*spacing
if w > wmax:
wmax = w
w = wmax
# Translate group to center of view, approximately
t = 'translate(' + str( self.view_center[0] - w/2) + ',' + str( self.view_center[1] ) + ')'
g.set( 'transform',t)
if __name__ == '__main__':
e = Hershey()
e.affect()