免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1253 | 回复: 0
打印 上一主题 下一主题

Freetype Note [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-19 21:09 |只看该作者 |倒序浏览
     由于最近做一个项目要用到多国语言,因为freetype 是采用模块化的形式,所以可以根据需要裁减到25K,觉得freetype用在嵌入式系统是一个不错的选择,以下信息大部分摘自互边网.freetyper用的矢量字体,下面先来了解下什么是矢量字体,它和点阵字体有什么区别,它又有哪些优势?
     点阵字库是把每一个汉字都分成16×16或24×24个点,然后用每个点的虚实来表示汉字的轮廓,常用来作为显示字库使用,这类点阵字库汉字最大的缺点是不能放大,一旦放大后就会发现文字边缘的锯齿。矢量字库保存的是对每一个汉字的描述信息,比如一个笔划的起始、终止坐标,半径、弧度等等。在显示、打印这一类字库时,要经过一系列的数学运算才能输出结果,但是这一类字库保存的汉字理论上可以被无限地放大,笔划轮廓仍然能保持圆滑,打印时使用的字库均为此类字库。Windows使用的字库也为以上两类,在FONTS目录下,如果字体扩展名为FON,表示该文件为点阵字库,扩展名为TTF则表示矢量字库。点阵字库文件的图标为一个红色的“A”,矢量字库图标是两个“T”。
      freetype的安装和运行Demo程序:
1.freetype是开放源代友的,它采用两种协议,The FreeType License and The GNU General Public License version 2. 所以你可以从网上下载用于商业用途. 从http://savannah.nongnu.org/download/freetype/ 下载了最新的版本   
2.安装步骤:
解压这些就不用说了,然后把它的文件夹名改成freetype2,然后 ./configure;make;make install ;安的库就会默认安装到了/usr目录下面.当然你可以根据自己的需要把它安装到其它目录.如 : ./configure --prefix="what you want to install" 执行的文件也可以安装到指定的相关目录, --exec-prefix="what directory you want to install bin" ,还有其它一些配置,你可以用 ./confingure --help 来查看帮助.
3.编译demo:
当你的demo(ftdemos-2.3.7) 和freetype是放在同一级的目录时,这种情况很好办.因为运行demo程序要用到X 环境,所以你可以先在终端下用命令: $whereis X 找出X的目录. 我的是在/usr/X11R6/ 只要前面的目录就可以.因为编译只需要用到其中的相关库而已. 之后再运行 make X11_PATH="/usr/X11R6" after a miniute it will spawn the exec file under the directority of bin.ok
How to use the demo? just ./$(exec) it will remind you how to use it, so don't worry about it.
4.now let's discuss how to programming with the freetype.
it is so easy:
(1) the include files
               
               
                #include ft2build.h>
#include FT_FREETYPE_H
(2) initrial the freetyoe library
FT_library library;
error = FT_Init_FreeType( &library);
.......if any error inform the user.
(3) load the font file
error = FT_New_Face(library, "usr/share/arial.ttf",0,&face);
or
error = FT_New_Memory_Face(library,/*first byte inmemory*/
                  size,  /*zize in bytes*/
                   0, /*face_index*/
                  &face);
(4) set the size of the font
error = FT_se_char_Size(
        face, /*handle to face object*/
        0,   /*char_width in 1/64th of points*/
        16*64, /*char_height in 1/64th of points*/
        300, /*horizontal device resolution*/
        300); /*vertical device resolution*/
error = FT_Set_Pixel_Sizes(
            face, /*handle to face object*/
            0, /*pixel_width*/
            16); /*pixel_height*/
(5)加载字符的glyph
glyph_index = FT_Get_Char_Index( face, charcode );
  error = FT_Load_Glyph(
            face,          /* handle to face object */
            glyph_index,   /* glyph index           */
            load_flags );  /* load flags, see below */
  error = FT_Render_Glyph( face->glyph,   /* glyph slot  */
                           render_mode ); /* render mode */
(6)字体变换(旋转和缩放)
error = FT_Set_Transform(
            face,       /* target face object    */
            &matrix,    /* pointer to 2x2 matrix */
&delta );   /* pointer to 2d vector  */
5.now let me show you a complete example.
(1).file: getbitmap.c
#include ft2build.h>
#include FT_FREETYPE_H
int main(int argc, char **argv){
  int psize;
  FT_Library library;
  FT_Face face;
  unsigned int ucode;
  FT_UInt glyph_index;
  int row, pixel;
  if(argc != 4){
    return 10;
  }
  ucode = strtol(argv[2], NULL, 16);
  psize = strtol(argv[3], NULL, 10);
  printf("unicode +%X size %d\n", ucode, psize);
  if(FT_Init_FreeType(&library)
     || FT_New_Face(library,
                    argv[1],
                    0,
                    &face)
     || FT_Set_Pixel_Sizes(face,
                           psize,
                           0)){
    return 1;
  }
  glyph_index = FT_Get_Char_Index(face, ucode);
  if(glyph_index == 0){
    return 2;
  }
  if(FT_Load_Glyph(face,
                   glyph_index,
                   FT_LOAD_DEFAULT)){
    return 3;
  }
  if(FT_Render_Glyph(face->glyph,
                     FT_RENDER_MODE_MONO)){
    return 4;
  }
  printf("bitmap_left=%d\n"
         "bitmap_top=%d\n"
         "bitmap.rows=%d\n"
         "bitmap.width=%d\n"
         "bitmap.pitch=%d\n"
         "bitmap.pixel_mode=%d\n",
         face->glyph->bitmap_left,
         face->glyph->bitmap_top,
         face->glyph->bitmap.rows,
         face->glyph->bitmap.width,
         face->glyph->bitmap.pitch,
         face->glyph->bitmap.pixel_mode);
  printf("\n\n");
  for(row = 0;
      row  (face->glyph->bitmap.rows - face->glyph->bitmap_top);
      ++row){
      for(pixel = 0; pixel  face->glyph->bitmap.width; ++pixel){
        printf("_");
      }
      printf("\n");
  }
  for(row = 0; row  face->glyph->bitmap.rows; ++row){
    for(pixel = 0; pixel  face->glyph->bitmap_left; ++pixel)
      printf("_");
    for(pixel = 0; pixel  face->glyph->bitmap.width; ++pixel){
      printf("%c", (face->glyph->bitmap.buffer
                    [row * face->glyph->bitmap.pitch +
                     pixel/8] & (0xC0 >> (pixel % 8)))?'O':'_');
    }
    printf("\n");
  }
  return 0;
}
(2) Makefile
all : getbitmap
getbitmap.o : getbitmap.c
    gcc $(CXXFLAGS) -c `pkg-config freetype2 --cflags` $ -o $@
getbitmap : getbitmap.o
    gcc $(CXXFLAGS) `pkg-config freetype2 --libs` -lm $ -o $@
clean :
    rm -f *.o getbitmap
(3)运行方式:
./getbitmap /usr/share/fonts/chinese/True
Type/uming.ttf $(mygetunicode 中) 16
(4) mygetunicode 为一个脚本文件,主要功能为获取中文字体的unicode码.内容如下:
#!/bin/sh
echo -n $1 |iconv -t ucs2 | od -tx1 | head -n1 |awk '{print $3$2}'
this example come form
http://blog.chinaunix.net/u/8057/showart_335549.html
thanks to the original writer!
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/76511/showart_1431588.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP