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