Code

Partial fix for "make check" compilation failure.
[inkscape.git] / 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), 'fill': 'none'}\r
97         line_attribs = {'style': simplestyle.formatStyle(style),\r
98                         'id': name,\r
99                         'd': 'M '+str(x1)+','+str(y1)+' L '+str(x2)+','+str(y2)}\r
100         inkex.etree.SubElement(parent, 'path', line_attribs)\r
101 \r
102     def draw_bleed_line(self, x1, y1, x2, y2, name, parent):\r
103         style = { 'stroke': '#000000', 'stroke-width': str(self.stroke_width), 'fill': 'none',\r
104                   'stroke-miterlimit': '4', 'stroke-dasharray': '4, 2, 1, 2',\r
105                   'stroke-dashoffset': '0' }\r
106         line_attribs = {'style': simplestyle.formatStyle(style),\r
107                         'id': name,\r
108                         'd': 'M '+str(x1)+','+str(y1)+' L '+str(x2)+','+str(y2)}\r
109         inkex.etree.SubElement(parent, 'path', line_attribs)\r
110 \r
111     def draw_reg_circles(self, cx, cy, r, name, colours, parent):\r
112         for i in range(len(colours)):\r
113             style = {'stroke':colours[i], 'stroke-width':str(r / len(colours)),\r
114                      'fill':'none'}\r
115             circle_attribs = {'style':simplestyle.formatStyle(style),\r
116                               inkex.addNS('label','inkscape'):name,\r
117                               'cx':str(cx), 'cy':str(cy),\r
118                               'r':str((r / len(colours)) * (i + 0.5))}\r
119             inkex.etree.SubElement(parent, inkex.addNS('circle','svg'),\r
120                                    circle_attribs)\r
121 \r
122     def draw_reg_marks(self, cx, cy, rotate, name, parent):\r
123         colours = ['#000000','#00ffff','#ff00ff','#ffff00','#000000']\r
124         g = inkex.etree.SubElement(parent, 'g', { 'id': name })\r
125         for i in range(len(colours)):\r
126             style = {'fill':colours[i], 'fill-opacity':'1', 'stroke':'none'}\r
127             r = (self.mark_size/2)\r
128             step = r\r
129             stroke = r / len(colours)\r
130             regoffset = stroke * i\r
131             regmark_attribs = {'style': simplestyle.formatStyle(style),\r
132                                'd': 'm' +\\r
133                                ' '+str(-regoffset)+','+str(r)  +\\r
134                                ' '+str(-stroke)   +',0'        +\\r
135                                ' '+str(step)      +','+str(-r) +\\r
136                                ' '+str(-step)     +','+str(-r) +\\r
137                                ' '+str(stroke)    +',0'        +\\r
138                                ' '+str(step)      +','+str(r)  +\\r
139                                ' '+str(-step)     +','+str(r)  +\\r
140                                ' z',\r
141                                'transform': 'translate('+str(cx)+','+str(cy)+ \\r
142                                             ') rotate('+str(rotate)+')'}\r
143             inkex.etree.SubElement(g, 'path', regmark_attribs)\r
144 \r
145     def draw_star_target(self, cx, cy, name, parent):\r
146         r = (self.mark_size/2)\r
147         style = {'fill':'#000', 'fill-opacity':'1', 'stroke':'none'}\r
148         d = ' M 0,0'\r
149         i = 0\r
150         while i < ( 2 * math.pi ):\r
151             i += math.pi / 16\r
152             d += ' L 0,0 ' +\\r
153                  ' L '+ str(math.sin(i)*r) +','+ str(math.cos(i)*r) +\\r
154                  ' L '+ str(math.sin(i+0.09)*r) +','+ str(math.cos(i+0.09)*r)\r
155         regmark_attribs = {'style':simplestyle.formatStyle(style),\r
156                           inkex.addNS('label','inkscape'):name,\r
157                           'transform':'translate('+str(cx)+','+str(cy)+')',\r
158                           'd':d}\r
159         inkex.etree.SubElement(parent, inkex.addNS('path','svg'), regmark_attribs)\r
160 \r
161     def draw_coluor_bars(self, cx, cy, rotate, name, parent):\r
162         g = inkex.etree.SubElement(parent, 'g', {\r
163                 'id':name,\r
164                 'transform':'translate('+str(cx)+','+str(cy)+\\r
165                             ') rotate('+str(rotate)+')' })\r
166         l = min( self.mark_size / 3, max(self.width,self.height) / 45 )\r
167         for bar in [{'c':'*', 'stroke':'#000', 'x':0,        'y':-(l+1)},\r
168                     {'c':'r', 'stroke':'#0FF', 'x':0,        'y':0},\r
169                     {'c':'g', 'stroke':'#F0F', 'x':(l*11)+1, 'y':-(l+1)},\r
170                     {'c':'b', 'stroke':'#FF0', 'x':(l*11)+1, 'y':0}\r
171                    ]:\r
172             i = 0\r
173             while i <= 1:\r
174                 cr = '255'\r
175                 cg = '255'\r
176                 cb = '255'\r
177                 if bar['c'] == 'r' or bar['c'] == '*' : cr = str(255*i)\r
178                 if bar['c'] == 'g' or bar['c'] == '*' : cg = str(255*i)\r
179                 if bar['c'] == 'b' or bar['c'] == '*' : cb = str(255*i)\r
180                 r_att = {'fill':'rgb('+cr+','+cg+','+cb+')',\r
181                          'stroke':bar['stroke'],\r
182                          'stroke-width':'0.5',\r
183                          'x':str((l*i*10)+bar['x']), 'y':str(bar['y']),\r
184                          'width':str(l), 'height':str(l)}\r
185                 r = inkex.etree.SubElement(g, 'rect', r_att)\r
186                 i += 0.1\r
187 \r
188     def effect(self):\r
189 \r
190         if self.options.where_to_crop == 'selection' :\r
191             inkex.errormsg('Sory, the crop to selection is a TODO feature')\r
192 \r
193         # Get SVG document dimensions\r
194         svg = self.document.getroot()\r
195         self.width  = width  = inkex.unittouu(svg.get('width'))\r
196         self.height = height = inkex.unittouu(svg.attrib['height'])\r
197 \r
198         # Convert parameters to user unit\r
199         offset = inkex.unittouu(str(self.options.crop_offset) + self.options.unit)\r
200         bt = inkex.unittouu(str(self.options.bleed_top)    + self.options.unit)\r
201         bb = inkex.unittouu(str(self.options.bleed_bottom) + self.options.unit)\r
202         bl = inkex.unittouu(str(self.options.bleed_left)   + self.options.unit)\r
203         br = inkex.unittouu(str(self.options.bleed_right)  + self.options.unit)\r
204         # Bleed margin\r
205         if bt < offset : bmt = 0\r
206         else :           bmt = bt - offset\r
207         if bb < offset : bmb = 0\r
208         else :           bmb = bb - offset\r
209         if bl < offset : bml = 0\r
210         else :           bml = bl - offset\r
211         if br < offset : bmr = 0\r
212         else :           bmr = br - offset\r
213 \r
214         # Define the new document limits\r
215         left   = - offset\r
216         right  = width + offset\r
217         top    = - offset\r
218         bottom = height + offset\r
219 \r
220         # Test if printing-marks layer existis\r
221         layer = self.document.xpath(\r
222                      '//*[@id="printing-marks" and @inkscape:groupmode="layer"]',\r
223                      namespaces=inkex.NSS)\r
224         if layer: svg.remove(layer[0]) # remove if it existis\r
225         # Create a new layer\r
226         layer = inkex.etree.SubElement(svg, 'g')\r
227         layer.set('id', 'printing-marks')\r
228         layer.set(inkex.addNS('label', 'inkscape'), 'Printing Marks')\r
229         layer.set(inkex.addNS('groupmode', 'inkscape'), 'layer')\r
230         layer.set(inkex.addNS('insensitive', 'sodipodi'), 'true')\r
231 \r
232         # Crop Mark\r
233         if self.options.crop_marks == True:\r
234             # Create a group for Crop Mark\r
235             g_attribs = {inkex.addNS('label','inkscape'):'CropMarks',\r
236                                                     'id':'CropMarks'}\r
237             g_crops = inkex.etree.SubElement(layer, 'g', g_attribs)\r
238 \r
239             # Top left Mark\r
240             self.draw_crop_line(0, top,\r
241                                 0, top - self.mark_size,\r
242                                 'cropTL1', g_crops)\r
243             self.draw_crop_line(left, 0,\r
244                                 left - self.mark_size, 0,\r
245                                 'cropTL2', g_crops)\r
246 \r
247             # Top right Mark\r
248             self.draw_crop_line(width, top,\r
249                                 width , top - self.mark_size,\r
250                                 'cropTR1', g_crops)\r
251             self.draw_crop_line(right, 0,\r
252                                 right + self.mark_size, 0,\r
253                                 'cropTR2', g_crops)\r
254 \r
255             # Bottom left Mark\r
256             self.draw_crop_line(0, bottom,\r
257                                 0, bottom + self.mark_size,\r
258                                 'cropBL1', g_crops)\r
259             self.draw_crop_line(left, height,\r
260                                 left - self.mark_size, height,\r
261                                 'cropBL2', g_crops)\r
262 \r
263             # Bottom right Mark\r
264             self.draw_crop_line(width, bottom,\r
265                                 width, bottom + self.mark_size,\r
266                                 'cropBR1', g_crops)\r
267             self.draw_crop_line(right, height,\r
268                                 right + self.mark_size, height,\r
269                                 'cropBR2', g_crops)\r
270 \r
271         # Bleed Mark\r
272         if self.options.bleed_marks == True:\r
273             # Create a group for Bleed Mark\r
274             g_attribs = {inkex.addNS('label','inkscape'):'BleedMarks',\r
275                                                     'id':'BleedMarks'}\r
276             g_bleed = inkex.etree.SubElement(layer, 'g', g_attribs)\r
277 \r
278             # Top left Mark\r
279             self.draw_bleed_line(-bl, top - bmt,\r
280                                  -bl, top - bmt - self.mark_size,\r
281                                  'bleedTL1', g_bleed)\r
282             self.draw_bleed_line(left - bml, -bt,\r
283                                  left - bml - self.mark_size, -bt,\r
284                                  'bleedTL2', g_bleed)\r
285 \r
286             # Top right Mark\r
287             self.draw_bleed_line(width + br, top - bmt,\r
288                                  width + br, top - bmt - self.mark_size,\r
289                                  'bleedTR1', g_bleed)\r
290             self.draw_bleed_line(right + bmr, -bt,\r
291                                  right + bmr + self.mark_size, -bt,\r
292                                  'bleedTR2', g_bleed)\r
293 \r
294             # Bottom left Mark\r
295             self.draw_bleed_line(-bl, bottom + bmb,\r
296                                  -bl, bottom + bmb + self.mark_size,\r
297                                  'bleedBL1', g_bleed)\r
298             self.draw_bleed_line(left - bml, height + bb,\r
299                                  left - bml - self.mark_size, height + bb,\r
300                                  'bleedBL2', g_bleed)   \r
301 \r
302             # Bottom right Mark\r
303             self.draw_bleed_line(width + br, bottom + bmb,\r
304                                  width + br, bottom + bmb + self.mark_size,\r
305                                  'bleedBR1', g_bleed)\r
306             self.draw_bleed_line(right + bmr, height + bb,\r
307                                  right + bmr + self.mark_size, height + bb,\r
308                                  'bleedBR2', g_bleed)\r
309 \r
310         # Registration Mark\r
311         if self.options.reg_marks == True:\r
312             # Create a group for Registration Mark\r
313             g_attribs = {inkex.addNS('label','inkscape'):'RegistrationMarks',\r
314                                                     'id':'RegistrationMarks'}\r
315             g_center = inkex.etree.SubElement(layer, 'g', g_attribs)\r
316 \r
317             # Left Mark\r
318             cx = max( bml + offset, self.min_mark_margin )\r
319             self.draw_reg_marks(-cx - (self.mark_size/2),\r
320                                 (height/2) - self.mark_size*1.5,\r
321                                 '0', 'regMarkL', g_center)\r
322 \r
323             # Right Mark\r
324             cx = max( bmr + offset, self.min_mark_margin )\r
325             self.draw_reg_marks(width + cx + (self.mark_size/2),\r
326                                 (height/2) - self.mark_size*1.5,\r
327                                 '180', 'regMarkR', g_center)\r
328 \r
329             # Top Mark\r
330             cy = max( bmt + offset, self.min_mark_margin )\r
331             self.draw_reg_marks((width/2) - self.mark_size*1.5,\r
332                                 -cy - (self.mark_size/2),\r
333                                 '90', 'regMarkT', g_center)\r
334 \r
335             # Bottom Mark\r
336             cy = max( bmb + offset, self.min_mark_margin )\r
337             self.draw_reg_marks((width/2) - self.mark_size*1.5,\r
338                                 height + cy + (self.mark_size/2),\r
339                                 '-90', 'regMarkB', g_center)\r
340 \r
341         # Star Target\r
342         if self.options.star_target == True:\r
343             # Create a group for Star Target\r
344             g_attribs = {inkex.addNS('label','inkscape'):'StarTarget',\r
345                                                     'id':'StarTarget'}\r
346             g_center = inkex.etree.SubElement(layer, 'g', g_attribs)\r
347 \r
348             if height < width :\r
349                 # Left Star\r
350                 cx = max( bml + offset, self.min_mark_margin )\r
351                 self.draw_star_target(-cx - (self.mark_size/2),\r
352                                       (height/2),\r
353                                       'starTargetL', g_center)\r
354                 # Right Star\r
355                 cx = max( bmr + offset, self.min_mark_margin )\r
356                 self.draw_star_target(width + cx + (self.mark_size/2),\r
357                                       (height/2),\r
358                                       'starTargetR', g_center)\r
359             else :\r
360                 # Top Star\r
361                 cy = max( bmt + offset, self.min_mark_margin )\r
362                 self.draw_star_target((width/2),\r
363                                       -cy - (self.mark_size/2),\r
364                                       'starTargetT', g_center)\r
365                 # Bottom Star\r
366                 cy = max( bmb + offset, self.min_mark_margin )\r
367                 self.draw_star_target((width/2),\r
368                                       height + cy + (self.mark_size/2),\r
369                                       'starTargetB', g_center)\r
370 \r
371 \r
372         # Colour Bars\r
373         if self.options.colour_bars == True:\r
374             # Create a group for Colour Bars\r
375             g_attribs = {inkex.addNS('label','inkscape'):'ColourBars',\r
376                                                     'id':'PrintingColourBars'}\r
377             g_center = inkex.etree.SubElement(layer, 'g', g_attribs)\r
378 \r
379             if height > width :\r
380                 # Left Bars\r
381                 cx = max( bml + offset, self.min_mark_margin )\r
382                 self.draw_coluor_bars(-cx - (self.mark_size/2),\r
383                                       height/2,\r
384                                       90,\r
385                                       'PrintingColourBarsL', g_center)\r
386                 # Right Bars\r
387                 cx = max( bmr + offset, self.min_mark_margin )\r
388                 self.draw_coluor_bars(width + cx + (self.mark_size/2),\r
389                                       height/2,\r
390                                       90,\r
391                                       'PrintingColourBarsR', g_center)\r
392             else :\r
393                 # Top Bars\r
394                 cy = max( bmt + offset, self.min_mark_margin )\r
395                 self.draw_coluor_bars(width/2,\r
396                                       -cy - (self.mark_size/2),\r
397                                       0,\r
398                                       'PrintingColourBarsT', g_center)\r
399                 # Bottom Bars\r
400                 cy = max( bmb + offset, self.min_mark_margin )\r
401                 self.draw_coluor_bars(width/2,\r
402                                       height + cy + (self.mark_size/2),\r
403                                       0,\r
404                                       'PrintingColourBarsB', g_center)\r
405 \r
406 \r
407         # Page Information\r
408         if self.options.page_info == True:\r
409             # Create a group for Page Information\r
410             g_attribs = {inkex.addNS('label','inkscape'):'PageInformation',\r
411                                                     'id':'PageInformation'}\r
412             g_pag_info = inkex.etree.SubElement(layer, 'g', g_attribs)\r
413             y_margin = max( bmb + offset, self.min_mark_margin )\r
414             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
415                            'x':str(width/2), 'y':str(height+y_margin+self.mark_size+20)}\r
416             txt = inkex.etree.SubElement(g_pag_info, 'text', txt_attribs)\r
417             txt.text = 'Page size: ' +\\r
418                        str(round(inkex.uutounit(width,self.options.unit),2)) +\\r
419                        'x' +\\r
420                        str(round(inkex.uutounit(height,self.options.unit),2)) +\\r
421                        ' ' + self.options.unit\r
422 \r
423 \r
424 if __name__ == '__main__':\r
425     e = Printing_Marks()\r
426     e.affect()\r