mirror of
https://github.com/lvgl/lvgl.git
synced 2025-01-28 07:03:00 +08:00
font: update fmt_txt
This commit is contained in:
parent
b6d24040bf
commit
e8605a52ed
@ -123,136 +123,6 @@ bool lv_font_get_glyph_dsc(const lv_font_t * font_p, lv_font_glyph_dsc_t * dsc_o
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
uint8_t lv_font_get_glyph_width(const lv_font_t * font, uint32_t letter, uint32_t letter_next)
|
||||
{
|
||||
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) {
|
||||
if(dsc.kern_table[i].space_sign == LV_FONT_KERN_POSITIVE) {
|
||||
w += dsc.kern_table[i].space;
|
||||
} else {
|
||||
w -= dsc.kern_table[i].space;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(w < 0) w = 0;
|
||||
|
||||
return w <= 0 ? 0 : LV_FONT_GET_WIDTH_INT(w);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used as `get_glyph_bitmap` callback in LittelvGL's native font format if the font is uncompressed.
|
||||
* @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
|
||||
*/
|
||||
const uint8_t * lv_font_get_glyph_bitmap_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;
|
||||
|
||||
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 not returned earlier then the letter is not found in this font*/
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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`
|
||||
*/
|
||||
bool lv_font_get_glyph_dsc_plain(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter)
|
||||
{
|
||||
/*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;
|
||||
int32_t index = -1;
|
||||
/*No Unicode list -> Continuous font*/
|
||||
if(font_dsc->unicode_list == NULL) {
|
||||
index = (unicode_letter - font->unicode_first);
|
||||
}
|
||||
/*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;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**********************
|
||||
* STATIC FUNCTIONS
|
||||
**********************/
|
||||
|
||||
/** 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.
|
||||
*
|
||||
*/
|
||||
static int32_t lv_font_codeCompare(const void * pRef, const void * pElement)
|
||||
{
|
||||
return (*(uint16_t *)pRef) - (*(uint16_t *)pElement);
|
||||
}
|
||||
|
@ -73,102 +73,10 @@ typedef struct _lv_font_struct
|
||||
void * dsc; /*Store implementation specific data here*/
|
||||
} lv_font_t;
|
||||
|
||||
|
||||
/*---------------------------------------------
|
||||
* Types for LittlevGL's internal font format
|
||||
*---------------------------------------------*/
|
||||
|
||||
/*Describe a glyph in internal font format */
|
||||
typedef struct
|
||||
{
|
||||
uint32_t bitmap_index : 20; /* Start index of the bitmap. A font can be max 1 MB. */
|
||||
uint32_t adv_w :12; /*The glyph needs this space. Draw the next glyph after this width. 8 bit integer, 4 bit fractional */
|
||||
|
||||
uint8_t box_w; /*Width of the glyph's bounding box*/
|
||||
uint8_t box_h; /*Height of the glyph's bounding box*/
|
||||
int8_t ofs_x; /*x offset of the bounding box*/
|
||||
int8_t ofs_y; /*y offset of the bounding box*/
|
||||
|
||||
}lv_font_glyph_dsc_built_in_t;
|
||||
|
||||
typedef struct {
|
||||
/* First Unicode character for this range */
|
||||
uint32_t range_start;
|
||||
|
||||
/* Number of Unicode characters related to this range.
|
||||
* Last Unicode character = range_start + range_length - 1*/
|
||||
uint16_t range_length;
|
||||
|
||||
/* First glyph ID (array index of `glyph_dsc`) for this range */
|
||||
uint16_t glyph_id_start;
|
||||
|
||||
/* Format 0 tiny
|
||||
* glyph_id = glyph_id_start + (codepoint - range_start) */
|
||||
|
||||
/* Format 0 full
|
||||
* glyph_id = glyph_id_start + glyph_id_list[codepoint - range_start] */
|
||||
|
||||
/* Sparse tiny
|
||||
* glyph_id = glyph_id_start + search(unicode_list, codepoint - range_start) */
|
||||
|
||||
/* Sparse full
|
||||
* glyph_id = glyph_id_start + glyph_id_list[search(unicode_list, codepoint - range_start)] */
|
||||
|
||||
uint16_t * unicode_list;
|
||||
|
||||
|
||||
|
||||
|
||||
/* NULL: the range is mapped continuously from `glyph_id_start`
|
||||
* Else map the Unicode characters from `glyph_id_start` (relative to `range_start`)*/
|
||||
uint16_t * unicode_list;
|
||||
}lv_font_cmap_built_in_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t * left_gylph_ids;
|
||||
uint16_t * right_gylph_ids;
|
||||
uint8_t * values;
|
||||
uint16_t pair_num;
|
||||
}lv_font_kern_pair_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t left_class_num;
|
||||
uint8_t right_class_num;
|
||||
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*/
|
||||
}lv_font_kern_classes_t;
|
||||
|
||||
/*Describe store additional data for fonts */
|
||||
typedef struct {
|
||||
const uint8_t * glyph_bitmap;
|
||||
const lv_font_glyph_dsc_built_in_t * glyph_dsc;
|
||||
|
||||
/* Map the glyphs to Unicode characters.
|
||||
* Array of `lv_font_cmap_built_in_t` variables*/
|
||||
const lv_font_cmap_built_in_t * cmaps;
|
||||
|
||||
/*Number of cmap tables*/
|
||||
uint8_t cmap_num;
|
||||
|
||||
/* Sotore kerning values. Only one oft hese pointer can have a valid values.
|
||||
* to other should be `NULL` */
|
||||
const lv_font_kern_pair_t * kern_table;
|
||||
const lv_font_kern_classes_t * kern_classes;
|
||||
|
||||
|
||||
/*Bit per pixel: 1, 2, 4 or 8*/
|
||||
uint8_t bpp;
|
||||
}lv_font_dsc_built_in_t;
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/*----------------
|
||||
* General API
|
||||
*---------------*/
|
||||
|
||||
/**
|
||||
* Initialize the font module
|
||||
*/
|
||||
@ -225,28 +133,6 @@ static inline uint8_t lv_font_get_line_height(const lv_font_t * font_p)
|
||||
return font_p->line_height;
|
||||
}
|
||||
|
||||
/*----------------------------------
|
||||
* LittlevGL's internal font format
|
||||
*----------------------------------*/
|
||||
|
||||
/**
|
||||
* Used as `get_glyph_bitmap` callback in LittelvGL's native font format if the font is uncompressed.
|
||||
* @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
|
||||
*/
|
||||
const uint8_t * lv_font_get_glyph_bitmap_plain(const lv_font_t * font, uint32_t letter);
|
||||
|
||||
/**
|
||||
* 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`
|
||||
*/
|
||||
bool lv_font_get_glyph_dsc_plain(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
@ -1,246 +0,0 @@
|
||||
/**
|
||||
* @file lv_font.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LV_FONT_H
|
||||
#define LV_FONT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
#ifdef LV_CONF_INCLUDE_SIMPLE
|
||||
#include "lv_conf.h"
|
||||
#else
|
||||
#include "../../../lv_conf.h"
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "lv_symbol_def.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
/*Number of fractional digits in the advanced width (`adv_w`) field of `lv_font_glyph_dsc_t`*/
|
||||
#define LV_FONT_WIDTH_FRACT_DIGIT 4
|
||||
|
||||
#define LV_FONT_KERN_POSITIVE 0
|
||||
#define LV_FONT_KERN_NEGATIVE 1
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
/*------------------
|
||||
* General types
|
||||
*-----------------*/
|
||||
|
||||
/*One element of a kerning table*/
|
||||
typedef struct {
|
||||
uint32_t next_unicode :23;
|
||||
|
||||
uint32_t space :8; /*5 integer, 4 fractional*/
|
||||
uint32_t space_sign :1; /*0: space positive; 1: space negative*/
|
||||
}lv_font_kern_t;
|
||||
|
||||
/*Describe the properties of a glyph*/
|
||||
typedef struct
|
||||
{
|
||||
uint16_t adv_w; /*The glyph needs this space. Draw the next glyph after this width. 8 bit integer, 4 bit fractional */
|
||||
uint8_t box_w; /*Width of the glyph's bounding box*/
|
||||
uint8_t box_h; /*Height of the glyph's bounding box*/
|
||||
int8_t ofs_x; /*x offset of the bounding box*/
|
||||
int8_t ofs_y; /*y offset of the bounding box*/
|
||||
uint8_t bpp; /*Bit-per-pixel: 1, 2, 4, 8*/
|
||||
const lv_font_kern_t * kern_table;
|
||||
}lv_font_glyph_dsc_t;
|
||||
|
||||
/*Describe the properties of a font*/
|
||||
typedef struct _lv_font_struct
|
||||
{
|
||||
uint32_t unicode_first;
|
||||
uint32_t unicode_last;
|
||||
|
||||
/*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);
|
||||
|
||||
/*Get a glyph's bitmap from a font*/
|
||||
const uint8_t * (*get_glyph_bitmap)(const struct _lv_font_struct *, uint32_t);
|
||||
|
||||
/*Pointer to the font in a font pack (must have the same line height)*/
|
||||
struct _lv_font_struct * next_page;
|
||||
uint8_t size; /*The original size (height)*/
|
||||
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*/
|
||||
} lv_font_t;
|
||||
|
||||
|
||||
/*---------------------------------------------
|
||||
* Types for LittlevGL's internal font format
|
||||
*---------------------------------------------*/
|
||||
|
||||
/*Describe a glyph in internal font format */
|
||||
typedef struct
|
||||
{
|
||||
uint32_t bitmap_index : 20; /* Start index of the bitmap. A font can be max 1 MB. */
|
||||
uint32_t adv_w :12; /*The glyph needs this space. Draw the next glyph after this width. 8 bit integer, 4 bit fractional */
|
||||
|
||||
uint8_t box_w; /*Width of the glyph's bounding box*/
|
||||
uint8_t box_h; /*Height of the glyph's bounding box*/
|
||||
int8_t ofs_x; /*x offset of the bounding box*/
|
||||
int8_t ofs_y; /*y offset of the bounding box*/
|
||||
|
||||
const lv_font_kern_t * kern_table;
|
||||
}lv_font_glyph_dsc_built_in_t;
|
||||
|
||||
typedef struct {
|
||||
/* First Unicode character for this range */
|
||||
uint32_t range_start;
|
||||
|
||||
/* Number of Unicode characters related to this range.
|
||||
* Last Unicode character = range_start + range_length - 1*/
|
||||
uint16_t range_length;
|
||||
|
||||
/* First glyph ID (array index of `glyph_dsc`) for this range */
|
||||
uint16_t glyph_id_start;
|
||||
|
||||
/* NULL: the range is mapped continuously from `glyph_id_start`
|
||||
* Else map the Unicode characters from `glyph_id_start` (relative to `range_start`)*/
|
||||
uint16_t * unicode_list;
|
||||
}lv_font_cmap_built_in_t;
|
||||
|
||||
/*Describe store additional data for fonts */
|
||||
typedef struct {
|
||||
const uint8_t * glyph_bitmap;
|
||||
const lv_font_glyph_dsc_built_in_t * glyph_dsc;
|
||||
|
||||
/* Map the glyphs to Unicode characters.
|
||||
* Array of `lv_font_cmap_built_in_t` variables*/
|
||||
const lv_font_cmap_built_in_t * cmaps;
|
||||
|
||||
/*Number of cmap tables*/
|
||||
uint8_t cmap_num;
|
||||
|
||||
/*Bit per pixel: 1, 2, 4 or 8*/
|
||||
uint8_t bpp;
|
||||
}lv_font_dsc_built_in_t;
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/*----------------
|
||||
* General API
|
||||
*---------------*/
|
||||
|
||||
/**
|
||||
* Initialize the font module
|
||||
*/
|
||||
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
|
||||
* @param letter an UNICODE character code
|
||||
* @return pointer to the bitmap of the letter
|
||||
*/
|
||||
const uint8_t * lv_font_get_glyph_bitmap(const lv_font_t * font_p, uint32_t letter);
|
||||
|
||||
/**
|
||||
* 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`
|
||||
*/
|
||||
bool lv_font_get_glyph_dsc(const lv_font_t * font_p, lv_font_glyph_dsc_t * dsc_out, uint32_t letter);
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
uint8_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
|
||||
* @param font_p pointer to a font
|
||||
* @return the height of a font
|
||||
*/
|
||||
static inline uint8_t lv_font_get_line_height(const lv_font_t * font_p)
|
||||
{
|
||||
return font_p->line_height;
|
||||
}
|
||||
|
||||
/*----------------------------------
|
||||
* LittlevGL's internal font format
|
||||
*----------------------------------*/
|
||||
|
||||
/**
|
||||
* Used as `get_glyph_bitmap` callback in LittelvGL's native font format if the font is uncompressed.
|
||||
* @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
|
||||
*/
|
||||
const uint8_t * lv_font_get_glyph_bitmap_plain(const lv_font_t * font, uint32_t letter);
|
||||
|
||||
/**
|
||||
* 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`
|
||||
*/
|
||||
bool lv_font_get_glyph_dsc_plain(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
#define LV_FONT_DECLARE(font_name) extern lv_font_t font_name;
|
||||
|
||||
#define LV_FONT_SET_WIDTH(_integer, _fract) ((_integer << LV_FONT_WIDTH_FRACT_DIGIT) + _fract)
|
||||
#define LV_FONT_GET_WIDTH_INT(_w) (_w >> LV_FONT_WIDTH_FRACT_DIGIT)
|
||||
#define LV_FONT_GET_WIDTH_FRACT(_w) (_w & ((1 << LV_FONT_WIDTH_FRACT_DIGIT) -1))
|
||||
|
||||
/**********************
|
||||
* ADD BUILT IN FONTS
|
||||
**********************/
|
||||
#include "../lv_fonts/lv_font_builtin.h"
|
||||
|
||||
/*Declare the custom (user defined) fonts*/
|
||||
#ifdef LV_FONT_CUSTOM_DECLARE
|
||||
LV_FONT_CUSTOM_DECLARE
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /*USE_FONT*/
|
141
src/lv_misc/lv_font_fmt/lv_font_fmt_txt.c
Normal file
141
src/lv_misc/lv_font_fmt/lv_font_fmt_txt.c
Normal file
@ -0,0 +1,141 @@
|
||||
/**
|
||||
* @file lv_font.c
|
||||
*
|
||||
*/
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
|
||||
#include "lv_font.h"
|
||||
#include "lv_log.h"
|
||||
#include "lv_utils.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* STATIC PROTOTYPES
|
||||
**********************/
|
||||
|
||||
static int32_t lv_font_codeCompare(const void * pRef, const void * pElement);
|
||||
|
||||
/**********************
|
||||
* STATIC VARIABLES
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* GLOBAL FUNCTIONS
|
||||
**********************/
|
||||
|
||||
/**
|
||||
* Used as `get_glyph_bitmap` callback in LittelvGL's native font format if the font is uncompressed.
|
||||
* @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
|
||||
*/
|
||||
const uint8_t * lv_font_get_bitmap_format_text_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;
|
||||
|
||||
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 not returned earlier then the letter is not found in this font*/
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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`
|
||||
*/
|
||||
bool lv_font_get_glyph_dsc_format_text_plain(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter)
|
||||
{
|
||||
/*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;
|
||||
int32_t index = -1;
|
||||
/*No Unicode list -> Continuous font*/
|
||||
if(font_dsc->unicode_list == NULL) {
|
||||
index = (unicode_letter - font->unicode_first);
|
||||
}
|
||||
/*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;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**********************
|
||||
* STATIC FUNCTIONS
|
||||
**********************/
|
||||
|
||||
/** 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.
|
||||
*
|
||||
*/
|
||||
static int32_t lv_font_codeCompare(const void * pRef, const void * pElement)
|
||||
{
|
||||
return (*(uint16_t *)pRef) - (*(uint16_t *)pElement);
|
||||
}
|
193
src/lv_misc/lv_font_fmt/lv_font_fmt_txt.h
Normal file
193
src/lv_misc/lv_font_fmt/lv_font_fmt_txt.h
Normal file
@ -0,0 +1,193 @@
|
||||
/**
|
||||
* @file lv_font.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LV_FONT_H
|
||||
#define LV_FONT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
#ifdef LV_CONF_INCLUDE_SIMPLE
|
||||
#include "lv_conf.h"
|
||||
#else
|
||||
#include "../../../lv_conf.h"
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "../lv_font.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
/*Describe a glyph*/
|
||||
typedef struct
|
||||
{
|
||||
uint32_t bitmap_index : 20; /* Start index of the bitmap. A font can be max 1 MB. */
|
||||
uint32_t adv_w :12; /*The glyph needs this space. Draw the next glyph after this width. 8.4 format. */
|
||||
|
||||
uint8_t box_w; /*Width of the glyph's bounding box*/
|
||||
uint8_t box_h; /*Height of the glyph's bounding box*/
|
||||
int8_t ofs_x; /*x offset of the bounding box*/
|
||||
uint8_t ofs_y; /*y offset of the bounding box. Measured from the top of the line*/
|
||||
}lv_font_glyph_dsc_fmt_txt_t;
|
||||
|
||||
/* Map codepoints to a `glyph_dsc`s
|
||||
* Several formats are supported to optimize memory usage
|
||||
* See https://github.com/littlevgl/lv_font_conv/blob/master/doc/font_spec.md
|
||||
*/
|
||||
typedef struct {
|
||||
/* First Unicode character for this range */
|
||||
uint32_t range_start;
|
||||
|
||||
/* Number of Unicode characters related to this range.
|
||||
* Last Unicode character = range_start + range_length - 1*/
|
||||
uint16_t range_length;
|
||||
|
||||
/* First glyph ID (array index of `glyph_dsc`) for this range */
|
||||
uint16_t glyph_id_start;
|
||||
|
||||
/*
|
||||
According the specification there are 4 formats:
|
||||
https://github.com/littlevgl/lv_font_conv/blob/master/doc/font_spec.md
|
||||
|
||||
For simplicity introduce "relative code point":
|
||||
rcp = codepoint - range_start
|
||||
|
||||
and a search function:
|
||||
search a "value" in an "array" and returns the index of "value".
|
||||
|
||||
Format 0 tiny
|
||||
unicode_list == NULL && glyph_id_list == NULL
|
||||
glyph_id = glyph_id_start + rcp
|
||||
|
||||
Format 0 full
|
||||
unicode_list == NULL && glyph_id_list != NULL
|
||||
glyph_id = glyph_id_start + glyph_id_list[rcp]
|
||||
|
||||
Sparse tiny
|
||||
unicode_list != NULL && glyph_id_list == NULL
|
||||
glyph_id = glyph_id_start + search(unicode_list, rcp)
|
||||
|
||||
Sparse full
|
||||
unicode_list != NULL && glyph_id_list != NULL
|
||||
glyph_id = glyph_id_start + glyph_id_list[search(unicode_list, rcp)]
|
||||
*/
|
||||
|
||||
uint16_t * unicode_list;
|
||||
|
||||
/* if unicode_list == NULL uint8_t *
|
||||
* else uint16_t *
|
||||
*/
|
||||
const void * glyph_id_list;
|
||||
}lv_font_cmap_fmt_txt_t;
|
||||
|
||||
/*A simple mapping of kern values from pairs*/
|
||||
typedef struct {
|
||||
/*To get a kern value of two code points:
|
||||
1. Get the `glyph_id_left` and `glyph_id_right` from `lv_font_cmap_built_in_t
|
||||
2 for(i = 0; i < pair_cnt; i++)
|
||||
if(left_gylph_ids[i] == glyph_id_left &&
|
||||
right_gylph_ids[i] == glyph_id_right)
|
||||
return values[i];
|
||||
*/
|
||||
uint16_t * left_gylph_ids;
|
||||
uint16_t * right_gylph_ids;
|
||||
uint8_t * values;
|
||||
uint16_t pair_cnt;
|
||||
}lv_font_kern_pair_fmt_txt_t;
|
||||
|
||||
/*More complex but more optimal class based kern value storage*/
|
||||
typedef struct {
|
||||
/*To get a kern value of two code points:
|
||||
1. Get the `glyph_id_left` and `glyph_id_right` from `lv_font_cmap_built_in_t
|
||||
2 Get the class of the left and right glyphs as `left_class` and `right_class`
|
||||
for(i = 0; i < left_class_num; i++)
|
||||
if(left_class_mapping[i] == glyph_id_left)
|
||||
return i;
|
||||
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*/
|
||||
uint8_t left_class_cnt;
|
||||
uint8_t right_class_cnt;
|
||||
}lv_font_kern_classes_fmt_txt_t;
|
||||
|
||||
/*Describe store additional data for fonts */
|
||||
typedef struct {
|
||||
/*The bitmaps os all glyphs*/
|
||||
const uint8_t * glyph_bitmap;
|
||||
|
||||
/*Describe the glyphs*/
|
||||
const lv_font_glyph_dsc_fmt_txt_t * glyph_dsc;
|
||||
|
||||
/* Map the glyphs to Unicode characters.
|
||||
* Array of `lv_font_cmap_fmt_txt_t` variables*/
|
||||
const lv_font_cmap_fmt_txt_t * cmaps;
|
||||
|
||||
/* Sotore kerning values.
|
||||
* Can be `lv_font_kern_pair_fmt_txt_t * or `lv_font_kern_classes_fmt_txt_t *`
|
||||
* depending on `kern_classes`
|
||||
* */
|
||||
const void * kern_dsc;
|
||||
|
||||
/*Number of cmap tables*/
|
||||
uint16_t cmap_num :12;
|
||||
|
||||
/*Bit per pixel: 1, 2, 4 or 8*/
|
||||
uint16_t bpp :3;
|
||||
|
||||
/*Type of `kern_dsc`*/
|
||||
uint16_t kern_classes :1;
|
||||
}lv_font_dsc_fmt_txt_t;
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/**
|
||||
* Used as `get_glyph_bitmap` callback in LittelvGL's native font format if the font is uncompressed.
|
||||
* @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
|
||||
*/
|
||||
const uint8_t * lv_font_get_bitmap_format_text_plain(const lv_font_t * font, uint32_t letter);
|
||||
|
||||
/**
|
||||
* 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`
|
||||
*/
|
||||
bool lv_font_get_glyph_dsc_format_text_plain(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* ADD BUILT IN FONTS
|
||||
**********************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /*USE_FONT*/
|
Loading…
x
Reference in New Issue
Block a user