Mercurial > hg > dmlib
annotate 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 |
rev | line source |
---|---|
160 | 1 /* |
2 * fontconv - Convert bitmap fonts | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
874 | 4 * (C) Copyright 2012-2015 Tecnic Software productions (TNSP) |
160 | 5 * |
6 * Please read file 'COPYING' for information on license and distribution. | |
7 */ | |
8 #include <stdio.h> | |
9 #include "dmlib.h" | |
10 #include "dmargs.h" | |
11 #include "dmfile.h" | |
12 #include "dmimage.h" | |
13 #include "dmtext.h" | |
1881
73545a442ffe
Move dmresw function declarations to dmres.h
Matti Hamalainen <ccr@tnsp.org>
parents:
1606
diff
changeset
|
14 #include "dmres.h" |
160 | 15 |
579
f87446a81887
Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents:
362
diff
changeset
|
16 char *optInFilename = NULL, *optOutFilename = NULL; |
160 | 17 |
579
f87446a81887
Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents:
362
diff
changeset
|
18 int optSplitWidth = 8, |
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 | 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 |
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 | 26 { |
27 { 0, '?', "help", "Show this help", OPT_NONE }, | |
28 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, | |
2075
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
29 { 2, 's', "size", "Set glyph dimensions (-s W:H or -s N) for image->font conversion", OPT_ARGREQ }, |
1272 | 30 #ifdef DM_GFX_TTF_TEXT |
1357
370c40e0847f
Remove useless -o option from fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
1272
diff
changeset
|
31 { 3, 'c', "color", "TTF font rendering color (def: 0xFFFFFF)", OPT_ARGREQ }, |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
32 { 4, 'b', "bpp", "Render font in 8 or 32 bits per pixel (default 32)", OPT_ARGREQ }, |
1272 | 33 #endif |
160 | 34 }; |
35 | |
36 const int optListN = sizeof(optList) / sizeof(optList[0]); | |
37 | |
38 | |
39 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | |
40 { | |
41 switch (optN) | |
42 { | |
43 case 0: | |
44 dmPrintBanner(stdout, dmProgName, | |
645
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
45 "[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
|
46 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
47 dmArgsPrintHelp(stdout, optList, optListN, 0); |
646
cc4e6ab6120b
Add some help text to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
645
diff
changeset
|
48 printf( |
647 | 49 "\n" |
646
cc4e6ab6120b
Add some help text to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
645
diff
changeset
|
50 "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
|
51 "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
|
52 "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
|
53 |
160 | 54 exit(0); |
55 break; | |
56 | |
57 case 1: | |
58 dmVerbosity++; | |
59 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
|
60 |
648
7303c43bf347
Fix fontconv option handling again.
Matti Hamalainen <ccr@tnsp.org>
parents:
647
diff
changeset
|
61 case 2: |
160 | 62 { |
2075
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
63 unsigned int fontW, fontH; |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
64 char *sep = strchr(optArg, ':'); |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
65 if (sep != NULL) |
160 | 66 { |
2075
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
67 char *tmpStr = dm_strndup(optArg, sep - optArg); |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
68 |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
69 if (!dmGetIntVal(tmpStr, &fontW, NULL) || |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
70 !dmGetIntVal(sep + 1, &fontH, NULL)) |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
71 { |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
72 dmErrorMsg("Invalid font width or height value ('%s')\n", |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
73 optArg); |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
74 |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
75 dmFree(tmpStr); |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
76 return FALSE; |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
77 } |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
78 |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
79 dmFree(tmpStr); |
160 | 80 } |
2075
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
81 else |
160 | 82 { |
2075
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
83 if (!dmGetIntVal(optArg, &fontW, NULL)) |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
84 { |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
85 dmErrorMsg("Invalid font size value ('%s')\n", |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
86 optArg); |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
87 |
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 fontH = fontW; |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
91 } |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
92 |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
93 if (fontW < DMFONT_MIN_WIDTH || fontW > DMFONT_MAX_WIDTH || |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
94 fontH < DMFONT_MIN_HEIGHT || fontH > DMFONT_MAX_HEIGHT) |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
95 { |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
96 dmErrorMsg("Invalid font dimensions, must be %d < W %d, %d < H < %d.\n", |
160 | 97 DMFONT_MIN_WIDTH , DMFONT_MAX_WIDTH, |
98 DMFONT_MIN_HEIGHT , DMFONT_MAX_HEIGHT); | |
99 return FALSE; | |
100 } | |
2075
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
101 optSplitWidth = fontW; |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
102 optSplitHeight = fontH; |
160 | 103 } |
104 break; | |
105 | |
1357
370c40e0847f
Remove useless -o option from fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
1272
diff
changeset
|
106 case 3: |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
107 { |
867 | 108 unsigned int colR, colG, colB, colA = 100; |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
109 if (optArg[0] == '#' || optArg[0] == '$') optArg++; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
110 else |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
111 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
|
112 |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
113 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
|
114 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
|
115 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
116 dmErrorMsg("Invalid RGB hex representation '%s'.\n", |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
117 optArg); |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
118 return FALSE; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
119 } |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
120 |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
121 optColor.r = colR; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
122 optColor.g = colG; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
123 optColor.b = colB; |
1557
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1357
diff
changeset
|
124 optColor.a = colA; |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
125 } |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
126 break; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
127 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
128 case 4: |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
129 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
|
130 { |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
131 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
|
132 optArg); |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
133 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
|
134 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
135 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
|
136 { |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
137 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
|
138 optBPP); |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
139 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
|
140 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
141 break; |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
142 |
160 | 143 default: |
2183
e3f0eaf23f4f
Change the error message for unimplemented option argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
2075
diff
changeset
|
144 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg); |
160 | 145 return FALSE; |
146 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
147 |
160 | 148 return TRUE; |
149 } | |
150 | |
151 | |
152 BOOL argHandleFile(char *currArg) | |
153 { | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
154 if (optInFilename == NULL) |
160 | 155 optInFilename = currArg; |
156 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
|
157 if (optOutFilename == NULL) |
645
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
158 optOutFilename = currArg; |
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
159 else |
160 | 160 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
161 dmErrorMsg("Too many filename arguments, '%s'\n", currArg); |
160 | 162 return FALSE; |
163 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
164 |
160 | 165 return TRUE; |
166 } | |
167 | |
168 | |
169 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont) | |
170 { | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
171 int nglyph, xglyphs, yglyphs; |
160 | 172 DMBitmapFont *font; |
173 | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
174 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
|
175 image->h < height || height < 2) |
160 | 176 return DMERR_INVALID_ARGS; |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
177 |
160 | 178 xglyphs = image->w / width; |
179 yglyphs = image->h / height; | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
180 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
181 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
|
182 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
|
183 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
|
184 width, height, image->format->BitsPerPixel)) == NULL) |
160 | 185 return DMERR_MALLOC; |
186 | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
187 dmMsg(1, "%d x %d split as %d x %d blocks => %d x %d = %d glyphs, bpp=%d.\n", |
160 | 188 image->w, image->h, |
189 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
|
190 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
|
191 xglyphs * yglyphs, image->format->BitsPerPixel); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
192 |
160 | 193 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
|
194 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
|
195 for (int xc = 0; xc < xglyphs; xc++) |
160 | 196 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
197 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
|
198 SDL_Rect src, dst; |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
199 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
200 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
|
201 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
|
202 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
|
203 dst.h = src.h = height; |
160 | 204 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
205 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
|
206 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
|
207 glyph->index = nglyph; |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
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 SDL_BlitSurface(image, &src, font->glyphs, &dst); |
160 | 210 } |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
211 |
160 | 212 *pfont = font; |
213 return DMERR_OK; | |
214 } | |
215 | |
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 int dmSaveBitmapFont(DMResource *fp, DMBitmapFont *font) |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
218 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
219 if (font == NULL) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
220 return DMERR_NULLPTR; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
221 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
222 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
|
223 font->maxglyph > DMFONT_MAX_GLYPHS || |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
224 font->width > DMFONT_MAX_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
225 font->height > DMFONT_MAX_HEIGHT || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
226 font->width < DMFONT_MIN_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
227 font->height < DMFONT_MIN_HEIGHT) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
228 return DMERR_INVALID_DATA; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
229 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
230 // 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
|
231 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
|
232 !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
|
233 !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
|
234 !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
|
235 !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
|
236 !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
|
237 !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
|
238 return DMERR_FWRITE; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
239 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
240 // 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
|
241 for (int index = 0; index < font->maxglyph; index++) |
874 | 242 { |
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 DMBitmapGlyph *glyph = &font->glyphMap[index]; |
1967
8a98d1517460
Fix glyph index saving .. sigh.
Matti Hamalainen <ccr@tnsp.org>
parents:
1965
diff
changeset
|
244 if (glyph->index >= 0) |
874 | 245 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
246 Uint8 *pixels = font->glyphs->pixels + font->gsize * glyph->index; |
874 | 247 |
248 // 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
|
249 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
|
250 !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
|
251 !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
|
252 return DMERR_FWRITE; |
874 | 253 |
254 // 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
|
255 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
|
256 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
257 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
|
258 glyph->width, fp) != (size_t) glyph->width) |
874 | 259 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
|
260 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
261 pixels += font->glyphs->pitch; |
178
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 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
281 dmInitProg("fontconv", "Bitmap font converter", "0.4", NULL, NULL); |
160 | 282 |
283 // Parse arguments | |
284 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
|
285 argHandleOpt, argHandleFile, OPTH_BAILOUT)) |
160 | 286 exit(1); |
287 | |
288 // 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
|
289 if (optInFilename == NULL || optOutFilename == NULL) |
160 | 290 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
291 dmErrorMsg("Input or output file not specified!\n"); |
160 | 292 return 1; |
293 } | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
294 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
295 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
296 if (TTF_Init() < 0) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
297 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
298 dmErrorMsg("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); |
2259 | 299 goto exit; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
300 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
301 initTTF = TRUE; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
302 #endif |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
303 |
160 | 304 // Open the source file |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
305 if ((res = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) |
160 | 306 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
307 dmErrorMsg("Error opening input file '%s', %d: %s\n", |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
655
diff
changeset
|
308 optInFilename, res, dmErrorStr(res)); |
2259 | 309 goto exit; |
160 | 310 } |
311 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
312 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
313 if ((res = dmLoadBitmapFont(inFile, &font)) == DMERR_OK) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
314 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
315 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
|
316 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
|
317 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
318 else |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
319 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
|
320 { |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
321 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
|
322 dmErrorStr(res)); |
2259 | 323 goto exit; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
324 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
325 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
326 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
|
327 if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth - 1)) != NULL) |
160 | 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 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
|
330 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
331 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
|
332 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
|
333 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
334 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
|
335 gmin, gmax); |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
336 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
337 TTF_SetFontStyle(ttf, TTF_STYLE_NORMAL); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
338 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
339 // 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
|
340 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
|
341 optSplitWidth - 6, optSplitHeight + 2, optBPP)) == NULL) |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
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 dmErrorMsg("Could not allocate bitmap font!\n"); |
2259 | 344 goto exit; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
345 } |
160 | 346 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
347 // 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
|
348 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
|
349 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
350 SDL_Surface *tmp; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
351 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
|
352 str[0] = nglyph; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
353 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
|
354 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
355 // 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
|
356 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
|
357 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
|
358 else |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
359 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
|
360 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
361 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
|
362 { |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
363 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
|
364 int minx, miny, advance; |
1960
b99e04c356ec
Some dead code was accidentally left in. Delete it.
Matti Hamalainen <ccr@tnsp.org>
parents:
1957
diff
changeset
|
365 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
|
366 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
367 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
|
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 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
|
370 nglyph, nglyph); |
2259 | 371 goto exit; |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
372 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
373 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
374 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
|
375 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
|
376 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
|
377 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
|
378 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
379 // 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
|
380 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
|
381 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
|
382 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
|
383 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
384 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
|
385 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
|
386 index++; |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
387 } |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
388 } |
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 #endif |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
391 else |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
392 { |
1203
2b48b7fe95bc
Use dmfreset() instead of dmfseek() to seek to stream start.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
393 dmfreset(inFile); |
160 | 394 |
395 if ((fontbmap = dmLoadImage(inFile)) == NULL) | |
396 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
397 dmErrorMsg("Could not load image file '%s'.\n", optInFilename); |
2259 | 398 goto exit; |
160 | 399 } |
400 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
401 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
|
402 fontbmap->w, fontbmap->h, fontbmap->format->BitsPerPixel, |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
403 optSplitWidth, optSplitHeight); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
404 |
160 | 405 if ((res = dmCreateBitmapFontFromImage(fontbmap, optSplitWidth, optSplitHeight, &font)) != DMERR_OK) |
406 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
407 dmErrorMsg("Could not create a font from image, %d: %s\n", |
160 | 408 res, dmErrorStr(res)); |
2259 | 409 goto exit; |
160 | 410 } |
411 } | |
412 | |
413 if (font == NULL) | |
414 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
415 dmErrorMsg("No font loaded.\n"); |
2259 | 416 goto exit; |
160 | 417 } |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
418 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
419 // 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
|
420 // 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
|
421 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
|
422 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
|
423 { |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
424 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
|
425 |
1968
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
426 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
|
427 { |
1968
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
428 font->nglyphs++; |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
429 if (glyph->width < DMFONT_MIN_WIDTH || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
430 glyph->height < DMFONT_MIN_HEIGHT || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
431 glyph->width > DMFONT_MAX_WIDTH || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
432 glyph->height > DMFONT_MAX_HEIGHT || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
433 glyph->width > font->width || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
434 glyph->height > font->height) |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
435 { |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
436 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
|
437 n, |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
438 glyph->width, glyph->height, |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
439 font->width, font->height); |
2259 | 440 goto exit; |
1968
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
441 } |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
442 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
443 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
444 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
445 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
|
446 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
|
447 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
|
448 font->glyphs->format->BitsPerPixel); |
160 | 449 |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
450 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
|
451 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
452 dmErrorMsg("Error creating file '%s', %d: %s\n", |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
655
diff
changeset
|
453 optInFilename, res, dmErrorStr(res)); |
2259 | 454 goto exit; |
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
|
455 } |
160 | 456 |
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
|
457 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
|
458 dmf_close(outFile); |
160 | 459 |
460 if (res != DMERR_OK) | |
461 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
462 dmErrorMsg("Error saving font, %d: %s\n", |
160 | 463 res, dmErrorStr(res)); |
464 } | |
465 | |
2259 | 466 exit: |
467 // Cleanup | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
468 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
469 if (initTTF) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
470 TTF_Quit(); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
471 #endif |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
472 |
160 | 473 dmf_close(inFile); |
474 dmFreeBitmapFont(font); | |
2259 | 475 if (fontbmap != NULL) |
476 SDL_FreeSurface(fontbmap); | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
477 |
160 | 478 return 0; |
479 } |