2014-12-02 19:00:08 +08:00
# include "stb_rect_pack.h"
2014-05-26 11:56:24 +08:00
# define STB_TRUETYPE_IMPLEMENTATION
# include "stb_truetype.h"
2014-12-02 19:00:08 +08:00
# include "stb_image_write.h"
2014-05-26 11:56:24 +08:00
# include <stdio.h>
char ttf_buffer [ 1 < < 25 ] ;
2014-09-11 06:35:06 +08:00
unsigned char output [ 512 * 100 ] ;
2014-05-26 11:56:24 +08:00
# ifdef TT_TEST
2014-09-11 06:35:06 +08:00
void debug ( void )
{
stbtt_fontinfo font ;
fread ( ttf_buffer , 1 , 1 < < 25 , fopen ( " c:/x/lm/LiberationMono-Regular.ttf " , " rb " ) ) ;
stbtt_InitFont ( & font , ttf_buffer , 0 ) ;
stbtt_MakeGlyphBitmap ( & font , output , 6 , 9 , 512 , 5.172414E-03 f , 5.172414E-03 f , 54 ) ;
}
2014-12-02 19:00:08 +08:00
# define BITMAP_W 256
2014-12-07 04:28:46 +08:00
# define BITMAP_H 512
2014-12-02 19:00:08 +08:00
unsigned char temp_bitmap [ BITMAP_H ] [ BITMAP_W ] ;
stbtt_bakedchar cdata [ 256 * 2 ] ; // ASCII 32..126 is 95 glyphs
2014-12-07 04:28:46 +08:00
stbtt_packedchar pdata [ 256 * 2 ] ;
2014-05-26 11:56:24 +08:00
int main ( int argc , char * * argv )
{
stbtt_fontinfo font ;
unsigned char * bitmap ;
int w , h , i , j , c = ( argc > 1 ? atoi ( argv [ 1 ] ) : 34807 ) , s = ( argc > 2 ? atoi ( argv [ 2 ] ) : 32 ) ;
2014-12-02 19:00:08 +08:00
//debug();
2014-09-11 06:35:06 +08:00
2014-12-02 19:00:08 +08:00
// @TODO: why is minglui.ttc failing?
2014-05-26 11:56:24 +08:00
fread ( ttf_buffer , 1 , 1 < < 25 , fopen ( argc > 3 ? argv [ 3 ] : " c:/windows/fonts/mingliu.ttc " , " rb " ) ) ;
2014-12-22 00:32:09 +08:00
#if 0
2014-12-02 19:00:08 +08:00
stbtt_BakeFontBitmap ( ttf_buffer , stbtt_GetFontOffsetForIndex ( ttf_buffer , 0 ) , 40.0 , temp_bitmap [ 0 ] , BITMAP_W , BITMAP_H , 32 , 96 , cdata ) ; // no guarantee this fits!
stbi_write_png ( " fonttest1.png " , BITMAP_W , BITMAP_H , 1 , temp_bitmap , 0 ) ;
{
stbtt_pack_context pc ;
2014-12-07 04:28:46 +08:00
stbtt_PackBegin ( & pc , temp_bitmap [ 0 ] , BITMAP_W , BITMAP_H , 0 , 1 , NULL ) ;
stbtt_PackFontRange ( & pc , ttf_buffer , 0 , 20.0 , 32 , 95 , pdata ) ;
stbtt_PackFontRange ( & pc , ttf_buffer , 0 , 20.0 , 0xa0 , 0x100 - 0xa0 , pdata ) ;
2014-12-02 19:00:08 +08:00
stbtt_PackEnd ( & pc ) ;
stbi_write_png ( " fonttest2.png " , BITMAP_W , BITMAP_H , 1 , temp_bitmap , 0 ) ;
}
{
stbtt_pack_context pc ;
stbtt_pack_range pr [ 2 ] ;
2014-12-07 04:28:46 +08:00
stbtt_PackBegin ( & pc , temp_bitmap [ 0 ] , BITMAP_W , BITMAP_H , 0 , 1 , NULL ) ;
2014-12-02 19:00:08 +08:00
2014-12-07 04:28:46 +08:00
pr [ 0 ] . chardata_for_range = pdata ;
2014-12-02 19:00:08 +08:00
pr [ 0 ] . first_unicode_char_in_range = 32 ;
pr [ 0 ] . num_chars_in_range = 95 ;
2014-12-07 04:28:46 +08:00
pr [ 0 ] . font_size = 20.0f ;
pr [ 1 ] . chardata_for_range = pdata + 256 ;
2014-12-02 19:00:08 +08:00
pr [ 1 ] . first_unicode_char_in_range = 0xa0 ;
pr [ 1 ] . num_chars_in_range = 0x100 - 0xa0 ;
2014-12-07 04:28:46 +08:00
pr [ 1 ] . font_size = 20.0f ;
2014-12-02 19:00:08 +08:00
2014-12-07 04:28:46 +08:00
stbtt_PackSetOversampling ( & pc , 2 , 2 ) ;
2014-12-02 19:00:08 +08:00
stbtt_PackFontRanges ( & pc , ttf_buffer , 0 , pr , 2 ) ;
stbtt_PackEnd ( & pc ) ;
stbi_write_png ( " fonttest3.png " , BITMAP_W , BITMAP_H , 1 , temp_bitmap , 0 ) ;
}
return 0 ;
2014-12-22 00:32:09 +08:00
# endif
2014-12-02 19:00:08 +08:00
2014-05-26 11:56:24 +08:00
stbtt_InitFont ( & font , ttf_buffer , stbtt_GetFontOffsetForIndex ( ttf_buffer , 0 ) ) ;
bitmap = stbtt_GetCodepointBitmap ( & font , 0 , stbtt_ScaleForPixelHeight ( & font , ( float ) s ) , c , & w , & h , 0 , 0 ) ;
for ( j = 0 ; j < h ; + + j ) {
for ( i = 0 ; i < w ; + + i )
putchar ( " .:ioVM@ " [ bitmap [ j * w + i ] > > 5 ] ) ;
putchar ( ' \n ' ) ;
}
return 0 ;
}
# endif