1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
7 FT_Library library; /* handle to library */
8 FT_Face face; /* handle to face object */
10 #define FT_Check(expr,err) do { \
12 errprint(#expr " failed\n"); \
17 #define max(a,b) ((a) > (b) ? (a) : (b))
18 #define min(a,b) ((a) < (b) ? (a) : (b))
20 static int errprint(const char *s) {
24 n = write(STDERR_FILENO,s,l);
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;
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);
43 buf[(i+y)*buf_width+x+j] |= glyph->bitmap.buffer[i*width+j];
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) {
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);
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;
68 int main(int argc,char** argv) {
74 fontdata = user_info[0];
75 fontsize = (int)user_info[1];
77 //fprintf(stderr,"Initializng freetype with a %d byte font at %p\n", fontsize, fontdata);
79 FT_Check(FT_Init_FreeType(&library),EXIT_FAILURE);
80 FT_Check(FT_New_Memory_Face(library, fontdata,fontsize, 0, &face),EXIT_FAILURE);
82 errprint("Freetype initialized\n");
84 errprint("Unpaused\n");