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 355 of file Font.c.
355 {
356 static Class *clazz;
357 static Once once;
358
359 do_once(&once, {
360 clazz = _initialize(&(const ClassDef) {
361 .name = "Font",
362 .superclass = _Object(),
363 .instanceSize =
sizeof(
Font),
365 .interfaceSize = sizeof(FontInterface),
368 });
369 });
370
371 return clazz;
372}
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 126 of file Font.c.
126 {
127
130 }
133 }
136 }
137
138 const Array *fonts = (Array *)
_fonts;
139 for (size_t i = 0; i < fonts->count; i++) {
140
141 Font *
font = $(fonts, objectAtIndex, i);
142
147 }
148 }
149
154
157
159 }
160
163}
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 169 of file Font.c.
169 {
170 $(
_cache, removeAllObjects);
171}
◆ defaultFont()
| Font * defaultFont |
( |
void |
| ) |
|
- Returns
- The default Font.
Definition at line 177 of file Font.c.
177 {
178 static Once once;
179
180 do_once(&once, {
181 Data *
data = $(alloc(Data), initWithConstMemory, coda_ttf, coda_ttf_len - 1);
183
185
187 });
188
190}
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 196 of file Font.c.
196 {
197
198 self = (
Font *) super(Object, self,
init);
199 if (self) {
200
203
206
209
212
214 }
215
216 return self;
217}
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 223 of file Font.c.
223 {
224
225 SDL_Surface *surface;
226 if (wrapWidth) {
227 surface = TTF_RenderText_Blended_Wrapped(self->
font, chars, 0, color, wrapWidth * self->
scale);
228 } else {
229 surface = TTF_RenderText_Blended(self->
font, chars, 0, color);
230 }
231
232 SDL_Surface *converted = NULL;
233 if (surface) {
234 converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32);
235 SDL_DestroySurface(surface);
236 } else {
238 }
239
240 return converted;
241}
#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 247 of file Font.c.
247 {
248
251
253
255 TTF_CloseFont(self->
font);
256 }
257
258 SDL_IOStream *buffer = SDL_IOFromConstMem(self->
data->bytes, (
int) self->
data->length);
259 assert(buffer);
260
263
264 TTF_SetFontStyle(self->
font, self->
style);
265 TTF_SetFontHinting(self->
font, TTF_HINTING_LIGHT_SUBPIXEL);
266 }
267}
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 273 of file Font.c.
273 {
274
275 if (w) {
276 *w = 0;
277 }
278 if (h) {
279 *h = 0;
280 }
281
282 if (chars) {
283 char *lines = strdup(chars);
284
285 const int font_h = TTF_GetFontHeight(self->
font);
286
287 for (char *line = strtok(lines, "\n\r"); line; line = strtok(NULL, "\n\r")) {
288
289 if (w) {
290 int line_w;
291 TTF_GetStringSize(self->
font, line, 0, &line_w, NULL);
292 *w = max(*w, line_w);
293 }
294 if (h) {
295 *h += font_h;
296 }
297 }
298 free(lines);
299
300 if (w) {
301 *w = ceilf(*w / self->
scale);
302 }
303 if (h) {
304 *h = ceilf(*h / self->
scale);
305 }
306 }
307}
◆ 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: