用freetype2顯示ascii-art中文

getbitmap.c

#include

 <ft2build.h>


#include FT_FREETYPE_H

int main (int argc , char **argv ){
int psize ;
FT_Library library ;
FT_Face face ;
unsigned int ucode ;
FT_UInt glyph_index ;
int row , pixel ;

if (argc != 4){
return 10;
}

ucode = strtol(argv[2], NULL , 16);
psize = strtol(argv[3], NULL , 10);

printf("unicode +%X size %d/n" , ucode, psize);
if (FT_Init_FreeType(&library)
|| FT_New_Face(library,
argv[1],
0,
&face)
|| FT_Set_Pixel_Sizes(face,
psize,
0)){
return 1;
}

glyph_index = FT_Get_Char_Index(face, ucode);
if (glyph_index == 0){
return 2;
}
if (FT_Load_Glyph(face,
glyph_index,
FT_LOAD_DEFAULT)){
return 3;
}
if (FT_Render_Glyph(face->glyph,
FT_RENDER_MODE_MONO)){
return 4;
}
printf("bitmap_left=%d/n"
"bitmap_top=%d/n"
"bitmap.rows=%d/n"
"bitmap.width=%d/n"
"bitmap.pitch=%d/n"
"bitmap.pixel_mode=%d/n" ,
face->glyph->bitmap_left,
face->glyph->bitmap_top,
face->glyph->bitmap.rows,
face->glyph->bitmap.width,
face->glyph->bitmap.pitch,
face->glyph->bitmap.pixel_mode);
printf("/n/n" );
for (row = 0;
row < (face->glyph->bitmap.rows - face->glyph->bitmap_top);
++row){
for (pixel = 0; pixel < face->glyph->bitmap.width; ++pixel){
printf("_" );
}
printf("/n" );
}
for (row = 0; row < face->glyph->bitmap.rows; ++row){
for (pixel = 0; pixel < face->glyph->bitmap_left; ++pixel)
printf("_" );
for (pixel = 0; pixel < face->glyph->bitmap.width; ++pixel){
printf("%c" , (face->glyph->bitmap.buffer
[row * face->glyph->bitmap.pitch +
pixel/8] & (0xC0 >> (pixel % 8)))?'O' :'_' );
}
printf("/n" );
}
return 0;
}

Makefile

all

 : getbitmap

getbitmap.o : getbitmap.c
gcc $(CXXFLAGS ) -c `pkg-config freetype2 --cflags` $< -o $ @

getbitmap : getbitmap.o
gcc $(CXXFLAGS ) `pkg-config freetype2 --libs` -lm $< -o $ @

clean :
rm -f *.o getbitmap


運行效果:

hellwolf

@



cocteau

#



pts/1



%



J0S2



:



getbitmap



$



 ./getbitmap /usr/share/fonts/chinese/True
Type/uming.ttf $(mygetunicode 永) 12
unicode +6C38 size 12
bitmap_left=0
bitmap_top=10
bitmap.rows=11
bitmap.width=11
bitmap.pitch=2
bitmap.pixel_mode=1


___________
____OOO____
______OO___
__OOOOO____
_____OO_OO_
OOOOOOO_O__
___OOOOO___
___OOOOO___
__OO_OO_O__
_OOOOOO_OO_
OO__OOO_OOO
O___OO___OO
hellwolf @ cocteau # pts/1 % J0S2 : getbitmap $ ./getbitmap /usr/share/fonts/chinese/True
Type/uming.ttf $(mygetunicode 永) 16
unicode +6C38 size 16
bitmap_left=1
bitmap_top=14
bitmap.rows=15
bitmap.width=14
bitmap.pitch=2
bitmap.pixel_mode=1


______________
_____OO________
______OOO______
_______OO______
_______________
__OOOOOOO__OOO_
_______OO__OO__
____OO_OO_OO___
_OOOOOOOOOO____
____OO_OOO_____
___OO__OOOO____
___OO__OOOO____
__OO___OO_OO___
_OO_OO_OO__OOO_
_O___OOOO___OOO
______OO_____OO
hellwolf @ cocteau # pts/1 % J0S2 : getbitmap $ ./getbitmap /usr/share/fonts/chinese/True
Type/uming.ttf $(mygetunicode 永) 20
unicode +6C38 size 20
bitmap_left=0
bitmap_top=18
bitmap.rows=20
bitmap.width=20
bitmap.pitch=4
bitmap.pixel_mode=1


____________________
____________________
____________________
________OOO_________
_________OOO________
____________________
_________OO_________
____OOOOOOOO____O___
_________OO___OOOO__
_________OOO__OO____
OOOOOOOO_OOO_OO_____
_____OOO_OOOOO______
_____OO__OOOO_______
_____OO__OOOO_______
____OOO__OO_OO______
____OO___OO__OO_____
___OO____OO__OOO____
___OO____OO___OOO___
__OO_____OO_____OOOO
_OO______OO_________
OO______OOO_________
____________________
hellwolf @ cocteau # pts/1 % J0S2 : getbitmap $ ./getbitmap /usr/share/fonts/chinese/True
Type/uming.ttf $(mygetunicode 中) 16
unicode +4E2D size 16
bitmap_left=1
bitmap_top=14
bitmap.rows=15
bitmap.width=14
bitmap.pitch=2
bitmap.pixel_mode=1


______________
______OOO______
______OO_______
______OO_______
______OO____OO_
_OOOOOOOOOOOOOO
_O____OO____OO_
_O____OO____OO_
_O____OO____OO_
_OOOOOOOOOOOOO_
_O____OO____OO_
______OO_______
______OO_______
______OO_______
______OO_______
______OO_______
hellwolf @ cocteau # pts/1 % J0S2 : getbitmap $ ./getbitmap /usr/share/fonts/chinese/True
Type/uming.ttf $(mygetunicode 中) 32
unicode +4E2D size 32
bitmap_left=4
bitmap_top=28
bitmap.rows=32
bitmap.width=26
bitmap.pitch=4
bitmap.pixel_mode=1


__________________________
__________________________
__________________________
__________________________
______________OO______________
______________OOOO____________
______________OOO_____________
______________OOO_____________
______________OOO_____________
______________OOO_____________
____O_________OOO________OOO__
____OOOOOOOOOOOOOOOOOOOOOOOOO_
____OO________OOO________OOO__
____OO________OOO________OOO__
____OO________OOO________OOO__
____OO________OOO________OOO__
____OO________OOO________OOO__
____OO________OOO________OOO__
____OO________OOO________OOO__
____OO________OOO________OOO__
____OO________OOO________OOO__
____OOOOOOOOOOOOOOOOOOOOOOOO__
____OO________OOO________OOO__
____OO________OOO________OOO__
____O_________OOO________OO___
______________OOO_____________
______________OOO_____________
______________OOO_____________
______________OOO_____________
______________OOO_____________
______________OOO_____________
______________OOO_____________
______________OOO_____________
______________OOO_____________
______________OO______________
______________________________
hellwolf @ cocteau # pts/1 % J0S2 : getbitmap $
發佈了60 篇原創文章 · 獲贊 12 · 訪問量 39萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章