使用freetype来显示中文汉字和英文字符
这里我们用到了freetype。进入官网http://savannah.nongnu.org/download/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 <stdio.h>
#include <string.h>
#include <math.h> #include <ft2build.h>
#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. */ 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 ( 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];
}
}
} void
show_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' );
}
} int
main( 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 <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <math.h> #include "show_font.h"
#include <ft2build.h>
#include FT_FREETYPE_H unsigned 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<<b))
{
lcd_put_pixel(x+7-b,y+i, 0xffffff );
}
else
{
lcd_put_pixel(x+7-b,y+i, 0x000000 );
}
}
} } void lcd_put_chinese( int x, int y, unsigned char *c )
{
unsigned int area = c[0] - 0xa1;
unsigned int where = c[1] - 0xa1;
unsigned char *dots = hzkmem + (area*94+where)*32;
unsigned char byte;
int i,j,b;
for( i=0; i<16; i++ )
{
for( j=0; j<2; j++ )
{
byte = dots[i*2+j];
for( b=7; b>=0; b-- )
{
if( byte & (1<<b) )
lcd_put_pixel(x+j*8+7-b,y+i, 0xffffff );
else
lcd_put_pixel(x+j*8+7-b,y+i, 0x000000 );
}
}
}
} 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 ( 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 <font_file>\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
使用freetype来显示中文汉字和英文字符的更多相关文章
- C#、Java实现按字节截取字符串包含中文汉字和英文字符数字标点符号等
C#.Java实现按字节截取字符串,字符串中包含中文汉字和英文字符数字标点符号等. 在实际项目应用过程中,尤其是在web开发时可能遇到的比较多,就以我的(JiYF笨小孩管理系统)为例,再发布文章时候, ...
- LCD1602显示中文汉字
小子在西藏 2011-11-25编写 特别说明笔者是上面的作者,感谢那些原意分享知识的人.时隔5年我又看到了笔者当年写的东西,我想这期间还有许许多多的人 今天写在博客上,愿更多后来者可以学习. LCD ...
- 如何让移植的嵌入式ARM显示中文汉字
如果你急于在ARM开发板上看到Qt显示中文,而不介意稍次的效果,可以在运行Qt程序时,增加设置字体的参数,比如运行名为hello的Qt程序:./hello -fn unifont 1.首先,需要文泉驿 ...
- EditText限制输入的几种方式及只显示中文汉字的做法
最近项目要求限制密码输入的字符类型, 例如不能输入中文. 现在总结一下EditText的各种实现方式, 以比较各种方法的优劣. 第一种方式: 设置EditText的inputType属性,可以 ...
- hadoop中汉字与英文字符混合的keyword做为combine的key的问题
近期,须要将汉字与字符的非常合串作为combine的输出的key, 这样做是希望,利用hadoop的归并来依照key进行分组,然后,在reduce阶段,拿到的都是一个一个组. 可是,发现,这样的,汉字 ...
- 基于stm32f4的ucGUI通过外部flash存储汉字库显示任意英文字符和汉字组合(控件可用)
在做一个用到ucGUI的项目的时候要用到不定的汉字和英文字符,但是ucGUI本身又不支持读取芯片外部flash的字库来显示,于是查了下资料,如下: http://www.cnblogs.com/hik ...
- 转】MySQL客户端输出窗口显示中文乱码问题解决办法
原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4008095.html 感谢! 最近发现,在MySQL的dos客户端输出窗口中查询表中的数据时,表中的中文数据都显 ...
- MySQL客户端输出窗口显示中文乱码问题解决办法
最近发现,在MySQL的dos客户端输出窗口中查询表中的数据时,表中的中文数据都显示成乱码,如下图所示:
- Android实现中文汉字笔划(笔画)、中文拼音排序、英文排序
发布时间:2018-11-16 技术:Android 概述 最近要做一个类似微信的,在登录界面选择国家地区的功能,微信有中文汉字笔画排序以及中文拼音排序等几种方式,如下所示: 简体中文 拼音排 ...
随机推荐
- Kubernetes Ingress-nginx使用
目录 简介 1. 部署Ingress-Controller 2. 使用Ingress规则 2.1 Ingress地址重写 2.2 配置HTTPS 2.3 黑白名单配置 2.4 匹配请求头 2.5 速率 ...
- Springboot核心注解
运行文中的代码需要在项目构建中引入springboot 相关依赖. ① @configuration configuration,用来将bean加入到ioc容器.代替传统xml中的bean配置.代码示 ...
- Spring Cloud Alibaba 初体验(三) Nacos 与 Dubbo 集成
一.新建项目 新建项目,只放置接口,用于暴露 Dubbo 服务接口 public interface GreetingService { String greeting(); } 二.provider ...
- 20200203_windows2012下安装mysql 5.7.29
一. 检查系统版本: 二. 下载mysql, 下载地址: https://dev.mysql.com/downloads/mysql/5.7.html#downloads 三. 解压下载后的压 ...
- 第15.40节、PyQt(Python+Qt)实战:moviepy实现MP4视频转gif动图的工具
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 一.引言 在写<第15.39节.splitDockWidget和 ...
- CentOS配置Nginx官方的Yum源
由于yum源中没有我们想要的nginx,那么我们就需要创建一个"/etc/yum.repos.d/nginx.repo"的文件,其实就是新增一个yum源. [root@niaoyu ...
- github内的一些操作
github远程仓库的克隆操作 1,找到你想要克隆的地址,复制下来 2,切入到git所在目录下,输入 git clone 复制的地址 设置过滤文件不纳入git管理 1,在git目录下创建一个.giti ...
- Docker 安装并部署Tomcat、Mysql8、Redis
1. 安装前检查 1 #ContOS 7安装Docker系统为64位,内核版本为3.10+ 2 lsb_release -a 3 4 uname -r 5 6 #更新yum源 7 yum -y up ...
- virtualbox sharefolder mount fail
ubuntu 14.04.1 LTS 64bit 安装完GuestAdditions后,在终端输入 sudo mount -t vboxsf sharename /mnt/share 提示错误:mou ...
- react第十三单元(react路由-react路由的跳转以及路由信息) #课程目标
第十三单元(react路由-react路由的跳转以及路由信息) #课程目标 熟悉掌握路由的配置 熟悉掌握跳转路由的方式 熟悉掌握路由跳转传参的方式 可以根据对其的理解封装一个类似Vue的router- ...