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