1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00

font: start to inegrate

This commit is contained in:
Gabor Kiss-Vamosi 2019-05-29 06:40:19 +02:00
parent b174398cb5
commit 4cffb73357
7 changed files with 102 additions and 172 deletions

3
lvgl.h
View File

@ -30,6 +30,9 @@ extern "C" {
#include "src/lv_themes/lv_theme.h"
#include "src/lv_misc/lv_font.h"
#include "src/lv_misc/lv_font_fmt/lv_font_fmt_txt.h"
#include "src/lv_objx/lv_btn.h"
#include "src/lv_objx/lv_imgbtn.h"
#include "src/lv_objx/lv_img.h"

View File

@ -247,7 +247,7 @@ void lv_draw_letter(const lv_point_t * pos_p, const lv_area_t * mask_p, const lv
}
lv_font_glyph_dsc_t g;
bool g_ret = lv_font_get_glyph_dsc(font_p, &g, letter);
bool g_ret = lv_font_get_glyph_dsc(font_p, &g, letter, '\0');
if(g_ret == false) return;
lv_coord_t pos_x = pos_p->x + g.ofs_x;

View File

@ -374,15 +374,15 @@ static const uint8_t gylph_bitmap[] = {
/* U+7E "~" */
0x7, 0x66, 0xdc, 0x20, 0x0
}
};
/*---------------------
* GLYPH DESCRIPTION
*--------------------*/
static lv_font_glyph_dsc_fmt_txt_t glyph_dsc[] = {
{.bitmap_index = 0, .adv_w = 0, .box_h = 0, .box_w = 0, .ofs_x = 0, .ofs_y = 0} // id = 0 reserved,
static const lv_font_fmt_txt_glyph_dsc_t glyph_dsc[] = {
{.bitmap_index = 0, .adv_w = 0, .box_h = 0, .box_w = 0, .ofs_x = 0, .ofs_y = 0}, // id = 0 reserved,
{.bitmap_index = 0, .adv_w = 64, .box_h = 0, .box_w = 0, .ofs_x = 0, .ofs_y = 0},
{.bitmap_index = 0, .adv_w = 67, .box_h = 12, .box_w = 2, .ofs_x = 1, .ofs_y = 0},
{.bitmap_index = 3, .adv_w = 92, .box_h = 5, .box_w = 5, .ofs_x = 0, .ofs_y = 7},
@ -478,7 +478,7 @@ static lv_font_glyph_dsc_fmt_txt_t glyph_dsc[] = {
{.bitmap_index = 1036, .adv_w = 63, .box_h = 15, .box_w = 2, .ofs_x = 1, .ofs_y = -3},
{.bitmap_index = 1040, .adv_w = 87, .box_h = 16, .box_w = 5, .ofs_x = 0, .ofs_y = -3},
{.bitmap_index = 1050, .adv_w = 174, .box_h = 4, .box_w = 9, .ofs_x = 1, .ofs_y = 3}
}
};
/*---------------------
@ -495,7 +495,7 @@ static const lv_font_fmt_txt_cmap_t cmaps[] =
.range_start = 32, .range_length = 95, .type = LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY,
.glyph_id_start = 1, .unicode_list = NULL, .glyph_id_ofs_list = NULL
}
}
};
/*-----------------
@ -504,7 +504,7 @@ static const lv_font_fmt_txt_cmap_t cmaps[] =
/*Pair left and right glyphs for kerning*/
static lv_font_fmt_txt_kern_pair_id_t kern_pair_glyph_ids[] =
static const lv_font_fmt_txt_kern_pair_id_t kern_pair_glyph_ids[] =
{
{.pair.left = 9, .pair.right = 43},
{.pair.left = 9, .pair.right = 55},
@ -994,7 +994,7 @@ static lv_font_fmt_txt_kern_pair_id_t kern_pair_glyph_ids[] =
/* Kerning between the respective left and right glyphs
* 4.4 format which needs to scaled with `kern_scale`*/
static int8_t kern_pair_values[] =
static const int8_t kern_pair_values[] =
{
-12, 5, 5, 6, 1, -4, 0, 1,
0, 1, -5, 1, -1, -1, -1, 0,
@ -1060,7 +1060,7 @@ static int8_t kern_pair_values[] =
};
/*Collect the kern pair's data in one place*/
static lv_font_fmt_txt_kern_pair_t kern_pairs =
static const lv_font_fmt_txt_kern_pair_t kern_pairs =
{
.glyph_ids = kern_pair_glyph_ids,
.values = kern_pair_values,
@ -1073,17 +1073,17 @@ static lv_font_fmt_txt_kern_pair_t kern_pairs =
*--------------------*/
/*Store all the custom data of the font*/
static lv_font_dsc_fmt_txt_t font_dsc = {
static const lv_font_fmt_txt_dsc_t font_dsc = {
.glyph_bitmap = gylph_bitmap,
.glyph_dsc = glyph_dsc,
.cmaps = cmaps,
.cmap_num = 1,
.bpp = 1,
.kerning_scale = 16,
.kern_scale = 16,
//.kern_dsc = kern_classes,
//.kern_classes = 1,
.kern_dsc = kern_pairs,
.kern_dsc = &kern_pairs,
.kern_classes = 0,
};

View File

@ -23,8 +23,6 @@
* STATIC PROTOTYPES
**********************/
static int32_t lv_font_codeCompare(const void * pRef, const void * pElement);
/**********************
* STATIC VARIABLES
**********************/
@ -49,39 +47,6 @@ void lv_font_init(void)
lv_font_builtin_init();
}
/**
* 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`
*/
void lv_font_add(lv_font_t * child, lv_font_t * parent)
{
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;
}
/**
* 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;
}
/**
* Return with the bitmap of a font.
* @param font_p pointer to a font
@ -90,15 +55,7 @@ void lv_font_remove(lv_font_t * child, lv_font_t * parent)
*/
const uint8_t * lv_font_get_glyph_bitmap(const lv_font_t * font_p, uint32_t letter)
{
const lv_font_t * font_i = font_p;
while(font_i != NULL) {
const uint8_t * bitmap = font_i->get_glyph_bitmap(font_i, letter);
if(bitmap) return bitmap;
font_i = font_i->next_page;
}
return NULL;
return font_p->get_glyph_bitmap(font_p, letter);
}
/**
@ -109,18 +66,25 @@ const uint8_t * lv_font_get_glyph_bitmap(const lv_font_t * font_p, uint32_t lett
* @return true: descriptor is successfully loaded into `dsc_out`.
* false: the letter was not found, no data is loaded to `dsc_out`
*/
bool lv_font_get_glyph_dsc(const lv_font_t * font_p, lv_font_glyph_dsc_t * dsc_out, uint32_t letter)
bool 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)
{
const lv_font_t * font_i = font_p;
return font_p->get_glyph_dsc(font_p, dsc_out, letter, letter_next);
}
/**
* 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
*/
uint16_t lv_font_get_glyph_width(const lv_font_t * font, uint32_t letter, uint32_t letter_next)
{
lv_font_glyph_dsc_t g;
bool ret;
while(font_i != NULL) {
ret = font_i->get_glyph_dsc(font_i, dsc_out, letter);
if(ret) return ret;
font_i = font_i->next_page;
}
return false;
ret = lv_font_get_glyph_dsc(font, &g, letter, letter_next);
if(ret) return g.adv_w;
else return 0;
}
/**********************

View File

@ -57,7 +57,7 @@ typedef struct
typedef struct _lv_font_struct
{
/*Get a glyph's descriptor from a font*/
bool (*get_glyph_dsc)(const struct _lv_font_struct *, lv_font_glyph_dsc_t *, uint32_t letter);
bool (*get_glyph_dsc)(const struct _lv_font_struct *, lv_font_glyph_dsc_t *, uint32_t letter, uint32_t letter_next);
/*Get a glyph's bitmap from a font*/
const uint8_t * (*get_glyph_bitmap)(const struct _lv_font_struct *, uint32_t);
@ -66,36 +66,6 @@ typedef struct _lv_font_struct
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_height*/
void * dsc; /*Store implementation specific data here*/
/*
- Font size:
not required for calculations
- Ascent
- Descent
- typoAscent
- typoDescent
- typographic descent:
Better to skip them to avoid confusion. Only line height and baseline matter for rendering.
- typoLineGap
Will be overwritten by the style.
- default advanceWidth
Not supported in text format. glyph->advacedWidth is always present
- glyphIdFormat
Has foxed size on text format
- advanceWidthFormat
Fix 8.4 format
- Glyph BBox x/y bits length (signed value)
- Glyph BBox w/h bits length (unsigned)
Has fixed size
- Glyph advanceWidth bits length (unsigned, may be FP4)
Fix 8.4 format
*/
} lv_font_t;
/**********************
@ -107,20 +77,6 @@ typedef struct _lv_font_struct
*/
void lv_font_init(void);
/**
* 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`
*/
void lv_font_add(lv_font_t * child, lv_font_t * parent);
/**
* 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);
/**
* Return with the bitmap of a font.
* @param font_p pointer to a font
@ -137,7 +93,7 @@ const uint8_t * lv_font_get_glyph_bitmap(const lv_font_t * font_p, uint32_t lett
* @return true: descriptor is successfully loaded into `dsc_out`.
* false: the letter was not found, no data is loaded to `dsc_out`
*/
bool lv_font_get_glyph_dsc(const lv_font_t * font_p, lv_font_glyph_dsc_t * dsc_out, uint32_t letter);
bool 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);
/**
* Get the width of a glyph with kerning
@ -146,7 +102,7 @@ bool lv_font_get_glyph_dsc(const lv_font_t * font_p, lv_font_glyph_dsc_t * dsc_o
* @param letter_next the next letter after `letter`. Used for kerning
* @return the width of the glyph
*/
uint8_t lv_font_get_glyph_width(const lv_font_t * font, uint32_t letter, uint32_t letter_next);
uint16_t lv_font_get_glyph_width(const lv_font_t * font, uint32_t letter, uint32_t letter_next);
/**
* Get the line height of a font. All characters fit into this height

View File

@ -7,9 +7,10 @@
* INCLUDES
*********************/
#include "lv_font.h"
#include "lv_log.h"
#include "lv_utils.h"
#include "../lv_font.h"
#include "../lv_log.h"
#include "../lv_utils.h"
#include "lv_font_fmt_txt.h"
/*********************
* DEFINES
@ -22,7 +23,7 @@
/**********************
* STATIC PROTOTYPES
**********************/
static lv_font_fmt_txt_glyph_dsc_t * get_glyph_dsc(const lv_font_t * font, uint32_t letter);
static int32_t lv_font_codeCompare(const void * pRef, const void * pElement);
/**********************
@ -49,26 +50,10 @@ static int32_t lv_font_codeCompare(const void * pRef, const void * pElement);
*/
const uint8_t * lv_font_get_bitmap_fmt_txt(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;
lv_font_fmt_txt_dsc_t * fdsc = (lv_font_fmt_txt_dsc_t *) font->dsc;
lv_font_fmt_txt_glyph_dsc_t * gdsc = get_glyph_dsc(font, unicode_letter);
lv_font_dsc_built_in_t * font_dsc = (lv_font_dsc_built_in_t *) font->dsc;
/*No Unicode list -> Continuous font*/
if(font_dsc->unicode_list == NULL) {
uint32_t index = (unicode_letter - font->unicode_first);
return &font_dsc->glyph_bitmap[font_dsc->glyph_dsc[index].bitmap_index];
}
/*Has Unicode list -> Sparse font */
else {
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_dsc->unicode_list);
return &font_dsc->glyph_bitmap[font_dsc->glyph_dsc[idx].bitmap_index];
}
}
if(gdsc) return &fdsc->glyph_bitmap[gdsc->bitmap_index];
/*If not returned earlier then the letter is not found in this font*/
return NULL;
@ -82,46 +67,68 @@ const uint8_t * lv_font_get_bitmap_fmt_txt(const lv_font_t * font, uint32_t unic
* @return true: descriptor is successfully loaded into `dsc_out`.
* false: the letter was not found, no data is loaded to `dsc_out`
*/
bool lv_font_get_glyph_dsc_fmt_txt(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter)
bool lv_font_get_glyph_dsc_fmt_txt(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter, uint32_t unicode_letter_next)
{
/*Check the range*/
if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) return NULL;
lv_font_fmt_txt_dsc_t * fdsc = (lv_font_fmt_txt_dsc_t *) font->dsc;
lv_font_fmt_txt_glyph_dsc_t * gdsc = get_glyph_dsc(font, unicode_letter);
lv_font_dsc_built_in_t * font_dsc = (lv_font_dsc_built_in_t *) font->dsc;
int32_t index = -1;
/*No Unicode list -> Continuous font*/
if(font_dsc->unicode_list == NULL) {
index = (unicode_letter - font->unicode_first);
if(gdsc) {
/*Get kerning value*/
uint32_t kvalue = 0;
uint32_t k;
if(fdsc->kern_classes == 0) {
const lv_font_fmt_txt_kern_pair_t * kdsc = fdsc->kern_dsc;
for(k = 0; k < kdsc->pair_cnt; k++) {
if(kdsc->glyph_ids[k].pair.left == unicode_letter &&
kdsc->glyph_ids[k].pair.right == unicode_letter_next) {
kvalue = kdsc->values[k];
break;
}
/*Has Unicode list -> Sparse font */
else {
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) {
index = (uint16_t)(pUnicode - font_dsc->unicode_list);
}
}
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;
uint32_t adw_w = gdsc->adv_w + ((kvalue * fdsc->kern_scale) >> 4);
adw_w =(adw_w + (1 << 3)) >> 4;
dsc_out->adv_w = adw_w;
dsc_out->box_h = gdsc->box_h;
dsc_out->box_w = gdsc->box_w;
dsc_out->ofs_x = gdsc->ofs_x;
dsc_out->ofs_y = gdsc->ofs_y;
dsc_out->bpp = fdsc->bpp;
return true;
} else {
return false;
}
return false;
}
/**********************
* STATIC FUNCTIONS
**********************/
static lv_font_fmt_txt_glyph_dsc_t * get_glyph_dsc(const lv_font_t * font, uint32_t letter)
{
lv_font_fmt_txt_dsc_t * fdsc = (lv_font_fmt_txt_dsc_t *) font->dsc;
uint16_t i;
for(i = 0; i < fdsc->cmap_num; i++) {
/*Relative code point*/
uint32_t rcp = letter - fdsc->cmaps[i].range_start;
uint32_t glyph_id = 0;
if(fdsc->cmaps[i].type == LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY) {
glyph_id = fdsc->cmaps[i].glyph_id_start + rcp;
}
if(glyph_id) return &fdsc->glyph_dsc[glyph_id];
}
return NULL;
}
/** Code Comparator.
*
* Compares the value of both input arguments.

View File

@ -16,7 +16,7 @@ extern "C" {
#ifdef LV_CONF_INCLUDE_SIMPLE
#include "lv_conf.h"
#else
#include "../../../lv_conf.h"
#include "../../../../lv_conf.h"
#endif
#include <stdint.h>
@ -128,8 +128,8 @@ typedef struct {
gylph_ids[i].pair.right == glyph_id_right)
return values[i];
*/
lv_font_fmt_txt_kern_pair_id_t glyph_ids;
uint8_t * values;
const lv_font_fmt_txt_kern_pair_id_t * glyph_ids;
const uint8_t * values;
uint16_t pair_cnt;
}lv_font_fmt_txt_kern_pair_t;
@ -144,9 +144,9 @@ typedef struct {
3. value = class_pair_values[(left_class-1)*right_class_cnt + (righ_class-1)]
*/
uint8_t * class_pair_values; /*left_class_num * right_class_num value*/
uint8_t * left_class_mapping; /*Map the glyph_ids to classes: index -> glyph_id -> class_id*/
uint8_t * right_class_mapping; /*Map the glyph_ids to classes: index -> glyph_id -> class_id*/
const uint8_t * class_pair_values; /*left_class_num * right_class_num value*/
const uint8_t * left_class_mapping; /*Map the glyph_ids to classes: index -> glyph_id -> class_id*/
const uint8_t * right_class_mapping; /*Map the glyph_ids to classes: index -> glyph_id -> class_id*/
uint8_t left_class_cnt;
uint8_t right_class_cnt;
}lv_font_fmt_txt_kern_classes_t;
@ -216,7 +216,7 @@ const uint8_t * lv_font_get_bitmap_fmt_txt(const lv_font_t * font, uint32_t lett
* @return true: descriptor is successfully loaded into `dsc_out`.
* false: the letter was not found, no data is loaded to `dsc_out`
*/
bool lv_font_get_glyph_dsc_fmt_txt(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter);
bool lv_font_get_glyph_dsc_fmt_txt(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter, uint32_t unicode_letter_next);
/**********************
* MACROS