Wed, 02 Jul 2025 23:21:17 +0200
resolve TODOs regarding input.h
a) mouse position must be integer, because it can be negative (though rarely)
b) we should not trade "access complexity" for space in the scancodes array
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * Copyright 2023 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "ascension/error.h" #include "ascension/context.h" #include "ascension/filesystem.h" #include "ascension/ui/font.h" #include <assert.h> #include <math.h> #include <cx/array_list.h> void asc_font(enum AscFontStyle style, int size) { asc_context.active_font.style = style; asc_context.active_font.size = size; } static const char *asc_font_filename(enum AscFontStyle style) { switch (style) { case ASC_FONT_REGULAR: return "OpenSans-Regular.ttf"; case ASC_FONT_BOLD: return "OpenSans-Bold.ttf"; case ASC_FONT_ITALIC: return "OpenSans-Italic.ttf"; case ASC_FONT_BOLD_ITALIC: return "OpenSans-BoldItalic.ttf"; default: assert(false); return NULL; } } struct asc_font_cache_entry { AscFont font; TTF_Font *ttf; }; static CxList *asc_font_cache; static void asc_font_unload(struct asc_font_cache_entry *entry) { TTF_CloseFont(entry->ttf); asc_dprintf("Closed font size %u, style %u", entry->font.size, entry->font.style); } void asc_font_cache_init(void) { assert(asc_font_cache == NULL); asc_font_cache = cxArrayListCreateSimple( sizeof(struct asc_font_cache_entry), 16 ); cxDefineDestructor(asc_font_cache, asc_font_unload); } void asc_font_cache_destroy(void) { assert(asc_font_cache != NULL); cxListFree(asc_font_cache); } TTF_Font *asc_font_load(AscFont font) { // apply the UI scaling factor first to get the actual font size const float ui_scale = asc_active_window->ui_scale; if (ui_scale != 1.f) { font.size = (int) roundf((float) font.size * ui_scale); } CxIterator iter = cxListIterator(asc_font_cache); cx_foreach(struct asc_font_cache_entry*, cache, iter) { if (cache->font.style == font.style && cache->font.size == font.size) { return cache->ttf; } } struct asc_font_cache_entry entry; entry.font = font; const char *font_name = asc_font_filename(font.style); cxmutstr fpath = asc_filesystem_combine_paths(cx_strcast(asc_context.font_path), cx_str(font_name)); asc_dprintf("Load font from %" CX_PRIstr, CX_SFMT(fpath)); entry.ttf = TTF_OpenFont(fpath.ptr, font.size); cx_strfree(&fpath); if (entry.ttf == NULL) { asc_error("Failed to load font %s: %s", font_name, TTF_GetError()); return NULL; } else { cxListAdd(asc_font_cache, &entry); asc_dprintf("Loaded font size %d, style %d from %s", font.size, font.style, font_name); return entry.ttf; } }