Mercurial > hg > dmlib
annotate tools/fontconv.c @ 1272:acae5f8ebc67
Fix build process.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 19 May 2016 12:08:16 +0300 |
parents | 5b8245e5f785 |
children | 370c40e0847f |
rev | line source |
---|---|
160 | 1 /* |
2 * fontconv - Convert bitmap fonts | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
874 | 4 * (C) Copyright 2012-2015 Tecnic Software productions (TNSP) |
160 | 5 * |
6 * Please read file 'COPYING' for information on license and distribution. | |
7 */ | |
8 #include <stdio.h> | |
9 #include "dmlib.h" | |
10 #include "dmargs.h" | |
11 #include "dmfile.h" | |
12 #include "dmimage.h" | |
13 #include "dmtext.h" | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
14 #include "dmresw.h" |
160 | 15 |
579
f87446a81887
Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents:
362
diff
changeset
|
16 char *optInFilename = NULL, *optOutFilename = NULL; |
160 | 17 |
579
f87446a81887
Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents:
362
diff
changeset
|
18 int optSplitWidth = 8, |
160 | 19 optSplitHeight = 8; |
20 | |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
21 SDL_Color optColor = { 255, 255, 255, 100 }; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
22 |
160 | 23 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
24 static const DMOptArg optList[] = |
160 | 25 { |
26 { 0, '?', "help", "Show this help", OPT_NONE }, | |
27 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, | |
649 | 28 { 2, 'o', "output", "Set output filename", OPT_ARGREQ }, |
885
ee46d039c45d
Change fontconv -s option syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
874
diff
changeset
|
29 { 3, 's', "size", "Set glyph dimensions (-s W:H) for image->font conversion", OPT_ARGREQ }, |
1272 | 30 #ifdef DM_GFX_TTF_TEXT |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
31 { 4, 'c', "color", "TTF font rendering color (def: 0xFFFFFF)", OPT_ARGREQ }, |
1272 | 32 #endif |
160 | 33 }; |
34 | |
35 const int optListN = sizeof(optList) / sizeof(optList[0]); | |
36 | |
37 | |
38 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | |
39 { | |
40 switch (optN) | |
41 { | |
42 case 0: | |
43 dmPrintBanner(stdout, dmProgName, | |
645
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
44 "[options] <sourcefile.(ttf|fnt|dmf|png)> <outputfile.dmf>"); |
646
cc4e6ab6120b
Add some help text to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
645
diff
changeset
|
45 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
46 dmArgsPrintHelp(stdout, optList, optListN, 0); |
646
cc4e6ab6120b
Add some help text to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
645
diff
changeset
|
47 printf( |
647 | 48 "\n" |
646
cc4e6ab6120b
Add some help text to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
645
diff
changeset
|
49 "This utility can be used to convert TSFONT files to bitmap DMFONT (DMF)\n" |
cc4e6ab6120b
Add some help text to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
645
diff
changeset
|
50 "files, render TrueType TTF to DMFONT at desired glyph resolution, or\n" |
cc4e6ab6120b
Add some help text to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
645
diff
changeset
|
51 "cut a PNG (or JPEG) image to glyphs of desired size.\n"); |
cc4e6ab6120b
Add some help text to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
645
diff
changeset
|
52 |
160 | 53 exit(0); |
54 break; | |
55 | |
56 case 1: | |
57 dmVerbosity++; | |
58 break; | |
213
5b1554eb9928
Add option for specifying the output variable name for C source output mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
212
diff
changeset
|
59 |
648
7303c43bf347
Fix fontconv option handling again.
Matti Hamalainen <ccr@tnsp.org>
parents:
647
diff
changeset
|
60 case 2: |
7303c43bf347
Fix fontconv option handling again.
Matti Hamalainen <ccr@tnsp.org>
parents:
647
diff
changeset
|
61 optOutFilename = optArg; |
7303c43bf347
Fix fontconv option handling again.
Matti Hamalainen <ccr@tnsp.org>
parents:
647
diff
changeset
|
62 break; |
7303c43bf347
Fix fontconv option handling again.
Matti Hamalainen <ccr@tnsp.org>
parents:
647
diff
changeset
|
63 |
160 | 64 case 3: |
65 { | |
66 int w, h; | |
885
ee46d039c45d
Change fontconv -s option syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
874
diff
changeset
|
67 if (sscanf(optArg, "%d:%d", &w, &h) != 2) |
160 | 68 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
69 dmErrorMsg("Invalid argument for -s option, '%s'.\n", |
160 | 70 optArg); |
71 return FALSE; | |
72 } | |
73 if (w < DMFONT_MIN_WIDTH || w > DMFONT_MAX_WIDTH || | |
74 h < DMFONT_MIN_HEIGHT || h > DMFONT_MAX_HEIGHT) | |
75 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
76 dmErrorMsg("Invalid dimensions, must be %d < W %d, %d < H < %d.\n", |
160 | 77 DMFONT_MIN_WIDTH , DMFONT_MAX_WIDTH, |
78 DMFONT_MIN_HEIGHT , DMFONT_MAX_HEIGHT); | |
79 return FALSE; | |
80 } | |
81 optSplitWidth = w; | |
82 optSplitHeight = h; | |
83 } | |
84 break; | |
85 | |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
86 case 4: |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
87 { |
867 | 88 unsigned int colR, colG, colB, colA = 100; |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
89 if (optArg[0] == '#' || optArg[0] == '$') optArg++; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
90 else |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
91 if (optArg[0] == '0' && optArg[1] == 'x') optArg += 2; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
92 |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
93 if (sscanf(optArg, "%02x%02x%02x", &colR, &colG, &colB) != 3 && |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
94 sscanf(optArg, "%02x%02x%02x%02x", &colR, &colG, &colB, &colA) != 4) |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
95 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
96 dmErrorMsg("Invalid RGB hex representation '%s'.\n", |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
97 optArg); |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
98 return FALSE; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
99 } |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
100 |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
101 optColor.r = colR; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
102 optColor.g = colG; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
103 optColor.b = colB; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
104 optColor.unused = colA; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
105 } |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
106 break; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
107 |
160 | 108 default: |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
109 dmErrorMsg("Unknown argument '%s'.\n", currArg); |
160 | 110 return FALSE; |
111 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
112 |
160 | 113 return TRUE; |
114 } | |
115 | |
116 | |
117 BOOL argHandleFile(char *currArg) | |
118 { | |
119 if (!optInFilename) | |
120 optInFilename = currArg; | |
121 else | |
648
7303c43bf347
Fix fontconv option handling again.
Matti Hamalainen <ccr@tnsp.org>
parents:
647
diff
changeset
|
122 if (!optOutFilename) |
645
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
123 optOutFilename = currArg; |
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
124 else |
160 | 125 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
126 dmErrorMsg("Too many filename arguments, '%s'\n", currArg); |
160 | 127 return FALSE; |
128 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
129 |
160 | 130 return TRUE; |
131 } | |
132 | |
133 | |
134 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont) | |
135 { | |
136 int nglyph, xc, yc, xglyphs, yglyphs; | |
137 DMBitmapFont *font; | |
138 | |
874 | 139 if (image->w < width || width < 2 || image->h < height || height < 2) |
160 | 140 return DMERR_INVALID_ARGS; |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
141 |
160 | 142 xglyphs = image->w / width; |
143 yglyphs = image->h / height; | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
144 |
160 | 145 if ((font = dmNewBitmapFont(xglyphs * yglyphs, width, height)) == NULL) |
146 return DMERR_MALLOC; | |
147 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
148 dmMsg(1, "%d x %d split as %d x %d blocks => %d x %d = %d glyphs.\n", |
160 | 149 image->w, image->h, |
150 width, height, | |
151 xglyphs, yglyphs, xglyphs * yglyphs); | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
152 |
160 | 153 nglyph = 0; |
154 for (yc = 0; yc < yglyphs; yc++) | |
155 for (xc = 0; xc < xglyphs; xc++) | |
156 { | |
157 SDL_Surface *glyph = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, | |
158 image->format->BitsPerPixel, | |
159 image->format->Rmask, | |
160 image->format->Gmask, | |
161 image->format->Bmask, | |
162 image->format->Amask); | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
163 |
160 | 164 if (glyph == NULL) |
165 { | |
166 dmFreeBitmapFont(font); | |
167 return DMERR_MALLOC; | |
168 } | |
169 | |
170 SDL_Rect r; | |
171 r.x = xc * width; | |
172 r.y = yc * height; | |
173 r.w = width; | |
174 r.h = height; | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
175 |
160 | 176 SDL_BlitSurface(image, &r, glyph, NULL); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
177 |
160 | 178 font->glyphs[nglyph++] = glyph; |
179 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
180 |
160 | 181 *pfont = font; |
182 return DMERR_OK; | |
183 } | |
184 | |
185 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
186 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
187 { |
874 | 188 SDL_Surface *glyph; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
189 int maxglyph, nglyphs, n; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
190 if (font == NULL) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
191 return DMERR_NULLPTR; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
192 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
193 if (font->nglyphs > DMFONT_MAX_GLYPHS || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
194 font->width > DMFONT_MAX_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
195 font->height > DMFONT_MAX_HEIGHT || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
196 font->width < DMFONT_MIN_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
197 font->height < DMFONT_MIN_HEIGHT) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
198 return DMERR_INVALID_DATA; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
199 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
200 // Count number of actually existing glyphs |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
201 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
|
202 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
203 SDL_Surface *glyph = font->glyphs[n]; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
204 if (glyph != NULL) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
205 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
206 maxglyph = n; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
207 if (glyph->w < DMFONT_MIN_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
208 glyph->h < DMFONT_MIN_HEIGHT || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
209 glyph->w > DMFONT_MAX_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
210 glyph->h > DMFONT_MAX_HEIGHT) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
211 continue; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
212 nglyphs++; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
213 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
214 } |
874 | 215 |
216 if (nglyphs == 0) | |
217 return DMERR_INVALID_DATA; | |
218 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
219 // Write the DMFONT header |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
220 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
|
221 return DMERR_FWRITE; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
222 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
223 dmf_write_le16(res, DMFONT_VERSION); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
224 dmf_write_le16(res, nglyphs); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
225 dmf_write_le16(res, maxglyph + 1); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
226 dmfputc(font->width, res); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
227 dmfputc(font->height, res); |
874 | 228 |
229 // Store glyph format data | |
230 glyph = font->glyphs[maxglyph]; | |
231 dmfputc(glyph->format->BitsPerPixel, res); | |
232 dmf_write_le32(res, glyph->format->Rmask); | |
233 dmf_write_le32(res, glyph->format->Gmask); | |
234 dmf_write_le32(res, glyph->format->Bmask); | |
235 dmf_write_le32(res, glyph->format->Amask); | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
236 |
874 | 237 for (n = 0; n < font->nglyphs; n++) |
238 { | |
239 glyph = font->glyphs[n]; | |
240 if (glyph != NULL) | |
241 { | |
242 int y; | |
243 Uint8 *pixels = glyph->pixels; | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
244 |
874 | 245 if (glyph->w < DMFONT_MIN_WIDTH || |
246 glyph->h < DMFONT_MIN_HEIGHT || | |
247 glyph->w > DMFONT_MAX_WIDTH || | |
248 glyph->h > DMFONT_MAX_HEIGHT) | |
249 continue; | |
250 | |
251 // Each glyph has its table index and w/h stored | |
252 dmf_write_le16(res, n); | |
253 dmfputc(glyph->w, res); | |
254 dmfputc(glyph->h, res); | |
255 | |
256 // Write the pixel data | |
257 for (y = 0; y < glyph->h; y++) | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
258 { |
874 | 259 if (dmfwrite(pixels, glyph->format->BytesPerPixel, glyph->w, res) != (size_t) glyph->w) |
260 return DMERR_FWRITE; | |
261 pixels += glyph->pitch; | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
262 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
263 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
264 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
265 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
266 return DMERR_OK; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
267 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
268 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
269 |
160 | 270 int main(int argc, char *argv[]) |
271 { | |
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
|
272 DMResource *inFile = NULL, *outFile = NULL; |
160 | 273 DMBitmapFont *font = NULL; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
274 SDL_Surface *fontbmap = NULL; |
160 | 275 int res; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
276 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
277 BOOL initTTF = FALSE; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
278 TTF_Font *ttf = NULL; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
279 #endif |
160 | 280 |
644 | 281 dmInitProg("fontconv", "Bitmap font converter", "0.3", NULL, NULL); |
160 | 282 dmVerbosity = 1; |
283 | |
284 // Parse arguments | |
285 if (!dmArgsProcess(argc, argv, optList, optListN, | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
286 argHandleOpt, argHandleFile, OPTH_BAILOUT)) |
160 | 287 exit(1); |
288 | |
289 // Check arguments | |
645
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
290 if (!optInFilename || !optOutFilename) |
160 | 291 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
292 dmErrorMsg("Input or output file not specified!\n"); |
160 | 293 return 1; |
294 } | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
295 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
296 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
297 if (TTF_Init() < 0) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
298 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
299 dmErrorMsg("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
300 goto error_exit; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
301 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
302 initTTF = TRUE; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
303 #endif |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
304 |
160 | 305 // Open the source file |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
655
diff
changeset
|
306 if ((res = dmf_create_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) |
160 | 307 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
308 dmErrorMsg("Error opening input file '%s', %d: %s\n", |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
655
diff
changeset
|
309 optInFilename, res, dmErrorStr(res)); |
160 | 310 return 1; |
311 } | |
312 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
313 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
314 if ((res = dmLoadBitmapFont(inFile, &font)) == DMERR_OK) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
315 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
316 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
|
317 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
318 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
319 else |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
320 if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth)) != NULL) |
160 | 321 { |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
322 int i; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
323 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
|
324 optSplitWidth, optSplitHeight); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
325 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
326 TTF_SetFontStyle(ttf, TTF_STYLE_NORMAL); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
327 |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
328 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
|
329 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
330 goto error_exit; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
331 } |
160 | 332 |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
333 for (i = 0; i < 255; i++) |
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 char str[2]; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
336 str[0] = i; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
337 str[1] = 0; |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
338 font->glyphs[i] = TTF_RenderText_Blended(ttf, str, optColor); |
178
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 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
341 #endif |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
342 else |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
343 { |
1203
2b48b7fe95bc
Use dmfreset() instead of dmfseek() to seek to stream start.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
344 dmfreset(inFile); |
160 | 345 |
346 if ((fontbmap = dmLoadImage(inFile)) == NULL) | |
347 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
348 dmErrorMsg("Could not load image file '%s'.\n", optInFilename); |
160 | 349 goto error_exit; |
350 } | |
351 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
352 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
|
353 fontbmap->w, fontbmap->h, fontbmap->format->BitsPerPixel, |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
354 optSplitWidth, optSplitHeight); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
355 |
160 | 356 if ((res = dmCreateBitmapFontFromImage(fontbmap, optSplitWidth, optSplitHeight, &font)) != DMERR_OK) |
357 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
358 dmErrorMsg("Could not create a font from image, %d: %s\n", |
160 | 359 res, dmErrorStr(res)); |
360 goto error_exit; | |
361 } | |
362 } | |
363 | |
364 if (font == NULL) | |
365 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
366 dmErrorMsg("No font loaded.\n"); |
160 | 367 goto error_exit; |
368 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
369 |
579
f87446a81887
Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents:
362
diff
changeset
|
370 dmMsg(1, "Outputting a DMFONT format bitmap font.\n"); |
160 | 371 |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
655
diff
changeset
|
372 if ((res = dmf_create_stdio(optOutFilename, "wb", &outFile)) != DMERR_OK) |
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
|
373 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
374 dmErrorMsg("Error creating file '%s', %d: %s\n", |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
655
diff
changeset
|
375 optInFilename, res, dmErrorStr(res)); |
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
|
376 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
|
377 } |
160 | 378 |
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
|
379 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
|
380 dmf_close(outFile); |
160 | 381 |
382 if (res != DMERR_OK) | |
383 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
384 dmErrorMsg("Error saving font, %d: %s\n", |
160 | 385 res, dmErrorStr(res)); |
386 } | |
387 | |
388 error_exit: | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
389 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
390 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
391 if (initTTF) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
392 TTF_Quit(); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
393 #endif |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
394 |
160 | 395 dmf_close(inFile); |
396 dmFreeBitmapFont(font); | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
397 SDL_FreeSurface(fontbmap); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
398 |
160 | 399 return 0; |
400 } |