Mercurial > hg > dmlib
annotate tools/fontconv.c @ 2408:60e119262c67
Option index re-ordering cleanup work.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 13 Jan 2020 21:21:25 +0200 |
parents | b7cd5dd0b82e |
children | bc05bcfc4598 |
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 { |
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 }, |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
28 { 2, 'v', "verbose" , "Be more verbose", OPT_NONE }, |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
29 |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
30 { 10, 's', "size" , "Set glyph dimensions (-s W:H or -s N) for image->font conversion", OPT_ARGREQ }, |
1272 | 31 #ifdef DM_GFX_TTF_TEXT |
2408
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
32 { 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
|
33 { 14, 'b', "bpp" , "Render font in 8 or 32 bits per pixel (default 32)", OPT_ARGREQ }, |
1272 | 34 #endif |
160 | 35 }; |
36 | |
37 const int optListN = sizeof(optList) / sizeof(optList[0]); | |
38 | |
39 | |
2408
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
40 void argShowHelp() |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
41 { |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
42 dmPrintBanner(stdout, dmProgName, |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
43 "[options] <sourcefile.(ttf|fnt|dmf|png)> <outputfile.dmf>"); |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
44 |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
45 dmArgsPrintHelp(stdout, optList, optListN, 0, 80 - 2); |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
46 printf( |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
47 "\n" |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
48 "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
|
49 "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
|
50 "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
|
51 } |
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 |
160 | 54 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) |
55 { | |
56 switch (optN) | |
57 { | |
58 case 0: | |
2408
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
59 argShowHelp(); |
160 | 60 exit(0); |
61 break; | |
62 | |
63 case 1: | |
64 dmVerbosity++; | |
65 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
|
66 |
2408
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
67 case 10: |
160 | 68 { |
2075
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
69 unsigned int fontW, fontH; |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
70 char *sep = strchr(optArg, ':'); |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
71 if (sep != NULL) |
160 | 72 { |
2075
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
73 char *tmpStr = dm_strndup(optArg, sep - 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 if (!dmGetIntVal(tmpStr, &fontW, NULL) || |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
76 !dmGetIntVal(sep + 1, &fontH, NULL)) |
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 dmErrorMsg("Invalid font width or height value ('%s')\n", |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
79 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 dmFree(tmpStr); |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
82 return FALSE; |
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 |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
85 dmFree(tmpStr); |
160 | 86 } |
2075
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
87 else |
160 | 88 { |
2075
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
89 if (!dmGetIntVal(optArg, &fontW, NULL)) |
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 dmErrorMsg("Invalid font size value ('%s')\n", |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
92 optArg); |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
93 |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
94 return FALSE; |
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 fontH = fontW; |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
97 } |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
98 |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
99 if (fontW < DMFONT_MIN_WIDTH || fontW > DMFONT_MAX_WIDTH || |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
100 fontH < DMFONT_MIN_HEIGHT || fontH > DMFONT_MAX_HEIGHT) |
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 dmErrorMsg("Invalid font dimensions, must be %d < W %d, %d < H < %d.\n", |
160 | 103 DMFONT_MIN_WIDTH , DMFONT_MAX_WIDTH, |
104 DMFONT_MIN_HEIGHT , DMFONT_MAX_HEIGHT); | |
105 return FALSE; | |
106 } | |
2075
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
107 optSplitWidth = fontW; |
2ca6a13b091b
Improve fontconv '-s' option parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
2007
diff
changeset
|
108 optSplitHeight = fontH; |
160 | 109 } |
110 break; | |
111 | |
2408
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
112 case 12: |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
113 { |
867 | 114 unsigned int colR, colG, colB, colA = 100; |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
115 if (optArg[0] == '#' || optArg[0] == '$') optArg++; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
116 else |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
117 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
|
118 |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
119 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
|
120 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
|
121 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
122 dmErrorMsg("Invalid RGB hex representation '%s'.\n", |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
123 optArg); |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
124 return FALSE; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
125 } |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
126 |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
127 optColor.r = colR; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
128 optColor.g = colG; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
129 optColor.b = colB; |
1557
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1357
diff
changeset
|
130 optColor.a = colA; |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
131 } |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
132 break; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
133 |
2408
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
134 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
|
135 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
|
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 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
|
138 optArg); |
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 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
|
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 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
|
144 optBPP); |
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 break; |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
148 |
160 | 149 default: |
2183
e3f0eaf23f4f
Change the error message for unimplemented option argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
2075
diff
changeset
|
150 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg); |
160 | 151 return FALSE; |
152 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
153 |
160 | 154 return TRUE; |
155 } | |
156 | |
157 | |
158 BOOL argHandleFile(char *currArg) | |
159 { | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
160 if (optInFilename == NULL) |
160 | 161 optInFilename = currArg; |
162 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
|
163 if (optOutFilename == NULL) |
645
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
164 optOutFilename = currArg; |
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
165 else |
160 | 166 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
167 dmErrorMsg("Too many filename arguments, '%s'\n", currArg); |
160 | 168 return FALSE; |
169 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
170 |
160 | 171 return TRUE; |
172 } | |
173 | |
174 | |
175 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont) | |
176 { | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
177 int nglyph, xglyphs, yglyphs; |
160 | 178 DMBitmapFont *font; |
179 | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
180 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
|
181 image->h < height || height < 2) |
160 | 182 return DMERR_INVALID_ARGS; |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
183 |
160 | 184 xglyphs = image->w / width; |
185 yglyphs = image->h / height; | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
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 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
|
188 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
|
189 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
|
190 width, height, image->format->BitsPerPixel)) == NULL) |
160 | 191 return DMERR_MALLOC; |
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 dmMsg(1, "%d x %d split as %d x %d blocks => %d x %d = %d glyphs, bpp=%d.\n", |
160 | 194 image->w, image->h, |
195 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
|
196 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
|
197 xglyphs * yglyphs, image->format->BitsPerPixel); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
198 |
160 | 199 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
|
200 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
|
201 for (int xc = 0; xc < xglyphs; xc++) |
160 | 202 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
203 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
|
204 SDL_Rect src, dst; |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
205 |
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 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
|
207 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
|
208 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
|
209 dst.h = src.h = height; |
160 | 210 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
211 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
|
212 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
|
213 glyph->index = nglyph; |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
214 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
215 SDL_BlitSurface(image, &src, font->glyphs, &dst); |
160 | 216 } |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
217 |
160 | 218 *pfont = font; |
219 return DMERR_OK; | |
220 } | |
221 | |
222 | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
223 int dmSaveBitmapFont(DMResource *fp, DMBitmapFont *font) |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
224 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
225 if (font == NULL) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
226 return DMERR_NULLPTR; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
227 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
228 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
|
229 font->maxglyph > DMFONT_MAX_GLYPHS || |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
230 font->width > DMFONT_MAX_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
231 font->height > DMFONT_MAX_HEIGHT || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
232 font->width < DMFONT_MIN_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
233 font->height < DMFONT_MIN_HEIGHT) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
234 return DMERR_INVALID_DATA; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
235 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
236 // 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
|
237 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
|
238 !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
|
239 !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
|
240 !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
|
241 !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
|
242 !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
|
243 !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
|
244 return DMERR_FWRITE; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
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 // 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
|
247 for (int index = 0; index < font->maxglyph; index++) |
874 | 248 { |
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 DMBitmapGlyph *glyph = &font->glyphMap[index]; |
1967
8a98d1517460
Fix glyph index saving .. sigh.
Matti Hamalainen <ccr@tnsp.org>
parents:
1965
diff
changeset
|
250 if (glyph->index >= 0) |
874 | 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 Uint8 *pixels = font->glyphs->pixels + font->gsize * glyph->index; |
874 | 253 |
254 // 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
|
255 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
|
256 !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
|
257 !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
|
258 return DMERR_FWRITE; |
874 | 259 |
260 // 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
|
261 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
|
262 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
263 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
|
264 glyph->width, fp) != (size_t) glyph->width) |
874 | 265 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
|
266 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
267 pixels += font->glyphs->pitch; |
178
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 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
270 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
271 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
272 return DMERR_OK; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
273 } |
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 |
160 | 276 int main(int argc, char *argv[]) |
277 { | |
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
|
278 DMResource *inFile = NULL, *outFile = NULL; |
160 | 279 DMBitmapFont *font = NULL; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
280 SDL_Surface *fontbmap = NULL; |
160 | 281 int res; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
282 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
283 BOOL initTTF = FALSE; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
284 TTF_Font *ttf = NULL; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
285 #endif |
160 | 286 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
287 dmInitProg("fontconv", "Bitmap font converter", "0.4", NULL, NULL); |
160 | 288 |
289 // Parse arguments | |
290 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
|
291 argHandleOpt, argHandleFile, OPTH_BAILOUT)) |
160 | 292 exit(1); |
293 | |
294 // 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
|
295 if (optInFilename == NULL || optOutFilename == NULL) |
160 | 296 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
297 dmErrorMsg("Input or output file not specified!\n"); |
160 | 298 return 1; |
299 } | |
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 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
302 if (TTF_Init() < 0) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
303 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
304 dmErrorMsg("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); |
2259 | 305 goto exit; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
306 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
307 initTTF = TRUE; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
308 #endif |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
309 |
160 | 310 // Open the source file |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
311 if ((res = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) |
160 | 312 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
313 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
|
314 optInFilename, res, dmErrorStr(res)); |
2259 | 315 goto exit; |
160 | 316 } |
317 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
318 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
319 if ((res = dmLoadBitmapFont(inFile, &font)) == DMERR_OK) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
320 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
321 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
|
322 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
|
323 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
324 else |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
325 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
|
326 { |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
327 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
|
328 dmErrorStr(res)); |
2259 | 329 goto exit; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
330 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
331 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
332 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
|
333 if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth - 1)) != NULL) |
160 | 334 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
335 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
|
336 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
337 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
|
338 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
|
339 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
340 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
|
341 gmin, gmax); |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
342 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
343 TTF_SetFontStyle(ttf, TTF_STYLE_NORMAL); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
344 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
345 // 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
|
346 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
|
347 optSplitWidth - 6, optSplitHeight + 2, optBPP)) == NULL) |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
348 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
349 dmErrorMsg("Could not allocate bitmap font!\n"); |
2259 | 350 goto exit; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
351 } |
160 | 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 // 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
|
354 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
|
355 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
356 SDL_Surface *tmp; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
357 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
|
358 str[0] = nglyph; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
359 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
|
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 // 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
|
362 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
|
363 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
|
364 else |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
365 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
|
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 (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
|
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 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
|
370 int minx, miny, advance; |
1960
b99e04c356ec
Some dead code was accidentally left in. Delete it.
Matti Hamalainen <ccr@tnsp.org>
parents:
1957
diff
changeset
|
371 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
|
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 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
|
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 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
|
376 nglyph, nglyph); |
2259 | 377 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
|
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 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
380 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
|
381 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
|
382 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
|
383 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
|
384 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
385 // 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
|
386 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
|
387 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
|
388 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
|
389 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
390 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
|
391 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
|
392 index++; |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
393 } |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
394 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
395 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
396 #endif |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
397 else |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
398 { |
1203
2b48b7fe95bc
Use dmfreset() instead of dmfseek() to seek to stream start.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
399 dmfreset(inFile); |
160 | 400 |
401 if ((fontbmap = dmLoadImage(inFile)) == NULL) | |
402 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
403 dmErrorMsg("Could not load image file '%s'.\n", optInFilename); |
2259 | 404 goto exit; |
160 | 405 } |
406 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
407 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
|
408 fontbmap->w, fontbmap->h, fontbmap->format->BitsPerPixel, |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
409 optSplitWidth, optSplitHeight); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
410 |
160 | 411 if ((res = dmCreateBitmapFontFromImage(fontbmap, optSplitWidth, optSplitHeight, &font)) != DMERR_OK) |
412 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
413 dmErrorMsg("Could not create a font from image, %d: %s\n", |
160 | 414 res, dmErrorStr(res)); |
2259 | 415 goto exit; |
160 | 416 } |
417 } | |
418 | |
419 if (font == NULL) | |
420 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
421 dmErrorMsg("No font loaded.\n"); |
2259 | 422 goto exit; |
160 | 423 } |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
424 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
425 // 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
|
426 // 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
|
427 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
|
428 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
|
429 { |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
430 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
|
431 |
1968
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
432 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
|
433 { |
1968
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
434 font->nglyphs++; |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
435 if (glyph->width < DMFONT_MIN_WIDTH || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
436 glyph->height < DMFONT_MIN_HEIGHT || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
437 glyph->width > DMFONT_MAX_WIDTH || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
438 glyph->height > DMFONT_MAX_HEIGHT || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
439 glyph->width > font->width || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
440 glyph->height > font->height) |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
441 { |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
442 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
|
443 n, |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
444 glyph->width, glyph->height, |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
445 font->width, font->height); |
2259 | 446 goto exit; |
1968
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
447 } |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
448 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
449 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
450 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
451 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
|
452 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
|
453 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
|
454 font->glyphs->format->BitsPerPixel); |
160 | 455 |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
456 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
|
457 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
458 dmErrorMsg("Error creating file '%s', %d: %s\n", |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
655
diff
changeset
|
459 optInFilename, res, dmErrorStr(res)); |
2259 | 460 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
|
461 } |
160 | 462 |
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
|
463 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
|
464 dmf_close(outFile); |
160 | 465 |
466 if (res != DMERR_OK) | |
467 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
468 dmErrorMsg("Error saving font, %d: %s\n", |
160 | 469 res, dmErrorStr(res)); |
470 } | |
471 | |
2259 | 472 exit: |
473 // Cleanup | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
474 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
475 if (initTTF) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
476 TTF_Quit(); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
477 #endif |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
478 |
160 | 479 dmf_close(inFile); |
480 dmFreeBitmapFont(font); | |
2259 | 481 if (fontbmap != NULL) |
482 SDL_FreeSurface(fontbmap); | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
483 |
160 | 484 return 0; |
485 } |