/**1* @file lv_font.c2*3*/45/*********************6* INCLUDES7*********************/8#include <stddef.h>9#include "lv_font.h"10#include "lv_log.h"1112/*********************13* DEFINES14*********************/1516/**********************17* TYPEDEFS18**********************/1920/**********************21* STATIC PROTOTYPES22**********************/2324/**********************25* STATIC VARIABLES26**********************/2728/**********************29* GLOBAL PROTOTYPES30**********************/3132/**********************33* MACROS34**********************/3536/**********************37* GLOBAL FUNCTIONS38**********************/3940/**41* Initialize the fonts42*/43void lv_font_init(void)44{45lv_font_builtin_init();46}4748/**49* Add a font to an other to extend the character set.50* @param child the font to add51* @param parent this font will be extended. Using it later will contain the characters from `child`52*/53void lv_font_add(lv_font_t * child, lv_font_t * parent)54{55if(parent == NULL) return;5657while(parent->next_page != NULL) {58parent = parent->next_page; /*Got to the last page and add the new font there*/59}6061parent->next_page = child;6263}6465/**66* Remove a font from a character set.67* @param child the font to remove68* @param parent remove `child` from here69*/70void lv_font_remove(lv_font_t * child, lv_font_t * parent)71{72if(parent == NULL) return;73if(child == NULL) return;7475while(parent->next_page != child) {76parent = parent->next_page; /*Got to the last page and add the new font there*/77}7879parent->next_page = child->next_page;80}818283/**84* Tells if font which contains `letter` is monospace or not85* @param font_p point to font86* @param letter an UNICODE character code87* @return true: the letter is monospace; false not monospace88*/89bool lv_font_is_monospace(const lv_font_t * font_p, uint32_t letter)90{91const lv_font_t * font_i = font_p;92int16_t w;93while(font_i != NULL) {94w = font_i->get_width(font_i, letter);95if(w >= 0) {96/*Glyph found*/97if(font_i->monospace) return true;98return false;99}100101font_i = font_i->next_page;102}103104return 0;105}106107/**108* Return with the bitmap of a font.109* @param font_p pointer to a font110* @param letter an UNICODE character code111* @return pointer to the bitmap of the letter112*/113const uint8_t * lv_font_get_bitmap(const lv_font_t * font_p, uint32_t letter)114{115const lv_font_t * font_i = font_p;116while(font_i != NULL) {117const uint8_t * bitmap = font_i->get_bitmap(font_i, letter);118if(bitmap) return bitmap;119120font_i = font_i->next_page;121}122123return NULL;124}125126/**127* Get the width of a letter in a font. If `monospace` is set then return with it.128* @param font_p pointer to a font129* @param letter an UNICODE character code130* @return the width of a letter131*/132uint8_t lv_font_get_width(const lv_font_t * font_p, uint32_t letter)133{134const lv_font_t * font_i = font_p;135int16_t w;136while(font_i != NULL) {137w = font_i->get_width(font_i, letter);138if(w >= 0) {139/*Glyph found*/140uint8_t m = font_i->monospace;141if(m) w = m;142return w;143}144145font_i = font_i->next_page;146}147148return 0;149150}151152/**153* Get the width of the letter without overwriting it with the `monospace` attribute154* @param font_p pointer to a font155* @param letter an UNICODE character code156* @return the width of a letter157*/158uint8_t lv_font_get_real_width(const lv_font_t * font_p, uint32_t letter)159{160const lv_font_t * font_i = font_p;161int16_t w;162while(font_i != NULL) {163w = font_i->get_width(font_i, letter);164if(w >= 0) return w;165166font_i = font_i->next_page;167}168169return 0;170}171172/**173* Get the bit-per-pixel of font174* @param font pointer to font175* @param letter a letter from font (font extensions can have different bpp)176* @return bpp of the font (or font extension)177*/178uint8_t lv_font_get_bpp(const lv_font_t * font, uint32_t letter)179{180const lv_font_t * font_i = font;181while(font_i != NULL) {182if(letter >= font_i->unicode_first && letter <= font_i->unicode_last) {183return font_i->bpp;184}185font_i = font_i->next_page;186}187188return 0;189190}191192/**193* Generic bitmap get function used in 'font->get_bitmap' when the font contains all characters in the range194* @param font pointer to font195* @param unicode_letter an unicode letter which bitmap should be get196* @return pointer to the bitmap or NULL if not found197*/198const uint8_t * lv_font_get_bitmap_continuous(const lv_font_t * font, uint32_t unicode_letter)199{200/*Check the range*/201if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) return NULL;202203uint32_t index = (unicode_letter - font->unicode_first);204return &font->glyph_bitmap[font->glyph_dsc[index].glyph_index];205}206207/**208* Generic bitmap get function used in 'font->get_bitmap' when the font NOT contains all characters in the range (sparse)209* @param font pointer to font210* @param unicode_letter an unicode letter which bitmap should be get211* @return pointer to the bitmap or NULL if not found212*/213const uint8_t * lv_font_get_bitmap_sparse(const lv_font_t * font, uint32_t unicode_letter)214{215/*Check the range*/216if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) return NULL;217218uint32_t i;219for(i = 0; font->unicode_list[i] != 0; i++) {220if(font->unicode_list[i] == unicode_letter) {221return &font->glyph_bitmap[font->glyph_dsc[i].glyph_index];222}223}224225return NULL;226}227228/**229* Generic glyph width get function used in 'font->get_width' when the font contains all characters in the range230* @param font pointer to font231* @param unicode_letter an unicode letter which width should be get232* @return width of the gylph or -1 if not found233*/234int16_t lv_font_get_width_continuous(const lv_font_t * font, uint32_t unicode_letter)235{236/*Check the range*/237if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) {238return -1;239}240241uint32_t index = (unicode_letter - font->unicode_first);242return font->glyph_dsc[index].w_px;243}244245/**246* Generic glyph width get function used in 'font->get_bitmap' when the font NOT contains all characters in the range (sparse)247* @param font pointer to font248* @param unicode_letter an unicode letter which width should be get249* @return width of the glyph or -1 if not found250*/251int16_t lv_font_get_width_sparse(const lv_font_t * font, uint32_t unicode_letter)252{253/*Check the range*/254if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) return -1;255256uint32_t i;257for(i = 0; font->unicode_list[i] != 0; i++) {258if(font->unicode_list[i] == unicode_letter) {259return font->glyph_dsc[i].w_px;260}261}262263return -1;264}265266/**********************267* STATIC FUNCTIONS268**********************/269270271