2 # -*- coding: UTF-8 -*-
5 # graphical user interface for pdf2swf
7 # Part of the swftools package.
9 # Copyright (c) 2008,2009 Matthias Kramm <kramm@quiss.org>
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
25 from __future__ import division
31 from wx.lib.pubsub import Publisher
32 from subprocess import Popen, PIPE
35 def __init__(self, pdffilename, filename, one_page_per_file, doc, pages, options):
37 self.__filename = filename
38 self.__pdffilename = pdffilename
39 self.__pages = pages or range(1, doc.pages+1)
40 self.__options = options
41 self.__one_page_per_file = one_page_per_file
44 self.__keep_running = self.__running = True
45 thread.start_new_thread(self.Run, ())
49 self.__keep_running = False
58 plugin = self.__options.viewers.init(swf, self.__filename)
60 wx.CallAfter(Publisher.sendMessage, "PLUGIN_ERROR")
61 self.__running = False
64 if self.__one_page_per_file and not plugin.one_page_per_file:
65 wx.CallAfter(Publisher.sendMessage,
66 "PLUGIN_ONE_PAGE_PER_FILE_NOT_SUPPORTED",
68 self.__running = False
71 pages = len(self.__pages)
72 wx.CallAfter(Publisher.sendMessage, "SWF_BEGIN_SAVE",
77 self.setparameters(gfx)
78 self.__doc = gfx.open("pdf", self.__pdffilename)
80 self.setparameters(swf)
83 plugin.before_render()
85 wx.CallAfter(Publisher.sendMessage, "PLUGIN_ERROR")
86 self.__running = False
89 for pagenr in self.__pages:
90 page = self.__doc.getPage(pagenr)
91 swf.startpage(page.width, page.height)
94 wx.CallAfter(Publisher.sendMessage, "SWF_PAGE_SAVED",
99 if self.__one_page_per_file:
100 form = '.%%0%dd.swf' % len(str(len(self.__pages)))
101 filename = self.__filename.replace('.swf', form % pagenr)
106 plugin = self.__options.viewers.init(swf, self.__filename)
108 wx.CallAfter(Publisher.sendMessage, "PLUGIN_ERROR")
109 self.__running = False
112 self.setparameters(swf)
114 if not self.__keep_running:
117 # This will not run if we break the for loop
118 if not self.__one_page_per_file:
120 plugin.before_save(page)
122 wx.CallAfter(Publisher.sendMessage, "PLUGIN_ERROR")
123 self.__running = False
125 swf.save(self.__filename)
127 plugin.after_save(page)
129 wx.CallAfter(Publisher.sendMessage, "PLUGIN_ERROR")
130 self.__running = False
133 # No need. But to be sure that it's clean up
134 # as soon as possible
137 wx.CallAfter(Publisher.sendMessage, "SWF_FILE_SAVED")
140 self.__running = False
142 def setparameters(self, swf):
144 for opt in self.__options.quality:
145 if type(opt.name) in (tuple, list):
147 # Example to better understand the list comprehension:
148 # opt.name = ['a', 'b', 'c']
149 # opt.value = [1, 2, 3]
150 # zip them = [('a',1), ('b', 2), ('c', 3)]
151 # pair will be in this example ('a', 1) due to
152 # the if pair[1] condition
153 pair for pair in zip(opt.name, opt.value) #if pair[1] == 1
155 #print "1.swf.setparameter(%s, %s)" % (name, value)
156 swf.setparameter(str(name), str(value))
158 #print "2.swf.setparameter(%s, %s)" % (opt.name, str(opt.value))
159 swf.setparameter(opt.name, str(opt.value))
161 #swf.setparameter('noclips', '1')
162 #swf.setparameter('reordertags', '1')
163 #swf.setparameter('animate', '1')
166 "title", "subject", "keywords", "author",
167 "creator", "producer", "creationdate", "moddate",
168 "linearized", "tagged", "encrypted", "oktoprint",
169 "oktocopy", "oktochange", "oktoaddnotes", "version",
172 class Document(object):
176 self.__lastsavefile = "output.swf"
177 self.__pdffilename = None
178 self.__preview_parameters = []
180 def __getattr__(self, name):
182 return self.__pdf.getInfo(name) or "n/a"
183 raise AttributeError, name
186 return self.__pdffilename
187 filename = property(filename)
189 def __get_lastsavefile(self):
190 return self.__lastsavefile
191 def __set_lastsavefile(self, lastsavefile):
192 self.__lastsavefile = lastsavefile
193 lastsavefile = property(__get_lastsavefile, __set_lastsavefile)
195 def __SwapExtension(self, filename, newext):
196 basename, ext = os.path.splitext(filename)
197 return "%s.%s" % (basename, newext)
200 Publisher.sendMessage("PAGE_CHANGED",
201 {'page': self.__page,
202 'width': int(self.__page.width * self.__zoom),
203 'height': int(self.__page.height * self.__zoom)})
205 def __get_preview_parameters(self):
206 return self.__preview_parameters
207 def __set_preview_parameters(self, parameters):
208 self.__preview_parameters = parameters
209 preview_parameters = property(__get_preview_parameters,
210 __set_preview_parameters)
212 def Load(self, filename):
213 self.__lastsavefile = self.__SwapExtension(filename, "swf")
214 self.__pdffilename = filename
216 #print 'Load',self.__preview_parameters
217 for parameter, value in self.__preview_parameters:
218 gfx.setparameter(parameter, value)
221 self.__pdf = gfx.open("pdf", filename)
223 Publisher.sendMessage("FILE_NOT_LOADED", {'filename': filename})
225 Publisher.sendMessage("FILE_LOADED", {'pages': self.__pdf.pages})
227 def ChangePage(self, pagenr=1, size=None):
228 self.__page = page = self.__pdf.getPage(pagenr)
232 w = size[0] / self.__page.width
233 h = size[1] / self.__page.height
234 self.__zoom = min(w, h)
237 def Zoom(self, zoom):
244 def GetThumbnails(self):
245 for pagenr in range(1, self.__pdf.pages + 1):
246 page = self.__pdf.getPage(pagenr)
249 def SaveSWF(self, filename, one_page_per_file, pages, options):
250 self.__lastsavefile = filename
251 t = _SaveSWFThread(self.__pdffilename, filename, one_page_per_file, self.__pdf, pages, options)