FreeType简介及在vs2010的编译使用
FreeType庫是一個(gè)開源、高質(zhì)量、可擴(kuò)展、可定制、可移植的字體引擎,它提供統(tǒng)一的接口來訪問多種字體格式文件,包括點(diǎn)陣字、TrueType、OpenType、Type1、CID、CFF、Windows FON/FNT、X11 PCF等。
FreeType有兩個(gè)License,一個(gè)是BSD-stype,它兼容GPLv3;另一個(gè)是GPLv2,在使用時(shí)可以根據(jù)需要選擇其中一種。
FreeType特點(diǎn):(1)、它使得客戶應(yīng)用程序可以方便地訪問字體文件,無論字體文件存儲(chǔ)在哪里,并且與字體格式無關(guān);(2)、能方便地提取全局字體數(shù)據(jù),這些數(shù)據(jù)普遍存在于一般的字體格式中(例如,全局度量標(biāo)準(zhǔn)、字符編碼/字符映射表等);(3)、能方便地提取某個(gè)字符的字形數(shù)據(jù)(例如,度量標(biāo)準(zhǔn)、圖像、名字等其它任何數(shù)據(jù));(4)、具備訪問字體格式特定的功能(例如,SFNT表、多重控制、OpenType輪廓表等)。
???????? FreeType字體引擎執(zhí)行流程:首先需要初始化FreeType庫,然后依次根據(jù)字符的編碼處理一個(gè)字符,把它轉(zhuǎn)化為點(diǎn)陣的位圖信息,最后根據(jù)字符的信息,在相應(yīng)的地方把它顯示出來。
字體是一組可以被顯示和打印的多樣的字符映像,在單個(gè)字體中共享一些共有的特性,包括外表、風(fēng)格、襯線等。
字符映像叫做字形,根據(jù)書寫、用法和上下文,單個(gè)字符能夠有多個(gè)不同的映像,即多個(gè)字形。多個(gè)字符也可以有一個(gè)字形(例如Roman)。
每個(gè)字符映像都關(guān)聯(lián)多種度量,被用來在渲染文本時(shí),描述如何放置和管理它們。
字體輪廓的源格式是一組封閉的路徑,叫做輪廓線。每個(gè)輪廓線劃定字體的外部或內(nèi)部區(qū)域,它們可以是線段或是Bezier曲線。
每個(gè)字形都有叫跨距和步進(jìn)的距離,它們的定義是常量,但是它們的值依賴布局,同樣的字形可以用來渲染橫向或縱向文字。
字距調(diào)整指用來在一個(gè)文本串中調(diào)整重合字形的相對(duì)位置的特定信息。
FreeType輪廓:一個(gè)輪廓是2D平面上一系列封閉的輪廓線。每個(gè)輪廓線由一系列線段和Bezier弧組成,根據(jù)文件格式不同,曲線可以是二次和三次多項(xiàng)式,前者叫quadratic或conic弧,它們?cè)赥rueType格式中用到,后者叫cubic弧,多數(shù)用于Type1格式。每條弧由一系列起點(diǎn)、終點(diǎn)和控制點(diǎn)描述,輪廓的每個(gè)點(diǎn)有一個(gè)特定的標(biāo)記,表示它用來描述一個(gè)線段還是一條弧。
邊界框(bbox)是一個(gè)完全包含指定輪廓的矩形,所要的是最小的邊界框。
控制框(cbox)是一個(gè)包含輪廓所有點(diǎn)的最小矩形,很明顯,它包含bbox,通常它們是一樣的。
一個(gè)位圖和像素圖通過一個(gè)叫FT_Bitmap的單一結(jié)構(gòu)描述。
FreeType可以看作是一組組件,每個(gè)組件負(fù)責(zé)一部分任務(wù),它們包括:(1)、客戶應(yīng)用程序一般會(huì)調(diào)用FT高層API,它的功能都在一個(gè)組件中,叫做基礎(chǔ)層;(2)、根據(jù)上下文和環(huán)境,基礎(chǔ)層會(huì)調(diào)用一個(gè)或多個(gè)模塊進(jìn)行工作,大多數(shù)情況下,客戶應(yīng)用程序不知道使用哪個(gè)模塊;(3)、基礎(chǔ)層還包含一組例程來進(jìn)行一些共通處理,例如內(nèi)存分配、列表處理、io流解析、固定點(diǎn)計(jì)算等,這些函數(shù)可以被模塊隨意調(diào)用,它們形成了一個(gè)底層基礎(chǔ)API。
雖然FreeType是使用ANSI C編寫,但是采用面向?qū)ο蟮乃枷?#xff0c;使這個(gè)庫非常容易擴(kuò)展,因此,有一些代碼規(guī)約:(1)、每個(gè)對(duì)象類型/類都有一個(gè)對(duì)應(yīng)的結(jié)構(gòu)類型和一個(gè)對(duì)應(yīng)的結(jié)構(gòu)指針類型,后者稱為類型/類的句柄類型;(2)、類繼承通過將基類包裝到一個(gè)新類中實(shí)現(xiàn)。
在FreeType中有若干種模塊:(1)、渲染模塊:用來管理可縮放的字形映像;(2)、字體驅(qū)動(dòng)模塊:用來支持一種或多種特定字體格式;(3)、助手模塊:用來處理一些共享代碼,通常被多個(gè)字體驅(qū)動(dòng),甚至是其它模塊使用;(4)、autohinter模塊:當(dāng)一個(gè)字體驅(qū)動(dòng)沒有提供自己的hint引擎時(shí),它可以在字形裝載時(shí)處理各自的字形輪廓。
如果我們只希望對(duì)特定字體做簡單的事情,則可以對(duì)FreeType進(jìn)行裁決,有兩種方式:(1)、修改/modules.cfg文件;(2)、修改/include/config/ftmodule.h文件。
???????? 下載的源代碼中freetype-2.5.5,/builds/windows/vc2010目錄下有配置好的vs2010工程,打開編譯,將會(huì)在/objs/vc2010,Win32或x64目錄里生成相應(yīng)的freetype255.lib和freetype255d.lib相應(yīng)靜態(tài)庫。由于后期可能需要對(duì)FreeType進(jìn)行裁決,所以這里通過源碼自己編譯下:
?
1.????????從https://sourceforge.net/projects/freetype/files/下載最新的穩(wěn)定版本ft255,解壓縮;
2.????????新建一個(gè)libFreeType靜態(tài)庫工程,將/include和/src下的.h和.c文件加入到此工程中,除/src/tools目錄下的.h、.c文件,/src下的其它大部分模塊,每個(gè)模塊僅添加一個(gè)模塊名的.c文件即可,如autofit模塊,僅添加autofit.h即可,因?yàn)榇宋募衖nclude了此模塊中相應(yīng)的.c文件,還有另外一些.c文件需添加,ftbitmap.c、ftglyph.c、ftinit.c、ftsystem.c;
3.????????將/include目錄添加到C/C++ --> General --> Additional Include Directories;
4.????????將FT2_BUILD_LIBRARY添加到C/C++ --> Preprocessor --> Preprocessor Definitions;
5.????????新建一個(gè)FreeTypetest控制臺(tái)工程,驗(yàn)證生成庫的正確性,各個(gè)文件內(nèi)容為(測試代碼來自于http://www.freetype.org/freetype2/docs/tutorial/step1.html):
stdafx.h:
?
#pragma once#include "targetver.h"#include <stdio.h>//It contains various macro declarations that are later used to #include the
//appropriate public FreeType 2 header files.
#include "ft2build.h"
//FT_FREETYPE_H is a special macro defined in file ftheader.h. It contains some
//installation-specific macros to name other public header files of the FreeType 2 API.
#include FT_FREETYPE_H#include "ftglyph.h"
stdafx.cpp:
?
?
#include "stdafx.h"// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
#ifdef _DEBUG#pragma comment(lib, "../../../lib/dbg/x86_vc10/libFreeType[dbg_x86_vc10].lib")
#else#pragma comment(lib, "../../../lib/rel/x86_vc10/libFreeType[rel_x86_vc10].lib")
#endif
FreeTypetest.cpp:
?
?
#include "stdafx.h"
#include <iostream>using namespace std;int main(int argc, char* argv[])
{FT_Library library; /* handle to library */FT_Error error; FT_Face face; /* handle to face object *///1. Library Initialization//To initialize the FreeType library, create a variable of type FT_Library named, and call the function FT_Init_FreeType.error = FT_Init_FreeType(&library); if (error) { cout<<"an error occurred during library initialization"<<endl; return -1; } //2. Loading a Font Face//From a Font File, Create a new face object by calling FT_New_Face. A face describes a given typeface and style.char* filename = "../../../testdata/kaiu.ttf";error = FT_New_Face(library, filename, 0, &face);if (error == FT_Err_Unknown_File_Format) {cout<<"the font file could be opened and read, its font format is unsupported"<<endl;return -1;} else if (error) {cout<<"the font file could not be opened or read, or that it is broken"<<endl;return -1;}//3. Accessing the Face Data//A face object models all information that globally describes the face.//Usually, this data can be accessed directly by dereferencing a handle, like in face?>num_glyphs.FT_Long numGlygphs = face->num_glyphs;FT_Long numFaces = face->num_faces;FT_String* familyName = face->family_name;cout<<"num_glyphs = "<<numGlygphs<<", num_faces = "<<numFaces<<", family_name = "<<familyName<<endl;//4. Setting the Current Pixel Size//FreeType 2 uses size objects to model all information related to a given character size for a given face.//error = FT_Set_Pixel_Sizes(face, 0, 16);error = FT_Set_Char_Size(face, 50 * 64, 0, 100, 0); if (error) {cout<<"an error occurs when trying to set the pixel size to a value"<<endl;return -1;}//5. Loading a Glyph Image//Converting a Character Code Into a Glyph IndexFT_UInt charIndex = FT_Get_Char_Index(face, 65); //65 => 'A'//Once you have a glyph index, you can load the corresponding glyph imageerror = FT_Load_Glyph(face, charIndex, FT_LOAD_DEFAULT);if (error) {cout<<"an error occurs when trying to load the corresponding glgyh image"<<endl;return -1;}//6. Simple Text Renderingerror = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);if (error) {cout<<"an error occurs when trying to render glyph"<<endl;return -1;}FT_Glyph glyph;error = FT_Get_Glyph(face->glyph, &glyph);if (error) {cout<<"get glyph error"<<endl;return -1;}//convert glyph to bitmap with 256 gray FT_Glyph_To_Bitmap(&glyph, ft_render_mode_normal, 0, 1); FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph; FT_Bitmap& bitmap = bitmap_glyph->bitmap; for (int i = 0 ; i < bitmap.rows; ++ i) { for (int j = 0 ; j < bitmap.width; ++ j) { //if it has gray > 0 we set show it as 1, 0 otherwise printf(" %d ", bitmap.buffer[i * bitmap.width + j] ? 1 : 0); } cout<<endl; }FT_Done_Glyph(glyph);FT_Done_Face(face);FT_Done_FreeType(library);cout<<"ok!"<<endl;return 0;
}
?
參考文獻(xiàn):
?
1.??????http://wenku.baidu.com/link?url=66OQSAxrwbLGuaeylVH-fyDm1KDEe9hEvreZKzkwuJVCNlOa1Tth598tEDzskuKvW6yrpl8qcym1BbCPJFS_W4xSTsRmNu18zVFyy_Hcgcu
2.??????http://www.docin.com/p-595358316.html??
?
3.??????http://www.cnblogs.com/kex1n/archive/2010/11/24/2286445.html
?
GitHub:https://github.com/fengbingchun/FreeType_Test
?
總結(jié)
以上是生活随笔為你收集整理的FreeType简介及在vs2010的编译使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wiki的使用
- 下一篇: 开源库Simd在vs2010中的编译及简