| 1 | # PySoy's textures.Print class |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2006,2007,2008 PySoy Group |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU Affero General Public License as published |
|---|
| 7 | # by the Free Software Foundation, either version 3 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU Affero General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU Affero General Public License |
|---|
| 16 | # along with this program; if not, see http://www.gnu.org/licenses |
|---|
| 17 | # |
|---|
| 18 | # $Id: Print.pxi 1200 2008-03-22 09:12:44Z ArcRiley $ |
|---|
| 19 | |
|---|
| 20 | cdef class Print (Texture) : |
|---|
| 21 | '''PySoy textures.Print Class |
|---|
| 22 | |
|---|
| 23 | This renders text using cairo. |
|---|
| 24 | ''' |
|---|
| 25 | |
|---|
| 26 | ############################################################################ |
|---|
| 27 | # |
|---|
| 28 | # Python functions |
|---|
| 29 | # |
|---|
| 30 | |
|---|
| 31 | def __cinit__(self, text='', font='serif', |
|---|
| 32 | background=None, foreground=None, |
|---|
| 33 | *args, **keywords) : |
|---|
| 34 | self._formats[3] = gl.GL_BGR |
|---|
| 35 | self._formats[4] = gl.GL_BGRA |
|---|
| 36 | self._resize(1, 4, 512, 256, 1) |
|---|
| 37 | # |
|---|
| 38 | # Get a surface and context |
|---|
| 39 | self._surface = cairo.cairo_image_surface_create_for_data ( |
|---|
| 40 | self._texels + (self._chans * self._width * (self._height-1)), |
|---|
| 41 | cairo.CAIRO_FORMAT_ARGB32, |
|---|
| 42 | self._width, |
|---|
| 43 | self._height, |
|---|
| 44 | self._width*self._chans*-1 ) |
|---|
| 45 | self._context = cairo.cairo_create(self._surface) |
|---|
| 46 | # |
|---|
| 47 | # Set initial data (via properties) |
|---|
| 48 | if background : |
|---|
| 49 | self._background = background |
|---|
| 50 | else : |
|---|
| 51 | self._background = soy.colors.clear |
|---|
| 52 | if foreground : |
|---|
| 53 | self._foreground = foreground |
|---|
| 54 | else : |
|---|
| 55 | self._foreground = soy.colors.white |
|---|
| 56 | self.text = text |
|---|
| 57 | self.font = font |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | ############################################################################ |
|---|
| 61 | # |
|---|
| 62 | # Properties |
|---|
| 63 | # |
|---|
| 64 | |
|---|
| 65 | property background : |
|---|
| 66 | def __get__(self) : |
|---|
| 67 | return self._background |
|---|
| 68 | def __set__(self, soy.colors.Color value) : |
|---|
| 69 | self._background = value |
|---|
| 70 | self._draw() |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | property foreground : |
|---|
| 74 | def __get__(self) : |
|---|
| 75 | return self._foreground |
|---|
| 76 | def __set__(self, soy.colors.Color value) : |
|---|
| 77 | self._foreground = value |
|---|
| 78 | self._draw() |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | property text : |
|---|
| 82 | def __get__(self) : |
|---|
| 83 | return self._text |
|---|
| 84 | def __set__(self, value) : |
|---|
| 85 | if type(value) == str : |
|---|
| 86 | self._text = value |
|---|
| 87 | else : |
|---|
| 88 | raise TypeError('Text must be a string') |
|---|
| 89 | self._draw() |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | property font : |
|---|
| 93 | def __get__(self) : |
|---|
| 94 | return self._font |
|---|
| 95 | def __set__(self, value) : |
|---|
| 96 | if type(value) != str : |
|---|
| 97 | raise TypeError('Text must be a string') |
|---|
| 98 | self._font = value |
|---|
| 99 | self._draw() |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | ############################################################################ |
|---|
| 103 | # |
|---|
| 104 | # C functions |
|---|
| 105 | # |
|---|
| 106 | |
|---|
| 107 | cdef void _draw(self) : |
|---|
| 108 | # |
|---|
| 109 | # _draw is called whenever the colors, font, or text is modified. |
|---|
| 110 | # It uses Cairo to (re)draw the texture based on these parameters |
|---|
| 111 | # to self._texels which the WindowLoop will then update the texture from. |
|---|
| 112 | # |
|---|
| 113 | cdef gl.GLfloat* _bg |
|---|
| 114 | cdef gl.GLfloat* _fg |
|---|
| 115 | # |
|---|
| 116 | # Don't bother rendering if either the font or text don't exist |
|---|
| 117 | if not (self._font and self._text) : |
|---|
| 118 | return |
|---|
| 119 | # |
|---|
| 120 | # Lock against rendering |
|---|
| 121 | py.PyThread_acquire_lock(self._mutex,1) |
|---|
| 122 | # |
|---|
| 123 | # Clear the background |
|---|
| 124 | cairo.cairo_save(self._context) |
|---|
| 125 | _bg = (<soy.colors.Color> self._background)._rgba |
|---|
| 126 | cairo.cairo_set_source_rgba(self._context, _bg[0], _bg[1], _bg[2], _bg[3]) |
|---|
| 127 | cairo.cairo_set_operator(self._context, cairo.CAIRO_OPERATOR_SOURCE) |
|---|
| 128 | cairo.cairo_paint(self._context) |
|---|
| 129 | cairo.cairo_restore(self._context) |
|---|
| 130 | # |
|---|
| 131 | # Now draw |
|---|
| 132 | cairo.cairo_select_font_face(self._context, |
|---|
| 133 | self._font, |
|---|
| 134 | cairo.CAIRO_FONT_SLANT_NORMAL, |
|---|
| 135 | cairo.CAIRO_FONT_WEIGHT_BOLD) |
|---|
| 136 | cairo.cairo_set_font_size(self._context, 32.0) |
|---|
| 137 | _fg = (<soy.colors.Color> self._foreground)._rgba |
|---|
| 138 | cairo.cairo_set_source_rgba(self._context, _fg[0], _fg[1], _fg[2], _fg[3]) |
|---|
| 139 | cairo.cairo_move_to(self._context, 10.0, 50.0) |
|---|
| 140 | cairo.cairo_show_text(self._context, self._text) |
|---|
| 141 | # |
|---|
| 142 | # By setting the _update flag we cause self._texels to be re-sent to GPU |
|---|
| 143 | self._update = 1 |
|---|
| 144 | # |
|---|
| 145 | # Release the lock |
|---|
| 146 | py.PyThread_release_lock(self._mutex) |
|---|