freetype2 下载编译以及测试代码

源码: https://sourceforge.net/projects/freetype/files/freetype2

  1. 下载解压后,进入源码目录执行cmake-gui,界面中配置源码目录与编译目录,然后点击左下角Configure。
  2. 界面中FT_DISABLE_BROTLI、FT_DISABLE_BZIP2、FT_DISABLE_HARFBUZZ、FT_DISABLE_PNG、FT_DISABLE_ZLIB全部选中。
  3. 再次点击Configure,点击Generate。
  4. 进入配置的编译目录,执行make命令即可编译出静态库。
  5. 编辑CMakeLists.txt,找到"add_library(freetype"这一行,改为 "add_library(freetype SHARED",保存后重新执行cmake-gui,Configure、Gernerate,进入编译目录执行make命令后即可编译出动态库。

以下为测试代码。

#include 
#include 
#include 
#include 
#include 

int main(int argc, char* argv[])
{
    FT_Library  library;
    FT_Face     face;
    FT_Vector   pen;
    FT_Error    error;
    FT_UInt     charIdx;
    wchar_t     wch_data[] = L"pp happy, new year!!!\n天地转,光阴迫,一万年太久,只争朝夕!";//u'Z', U'Z'
    char*       char_buffer;        // 用户申请的显示区域空间
    int         startX, startY; // 字符图像开始装入的位置
    
    char *font_file = "simfang.ttf";
    int font_width = 32;//
    int font_height = 32;//!!!
    printf("sizeof(wchar_t) = %ld\n", sizeof(wchar_t));
    /*
    1.在windows平台下sizeof(wchar_t)为2,而在linux平台下sizeof(wchar_t)为4。
    2.在windows平台下宽字符(或字符串)字面量使用UTF-16编码,linux平台下使用UTF-32编码。
    */
    if(argc <= 1){
        
    }else if(argc <= 2){
        font_file = argv[1];
    }else if(argc <= 3){
        font_file = argv[1];
        font_width = atoi(argv[2]);
        font_height = atoi(argv[2]);
    }else if(argc <= 4){
        font_file = argv[1];
        font_width = atoi(argv[2]);
        font_height = atoi(argv[3]);
    }
    // 1. 初始化freetype2库
    error = FT_Init_FreeType(&library);
 
    // 2. 创建一个face
    error = FT_New_Face(library, font_file, 0, &face);

    // 3. 设置字体尺寸
#if 0
    /*
    字符宽度和高度以1/64点为单位表示。点是物理上的距离,一个点代表1/72英寸(2.54cm)
    分辨率以dpi(dots per inch)为单位表示,表示一个英寸有多少个像素
    字符物理大小为: char_width*64* (1/64) * (1/72)英寸
    字符的像素为: char_width*64* (1/64) * (1/72)*horz_resolution
    
    FT_Set_Char_Size( FT_Face     face,
                  FT_F26Dot6  char_width,  //字符宽度,单位为1/64点
                  FT_F26Dot6  char_height, //字符高度,单位为1/64点
                  FT_UInt     horz_resolution, //水平分辨率
                  FT_UInt     vert_resolution ); //垂直分辨率
    */
    error = FT_Set_Char_Size(face, 0, font_height*64, 72, 72);
#else
    error = FT_Set_Pixel_Sizes(face, font_width, font_height);
#endif
    //斜体在FreeType中可以通过矩阵变换来实现,只要把矩阵设置成一个切边矩阵就可以了
    // 倾斜度,越大就越斜
    float lean = 0.6f;
    FT_Matrix matrix;
    matrix.xx = 0x10000L;
    matrix.xy = lean * 0x10000L;
    matrix.yx = 0;
    matrix.yy = 0x10000L;
    //FT_Set_Transform(face, &matrix, 0);
    //pen.x = img_x*64;
    //pen.y = (img_y)*64;//
    pen.x = 0;
    pen.y = 0;
    FT_Set_Transform(face, &matrix, &pen);
    //error = FT_Select_Charmap(fontFace, FT_ENCODING_UNICODE);
    //error = FT_Select_Charmap(face, FT_ENCODING_BIG5 ); /* big5编码 , 默认为UNICODE */
    //error = FT_Set_Transform( face, &matrix, &pen );//旋转
    
    char bitmap_file[256];
    int img_width = 320;
    int img_height = 240;
    int img_x = 0;
    int img_y = 0;
    char *bitmap_buffer = malloc(img_width*img_height);
    memset(bitmap_file, 0, sizeof(bitmap_file));
    sprintf(bitmap_file, "bitmap_%d_%d_gray.yuv", img_width, img_height);
    FILE *pBitMapFd = fopen(bitmap_file, "wb");
    
    for(int index = 0; index < sizeof(wch_data)/sizeof(wch_data[0]); index++){
        wchar_t wch = wch_data[index];
        if(wch == 0){
            break;
        }else if(wch == U'\r'){
            img_x = 0;
            continue;
        }else if(wch == U'\n'){
            img_x = 0;
            img_y += font_height;
            continue;
        }
        
#if 0
        // 4. 获取字符图像索引
        charIdx = FT_Get_Char_Index(face, wch);
     
        // 5. 加载字符图像
        FT_Load_Glyph(face, charIdx, FT_LOAD_DEFAULT);
        if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
        {
            FT_Outline_Embolden(&(face->glyph->outline), 16);   // 加粗轮廓线
        }
     
        // 6. 获取字符位图
        if (face->glyph->format != FT_GLYPH_FORMAT_BITMAP)
        {
            FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
        }
#else
        error = FT_Load_Char(face, wch, FT_LOAD_RENDER);//FT_LOAD_MONOCHROME 8pixel per byte
#endif
        /*
        FT_Bitmap(字形位图对象):
            rows:  位图行数(高度)
            width:  位图宽度
            buffer:  位图数据(默认8位灰度值)
        */
        
        FT_Glyph  glyph;
        FT_BBox   acbox;
        FT_GlyphSlot  slot     = face->glyph;
        error = FT_Get_Glyph(face->glyph, &glyph);
        FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &acbox);
        int lcd_x_min = acbox.xMin;
        int lcd_x_max = acbox.xMax;
        int lcd_y_min = font_height - acbox.yMax;
        int lcd_y_max = font_height - acbox.yMin;
        lcd_y_min -= (font_height/8);
        lcd_y_max -= (font_height/8);
        printf("0x%x: xMin=%ld, xMax=%ld, yMin=%ld, yMax=%ld, V-yMin = %ld, v-yMax = %ld, advance x = %ld, advance y = %ld, w = %d, h = %d, left = %d, top = %d\n", \
            wch, acbox.xMin, acbox.xMax, acbox.yMin, acbox.yMax, font_height - acbox.yMax, font_height - acbox.yMin, slot->advance.x, slot->advance.y, \
            slot->bitmap.width, slot->bitmap.rows, slot->bitmap_left, slot->bitmap_top);
        
        int wch_h_size = (slot->advance.x/64)>=slot->bitmap.width ? (slot->advance.x/64) : slot->bitmap.width;
        
        if(img_x + wch_h_size >= img_width){
            img_x = 0;
            img_y += font_height;
        }
        
        if(img_y + font_height >= img_height){
            img_y = 0;
            break;//
        }
        for(int j = 0; j < slot->bitmap.rows; j++){
            for(int i = 0; i < slot->bitmap.width; i++){
                if(slot->bitmap.buffer[j*slot->bitmap.width + i]){
#if 1//
                    //if(lcd_x_min < 0){
                    if(img_x + lcd_x_min + i < 0){
                        bitmap_buffer[img_width*img_y + img_x + (j+lcd_y_min)*img_width + i] = slot->bitmap.buffer[j*slot->bitmap.width + i];
                    }else
#endif
                    {
                        bitmap_buffer[img_width*img_y + img_x + (j+lcd_y_min)*img_width + i + lcd_x_min] = slot->bitmap.buffer[j*slot->bitmap.width + i];
                    }
                }
            }
        }
        img_x += slot->advance.x/64;
        //img_x += wch_h_size;
    }
    
    fwrite(bitmap_buffer, 1, img_width*img_height, pBitMapFd);
    fclose(pBitMapFd);
    
    FT_Done_Face(face);
    FT_Done_FreeType(library);
    printf("-------------- end ----------------\n");
        
}

预览生成的yuv数据

ffplay -i bitmap_320_240_gray.yuv -pixel_format gray  -video_size 320*240 
ffplay显示结果.png

你可能感兴趣的:(freetype2 下载编译以及测试代码)