name=FreeType version=2.11.1 author=David Turner, Robert Wilhelm, Werner Lemberg maintainer=Werner Lemberg sentence=A freely available software library to render fonts. paragraph=It is written in C, designed to be small, efficient, highly customizable, and portable while capable of producing high-quality output (glyph images) of most vector and bitmap font formats.documentation. category=Font url=https://freetype.org/ architectures=* repository=https://gitlab.freedesktop.org/freetype/freetype license=GNU
/*FreeType library*/ #define LV_USE_FREETYPE 0 #define LV_USE_FREETYPE 1 #if LV_USE_FREETYPE /*Memory used by FreeType to cache characters [bytes] (-1: no caching)*/ #define LV_FREETYPE_CACHE_SIZE (16 * 1024) #if LV_FREETYPE_CACHE_SIZE >= 0 /* 1: bitmap cache use the sbit cache, 0:bitmap cache use the image cache. */ /* sbit cache:it is much more memory efficient for small bitmaps(font size < 256) */ /* if font size >= 256, must be configured as image cache */ #define LV_FREETYPE_SBIT_CACHE 1 /* Maximum number of opened FT_Face/FT_Size objects managed by this cache instance. */ /* (0:use system defaults) */ #define LV_FREETYPE_CACHE_FT_FACES 0 #define LV_FREETYPE_CACHE_FT_SIZES 0 #endif #endif
typedefstruct { constchar * name; /* The name of the font file */ constvoid * mem; /* The pointer of the font file */ size_t mem_size; /* The size of the memory */ lv_font_t * font; /* point to lvgl font */ uint16_t height; /* font size */ uint16_t weight; /* font weight */ uint16_t style; /* font style */ } lv_ft_info_t;
然后去lv_freetype.c里添加可变字体操作代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
FT_MM_Var *amaster = NULL; FT_Error err = FT_Get_MM_Var(face, &amaster); if (err) { LV_LOG_ERROR("FT_Get_MM_Var error:%d\n", err); return err; } FT_Fixed w = dsc->weight << 16; if (w > amaster->axis->maximum) { w = amaster->axis->maximum; } err = FT_Set_Var_Design_Coordinates(face, 1, &w); if (err) { LV_LOG_ERROR("FT_Set_Var_Design_Coordinates error:%d\n", err); return err; } FT_Done_MM_Var(library, amaster);
// 为上面的style创建一个label以展示 lv_obj_t * label = lv_label_create(lv_scr_act()); // 为label添加刚才创建的style lv_obj_add_style(label, &style, 0); // 设置label的内容为Hello world\nI'm a font created with FreeType lv_label_set_text(label, "Hello world\nI'm a font created with FreeType"); // 居中label lv_obj_center(label); }
typedefstruct { constchar * name; /* The name of the font file */ lv_font_t * font; /* point to lvgl font */ uint16_t weight; /* font weight */ uint16_t height; /* font size */ uint16_t style; /* font style */ } lv_ft_info_t;
dsc_out->adv_w = sbit->xadvance; dsc_out->box_h = sbit->height; /*Height of the bitmap in [px]*/ dsc_out->box_w = sbit->width; /*Width of the bitmap in [px]*/ dsc_out->ofs_x = sbit->left; /*X offset of the bitmap in [pf]*/ dsc_out->ofs_y = sbit->top - sbit->height; /*Y offset of the bitmap measured from the as line*/ dsc_out->bpp = 8; /*Bit per pixel: 1/2/4/8*/ #else FT_Error error = FTC_ImageCache_Lookup(image_cache, &desc_type, glyph_index, &image_glyph, NULL); if(error) { LV_LOG_ERROR("ImageCache_Lookup error"); returnfalse; } if(image_glyph->format != FT_GLYPH_FORMAT_BITMAP) { LV_LOG_ERROR("Glyph_To_Bitmap error"); returnfalse; }
FT_BitmapGlyph glyph_bitmap = (FT_BitmapGlyph)image_glyph; dsc_out->adv_w = (glyph_bitmap->root.advance.x >> 16); dsc_out->box_h = glyph_bitmap->bitmap.rows; /*Height of the bitmap in [px]*/ dsc_out->box_w = glyph_bitmap->bitmap.width; /*Width of the bitmap in [px]*/ dsc_out->ofs_x = glyph_bitmap->left; /*X offset of the bitmap in [pf]*/ dsc_out->ofs_y = glyph_bitmap->top - glyph_bitmap->bitmap.rows; /*Y offset of the bitmap measured from the as line*/ dsc_out->bpp = 8; /*Bit per pixel: 1/2/4/8*/ #endif