add font management

Tue, 07 Nov 2023 21:24:06 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 07 Nov 2023 21:24:06 +0100
changeset 11
d83af80eb09b
parent 10
05d329adcecc
child 12
d89e0ebc76d2

add font management

fonts/OFL.txt file | annotate | diff | comparison | revisions
fonts/OpenSans-Bold.ttf file | annotate | diff | comparison | revisions
fonts/OpenSans-BoldItalic.ttf file | annotate | diff | comparison | revisions
fonts/OpenSans-Italic.ttf file | annotate | diff | comparison | revisions
fonts/OpenSans-Regular.ttf file | annotate | diff | comparison | revisions
src/Makefile file | annotate | diff | comparison | revisions
src/ascension/context.h file | annotate | diff | comparison | revisions
src/ascension/font.h file | annotate | diff | comparison | revisions
src/context.c file | annotate | diff | comparison | revisions
src/font.c file | annotate | diff | comparison | revisions
test/Makefile file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fonts/OFL.txt	Tue Nov 07 21:24:06 2023 +0100
@@ -0,0 +1,93 @@
+Copyright 2020 The Open Sans Project Authors (https://github.com/googlefonts/opensans)
+
+This Font Software is licensed under the SIL Open Font License, Version 1.1.
+This license is copied below, and is also available with a FAQ at:
+http://scripts.sil.org/OFL
+
+
+-----------------------------------------------------------
+SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
+-----------------------------------------------------------
+
+PREAMBLE
+The goals of the Open Font License (OFL) are to stimulate worldwide
+development of collaborative font projects, to support the font creation
+efforts of academic and linguistic communities, and to provide a free and
+open framework in which fonts may be shared and improved in partnership
+with others.
+
+The OFL allows the licensed fonts to be used, studied, modified and
+redistributed freely as long as they are not sold by themselves. The
+fonts, including any derivative works, can be bundled, embedded, 
+redistributed and/or sold with any software provided that any reserved
+names are not used by derivative works. The fonts and derivatives,
+however, cannot be released under any other type of license. The
+requirement for fonts to remain under this license does not apply
+to any document created using the fonts or their derivatives.
+
+DEFINITIONS
+"Font Software" refers to the set of files released by the Copyright
+Holder(s) under this license and clearly marked as such. This may
+include source files, build scripts and documentation.
+
+"Reserved Font Name" refers to any names specified as such after the
+copyright statement(s).
+
+"Original Version" refers to the collection of Font Software components as
+distributed by the Copyright Holder(s).
+
+"Modified Version" refers to any derivative made by adding to, deleting,
+or substituting -- in part or in whole -- any of the components of the
+Original Version, by changing formats or by porting the Font Software to a
+new environment.
+
+"Author" refers to any designer, engineer, programmer, technical
+writer or other person who contributed to the Font Software.
+
+PERMISSION & CONDITIONS
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of the Font Software, to use, study, copy, merge, embed, modify,
+redistribute, and sell modified and unmodified copies of the Font
+Software, subject to the following conditions:
+
+1) Neither the Font Software nor any of its individual components,
+in Original or Modified Versions, may be sold by itself.
+
+2) Original or Modified Versions of the Font Software may be bundled,
+redistributed and/or sold with any software, provided that each copy
+contains the above copyright notice and this license. These can be
+included either as stand-alone text files, human-readable headers or
+in the appropriate machine-readable metadata fields within text or
+binary files as long as those fields can be easily viewed by the user.
+
+3) No Modified Version of the Font Software may use the Reserved Font
+Name(s) unless explicit written permission is granted by the corresponding
+Copyright Holder. This restriction only applies to the primary font name as
+presented to the users.
+
+4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
+Software shall not be used to promote, endorse or advertise any
+Modified Version, except to acknowledge the contribution(s) of the
+Copyright Holder(s) and the Author(s) or with their explicit written
+permission.
+
+5) The Font Software, modified or unmodified, in part or in whole,
+must be distributed entirely under this license, and must not be
+distributed under any other license. The requirement for fonts to
+remain under this license does not apply to any document created
+using the Font Software.
+
+TERMINATION
+This license becomes null and void if any of the above conditions are
+not met.
+
+DISCLAIMER
+THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
+OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
+COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
+DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
+OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file fonts/OpenSans-Bold.ttf has changed
Binary file fonts/OpenSans-BoldItalic.ttf has changed
Binary file fonts/OpenSans-Italic.ttf has changed
Binary file fonts/OpenSans-Regular.ttf has changed
--- a/src/Makefile	Tue Nov 07 21:13:04 2023 +0100
+++ b/src/Makefile	Tue Nov 07 21:24:06 2023 +0100
@@ -27,7 +27,7 @@
 
 BUILD_DIR=../build/lib
 
