Code

some improvements to printing-marks.py
authoraurium <aurium@users.sourceforge.net>
Mon, 1 Dec 2008 22:46:58 +0000 (22:46 +0000)
committeraurium <aurium@users.sourceforge.net>
Mon, 1 Dec 2008 22:46:58 +0000 (22:46 +0000)
share/extensions/printing-marks.py

index b75f32596ec99ad2f28ce080f0abc5ff7655fdf8..0a8d54abba9ba92b653a11b2621690f550dcacae 100644 (file)
@@ -1,13 +1,13 @@
 #!/usr/bin/env python\r
-'''
+'''\r
 This extension allows you to draw crop, registration and other\r
 printing marks in Inkscape.\r
-
-Authors:
-  Nicolas Dufour - Association Inkscape-fr
-  Aurelio A. Heckert <aurium(a)gmail.com>
-
-Copyright (C) 2008 Authors
+\r
+Authors:\r
+  Nicolas Dufour - Association Inkscape-fr\r
+  Aurelio A. Heckert <aurium(a)gmail.com>\r
+\r
+Copyright (C) 2008 Authors\r
 \r
 This program is free software; you can redistribute it and/or modify\r
 it under the terms of the GNU General Public License as published by\r
@@ -27,12 +27,12 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 import inkex, simplestyle, math\r
 \r
 class Printing_Marks (inkex.Effect):\r
-
-    # Default parameters
+\r
+    # Default parameters\r
     stroke_width = 0.25\r
     mark_size = inkex.unittouu('1cm')\r
     min_mark_margin = inkex.unittouu('3mm')\r
-
+\r
     def __init__(self):\r
         inkex.Effect.__init__(self)\r
         self.OptionParser.add_option("--tab",\r
@@ -91,7 +91,7 @@ class Printing_Marks (inkex.Effect):
                                      dest="bleed_right", default=0,\r
                                      help="Bleed Right Size")\r
 \r
-
+\r
     def draw_crop_line(self, x1, y1, x2, y2, name, parent):\r
         style = { 'stroke': '#000000', 'stroke-width': str(self.stroke_width), 'fill': 'none'}\r
         line_attribs = {'style': simplestyle.formatStyle(style),\r
@@ -123,13 +123,13 @@ class Printing_Marks (inkex.Effect):
         colours = ['#000000','#00ffff','#ff00ff','#ffff00','#000000']\r
         g = inkex.etree.SubElement(parent, 'g', { 'id': name })\r
         for i in range(len(colours)):\r
-            style = {'fill':colours[i], 'fill-opacity':'1', 'stroke':'none'}
+            style = {'fill':colours[i], 'fill-opacity':'1', 'stroke':'none'}\r
             r = (self.mark_size/2)\r
             step = r\r
             stroke = r / len(colours)\r
             regoffset = stroke * i\r
             regmark_attribs = {'style': simplestyle.formatStyle(style),\r
-                               'd': 'm' +\
+                               'd': 'm' +\\r
                                ' '+str(-regoffset)+','+str(r)  +\\r
                                ' '+str(-stroke)   +',0'        +\\r
                                ' '+str(step)      +','+str(-r) +\\r
@@ -138,7 +138,7 @@ class Printing_Marks (inkex.Effect):
                                ' '+str(step)      +','+str(r)  +\\r
                                ' '+str(-step)     +','+str(r)  +\\r
                                ' z',\r
-                               'transform': 'translate('+str(cx)+','+str(cy)+ \
+                               'transform': 'translate('+str(cx)+','+str(cy)+ \\r
                                             ') rotate('+str(rotate)+')'}\r
             inkex.etree.SubElement(g, 'path', regmark_attribs)\r
 \r
@@ -157,42 +157,43 @@ class Printing_Marks (inkex.Effect):
                           'transform':'translate('+str(cx)+','+str(cy)+')',\r
                           'd':d}\r
         inkex.etree.SubElement(parent, inkex.addNS('path','svg'), regmark_attribs)\r
