fix(tiny_ttf): fix font metrics confusion (#4714)
This commit is contained in:
@@ -9,7 +9,7 @@ Allow using TrueType fonts LVGL. Based on
|
|||||||
https://github.com/nothings/stb
|
https://github.com/nothings/stb
|
||||||
|
|
||||||
When enabled in ``lv_conf.h`` with :c:macro:`LV_USE_TINY_TTF`
|
When enabled in ``lv_conf.h`` with :c:macro:`LV_USE_TINY_TTF`
|
||||||
:c:expr:`lv_tiny_ttf_create_data(data, data_size, line_height)` can be used to
|
:c:expr:`lv_tiny_ttf_create_data(data, data_size, font_size)` can be used to
|
||||||
create a TTF font instance at the specified line height. You can then
|
create a TTF font instance at the specified line height. You can then
|
||||||
use that font anywhere :c:struct:`lv_font_t` is accepted.
|
use that font anywhere :c:struct:`lv_font_t` is accepted.
|
||||||
|
|
||||||
@@ -17,17 +17,17 @@ By default, the TTF or OTF file must be embedded as an array, either in
|
|||||||
a header, or loaded into RAM in order to function.
|
a header, or loaded into RAM in order to function.
|
||||||
|
|
||||||
However, if :c:macro:`LV_TINY_TTF_FILE_SUPPORT` is enabled,
|
However, if :c:macro:`LV_TINY_TTF_FILE_SUPPORT` is enabled,
|
||||||
:c:expr:`lv_tiny_ttf_create_file(path, line_height)` will also be available,
|
:c:expr:`lv_tiny_ttf_create_file(path, font_size)` will also be available,
|
||||||
allowing tiny_ttf to stream from a file. The file must remain open the
|
allowing tiny_ttf to stream from a file. The file must remain open the
|
||||||
entire time the font is being used.
|
entire time the font is being used.
|
||||||
|
|
||||||
After a font is created, you can change the size by using
|
After a font is created, you can change the font size in pixels by using
|
||||||
:c:expr:`lv_tiny_ttf_set_size(font, line_height)`.
|
:c:expr:`lv_tiny_ttf_set_size(font, font_size)`.
|
||||||
|
|
||||||
By default, a font will use up to 4KB of cache to speed up rendering
|
By default, a font will use up to 4KB of cache to speed up rendering
|
||||||
glyphs. This maximum can be changed by using
|
glyphs. This maximum can be changed by using
|
||||||
:c:expr:`lv_tiny_ttf_create_data_ex(data, data_size, line_height, cache_size)`
|
:c:expr:`lv_tiny_ttf_create_data_ex(data, data_size, font_size, cache_size)`
|
||||||
or :c:expr:`lv_tiny_ttf_create_file_ex(path, line_height, cache_size)` (when
|
or :c:expr:`lv_tiny_ttf_create_file_ex(path, font_size, cache_size)` (when
|
||||||
available). The cache size is indicated in bytes.
|
available). The cache size is indicated in bytes.
|
||||||
|
|
||||||
Example
|
Example
|
||||||
@@ -35,11 +35,9 @@ Example
|
|||||||
|
|
||||||
.. include:: ../examples/libs/tiny_ttf/index.rst
|
.. include:: ../examples/libs/tiny_ttf/index.rst
|
||||||
|
|
||||||
|
|
||||||
API
|
API
|
||||||
---
|
---
|
||||||
|
|
||||||
:ref:`stb_rect_pack`
|
:ref:`stb_rect_pack`
|
||||||
|
|
||||||
:ref:`stb_truetype_htcw`
|
:ref:`stb_truetype_htcw`
|
||||||
|
|
||||||
|
|||||||
@@ -174,12 +174,12 @@ static const uint8_t * ttf_get_glyph_bitmap_cb(const lv_font_t * font, uint32_t
|
|||||||
return buffer; /*Or NULL if not found*/
|
return buffer; /*Or NULL if not found*/
|
||||||
}
|
}
|
||||||
|
|
||||||
static lv_font_t * lv_tiny_ttf_create(const char * path, const void * data, size_t data_size, lv_coord_t line_height,
|
static lv_font_t * lv_tiny_ttf_create(const char * path, const void * data, size_t data_size, lv_coord_t font_size,
|
||||||
size_t cache_size)
|
size_t cache_size)
|
||||||
{
|
{
|
||||||
LV_UNUSED(data_size);
|
LV_UNUSED(data_size);
|
||||||
LV_UNUSED(cache_size);
|
LV_UNUSED(cache_size);
|
||||||
if((path == NULL && data == NULL) || 0 >= line_height) {
|
if((path == NULL && data == NULL) || 0 >= font_size) {
|
||||||
LV_LOG_ERROR("tiny_ttf: invalid argument\n");
|
LV_LOG_ERROR("tiny_ttf: invalid argument\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -217,57 +217,51 @@ static lv_font_t * lv_tiny_ttf_create(const char * path, const void * data, size
|
|||||||
LV_LOG_ERROR("tiny_ttf: init failed\n");
|
LV_LOG_ERROR("tiny_ttf: init failed\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
float scale = stbtt_ScaleForPixelHeight(&dsc->info, line_height);
|
|
||||||
lv_font_t * out_font = (lv_font_t *)TTF_MALLOC(sizeof(lv_font_t));
|
lv_font_t * out_font = (lv_font_t *)TTF_MALLOC(sizeof(lv_font_t));
|
||||||
if(out_font == NULL) {
|
if(out_font == NULL) {
|
||||||
TTF_FREE(dsc);
|
TTF_FREE(dsc);
|
||||||
LV_LOG_ERROR("tiny_ttf: out of memory\n");
|
LV_LOG_ERROR("tiny_ttf: out of memory\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
out_font->line_height = line_height;
|
lv_memzero(out_font, sizeof(lv_font_t));
|
||||||
out_font->fallback = NULL;
|
|
||||||
out_font->dsc = dsc;
|
|
||||||
int line_gap;
|
|
||||||
stbtt_GetFontVMetrics(&dsc->info, &dsc->ascent, &dsc->descent, &line_gap);
|
|
||||||
dsc->scale = scale;
|
|
||||||
out_font->base_line = line_height - (lv_coord_t)(dsc->ascent * scale);
|
|
||||||
out_font->underline_position = (uint8_t)line_height - dsc->descent;
|
|
||||||
out_font->underline_thickness = 0;
|
|
||||||
out_font->subpx = 0;
|
|
||||||
out_font->get_glyph_dsc = ttf_get_glyph_dsc_cb;
|
out_font->get_glyph_dsc = ttf_get_glyph_dsc_cb;
|
||||||
out_font->get_glyph_bitmap = ttf_get_glyph_bitmap_cb;
|
out_font->get_glyph_bitmap = ttf_get_glyph_bitmap_cb;
|
||||||
|
out_font->dsc = dsc;
|
||||||
|
lv_tiny_ttf_set_size(out_font, font_size);
|
||||||
return out_font;
|
return out_font;
|
||||||
}
|
}
|
||||||
#if LV_TINY_TTF_FILE_SUPPORT != 0
|
#if LV_TINY_TTF_FILE_SUPPORT != 0
|
||||||
lv_font_t * lv_tiny_ttf_create_file_ex(const char * path, lv_coord_t line_height, size_t cache_size)
|
lv_font_t * lv_tiny_ttf_create_file_ex(const char * path, lv_coord_t font_size, size_t cache_size)
|
||||||
{
|
{
|
||||||
return lv_tiny_ttf_create(path, NULL, 0, line_height, cache_size);
|
return lv_tiny_ttf_create(path, NULL, 0, font_size, cache_size);
|
||||||
}
|
}
|
||||||
lv_font_t * lv_tiny_ttf_create_file(const char * path, lv_coord_t line_height)
|
lv_font_t * lv_tiny_ttf_create_file(const char * path, lv_coord_t font_size)
|
||||||
{
|
{
|
||||||
return lv_tiny_ttf_create(path, NULL, 0, line_height, 0);
|
return lv_tiny_ttf_create(path, NULL, 0, font_size, 0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
lv_font_t * lv_tiny_ttf_create_data_ex(const void * data, size_t data_size, lv_coord_t line_height, size_t cache_size)
|
lv_font_t * lv_tiny_ttf_create_data_ex(const void * data, size_t data_size, lv_coord_t font_size, size_t cache_size)
|
||||||
{
|
{
|
||||||
return lv_tiny_ttf_create(NULL, data, data_size, line_height, cache_size);
|
return lv_tiny_ttf_create(NULL, data, data_size, font_size, cache_size);
|
||||||
}
|
}
|
||||||
lv_font_t * lv_tiny_ttf_create_data(const void * data, size_t data_size, lv_coord_t line_height)
|
lv_font_t * lv_tiny_ttf_create_data(const void * data, size_t data_size, lv_coord_t font_size)
|
||||||
{
|
{
|
||||||
return lv_tiny_ttf_create(NULL, data, data_size, line_height, 0);
|
return lv_tiny_ttf_create(NULL, data, data_size, font_size, 0);
|
||||||
}
|
}
|
||||||
void lv_tiny_ttf_set_size(lv_font_t * font, lv_coord_t line_height)
|
void lv_tiny_ttf_set_size(lv_font_t * font, lv_coord_t font_size)
|
||||||
{
|
{
|
||||||
ttf_font_desc_t * dsc = (ttf_font_desc_t *)font->dsc;
|
if(font_size <= 0) {
|
||||||
if(line_height > 0) {
|
LV_LOG_ERROR("invalid font size: %d", font_size);
|
||||||
font->line_height = line_height;
|
return;
|
||||||
dsc->scale = stbtt_ScaleForPixelHeight(&dsc->info, line_height);
|
|
||||||
font->base_line = line_height - (lv_coord_t)(dsc->ascent * dsc->scale);
|
|
||||||
font->underline_position = (uint8_t)line_height - dsc->descent;
|
|
||||||
}
|
}
|
||||||
|
ttf_font_desc_t * dsc = (ttf_font_desc_t *)font->dsc;
|
||||||
|
dsc->scale = stbtt_ScaleForMappingEmToPixels(&dsc->info, font_size);
|
||||||
|
int line_gap = 0;
|
||||||
|
stbtt_GetFontVMetrics(&dsc->info, &dsc->ascent, &dsc->descent, &line_gap);
|
||||||
|
font->line_height = (lv_coord_t)(dsc->scale * (dsc->ascent - dsc->descent + line_gap));
|
||||||
|
font->base_line = (lv_coord_t)(dsc->scale * (line_gap - dsc->descent));
|
||||||
}
|
}
|
||||||
void lv_tiny_ttf_destroy(lv_font_t * font)
|
void lv_tiny_ttf_destroy(lv_font_t * font)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -30,20 +30,20 @@ extern "C" {
|
|||||||
**********************/
|
**********************/
|
||||||
#if LV_TINY_TTF_FILE_SUPPORT !=0
|
#if LV_TINY_TTF_FILE_SUPPORT !=0
|
||||||
/* create a font from the specified file or path with the specified line height.*/
|
/* create a font from the specified file or path with the specified line height.*/
|
||||||
lv_font_t * lv_tiny_ttf_create_file(const char * path, lv_coord_t line_height);
|
lv_font_t * lv_tiny_ttf_create_file(const char * path, lv_coord_t font_size);
|
||||||
|
|
||||||
/* create a font from the specified file or path with the specified line height with the specified cache size.*/
|
/* create a font from the specified file or path with the specified line height with the specified cache size.*/
|
||||||
lv_font_t * lv_tiny_ttf_create_file_ex(const char * path, lv_coord_t line_height, size_t cache_size);
|
lv_font_t * lv_tiny_ttf_create_file_ex(const char * path, lv_coord_t font_size, size_t cache_size);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* create a font from the specified data pointer with the specified line height.*/
|
/* create a font from the specified data pointer with the specified line height.*/
|
||||||
lv_font_t * lv_tiny_ttf_create_data(const void * data, size_t data_size, lv_coord_t line_height);
|
lv_font_t * lv_tiny_ttf_create_data(const void * data, size_t data_size, lv_coord_t font_size);
|
||||||
|
|
||||||
/* create a font from the specified data pointer with the specified line height and the specified cache size.*/
|
/* create a font from the specified data pointer with the specified line height and the specified cache size.*/
|
||||||
lv_font_t * lv_tiny_ttf_create_data_ex(const void * data, size_t data_size, lv_coord_t line_height, size_t cache_size);
|
lv_font_t * lv_tiny_ttf_create_data_ex(const void * data, size_t data_size, lv_coord_t font_size, size_t cache_size);
|
||||||
|
|
||||||
/* set the size of the font to a new line_height*/
|
/* set the size of the font to a new font_size*/
|
||||||
void lv_tiny_ttf_set_size(lv_font_t * font, lv_coord_t line_height);
|
void lv_tiny_ttf_set_size(lv_font_t * font, lv_coord_t font_size);
|
||||||
|
|
||||||
/* destroy a font previously created with lv_tiny_ttf_create_xxxx()*/
|
/* destroy a font previously created with lv_tiny_ttf_create_xxxx()*/
|
||||||
void lv_tiny_ttf_destroy(lv_font_t * font);
|
void lv_tiny_ttf_destroy(lv_font_t * font);
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
Reference in New Issue
Block a user