forked from sk1project/pymfvu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mfpage.py
280 lines (247 loc) · 7.17 KB
/
mfpage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# This program was made as part of the sk1project activity to improve UniConvertor
# See http://www.sk1project.org
#
# Copyright (C) 2007, Valek Filippov (frob@df.ru)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 3 or later of the GNU General Public
# License as published by the Free Software Foundation.
# 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 Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
#
import gtk
import cairo
import math
import pyemf
import pywmf
import wmfdraw
import wmfcmd
import emfdraw
import emfcmd
import hexdump
class Page:
def __init__(self):
self.type = 0
self.alpha = 1
self.ai = 1
self.fname = ''
self.hadj = None
self.hd = hexdump.hexdump()
self.hdv = 0
self.hdv2 = 0
self.curdc = 0
self.DCs = []
dc = DC()
self.DCs.append(dc)
self.zoom = 1.
self.scale = 1
self.width = 1.
self.height = 1.
self.kx = 1.
self.ky = 1.
self.VPx = 1.
self.VPy = 1.
self.VPOx = 0.
self.VPOy = 0.
self.maxobj = 0
self.curobj = 0
self.curbg = 0
self.curfg = 0
self.curpal = 0
self.mfobjs = {}
self.palette = {}
self.cmds = []
self.txtclr = color()
self.txtclr.r = 0.
self.txtclr.g = 0.
self.txtclr.b = 0.
self.txtalign = 0
self.bkmode = 0
self.bkcolor = color()
self.bkcolor.r,self.bkcolor.g,self.bkcolor.b = 255.,255.,255.
def usage():
class_cmds = cmd()
class_DCs = DC()
def parse(self):
pass
class DC:
def __init__(self):
self.VPx = 1.
self.VPy = 1.
self.VPOx = 0.
self.VPOy = 0.
self.Wx = 1.
self.Wy = 1.
self.x = 0.
self.y = 0.
self.cliplist = []
def usage():
class_cliplist = Clip()
class Clip:
path = []
type = 0 ## 0 -- Intersect, 1 -- Exclude
class Path:
def __init__(self):
self.pplist = []
self.clip = 0
def usage(self):
class_pplist = PathPoint()
def prep(self,ctx,xo=0,yo=0):
pathdraw = {1:self.pathmove,2:self.pathline,3:self.pathcurve,4:self.pathclose}
for i in self.pplist:
pathdraw[i.type](i,ctx,xo,yo)
def pathmove(self,i,ctx,xo,yo):
i.pts[0].x+=xo
i.pts[0].y+=yo
ctx.move_to(i.pts[0].x,i.pts[0].y)
## print 'Path moveto:',i.pts[0].x+xo,i.pts[0].y+yo
def pathline(self,i,ctx,xo,yo):
i.pts[0].x+=xo
i.pts[0].y+=yo
ctx.line_to(i.pts[0].x,i.pts[0].y)
## print 'Path lineto:',i.pts[0].x+xo,i.pts[0].y+yo
def pathcurve(self,i,ctx,xo,yo):
i.pts[0].x+=xo
i.pts[0].y+=yo
i.pts[1].x+=xo
i.pts[1].y+=yo
i.pts[2].x+=xo
i.pts[2].y+=yo
ctx.curve_to(i.pts[0].x,i.pts[0].y,i.pts[1].x,i.pts[1].y,i.pts[2].x,i.pts[2].y)
def pathclose(self,i,ctx,xo,yo):
ctx.close_path()
## print 'Path close'
class PathPoint:
def __init__(self):
self.type = 0 ## 0 - N/A, 1 - Move, 2 - Line, 3 - Curve, 4 - Close
self.pts = []
def usage(self):
class_pts = Point()
class Point:
def __init__(self):
self.x = 0.
self.y = 0.
class color:
r = 0.
g = 0.
b = 0.
a = 0.
class mfobj:
type = 0 # 1-- pen, 2 -- brush, 3 -- font
clr = color()
width = 5. ## use for pen
font = 'Arial'
italic = 0
size = 20
style = 0
escape = 0
orient = 0
under = 0
strike = 0
charset = 1
hatch = 0
data = None
flag = 0
class cmd:
type = ''
args = ()
class wmfPage(Page):
def __init__(self):
Page.__init__(self)
self.Face = wmfFace(self)
self.file = pywmf.WMF()
self.type = 1
def parse(self):
wmfcmd.parse(self)
class emfPage(Page):
def __init__(self):
Page.__init__(self)
self.Face = emfFace(self)
self.file = pyemf.EMF()
self.type = 2
bgcolor = color()
bgcolor.r,bgcolor.g,bgcolor.b = (255.,255.,255.)
bgcolor2 = color()
bgcolor2.r,bgcolor2.g,bgcolor2.b = (192.,192.,192.)
bgcolor3 = color()
bgcolor3.r,bgcolor3.g,bgcolor3.b = (128.,128.,128.)
bgcolor4 = color()
bgcolor4.r,bgcolor4.g,bgcolor4.b = (64.,64.,64.)
fgcolor = color()
fgcolor.r,fgcolor.g,fgcolor.b = (0,0,0)
eo = mfobj()
eo.clr = bgcolor
eo.type = 2
self.mfobjs[0x80000000] = eo
eo = mfobj()
eo.clr = bgcolor2
eo.type = 2
self.mfobjs[0x80000001] = eo
eo = mfobj()
eo.clr = bgcolor3
eo.type = 2
self.mfobjs[0x80000002] = eo
eo = mfobj()
eo.clr = bgcolor4
eo.type = 2
self.mfobjs[0x80000003] = eo
self.mfobjs[0] = eo
eo = mfobj()
eo.clr = fgcolor
eo.type = 2
self.mfobjs[0x80000004] = eo
self.mfobjs[0x80000005] = eo ## NULL brush -- don't fill
eo = mfobj()
eo.clr = bgcolor
eo.type = 1
self.mfobjs[0x80000006] = eo
eo = mfobj()
eo.clr = fgcolor
eo.type = 1
self.mfobjs[0x80000007] = eo
self.mfobjs[0x80000008] = eo ## NULL pen -- don't stroke
eo = mfobj()
eo.type = 3
self.mfobjs[0x8000000d] = eo ## system font
self.mfobjs[0x8000000e] = eo ## default font
self.cmds = []
def parse(self):
emfcmd.parse(self)
class Face(gtk.DrawingArea):
def __init__(self,page):
self.page = page
self.comms = 10
super(Face, self).__init__()
self.connect("expose_event", self.expose)
self._dragging = False # true if the interface is being dragged
def expose(self, widget, event):
self.ctx = widget.window.cairo_create()
self.ctx.rectangle(event.area.x,event.area.y,event.area.width,event.area.height)
self.ctx.clip()
self.draw(self.ctx)
return False
def draw(self, ctx):
print 'WxH: ',self.page.width,self.page.height
self.render(ctx,self.page)
def render(self,ctx,page):
pass
class wmfFace(Face):
def __init__(self,page):
Face.__init__(self,page)
def render(self,ctx,page):
if self.page.width !=1 and self.page.height != 1:
ctx.scale(self.page.width*1.*self.page.zoom,self.page.height*1.*self.page.zoom)
wmfdraw.render(self,ctx)
class emfFace(Face):
def __init__(self,page):
Face.__init__(self,page)
def render(self,ctx,page):
## ctx.scale(self.page.zoom,self.page.zoom)
emfdraw.render(self,ctx,page)