1 /* Generic font type that supports fonts with multiple kinds of compression. 2 * Provides an interface for decoding and rendering single characters. 3 */ 4 5 #ifndef _MF_FONT_H_ 6 #define _MF_FONT_H_ 7 8 #include "mf_encoding.h" 9 10 /* Callback function that writes pixels to screen / buffer / whatever. 11 * 12 * x: X coordinate of the first pixel to write. 13 * y: Y coordinate of the first pixel to write. 14 * count: Number of pixels to fill (horizontally). 15 * alpha: The "opaqueness" of the pixels, 0 for background, 255 for text. 16 * state: Free variable that was passed to render_character(). 17 */ 18 typedef void (*mf_pixel_callback_t) (int16_t x, int16_t y, uint8_t count, 19 uint8_t alpha, void *state); 20 21 /* General information about a font. */ 22 struct mf_font_s 23 { 24 /* Full name of the font, comes from the original font file. */ 25 char *full_name; 26 27 /* Short name of the font, comes from file name. */ 28 char *short_name; 29 30 /* Width and height of the character bounding box. */ 31 uint8_t width; 32 uint8_t height; 33 34 /* Minimum and maximum tracking width of characters. */ 35 uint8_t min_x_advance; 36 uint8_t max_x_advance; 37 38 /* Location of the text baseline relative to character. */ 39 int8_t baseline_x; 40 uint8_t baseline_y; 41 42 /* Line height of the font (vertical advance). */ 43 uint8_t line_height; 44 45 /* Flags identifying various aspects of the font. */ 46 uint8_t flags; 47 48 /* Fallback character to use for missing glyphs. */ 49 mf_char fallback_character; 50 51 /* Function to get character width. Should return 0 if character is 52 * not found. */ 53 uint8_t (*character_width)(const struct mf_font_s *font, mf_char character); 54 55 /* Function to render a character. Returns the character width or 0 if 56 * character is not found. */ 57 uint8_t (*render_character)(const struct mf_font_s *font, 58 int16_t x0, int16_t y0, 59 mf_char character, 60 mf_pixel_callback_t callback, 61 void *state); 62 }; 63 64 /* The flag definitions for the font.flags field. */ 65 #define MF_FONT_FLAG_MONOSPACE 0x01 66 #define MF_FONT_FLAG_BW 0x02 67 68 /* Lookup structure for searching fonts by name. */ 69 struct mf_font_list_s 70 { 71 const struct mf_font_list_s *next; 72 const struct mf_font_s *font; 73 }; 74 75 76 /* Function to decode and render a single character. 77 * 78 * font: Pointer to the font definition. 79 * x0, y0: Upper left corner of the target area. 80 * character: The character code (unicode) to render. 81 * callback: Callback function to write out the pixels. 82 * state: Free variable for caller to use (can be NULL). 83 * 84 * Returns width of the character. 85 */ 86 MF_EXTERN uint8_t mf_render_character(const struct mf_font_s *font, 87 int16_t x0, int16_t y0, 88 mf_char character, 89 mf_pixel_callback_t callback, 90 void *state); 91 92 /* Function to get the width of a single character. 93 * This is not necessarily the bounding box of the character 94 * data, but rather the tracking width. 95 * 96 * font: Pointer to the font definition. 97 * character: The character code (unicode) to check width of. 98 * 99 * Returns width of the character in pixels. 100 */ 101 MF_EXTERN uint8_t mf_character_width(const struct mf_font_s *font, 102 mf_char character); 103 104 /* Count the amount of white space at the borders of a character. 105 * 106 * E.g. if the font->width and font->height are 10x20, but the character 107 * is only a thin line at the very left edge, this function will return 108 * (0, 0, 9, 0). If the character is fully whitespace, the function will 109 * return (10, 20, 0, 0). 110 * 111 * font: Pointer to the font definition. 112 * character: The character code (unicode) to check white space of. 113 * left: Number of empty rows at left edge. Can be NULL. 114 * top: Number of empty rows at top edge. Can be NULL. 115 * right: Number of empty rows at right edge. Can be NULL. 116 * bottom: Number of empty rows at bottom edge. Can be NULL. 117 */ 118 MF_EXTERN void mf_character_whitespace(const struct mf_font_s *font, 119 mf_char character, 120 uint8_t *left, uint8_t *top, 121 uint8_t *right, uint8_t *bottom); 122 123 /* Find a font based on name. The name can be either short name or full name. 124 * Note: You can pass MF_INCLUDED_FONTS to search among all the included .h 125 * files. 126 * 127 * name: Font name to search for. 128 * fonts: Pointer to the first font search entry. 129 * 130 * Returns a pointer to the font or NULL if not found. 131 */ 132 MF_EXTERN const struct mf_font_s *mf_find_font(const char *name); 133 134 /* Get the list of included fonts */ 135 MF_EXTERN const struct mf_font_list_s *mf_get_font_list(); 136 137 #endif 138