ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Font Struct Reference

TrueType fonts. More...

#include <Font.h>

Inheritance diagram for Font:

Public Member Functions

Class * _Font (void)
 The Font archetype.
 
FontcachedFont (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.
 
FontdefaultFont (void)
 
FontinitWithData (Font *self, Data *data, const char *family, int size, int style)
 Initializes this Font with the given TTF Data and attributes.
 
FontinitWithData (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 Fields

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.
 

Static Public Attributes

void(* cacheFont )(Data *data, const char *family)
 Caches the specified font Data.
 

Protected Attributes

FontInterface * interface
 The interface.
 

Detailed Description

TrueType fonts.

Definition at line 63 of file Font.h.

Member Function Documentation

◆ _Font()

Class * _Font ( void  )

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),
364 .interfaceOffset = offsetof(Font, interface),
365 .interfaceSize = sizeof(FontInterface),
367 .destroy = destroy,
368 });
369 });
370
371 return clazz;
372}
static void initialize(Class *clazz)
Definition Box.c:123
static void destroy(Class *clazz)
Definition Checkbox.c:176
TrueType fonts.
Definition Font.h:63
FontInterface * interface
The interface.
Definition Font.h:74

◆ cachedFont()

Font * cachedFont ( const char *  family,
int  size,
int  style 
)

Resolves the cached Font with the given attributes.

Parameters
familyThe family.
sizeThe size.
styleThe style.
Returns
The cached Font, or the default Font if not found.

Definition at line 126 of file Font.c.

126 {
127
128 if (family == NULL) {
130 }
131 if (size < 1) {
133 }
134 if (style < FontStyleRegular || style > FontStyleStrikeThrough) {
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
143 if (!strcmp(font->family, family) &&
144 font->size == size &&
145 font->style == style) {
146 return font;
147 }
148 }
149
150 Data *data = $((Dictionary *) _cache, objectForKeyPath, family);
151 if (data) {
152 Font *font = $(alloc(Font), initWithData, data, family, size, style);
153 assert(font);
154
155 $(_fonts, addObject, font);
156 release(font);
157
158 return font;
159 }
160
161 MVC_LogWarn("%s-%d-%d not found\n", family, size, style);
162 return $$(Font, defaultFont);
163}
static Dictionary * _cache
Definition Font.c:111
static Array * _fonts
Definition Font.c:112
#define DEFAULT_FONT_STYLE
Definition Font.h:36
@ FontStyleStrikeThrough
Definition Font.h:51
#define DEFAULT_FONT_FAMILY
Definition Font.h:34
#define DEFAULT_FONT_SIZE
Definition Font.h:35
#define MVC_LogWarn(fmt,...)
Definition Log.h:54
char * family
The family name.
Definition Font.h:84
Font * defaultFont(void)
Definition Font.c:177
Font * initWithData(Font *self, Data *data, int size, int index)
int size
The point size.
Definition Font.h:105
TTF_Font * font
The backing font.
Definition Font.h:89
Data * data
The raw font data.
Definition Font.h:79
int style
The style.
Definition Font.h:110

◆ cacheFont()

void cacheFont ( Data *  data,
const char *  family 
)

Definition at line 118 of file Font.c.

118 {
119 $(_cache, setObjectForKeyPath, data, family);
120}

◆ clearCache()

void clearCache ( void  )

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);
182 assert(data);
183
185
186 release(data);
187 });
188
190}
void(* cacheFont)(Data *data, const char *family)
Caches the specified font Data.
Definition Font.h:142
Font * cachedFont(const char *family, int size, int style)
Resolves the cached Font with the given attributes.
Definition Font.c:126

◆ 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
selfThe Font.
dataThe Data.
familyThe family.
sizeThe size.
styleThe 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
201 self->data = retain(data);
202 assert(self->data);
203
204 self->family = strdup(family);
205 assert(self->family);
206
207 self->size = size;
208 assert(self->size);
209
210 self->style = style;
211 self->scale = 1.0f;
212
213 $(self, renderDeviceDidReset);
214 }
215
216 return self;
217}
static View * init(View *self)
Definition Box.c:67
float scale
The display pixel density scale (e.g. 2.0 on Retina). Updated by callers via the scale field before i...
Definition Font.h:100
void renderDeviceDidReset(Font *self)
This method should be invoked when the render context is invalidated. Callers must update self->scale...
Definition Font.c:247

◆ 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
selfThe Font.
charsThe null-terminated UTF-8 encoded C string to render.
colorThe color.
wrapWidthThe 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 {
237 MVC_LogError("%s\n", SDL_GetError());
238 }
239
240 return converted;
241}
#define MVC_LogError(fmt,...)
Definition Log.h:57

◆ 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
selfThe Font.

Definition at line 247 of file Font.c.

247 {
248
249 const int renderSize = self->size * self->scale;
250 if (renderSize != self->renderSize) {
251
252 self->renderSize = renderSize;
253
254 if (self->font) {
255 TTF_CloseFont(self->font);
256 }
257
258 SDL_IOStream *buffer = SDL_IOFromConstMem(self->data->bytes, (int) self->data->length);
259 assert(buffer);
260
261 self->font = TTF_OpenFontIO(buffer, 1, self->renderSize);
262 assert(self->font);
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.
Definition Font.h:94

◆ sizeCharacters()

void sizeCharacters ( const Font self,
const char *  chars,
int *  w,
int *  h 
)

Measures the given characters in this Font.

Parameters
selfThe Font.
charsThe null-terminated UTF-8 encoded C string to size.
wThe width to return.
hThe 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}

Field Documentation

◆ cacheFont

void(* cacheFont) (Data *data, const char *family)
static

Caches the specified font Data.

Parameters
dataThe TTF Data.
familyThe family.

Definition at line 142 of file Font.h.

◆ data

Data* Font::data

The raw font data.

Definition at line 79 of file Font.h.

◆ family

char* Font::family

The family name.

Definition at line 84 of file Font.h.

◆ font

TTF_Font* Font::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

Object Font::object

The superclass.

Definition at line 68 of file Font.h.

◆ renderSize

int Font::renderSize

The render size, adjusted for display density.

Definition at line 94 of file Font.h.

◆ scale

float Font::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

int Font::size

The point size.

Definition at line 105 of file Font.h.

◆ style

int Font::style

The style.

Definition at line 110 of file Font.h.


The documentation for this struct was generated from the following files: