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, filename, doc, pages, options):
37 self.__filename = filename
38 self.__pages = pages or range(1, doc.pages+1)
39 self.__options = options
42 self.__keep_running = self.__running = True
43 thread.start_new_thread(self.Run, ())
47 self.__keep_running = False
53 pages = len(self.__pages)
54 wx.CallAfter(Publisher.sendMessage, "SWF_BEGIN_SAVE",
60 self.set_swf_parameters(swf)
62 plugin = self.__options.viewers.init(swf, self.__filename)
64 plugin.before_render()
65 for i, pagenr in enumerate(self.__pages):
66 page = self.__doc.getPage(pagenr)
67 swf.startpage(page.width, page.height)
70 wx.CallAfter(Publisher.sendMessage, "SWF_PAGE_SAVED",
74 if not self.__keep_running:
77 # This will not run if we break the for loop
78 plugin.before_save(page)
79 swf.save(self.__filename)
80 plugin.after_save(page)
82 # No need. But to be sure that it's clean up
86 wx.CallAfter(Publisher.sendMessage, "SWF_FILE_SAVED")
89 self.__running = False
91 def set_swf_parameters(self, swf):
92 for opt in self.__options.quality:
93 if type(opt.name) in (tuple, list):
95 # Example to better understand the list comprehension:
96 # opt.name = ['a', 'b', 'c']
97 # opt.value = [1, 2, 3]
98 # zip them = [('a',1), ('b', 2), ('c', 3)]
99 # pair will be in this example ('a', 1) due to
100 # the if pair[1] condition
101 pair for pair in zip(opt.name, opt.value) if pair[1] == 1
103 #print "1.swf.setparameter(%s, %s)" % (name, value)
104 swf.setparameter(str(name), str(value))
106 #print "2.swf.setparameter(%s, %s)" % (opt.name, str(opt.value))
107 swf.setparameter(opt.name, str(opt.value))
114 self.__lastsavefile = "output.swf"
116 def __get_lastsavefile(self):
117 return self.__lastsavefile
118 def __set_lastsavefile(self, lastsavefile):
119 self.__lastsavefile = lastsavefile
120 lastsavefile = property(__get_lastsavefile, __set_lastsavefile)
122 def __SwapExtension(self, filename, newext):
123 basename, ext = os.path.splitext(filename)
124 return "%s.%s" % (basename, newext)
127 Publisher.sendMessage("PAGE_CHANGED",
128 {'page': self.__page,
129 'width': int(self.__page.width * self.__zoom),
130 'height': int(self.__page.height * self.__zoom)})
132 def Load(self, filename):
133 self.__lastsavefile = self.__SwapExtension(filename, "swf")
134 #self.__htmlfilename = self.__SwapExtension(filename, "html")
135 self.__pdf = gfx.open("pdf", filename)
136 Publisher.sendMessage("FILE_LOADED", {'pages': self.__pdf.pages})
138 def ChangePage(self, pagenr=1, size=None):
139 self.__page = page = self.__pdf.getPage(pagenr)
143 w = size[0] / self.__page.width
144 h = size[1] / self.__page.height
145 self.__zoom = min(w, h)
148 def Zoom(self, zoom):
149 self.__zoom = 1 if zoom == 1 else self.__zoom + zoom
152 def GetThumbnails(self):
153 for pagenr in range(1, self.__pdf.pages + 1):
154 page = self.__pdf.getPage(pagenr)
157 def SaveSWF(self, filename, pages, options):
158 self.__lastsavefile = filename
159 t = _SaveSWFThread(filename, self.__pdf, pages, options)