您尚未登录。

楼主 #1 2018-08-11 14:06:10

晕哥
管理员
所在地: wechat: whycan_cn
注册时间: 2017-09-06
已发帖子: 9,478
积分: 9207

@assert 分享的在littlevgl 调用freetye显示文字, 而不用通过字模软件生成

一个初始化
一个获取 bitmap 和 width 和 rows
只要这三个函数就好了
同样弄一个结构体
里面 h_px 这个成员要实时去设置 因为 freetype 里面有获取的

哪位有空可以照着这种套路试一试,
做出来分享一下.





离线

楼主 #3 2018-08-11 14:47:40

晕哥
管理员
所在地: wechat: whycan_cn
注册时间: 2017-09-06
已发帖子: 9,478
积分: 9207

Re: @assert 分享的在littlevgl 调用freetye显示文字, 而不用通过字模软件生成

他做的Linux下面的, 裸机的套路应该是一样的。
但是裸机这么做必要性不太大了。





离线

楼主 #5 2018-08-11 20:36:01

晕哥
管理员
所在地: wechat: whycan_cn
注册时间: 2017-09-06
已发帖子: 9,478
积分: 9207

Re: @assert 分享的在littlevgl 调用freetye显示文字, 而不用通过字模软件生成

john78 wrote:

木有看懂,也有汉字显示移植计划。不用小字库,至少HzK16,目标支持矢量字体

使用libfreetype这种矢量字体(ttf)解析库,
直接解析ttf文件,
可以生成任意大小的字,
而不需要预先生成固定大小的字模文件。

https://whycan.cn/t_741.html





离线

楼主 #8 2018-10-27 08:06:06

晕哥
管理员
所在地: wechat: whycan_cn
注册时间: 2017-09-06
已发帖子: 9,478
积分: 9207

Re: @assert 分享的在littlevgl 调用freetye显示文字, 而不用通过字模软件生成

tango_zhu wrote:

mark 下 这个是直接解析ttf文件吗

对的,对的。





离线

楼主 #12 2018-12-25 08:55:03

晕哥
管理员
所在地: wechat: whycan_cn
注册时间: 2017-09-06
已发帖子: 9,478
积分: 9207

Re: @assert 分享的在littlevgl 调用freetye显示文字, 而不用通过字模软件生成

http://bbs.21ic.com/icview-586134-1-1.html

今天发现了Freetype与文件系统相关的接口定义
在ftstdlib.h 这个头中

#include <string.h>

#define ft_memchr memchr
#define ft_memcmp memcmp
#define ft_memcpy memcpy
#define ft_memmove memmove
#define ft_memset memset
#define ft_strcat strcat
#define ft_strcmp strcmp
#define ft_strcpy strcpy
#define ft_strlen strlen
#define ft_strncmp strncmp
#define ft_strncpy strncpy
#define ft_strrchr strrchr
#define ft_strstr strstr
这些是内存管理

#include <stdio.h>

#define FT_FILE FILE
#define ft_fclose fclose
#define ft_fopen fopen
#define ft_fread fread
#define ft_fseek fseek
#define ft_ftell ftell
#define ft_sprintf sprintf
这些是输入输出的接口

个人感觉,需要移植的接口就是以上内容,现在的问题是要如何将那些源码加载到工程中
尽管我在Docs文件加下打开了一个叫做INSTALL.any的文件
里面提示了如何添加源码,但是我按照其操作依然错误很多,有些想放弃了。

  • DO NOT COMPILE ALL C FILES! Rather, compile the following ones.

-- base components (required)

src/base/ftsystem.c
src/base/ftinit.c
src/base/ftdebug.c

src/base/ftbase.c

src/base/ftbbox.c -- recommended, see <freetype/ftbbox.h>
src/base/ftglyph.c -- recommended, see <freetype/ftglyph.h>

