Linux下利用FreeType2的API实现字符的显示


网上的FreeType2例子太少,能显示汉字的比较难找,C语言代码写的更难找,能找到的,基本上是被转载了N遍的同一个示例代码,基本上解决不了我的问题。

于是乎,花费了不少时间才完成了这些代码;

主要功能是:

将传入的GB2312编码的char型字符串转换成图片数组并输出。

主要原理是:

将GB2312编码的char型字符串,转换成unicode编码的wchar_t型字符串;

之后,利用FreeType2的API获取汉字的字体位图。

代码看似不怎么复杂,之前找char转wchar_t的代码,没找到一个合适的,最后呢,东拼西凑,就得到了想要的代码;

还有,输出的是记录每个像素点的256级灰度的数组,也就是和png图片中alpha通道一样,0 - 255表示透明度, 0为完全透明,255为不透明;

想要将这些文字贴到图片上,有个公式:

r2 = (r1 * alpha + r2 * (255 - alpha)) /255;
g2 = (g1 * alpha + g2 * (255 - alpha)) /255;
b2 = (b1 * alpha + b2 * (255 - alpha)) /255;

r、g、b表示图片的red、greenblue三种颜色,这三种颜色通过组合可以变成不同的颜色;

alpha是文字位图的alpha通道; 

r2、b2g2是图片重叠后该像素点显示的颜色;
r1、b1g1是文字位图的每个像素点显示的颜色。

相关信息请参考这篇文章:

有需要这些代码的人的话,请把代码改一下,因为这是从我的LCUI图形库的代码中复制过来的,不能直接使用。

