Freetype 的安装与使用

4 交叉编译程序

4.1 程序运行的一些基础知识

  1. 编译程序时去哪找头文件?
    系统目录:就是交叉编译工具链里的某个 include 目录;
    也可以自己指定:编译时用 “ -I dir ” 选项指定。

  2. 链接时去哪找库文件?
    系统目录:就是交叉编译工具链里的某个lib目录;
    也可以自己指定:链接时用 “ -L dir ”选项指定。

  3. 运行时去哪找库文件?
    系统目录:就是板子上的 /lib、/usr/lib 目录;
    也可以自己指定:运行程序用环境变量 LD_LIBRARY_PATH 指定。

  4. 运行时不需要头文件,所以头文件不用放到板子上

  5. 这样确定目录

    echo 'main(){}'| arm-buildroot-linux-gnueabihf-gcc -E -v -

    它会列出头文件目录、库目录(LIBRARY_PATH)。

4.2 给 IMX6ULL 交叉编译 freetype

手动编译

freetype 依赖于 libpng,libpng 又依赖于 zlib,所以我们应该:先编译安装 zlib,再编译安装 libpng,最后编译安装 freetype。

  1. 确定头文件、库文件在工具链中的目录

    echo 'main(){}'| arm-linux-gnueabihf-gcc -E -v -
    在这里插入图片描述

  2. 交叉编译、安装 zlib

    tar xzf zlib-1.2.11.tar.gz
    cd zlib-1.2.11
    export CC=arm-linux-gnueabihf-gcc  
    ./configure --prefix=$PWD/tmp #配置生成 makefile,将生成的文件保存到 tmp 中
    make
    make install#安装 zlib,生成的库文件等放到 tmp
    cd  tmp
    #然后将 include 下的文件复制到交叉编译工具的 include 中
    cp  include/*  -rf  /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux- \
    	gnueabihf/bin/../arm-linux-gnueabihf/libc/usr/include
    #然后将 lib 下的文件复制到交叉编译工具的 lib 中
    cp  lib/*  -drf  /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux- \
    	gnueabihf/bin/../arm-linux-gnueabihf/libc/usr/lib
    
  3. 交叉编译、安装 libpng

    tar  xJf  libpng-1.6.37.tar.xz
    cd  libpng-1.6.37
    ./configure  --host=arm-linux-gnueabihf   --prefix=$PWD/tmp
    make
    make install
    cd  tmp
    #然后将 include 下的文件复制到交叉编译工具的 include 中
    cp  include/*  -rf  /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux- \
    	gnueabihf/bin/../arm-linux-gnueabihf/libc/usr/include
    #然后将 lib 下的文件复制到交叉编译工具的 lib 中
    cp  lib/*  -drf  /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux- \
    	gnueabihf/bin/../arm-linux-gnueabihf/libc/usr/lib
    #然后将 bin 下的文件复制到交叉编译工具的 bin 中
    
    
  4. 交叉编译、安装 freetype

    tar  xJf  freetype-2.10.2.tar.xz
    cd  freetype-2.10.2
    ./configure  --host=arm-linux-gnueabihf   --prefix=$PWD/tmp
    make
    make install
    cd  tmp
    #然后将 include 下的文件复制到交叉编译工具的 include 中
    cp  include/*  -rf  /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux- \
    	gnueabihf/bin/../arm-linux-gnueabihf/libc/usr/include
    #然后将 lib 下的文件复制到交叉编译工具的 lib 中
    cp  lib/*  -drf  /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux- \
    	gnueabihf/bin/../arm-linux-gnueabihf/libc/usr/lib
    
  5. 没有安装 python 的话,提示出错,安装 python ,重新 配置就好

使用 buildroot 编译

  1. 设置好编译工具链

  2. 在 buildroot 顶层目录输入 make menuconfig 命令,进入配置界面,配置如下:

    Target packages
    	->Libraries
    		->Graphics
    			->[*] freetype
    
  3. sudo make

  4. 将编译出来的库文件拷贝到交叉编译工具链的对应目录下

    1. 在 buildroot-2019.02.6 目录搜索 freetype 相关的头文件 find -name "freetype*"

在这里插入图片描述

2. 查看 freetype2 还有那些文件:`ls ./output/host/arm-buildroot-linux-gnueabi/sysroot/usr/include/freetype2/*`

在这里插入图片描述

3. 在 buildroot-2019.02.6 目录搜索 freetype 相关的动态库文件`find -name "*freetype*so*"`

在这里插入图片描述

4. 参考手动编译,将这些文件全部复制到对应的文件夹

5. 我们尝试重新编译程序,出现了如下错误:`warning: libz.so.1`,导致编译失败:

    `/tools/arm-linux-gcc-4.4.3/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.3/../../../../arm-none-linux-gnueabi/bin/ld: warning: libz.so.1, needed by /tools/arm-linux-gcc-4.4.3/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.3/../../../../arm-none-linux-gnueabi/lib/libfreetype.so, not found (try using -rpath or -rpath-link)`

    使用 `flie` 命令查看一下该文件,发现这只是一个链接文件,真正的库文件是 `libz.so.1.2.11`

在这里插入图片描述

    因为我们需要把这两个文件也拷贝到交叉编译器的 lib 目录:
  1. buildroot-2019.02.6/output/images 目录下的 rootfs.tar 拷贝到 nfs 网络文件系统

  2. 解压后挂载根文件系统tar -xf rootfs.tar

  3. 挂载新的文件系统,文件系统挂成功后,搜索 freetype 相关的文件,结果如下图所示:

在这里插入图片描述

5 使用 freetype 显示单个文字

5.1 矢量字体引入

使用点阵字库显示英文字母、汉字时,大小固定,如果放大缩小则会模糊甚至有锯齿出现,为了解决这个问题,引用矢量字体。

矢量字体形成分三步:

  1. 确定关键点,
  2. 使用数学曲线(贝塞尔曲线)连接头键点,
  3. 填充闭合区线内部空间。

什么是关键点?以字母“A”为例,它的的关键点如下图中的黄色所示。

在这里插入图片描述

再用数学曲线(比如贝塞尔曲线)将关键点都连接起来,得到一系列的封闭的曲线,如下图所示:

在这里插入图片描述

最后把封闭空间填满颜色,就显示出一个A字母,如下图所示:

在这里插入图片描述

如果需要放大或者缩小字体,关键点的相对位置是不变的,只要数学曲线平滑,字体就不会变形。

5.2 Freetype 介绍

Freetype 是开源的字体引擎库,它提供统一的接口来访问多种字体格式文件,从而实现矢量字体显示。我们只需要移植这个字体引擎,调用对应的 API 接口,提供字体文件,就可以让 freetype 库帮我们取出关键点、实现闭合曲线,填充颜色,达到显示矢量字体的目的。

关键点 (glyph) 存在字体文件中,Windows 使用的字体文件在 c:\Windows\Fonts 目录下,扩展名为 TTF 的都是矢量字库,本次使用实验使用的是新宋字体 simsun.ttc。

给定一个字符,怎么在字体文件中找到它的关键点?

首先要确定该字符的编码值:比如 ASCII 码、GB2312 码、UNICODE 码。如果字体文件支持某种编码格式 (charset),就可以使用这类编码值去找到该字符的关键点 (glyph)。有些字体文件支持多种编码格式(charset),这在文件中被称为 charmaps (注意:这个单词是复数,意味着可能支持多种 charset)。

以 simsun.ttc 为值,该字体文件的格如下:头部含有 charmaps,可以使用某种编码值去 charmaps 中找到它对应的关键点。下图中的“A、B、中、国、韦”等只是 glyph 的示意图,表示关键点。

在这里插入图片描述

Charmaps表示字符映射表,字体文件可能支持哪一些编码,GB2312、UNICODE、BIG5或其他。如果字体文件支持该编码,使用编码值通过 charmap 就可以找到对应的glyph,一般而言都支持 UNICODE 码。

有了以上基础,一个文字的显示过程可以概括如下:

  1. 给定一个字符可以确定它的编码值 (ASCII、UNICODE、GB2312);
  2. 设置字体大小;
  3. 根据编码值,从文件头部中通过charmap找到对应的关键点 (glyph),它会根据字体大小调整关键点;
  4. 把关键点转换为位图点阵;
  5. 在 LCD 上显示出来

从https://www.freetype.org/可以下载到“freetype-doc-2.10.2.tar.xz”,下图中的文件就是官方文档:

在这里插入图片描述

参照上图中 step1,step2,step3 里的内容,可以学习如何使用 freetype 库,总结出下列步骤:

  1. 初始化:FT_InitFreetype
  2. 加载(打开)字体 Face:FT_New_Face
  3. 设置字体大小:FT_Set_Char_Sizes 或 FT_Set_Pixel_Sizes
  4. 选择 charmap:FT_Select_Charmap
  5. 根据编码值 charcode 找到 glyph_index:glyph_index = FT_Get_Char_Index(face,charcode)
  6. 根据 glyph_index 取出 glyph:FT_Load_Glyph(face,glyph_index)
  7. 转为位图:FT_Render_Glyph
  8. 移动或旋转:FT_Set_Transform 最后显示出来。

上面的⑤⑥⑦可以使用一个函数代替:FT_Load_Char(face, charcode, FT_LOAD_RENDER),它就可以得到位图。

5.3 在 LCD 上显示一个矢量字体

将 freetype 解压后复制 source insight 的 linux 源代码中,然后添加文件,文件树,同步代码,写代码更方便。

5.3.0 freetype 的几个重要数据结构

  1. FT_Library

    对应 freetype 库,使用 freetype 之前要先调用以下代码:

    FT_Library  library; /* 对应freetype库 */
    error = FT_Init_FreeType( &library ); /* 初始化freetype库 */
    
  2. FT_Face

    它对应一个矢量字体文件,在源码中使用FT_New_Face函数打开字体文件后,就可以得到一个face。

    为什么称之为face?

    估计是文字都是写在二维平面上的吧,正对着人脸?不用管原因了,总之认为它对应一个字体文件就可以。

    代码如下:

    error = FT_New_Face(library, font_file, 0, &face ); /* 加载字体文件 */

  3. FT_GlyphSlot

    插槽?用来保存字符的处理结果:比如转换后的 glyph、位图,如下图:

在这里插入图片描述

一个 face 中有很多字符,生成一个字符的点阵位图时,位图保存在哪里?保存在插槽中:face->glyph。

生成第1个字符位图时,它保存在 face->glyph 中;生成第2个字符位图时,也会保存在 face->glyph 中,会覆盖第1个字符的位图。

代码如下:
  1. FT_Glyph

    字体文件中保存有字符的原始关键点信息,使用 freetype 的函数可以放大、缩小、旋转,这些新的关键点保存在插槽中(注意:位图也是保存在插槽中)。

    新的关键点使用 FT_Glyph 来表示,可以使用这样的代码从 slot 中获得 glyph:

    error = FT_Get_Glyph(slot , &glyph);

  2. FT_BBox

    FT_BBox 结构体定义如下,它表示一个字符的外框,即新 glyph 的外框:

在这里插入图片描述

可以使用以下代码从 glyph 中获得这些信息:

`FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox );`

针对上述流程,示例代码如下:

在这里插入图片描述

5.3.1 使用 wchar_t 获得字符的 UNICODE 值

要显示一个字符,首先要确定它的编码值。常用的是 UNICODE 编码,在程序里使用这样的语句定义字符串时,str 中保存的要么是 GB2312 编码值,要么是 UTF-8 格式的编码值,即使编译时使用“-fexec-charset=UTF-8”,str 中保存的也不是直接能使用的 UNICODE 值:

char *str = “中”;

如果想在代码中能直接使用 UNICODE 值,需要使用 wchar_t,宽字符,示例代码如下:

#include <stdio.h>
#include <string.h>
#include <wchar.h>

int main( int argc, char** argv)
{
	wchar_t *chinese_str = L"��gif";
	unsigned int *p = (wchar_t *)chinese_str;
	int i;

	printf("sizeof(wchar_t) = %d, str's Uniocde: \n", (int)sizeof(wchar_t));
	for (i = 0; i < wcslen(chinese_str); i++)
	{
		printf("0x%x ", p[i]);
	}
	printf("\n");

	return 0;
}

以 UTF-8 格式保存 test_wchar.c,编译、测试命令如下:

gcc -o test_wchar test_wchar.c
./test_wchar
#输出
sizeof(wchar_t) = 4, str's Uniocde:
0x4e2d 0x67 0x69 0x66

注意:如果test_wchar.c是以ANSI(GB2312)格式保存,那么需要使用以下命令来编译:

gcc -finput-charset=GB2312 -fexec-charset=UTF-8 -o test_wchar test_wchar.c

5.3.2 使用 freetype

具体流程:

  1. 初始化 freetype 库,FT_Init_FreeType( &library )

  2. 加载字体文件,保存在 &face 中, FT_New_Face( library, argv[1], 0, &face )

  3. 从 face 中获得 FT_GlyphSlot,后面的代码中文字的位图就是保存在 FT_GlyphSlot 里, slot = face->glyph

  4. 设置字体大小, FT_Set_Pixel_Sizes(face, font_size, 0)

  5. 根据编码值得到位图

    使用FT_Load_Char 函数,就可以实现这3个功能:

    1. 根据编码值获得glyph_index:FT_Get_Char_Index
    2. 根据glyph_idex取出glyph:FT_Load_Glyph
    3. 渲染出位图:FT_Render_Glyph

    FT_Load_Char( face, chinese_str[0], FT_LOAD_RENDER )

  6. 在屏幕上显示位图

    参考example1.c的代码

在这里插入图片描述

 `draw_bitmap( &slot->bitmap, var.xres/2, var.yres/2)`

draw_bitmap 函数代码如下,由于位图中每一个像素用一个字节来表示,在0x00RRGGBB的颜色格式中它只能表示蓝色,所以在LCD上显示出来的文字是蓝色的:

一些头文件和定义
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <wchar.h>
#include <sys/ioctl.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H

int fd_fb;
struct fb_var_screeninfo var;	/* Current var */
int screen_size;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;
描点函数
/**********************************************************************
 * 函数名称: lcd_put_pixel
 * 功能描述: 在LCD指定位置上输出指定颜色(描点)
 * 输入参数: x坐标,y坐标,颜色
 ***********************************************************************/ 
void lcd_put_pixel(int x, int y, unsigned int color)
{
	unsigned char *pen_8 = fbmem+y*line_width+x*pixel_width;
	unsigned short *pen_16;	
	unsigned int *pen_32;	

	unsigned int red, green, blue;	

	pen_16 = (unsigned short *)pen_8;
	pen_32 = (unsigned int *)pen_8;

	switch (var.bits_per_pixel)
	{
		case 8:
		{
			*pen_8 = color;
			break;
		}
		case 16:
		{
			/* 565 */
			red   = (color >> 16) & 0xff;
			green = (color >> 8) & 0xff;
			blue  = (color >> 0) & 0xff;
			color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
			*pen_16 = color;
			break;
		}
		case 32:
		{
			*pen_32 = color;
			break;
		}
		default:
		{
			printf("can't surport %dbpp\n", var.bits_per_pixel);
			break;
		}
	}
}
显示汉字函数
/* Replace this function with something useful. */
/**********************************************************************
 * 函数名称: draw_bitmap
 * 功能描述: 根据bitmap位图,在LCD指定位置显示汉字
 * 输入参数: x坐标,y坐标,位图指针
 ***********************************************************************/ 
void
draw_bitmap( FT_Bitmap*  bitmap,
             FT_Int      x,
             FT_Int      y)
{
  FT_Int  i, j, p, q;
  FT_Int  x_max = x + bitmap->width;
  FT_Int  y_max = y + bitmap->rows;


  /* for simplicity, we assume that `bitmap->pixel_mode' */
  /* is `FT_PIXEL_MODE_GRAY' (i.e., not a bitmap font)   */

  for ( i = x, p = 0; i < x_max; i++, p++ )
  {
    for ( j = y, q = 0; j < y_max; j++, q++ )
    {
      if ( i < 0      || j < 0       ||
           i >= WIDTH || j >= HEIGHT )
        continue;

      //image[j][i] |= bitmap->buffer[q * bitmap->width + p];
        lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]);
    }
  }
}
main
int main(int argc, char **argv)
{
	wchar_t *chinese_str = L"繁";

	FT_Library	  library;
	FT_Face 	  face;
	int error;
    FT_Vector     pen;
	FT_GlyphSlot  slot;
	int font_size = 24;

	if (argc < 2)
	{
		printf("Usage : %s <font_file> [font_size]\n", argv[0]);
		return -1;
	}

	if (argc == 3)
		font_size = strtoul(argv[2], NULL, 0);
		
	fd_fb = open("/dev/fb0", O_RDWR);
	if (fd_fb < 0)
	{
		printf("can't open /dev/fb0\n");
		return -1;
	}

	if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
	{
		printf("can't get var\n");
		return -1;
	}

	line_width  = var.xres * var.bits_per_pixel / 8;
	pixel_width = var.bits_per_pixel / 8;
	screen_size = var.xres * var.yres * var.bits_per_pixel / 8;
	fbmem = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
	if (fbmem == (unsigned char *)-1)
	{
		printf("can't mmap\n");
		return -1;
	}

	/* 清屏: 全部设为黑色 */
	memset(fbmem, 0, screen_size);

	/* 显示矢量字体 */
	error = FT_Init_FreeType( &library );			   /* initialize library */
	/* error handling omitted */
	
	error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */
	/* error handling omitted */	

    /* cmap selection omitted;                                        */
    /* for simplicity we assume that the font contains a Unicode cmap */
    
	slot = face->glyph;

	FT_Set_Pixel_Sizes(face, font_size, 0);
//因为不涉及旋转和多个文字一起显示,所以不需要 FT_Set_Transform
	/* 确定座标:
	 */
	//pen.x = 0;
	//pen.y = 0;

    /* set transformation */
    //FT_Set_Transform( face, 0, &pen);

    /* load glyph image into the slot (erase previous one) */
    error = FT_Load_Char( face, chinese_str[0], FT_LOAD_RENDER );
	if (error)
	{
		printf("FT_Load_Char error\n");
		return -1;
	}
	/* now, draw to our target surface (convert position) */
    draw_bitmap( &slot->bitmap,
                 var.xres/2,
                 var.yres/2);
   	FT_Done_Face    ( face );
   	FT_Done_FreeType( library );
	return 0;	
}

5.3.3 上机

将编译好的 freetype_show_font 文件与 simsun.ttc 字体文件拷贝至开发板,这2个文件放在同一个目录下,然后执行以下命令。

./freetype_show_font  ./simsun.ttc   300

5.3.4 在 LCD 上令矢量字体旋转某个角度

流程:

  1. 定义2个变量:角度、矩阵,如下:

    FT_Matrix matrix;		/* transformation matrix */
    double angle;
    
  2. 设置角度值:

    angle = ( 25.0 / 360 ) * 3.14159 * 2; /* use 25 degrees */

  3. 设置矩阵、交形、加载位图:

在这里插入图片描述

6 使用 freetype 显示一行文字

6.1 笛卡尔坐标系

在LCD的坐标系中,原点在屏幕的左上角。对于笛卡尔坐标系,原点在左下角。freetype使用笛卡尔坐标系,在显示时需要转换为LCD坐标系。

从下图可知,X方向坐标值是一样的。在Y方向坐标值需要换算,假设LCD的高度是V。在LCD坐标系中坐标是(x, y),那么它在笛卡尔坐标系中的坐标值为(x, V-y)。反过来也是一样的,在笛卡尔坐标系中坐标是(x, y),那么它在LCD坐标系中坐标值为(x, V-y)。

在这里插入图片描述

6.2 每个字符的大小可能不同

在使用FT_Set_Pixel_Sizes函数设置字体大小时,这只是“期望值”。比如“百问网www.100ask.net”,如果把“.”显示得跟其他汉字一样大,不好看。

所以在显示一行文字时,后面文字的位置会受到前面文字的影响。幸好,freetype帮我们考虑到了这些影响。

在这里插入图片描述

在显示一行文字时,这些文字会基于同一个基线来绘制位图:baseline。

在 baseline上,每一个字符都有它的原点 (origin),比如上图中 baseline 左边的黑色圆点就是字母 “g” 的原点。当前 origin 加上 advance 就可以得到下一个字符的 origin,比如上图中 baseline 右边的黑色圆点。在显示一行中多个文件字时,后一个文字的原点依赖于前一个文字的原点及 advance。

字符的位图是有可能越过 baseline 的,比如上图中字母 “g” 在 baseline下方还有图像。

上图中红色方框内就是字母 “g” 所点据的位图,它的四个角落不一定与原点重合。

上图中那些 xMin、xMax、yMin、yMax 如何获得?可以使用 FT_Glyph_Get_CBox 函数获得一个字体的这些参数,将会保存在一个 FT_BBox 结构体中,以后想计算一行文字的外框时要用到这些信息:

在这里插入图片描述

6.3 怎么在指定位置显示一行文字

要显示一行文字时,每一个字符都有自己外框:xMin、xMax、yMin、yMax。把这些字符的 xMin、yMin 中的最小值取出来,把这些字符的 xMax、yMax 中的最大值取出来,就可以确定这行文字的外框了。

在这里插入图片描述

要想在指定位置(x, y)显示一行文字,步骤如下图所示:

在这里插入图片描述

  1. 先指定第1个字符的原点pen坐标为(0, 0),计算出它的外框

  2. 再计算右边字符的原点,也计算出它的外框

    把所有字符都处理完后就可以得到一行文字的整体外框:假设外框左上角坐标为(x’, y’)。

  3. 想在(x, y)处显示这行文字,调整一下pen坐标即可

    怎么调整?

    pen为(0, 0)时对应左上角(x’, y’);

    那么左上角为(x, y)时就可以算出pen为(x-x’, y-y’)。

6.4 code

计算一行文字的外框

int compute_string_bbox(FT_Face       face, wchar_t *wstr, FT_BBox  *abbox)
{
    int i;
    int error;
    FT_BBox bbox;
    FT_BBox glyph_bbox;
    FT_Vector pen;
    FT_Glyph  glyph;
    FT_GlyphSlot slot = face->glyph;

    /* 初始化 */
    bbox.xMin = bbox.yMin = 32000;
    bbox.xMax = bbox.yMax = -32000;

    /* 指定原点为(0, 0) */
    pen.x = 0;
    pen.y = 0;

    /* 计算每个字符的bounding box */
    /* 先translate, 再load char, 就可以得到它的外框了 */
    for (i = 0; i < wcslen(wstr); i++)
    {
        /* 转换:transformation */
        FT_Set_Transform(face, 0, &pen);

        /* 加载位图: load glyph image into the slot (erase previous one) */
        error = FT_Load_Char(face, wstr[i], FT_LOAD_RENDER);
        if (error)
        {
            printf("FT_Load_Char error\n");
            return -1;
        }

        /* 取出glyph */
        error = FT_Get_Glyph(face->glyph, &glyph);
        if (error)
        {
            printf("FT_Get_Glyph error!\n");
            return -1;
        }
        
        /* 从glyph得到外框: bbox */
        FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &glyph_bbox);

        /* 更新外框 */
        if ( glyph_bbox.xMin < bbox.xMin )
            bbox.xMin = glyph_bbox.xMin;

        if ( glyph_bbox.yMin < bbox.yMin )
            bbox.yMin = glyph_bbox.yMin;

        if ( glyph_bbox.xMax > bbox.xMax )
            bbox.xMax = glyph_bbox.xMax;

        if ( glyph_bbox.yMax > bbox.yMax )
            bbox.yMax = glyph_bbox.yMax;
        
        /* 计算下一个字符的原点: increment pen position */
        pen.x += slot->advance.x;
        pen.y += slot->advance.y;
    }

    /* return string bbox */
    *abbox = bbox;
}

调整原点并绘制

int display_string(FT_Face     face, wchar_t *wstr, int lcd_x, int lcd_y)
{
    int i;
    int error;
    FT_BBox bbox;
    FT_Vector pen;
    FT_Glyph  glyph;
    FT_GlyphSlot slot = face->glyph;

    /* 把LCD坐标转换为笛卡尔坐标 */
    int x = lcd_x;
    int y = var.yres - lcd_y;

    /* 计算外框 */
    compute_string_bbox(face, wstr, &bbox);

    /* 反推原点 */
    /* FT_Set_Transform 要求坐标单位是 64 像素 */
    pen.x = (x - bbox.xMin) * 64; /* 单位: 1/64像素 */
    pen.y = (y - bbox.yMax) * 64; /* 单位: 1/64像素 */

    /* 处理每个字符 */
    for (i = 0; i < wcslen(wstr); i++)
    {
        /* 转换:transformation */
        FT_Set_Transform(face, 0, &pen);

        /* 加载位图: load glyph image into the slot (erase previous one) */
        error = FT_Load_Char(face, wstr[i], FT_LOAD_RENDER);
        if (error)
        {
            printf("FT_Load_Char error\n");
            return -1;
        }

        /* 在LCD上绘制: 使用LCD坐标 */
        draw_bitmap( &slot->bitmap,
                        slot->bitmap_left,
                        var.yres - slot->bitmap_top);

        /* 计算下一个字符的原点: increment pen position */
        pen.x += slot->advance.x;
        pen.y += slot->advance.y;
    }

    return 0;
}

main

int main(int argc, char **argv)
{
    wchar_t *wstr = L"百问网www.100ask.net";

    FT_Library    library;
    FT_Face       face;
    int error;
    FT_BBox bbox;
    int font_size = 24;
    int lcd_x, lcd_y;

    if (argc < 4)
    {
        printf("Usage : %s <font_file> <lcd_x> <lcd_y> [font_size]\n", argv[0]);
        return -1;
    }

    lcd_x = strtoul(argv[2], NULL, 0);      
    lcd_y = strtoul(argv[3], NULL, 0);      
    
    if (argc == 5)
        font_size = strtoul(argv[4], NULL, 0);      

    fd_fb = open("/dev/fb0", O_RDWR);
    if (fd_fb < 0)
    {
        printf("can't open /dev/fb0\n");
        return -1;
    }

    if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
    {
        printf("can't get var\n");
        return -1;
    }

    if (ioctl(fd_fb, FBIOGET_FSCREENINFO, &fix))
    {
        printf("can't get fix\n");
        return -1;
    }

    line_width  = var.xres * var.bits_per_pixel / 8;
    pixel_width = var.bits_per_pixel / 8;
    screen_size = var.xres * var.yres * var.bits_per_pixel / 8;
    fbmem = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
    if (fbmem == (unsigned char *)-1)
    {
        printf("can't mmap\n");
        return -1;
    }

    /* 清屏: 全部设为黑色 */
    memset(fbmem, 0, screen_size);

    error = FT_Init_FreeType( &library );              /* initialize library */
    
    error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */

    FT_Set_Pixel_Sizes(face, font_size, 0);

    display_string(face, wstr, lcd_x, lcd_y);
    
    return 0;   
}

  • 10
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值