WhyCan Forum

本站无需注册,无需积分,无需回复可下载所有资料,如果真的喜欢小站,请您注册之后请至少回复一个帖子激活Id,谢谢支持! 站长QQ: 516333132 (挖坑网/填坑网) admin@whycan.cn

您尚未登录。

#1 2018-02-04 09:02:48

晕哥
Administrator
注册时间: 2017-09-06
累计积分: 6,123

参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

// test.c
// gcc -o test test.c `freetype-config --cflags`   `freetype-config --libs`
#include <freetype2/ft2build.h>
#include <freetype2/freetype.h>

static FT_Library library;
static FT_Face face;

int LoadChar(wchar_t cvalue, int textsize, int *x, int* bitmap_left, 
      int* bitmap_top, int* rows, int* width, unsigned char** buf)
{
	int ret = FT_Load_Char(face, cvalue, FT_LOAD_RENDER); //0=SUCCESS, otherwise=FAIL
	*x = face->glyph->advance.x;
	*bitmap_left = face->glyph->bitmap_left;
	*bitmap_top = face->glyph->bitmap_top;
	*rows = face->glyph->bitmap.rows;
	*width = face->glyph->bitmap.width;
	*buf = face->glyph->bitmap.buffer;

	printf("%s(%s)\n", __FILE__, __FUNCTION__);
	printf("-------------------------\n");
	printf("%15s = %ld\n", "advance.x", face->glyph->advance.x);
	printf("%15s = %d\n", "bitmap_left", face->glyph->bitmap_left);
	printf("%15s = %d\n", "bitmap_top", face->glyph->bitmap_top);
	printf("%15s = %d\n", "bitmap.rows", face->glyph->bitmap.rows);
	printf("%15s = %d\n", "bitmap.width", face->glyph->bitmap.width);
	printf("%15s = %ld\n", "bitmap.buffer", sizeof(face->glyph->bitmap.buffer));
	printf("%15s = %ld\n", "bitmap_buffer", strlen(face->glyph->bitmap.buffer));
	printf("-------------------------\n");
	unsigned char*  buffer; 
	buffer = face->glyph->bitmap.buffer;
	int i=0, j=0;
	int height = *rows;
	int wid = *width;

	for(j = 0; j < height; j++)
	{
		for(i = 0; i < wid ; i++)
		{
			printf("%02x ", buffer[i + wid*j]);
		}
		printf("\n");
	}

	return 1;
  }

int main(int argc, char** argv)
{
	printf("%s(%s)\n", __FILE__, __FUNCTION__);
	int bError = FT_Init_FreeType(&library);
	if(bError) return 0;
	bError = FT_New_Face(library,(char*)"[ins]/mnt/hgfs/D/Downloads/msyh.ttf[/ins]",0, &face);
	if(bError) return 0;

	int textHeight = 20;
	FT_Set_Pixel_Sizes(face, textHeight, 0);

	int left, top, rows, width, x;
	unsigned char* buf;
	wchar_t ch = 'W';
	LoadChar(ch, textHeight, &x, &left, &top, &rows, &width, &buf);
	return 0;   
}

W输出:

hexing@ubuntu:/tmp$ ./test 
test.c(main)
test.c(LoadChar)
-------------------------
      advance.x = 1280
    bitmap_left = 0
     bitmap_top = 15
    bitmap.rows = 15
   bitmap.width = 20
  bitmap.buffer = 8
  bitmap_buffer = 3
