Skip to content

Commit 67cfdad

Browse files
committed
Add new TTF init/quit funcs, FontManager cleanup
1 parent d1c703e commit 67cfdad

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

sdl2/ext/ttf.py

+45-20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import os
22
from .. import surface, pixels
3-
from .common import SDLError
3+
from .common import SDLError, raise_sdl_err
44
from .compat import *
55
from .color import Color, convert_to_color
66
from .draw import prepare_color
7+
from .resources import _validate_path
78

89
_HASSDLTTF = True
910
try:
@@ -14,6 +15,40 @@
1415
__all__ = ["FontManager"]
1516

1617

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+
1752
class FontManager(object):
1853
"""A class for managing and rendering TrueType fonts.
1954
@@ -34,10 +69,7 @@ class FontManager(object):
3469
"""
3570
def __init__(self, font_path, alias=None, size=16,
3671
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()
4173
self.fonts = {} # fonts = {alias: {size:font_ptr}}
4274
self.aliases = {} # aliases = {alias:font_path}
4375
self._textcolor = pixels.SDL_Color(0, 0, 0)
@@ -96,13 +128,11 @@ def _load_font(self, font_path, size, index=0):
96128
97129
Raises an exception if something went wrong.
98130
"""
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)
104134
if not font:
105-
raise SDLError(sdlttf.TTF_GetError())
135+
raise_sdl_err("opening the font '{0}'".format(fname))
106136
return font
107137

108138
def _change_font_size(self, alias, size):
@@ -115,8 +145,8 @@ def _change_font_size(self, alias, size):
115145
@property
116146
def color(self):
117147
""":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)
120150

121151
@color.setter
122152
def color(self, value):
@@ -126,8 +156,8 @@ def color(self, value):
126156
@property
127157
def bg_color(self):
128158
""":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)
131161

132162
@bg_color.setter
133163
def bg_color(self, value):
@@ -147,11 +177,6 @@ def default_font(self):
147177

148178
@default_font.setter
149179
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-
"""
155180
alias = value
156181
size = self.size
157182
if alias not in self.fonts:

0 commit comments

Comments
 (0)