1
1
import os
2
2
from .. import surface , pixels
3
- from .common import SDLError
3
+ from .common import SDLError , raise_sdl_err
4
4
from .compat import *
5
5
from .color import Color , convert_to_color
6
6
from .draw import prepare_color
7
+ from .resources import _validate_path
7
8
8
9
_HASSDLTTF = True
9
10
try :
14
15
__all__ = ["FontManager" ]
15
16
16
17
18
+ def _ttf_init ():
19
+ if not _HASSDLTTF :
20
+ raise RuntimeError ("SDL_ttf is required, but is not installed." )
21
+
22
+ # Check if TTF already initialized, return immediately if it was
23
+ if sdlttf .TTF_WasInit > 0 :
24
+ return
25
+
26
+ # Handle a weirdness in how TTF_Init and TTF_Quit work: TTF_Init
27
+ # blindly increments TTF_WasInit every time it's called and TTF_Quit
28
+ # blindly decrements it, but TTF_Quit only *actually* quits when
29
+ # TTF_WasInit - 1 == 0. Here, we try to ensure we're starting at 0.
30
+ while sdlttf .TTF_WasInit () < 1 :
31
+ ret = sdlttf .TTF_Init ()
32
+ if ret != 0 :
33
+ raise_sdl_err ("initializing the SDL_ttf library" )
34
+
35
+
36
+ def _ttf_quit ():
37
+ if not _HASSDLTTF :
38
+ raise RuntimeError ("SDL_ttf is required, but is not installed." )
39
+
40
+ # Make sure WasInit is non-negative before trying to quit
41
+ while sdlttf .TTF_WasInit () < 1 :
42
+ ret = sdlttf .TTF_Init ()
43
+ if ret != 0 :
44
+ raise_sdl_err ("initializing the SDL_ttf library" )
45
+
46
+ # Actually quit the library (won't really quit until TTF_WasInit == 0)
47
+ while sdlttf .TTF_WasInit > 0 :
48
+ sdlttf .TTF_Quit ()
49
+
50
+
51
+
17
52
class FontManager (object ):
18
53
"""A class for managing and rendering TrueType fonts.
19
54
@@ -34,10 +69,7 @@ class FontManager(object):
34
69
"""
35
70
def __init__ (self , font_path , alias = None , size = 16 ,
36
71
color = Color (255 , 255 , 255 ), bg_color = Color (0 , 0 , 0 ), index = 0 ):
37
- if not _HASSDLTTF :
38
- raise UnsupportedError ("FontManager requires sdlttf support" )
39
- if sdlttf .TTF_WasInit () == 0 and sdlttf .TTF_Init () != 0 :
40
- raise SDLError ()
72
+ _ttf_init ()
41
73
self .fonts = {} # fonts = {alias: {size:font_ptr}}
42
74
self .aliases = {} # aliases = {alias:font_path}
43
75
self ._textcolor = pixels .SDL_Color (0 , 0 , 0 )
@@ -96,13 +128,11 @@ def _load_font(self, font_path, size, index=0):
96
128
97
129
Raises an exception if something went wrong.
98
130
"""
99
- if index == 0 :
100
- font = sdlttf .TTF_OpenFont (byteify (font_path , "utf-8" ), size )
101
- else :
102
- font = sdlttf .TTF_OpenFontIndex (byteify (font_path , "utf-8" ), size ,
103
- index )
131
+ fullpath , fname = _validate_path (font_path , "a font" )
132
+ fullpath = byteify (fullpath )
133
+ font = sdlttf .TTF_OpenFontIndex (fullpath , size , index )
104
134
if not font :
105
- raise SDLError ( sdlttf . TTF_GetError ( ))
135
+ raise_sdl_err ( "opening the font '{0}'" . format ( fname ))
106
136
return font
107
137
108
138
def _change_font_size (self , alias , size ):
@@ -115,8 +145,8 @@ def _change_font_size(self, alias, size):
115
145
@property
116
146
def color (self ):
117
147
""":obj:`~sdl2.ext.Color`: The color to use for rendering text."""
118
- return Color ( self . _textcolor . r , self ._textcolor . g , self . _textcolor . b ,
119
- self . _textcolor .a )
148
+ c = self ._textcolor
149
+ return Color ( c . r , c . g , c . b , c .a )
120
150
121
151
@color .setter
122
152
def color (self , value ):
@@ -126,8 +156,8 @@ def color(self, value):
126
156
@property
127
157
def bg_color (self ):
128
158
""":obj:`~sdl2.ext.Color`: The background color to use for rendering."""
129
- return Color ( self . _bgcolor . r , self ._bgcolor . g , self . _bgcolor . b ,
130
- self . _bgcolor .a )
159
+ c = self ._bgcolor
160
+ return Color ( c . r , c . g , c . b , c .a )
131
161
132
162
@bg_color .setter
133
163
def bg_color (self , value ):
@@ -147,11 +177,6 @@ def default_font(self):
147
177
148
178
@default_font .setter
149
179
def default_font (self , value ):
150
- """value must be a font alias
151
-
152
- Set the default_font to the given font name alias,
153
- provided it's loaded in the font manager.
154
- """
155
180
alias = value
156
181
size = self .size
157
182
if alias not in self .fonts :
0 commit comments