setup_typography_canvas.py 3.2 KB
Newer Older
1
#!/usr/bin/env python 
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
'''
Copyright (C) 2011 Felipe Correa da Silva Sanches

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
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 19 20 21 22 23 24 25
'''

import inkex
import sys

class SetupTypographyCanvas(inkex.Effect):
	def __init__(self):
		inkex.Effect.__init__(self)
26
		self.OptionParser.add_option("-e", "--emsize",
27
						action="store", type="int",
28 29
						dest="emsize", default=1000,
						help="Em-size")
30 31 32 33 34 35 36 37 38 39 40 41 42 43
		self.OptionParser.add_option("-a", "--ascender",
						action="store", type="int",
						dest="ascender", default='750',
						help="Ascender")
		self.OptionParser.add_option("-c", "--caps",
						action="store", type="int",
						dest="caps", default='700',
						help="Caps Height")
		self.OptionParser.add_option("-x", "--xheight",
						action="store", type="int",
						dest="xheight", default='500',
						help="x-height")
		self.OptionParser.add_option("-d", "--descender",
						action="store", type="int",
44
						dest="descender", default='250',
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
						help="Descender")

	def create_horizontal_guideline(self, name, position):
		self.create_guideline(name, "0,1", 0, position)

	def create_vertical_guideline(self, name, position):
		self.create_guideline(name, "1,0", position, 0)

	def create_guideline(self, label, orientation, x,y):
		namedview = self.svg.find(inkex.addNS('namedview', 'sodipodi'))
		guide = inkex.etree.SubElement(namedview, inkex.addNS('guide', 'sodipodi'))
		guide.set("orientation", orientation)
		guide.set("position", str(x)+","+str(y))
		guide.set(inkex.addNS('label', 'inkscape'), label)

	def effect(self):
		# Get all the options
62
		emsize = self.options.emsize
63 64 65 66 67 68 69
		ascender = self.options.ascender
		caps = self.options.caps
		xheight = self.options.xheight
		descender = self.options.descender

		# Get access to main SVG document element
		self.svg = self.document.getroot()
70 71
		self.svg.set("width", str(emsize))
		self.svg.set("height", str(emsize))
72
		self.svg.set("viewBox", "0 0 " + str(emsize) + " " + str(emsize) )
73

74
		baseline = descender
75 76 77 78 79
		# Create guidelines
		self.create_horizontal_guideline("baseline", baseline)
		self.create_horizontal_guideline("ascender", baseline+ascender)
		self.create_horizontal_guideline("caps", baseline+caps)
		self.create_horizontal_guideline("xheight", baseline+xheight)
80
		self.create_horizontal_guideline("descender", baseline-descender)
81

82 83 84 85
		namedview = self.svg.find(inkex.addNS('namedview', 'sodipodi'))
		namedview.set(inkex.addNS('document-units', 'inkscape'), 'px')
		namedview.set(inkex.addNS('cx', 'inkscape'), str(emsize/2.0 ))
		namedview.set(inkex.addNS('cy', 'inkscape'), str(emsize/2.0 ))
86 87


88 89 90 91
if __name__ == '__main__':
	e = SetupTypographyCanvas()
	e.affect()