Mercurial > hg > dmlib
changeset 209:17d4cc4c3ed1
Add ability to dump a DMFONT as a binary data in a C header file.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 07 Oct 2012 15:09:44 +0300 |
parents | c6b4fa03744c |
children | 1aac7a53b2ae |
files | fontconv.c |
diffstat | 1 files changed, 148 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/fontconv.c Sun Oct 07 14:54:51 2012 +0300 +++ b/fontconv.c Sun Oct 07 15:09:44 2012 +0300 @@ -17,6 +17,7 @@ enum { OFMT_DMFONT, + OFMT_C }; char *optInFilename = NULL, *optOutFilename = NULL; @@ -31,6 +32,7 @@ { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, { 2, 'o', "output", "Output file (default stdout)", OPT_ARGREQ }, { 3, 's', "size", "Set glyph dimensions (-s WxH) for image->font conversion", OPT_ARGREQ }, + { 4, 'C', "csource", "DMFONT as C source", OPT_NONE }, }; const int optListN = sizeof(optList) / sizeof(optList[0]); @@ -78,6 +80,10 @@ } break; + case 4: + optOutFormat = OFMT_C; + break; + default: dmError("Unknown argument '%s'.\n", currArg); return FALSE; @@ -101,6 +107,126 @@ } +static int dm_csrc_ferror(DMResource * f) +{ + return f->error; +} + + +static int dm_csrc_fputc(int v, DMResource * f) +{ + if (f->dataSize++ >= 16) + { + fprintf(f->fh, "\n"); + f->dataSize = 0; + } + fprintf(f->fh, "%3d,", v); + f->error = dmGetErrno(); + return 1; +} + + +static size_t dm_csrc_fwrite(void *ptr, size_t size, size_t nmemb, DMResource * f) +{ + size_t n; + Uint8 *p = (Uint8 *) ptr; + for (n = 0; n < size * nmemb; n++, p++) + { + if (f->dataSize++ >= 16) + { + fprintf(f->fh, "\n"); + f->dataSize = 0; + } + fprintf(f->fh, "%3d,", *p); + } + f->error = dmGetErrno(); + return nmemb; +} + + +static int dm_csrc_fopen(DMResource * f) +{ + fprintf(f->fh, + "const Uint8 %s = {\n", (char *) f->data + ); + return DMERR_OK; +} + + +static void dm_csrc_fclose(DMResource * f) +{ + if (f->fh != NULL) + { + fprintf(f->fh, + "\n};\n" + ); + fclose(f->fh); + f->fh = NULL; + } +} + + +DMResourceOps dfCSourceFileOps = +{ + dm_csrc_ferror, + NULL, + NULL, + NULL, + NULL, + NULL, + dm_csrc_fputc, + NULL, + dm_csrc_fwrite, + + dm_csrc_fopen, + dm_csrc_fclose, + NULL +}; + + +DMResource * dmf_create_csrc(const char *filename, const char *name) +{ + DMResource *handle = dmres_new(filename, 0, 0); + if (handle == NULL) + return NULL; + + handle->fops = &dfCSourceFileOps; + + handle->fh = fopen(filename, "w"); + handle->error = dmGetErrno(); + handle->data = (Uint8 *) dm_strdup(name); + + if (handle->fh != NULL) + { + handle->fops->fopen(handle); + dmres_ref(handle); + return handle; + } + else + { + dmres_free(handle); + return NULL; + } +} + + +DMResource * dmf_create_csrc_stream(FILE *fh, const char *name) +{ + DMResource *handle = dmres_new(NULL, 0, 0); + if (handle == NULL) + return NULL; + + handle->fops = &dfCSourceFileOps; + + handle->fh = fh; + handle->error = dmGetErrno(); + handle->data = (Uint8 *) dm_strdup(name); + handle->fops->fopen(handle); + dmres_ref(handle); + return handle; +} + + int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont) { int nglyph, xc, yc, xglyphs, yglyphs; @@ -240,7 +366,7 @@ int main(int argc, char *argv[]) { - DMResource *inFile = NULL; + DMResource *inFile = NULL, *outFile = NULL; DMBitmapFont *font = NULL; SDL_Surface *fontbmap = NULL; int res; @@ -291,7 +417,7 @@ if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth)) != NULL) { int i; - SDL_Color col = { 255, 255, 255, 100 }; //255, 255, 255, 100 }; + SDL_Color col = { 255, 255, 255, 100 }; dmMsg(1, "Input is a TTF TrueType font, rendering at %d x %d.\n", optSplitWidth, optSplitHeight); @@ -341,25 +467,31 @@ if (optOutFormat == OFMT_DMFONT) { - DMResource *file; - dmMsg(1, "Outputting a DMFONT format bitmap font.\n"); - if (optOutFilename == NULL) - file = dmf_create_stdio_stream(stdout); + outFile = dmf_create_stdio_stream(stdout); else - file = dmf_create_stdio(optOutFilename, "wb"); + outFile = dmf_create_stdio(optOutFilename, "wb"); + } + else + if (optOutFormat == OFMT_C) + { + dmMsg(1, "Outputting a DMFONT format bitmap font as C source file.\n"); + if (optOutFilename == NULL) + outFile = dmf_create_csrc_stream(stdout, "fantti"); + else + outFile = dmf_create_csrc(optOutFilename, "fantti"); + } - if (file == NULL) - { - dmError("Error creating file '%s', %d: %s\n", - optInFilename, errno, strerror(errno)); - goto error_exit; - } + if (outFile == NULL) + { + dmError("Error creating file '%s', %d: %s\n", + optInFilename, errno, strerror(errno)); + goto error_exit; + } - res = dmSaveBitmapFont(file, font); - dmf_close(file); - } + res = dmSaveBitmapFont(outFile, font); + dmf_close(outFile); if (res != DMERR_OK) {