src/font.c

Mon, 01 Apr 2024 19:01:04 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 01 Apr 2024 19:01:04 +0200
changeset 48
6e5b5ba2752c
parent 11
d83af80eb09b
child 65
9c44c55d327a
permissions
-rw-r--r--

create new UI subdir

universe@11 1 /*
universe@11 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@11 3 * Copyright 2023 Mike Becker. All rights reserved.
universe@11 4 *
universe@11 5 * Redistribution and use in source and binary forms, with or without
universe@11 6 * modification, are permitted provided that the following conditions are met:
universe@11 7 *
universe@11 8 * 1. Redistributions of source code must retain the above copyright
universe@11 9 * notice, this list of conditions and the following disclaimer.
universe@11 10 *
universe@11 11 * 2. Redistributions in binary form must reproduce the above copyright
universe@11 12 * notice, this list of conditions and the following disclaimer in the
universe@11 13 * documentation and/or other materials provided with the distribution.
universe@11 14 *
universe@11 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@11 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@11 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@11 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@11 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@11 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@11 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@11 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@11 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@11 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@11 25 * POSSIBILITY OF SUCH DAMAGE.
universe@11 26 */
universe@11 27
universe@48 28 #include "ascension/ui/font.h"
universe@11 29 #include "ascension/context.h"
universe@11 30 #include "ascension/error.h"
universe@11 31
universe@11 32 static char const *asc_font_filename(enum AscFontStyle style) {
universe@11 33 // TODO: do not assume we are running from the program dir
universe@11 34 switch (style) {
universe@11 35 case ASC_FONT_REGULAR:
universe@11 36 return "fonts/OpenSans-Regular.ttf";
universe@11 37 case ASC_FONT_BOLD:
universe@11 38 return "fonts/OpenSans-Bold.ttf";
universe@11 39 case ASC_FONT_ITALIC:
universe@11 40 return "fonts/OpenSans-Italic.ttf";
universe@11 41 case ASC_FONT_BOLD_ITALIC:
universe@11 42 return "fonts/OpenSans-BoldItalic.ttf";
universe@11 43 }
universe@11 44 }
universe@11 45
universe@11 46 AscFont const *asc_font(enum AscFontStyle style, int size) {
universe@11 47 for (unsigned int i = 0 ; i < asc_context.fonts_loaded ; i++) {
universe@11 48 AscFont *font = &asc_context.fonts[i];
universe@11 49 if (font->size == size && font->style == style) {
universe@11 50 return font;
universe@11 51 }
universe@11 52 }
universe@11 53
universe@11 54 if (asc_context.fonts_loaded == ASC_MAX_FONTS) {
universe@11 55 asc_dprintf("WARNING: Maximum number of fonts reached, wiping cache!");
universe@11 56 asc_font_cache_clear();
universe@11 57 }
universe@11 58
universe@11 59 unsigned int slot = asc_context.fonts_loaded++;
universe@11 60 AscFont *font = &asc_context.fonts[slot];
universe@11 61 font->size = size;
universe@11 62 font->style = style;
universe@11 63 font->ptr = TTF_OpenFont(asc_font_filename(style), size);
universe@11 64 if (font->ptr == NULL) {
universe@11 65 asc_context.fonts_loaded--;
universe@11 66 asc_error(TTF_GetError());
universe@11 67 return NULL;
universe@11 68 }
universe@11 69 asc_dprintf("Loaded font size %u, style %u in slot %u", size, style, slot);
universe@11 70 return font;
universe@11 71 }
universe@11 72
universe@11 73 void asc_font_cache_clear(void) {
universe@11 74 asc_dprintf("Fonts in cache that are being unloaded: %u", asc_context.fonts_loaded);
universe@11 75 while (asc_context.fonts_loaded > 0) {
universe@11 76 unsigned int i = --asc_context.fonts_loaded;
universe@11 77 AscFont *font = &asc_context.fonts[i];
universe@11 78 TTF_CloseFont(font->ptr);
universe@11 79 font->ptr = NULL;
universe@11 80 }
universe@11 81 }
universe@11 82
universe@11 83 AscFont const *asc_font_cache_validate(AscFont const *font) {
universe@11 84 if (font->ptr) {
universe@11 85 return font;
universe@11 86 } else {
universe@11 87 asc_dprintf("Cache miss for font size %u, style %u", font->size, font->style);
universe@11 88 return asc_font(font->style, font->size);
universe@11 89 }
universe@11 90 }

mercurial