-
-    def draw_coluor_bars(self, cx, cy, rotate, name, parent):
-        g = inkex.etree.SubElement(parent, 'g', {
-                'id':name,
-                'transform':'translate('+str(cx)+','+str(cy)+\
+\r
+    def draw_coluor_bars(self, cx, cy, rotate, name, parent):\r
+        g = inkex.etree.SubElement(parent, 'g', {\r
+                'id':name,\r
+                'transform':'translate('+str(cx)+','+str(cy)+\\r
                             ') rotate('+str(rotate)+')' })\r
-        l = self.mark_size / 3\r
-        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)},
-                    {'c':'b', 'stroke':'#FF0', 'x':(l*11)+1, 'y':0}
-                   ]:
+        l = min( self.mark_size / 3, max(self.width,self.height) / 45 )\r
+        for bar in [{'c':'*', 'stroke':'#000', 'x':0,        'y':-(l+1)},\r
+                    {'c':'r', 'stroke':'#0FF', 'x':0,        'y':0},\r
+                    {'c':'g', 'stroke':'#F0F', 'x':(l*11)+1, 'y':-(l+1)},\r
+                    {'c':'b', 'stroke':'#FF0', 'x':(l*11)+1, 'y':0}\r
+                   ]:\r
             i = 0\r
-            while i <= 1:
-                cr = '255'
-                cg = '255'
-                cb = '255'
-                if bar['c'] == 'r' or bar['c'] == '*' : cr = str(255*i)
-                if bar['c'] == 'g' or bar['c'] == '*' : cg = str(255*i)
-                if bar['c'] == 'b' or bar['c'] == '*' : cb = str(255*i)
-                r_att = {'fill':'rgb('+cr+','+cg+','+cb+')',
-                         'stroke':bar['stroke'],
-                         'x':str((l*i*10)+bar['x']), 'y':str(bar['y']),
-                         'width':str(l), 'height':str(l)}
+            while i <= 1:\r
+                cr = '255'\r
+                cg = '255'\r
+                cb = '255'\r
+                if bar['c'] == 'r' or bar['c'] == '*' : cr = str(255*i)\r
+                if bar['c'] == 'g' or bar['c'] == '*' : cg = str(255*i)\r
+                if bar['c'] == 'b' or bar['c'] == '*' : cb = str(255*i)\r
+                r_att = {'fill':'rgb('+cr+','+cg+','+cb+')',\r
+                         'stroke':bar['stroke'],\r
+                         'stroke-width':'0.5',\r
+                         'x':str((l*i*10)+bar['x']), 'y':str(bar['y']),\r
+                         'width':str(l), 'height':str(l)}\r
                 r = inkex.etree.SubElement(g, 'rect', r_att)\r
-                i += 0.1
+                i += 0.1\r
 \r
     def effect(self):\r
-
-        if self.options.where_to_crop == 'selection' :
-            inkex.errormsg('Sory, the crop to selection is a TODO feature')
+\r
+        if self.options.where_to_crop == 'selection' :\r
+            inkex.errormsg('Sory, the crop to selection is a TODO feature')\r
 \r
         # Get SVG document dimensions\r
         svg = self.document.getroot()\r
-        width  = inkex.unittouu(svg.get('width'))\r
-        height = inkex.unittouu(svg.attrib['height'])\r
+        self.width  = width  = inkex.unittouu(svg.get('width'))\r
+        self.height = height = inkex.unittouu(svg.attrib['height'])\r
 \r
         # Convert parameters to user unit\r
         offset = inkex.unittouu(str(self.options.crop_offset) + self.options.unit)\r
@@ -200,88 +201,94 @@ class Printing_Marks (inkex.Effect):
         bb = inkex.unittouu(str(self.options.bleed_bottom) + self.options.unit)\r
         bl = inkex.unittouu(str(self.options.bleed_left)   + self.options.unit)\r
         br = inkex.unittouu(str(self.options.bleed_right)  + self.options.unit)\r
-        # Bleed margin
-        if bt < offset : bmt = 0
+        # Bleed margin\r
+        if bt < offset : bmt = 0\r
         else :           bmt = bt - offset\r
