關(guān)于freetype的移植和其官方demos的使用總結(jié)

關(guān)于freetype的移植和其官方demos的使用總結(jié)

轉(zhuǎn)載時(shí)請(qǐng)標(biāo)明出處
作者聯(lián)系方式QQ:854290197

一、Windows下移植feetype到工程中

步驟如下:

1.從官網(wǎng)上下載最新的freetype庫
下載地址:https://www.freetype.org/download.html

2.生成freetype的動(dòng)態(tài)庫
由于Windows使用make不方便且生成的庫文件容易與x64系統(tǒng)產(chǎn)生沖突,建議在Windows下先安裝visual studio。freetype已經(jīng)搭建好了vs工程,只需要編譯即可生成工程。
①進(jìn)入freetype工程目錄,進(jìn)入build文件夾->進(jìn)入windows文件夾->點(diǎn)擊vc2015文件夾->打開freetype.sln工程
②編譯freetype的VS工程,點(diǎn)擊生成->點(diǎn)擊生成解決方案。(此時(shí)生成的是x86的動(dòng)態(tài)庫,如需生成
x64的動(dòng)態(tài)庫可在Debug里切換到x64,如圖所示 )[圖片上傳失敗...(image-afc484-1604417733724)]
③拷貝生成的動(dòng)態(tài)庫到項(xiàng)目工程中,進(jìn)入objs文件夾->進(jìn)入Win32文件夾->進(jìn)入Debug文件夾->拷貝freetype.lib和freetype.dll文件到工程中。

3.將freetype的頭文件添加到自己的工程項(xiàng)目中
可將freetype下的nclude文件夾整個(gè)添加進(jìn)工程項(xiàng)目中

4.編譯時(shí)添加freetype.lib的路徑和頭文件的路徑

二、Linux下移植feetype到工程中

在Linux下可用make編譯整個(gè)freetype庫。但需要注意的是,從官網(wǎng)上下載freetpye壓縮包時(shí),要下載.xz或者.gz格式到壓縮包到Linux下解壓使用,或者將無法運(yùn)行配置文件。

  • 解壓freetype的壓縮包
  • 命令行輸入:$./configure
  • 命令行輸入:$ make
  • 命令行輸入:$make install

依次輸入上面的命令后便可在/usr/local/lib/文件夾下找到相應(yīng)的庫文件(如果生成的庫文件需要安裝到其他文件夾下 ,可在運(yùn)行configure文件時(shí)指定路徑。如。/configure –prefix=/usr)

三、使用freetype的例程

在官網(wǎng)的freetype-docs的tutorial文件下有個(gè)example1.c文件,是一個(gè)簡(jiǎn)單測(cè)試?yán)?,在這個(gè)例程中你能夠看到如何簡(jiǎn)單的使用freetype。
它大致包括這幾個(gè)步驟:

1.初始化freetype庫
2.創(chuàng)建face對(duì)象
3.設(shè)置字體的大小
4.加載字符
5.在槽里面提取字符的位圖數(shù)據(jù)
例程如下,為了方面看到效果,做了如下幾處修改:

/* 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 );
    /* 角度設(shè)為0不旋轉(zhuǎn) */
    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  */
    /* 這里也要改 因?yàn)樯厦娓牧?*/
    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的命令行編譯例程。(需指名頭文件和庫文件的路徑)


在這里插入圖片描述

運(yùn)行example1.exe文件 ,運(yùn)行時(shí)需指明要解析的字體文件和字體。

在這里插入圖片描述

freetype官方也提供了查看完整字體的demos例程。
1.從官網(wǎng)下載freetype-demos(這里依然要下載.xz或者.gz格式的壓縮包)

  1. 解壓freetype的壓縮包
  2. 命令行輸入:/configure
    4.命令行輸入: make
    5.命令行輸入:make install

在編譯工程的時(shí)候可能會(huì)出現(xiàn)以下問題:

      __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.升級(jí)pnglib庫到16。因?yàn)镾DK包是基于12.8的,升級(jí)到16可能會(huì)出現(xiàn)新的問題,此時(shí)可以用make工具的記憶性又將16的庫卸載,再用12.8的庫make編譯一遍。
2.把freetype中include “setjmp.h” 的地方挪到一個(gè)新的頭文件中,
在沒有include libpng頭文件, 而又需要setjmp的地方,再include 這個(gè)新的頭文件。
在freetype里幾處調(diào)整之后,最終可以編譯通過,
整理補(bǔ)丁如下,

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_ */

編譯通過之后便可運(yùn)行bin文件夾下的應(yīng)用程序,解析文本的字體并完整的顯示。


以上是我移植freetype記錄下來的學(xué)習(xí)心得,之后希望有更多好的內(nèi)容與大家分享

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容