hpgl_output.py revision e4c3132f5a1dedfe87b7fbfd3b7587a0172d9f92
5542N/A#!/usr/bin/env python
5542N/A'''
5542N/ACopyright (C) 2008 Aaron Spike, aaron@ekips.org
5542N/A
5542N/AThis program is free software; you can redistribute it and/or modify
5542N/Ait under the terms of the GNU General Public License as published by
5542N/Athe Free Software Foundation; either version 2 of the License, or
5542N/A(at your option) any later version.
5542N/A
5542N/AThis program is distributed in the hope that it will be useful,
5542N/Abut WITHOUT ANY WARRANTY; without even the implied warranty of
5542N/AMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5542N/AGNU General Public License for more details.
5542N/A
5542N/AYou should have received a copy of the GNU General Public License
5542N/Aalong with this program; if not, write to the Free Software
5542N/AFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
5542N/A'''
5542N/Aimport inkex, simpletransform, cubicsuperpath, simplestyle, cspsubdiv
5542N/A
5542N/Aclass MyEffect(inkex.Effect):
5542N/A def __init__(self):
5542N/A inkex.Effect.__init__(self)
5542N/A self.OptionParser.add_option("-f", "--flatness",
5542N/A action="store", type="float",
5542N/A dest="flat", default=0.2,
5542N/A help="Minimum flatness of the subdivided curves")
5542N/A self.OptionParser.add_option("-m", "--mirror",
5542N/A action="store", type="inkbool",
5542N/A dest="mirror", default="FALSE",
5542N/A help="Mirror Y-Axis")
5542N/A self.OptionParser.add_option("-x", "--xOrigin",
5542N/A action="store", type="float",
5542N/A dest="xOrigin", default=0.0,
5542N/A help="X Origin (pixels)")
5542N/A self.OptionParser.add_option("-y", "--yOrigin",
5542N/A action="store", type="float",
5542N/A dest="yOrigin", default=0.0,
5542N/A help="Y Origin (pixels)")
5542N/A self.OptionParser.add_option("-r", "--resolution",
5542N/A action="store", type="int",
5542N/A dest="resolution", default=1016,
5542N/A help="Resolution (dpi)")
5542N/A self.OptionParser.add_option("-n", "--pen",
5542N/A action="store", type="int",
5542N/A dest="pen", default=1,
5542N/A help="Pen number")
5542N/A self.OptionParser.add_option("-p", "--plotInvisibleLayers",
5542N/A action="store", type="inkbool",
5542N/A dest="plotInvisibleLayers", default="FALSE",
5542N/A help="Plot invisible layers")
5542N/A
5542N/A def output(self):
5542N/A print ''.join(self.hpgl)
5542N/A
5542N/A def process_path(self, node, mat):
5542N/A d = node.get('d')
5542N/A if d:
5542N/A p = cubicsuperpath.parsePath(d)
5542N/A trans = node.get('transform')
5542N/A if trans:
5542N/A mat = simpletransform.composeTransform(mat, simpletransform.parseTransform(trans))
5542N/A simpletransform.applyTransformToPath(mat, p)
5542N/A cspsubdiv.cspsubdiv(p, self.options.flat)
5542N/A for sp in p:
5542N/A first = True
5542N/A for csp in sp:
5542N/A cmd = 'PD'
5542N/A if first:
5542N/A cmd = 'PU'
5542N/A first = False
5542N/A self.hpgl.append('%s%d,%d;' % (cmd,csp[1][0],csp[1][1]))
5542N/A
5542N/A def process_group(self, group):
5542N/A style = group.get('style')
5542N/A if style:
5542N/A style = simplestyle.parseStyle(style)
5542N/A if style.has_key('display'):
5542N/A if style['display']=='none':
5542N/A if not self.options.plotInvisibleLayers:
5542N/A return
5542N/A trans = group.get('transform')
5542N/A if trans:
5542N/A self.groupmat.append(simpletransform.composeTransform(self.groupmat[-1], simpletransform.parseTransform(trans)))
5542N/A for node in group:
5542N/A if node.tag == inkex.addNS('path','svg'):
5542N/A self.process_path(node, self.groupmat[-1])
5542N/A if node.tag == inkex.addNS('g','svg'):
5542N/A self.process_group(node)
5542N/A if trans:
5542N/A self.groupmat.pop()
5542N/A
5542N/A def effect(self):
5542N/A self.hpgl = ['IN;SP%d;' % self.options.pen]
5542N/A x0 = self.options.xOrigin
5542N/A y0 = self.options.yOrigin
5542N/A scale = float(self.options.resolution)/90
5542N/A self.options.flat *= scale
5542N/A mirror = 1.0
5542N/A if self.options.mirror:
5542N/A mirror = -1.0
5542N/A if inkex.unittouu(self.document.getroot().xpath('@height', namespaces=inkex.NSS)[0]):
5542N/A y0 -= float(inkex.unittouu(self.document.getroot().xpath('@height', namespaces=inkex.NSS)[0]))
5542N/A self.groupmat = [[[scale, 0.0, -x0*scale], [0.0, mirror*scale, -y0*scale]]]
5542N/A doc = self.document.getroot()
5542N/A self.process_group(doc)
5542N/A self.hpgl.append('PU;')
5542N/A
5542N/Aif __name__ == '__main__': #pragma: no cover
5542N/A e = MyEffect()
5542N/A e.affect()
5542N/A
5542N/A# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
5542N/A