36928Fermer36930
robinHoodLe 18/11/2019 à 23:54
#include <stdio.h> #include <ft2build.h> #include FT_FREETYPE_H #include "unicode/utf8.h" #define FONT_WIDTH 12 #define FONT_HEIGHT 12 #define u32 unsigned int #define u8 unsigned char void printChar( FT_Bitmap * b ){ u32 sx = b->width, sy = b->rows ; u8 *p = b->buffer, *pend = p + sx*sy, *lend = p ; while( p != pend ){ lend += sx; while( p != lend ){ printf("%c",*p ? *p > 127 ? '*' : 'o' : ' '); p++; }; printf("\n"); }; } UChar32 * read_utf8( const char * s ){ const char * p = s; while( *p++ ); p--; u32 l = p - s; UChar32 * s32 = (UChar32*)malloc( (l + 2) * sizeof(UChar32) ); UChar32 * p32 = &s32[1]; unsigned int i = 0; UChar32 c; while( 1 ){ //printf("%u/%u\n",i,l); U8_NEXT( s, i, l, c ); //printf("%08x\n",c); if( !c || i > l ) break; *p32++ = c; }; *p32++ = 0; l = p32 - s32; *s32 = l - 2; /*for( u32 n=0; n< l; n++ ) printf("%02u 0x%08x\n",n,s32[n]); printf("realloc to %u\n",l * sizeof(UChar32)); */ p32 = (UChar32*)realloc( s32, l * sizeof(UChar32) ); if( p32 ) s32 = p32; return s32; } int main( int argc, char** argv ){ FT_Library library; FT_Face face; FT_Error error; if ( argc != 3 ){ fprintf ( stderr, "usage: %s *font* *text*\n", argv[0] ); exit( 1 ); } error = FT_Init_FreeType( &library ); error = FT_New_Face( library, argv[1], 0, &face ); FT_Set_Pixel_Sizes( face, FONT_WIDTH, FONT_HEIGHT ); FT_Select_Charmap(face , FT_ENCODING_UNICODE); UChar32 * s = read_utf8( argv[2] ); printf("strlen %u\n",*s); UChar32 * ps = s + 1; FT_Bitmap * b = &face->glyph->bitmap; while( *ps ){ FT_UInt glyph = FT_Get_Char_Index( face, *ps ); error = FT_Load_Glyph( face, glyph, FT_LOAD_RENDER ); printf("\n0x%08x >> %ux%u\n",*ps, b->width, b->rows ); printChar( b ); ps++; }; FT_Done_Face( face ); FT_Done_FreeType( library ); free( s ); return 0; }