src/font.c

Wed, 08 Nov 2023 21:46:29 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 08 Nov 2023 21:46:29 +0100
changeset 12
d89e0ebc76d2
parent 11
d83af80eb09b
child 48
6e5b5ba2752c
permissions
-rw-r--r--

add projection matrix to AscWindow

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
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 #include "ascension/font.h"
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
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 AscFont const *asc_font(enum AscFontStyle style, int size) {
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) {
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 return font;
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
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 if (asc_context.fonts_loaded == ASC_MAX_FONTS) {
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 asc_dprintf("WARNING: Maximum number of fonts reached, wiping cache!");
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 asc_font_cache_clear();
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 }
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 unsigned int slot = asc_context.fonts_loaded++;
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 AscFont *font = &asc_context.fonts[slot];
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 font->size = size;
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 font->style = style;
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 font->ptr = TTF_OpenFont(asc_font_filename(style), size);
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 if (font->ptr == NULL) {
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 asc_context.fonts_loaded--;
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 asc_error(TTF_GetError());
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 return NULL;
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 }
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 asc_dprintf("Loaded font size %u, style %u in slot %u", size, style, slot);
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 return font;
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 }
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 void asc_font_cache_clear(void) {
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 asc_dprintf("Fonts in cache that are being unloaded: %u", asc_context.fonts_loaded);
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 while (asc_context.fonts_loaded > 0) {
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 unsigned int i = --asc_context.fonts_loaded;
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 AscFont *font = &asc_context.fonts[i];
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 TTF_CloseFont(font->ptr);
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 font->ptr = NULL;
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 }
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 }
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 AscFont const *asc_font_cache_validate(AscFont const *font) {
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 if (font->ptr) {
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 return font;
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 } else {
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 asc_dprintf("Cache miss for font size %u, style %u", font->size, font->style);
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 return asc_font(font->style, font->size);
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 }
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 }

mercurial