FreeType简介及在vs2010的编译使用
FreeType庫是一個開源、高質量、可擴展、可定制、可移植的字體引擎,它提供統一的接口來訪問多種字體格式文件,包括點陣字、TrueType、OpenType、Type1、CID、CFF、Windows FON/FNT、X11 PCF等。
FreeType有兩個License,一個是BSD-stype,它兼容GPLv3;另一個是GPLv2,在使用時可以根據需要選擇其中一種。
FreeType特點:(1)、它使得客戶應用程序可以方便地訪問字體文件,無論字體文件存儲在哪里,并且與字體格式無關;(2)、能方便地提取全局字體數據,這些數據普遍存在于一般的字體格式中(例如,全局度量標準、字符編碼/字符映射表等);(3)、能方便地提取某個字符的字形數據(例如,度量標準、圖像、名字等其它任何數據);(4)、具備訪問字體格式特定的功能(例如,SFNT表、多重控制、OpenType輪廓表等)。
???????? FreeType字體引擎執行流程:首先需要初始化FreeType庫,然后依次根據字符的編碼處理一個字符,把它轉化為點陣的位圖信息,最后根據字符的信息,在相應的地方把它顯示出來。
字體是一組可以被顯示和打印的多樣的字符映像,在單個字體中共享一些共有的特性,包括外表、風格、襯線等。
字符映像叫做字形,根據書寫、用法和上下文,單個字符能夠有多個不同的映像,即多個字形。多個字符也可以有一個字形(例如Roman)。
每個字符映像都關聯多種度量,被用來在渲染文本時,描述如何放置和管理它們。
字體輪廓的源格式是一組封閉的路徑,叫做輪廓線。每個輪廓線劃定字體的外部或內部區域,它們可以是線段或是Bezier曲線。
每個字形都有叫跨距和步進的距離,它們的定義是常量,但是它們的值依賴布局,同樣的字形可以用來渲染橫向或縱向文字。
字距調整指用來在一個文本串中調整重合字形的相對位置的特定信息。
FreeType輪廓:一個輪廓是2D平面上一系列封閉的輪廓線。每個輪廓線由一系列線段和Bezier弧組成,根據文件格式不同,曲線可以是二次和三次多項式,前者叫quadratic或conic弧,它們在TrueType格式中用到,后者叫cubic弧,多數用于Type1格式。每條弧由一系列起點、終點和控制點描述,輪廓的每個點有一個特定的標記,表示它用來描述一個線段還是一條弧。
邊界框(bbox)是一個完全包含指定輪廓的矩形,所要的是最小的邊界框。
控制框(cbox)是一個包含輪廓所有點的最小矩形,很明顯,它包含bbox,通常它們是一樣的。
一個位圖和像素圖通過一個叫FT_Bitmap的單一結構描述。
FreeType可以看作是一組組件,每個組件負責一部分任務,它們包括:(1)、客戶應用程序一般會調用FT高層API,它的功能都在一個組件中,叫做基礎層;(2)、根據上下文和環境,基礎層會調用一個或多個模塊進行工作,大多數情況下,客戶應用程序不知道使用哪個模塊;(3)、基礎層還包含一組例程來進行一些共通處理,例如內存分配、列表處理、io流解析、固定點計算等,這些函數可以被模塊隨意調用,它們形成了一個底層基礎API。
雖然FreeType是使用ANSI C編寫,但是采用面向對象的思想,使這個庫非常容易擴展,因此,有一些代碼規約:(1)、每個對象類型/類都有一個對應的結構類型和一個對應的結構指針類型,后者稱為類型/類的句柄類型;(2)、類繼承通過將基類包裝到一個新類中實現。
在FreeType中有若干種模塊:(1)、渲染模塊:用來管理可縮放的字形映像;(2)、字體驅動模塊:用來支持一種或多種特定字體格式;(3)、助手模塊:用來處理一些共享代碼,通常被多個字體驅動,甚至是其它模塊使用;(4)、autohinter模塊:當一個字體驅動沒有提供自己的hint引擎時,它可以在字形裝載時處理各自的字形輪廓。
如果我們只希望對特定字體做簡單的事情,則可以對FreeType進行裁決,有兩種方式:(1)、修改/modules.cfg文件;(2)、修改/include/config/ftmodule.h文件。
???????? 下載的源代碼中freetype-2.5.5,/builds/windows/vc2010目錄下有配置好的vs2010工程,打開編譯,將會在/objs/vc2010,Win32或x64目錄里生成相應的freetype255.lib和freetype255d.lib相應靜態庫。由于后期可能需要對FreeType進行裁決,所以這里通過源碼自己編譯下:
?
1.????????從https://sourceforge.net/projects/freetype/files/下載最新的穩定版本ft255,解壓縮;
2.????????新建一個libFreeType靜態庫工程,將/include和/src下的.h和.c文件加入到此工程中,除/src/tools目錄下的.h、.c文件,/src下的其它大部分模塊,每個模塊僅添加一個模塊名的.c文件即可,如autofit模塊,僅添加autofit.h即可,因為此文件中include了此模塊中相應的.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.????????新建一個FreeTypetest控制臺工程,驗證生成庫的正確性,各個文件內容為(測試代碼來自于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;
}
?
參考文獻:
?
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
?
總結
以上是生活随笔為你收集整理的FreeType简介及在vs2010的编译使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wiki的使用
- 下一篇: 开源库Simd在vs2010中的编译及简