| 1 |
# Copyright 2014 Patrick Dawson <[email protected]> |
| 2 |
# |
| 3 |
# This software is provided 'as-is', without any express or implied |
| 4 |
# warranty. In no event will the authors be held liable for any damages |
| 5 |
# arising from the use of this software. |
| 6 |
# |
| 7 |
# Permission is granted to anyone to use this software for any purpose, |
| 8 |
# including commercial applications, and to alter it and redistribute it |
| 9 |
# freely, subject to the following restrictions: |
| 10 |
# |
| 11 |
# 1. The origin of this software must not be misrepresented; you must not |
| 12 |
# claim that you wrote the original software. If you use this software |
| 13 |
# in a product, an acknowledgment in the product documentation would be |
| 14 |
# appreciated but is not required. |
| 15 |
# 2. Altered source versions must be plainly marked as such, and must not be |
| 16 |
# misrepresented as being the original software. |
| 17 |
# 3. This notice may not be removed or altered from any source distribution. |
| 18 |
|
| 19 |
from sdl2 cimport * |
| 20 |
from sdl2_ttf cimport * |
| 21 |
from pygame_sdl2.surface cimport * |
| 22 |
from pygame_sdl2.color cimport * |
| 23 |
from pygame_sdl2.rwobject cimport to_rwops |
| 24 |
|
| 25 |
from pygame_sdl2.sysfont import SysFont, match_font, get_fonts |
| 26 |
from pygame_sdl2.error import error |
| 27 |
import pygame_sdl2 |
| 28 |
|
| 29 |
@pygame_sdl2.register_init |
| 30 |
def init(): |
| 31 |
TTF_Init() |
| 32 |
|
| 33 |
@pygame_sdl2.register_quit |
| 34 |
def quit(): # @ReservedAssignment |
| 35 |
TTF_Quit() |
| 36 |
|
| 37 |
def get_init(): |
| 38 |
return TTF_WasInit() != 0 |
| 39 |
|
| 40 |
def get_default_font(): |
| 41 |
import os |
| 42 |
|
| 43 |
default = os.path.join(os.path.dirname(__file__), "DejaVuSans.ttf") |
| 44 |
|
| 45 |
if os.path.exists(default): |
| 46 |
return default |
| 47 |
|
| 48 |
return match_font("sans") |
| 49 |
|
| 50 |
cdef class Font: |
| 51 |
cdef TTF_Font *font |
| 52 |
cdef int style |
| 53 |
|
| 54 |
def __cinit__(self): |
| 55 |
self.font = NULL |
| 56 |
self.style = TTF_STYLE_NORMAL |
| 57 |
|
| 58 |
def __dealloc__(self): |
| 59 |
if self.font: |
| 60 |
TTF_CloseFont(self.font) |
| 61 |
|
| 62 |
def __init__(self, fi, size): |
| 63 |
if fi is None: |
| 64 |
fi = get_default_font() |
| 65 |
self.font = TTF_OpenFontRW(to_rwops(fi), 1, size) |
| 66 |
if self.font == NULL: |
| 67 |
raise error() |
| 68 |
|
| 69 |
def render(self, text, antialias, color, background=None): |
| 70 |
cdef SDL_Surface *surf |
| 71 |
cdef SDL_Color fg |
| 72 |
|
| 73 |
if not text: |
| 74 |
w, h = self.size(" ") |
| 75 |
return Surface((0, h)) |
| 76 |
|
| 77 |
to_sdl_color(color, &fg) |
| 78 |
|
| 79 |
TTF_SetFontStyle(self.font, self.style) |
| 80 |
if antialias: |
| 81 |
surf = TTF_RenderUTF8_Blended(self.font, text.encode('utf-8'), fg) |
| 82 |
else: |
| 83 |
surf = TTF_RenderUTF8_Solid(self.font, text.encode('utf-8'), fg) |
| 84 |
|
| 85 |
if surf == NULL: |
| 86 |
raise error() |
| 87 |
|
| 88 |
cdef Surface rv = Surface(()) |
| 89 |
rv.take_surface(surf) |
| 90 |
|
| 91 |
if rv.surface.format.BitsPerPixel != 32: |
| 92 |
rv = rv.convert() |
| 93 |
|
| 94 |
if background is not None: |
| 95 |
bgsurf = rv.copy() |
| 96 |
bgsurf.fill(background) |
| 97 |
bgsurf.blit(rv, (0,0)) |
| 98 |
return bgsurf |
| 99 |
return rv |
| 100 |
|
| 101 |
def size(self, text): |
| 102 |
cdef int w, h |
| 103 |
if TTF_SizeUTF8(self.font, text.encode('utf-8'), &w, &h) != 0: |
| 104 |
raise error() |
| 105 |
return w, h |
| 106 |
|
| 107 |
cdef set_style(self, flag, on): |
| 108 |
if on: |
| 109 |
self.style |= flag |
| 110 |
else: |
| 111 |
self.style &= ~flag |
| 112 |
|
| 113 |
def set_underline(self, on): |
| 114 |
self.set_style(TTF_STYLE_UNDERLINE, on) |
| 115 |
|
| 116 |
def get_underline(self): |
| 117 |
return self.style & TTF_STYLE_UNDERLINE |
| 118 |
|
| 119 |
def set_bold(self, on): |
| 120 |
self.set_style(TTF_STYLE_BOLD, on) |
| 121 |
|
| 122 |
def get_bold(self): |
| 123 |
return self.style & TTF_STYLE_BOLD |
| 124 |
|
| 125 |
def set_italic(self, on): |
| 126 |
self.set_style(TTF_STYLE_ITALIC, on) |
| 127 |
|
| 128 |
def get_italic(self): |
| 129 |
return self.style & TTF_STYLE_ITALIC |
| 130 |
|
| 131 |
def get_linesize(self): |
| 132 |
return TTF_FontLineSkip(self.font) |
| 133 |
|
| 134 |
def get_height(self): |
| 135 |
return TTF_FontHeight(self.font) |
| 136 |
|
| 137 |
def get_ascent(self): |
| 138 |
return TTF_FontAscent(self.font) |
| 139 |
|
| 140 |
def get_descent(self): |
| 141 |
return TTF_FontDescent(self.font) |
| 142 |
|
| 143 |
def metrics(self, text not None): |
| 144 |
cdef int minx, maxx, miny, maxy, advance |
| 145 |
cdef uint16_t chnum |
| 146 |
rv = [] |
| 147 |
|
| 148 |
for ch in text: |
| 149 |
chnum = ord(ch) |
| 150 |
|
| 151 |
if TTF_GlyphMetrics(self.font, chnum, &minx, &maxx, &miny, &maxy, &advance) == 0: |
| 152 |
rv.append((minx, maxx, miny, maxy, advance)) |
| 153 |
else: |
| 154 |
rv.append(None) |
| 155 |
|
| 156 |
return rv |