1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
4 #include <freetype/freetype.h>
6 FT_Library library; /* handle to library */
7 FT_Face face; /* handle to face object */
9 #define FT_Check(expr,err) do { \
11 errprint(#expr " failed\n"); \
16 #define max(a,b) ((a) > (b) ? (a) : (b))
17 #define min(a,b) ((a) < (b) ? (a) : (b))
19 static int errprint(const char *s) {
23 n = write(STDERR_FILENO,s,l);
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;
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);
42 buf[(i+y)*buf_width+x+j] |= glyph->bitmap.buffer[i*width+j];
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) {
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);
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;
67 int main(int argc,char** argv) {
73 fontdata = user_info[0];
74 fontsize = (int)user_info[1];
76 //fprintf(stderr,"Initializng freetype with a %d byte font at %p\n", fontsize, fontdata);
78 FT_Check(FT_Init_FreeType(&library),EXIT_FAILURE);
79 FT_Check(FT_New_Memory_Face(library, fontdata,fontsize, 0, &face),EXIT_FAILURE);
81 errprint("Freetype initialized\n");
83 errprint("Unpaused\n");