• freetype库的移植


    freetype库的移植

    freetype依赖于libpng,而libpng又依赖zlib,所以我们本次的移植实际上是需要移植三个库,每个的移植操作是类似的,只不过顺序不能颠倒

    1. 移植zlib库

    1.1 下载zlib库

    下载地址: zlib库官网

    1.2 将zlib移动到linux虚拟机上,并解压,解压后进入得到的目录

    1. tar -xzf zlib-1.2.11.tar.gz
    2. cd zlib-l.2.11/
    
    • 1
    • 2

    1.3 使用configure来配置makefile

    1. export CC=arm-linux-gnueabihf-gcc   # 你的编译工具,我的和你的可能不一样
    2. ./configure --prefix=/home/hxd/tools/zlib  # prefix用于指定库安装的位置
    
    • 1
    • 2

    1.4 使用make && make install 安装库

    1. make
    2. make install
    
    • 1
    • 2

    安装成功后,会在你刚刚设置的安装目录下得到三个目录,分别是include/, lib/, share/

    1.5 将得到的include/目录下的所有文件拷贝到你的编译工具链的默认寻找头文件目录的下,将得到的lib/目录下的所有文件拷贝到你的编译工具链的默认寻找库文件的目录下(这样做的目的是为了以后编译的时候不需要自己手动来添加路径,所以这一步也可以不做)

    1. echo "main(){}" | arm-linux-gnueabihf-gcc -E -v -    # 该命令可以查看编译工具的默认查找路径,你自己任选一个合适的
    
    
    • 1
    • 2
    • include路径,我是选的我的交叉工具链的路径/usr/lib/gcc-cross/arm-linux-gnueabihf/9/include
      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DA3ud1CH-1656498779106)(D:\Myblog\blogimages\image-20220627213644947.png)]
    cp -rfd /home/hxd/tools/zlib/include/* /usr/lib/gcc-cross/arm-linux-gnueabihf/9/include  # 加上-d的目的是将链接文件也这样拷贝过去,节约空间
    
    • 1
    • lib路径, 我是选的我的交叉工具链的路径/usr/lib/gcc-cross/arm-linux-gnueabihf/9
      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HuaiIXRU-1656498779108)(D:\Myblog\blogimages\image-20220627213842124.png)]
    cp -rf /home/hxd/tools/zlib/lib/* /usr/lib/gcc-cross/arm-linux-gnueabihf/9/
    
    • 1

    1.6 将编译好的zlib库移植到开发板

    只需要将lib/目录下的所有文件拷贝到开发板的/usr/lib/或/lib/下

    1. 先将zlib文件夹复制nfs的目录下,然后开发板挂载该nfs
    2. rm -rf /usr/lib/libz* /lib/libz*   # 如果以前存在该库,可能会互相干扰,所以删除以前的库
    3. cp -rfd /mnt/zlib/lib/* /usr/lib
    
    • 1
    • 2
    • 3

    2. 移植libpng库

    2.1 下载libpng库

    下载地址: 官网

    2.2 将libpng移动到linux虚拟机上,并解压,解压后进入得到的目录

    1. tar -xJf libpng-1.6.37.tar.xz
    2. cd libpng-l.6.37
    
    • 1
    • 2

    2.3 执行./configure

    if 之前完成了1.5操作
    	1. ./configure --host=arm-linux-gnueabihf --prefix=/home/hxd/tools/libpng
    else
    	1. export CFLAGS="$CFLAGS -I/home/hxd/tools/zlib/include"   # 与你的zlib安装路径有关
    	2. export CPPFLAGS="$CPPFLAGS -I/home/hxd/tools/zlib/include"
    	3. export LDFLAGS="$LDFLAGS -L/home/hxd/tools/zlib/lib"
    	4. ./configure --host=arm-linux-gnueabihf --prefix=/home/hxd/tools/libpng
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.4 安装

    make && make install
    
    • 1

    2.5 配置到编译链中,方便后面的编译使用(具体路径1.5有解释), 还需要将freetype2目录下的所有文件移动到freetype2同级目录

    1. cp -rfd /home/hxd/tools/libpng/include/* /usr/lib/gcc-cross/arm-linux-gnueabihf/9/include  # 加上-d的目的是将链接文件也这样拷贝过去,节约空间
    2. cp -rf /home/hxd/tools/libpng/lib/* /usr/lib/gcc-cross/arm-linux-gnueabihf/9/
    3. cp -rf /usr/lib/gcc-cross/arm-linux-gnueabihf/9/include/freetype2/* /usr/lib/gcc-cross/arm-linux-gnueabihf/9/include/
    
    • 1
    • 2
    • 3

    2.6 移植到开发板

    需要将lib/目录下的所有文件拷贝到开发板的/usr/lib/或/lib/下

    1. 先将libpng文件夹复制nfs的目录下,然后开发板挂载该nfs
    2. rm -rf /usr/lib/libpng* /lib/libpng*   # 如果以前存在该库,可能会互相干扰,所以删除以前的库
    3. cp -rfd /mnt/libpng/lib/* /usr/lib
    
    • 1
    • 2
    • 3

    3. 移植freetype库

    3.1 下载

    下载地址: 官网

    3.2 移动到linux虚拟机上,解压后,进入该目录

    1. tar -xJf freetype-2.10.2.tar.xz
    2. cd freetype-2.10.2
    
    • 1
    • 2

    3.3 使用configure配置makefile

    if 前面的1.5和2.5都配置了
    	1. ./configure --prefix=/home/hxd/tools/freetype --host=arm-linux-gnueabihf
    elif 前面都没配置
    	1. export CFLAGS="$CFLAGS -I/home/hxd/tools/zlib/include -I/home/hxd/tools/libpng/include"   # 与你的zlib和libpng安装路径有关
    	2. export CPPFLAGS="$CPPFLAGS -I/home/hxd/tools/zlib/include -I/home/hxd/tools/libpng/include"
    	3. export LDFLAGS="$LDFLAGS -L/home/hxd/tools/zlib/lib -L/home/hxd/tools/libpng/lib"
    	4. ./configure --prefix=/home/hxd/tools/freetype --host=arm-linux-gnueabihf
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3.4 安装

    make && make install
    
    • 1

    3.5 配置到交叉编译工具链里面,方便后面的编译使用(具体路径1.5有解释)

    1. cp -rfd /home/hxd/tools/freetype/include/* /usr/lib/gcc-cross/arm-linux-gnueabihf/9/include  # 加上-d的目的是将链接文件也这样拷贝过去,节约空间
    2. cp -rf /home/hxd/tools/freetype/lib/* /usr/lib/gcc-cross/arm-linux-gnueabihf/9/
    
    • 1
    • 2

    3.6 移植到开发板上

    1. 先将libpng文件夹复制nfs的目录下,然后开发板挂载该nfs
    2. rm -rf /usr/lib/libfreetype* /lib/libfreetype*   # 如果以前存在该库,可能会互相干扰,所以删除以前的库
    3. cp -rfd /mnt/libpng/lib/* /usr/lib
    
    • 1
    • 2
    • 3

    4. 使用

    4.1 如果没有配置1.5,2.5,3.5

    编译示例:

    arm-linux-gnueabihf-gcc test.c -o test -lz -lm -lpng -freetype -I/home/hxd/tools/freetype/include/freetype2 -L/home/hxd/tools/freetype/lib -L/home/hxd/tools/zlib/lib -L/home/hxd/tools/libpng/lib
    
    • 1

    4.2 如果配置了

    编译示例:

    arm-linux-gnueabihf-gcc test.c -o test -lz -lm -lpng -freetype
    
    • 1

    5. 实例(源自韦哥的代码)

    #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 <sys/ioctl.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;
    
    /* 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);
                *pen_16 = color;
                break;
            }
            case 32:
            {
                *pen_32 = color;
                break;
            }
            default:
            {
                printf("can't surport %dbpp\n", var.bits_per_pixel);
                break;
            }
        }
    }
    
    /**********************************************************************
     * 函数名称: draw_bitmap
     * 功能描述: 根据bitmap位图,在LCD指定位置显示汉字
     * 输入参数: x坐标,y坐标,位图指针
     * 输出参数: 无
     * 返 回 值: 无
     * 修改日期        版本号     修改人        修改内容
     * -----------------------------------------------
     * 2020/05/12        V1.0     zh(angenao)         创建
     ***********************************************************************/ 
    void
    draw_bitmap( FT_Bitmap*  bitmap,
                 FT_Int      x,
                 FT_Int      y,
                 unsigned int color)
    {
        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 ( j = y, q = 0; j < y_max; j++, q++ )
        {
            for ( i = x, p = 0; i < x_max; i++, p++ )
            {
                if ( i < 0      || j < 0       ||
                    i >= var.xres || j >= var.yres )
                continue;
                //image[j][i] |= bitmap->buffer[q * bitmap->width + p];
                if (bitmap->buffer[q * bitmap->width + p]) lcd_put_pixel(i, j, color);
            }
        }
    }
    
    int compute_string_bbox(FT_Face face, wchar_t *wstr, FT_BBox  *abbox)
    {
        int i;
        int error;
        FT_BBox bbox;
        FT_BBox glyph_bbox;
        FT_Vector pen;
        FT_Glyph  glyph;
        FT_GlyphSlot slot = face->glyph;
    
        /* 初始化 */
        bbox.xMin = bbox.yMin = 32000;
        bbox.xMax = bbox.yMax = -32000;
    
        /* 指定原点为(0, 0) */
        pen.x = 0;
        pen.y = 0;
    
        /* 计算每个字符的bounding box */
        /* 先translate, 再load char, 就可以得到它的外框了 */
        for (i = 0; i < wcslen(wstr); i++)
        {
            /* 转换:transformation */
            FT_Set_Transform(face, 0, &pen);
    
            /* 加载位图: load glyph image into the slot (erase previous one) */
            error = FT_Load_Char(face, wstr[i], FT_LOAD_RENDER);
            if (error)
            {
                printf("FT_Load_Char error\n");
                return -1;
            }
    
            /* 取出glyph */
            error = FT_Get_Glyph(face->glyph, &glyph);
            if (error)
            {
                printf("FT_Get_Glyph error!\n");
                return -1;
            }
            
            /* 从glyph得到外框: bbox */
            FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &glyph_bbox);
    
            /* 更新外框 */
            if ( glyph_bbox.xMin < bbox.xMin )
                bbox.xMin = glyph_bbox.xMin;
    
            if ( glyph_bbox.yMin < bbox.yMin )
                bbox.yMin = glyph_bbox.yMin;
    
            if ( glyph_bbox.xMax > bbox.xMax )
                bbox.xMax = glyph_bbox.xMax;
    
            if ( glyph_bbox.yMax > bbox.yMax )
                bbox.yMax = glyph_bbox.yMax;
            
            /* 计算下一个字符的原点: increment pen position */
            pen.x += slot->advance.x;
            pen.y += slot->advance.y;
        }
    
        /* return string bbox */
        *abbox = bbox;
    }
    
    
    int display_string(FT_Face face, wchar_t *wstr, int lcd_x, int lcd_y, unsigned int color, int angle)
    {
        int i;
        int error;
        FT_BBox bbox;
        FT_Vector pen;
        FT_Glyph  glyph;
        FT_GlyphSlot slot = face->glyph;
        FT_Matrix     matrix;
    
        /* 把LCD坐标转换为笛卡尔坐标 */
        int x = lcd_x;
        int y = var.yres - lcd_y;
    
        /* 计算外框 */
        compute_string_bbox(face, wstr, &bbox);
    
        /* 反推原点 */
        pen.x = (x - bbox.xMin) * 64; /* 单位: 1/64像素 */
        pen.y = (y - bbox.yMax) * 64; /* 单位: 1/64像素 */
    
        /* 旋转角度 */
        /* 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 );
    
        /* 处理每个字符 */
        for (i = 0; i < wcslen(wstr); i++)
        {
            /* 转换:transformation */
            FT_Set_Transform(face, &matrix, &pen);
    
            /* 加载位图: load glyph image into the slot (erase previous one) */
            error = FT_Load_Char(face, wstr[i], FT_LOAD_RENDER);
            if (error)
            {
                printf("FT_Load_Char error\n");
                return -1;
            }
    
            /* 在LCD上绘制: 使用LCD坐标 */
            draw_bitmap( &slot->bitmap,
                            slot->bitmap_left,
                            var.yres - slot->bitmap_top, color);
    
            /* 计算下一个字符的原点: increment pen position */
            pen.x += slot->advance.x;
            pen.y += slot->advance.y;
        }
    
        return 0;
    }
    
    
    int main(int argc, char **argv)
    {
        wchar_t *wstr = L"你好世界!Hello World!";
    
        FT_Library    library;
        FT_Face       face;
        int error;
        FT_BBox bbox;
        int font_size = 24;
        int angle = 0;
        int lcd_x, lcd_y;
    
        if (argc < 4)
        {
            printf("Usage : %s <font_file> <lcd_x> <lcd_y> [font_size] [angle]\n", argv[0]);
            return -1;
        }
    
        lcd_x = strtoul(argv[2], NULL, 0);      
        lcd_y = strtoul(argv[3], NULL, 0);      
        
        if (argc == 5)
            font_size = strtoul(argv[4], NULL, 0);
    
        if (argc == 6)
        {
            font_size = strtoul(argv[4], NULL, 0);
            angle = atoi(argv[5]);    
        }
        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, 0, screen_size);
    
        error = FT_Init_FreeType( &library );              /* initialize library */
        
        error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */
    
        FT_Set_Pixel_Sizes(face, font_size, 0);
    
        printf("font_size: %d\n", font_size);
    
        display_string(face, L"你好世界!Hello World!", lcd_x, lcd_y, 0x112233, angle);
        display_string(face, L"你好世界!Hello World!", lcd_x, lcd_y+font_size, 0x222233, angle);
        display_string(face, L"你好世界!Hello World!", lcd_x, lcd_y+font_size*2, 0x332233, angle);
        display_string(face, L"你好世界!Hello World!", lcd_x, lcd_y+font_size*3, 0x442233, angle);
        
        return 0;   
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
  • 相关阅读:
    图像处理技术与应用(二)
    Windows下实用工具汇总(更新……)
    零基础学ptyhon之字典
    大一学生《Web编程基础》HTML实例网页代码 HTML+CSS+JS 黑色横排的个人主页作品
    BMS电池管理系统理论基础
    Spring注解开发
    【防作弊】Unity防本地调时间作弊
    SuccBI+低代码文档中心 — 可视化分析(仪表板)(上)
    原来你不会找资源,三个宝藏白嫖书籍网站,阅读改变生活(一)
    numpy.unique
  • 原文地址:https://blog.csdn.net/weixin_47024013/article/details/125527484