1 /** 2 * @file lv_font.h 3 * 4 */ 5 6 #ifndef LV_FONT_FMT_TXT_H 7 #define LV_FONT_FMT_TXT_H 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 /********************* 14 * INCLUDES 15 *********************/ 16 #include <stdint.h> 17 #include <stddef.h> 18 #include <stdbool.h> 19 #include "lv_font.h" 20 21 /********************* 22 * DEFINES 23 *********************/ 24 25 /********************** 26 * TYPEDEFS 27 **********************/ 28 29 /** This describes a glyph. */ 30 typedef struct { 31 #if LV_FONT_FMT_TXT_LARGE == 0 32 uint32_t bitmap_index : 20; /**< Start index of the bitmap. A font can be max 1 MB. */ 33 uint32_t adv_w : 12; /**< Draw the next glyph after this width. 8.4 format (real_value * 16 is stored). */ 34 uint8_t box_w; /**< Width of the glyph's bounding box*/ 35 uint8_t box_h; /**< Height of the glyph's bounding box*/ 36 int8_t ofs_x; /**< x offset of the bounding box*/ 37 int8_t ofs_y; /**< y offset of the bounding box. Measured from the top of the line*/ 38 #else 39 uint32_t bitmap_index; /**< Start index of the bitmap. A font can be max 4 GB. */ 40 uint32_t adv_w; /**< Draw the next glyph after this width. 28.4 format (real_value * 16 is stored). */ 41 uint16_t box_w; /**< Width of the glyph's bounding box*/ 42 uint16_t box_h; /**< Height of the glyph's bounding box*/ 43 int16_t ofs_x; /**< x offset of the bounding box*/ 44 int16_t ofs_y; /**< y offset of the bounding box. Measured from the top of the line*/ 45 #endif 46 } lv_font_fmt_txt_glyph_dsc_t; 47 48 49 /** Format of font character map. */ 50 enum { 51 LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY, 52 LV_FONT_FMT_TXT_CMAP_FORMAT0_FULL, 53 LV_FONT_FMT_TXT_CMAP_SPARSE_TINY, 54 LV_FONT_FMT_TXT_CMAP_SPARSE_FULL, 55 }; 56 57 typedef uint8_t lv_font_fmt_txt_cmap_type_t; 58 59 60 /* Map codepoints to a `glyph_dsc`s 61 * Several formats are supported to optimize memory usage 62 * See https://github.com/lvgl/lv_font_conv/blob/master/doc/font_spec.md 63 */ 64 typedef struct { 65 /** First Unicode character for this range */ 66 uint32_t range_start; 67 68 /** Number of Unicode characters related to this range. 69 * Last Unicode character = range_start + range_length - 1*/ 70 uint16_t range_length; 71 72 /** First glyph ID (array index of `glyph_dsc`) for this range */ 73 uint16_t glyph_id_start; 74 75 /* 76 According the specification there are 4 formats: 77 https://github.com/lvgl/lv_font_conv/blob/master/doc/font_spec.md 78 79 For simplicity introduce "relative code point": 80 rcp = codepoint - range_start 81 82 and a search function: 83 search a "value" in an "array" and returns the index of "value". 84 85 Format 0 tiny 86 unicode_list == NULL && glyph_id_ofs_list == NULL 87 glyph_id = glyph_id_start + rcp 88 89 Format 0 full 90 unicode_list == NULL && glyph_id_ofs_list != NULL 91 glyph_id = glyph_id_start + glyph_id_ofs_list[rcp] 92 93 Sparse tiny 94 unicode_list != NULL && glyph_id_ofs_list == NULL 95 glyph_id = glyph_id_start + search(unicode_list, rcp) 96 97 Sparse full 98 unicode_list != NULL && glyph_id_ofs_list != NULL 99 glyph_id = glyph_id_start + glyph_id_ofs_list[search(unicode_list, rcp)] 100 */ 101 102 const uint16_t * unicode_list; 103 104 /** if(type == LV_FONT_FMT_TXT_CMAP_FORMAT0_...) it's `uint8_t *` 105 * if(type == LV_FONT_FMT_TXT_CMAP_SPARSE_...) it's `uint16_t *` 106 */ 107 const void * glyph_id_ofs_list; 108 109 /** Length of `unicode_list` and/or `glyph_id_ofs_list`*/ 110 uint16_t list_length; 111 112 /** Type of this character map*/ 113 lv_font_fmt_txt_cmap_type_t type; 114 } lv_font_fmt_txt_cmap_t; 115 116 /** A simple mapping of kern values from pairs*/ 117 typedef struct { 118 /*To get a kern value of two code points: 119 1. Get the `glyph_id_left` and `glyph_id_right` from `lv_font_fmt_txt_cmap_t 120 2 for(i = 0; i < pair_cnt * 2; i+2) 121 if(gylph_ids[i] == glyph_id_left && 122 gylph_ids[i+1] == glyph_id_right) 123 return values[i / 2]; 124 */ 125 const void * glyph_ids; 126 const int8_t * values; 127 uint32_t pair_cnt : 24; 128 uint32_t glyph_ids_size : 2; /*0: `glyph_ids` is stored as `uint8_t`; 1: as `uint16_t`*/ 129 } lv_font_fmt_txt_kern_pair_t; 130 131 /** More complex but more optimal class based kern value storage*/ 132 typedef struct { 133 /*To get a kern value of two code points: 134 1. Get the `glyph_id_left` and `glyph_id_right` from `lv_font_fmt_txt_cmap_t 135 2 Get the class of the left and right glyphs as `left_class` and `right_class` 136 left_class = left_class_mapping[glyph_id_left]; 137 right_class = right_class_mapping[glyph_id_right]; 138 3. value = class_pair_values[(left_class-1)*right_class_cnt + (right_class-1)] 139 */ 140 141 const int8_t * class_pair_values; /*left_class_num * right_class_num value*/ 142 const uint8_t * left_class_mapping; /*Map the glyph_ids to classes: index -> glyph_id -> class_id*/ 143 const uint8_t * right_class_mapping; /*Map the glyph_ids to classes: index -> glyph_id -> class_id*/ 144 uint8_t left_class_cnt; 145 uint8_t right_class_cnt; 146 } lv_font_fmt_txt_kern_classes_t; 147 148 149 /** Bitmap formats*/ 150 typedef enum { 151 LV_FONT_FMT_TXT_PLAIN = 0, 152 LV_FONT_FMT_TXT_COMPRESSED = 1, 153 LV_FONT_FMT_TXT_COMPRESSED_NO_PREFILTER = 1, 154 } lv_font_fmt_txt_bitmap_format_t; 155 156 157 /*Describe store additional data for fonts */ 158 typedef struct { 159 /*The bitmaps of all glyphs*/ 160 const uint8_t * glyph_bitmap; 161 162 /*Describe the glyphs*/ 163 const lv_font_fmt_txt_glyph_dsc_t * glyph_dsc; 164 165 /* Map the glyphs to Unicode characters. 166 * Array of `lv_font_cmap_fmt_txt_t` variables*/ 167 const lv_font_fmt_txt_cmap_t * cmaps; 168 169 /* Store kerning values. 170 * Can be `lv_font_fmt_txt_kern_pair_t * or `lv_font_kern_classes_fmt_txt_t *` 171 * depending on `kern_classes` 172 */ 173 const void * kern_dsc; 174 175 /*Scale kern values in 12.4 format*/ 176 uint16_t kern_scale; 177 178 /*Number of cmap tables*/ 179 uint16_t cmap_num : 10; 180 181 /*Bit per pixel: 1, 2, 3, 4, 8*/ 182 uint16_t bpp : 4; 183 184 /*Type of `kern_dsc`*/ 185 uint16_t kern_classes : 1; 186 187 /* 188 * storage format of the bitmap 189 * from `lv_font_fmt_txt_bitmap_format_t` 190 */ 191 uint16_t bitmap_format : 2; 192 193 /*Cache the last letter and is glyph id*/ 194 uint32_t last_letter; 195 uint32_t last_glyph_id; 196 197 } lv_font_fmt_txt_dsc_t; 198 199 /********************** 200 * GLOBAL PROTOTYPES 201 **********************/ 202 203 /** 204 * Used as `get_glyph_bitmap` callback in LittelvGL's native font format if the font is uncompressed. 205 * @param font pointer to font 206 * @param unicode_letter an unicode letter which bitmap should be get 207 * @return pointer to the bitmap or NULL if not found 208 */ 209 const uint8_t * lv_font_get_bitmap_fmt_txt(const lv_font_t * font, uint32_t letter); 210 211 /** 212 * Used as `get_glyph_dsc` callback in LittelvGL's native font format if the font is uncompressed. 213 * @param font_p pointer to font 214 * @param dsc_out store the result descriptor here 215 * @param letter an UNICODE letter code 216 * @return true: descriptor is successfully loaded into `dsc_out`. 217 * false: the letter was not found, no data is loaded to `dsc_out` 218 */ 219 bool lv_font_get_glyph_dsc_fmt_txt(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter, 220 uint32_t unicode_letter_next); 221 222 /** 223 * Free the allocated memories. 224 */ 225 void _lv_font_clean_up_fmt_txt(void); 226 227 /********************** 228 * MACROS 229 **********************/ 230 231 /********************** 232 * ADD BUILT IN FONTS 233 **********************/ 234 235 #ifdef __cplusplus 236 } /* extern "C" */ 237 #endif 238 239 #endif /*LV_FONT_FMT_TXT_H*/ 240