Rasterizer generate C files

This commit is contained in:
Romain Goyet
2015-05-14 20:09:05 +02:00
parent 9d705318f2
commit d54cf445cf

View File

@@ -21,9 +21,9 @@
// Pixel format is RGB888
typedef struct {
char red;
char green;
char blue;
unsigned char red;
unsigned char green;
unsigned char blue;
} pixel_t;
typedef struct {
@@ -37,6 +37,16 @@ typedef struct {
void writeImageToPNGFile(image_t * image, char * filename);
#endif
#define CHARACTER_RANGE_START 0x20
#define CHARACTER_RANGE_END 0x7E
#define GRID_WIDTH 19
#define GRID_HEIGHT 5
#if (GRID_WIDTH*GRID_HEIGHT < (CHARACTER_RANGE_END-CHARACTER_RANGE_START+1))
#error Grid too small. Consider increasing GRID_WIDTH or GRID_HEIGHT
#endif
void drawGlyphInImage(FT_Bitmap * glyphBitmap, image_t * image, int x, int y);
@@ -67,13 +77,11 @@ int main(int argc, char * argv[]) {
int maxHeight = 0;
int maxWidth = 0;
int minTop = requested_glyph_height;
for (int i=0x28; i<0x7E;i++) {
unsigned char character = (unsigned char)i;
for (unsigned char character = CHARACTER_RANGE_START; character <= CHARACTER_RANGE_END; character++) {
ENSURE(!FT_Load_Char(face, character, FT_LOAD_RENDER), "Loading character 0x%02x", character);
int top = requested_glyph_height - face->glyph->bitmap_top;
int height = top + face->glyph->bitmap.rows;
int width = face->glyph->bitmap.width;
printf("Character %c(%x) has top %d\n", character, character, top);
if (top < minTop) {
minTop = top;
}
@@ -85,16 +93,16 @@ int main(int argc, char * argv[]) {
}
}
int glyph_height = maxHeight-minTop;
int glyph_width = maxWidth;
int glyph_height = maxHeight-minTop;
printf("Actual glyph size = %dx%d\n", glyph_width, glyph_height);
int grid_size = 10;
int grid_size = 1;
bitmap_image.width = 16*glyph_width+15*grid_size;
bitmap_image.height = 16*glyph_height+15*grid_size;
bitmap_image.width = GRID_WIDTH*glyph_width+(GRID_WIDTH-1)*grid_size;
bitmap_image.height = GRID_HEIGHT*glyph_height+(GRID_HEIGHT-1)*grid_size;
bitmap_image.pixels = malloc(sizeof(pixel_t)*bitmap_image.width*bitmap_image.height);
ENSURE(bitmap_image.pixels != NULL, "Allocating bitmap image of size %dx%d at %ld bytes per pixel", glyph_width*16, glyph_height*16, sizeof(pixel_t));
ENSURE(bitmap_image.pixels != NULL, "Allocating bitmap image of size %dx%d at %ld bytes per pixel", bitmap_image.width, bitmap_image.height, sizeof(pixel_t));
// Draw the grid and the background
for (int i = 0; i<bitmap_image.width;i++) {
@@ -110,10 +118,9 @@ int main(int argc, char * argv[]) {
// We're doing the ASCII table, so characters from 0 to 255 inclusive
for (int i=0x28; i<0x7E; i++) {
unsigned char character = (unsigned char)i;
int x = character%16;
int y = character/16;
for (unsigned char character = CHARACTER_RANGE_START; character <= CHARACTER_RANGE_END; character++) {
int x = (character-CHARACTER_RANGE_START)%(GRID_WIDTH);
int y = (character-CHARACTER_RANGE_START)/(GRID_WIDTH);
// FT_LOAD_RENDER: Render the glyph upon load
ENSURE(!FT_Load_Char(face, character, FT_LOAD_RENDER), "Loading character 0x%02x", character);
//printf("Advances = %dx%d\n", face->glyph->bitmap_left, face->glyph->bitmap_top);
@@ -127,6 +134,47 @@ int main(int argc, char * argv[]) {
writeImageToPNGFile(&bitmap_image, "out.png");
#endif
FILE * headerFile = fopen("out.h", "w");
fprintf(headerFile, "/* Auto-generated by rasterizer */\n\n");
fprintf(headerFile, "#define BITMAP_FONT_FIRST_CHARACTER 0x%2x\n", CHARACTER_RANGE_START);
fprintf(headerFile, "#define BITMAP_FONT_LAST_CHARACTER 0x%2x\n\n", CHARACTER_RANGE_END);
fprintf(headerFile, "#define BITMAP_FONT_CHARACTER_WIDTH %d\n", glyph_width);
fprintf(headerFile, "#define BITMAP_FONT_CHARACTER_HEIGHT %d\n\n", glyph_height);
fprintf(headerFile, "extern unsigned char bitmapFont[%d][%d][%d];\n", CHARACTER_RANGE_END-CHARACTER_RANGE_START+1, glyph_width, glyph_height);
fclose(headerFile);
FILE * sourceFile = fopen("out.c", "w");
fprintf(sourceFile, "/* Auto-generated by rasterizer */\n\n");
fprintf(sourceFile, "unsigned char bitmapFont[%d][%d][%d] = {\n", CHARACTER_RANGE_END-CHARACTER_RANGE_START+1, glyph_width, glyph_height);
for (unsigned char character = CHARACTER_RANGE_START; character <= CHARACTER_RANGE_END; character++) {
fprintf(sourceFile, " {\n");
int characterX = ((character-CHARACTER_RANGE_START)%GRID_WIDTH * (glyph_width+grid_size));
int characterY = ((character-CHARACTER_RANGE_START)/GRID_WIDTH * (glyph_height+grid_size));
for (int y = 0; y < glyph_height; y++) {
fprintf(sourceFile, " {");
for (int x = 0; x < glyph_width; x++) {
pixel_t * pixel = (bitmap_image.pixels + (y+characterY)*bitmap_image.width + (x+characterX));
fprintf(sourceFile, "0x%02x", pixel->green);
if (x+1 < glyph_width) {
fprintf(sourceFile, ", ");
}
}
fprintf(sourceFile, "}");
if (y+1 < glyph_height) {
fprintf(sourceFile, ",");
}
fprintf(sourceFile, "\n");
}
fprintf(sourceFile, " }");
if (character+1 <= CHARACTER_RANGE_END) {
fprintf(sourceFile, ",");
}
fprintf(sourceFile, "\n");
}
fprintf(sourceFile, "};\n");
fclose(sourceFile);
free(bitmap_image.pixels);
return 0;