sync with my cvs
[nestedvm.git] / src / tests / FreeTypeDemoHelper.c
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2
3 #include <unistd.h>
4 #include <freetype/freetype.h>
5
6 FT_Library  library;   /* handle to library     */
7 FT_Face     face;      /* handle to face object */
8
9 #define FT_Check(expr,err) do { \
10     if((expr) != 0) { \
11         errprint(#expr " failed\n"); \
12             return err; \
13     } \
14 } while(0)
15
16 #define max(a,b) ((a) > (b) ? (a) : (b))
17 #define min(a,b) ((a) < (b) ? (a) : (b))
18
19 static int errprint(const char *s) {
20     int l = strlen(s);
21     int n;
22     while(l) {
23         n = write(STDERR_FILENO,s,l);
24         if(n < 0) return n;
25         l -= n;
26         s += n;
27     }
28     return 0;
29 }
30         
31 void draw(FT_GlyphSlot glyph,int x, char *buf, int buf_width, int buf_height, int baseline) {
32     int y = max(baseline - glyph->bitmap_top,0);
33     int rows = glyph->bitmap.rows;
34     int width = glyph->bitmap.width;
35     int i,j;
36     x = x + glyph->bitmap_left;
37     if(x + width >= buf_width) return;
38     if(y + rows >= buf_height) return;
39     //if(buf == NULL) fprintf(stderr,"ABout to dereference %p\n",buf);
40     for(i=0;i<rows;i++)
41         for(j=0;j<width;j++)
42             buf[(i+y)*buf_width+x+j] |= glyph->bitmap.buffer[i*width+j];
43 }
44
45 /* Prevent --gc-sections from blowing this away */
46 int render(short *s, int size, char *buf, int buf_width, int buf_height, int baseline)  __attribute__((section(".text")));
47 int render(short *s, int size, char *buf, int buf_width, int buf_height, int baseline) {
48     int glyph_index;
49     int x = 0;
50     FT_Check(FT_Set_Pixel_Sizes(face,0,size),0);
51     memset(buf,'\0',buf_width*buf_height);
52     //fprintf(stderr,"Rendering %d pt %c... at %p (%dx%d)\n",size,*s,buf,buf_width,buf_height);
53     while(*s) {
54         glyph_index = FT_Get_Char_Index(face,*s);
55         FT_Check(FT_Load_Glyph(face,glyph_index,FT_LOAD_DEFAULT),0);
56         FT_Check(FT_Render_Glyph(face->glyph,FT_RENDER_MODE_NORMAL/*256color antialiased*/),0);
57         draw(face->glyph,x,buf,buf_width,buf_height,baseline);
58         x += face->glyph->advance.x/64;
59         s++;
60     }
61     return 1;
62 }
63
64 char * user_info[2];
65 extern void _pause();
66
67 int main(int argc,char** argv) {
68     char *fontdata;
69     int fontsize;
70
71     _pause();
72     
73     fontdata = user_info[0];
74     fontsize = (int)user_info[1];
75     
76     //fprintf(stderr,"Initializng freetype with a %d byte font at %p\n", fontsize, fontdata);
77     
78     FT_Check(FT_Init_FreeType(&library),EXIT_FAILURE);
79     FT_Check(FT_New_Memory_Face(library, fontdata,fontsize, 0, &face),EXIT_FAILURE);
80     
81     errprint("Freetype initialized\n");
82     _pause();
83     errprint("Unpaused\n");
84     
85     /* not reached */
86     return EXIT_FAILURE;
87 }