Thu, 18 Apr 2024 22:53:55 +0200
consistently refer to windows by ID - fixes #381
This change discovered that the font cache is completely broken. We created issue #387 for this.
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * Copyright 2023 Mike Becker. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "ascension/ui/font.h"
29 #include "ascension/context.h"
30 #include "ascension/error.h"
32 static char const *asc_font_filename(enum AscFontStyle style) {
33 // TODO: do not assume we are running from the program dir
34 switch (style) {
35 case ASC_FONT_REGULAR:
36 return "fonts/OpenSans-Regular.ttf";
37 case ASC_FONT_BOLD:
38 return "fonts/OpenSans-Bold.ttf";
39 case ASC_FONT_ITALIC:
40 return "fonts/OpenSans-Italic.ttf";
41 case ASC_FONT_BOLD_ITALIC:
42 return "fonts/OpenSans-BoldItalic.ttf";
43 }
44 }
46 void asc_font(enum AscFontStyle style, int size) {
47 for (unsigned int i = 0 ; i < asc_context.fonts_loaded ; i++) {
48 AscFont *font = &asc_context.fonts[i];
49 if (font->size == size && font->style == style) {
50 asc_context.active_font = i;
51 return;
52 }
53 }
55 if (asc_context.fonts_loaded == ASC_MAX_FONTS) {
56 asc_error("Too many fonts. Cannot load more until cache is repaired.");
57 asc_context.active_font = ASC_MAX_FONTS;
58 return;
59 }
61 unsigned int slot = asc_context.fonts_loaded++;
62 AscFont *font = &asc_context.fonts[slot];
63 font->size = size;
64 font->style = style;
65 font->ptr = TTF_OpenFont(asc_font_filename(style), size);
66 if (font->ptr == NULL) {
67 asc_context.fonts_loaded--;
68 asc_error(TTF_GetError());
69 } else {
70 asc_dprintf("Loaded font size %u, style %u in slot %u", size, style, slot);
71 asc_context.active_font = slot;
72 }
73 }