annotate tools/fontconv.c @ 2572:92b93a12c014

Make some casts explicit instead of implicit, fixes some -pedantic warnings.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 10 Mar 2022 22:36:48 +0200
parents d56a0e86067a
children 9807ae37ad69
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * fontconv - Convert bitmap fonts
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
874
9d874c1c4a58 Cleanups in fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 867
diff changeset
4 * (C) Copyright 2012-2015 Tecnic Software productions (TNSP)
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include <stdio.h>
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include "dmlib.h"
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "dmargs.h"
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include "dmfile.h"
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include "dmimage.h"
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #include "dmtext.h"
1881
73545a442ffe Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents: 1606
diff changeset
14 #include "dmres.h"
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17
579
f87446a81887 Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents: 362
diff changeset
18 int optSplitWidth = 8,
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
19 optSplitHeight = 8,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
20 optBPP = 32;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
860
daebbf28953d The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 730
diff changeset
25 static const DMOptArg optList[] =
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 {
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
27 { 0, '?', "help" , "Show this help", OPT_NONE },
2410
bc05bcfc4598 Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents: 2408
diff changeset
28 { 1, 0, "license" , "Print out this program's license agreement", OPT_NONE },
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
29 { 2, 'v', "verbose" , "Be more verbose", OPT_NONE },
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
30
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
31 { 10, 's', "size" , "Set glyph dimensions (-s W:H or -s N) for image->font conversion", OPT_ARGREQ },
1272
acae5f8ebc67 Fix build process.
Matti Hamalainen <ccr@tnsp.org>
parents: 1235
diff changeset
32 #ifdef DM_GFX_TTF_TEXT
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
33 { 12, 'c', "color" , "TTF font rendering color (def: 0xFFFFFF)", OPT_ARGREQ },
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
34 { 14, 'b', "bpp" , "Render font in 8 or 32 bits per pixel (default 32)", OPT_ARGREQ },
1272
acae5f8ebc67 Fix build process.
Matti Hamalainen <ccr@tnsp.org>
parents: 1235
diff changeset
35 #endif
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 };
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 const int optListN = sizeof(optList) / sizeof(optList[0]);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
41 void argShowHelp()
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
42 {
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
43 dmPrintBanner(stdout, dmProgName,
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
44 "[options] <sourcefile.(ttf|fnt|dmf|png)> <outputfile.dmf>");
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
45
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
46 dmArgsPrintHelp(stdout, optList, optListN, 0, 80 - 2);
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
47 printf(
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
48 "\n"
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
49 "This utility can be used to convert TSFONT files to bitmap DMFONT (DMF)\n"
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
50 "files, render TrueType TTF to DMFONT at desired glyph resolution, or\n"
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
51 "cut a PNG (or JPEG) image to glyphs of desired size.\n");
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
52 }
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
53
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
54
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 switch (optN)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 case 0:
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
60 argShowHelp();
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 exit(0);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 break;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 case 1:
2410
bc05bcfc4598 Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents: 2408
diff changeset
65 dmPrintLicense(stdout);
bc05bcfc4598 Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents: 2408
diff changeset
66 exit(0);
bc05bcfc4598 Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents: 2408
diff changeset
67 break;
bc05bcfc4598 Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents: 2408
diff changeset
68
bc05bcfc4598 Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents: 2408
diff changeset
69 case 2:
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 dmVerbosity++;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 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
72
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
73 case 10:
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 {
2075
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
75 unsigned int fontW, fontH;
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
76 char *sep = strchr(optArg, ':');
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
77 if (sep != NULL)
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 {
2075
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
79 char *tmpStr = dm_strndup(optArg, sep - optArg);
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
80
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
81 if (!dmGetIntVal(tmpStr, &fontW, NULL) ||
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
82 !dmGetIntVal(sep + 1, &fontH, NULL))
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
83 {
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
84 dmErrorMsg("Invalid font width or height value ('%s')\n",
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
85 optArg);
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
86
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
87 dmFree(tmpStr);
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
88 return FALSE;
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
89 }
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
90
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
91 dmFree(tmpStr);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 }
2075
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
93 else
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 {
2075
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
95 if (!dmGetIntVal(optArg, &fontW, NULL))
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
96 {
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
97 dmErrorMsg("Invalid font size value ('%s')\n",
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
98 optArg);
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
99
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
100 return FALSE;
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
101 }
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
102 fontH = fontW;
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
103 }
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
104
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
105 if (fontW < DMFONT_MIN_WIDTH || fontW > DMFONT_MAX_WIDTH ||
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
106 fontH < DMFONT_MIN_HEIGHT || fontH > DMFONT_MAX_HEIGHT)
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
107 {
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
108 dmErrorMsg("Invalid font dimensions, must be %d < W %d, %d < H < %d.\n",
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 DMFONT_MIN_WIDTH , DMFONT_MAX_WIDTH,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 DMFONT_MIN_HEIGHT , DMFONT_MAX_HEIGHT);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 return FALSE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 }
2075
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
113 optSplitWidth = fontW;
2ca6a13b091b Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2007
diff changeset
114 optSplitHeight = fontH;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 break;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
118 case 12:
633
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
119 {
867
56e12109b936 Portability warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 860
diff changeset
120 unsigned int colR, colG, colB, colA = 100;
633
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
121 if (optArg[0] == '#' || optArg[0] == '$') optArg++;
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
122 else
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
123 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
124
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
125 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
126 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
127 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 885
diff changeset
128 dmErrorMsg("Invalid RGB hex representation '%s'.\n",
633
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
129 optArg);
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
130 return FALSE;
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
131 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
132
633
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
133 optColor.r = colR;
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
134 optColor.g = colG;
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
135 optColor.b = colB;
1557
5e5f75b45f8d Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 1357
diff changeset
136 optColor.a = colA;
633
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
137 }
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
138 break;
151747a24f57 Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 579
diff changeset
139
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
140 case 14:
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
141 if (sscanf(optArg, "%d", &optBPP) != 1)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
142 {
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
143 dmErrorMsg("Invalid argument for -b option, '%s'.\n",
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
144 optArg);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
145 return FALSE;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
146 }
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
147 if (optBPP != 8 && optBPP != 32)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
148 {
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
149 dmErrorMsg("Invalid bit depth %d, must be 8 or 32.\n",
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
150 optBPP);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
151 return FALSE;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
152 }
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
153 break;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
154
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 default:
2183
e3f0eaf23f4f Change the error message for unimplemented option argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 2075
diff changeset
156 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 return FALSE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
159
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 return TRUE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 BOOL argHandleFile(char *currArg)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
166 if (optInFilename == NULL)
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 optInFilename = currArg;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 else
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
169 if (optOutFilename == NULL)
645
6dd155bbfc5c Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents: 644
diff changeset
170 optOutFilename = currArg;
6dd155bbfc5c Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents: 644
diff changeset
171 else
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 885
diff changeset
173 dmErrorMsg("Too many filename arguments, '%s'\n", currArg);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 return FALSE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
176
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 return TRUE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
183 int nglyph, xglyphs, yglyphs;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 DMBitmapFont *font;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
186 if (image->w < width || width < 2 ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
187 image->h < height || height < 2)
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 return DMERR_INVALID_ARGS;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
189
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 xglyphs = image->w / width;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 yglyphs = image->h / height;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
192
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
193 if ((font = dmNewBitmapFont(
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
194 xglyphs * yglyphs,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
195 xglyphs * yglyphs,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
196 width, height, image->format->BitsPerPixel)) == NULL)
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 return DMERR_MALLOC;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
199 dmMsg(1, "%d x %d split as %d x %d blocks => %d x %d = %d glyphs, bpp=%d.\n",
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 image->w, image->h,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 width, height,
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
202 xglyphs, yglyphs,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
203 xglyphs * yglyphs, image->format->BitsPerPixel);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
204
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 nglyph = 0;
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
206 for (int yc = 0; yc < yglyphs; yc++)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
207 for (int xc = 0; xc < xglyphs; xc++)
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
209 DMBitmapGlyph *glyph = &font->glyphMap[nglyph++];
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
210 SDL_Rect src, dst;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
211
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
212 src.x = xc * width;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
213 src.y = yc * height;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
214 dst.w = src.w = width;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
215 dst.h = src.h = height;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
217 dst.x = 0;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
218 dst.y = nglyph * height;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
219 glyph->index = nglyph;
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
220
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
221 SDL_BlitSurface(image, &src, font->glyphs, &dst);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
223
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 *pfont = font;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 return DMERR_OK;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
229 int dmSaveBitmapFont(DMResource *fp, DMBitmapFont *font)
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
230 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
231 if (font == NULL)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
232 return DMERR_NULLPTR;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
233
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
234 if (font->nglyphs > font->maxglyph ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
235 font->maxglyph > DMFONT_MAX_GLYPHS ||
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
236 font->width > DMFONT_MAX_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
237 font->height > DMFONT_MAX_HEIGHT ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
238 font->width < DMFONT_MIN_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
239 font->height < DMFONT_MIN_HEIGHT)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
240 return DMERR_INVALID_DATA;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
241
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
242 // Write the DMFONT header
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
243 if (!dmf_write_str(fp, (Uint8 *) DMFONT_MAGIC, 6) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
244 !dmf_write_le16(fp, DMFONT_VERSION) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
245 !dmf_write_le16(fp, font->nglyphs) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
246 !dmf_write_le16(fp, font->maxglyph) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
247 !dmf_write_byte(fp, font->width) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
248 !dmf_write_byte(fp, font->height) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
249 !dmf_write_byte(fp, font->glyphs->format->BitsPerPixel))
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
250 return DMERR_FWRITE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
251
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
252 // Write the glyph data
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
253 for (int index = 0; index < font->maxglyph; index++)
874
9d874c1c4a58 Cleanups in fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 867
diff changeset
254 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
255 DMBitmapGlyph *glyph = &font->glyphMap[index];
1967
8a98d1517460 Fix glyph index saving .. sigh.
Matti Hamalainen <ccr@tnsp.org>
parents: 1965
diff changeset
256 if (glyph->index >= 0)
874
9d874c1c4a58 Cleanups in fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 867
diff changeset
257 {
2572
92b93a12c014 Make some casts explicit instead of implicit, fixes some -pedantic warnings.
Matti Hamalainen <ccr@tnsp.org>
parents: 2565
diff changeset
258 Uint8 *pixels = (Uint8 *) font->glyphs->pixels + font->gsize * glyph->index;
874
9d874c1c4a58 Cleanups in fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 867
diff changeset
259
9d874c1c4a58 Cleanups in fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 867
diff changeset
260 // Each glyph has its table index and w/h stored
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
261 if (!dmf_write_le16(fp, index) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
262 !dmf_write_byte(fp, glyph->width) ||
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
263 !dmf_write_byte(fp, glyph->height))
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
264 return DMERR_FWRITE;
874
9d874c1c4a58 Cleanups in fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 867
diff changeset
265
9d874c1c4a58 Cleanups in fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 867
diff changeset
266 // Write the pixel data
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
267 for (int y = 0; y < glyph->height; y++)
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
268 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
269 if (dmfwrite(pixels, font->glyphs->format->BytesPerPixel,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
270 glyph->width, fp) != (size_t) glyph->width)
874
9d874c1c4a58 Cleanups in fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents: 867
diff changeset
271 return DMERR_FWRITE;
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
272
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
273 pixels += font->glyphs->pitch;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
274 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
275 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
276 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
277
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
278 return DMERR_OK;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
279 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
280
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
281
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 int main(int argc, char *argv[])
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 {
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
284 DMResource *inFile = NULL, *outFile = NULL;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285 DMBitmapFont *font = NULL;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
286 SDL_Surface *fontbmap = NULL;
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
287 int res = DMERR_OK;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
288 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
289 BOOL initTTF = FALSE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
290 TTF_Font *ttf = NULL;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
291 #endif
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
293 dmInitProg("fontconv", "Bitmap font converter", "0.4", NULL, NULL);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 // Parse arguments
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 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
297 argHandleOpt, argHandleFile, OPTH_BAILOUT))
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
298 goto out;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 // Check arguments
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
301 if (optInFilename == NULL || optOutFilename == NULL)
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 {
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
303 argShowHelp();
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
304 res = dmError(DMERR_INVALID_ARGS,
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
305 "No input or output file specified!\n");
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
306 goto out;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
310 if (TTF_Init() < 0)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
311 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 885
diff changeset
312 dmErrorMsg("Could not initialize FreeType/TTF: %s\n", SDL_GetError());
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
313 goto out;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
314 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
315 initTTF = TRUE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
316 #endif
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
317
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 // Open the source file
1606
93d1050eac99 Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents: 1557
diff changeset
319 if ((res = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK)
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 {
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
321 dmErrorMsg("Error opening input file '%s': %s\n",
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
322 optInFilename, dmErrorStr(res));
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
323 goto out;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
326
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
327 if ((res = dmLoadBitmapFont(inFile, &font)) == DMERR_OK)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
328 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
329 dmMsg(1, "Input is a TSFONT/DMFONT font file, %d x %d, %d glyphs (%d max).\n",
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
330 font->width, font->height, font->nglyphs, font->maxglyph);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
331 }
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
332 else
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
333 if (res != DMERR_INVALID)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
334 {
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
335 dmErrorMsg("Input is a TSFONT/DMFONT font file, but there is an error: %s\n",
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
336 dmErrorStr(res));
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
337 goto out;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
338 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
339 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
340 else
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
341 if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth - 1)) != NULL)
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
343 int gmin = 34, gmax = 127;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
344
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
345 dmMsg(1, "Input is a TTF TrueType font, rendering at %d x %d, %d bpp.\n",
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
346 optSplitWidth, optSplitHeight, optBPP);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
347
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
348 dmMsg(1, "Rendering glyph range %d to %d inclusive.\n",
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
349 gmin, gmax);
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
350
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
351 TTF_SetFontStyle(ttf, TTF_STYLE_NORMAL);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
352
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
353 // Create the bitmap font
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
354 if ((font = dmNewBitmapFont(gmax - gmin + 1, 256,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
355 optSplitWidth - 6, optSplitHeight + 2, optBPP)) == NULL)
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
356 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
357 dmErrorMsg("Could not allocate bitmap font!\n");
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
358 goto out;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
359 }
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
361 // Render glyphs from the normal ASCII range only
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
362 for (int index = 0, nglyph = gmin; nglyph <= gmax; nglyph++)
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
363 {
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
364 SDL_Surface *tmp;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
365 char str[2];
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
366 str[0] = nglyph;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
367 str[1] = 0;
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
368
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
369 // Render the glyph from TTF to surface
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
370 if (optBPP == 8)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
371 tmp = TTF_RenderText_Solid(ttf, str, optColor);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
372 else
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
373 tmp = TTF_RenderText_Blended(ttf, str, optColor);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
374
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
375 if (tmp != NULL)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
376 {
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
377 DMBitmapGlyph *glyph = &font->glyphMap[nglyph];
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
378 int minx, miny, advance;
1960
b99e04c356ec Some dead code was accidentally left in. Delete it.
Matti Hamalainen <ccr@tnsp.org>
parents: 1957
diff changeset
379 SDL_Rect dst;
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
380
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
381 if (TTF_GlyphMetrics(ttf, nglyph, &minx, NULL, &miny, NULL, &advance) == -1)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
382 {
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
383 dmErrorMsg("Could not get TTF glyph metrics for character '%c' (%d).\n",
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
384 nglyph, nglyph);
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
385 goto out;
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
386 }
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
387
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
388 dst.x = 0;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
389 dst.y = index * font->height;
1976
5f9e8dd62c70 Use font general width/height for glyphs when generating from TrueType font,
Matti Hamalainen <ccr@tnsp.org>
parents: 1968
diff changeset
390 dst.w = font->width;
5f9e8dd62c70 Use font general width/height for glyphs when generating from TrueType font,
Matti Hamalainen <ccr@tnsp.org>
parents: 1968
diff changeset
391 dst.h = font->height;
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
392
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
393 // Set glyph data
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
394 glyph->width = font->width;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
395 glyph->height = font->height;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
396 glyph->index = index;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
397
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
398 SDL_BlitSurface(tmp, NULL, font->glyphs, &dst);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
399 SDL_FreeSurface(tmp);
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
400 index++;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
401 }
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
402 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
403 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
404 #endif
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
405 else
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
406 {
1203
2b48b7fe95bc Use dmfreset() instead of dmfseek() to seek to stream start.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
407 dmfreset(inFile);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 if ((fontbmap = dmLoadImage(inFile)) == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 885
diff changeset
411 dmErrorMsg("Could not load image file '%s'.\n", optInFilename);
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
412 goto out;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
414
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
415 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
416 fontbmap->w, fontbmap->h, fontbmap->format->BitsPerPixel,
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
417 optSplitWidth, optSplitHeight);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
418
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419 if ((res = dmCreateBitmapFontFromImage(fontbmap, optSplitWidth, optSplitHeight, &font)) != DMERR_OK)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 885
diff changeset
421 dmErrorMsg("Could not create a font from image, %d: %s\n",
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 res, dmErrorStr(res));
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
423 goto out;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 if (font == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 885
diff changeset
429 dmErrorMsg("No font loaded.\n");
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
430 goto out;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
431 }
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
432
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
433 // Count number of actually existing glyphs despite that we should have
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
434 // that information in font->nglyphs. Also sanity check the glyphs.
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
435 font->nglyphs = 0;
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
436 for (int n = 0; n < font->maxglyph; n++)
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
437 {
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
438 DMBitmapGlyph *glyph = &font->glyphMap[n];
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
439
1968
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
440 if (glyph->index >= 0)
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
441 {
1968
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
442 font->nglyphs++;
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
443 if (glyph->width < DMFONT_MIN_WIDTH ||
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
444 glyph->height < DMFONT_MIN_HEIGHT ||
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
445 glyph->width > DMFONT_MAX_WIDTH ||
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
446 glyph->height > DMFONT_MAX_HEIGHT ||
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
447 glyph->width > font->width ||
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
448 glyph->height > font->height)
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
449 {
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
450 dmErrorMsg("Invalid glyph #%d: %d x %d (font %d x %d)\n",
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
451 n,
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
452 glyph->width, glyph->height,
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
453 font->width, font->height);
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
454 goto out;
1968
868e39741d26 Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents: 1967
diff changeset
455 }
1957
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
456 }
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
457 }
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
458
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
459 dmMsg(1, "Outputting a DMFONT format bitmap font, %d x %d with %d glyphs (%d max), %d bpp.\n",
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
460 font->width, font->height,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
461 font->nglyphs, font->maxglyph,
ef08af6887b7 Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents: 1881
diff changeset
462 font->glyphs->format->BitsPerPixel);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463
1606
93d1050eac99 Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents: 1557
diff changeset
464 if ((res = dmf_open_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
465 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 885
diff changeset
466 dmErrorMsg("Error creating file '%s', %d: %s\n",
730
3d813c81f33c More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents: 655
diff changeset
467 optInFilename, res, dmErrorStr(res));
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
468 goto out;
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
469 }
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470
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
471 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
472 dmf_close(outFile);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
474 if (res != DMERR_OK)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
475 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 885
diff changeset
476 dmErrorMsg("Error saving font, %d: %s\n",
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
477 res, dmErrorStr(res));
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
478 goto out;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
481 out:
2259
8ca515ab9c84 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
482 // Cleanup
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
483 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
484 if (initTTF)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
485 TTF_Quit();
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
486 #endif
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
487
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 dmf_close(inFile);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 dmFreeBitmapFont(font);
2259
8ca515ab9c84 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
490 if (fontbmap != NULL)
8ca515ab9c84 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
491 SDL_FreeSurface(fontbmap);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1203
diff changeset
492
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
493 return res;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494 }