2017-11-23 20:42:14 +01:00
|
|
|
/**
|
2017-11-23 21:28:36 +01:00
|
|
|
* @file lv_font.c
|
2018-06-19 09:49:58 +02:00
|
|
|
*
|
2017-11-23 20:42:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
2019-03-06 11:14:06 +01:00
|
|
|
|
2017-11-23 20:42:14 +01:00
|
|
|
#include "lv_font.h"
|
2018-07-25 17:57:08 +02:00
|
|
|
#include "lv_log.h"
|
2019-03-18 07:25:20 +01:00
|
|
|
#include "lv_utils.h"
|
2017-11-23 20:42:14 +01:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* STATIC PROTOTYPES
|
|
|
|
**********************/
|
|
|
|
|
2019-04-04 07:15:40 +02:00
|
|
|
static int32_t lv_font_codeCompare(const void * pRef, const void * pElement);
|
2019-03-06 11:14:06 +01:00
|
|
|
|
2017-11-23 20:42:14 +01:00
|
|
|
/**********************
|
|
|
|
* STATIC VARIABLES
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL PROTOTYPES
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL FUNCTIONS
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
/**
|
2019-05-01 18:08:56 +02:00
|
|
|
* Initialize the font module
|
2017-11-23 20:42:14 +01:00
|
|
|
*/
|
2017-11-23 21:28:36 +01:00
|
|
|
void lv_font_init(void)
|
2017-11-23 20:42:14 +01:00
|
|
|
{
|
2018-10-05 17:22:49 +02:00
|
|
|
lv_font_builtin_init();
|
2017-11-23 20:42:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-13 15:18:06 +01:00
|
|
|
* Add a font to an other to extend the character set.
|
|
|
|
* @param child the font to add
|
|
|
|
* @param parent this font will be extended. Using it later will contain the characters from `child`
|
2017-11-23 20:42:14 +01:00
|
|
|
*/
|
2018-06-19 09:49:58 +02:00
|
|
|
void lv_font_add(lv_font_t * child, lv_font_t * parent)
|
2017-11-23 20:42:14 +01:00
|
|
|
{
|
|
|
|
if(parent == NULL) return;
|
|
|
|
|
|
|
|
while(parent->next_page != NULL) {
|
|
|
|
parent = parent->next_page; /*Got to the last page and add the new font there*/
|
|
|
|
}
|
|
|
|
|
|
|
|
parent->next_page = child;
|
|
|
|
}
|
|
|
|
|
2018-12-13 15:18:06 +01:00
|
|
|
/**
|
|
|
|
* Remove a font from a character set.
|
|
|
|
* @param child the font to remove
|
|
|
|
* @param parent remove `child` from here
|
|
|
|
*/
|
|
|
|
void lv_font_remove(lv_font_t * child, lv_font_t * parent)
|
|
|
|
{
|
|
|
|
if(parent == NULL) return;
|
|
|
|
if(child == NULL) return;
|
|
|
|
|
|
|
|
while(parent->next_page != child) {
|
|
|
|
parent = parent->next_page; /*Got to the last page and add the new font there*/
|
|
|
|
}
|
|
|
|
|
|
|
|
parent->next_page = child->next_page;
|
|
|
|
}
|
|
|
|
|
2017-11-23 20:42:14 +01:00
|
|
|
/**
|
|
|
|
* Return with the bitmap of a font.
|
|
|
|
* @param font_p pointer to a font
|
2018-06-22 23:32:21 +02:00
|
|
|
* @param letter an UNICODE character code
|
2017-11-23 20:42:14 +01:00
|
|
|
* @return pointer to the bitmap of the letter
|
|
|
|
*/
|
2019-04-22 05:21:35 +02:00
|
|
|
const uint8_t * lv_font_get_glyph_bitmap(const lv_font_t * font_p, uint32_t letter)
|
2017-11-23 20:42:14 +01:00
|
|
|
{
|
2017-11-23 21:28:36 +01:00
|
|
|
const lv_font_t * font_i = font_p;
|
2017-11-23 20:42:14 +01:00
|
|
|
while(font_i != NULL) {
|
2019-04-24 17:28:38 +02:00
|
|
|
const uint8_t * bitmap = font_i->get_glyph_bitmap(font_i, letter);
|
2018-02-23 13:56:04 +01:00
|
|
|
if(bitmap) return bitmap;
|
2017-11-23 20:42:14 +01:00
|
|
|
|
|
|
|
font_i = font_i->next_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-01 16:43:32 +02:00
|
|
|
* Get the descriptor of a glyph
|
|
|
|
* @param font_p pointer to font
|
|
|
|
* @param dsc_out store the result descriptor here
|
|
|
|
* @param letter an UNICODE letter code
|
|
|
|
* @return true: descriptor is successfully loaded into `dsc_out`.
|
|
|
|
* false: the letter was not found, no data is loaded to `dsc_out`
|
2017-11-23 20:42:14 +01:00
|
|
|
*/
|
2019-05-01 16:43:32 +02:00
|
|
|
bool lv_font_get_glyph_dsc(const lv_font_t * font_p, lv_font_glyph_dsc_t * dsc_out, uint32_t letter)
|
2018-06-22 23:32:21 +02:00
|
|
|
{
|
|
|
|
const lv_font_t * font_i = font_p;
|
2019-05-01 16:43:32 +02:00
|
|
|
bool ret;
|
2018-06-22 23:32:21 +02:00
|
|
|
while(font_i != NULL) {
|
2019-05-01 16:43:32 +02:00
|
|
|
ret = font_i->get_glyph_dsc(font_i, dsc_out, letter);
|
|
|
|
if(ret) return ret;
|
2018-06-22 23:32:21 +02:00
|
|
|
|
|
|
|
font_i = font_i->next_page;
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:43:32 +02:00
|
|
|
return false;
|
2019-04-24 17:28:38 +02:00
|
|
|
}
|
|
|
|
|
2018-02-09 12:40:00 +01:00
|
|
|
/**
|
2019-05-01 16:43:32 +02:00
|
|
|
* Get the width of a glyph with kerning
|
|
|
|
* @param font pointer to a font
|
|
|
|
* @param letter an UNICODE letter
|
|
|
|
* @param letter_next the next letter after `letter`. Used for kerning
|
|
|
|
* @return the width of the glyph
|
2018-02-09 12:40:00 +01:00
|
|
|
*/
|
2019-05-01 16:43:32 +02:00
|
|
|
uint8_t lv_font_get_glyph_width(const lv_font_t * font, uint32_t letter, uint32_t letter_next)
|
2018-02-09 12:40:00 +01:00
|
|
|
{
|
2019-05-01 16:43:32 +02:00
|
|
|
lv_font_glyph_dsc_t dsc;
|
|
|
|
bool ret = lv_font_get_glyph_dsc(font, &dsc, letter);
|
|
|
|
if(ret == false) return 0;
|
|
|
|
|
|
|
|
int32_t w = dsc.adv_w;
|
|
|
|
|
|
|
|
/*Apply kerning is required*/
|
|
|
|
if(dsc.kern_table && letter_next != 0) {
|
|
|
|
uint32_t i;
|
|
|
|
for(i = 0; dsc.kern_table[i].next_unicode != 0; i++) {
|
|
|
|
if((uint32_t)dsc.kern_table[i].next_unicode == letter_next) {
|
2019-05-01 18:08:56 +02:00
|
|
|
if(dsc.kern_table[i].space_sign == LV_FONT_KERN_POSITIVE) {
|
|
|
|
w += dsc.kern_table[i].space;
|
|
|
|
} else {
|
|
|
|
w -= dsc.kern_table[i].space;
|
|
|
|
}
|
2019-05-01 16:43:32 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-06-19 09:49:58 +02:00
|
|
|
}
|
2018-02-09 12:40:00 +01:00
|
|
|
}
|
|
|
|
|
2019-05-01 16:43:32 +02:00
|
|
|
if(w < 0) w = 0;
|
|
|
|
|
|
|
|
return w <= 0 ? 0 : LV_FONT_GET_WIDTH_INT(w);
|
2018-02-09 12:40:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-01 16:43:32 +02:00
|
|
|
* Used as `get_glyph_bitmap` callback in LittelvGL's native font format if the font is uncompressed.
|
2018-02-09 12:40:00 +01:00
|
|
|
* @param font pointer to font
|
|
|
|
* @param unicode_letter an unicode letter which bitmap should be get
|
|
|
|
* @return pointer to the bitmap or NULL if not found
|
|
|
|
*/
|
2019-04-22 05:21:35 +02:00
|
|
|
const uint8_t * lv_font_get_glyph_bitmap_plain(const lv_font_t * font, uint32_t unicode_letter)
|
2018-02-09 12:40:00 +01:00
|
|
|
{
|
2018-02-23 13:56:04 +01:00
|
|
|
/*Check the range*/
|
|
|
|
if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) return NULL;
|
|
|
|
|
2019-04-24 17:28:38 +02:00
|
|
|
lv_font_dsc_built_in_t * font_dsc = (lv_font_dsc_built_in_t *) font->dsc;
|
|
|
|
|
2019-04-22 05:21:35 +02:00
|
|
|
/*No Unicode list -> Continuous font*/
|
2019-04-24 17:28:38 +02:00
|
|
|
if(font_dsc->unicode_list == NULL) {
|
2019-04-22 05:21:35 +02:00
|
|
|
uint32_t index = (unicode_letter - font->unicode_first);
|
2019-04-24 17:28:38 +02:00
|
|
|
return &font_dsc->glyph_bitmap[font_dsc->glyph_dsc[index].bitmap_index];
|
2019-04-22 05:21:35 +02:00
|
|
|
}
|
|
|
|
/*Has Unicode list -> Sparse font */
|
|
|
|
else {
|
2019-04-24 17:28:38 +02:00
|
|
|
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);
|
2019-04-22 05:21:35 +02:00
|
|
|
if(pUnicode != NULL) {
|
2019-04-24 17:28:38 +02:00
|
|
|
uint32_t idx = (uint32_t)(pUnicode - font_dsc->unicode_list);
|
|
|
|
return &font_dsc->glyph_bitmap[font_dsc->glyph_dsc[idx].bitmap_index];
|
2019-04-22 05:21:35 +02:00
|
|
|
}
|
2018-02-09 12:40:00 +01:00
|
|
|
}
|
|
|
|
|
2019-04-22 05:21:35 +02:00
|
|
|
/*If not returned earlier then the letter is not found in this font*/
|
2018-02-09 12:40:00 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:56:04 +01:00
|
|
|
/**
|
2019-05-01 16:43:32 +02:00
|
|
|
* Used as `get_glyph_dsc` callback in LittelvGL's native font format if the font is uncompressed.
|
|
|
|
* @param font_p pointer to font
|
|
|
|
* @param dsc_out store the result descriptor here
|
|
|
|
* @param letter an UNICODE letter code
|
|
|
|
* @return true: descriptor is successfully loaded into `dsc_out`.
|
|
|
|
* false: the letter was not found, no data is loaded to `dsc_out`
|
2018-02-23 13:56:04 +01:00
|
|
|
*/
|
2019-05-01 16:43:32 +02:00
|
|
|
bool lv_font_get_glyph_dsc_plain(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter)
|
2018-02-23 13:56:04 +01:00
|
|
|
{
|
|
|
|
/*Check the range*/
|
2019-04-24 17:28:38 +02:00
|
|
|
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;
|
2019-05-01 16:43:32 +02:00
|
|
|
int32_t index = -1;
|
2019-04-22 05:21:35 +02:00
|
|
|
/*No Unicode list -> Continuous font*/
|
2019-04-24 17:28:38 +02:00
|
|
|
if(font_dsc->unicode_list == NULL) {
|
2019-05-01 16:43:32 +02:00
|
|
|
index = (unicode_letter - font->unicode_first);
|
2019-04-22 05:21:35 +02:00
|
|
|
}
|
|
|
|
/*Has Unicode list -> Sparse font */
|
|
|
|
else {
|
2019-04-24 17:28:38 +02:00
|
|
|
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);
|
2019-04-22 05:21:35 +02:00
|
|
|
|
|
|
|
if(pUnicode != NULL) {
|
2019-05-01 16:43:32 +02:00
|
|
|
index = (uint16_t)(pUnicode - font_dsc->unicode_list);
|
2019-04-22 05:21:35 +02:00
|
|
|
}
|
2018-02-23 13:56:04 +01:00
|
|
|
}
|
|
|
|
|
2019-05-01 16:43:32 +02:00
|
|
|
if(index > 0) {
|
|
|
|
dsc_out->adv_w = font_dsc->glyph_dsc[index].adv_w;
|
|
|
|
dsc_out->box_h = font_dsc->glyph_dsc[index].box_h;
|
|
|
|
dsc_out->box_w = font_dsc->glyph_dsc[index].box_w;
|
|
|
|
dsc_out->ofs_x = font_dsc->glyph_dsc[index].ofs_x;
|
|
|
|
dsc_out->ofs_y = font_dsc->glyph_dsc[index].ofs_y;
|
|
|
|
dsc_out->kern_table = font_dsc->glyph_dsc[index].kern_table;
|
|
|
|
dsc_out->bpp = font_dsc->bpp;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-23 13:56:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-23 20:42:14 +01:00
|
|
|
/**********************
|
|
|
|
* STATIC FUNCTIONS
|
|
|
|
**********************/
|
2019-03-06 11:14:06 +01:00
|
|
|
|
|
|
|
/** Code Comparator.
|
|
|
|
*
|
|
|
|
* Compares the value of both input arguments.
|
|
|
|
*
|
|
|
|
* @param[in] pRef Pointer to the reference.
|
|
|
|
* @param[in] pElement Pointer to the element to compare.
|
|
|
|
*
|
|
|
|
* @return Result of comparison.
|
|
|
|
* @retval < 0 Reference is greater than element.
|
|
|
|
* @retval = 0 Reference is equal to element.
|
|
|
|
* @retval > 0 Reference is less than element.
|
|
|
|
*
|
|
|
|
*/
|
2019-04-04 07:15:40 +02:00
|
|
|
static int32_t lv_font_codeCompare(const void * pRef, const void * pElement)
|
2019-03-06 11:14:06 +01:00
|
|
|
{
|
2019-04-24 17:28:38 +02:00
|
|
|
return (*(uint16_t *)pRef) - (*(uint16_t *)pElement);
|
2019-03-06 11:14:06 +01:00
|
|
|
}
|