#include "ftmisc.h" #include "ftimage.h" #include "malloc.h" #include // Define an acorn shape to test with static float k_shape[][2] = { {-3, -18}, {0, -12}, {6, -10}, {12, -6}, {12, -4}, {11, -4}, {10, -5}, {10, 1}, {9, 6}, {7, 10}, {5, 12}, {4, 15},{3, 14}, {1, 13}, {-1, 13}, {-5, 11}, {-8, 8}, {-11, 2}, {-11, -2}, {-14, 0}, {-14, -2}, {-11, -7}, {-9, -9}, {-8, -9}, {-5, -12}, {-5, -14}, {-7, -15}, {-8, -14}, {-9, -15}, {-9, -17}, {-7, -17}, {-6, -18} }; // Some freetype definitions ripped to get this to work in standalone. typedef struct FT_MemoryRec_* FT_Memory; typedef void* (*FT_Alloc_Func)(FT_Memory memory, long size ); typedef void (*FT_Free_Func)(FT_Memory memory, void* block ); typedef void* (*FT_Realloc_Func)(FT_Memory memory,long cur_size, long new_size, void* block); struct FT_MemoryRec_ { void* user; FT_Alloc_Func alloc; FT_Free_Func free; FT_Realloc_Func realloc; }; // Just use malloc and free void* MY_Alloc_Func(FT_Memory memory, long size) { return malloc(size); } void MY_Free_Func(FT_Memory memory, void *block) { free(block); } void* MY_Realloc_Func(FT_Memory memory, long cur_size, long new_size, void* block) { return realloc(block, new_size); } FT_Memory mem; extern "C" FT_Raster_Funcs ft_standard_raster; // Render a shape and dump it out as a raw image void main() { // Set up the memory management to use malloc and free mem = new FT_MemoryRec_; mem->alloc = MY_Alloc_Func; mem->free = MY_Free_Func; mem->realloc = MY_Realloc_Func; // Build an outline manually from our acorn shape FT_Outline_ outline; outline.n_contours = 1; outline.n_points = sizeof(k_shape)/(sizeof(float) * 2); outline.points = new FT_Vector[outline.n_points]; // offset it to fit in the image and scale it up 10 times for (int i = 0; i < outline.n_points; ++i) { FT_Vector v; v.x = (20 + k_shape[i][0]) * 10 * 64; v.y = (20 + k_shape[i][1]) * 10 * 64; outline.points[i] = v; } outline.tags = new char[outline.n_points]; for (int i = 0; i < outline.n_points; ++i) outline.tags[i] = 1; outline.contours = new short[outline.n_contours]; outline.contours[0] = outline.n_points - 1; outline.flags = 0; // Set up a bitmap FT_Bitmap bmp; memset(&bmp, 0, sizeof(bmp)); bmp.buffer = new unsigned char[400*400*1]; memset(bmp.buffer, 0xff, 400*400*1); bmp.width = 400; bmp.rows = 400; bmp.pitch = -400*1; bmp.pixel_mode = FT_PIXEL_MODE_GRAY; bmp.num_grays = 5; // Set up the raster params (these seem to be the only two checked). FT_Raster_Params params; memset(¶ms, 0, sizeof(params)); params.source = &outline; params.target = &bmp; params.flags = FT_RASTER_FLAG_AA; // Allocate a chunk of mem for the render pool. const int kRenderPoolSize = 1024 * 1024; unsigned char *renderPool = new unsigned char[kRenderPoolSize]; // Initialize the rasterer and get it to render into the bitmap. FT_Raster raster; ft_standard_raster.raster_new(mem, &raster); ft_standard_raster.raster_reset(raster, renderPool, kRenderPoolSize); ft_standard_raster.raster_render(raster, ¶ms); // Dump out the raw image data. std::ofstream out("out.raw", std::ios::binary); out.write((const char *)bmp.buffer, 400*400*1); }