记录每天学习的内容
freetype2 使用
下载源代码在linux下编译 make make install
https://blog.youkuaiyun.com/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_HFT_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);
}
///