vs移植的.exe再编译无法更新_关于freetype的移植和其官方demos的使用总结

# 关于freetype的移植和其官方demos的使用总结 转载时请标明出处 作者联系方式QQ:854290197

一、Windows下移植feetype到工程中

步骤如下:

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,如图所示 )

f89f6e4b077413f6e4aefb0db58dadf3.png

③拷贝生成的动态库到项目工程中,进入objs文件夹->进入Win32文件夹->进入Debug文件夹->拷贝freetype.lib和freetype.dll文件到工程中。

3.将freetype的头文件添加到自己的工程项目中 可将freetype下的nclude文件夹整个添加进工程项目中

4.编译时添加freetype.lib的路径和头文件的路径

二、Linux下移植feetype到工程中

在Linux下可用make编译整个freetype库。但需要注意的是,从官网上下载freetpye压缩包时,要下载.xz或者.gz格式到压缩包到Linux下解压使用,或者将无法运行配置文件。

  • 解压freetype的压缩包
  • 命令行输入:$./configure
  • 命令行输入:$ make
  • 命令行输入:$make install

依次输入上面的命令后便可在/usr/local/lib/文件夹下找到相应的库文件(如果生成的库文件需要安装到其他文件夹下 ,可在运行configure文件时指定路径。如。/configure –prefix=/usr)

三、使用freetype的例程

在官网的freetype-docs的tutorial文件下有个example1.c文件,是一个简单测试例程,在这个例程中你能够看到如何简单的使用freetype。 它大致包括这几个步骤:

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-textn", 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的命令行编译例程。(需指名头文件和库文件的路径)

42c84eb1861a5d2d5e9ef99109810894.png

运行example1.exe文件 ,运行时需指明要解析的字体文件和字体。

b84cf7c7627b2dc1e1631d5f4fe8b123.png

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 , 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记录下来的学习心得,之后希望有更多好的内容与大家分享关于freetype的移植和其官方demos的使用总结

转载时请标明出处
作者联系方式QQ:854290197
文章目录

  • 关于freetype的移植和其官方demos的使用总结
  • 一、Windows下移植feetype到工程中
  • 二、Linux下移植feetype到工程中
  • 三、使用freetype的例程

一、Windows下移植feetype到工程中

步骤如下:

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,如图所示 )

f89f6e4b077413f6e4aefb0db58dadf3.png


③拷贝生成的动态库到项目工程中,进入objs文件夹->进入Win32文件夹->进入Debug文件夹->拷贝freetype.lib和freetype.dll文件到工程中。

3.将freetype的头文件添加到自己的工程项目中
可将freetype下的nclude文件夹整个添加进工程项目中

4.编译时添加freetype.lib的路径和头文件的路径

二、Linux下移植feetype到工程中

在Linux下可用make编译整个freetype库。但需要注意的是,从官网上下载freetpye压缩包时,要下载.xz或者.gz格式到压缩包到Linux下解压使用,或者将无法运行配置文件。

  • 解压freetype的压缩包
  • 命令行输入:$./configure
  • 命令行输入:$ make
  • 命令行输入:$make install

依次输入上面的命令后便可在/usr/local/lib/文件夹下找到相应的库文件(如果生成的库文件需要安装到其他文件夹下 ,可在运行configure文件时指定路径。如。/configure –prefix=/usr)

三、使用freetype的例程

在官网的freetype-docs的tutorial文件下有个example1.c文件,是一个简单测试例程,在这个例程中你能够看到如何简单的使用freetype。
它大致包括这几个步骤:

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-textn", 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的命令行编译例程。(需指名头文件和库文件的路径)

42c84eb1861a5d2d5e9ef99109810894.png


运行example1.exe文件 ,运行时需指明要解析的字体文件和字体。

b84cf7c7627b2dc1e1631d5f4fe8b123.png


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记录下来的学习心得,之后希望有更多好的内容与大家分享

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值