-------------------------
de ff 1f 00 00 00 00 00 29 ff ff 29 00 00 00 00 00 1e ff dd 
9a ff 5d 00 00 00 00 00 7a ff ff 7b 00 00 00 00 00 5b ff 99 
56 ff 9b 00 00 00 00 00 cc ff ff cd 00 00 00 00 00 98 ff 55 
13 fd d9 00 00 00 00 1e ff e3 e7 ff 1f 00 00 00 00 d5 fd 12 
00 cd ff 18 00 00 00 6f ff a6 af ff 71 00 00 00 13 fe cc 00 
00 89 ff 56 00 00 00 c0 ff 50 5a ff c3 00 00 00 4e ff 88 00 
00 45 ff 94 00 00 15 fc f0 07 0b f4 fd 17 00 00 8b ff 44 00 
00 08 f7 d2 00 00 63 ff 9e 00 00 a5 ff 67 00 00 c8 f7 08 00 
00 00 bc fe 12 00 b5 ff 44 00 00 4b ff b9 00 0a fa bb 00 00 
00 00 78 ff 4f 0d f8 e7 03 00 00 05 ec fa 10 42 ff 77 00 00 
00 00 34 ff 8d 58 ff 91 00 00 00 00 97 ff 5e 7e ff 33 00 00 
00 00 02 ed c7 a7 ff 38 00 00 00 00 3c ff ae b9 ec 02 00 00 
00 00 00 ab ee e9 de 00 00 00 00 00 01 e0 ea e9 aa 00 00 00 
00 00 00 67 ff ff 85 00 00 00 00 00 00 88 ff ff 66 00 00 00 
00 00 00 22 ff ff 2c 00 00 00 00 00 00 2d ff ff 22 00 00 00 

T输出:

hexing@ubuntu:/tmp$ ./test 
test.c(main)
test.c(LoadChar)
-------------------------
      advance.x = 704
    bitmap_left = 1
     bitmap_top = 15
    bitmap.rows = 15
   bitmap.width = 10
  bitmap.buffer = 8
  bitmap_buffer = 20
-------------------------
ff ff ff ff ff ff ff ff ff ff 
ff ff ff ff ff ff ff ff ff ff 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 

离线

#2 2018-02-04 09:10:09

晕哥
Administrator
注册时间: 2017-09-06
累计积分: 6,123

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

如果要想顯示漢字,改動一行即可:

wchar_t ch = L'漢';

离线

#3 2018-08-01 09:08:31

abc3240660
会员
注册时间: 2018-07-31
累计积分: 89

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

stm32上能用不?

离线

#4 2018-08-01 14:31:44

达克罗德
会员
注册时间: 2018-04-10
累计积分: 480

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

abc3240660 说:

stm32上能用不?

我见多F4的项目用了。随着现在芯片越来越强大越来越便宜,矢量字体应该会流行

离线

#5 2018-08-01 15:37:26

abc3240660
会员
注册时间: 2018-07-31
累计积分: 89

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

stm32上 字体太大时 耗时好多啊

比如正常的一个循环延时100ms用delay_ms(100),但是绘大字体后,delay_ms(90),才能让一个循环是100ms

离线

#6 2018-08-02 21:47:05

sblpp
会员
注册时间: 2018-02-14
累计积分: 44

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

谢谢分享!

离线

#7 2019-01-25 11:18:57

trigger
会员
注册时间: 2018-12-30
累计积分: 41

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

20190125111232.png

文件下载: freetype-2_9_1_20190125.7z

工程路径: freetype-2.9.1\builds\windows\vc2010\freetype.sln

可以用 VC2015 打开/编译/调试

离线

#8 2019-04-10 01:15:45

Hinsun
会员
注册时间: 2019-04-09
累计积分: 2

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

晕哥 说:
// test.c
// gcc -o test test.c `freetype-config --cflags`   `freetype-config --libs`
#include <freetype2/ft2build.h>
#include <freetype2/freetype.h>

static FT_Library library;
static FT_Face face;

int LoadChar(wchar_t cvalue, int textsize, int *x, int* bitmap_left, 
      int* bitmap_top, int* rows, int* width, unsigned char** buf)
{
	int ret = FT_Load_Char(face, cvalue, FT_LOAD_RENDER); //0=SUCCESS, otherwise=FAIL
	*x = face->glyph->advance.x;
	*bitmap_left = face->glyph->bitmap_left;
	*bitmap_top = face->glyph->bitmap_top;
	*rows = face->glyph->bitmap.rows;
	*width = face->glyph->bitmap.width;
	*buf = face->glyph->bitmap.buffer;

	printf("%s(%s)\n", __FILE__, __FUNCTION__);
	printf("-------------------------\n");
	printf("%15s = %ld\n", "advance.x", face->glyph->advance.x);
	printf("%15s = %d\n", "bitmap_left", face->glyph->bitmap_left);
	printf("%15s = %d\n", "bitmap_top", face->glyph->bitmap_top);
	printf("%15s = %d\n", "bitmap.rows", face->glyph->bitmap.rows);
	printf("%15s = %d\n", "bitmap.width", face->glyph->bitmap.width);
	printf("%15s = %ld\n", "bitmap.buffer", sizeof(face->glyph->bitmap.buffer));
	printf("%15s = %ld\n", "bitmap_buffer", strlen(face->glyph->bitmap.buffer));
	printf("-------------------------\n");
	unsigned char*  buffer; 
	buffer = face->glyph->bitmap.buffer;
	int i=0, j=0;
	int height = *rows;
	int wid = *width;

	for(j = 0; j < height; j++)
	{
		for(i = 0; i < wid ; i++)
		{
			printf("%02x ", buffer[i + wid*j]);
		}
		printf("\n");
	}

	return 1;
  }

