Tue, 28 Oct 2025 22:03:06 +0100
fix that dependencies for tools are always checked
| 11 | 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 | */ | |
| 27 | ||
| 106 
895f92cff6b8
bring back error.h to reduce compile dependencies
 Mike Becker <universe@uap-core.de> parents: 
95diff
changeset | 28 | #include "ascension/error.h" | 
| 11 | 29 | #include "ascension/context.h" | 
| 86 
943bf9d7c6d6
make asset paths configurable
 Mike Becker <universe@uap-core.de> parents: 
83diff
changeset | 30 | #include "ascension/filesystem.h" | 
| 198 
916d2d125ecf
remove ui subdir in includes
 Mike Becker <universe@uap-core.de> parents: 
154diff
changeset | 31 | #include "ascension/font.h" | 
| 11 | 32 | |
| 66 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 33 | #include <assert.h> | 
| 128 | 34 | #include <math.h> | 
| 66 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 35 | #include <cx/array_list.h> | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 36 | |
| 253 | 37 | #include <SDL3_ttf/SDL_ttf.h> | 
| 38 | ||
| 149 
560772519ff9
resolve east-west conflict
 Mike Becker <universe@uap-core.de> parents: 
128diff
changeset | 39 | static const char *asc_font_filename(enum AscFontStyle style) { | 
| 11 | 40 | switch (style) { | 
| 41 | case ASC_FONT_REGULAR: | |
| 86 
943bf9d7c6d6
make asset paths configurable
 Mike Becker <universe@uap-core.de> parents: 
83diff
changeset | 42 | return "OpenSans-Regular.ttf"; | 
| 11 | 43 | case ASC_FONT_BOLD: | 
| 86 
943bf9d7c6d6
make asset paths configurable
 Mike Becker <universe@uap-core.de> parents: 
83diff
changeset | 44 | return "OpenSans-Bold.ttf"; | 
| 11 | 45 | case ASC_FONT_ITALIC: | 
| 86 
943bf9d7c6d6
make asset paths configurable
 Mike Becker <universe@uap-core.de> parents: 
83diff
changeset | 46 | return "OpenSans-Italic.ttf"; | 
| 11 | 47 | case ASC_FONT_BOLD_ITALIC: | 
| 86 
943bf9d7c6d6
make asset paths configurable
 Mike Becker <universe@uap-core.de> parents: 
83diff
changeset | 48 | return "OpenSans-BoldItalic.ttf"; | 
| 83 
f7ce0db6f72b
implement view matrix in sprite shader
 Mike Becker <universe@uap-core.de> parents: 
76diff
changeset | 49 | default: | 
| 
f7ce0db6f72b
implement view matrix in sprite shader
 Mike Becker <universe@uap-core.de> parents: 
76diff
changeset | 50 | assert(false); | 
| 
f7ce0db6f72b
implement view matrix in sprite shader
 Mike Becker <universe@uap-core.de> parents: 
76diff
changeset | 51 | return NULL; | 
| 11 | 52 | } | 
| 53 | } | |
| 54 | ||
| 66 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 55 | struct asc_font_cache_entry { | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 56 | AscFont font; | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 57 | TTF_Font *ttf; | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 58 | }; | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 59 | |
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 60 | static CxList *asc_font_cache; | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 61 | |
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 62 | 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: 
65diff
changeset | 63 | TTF_CloseFont(entry->ttf); | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 64 | 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: 
65diff
changeset | 65 | } | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 66 | |
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 67 | void asc_font_cache_init(void) { | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 68 | assert(asc_font_cache == NULL); | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 69 | asc_font_cache = cxArrayListCreateSimple( | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 70 | sizeof(struct asc_font_cache_entry), 16 | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 71 | ); | 
| 67 
0b96fe6d6b5e
update to recent snapshot of UCX 3.1
 Mike Becker <universe@uap-core.de> parents: 
66diff
changeset | 72 | 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: 
65diff
changeset | 73 | } | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 74 | |
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 75 | void asc_font_cache_destroy(void) { | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 76 | assert(asc_font_cache != NULL); | 
| 76 
eb16be99b0ad
update to newest versions of uwproj and ucx
 Mike Becker <universe@uap-core.de> parents: 
67diff
changeset | 77 | cxListFree(asc_font_cache); | 
| 66 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 78 | } | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 79 | |
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 80 | TTF_Font *asc_font_load(AscFont font) { | 
| 128 | 81 | // apply the UI scaling factor first to get the actual font size | 
| 154 
4dff9cc488fe
add auto-scaling of UI depending on screen resolution - resolves #682
 Mike Becker <universe@uap-core.de> parents: 
149diff
changeset | 82 | const float ui_scale = asc_active_window->ui_scale; | 
| 
4dff9cc488fe
add auto-scaling of UI depending on screen resolution - resolves #682
 Mike Becker <universe@uap-core.de> parents: 
149diff
changeset | 83 | if (ui_scale != 1.f) { | 
| 
4dff9cc488fe
add auto-scaling of UI depending on screen resolution - resolves #682
 Mike Becker <universe@uap-core.de> parents: 
149diff
changeset | 84 | font.size = (int) roundf((float) font.size * ui_scale); | 
| 128 | 85 | } | 
| 86 | ||
| 66 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 87 | CxIterator iter = cxListIterator(asc_font_cache); | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 88 | 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: 
65diff
changeset | 89 | 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: 
65diff
changeset | 90 | return cache->ttf; | 
| 11 | 91 | } | 
| 92 | } | |
| 93 | ||
| 66 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 94 | struct asc_font_cache_entry entry; | 
| 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 95 | entry.font = font; | 
| 92 | 96 | const char *font_name = asc_font_filename(font.style); | 
| 97 | cxmutstr fpath = asc_filesystem_combine_paths(cx_strcast(asc_context.font_path), cx_str(font_name)); | |
| 88 
6234b7ea48f3
add support for 2d textures in sprite shader - fixes #386
 Mike Becker <universe@uap-core.de> parents: 
86diff
changeset | 98 | asc_dprintf("Load font from %" CX_PRIstr, CX_SFMT(fpath)); | 
| 86 
943bf9d7c6d6
make asset paths configurable
 Mike Becker <universe@uap-core.de> parents: 
83diff
changeset | 99 | entry.ttf = TTF_OpenFont(fpath.ptr, font.size); | 
| 
943bf9d7c6d6
make asset paths configurable
 Mike Becker <universe@uap-core.de> parents: 
83diff
changeset | 100 | cx_strfree(&fpath); | 
| 66 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 101 | if (entry.ttf == NULL) { | 
| 253 | 102 | asc_error("Failed to load font %s: %s", font_name, SDL_GetError()); | 
| 66 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 103 | return NULL; | 
| 65 
9c44c55d327a
consistently refer to windows by ID - fixes #381
 Mike Becker <universe@uap-core.de> parents: 
48diff
changeset | 104 | } else { | 
| 66 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 105 | cxListAdd(asc_font_cache, &entry); | 
| 92 | 106 | asc_dprintf("Loaded font size %d, style %d from %s", font.size, font.style, font_name); | 
| 66 
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
 Mike Becker <universe@uap-core.de> parents: 
65diff
changeset | 107 | return entry.ttf; | 
| 11 | 108 | } | 
| 109 | } |