FreeType 矢量字體 測試移植(1)

 以前有作過 ascii 和漢字庫的字體點陣在lcd上顯示的例子,都是按照指定大小的字庫的點陣來顯示的,因此一但選定了字體文件後,就固定了大小,不可變化,固然也能夠存放各類 大小的字體文件,但這樣的話就須要不少的空間,這種方法顯然很差使,因此就引入了失量字體,關於字體的特色就不囉嗦了。能夠去網上搜到不少說明。下面咱們一步一步的來作實驗測試瞭解失量字體的用法,先在PC機上測試,而後再移植到開發板上用lcd顯示。html

  1、首先咱們要去得到失量字體的源碼和一些文檔,https://www.freetype.org/freetype2/docs/documentation.html 這裏就是官網。而後按照他的文檔來簡單瞭解一下,同時還提供了一個C例子,分析例子,修改後能夠先在PC機上測試。linux

  2、獲得源碼後,解壓配置安裝。列出步驟和命令。注意這是在PC機上運行的命令若是要在開發板上運行,要用交叉編譯 還有配置也有不一樣服務器

    ./configure    配置ide

    make      編譯函數

    sudo make install  安裝工具

  3、把源碼例子拿過來編譯
測試

    gcc -o example example.c -I /usr/local/include/freetype2/ -lfreetype -lm 字體

    /* -I /usr/local/include/freetype2/ 指定頭文件目錄       -lfreetype 指定庫類型        -lm 指定數學庫 */ui

    /* 這是大i                                                                     這是小L                               小L */this

    若是沒加會出錯,不信能夠先不加試一試而後一個一個加試下,這是方法,爲何我也不知道

    ./example ./simsun.ttc abc    執行就會打印出字體文件在終端上,可是看不到,由於源碼裏的設置太大了,要改小一些才能夠

    在執行時須要一個字體文件,能夠從C:\Windows\Fonts裏找一個複製過去

    下面貼出一段在PC上顯示代碼 用FreeType官方例子稍改就能夠了。

/* 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 0
    /* use 50pt at 100dpi */ error = FT_Set_Char_Size( face, 50 * 64, 0, 100, 0 );                /* set character size */
    /* error handling omitted */
#else error = FT_Set_Pixel_Sizes( face, /* handle to face object */
              0,      /* pixel_width */
              16 );   /* 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; } /* EOF */
View Code

 

  上面顯示了英文字符,經過執行程序時傳進去的 abc 若是咱們想顯示中文怎麼辦呢,咱們在源碼裏先定義一個字符串,再顯示字符串 這裏有一個地方要注意,字符串要以寬字符的方式定義保存,不能以 char *str = "矢量字體"; 這樣的方式定義,由於中文是用二個字節保存的 英文是一個字節,若是一個字符串裏又有中文又有英文就很差處理了。所以又引入了「寬字符」 寬字符怎麼用,老辦法 搜。wchar_t *str = L"矢量字體"; 這樣定義 同時還有一個頭文件要包含進去 #include <wchar.h> ,下面列出一個例子,一樣是在上面的基礎上改的。

  在這個例子裏會出現編譯時 提示沒法轉換字符集 error:converting to execution character set: Invalid or incomplete multibyte or wide character 

  在指定字符輸入輸出字符集編譯 gcc  -finput-charset=GBK -fexec-charset=UTF-8 -o example example.c -I /usr/local/include/freetype2/ -lfreetype -lm 

這裏列出源碼

 

/* 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 <wchar.h> #include <ft2build.h> #include FT_FREETYPE_H /* 這裏修改 原來是680 480 太大 */
#define WIDTH   100
#define HEIGHT  100


/* 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; wchar_t *chinese_str = L"矢量字體"; /* 把參數改成2個 */
    if ( argc != 2 ) { fprintf ( stderr, "usage: %s font \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 0
    /* use 50pt at 100dpi */ error = FT_Set_Char_Size( face, 50 * 64, 0, 100, 0 );                /* set character size */
    /* error handling omitted */
#else
    /* 直接用這個函數設字像素大小 */ error = FT_Set_Pixel_Sizes( face, /* handle to face object */
              0,      /* pixel_width */
              24 );   /* 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 = 10 * 64; pen.y = ( target_height - 40 ) * 64; /* man wcslen man strlen 去找到用法 獲得字符串長度 */
    for ( n = 0; n < wcslen(chinese_str); n++ ) { /* set transformation */ FT_Set_Transform( face, &matrix, &pen ); /* load glyph image into the slot (erase previous one) */
    /* 直接顯示字符串不使用傳入參數 */ error = FT_Load_Char( face, chinese_str[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; } /* EOF */
View Code

 

經過上面的測試,下面總結一下freetype字體的用法,和一些注意事項。參考官方文檔。後面會慢慢增長對FreeType的測試代碼。

 

上面的代碼都是在PC機上運行的,如今咱們來移植到ARM開發板上去,首先要進行交叉編譯,試一下一大堆錯誤,咱們連freetype都沒有編譯配置,下面先來編譯

1:解壓 tar xjf freetype-2.9.1.tar.bz2  而後進入目錄

2:配置 ./configure --host=arm-linux  指定爲交叉編譯用於ARM平臺

3:編譯 make

4:安裝 make DESTDIR=$PWD/tmp install 先安裝到當前目錄下的tmp目錄裏,而後把 lib include裏的東西拷貝到咱們編譯器工具鏈裏 若是直接make 會安裝到PC機根目錄下

  cd tmp/usr/local/include/

  sudo cp * /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include -rf

  cd tmp/usr/local/lib/

  sudo cp * /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib -d -rf

5:編譯應用程序 arm-linux-gcc example example.c 出現一大堆錯誤

  5.1:sudo mv freetype2/freetype .   編譯時找頭文件目錄多了一個freetype2 咱們把下面的都移上來。仍是不行,那就指定頭文件目錄 指定動態庫 指定字符集再編譯

    arm-linux-gcc -finput-charset=GBK -o freetype freetype.c -I /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include/freetype -lfreetype

    編譯成功後 可執行程序freetype 字體文件simsun.ttc 拷貝到根文件系統目錄裏去  動態庫tmp/usr/local/lib/ 拷貝到文件系統根目錄lib/目錄下 

    而後就能夠執行,且能夠在終端打印出字體了。

 

下一步,修改應用程序源碼,讓其在lcd上顯示,咱們以前有作過在Lcd上顯示英文和中文字體,那些顯示函數是能夠用的,這裏咱們修改的就是把在終端輸出改爲在lcd上顯示

下面列出源碼。

  修改後 編譯仍是出錯  error: failure to convert GBK to UTF-8 用amn gcc 找一下 charset 看一個gcc裏怎麼轉換 看到這裏

 

  -fexec-charset=charset
    Set the execution character set, used for string and character constants. The default is UTF-8. charset can be any encoding supported by the system's "iconv" library
    routine.

  咱們看一下 iconv --help 怎麼用 

  Input/Output format specification:
  -f, --from-code=NAME encoding of original text
  -t, --to-code=NAME encoding for output

  咱們用執行一下看看 

  iconv -f GBK -t UTF-8 freetype.c 

  能夠看到在處理asicc字體文件時有個別字符他沒法識別,咱們去掉這些字符就能夠了 源碼經測試可經過,徹底顯示在lcd上,能夠修改參數,換字體 換大小,換角度均可以修改測試,最關鍵的地方就是 座標 freetype是基於「笛卡爾」座標,咱們用的是「lcd」座標 。

 

 

/* example1.c */
/*                                                                 */
/* This small program shows how to print a rotated string with the */
/* FreeType 2 library. */ #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdio.h> #include <linux/fb.h> #include <fcntl.h> #include <string.h> #include <math.h> #include <wchar.h> #include <ft2build.h> #include FT_FREETYPE_H static int fd_fb; static struct fb_var_screeninfo var;    /* 定義一個可變信息結構體 用於保存得到的可變信息 */
static struct fb_fix_screeninfo fix;    /* 定義一個固定信息結構體 用於保存得到的固定信息 */
static int screen_size;                /* 定義一個變量 表示 framebuffer 進行內存映射 */
static unsigned char *fbmem;            /* 定認一個指針變量 用於保存內存映射後的地址 */

static int fd_hzk; static struct stat hzk_stat;            /* 定義一個結構體用於保存統計信息 */
static unsigned char *hzkmem;            /* 定義一個指針變量 用於保存央存映射的地址 */

static unsigned int line_width; static unsigned int pixel_whdth; static unsigned char *str = ""; /* 描點函數 根據 x,y,值 肯定對應framebuffer的位置 * 而後依次的在對應的framebuffer上寫入顏色值便可 */
void lcd_put_pixel(int x, int y, unsigned int color) { unsigned char *pen = fbmem + y*line_width + x*pixel_whdth; *pen = color; } /* 顯示中文 如何顯示呢? * 1: 先獲得漢字庫的文件 而後能夠像打開framebuffe同樣打開這個文件 * 2: 打開後就能夠去讀了,咱們也能夠把這個文件當成內存同樣用 即映射一下 * 3: 而後就能夠去裏面獲得字體點陣數據了,接着和顯示字符同樣描點便可 * 把文件當成內存去映射先要獲得文件大小 能夠用 fstat 這個函數來得到大小 * 如何使用這個函數 能夠在服務器上輸入 man fstat 獲得使用說明 * 漢字庫的使用方法能夠去"度娘"去找,基本上就是下面幾個注意的地方 * 1: GBK編碼用四個字節表示一箇中文 第一個字節表示區碼 第二個字節表示位碼 * 2: 爲了兼容ascii碼 編碼從a1 開始 例:"中"字 值是 D6 D0 D6=區碼 D0=位碼 * 3: 因此編碼的值是 區碼-A1 位碼-A1 */
void lcd_put_chinses(int x, int y, unsigned char *str) { unsigned int area   = str[0] - 0xa1; unsigned int where  = str[1] - 0xa1; unsigned char *dots = hzkmem + (area * 94 + where) * 32; unsigned char byte; int i,j,k; for (i = 0;i < 16;i ++) { for (j = 0;j < 2;j ++) { byte = dots[i * 2 + j]; for (k = 7;k >=0;k --) { if (byte & (1 << k)) { /* 傳入參數的值是lcd要顯示的起點座標 這裏每一個點的座標每描一個點要移動一次 */ lcd_put_pixel(x + j * 8 + 7 - k, y + i, 0xffffff);    /* 0xffffff 表示白色 */ } else { /* 傳入參數的值是lcd要顯示的起點座標 這裏每一個點的座標每描一個點要移動一次 */ lcd_put_pixel(x + j * 8 + 7 - k, y + i, 0);        /* 0 表示黑色 */ } } } } } 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 >= var.xres|| j >= var.yres ) continue; //image[j][i] |= bitmap->buffer[q * bitmap->width + p];
          lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]); } } } 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; wchar_t *chinese_str = L"翻繁敏酩aAbB"; fd_fb = open("/dev/fb0", O_RDWR);    /* 打開lcd設備 可讀可寫 */
    if (fd_fb < 0) { printf("cnt't open /dev/fb0 !\n"); return -1; } if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))        /* 得到可變信息 */ { /* 正常得到信息的話 ioctl 會返回0 若是返回值不爲0時表示出錯 */ printf("can't get var! \n"); return -1; } if (ioctl(fd_fb,FBIOGET_FSCREENINFO, &fix))        /* 得到固定信息 */ { /* 正常得到信息的話 ioctl 會返回0 若是返回值不爲0時表示出錯 */ printf("can't get fix! \n "); return -1; } /* 計算 framebuffer 的大小 用於內存映射單位字節 用x分辯率*y分辯率*每一個像素獲得總大小這時的單位是bit 除以8 轉換成字節 */ screen_size = var.xres * var.yres * var.bits_per_pixel / 8; /* 內存映射 mmap 怎麼用呢,能夠在服務器上輸入man mmap 獲得說明 * void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset); * 參數說明:void *addr 設置爲0 讓內核自動給咱們分配地址 * :size_t length 映射內存大小 * :int prot 屬性爲可讀可寫 * :int flags 共享 其它進程均可見 * :int fd framebuffer * :off_t offset 偏移值 */ fbmem = (unsigned char *)mmap(NULL, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0); if (fbmem == (unsigned char *)-1) { printf("can't mmap!\n"); return -1; } line_width = var.xres * var.bits_per_pixel / 8; pixel_whdth = var.bits_per_pixel / 8; /* 打開當前目錄下的 HZK16 文件 屬性設爲只讀 */ fd_hzk = open("HZK16", O_RDONLY); if (fd_hzk < 0) { printf("can't open HZK16\n"); return -1; } /* 獲得這個件的統計信息固然也包含了大小 */
    if (fstat(fd_hzk, &hzk_stat)) { printf("can't get hzk_stat! \n "); return -1; } /* 內存映射 mmap 怎麼用呢,能夠在服務器上輸入man mmap 獲得說明 * void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset); * 參數說明:void *addr 設置爲0 讓內核自動給咱們分配地址 * :size_t length 映射內存大小 * :int prot 屬性爲可讀可寫 * :int flags 共享 其它進程均可見 * :int fd 文件 * :off_t offset 偏移值 */ hzkmem = (unsigned char *)mmap(NULL, hzk_stat.st_size, PROT_READ, MAP_SHARED, fd_hzk, 0); if (hzkmem == (unsigned char *)-1) { printf("can't mmap!\n"); return -1; } /* 這裏先清屏 所有顯黑色 */ memset(fbmem, 0, screen_size); filename = argv[1];                           /* first argument */ angle = ( 10.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 0
    /* use 50pt at 100dpi */ error = FT_Set_Char_Size( face, 50 * 64, 0, 100, 0 );                /* set character size */
    /* error handling omitted */
#else
    /* 直接用這個函數設字像素大小 */ error = FT_Set_Pixel_Sizes( face, /* handle to face object */
              0,      /* pixel_width */
              48 );   /* 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 = (var.xres/2 + 24) * 64;
    pen.x = (0 + 24) * 64; pen.y = (var.yres / 2 - 16)  * 64; /* man wcslen man strlen 去找到用法 獲得字符串長度 */
    for ( n = 0; n < wcslen(chinese_str); n++ ) { /* set transformation */ FT_Set_Transform( face, &matrix, &pen ); /* load glyph image into the slot (erase previous one) */
        /* 直接顯示字符串不使用傳入參數 */ error = FT_Load_Char( face, chinese_str[n], FT_LOAD_RENDER ); if ( error ) continue;                 /* ignore errors */

        /* now, draw to our target surface (convert position) */ draw_bitmap( &slot->bitmap, slot->bitmap_left, var.yres - slot->bitmap_top ); /* increment pen position */ pen.x += slot->advance.x; pen.y += slot->advance.y; } // show_image(); 

    /* 在lcd上顯示ascii字符 即然是顯示,確定要有位置 在那顯示先定在中間 */
// lcd_put_ascii(var.xres / 2, var.yres / 2, 'F');
    /* 在lcd上顯示中文 即然是顯示,確定要有位置 在那顯示 */ lcd_put_chinses(var.xres / 2 + 8, var.yres / 2, str); FT_Done_Face ( face ); FT_Done_FreeType( library ); return 0; }
View Code
相關文章
相關標籤/搜索