Fri, 19 Apr 2024 22:28:29 +0200
replaces broken font cache with improved cache - fixes #387
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 #include <assert.h>
33 #include <cx/array_list.h>
35 void asc_font(enum AscFontStyle style, int size) {
36 asc_context.active_font.style = style;
37 asc_context.active_font.size = size;
38 }
40 static char const *asc_font_filename(enum AscFontStyle style) {
41 // TODO: do not assume we are running from the program dir
42 switch (style) {
43 case ASC_FONT_REGULAR:
44 return "fonts/OpenSans-Regular.ttf";
45 case ASC_FONT_BOLD:
46 return "fonts/OpenSans-Bold.ttf";
47 case ASC_FONT_ITALIC:
48 return "fonts/OpenSans-Italic.ttf";
49 case ASC_FONT_BOLD_ITALIC:
50 return "fonts/OpenSans-BoldItalic.ttf";
51 }
52 }
54 struct asc_font_cache_entry {
55 AscFont font;
56 TTF_Font *ttf;
57 };
59 static CxList *asc_font_cache;
61 static void asc_font_unload(struct asc_font_cache_entry *entry) {
62 TTF_CloseFont(entry->ttf);
63 asc_dprintf("Closed font size %u, style %u", entry->font.size, entry->font.style);
64 }
66 void asc_font_cache_init(void) {
67 assert(asc_font_cache == NULL);
68 asc_font_cache = cxArrayListCreateSimple(
69 sizeof(struct asc_font_cache_entry), 16
70 );
71 asc_font_cache->simple_destructor = (cx_destructor_func) asc_font_unload;
72 }
74 void asc_font_cache_destroy(void) {
75 assert(asc_font_cache != NULL);
76 cxListDestroy(asc_font_cache);
77 }
80 TTF_Font *asc_font_load(AscFont font) {
81 CxIterator iter = cxListIterator(asc_font_cache);
82 cx_foreach(struct asc_font_cache_entry*, cache, iter) {
83 if (cache->font.style == font.style && cache->font.size == font.size) {
84 return cache->ttf;
85 }
86 }
88 struct asc_font_cache_entry entry;
89 entry.font = font;
90 entry.ttf = TTF_OpenFont(asc_font_filename(font.style), font.size);
91 if (entry.ttf == NULL) {
92 asc_error(TTF_GetError());
93 return NULL;
94 } else {
95 cxListAdd(asc_font_cache, &entry);
96 asc_dprintf("Loaded font size %d, style %d from disk", font.size, font.style);
97 return entry.ttf;
98 }
99 }