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