Freetype 基本的API调用

简介: 如您所见,该函数返回一个错误代码,例如 FreeType API 的大多数其他函数。错误代码 的 0表示 操作成功;否则,该值描述 错误,并设置library为 NULL。

Freetype


Simple Glyph Loading - 简单的字形加载

您需要按照如下方式添加freetype头文件

#include <ft2build.h>
#include FT_FREETYPE_H

初始化库

要初始化 FreeType 库,请创建一个变量 键入命名[FT_Library]例如,,并调用 功能[FT_Init_FreeType]library

#include <ft2build.h>
#include FT_FREETYPE_H
FT_Library  library;
...
error = FT_Init_FreeType( &library );
if ( error )
{
  ... an error occurred during library initialization ...
}


FT_Init_FreeType


●  创建一个实例句柄 library

●  加载每一个FreeType所知道的每一个模块


如您所见,该函数返回一个错误代码,例如 FreeType API 的大多数其他函数。错误代码 的 0表示 操作成功;否则,该值描述 错误,并设置library为 NULL。


加载FontFace

使用FT_New_Face创建一个新的对象,描述给定的字体和样式

FT_Library  library;   /* handle to library     */
FT_Face     face;      /* handle to face object */
error = FT_Init_FreeType( &library );
if ( error ) { ... }
error = FT_New_Face( library,
                     "/usr/share/fonts/truetype/arial.ttf",
                     0,
                     &face );
if ( error == FT_Err_Unknown_File_Format )
{
  ... the font file could be opened and read, but it appears
  ... that its font format is unsupported
}
else if ( error )
{
  ... another error code means that the font file could not
  ... be opened or read, or that it is broken...
}
FT_New_Face( FT_Library   library,
               const char*  filepathname,
               FT_Long      face_index,
               FT_Face     *aface );

正如你想象的那样,打开一个字体文件,然后尝试提取一个Face从字体中,参数如下


●  library 是由FT_Init_FreeType初始化的句柄


●  filepathname 字体文件位置


●  face_index


   某些字体格式允许多个字体 嵌入在单个文件中。


   此索引告诉您要加载哪个人脸。一 如果其值太大,则返回错误。


   不过,索引 0 始终有效。


●  aface


指向设置为描述新的面对象。


如果出现错误,它将设置为 NULL


From Memory从内存中加载Face

FT_New_Memory_Face

FT_Library  library;   /* handle to library     */
FT_Face     face;      /* handle to face object */
error = FT_Init_FreeType( &library );
if ( error ) { ... }
error = FT_New_Memory_Face( library,
                            buffer,    /* first byte in memory */
                            size,      /* size in bytes        */
                            0,         /* face_index           */
                            &face );
if ( error ) { ... }

如您所见, 该函数多了指向字体文件缓冲器及其大小的指针,而不是文件名称。除此之外和FT_New_Face具有相同的语义。


不能在调用FT_Done_Face之后调用该函数。


来自其他来源(压缩文件、网络、 等)

该处不做介绍。


访问Face数据

Face对象对以下所有信息进行建模: 全局描述面部。通常,此数据可以是 通过取消引用句柄直接访问,face−>num_glyphs


可用字段的完整列表位于 FT_FaceRec结构说明:


num_glyphs 此变量提供字体中可用的字形数


face_flags 一个 32 位整数,包含描述的位标志 一些Face属性


units_per_EM 此字段仅对可缩放格式有效(它是 否则设置为 0)

num_fixed_sizes 此字段给出嵌入位图的次数 在当前面孔中


available_sizes 指向数组的指针FT_Bitmap_Size元素。


设置当前像素大小

FreeType2使用size对象对所有对象进行建模,与给定Face大小相关的信息。


创建新的Face对象时,将设置所有元素 初始化期间为 0。要填充 具有合理值的结构,您应该调用FT_Set_Char_Size。 下面是一个示例,将字符大小设置为 16pt 300×300dpi 设备:

error = FT_Set_Char_Size(
          face,    /* handle to face object         */
          0,       /* char_width in 1/64 of points  */
          16*64,   /* char_height in 1/64 of points */
          300,     /* horizontal device resolution  */
          300 );   /* vertical device resolution    */
