--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/font.c Tue Nov 07 21:24:06 2023 +0100 @@ -0,0 +1,90 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2023 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ascension/font.h" +#include "ascension/context.h" +#include "ascension/error.h" + +static char const *asc_font_filename(enum AscFontStyle style) { + // TODO: do not assume we are running from the program dir + switch (style) { + case ASC_FONT_REGULAR: + return "fonts/OpenSans-Regular.ttf"; + case ASC_FONT_BOLD: + return "fonts/OpenSans-Bold.ttf"; + case ASC_FONT_ITALIC: + return "fonts/OpenSans-Italic.ttf"; + case ASC_FONT_BOLD_ITALIC: + return "fonts/OpenSans-BoldItalic.ttf"; + } +} + +AscFont const *asc_font(enum AscFontStyle style, int size) { + for (unsigned int i = 0 ; i < asc_context.fonts_loaded ; i++) { + AscFont *font = &asc_context.fonts[i]; + if (font->size == size && font->style == style) { + return font; + } + } + + if (asc_context.fonts_loaded == ASC_MAX_FONTS) { + asc_dprintf("WARNING: Maximum number of fonts reached, wiping cache!"); + asc_font_cache_clear(); + } + + unsigned int slot = asc_context.fonts_loaded++; + AscFont *font = &asc_context.fonts[slot]; + font->size = size; + font->style = style; + font->ptr = TTF_OpenFont(asc_font_filename(style), size); + if (font->ptr == NULL) { + asc_context.fonts_loaded--; + asc_error(TTF_GetError()); + return NULL; + } + asc_dprintf("Loaded font size %u, style %u in slot %u", size, style, slot); + return font; +} + +void asc_font_cache_clear(void) { + asc_dprintf("Fonts in cache that are being unloaded: %u", asc_context.fonts_loaded); + while (asc_context.fonts_loaded > 0) { + unsigned int i = --asc_context.fonts_loaded; + AscFont *font = &asc_context.fonts[i]; + TTF_CloseFont(font->ptr); + font->ptr = NULL; + } +} + +AscFont const *asc_font_cache_validate(AscFont const *font) { + if (font->ptr) { + return font; + } else { + asc_dprintf("Cache miss for font size %u, style %u", font->size, font->style); + return asc_font(font->style, font->size); + } +}