ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Public Member Functions | Data Fields | Static Public Attributes | Protected Attributes
Font Struct Reference

TrueType fonts. More...

#include <Font.h>

Inheritance diagram for Font:

Public Member Functions

Class * _Font (void)
 The Font archetype. More...
 
FontcachedFont (const char *family, int size, int style)
 Resolves the cached Font with the given attributes. More...
 
void cacheFont (Data *data, const char *family)
 
void clearCache (void)
 Clears the Font cache. More...
 
FontdefaultFont (void)
 
FontinitWithData (Font *self, Data *data, const char *family, int size, int style)
 Initializes this Font with the given TTF Data and attributes. More...
 
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. More...
 
void renderDeviceDidReset (Font *self)
 This method should be invoked when the render context is invalidated. More...
 
void sizeCharacters (const Font *self, const char *chars, int *w, int *h)
 

Data Fields

Data * data
 The raw font data. More...
 
char * family
 The family name. More...
 
ident font
 The backing font. More...
 
Object object
 The superclass. More...
 
int renderSize
 The render size, adjusted for display density. More...
 
int size
 The point size. More...
 
int style
 The style. More...
 

Static Public Attributes

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

Protected Attributes

FontInterface * interface
 The interface. More...
 

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

353 {
354 static Class *clazz;
355 static Once once;
356
357 do_once(&once, {
358 clazz = _initialize(&(const ClassDef) {
359 .name = "Font",
360 .superclass = _Object(),
361 .instanceSize = sizeof(Font),
362 .interfaceOffset = offsetof(Font, interface),
363 .interfaceSize = sizeof(FontInterface),
365 .destroy = destroy,
366 });
367 });
368
369 return clazz;
370}
static void destroy(Class *clazz)
Definition: Font.c:341
static void initialize(Class *clazz)
Definition: Font.c:313
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 124 of file Font.c.

124 {
125
126 if (family == NULL) {
128 }
129 if (size < 1) {
131 }
132 if (style < FontStyleRegular || style > FontStyleStrikeThrough) {
134 }
135
136 const int renderSize = size * MVC_WindowScale(NULL, NULL, NULL);
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 font->renderSize == renderSize) {
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 MutableDictionary * _cache
Definition: Font.c:109
static MutableArray * _fonts
Definition: Font.c:110
#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:52
double MVC_WindowScale(SDL_Window *window, int *height, int *drawableHeight)
Resolves the scale factor of the specified window for High-DPI support.
Definition: Window.c:47
ident font
The backing font.
Definition: Font.h:89
int renderSize
The render size, adjusted for display density.
Definition: Font.h:94
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:99
Data * data
The raw font data.
Definition: Font.h:79
int style
The style.
Definition: Font.h:104

◆ cacheFont()

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

Definition at line 116 of file Font.c.

116 {
117 $(_cache, setObjectForKeyPath, data, family);
118}

◆ 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  )

omg *

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);
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:136
Font * cachedFont(const char *family, int size, int style)
Resolves the cached Font with the given attributes.
Definition: Font.c:124

◆ 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
213 $(self, renderDeviceDidReset);
214 }
215
216 return self;
217}
static View * init(View *self)
Definition: Box.c:67
void renderDeviceDidReset(Font *self)
This method should be invoked when the render context is invalidated.
Definition: Font.c:247

◆ initWithData() [2/2]

Font * initWithData ( Font self,
Data *  data,
int  size,
int  index 
)

◆ renderCharacters()

void 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 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_RenderUTF8_Blended_Wrapped(self->font, chars, color, wrapWidth * MVC_WindowScale(NULL, NULL, NULL));
228 } else {
229 surface = TTF_RenderUTF8_Blended(self->font, chars, color);
230 }
231
232 SDL_Surface *converted = NULL;
233 if (surface) {
234 converted = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32, 0);
235 SDL_FreeSurface(surface);
236 } else {
237 MVC_LogError("%s\n", TTF_GetError());
238 }
239
240 return converted;
241}
#define MVC_LogError(fmt,...)
Definition: Log.h:55

◆ renderDeviceDidReset()

void renderDeviceDidReset ( Font self)

This method should be invoked when the render context is invalidated.

Parameters
selfThe Font.

Definition at line 247 of file Font.c.

247 {
248
249 const int renderSize = self->size * MVC_WindowScale(NULL, NULL, NULL);
250 if (renderSize != self->renderSize) {
251
252 self->renderSize = renderSize;
253
254 if (self->font) {
255 TTF_CloseFont(self->font);
256 }
257
258 SDL_RWops *buffer = SDL_RWFromConstMem(self->data->bytes, (int) self->data->length);
259 assert(buffer);
260
261 self->font = TTF_OpenFontRW(buffer, 1, self->renderSize);
262 assert(self->font);
263
264 TTF_SetFontStyle(self->font, self->style);
265 }
266}

◆ sizeCharacters()

void sizeCharacters ( const Font self,
const char *  chars,
int *  w,
int *  h 
)
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 pixels.

Definition at line 272 of file Font.c.

272 {
273
274 if (w) {
275 *w = 0;
276 }
277 if (h) {
278 *h = 0;
279 }
280
281 if (chars) {
282 char *lines = strdup(chars);
283
284 for (char *line = strtok(lines, "\n\r"); line; line = strtok(NULL, "\n\r")) {
285
286 int line_w, line_h;
287 TTF_SizeUTF8(self->font, line, &line_w, &line_h);
288
289 if (w) {
290 *w = max(*w, line_w);
291 }
292 if (h) {
293 *h += line_h;
294 }
295 }
296 free(lines);
297
298 const float scale = MVC_WindowScale(NULL, NULL, NULL);
299 if (w) {
300 *w = ceil(*w / scale);
301 }
302 if (h) {
303 *h = ceil(*h / scale);
304 }
305 }
306}

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 136 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

ident 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.

◆ size

int Font::size

The point size.

Definition at line 99 of file Font.h.

◆ style

int Font::style

The style.

Definition at line 104 of file Font.h.


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