1 /**
2  * @file lv_font.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 
10 #include "lv_font.h"
11 #include "../misc/lv_text_private.h"
12 #include "../misc/lv_utils.h"
13 #include "../misc/lv_log.h"
14 #include "../misc/lv_assert.h"
15 #include "../stdlib/lv_string.h"
16 
17 /*********************
18  *      DEFINES
19  *********************/
20 
21 /**********************
22  *      TYPEDEFS
23  **********************/
24 
25 /**********************
26  *  STATIC PROTOTYPES
27  **********************/
28 
29 /**********************
30  *  STATIC VARIABLES
31  **********************/
32 
33 /**********************
34  * GLOBAL PROTOTYPES
35  **********************/
36 
37 /**********************
38  *      MACROS
39  **********************/
40 
41 /**********************
42  *   GLOBAL FUNCTIONS
43  **********************/
44 
lv_font_get_glyph_bitmap(lv_font_glyph_dsc_t * g_dsc,lv_draw_buf_t * draw_buf)45 const void * lv_font_get_glyph_bitmap(lv_font_glyph_dsc_t * g_dsc, lv_draw_buf_t * draw_buf)
46 {
47     const lv_font_t * font_p = g_dsc->resolved_font;
48     LV_ASSERT_NULL(font_p);
49     return font_p->get_glyph_bitmap(g_dsc, draw_buf);
50 }
51 
lv_font_glyph_release_draw_data(lv_font_glyph_dsc_t * g_dsc)52 void lv_font_glyph_release_draw_data(lv_font_glyph_dsc_t * g_dsc)
53 {
54     const lv_font_t * font = g_dsc->resolved_font;
55 
56     if(font != NULL && font->release_glyph) {
57         font->release_glyph(font, g_dsc);
58     }
59 }
60 
lv_font_get_glyph_dsc(const lv_font_t * font_p,lv_font_glyph_dsc_t * dsc_out,uint32_t letter,uint32_t letter_next)61 bool lv_font_get_glyph_dsc(const lv_font_t * font_p, lv_font_glyph_dsc_t * dsc_out, uint32_t letter,
62                            uint32_t letter_next)
63 {
64 
65     LV_ASSERT_NULL(font_p);
66     LV_ASSERT_NULL(dsc_out);
67 
68 #if LV_USE_FONT_PLACEHOLDER
69     const lv_font_t * placeholder_font = NULL;
70 #endif
71 
72     const lv_font_t * f = font_p;
73 
74     dsc_out->resolved_font = NULL;
75     dsc_out->req_raw_bitmap = 0;
76 
77     while(f) {
78         bool found = f->get_glyph_dsc(f, dsc_out, letter, f->kerning == LV_FONT_KERNING_NONE ? 0 : letter_next);
79         if(found) {
80             if(!dsc_out->is_placeholder) {
81                 dsc_out->resolved_font = f;
82                 return true;
83             }
84 #if LV_USE_FONT_PLACEHOLDER
85             else if(placeholder_font == NULL) {
86                 placeholder_font = f;
87             }
88 #endif
89         }
90         f = f->fallback;
91     }
92 
93 #if LV_USE_FONT_PLACEHOLDER
94     if(placeholder_font != NULL) {
95         placeholder_font->get_glyph_dsc(placeholder_font, dsc_out, letter,
96                                         placeholder_font->kerning == LV_FONT_KERNING_NONE ? 0 : letter_next);
97         dsc_out->resolved_font = placeholder_font;
98         return true;
99     }
100 #endif
101 
102 #if LV_USE_FONT_PLACEHOLDER
103     dsc_out->box_w = font_p->line_height / 2;
104     dsc_out->adv_w = dsc_out->box_w + 2;
105 #else
106     dsc_out->box_w = 0;
107     dsc_out->adv_w = 0;
108 #endif
109 
110     dsc_out->resolved_font = NULL;
111     dsc_out->box_h = font_p->line_height;
112     dsc_out->ofs_x = 0;
113     dsc_out->ofs_y = 0;
114     dsc_out->format = LV_FONT_GLYPH_FORMAT_A1;
115     dsc_out->is_placeholder = true;
116 
117     return false;
118 }
119 
lv_font_get_glyph_width(const lv_font_t * font,uint32_t letter,uint32_t letter_next)120 uint16_t lv_font_get_glyph_width(const lv_font_t * font, uint32_t letter, uint32_t letter_next)
121 {
122     LV_ASSERT_NULL(font);
123     lv_font_glyph_dsc_t g;
124 
125     /*Return zero if letter is marker*/
126     if(lv_text_is_marker(letter)) return 0;
127 
128     lv_font_get_glyph_dsc(font, &g, letter, letter_next);
129     return g.adv_w;
130 }
131 
lv_font_set_kerning(lv_font_t * font,lv_font_kerning_t kerning)132 void lv_font_set_kerning(lv_font_t * font, lv_font_kerning_t kerning)
133 {
134     LV_ASSERT_NULL(font);
135     font->kerning = kerning;
136 }
137 
lv_font_get_line_height(const lv_font_t * font)138 int32_t lv_font_get_line_height(const lv_font_t * font)
139 {
140     return font->line_height;
141 }
142 
lv_font_default(void)143 const lv_font_t * lv_font_default(void)
144 {
145     return LV_FONT_DEFAULT;
146 }
147 
148 /**********************
149  *   STATIC FUNCTIONS
150  **********************/
151