|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright (c) 2025 Nicolas Schodet |
| 3 | + |
| 4 | +/** |
| 5 | + * @addtogroup Font Text font. |
| 6 | + * @{ |
| 7 | + */ |
| 8 | + |
| 9 | +#ifndef _PBIO_FONT_H_ |
| 10 | +#define _PBIO_FONT_H_ |
| 11 | + |
| 12 | +#include <pbio/config.h> |
| 13 | + |
| 14 | +#include <stdint.h> |
| 15 | + |
| 16 | +/** |
| 17 | + * Single glyph. |
| 18 | + */ |
| 19 | +typedef struct _pbio_font_glyph_t { |
| 20 | + /** |
| 21 | + * Width of glyph bitmap. |
| 22 | + */ |
| 23 | + uint8_t width; |
| 24 | + /** |
| 25 | + * Height of glyph bitmap. |
| 26 | + */ |
| 27 | + uint8_t height; |
| 28 | + /** |
| 29 | + * Advance to next character in horizontal direction. |
| 30 | + */ |
| 31 | + uint8_t advance; |
| 32 | + /** |
| 33 | + * Horizontal distance between pen position and leftmost bitmap column. |
| 34 | + * Positive values goes left. |
| 35 | + */ |
| 36 | + int8_t left; |
| 37 | + /** |
| 38 | + * Vertical distance between baseline and topmost bitmap row. Positive |
| 39 | + * values goes up. |
| 40 | + */ |
| 41 | + int8_t top; |
| 42 | + /** |
| 43 | + * Index of bitmap start in data table. |
| 44 | + */ |
| 45 | + uint16_t data_index; |
| 46 | + /** |
| 47 | + * Start index of kerning values in kerning table. The Stop index is the |
| 48 | + * start index of the next glyph. |
| 49 | + */ |
| 50 | + uint16_t kerning_index; |
| 51 | +} pbio_font_glyph_t; |
| 52 | + |
| 53 | +/** |
| 54 | + * Kerning information for a glyph. |
| 55 | + */ |
| 56 | +typedef struct _pbio_font_kerning_t { |
| 57 | + /** |
| 58 | + * Previous glyph. |
| 59 | + */ |
| 60 | + uint8_t previous; |
| 61 | + /** |
| 62 | + * Kerning from previous glyph to current one. |
| 63 | + */ |
| 64 | + int8_t kerning; |
| 65 | +} pbio_font_kerning_t; |
| 66 | + |
| 67 | +/** |
| 68 | + * Font. |
| 69 | + * |
| 70 | + * Every bitmap data is composed of one or several rows as indicated in the |
| 71 | + * glyph structure, with the top row first. Eight pixels are packed into |
| 72 | + * a bytes, with the first leftmost pixel in the most significant bit. The |
| 73 | + * bitmap width is padded to the next multiple of eight. |
| 74 | + */ |
| 75 | +typedef struct _pbio_font_t { |
| 76 | + /** |
| 77 | + * First glyph. |
| 78 | + */ |
| 79 | + uint8_t first; |
| 80 | + /** |
| 81 | + * Last glyph. |
| 82 | + */ |
| 83 | + uint8_t last; |
| 84 | + /** |
| 85 | + * Line height, number of pixels between consecutive baselines. |
| 86 | + */ |
| 87 | + uint8_t line_height; |
| 88 | + /** |
| 89 | + * Maximum value of glyph top. |
| 90 | + */ |
| 91 | + int8_t top_max; |
| 92 | + /** |
| 93 | + * Glyph table. Must contain a dummy last glyph for the last kerning stop |
| 94 | + * index. |
| 95 | + */ |
| 96 | + const pbio_font_glyph_t *glyphs; |
| 97 | + /** |
| 98 | + * Data table with bitmaps. |
| 99 | + */ |
| 100 | + const uint8_t *data; |
| 101 | + /** |
| 102 | + * Kerning table, may be NULL if no kerning. |
| 103 | + */ |
| 104 | + const pbio_font_kerning_t *kernings; |
| 105 | +} pbio_font_t; |
| 106 | + |
| 107 | +#if PBIO_CONFIG_IMAGE |
| 108 | + |
| 109 | +extern const pbio_font_t pbio_font_liberationsans_regular_14; |
| 110 | +extern const pbio_font_t pbio_font_terminus_normal_16; |
| 111 | + |
| 112 | +#endif // PBIO_CONFIG_IMAGE |
| 113 | + |
| 114 | +#endif // _PBIO_FONT_H_ |
| 115 | + |
| 116 | +/** @} */ |
0 commit comments