/* example1.c */ /* */ /* This small program shows how to print a rotated string with the */ /* FreeType 2 library. */ #include #include #include #include #include FT_FREETYPE_H #include FT_GLYPH_H /* FIXME */ #define WIDTH 2048 #define HEIGHT 1024 //#define DEBUG /* origin is the upper left corner */ unsigned char image[HEIGHT][WIDTH]; /* Replace this function with something useful. */ void draw_bitmap( FT_Bitmap* bitmap, FT_Int x, FT_Int y) { FT_Int i, j, p, q; FT_Int x_max = x + bitmap->width; FT_Int y_max = y + bitmap->rows; if ( bitmap->pixel_mode == FT_PIXEL_MODE_MONO) { /* for monochrome */ for ( i = x, p = 0; i < x_max; i++, p++ ) { for ( j = y, q = 0; j < y_max; j++, q++ ) { int ix, ii; char b; if ( i >= WIDTH || j >= HEIGHT ) continue; ix = p / 8; b = bitmap->buffer[q * bitmap->pitch + ix ]; ii = 8 - p % 8; b = (b & 1 << ii) ? 1 : 0; image[j - 1][i - 1] |= b; } } } else { for ( i = x, p = 0; i < x_max; i++, p++ ) { for ( j = y, q = 0; j < y_max; j++, q++ ) { if ( i >= WIDTH || j >= HEIGHT ) continue; image[j][i] |= bitmap->buffer[q * bitmap->width + p]; } } } } void show_image( int w, int h ) { int i, j; #ifdef DEBUG printf("# bitmap %dx%d\n", w, h); for ( i = 0; i < h; i++ ) { for ( j = 0; j < w; j++ ) putchar( image[i][j] == 0 ? '-' : image[i][j] < 128 ? '+' : '*' ); putchar( '\n' ); } #else printf("P2 %d %d %d\n", w, h, 255); for ( i = 0; i < h; i++ ) { for ( j = 0; j < w; j++ ) printf("%d ", image[i][j] == 0 ? 255 : 255 - image[i][j]); putchar( '\n' ); } #endif } /* Decode and return the next UTF8 character refered to by the given string */ /* handle. See that *((*p)++) business down there? You shouldn't do that. */ static int utf8(const char **p) { if ((**p & 0x80) == 0x00) { int a = *((*p)++); return a; } if ((**p & 0xE0) == 0xC0) { int a = *((*p)++) & 0x1F; int b = *((*p)++) & 0x3F; return (a << 6) | b; } if ((**p & 0xF0) == 0xE0) { int a = *((*p)++) & 0x0F; int b = *((*p)++) & 0x3F; int c = *((*p)++) & 0x3F; return (a << 12) | (b << 6) | c; } if ((**p & 0xF8) == 0xF0) { int a = *((*p)++) & 0x07; int b = *((*p)++) & 0x3F; int c = *((*p)++) & 0x3F; int d = *((*p)++) & 0x3F; return (a << 18) | (b << 12) | (c << 8) | d; } return 0; } int main( int argc, char** argv ) { FT_Library library; FT_Face face; FT_GlyphSlot slot; FT_Matrix matrix; /* transformation matrix */ FT_UInt glyph_index; FT_Vector pen; /* untransformed origin */ FT_Error error; char* filename; char* text; double angle; int target_height; int ch, w, h; int psize, dpi; const char *p; w = h = 0; psize = 100; /* */ dpi = 72; /* */ if ( argc != 3 ) { fprintf ( stderr, "usage: %s font sample-text\n", argv[0] ); exit( 1 ); } filename = argv[1]; /* first argument */ text = argv[2]; /* second argument */ angle = ( 0.0 / 360 ) * 3.14159 * 2; /* use 0 degrees */ target_height = HEIGHT; error = FT_Init_FreeType( &library ); /* initialize library */ /* error handling omitted */ error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */ /* error handling omitted */ /* select encoding */ error = FT_Select_Charmap(face, FT_ENCODING_UNICODE); if ( error ) { fprintf ( stderr, "FATAL: encoding not found\n"); exit( 1 ); } /* use "psize" pt at "dpi" dpi */ error = FT_Set_Char_Size( face, psize * 64, 0, dpi, dpi ); /* set character size */ /* error handling omitted */ slot = face->glyph; /* set up matrix */ matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L ); matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L ); matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L ); matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L ); /* the pen position in 26.6 cartesian space coordinates; */ /* start at (300,200) relative to the upper left corner */ pen.x = 0 * 64; //pen.y = ( target_height - 100 ) * 64; pen.y = 0 * 64; /* try to set transformation */ FT_Set_Transform( face, &matrix, &pen ); #ifdef DEBUG printf("# x_ppem= %d, y_ppem= %d\n", face->size->metrics.x_ppem, face->size->metrics.y_ppem); printf("# height= %d, y_ppem= %d\n", face->size->metrics.height / 64, face->size->metrics.y_ppem); printf("# ascender= %d, descender= %d\n\n", face->size->metrics.ascender / 64, face->size->metrics.descender / 64); #endif pen.y = - face->size->metrics.descender; pen.y = face->size->metrics.height - face->size->metrics.x_ppem * 64; p = text; while ( ch = utf8(&p)) { FT_UInt glyph_index; FT_BBox bbox; FT_Glyph glyph; /* set transformation */ FT_Set_Transform( face, &matrix, &pen ); #if 0 /* load glyph image into the slot (erase previous one) */ error = FT_Load_Char( face, ch, FT_LOAD_RENDER ); //error = FT_Load_Char( face, ch, FT_LOAD_RENDER | FT_LOAD_TARGET_MONO ); if ( error ) continue; /* ignore errors */ #else glyph_index = FT_Get_Char_Index( face, ch ); /* load glyph image into the slot (erase previous one) */ //error = FT_Load_Glyph( face, glyph_index, FT_LOAD_MONOCHROME ); error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT ); if ( error ) continue; /* ignore errors */ /* convert to an anti-aliased bitmap */ error = FT_Render_Glyph( face->glyph, FT_RENDER_MODE_NORMAL ); //error = FT_Render_Glyph( face->glyph, FT_RENDER_MODE_MONO ); if ( error ) continue; #endif /* get bbox */ error = FT_Get_Glyph( face->glyph, &glyph ); if ( error ) continue; FT_Glyph_Get_CBox( glyph, FT_GLYPH_BBOX_PIXELS, &bbox ); //h = face->size->metrics.height / 64; h = face->size->metrics.y_ppem; w+= slot->advance.x / 64; //w+= slot->advance.x / 64 + slot->bitmap_left; #ifdef DEBUG printf("# x_ppem= %d, y_ppem= %d\n", face->size->metrics.x_ppem, face->size->metrics.y_ppem); printf("# height= %d, y_ppem= %d\n", face->size->metrics.height / 64, face->size->metrics.y_ppem); printf("# ascender= %d, descender= %d\n", face->size->metrics.ascender / 64, face->size->metrics.descender / 64); printf("# bbox.yMax = %d, bbox.yMin = %d\n", bbox.yMax, bbox.yMin); printf("# bitmap_top = %d\n", slot->bitmap_top); printf("# horiBearingY= %d, horiBearingX= %d\n", face->glyph->metrics.horiBearingY / 64, face->glyph->metrics.horiBearingX / 64); printf("# vertBearingY= %d, vertBearingX= %d\n", face->glyph->metrics.vertBearingY / 64, face->glyph->metrics.vertBearingX / 64); printf("# ypos = %d\n", face->size->metrics.height / 64 - slot->bitmap_top + face->glyph->metrics.horiBearingY / 64); printf("# bitmap width = %d\n", slot->bitmap.width); printf("# bitmap width = %d, pen.x = %d\n", slot->bitmap.width, slot->advance.x / 64); printf("# pen.x = %d, pen.y = %d\n", pen.x / 64, pen.y / 64); printf("# y_ppem - bitmap_top = %d\n", face->size->metrics.y_ppem / 64 - slot->bitmap_top); #endif //exit(1); /* now, draw to our target surface (convert position) */ draw_bitmap( &slot->bitmap, slot->bitmap_left, //target_height - slot->bitmap_top ); //face->size->metrics.height / 64 - slot->bitmap_top); face->size->metrics.y_ppem - slot->bitmap_top); /* increment pen position */ pen.x += slot->advance.x; pen.y += slot->advance.y; } show_image(w, h); FT_Done_Face ( face ); FT_Done_FreeType( library ); return 0; } /* EOF */