-SRC  = context.c error.c window.c
+SRC  = context.c error.c window.c font.c
 
 OBJ = $(SRC:%.c=$(BUILD_DIR)/%.o)
 
@@ -41,17 +41,26 @@
 FORCE:
 
 $(BUILD_DIR)/context.o: context.c ascension/context.h ascension/window.h \
- ascension/datatypes.h ascension/utils.h
+ ascension/datatypes.h ascension/font.h ascension/error.h \
+ ascension/utils.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
 $(BUILD_DIR)/error.o: error.c ascension/context.h ascension/window.h \
- ascension/datatypes.h ascension/error.h ascension/utils.h
+ ascension/datatypes.h ascension/font.h ascension/error.h \
+ ascension/utils.h
+	@echo "Compiling $<"
+	$(CC) -o $@ $(CFLAGS) -c $<
+
+$(BUILD_DIR)/font.o: font.c ascension/font.h ascension/context.h \
+ ascension/window.h ascension/datatypes.h ascension/font.h \
+ ascension/error.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
 $(BUILD_DIR)/window.o: window.c ascension/window.h ascension/datatypes.h \
- ascension/context.h ascension/window.h ascension/error.h
+ ascension/context.h ascension/window.h ascension/font.h \
+ ascension/error.h ascension/utils.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
--- a/src/ascension/context.h	Tue Nov 07 21:13:04 2023 +0100
+++ b/src/ascension/context.h	Tue Nov 07 21:24:06 2023 +0100
@@ -29,6 +29,7 @@
 #define ASCENSION_CONTEXT_H
 
 #include "window.h"
+#include "font.h"
 
 #include <cx/buffer.h>
 #include <cx/list.h>
@@ -53,6 +54,8 @@
     unsigned int flags;
     CxBuffer error_buffer;
     AscWindow windows[ASC_MAX_WINDOWS];
+    AscFont fonts[ASC_MAX_FONTS];
+    unsigned int fonts_loaded;
 } AscContext;
 
 /** Global ascension context. */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ascension/font.h	Tue Nov 07 21:24:06 2023 +0100
@@ -0,0 +1,102 @@
+/*
+ * 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.
+ */
+
+#ifndef ASCENSION_FONT_H
+#define ASCENSION_FONT_H
+
+#include <SDL2/SDL_ttf.h>
+
+#ifndef ASC_MAX_FONTS
+/** The maximum number of style/size combinations that can be loaded in parallel. */
+#define ASC_MAX_FONTS 64u
+#endif // ASC_MAX_FONTS
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum AscFontStyle {
+    ASC_FONT_REGULAR,
+    ASC_FONT_BOLD,
+    ASC_FONT_ITALIC,
+    ASC_FONT_BOLD_ITALIC,
+};
+
+typedef struct AscFont {
+    /**
+     * Style of the font.
+     */
+    enum AscFontStyle style;
+    /**
+     * Point size.
+     */
+    int size;
+    /**
+     * Pointer to the SDL TTF font structure.
+     */
+    TTF_Font *ptr;
+} AscFont;
+
+/**
+ * Loads a font with the given style and size.
+ *
+ * The font is cached and returned faster the next time you call this
+ * function with the same arguments. However, when you reach the maximum
+ * number of fonts, the cache is completely cleared and rebuilt.
+ *
+ * That means in general, that you should not be using too many fonts of
+ * different sizes at the same time, and you should not keep the pointer
+ * returned by this function too long, because you would risking cache misses.
+ *
+ * @param style the style
+ * @param size the point size
+ * @return a pointer to the font structure (do not free)
+ */
+AscFont const *asc_font(enum AscFontStyle style, int size);
+
+/**
+ * Unloads all cached fonts from the context.
+ */
+void asc_font_cache_clear(void);
+
+/**
+ * Checks, if the font is still loaded and reloads it, if required.
+ *
+ * There is no need to call this function manually. Every Ascension function
+ * that you pass a font will do that for you and use the returned pointer in
+ * case of a cache miss.
+ *
+ * @param font the font to validate
+ * @return \p font, or a pointer to the new location of the loaded font
+ */
+AscFont const *asc_font_cache_validate(AscFont const *font);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#endif //ASCENSION_FONT_H
--- a/src/context.c	Tue Nov 07 21:13:04 2023 +0100
+++ b/src/context.c	Tue Nov 07 21:24:06 2023 +0100
@@ -39,7 +39,7 @@
 void asc_context_initialize(void) {
     if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED))
         return;
