comparison tools/fontconv.c @ 2259:8ca515ab9c84

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 17 Jun 2019 00:45:26 +0300
parents e3f0eaf23f4f
children b7cd5dd0b82e
comparison
equal deleted inserted replaced
2258:c146033f1f6a 2259:8ca515ab9c84
294 294
295 #ifdef DM_GFX_TTF_TEXT 295 #ifdef DM_GFX_TTF_TEXT
296 if (TTF_Init() < 0) 296 if (TTF_Init() < 0)
297 { 297 {
298 dmErrorMsg("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); 298 dmErrorMsg("Could not initialize FreeType/TTF: %s\n", SDL_GetError());
299 goto out; 299 goto exit;
300 } 300 }
301 initTTF = TRUE; 301 initTTF = TRUE;
302 #endif 302 #endif
303 303
304 // Open the source file 304 // Open the source file
305 if ((res = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) 305 if ((res = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK)
306 { 306 {
307 dmErrorMsg("Error opening input file '%s', %d: %s\n", 307 dmErrorMsg("Error opening input file '%s', %d: %s\n",
308 optInFilename, res, dmErrorStr(res)); 308 optInFilename, res, dmErrorStr(res));
309 return 1; 309 goto exit;
310 } 310 }
311 311
312 312
313 if ((res = dmLoadBitmapFont(inFile, &font)) == DMERR_OK) 313 if ((res = dmLoadBitmapFont(inFile, &font)) == DMERR_OK)
314 { 314 {
318 else 318 else
319 if (res != DMERR_INVALID) 319 if (res != DMERR_INVALID)
320 { 320 {
321 dmErrorMsg("Input is a TSFONT/DMFONT font file, but there is an error: %s\n", 321 dmErrorMsg("Input is a TSFONT/DMFONT font file, but there is an error: %s\n",
322 dmErrorStr(res)); 322 dmErrorStr(res));
323 goto out; 323 goto exit;
324 } 324 }
325 #ifdef DM_GFX_TTF_TEXT 325 #ifdef DM_GFX_TTF_TEXT
326 else 326 else
327 if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth - 1)) != NULL) 327 if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth - 1)) != NULL)
328 { 328 {
339 // Create the bitmap font 339 // Create the bitmap font
340 if ((font = dmNewBitmapFont(gmax - gmin + 1, 256, 340 if ((font = dmNewBitmapFont(gmax - gmin + 1, 256,
341 optSplitWidth - 6, optSplitHeight + 2, optBPP)) == NULL) 341 optSplitWidth - 6, optSplitHeight + 2, optBPP)) == NULL)
342 { 342 {
343 dmErrorMsg("Could not allocate bitmap font!\n"); 343 dmErrorMsg("Could not allocate bitmap font!\n");
344 goto out; 344 goto exit;
345 } 345 }
346 346
347 // Render glyphs from the normal ASCII range only 347 // Render glyphs from the normal ASCII range only
348 for (int index = 0, nglyph = gmin; nglyph <= gmax; nglyph++) 348 for (int index = 0, nglyph = gmin; nglyph <= gmax; nglyph++)
349 { 349 {
366 366
367 if (TTF_GlyphMetrics(ttf, nglyph, &minx, NULL, &miny, NULL, &advance) == -1) 367 if (TTF_GlyphMetrics(ttf, nglyph, &minx, NULL, &miny, NULL, &advance) == -1)
368 { 368 {
369 dmErrorMsg("Could not get TTF glyph metrics for character '%c' (%d).\n", 369 dmErrorMsg("Could not get TTF glyph metrics for character '%c' (%d).\n",
370 nglyph, nglyph); 370 nglyph, nglyph);
371 goto out; 371 goto exit;
372 } 372 }
373 373
374 dst.x = 0; 374 dst.x = 0;
375 dst.y = index * font->height; 375 dst.y = index * font->height;
376 dst.w = font->width; 376 dst.w = font->width;
393 dmfreset(inFile); 393 dmfreset(inFile);
394 394
395 if ((fontbmap = dmLoadImage(inFile)) == NULL) 395 if ((fontbmap = dmLoadImage(inFile)) == NULL)
396 { 396 {
397 dmErrorMsg("Could not load image file '%s'.\n", optInFilename); 397 dmErrorMsg("Could not load image file '%s'.\n", optInFilename);
398 goto out; 398 goto exit;
399 } 399 }
400 400
401 dmMsg(1, "Input is a bitmap image (%d x %d, %d bpp), splitting to %d x %d.\n", 401 dmMsg(1, "Input is a bitmap image (%d x %d, %d bpp), splitting to %d x %d.\n",
402 fontbmap->w, fontbmap->h, fontbmap->format->BitsPerPixel, 402 fontbmap->w, fontbmap->h, fontbmap->format->BitsPerPixel,
403 optSplitWidth, optSplitHeight); 403 optSplitWidth, optSplitHeight);
404 404
405 if ((res = dmCreateBitmapFontFromImage(fontbmap, optSplitWidth, optSplitHeight, &font)) != DMERR_OK) 405 if ((res = dmCreateBitmapFontFromImage(fontbmap, optSplitWidth, optSplitHeight, &font)) != DMERR_OK)
406 { 406 {
407 dmErrorMsg("Could not create a font from image, %d: %s\n", 407 dmErrorMsg("Could not create a font from image, %d: %s\n",
408 res, dmErrorStr(res)); 408 res, dmErrorStr(res));
409 goto out; 409 goto exit;
410 } 410 }
411 } 411 }
412 412
413 if (font == NULL) 413 if (font == NULL)
414 { 414 {
415 dmErrorMsg("No font loaded.\n"); 415 dmErrorMsg("No font loaded.\n");
416 goto out; 416 goto exit;
417 } 417 }
418 418
419 // Count number of actually existing glyphs despite that we should have 419 // Count number of actually existing glyphs despite that we should have
420 // that information in font->nglyphs. Also sanity check the glyphs. 420 // that information in font->nglyphs. Also sanity check the glyphs.
421 font->nglyphs = 0; 421 font->nglyphs = 0;
435 { 435 {
436 dmErrorMsg("Invalid glyph #%d: %d x %d (font %d x %d)\n", 436 dmErrorMsg("Invalid glyph #%d: %d x %d (font %d x %d)\n",
437 n, 437 n,
438 glyph->width, glyph->height, 438 glyph->width, glyph->height,
439 font->width, font->height); 439 font->width, font->height);
440 goto out; 440 goto exit;
441 } 441 }
442 } 442 }
443 } 443 }
444 444
445 dmMsg(1, "Outputting a DMFONT format bitmap font, %d x %d with %d glyphs (%d max), %d bpp.\n", 445 dmMsg(1, "Outputting a DMFONT format bitmap font, %d x %d with %d glyphs (%d max), %d bpp.\n",
449 449
450 if ((res = dmf_open_stdio(optOutFilename, "wb", &outFile)) != DMERR_OK) 450 if ((res = dmf_open_stdio(optOutFilename, "wb", &outFile)) != DMERR_OK)
451 { 451 {
452 dmErrorMsg("Error creating file '%s', %d: %s\n", 452 dmErrorMsg("Error creating file '%s', %d: %s\n",
453 optInFilename, res, dmErrorStr(res)); 453 optInFilename, res, dmErrorStr(res));
454 goto out; 454 goto exit;
455 } 455 }
456 456
457 res = dmSaveBitmapFont(outFile, font); 457 res = dmSaveBitmapFont(outFile, font);
458 dmf_close(outFile); 458 dmf_close(outFile);
459 459
461 { 461 {
462 dmErrorMsg("Error saving font, %d: %s\n", 462 dmErrorMsg("Error saving font, %d: %s\n",
463 res, dmErrorStr(res)); 463 res, dmErrorStr(res));
464 } 464 }
465 465
466 out: 466 exit:
467 467 // Cleanup
468 #ifdef DM_GFX_TTF_TEXT 468 #ifdef DM_GFX_TTF_TEXT
469 if (initTTF) 469 if (initTTF)
470 TTF_Quit(); 470 TTF_Quit();
471 #endif 471 #endif
472 472
473 dmf_close(inFile); 473 dmf_close(inFile);
474 dmFreeBitmapFont(font); 474 dmFreeBitmapFont(font);
475 SDL_FreeSurface(fontbmap); 475 if (fontbmap != NULL)
476 SDL_FreeSurface(fontbmap);
476 477
477 return 0; 478 return 0;
478 } 479 }