src/base/ftbdf.c -- optional, see <freetype/ftbdf.h>
src/base/ftbitmap.c -- optional, see <freetype/ftbitmap.h>
src/base/ftcid.c -- optional, see <freetype/ftcid.h>
src/base/ftfstype.c -- optional
src/base/ftgasp.c -- optional, see <freetype/ftgasp.h>
src/base/ftgxval.c -- optional, see <freetype/ftgxval.h>
src/base/ftlcdfil.c -- optional, see <freetype/ftlcdfil.h>
src/base/ftmm.c -- optional, see <freetype/ftmm.h>
src/base/ftotval.c -- optional, see <freetype/ftotval.h>
src/base/ftpatent.c -- optional
src/base/ftpfr.c -- optional, see <freetype/ftpfr.h>
src/base/ftstroke.c -- optional, see <freetype/ftstroke.h>
src/base/ftsynth.c -- optional, see <freetype/ftsynth.h>
src/base/fttype1.c -- optional, see <freetype/t1tables.h>
src/base/ftwinfnt.c -- optional, see <freetype/ftwinfnt.h>
src/base/ftxf86.c -- optional, see <freetype/ftxf86.h>

src/base/ftmac.c -- only on the Macintosh

-- font drivers (optional; at least one is needed)

src/bdf/bdf.c -- BDF font driver
src/cff/cff.c -- CFF/OpenType font driver
src/cid/type1cid.c -- Type 1 CID-keyed font driver
src/pcf/pcf.c -- PCF font driver
src/pfr/pfr.c -- PFR/TrueDoc font driver
src/sfnt/sfnt.c -- SFNT files support
(TrueType & OpenType)
src/truetype/truetype.c -- TrueType font driver
src/type1/type1.c -- Type 1 font driver
src/type42/type42.c -- Type 42 font driver
src/winfonts/winfnt.c -- Windows FONT / FNT font driver

-- rasterizers (optional; at least one is needed for vector
formats)

src/raster/raster.c -- monochrome rasterizer
src/smooth/smooth.c -- anti-aliasing rasterizer

-- auxiliary modules (optional)

src/autofit/autofit.c -- auto hinting module
src/cache/ftcache.c -- cache sub-system (in beta)
src/gzip/ftgzip.c -- support for compressed fonts (.gz)
src/lzw/ftlzw.c -- support for compressed fonts (.Z)
src/gxvalid/gxvalid.c -- TrueTypeGX/AAT table validation
src/otvalid/otvalid.c -- OpenType table validation
src/psaux/psaux.c -- PostScript Type 1 parsing
src/pshinter/pshinter.c -- PS hinting module
src/psnames/psnames.c -- PostScript glyph names support

Notes:

ftcache.c' needs ftglyph.c'
ftfstype.c' needs fttype1.c'
ftglyph.c' needs ftbitmap.c'
ftstroke.c' needs ftglyph.c'
ftsynth.c' needs ftbitmap.c'

