Code

6940404342bf9349dd7ea2e3dad10e22210715db
[inkscape.git] / share / extensions / perfectboundcover.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2007 John Bintz, jcoswell@cosellproductions.org
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 '''
19 import sys, inkex
21 def caliper_to_ppi(caliper):
22     return 2 / caliper
24 def bond_weight_to_ppi(bond_weight):
25     return caliper_to_ppi(bond_weight * .0002)
27 def points_to_ppi(points):
28     return caliper_to_ppi(points / 1000.0)
30 class PerfectBoundCover(inkex.Effect):
31     def __init__(self):
32         inkex.Effect.__init__(self)
33         self.OptionParser.add_option("--width",
34                         action="store", type="float", 
35                         dest="width", default=6.0,
36                         help="cover width (in)")
37         self.OptionParser.add_option("--height",
38                         action="store", type="float", 
39                         dest="height", default=9.0,
40                         help="cover height (in)")
41         self.OptionParser.add_option("--pages",
42                         action="store", type="int",
43                         dest="pages", default=64,
44                         help="number of pages")
45         self.OptionParser.add_option("--paperthicknessmeasurement",
46                         action="store", type="string", 
47                         dest="paperthicknessmeasurement", default=100.0,
48                         help="paper thickness measurement")
49         self.OptionParser.add_option("--paperthickness",
50                         action="store", type="float", 
51                         dest="paperthickness", default=0.0,
52                         help="paper thickness")
53         self.OptionParser.add_option("--coverthicknessmeasurement",
54                         action="store", type="string", 
55                         dest="coverthicknessmeasurement", default=100.0,
56                         help="cover thickness measurement")
57         self.OptionParser.add_option("--coverthickness",
58                         action="store", type="float", 
59                         dest="coverthickness", default=0.0,
60                         help="cover thickness")
61         self.OptionParser.add_option("--bleed",
62                         action="store", type="float", 
63                         dest="bleed", default=0.25,
64                         help="cover bleed (in)")
65         self.OptionParser.add_option("--removeguides",
66                         action="store", type="inkbool", 
67                         dest="removeguides", default=False,
68                         help="remove guides")
69         self.OptionParser.add_option("--book",
70                         action="store", type="string",
71                         dest="book", default=False,
72                         help="dummy")
73         self.OptionParser.add_option("--cover",
74                         action="store", type="string",
75                         dest="cover", default=False,
76                         help="dummy")
77         self.OptionParser.add_option("--paper",
78                         action="store", type="string",
79                         dest="paper", default=False,
80                         help="dummy")
81         self.OptionParser.add_option("--warning",
82                         action="store", type="string",
83                         dest="warning", default=False,
84                         help="dummy")
85     def effect(self):
86         switch = {
87           "ppi": lambda x: x,
88           "caliper": lambda x: caliper_to_ppi(x),
89           "bond_weight": lambda x: bond_weight_to_ppi(x),
90           "points": lambda x: points_to_ppi(x),
91           "width": lambda x: x
92         }
94         if self.options.paperthickness > 0:
95             if self.options.paperthicknessmeasurement == "width":
96                 paper_spine = self.options.paperthickness
97             else:
98                 paper_spine = self.options.pages / switch[self.options.paperthicknessmeasurement](self.options.paperthickness)
99         else:
100             paper_spine = 0
102         if self.options.coverthickness > 0:
103             if self.options.coverthicknessmeasurement == "width":
104                 cover_spine = self.options.coverthickness
105             else:
106                 cover_spine = 4.0 / switch[self.options.coverthicknessmeasurement](self.options.coverthickness)
107         else:
108             cover_spine = 0
110         spine_width = paper_spine + cover_spine
112         document_width = (self.options.bleed + self.options.width * 2) + spine_width
113         document_height = self.options.bleed * 2 + self.options.height
115         root = self.document.getroot()
117         root.set("width", "%sin" % document_width)
118         root.set("height", "%sin" % document_height)
120         guides = []
122         guides.append(["horizontal", self.options.bleed])
123         guides.append(["horizontal", document_height - self.options.bleed])
124         guides.append(["vertical", self.options.bleed])
125         guides.append(["vertical", document_width - self.options.bleed])
126         guides.append(["vertical", (document_width / 2) - (spine_width / 2)])
127         guides.append(["vertical", (document_width / 2) + (spine_width / 2)])
129         namedview = self.document.xpath('/svg:svg/sodipodi:namedview', namespaces=inkex.NSS)
130         if namedview:
131             if self.options.removeguides == True:
132                 for node in self.document.xpath('/svg:svg/sodipodi:namedview/sodipodi:guide', namespaces=inkex.NSS):
133                     parent = node.getparent()
134                     parent.remove(node)
135             for guide in guides:
136                 newguide = inkex.etree.Element(inkex.addNS('guide','sodipodi'))
137                 newguide.set("orientation", guide[0])
138                 newguide.set("position", "%f" % (guide[1] * 90))
139                 namedview[0].append(newguide)
140         
141         '''
142         for id, node in self.selected.iteritems():
143             if node.tag == inkex.addNS('path','svg'):
144                 p = cubicsuperpath.parsePath(node.get('d'))
145                 
146                 #lens, total = csplength(p)
147                 #avg = total/numlengths(lens)
148                 #inkex.debug("average segment length: %s" % avg)
150                 new = []
151                 for sub in p:
152                     new.append([sub[0][:]])
153                     i = 1
154                     while i <= len(sub)-1:
155                         length = cspseglength(new[-1][-1], sub[i])
156                         if length > self.options.max:
157                             splits = math.ceil(length/self.options.max)
158                             for s in xrange(int(splits),1,-1):
159                                 new[-1][-1], next, sub[i] = cspbezsplitatlength(new[-1][-1], sub[i], 1.0/s)
160                                 new[-1].append(next[:])
161                         new[-1].append(sub[i])
162                         i+=1
163                     
164                 node.set('d',cubicsuperpath.formatPath(new))
165             '''
167 if __name__ == '__main__':
168     e = PerfectBoundCover()
169     e.affect()
172 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99