Mercurial > hg > dmlib
comparison tools/fontconv.c @ 874:9d874c1c4a58
Cleanups in fontconv.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 04 Feb 2015 17:49:04 +0200 |
parents | 56e12109b936 |
children | ee46d039c45d |
comparison
equal
deleted
inserted
replaced
873:26ea35e914ca | 874:9d874c1c4a58 |
---|---|
1 /* | 1 /* |
2 * fontconv - Convert bitmap fonts | 2 * fontconv - Convert bitmap fonts |
3 * Programmed and designed by Matti 'ccr' Hamalainen | 3 * Programmed and designed by Matti 'ccr' Hamalainen |
4 * (C) Copyright 2012 Tecnic Software productions (TNSP) | 4 * (C) Copyright 2012-2015 Tecnic Software productions (TNSP) |
5 * | 5 * |
6 * Please read file 'COPYING' for information on license and distribution. | 6 * Please read file 'COPYING' for information on license and distribution. |
7 */ | 7 */ |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include "dmlib.h" | 9 #include "dmlib.h" |
132 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont) | 132 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont) |
133 { | 133 { |
134 int nglyph, xc, yc, xglyphs, yglyphs; | 134 int nglyph, xc, yc, xglyphs, yglyphs; |
135 DMBitmapFont *font; | 135 DMBitmapFont *font; |
136 | 136 |
137 if (image->w < width || width < 4 || image->h < height || height < 4) | 137 if (image->w < width || width < 2 || image->h < height || height < 2) |
138 return DMERR_INVALID_ARGS; | 138 return DMERR_INVALID_ARGS; |
139 | 139 |
140 xglyphs = image->w / width; | 140 xglyphs = image->w / width; |
141 yglyphs = image->h / height; | 141 yglyphs = image->h / height; |
142 | 142 |
181 } | 181 } |
182 | 182 |
183 | 183 |
184 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font) | 184 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font) |
185 { | 185 { |
186 SDL_Surface *glyph; | |
186 int maxglyph, nglyphs, n; | 187 int maxglyph, nglyphs, n; |
187 if (font == NULL) | 188 if (font == NULL) |
188 return DMERR_NULLPTR; | 189 return DMERR_NULLPTR; |
189 | 190 |
190 if (font->nglyphs > DMFONT_MAX_GLYPHS || | 191 if (font->nglyphs > DMFONT_MAX_GLYPHS || |
207 glyph->h > DMFONT_MAX_HEIGHT) | 208 glyph->h > DMFONT_MAX_HEIGHT) |
208 continue; | 209 continue; |
209 nglyphs++; | 210 nglyphs++; |
210 } | 211 } |
211 } | 212 } |
212 | 213 |
214 if (nglyphs == 0) | |
215 return DMERR_INVALID_DATA; | |
216 | |
213 // Write the DMFONT header | 217 // Write the DMFONT header |
214 if (!dmf_write_str(res, (Uint8 *) DMFONT_MAGIC, 6)) | 218 if (!dmf_write_str(res, (Uint8 *) DMFONT_MAGIC, 6)) |
215 return DMERR_FWRITE; | 219 return DMERR_FWRITE; |
216 | 220 |
217 dmf_write_le16(res, DMFONT_VERSION); | 221 dmf_write_le16(res, DMFONT_VERSION); |
218 dmf_write_le16(res, nglyphs); | 222 dmf_write_le16(res, nglyphs); |
219 dmf_write_le16(res, maxglyph + 1); | 223 dmf_write_le16(res, maxglyph + 1); |
220 dmfputc(font->width, res); | 224 dmfputc(font->width, res); |
221 dmfputc(font->height, res); | 225 dmfputc(font->height, res); |
222 | 226 |
223 if (nglyphs > 0) | 227 // Store glyph format data |
224 { | 228 glyph = font->glyphs[maxglyph]; |
225 int i; | 229 dmfputc(glyph->format->BitsPerPixel, res); |
226 SDL_Surface *glyph = font->glyphs[maxglyph]; | 230 dmf_write_le32(res, glyph->format->Rmask); |
227 | 231 dmf_write_le32(res, glyph->format->Gmask); |
228 // If there are actual glyphs stored, save this | 232 dmf_write_le32(res, glyph->format->Bmask); |
229 dmfputc(glyph->format->BitsPerPixel, res); | 233 dmf_write_le32(res, glyph->format->Amask); |
230 dmf_write_le32(res, glyph->format->Rmask); | 234 |
231 dmf_write_le32(res, glyph->format->Gmask); | 235 for (n = 0; n < font->nglyphs; n++) |
232 dmf_write_le32(res, glyph->format->Bmask); | 236 { |
233 dmf_write_le32(res, glyph->format->Amask); | 237 glyph = font->glyphs[n]; |
234 | 238 if (glyph != NULL) |
235 for (i = 0; i < font->nglyphs; i++) | 239 { |
236 { | 240 int y; |
237 glyph = font->glyphs[i]; | 241 Uint8 *pixels = glyph->pixels; |
238 if (glyph != NULL) | 242 |
243 if (glyph->w < DMFONT_MIN_WIDTH || | |
244 glyph->h < DMFONT_MIN_HEIGHT || | |
245 glyph->w > DMFONT_MAX_WIDTH || | |
246 glyph->h > DMFONT_MAX_HEIGHT) | |
247 continue; | |
248 | |
249 // Each glyph has its table index and w/h stored | |
250 dmf_write_le16(res, n); | |
251 dmfputc(glyph->w, res); | |
252 dmfputc(glyph->h, res); | |
253 | |
254 // Write the pixel data | |
255 for (y = 0; y < glyph->h; y++) | |
239 { | 256 { |
240 int y; | 257 if (dmfwrite(pixels, glyph->format->BytesPerPixel, glyph->w, res) != (size_t) glyph->w) |
241 Uint8 *pixels = glyph->pixels; | 258 return DMERR_FWRITE; |
242 | 259 pixels += glyph->pitch; |
243 if (glyph->w < DMFONT_MIN_WIDTH || | |
244 glyph->h < DMFONT_MIN_HEIGHT || | |
245 glyph->w > DMFONT_MAX_WIDTH || | |
246 glyph->h > DMFONT_MAX_HEIGHT) | |
247 continue; | |
248 | |
249 // Each glyph has its table index and w/h stored | |
250 dmf_write_le16(res, i); | |
251 dmfputc(glyph->w, res); | |
252 dmfputc(glyph->h, res); | |
253 | |
254 // Write the pixel data | |
255 for (y = 0; y < glyph->h; y++) | |
256 { | |
257 if (dmfwrite(pixels, glyph->format->BytesPerPixel, glyph->w, res) != (size_t) glyph->w) | |
258 return DMERR_FWRITE; | |
259 pixels += glyph->pitch; | |
260 } | |
261 } | 260 } |
262 } | 261 } |
263 } | 262 } |
264 | 263 |
265 return DMERR_OK; | 264 return DMERR_OK; |