src/font.c

Thu, 18 Apr 2024 22:53:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 18 Apr 2024 22:53:55 +0200
changeset 65
9c44c55d327a
parent 48
6e5b5ba2752c
child 66
8297afa1c29c
permissions
-rw-r--r--

consistently refer to windows by ID - fixes #381

This change discovered that the font cache is completely broken. We created issue #387 for this.

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@65 46 void 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@65 50 asc_context.active_font = i;
universe@65 51 return;
universe@11 52 }
universe@11 53 }
universe@11 54
universe@11 55 if (asc_context.fonts_loaded == ASC_MAX_FONTS) {
universe@65 56 asc_error("Too many fonts. Cannot load more until cache is repaired.");
universe@65 57 asc_context.active_font = ASC_MAX_FONTS;
universe@65 58 return;
universe@11 59 }
universe@11 60
universe@11 61 unsigned int slot = asc_context.fonts_loaded++;
universe@11 62 AscFont *font = &asc_context.fonts[slot];
universe@11 63 font->size = size;
universe@11 64 font->style = style;
universe@11 65 font->ptr = TTF_OpenFont(asc_font_filename(style), size);
universe@11 66 if (font->ptr == NULL) {
universe@11 67 asc_context.fonts_loaded--;
universe@11 68 asc_error(TTF_GetError());
universe@65 69 } else {
universe@65 70 asc_dprintf("Loaded font size %u, style %u in slot %u", size, style, slot);
universe@65 71 asc_context.active_font = slot;
universe@11 72 }
universe@11 73 }

mercurial