~ubuntu-branches/ubuntu/trusty/python-enable/trusty

« back to all changes in this revision

Viewing changes to enthought/savage/svg/backends/kiva/renderer.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2010-02-28 14:56:36 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100228145636-9ghfhe3uy37tt3q6
Tags: 3.3.0-1
* New upstream release
* Bump Standards-Version to 3.8.4
* Switch to source format 3.0
* Update patches/freetype2.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
 
45
45
    def elliptical_arc_to(self, rx, ry, phi, large_arc_flag, sweep_flag, x2, y2):
46
46
        if sys.platform == 'darwin':
47
 
            x1, y1 = path.get_current_point()
 
47
            x1, y1 = self.get_current_point()
48
48
        else:
49
49
            def _get_current_point(path):
50
50
                total_vertices = path.total_vertices()
69
69
            if i == 0:
70
70
                self.move_to(x1,y1)
71
71
            self.curve_to(x2,y2, x3,y3, x4,y4)
 
72
            
 
73
    def AddRoundedRectangleEx(self, x, y, w, h, rx, ry):
 
74
        #origin
 
75
        self.move_to(x+rx, y)
 
76
        self.line_to(x+w-rx, y)
 
77
        #top right
 
78
        cx = rx * 2
 
79
        cy = ry * 2
 
80
        self.AddEllipticalArcTo(
 
81
            x+w-cx, y,
 
82
            cx, cy,
 
83
            270, 90,
 
84
        )
 
85
        self.AddLineToPoint(x+w, y+h-ry)
 
86
        #top left
 
87
        self.AddEllipticalArcTo(
 
88
            x+w-cx, y+h-cy,
 
89
            cx, cy,
 
90
            0, 90,
 
91
        )
 
92
        self.line_to(x+rx, y+h)
 
93
        #bottom left
 
94
        self.AddEllipticalArcTo(
 
95
            x, y+h-cy,
 
96
            cx, cy,
 
97
            90,
 
98
            90,
 
99
        )
 
100
        self.line_to(x, y+ry)
 
101
        #bottom right
 
102
        self.AddEllipticalArcTo(
 
103
            x, y,
 
104
            cx, cy,
 
105
            180,
 
106
            90,
 
107
        )
 
108
        self.close_path()
 
109
        
72
110
 
73
111
 
74
112
Canvas = kiva.Canvas
130
168
        """ Set the appropriate properties on the GraphicsContext.
131
169
        """
132
170
        # translate from 0-255 to 0-1 values.
133
 
        color = tuple([x/255.0 for x in self.color])
 
171
        try:
 
172
            color = tuple([x/255.0 for x in list(self.color)])
 
173
        except:
 
174
            color = (0,0,0,1)
134
175
        gc.set_fill_color(color)
135
176
 
136
177
class LinearGradientBrush(AbstractGradientBrush):
137
178
    """ A Brush representing a linear gradient.
138
179
    """
139
180
    def __init__(self, x1,y1, x2,y2, stops, spreadMethod='pad',
140
 
        transforms=None, units='userSpaceOnUse'):
 
181
        transforms=[], units='userSpaceOnUse'):
141
182
        self.x1 = x1
142
183
        self.y1 = y1
143
184
        self.x2 = x2
146
187
        self.spreadMethod = spreadMethod
147
188
        self.transforms = transforms
148
189
        self.units = units
149
 
 
 
190
        
150
191
    def __repr__(self):
151
192
        return ('LinearGradientBrush(%r,%r, %r,%r, %r, spreadMethod=%r, '
152
193
            'transforms=%r, units=%r)' % (self.x1,self.y1, self.x2,self.y2, self.stops,
153
194
                self.spreadMethod, self.transforms, self.units))
154
195
 
155
196
    def set_on_gc(self, gc, bbox=None):
156
 
 
 
197
        
157
198
        # Apply transforms
158
199
        if self.transforms is not None:
159
 
            for func, args in self.transforms:
160
 
                func(gc, *args)
161
 
                
162
 
 
 
200
            for func, f_args in self.transforms:
 
201
                if isinstance(f_args, tuple):
 
202
                    func(gc, *f_args)
 
203
                else:
 
204
                    func(gc, f_args)
 
205
                    
163
206
        x1 = self.x1
164
207
        x2 = self.x2
165
208
        y1 = self.y1
179
222
                x2 = (bbox[2] + bbox[0])*x2
180
223
                y2 = (bbox[3] + bbox[1])*y2
181
224
                
182
 
            # kiva and SVGs have different origins, so flip the points
