1 #include "stb_rect_pack.h"
2 #define STB_TRUETYPE_IMPLEMENTATION
3 #include "stb_truetype.h"
4 #include "stb_image_write.h"
10 char ttf_buffer
[1<<25];
11 unsigned char output
[512*100];
16 fread(ttf_buffer
, 1, 1<<25, fopen("c:/x/lm/LiberationMono-Regular.ttf", "rb"));
17 stbtt_InitFont(&font
, ttf_buffer
, 0);
19 stbtt_MakeGlyphBitmap(&font
, output
, 6, 9, 512, 5.172414E-03f
, 5.172414E-03f
, 54);
24 unsigned char temp_bitmap
[BITMAP_H
][BITMAP_W
];
25 stbtt_bakedchar cdata
[256*2]; // ASCII 32..126 is 95 glyphs
26 stbtt_packedchar pdata
[256*2];
28 int main(int argc
, char **argv
)
31 unsigned char *bitmap
;
32 int w
,h
,i
,j
,c
= (argc
> 1 ? atoi(argv
[1]) : 34807), s
= (argc
> 2 ? atoi(argv
[2]) : 32);
36 // @TODO: why is minglui.ttc failing?
37 fread(ttf_buffer
, 1, 1<<25, fopen(argc
> 3 ? argv
[3] : "c:/windows/fonts/mingliu.ttc", "rb"));
39 //fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/x/DroidSansMono.ttf", "rb"));
41 static stbtt_pack_context pc
;
42 static stbtt_packedchar cd
[256];
43 static unsigned char atlas
[1024*1024];
45 stbtt_PackBegin(&pc
, atlas
, 1024,1024,1024,1,NULL
);
46 stbtt_PackFontRange(&pc
, ttf_buffer
, 0, 32.0, 0, 256, cd
);
51 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!
52 stbi_write_png("fonttest1.png", BITMAP_W
, BITMAP_H
, 1, temp_bitmap
, 0);
55 stbtt_pack_context pc
;
56 stbtt_PackBegin(&pc
, temp_bitmap
[0], BITMAP_W
, BITMAP_H
, 0, 1, NULL
);
57 stbtt_PackFontRange(&pc
, ttf_buffer
, 0, 20.0, 32, 95, pdata
);
58 stbtt_PackFontRange(&pc
, ttf_buffer
, 0, 20.0, 0xa0, 0x100-0xa0, pdata
);
60 stbi_write_png("fonttest2.png", BITMAP_W
, BITMAP_H
, 1, temp_bitmap
, 0);
64 stbtt_pack_context pc
;
65 stbtt_pack_range pr
[2];
66 stbtt_PackBegin(&pc
, temp_bitmap
[0], BITMAP_W
, BITMAP_H
, 0, 1, NULL
);
68 pr
[0].chardata_for_range
= pdata
;
69 pr
[0].first_unicode_char_in_range
= 32;
70 pr
[0].num_chars_in_range
= 95;
71 pr
[0].font_size
= 20.0f
;
72 pr
[1].chardata_for_range
= pdata
+256;
73 pr
[1].first_unicode_char_in_range
= 0xa0;
74 pr
[1].num_chars_in_range
= 0x100 - 0xa0;
75 pr
[1].font_size
= 20.0f
;
77 stbtt_PackSetOversampling(&pc
, 2, 2);
78 stbtt_PackFontRanges(&pc
, ttf_buffer
, 0, pr
, 2);
80 stbi_write_png("fonttest3.png", BITMAP_W
, BITMAP_H
, 1, temp_bitmap
, 0);
85 stbtt_InitFont(&font
, ttf_buffer
, stbtt_GetFontOffsetForIndex(ttf_buffer
,0));
86 bitmap
= stbtt_GetCodepointBitmap(&font
, 0,stbtt_ScaleForPixelHeight(&font
, (float)s
), c
, &w
, &h
, 0,0);
88 for (j
=0; j
< h
; ++j
) {
90 putchar(" .:ioVM@"[bitmap
[j
*w
+i
]>>5]);