cff.c' needs sfnt.c', pshinter.c', and psnames.c'
truetype.c' needs sfnt.c' and `psnames.c'
type1.c' needs psaux.c' pshinter.c', and psnames.c'
type1cid.c' needs psaux.c', pshinter.c', and psnames.c'
type42.c' needs truetype.c'

感觉把下面这些接口实现了就可以操作 freetype了, 21ic 上面有朋友把fatfs弄上去整合就可以用了:

#define ft_fclose fclose
#define ft_fopen fopen
#define ft_fread fread
#define ft_fseek fseek
#define ft_ftell ftell
#define ft_sprintf sprintf
这些是输入输出的接口

14楼:

经过三个晚上的努力终于在stm32+ucgui+sdio上成功移植FreeType2,下面是几张照片,字库是从电脑上拷贝的SIMFANG.TTF
文件系统用的是Fatfs,不过FreeType2需要开辟很大的堆空间,目前还没找到好的优化方法,移植了个FT2,ucgui我只分配了5K的动态内存,

还带旋转:





离线

楼主 #15 2019-01-14 21:57:26

晕哥
管理员
所在地: wechat: whycan_cn
注册时间: 2017-09-06
已发帖子: 9,478
积分: 9207

Re: @assert 分享的在littlevgl 调用freetye显示文字, 而不用通过字模软件生成

/**
 * @file lv_freetype.c
 * 
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_freetype.h"
#include "lv_font.h"
#include <wchar.h>
#include <pthread.h>

static pthread_mutex_t ft_mutex = PTHREAD_MUTEX_INITIALIZER;

/**
 * Freetype2 set fonts size
 */
uint8_t lv_font_size(uint8_t sz)
{
	static uint8_t size=0;
	
	if( 0 == sz || size == sz ) {
		// printf("[debug] size = %d\n", size);
		return size;
	}
	pthread_mutex_lock( &ft_mutex);
	size = sz;
	
	FT_Set_Pixel_Sizes( lv_font_freetype2.handle.face, size, 0 );
	pthread_mutex_unlock( &ft_mutex);
	// printf("[debug] size = %d\n", size);
	
	return size;
}
/**
 * Initialize the built-in fonts
 */
void lv_font_init_freetype2(char *fontname)
{
	int error;
	if( fontname == NULL )
		return ;
	pthread_mutex_lock( &ft_mutex);
	lv_font_freetype2.handle.ft_path = fontname;
	printf("%s < %s\n", __func__, lv_font_freetype2.handle.ft_path);
	error = FT_Init_FreeType( &lv_font_freetype2.handle.library ); /* initialize library */
	
	error = FT_New_Face( lv_font_freetype2.handle.library, lv_font_freetype2.handle.ft_path, 0, &lv_font_freetype2.handle.face );

	pthread_mutex_unlock( &ft_mutex);
	lv_font_size(24);
}

/**
 * Generic bitmap get function used in 'font->get_bitmap' when the font contains all characters in the range
 * @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
 */
static uint32_t last_letter = 0;
const uint8_t * lv_font_get_bitmap_freetype2(const lv_font_t * font, uint32_t unicode_letter)
{
	pthread_mutex_lock( &ft_mutex);
	FT_GlyphSlot  slot;
	int error;
	slot = lv_font_freetype2.handle.face->glyph;
	if( last_letter == unicode_letter ) {
		lv_font_freetype2.handle.top =  slot->bitmap_top;
		pthread_mutex_unlock( &ft_mutex);
		// printf("%x [ %c%c%c ] < top %d left %d row %d col %d >\n", unicode_letter, unicode_letter&0xff, unicode_letter>>8, unicode_letter>>16, slot->bitmap_top, slot->bitmap_left, slot->bitmap.rows, slot->bitmap.width);
		return slot->bitmap.buffer;
	}
	last_letter = unicode_letter;
	error = FT_Load_Char( lv_font_freetype2.handle.face, unicode_letter, FT_LOAD_RENDER );
	if (error)
	{
		printf("FT_Load_Char error\n");
		pthread_mutex_unlock( &ft_mutex);
		return NULL;
	}
	lv_font_freetype2.handle.top =  slot->bitmap_top;

	pthread_mutex_unlock( &ft_mutex);
    return slot->bitmap.buffer;
}

/**
 * Generic glyph width get function used in 'font->get_width' when the font contains all characters in the range
 * @param font pointer to font
 * @param unicode_letter an unicode letter which width should be get
 * @return width of the gylph or -1 if not found
 */
const int16_t lv_font_get_width_freetype2(const lv_font_t * font, uint32_t unicode_letter)
{
	pthread_mutex_lock( &ft_mutex);
	FT_GlyphSlot  slot;
	int error;
	slot = lv_font_freetype2.handle.face->glyph;
	if( last_letter == unicode_letter ) {
		pthread_mutex_unlock( &ft_mutex);
		if( slot->bitmap.width == 0 ) {
			if( unicode_letter == 0x09 )
				return lv_font_size(0)*2;
			else
				return lv_font_size(0)/2;
		}
		return slot->bitmap.width;
	}
	last_letter = unicode_letter;
	error = FT_Load_Char( lv_font_freetype2.handle.face, unicode_letter, FT_LOAD_RENDER );
	if (error)
	{
		printf("FT_Load_Char error\n");
		pthread_mutex_unlock( &ft_mutex);
		return 0;
	}

	// for get rows
	lv_font_freetype2.h_px = lv_font_size(0); // slot->bitmap.rows;
	lv_font_freetype2.bpp = 8;
	
	pthread_mutex_unlock( &ft_mutex);
	if( slot->bitmap.width == 0 ) {
		if( unicode_letter == 0x09 )
			return lv_font_size(0)*2;
		else
			return lv_font_size(0)/2;
	}
    return slot->bitmap.width;
}

lv_font_t lv_font_freetype2 = {	
    .unicode_first = 0,    /*First Unicode letter in this font*/
    .unicode_last = 0,    /*Last Unicode letter in this font*/
    .h_px = 0,             /*Font height in pixels*/
    .glyph_bitmap = NULL, /*Bitmap of glyphs*/
    .glyph_dsc = NULL,       /*Description of glyphs*/
    .unicode_list = NULL,   /*Every character in the font from 'unicode_first' to 'unicode_last'*/
    .get_bitmap = lv_font_get_bitmap_freetype2,    /*Function pointer to get glyph's bitmap*/
    .get_width = lv_font_get_width_freetype2,  /*Function pointer to get glyph's width*/
    .bpp = 8,               /*Bit per pixel*/
    .type = FONT_TYPE_BUILT_FREETYPE2,
    .handle.ft_size = lv_font_size,
    .next_page = NULL,      /*Pointer to a font extension*/
};

以上是 @assert 发给我的release_static_freetype.c 代码.





离线

楼主 #22 2019-01-17 17:25:24

晕哥
管理员
所在地: wechat: whycan_cn
注册时间: 2017-09-06
已发帖子: 9,478
积分: 9207

Re: @assert 分享的在littlevgl 调用freetye显示文字, 而不用通过字模软件生成

yywyai wrote:

static const uint8_t*
lv_font_get_bitmap_freetype(const struct _lv_font_struct *font, uint32_t unicode_letter)
{
return bitmap;
}

static int16_t
lv_font_get_width_freetype(const struct _lv_font_struct *font, uint32_t unicode_letter)
{
/Check the range/
if(unicode_letter < font->unicode_first || unicode_letter > font->unicode_last) return -1;

FT_Error err = FT_Load_Char(face, unicode_letter, FT_LOAD_RENDER);
if (err == 0) {
lv_font_freetype.bpp = 8;
lv_font_freetype.bitmap_dx = face->glyph->bitmap_left;
lv_font_freetype.bitmap_dy = lv_font_freetype.monospace - face->glyph->bitmap_top;
lv_font_freetype.bitmap_row = face->glyph->bitmap.rows;
bitmap = face->glyph->bitmap.buffer;

return face->glyph->bitmap.width;
}

bitmap = NULL;
return -1;
}

代码很简单的,但是修改了部分的LV代码

非常感谢分享!





离线

楼主 #34 2019-05-25 20:45:34

晕哥
管理员
所在地: wechat: whycan_cn
注册时间: 2017-09-06
已发帖子: 9,478
积分: 9207

Re: @assert 分享的在littlevgl 调用freetye显示文字, 而不用通过字模软件生成

oldersu wrote:

晕哥,移植这个freetype的源码能提供下吗?最近也在移植,没有思路

https://whycan.cn/t_741.html
https://whycan.cn/t_2475.html





离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn


东莞哇酷科技有限公司开发