27#include <Objectively/Hash.h>
28#include <Objectively/MutableArray.h>
29#include <Objectively/String.h>
61 TTF_CloseFont(this->font);
69static int hash(
const Object *self) {
74 hash = HashForCString(
hash, this->family);
76 hash = HashForInteger(
hash, this->style);
77 hash = HashForInteger(
hash, this->renderSize);
85static _Bool
isEqual(
const Object *self,
const Object *other) {
87 if (super(Object, self,
isEqual, other)) {
91 if (other && $(other, isKindOfClass,
_Font())) {
96 if (!strcmp(this->family, that->
family) &&
98 this->style == that->
style &&
117 $(
_cache, setObjectForKeyPath, data, family);
126 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 &&
151 Data *data = $((Dictionary *)
_cache, objectForKeyPath, family);
156 $(
_fonts, addObject, font);
171 $(
_cache, removeAllObjects);
182 Data *data = $(alloc(Data), initWithConstMemory, coda_ttf, coda_ttf_len);
199 self = (
Font *) super(Object, self,
init);
202 self->
data = retain(data);
205 self->
family = strdup(family);
225 SDL_Surface *surface;
227 surface = TTF_RenderUTF8_Blended_Wrapped(self->
font, chars, color, wrapWidth *
MVC_WindowScale(NULL, NULL, NULL));
229 surface = TTF_RenderUTF8_Blended(self->
font, chars, color);
232 SDL_Surface *converted = NULL;
234 converted = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32, 0);
235 SDL_FreeSurface(surface);
255 TTF_CloseFont(self->
font);
258 SDL_RWops *buffer = SDL_RWFromConstMem(self->
data->bytes, (
int) self->
data->length);
264 TTF_SetFontStyle(self->
font, self->
style);
282 char *lines = strdup(chars);
284 for (
char *line = strtok(lines,
"\n\r"); line; line = strtok(NULL,
"\n\r")) {
287 TTF_SizeUTF8(self->
font, line, &line_w, &line_h);
290 *w = max(*w, line_w);
300 *w = ceil(*w / scale);
303 *h = ceil(*h / scale);
308#pragma mark - Class lifecycle
315 ((ObjectInterface *) clazz->interface)->dealloc =
dealloc;
316 ((ObjectInterface *) clazz->interface)->hash =
hash;
317 ((ObjectInterface *) clazz->interface)->isEqual =
isEqual;
319 ((FontInterface *) clazz->interface)->cachedFont =
cachedFont;
321 ((FontInterface *) clazz->interface)->clearCache =
clearCache;
322 ((FontInterface *) clazz->interface)->defaultFont =
defaultFont;
326 ((FontInterface *) clazz->interface)->sizeCharacters =
sizeCharacters;
328 const int err = TTF_Init();
331 _cache = $$(MutableDictionary, dictionary);
334 _fonts = $$(MutableArray, array);
358 clazz = _initialize(&(
const ClassDef) {
360 .superclass = _Object(),
361 .instanceSize =
sizeof(
Font),
362 .interfaceOffset = offsetof(
Font, interface),
363 .interfaceSize =
sizeof(FontInterface),
static void destroy(Class *clazz)
static _Bool isEqual(const Object *self, const Object *other)
static void dealloc(Object *self)
const EnumName FontStyleNames[]
static void initialize(Class *clazz)
static MutableDictionary * _cache
static MutableArray * _fonts
static int hash(const Object *self)
#define DEFAULT_FONT_STYLE
#define DEFAULT_FONT_FAMILY
#define DEFAULT_FONT_SIZE
View logging facilities via SDL_Log.
#define MVC_LogWarn(fmt,...)
#define MVC_LogError(fmt,...)
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
double MVC_WindowScale(SDL_Window *window, int *height, int *drawableHeight)
Resolves the scale factor of the specified window for High-DPI support.
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.
void(* cacheFont)(Data *data, const char *family)
Caches the specified font Data.
ident font
The backing font.
int renderSize
The render size, adjusted for display density.
Font * cachedFont(const char *family, int size, int style)
Resolves the cached Font with the given attributes.
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.
char * family
The family name.
void clearCache(void)
Clears the Font cache.
Font * initWithData(Font *self, Data *data, int size, int index)
Class * _Font(void)
The Font archetype.
void sizeCharacters(const Font *self, const char *chars, int *w, int *h)
Data * data
The raw font data.
SDL_Size size(const Image *self)