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 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),
374 .interfaceOffset = offsetof(Font, interface),
375 .interfaceSize = sizeof(FontInterface),
377 .destroy = destroy,
378 });
379 });
380
381 return clazz;
382}
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 127 of file Font.c.

127 {
128
129 if (family == NULL) {
131 }
132 if (size < 1) {
134 }
135 if (style < FontStyleRegular || style > FontStyleStrikeThrough) {
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
144 if (!strcmp(font->family, family) &&
145 font->size == size &&
146 font->style == style) {
147 return font;
148 }
149 }
150
151 Data *data = $((Dictionary *) _cache, objectForKeyPath, family);
152 if (data) {
153 Font *font = $(alloc(Font), initWithData, data, family, size, style);
154 assert(font);
155
156 $(_fonts, addObject, font);
157 release(font);
158
159 return font;
160 }
161
162 MVC_LogWarn("%s-%d-%d not found\n", family, size, style);
163 return $$(Font, defaultFont);
164}
static Dictionary * _cache
Definition Font.c:112
static Array * _fonts
Definition Font.c:113
#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:178
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 119 of file Font.c.

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

◆ clearCache()

void clearCache ( void  )

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);
183 assert(data);
184
186
187 release(data);
188 });
189
191}
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:127

◆ 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 197 of file Font.c.

197 {
198
199 self = (Font *) super(Object, self, init);
200 if (self) {
201
202 self->data = retain(data);
203 assert(self->data);
204
205 self->family = strdup(family);
206 assert(self->family);
207
208 self->size = size;
209 assert(self->size);
210
211 self->style = style;
212 self->scale = 1.0f;
213
214 $(self, renderDeviceDidReset);
215 }
216
217 return self;
218}
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:248

◆ 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 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 {
238 MVC_LogError("%s\n", SDL_GetError());
239 }
240
241 return converted;
242}
#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 248 of file Font.c.

248 {
249
250 const int renderSize = self->size * self->scale;
251 if (renderSize != self->renderSize) {
252
253 self->renderSize = renderSize;
254
255 if (self->font) {
256 TTF_CloseFont(self->font);
257 }
258
259 SDL_IOStream *buffer = SDL_IOFromConstMem(self->data->bytes, (int) self->data->length);
260 assert(buffer);
261
262 self->font = TTF_OpenFontIO(buffer, 1, self->renderSize);
263 assert(self->font);
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.
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 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 // A little ghetto whitespace padding to ensure TTF doesn't truncate words 🫠
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}

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: