博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用freetype来显示中文汉字和英文字符
阅读量:5105 次
发布时间:2019-06-13

本文共 8271 字,大约阅读时间需要 27 分钟。

这里我们用到了freetype。进入官网 中下载最新的版本2.7的源代码和文件。freetype-2.7.tar.gz freetype-doc-2.7.tar.gz

首先我们在使用官方提供的程序在pc上运行一下。需要安装zlib库。

freetype-doc-2.7.tar.gz文件中的docus目录下的tutorial有一个example.c的源文件。

example.c:

/* example1.c                                                      *//*                                                                 *//* This small program shows how to print a rotated string with the *//* FreeType 2 library.                                             */#include 
#include
#include
#include
#include FT_FREETYPE_H#define WIDTH 80#define HEIGHT 80/* origin is the upper left corner */unsigned char image[HEIGHT][WIDTH];/* Replace this function with something useful. */voiddraw_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 ( 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]; } }}voidshow_image( void ){ int i, j; for ( i = 0; i < HEIGHT; i++ ) { for ( j = 0; j < WIDTH; j++ ) putchar( image[i][j] == 0 ? ' ' : image[i][j] < 128 ? '+' : '*' ); putchar( '\n' ); }}intmain( int argc, char** argv ){ FT_Library library; FT_Face face; FT_GlyphSlot slot; FT_Matrix matrix; /* transformation matrix */ FT_Vector pen; /* untransformed origin */ FT_Error error; char* filename; char* text; double angle; int target_height; int n, num_chars; if ( argc != 3 ) { fprintf ( stderr, "usage: %s font sample-text\n", argv[0] ); exit( 1 ); } filename = argv[1]; /* first argument */ text = argv[2]; /* second argument */ num_chars = strlen( text ); angle = ( 0.0 / 360 ) * 3.14159 * 2; /* use 0 degrees 旋转 */ target_height = HEIGHT; error = FT_Init_FreeType( &library ); /* initialize library */ /* error handling omitted */ error = FT_New_Face( library, filename, 0, &face );/* create face object */ /* error handling omitted */ /* use 50pt at 100dpi */ error = FT_Set_Char_Size( face, 50 * 64, 0, 30, 0 ); /* set character size */ /* error handling omitted */ slot = face->glyph; /* set up matrix */ matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L ); matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L ); matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L ); matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L ); /* the pen position in 26.6 cartesian space coordinates; */ /* start at (40,0) relative to the upper left corner */ pen.x = 0 * 64; pen.y = ( target_height - 40 ) * 64; for ( n = 0; n < num_chars; n++ ) { /* set transformation */ FT_Set_Transform( face, &matrix, &pen ); /* load glyph image into the slot (erase previous one) */ error = FT_Load_Char( face, text[n], FT_LOAD_RENDER ); if ( error ) continue; /* ignore errors */ /* now, draw to our target surface (convert position) */ draw_bitmap( &slot->bitmap, slot->bitmap_left, target_height - slot->bitmap_top ); /* increment pen position */ pen.x += slot->advance.x; pen.y += slot->advance.y; } show_image(); FT_Done_Face ( face ); FT_Done_FreeType( library ); return 0;}/* EOF */

 程序中把输出的字符打印在屏幕上,分辨率是640*480,从300,200 处开始显示.这里我为了便于查看就改为了80*80.从40,0处开始显示

 编译:

gcc -o example1 example1.c -I /usr/local/include/freetype2/ -lfreetype -lm

 运行程序即可。

arm:

首先配置:

./configure --host=arm-linux make make DESTDIR=$PWD/tmp install

将生成的文件拷贝到交叉编译器和开发板文件系统相应的位置。

编译文件:

arm-linux-gcc -o test main.c  -lfreetype -lm

将test和字体文件拷贝到开发板中即可运行程序.

源代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include "show_font.h"#include
#include FT_FREETYPE_Hunsigned char *hzkmem;unsigned char *fbmem;unsigned int line_width;unsigned int pixel_width;struct fb_var_screeninfo var;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 short *pen_32; unsigned char red,green,blue; pen_16 = (unsigned short *)pen_8; pen_32 = (unsigned short *)pen_8; switch( pixel_width*8 ) { 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 support %ddpp\n",pixel_width*8 ); break; }}void lcd_put_ascii( int x, int y, unsigned char c ){ unsigned char *dots = (unsigned char *)&fontdata_8x16[c*16]; int i,b; unsigned char byte; for( i=0; i< 16; i++ ) { byte = dots[i]; for( b=7;b>=0;b--) { if( byte & (1<
=0; b-- ) { if( byte & (1<
width; FT_Int y_max = y + bitmap->rows; 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 >= var.xres || j >= var.yres ) continue; //image[j][i] |= bitmap->buffer[q * bitmap->width + p]; lcd_put_pixel(i,j,bitmap->buffer[q * bitmap->width + p]); } }}int main( int argc, char **argv ){ int hzk_fd; int fd_fb; struct fb_fix_screeninfo fix; int screen_size; FT_Library library; FT_Error error; FT_Face face; FT_Matrix matrix; /* transformation matrix */ FT_Vector pen; /* untransformed origin */ double angle; wchar_t *chinese_char = L"周zhou"; unsigned char str[]={ 0xd6,0xd0}; struct stat hzk_stat; fd_fb = open("/dev/fb0", O_RDWR ); if( fd_fb<0 ) { perror("oepn failed"); 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("mmap is failed\n"); return -1; } hzk_fd = open("HZK16", O_RDONLY ); if( hzk_fd<0 ) { printf("can't open hzk\n"); return -1; } if( fstat(hzk_fd, &hzk_stat)) { printf("can't get fstat\n"); return -1; } hzkmem = (unsigned char *)mmap( NULL, hzk_stat.st_size, PROT_READ, MAP_SHARED,hzk_fd,0 ); if( hzkmem == (unsigned char *)-1 ) { printf("mmap hzk is failed\n"); return -1; } memset( fbmem, 0, screen_size ); lcd_put_ascii(var.xres/2,var.yres/2,'A' ); printf("chinese code : %02x %02x\n", str[0],str[1]); lcd_put_chinese( var.xres/2 +8 ,var.yres/2, str ); if( argc != 2 ) { printf("Usage: %s
\n", argv[0]); return -1; } error = FT_Init_FreeType( &library ); /* initialize library */ /* error handling omitted */ error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */ /* use 50pt at 100dpi */ error = FT_Set_Pixel_Sizes( face, 30, 0 ); /* set character size */ angle = ( 0.0 / 360 ) * 3.14159 * 2; /* use 25 degrees */ /* set up matrix */ matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L ); matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L ); matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L ); matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L ); /* the pen position in 26.6 cartesian space coordinates; */ /* start at (40,0) relative to the upper left corner */ pen.x = (var.xres/2+8+16) * 64; pen.y = ( var.yres/2 - 16 ) * 64; /* set transformation */ FT_Set_Transform( face, &matrix, &pen ); /* load glyph image into the slot (erase previous one) */ error = FT_Load_Char( face, chinese_char[0], FT_LOAD_RENDER ); if(error) { printf("FT_load_char error\n"); return -1; } draw_bitmap( &face->glyph->bitmap, face->glyph->bitmap_left, var.yres - face->glyph->bitmap_top ); return 0;}

 

sds

转载于:https://www.cnblogs.com/ynxf/p/6274067.html

你可能感兴趣的文章
可选参数的函数还可以这样设计!
查看>>
[你必须知道的.NET]第二十一回:认识全面的null
查看>>
Java语言概述
查看>>
关于BOM知识的整理
查看>>
使用word发布博客
查看>>
面向对象的小demo
查看>>
微服务之初了解(一)
查看>>
GDOI DAY1游记
查看>>
收集WebDriver的执行命令和参数信息
查看>>
数据结构与算法(三)-线性表之静态链表
查看>>
mac下的mysql报错:ERROR 1045(28000)和ERROR 2002 (HY000)的解决办法
查看>>
快速幂
查看>>
AIO 开始不定时的抛异常: java.io.IOException: 指定的网络名不再可用
查看>>
MyBaits动态sql语句
查看>>
HDU4405(期望DP)
查看>>
拉格朗日乘子法 那些年学过的高数
查看>>
vs code 的便捷使用
查看>>
Spring MVC @ResponseBody返回中文字符串乱码问题
查看>>
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
JS 中的跨域请求
查看>>