annotate fontconv.c @ 212:8fba01374a29

Improve C source output of fontconv.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 07 Oct 2012 15:33:55 +0300
parents 17d4cc4c3ed1
children 5b1554eb9928
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * fontconv - Convert bitmap fonts
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * (C) Copyright 2012 Tecnic Software productions (TNSP)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include <stdio.h>
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include <errno.h>
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "dmlib.h"
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include "dmargs.h"
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include "dmfile.h"
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #include "dmimage.h"
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 #include "dmtext.h"
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
15 #include "dmresw.h"
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 enum
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 OFMT_DMFONT,
209
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
20 OFMT_C
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 };
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 char *optInFilename = NULL, *optOutFilename = NULL;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 int optOutFormat = OFMT_DMFONT,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 optSplitWidth = 8,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 optSplitHeight = 8;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 DMOptArg optList[] =
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 { 0, '?', "help", "Show this help", OPT_NONE },
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 { 2, 'o', "output", "Output file (default stdout)", OPT_ARGREQ },
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 { 3, 's', "size", "Set glyph dimensions (-s WxH) for image->font conversion", OPT_ARGREQ },
209
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
35 { 4, 'C', "csource", "DMFONT as C source", OPT_NONE },
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 };
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 const int optListN = sizeof(optList) / sizeof(optList[0]);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 switch (optN)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 case 0:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 dmPrintBanner(stdout, dmProgName,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 "[options] [sourcefile]");
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 dmArgsPrintHelp(stdout, optList, optListN);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 exit(0);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 break;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 case 1:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 dmVerbosity++;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 break;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 case 2:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 optOutFilename = optArg;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 break;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 case 3:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 int w, h;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 if (sscanf(optArg, "%dx%d", &w, &h) != 2)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 dmError("Invalid argument for -s option, '%s'.\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 optArg);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 return FALSE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 if (w < DMFONT_MIN_WIDTH || w > DMFONT_MAX_WIDTH ||
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 h < DMFONT_MIN_HEIGHT || h > DMFONT_MAX_HEIGHT)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 dmError("Invalid dimensions, must be %d < W %d, %d < H < %d.\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 DMFONT_MIN_WIDTH , DMFONT_MAX_WIDTH,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 DMFONT_MIN_HEIGHT , DMFONT_MAX_HEIGHT);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 return FALSE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 optSplitWidth = w;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 optSplitHeight = h;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 break;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82
209
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
83 case 4:
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
84 optOutFormat = OFMT_C;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
85 break;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
86
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 default:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 dmError("Unknown argument '%s'.\n", currArg);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 return FALSE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 return TRUE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 BOOL argHandleFile(char *currArg)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 if (!optInFilename)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 optInFilename = currArg;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 else
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 dmError("Too many filename arguments, '%s'\n", currArg);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 return FALSE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 return TRUE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109
209
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
110 static int dm_csrc_ferror(DMResource * f)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
111 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
112 return f->error;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
113 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
114
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
115
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
116 static int dm_csrc_fputc(int v, DMResource * f)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
117 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
118 if (f->dataSize++ >= 16)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
119 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
120 fprintf(f->fh, "\n");
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
121 f->dataSize = 0;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
122 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
123 fprintf(f->fh, "%3d,", v);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
124 f->error = dmGetErrno();
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
125 return 1;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
126 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
127
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
128
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
129 static size_t dm_csrc_fwrite(void *ptr, size_t size, size_t nmemb, DMResource * f)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
130 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
131 size_t n;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
132 Uint8 *p = (Uint8 *) ptr;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
133 for (n = 0; n < size * nmemb; n++, p++)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
134 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
135 if (f->dataSize++ >= 16)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
136 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
137 fprintf(f->fh, "\n");
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
138 f->dataSize = 0;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
139 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
140 fprintf(f->fh, "%3d,", *p);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
141 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
142 f->error = dmGetErrno();
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
143 return nmemb;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
144 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
145
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
146
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
147 static int dm_csrc_fopen(DMResource * f)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
148 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
149 fprintf(f->fh,
212
8fba01374a29 Improve C source output of fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 209
diff changeset
150 "const Uint8 %s[] = {\n", (char *) f->data
209
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
151 );
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
152 return DMERR_OK;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
153 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
154
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
155
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
156 static void dm_csrc_fclose(DMResource * f)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
157 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
158 if (f->fh != NULL)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
159 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
160 fprintf(f->fh,
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
161 "\n};\n"
212
8fba01374a29 Improve C source output of fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 209
diff changeset
162 "const int %s_size = sizeof(%s) / sizeof(%s[0]);\n",
8fba01374a29 Improve C source output of fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 209
diff changeset
163 (char *)f->data, (char *)f->data, (char *)f->data
209
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
164 );
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
165 fclose(f->fh);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
166 f->fh = NULL;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
167 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
168 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
169
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
170
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
171 DMResourceOps dfCSourceFileOps =
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
172 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
173 dm_csrc_ferror,
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
174 NULL,
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
175 NULL,
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
176 NULL,
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
177 NULL,
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
178 NULL,
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
179 dm_csrc_fputc,
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
180 NULL,
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
181 dm_csrc_fwrite,
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
182
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
183 dm_csrc_fopen,
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
184 dm_csrc_fclose,
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
185 NULL
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
186 };
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
187
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
188
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
189 DMResource * dmf_create_csrc(const char *filename, const char *name)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
190 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
191 DMResource *handle = dmres_new(filename, 0, 0);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
192 if (handle == NULL)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
193 return NULL;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
194
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
195 handle->fops = &dfCSourceFileOps;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
196
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
197 handle->fh = fopen(filename, "w");
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
198 handle->error = dmGetErrno();
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
199 handle->data = (Uint8 *) dm_strdup(name);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
200
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
201 if (handle->fh != NULL)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
202 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
203 handle->fops->fopen(handle);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
204 dmres_ref(handle);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
205 return handle;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
206 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
207 else
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
208 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
209 dmres_free(handle);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
210 return NULL;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
211 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
212 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
213
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
214
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
215 DMResource * dmf_create_csrc_stream(FILE *fh, const char *name)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
216 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
217 DMResource *handle = dmres_new(NULL, 0, 0);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
218 if (handle == NULL)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
219 return NULL;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
220
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
221 handle->fops = &dfCSourceFileOps;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
222
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
223 handle->fh = fh;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
224 handle->error = dmGetErrno();
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
225 handle->data = (Uint8 *) dm_strdup(name);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
226 handle->fops->fopen(handle);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
227 dmres_ref(handle);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
228 return handle;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
229 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
230
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
231
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234 int nglyph, xc, yc, xglyphs, yglyphs;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 DMBitmapFont *font;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 if (image->w < width || width < 4 || image->h < height || height < 4)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 return DMERR_INVALID_ARGS;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 xglyphs = image->w / width;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 yglyphs = image->h / height;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 if ((font = dmNewBitmapFont(xglyphs * yglyphs, width, height)) == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 return DMERR_MALLOC;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
246 dmMsg(1, "%d x %d split as %d x %d blocks => %d x %d = %d glyphs.\n",
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 image->w, image->h,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 width, height,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 xglyphs, yglyphs, xglyphs * yglyphs);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 nglyph = 0;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 for (yc = 0; yc < yglyphs; yc++)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 for (xc = 0; xc < xglyphs; xc++)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 SDL_Surface *glyph = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 image->format->BitsPerPixel,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 image->format->Rmask,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 image->format->Gmask,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 image->format->Bmask,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 image->format->Amask);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 if (glyph == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 dmFreeBitmapFont(font);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 return DMERR_MALLOC;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 SDL_Rect r;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 r.x = xc * width;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270 r.y = yc * height;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 r.w = width;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 r.h = height;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 SDL_BlitSurface(image, &r, glyph, NULL);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 font->glyphs[nglyph++] = glyph;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 *pfont = font;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280 return DMERR_OK;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
284 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
285 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
286 int maxglyph, nglyphs, n;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
287 if (font == NULL)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
288 return DMERR_NULLPTR;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
289
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
290 if (font->nglyphs > DMFONT_MAX_GLYPHS ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
291 font->width > DMFONT_MAX_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
292 font->height > DMFONT_MAX_HEIGHT ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
293 font->width < DMFONT_MIN_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
294 font->height < DMFONT_MIN_HEIGHT)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
295 return DMERR_INVALID_DATA;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
296
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
297 // Count number of actually existing glyphs
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
298 for (maxglyph = nglyphs = n = 0; n < font->nglyphs; n++)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
299 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
300 SDL_Surface *glyph = font->glyphs[n];
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
301 if (glyph != NULL)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
302 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
303 maxglyph = n;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
304 if (glyph->w < DMFONT_MIN_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
305 glyph->h < DMFONT_MIN_HEIGHT ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
306 glyph->w > DMFONT_MAX_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
307 glyph->h > DMFONT_MAX_HEIGHT)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
308 continue;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
309 nglyphs++;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
310 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
311 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
312
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
313 // Write the DMFONT header
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
314 if (!dmf_write_str(res, (Uint8 *) DMFONT_MAGIC, 6))
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
315 return DMERR_FWRITE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
316
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
317 dmf_write_le16(res, DMFONT_VERSION);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
318 dmf_write_le16(res, nglyphs);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
319 dmf_write_le16(res, maxglyph + 1);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
320 dmfputc(font->width, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
321 dmfputc(font->height, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
322
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
323 if (nglyphs > 0)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
324 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
325 int i;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
326 SDL_Surface *glyph = font->glyphs[maxglyph];
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
327
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
328 // If there are actual glyphs stored, save this
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
329 dmfputc(glyph->format->BitsPerPixel, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
330 dmf_write_le32(res, glyph->format->Rmask);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
331 dmf_write_le32(res, glyph->format->Gmask);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
332 dmf_write_le32(res, glyph->format->Bmask);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
333 dmf_write_le32(res, glyph->format->Amask);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
334
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
335 for (i = 0; i < font->nglyphs; i++)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
336 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
337 glyph = font->glyphs[i];
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
338 if (glyph != NULL)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
339 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
340 int y;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
341 Uint8 *pixels = glyph->pixels;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
342
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
343 if (glyph->w < DMFONT_MIN_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
344 glyph->h < DMFONT_MIN_HEIGHT ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
345 glyph->w > DMFONT_MAX_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
346 glyph->h > DMFONT_MAX_HEIGHT)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
347 continue;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
348
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
349 // Each glyph has its table index and w/h stored
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
350 dmf_write_le16(res, i);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
351 dmfputc(glyph->w, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
352 dmfputc(glyph->h, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
353
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
354 // Write the pixel data
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
355 for (y = 0; y < glyph->h; y++)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
356 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
357 if (dmfwrite(pixels, glyph->format->BytesPerPixel, glyph->w, res) != (size_t) glyph->w)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
358 return DMERR_FWRITE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
359 pixels += glyph->pitch;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
360 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
361 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
362 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
363 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
364
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
365 return DMERR_OK;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
366 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
367
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
368
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 int main(int argc, char *argv[])
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 {
209
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
371 DMResource *inFile = NULL, *outFile = NULL;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 DMBitmapFont *font = NULL;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
373 SDL_Surface *fontbmap = NULL;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 int res;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
375 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
376 BOOL initTTF = FALSE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
377 TTF_Font *ttf = NULL;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
378 #endif
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 dmInitProg("fontconv", "Bitmap font converter", "0.2", NULL, NULL);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 dmVerbosity = 1;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 // Parse arguments
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 if (!dmArgsProcess(argc, argv, optList, optListN,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 argHandleOpt, argHandleFile, TRUE))
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 exit(1);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388 // Check arguments
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 if (!optInFilename)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 dmError("Input or output file not specified!\n");
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392 return 1;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 }
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
394
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
395 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
396 if (TTF_Init() < 0)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
397 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
398 dmError("Could not initialize FreeType/TTF: %s\n", SDL_GetError());
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
399 goto error_exit;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
400 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
401 initTTF = TRUE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
402 #endif
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 // Open the source file
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 if ((inFile = dmf_create_stdio(optInFilename, "rb")) == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 dmError("Error opening input file '%s', %d: %s\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 optInFilename, errno, strerror(errno));
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 return 1;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
412
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
413 if ((res = dmLoadBitmapFont(inFile, &font)) == DMERR_OK)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
414 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
415 dmMsg(1, "Input is a TSFONT/DMFONT font file.\n");
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
416 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
417 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
418 else
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
419 if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth)) != NULL)
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 {
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
421 int i;
209
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
422 SDL_Color col = { 255, 255, 255, 100 };
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
423 dmMsg(1, "Input is a TTF TrueType font, rendering at %d x %d.\n",
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
424 optSplitWidth, optSplitHeight);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
425
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
426 TTF_SetFontStyle(ttf, TTF_STYLE_NORMAL);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
427
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
428 if ((font = dmNewBitmapFont(256, optSplitWidth - 1, optSplitHeight+4)) == NULL)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
429 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
430 goto error_exit;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
431 }
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
433 for (i = 0; i < 255; i++)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
434 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
435 char str[2];
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
436 str[0] = i;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
437 str[1] = 0;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
438 font->glyphs[i] = TTF_RenderText_Blended(ttf, str, col);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
439 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
440 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
441 #endif
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
442 else
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
443 {
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 dmfseek(inFile, 0L, SEEK_SET);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 if ((fontbmap = dmLoadImage(inFile)) == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448 dmError("Could not load image file '%s'.\n", optInFilename);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 goto error_exit;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
452 dmMsg(1, "Input is a bitmap image (%d x %d, %d bpp), splitting to %d x %d.\n",
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
453 fontbmap->w, fontbmap->h, fontbmap->format->BitsPerPixel,
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
454 optSplitWidth, optSplitHeight);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
455
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 if ((res = dmCreateBitmapFontFromImage(fontbmap, optSplitWidth, optSplitHeight, &font)) != DMERR_OK)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 dmError("Could not create a font from image, %d: %s\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459 res, dmErrorStr(res));
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 goto error_exit;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
462 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464 if (font == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466 dmError("No font loaded.\n");
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
467 goto error_exit;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470 if (optOutFormat == OFMT_DMFONT)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
471 {
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
472 dmMsg(1, "Outputting a DMFONT format bitmap font.\n");
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473 if (optOutFilename == NULL)
209
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
474 outFile = dmf_create_stdio_stream(stdout);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
475 else
209
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
476 outFile = dmf_create_stdio(optOutFilename, "wb");
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
477 }
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
478 else
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
479 if (optOutFormat == OFMT_C)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
480 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
481 dmMsg(1, "Outputting a DMFONT format bitmap font as C source file.\n");
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
482 if (optOutFilename == NULL)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
483 outFile = dmf_create_csrc_stream(stdout, "fantti");
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
484 else
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
485 outFile = dmf_create_csrc(optOutFilename, "fantti");
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
486 }
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
487
209
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
488 if (outFile == NULL)
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
489 {
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
490 dmError("Error creating file '%s', %d: %s\n",
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
491 optInFilename, errno, strerror(errno));
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
492 goto error_exit;
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
493 }
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494
209
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
495 res = dmSaveBitmapFont(outFile, font);
17d4cc4c3ed1 Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
496 dmf_close(outFile);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 if (res != DMERR_OK)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500 dmError("Error saving font, %d: %s\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501 res, dmErrorStr(res));
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 error_exit:
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
505
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
506 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
507 if (initTTF)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
508 TTF_Quit();
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
509 #endif
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511 dmf_close(inFile);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512 dmFreeBitmapFont(font);
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
513 SDL_FreeSurface(fontbmap);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
515 return 0;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 }