small fix for previous commit
[swftools.git] / wx / document.py
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 #
4 # gpdf2swf.py
5 # graphical user interface for pdf2swf
6 #
7 # Part of the swftools package.
8
9 # Copyright (c) 2008,2009 Matthias Kramm <kramm@quiss.org> 
10 #
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.
15 #
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.
20 #
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 */
24
25 from __future__ import division
26 import os
27 import time
28 import thread
29 import gfx
30 import wx
31 from wx.lib.pubsub import Publisher
32 from subprocess import Popen, PIPE
33
34 class _SaveSWFThread:
35     def __init__(self, filename, doc, pages, options):
36         self.__doc = doc
37         self.__filename = filename
38         self.__pages = pages or range(1, doc.pages+1)
39         self.__options = options
40
41     def Start(self):
42         self.__keep_running = self.__running = True
43         thread.start_new_thread(self.Run, ())
44         #self.Run()
45
46     def Stop(self):
47         self.__keep_running = False
48
49     def IsRunning(self):
50         return self.__running
51
52     def Run(self):
53         pages = len(self.__pages)
54         wx.CallAfter(Publisher.sendMessage, "SWF_BEGIN_SAVE",
55                                             {'pages': pages,})
56
57         time.sleep(0.05)
58
59         swf = gfx.SWF()
60         self.set_swf_parameters(swf)
61
62         plugin = self.__options.viewers.init(swf, self.__filename)
63
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)
68             page.render(swf)
69             swf.endpage()
70             wx.CallAfter(Publisher.sendMessage, "SWF_PAGE_SAVED",
71                                                 {'pagenr': i+1,
72                                                  'pages': pages,})
73             time.sleep(0.05)
74             if not self.__keep_running:
75                 break
76         else:
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)
81
82         # No need. But to be sure that it's clean up
83         # as soon as possible
84         del swf
85
86         wx.CallAfter(Publisher.sendMessage, "SWF_FILE_SAVED")
87         time.sleep(0.05)
88
89         self.__running = False
90
91     def set_swf_parameters(self, swf):
92         for opt in self.__options.quality:
93             if type(opt.name) in (tuple, list):
94                 for name, value in (
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
102                 ):
103                     #print "1.swf.setparameter(%s, %s)" % (name, value)
104                     swf.setparameter(str(name), str(value))
105             else:
106                 #print "2.swf.setparameter(%s, %s)" % (opt.name, str(opt.value))
107                 swf.setparameter(opt.name, str(opt.value))
108
109
110 class Document:
111     def __init__(self):
112         self.__page = None
113         self.__zoom = 1
114         self.__lastsavefile = "output.swf"
115
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)
121
122     def __SwapExtension(self, filename, newext):
123         basename, ext = os.path.splitext(filename)
124         return "%s.%s" % (basename, newext)
125
126     def __Reload(self):
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)})
131
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})
137
138     def ChangePage(self, pagenr=1, size=None):
139         self.__page = page = self.__pdf.getPage(pagenr)
140         self.__Reload()
141
142     def Fit(self, size):
143         w = size[0] / self.__page.width
144         h = size[1] / self.__page.height
145         self.__zoom = min(w, h)
146         self.__Reload()
147
148     def Zoom(self, zoom):
149         self.__zoom = 1 if zoom == 1 else self.__zoom + zoom
150         self.__Reload()
151
152     def GetThumbnails(self):
153         for pagenr in range(1, self.__pdf.pages + 1):
154             page = self.__pdf.getPage(pagenr)
155             yield page
156
157     def SaveSWF(self, filename, pages, options):
158         self.__lastsavefile = filename
159         t = _SaveSWFThread(filename, self.__pdf, pages, options)
160         t.Start()
161         return t
162         
163