-        if bb < offset : bmb = 0
+        if bb < offset : bmb = 0\r
         else :           bmb = bb - offset\r
-        if bl < offset : bml = 0
+        if bl < offset : bml = 0\r
         else :           bml = bl - offset\r
-        if br < offset : bmr = 0
+        if br < offset : bmr = 0\r
         else :           bmr = br - offset\r
-
+\r
         # Define the new document limits\r
         left   = - offset\r
         right  = width + offset\r
         top    = - offset\r
-        bottom = height + offset
+        bottom = height + offset\r
 \r
+        # Test if printing-marks layer existis\r
+        layer = self.document.xpath(\r
+                     '//*[@id="printing-marks" and @inkscape:groupmode="layer"]',\r
+                     namespaces=inkex.NSS)\r
+        if layer: svg.remove(layer[0]) # remove if it existis\r
         # Create a new layer\r
         layer = inkex.etree.SubElement(svg, 'g')\r
         layer.set('id', 'printing-marks')\r
         layer.set(inkex.addNS('label', 'inkscape'), 'Printing Marks')\r
         layer.set(inkex.addNS('groupmode', 'inkscape'), 'layer')\r
+        layer.set(inkex.addNS('insensitive', 'sodipodi'), 'true')\r
 \r
         # Crop Mark\r
         if self.options.crop_marks == True:\r
             # Create a group for Crop Mark\r
