Package pyjamas :: Package Canvas :: Module GWTCanvasImplDefault
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.Canvas.GWTCanvasImplDefault

  1  """ 
  2  * Copyright 2008 Google Inc. 
  3  * 
  4  * Licensed under the Apache License, Version 2.0 (the "License") you may not 
  5  * use this file except in compliance with the License. You may obtain a copy of 
  6  * the License at 
  7  * 
  8  * http:#www.apache.org/licenses/LICENSE-2.0 
  9  * 
 10  * Unless required by applicable law or agreed to in writing, software 
 11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 13  * License for the specific language governing permissions and limitations under 
 14  * the License. 
 15  """ 
 16   
 17   
 18   
 19   
 20  from pyjamas import DOM 
 21  from pyjamas import log 
 22  from pyjamas.ui.Widget import Widget 
 23   
 24  from pyjamas.Canvas.Color import Color 
 25   
26 -def cvt(s):
27 return s
28 29 """* 30 * Deferred binding implementation of GWTCanvas. 31 """
32 -class GWTCanvasImplDefault:
33
34 - def __init__(self):
35 self.canvasContext = None
36
37 - def arc(self, x, y, radius, startAngle, endAngle, antiClockwise):
38 self.canvasContext.arc(x,y,radius,startAngle,endAngle,antiClockwise)
39 40
41 - def beginPath(self):
42 self.canvasContext.beginPath()
43 44
45 - def clear(self, width, height):
46 self.clearRect(0,0,width,height)
47 48
49 - def closePath(self):
50 self.canvasContext.closePath()
51 52
53 - def createElement(self):
54 e = DOM.createElement("CANVAS") 55 self.setCanvasContext(e.getContext('2d')) 56 return e
57 58
59 - def cubicCurveTo(self, cp1x, cp1y, cp2x, cp2y, x, y):
60 self.canvasContext.bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y)
61 62
63 - def fillText(self, text, sourceX, sourceY, maxWidth=None):
64 # TODO: split this dog's dinner into browser-specific 65 # and pyjd-specific overrides... 66 67 try: 68 if maxWidth is None: 69 self.canvasContext.fillText(text, sourceX, sourceY) 70 else: 71 self.canvasContext.fillText(text, sourceX, sourceY, maxWidth) 72 except: 73 self.saveContext() 74 self.translate(sourceX, sourceY) 75 try: 76 text = unicode(text) # for pyjd / xulrunner 77 self.canvasContext.mozDrawText(text) # old xulrunner 78 except: 79 self.canvasContext.drawText(text) 80 self.restoreContext()
81
82 - def drawImage(self, img, sourceX, sourceY, sourceWidth=None, sourceHeight=None, destX=None, destY=None, destWidth=None, destHeight=None):
83 84 if isinstance(img, Widget): 85 img = img.getElement() 86 if sourceWidth is None: 87 self.canvasContext.drawImage(img,sourceX,sourceY) 88 else: 89 self.canvasContext.drawImage(img,sourceX,sourceY,sourceWidth,sourceHeight,destX,destY,destWidth,destHeight)
90 91 92
93 - def fill(self):
94 self.canvasContext.fill()
95 96
97 - def fillRect(self, startX, startY, width, height):
98 self.canvasContext.fillRect(startX,startY,width,height)
99 100
101 - def getGlobalAlpha(self):
102 return self.canvasContext.globalAlpha
103 104
106 return self.canvasContext.globalCompositeOperation
107 108
109 - def getHeight(self, elem):
110 return DOM.getElementPropertyInt(elem, "height")
111 112
113 - def getLineCap(self):
114 return self.canvasContext.lineCap
115 116
117 - def getLineJoin(self):
118 return self.canvasContext.lineJoin
119 120
121 - def getLineWidth(self):
122 return self.canvasContext.lineWidth
123 124
125 - def getMiterLimit(self):
126 return self.canvasContext.miterLimit
127 128
129 - def getWidth(self, elem):
130 return DOM.getElementPropertyInt(elem, "width")
131 132
133 - def lineTo(self, x, y):
134 self.canvasContext.lineTo(x,y)
135 136
137 - def moveTo(self, x, y):
138 self.canvasContext.moveTo(x,y)
139 140
141 - def quadraticCurveTo(self, cpx, cpy, x, y):
142 self.canvasContext.quadraticCurveTo(cpx,cpy,x,y)
143 144
145 - def rect(self, x, y, width, height):
146 self.canvasContext.rect(x,y,width,height)
147 148
149 - def restoreContext(self):
150 self.canvasContext.restore()
151 152
153 - def rotate(self, angle):
154 self.canvasContext.rotate(angle)
155 156
157 - def saveContext(self):
158 self.canvasContext.save()
159 160
161 - def scale(self, x, y):
162 self.canvasContext.scale(x,y)
163 164
165 - def setBackgroundColor(self, element, color):
166 DOM.setStyleAttribute(element, "backgroundColor", color)
167 168
169 - def setCoordHeight(self, elem, height):
170 DOM.setElemAttribute(elem, "height", str(height))
171 172
173 - def setCoordWidth(self, elem, width):
174 DOM.setElemAttribute(elem,"width", str(width))
175 176
177 - def setStrokeStyle(self, gradient):
178 if isinstance(gradient, Color): # is it a colorString? 179 gradient = str(gradient) 180 elif not isinstance(gradient, str): # is it a colorString? 181 gradient = gradient.getObject() # it's a gradient object 182 self.canvasContext.strokeStyle = cvt(gradient)
183 184
185 - def setFillStyle(self, gradient):
186 if isinstance(gradient, Color): # is it a colorString? 187 gradient = str(gradient) 188 elif not isinstance(gradient, str): # is it a colorString? 189 gradient = gradient.getObject() # it's a gradient object 190 self.canvasContext.fillStyle = cvt(gradient)
191
192 - def setGlobalAlpha(self, alpha):
193 self.canvasContext.globalAlpha = alpha
194 195
196 - def setGlobalCompositeOperation(self, globalCompositeOperation):
197 self.canvasContext.globalCompositeOperation = cvt(globalCompositeOperation)
198 199
200 - def setLineCap(self, lineCap):
201 self.canvasContext.lineCap = cvt(lineCap)
202 203
204 - def setLineJoin(self, lineJoin):
205 self.canvasContext.lineJoin = cvt(lineJoin)
206 207
208 - def setLineWidth(self, width):
209 self.canvasContext.lineWidth = width
210 211
212 - def setMiterLimit(self, miterLimit):
213 self.canvasContext.miterLimit = miterLimit
214 215
216 - def setPixelHeight(self, elem, height):
217 DOM.setStyleAttribute(elem, "height", "%dpx" % height)
218 219
220 - def setPixelWidth(self, elem, width):
221 DOM.setStyleAttribute(elem, "width", "%dpx" % width)
222 223
224 - def stroke(self):
225 self.canvasContext.stroke()
226 227
228 - def strokeRect(self, startX, startY, width, height):
229 self.canvasContext.strokeRect(startX,startY,width,height)
230
231 - def transform(self, m11, m12, m21, m22, dx, dy):
232 self.canvasContext.transform(m11,m12,m21,m22,dx,dy)
233 234
235 - def translate(self, x, y):
236 self.canvasContext.translate(x,y)
237 238
239 - def clearRect(self, startX, startY, width, height):
240 self.canvasContext.clearRect(startX,startY,width,height)
241 242
243 - def setCanvasContext(self, ctx):
244 self.canvasContext = ctx
245