src/font.c

Sun, 11 Aug 2024 16:29:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 11 Aug 2024 16:29:09 +0200
changeset 70
81a089a47eff
parent 67
0b96fe6d6b5e
permissions
-rw-r--r--

redesign FPS counter

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
66
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
32 #include <assert.h>
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
33 #include <cx/array_list.h>
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
34
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
35 void asc_font(enum AscFontStyle style, int size) {
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
36 asc_context.active_font.style = style;
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
37 asc_context.active_font.size = size;
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
38 }
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
39
11
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 static char const *asc_font_filename(enum AscFontStyle style) {
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 // TODO: do not assume we are running from the program dir
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 switch (style) {
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 case ASC_FONT_REGULAR:
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 return "fonts/OpenSans-Regular.ttf";
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 case ASC_FONT_BOLD:
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 return "fonts/OpenSans-Bold.ttf";
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 case ASC_FONT_ITALIC:
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 return "fonts/OpenSans-Italic.ttf";
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 case ASC_FONT_BOLD_ITALIC:
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 return "fonts/OpenSans-BoldItalic.ttf";
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 }
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
66
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
54 struct asc_font_cache_entry {
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
55 AscFont font;
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
56 TTF_Font *ttf;
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
57 };
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
58
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
59 static CxList *asc_font_cache;
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
60
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
61 static void asc_font_unload(struct asc_font_cache_entry *entry) {
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
62 TTF_CloseFont(entry->ttf);
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
63 asc_dprintf("Closed font size %u, style %u", entry->font.size, entry->font.style);
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
64 }
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
65
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
66 void asc_font_cache_init(void) {
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
67 assert(asc_font_cache == NULL);
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
68 asc_font_cache = cxArrayListCreateSimple(
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
69 sizeof(struct asc_font_cache_entry), 16
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
70 );
67
0b96fe6d6b5e update to recent snapshot of UCX 3.1
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
71 cxDefineDestructor(asc_font_cache, asc_font_unload);
66
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
72 }
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
73
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
74 void asc_font_cache_destroy(void) {
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
75 assert(asc_font_cache != NULL);
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
76 cxListDestroy(asc_font_cache);
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
77 }
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
78
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
79
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
80 TTF_Font *asc_font_load(AscFont font) {
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
81 CxIterator iter = cxListIterator(asc_font_cache);
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
82 cx_foreach(struct asc_font_cache_entry*, cache, iter) {
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
83 if (cache->font.style == font.style && cache->font.size == font.size) {
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
84 return cache->ttf;
11
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 }
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 }
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87
66
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
88 struct asc_font_cache_entry entry;
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
89 entry.font = font;
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
90 entry.ttf = TTF_OpenFont(asc_font_filename(font.style), font.size);
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
91 if (entry.ttf == NULL) {
11
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 asc_error(TTF_GetError());
66
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
93 return NULL;
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 48
diff changeset
94 } else {
66
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
95 cxListAdd(asc_font_cache, &entry);
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
96 asc_dprintf("Loaded font size %d, style %d from disk", font.size, font.style);
8297afa1c29c replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
97 return entry.ttf;
11
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 }
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 }

mercurial