Mercurial > hg > dmlib
annotate tools/fontconv.c @ 2007:a8a942c25df3
Lessen the default verbosity level of fontconv.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 07 Jul 2018 02:58:49 +0300 |
parents | 5f9e8dd62c70 |
children | 2ca6a13b091b |
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 }, | |
1357
370c40e0847f
Remove useless -o option from fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
1272
diff
changeset
|
29 { 2, 's', "size", "Set glyph dimensions (-s W:H) 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 { |
63 int w, h; | |
885
ee46d039c45d
Change fontconv -s option syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
874
diff
changeset
|
64 if (sscanf(optArg, "%d:%d", &w, &h) != 2) |
160 | 65 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
66 dmErrorMsg("Invalid argument for -s option, '%s'.\n", |
160 | 67 optArg); |
68 return FALSE; | |
69 } | |
70 if (w < DMFONT_MIN_WIDTH || w > DMFONT_MAX_WIDTH || | |
71 h < DMFONT_MIN_HEIGHT || h > DMFONT_MAX_HEIGHT) | |
72 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
73 dmErrorMsg("Invalid dimensions, must be %d < W %d, %d < H < %d.\n", |
160 | 74 DMFONT_MIN_WIDTH , DMFONT_MAX_WIDTH, |
75 DMFONT_MIN_HEIGHT , DMFONT_MAX_HEIGHT); | |
76 return FALSE; | |
77 } | |
78 optSplitWidth = w; | |
79 optSplitHeight = h; | |
80 } | |
81 break; | |
82 | |
1357
370c40e0847f
Remove useless -o option from fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
1272
diff
changeset
|
83 case 3: |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
84 { |
867 | 85 unsigned int colR, colG, colB, colA = 100; |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
86 if (optArg[0] == '#' || optArg[0] == '$') optArg++; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
87 else |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
88 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
|
89 |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
90 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
|
91 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
|
92 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
93 dmErrorMsg("Invalid RGB hex representation '%s'.\n", |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
94 optArg); |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
95 return FALSE; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
96 } |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
97 |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
98 optColor.r = colR; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
99 optColor.g = colG; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
100 optColor.b = colB; |
1557
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1357
diff
changeset
|
101 optColor.a = colA; |
633
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
102 } |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
103 break; |
151747a24f57
Add color setting option to fontconv.
Matti Hamalainen <ccr@tnsp.org>
parents:
579
diff
changeset
|
104 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
105 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
|
106 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
|
107 { |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
108 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
|
109 optArg); |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
110 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
|
111 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
112 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
|
113 { |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
114 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
|
115 optBPP); |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
116 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
|
117 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
118 break; |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
119 |
160 | 120 default: |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
121 dmErrorMsg("Unknown argument '%s'.\n", currArg); |
160 | 122 return FALSE; |
123 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
124 |
160 | 125 return TRUE; |
126 } | |
127 | |
128 | |
129 BOOL argHandleFile(char *currArg) | |
130 { | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
131 if (optInFilename == NULL) |
160 | 132 optInFilename = currArg; |
133 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
|
134 if (optOutFilename == NULL) |
645
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
135 optOutFilename = currArg; |
6dd155bbfc5c
Change semantics of fontconv utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
644
diff
changeset
|
136 else |
160 | 137 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
138 dmErrorMsg("Too many filename arguments, '%s'\n", currArg); |
160 | 139 return FALSE; |
140 } | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
141 |
160 | 142 return TRUE; |
143 } | |
144 | |
145 | |
146 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont) | |
147 { | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
148 int nglyph, xglyphs, yglyphs; |
160 | 149 DMBitmapFont *font; |
150 | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
151 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
|
152 image->h < height || height < 2) |
160 | 153 return DMERR_INVALID_ARGS; |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
154 |
160 | 155 xglyphs = image->w / width; |
156 yglyphs = image->h / height; | |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
157 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
158 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
|
159 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
|
160 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
|
161 width, height, image->format->BitsPerPixel)) == NULL) |
160 | 162 return DMERR_MALLOC; |
163 | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
164 dmMsg(1, "%d x %d split as %d x %d blocks => %d x %d = %d glyphs, bpp=%d.\n", |
160 | 165 image->w, image->h, |
166 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
|
167 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
|
168 xglyphs * yglyphs, image->format->BitsPerPixel); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
169 |
160 | 170 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
|
171 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
|
172 for (int xc = 0; xc < xglyphs; xc++) |
160 | 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 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
|
175 SDL_Rect src, dst; |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
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 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
|
178 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
|
179 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
|
180 dst.h = src.h = height; |
160 | 181 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
182 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
|
183 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
|
184 glyph->index = nglyph; |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
185 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
186 SDL_BlitSurface(image, &src, font->glyphs, &dst); |
160 | 187 } |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
188 |
160 | 189 *pfont = font; |
190 return DMERR_OK; | |
191 } | |
192 | |
193 | |
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 int dmSaveBitmapFont(DMResource *fp, DMBitmapFont *font) |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
195 { |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
196 if (font == NULL) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
197 return DMERR_NULLPTR; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
198 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
199 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
|
200 font->maxglyph > DMFONT_MAX_GLYPHS || |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
201 font->width > DMFONT_MAX_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
202 font->height > DMFONT_MAX_HEIGHT || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
203 font->width < DMFONT_MIN_WIDTH || |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
204 font->height < DMFONT_MIN_HEIGHT) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
205 return DMERR_INVALID_DATA; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
206 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
207 // 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
|
208 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
|
209 !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
|
210 !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
|
211 !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
|
212 !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
|
213 !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
|
214 !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
|
215 return DMERR_FWRITE; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
216 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
217 // 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
|
218 for (int index = 0; index < font->maxglyph; index++) |
874 | 219 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
220 DMBitmapGlyph *glyph = &font->glyphMap[index]; |
1967
8a98d1517460
Fix glyph index saving .. sigh.
Matti Hamalainen <ccr@tnsp.org>
parents:
1965
diff
changeset
|
221 if (glyph->index >= 0) |
874 | 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 Uint8 *pixels = font->glyphs->pixels + font->gsize * glyph->index; |
874 | 224 |
225 // 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
|
226 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
|
227 !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
|
228 !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
|
229 return DMERR_FWRITE; |
874 | 230 |
231 // 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
|
232 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
|
233 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
234 if (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
|
235 glyph->width, fp) != (size_t) glyph->width) |
874 | 236 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
|
237 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
238 pixels += font->glyphs->pitch; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
239 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
240 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
241 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
242 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
243 return DMERR_OK; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
244 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
245 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
246 |
160 | 247 int main(int argc, char *argv[]) |
248 { | |
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
|
249 DMResource *inFile = NULL, *outFile = NULL; |
160 | 250 DMBitmapFont *font = NULL; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
251 SDL_Surface *fontbmap = NULL; |
160 | 252 int res; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
253 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
254 BOOL initTTF = FALSE; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
255 TTF_Font *ttf = NULL; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
256 #endif |
160 | 257 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
258 dmInitProg("fontconv", "Bitmap font converter", "0.4", NULL, NULL); |
160 | 259 |
260 // Parse arguments | |
261 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
|
262 argHandleOpt, argHandleFile, OPTH_BAILOUT)) |
160 | 263 exit(1); |
264 | |
265 // 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
|
266 if (optInFilename == NULL || optOutFilename == NULL) |
160 | 267 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
268 dmErrorMsg("Input or output file not specified!\n"); |
160 | 269 return 1; |
270 } | |
178
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 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
273 if (TTF_Init() < 0) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
274 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
275 dmErrorMsg("Could not initialize FreeType/TTF: %s\n", SDL_GetError()); |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
276 goto out; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
277 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
278 initTTF = TRUE; |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
279 #endif |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
280 |
160 | 281 // Open the source file |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
282 if ((res = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) |
160 | 283 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
284 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
|
285 optInFilename, res, dmErrorStr(res)); |
160 | 286 return 1; |
287 } | |
288 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
289 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
290 if ((res = dmLoadBitmapFont(inFile, &font)) == DMERR_OK) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
291 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
292 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
|
293 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
|
294 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
295 else |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
296 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
|
297 { |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
298 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
|
299 dmErrorStr(res)); |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
300 goto out; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
301 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
302 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
303 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
|
304 if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth - 1)) != NULL) |
160 | 305 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
306 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
|
307 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
308 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
|
309 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
|
310 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
311 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
|
312 gmin, gmax); |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
313 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
314 TTF_SetFontStyle(ttf, TTF_STYLE_NORMAL); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
315 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
316 // 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
|
317 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
|
318 optSplitWidth - 6, optSplitHeight + 2, optBPP)) == NULL) |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
319 { |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
320 dmErrorMsg("Could not allocate bitmap font!\n"); |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
321 goto out; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
322 } |
160 | 323 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
324 // 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
|
325 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
|
326 { |
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 SDL_Surface *tmp; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
328 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
|
329 str[0] = nglyph; |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
330 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
|
331 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
332 // 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
|
333 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
|
334 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
|
335 else |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
336 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
|
337 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
338 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
|
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 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
|
341 int minx, miny, advance; |
1960
b99e04c356ec
Some dead code was accidentally left in. Delete it.
Matti Hamalainen <ccr@tnsp.org>
parents:
1957
diff
changeset
|
342 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
|
343 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
344 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
|
345 { |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
346 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
|
347 nglyph, nglyph); |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
348 goto out; |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
349 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
350 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
351 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
|
352 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
|
353 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
|
354 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
|
355 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
356 // 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
|
357 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
|
358 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
|
359 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
|
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 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
|
362 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
|
363 index++; |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
364 } |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
365 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
366 } |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
367 #endif |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
368 else |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
369 { |
1203
2b48b7fe95bc
Use dmfreset() instead of dmfseek() to seek to stream start.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
370 dmfreset(inFile); |
160 | 371 |
372 if ((fontbmap = dmLoadImage(inFile)) == NULL) | |
373 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
374 dmErrorMsg("Could not load image file '%s'.\n", optInFilename); |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
375 goto out; |
160 | 376 } |
377 | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
378 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
|
379 fontbmap->w, fontbmap->h, fontbmap->format->BitsPerPixel, |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
380 optSplitWidth, optSplitHeight); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
381 |
160 | 382 if ((res = dmCreateBitmapFontFromImage(fontbmap, optSplitWidth, optSplitHeight, &font)) != DMERR_OK) |
383 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
384 dmErrorMsg("Could not create a font from image, %d: %s\n", |
160 | 385 res, dmErrorStr(res)); |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
386 goto out; |
160 | 387 } |
388 } | |
389 | |
390 if (font == NULL) | |
391 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
392 dmErrorMsg("No font loaded.\n"); |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
393 goto out; |
160 | 394 } |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
395 |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
396 // 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
|
397 // 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
|
398 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
|
399 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
|
400 { |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
401 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
|
402 |
1968
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
403 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
|
404 { |
1968
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
405 font->nglyphs++; |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
406 if (glyph->width < DMFONT_MIN_WIDTH || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
407 glyph->height < DMFONT_MIN_HEIGHT || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
408 glyph->width > DMFONT_MAX_WIDTH || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
409 glyph->height > DMFONT_MAX_HEIGHT || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
410 glyph->width > font->width || |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
411 glyph->height > font->height) |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
412 { |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
413 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
|
414 n, |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
415 glyph->width, glyph->height, |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
416 font->width, font->height); |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
diff
changeset
|
417 goto out; |
868e39741d26
Only check glyphs that will be saved.
Matti Hamalainen <ccr@tnsp.org>
parents:
1967
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 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
420 } |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
421 |
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
422 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
|
423 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
|
424 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
|
425 font->glyphs->format->BitsPerPixel); |
160 | 426 |
1606
93d1050eac99
Rename dmf_create_*() functions to dmf_open_*().
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
427 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
|
428 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
429 dmErrorMsg("Error creating file '%s', %d: %s\n", |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
655
diff
changeset
|
430 optInFilename, res, dmErrorStr(res)); |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
431 goto out; |
209
17d4cc4c3ed1
Add ability to dump a DMFONT as a binary data in a C header file.
Matti Hamalainen <ccr@tnsp.org>
parents:
178
diff
changeset
|
432 } |
160 | 433 |
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
|
434 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
|
435 dmf_close(outFile); |
160 | 436 |
437 if (res != DMERR_OK) | |
438 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
885
diff
changeset
|
439 dmErrorMsg("Error saving font, %d: %s\n", |
160 | 440 res, dmErrorStr(res)); |
441 } | |
442 | |
1957
ef08af6887b7
Revamp the bitmap font system to use single SDL_Surface for the font
Matti Hamalainen <ccr@tnsp.org>
parents:
1881
diff
changeset
|
443 out: |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
444 |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
445 #ifdef DM_GFX_TTF_TEXT |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
446 if (initTTF) |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
447 TTF_Quit(); |
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
448 #endif |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
449 |
160 | 450 dmf_close(inFile); |
451 dmFreeBitmapFont(font); | |
178
63ff0fb944cd
Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents:
160
diff
changeset
|
452 SDL_FreeSurface(fontbmap); |
1235
5b8245e5f785
Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
453 |
160 | 454 return 0; |
455 } |