From 5321b9b62e32f80410ff3d6cd6e31d8437bdb0ce Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Wed, 24 Apr 2019 17:28:38 +0200 Subject: [PATCH] make the basics of the new font system work --- src/lv_draw/lv_draw_basic.c | 58 ++++++++++++++++++-------------- src/lv_fonts/lv_font_dejavu_20.c | 24 +++++++------ src/lv_misc/lv_font.c | 52 ++++++++++++++++------------ src/lv_misc/lv_font.h | 40 +++++++++++++++------- src/lv_objx/lv_tabview.c | 2 +- 5 files changed, 105 insertions(+), 71 deletions(-) diff --git a/src/lv_draw/lv_draw_basic.c b/src/lv_draw/lv_draw_basic.c index 6cfc222a5..5a911d2a6 100644 --- a/src/lv_draw/lv_draw_basic.c +++ b/src/lv_draw/lv_draw_basic.c @@ -227,11 +227,15 @@ void lv_draw_fill(const lv_area_t * cords_p, const lv_area_t * mask_p, lv_color_ void lv_draw_letter(const lv_point_t * pos_p, const lv_area_t * mask_p, const lv_font_t * font_p, uint32_t letter, lv_color_t color, lv_opa_t opa) { - const uint8_t bpp1_opa_table[2] = { - 0, 255}; /*Opacity mapping with bpp = 1 (Just for compatibility)*/ + /*clang-format off*/ + const uint8_t bpp1_opa_table[2] = { 0, 255}; /*Opacity mapping with bpp = 1 (Just for compatibility)*/ const uint8_t bpp2_opa_table[4] = {0, 85, 170, 255}; /*Opacity mapping with bpp = 2*/ const uint8_t bpp4_opa_table[16] = {0, 17, 34, 51, /*Opacity mapping with bpp = 4*/ - 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255}; + 68, 85, 102, 119, + 136, 153, 170, 187, + 204, 221, 238, 255}; + /*clang-format on*/ + if(opa < LV_OPA_MIN) return; if(opa > LV_OPA_MAX) opa = LV_OPA_COVER; @@ -240,35 +244,38 @@ void lv_draw_letter(const lv_point_t * pos_p, const lv_area_t * mask_p, const lv return; } - 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_line_height(font_p); + const lv_font_glyph_dsc_t * g = lv_font_get_glyph_dsc(font_p, letter); + + lv_coord_t pos_x = pos_p->x + g->ofs_x; + lv_coord_t pos_y = pos_p->y + g->ofs_y; + uint8_t bpp = lv_font_get_bpp(font_p, letter); /*Bit per pixel (1,2, 4 or 8)*/ + + const uint8_t * bpp_opa_table; - uint8_t mask_init; - uint8_t mask; + uint8_t bitmask_init; + uint8_t bitmask; if(lv_font_is_monospace(font_p, letter)) { - pos_x += (lv_font_get_width(font_p, letter) - letter_w) / 2; + pos_x += (lv_font_get_width(font_p, letter) - g->box_w) / 2; } switch(bpp) { case 1: bpp_opa_table = bpp1_opa_table; - mask_init = 0x80; + bitmask_init = 0x80; break; case 2: bpp_opa_table = bpp2_opa_table; - mask_init = 0xC0; + bitmask_init = 0xC0; break; case 4: bpp_opa_table = bpp4_opa_table; - mask_init = 0xF0; + bitmask_init = 0xF0; break; case 8: bpp_opa_table = NULL; - mask_init = 0xFF; + bitmask_init = 0xFF; break; /*No opa table, pixel value will be used directly*/ default: return; /*Invalid bpp. Can't render the letter*/ } @@ -278,7 +285,7 @@ void lv_draw_letter(const lv_point_t * pos_p, const lv_area_t * mask_p, const lv if(map_p == NULL) return; /*If the letter is completely out of mask don't draw it */ - if(pos_x + letter_w < mask_p->x1 || pos_x > mask_p->x2 || pos_y + letter_h < mask_p->y1 || + if(pos_x + g->box_w < mask_p->x1 || pos_x > mask_p->x2 || pos_y + g->box_h < mask_p->y1 || pos_y > mask_p->y2) return; @@ -290,17 +297,16 @@ void lv_draw_letter(const lv_point_t * pos_p, const lv_area_t * mask_p, const lv lv_coord_t col, row; uint8_t col_bit; uint8_t col_byte_cnt; - uint8_t width_byte_scr = - letter_w >> 3; /*Width in bytes (on the screen finally) (e.g. w = 11 -> 2 bytes wide)*/ - if(letter_w & 0x7) width_byte_scr++; - uint8_t width_byte_bpp = (letter_w * bpp) >> 3; /*Letter width in byte. Real width in the font*/ - if((letter_w * bpp) & 0x7) width_byte_bpp++; + uint8_t width_byte_scr = g->box_w >> 3; /*Width in bytes (on the screen finally) (e.g. w = 11 -> 2 bytes wide)*/ + if(g->box_w & 0x7) width_byte_scr++; + uint8_t width_byte_bpp = (g->box_w * bpp) >> 3; /*Letter width in byte. Real width in the font*/ + if((g->box_w * bpp) & 0x7) width_byte_bpp++; /* Calculate the col/row start/end on the map*/ 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 col_end = pos_x + g->box_w <= mask_p->x2 ? g->box_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; + lv_coord_t row_end = pos_y + g->box_h <= mask_p->y2 ? g->box_h : mask_p->y2 - pos_y + 1; /*Set a pointer on VDB to the first pixel of the letter*/ vdb_buf_tmp += ((pos_y - vdb->area.y1) * vdb_width) + pos_x - vdb->area.x1; @@ -316,9 +322,9 @@ void lv_draw_letter(const lv_point_t * pos_p, const lv_area_t * mask_p, const lv for(row = row_start; row < row_end; row++) { col_byte_cnt = 0; col_bit = (col_start * bpp) % 8; - mask = mask_init >> col_bit; + bitmask = bitmask_init >> col_bit; for(col = col_start; col < col_end; col++) { - letter_px = (*map_p & mask) >> (8 - col_bit - bpp); + letter_px = (*map_p & bitmask) >> (8 - col_bit - bpp); if(letter_px != 0) { if(opa == LV_OPA_COVER) { px_opa = bpp == 8 ? letter_px : bpp_opa_table[letter_px]; @@ -345,11 +351,11 @@ void lv_draw_letter(const lv_point_t * pos_p, const lv_area_t * mask_p, const lv if(col_bit < 8 - bpp) { col_bit += bpp; - mask = mask >> bpp; + bitmask = bitmask >> bpp; } else { col_bit = 0; col_byte_cnt++; - mask = mask_init; + bitmask = bitmask_init; map_p++; } } diff --git a/src/lv_fonts/lv_font_dejavu_20.c b/src/lv_fonts/lv_font_dejavu_20.c index 4b74ef393..22ed3c36a 100644 --- a/src/lv_fonts/lv_font_dejavu_20.c +++ b/src/lv_fonts/lv_font_dejavu_20.c @@ -86,11 +86,11 @@ static const lv_font_glyph_dsc_t lv_font_dejavu_20_glyph_dsc[] = { {.adv_w = 6, .adv_w_fract = 0, .box_w = 6, .box_h = 0, .ofs_x = 0, .ofs_y = 0, .bitmap_index = 0}, /*Unicode: U+0020 ( )*/ {.adv_w = 8, .adv_w_fract = 0, .box_w = 8, .box_h = 13,.ofs_x = 0, .ofs_y = 3, .bitmap_index = 0}, /*Unicode: U+0031 (1)*/ {.adv_w = 9, .adv_w_fract = 0, .box_w = 9, .box_h = 13,.ofs_x = 0, .ofs_y = 3, .bitmap_index = 13}, /*Unicode: U+0033 (3)*/ - {.adv_w = 12, .adv_w_fract = 0, .box_w = 12,.box_h = 13,.ofs_x = 0, .ofs_y = 3, .bitmap_index = 26}, /*Unicode: U+0041 (A)*/ - {.adv_w = 8, .adv_w_fract = 0, .box_w = 8, .box_h = 10,.ofs_x = 0, .ofs_y = 6, .bitmap_index = 39}, /*Unicode: U+0061 (a)*/ + {.adv_w = 12, .adv_w_fract = 0, .box_w = 12,.box_h = 13,.ofs_x = 0, .ofs_y = 3, .bitmap_index = 39}, /*Unicode: U+0041 (A)*/ + {.adv_w = 8, .adv_w_fract = 0, .box_w = 8, .box_h = 10,.ofs_x = 0, .ofs_y = 6, .bitmap_index = 65}, /*Unicode: U+0061 (a)*/ }; -static const uint16_t lv_font_dejavu_20_unicode_map[] = { +static const uint16_t lv_font_dejavu_20_unicode_list[] = { 32, /*Unicode: U+0020 ( )*/ 49, /*Unicode: U+0031 (1)*/ 51, /*Unicode: U+0033 (3)*/ @@ -98,17 +98,21 @@ static const uint16_t lv_font_dejavu_20_unicode_map[] = { 97, /*Unicode: U+0061 (a)*/ }; +static lv_font_dsc_built_in_t lv_font_dejavu_20_dsc = { + .glyph_cnt = 5, /*Number of glyphs in the font*/ + .glyph_bitmap = lv_font_dejavu_20_glyph_bitmap, /*Bitmap of glyphs*/ + .glyph_dsc = lv_font_dejavu_20_glyph_dsc, /*Description of glyphs*/ + .unicode_list = lv_font_dejavu_20_unicode_list, /*Every character in the font from 'unicode_first' to 'unicode_last'*/ +}; + lv_font_t lv_font_dejavu_20 = { .unicode_first = 32, /*First Unicode letter in this font*/ .unicode_last = 126, /*Last Unicode letter in this font*/ - .h_px = 20, /*Font height in pixels*/ - .glyph_bitmap = lv_font_dejavu_20_glyph_bitmap, /*Bitmap of glyphs*/ - .glyph_dsc = lv_font_dejavu_20_glyph_dsc, /*Description of glyphs*/ - .glyph_cnt = 5, /*Number of glyphs in the font*/ - .unicode_list = NULL, /*Every character in the font from 'unicode_first' to 'unicode_last'*/ - .get_bitmap = lv_font_get_glyph_bitmap_plain, /*Function pointer to get glyph's bitmap*/ - .get_dsc = lv_font_get_glyph_dsc_plain, /*Function pointer to get glyph's width*/ + .dsc = &lv_font_dejavu_20_dsc, + .get_glyph_bitmap = lv_font_get_glyph_bitmap_plain, /*Function pointer to get glyph's bitmap*/ + .get_glyph_dsc = lv_font_get_glyph_dsc_plain, /*Function pointer to get glyph's width*/ .bpp = 1, /*Bit per pixel*/ + .line_height = 20, /*Font height in pixels*/ .monospace = 0, .next_page = NULL, /*Pointer to a font extension*/ }; diff --git a/src/lv_misc/lv_font.c b/src/lv_misc/lv_font.c index 6fd58ce41..cd3d0c10c 100644 --- a/src/lv_misc/lv_font.c +++ b/src/lv_misc/lv_font.c @@ -118,7 +118,7 @@ const uint8_t * lv_font_get_glyph_bitmap(const lv_font_t * font_p, uint32_t lett { const lv_font_t * font_i = font_p; while(font_i != NULL) { - const uint8_t * bitmap = font_i->get_bitmap(font_i, letter); + const uint8_t * bitmap = font_i->get_glyph_bitmap(font_i, letter); if(bitmap) return bitmap; font_i = font_i->next_page; @@ -133,12 +133,12 @@ const uint8_t * lv_font_get_glyph_bitmap(const lv_font_t * font_p, uint32_t lett * @param letter an UNICODE character code * @return pointer to a glyph descriptor */ -lv_font_glyph_dsc_t * lv_font_get_glyph_dsc(const lv_font_t * font_p, uint32_t letter) +const lv_font_glyph_dsc_t * lv_font_get_glyph_dsc(const lv_font_t * font_p, uint32_t letter) { const lv_font_t * font_i = font_p; - lv_font_glyph_dsc_t * dsc; + const lv_font_glyph_dsc_t * dsc; while(font_i != NULL) { - dsc = font_i->get_dsc(font_i, letter); + dsc = font_i->get_glyph_dsc(font_i, letter); if(dsc) { /*Glyph found*/ return dsc; @@ -150,6 +150,12 @@ lv_font_glyph_dsc_t * lv_font_get_glyph_dsc(const lv_font_t * font_p, uint32_t l return NULL; } +uint8_t lv_font_get_width(const lv_font_t * font, uint32_t letter) +{ + const lv_font_glyph_dsc_t * dsc = lv_font_get_glyph_dsc(font, letter); + return dsc ? dsc->adv_w : 0; +} + /** * Get the bit-per-pixel of font * @param font pointer to font @@ -181,19 +187,21 @@ const uint8_t * lv_font_get_glyph_bitmap_plain(const lv_font_t * font, uint32_t /*Check the range*/ if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) return NULL; + lv_font_dsc_built_in_t * font_dsc = (lv_font_dsc_built_in_t *) font->dsc; + /*No Unicode list -> Continuous font*/ - if(font->unicode_list == NULL) { + if(font_dsc->unicode_list == NULL) { uint32_t index = (unicode_letter - font->unicode_first); - return &font->glyph_bitmap[font->glyph_dsc[index].bitmap_index]; + return &font_dsc->glyph_bitmap[font_dsc->glyph_dsc[index].bitmap_index]; } /*Has Unicode list -> Sparse font */ else { - uint32_t * pUnicode; - pUnicode = lv_utils_bsearch(&unicode_letter, (uint32_t *)font->unicode_list, font->glyph_cnt, - sizeof(uint32_t), lv_font_codeCompare); + uint16_t * pUnicode; + pUnicode = lv_utils_bsearch(&unicode_letter, font_dsc->unicode_list, font_dsc->glyph_cnt, + sizeof(font_dsc->unicode_list[0]), lv_font_codeCompare); if(pUnicode != NULL) { - uint32_t idx = (uint32_t)(pUnicode - font->unicode_list); - return &font->glyph_bitmap[font->glyph_dsc[idx].bitmap_index]; + uint32_t idx = (uint32_t)(pUnicode - font_dsc->unicode_list); + return &font_dsc->glyph_bitmap[font_dsc->glyph_dsc[idx].bitmap_index]; } } @@ -211,24 +219,24 @@ const uint8_t * lv_font_get_glyph_bitmap_plain(const lv_font_t * font, uint32_t const lv_font_glyph_dsc_t * lv_font_get_glyph_dsc_plain(const lv_font_t * font, uint32_t unicode_letter) { /*Check the range*/ - if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) { - return NULL; - } + if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) return NULL; + + lv_font_dsc_built_in_t * font_dsc = (lv_font_dsc_built_in_t *) font->dsc; /*No Unicode list -> Continuous font*/ - if(font->unicode_list == NULL) { + if(font_dsc->unicode_list == NULL) { uint32_t index = (unicode_letter - font->unicode_first); - return &font->glyph_dsc[index]; + return &font_dsc->glyph_dsc[index]; } /*Has Unicode list -> Sparse font */ else { - uint32_t * pUnicode; - pUnicode = lv_utils_bsearch(&unicode_letter, (uint32_t *)font->unicode_list, font->glyph_cnt, - sizeof(uint32_t), lv_font_codeCompare); + uint16_t * pUnicode; + pUnicode = lv_utils_bsearch(&unicode_letter, font_dsc->unicode_list, font_dsc->glyph_cnt, + sizeof(font_dsc->unicode_list[0]), lv_font_codeCompare); if(pUnicode != NULL) { - uint32_t idx = (uint32_t)(pUnicode - font->unicode_list); - return &font->glyph_dsc[idx]; + uint32_t idx = (uint16_t)(pUnicode - font_dsc->unicode_list); + return &font_dsc->glyph_dsc[idx]; } } @@ -255,5 +263,5 @@ const lv_font_glyph_dsc_t * lv_font_get_glyph_dsc_plain(const lv_font_t * font, */ static int32_t lv_font_codeCompare(const void * pRef, const void * pElement) { - return (*(uint32_t *)pRef) - (*(uint32_t *)pElement); + return (*(uint16_t *)pRef) - (*(uint16_t *)pElement); } diff --git a/src/lv_misc/lv_font.h b/src/lv_misc/lv_font.h index e4cea7797..2bfd052d3 100644 --- a/src/lv_misc/lv_font.h +++ b/src/lv_misc/lv_font.h @@ -45,28 +45,34 @@ typedef struct int8_t ofs_y; /*y offset of the bounding box*/ } lv_font_glyph_dsc_t; +typedef struct { + const uint8_t * glyph_bitmap; + const lv_font_glyph_dsc_t * glyph_dsc; + const uint16_t * unicode_list; + uint16_t glyph_cnt; /*Number of glyphs in the font. */ +}lv_font_dsc_built_in_t; + + typedef struct _lv_font_struct { uint32_t unicode_first; uint32_t unicode_last; - const uint8_t * glyph_bitmap; - const lv_font_glyph_dsc_t * glyph_dsc; - const uint32_t * unicode_list; /*Get a glyph's descriptor from a font*/ - const uint8_t * (*get_dsc)(const struct _lv_font_struct *, lv_font_glyph_dsc_t * dsc); + const lv_font_glyph_dsc_t * (*get_glyph_dsc)(const struct _lv_font_struct *, uint32_t letter); /*Get a glyph's bitmap from a font*/ - const uint8_t * (*get_bitmap)(const struct _lv_font_struct *, uint32_t); + const uint8_t * (*get_glyph_bitmap)(const struct _lv_font_struct *, uint32_t); - /*Pointer to a font extension*/ + /*Pointer to the font in a font pack (must have the same line height)*/ struct _lv_font_struct * next_page; - uint8_t size; - uint8_t ascent; - int8_t descent; - uint8_t monospace; /*Fix width (0: normal width)*/ + uint8_t size; /*The original size*/ + uint8_t line_height; /*The real line height where any text fits*/ + uint8_t base_line; /*Base line measured from the top of the line*/ + uint8_t monospace; /*Overwrite the glyph's width (0: normal width)*/ uint8_t bpp; /*Bit per pixel: 1, 2, 4 or 8*/ - uint16_t glyph_cnt; /*Number of glyphs in the font. Max. 2048*/ + + void * dsc; /*Store implementation specific data here*/ } lv_font_t; /********************** @@ -100,6 +106,7 @@ void lv_font_remove(lv_font_t * child, lv_font_t * parent); */ 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 @@ -108,6 +115,15 @@ bool lv_font_is_monospace(const lv_font_t * font_p, uint32_t letter); */ const uint8_t * lv_font_get_glyph_bitmap(const lv_font_t * font_p, uint32_t letter); +/** + * Get the description of a glyph in a font. + * @param font_p pointer to a font + * @param letter an UNICODE character code + * @return pointer to a glyph descriptor + */ +const lv_font_glyph_dsc_t * lv_font_get_glyph_dsc(const lv_font_t * font_p, uint32_t letter); + +uint8_t lv_font_get_width(const lv_font_t * font, 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 @@ -131,7 +147,7 @@ uint8_t lv_font_get_real_width(const lv_font_t * font_p, uint32_t letter); */ static inline uint8_t lv_font_get_line_height(const lv_font_t * font_p) { - return (uint8_t)((int16_t)font_p->ascent - font_p->descent); + return font_p->line_height;//(uint8_t)((int16_t)font_p->ascent - font_p->descent); } /** diff --git a/src/lv_objx/lv_tabview.c b/src/lv_objx/lv_tabview.c index d5d106683..cdf0395b2 100644 --- a/src/lv_objx/lv_tabview.c +++ b/src/lv_objx/lv_tabview.c @@ -1023,7 +1023,7 @@ static void tabview_realign(lv_obj_t * tabview) switch(ext->btns_pos) { case LV_TABVIEW_BTNS_POS_TOP: case LV_TABVIEW_BTNS_POS_BOTTOM: - btns_size = lv_font_get_height(style_btn_rel->text.font) + + btns_size = lv_font_get_line_height(style_btn_rel->text.font) + style_btn_rel->body.padding.top + style_btn_rel->body.padding.bottom + style_btn_bg->body.padding.top + style_btn_bg->body.padding.bottom;