183
 
            y1 = gc.height() - y1
184
 
            y2 = gc.height() - y2
185
 
 
186
 
        if self.units == 'objectBoundingBox' and bbox is not None:
187
 
            self.bbox_transform(gc, bbox)
188
 
                
 
225
                self.bbox_transform(gc, bbox)
 
226
            
189
227
        stops = np.transpose(self.stops)
190
 
        gc.linear_gradient(x1, y1, x2, y2, stops, self.spreadMethod)            
 
228
        gc.linear_gradient(x1, y1, x2, y2, stops, self.spreadMethod, self.units)            
191
229
 
192
230
 
193
231
class RadialGradientBrush(AbstractGradientBrush):
194
232
    """ A Brush representing a radial gradient.
195
233
    """
196
234
    def __init__(self, cx,cy, r, stops, fx=None,fy=None, spreadMethod='pad',
197
 
        transforms=None, units='userSpaceOnUse'):
 
235
        transforms=[], units='userSpaceOnUse'):
198
236
        self.cx = cx
199
237
        self.cy = cy
200
238
        self.r = r
218
256
    def set_on_gc(self, gc, bbox=None):
219
257
        
220
258
        if self.transforms is not None:
221
 
            for func, args in self.transforms:
222
 
                func(gc, *args)
 
259
            for func, f_args in self.transforms:
 
260
                if isinstance(f_args, tuple):
 
261
                    func(gc, *f_args)
 
262
                else:
 
263
                    func(gc, f_args)
223
264
 
224
265
        cx = self.cx
225
266
        cy = self.cy
242
283
                fy = (bbox[3] + bbox[1])*fy
243
284
                r *= np.sqrt((bbox[2] - bbox[0])**2 + (bbox[3] - bbox[1])**2)
244
285
 
245
 
            # kiva and SVGs have different origins, so flip the points
246
 
            cy = gc.height()-cy
247
 
            fy = gc.height()-fy
248
 
 
249
 
        if self.units == 'objectBoundingBox' and bbox is not None:
250
 
            self.bbox_transform(gc, bbox)
251
 
 
252
 
            pass
253
 
 
254
 
                            
 
286
                self.bbox_transform(gc, bbox)
 
287
 
255
288
        stops = np.transpose(self.stops)
256
 
        gc.radial_gradient(cx, cy, r, fx, fy, stops, self.spreadMethod)
 
289
        gc.radial_gradient(cx, cy, r, fx, fy, stops, self.spreadMethod, self.units)
257
290
 
258
291
 
259
292
def font_style(font):
329
362
 
330
363
    @classmethod
331
364
    def createLinearGradientBrush(cls, x1,y1,x2,y2, stops, spreadMethod='pad',
332
 
                                  transforms=None, units='userSpaceOnUse'):
 
365
                                  transforms=[], units='userSpaceOnUse'):
333
366
        return LinearGradientBrush(x1,y1,x2,y2,stops, spreadMethod, transforms,
334
367
            units)
335
368
 
336
369
    @classmethod
337
370
    def createRadialGradientBrush(cls, cx,cy, r, stops, fx=None,fy=None,
338
 
                                  spreadMethod='pad', transforms=None,
 
371
                                  spreadMethod='pad', transforms=[],
339
372
                                  units='userSpaceOnUse'):
340
373
        return RadialGradientBrush(cx,cy, r, stops, fx,fy, spreadMethod,
341
374
            transforms, units)
456
489
    def gradientPath(cls, gc, path, brush):
457
490
        gc.save_state()
458
491
        gc.add_path(path)
459
 
        #gc.clip()
 
492
        
 
493
        gc.clip()
460
494
        if hasattr(path, 'get_bounding_box'):
461
495
            bbox = path.get_bounding_box()
462
496
        else:
469
503
                bbox[3] = max(bbox[3], vertex[0][1])
470
504
 
471
505
        brush.set_on_gc(gc, bbox=bbox)
 
506
        
 
507
        gc.close_path()
472
508
        gc.fill_path()
473
509
        gc.restore_state()
474
510
 
475
 
    #@classmethod
476
 
    #def clipPath(cls, gc, path):
477
 
        #print "clipping!"
478
 
        #return gc.clip_to_rect(100, 10, 100, 60)
479
 
        ##gc.add_path(path)
480
 
        ##return gc.clip()
 
511
    @classmethod
 
512
    def clipPath(cls, gc, path):
 
513
        gc.add_path(path)
 
514
        return gc.clip()
481
515
 
482
516
    @classmethod
483
517
    def translate(cls, gc, *args):