gimppath2svg.py 3.5 KB
Newer Older
Salamandar's avatar
Salamandar committed
1
#!/usr/bin/env python2
2 3 4 5 6 7 8

import sys,re

"""
gimppath2svg.py -- Converts Gimp-Paths to a SVG-File.
Copyright (C) 2000  Simon Budig <simon@gimp.org>

9
This program is free software: you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 3 of the License, or
12 13 14 15 16 17 18 19
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
20
along with this program.  If not, see <https://www.gnu.org/licenses/>.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48


Usage: gimppath2svg.py [infile [outfile]]
"""


svgtemplate = """<?xml version="1.0" standalone="yes"?>
<svg width="%d" height="%d">
<g>
<path id="%s" transform="translate (%d,%d)"
      style="stroke:#000000; stroke-width:1; fill:none"
      d="%s"/>
</g>
</svg>
"""

emptysvgtemplate = """<?xml version="1.0" standalone="yes"?>
<svg width="1" height="1">
</svg>
"""


class Path:
   def __init__(self):
      self.name = ""
      self.svgpath = ""
      self.gimppoints = [[]]
      self.bounds = None
49

50 51 52
   def readgimpfile (self, filedesc):
      text = filedesc.readlines()
      for line in text:
Salamandar's avatar
Salamandar committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
         namematch = re.match ("Name: (.*)$", line)
         if namematch:
            path.name = namematch.group(1)
         pointmatch = re.match ("TYPE: (\d) X: (\d+) Y: (\d+)", line)
         if pointmatch:
            if pointmatch.group (1) == "3":
               path.gimppoints.append ([])
            (x, y) = map (int, pointmatch.groups()[1:])
            path.gimppoints[-1].append (map (int, pointmatch.groups()))
            if self.bounds:
               if self.bounds[0] > x: self.bounds[0] = x
               if self.bounds[1] > y: self.bounds[1] = y
               if self.bounds[2] < x: self.bounds[2] = x
               if self.bounds[3] < y: self.bounds[3] = y
            else:
               self.bounds = [x,y,x,y]
69

70 71 72
   def makesvg (self):
      for path in self.gimppoints:
         if path:
Salamandar's avatar
Salamandar committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
            start = path[0]
            svg = "M %d %d " % tuple (start[1:])
            path = path[1:]
            while path:
               curve = path [0:3]
               path = path[3:]
               if len (curve) == 2:
                  svg = svg + "C %d %d %d %d %d %d z " % tuple (
                           tuple (curve [0][1:]) +
                           tuple (curve [1][1:]) +
                           tuple (start [1:]))
               if len (curve) == 3:
                  svg = svg + "C %d %d %d %d %d %d " % tuple (
                           tuple (curve [0][1:]) +
                           tuple (curve [1][1:]) +
                           tuple (curve [2][1:]))
            self.svgpath = self.svgpath + svg
90 91 92

   def writesvgfile (self, outfile):
      if self.svgpath:
Salamandar's avatar
Salamandar committed
93 94 95 96 97
         svg = svgtemplate % (self.bounds[2]-self.bounds[0],
                              self.bounds[3]-self.bounds[1],
                              self.name,
                              -self.bounds[0], -self.bounds[1],
                              self.svgpath)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
      else:
         svg = emptysvgtemplate
      outfile.write (svg)


if len (sys.argv) > 1:
   infile = open (sys.argv[1])
else:
   infile = sys.stdin

if len (sys.argv) > 2:
   outfile = open (sys.argv[2], "w")
else:
   outfile = sys.stdout


path = Path()
path.readgimpfile (infile)
path.makesvg()
path.writesvgfile (outfile)