// ************************************************************************ // Test the font rendering capabilities of the freetype2 library by drawing // ascii characters to a PNG file using the gd library. // // compile with: gcc -g -Wall test_ft.c -I/usr/include/freetype2 -lfreetype -lgd -o test_ft #include #include #include #include #include #define FT_DEFAULT_DPI 72 // ************************************************************************ void draw_bitmap_glyph(gdImagePtr im, FT_Bitmap bitmap, FT_Int xstart, FT_Int ystart, int color) { FT_Int x = 0, y = 0; FT_Int row = 0, col = 0; int bit_value = 0; unsigned char byte = 0; for (row = 0; row < bitmap.rows; row++ ) { y = ystart + row; for (col = 0; col < bitmap.width; col++) { x = xstart + col; // select the correct byte from the bitmap byte = bitmap.buffer[(row * bitmap.pitch) + (col / 8)]; // determine the proper bit value bit_value = (byte >> (7 - (col % 8))) & 0x01; if (0 == bit_value) { continue; } gdImageSetPixel(im, x, y, color); } } } // ************************************************************************ int simple_draw_ascii_text(gdImagePtr im, int x, int y, FT_Face face, double fontsize, const char *text, int color) { FT_Error error = 0; FT_UInt glyph_index = 0; FT_Vector pen = { 0, 0 }; FT_Int32 load_flags = FT_LOAD_TARGET_MONO; FT_Int32 render_flags = FT_RENDER_MODE_MONO; int n = 0; if (0 != (error = FT_Set_Char_Size(face, 0, (FT_UInt) (fontsize * 64), FT_DEFAULT_DPI, FT_DEFAULT_DPI))) { fprintf(stderr, "cannot set FT face character size: %d\n", error); return -1; } pen.x = x; pen.y = y; for (n = 0; '\0' != text[n]; n++) { // convert character code to glyph index glyph_index = FT_Get_Char_Index(face, text[n]); // load glyph image into the slot without rendering if (0 != (error = FT_Load_Glyph( face, glyph_index, load_flags))) { fprintf(stderr, "unable to load glyph for character %d\n", text[n]); continue; } // render the glyph to a bitmap if (0 != (error = FT_Render_Glyph(face->glyph, render_flags))) { fprintf(stderr, "unable to render glyph for character %d\n", text[n]); continue; } // the pixel mode should be mono. If not draw_bitmap_glyph() won't work if (FT_PIXEL_MODE_MONO != face->glyph->bitmap.pixel_mode) { fprintf(stderr, "bitmap is not in mono pixel mode\n"); return -1; } // draw the glyph bitmap to the image draw_bitmap_glyph(im, face->glyph->bitmap, pen.x + face->glyph->bitmap_left, pen.y - face->glyph->bitmap_top, color); pen.x += face->glyph->advance.x >> 6; pen.y += face->glyph->advance.y >> 6; } return 0; } /************************************************************************/ #define WIDTH 500 #define HEIGHT 200 int main( int argc, char** argv ) { FT_Library ft_library = NULL; FT_Face face; FT_Int maj = 0; FT_Int min = 0; FT_Int patch = 0; gdImagePtr im = NULL; double fontsize = 0; FILE *out; char *fontpath = "arial.ttf"; char *img_name = "freetype_test.png"; char *text = "The quick brown fox jumped over the lazy dog"; int black = 0; int x = 0; int y = 0; if (argc > 1) { fontpath = argv[1]; } if (argc > 2) { img_name = argv[2]; } im = gdImageCreate(WIDTH, HEIGHT); // allocate the background color gdImageColorAllocate(im, 225, 225, 255); // allocate the foreground color black = gdImageColorAllocate(im, 0, 0, 0); // initialize the FreeType library and print the version number if (0 != FT_Init_FreeType(&ft_library)) { fprintf(stderr, "cannot init freetype library\n"); return -1; } FT_Library_Version(ft_library, &maj, &min, &patch); printf("FreeType Version: %d.%d.%d\n", maj, min, patch); // load the FT_Face from the font file if (0 != FT_New_Face(ft_library, fontpath, 0, &face)) { fprintf(stderr, "cannot create font face from file: %s\n", fontpath); return -1; } x = 6; y = 12; fontsize = 9; if (0 > simple_draw_ascii_text(im, x, y, face, fontsize, text, black)) { return -1; } y += 5 + fontsize; fontsize = 10; if (0 > simple_draw_ascii_text(im, x, y, face, fontsize, text, black)) { return -1; } y += 5 + fontsize; fontsize = 11; if (0 > simple_draw_ascii_text(im, x, y, face, fontsize, text, black)) { return -1; } y += 5 + fontsize; fontsize = 12; if (0 > simple_draw_ascii_text(im, x, y, face, fontsize, text, black)) { return -1; } y += 5 + fontsize; fontsize = 13; if (0 > simple_draw_ascii_text(im, x, y, face, fontsize, text, black)) { return -1; } y += 5 + fontsize; fontsize = 14; if (0 > simple_draw_ascii_text(im, x, y, face, fontsize, text, black)) { return -1; } y += 5 + fontsize; fontsize = 15; if (0 > simple_draw_ascii_text(im, x, y, face, fontsize, text, black)) { return -1; } y += 5 + fontsize; fontsize = 16; if (0 > simple_draw_ascii_text(im, x, y, face, fontsize, text, black)) { return -1; } y += 5 + fontsize; fontsize = 17; if (0 > simple_draw_ascii_text(im, x, y, face, fontsize, text, black)) { return -1; } y += 5 + fontsize; fontsize = 18; if (0 > simple_draw_ascii_text(im, x, y, face, fontsize, text, black)) { return -1; } if (NULL == (out = fopen(img_name, "w"))) { fprintf(stderr, "Error: %s: %s: Can't open output file.\n", img_name, strerror(errno)); return -1; } gdImagePng(im, out); gdImageDestroy(im); fclose(out); printf("finished drawing image: %s\n", img_name); FT_Done_Face(face); FT_Done_FreeType(ft_library); return 0; }