/*
1.字符宽度和高度在 1/64 中指定 的点数。点是物理距离, 等于 1/72 英寸。通常,它不是 相当于一个像素。
2.字符宽度值 0 表示 “与字符高度相同”,值为 0 对于字符高度表示“与字符相同” 宽度'。否则,可以指定 不同的字符宽度和高度。
3.水平和垂直设备分辨率为 以每英寸点数或 DPI 表示。 标准值为 72 或 96 dpi,用于显示 像屏幕这样的设备。分辨率用于 根据字符计算字符像素大小 点大小。
4.水平分辨率平均值的值为 0 “与垂直分辨率相同”,值 垂直分辨率为 0 表示“相同” 作为水平分辨率'。如果两个值都为 零,72 dpi 用于两个维度。
5.第一个参数是Face对象的句柄,而不是 大小对象。
*/
// 如果要指定(整数)像素大小 你自己,你可以打调用FT_Set_Pixel_Sizes。
error = FT_Set_Pixel_Sizes(
          face,   /* handle to face object */
          0,      /* pixel_width           */
          16 );   /* pixel_height          */

Loading a Glyph Image – 加载字形图像

1.将字符代码转换为字形索引

FT_Get_Char_Index( FT_Face   face,
                     FT_ULong  charcode );

2.从Face加载字形

FT_Load_Glyph( FT_Face   face,
                 FT_UInt   glyph_index,
                 FT_Int32  load_flags );

3.使用其他字符集

FT_Select_Charmap( FT_Face      face,
                     FT_Encoding  encoding );
// 使用示例
FT_CharMap  found = 0;
FT_CharMap  charmap;
int         n;
for ( n = 0; n < face->num_charmaps; n++ )
{
  charmap = face->charmaps[n];
  if ( charmap->platform_id == my_platform_id &&
       charmap->encoding_id == my_encoding_id )
  {
    found = charmap;
    break;
  }
}
if ( !found ) { ... }
/* now, select the charmap for the face object */
error = FT_Set_Charmap( face, found );
if ( error ) { ... }

4.字形变换

FT_Set_Transform( FT_Face     face,
                    FT_Matrix*  matrix,
                    FT_Vector*  delta );

此函数设置给定的当前转换 Face对象。它的第二个参数是指向描述 2×2 仿射矩阵的FT_Matrix结构的指针。这 第三个参数是指向的指针 一个FT_Vector结构,描述一个二维向量 在 2×2 之后翻译字形图像 转型。


简单的文本渲染

/* 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   640
#define HEIGHT  480
/* 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 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];
    }
  }
}
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         = ( 25.0 / 360 ) * 3.14159 * 2;      /* use 25 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,
                            100, 0 );                /* set character size */
  /* error handling omitted */
  /* cmap selection omitted;                                        */
  /* for simplicity we assume that the font contains a Unicode cmap */
  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 (300,200) relative to the upper left corner  */
  pen.x = 300 * 64;
  pen.y = ( target_height - 200 ) * 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 */


目录
相关文章
|
7月前
|
JavaScript API
Componsition API与Options API的对比
Componsition API与Options API的对比
25 0
|
API 开发工具 数据安全/隐私保护
如何调用 API | 学习笔记
简介:快速学习如何调用 API
992 0
如何调用 API | 学习笔记
|
12月前
|
JSON 监控 网络协议
针对podman REST API 的curl调用-2
针对podman REST API 的curl调用-2
113 0
针对podman REST API 的curl调用-2
|
12月前
|
XML JSON 算法
php 的 api 接口
php 的 api 接口
|
12月前
|
API 开发工具
|
关系型数据库 OLAP API
调用 API|学习笔记
快速学习调用 API
127 0
调用 API|学习笔记
|
编解码 API 数据处理
FFmpeg的API库介绍
FFmpeg的API库介绍
231 0
|
编解码 API 容器
FFmpeg基础API
FFmpeg基础API
225 0
|
机器学习/深度学习 JSON 文字识别