TrueType fonts.
More...
#include <Font.h>
|
| Class * | _Font (void) |
| | The Font archetype.
|
| |
| Font * | cachedFont (const char *family, int size, int style) |
| | Resolves the cached Font with the given attributes.
|
| |
| void | cacheFont (Data *data, const char *family) |
| |
| void | clearCache (void) |
| | Clears the Font cache.
|
| |
| Font * | defaultFont (void) |
| |
| Font * | initWithData (Font *self, Data *data, const char *family, int size, int style) |
| | Initializes this Font with the given TTF Data and attributes.
|
| |
| Font * | initWithData (Font *self, Data *data, int size, int index) |
| |
| void | renderCharacters (const Font *self, const char *chars, SDL_Color color, int wrapWidth) |
| | Renders the given characters in this Font.
|
| |
| void | renderDeviceDidReset (Font *self) |
| | This method should be invoked when the render context is invalidated. Callers must update self->scale before invoking this method.
|
| |
| void | sizeCharacters (const Font *self, const char *chars, int *w, int *h) |
| | Measures the given characters in this Font.
|
| |
|
| Data * | data |
| | The raw font data.
|
| |
| char * | family |
| | The family name.
|
| |
| TTF_Font * | font |
| | The backing font.
|
| |
| Object | object |
| | The superclass.
|
| |
| int | renderSize |
| | The render size, adjusted for display density.
|
| |
| float | scale |
| | The display pixel density scale (e.g. 2.0 on Retina). Updated by callers via the scale field before invoking renderDeviceDidReset.
|
| |
| int | size |
| | The point size.
|
| |
| int | style |
| | The style.
|
| |
TrueType fonts.
Definition at line 63 of file Font.h.
◆ _Font()
The Font archetype.
- Returns
- The Font Class.
Definition at line 365 of file Font.c.
365 {
366 static Class *clazz;
367 static Once once;
368
369 do_once(&once, {
370 clazz = _initialize(&(const ClassDef) {
371 .name = "Font",
372 .superclass = _Object(),
373 .instanceSize =
sizeof(
Font),
375 .interfaceSize = sizeof(FontInterface),
378 });
379 });
380
381 return clazz;
382}
static void initialize(Class *clazz)
static void destroy(Class *clazz)
FontInterface * interface
The interface.
◆ cachedFont()
| Font * cachedFont |
( |
const char * |
family, |
|
|
int |
size, |
|
|
int |
style |
|
) |
| |
Resolves the cached Font with the given attributes.
- Parameters
-
| family | The family. |
| size | The size. |
| style | The style. |
- Returns
- The cached Font, or the default Font if not found.
Definition at line 127 of file Font.c.
127 {
128
131 }
134 }
137 }
138
139 const Array *fonts = (Array *)
_fonts;
140 for (size_t i = 0; i < fonts->count; i++) {
141
142 Font *
font = $(fonts, objectAtIndex, i);
143
148 }
149 }
150
155
158
160 }
161
164}
static Dictionary * _cache
#define DEFAULT_FONT_STYLE
#define DEFAULT_FONT_FAMILY
#define DEFAULT_FONT_SIZE
#define MVC_LogWarn(fmt,...)
char * family
The family name.
Font * initWithData(Font *self, Data *data, int size, int index)
TTF_Font * font
The backing font.
Data * data
The raw font data.
◆ cacheFont()
| void cacheFont |
( |
Data * |
data, |
|
|
const char * |
family |
|
) |
| |
◆ clearCache()
Clears the Font cache.
Definition at line 170 of file Font.c.
170 {
171 $(
_cache, removeAllObjects);
172}
◆ defaultFont()
| Font * defaultFont |
( |
void |
| ) |
|
- Returns
- The default Font.
Definition at line 178 of file Font.c.
178 {
179 static Once once;
180
181 do_once(&once, {
182 Data *
data = $(alloc(Data), initWithConstMemory, coda_ttf, coda_ttf_len - 1);
184
186
188 });
189
191}
void(* cacheFont)(Data *data, const char *family)
Caches the specified font Data.
Font * cachedFont(const char *family, int size, int style)
Resolves the cached Font with the given attributes.
◆ initWithData() [1/2]
| Font * initWithData |
( |
Font * |
self, |
|
|
Data * |
data, |
|
|
const char * |
family, |
|
|
int |
size, |
|
|
int |
style |
|
) |
| |
Initializes this Font with the given TTF Data and attributes.
- Parameters
-
| self | The Font. |
| data | The Data. |
| family | The family. |
| size | The size. |
| style | The style. |
- Returns
- The initialized Font, or
NULL on error.
Definition at line 197 of file Font.c.
197 {
198
199 self = (
Font *) super(Object, self,
init);
200 if (self) {
201
204
207
210
213
215 }
216
217 return self;
218}
static View * init(View *self)
float scale
The display pixel density scale (e.g. 2.0 on Retina). Updated by callers via the scale field before i...
void renderDeviceDidReset(Font *self)
This method should be invoked when the render context is invalidated. Callers must update self->scale...
◆ initWithData() [2/2]
| Font * initWithData |
( |
Font * |
self, |
|
|
Data * |
data, |
|
|
int |
size, |
|
|
int |
index |
|
) |
| |
◆ renderCharacters()
| SDL_Surface * renderCharacters |
( |
const Font * |
self, |
|
|
const char * |
chars, |
|
|
SDL_Color |
color, |
|
|
int |
wrapWidth |
|
) |
| |
Renders the given characters in this Font.
- Parameters
-
| self | The Font. |
| chars | The null-terminated UTF-8 encoded C string to render. |
| color | The color. |
| wrapWidth | The maximum line width, in logical pixels, where wrapping should occur. |
- Returns
- The rendered surface, or
NULL on error.
Definition at line 224 of file Font.c.
224 {
225
226 SDL_Surface *surface;
227 if (wrapWidth) {
228 surface = TTF_RenderText_Blended_Wrapped(self->
font, chars, 0, color, wrapWidth * self->
scale);
229 } else {
230 surface = TTF_RenderText_Blended(self->
font, chars, 0, color);
231 }
232
233 SDL_Surface *converted = NULL;
234 if (surface) {
235 converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32);
236 SDL_DestroySurface(surface);
237 } else {
239 }
240
241 return converted;
242}
#define MVC_LogError(fmt,...)
◆ renderDeviceDidReset()
| void renderDeviceDidReset |
( |
Font * |
self | ) |
|
This method should be invoked when the render context is invalidated. Callers must update self->scale before invoking this method.
- Parameters
-
Definition at line 248 of file Font.c.
248 {
249
252
254
256 TTF_CloseFont(self->
font);
257 }
258
259 SDL_IOStream *buffer = SDL_IOFromConstMem(self->
data->bytes, (
int) self->
data->length);
260 assert(buffer);
261
264
265 TTF_SetFontStyle(self->
font, self->
style);
266 TTF_SetFontHinting(self->
font, TTF_HINTING_LIGHT_SUBPIXEL);
267 }
268}
int renderSize
The render size, adjusted for display density.
◆ sizeCharacters()
| void sizeCharacters |
( |
const Font * |
self, |
|
|
const char * |
chars, |
|
|
int * |
w, |
|
|
int * |
h |
|
) |
| |
Measures the given characters in this Font.
- Parameters
-
| self | The Font. |
| chars | The null-terminated UTF-8 encoded C string to size. |
| w | The width to return. |
| h | The height to return. |
- Returns
- The size of the rendered characters in logical pixels.
Definition at line 274 of file Font.c.
274 {
275
276 if (w) {
277 *w = 0;
278 }
279 if (h) {
280 *h = 0;
281 }
282
283 if (chars) {
284 char *lines = strdup(chars);
285
286 const int font_h = TTF_GetFontHeight(self->
font);
287
288 for (char *line = strtok(lines, "\n\r"); line; line = strtok(NULL, "\n\r")) {
289
290 if (w) {
291
292
293
294 const size_t len = strlen(line) + 2;
295 char buf[len];
296
297 snprintf(buf, len, "%s ", line);
298
299 int line_w;
300
301 TTF_GetStringSize(self->
font, buf, 0, &line_w, NULL);
302 *w = max(*w, line_w);
303 }
304 if (h) {
305 *h += font_h;
306 }
307 }
308 free(lines);
309
310 if (w) {
311 *w = ceilf(*w / self->
scale);
312 }
313 if (h) {
314 *h = ceilf(*h / self->
scale);
315 }
316 }
317}
◆ cacheFont
| void(* cacheFont) (Data *data, const char *family) |
|
static |
Caches the specified font Data.
- Parameters
-
| data | The TTF Data. |
| family | The family. |
Definition at line 142 of file Font.h.
◆ data
The raw font data.
Definition at line 79 of file Font.h.
◆ family
The family name.
Definition at line 84 of file Font.h.
◆ font
The backing font.
Definition at line 89 of file Font.h.
◆ interface
| FontInterface* Font::interface |
|
protected |
The interface.
Definition at line 74 of file Font.h.
◆ object
The superclass.
Definition at line 68 of file Font.h.
◆ renderSize
The render size, adjusted for display density.
Definition at line 94 of file Font.h.
◆ scale
The display pixel density scale (e.g. 2.0 on Retina). Updated by callers via the scale field before invoking renderDeviceDidReset.
Definition at line 100 of file Font.h.
◆ size
The point size.
Definition at line 105 of file Font.h.
◆ style
The style.
Definition at line 110 of file Font.h.
The documentation for this struct was generated from the following files: