added stb, more binaryout changes"
[henge/apc.git] / stb / tests / test_truetype.c
1 #include "stb_rect_pack.h"
2 #define STB_TRUETYPE_IMPLEMENTATION
3 #include "stb_truetype.h"
4 #include "stb_image_write.h"
5
6 #ifdef TT_TEST
7
8 #include <stdio.h>
9
10 char ttf_buffer[1<<25];
11 unsigned char output[512*100];
12
13 void debug(void)
14 {
15 stbtt_fontinfo font;
16 fread(ttf_buffer, 1, 1<<25, fopen("c:/x/lm/LiberationMono-Regular.ttf", "rb"));
17 stbtt_InitFont(&font, ttf_buffer, 0);
18
19 stbtt_MakeGlyphBitmap(&font, output, 6, 9, 512, 5.172414E-03f, 5.172414E-03f, 54);
20 }
21
22 #define BITMAP_W 256
23 #define BITMAP_H 512
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];
27
28 int main(int argc, char **argv)
29 {
30 stbtt_fontinfo font;
31 unsigned char *bitmap;
32 int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) : 34807), s = (argc > 2 ? atoi(argv[2]) : 32);
33
34 //debug();
35
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"));
38
39 //fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/x/DroidSansMono.ttf", "rb"));
40 {
41 static stbtt_pack_context pc;
42 static stbtt_packedchar cd[256];
43 static unsigned char atlas[1024*1024];
44
45 stbtt_PackBegin(&pc, atlas, 1024,1024,1024,1,NULL);
46 stbtt_PackFontRange(&pc, ttf_buffer, 0, 32.0, 0, 256, cd);
47 stbtt_PackEnd(&pc);
48 }
49
50 #if 0
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);
53
54 {
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);
59 stbtt_PackEnd(&pc);
60 stbi_write_png("fonttest2.png", BITMAP_W, BITMAP_H, 1, temp_bitmap, 0);
61 }
62
63 {
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);
67
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;
76
77 stbtt_PackSetOversampling(&pc, 2, 2);
78 stbtt_PackFontRanges(&pc, ttf_buffer, 0, pr, 2);
79 stbtt_PackEnd(&pc);
80 stbi_write_png("fonttest3.png", BITMAP_W, BITMAP_H, 1, temp_bitmap, 0);
81 }
82 return 0;
83 #endif
84
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);
87
88 for (j=0; j < h; ++j) {
89 for (i=0; i < w; ++i)
90 putchar(" .:ioVM@"[bitmap[j*w+i]>>5]);
91 putchar('\n');
92 }
93 return 0;
94 }
95 #endif