29#include <Objectively/Hash.h>
30#include <Objectively/Array.h>
31#include <Objectively/String.h>
38#include "../Assets/coda.ttf.h"
63 TTF_CloseFont(this->font);
71static int hash(
const Object *self) {
76 hash = HashForCString(
hash, this->family);
78 hash = HashForInteger(
hash, this->style);
79 hash = HashForInteger(
hash, this->renderSize);
87static bool isEqual(
const Object *self,
const Object *other) {
89 if (super(Object, self,
isEqual, other)) {
93 if (other && $(other, isKindOfClass,
_Font())) {
98 if (!strcmp(this->family, that->
family) &&
100 this->style == that->
style &&
119 $(
_cache, setObjectForKeyPath, data, family);
128 if (family == NULL) {
138 const Array *fonts = (Array *)
_fonts;
139 for (
size_t i = 0; i < fonts->count; i++) {
141 Font *font = $(fonts, objectAtIndex, i);
143 if (!strcmp(font->
family, family) &&
145 font->
style == style) {
150 Data *data = $((Dictionary *)
_cache, objectForKeyPath, family);
155 $(
_fonts, addObject, font);
170 $(
_cache, removeAllObjects);
181 Data *data = $(alloc(Data), initWithConstMemory, coda_ttf, coda_ttf_len - 1);
198 self = (
Font *) super(Object, self,
init);
201 self->
data = retain(data);
204 self->
family = strdup(family);
225 SDL_Surface *surface;
227 surface = TTF_RenderText_Blended_Wrapped(self->
font, chars, 0, color, wrapWidth * self->
scale);
229 surface = TTF_RenderText_Blended(self->
font, chars, 0, color);
232 SDL_Surface *converted = NULL;
234 converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32);
235 SDL_DestroySurface(surface);
249 const int renderSize = self->
size * self->
scale;
255 TTF_CloseFont(self->
font);
258 SDL_IOStream *buffer = SDL_IOFromConstMem(self->
data->bytes, (
int) self->
data->length);
264 TTF_SetFontStyle(self->
font, self->
style);
265 TTF_SetFontHinting(self->
font, TTF_HINTING_LIGHT_SUBPIXEL);
283 char *lines = strdup(chars);
285 const int font_h = TTF_GetFontHeight(self->
font);
287 for (
char *line = strtok(lines,
"\n\r"); line; line = strtok(NULL,
"\n\r")) {
291 TTF_GetStringSize(self->
font, line, 0, &line_w, NULL);
292 *w = max(*w, line_w);
301 *w = ceilf(*w / self->
scale);
304 *h = ceilf(*h / self->
scale);
309#pragma mark - Class lifecycle
316 ((ObjectInterface *) clazz->interface)->dealloc =
dealloc;
317 ((ObjectInterface *) clazz->interface)->hash =
hash;
318 ((ObjectInterface *) clazz->interface)->isEqual =
isEqual;
320 ((FontInterface *) clazz->interface)->cachedFont =
cachedFont;
322 ((FontInterface *) clazz->interface)->clearCache =
clearCache;
323 ((FontInterface *) clazz->interface)->defaultFont =
defaultFont;
327 ((FontInterface *) clazz->interface)->sizeCharacters =
sizeCharacters;
329 const bool init = TTF_Init();
333 _cache = $$(Dictionary, dictionary);
336 _fonts = $$(Array, array);
360 clazz = _initialize(&(
const ClassDef) {
362 .superclass = _Object(),
363 .instanceSize =
sizeof(
Font),
364 .interfaceOffset = offsetof(
Font, interface),
365 .interfaceSize =
sizeof(FontInterface),
static View * init(View *self)
static void destroy(Class *clazz)
static void sizeCharacters(const Font *self, const char *chars, int *w, int *h)
static SDL_Surface * renderCharacters(const Font *self, const char *chars, SDL_Color color, int wrapWidth)
static void clearCache(void)
static Font * cachedFont(const char *family, int size, int style)
static bool isEqual(const Object *self, const Object *other)
static void renderDeviceDidReset(Font *self)
static Font * initWithData(Font *self, Data *data, const char *family, int size, int style)
static void dealloc(Object *self)
static Dictionary * _cache
const EnumName FontStyleNames[]
static void initialize(Class *clazz)
static void cacheFont(Data *data, const char *family)
static int hash(const Object *self)
static Font * defaultFont(void)
#define DEFAULT_FONT_STYLE
#define DEFAULT_FONT_FAMILY
#define DEFAULT_FONT_SIZE
static SDL_Size size(const Image *self)
View logging facilities via SDL_Log.
#define MVC_LogWarn(fmt,...)
#define MVC_LogError(fmt,...)
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
void(* cacheFont)(Data *data, const char *family)
Caches the specified font Data.
float scale
The display pixel density scale (e.g. 2.0 on Retina). Updated by callers via the scale field before i...
int renderSize
The render size, adjusted for display density.
char * family
The family name.
void renderCharacters(const Font *self, const char *chars, SDL_Color color, int wrapWidth)
Renders the given characters in this Font.
Font * initWithData(Font *self, Data *data, int size, int index)
TTF_Font * font
The backing font.
Data * data
The raw font data.