freetype2 使用

记录每天学习的内容

freetype2 使用

下载源代码在linux下编译 make   make install

https://blog.csdn.net/bryce_xiao/article/details/70787810    pkg-config使用方法

gcc test_freetype.c -o test_freetype -I/usr/local/include/freetype2 -lfreetype -lm

以下代码保存为 test_freetype.c

//

#include <ft2build.h>

#include FT_FREETYPE_H


FT_Library library;
FT_Face face;
#define slot face->glyph
int error;
FT_UInt  glyph_index;


int pen_x = 0;
int pen_y = 0;


void DisplayByte(unsigned char value)
{
    int i;
    for(i=0; i<8; i++)
    {
        if(value & 0x80)
        {
            printf("0");
        }
        else
        {
            printf(" ");
        }
        value = (value << 1);
    }
}
void my_draw_bitmap( FT_Bitmap * pBitmap, int x, int y)
{
    int row, i;
    printf("x = %d, y = %d\n", x, y);
    printf("pBitmap rows=%d, width=%d, pitch=%d, pixel_mode=%d\n", 
        pBitmap->rows, pBitmap->width, pBitmap->pitch, pBitmap->pixel_mode);
    for(row=0; row<pBitmap->rows; row++)
    {
        for(i=0; i<pBitmap->pitch; i++)
        {
            DisplayByte(pBitmap->buffer[row*pBitmap->pitch+i]);
        }
        printf("\n");
            
    }
    
}


int main(void)
{
    //FT_GlyphSlot  slot = face->glyph;  /* a small shortcut */
    error = FT_Init_FreeType( &library);
    if( error )
    {
        printf("FT_Init_FreeType error = %d\n", error);
        return -1;
    }
    
    error = FT_New_Face(library, "OpenSans-Regular.ttf", 0, &face);


    if ( error == FT_Err_Unknown_File_Format )
    {
        printf("FT_Err_Unknown_File_Format\n");
        return -2;
    }
    else if ( error )
    {
        printf("FT_New_Face error = %d\n", error);
        return -3;
    }
    
    error = FT_Set_Pixel_Sizes(
          face,   /* handle to face object */
          0,      /* pixel_width           */
          16 );   /* pixel_height          */
          
    if ( error )
    {
        printf("FT_Set_Pixel_Sizes error = %d\n", error);
        return -4;
    }
    
    /* retrieve glyph index from character code */
    glyph_index = FT_Get_Char_Index( face, '!' );


    /* load glyph image into the slot (erase previous one) */
    error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
    if ( error )
    {
        printf("FT_Load_Glyph error = %d\n", error);
        return -5;
    }
    
    error = FT_Render_Glyph( face->glyph,   /* glyph slot  */
                         FT_RENDER_MODE_MONO ); /* render mode FT_RENDER_MODE_MONO FT_RENDER_MODE_NORMAL */
    /* now, draw to our target surface */
    my_draw_bitmap( &face->glyph->bitmap,
                  pen_x+face->glyph->bitmap_left, pen_y+face->glyph->bitmap_top);


    /* increment pen position */
    pen_x += face->glyph->advance.x >> 6;
    pen_y += face->glyph->advance.y >> 6; /* not useful for now */    
    printf("pen_x = %d, pen_y = %d\n", pen_x, pen_y);

}

///


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值