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

/*
 * 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/ui/font.h"
#include "ascension/context.h"
#include "ascension/error.h"

#include <assert.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 char const *asc_font_filename(enum AscFontStyle style) {
    // TODO: do not assume we are running from the program dir
    switch (style) {
        case ASC_FONT_REGULAR:
            return "fonts/OpenSans-Regular.ttf";
        case ASC_FONT_BOLD:
            return "fonts/OpenSans-Bold.ttf";
        case ASC_FONT_ITALIC:
            return "fonts/OpenSans-Italic.ttf";
        case ASC_FONT_BOLD_ITALIC:
            return "fonts/OpenSans-BoldItalic.ttf";
    }
}

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);
    cxListDestroy(asc_font_cache);
}


TTF_Font *asc_font_load(AscFont font) {
    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;
    entry.ttf = TTF_OpenFont(asc_font_filename(font.style), font.size);
    if (entry.ttf == NULL) {
        asc_error(TTF_GetError());
        return NULL;
    } else {
        cxListAdd(asc_font_cache, &entry);
        asc_dprintf("Loaded font size %d, style %d from disk", font.size, font.style);
        return entry.ttf;
    }
}

mercurial