[cpp]
  1. #include "LCUI_Build.h"   
  2. #include LCUI_MAIN_H   
  3. #include LCUI_FONTS_H    
  4. #include "all.h"   
  5.   
  6. /*代码转换:从一种编码转为另一种编码*/  
  7. int code_convert(char *from_charset,char *to_charset,const char *inbuf,unsigned int inlen,  
  8. unsigned char *outbuf,unsigned int outlen)  
  9. {  
  10.     iconv_t cd;  
  11.     const char **pin = &inbuf;  
  12.     unsigned char **pout = &outbuf;  
  13.     cd = iconv_open(to_charset,from_charset);  
  14.     if (cd==0) return -1;  
  15.     memset(outbuf,0,outlen);  
  16.     if (iconv(cd,(char**)pin,&inlen,(char**)pout,&outlen)==-1) return -1;  
  17.     iconv_close(cd);  
  18.     return 0;  
  19. }  
  20.   
  21. /*GB2312码转为utf-8码*/  
  22. int GB2312_To_UTF8(const char *inbuf,unsigned int inlen,unsigned char *outbuf,unsigned int outlen)  
  23. {  
  24.     return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);  
  25. }  
  26.   
  27. unsigned short Get_Unicode(char *in_gb2312)  
  28. {  
  29.     unsigned char out[256];  
  30.     int rc;  
  31.     unsigned int length_gb2312;  
  32.   
  33.     /* gb2312码转为utf8码 */  
  34.     length_gb2312 = strlen(in_gb2312);  
  35.     rc = GB2312_To_UTF8(in_gb2312,length_gb2312,out,256);  
  36.   
  37.     /* utf8转unicode码 */  
  38.     unsigned short unicode;  
  39.     unicode = out[0];  
  40.     if (unicode >= 0xF0) {  
  41.         unicode = (unsigned short) (out[0] & 0x07) << 18;  
  42.         unicode |= (unsigned short) (out[1] & 0x3F) << 12;  
  43.         unicode |= (unsigned short) (out[2] & 0x3F) << 6;  
  44.         unicode |= (unsigned short) (out[3] & 0x3F);  
  45.     } else if (unicode >= 0xE0) {  
  46.         unicode = (unsigned short) (out[0] & 0x0F) << 12;  
  47.         unicode |= (unsigned short) (out[1] & 0x3F) << 6;  
  48.         unicode |= (unsigned short) (out[2] & 0x3F);  
  49.     } else if (unicode >= 0xC0) {  
  50.         unicode = (unsigned short) (out[0] & 0x1F) << 6;  
  51.         unicode |= (unsigned short) (out[1] & 0x3F);  
  52.     }  
  53.     return unicode;  
  54. }  
  55.   
  56.   
  57.   
  58.   
  59. int Show_Font_Bitmap(Font_Bitmap_Data *in_fonts)  
  60. /* 功能:在屏幕上以0和1表示字体位图 */  
  61. {  
  62.     int x,y;  
  63.     for(y=0;y<in_fonts->height;++y){  
  64.         for(x=0;x<in_fonts->width;++x){  
  65.             if(in_fonts->text_alpha[y*in_fonts->width+x]>0)  
  66.             printf("1");  
  67.             else printf("0");  
  68.         }  
  69.         printf("\n");  
  70.     }  
  71.     printf("\n");  
  72.     return 0;  
  73. }  
  74.   
  75.   
  76.   
  77.   
  78. int Get_Fonts_Bitmap(  
  79. char *font_file,             /* 字体文件路径 */  
  80. char *in_text,               /* 输入的字符串 */  
  81. int fonts_pixel_size,        /* 字体大小 */  
  82. int space,                   /* 间距 */  
  83. Font_Bitmap_Data *out_fonts  /* 输出的位图数据,Pic_Data是结构体 */  
  84. )  
  85. /* ****************************************** */  
  86. /* 根据传入的字体库路径、字符串、字体尺寸,输 */  
  87. /* 出该字符串的位图数组                       */  
  88. /* 本函数使用了freetype2的API                 */  
  89. /* ****************************************** */  
  90. {  
  91.     FT_Library         p_FT_Lib = NULL;    /* 库的句柄  */  
  92.     FT_Face            p_FT_Face = NULL;      /* face对象的句柄 */  
  93.     FT_Error           error = 0;  
  94.     FT_Bitmap          bitmap;  
  95.     FT_BitmapGlyph     bitmap_glyph;  
  96.     FT_Glyph           glyph;  
  97.     FT_GlyphSlot       slot;  
  98.     int i , j ,temp,num,bg_height;  
  99.     char error_str[200];  
  100.     error = FT_Init_FreeType( & p_FT_Lib);  /* 初始化FreeType库 */  
  101.     if (error)   /* 当初始化库时发生了一个错误 */  
  102.     {  
  103.         p_FT_Lib = 0 ;  
  104.         printf(FT_INIT_ERROR);  
  105.         return - 1 ;  
  106.     }  
  107.     /* 从字体库文件中获取字体 */  
  108.     error = FT_New_Face(p_FT_Lib, font_file , 0 , & p_FT_Face);  
  109.     if ( error == FT_Err_Unknown_File_Format )   
  110.     {   
  111.         printf(FT_UNKNOWN_FILE_FORMAT); /* 未知文件格式 */  
  112.         return - 1 ;  
  113.     }   
  114.     else if (error)  
  115.     {  
  116.         printf(FT_OPEN_FILE_ERROR);/* 打开错误 */  
  117.         perror("FreeeType2");  
  118.         return - 1 ;  
  119.     }  
  120.     j = 0;  
  121.     wchar_t *unicode_text;/* 用于存储unicode字符 */  
  122.     char ch[256];  
  123.     unicode_text = (wchar_t*)calloc(1,sizeof(wchar_t)*(strlen(in_text)*2));/* 申请内存 */  
  124.     for(i=0;i<strlen(in_text);++i){  
  125.         memset(ch,0,sizeof(ch));  /* 所有元素置零 */  
  126.         ch[0] = in_text[i];  /* 获取in_text中的第i个元素 */  
  127.         if(ch[0] < 0) {   
  128.             /* GB2312编码的汉字,每byte是负数,因此,可以用来判断是否有汉字 */  
  129.             if(i < strlen(in_text)-1){/* 如果没有到字符串末尾 */  
  130.                 ch[1] = in_text[i+1];  
  131.                 ++i;  
  132.             }  
  133.             else break;  
  134.         }  
  135.         unicode_text[j] = Get_Unicode(ch);  /* 开始转换编码 */  
  136.         ++j;  
  137.     }  
  138.     num = j; /* 记录字符的数量 */  
  139.       
  140.     int start_x = 0,start_y = 0;  
  141.     int ch_height = 0,ch_width = 0;  
  142.     int k,text_width = 0;  
  143.     size_t size = 0;  
  144.     unsigned char **text_alpha;   
  145.     bg_height = fonts_pixel_size+5; /* 背景图形的高度,这个高度要大于字体的高度,所以是+5 */  
  146.     /* 分配内存,用于存储字体背景图的数据 */  
  147.     text_alpha = (unsigned char**)malloc(sizeof(unsigned char*)*bg_height);   
  148.     for(i=0;i<bg_height;++i){  
  149.     /* 预先为背景图的每一行分配内存 */  
  150.         text_alpha[i] = (unsigned char*)malloc(sizeof(unsigned char)*1);   
  151.     }  
  152.     FT_Select_Charmap(p_FT_Face,FT_ENCODING_UNICODE);   /* 设定为UNICODE,默认的也是 */  
  153.     FT_Set_Pixel_Sizes(p_FT_Face,0,fonts_pixel_size);   /* 设定字体大小 */  
  154.   
  155.     slot = p_FT_Face->glyph;  
  156.     for(temp=0;temp<num;++temp){  
  157.         /* 开始遍历unicode编码的字符串中的每个元素  */  
  158.         /* 这个函数只是简单地调用FT_Get_Char_Index和FT_Load_Glyph */  
  159.         error = FT_Load_Char( p_FT_Face, unicode_text[temp],  FT_LOAD_RENDER | FT_LOAD_NO_AUTOHINT);   
  160.         if(!error){  
  161.             /* 从插槽中提取一个字形图像 */  
  162.             /* 请注意,创建的FT_Glyph对象必须与FT_Done_Glyph成对使用 */  
  163.             error = FT_Get_Glyph(p_FT_Face -> glyph, &glyph);  
  164.             if (!error)  
  165.             {  
  166.                 if(unicode_text[temp] == ' ') {  
  167.                     /* 如果有空格 */  
  168.                     k = 0;  
  169.                     ch_width   = (fonts_pixel_size -2)/2;  
  170.                     ch_height  = fonts_pixel_size;  
  171.                     text_width = start_x + ch_width;  
  172.                     start_y = 0;  
  173.                     for(i=0;i<bg_height;++i){  
  174.                         text_alpha[i] = (unsigned char*)realloc(text_alpha[i],sizeof(unsigned char)*text_width);  
  175.                         for(j=start_x-space;j<text_width;++j) text_alpha[i][j] = 0;  
  176.                     }  
  177.                     for ( i = 0 ; i < ch_height; ++i)  
  178.                     {  
  179.                         for ( j = 0 ; j < ch_width; ++j)  
  180.                         {  
  181.                             text_alpha[start_y + i][start_x + j] = 0;  
  182.                             ++k;  
  183.                         }  
  184.                     }  
  185.                     start_x += (ch_width+space); /* 画笔向右边移动 */  
  186.                 }  
  187.                 else{  
  188.                     /* 256级灰度字形转换成位图 */  
  189.                     FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0 ,1);  
  190.                     /* FT_RENDER_MODE_NORMAL       这是默认渲染模式,它对应于8位抗锯齿位图。 */  
  191.                     bitmap_glyph = (FT_BitmapGlyph)glyph;  
  192.                     bitmap       = bitmap_glyph -> bitmap;  
  193.                     k = 0;  
  194.                       
  195.                     start_y = fonts_pixel_size - slot->bitmap_top + 2; /* 获取起点的y轴坐标 */  
  196.                     if(start_y < 0) start_y = 0;  
  197.                     if(bitmap.rows > bg_height) ch_height = fonts_pixel_size;  
  198.                     else ch_height = bitmap.rows;  
  199.                     if(ch_height+start_y > bg_height) ch_height = bg_height - start_y;  
  200.                     ch_width = bitmap.width;  
  201.                       
  202.                     text_width = start_x + bitmap.width;  
  203.                     for(i=0;i<bg_height;++i){  
  204.                     /* 动态扩增存储字体位图的背景图形占用的空间 */  
  205.                         text_alpha[i] = (unsigned char*)realloc(text_alpha[i],sizeof(unsigned char)*text_width);  
  206.                         for(j=start_x-space;j<text_width;++j) text_alpha[i][j] = 0;/* 多出来的空间全部置零 */  
  207.                     }  
  208.                     /* 开始将字体位图贴到背景图形中 */  
  209.                     for(i = 0; i < bg_height; ++i){   
  210.                         for(j = 0;j < ch_width; ++j){  
  211.                             if(i >= start_y && i < start_y + ch_height){  
  212.                             /* 如果在字体位图的范围内 */  
  213.                                 text_alpha[i][start_x + j] = bitmap.buffer[k];  
  214.                                 ++k;  
  215.                             }  
  216.                             else text_alpha[i][start_x + j] = 0;/* 否则就置零 */  
  217.                         }  
  218.                     }  
  219.                     start_x += (ch_width+space); /* 画笔向右边移动 */  
  220.                     /* 释放字形占用的内存 */  
  221.                     FT_Done_Glyph(glyph);  
  222.                     glyph = NULL;  
  223.                 }  
  224.             }  
  225.             else{  
  226.                 sprintf(error_str,"FreeType2 错误[%d]",error);  
  227.                 perror(error_str);  
  228.             }  
  229.         }  
  230.         else{  
  231.             sprintf(error_str,"FreeType2 错误[%d]",error);  
  232.             perror(error_str);  
  233.         }  
  234.     }  
  235.     /* 释放face占用的内存 */  
  236.     FT_Done_Face(p_FT_Face);  
  237.     p_FT_Face = NULL;  
  238.     /* 释放FreeType Lib占用的内存 */  
  239.     FT_Done_FreeType(p_FT_Lib);  
  240.     p_FT_Lib = NULL;    
  241.     temp = 0;  
  242.     out_fonts->width    = text_width;          /* 要输出的位图的宽度 */  
  243.     out_fonts->height   = bg_height;           /* 要输出的位图的高度 */  
  244.     if(out_fonts->malloc == IS_TRUE) free(out_fonts->text_alpha);  
  245.     size = sizeof(unsigned char) * text_width * bg_height;  
  246.     out_fonts->text_alpha = (unsigned char*)calloc(1,size);   /* 申请内存用来存储 */  
  247.     k = 0;  
  248.     for ( i = 0 ; i < bg_height; ++i)  
  249.     {  
  250.         for ( j = 0 ; j < text_width; ++j)  
  251.         {  
  252.             out_fonts->text_alpha[k] = text_alpha[i][j];  
  253.             ++k;  
  254.         }  
  255.     }  
  256.     out_fonts->malloc = IS_TRUE;  
  257.     /* 释放内存 */  
  258.     for(i=0;i<bg_height;++i){  
  259.         free(text_alpha[i]);  
  260.     }  
  261.     free(text_alpha);  
  262.     free(unicode_text);  
  263.     return 0;  
  264. }  

这些代码在我的工程里的运行效果:

相关内容