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_HFT_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 );
  1. 从Face加载字形
FT_Load_Glyph( FT_Face   face,FT_UInt   glyph_index,FT_Int32  load_flags );
  1. 使用其他字符集
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 ) { ... }
  1. 字形变换
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 */

Freetype 基本的API调用相关推荐

  1. Tensorflow C++ API调用Keras模型实现RGB图像语义分割

    我的实验是基于PSPNet模型实现二维图像的语义分割,下面的代码直接从得到的h5文件开始往下做... 也不知道是自己的检索能力出现了问题还是咋回事,搜遍全网都没有可以直接拿来用的语义分割代码,东拼西凑 ...

  2. novaclient的api调用流程与开发

    novaclient的api调用流程与开发 2015年07月05日 19:27:17 qiushanjushi 阅读数:3915 http://blog.csdn.net/tpiperatgod/ar ...

  3. Windows恶意软件API调用特征分析

    本文讲的是Windows恶意软件API调用特征分析, 1.背景 目标: 1)找到病毒调用概率高的API 2)找到病毒调用概率不高,但是当调用频次高的时候,是病毒概率高的API. 通常对病毒使用API的 ...

  4. WPF技术触屏上的应用系列(二): 嵌入百度地图、API调用及结合本地数据库在地图上进行自定义标点的实现...

    原文:WPF技术触屏上的应用系列(二): 嵌入百度地图.API调用及结合本地数据库在地图上进行自定义标点的实现 去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用.客户端是Window7操作系 ...

  5. saltstack一些常用模块和api调用方法

    研究可执行模块的时候,发现很多自带的模块已经很完善,可以帮助我们完成日常工作了,这里写入自己的记录操作: 1.使用salt.clien调用接口操作举例: >>> import sal ...

  6. 【Flutter】Dart 数据类型 字符串类型 ( 字符串定义 | 字符串拼接 | 字符串 API 调用 )

    文章目录 I . 字符串定义 I . 字符串拼接 III . 字符串 API 调用 IV . 字符串 Demo 示例 I . 字符串定义 使用单引号 ' ' 和 双引号 " " 都 ...

  7. 【阿里云API】 阿里云API调用的若干说明

    阿里云API 为了监控我们使用的一些阿里云产品,需要些一些脚本,定时调用这些脚本来获得相关阿里云产品的信息. ■ 概述 调用阿里云API大约分成两类方法,一个是直接从HTTP协议开始,自己根据阿里云的 ...

  8. Python 技术篇-20行代码实现微信机器人斗图功能实例演示!斗图啦官网API调用方法

    话不多说,看效果图: 先说下原理: 微信接收到你说的话,发给机器人来回复,用回复的话传参给斗图网,然后获得斗图网返回的图片,保存后把图片再发给跟你斗图的人. 斗图啦官网API调用文档 斗图啦官网 菜单 ...

  9. php 菜谱 源码,基于php的菜谱大全api调用代码实例

    代码描述:基于php的菜谱大全api调用代码实例 接口地址:http://www.juhe.cn/docs/api/id/46 PHP代码 // +-------------------------- ...

最新文章

  1. 学 Win32 汇编[17]: 关于压栈(PUSH)与出栈(POP) 之一
  2. Struts2+Spring详解
  3. ssl2290-潜水员【dp之二维费用】
  4. 最优化学习笔记(十二)——基本共轭方向算法(续)
  5. 2021中国实体零售数字化专题报告——便利店篇
  6. 启动器和选择器学习-----(1)总括
  7. mysql field id doesnt have a default value_为什么出现“Field ID'doesn't have a default value”?...
  8. BarnBridge出现前端错误
  9. 在使用pydelicious时出现HTTP Error 500: Internal Server Error的错误的解决方法:
  10. iOS开发手记-仿QQ音乐播放器动态歌词的实现
  11. 微信小程序 基础操作(边做边学2)
  12. java可不可以写挂_用Java怎么写呀???我怕是java要挂了,太艰难了
  13. 快速突破面试算法(内含从简到难的高频题型目录及每题的详解,已经归类整理好并外带博主的免费答疑)
  14. 网络安全哪个培训班比较好?挑战年薪百万的技术型人才
  15. 解决远程桌面无法全屏的方法
  16. mysql经典sql语句大全_经典SQL语句大全(sql查询语句大全集锦)
  17. PUE 1.2,总投资达36.4亿,17600个机柜!天和防务拟建陕西最大数据中心
  18. 如何使用代理ip软件的代理ip来维护你的隐私安全?
  19. wps插入C/C++代码
  20. command: robot --argumentfile

热门文章

  1. 「自控原理」5.1 频率特性及其图示
  2. 整理下使用yum localinstall做离线安装的点
  3. 什么是对称正定矩阵?
  4. 【NLP】Python NLTK结合Stanford NLP工具包进行分词、词性标注、句法分析
  5. 抖音电脑版怎么自动播放视频?
  6. python好玩的黑科技_用Python玩微信跳一跳黑科技详细使用教程
  7. __dirname与__filename
  8. 游戏 AI 设计之 FSM 有限状态机
  9. 【LSSVM回归预测】基于matlab人工蜂群算法优化最小二乘支持向量机LSSVM数据回归预测【含Matlab源码 2213期】
  10. 【LSSVM回归预测】基于matlab狮群算法优化最小二乘支持向量机LSO-LSSVM数据回归预测【含Matlab源码 2261期】