Mercurial > hg > dmlib
annotate fontconv.c @ 649:8986893fd5e5
And another 10L.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 15 Apr 2013 14:20:45 +0300 |
parents | 7303c43bf347 |
children |
rev | line source |
---|---|
160 | 1 /* |
2 * fontconv - Convert bitmap fonts | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
4 * (C) Copyright 2012 Tecnic Software productions (TNSP) | |
5 * | |
6 * Please read file 'COPYING' for information on license and distribution. | |
7 */ | |
8 #include <stdio.h> | |
9 #include <errno.h> | |
10 #include "dmlib.h" | |
11 #include "dmargs.h" | |
12 #include "dmfile.h" | |
13 #include "dmimage.h" | |
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 | 16 |
579
f87446a81887
Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents:
362
diff
changeset
|
17 char *optInFilename = NULL, *optOutFilename = NULL; |
160 | 18 |
579
f87446a81887
Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents:
362
diff
changeset
|
19 int optSplitWidth = 8, |
160 | 20 optSplitHeight = 8; |
21 | |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
22 SDL_Color optColor = { 255, 255, 255, 100 }; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
23 |
160 | 24 |
25 DMOptArg optList[] = | |
26 { | |
27 { 0, '?', "help", "Show this help", OPT_NONE }, | |
28 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, | |
649 | 29 { 2, 'o', "output", "Set output filename", OPT_ARGREQ }, |
160 | 30 { 3, 's', "size", "Set glyph dimensions (-s WxH) for image->font conversion", OPT_ARGREQ }, |
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 }, |
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 |
160 | 45 dmArgsPrintHelp(stdout, optList, optListN); |
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: |
7303c43bf347
Fix fontconv option handling again.
Matti Hamalainen <ccr@tnsp.org>
parents:
647
diff
changeset
|
60 optOutFilename = optArg; |
7303c43bf347
Fix fontconv option handling again.
Matti Hamalainen <ccr@tnsp.org>
parents:
647
diff
changeset
|
61 break; |
7303c43bf347
Fix fontconv option handling again.
Matti Hamalainen <ccr@tnsp.org>
parents:
647
diff
changeset
|
62 |
160 | 63 case 3: |
64 { | |
65 int w, h; | |
66 if (sscanf(optArg, "%dx%d", &w, &h) != 2) | |
67 { | |
68 dmError("Invalid argument for -s option, '%s'.\n", | |
69 optArg); | |
70 return FALSE; | |
71 } | |
72 if (w < DMFONT_MIN_WIDTH || w > DMFONT_MAX_WIDTH || | |
73 h < DMFONT_MIN_HEIGHT || h > DMFONT_MAX_HEIGHT) | |
74 { | |
75 dmError("Invalid dimensions, must be %d < W %d, %d < H < %d.\n", | |
76 DMFONT_MIN_WIDTH , DMFONT_MAX_WIDTH, | |
77 DMFONT_MIN_HEIGHT , DMFONT_MAX_HEIGHT); | |
78 return FALSE; | |
79 } | |
80 optSplitWidth = w; | |
81 optSplitHeight = h; | |
82 } | |
83 break; | |
84 | |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
85 case 4: |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
86 { |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
87 int colR, colG, colB, colA = 100; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
88 if (optArg[0] == '#' || optArg[0] == '$') optArg++; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
89 else |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
90 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
|
91 |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
92 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
|
93 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
|
94 { |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
95 dmError("Invalid RGB hex representation '%s'.\n", |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
96 optArg); |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
97 return FALSE; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
98 } |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
99 |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
100 optColor.r = colR; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
101 optColor.g = colG; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
102 optColor.b = colB; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
103 optColor.unused = colA; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
104 } |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
105 break; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
106 |
160 | 107 default: |
108 dmError("Unknown argument '%s'.\n", currArg); | |
109 return FALSE; | |
110 } | |
111 | |
112 return TRUE; | |
113 } | |
114 | |
115 | |
116 BOOL argHandleFile(char *currArg) | |
117 { | |
118 if (!optInFilename) | |
119 optInFilename = currArg; | |
120 else | |
648
7303c43bf347
Fix fontconv option handling again.
Matti Hamalainen <ccr@tnsp.org>
parents:
647
diff
changeset
|
121 if (!optOutFilename) |
645
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
122 optOutFilename = currArg; |
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
123 else |
160 | 124 { |
125 dmError("Too many filename arguments, '%s'\n", currArg); | |
126 return FALSE; | |
127 } | |
128 | |
129 return TRUE; | |
130 } | |
131 | |
132 | |
133 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont) | |
134 { | |
135 int nglyph, xc, yc, xglyphs, yglyphs; | |
136 DMBitmapFont *font; | |
137 | |
138 if (image->w < width || width < 4 || image->h < height || height < 4) | |
139 return DMERR_INVALID_ARGS; | |
140 | |
141 xglyphs = image->w / width; | |
142 yglyphs = image->h / height; | |
143 | |
144 if ((font = dmNewBitmapFont(xglyphs * yglyphs, width, height)) == NULL) | |
145 return DMERR_MALLOC; | |
146 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
147 dmMsg(1, "%d x %d split as %d x %d blocks => %d x %d = %d glyphs.\n", |
160 | 148 image->w, image->h, |
149 width, height, | |
150 xglyphs, yglyphs, xglyphs * yglyphs); | |
151 | |
152 nglyph = 0; | |
153 for (yc = 0; yc < yglyphs; yc++) | |
154 for (xc = 0; xc < xglyphs; xc++) | |
155 { | |
156 SDL_Surface *glyph = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, | |
157 image->format->BitsPerPixel, | |
158 image->format->Rmask, | |
159 image->format->Gmask, | |
160 image->format->Bmask, | |
161 image->format->Amask); | |
162 | |
163 if (glyph == NULL) | |
164 { | |
165 dmFreeBitmapFont(font); | |
166 return DMERR_MALLOC; | |
167 } | |
168 | |
169 SDL_Rect r; | |
170 r.x = xc * width; | |
171 r.y = yc * height; | |
172 r.w = width; | |
173 r.h = height; | |
174 | |
175 SDL_BlitSurface(image, &r, glyph, NULL); | |
176 | |
177 font->glyphs[nglyph++] = glyph; | |
178 } | |
179 | |
180 *pfont = font; | |
181 return DMERR_OK; | |
182 } | |
183 | |
184 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
185 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
186 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
187 int maxglyph, nglyphs, n; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
188 if (font == NULL) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
189 return DMERR_NULLPTR; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
190 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
191 if (font->nglyphs > DMFONT_MAX_GLYPHS || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
192 font->width > DMFONT_MAX_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
193 font->height > DMFONT_MAX_HEIGHT || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
194 font->width < DMFONT_MIN_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
195 font->height < DMFONT_MIN_HEIGHT) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
196 return DMERR_INVALID_DATA; |
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 // Count number of actually existing glyphs |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
199 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
|
200 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
201 SDL_Surface *glyph = font->glyphs[n]; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
202 if (glyph != NULL) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
203 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
204 maxglyph = n; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
205 if (glyph->w < DMFONT_MIN_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
206 glyph->h < DMFONT_MIN_HEIGHT || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
207 glyph->w > DMFONT_MAX_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
208 glyph->h > DMFONT_MAX_HEIGHT) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
209 continue; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
210 nglyphs++; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
211 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
212 } |
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 // 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); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
223 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
224 if (nglyphs > 0) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
225 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
226 int i; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
227 SDL_Surface *glyph = font->glyphs[maxglyph]; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
228 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
229 // 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
|
230 dmfputc(glyph->format->BitsPerPixel, res); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
231 dmf_write_le32(res, glyph->format->Rmask); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
232 dmf_write_le32(res, glyph->format->Gmask); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
233 dmf_write_le32(res, glyph->format->Bmask); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
234 dmf_write_le32(res, glyph->format->Amask); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
235 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
236 for (i = 0; i < font->nglyphs; i++) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
237 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
238 glyph = font->glyphs[i]; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
239 if (glyph != NULL) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
240 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
241 int y; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
242 Uint8 *pixels = glyph->pixels; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
243 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
244 if (glyph->w < DMFONT_MIN_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
245 glyph->h < DMFONT_MIN_HEIGHT || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
246 glyph->w > DMFONT_MAX_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
247 glyph->h > DMFONT_MAX_HEIGHT) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
248 continue; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
249 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
250 // 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
|
251 dmf_write_le16(res, i); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
252 dmfputc(glyph->w, res); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
253 dmfputc(glyph->h, res); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
254 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
255 // Write the pixel data |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
256 for (y = 0; y < glyph->h; y++) |
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 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
|
259 return DMERR_FWRITE; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
260 pixels += glyph->pitch; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
261 } |
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, | |
286 argHandleOpt, argHandleFile, TRUE)) | |
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 { |
292 dmError("Input or output file not specified!\n"); | |
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 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
299 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
|
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 |
160 | 304 |
305 // Open the source file | |
306 if ((inFile = dmf_create_stdio(optInFilename, "rb")) == NULL) | |
307 { | |
308 dmError("Error opening input file '%s', %d: %s\n", | |
309 optInFilename, errno, strerror(errno)); | |
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); |
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 ((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 { |
160 | 344 dmfseek(inFile, 0L, SEEK_SET); |
345 | |
346 if ((fontbmap = dmLoadImage(inFile)) == NULL) | |
347 { | |
348 dmError("Could not load image file '%s'.\n", optInFilename); | |
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 { | |
358 dmError("Could not create a font from image, %d: %s\n", | |
359 res, dmErrorStr(res)); | |
360 goto error_exit; | |
361 } | |
362 } | |
363 | |
364 if (font == NULL) | |
365 { | |
366 dmError("No font loaded.\n"); | |
367 goto error_exit; | |
368 } | |
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 |
645
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
372 if ((outFile = dmf_create_stdio(optOutFilename, "wb")) == 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
|
373 { |
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 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
|
375 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
|
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 { | |
384 dmError("Error saving font, %d: %s\n", | |
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 |
160 | 394 |
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); |
160 | 398 |
399 return 0; | |
400 } |