Code

add group code 70 to LWPOLYLINE
[inkscape.git] / share / extensions / gimp_xcf.py
index f295641f3509ba13b02b5a3c99ea5f92d538904b..121cb360d228356156f0a1dc9b43ca0e3dfc3e83 100755 (executable)
@@ -22,34 +22,93 @@ import sys, os, tempfile
 class MyEffect(inkex.Effect):
     def __init__(self):
         inkex.Effect.__init__(self)
+       self.OptionParser.add_option("-d", "--guides",
+                                     action="store", type="inkbool",
+                                     dest="saveGuides", default=False,
+                                     help="Save the Guides with the .XCF")
+       self.OptionParser.add_option("-r", "--grid",
+                                     action="store", type="inkbool",
+                                     dest="saveGrid", default=False,
+                                     help="Save the Grid with the .XCF")
     def output(self):
         pass
     def effect(self):
         svg_file = self.args[-1]
-        node = inkex.xml.xpath.Evaluate('/svg',self.document)[0]
-        docname = node.attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'docname').value[:-4]
+        docname = self.xpathSingle('/svg:svg/@sodipodi:docname')[:-4]
+       pageHeight = int(self.xpathSingle('/svg:svg/@height').split('.')[0])
+       pageWidth = int(self.xpathSingle('/svg:svg/@width').split('.')[0])
 
         #create os temp dir
         tmp_dir = tempfile.mkdtemp()
 
+       hGuides = []
+       vGuides = []
+       if self.options.saveGuides:
+               guideXpath = "sodipodi:namedview/sodipodi:guide" #grab all guide tags in the namedview tag
+               for guideNode in self.document.xpath(guideXpath, namespaces=inkex.NSS):
+                       ori = guideNode.get('orientation')
+                       if  ori == '0,1':
+                               #this is a horizontal guide
+                               pos = int(guideNode.get('position').split(',')[1].split('.')[0])
+                               #GIMP doesn't like guides that are outside of the image
+                               if pos > 0 and pos < pageHeight:
+                                       #the origin is at the top in GIMP land
+                                       hGuides.append(str(pageHeight - pos))
+                       elif ori == '1,0':
+                               #this is a vertical guide
+                               pos = int(guideNode.get('position').split(',')[0].split('.')[0])
+                               #GIMP doesn't like guides that are outside of the image
+                               if pos > 0 and pos < pageWidth:
+                                       vGuides.append(str(pos))
+
+       hGList = ' '.join(hGuides)
+       vGList = ' '.join(vGuides)
+
+       gridSpacingFunc = ''
+       gridOriginFunc = '' 
+       #GIMP only allows one rectangular grid
+       if self.options.saveGrid:
+               gridNode = self.xpathSingle("sodipodi:namedview/inkscape:grid[@type='xygrid' and (not(@units) or @units='px')]")
+               if gridNode != None:
+                       #these attributes could be nonexistant
+                       spacingX = gridNode.get('spacingx')
+                       if spacingX == None: spacingX = '1  '
+
+                       spacingY = gridNode.get('spacingy')
+                       if spacingY == None: spacingY = '1  '
+
+                       originX = gridNode.get('originx')
+                       if originX == None: originX = '0  '
+
+                       originY = gridNode.get('originy')
+                       if originY == None: originY = '0  '
+
+                       gridSpacingFunc = '(gimp-image-grid-set-spacing img %s %s)' % (spacingX[:-2], spacingY[:-2])
+                       gridOriginFunc = '(gimp-image-grid-set-offset img %s %s)'% (originX[:-2], originY[:-2])
+
         area = '--export-area-canvas'
         pngs = []
-        path = "/svg/*[name()='g' or @style][@id]"
-        for node in inkex.xml.xpath.Evaluate(path,self.document):
-            id = node.attributes.getNamedItem('id').value
+        names = []
+        path = "/svg:svg/*[name()='g' or @style][@id]"
+        for node in self.document.xpath(path, namespaces=inkex.NSS):
+            id = node.get('id')
             name = "%s.png" % id
             filename = os.path.join(tmp_dir, name)
             command = "inkscape -i %s -j %s -e %s %s " % (id, area, filename, svg_file)
-            f = os.popen(command,'r')
+            _,f,err = os.popen3(command,'r')
             f.read()
             f.close()
+            err.close()
             pngs.append(filename)
+            names.append(id)
 
         filelist = '"%s"' % '" "'.join(pngs)
+        namelist = '"%s"' % '" "'.join(names)
         xcf = os.path.join(tmp_dir, "%s.xcf" % docname)
         script_fu = """
+(tracing 1)
 (define
-  (png-to-layer img png_filename)
+  (png-to-layer img png_filename layer_name)
   (let*
     (
       (png (car (file-png-load RUN-NONINTERACTIVE png_filename png_filename)))
@@ -57,7 +116,7 @@ class MyEffect(inkex.Effect):
       (xcf_layer (car (gimp-layer-new-from-drawable png_layer img)))
     )
     (gimp-image-add-layer img xcf_layer -1)
-    (gimp-drawable-set-name xcf_layer png_filename)
+    (gimp-drawable-set-name xcf_layer layer_name)
   )
 )
 (let*
@@ -65,28 +124,51 @@ class MyEffect(inkex.Effect):
     (img (car (gimp-image-new 200 200 RGB)))
   )
   (gimp-image-undo-disable img)
-  (let* ((filelist '(%s)))
-    (while filelist
-      (let* ((filename (car filelist)))
-        (png-to-layer img filename)
-      )
-      (set! filelist (cdr filelist))
+  (for-each
+    (lambda (names)
+      (png-to-layer img (car names) (cdr names))
     )
+    (map cons '(%s) '(%s))
   )
+
   (gimp-image-resize-to-layers img)
+
+  (for-each
+    (lambda (hGuide)
+      (gimp-image-add-hguide img hGuide)
+    )
+    '(%s)
+  )
+
+  (for-each
+    (lambda (vGuide)
+      (gimp-image-add-vguide img vGuide)
+    )
+    '(%s)
+  )
+
+  %s
+  %s
+
   (gimp-image-undo-enable img)
   (gimp-file-save RUN-NONINTERACTIVE img (car (gimp-image-get-active-layer img)) "%s" "%s"))
 (gimp-quit 0)
-        """ % (filelist, xcf, xcf)
+        """ % (filelist, namelist, hGList, vGList, gridSpacingFunc, gridOriginFunc, xcf, xcf)
 
         junk = os.path.join(tmp_dir, 'junk_from_gimp.txt')
-        f = os.popen('gimp -i -b - > %s' % junk,'w')
+        f = os.popen('gimp -i --batch-interpreter plug-in-script-fu-eval -b - > %s 2>&1' % junk,'w')
         f.write(script_fu)
         f.close()
-        
+        # uncomment these lines to see the output from gimp
+        #err = open(junk, 'r')
+        #inkex.debug(err.read())
+        #err.close()
+
         x = open(xcf, 'r')
         sys.stdout.write(x.read())
         x.close()
 
-e = MyEffect()
-e.affect()
\ No newline at end of file
+if __name__ == '__main__':
+    e = MyEffect()
+    e.affect()
+