-    asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR);
+    memset(&asc_context, 0, sizeof(AscContext));
 
     // initialize error buffer
     cxBufferInit(
@@ -50,9 +50,6 @@
             CX_BUFFER_AUTO_EXTEND
     );
 
-    // initialize data
-    memset(asc_context.windows, 0, sizeof (asc_context.windows));
-
     // initialize SDL
     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
         asc_error(SDL_GetError());
@@ -71,6 +68,7 @@
     for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
         asc_window_destroy(&asc_context.windows[i]);
     }
+    asc_font_cache_clear();
 
     // quit SDL
     if (TTF_WasInit())
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/font.c	Tue Nov 07 21:24:06 2023 +0100
@@ -0,0 +1,90 @@
+/*
+ * 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/font.h"
+#include "ascension/context.h"
+#include "ascension/error.h"
+
+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";
+    }
+}
+
+AscFont const *asc_font(enum AscFontStyle style, int size) {
+    for (unsigned int i = 0 ; i < asc_context.fonts_loaded ; i++) {
+        AscFont *font = &asc_context.fonts[i];
+        if (font->size == size && font->style == style) {
+            return font;
+        }
+    }
+
+    if (asc_context.fonts_loaded == ASC_MAX_FONTS) {
+        asc_dprintf("WARNING: Maximum number of fonts reached, wiping cache!");
+        asc_font_cache_clear();
+    }
+
+    unsigned int slot = asc_context.fonts_loaded++;
+    AscFont *font = &asc_context.fonts[slot];
+    font->size = size;
+    font->style = style;
+    font->ptr = TTF_OpenFont(asc_font_filename(style), size);
+    if (font->ptr == NULL) {
+        asc_context.fonts_loaded--;
+        asc_error(TTF_GetError());
+        return NULL;
+    }
+    asc_dprintf("Loaded font size %u, style %u in slot %u", size, style, slot);
+    return font;
+}
+
+void asc_font_cache_clear(void) {
+    asc_dprintf("Fonts in cache that are being unloaded: %u", asc_context.fonts_loaded);
+    while (asc_context.fonts_loaded > 0) {
+        unsigned int i = --asc_context.fonts_loaded;
+        AscFont *font = &asc_context.fonts[i];
+        TTF_CloseFont(font->ptr);
+        font->ptr = NULL;
+    }
+}
+
+AscFont const *asc_font_cache_validate(AscFont const *font) {
+    if (font->ptr) {
+        return font;
+    } else {
+        asc_dprintf("Cache miss for font size %u, style %u", font->size, font->style);
+        return asc_font(font->style, font->size);
+    }
+}
--- a/test/Makefile	Tue Nov 07 21:13:04 2023 +0100
+++ b/test/Makefile	Tue Nov 07 21:24:06 2023 +0100
@@ -40,7 +40,8 @@
 
 $(BUILD_DIR)/sandbox.o: sandbox.c ../src/ascension/ascension.h \
  ../src/ascension/context.h ../src/ascension/window.h \
- ../src/ascension/datatypes.h ../src/ascension/error.h
+ ../src/ascension/datatypes.h ../src/ascension/font.h \
+ ../src/ascension/error.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 

mercurial