-
Notifications
You must be signed in to change notification settings - Fork 3
/
graphics.py
207 lines (187 loc) · 7.05 KB
/
graphics.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
#
# graphics
#
# Copyright 2006 Alexander Igonichev. All rights reserved.
#
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
wxPython emulation of graphics module from Python for S60
"""
import wx
Draw=lambda x: x
def GetFont(fill,font):
fonts = {
u'LatinBold12': (8, wx.SWISS, wx.NORMAL, wx.BOLD, 'arial'),
u'LatinPlain12': (8, wx.SWISS, wx.NORMAL, wx.NORMAL, 'arial'),
u'LatinBold13': (9, wx.SWISS, wx.NORMAL, wx.BOLD, 'tahoma'),
u'LatinBold17': (12, wx.SWISS, wx.NORMAL, wx.BOLD, 'arial'),
u'LatinBold19': (13, wx.SWISS, wx.NORMAL, wx.BOLD, 'arial narrow'),
}
(size,family,style,weight,face) = fonts[font]
fnt = wx.Font(size,family,style,weight,False,face=face,encoding=wx.FONTENCODING_UTF8)
return fnt
class DC:
def __init__(self,size):
self._size = size
size=property(lambda self:self._size)
def Colour(self,clr):
if isinstance(clr,int):
b = clr % 256
g = (clr>>8) % 256
r = (clr>>16) % 256
else:
(r,g,b) = clr
return wx.Colour(r,g,b)
def SetColors(self,dc,outline=None,fill=None,width=1):
if fill and outline is None:
outline = fill
if not outline is None:
color = self.Colour(outline)
else:
color = 'BLACK'
dc.SetPen(wx.Pen(color,width))
if not fill is None:
color = self.Colour(fill)
elif self.bgcolor:
color = self.bgcolor
else:
color = 'WHITE'
dc.SetBrush(wx.Brush(color))
def Coords(self,coords,rect=0):
list = []
try:
for x in coords:
for i in (0,1):
list.append(int("%.0f"%x[i]))
except:
for x in coords:
list.append(int("%.0f"%x))
ret = []
i = 0
prev = None
nc = rect > 0 and 4 or 2
while i < len(list):
x = list[i:i+nc]
i += nc
if rect == 1:
#ret.append((x[0],x[1],x[2]-x[0]+1,x[3]-x[1]+1))
ret.append((x[0],x[1],x[2]-x[0],x[3]-x[1]))
elif rect == 2:
ret.append((x[0],x[1],x[2]-x[0]-1,x[3]-x[1]-1))
else:
if rect == 0:
if prev:
tmp = x
x = (prev[0],prev[1],x[0],x[1])
prev = tmp
else:
prev = x
continue
ret.append(x)
return ret
def text(self,coords,text,fill=0,font=u'LatinBold12'):
dc = (self.dc or wx.BufferedPaintDC(self, self.buffer))
Font = GetFont(fill,font)
dc.SetFont(Font)
#color = isinstance(fill,int) and wx.ColourRGB(fill) or wx.Colour(fill[0],fill[1],fill[2])
color = self.Colour(fill)
dc.SetTextForeground(color)
x = self.Coords(coords,-1)
dc.BeginDrawing()
dc.DrawText(text, x[0][0], x[0][1]-Font.GetPointSize()-3)
dc.EndDrawing()
def line(self,coords,outline=None,fill=None,width=1,pattern=None):
dc = (self.dc or wx.BufferedPaintDC(self, self.buffer))
dc.BeginDrawing()
self.SetColors(dc,outline,fill,width)
dc.DrawLineList(self.Coords(coords))
dc.EndDrawing()
def polygon(self,coords,outline=None,fill=None,width=1,pattern=None):
dc = (self.dc or wx.BufferedPaintDC(self, self.buffer))
dc.BeginDrawing()
self.SetColors(dc,outline,fill,width)
dc.DrawPolygon(self.Coords(coords,-1))
dc.EndDrawing()
def point(self,coords,outline=None,fill=None,width=1,pattern=None):
if width > 1:
new_coords = []
hw = width/2
for x in self.Coords(coords,-1):
new_coords.append(x[0]+hw)
new_coords.append(x[1]+hw)
new_coords.append(x[0]-hw)
new_coords.append(x[1]-hw)
self.ellipse(new_coords,outline,outline,1)
else:
dc = (self.dc or wx.BufferedPaintDC(self, self.buffer))
dc.BeginDrawing()
self.SetColors(dc,outline,fill,width)
dc.DrawPointList(self.Coords(coords,-1))
dc.EndDrawing()
def rectangle(self,coords,outline=None,fill=None,width=1,pattern=None):
dc = (self.dc or wx.BufferedPaintDC(self, self.buffer))
dc.BeginDrawing()
self.SetColors(dc,outline,fill,width)
dc.DrawRectangleList(self.Coords(coords,1))
dc.EndDrawing()
def ellipse(self,coords,outline=None,fill=None,width=1,pattern=None):
dc = (self.dc or wx.BufferedPaintDC(self, self.buffer))
dc.BeginDrawing()
self.SetColors(dc,outline,fill,width)
dc.DrawEllipseList(self.Coords(coords,2))
dc.EndDrawing()
def clear(self,color=0xffffff):
dc = (self.dc or wx.BufferedPaintDC(self, self.buffer))
self.SetColors(dc,fill=color)
dc.SetBackground(dc.GetBrush())
self.bgcolor = dc.GetBrush().GetColour()
dc.Clear()
def blit(self,image,target=(0,0),source=((0,0),(None,None)),mask=None,scale=0):
if mask:
image.buffer.SetMask(wx.Mask(mask.buffer,wx.Colour(0,0,0)))
dc = (self.dc or wx.BufferedPaintDC(self, self.buffer))
xdest = target[0]
ydest = target[0]
xsrc = source[0][0]
ysrc = source[0][1]
width = source[1][0] or image.size[0]
height = source[1][0] or image.size[1]
dc.Blit(target[0],target[1],width,height,image.dc,xsrc,ysrc,useMask=(mask and True or False))
class Image(DC):
def __init__(self,size):
DC.__init__(self,size)
dc = wx.MemoryDC()
bmp = wx.EmptyBitmap(size[0], size[1])
dc.SelectObject(bmp)
dc.Clear()
self.buffer = bmp
self.dc = dc
self.bgcolor = None
def new(size,mode=None):
return Image(size)
new=staticmethod(new)
if __name__ == '__main__':
app = wx.PySimpleApp()
img = Image.new((100,100))
print img
print img.size
app.MainLoop()