Code

extension printing-marks recognizes selection area
[inkscape.git] / share / extensions / printing-marks.py
index 6128d7027c9bd231dffaa5021f14866abeecc2a2..3cebd665bbf1ad4e42bc7b1ce6c31e5560721986 100644 (file)
@@ -25,6 +25,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 '''
 
 import inkex, simplestyle, math
+from subprocess import Popen, PIPE, STDOUT
 
 class Printing_Marks (inkex.Effect):
 
@@ -166,7 +167,7 @@ class Printing_Marks (inkex.Effect):
                 'id':name,
                 'transform':'translate('+str(cx)+','+str(cy)+\
                             ') rotate('+str(rotate)+')' })
-        l = min( self.mark_size / 3, max(self.width,self.height) / 45 )
+        l = min( self.mark_size / 3, max(self.area_w,self.area_h) / 45 )
         for bar in [{'c':'*', 'stroke':'#000', 'x':0,        'y':-(l+1)},
                     {'c':'r', 'stroke':'#0FF', 'x':0,        'y':0},
                     {'c':'g', 'stroke':'#F0F', 'x':(l*11)+1, 'y':-(l+1)},
@@ -188,12 +189,58 @@ class Printing_Marks (inkex.Effect):
                 r = inkex.etree.SubElement(g, 'rect', r_att)
                 i += 0.1
 
+    def get_selection_area(self):
+        sel_area = {}
+        min_x, min_y, max_x, max_y = False, False, False, False
+        for id in self.options.ids:
+            sel_area[id] = {}
+            for att in [ "x", "y", "width", "height" ]:
+                args = [ "inkscape", "-I", id, "--query-"+att, self.svg_file ]
+                sel_area[id][att] = \
+                    Popen(args, stdout=PIPE, stderr=PIPE).communicate()[0]
+            current_min_x = float( sel_area[id]["x"] )
+            current_min_y = float( sel_area[id]["y"] )
+            current_max_x = float( sel_area[id]["x"] ) + \
+                            float( sel_area[id]["width"] )
+            current_max_y = float( sel_area[id]["y"] ) + \
+                            float( sel_area[id]["height"] )
+            if not min_x: min_x = current_min_x
+            if not min_y: min_y = current_min_y
+            if not max_x: max_x = current_max_x
+            if not max_y: max_y = current_max_y
+            if current_min_x < min_x: min_x = current_min_x
+            if current_min_y < min_y: min_y = current_min_y
+            if current_max_x > max_x: max_x = current_max_x
+            if current_max_y > max_y: max_y = current_max_y
+            #inkex.errormsg( '>> '+ id +
+            #                ' min_x:'+ str(min_x) +
+            #                ' min_y:'+ str(min_y) +
+            #                ' max_x:'+ str(max_x) +
+            #                ' max_y:'+ str(max_y) )
+        self.area_x1 = min_x
+        self.area_y1 = min_y
+        self.area_x2 = max_x
+        self.area_xy = max_y
+        self.area_w = max_x - min_x
+        self.area_h = max_y - min_y
+
     def effect(self):
 
         if self.options.where_to_crop == 'selection' :
+            self.get_selection_area()
             inkex.errormsg('Sory, the crop to selection is a TODO feature')
+            exit(1)
+        else :
+            svg = self.document.getroot()
+            self.area_w  = inkex.unittouu(svg.get('width'))
+            self.area_h  = inkex.unittouu(svg.attrib['height'])
+            self.area_x1 = 0
+            self.area_y1 = 0
+            self.area_x2 = self.area_w
+            self.area_xy = self.area_h
 
         # Get SVG document dimensions
+        # self.width must be replaced by self.area_x2. same to others.
         svg = self.document.getroot()
         self.width  = width  = inkex.unittouu(svg.get('width'))
         self.height = height = inkex.unittouu(svg.attrib['height'])
@@ -223,7 +270,8 @@ class Printing_Marks (inkex.Effect):
 
         # Test if printing-marks layer existis
         layer = self.document.xpath(
-                     '//*[@id="printing-marks" and @inkscape:groupmode="layer"]',
+                     '//*[@id="printing-marks" and
+@inkscape:groupmode="layer"]',
                      namespaces=inkex.NSS)
         if layer: svg.remove(layer[0]) # remove if it existis
         # Create a new layer
@@ -301,7 +349,7 @@ class Printing_Marks (inkex.Effect):
                                  'bleedBL1', g_bleed)
             self.draw_bleed_line(left - bml, height + bb,
                                  left - bml - self.mark_size, height + bb,
-                                 'bleedBL2', g_bleed)   
+                                 'bleedBL2', g_bleed)
 
             # Bottom right Mark
             self.draw_bleed_line(width + br, bottom + bmb,
@@ -384,25 +432,25 @@ class Printing_Marks (inkex.Effect):
                 # Left Bars
                 cx = max( bml + offset, self.min_mark_margin )
                 self.draw_coluor_bars(-cx - (self.mark_size/2),
-                                      height/2,
+                                      height/2 + self.mark_size,
                                       90,
                                       'PrintingColourBarsL', g_center)
                 # Right Bars
                 cx = max( bmr + offset, self.min_mark_margin )
                 self.draw_coluor_bars(width + cx + (self.mark_size/2),
-                                      height/2,
+                                      height/2 + self.mark_size,
                                       90,
                                       'PrintingColourBarsR', g_center)
             else :
                 # Top Bars
                 cy = max( bmt + offset, self.min_mark_margin )
-                self.draw_coluor_bars(width/2,
+                self.draw_coluor_bars(width/2 + self.mark_size,
                                       -cy - (self.mark_size/2),
                                       0,
                                       'PrintingColourBarsT', g_center)
                 # Bottom Bars
                 cy = max( bmb + offset, self.min_mark_margin )
-                self.draw_coluor_bars(width/2,
+                self.draw_coluor_bars(width/2 + self.mark_size,
                                       height + cy + (self.mark_size/2),
                                       0,
                                       'PrintingColourBarsB', g_center)
@@ -415,8 +463,11 @@ class Printing_Marks (inkex.Effect):
                                                     'id':'PageInformation'}
             g_pag_info = inkex.etree.SubElement(layer, 'g', g_attribs)
             y_margin = max( bmb + offset, self.min_mark_margin )
-            txt_attribs = {'style':'font-size:12px;font-style:normal;font-weight:normal;fill:#000000;font-family:Bitstream Vera Sans,sans-serif;text-anchor:middle;text-align:center',
-                           'x':str(width/2), 'y':str(height+y_margin+self.mark_size+20)}
+            txt_attribs =
+{'style':'font-size:12px;font-style:normal;font-weight:normal;fill:#000000;font-family:Bitstream
+Vera Sans,sans-serif;text-anchor:middle;text-align:center',
+                           'x':str(width/2),
+'y':str(height+y_margin+self.mark_size+20)}
             txt = inkex.etree.SubElement(g_pag_info, 'text', txt_attribs)
             txt.text = 'Page size: ' +\
                        str(round(inkex.uutounit(width,self.options.unit),2)) +\