Skip to content

Commit

Permalink
rdpq_font: add builtin fonts for debugging purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
rasky committed Jun 15, 2024
1 parent b3799e0 commit 5c0a638
Show file tree
Hide file tree
Showing 3 changed files with 589 additions and 1 deletion.
45 changes: 45 additions & 0 deletions include/rdpq_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define LIBDRAGON_RDPQ_FONT_H

#include "graphics.h"
#include "debug.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -16,6 +17,8 @@ extern "C" {
///@cond
typedef struct rdpq_font_s rdpq_font_t;
typedef struct rdpq_paragraph_char_s rdpq_paragraph_char_t;
rdpq_font_t* __rdpq_font_load_builtin_0(void);
rdpq_font_t* __rdpq_font_load_builtin_1(void);
///@endcond

/**
Expand Down Expand Up @@ -45,6 +48,48 @@ rdpq_font_t* rdpq_font_load(const char *fn);
*/
rdpq_font_t* rdpq_font_load_buf(void *buf, int sz);

/**
* @brief Builtin fonts, shipped with libdragon
*
* All builtin fonts are licensed under CC0 or similar license that effectively
* place them into public domain, so there are no restrictions on their usage.
*/
typedef enum {
/// ASCII Debug font, outlined, monospace (8x8 pixels, plus outline)
/// Monogram by datagoblin (https://datagoblin.itch.io/monogram)
/// License: CC0
FONT_BUILTIN_DEBUG_MONO = 0,

/// ASCII Debug font, outlined, variable width (7x9 pixels, plus outline)
/// At01 by GrafxKid (https://grafxkid.itch.io/at01)
/// License: CC0
FONT_BUILTIN_DEBUG_VAR = 1,
} rdpq_font_builtin_t;

/**
* @brief Load a builtin font provided by libdragon.
*
* Builtin fonts are simple debug fonts shipped with libdragon itself, to let
* people quickly write something on the screen without much hassle. They are
* meant mainly for debug purposes.
*
* See #rdpq_font_builtin_t for a list of available builtin fonts.
*
* @param font Builtin font to load
*
* @return rdpq_font_t* Loaded font
*/
inline rdpq_font_t* rdpq_font_load_builtin(rdpq_font_builtin_t font) {
switch (font) {
case FONT_BUILTIN_DEBUG_MONO:
return __rdpq_font_load_builtin_0();
case FONT_BUILTIN_DEBUG_VAR:
return __rdpq_font_load_builtin_1();
default:
assertf(false, "Invalid builtin font");
}
}

/**
* @brief Free a font.
*
Expand Down
17 changes: 16 additions & 1 deletion src/rdpq/rdpq_font.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "fmath.h"
#include "utils.h"

// Include the built-in font data
#include "rdpq_font_builtin.c"

#define MAX_STYLES 256

_Static_assert(sizeof(glyph_t) == 16, "glyph_t size is wrong");
Expand Down Expand Up @@ -306,5 +309,17 @@ int rdpq_font_render_paragraph(const rdpq_font_t *fnt, const rdpq_paragraph_char
return ch - chars;
}

// extern inline void rdpq_font_print(rdpq_font_t *fnt, const char *text, const rdpq_parparms_t *parms);
rdpq_font_t *__rdpq_font_load_builtin_0(void)
{
return rdpq_font_load_buf((void*)__fontdb_monogram, __fontdb_monogram_len);
}

rdpq_font_t *__rdpq_font_load_builtin_1(void)
{
return rdpq_font_load_buf((void*)__fontdb_at01, __fontdb_at01_len);
}


extern inline void __rdpq_font_glyph_metrics(const rdpq_font_t *fnt, int16_t index, float *xadvance, int8_t *xoff, int8_t *xoff2, bool *has_kerning, uint8_t *sort_key);
extern inline rdpq_font_t* rdpq_font_load_builtin(rdpq_font_builtin_t font);

Loading

0 comments on commit 5c0a638

Please sign in to comment.