Mercurial > hg > dmlib
comparison fontconv.c @ 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 | 63ff0fb944cd |
children | 8fba01374a29 |
comparison
equal
deleted
inserted
replaced
208:c6b4fa03744c | 209:17d4cc4c3ed1 |
---|---|
15 #include "dmresw.h" | 15 #include "dmresw.h" |
16 | 16 |
17 enum | 17 enum |
18 { | 18 { |
19 OFMT_DMFONT, | 19 OFMT_DMFONT, |
20 OFMT_C | |
20 }; | 21 }; |
21 | 22 |
22 char *optInFilename = NULL, *optOutFilename = NULL; | 23 char *optInFilename = NULL, *optOutFilename = NULL; |
23 int optOutFormat = OFMT_DMFONT, | 24 int optOutFormat = OFMT_DMFONT, |
24 optSplitWidth = 8, | 25 optSplitWidth = 8, |
29 { | 30 { |
30 { 0, '?', "help", "Show this help", OPT_NONE }, | 31 { 0, '?', "help", "Show this help", OPT_NONE }, |
31 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, | 32 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, |
32 { 2, 'o', "output", "Output file (default stdout)", OPT_ARGREQ }, | 33 { 2, 'o', "output", "Output file (default stdout)", OPT_ARGREQ }, |
33 { 3, 's', "size", "Set glyph dimensions (-s WxH) for image->font conversion", OPT_ARGREQ }, | 34 { 3, 's', "size", "Set glyph dimensions (-s WxH) for image->font conversion", OPT_ARGREQ }, |
35 { 4, 'C', "csource", "DMFONT as C source", OPT_NONE }, | |
34 }; | 36 }; |
35 | 37 |
36 const int optListN = sizeof(optList) / sizeof(optList[0]); | 38 const int optListN = sizeof(optList) / sizeof(optList[0]); |
37 | 39 |
38 | 40 |
76 optSplitWidth = w; | 78 optSplitWidth = w; |
77 optSplitHeight = h; | 79 optSplitHeight = h; |
78 } | 80 } |
79 break; | 81 break; |
80 | 82 |
83 case 4: | |
84 optOutFormat = OFMT_C; | |
85 break; | |
86 | |
81 default: | 87 default: |
82 dmError("Unknown argument '%s'.\n", currArg); | 88 dmError("Unknown argument '%s'.\n", currArg); |
83 return FALSE; | 89 return FALSE; |
84 } | 90 } |
85 | 91 |
96 dmError("Too many filename arguments, '%s'\n", currArg); | 102 dmError("Too many filename arguments, '%s'\n", currArg); |
97 return FALSE; | 103 return FALSE; |
98 } | 104 } |
99 | 105 |
100 return TRUE; | 106 return TRUE; |
107 } | |
108 | |
109 | |
110 static int dm_csrc_ferror(DMResource * f) | |
111 { | |
112 return f->error; | |
113 } | |
114 | |
115 | |
116 static int dm_csrc_fputc(int v, DMResource * f) | |
117 { | |
118 if (f->dataSize++ >= 16) | |
119 { | |
120 fprintf(f->fh, "\n"); | |
121 f->dataSize = 0; | |
122 } | |
123 fprintf(f->fh, "%3d,", v); | |
124 f->error = dmGetErrno(); | |
125 return 1; | |
126 } | |
127 | |
128 | |
129 static size_t dm_csrc_fwrite(void *ptr, size_t size, size_t nmemb, DMResource * f) | |
130 { | |
131 size_t n; | |
132 Uint8 *p = (Uint8 *) ptr; | |
133 for (n = 0; n < size * nmemb; n++, p++) | |
134 { | |
135 if (f->dataSize++ >= 16) | |
136 { | |
137 fprintf(f->fh, "\n"); | |
138 f->dataSize = 0; | |
139 } | |
140 fprintf(f->fh, "%3d,", *p); | |
141 } | |
142 f->error = dmGetErrno(); | |
143 return nmemb; | |
144 } | |
145 | |
146 | |
147 static int dm_csrc_fopen(DMResource * f) | |
148 { | |
149 fprintf(f->fh, | |
150 "const Uint8 %s = {\n", (char *) f->data | |
151 ); | |
152 return DMERR_OK; | |
153 } | |
154 | |
155 | |
156 static void dm_csrc_fclose(DMResource * f) | |
157 { | |
158 if (f->fh != NULL) | |
159 { | |
160 fprintf(f->fh, | |
161 "\n};\n" | |
162 ); | |
163 fclose(f->fh); | |
164 f->fh = NULL; | |
165 } | |
166 } | |
167 | |
168 | |
169 DMResourceOps dfCSourceFileOps = | |
170 { | |
171 dm_csrc_ferror, | |
172 NULL, | |
173 NULL, | |
174 NULL, | |
175 NULL, | |
176 NULL, | |
177 dm_csrc_fputc, | |
178 NULL, | |
179 dm_csrc_fwrite, | |
180 | |
181 dm_csrc_fopen, | |
182 dm_csrc_fclose, | |
183 NULL | |
184 }; | |
185 | |
186 | |
187 DMResource * dmf_create_csrc(const char *filename, const char *name) | |
188 { | |
189 DMResource *handle = dmres_new(filename, 0, 0); | |
190 if (handle == NULL) | |
191 return NULL; | |
192 | |
193 handle->fops = &dfCSourceFileOps; | |
194 | |
195 handle->fh = fopen(filename, "w"); | |
196 handle->error = dmGetErrno(); | |
197 handle->data = (Uint8 *) dm_strdup(name); | |
198 | |
199 if (handle->fh != NULL) | |
200 { | |
201 handle->fops->fopen(handle); | |
202 dmres_ref(handle); | |
203 return handle; | |
204 } | |
205 else | |
206 { | |
207 dmres_free(handle); | |
208 return NULL; | |
209 } | |
210 } | |
211 | |
212 | |
213 DMResource * dmf_create_csrc_stream(FILE *fh, const char *name) | |
214 { | |
215 DMResource *handle = dmres_new(NULL, 0, 0); | |
216 if (handle == NULL) | |
217 return NULL; | |
218 | |
219 handle->fops = &dfCSourceFileOps; | |
220 | |
221 handle->fh = fh; | |
222 handle->error = dmGetErrno(); | |
223 handle->data = (Uint8 *) dm_strdup(name); | |
224 handle->fops->fopen(handle); | |
225 dmres_ref(handle); | |
226 return handle; | |
101 } | 227 } |
102 | 228 |
103 | 229 |
104 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont) | 230 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont) |
105 { | 231 { |
238 } | 364 } |
239 | 365 |
240 | 366 |
241 int main(int argc, char *argv[]) | 367 int main(int argc, char *argv[]) |
242 { | 368 { |
243 DMResource *inFile = NULL; | 369 DMResource *inFile = NULL, *outFile = NULL; |
244 DMBitmapFont *font = NULL; | 370 DMBitmapFont *font = NULL; |
245 SDL_Surface *fontbmap = NULL; | 371 SDL_Surface *fontbmap = NULL; |
246 int res; | 372 int res; |
247 #ifdef DM_GFX_TTF_TEXT | 373 #ifdef DM_GFX_TTF_TEXT |
248 BOOL initTTF = FALSE; | 374 BOOL initTTF = FALSE; |
289 #ifdef DM_GFX_TTF_TEXT | 415 #ifdef DM_GFX_TTF_TEXT |
290 else | 416 else |
291 if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth)) != NULL) | 417 if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth)) != NULL) |
292 { | 418 { |
293 int i; | 419 int i; |
294 SDL_Color col = { 255, 255, 255, 100 }; //255, 255, 255, 100 }; | 420 SDL_Color col = { 255, 255, 255, 100 }; |
295 dmMsg(1, "Input is a TTF TrueType font, rendering at %d x %d.\n", | 421 dmMsg(1, "Input is a TTF TrueType font, rendering at %d x %d.\n", |
296 optSplitWidth, optSplitHeight); | 422 optSplitWidth, optSplitHeight); |
297 | 423 |
298 TTF_SetFontStyle(ttf, TTF_STYLE_NORMAL); | 424 TTF_SetFontStyle(ttf, TTF_STYLE_NORMAL); |
299 | 425 |
339 goto error_exit; | 465 goto error_exit; |
340 } | 466 } |
341 | 467 |
342 if (optOutFormat == OFMT_DMFONT) | 468 if (optOutFormat == OFMT_DMFONT) |
343 { | 469 { |
344 DMResource *file; | |
345 | |
346 dmMsg(1, "Outputting a DMFONT format bitmap font.\n"); | 470 dmMsg(1, "Outputting a DMFONT format bitmap font.\n"); |
347 | |
348 if (optOutFilename == NULL) | 471 if (optOutFilename == NULL) |
349 file = dmf_create_stdio_stream(stdout); | 472 outFile = dmf_create_stdio_stream(stdout); |
350 else | 473 else |
351 file = dmf_create_stdio(optOutFilename, "wb"); | 474 outFile = dmf_create_stdio(optOutFilename, "wb"); |
352 | 475 } |
353 if (file == NULL) | 476 else |
354 { | 477 if (optOutFormat == OFMT_C) |
355 dmError("Error creating file '%s', %d: %s\n", | 478 { |
356 optInFilename, errno, strerror(errno)); | 479 dmMsg(1, "Outputting a DMFONT format bitmap font as C source file.\n"); |
357 goto error_exit; | 480 if (optOutFilename == NULL) |
358 } | 481 outFile = dmf_create_csrc_stream(stdout, "fantti"); |
359 | 482 else |
360 res = dmSaveBitmapFont(file, font); | 483 outFile = dmf_create_csrc(optOutFilename, "fantti"); |
361 dmf_close(file); | 484 } |
362 } | 485 |
486 if (outFile == NULL) | |
487 { | |
488 dmError("Error creating file '%s', %d: %s\n", | |
489 optInFilename, errno, strerror(errno)); | |
490 goto error_exit; | |
491 } | |
492 | |
493 res = dmSaveBitmapFont(outFile, font); | |
494 dmf_close(outFile); | |
363 | 495 |
364 if (res != DMERR_OK) | 496 if (res != DMERR_OK) |
365 { | 497 { |
366 dmError("Error saving font, %d: %s\n", | 498 dmError("Error saving font, %d: %s\n", |
367 res, dmErrorStr(res)); | 499 res, dmErrorStr(res)); |