關於freetype的移植和其官方demos的使用總結
轉載時請標明出處 做者聯繫方式QQ:854290197html
1、Windows下移植feetype到工程中
步驟以下:windows
1.從官網上下載最新的freetype庫 下載地址:https://www.freetype.org/download.html工具
2.生成freetype的動態庫 因爲Windows使用make不方便且生成的庫文件容易與x64系統產生衝突,建議在Windows下先安裝visual studio。freetype已經搭建好了vs工程,只須要編譯便可生成工程。 ①進入freetype工程目錄,進入build文件夾->進入windows文件夾->點擊vc2015文件夾->打開freetype.sln工程 ②編譯freetype的VS工程,點擊生成->點擊生成解決方案。(此時生成的是x86的動態庫,如需生成 x64的動態庫可在Debug裏切換到x64,如圖所示 ) ③拷貝生成的動態庫到項目工程中,進入objs文件夾->進入Win32文件夾->進入Debug文件夾->拷貝freetype.lib和freetype.dll文件到工程中。學習
3.將freetype的頭文件添加到本身的工程項目中 可將freetype下的nclude文件夾整個添加進工程項目中測試
4.編譯時添加freetype.lib的路徑和頭文件的路徑字體
2、Linux下移植feetype到工程中
在Linux下可用make編譯整個freetype庫。但須要注意的是,從官網上下載freetpye壓縮包時,要下載.xz或者.gz格式到壓縮包到Linux下解壓使用,或者將沒法運行配置文件。ui
- 解壓freetype的壓縮包
- 命令行輸入:$./configure
- 命令行輸入:$ make
- 命令行輸入:$make install
依次輸入上面的命令後即可在/usr/local/lib/文件夾下找到相應的庫文件(若是生成的庫文件須要安裝到其餘文件夾下 ,可在運行configure文件時指定路徑。如。/configure –prefix=/usr)this
3、使用freetype的例程
在官網的freetype-docs的tutorial文件下有個example1.c文件,是一個簡單測試例程,在這個例程中你可以看到如何簡單的使用freetype。 它大體包括這幾個步驟:spa
1.初始化freetype庫 2.建立face對象 3.設置字體的大小 4.加載字符 5.在槽裏面提取字符的位圖數據 例程以下,爲了方面看到效果,作了以下幾處修改:命令行
/* 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 /* 這裏修改 原來是680 480 太大 */ #define WIDTH 80 #define HEIGHT 80 /* 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 ); /* 角度設爲0不旋轉 */ angle = ( 0.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 */ #if 1 /* use 50pt at 100dpi */ error = FT_Set_Char_Size( face, 50 * 64, 0, 0,100 ); /* set character size */ /* error handling omitted */ #else error = FT_Set_Pixel_Sizes( face, /* handle to face object */ 0, /* pixel_width */ 100 ); /* pixel_height */ #endif /* 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 = 0 * 64; pen.y = ( target_height - 40 ) * 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; }
安裝了vs的用戶,可在vs的命令行編譯例程。(需指名頭文件和庫文件的路徑) 運行example1.exe文件 ,運行時需指明要解析的字體文件和字體。
freetype官方也提供了查看完整字體的demos例程。 1.從官網下載freetype-demos(這裏依然要下載.xz或者.gz格式的壓縮包) 2. 解壓freetype的壓縮包 3. 命令行輸入:/configure 4.命令行輸入: make 5.命令行輸入:make install
在編譯工程的時候可能會出現如下問題:
__pngconf.h__ already includes setjmp.h; __dont__ include it again.;
意思是freetype 裏也include 了setjmp.h?
查看 freetype 代碼。果真
freetype-2.9/src/sfnt/pngshim.c:30: /* We always include <setjmp.h>, so make libpng shut up! */
彷佛freetype 與 libpng 有着不可描述的故事,頗有意思。 解決方法: 1.升級pnglib庫到16。由於SDK包是基於12.8的,升級到16可能會出現新的問題,此時能夠用make工具的記憶性又將16的庫卸載,再用12.8的庫make編譯一遍。 2.把freetype中include 「setjmp.h」 的地方挪到一個新的頭文件中, 在沒有include libpng頭文件, 而又須要setjmp的地方,再include 這個新的頭文件。 在freetype裏幾處調整以後,最終能夠編譯經過, 整理補丁以下,
diff -Naurp freetype-2.9_org/include/for_png_1_2_X.h freetype-2.9/include/for_png_1_2_X.h --- freetype-2.9_org/include/for_png_1_2_X.h 1970-01-01 08:00:00.000000000 +0800 +++ freetype-2.9/include/for_png_1_2_X.h 2020-09-04 15:29:42.428884604 +0800 @@ -0,0 +1,17 @@ +/* +Fix setjmp.h conflict when using png 1.2.X +*/ +#ifndef FOR_PNG_1_2_X_H +#define FOR_PNG_1_2_X_H + +#include <setjmp.h> + +#define ft_jmp_buf jmp_buf /* note: this cannot be a typedef since */ + /* jmp_buf is defined as a macro */ + /* on certain platforms */ + +#define ft_longjmp longjmp +#define ft_setjmp( b ) setjmp( *(ft_jmp_buf*) &(b) ) /* same thing here */ + +#endif + diff -Naurp freetype-2.9_org/include/freetype/config/ftstdlib.h freetype-2.9/include/freetype/config/ftstdlib.h --- freetype-2.9_org/include/freetype/config/ftstdlib.h 2020-09-04 15:33:27.253502905 +0800 +++ freetype-2.9/include/freetype/config/ftstdlib.h 2020-09-04 15:31:56.921021663 +0800 @@ -152,7 +152,7 @@ /* */ /**********************************************************************/ - +#if 0 /*move it to "for_png_1_2_X.h" when using png1.2.X. fix setjmp.h conflict issue*/ #include <setjmp.h> #define ft_jmp_buf jmp_buf /* note: this cannot be a typedef since */ @@ -161,7 +161,7 @@ #define ft_longjmp longjmp #define ft_setjmp( b ) setjmp( *(ft_jmp_buf*) &(b) ) /* same thing here */ - +#endif /* the following is only used for debugging purposes, i.e., if */ /* FT_DEBUG_LEVEL_ERROR or FT_DEBUG_LEVEL_TRACE are defined */ diff -Naurp freetype-2.9_org/include/freetype/internal/ftvalid.h freetype-2.9/include/freetype/internal/ftvalid.h --- freetype-2.9_org/include/freetype/internal/ftvalid.h 2020-09-04 15:33:27.253502905 +0800 +++ freetype-2.9/include/freetype/internal/ftvalid.h 2020-09-04 15:32:23.158323991 +0800 @@ -22,6 +22,7 @@ #include <ft2build.h> #include FT_CONFIG_STANDARD_LIBRARY_H /* for ft_setjmp and ft_longjmp */ +#include "for_png_1_2_X.h" FT_BEGIN_HEADER diff -Naurp freetype-2.9_org/src/sfnt/pngshim.c freetype-2.9/src/sfnt/pngshim.c --- freetype-2.9_org/src/sfnt/pngshim.c 2020-09-04 15:33:27.217502806 +0800 +++ freetype-2.9/src/sfnt/pngshim.c 2020-09-04 15:32:10.170055530 +0800 @@ -34,6 +34,7 @@ #include "sferrors.h" +#include "for_png_1_2_X.h" /* This code is freely based on cairo-png.c. There's so many ways */ /* to call libpng, and the way cairo does it is defacto standard. */ diff -Naurp freetype-2.9_org/src/smooth/ftgrays.c freetype-2.9/src/smooth/ftgrays.c --- freetype-2.9_org/src/smooth/ftgrays.c 2020-09-04 15:33:27.221502818 +0800 +++ freetype-2.9/src/smooth/ftgrays.c 2020-09-04 15:32:00.928953980 +0800 @@ -285,6 +285,7 @@ typedef ptrdiff_t FT_PtrDist; #define Smooth_Err_Memory_Overflow Smooth_Err_Out_Of_Memory #define ErrRaster_Memory_Overflow Smooth_Err_Out_Of_Memory +#include "for_png_1_2_X.h" #endif /* !STANDALONE_ */
編譯經過以後即可運行bin文件夾下的應用程序,解析文本的字體並完整的顯示。
以上是我移植freetype記錄下來的學習心得,以後但願有更多好的內容與你們分享