diff --git a/lv_draw/lv_draw_vbasic.c b/lv_draw/lv_draw_vbasic.c index 1f2033267f..4185cef850 100644 --- a/lv_draw/lv_draw_vbasic.c +++ b/lv_draw/lv_draw_vbasic.c @@ -217,13 +217,21 @@ void lv_vletter(const lv_point_t * pos_p, const lv_area_t * mask_p, if(font_p == NULL) return; - uint8_t letter_w = lv_font_get_width(font_p, letter); + + lv_coord_t pos_x = pos_p->x; + lv_coord_t pos_y = pos_p->y; + uint8_t letter_w = lv_font_get_real_width(font_p, letter); uint8_t letter_h = lv_font_get_height(font_p); uint8_t bpp = lv_font_get_bpp(font_p, letter); /*Bit per pixel (1,2, 4 or 8)*/ uint8_t * bpp_opa_table; uint8_t mask_init; uint8_t mask; + if(lv_font_is_monospace(font_p, letter)) { + pos_x += (lv_font_get_width(font_p, letter) - letter_w) / 2; + } + + switch(bpp) { case 1: bpp_opa_table = bpp1_opa_table; @@ -250,8 +258,8 @@ void lv_vletter(const lv_point_t * pos_p, const lv_area_t * mask_p, if(map_p == NULL) return; /*If the letter is completely out of mask don't draw it */ - if(pos_p->x + letter_w < mask_p->x1 || pos_p->x > mask_p->x2 || - pos_p->y + letter_h < mask_p->y1 || pos_p->y > mask_p->y2) return; + if(pos_x + letter_w < mask_p->x1 || pos_x > mask_p->x2 || + pos_y + letter_h < mask_p->y1 || pos_y > mask_p->y2) return; lv_vdb_t * vdb_p = lv_vdb_get(); lv_coord_t vdb_width = lv_area_get_width(&vdb_p->area); @@ -265,14 +273,14 @@ void lv_vletter(const lv_point_t * pos_p, const lv_area_t * mask_p, if((letter_w * bpp) & 0x7) width_byte_bpp++; /* Calculate the col/row start/end on the map*/ - lv_coord_t col_start = pos_p->x >= mask_p->x1 ? 0 : mask_p->x1 - pos_p->x; - lv_coord_t col_end = pos_p->x + letter_w <= mask_p->x2 ? letter_w : mask_p->x2 - pos_p->x + 1; - lv_coord_t row_start = pos_p->y >= mask_p->y1 ? 0 : mask_p->y1 - pos_p->y; - lv_coord_t row_end = pos_p->y + letter_h <= mask_p->y2 ? letter_h : mask_p->y2 - pos_p->y + 1; + lv_coord_t col_start = pos_x >= mask_p->x1 ? 0 : mask_p->x1 - pos_x; + lv_coord_t col_end = pos_x + letter_w <= mask_p->x2 ? letter_w : mask_p->x2 - pos_x + 1; + lv_coord_t row_start = pos_y >= mask_p->y1 ? 0 : mask_p->y1 - pos_y; + lv_coord_t row_end = pos_y + letter_h <= mask_p->y2 ? letter_h : mask_p->y2 - pos_y + 1; /*Set a pointer on VDB to the first pixel of the letter*/ - vdb_buf_tmp += ((pos_p->y - vdb_p->area.y1) * vdb_width) - + pos_p->x - vdb_p->area.x1; + vdb_buf_tmp += ((pos_y - vdb_p->area.y1) * vdb_width) + + pos_x - vdb_p->area.x1; /*If the letter is partially out of mask the move there on VDB*/ vdb_buf_tmp += (row_start * vdb_width) + col_start; diff --git a/lv_misc/lv_font.c b/lv_misc/lv_font.c index 228cfff7ca..ab02484f56 100644 --- a/lv_misc/lv_font.c +++ b/lv_misc/lv_font.c @@ -18,11 +18,6 @@ /********************** * TYPEDEFS **********************/ -typedef struct { - uint32_t glyph_index; - uint32_t unicode; - uint8_t w_px; -} asd_glyph_dsc_t; /********************** * STATIC PROTOTYPES @@ -191,10 +186,34 @@ void lv_font_add(lv_font_t * child, lv_font_t * parent) } +/** + * Tells if font which contains `letter` is monospace or not + * @param font_p point to font + * @param letter an UNICODE character code + * @return true: the letter is monospace; false not monospace + */ +bool lv_font_is_monospace(const lv_font_t * font_p, uint32_t letter) +{ + const lv_font_t * font_i = font_p; + int16_t w; + while(font_i != NULL) { + w = font_i->get_width(font_i, letter); + if(w >= 0) { + /*Glyph found*/ + if(font_i->monospace) return true; + return false; + } + + font_i = font_i->next_page; +} + +return 0; +} + /** * Return with the bitmap of a font. * @param font_p pointer to a font - * @param letter a letter + * @param letter an UNICODE character code * @return pointer to the bitmap of the letter */ const uint8_t * lv_font_get_bitmap(const lv_font_t * font_p, uint32_t letter) @@ -211,12 +230,37 @@ const uint8_t * lv_font_get_bitmap(const lv_font_t * font_p, uint32_t letter) } /** - * Get the width of a letter in a font + * Get the width of a letter in a font. If `monospace` is set then return with it. * @param font_p pointer to a font * @param letter an UNICODE character code * @return the width of a letter */ uint8_t lv_font_get_width(const lv_font_t * font_p, uint32_t letter) +{ + const lv_font_t * font_i = font_p; + int16_t w; + while(font_i != NULL) { + w = font_i->get_width(font_i, letter); + if(w >= 0) { + /*Glyph found*/ + uint8_t m = font_i->monospace; + if(m) w = m; + return w; + } + + font_i = font_i->next_page; + } + + return 0; +} + +/** + * Get the width of the letter without overwriting it with the `monospace` attribute + * @param font_p pointer to a font + * @param letter an UNICODE character code + * @return the width of a letter + */ +uint8_t lv_font_get_real_width(const lv_font_t * font_p, uint32_t letter) { const lv_font_t * font_i = font_p; int16_t w; diff --git a/lv_misc/lv_font.h b/lv_misc/lv_font.h index 8941146152..9287751033 100644 --- a/lv_misc/lv_font.h +++ b/lv_misc/lv_font.h @@ -18,6 +18,7 @@ extern "C" { #include #include +#include #include "lv_fonts/lv_symbol_def.h" @@ -52,7 +53,8 @@ typedef struct _lv_font_struct const uint8_t * (*get_bitmap)(const struct _lv_font_struct *,uint32_t); /*Get a glyph's bitmap from a font*/ int16_t (*get_width)(const struct _lv_font_struct *,uint32_t); /*Get a glyph's with with a given font*/ struct _lv_font_struct * next_page; /*Pointer to a font extension*/ - uint32_t bpp :4; /*Bit per pixel: 1, 2 or 4*/ + uint32_t bpp :4; /*Bit per pixel: 1, 2 or 4*/ + uint32_t monospace :8; /*Fix width (0: normal width)*/ } lv_font_t; /********************** @@ -71,14 +73,31 @@ void lv_font_init(void); */ void lv_font_add(lv_font_t *child, lv_font_t *parent); +/** + * Tells if font which contains `letter` is monospace or not + * @param font_p point to font + * @param letter an UNICODE character code + * @return true: the letter is monospace; false not monospace + */ +bool lv_font_is_monospace(const lv_font_t * font_p, uint32_t letter); + /** * Return with the bitmap of a font. * @param font_p pointer to a font - * @param letter a letter + * @param letter an UNICODE character code * @return pointer to the bitmap of the letter */ const uint8_t * lv_font_get_bitmap(const lv_font_t * font_p, uint32_t letter); +/** + * Get the width of a letter in a font. If `monospace` is set then return with it. + * @param font_p pointer to a font + * @param letter an UNICODE character code + * @return the width of a letter + */ +uint8_t lv_font_get_width(const lv_font_t * font_p, uint32_t letter); + + /** * Get the height of a font * @param font_p pointer to a font @@ -89,14 +108,6 @@ static inline uint8_t lv_font_get_height(const lv_font_t * font_p) return font_p->h_px; } -/** - * Get the width of a letter in a font - * @param font_p pointer to a font - * @param letter a letter - * @return the width of a letter - */ -uint8_t lv_font_get_width(const lv_font_t * font_p, uint32_t letter); - /** * Get the bit-per-pixel of font * @param font pointer to font diff --git a/lv_misc/lv_fonts/lv_font_dejavu_10.c b/lv_misc/lv_fonts/lv_font_dejavu_10.c index 1f9051b958..d67400e6b0 100644 --- a/lv_misc/lv_fonts/lv_font_dejavu_10.c +++ b/lv_misc/lv_fonts/lv_font_dejavu_10.c @@ -5372,6 +5372,7 @@ lv_font_t lv_font_dejavu_10 = { #elif USE_LV_FONT_DEJAVU_10 == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_dejavu_10_cyrillic.c b/lv_misc/lv_fonts/lv_font_dejavu_10_cyrillic.c index a5a8c9d5b9..2e76258f74 100644 --- a/lv_misc/lv_fonts/lv_font_dejavu_10_cyrillic.c +++ b/lv_misc/lv_fonts/lv_font_dejavu_10_cyrillic.c @@ -3636,6 +3636,7 @@ lv_font_t lv_font_dejavu_10_cyrillic = { #elif USE_LV_FONT_DEJAVU_10_CYRILLIC == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_dejavu_10_latin_sup.c b/lv_misc/lv_fonts/lv_font_dejavu_10_latin_sup.c index 4ed0ebd0e6..e57a147e31 100644 --- a/lv_misc/lv_fonts/lv_font_dejavu_10_latin_sup.c +++ b/lv_misc/lv_fonts/lv_font_dejavu_10_latin_sup.c @@ -5428,6 +5428,7 @@ lv_font_t lv_font_dejavu_10_latin_sup = { #elif USE_LV_FONT_DEJAVU_10_LATIN_SUP == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_dejavu_20.c b/lv_misc/lv_fonts/lv_font_dejavu_20.c index 685f8eaa48..8c4028d268 100644 --- a/lv_misc/lv_fonts/lv_font_dejavu_20.c +++ b/lv_misc/lv_fonts/lv_font_dejavu_20.c @@ -9172,6 +9172,7 @@ lv_font_t lv_font_dejavu_20 = { #elif USE_LV_FONT_DEJAVU_20 == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_dejavu_20_cyrillic.c b/lv_misc/lv_fonts/lv_font_dejavu_20_cyrillic.c index 4778a03fef..0006e30e70 100644 --- a/lv_misc/lv_fonts/lv_font_dejavu_20_cyrillic.c +++ b/lv_misc/lv_fonts/lv_font_dejavu_20_cyrillic.c @@ -6196,6 +6196,7 @@ lv_font_t lv_font_dejavu_20_cyrillic = { #elif USE_LV_FONT_DEJAVU_20_CYRILLIC == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_dejavu_20_latin_sup.c b/lv_misc/lv_fonts/lv_font_dejavu_20_latin_sup.c index 7ff4206767..733fadb75f 100644 --- a/lv_misc/lv_fonts/lv_font_dejavu_20_latin_sup.c +++ b/lv_misc/lv_fonts/lv_font_dejavu_20_latin_sup.c @@ -9268,6 +9268,7 @@ lv_font_t lv_font_dejavu_20_latin_sup = { #elif USE_LV_FONT_DEJAVU_20_LATIN_SUP == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_dejavu_30.c b/lv_misc/lv_fonts/lv_font_dejavu_30.c index 9fd459eedf..310110a9fc 100644 --- a/lv_misc/lv_fonts/lv_font_dejavu_30.c +++ b/lv_misc/lv_fonts/lv_font_dejavu_30.c @@ -12972,6 +12972,7 @@ lv_font_t lv_font_dejavu_30 = { #elif USE_LV_FONT_DEJAVU_30 == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_dejavu_30_cyrillic.c b/lv_misc/lv_fonts/lv_font_dejavu_30_cyrillic.c index e4f722abb7..616ac9d844 100644 --- a/lv_misc/lv_fonts/lv_font_dejavu_30_cyrillic.c +++ b/lv_misc/lv_fonts/lv_font_dejavu_30_cyrillic.c @@ -8756,6 +8756,7 @@ lv_font_t lv_font_dejavu_30_cyrillic = { #elif USE_LV_FONT_DEJAVU_30_CYRILLIC == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_dejavu_30_latin_sup.c b/lv_misc/lv_fonts/lv_font_dejavu_30_latin_sup.c index 15a9d8bedb..2e754bcbd4 100644 --- a/lv_misc/lv_fonts/lv_font_dejavu_30_latin_sup.c +++ b/lv_misc/lv_fonts/lv_font_dejavu_30_latin_sup.c @@ -13108,6 +13108,7 @@ lv_font_t lv_font_dejavu_30_latin_sup = { #elif USE_LV_FONT_DEJAVU_30_LATIN_SUP == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_dejavu_40.c b/lv_misc/lv_fonts/lv_font_dejavu_40.c index e5daa5d97e..4c9ce1cb26 100644 --- a/lv_misc/lv_fonts/lv_font_dejavu_40.c +++ b/lv_misc/lv_fonts/lv_font_dejavu_40.c @@ -16772,6 +16772,7 @@ lv_font_t lv_font_dejavu_40 = { #elif USE_LV_FONT_DEJAVU_40 == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_dejavu_40_latin_sup.c b/lv_misc/lv_fonts/lv_font_dejavu_40_latin_sup.c index 5b0fc1fe88..3b0332c8aa 100644 --- a/lv_misc/lv_fonts/lv_font_dejavu_40_latin_sup.c +++ b/lv_misc/lv_fonts/lv_font_dejavu_40_latin_sup.c @@ -16948,6 +16948,7 @@ lv_font_t lv_font_dejavu_40_latin_sup = { #elif USE_LV_FONT_DEJAVU_40_LATIN_SUP == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_symbol_10.c b/lv_misc/lv_fonts/lv_font_symbol_10.c index cf202ba33c..074a95328b 100644 --- a/lv_misc/lv_fonts/lv_font_symbol_10.c +++ b/lv_misc/lv_fonts/lv_font_symbol_10.c @@ -2858,6 +2858,7 @@ lv_font_t lv_font_symbol_10 = { #elif USE_LV_FONT_SYMBOL_10 == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_symbol_20.c b/lv_misc/lv_fonts/lv_font_symbol_20.c index dd073ae47c..700d90815b 100644 --- a/lv_misc/lv_fonts/lv_font_symbol_20.c +++ b/lv_misc/lv_fonts/lv_font_symbol_20.c @@ -4858,6 +4858,7 @@ lv_font_t lv_font_symbol_20 = { #elif USE_LV_FONT_SYMBOL_20 == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_symbol_30.c b/lv_misc/lv_fonts/lv_font_symbol_30.c index 4886a2c12a..7ba222f64e 100644 --- a/lv_misc/lv_fonts/lv_font_symbol_30.c +++ b/lv_misc/lv_fonts/lv_font_symbol_30.c @@ -6852,6 +6852,7 @@ lv_font_t lv_font_symbol_30 = { #elif USE_LV_FONT_SYMBOL_30 == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/lv_misc/lv_fonts/lv_font_symbol_40.c b/lv_misc/lv_fonts/lv_font_symbol_40.c index 8426194ec3..f797672297 100644 --- a/lv_misc/lv_fonts/lv_font_symbol_40.c +++ b/lv_misc/lv_fonts/lv_font_symbol_40.c @@ -8858,6 +8858,7 @@ lv_font_t lv_font_symbol_40 = { #elif USE_LV_FONT_SYMBOL_40 == 8 .bpp = 8, /*Bit per pixel*/ #endif + .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ };