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