int main(int argc, char** argv)
{
	printf("%s(%s)\n", __FILE__, __FUNCTION__);
	int bError = FT_Init_FreeType(&library);
	if(bError) return 0;
	bError = FT_New_Face(library,(char*)"[ins]/mnt/hgfs/D/Downloads/msyh.ttf[/ins]",0, &face);
	if(bError) return 0;

	int textHeight = 20;
	FT_Set_Pixel_Sizes(face, textHeight, 0);

	int left, top, rows, width, x;
	unsigned char* buf;
	wchar_t ch = 'W';
	LoadChar(ch, textHeight, &x, &left, &top, &rows, &width, &buf);
	return 0;   
}

W输出:

hexing@ubuntu:/tmp$ ./test 
test.c(main)
test.c(LoadChar)
-------------------------
      advance.x = 1280
    bitmap_left = 0
     bitmap_top = 15
    bitmap.rows = 15
   bitmap.width = 20
  bitmap.buffer = 8
  bitmap_buffer = 3
-------------------------
de ff 1f 00 00 00 00 00 29 ff ff 29 00 00 00 00 00 1e ff dd 
9a ff 5d 00 00 00 00 00 7a ff ff 7b 00 00 00 00 00 5b ff 99 
56 ff 9b 00 00 00 00 00 cc ff ff cd 00 00 00 00 00 98 ff 55 
13 fd d9 00 00 00 00 1e ff e3 e7 ff 1f 00 00 00 00 d5 fd 12 
00 cd ff 18 00 00 00 6f ff a6 af ff 71 00 00 00 13 fe cc 00 
00 89 ff 56 00 00 00 c0 ff 50 5a ff c3 00 00 00 4e ff 88 00 
00 45 ff 94 00 00 15 fc f0 07 0b f4 fd 17 00 00 8b ff 44 00 
00 08 f7 d2 00 00 63 ff 9e 00 00 a5 ff 67 00 00 c8 f7 08 00 
00 00 bc fe 12 00 b5 ff 44 00 00 4b ff b9 00 0a fa bb 00 00 
00 00 78 ff 4f 0d f8 e7 03 00 00 05 ec fa 10 42 ff 77 00 00 
00 00 34 ff 8d 58 ff 91 00 00 00 00 97 ff 5e 7e ff 33 00 00 
00 00 02 ed c7 a7 ff 38 00 00 00 00 3c ff ae b9 ec 02 00 00 
00 00 00 ab ee e9 de 00 00 00 00 00 01 e0 ea e9 aa 00 00 00 
00 00 00 67 ff ff 85 00 00 00 00 00 00 88 ff ff 66 00 00 00 
00 00 00 22 ff ff 2c 00 00 00 00 00 00 2d ff ff 22 00 00 00 

T输出:

hexing@ubuntu:/tmp$ ./test 
test.c(main)
test.c(LoadChar)
-------------------------
      advance.x = 704
    bitmap_left = 1
     bitmap_top = 15
    bitmap.rows = 15
   bitmap.width = 10
  bitmap.buffer = 8
  bitmap_buffer = 20
-------------------------
ff ff ff ff ff ff ff ff ff ff 
ff ff ff ff ff ff ff ff ff ff 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 

@晕哥,你这个demo有两个问题请教:
1、FT_Set_Pixel_Sizes(face, textHeight, 0); 这里应该是笔误哈,textHeight和0这两个参数位置要互换?
2、设置的像素高度为20px,但为什么字模点阵里面只有16行的高呢?
是不是freetype有特殊处理,只获取了字的完全高度?如果我想把转换为20px的点阵,有接口支持么?谢谢。

离线

页脚