Rename GP_Context -> GP_Pixmap
[gfxprim.git] / pylib / gfxprim / gfx / __init__.py
bloba6bd238dbcb41d7433ade5cdd2c37024d9650dd0
1 """
2 Module extending the Pixmap class with .gfx submodule and its drawing functions.
4 Use as in "import gfxprim.gfx; pixmap_foo.gfx.Line(...)"
5 """
7 # Import the SWIG wrapper
8 from . import c_gfx
10 # Constants module
11 from . import C
13 def _init(module):
14 "Extend Pixmap with gfx submodule"
16 from ..utils import extend, add_swig_getmethod, add_swig_setmethod
17 from ..core import Pixmap as _pixmap
19 # New Pixmap submodule
20 class GfxSubmodule(object):
21 def __init__(self, ctx):
22 self.ctx = ctx
23 self.C = C
25 _pixmap._submodules['gfx'] = GfxSubmodule
27 # Imports from the SWIG module
28 from ..utils import import_members, extend_submodule
29 import re
30 def strip_GP(s):
31 return re.sub('^GP_', '', s)
33 const_regexes = [
34 '^GP_[A-Z0-9_]*$',
36 import_members(c_gfx, C, include=const_regexes, sub=strip_GP)
38 for name in [
39 'ArcSegment', 'Circle', 'Ellipse', 'FillCircle', 'FillEllipse',
40 'FillRect', 'FillRing',
41 'FillTetragon', 'FillTriangle', 'HLine', 'HLineAA', 'Line', 'LineAA',
42 'PutPixelAA', 'Rect', 'Ring', 'Tetragon',
43 'Triangle', 'VLine', 'VLineAA']:
44 extend_submodule(GfxSubmodule, name, c_gfx.__getattribute__('GP_' + name))
46 @extend(GfxSubmodule)
47 def Fill(self, color):
48 self.ctx.Fill(color)
50 def flatten_coords(points):
51 "Helper for Polygon and FillPolygon coordinates"
52 l = []
53 for p in points:
54 if hasattr(p, '__iter__'):
55 l.extend(p)
56 else:
57 l.append(p)
58 for i in l:
59 assert isinstance(i, int)
60 return tuple(l)
62 @extend(GfxSubmodule)
63 def Polygon(self, points, pixel):
64 """
65 Polygon(pixmap, coordinates, pixel)
67 Draw a polygon with color `pixel`.
68 `coordinates` is either an iterable of `int` coordinates `(x0, y0, x1, y1, ...)`
69 or an iterable of tuples `[(x0, y0), (x1, y1), ...]`.
70 """
71 c_gfx.GP_Polygon_wrap(self.ctx, flatten_coords(points), pixel)
73 @extend(GfxSubmodule)
74 def FillPolygon(self, points, pixel):
75 """
76 FillPolygon(pixmap, coordinates, pixel)
78 Draw a filled polygon with color `pixel`.
79 `coordinates` is either an iterable of `int` coordinates `(x0, y0, x1, y1, ...)`
80 or an iterable of tuples `[(x0, y0), (x1, y1), ...]`.
81 """
82 c_gfx.GP_FillPolygon_wrap(self.ctx, flatten_coords(points), pixel)
84 _init(locals())
85 del _init