Code

updated spanish.nsh and inkscape.nsi to reflect latest file-changes
[inkscape.git] / trunk / share / extensions / printing-marks.py
1 #!/usr/bin/env python\r
2 '''\r
3 This extension allows you to draw crop, registration and other\r
4 printing marks in Inkscape.\r
5 \r
6 Authors:\r
7   Nicolas Dufour - Association Inkscape-fr\r
8   Aurelio A. Heckert <aurium(a)gmail.com>\r
9 \r
10 Copyright (C) 2008 Authors\r
11 \r
12 This program is free software; you can redistribute it and/or modify\r
13 it under the terms of the GNU General Public License as published by\r
14 the Free Software Foundation; either version 2 of the License, or\r
15 (at your option) any later version.\r
16 \r
17 This program is distributed in the hope that it will be useful,\r
18 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
20 GNU General Public License for more details.\r
21 \r
22 You should have received a copy of the GNU General Public License\r
23 along with this program; if not, write to the Free Software\r
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
25 '''\r
26 \r
27 import inkex, simplestyle, math\r
28 \r
29 class Printing_Marks (inkex.Effect):\r
30 \r
31     # Default parameters\r
32     stroke_width = 0.25\r
33     mark_size = inkex.unittouu('1cm')\r
34     min_mark_margin = inkex.unittouu('3mm')\r
35 \r
36     def __init__(self):\r
37         inkex.Effect.__init__(self)\r
38         self.OptionParser.add_option("--tab",\r
39                                      action="store", type="string",\r
40                                      dest="tab")\r
41         self.OptionParser.add_option("--where",\r
42                                      action="store", type="string",\r
43                                      dest="where_to_crop", default=True,\r
44                                      help="Apply crop marks to...")\r
45         self.OptionParser.add_option("--crop_marks",\r
46                                      action="store", type="inkbool",\r
47                                      dest="crop_marks", default=True,\r
48                                      help="Draw crop Marks?")\r
49         self.OptionParser.add_option("--bleed_marks",\r
50                                      action="store", type="inkbool",\r
51                                      dest="bleed_marks", default=False,\r
52                                      help="Draw Bleed Marks?")\r
53         self.OptionParser.add_option("--registration_marks",\r
54                                      action="store", type="inkbool",\r
55                                      dest="reg_marks", default=False,\r
56                                      help="Draw Registration Marks?")\r
57         self.OptionParser.add_option("--star_target",\r
58                                      action="store", type="inkbool",\r
59                                      dest="star_target", default=False,\r
60                                      help="Draw Star Target?")\r
61         self.OptionParser.add_option("--colour_bars",\r
62                                      action="store", type="inkbool",\r
63                                      dest="colour_bars", default=False,\r
64                                      help="Draw Colour Bars?")\r
65         self.OptionParser.add_option("--page_info",\r
66                                      action="store", type="inkbool",\r
67                                      dest="page_info", default=False,\r
68                                      help="Draw Page Information?")\r
69         self.OptionParser.add_option("--unit",\r
70                                      action="store", type="string",\r
71                                      dest="unit", default=100.0,\r
72                                      help="Draw measurment")\r
73         self.OptionParser.add_option("--crop_offset",\r
74                                      action="store", type="float",\r
75                                      dest="crop_offset", default=0,\r
76                                      help="Offset")\r
77         self.OptionParser.add_option("--bleed_top",\r
78                                      action="store", type="float",\r
79                                      dest="bleed_top", default=0,\r
80                                      help="Bleed Top Size")\r
81         self.OptionParser.add_option("--bleed_bottom",\r
82                                      action="store", type="float",\r
83                                      dest="bleed_bottom", default=0,\r
84                                      help="Bleed Bottom Size")\r
85         self.OptionParser.add_option("--bleed_left",\r
86                                      action="store", type="float",\r
87                                      dest="bleed_left", default=0,\r
88                                      help="Bleed Left Size")\r
89         self.OptionParser.add_option("--bleed_right",\r
90                                      action="store", type="float",\r
91                                      dest="bleed_right", default=0,\r
92                                      help="Bleed Right Size")\r
93 \r
94 \r
95     def draw_crop_line(self, x1, y1, x2, y2, name, parent):\r
96         style = { 'stroke': '#000000', 'stroke-width': str(self.stroke_width),\r
97                   'fill': 'none'}\r
98         line_attribs = {'style': simplestyle.formatStyle(style),\r
99                         'id': name,\r
100                         'd': 'M '+str(x1)+','+str(y1)+' L '+str(x2)+','+str(y2)}\r
101         inkex.etree.SubElement(parent, 'path', line_attribs)\r
102 \r
103     def draw_bleed_line(self, x1, y1, x2, y2, name, parent):\r
104         style = { 'stroke': '#000000', 'stroke-width': str(self.stroke_width),\r
105                   'fill': 'none',\r
106                   'stroke-miterlimit': '4', 'stroke-dasharray': '4, 2, 1, 2',\r
107                   'stroke-dashoffset': '0' }\r
108         line_attribs = {'style': simplestyle.formatStyle(style),\r
109                         'id': name,\r
110                         'd': 'M '+str(x1)+','+str(y1)+' L '+str(x2)+','+str(y2)}\r
111         inkex.etree.SubElement(parent, 'path', line_attribs)\r
112 \r
113     def draw_reg_circles(self, cx, cy, r, name, colours, parent):\r
114         for i in range(len(colours)):\r
115             style = {'stroke':colours[i], 'stroke-width':str(r / len(colours)),\r
116                      'fill':'none'}\r
117             circle_attribs = {'style':simplestyle.formatStyle(style),\r
118                               inkex.addNS('label','inkscape'):name,\r
119                               'cx':str(cx), 'cy':str(cy),\r
120                               'r':str((r / len(colours)) * (i + 0.5))}\r
121             inkex.etree.SubElement(parent, inkex.addNS('circle','svg'),\r
122                                    circle_attribs)\r
123 \r
124     def draw_reg_marks(self, cx, cy, rotate, name, parent):\r
125         colours = ['#000000','#00ffff','#ff00ff','#ffff00','#000000']\r
126         g = inkex.etree.SubElement(parent, 'g', { 'id': name })\r
127         for i in range(len(colours)):\r
128             style = {'fill':colours[i], 'fill-opacity':'1', 'stroke':'none'}\r
129             r = (self.mark_size/2)\r
130             step = r\r
131             stroke = r / len(colours)\r
132             regoffset = stroke * i\r
133             regmark_attribs = {'style': simplestyle.formatStyle(style),\r
134                                'd': 'm' +\\r
135                                ' '+str(-regoffset)+','+str(r)  +\\r
136                                ' '+str(-stroke)   +',0'        +\\r
137                                ' '+str(step)      +','+str(-r) +\\r
138                                ' '+str(-step)     +','+str(-r) +\\r
139                                ' '+str(stroke)    +',0'        +\\r
140                                ' '+str(step)      +','+str(r)  +\\r
141                                ' '+str(-step)     +','+str(r)  +\\r
142                                ' z',\r
143                                'transform': 'translate('+str(cx)+','+str(cy)+ \\r
144                                             ') rotate('+str(rotate)+')'}\r
145             inkex.etree.SubElement(g, 'path', regmark_attribs)\r
146 \r
147     def draw_star_target(self, cx, cy, name, parent):\r
148         r = (self.mark_size/2)\r
149         style = {'fill':'#000', 'fill-opacity':'1', 'stroke':'none'}\r
150         d = ' M 0,0'\r
151         i = 0\r
152         while i < ( 2 * math.pi ):\r
153             i += math.pi / 16\r
154             d += ' L 0,0 ' +\\r
155                  ' L '+ str(math.sin(i)*r) +','+ str(math.cos(i)*r) +\\r
156                  ' L '+ str(math.sin(i+0.09)*r) +','+ str(math.cos(i+0.09)*r)\r
157         regmark_attribs = {'style':simplestyle.formatStyle(style),\r
158                           inkex.addNS('label','inkscape'):name,\r
159                           'transform':'translate('+str(cx)+','+str(cy)+')',\r
160                           'd':d}\r
161         inkex.etree.SubElement(parent, inkex.addNS('path','svg'),\r
162                                regmark_attribs)\r
163 \r
164     def draw_coluor_bars(self, cx, cy, rotate, name, parent):\r
165         g = inkex.etree.SubElement(parent, 'g', {\r
166                 'id':name,\r
167                 'transform':'translate('+str(cx)+','+str(cy)+\\r
168                             ') rotate('+str(rotate)+')' })\r
169         l = min( self.mark_size / 3, max(self.width,self.height) / 45 )\r
170         for bar in [{'c':'*', 'stroke':'#000', 'x':0,        'y':-(l+1)},\r
171                     {'c':'r', 'stroke':'#0FF', 'x':0,        'y':0},\r
172                     {'c':'g', 'stroke':'#F0F', 'x':(l*11)+1, 'y':-(l+1)},\r
173                     {'c':'b', 'stroke':'#FF0', 'x':(l*11)+1, 'y':0}\r
174                    ]:\r
175             i = 0\r
176             while i <= 1:\r
177                 cr = '255'\r
178                 cg = '255'\r
179                 cb = '255'\r
180                 if bar['c'] == 'r' or bar['c'] == '*' : cr = str(255*i)\r
181                 if bar['c'] == 'g' or bar['c'] == '*' : cg = str(255*i)\r
182                 if bar['c'] == 'b' or bar['c'] == '*' : cb = str(255*i)\r
183                 r_att = {'fill':'rgb('+cr+','+cg+','+cb+')',\r
184                          'stroke':bar['stroke'],\r
185                          'stroke-width':'0.5',\r
186                          'x':str((l*i*10)+bar['x']), 'y':str(bar['y']),\r
187                          'width':str(l), 'height':str(l)}\r
188                 r = inkex.etree.SubElement(g, 'rect', r_att)\r
189                 i += 0.1\r
190 \r
191     def effect(self):\r
192 \r
193         if self.options.where_to_crop == 'selection' :\r
194             inkex.errormsg('Sory, the crop to selection is a TODO feature')\r
195 \r
196         # Get SVG document dimensions\r
197         svg = self.document.getroot()\r
198         self.width  = width  = inkex.unittouu(svg.get('width'))\r
199         self.height = height = inkex.unittouu(svg.attrib['height'])\r
200 \r
201         # Convert parameters to user unit\r
202         offset = inkex.unittouu(str(self.options.crop_offset) + \\r
203                                 self.options.unit)\r
204         bt = inkex.unittouu(str(self.options.bleed_top)    + self.options.unit)\r
205         bb = inkex.unittouu(str(self.options.bleed_bottom) + self.options.unit)\r
206         bl = inkex.unittouu(str(self.options.bleed_left)   + self.options.unit)\r
207         br = inkex.unittouu(str(self.options.bleed_right)  + self.options.unit)\r
208         # Bleed margin\r
209         if bt < offset : bmt = 0\r
210         else :           bmt = bt - offset\r
211         if bb < offset : bmb = 0\r
212         else :           bmb = bb - offset\r
213         if bl < offset : bml = 0\r
214         else :           bml = bl - offset\r
215         if br < offset : bmr = 0\r
216         else :           bmr = br - offset\r
217 \r
218         # Define the new document limits\r
219         left   = - offset\r
220         right  = width + offset\r
221         top    = - offset\r
222         bottom = height + offset\r
223 \r
224         # Test if printing-marks layer existis\r
225         layer = self.document.xpath(\r
226                      '//*[@id="printing-marks" and @inkscape:groupmode="layer"]',\r
227                      namespaces=inkex.NSS)\r
228         if layer: svg.remove(layer[0]) # remove if it existis\r
229         # Create a new layer\r
230         layer = inkex.etree.SubElement(svg, 'g')\r
231         layer.set('id', 'printing-marks')\r
232         layer.set(inkex.addNS('label', 'inkscape'), 'Printing Marks')\r
233         layer.set(inkex.addNS('groupmode', 'inkscape'), 'layer')\r
234         layer.set(inkex.addNS('insensitive', 'sodipodi'), 'true')\r
235 \r
236         # Crop Mark\r
237         if self.options.crop_marks == True:\r
238             # Create a group for Crop Mark\r
239             g_attribs = {inkex.addNS('label','inkscape'):'CropMarks',\r
240                                                     'id':'CropMarks'}\r
241             g_crops = inkex.etree.SubElement(layer, 'g', g_attribs)\r
242 \r
243             # Top left Mark\r
244             self.draw_crop_line(0, top,\r
245                                 0, top - self.mark_size,\r
246                                 'cropTL1', g_crops)\r
247             self.draw_crop_line(left, 0,\r
248                                 left - self.mark_size, 0,\r
249                                 'cropTL2', g_crops)\r
250 \r
251             # Top right Mark\r
252             self.draw_crop_line(width, top,\r
253                                 width , top - self.mark_size,\r
254                                 'cropTR1', g_crops)\r
255             self.draw_crop_line(right, 0,\r
256                                 right + self.mark_size, 0,\r
257                                 'cropTR2', g_crops)\r
258 \r
259             # Bottom left Mark\r
260             self.draw_crop_line(0, bottom,\r
261                                 0, bottom + self.mark_size,\r
262                                 'cropBL1', g_crops)\r
263             self.draw_crop_line(left, height,\r
264                                 left - self.mark_size, height,\r
265                                 'cropBL2', g_crops)\r
266 \r
267             # Bottom right Mark\r
268             self.draw_crop_line(width, bottom,\r
269                                 width, bottom + self.mark_size,\r
270                                 'cropBR1', g_crops)\r
271             self.draw_crop_line(right, height,\r
272                                 right + self.mark_size, height,\r
273                                 'cropBR2', g_crops)\r
274 \r
275         # Bleed Mark\r
276         if self.options.bleed_marks == True:\r
277             # Create a group for Bleed Mark\r
278             g_attribs = {inkex.addNS('label','inkscape'):'BleedMarks',\r
279                                                     'id':'BleedMarks'}\r
280             g_bleed = inkex.etree.SubElement(layer, 'g', g_attribs)\r
281 \r
282             # Top left Mark\r
283             self.draw_bleed_line(-bl, top - bmt,\r
284                                  -bl, top - bmt - self.mark_size,\r
285                                  'bleedTL1', g_bleed)\r
286             self.draw_bleed_line(left - bml, -bt,\r
287                                  left - bml - self.mark_size, -bt,\r
288                                  'bleedTL2', g_bleed)\r
289 \r
290             # Top right Mark\r
291             self.draw_bleed_line(width + br, top - bmt,\r
292                                  width + br, top - bmt - self.mark_size,\r
293                                  'bleedTR1', g_bleed)\r
294             self.draw_bleed_line(right + bmr, -bt,\r
295                                  right + bmr + self.mark_size, -bt,\r
296                                  'bleedTR2', g_bleed)\r
297 \r
298             # Bottom left Mark\r
299             self.draw_bleed_line(-bl, bottom + bmb,\r
300                                  -bl, bottom + bmb + self.mark_size,\r
301                                  'bleedBL1', g_bleed)\r
302             self.draw_bleed_line(left - bml, height + bb,\r
303                                  left - bml - self.mark_size, height + bb,\r
304                                  'bleedBL2', g_bleed)   \r
305 \r
306             # Bottom right Mark\r
307             self.draw_bleed_line(width + br, bottom + bmb,\r
308                                  width + br, bottom + bmb + self.mark_size,\r
309                                  'bleedBR1', g_bleed)\r
310             self.draw_bleed_line(right + bmr, height + bb,\r
311                                  right + bmr + self.mark_size, height + bb,\r
312                                  'bleedBR2', g_bleed)\r
313 \r
314         # Registration Mark\r
315         if self.options.reg_marks == True:\r
316             # Create a group for Registration Mark\r
317             g_attribs = {inkex.addNS('label','inkscape'):'RegistrationMarks',\r
318                                                     'id':'RegistrationMarks'}\r
319             g_center = inkex.etree.SubElement(layer, 'g', g_attribs)\r
320 \r
321             # Left Mark\r
322             cx = max( bml + offset, self.min_mark_margin )\r
323             self.draw_reg_marks(-cx - (self.mark_size/2),\r
324                                 (height/2) - self.mark_size*1.5,\r
325                                 '0', 'regMarkL', g_center)\r
326 \r
327             # Right Mark\r
328             cx = max( bmr + offset, self.min_mark_margin )\r
329             self.draw_reg_marks(width + cx + (self.mark_size/2),\r
330                                 (height/2) - self.mark_size*1.5,\r
331                                 '180', 'regMarkR', g_center)\r
332 \r
333             # Top Mark\r
334             cy = max( bmt + offset, self.min_mark_margin )\r
335             self.draw_reg_marks((width/2),\r
336                                 -cy - (self.mark_size/2),\r
337                                 '90', 'regMarkT', g_center)\r
338 \r
339             # Bottom Mark\r
340             cy = max( bmb + offset, self.min_mark_margin )\r
341             self.draw_reg_marks((width/2),\r
342                                 height + cy + (self.mark_size/2),\r
343                                 '-90', 'regMarkB', g_center)\r
344 \r
345         # Star Target\r
346         if self.options.star_target == True:\r
347             # Create a group for Star Target\r
348             g_attribs = {inkex.addNS('label','inkscape'):'StarTarget',\r
349                                                     'id':'StarTarget'}\r
350             g_center = inkex.etree.SubElement(layer, 'g', g_attribs)\r
351 \r
352             if height < width :\r
353                 # Left Star\r
354                 cx = max( bml + offset, self.min_mark_margin )\r
355                 self.draw_star_target(-cx - (self.mark_size/2),\r
356                                       (height/2),\r
357                                       'starTargetL', g_center)\r
358                 # Right Star\r
359                 cx = max( bmr + offset, self.min_mark_margin )\r
360                 self.draw_star_target(width + cx + (self.mark_size/2),\r
361                                       (height/2),\r
362                                       'starTargetR', g_center)\r
363             else :\r
364                 # Top Star\r
365                 cy = max( bmt + offset, self.min_mark_margin )\r
366                 self.draw_star_target((width/2) - self.mark_size*1.5,\r
367                                       -cy - (self.mark_size/2),\r
368                                       'starTargetT', g_center)\r
369                 # Bottom Star\r
370                 cy = max( bmb + offset, self.min_mark_margin )\r
371                 self.draw_star_target((width/2) - self.mark_size*1.5,\r
372                                       height + cy + (self.mark_size/2),\r
373                                       'starTargetB', g_center)\r
374 \r
375 \r
376         # Colour Bars\r
377         if self.options.colour_bars == True:\r
378             # Create a group for Colour Bars\r
379             g_attribs = {inkex.addNS('label','inkscape'):'ColourBars',\r
380                                                     'id':'PrintingColourBars'}\r
381             g_center = inkex.etree.SubElement(layer, 'g', g_attribs)\r
382 \r
383             if height > width :\r
384                 # Left Bars\r
385                 cx = max( bml + offset, self.min_mark_margin )\r
386                 self.draw_coluor_bars(-cx - (self.mark_size/2),\r
387                                       height/2,\r
388                                       90,\r
389                                       'PrintingColourBarsL', g_center)\r
390                 # Right Bars\r
391                 cx = max( bmr + offset, self.min_mark_margin )\r
392                 self.draw_coluor_bars(width + cx + (self.mark_size/2),\r
393                                       height/2,\r
394                                       90,\r
395                                       'PrintingColourBarsR', g_center)\r
396             else :\r
397                 # Top Bars\r
398                 cy = max( bmt + offset, self.min_mark_margin )\r
399                 self.draw_coluor_bars(width/2,\r
400                                       -cy - (self.mark_size/2),\r
401                                       0,\r
402                                       'PrintingColourBarsT', g_center)\r
403                 # Bottom Bars\r
404                 cy = max( bmb + offset, self.min_mark_margin )\r
405                 self.draw_coluor_bars(width/2,\r
406                                       height + cy + (self.mark_size/2),\r
407                                       0,\r
408                                       'PrintingColourBarsB', g_center)\r
409 \r
410 \r
411         # Page Information\r
412         if self.options.page_info == True:\r
413             # Create a group for Page Information\r
414             g_attribs = {inkex.addNS('label','inkscape'):'PageInformation',\r
415                                                     'id':'PageInformation'}\r
416             g_pag_info = inkex.etree.SubElement(layer, 'g', g_attribs)\r
417             y_margin = max( bmb + offset, self.min_mark_margin )\r
418             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
419                            'x':str(width/2), 'y':str(height+y_margin+self.mark_size+20)}\r
420             txt = inkex.etree.SubElement(g_pag_info, 'text', txt_attribs)\r
421             txt.text = 'Page size: ' +\\r
422                        str(round(inkex.uutounit(width,self.options.unit),2)) +\\r
423                        'x' +\\r
424                        str(round(inkex.uutounit(height,self.options.unit),2)) +\\r
425                        ' ' + self.options.unit\r
426 \r
427 \r
428 if __name__ == '__main__':\r
429     e = Printing_Marks()\r
430     e.affect()\r