-            g_attribs = {inkex.addNS('label','inkscape'):'CropMarks',
+            g_attribs = {inkex.addNS('label','inkscape'):'CropMarks',\r
                                                     'id':'CropMarks'}\r
             g_crops = inkex.etree.SubElement(layer, 'g', g_attribs)\r
 \r
             # Top left Mark\r
-            self.draw_crop_line(0, top,
-                                0, top - self.mark_size,
+            self.draw_crop_line(0, top,\r
+                                0, top - self.mark_size,\r
                                 'cropTL1', g_crops)\r
-            self.draw_crop_line(left, 0,
-                                left - self.mark_size, 0,
+            self.draw_crop_line(left, 0,\r
+                                left - self.mark_size, 0,\r
                                 'cropTL2', g_crops)\r
 \r
             # Top right Mark\r
-            self.draw_crop_line(width, top,
-                                width , top - self.mark_size,
+            self.draw_crop_line(width, top,\r
+                                width , top - self.mark_size,\r
                                 'cropTR1', g_crops)\r
-            self.draw_crop_line(right, 0,
-                                right + self.mark_size, 0,
+            self.draw_crop_line(right, 0,\r
+                                right + self.mark_size, 0,\r
                                 'cropTR2', g_crops)\r
 \r
             # Bottom left Mark\r
-            self.draw_crop_line(0, bottom,
-                                0, bottom + self.mark_size,
+            self.draw_crop_line(0, bottom,\r
+                                0, bottom + self.mark_size,\r
                                 'cropBL1', g_crops)\r
-            self.draw_crop_line(left, height,
-                                left - self.mark_size, height,
+            self.draw_crop_line(left, height,\r
+                                left - self.mark_size, height,\r
                                 'cropBL2', g_crops)\r
 \r
             # Bottom right Mark\r
-            self.draw_crop_line(width, bottom,
-                                width, bottom + self.mark_size,
+            self.draw_crop_line(width, bottom,\r
+                                width, bottom + self.mark_size,\r
                                 'cropBR1', g_crops)\r
-            self.draw_crop_line(right, height,
-                                right + self.mark_size, height,
+            self.draw_crop_line(right, height,\r
+                                right + self.mark_size, height,\r
                                 'cropBR2', g_crops)\r
 \r
         # Bleed Mark\r
         if self.options.bleed_marks == True:\r
             # Create a group for Bleed Mark\r
-            g_attribs = {inkex.addNS('label','inkscape'):'BleedMarks',
+            g_attribs = {inkex.addNS('label','inkscape'):'BleedMarks',\r
                                                     'id':'BleedMarks'}\r
             g_bleed = inkex.etree.SubElement(layer, 'g', g_attribs)\r
 \r
-            # Top left Mark
-            self.draw_bleed_line(-bl, top - bmt,
-                                 -bl, top - bmt - self.mark_size,
+            # Top left Mark\r
+            self.draw_bleed_line(-bl, top - bmt,\r
+                                 -bl, top - bmt - self.mark_size,\r
                                  'bleedTL1', g_bleed)\r
-            self.draw_bleed_line(left - bml, -bt,
-                                 left - bml - self.mark_size, -bt,
+            self.draw_bleed_line(left - bml, -bt,\r
+                                 left - bml - self.mark_size, -bt,\r
                                  'bleedTL2', g_bleed)\r
 \r
             # Top right Mark\r
-            self.draw_bleed_line(width + br, top - bmt,
-                                 width + br, top - bmt - self.mark_size,
+            self.draw_bleed_line(width + br, top - bmt,\r
+                                 width + br, top - bmt - self.mark_size,\r
                                  'bleedTR1', g_bleed)\r
             self.draw_bleed_line(right + bmr, -bt,\r
-                                 right + bmr + self.mark_size, -bt,
+                                 right + bmr + self.mark_size, -bt,\r
                                  'bleedTR2', g_bleed)\r
 \r
             # Bottom left Mark\r
@@ -303,25 +310,25 @@ class Printing_Marks (inkex.Effect):
         # Registration Mark\r
         if self.options.reg_marks == True:\r
             # Create a group for Registration Mark\r
-            g_attribs = {inkex.addNS('label','inkscape'):'RegistrationMarks',
+            g_attribs = {inkex.addNS('label','inkscape'):'RegistrationMarks',\r
                                                     'id':'RegistrationMarks'}\r
-            g_center = inkex.etree.SubElement(layer, 'g', g_attribs)
+            g_center = inkex.etree.SubElement(layer, 'g', g_attribs)\r
 \r
-            # Left Mark
+            # Left Mark\r
             cx = max( bml + offset, self.min_mark_margin )\r
-            self.draw_reg_marks(-cx - (self.mark_size/2),
+            self.draw_reg_marks(-cx - (self.mark_size/2),\r
                                 (height/2) - self.mark_size*1.5,\r
                                 '0', 'regMarkL', g_center)\r
 \r
             # Right Mark\r
             cx = max( bmr + offset, self.min_mark_margin )\r
-            self.draw_reg_marks(width + cx + (self.mark_size/2),
+            self.draw_reg_marks(width + cx + (self.mark_size/2),\r
                                 (height/2) - self.mark_size*1.5,\r
                                 '180', 'regMarkR', g_center)\r
 \r
             # Top Mark\r
             cy = max( bmt + offset, self.min_mark_margin )\r
-            self.draw_reg_marks((width/2) - self.mark_size*1.5,
+            self.draw_reg_marks((width/2) - self.mark_size*1.5,\r
                                 -cy - (self.mark_size/2),\r
                                 '90', 'regMarkT', g_center)\r
 \r
@@ -334,25 +341,25 @@ class Printing_Marks (inkex.Effect):
         # Star Target\r
         if self.options.star_target == True:\r
             # Create a group for Star Target\r
-            g_attribs = {inkex.addNS('label','inkscape'):'StarTarget',
+            g_attribs = {inkex.addNS('label','inkscape'):'StarTarget',\r
                                                     'id':'StarTarget'}\r
             g_center = inkex.etree.SubElement(layer, 'g', g_attribs)\r
-
+\r
             if height < width :\r
                 # Left Star\r
                 cx = max( bml + offset, self.min_mark_margin )\r
-                self.draw_star_target(-cx - (self.mark_size/2),
+                self.draw_star_target(-cx - (self.mark_size/2),\r
                                       (height/2),\r
-                                      'starTargetL', g_center)
+                                      'starTargetL', g_center)\r
                 # Right Star\r
                 cx = max( bmr + offset, self.min_mark_margin )\r
-                self.draw_star_target(width + cx + (self.mark_size/2),
+                self.draw_star_target(width + cx + (self.mark_size/2),\r
                                       (height/2),\r
                                       'starTargetR', g_center)\r
             else :\r
                 # Top Star\r
                 cy = max( bmt + offset, self.min_mark_margin )\r
-                self.draw_star_target((width/2),
+                self.draw_star_target((width/2),\r
                                       -cy - (self.mark_size/2),\r
                                       'starTargetT', g_center)\r
                 # Bottom Star\r
@@ -360,39 +367,39 @@ class Printing_Marks (inkex.Effect):
                 self.draw_star_target((width/2),\r
                                       height + cy + (self.mark_size/2),\r
                                       'starTargetB', g_center)\r
-
+\r
 \r
         # Colour Bars\r
         if self.options.colour_bars == True:\r
             # Create a group for Colour Bars\r
-            g_attribs = {inkex.addNS('label','inkscape'):'ColourBars',
+            g_attribs = {inkex.addNS('label','inkscape'):'ColourBars',\r
                                                     'id':'PrintingColourBars'}\r
             g_center = inkex.etree.SubElement(layer, 'g', g_attribs)\r
-
+\r
             if height > width :\r
                 # Left Bars\r
                 cx = max( bml + offset, self.min_mark_margin )\r
-                self.draw_coluor_bars(-cx - (self.mark_size/2),
-                                      height/2,
+                self.draw_coluor_bars(-cx - (self.mark_size/2),\r
+                                      height/2,\r
                                       90,\r
                                       'PrintingColourBarsL', g_center)\r
                 # Right Bars\r
                 cx = max( bmr + offset, self.min_mark_margin )\r
-                self.draw_coluor_bars(width + cx + (self.mark_size/2),
-                                      height/2,
+                self.draw_coluor_bars(width + cx + (self.mark_size/2),\r
+                                      height/2,\r
                                       90,\r
                                       'PrintingColourBarsR', g_center)\r
             else :\r
                 # Top Bars\r
                 cy = max( bmt + offset, self.min_mark_margin )\r
-                self.draw_coluor_bars(width/2,
-                                      -cy - (self.mark_size/2),
+                self.draw_coluor_bars(width/2,\r
+                                      -cy - (self.mark_size/2),\r
                                       0,\r
                                       'PrintingColourBarsT', g_center)\r
                 # Bottom Bars\r
                 cy = max( bmb + offset, self.min_mark_margin )\r
-                self.draw_coluor_bars(width/2,
-                                      height + cy + (self.mark_size/2),
+                self.draw_coluor_bars(width/2,\r
+                                      height + cy + (self.mark_size/2),\r
                                       0,\r
                                       'PrintingColourBarsB', g_center)\r
 \r
@@ -400,17 +407,17 @@ class Printing_Marks (inkex.Effect):
         # Page Information\r
         if self.options.page_info == True:\r
             # Create a group for Page Information\r
-            g_attribs = {inkex.addNS('label','inkscape'):'PageInformation',
+            g_attribs = {inkex.addNS('label','inkscape'):'PageInformation',\r
                                                     'id':'PageInformation'}\r
-            g_pag_info = inkex.etree.SubElement(layer, 'g', g_attribs)
+            g_pag_info = inkex.etree.SubElement(layer, 'g', g_attribs)\r
             y_margin = max( bmb + offset, self.min_mark_margin )\r
-            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',
+            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',\r
                            'x':str(width/2), 'y':str(height+y_margin+self.mark_size+20)}\r
-            txt = inkex.etree.SubElement(g_pag_info, 'text', txt_attribs)
-            txt.text = 'Page size: ' +\
-                       str(round(inkex.uutounit(width,self.options.unit),2)) +\
-                       'x' +\
-                       str(round(inkex.uutounit(height,self.options.unit),2)) +\
+            txt = inkex.etree.SubElement(g_pag_info, 'text', txt_attribs)\r
+            txt.text = 'Page size: ' +\\r
+                       str(round(inkex.uutounit(width,self.options.unit),2)) +\\r
+                       'x' +\\r
+                       str(round(inkex.uutounit(height,self.options.unit),2)) +\\r
                        ' ' + self.options.unit\r
 \r
 \r