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.

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

mercurial