The FreeType Library is an open source, high quality, extensible, customizable, portable font engine that provides a unified interface for accessing multiple font format files, including dot Matrix, TrueType, OpenType, Type1, CID, CFF, Windows FON/FNT, X11 PCF and so on.
FreeType has two license, one is Bsd-stype, it is compatible with GPLv3, and the other is GPLv2, which can be selected as needed when used.
FreeType Features: (1), it makes it easy for the client application to access the font file, regardless of where the font file is stored and regardless of the font format, (2), the ability to easily extract global font data, which is commonly found in the General font format (for example, global metrics, character encoding/ Character mapping table, etc.), (3), can easily extract a character glyph data (for example, metrics, images, names and other other data), (4), with access to font format-specific features (for example, SFNT table, multi-control, OpenType contour table, etc.).
FreeType font engine execution process : First need to initialize the FreeType library, and then sequentially according to the character encoding processing a character, convert it to the bitmap information lattice, and finally according to the information of the character, in the corresponding place to display it.
A font is a group of mappings images that can be displayed and printed, sharing some common features in a single font, including appearances, styles, liners, and so on.
The character mappings is called a glyph, and depending on the writing, usage, and context, a single character can have multiple different images, that is, multiple glyphs. Multiple characters can also have a glyph (for example, Roman).
Each character mappings is associated with several measures, and is used to describe how to place and manage the text as it is rendered.
The source format for font outlines is a set of closed paths, called footprints. Each contour line delimits the outer or inner area of the font, which can be a line segment or Bezier curve.
Each glyph has distances that are called spans and steps, and they are defined as constants, but their values depend on the layout, and the same glyphs can be used to render horizontal or vertical text.
Kerning refers to the specific information used to adjust the relative position of the coincident glyphs in a text string.
FreeType Contour: A contour is a series of closed contour lines on the 2D plane. Each contour line consists of a series of segments and Bézier arcs, depending on the file format, the curve can be two and three polynomial, the former is called quadratic or conic arc, they are used in the TrueType format, the latter is called Cubic arc, most for Type1 format. Each arc is described by a series of start points, endpoints, and control points, and each point of the contour has a specific marker indicating whether it is used to describe a segment or an arc.
A bounding box (bbox) is a rectangle that completely contains the specified outline, with the smallest bounding box.
The control box (Cbox) is a minimum rectangle that contains all the points of the contour, and it is obvious that it contains bbox, which is usually the same.
A bitmap and pixel graph is described by a single structure called Ft_bitmap.
FreeType can be thought of as a set of components, each of which is responsible for a subset of tasks, including: (1), the client application typically calls the FT high-level API, which functions in one component, called the base layer, and (2), depending on context and environment, the base layer invokes one or more modules to work on, In most cases, the client application does not know which module to use, (3), the base layer also contains a set of routines to do some common processing, such as memory allocation, list processing, IO stream parsing, fixed point calculation, and so on, these functions can be arbitrarily called by the module, they form a basic underlying API.
Although FreeType is written using ANSI C, the object-oriented idea makes the library very easy to extend, so there are some code specifications: (1), each object type/class has a corresponding struct type and a corresponding struct pointer type, which is called the type/class handle type; (2), Class inheritance is implemented by wrapping a base class into a new class.
There are several kinds of modules in FreeType: (1), rendering module: Used to manage the scalable glyph image, (2), font driver module: Used to support one or more specific font formats, (3), helper module: Used to handle some shared code, usually driven by multiple fonts, or even other modules used; (4), Autohinter module: When a font driver does not provide its own hint engine, it can handle the individual glyph outlines when the glyph is loaded.
If we only want to do simple things for a particular font, we can rule on freetype, there are two ways: (1), modify the/modules.cfg file, (2), modify the/include/config/ftmodule.h file.
Download the source code in the freetype-2.5.5,/builds/windows/vc2010 directory under the configuration of the VS2010 project, open the compilation, will be in/objs/vc2010, The corresponding static libraries for Freetype255.lib and Freetype255d.lib are generated in the Win32 or x64 directory. Because FreeType may need to be decided later, it is compiled by the source code itself:
1. Download the latest stable version ft255 from https://sourceforge.net/projects/freetype/files/and unzip it;
2. Create a new Libfreetype static library project to add the. h and. c files under/include and/SRC to this project, in addition to the. h,. c files in the/src/tools directory, and most of the other modules under/SRC, A. c file with only one module name is added to each module, such as the AutoFit module, only add autofit.h, because this file has the corresponding. c file in this module, and there are other. c files that need to be added, FTBITMAP.C, FTGLYPH.C, Ftinit.c, FTSYSTEM.C;
3. Add the/include directory to C + +---Additional Include directories;
4. Add the Ft2_build_library to C + +---preprocessor-preprocessor definitions;
5. Create a new Freetypetest console project to verify the correctness of the build library and the contents of each file (test code from 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 is later used to #include the//appropriate public FreeType 2 headers files. #include "Ft2build.h"//ft_freetype_h is a special macro defined I N File ftheader.h. It contains some//installation-specific macros to name other public header files of the the FreeType 2 API. #include Ft_freetyp E_h#include "FTGLYPH.H"
Stdafx.cpp:
#include "stdafx.h"//todo:reference any additional headers the 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;/* H Andle 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 Funct Ion 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, it font format is unsupported" <<endl;return-1;} else if (error) {cout<< "The font file could not being opened or read, or the 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 is Accessed directly by dereferencing a handle, as 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 fo R a given face.//error = ft_set_pixel_sizes (face, 0, +); 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); "A"//once you has a glyph index, you can load the CorreSponding Glyph imageerror = Ft_load_glyph (Face, CharIndex, ft_load_default); if (error) {cout<< "A 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 Ft_glyph_to_bitmap Gray (&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;}
Reference documents:
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
Introduction of FreeType and its compiling and use in VS2010