|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Extracted from inkscape extension; original comments below: |
| 4 | +''' |
| 5 | +This extension converts a path into a dashed line using 'stroke-dasharray' |
| 6 | +It is a modification of the file addnodes.py |
| 7 | +
|
| 8 | +Copyright (C) 2005,2007 Aaron Spike, aaron@ekips.org |
| 9 | +Copyright (C) 2009 Alvin Penner, penner@vaxxine.com |
| 10 | +
|
| 11 | +This program is free software; you can redistribute it and/or modify |
| 12 | +it under the terms of the GNU General Public License as published by |
| 13 | +the Free Software Foundation; either version 2 of the License, or |
| 14 | +(at your option) any later version. |
| 15 | +
|
| 16 | +This program is distributed in the hope that it will be useful, |
| 17 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +GNU General Public License for more details. |
| 20 | +
|
| 21 | +You should have received a copy of the GNU General Public License |
| 22 | +along with this program; if not, write to the Free Software |
| 23 | +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | +''' |
| 25 | +# local library |
| 26 | +import cubicsuperpath |
| 27 | +import bezmisc |
| 28 | +import simplestyle |
| 29 | + |
| 30 | +def tpoint((x1,y1), (x2,y2), t = 0.5): |
| 31 | + return [x1+t*(x2-x1),y1+t*(y2-y1)] |
| 32 | +def cspbezsplit(sp1, sp2, t = 0.5): |
| 33 | + m1=tpoint(sp1[1],sp1[2],t) |
| 34 | + m2=tpoint(sp1[2],sp2[0],t) |
| 35 | + m3=tpoint(sp2[0],sp2[1],t) |
| 36 | + m4=tpoint(m1,m2,t) |
| 37 | + m5=tpoint(m2,m3,t) |
| 38 | + m=tpoint(m4,m5,t) |
| 39 | + return [[sp1[0][:],sp1[1][:],m1], [m4,m,m5], [m3,sp2[1][:],sp2[2][:]]] |
| 40 | +def cspbezsplitatlength(sp1, sp2, l = 0.5, tolerance = 0.001): |
| 41 | + bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:]) |
| 42 | + t = bezmisc.beziertatlength(bez, l, tolerance) |
| 43 | + return cspbezsplit(sp1, sp2, t) |
| 44 | +def cspseglength(sp1,sp2, tolerance = 0.001): |
| 45 | + bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:]) |
| 46 | + return bezmisc.bezierlength(bez, tolerance) |
| 47 | + |
| 48 | +def splitPath(inkex, node): |
| 49 | + dashes = [] |
| 50 | + style = simplestyle.parseStyle(node.get('style')) |
| 51 | + if style.has_key('stroke-dasharray'): |
| 52 | + if style['stroke-dasharray'].find(',') > 0: |
| 53 | + dashes = [float (dash) for dash in style['stroke-dasharray'].split(',') if dash] |
| 54 | + if dashes: |
| 55 | + p = cubicsuperpath.parsePath(node.get('d')) |
| 56 | + new = [] |
| 57 | + for sub in p: |
| 58 | + idash = 0 |
| 59 | + dash = dashes[0] |
| 60 | + length = 0 |
| 61 | + new.append([sub[0][:]]) |
| 62 | + i = 1 |
| 63 | + while i < len(sub): |
| 64 | + dash = dash - length |
| 65 | + length = cspseglength(new[-1][-1], sub[i]) |
| 66 | + while dash < length: |
| 67 | + new[-1][-1], next, sub[i] = cspbezsplitatlength(new[-1][-1], sub[i], dash/length) |
| 68 | + if idash % 2: # create a gap |
| 69 | + new.append([next[:]]) |
| 70 | + else: # splice the curve |
| 71 | + new[-1].append(next[:]) |
| 72 | + length = length - dash |
| 73 | + idash = (idash + 1) % len(dashes) |
| 74 | + dash = dashes[idash] |
| 75 | + if idash % 2: |
| 76 | + new.append([sub[i]]) |
| 77 | + else: |
| 78 | + new[-1].append(sub[i]) |
| 79 | + i+=1 |
| 80 | + node.set('d',cubicsuperpath.formatPath(new)) |
| 81 | + del style['stroke-dasharray'] |
| 82 | + node.set('style', simplestyle.formatStyle(style)) |
| 83 | + if node.get(inkex.addNS('type','sodipodi')): |
| 84 | + del node.attrib[inkex.addNS('type', 'sodipodi')] |
0 commit comments