交叉编译freetype2(带harfbuzz)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wyy626562203/article/details/82595034

交叉编译freetype2(带harfbuzz)

ubuntu16.04

arm-linux-gnueabihf-gcc
freetype 2.9
harfbuzz 1.9

freetype:https://download.savannah.gnu.org/releases/freetype/
harfbuzz:https://github.com/harfbuzz/harfbuzz

交叉编译

freetype和harfbuzz相互依赖,之前试过先编译freetype在编译harfbuzz,虽然编译成功但是编译链接应用的时候提示未定义函数,所以我把编译顺序调换过来先编译harfbuzz在编译freetype,这样就不存在问题了。

所以第一步先编译harfbuzz不带freetype

#harfbuzz --enable-static编译静态库
./configure --host=arm-linux-gnueabihf --prefix=/home/wyy/Develop/freetype/harfbuzz-master/install --with-freetype=yes --enable-static=yes --enable-shared=yes 

make -j4
make install

编译freetype链接到harfbuzz (不带freetype)

#freetype 默认会生成动态库和静态库
./configure --host=arm-linux-gnueabihf --prefix=/home/wyy/Develop/freetype/freetype-2.9/install --with-harfbuzz=yes --enable-shared=yes --enable-static=yes  HARFBUZZ_CFLAGS='-I/home/wyy/Develop/freetype/harfbuzz-master/install/include/harfbuzz' HARFBUZZ_LIBS='-L/home/wyy/Develop/freetype/harfbuzz-master/install/lib -harfbuzz -harfbuzz-subset'


make -j4
make install

最后编译带freetype的harfbuzz库

#harfbuzz --enable-static编译静态库
./configure --host=arm-linux-gnueabihf --prefix=/home/wyy/Develop/freetype/harfbuzz-master/install --with-freetype=yes --enable-static=yes --enable-shared=yes FREETYPE_CFLAGS=-I/home/wyy/Develop/freetype/freetype-2.9/install/include/freetype2  FREETYPE_LIBS='-L/home/wyy/Develop/freetype/freetype-2.9/install/lib -lfreetype'

make -j4
make install

编程

编程实例

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <wchar.h>
#include <stdlib.h>

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H

int fd_fb;
struct fb_var_screeninfo var;   /* Current var */
struct fb_fix_screeninfo fix;   /* Current fix */
int screen_size;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;
int number;

unsigned int color_array[6]={
                    0x0000FF , /* blue */
                    0x00FF00 , /* green */
                    0x00FFFF ,/* blackish green  */
                    0xFF0000 ,/* red */
                    0xFF00FF ,/* carmine */
                    0xFFFF00 ,/* yellow */
                    };

/* color : 0x00RRGGBB */
void lcd_put_pixel(int x, int y, unsigned int color)
{
    unsigned char *pen_8 = fbmem+y*line_width+x*pixel_width;
    unsigned short *pen_16; 
    unsigned int *pen_32;   

    unsigned int red, green, blue;  

    pen_16 = (unsigned short *)pen_8;
    pen_32 = (unsigned int *)pen_8;

    switch (var.bits_per_pixel)
    {
        case 8:
        {
            *pen_8 = color;
            break;
        }
        case 16:
        {
            /* 565 */
            //red   = (color >> 16) & 0xff;
            //green = (color >> 8) & 0xff;
            //blue  = (color >> 0) & 0xff;
            //color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
            if(color!=0)  /* 这里非常重要,有些轮廓信息是0,就是字体空白区域*/
            {
            red   = (color_array[number] >> 16) & 0xff;
            green = (color_array[number] >> 8) & 0xff;
            blue  = (color_array[number] >> 0) & 0xff;
            color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);     
            }
            *pen_16 = color;
            break;
        }
        case 32:
        {
            /*去除字体问题中的黑色背景,如果需要不同背景可以通过在这里设置相应的颜色。*/
            if(color == 0) return;

            *pen_32 = color;
            break;
        }
        default:
        {
            printf("can't surport %dbpp\n", var.bits_per_pixel);
            break;
        }
    }
}



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

    //printf("x = %d, y = %d\n", x, y);

  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)
{
    wchar_t *wstr1 = L"网络人VS灰鸽子";

    FT_Library    library;
    FT_Face       face;
    int error;
    FT_Vector     pen;
    FT_GlyphSlot  slot;
    int i;
    int font_size;

    if (argc != 4)
    {
        printf("Usage : %s <font_file> <font_size> <color number0~5>\n", argv[0]);
        return -1;
    }
    font_size=atol(argv[2]);    
    number   =atol(argv[3]);
    fd_fb = open("/dev/fb0", O_RDWR);
    if (fd_fb < 0)
    {
        printf("can't open /dev/fb0\n");
        return -1;
    }
    if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
    {
        printf("can't get var\n");
        return -1;
    }
    if (ioctl(fd_fb, FBIOGET_FSCREENINFO, &fix))
    {
        printf("can't get fix\n");
        return -1;
    }
    line_width  = var.xres * var.bits_per_pixel / 8;
    pixel_width = var.bits_per_pixel / 8;
    screen_size = var.xres * var.yres * var.bits_per_pixel / 8;
    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;
    }
    /* 清屏: 全部设为黑色 */
    memset(fbmem, 130, screen_size);
    /* 显示矢量字体 */
    error = FT_Init_FreeType( &library );              /* initialize library */
    /* error handling omitted */

    error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */
    /* error handling omitted */    
    slot = face->glyph;

    FT_Set_Pixel_Sizes(face,24, 0);

    /* 确定座标:
     * lcd_x = 0
     * lcd_y = font_size
     * 笛卡尔座标系:
     * x = lcd_x = 0
     * y = var.yres - lcd_y = var.yres - 24
     */
    pen.x = 0 * 64;
    pen.y = (var.yres - 24) * 64;

    for (i = 0; i < wcslen(wstr1); i++)
    {
        /* set transformation */
        FT_Set_Transform( face, 0, &pen);
        /* load glyph image into the slot (erase previous one) */
        error = FT_Load_Char( face, wstr1[i], FT_LOAD_RENDER );
        if (error)
        {
            printf("FT_Load_Char error\n");
            return -1;
        }

        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;

    }
    return 0;   
}

编译命令

arm-linux-gnueabihf-gcc show_lines.c -o show_lines -L/home/wyy/Develop/freetype/freetype-2.9/install/lib -lfreetype -L/home/wyy/Develop/freetype/harfbuzz-master/install/lib -lharfbuzz -lharfbuzz-subset -lpthread -lm -I/home/wyy/Develop/freetype/freetype-2.9/install/include/freetype2 -I/home/wyy/Develop/freetype/harfbuzz-master/install/include/harfbuzz -I/home/wyy/Develop/freetype/harfbuzz-master/src -static

猜你喜欢

转载自blog.csdn.net/wyy626562203/article/details/82595034
今日推荐