imported brians code
[nestedvm.git] / src / tests / FTBench.c
1 #include <stdio.h>
2 #include <freetype/freetype.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 #define FT_Check(expr) do { \
7     if((expr) != 0) { \
8         fprintf(stderr,#expr " failed\n"); \
9         exit(EXIT_FAILURE); \
10     } \
11 } while(0)
12
13 #define BMP_WIDTH 800
14 #define BMP_HEIGHT 600
15
16 static char buf[BMP_WIDTH*BMP_HEIGHT];
17
18 int main(int argc, char **argv) {
19     char *ttf;
20     char *out;
21     FT_Library library;
22     FT_Face face;
23     FT_GlyphSlot glyph;
24     int num_glyphs;
25     int c;
26     int glyph_index;
27     int loc_x;
28     int loc_y;
29     int glyph_width;
30     int glyph_height;
31     int i,j;
32     int fd;
33     char *p;
34     int n,count;
35     char *glyph_buf;
36     int pixel_size;
37     
38     if(argc < 3) {
39         fprintf(stderr,"Usage: %s ttf bmp\n",argv[0]);
40         exit(1);
41     }
42     
43     ttf = argv[1];
44     out = argv[2];
45     
46     memset(buf,'\377',BMP_WIDTH*BMP_HEIGHT);
47     
48     FT_Check(FT_Init_FreeType(&library));
49     FT_Check(FT_New_Face(library,ttf,0,&face));
50     
51     loc_y = loc_x = 0;
52     for(pixel_size=8;pixel_size<48;pixel_size+=4) {
53         FT_Check(FT_Set_Pixel_Sizes(face,0,pixel_size));
54         for(c=32;c<127;c++) {
55             glyph_index = FT_Get_Char_Index(face,c);
56             FT_Check(FT_Load_Glyph(face,glyph_index,FT_LOAD_DEFAULT));
57             FT_Check(FT_Render_Glyph(face->glyph, ft_render_mode_normal));
58             glyph = face->glyph;
59             glyph_width = glyph->bitmap.width;
60             glyph_height = glyph->bitmap.rows;
61             glyph_buf = glyph->bitmap.buffer;
62             if(loc_x + glyph_width + glyph->bitmap_left >= BMP_WIDTH) {
63                 loc_x = 0;
64                 loc_y += pixel_size;
65                 if(loc_y >= BMP_HEIGHT-pixel_size) goto done;
66             }
67             
68             for(i=0;i<glyph_height;i++)
69                 for(j=0;j<glyph_width;j++)
70                     buf[(loc_y+i)*BMP_WIDTH+loc_x+j] &= (~glyph_buf[i*glyph_width+j]);
71             loc_x += face->glyph->advance.x/64;
72         }
73     }
74 done:
75     
76     if((fd = open(out,O_CREAT|O_WRONLY,0644)) < 0) {
77         perror("open");
78         exit(1);
79     }
80     p = buf;
81     count = BMP_WIDTH*BMP_HEIGHT;
82     
83     while(count) {
84         n = write(fd,p,count);
85         if(n < 0) {
86             perror("write");
87             exit(1);
88         }
89         count -=n;
90         p += n;
91     }
92     close(fd);
93     
94     return 0;
95 }