annotate fontconv.c @ 579:f87446a81887

Remove C source output mode, it is useless.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 12 Apr 2013 04:52:56 +0300
parents d6c184800384
children 151747a24f57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * fontconv - Convert bitmap fonts
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * (C) Copyright 2012 Tecnic Software productions (TNSP)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include <stdio.h>
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include <errno.h>
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "dmlib.h"
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include "dmargs.h"
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include "dmfile.h"
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #include "dmimage.h"
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 #include "dmtext.h"
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
15 #include "dmresw.h"
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
579
f87446a81887 Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents: 362
diff changeset
17 char *optInFilename = NULL, *optOutFilename = NULL;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18
579
f87446a81887 Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents: 362
diff changeset
19 int optSplitWidth = 8,
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 optSplitHeight = 8;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 DMOptArg optList[] =
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 { 0, '?', "help", "Show this help", OPT_NONE },
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 { 2, 'o', "output", "Output file (default stdout)", OPT_ARGREQ },
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 { 3, 's', "size", "Set glyph dimensions (-s WxH) for image->font conversion", OPT_ARGREQ },
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 };
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 const int optListN = sizeof(optList) / sizeof(optList[0]);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 switch (optN)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 case 0:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 dmPrintBanner(stdout, dmProgName,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 "[options] [sourcefile]");
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 dmArgsPrintHelp(stdout, optList, optListN);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 exit(0);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 break;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 case 1:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 dmVerbosity++;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 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
49
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 case 2:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 optOutFilename = optArg;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 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
53
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 case 3:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 int w, h;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 if (sscanf(optArg, "%dx%d", &w, &h) != 2)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 dmError("Invalid argument for -s option, '%s'.\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 optArg);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 return FALSE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 if (w < DMFONT_MIN_WIDTH || w > DMFONT_MAX_WIDTH ||
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 h < DMFONT_MIN_HEIGHT || h > DMFONT_MAX_HEIGHT)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 dmError("Invalid dimensions, must be %d < W %d, %d < H < %d.\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 DMFONT_MIN_WIDTH , DMFONT_MAX_WIDTH,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 DMFONT_MIN_HEIGHT , DMFONT_MAX_HEIGHT);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 return FALSE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 optSplitWidth = w;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 optSplitHeight = h;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 break;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 default:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 dmError("Unknown argument '%s'.\n", currArg);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 return FALSE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 return TRUE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 BOOL argHandleFile(char *currArg)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 if (!optInFilename)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 optInFilename = currArg;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 else
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 dmError("Too many filename arguments, '%s'\n", currArg);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 return FALSE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 return TRUE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 int nglyph, xc, yc, xglyphs, yglyphs;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 DMBitmapFont *font;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 if (image->w < width || width < 4 || image->h < height || height < 4)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 return DMERR_INVALID_ARGS;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 xglyphs = image->w / width;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 yglyphs = image->h / height;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 if ((font = dmNewBitmapFont(xglyphs * yglyphs, width, height)) == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 return DMERR_MALLOC;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
113 dmMsg(1, "%d x %d split as %d x %d blocks => %d x %d = %d glyphs.\n",
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 image->w, image->h,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 width, height,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 xglyphs, yglyphs, xglyphs * yglyphs);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 nglyph = 0;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 for (yc = 0; yc < yglyphs; yc++)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 for (xc = 0; xc < xglyphs; xc++)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 SDL_Surface *glyph = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 image->format->BitsPerPixel,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 image->format->Rmask,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 image->format->Gmask,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 image->format->Bmask,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 image->format->Amask);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 if (glyph == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 dmFreeBitmapFont(font);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 return DMERR_MALLOC;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 SDL_Rect r;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 r.x = xc * width;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 r.y = yc * height;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 r.w = width;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 r.h = height;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 SDL_BlitSurface(image, &r, glyph, NULL);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 font->glyphs[nglyph++] = glyph;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 *pfont = font;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 return DMERR_OK;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
151 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
152 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
153 int maxglyph, nglyphs, n;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
154 if (font == NULL)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
155 return DMERR_NULLPTR;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
156
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
157 if (font->nglyphs > DMFONT_MAX_GLYPHS ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
158 font->width > DMFONT_MAX_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
159 font->height > DMFONT_MAX_HEIGHT ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
160 font->width < DMFONT_MIN_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
161 font->height < DMFONT_MIN_HEIGHT)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
162 return DMERR_INVALID_DATA;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
163
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
164 // Count number of actually existing glyphs
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
165 for (maxglyph = nglyphs = n = 0; n < font->nglyphs; n++)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
166 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
167 SDL_Surface *glyph = font->glyphs[n];
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
168 if (glyph != NULL)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
169 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
170 maxglyph = n;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
171 if (glyph->w < DMFONT_MIN_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
172 glyph->h < DMFONT_MIN_HEIGHT ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
173 glyph->w > DMFONT_MAX_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
174 glyph->h > DMFONT_MAX_HEIGHT)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
175 continue;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
176 nglyphs++;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
177 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
178 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
179
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
180 // Write the DMFONT header
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
181 if (!dmf_write_str(res, (Uint8 *) DMFONT_MAGIC, 6))
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
182 return DMERR_FWRITE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
183
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
184 dmf_write_le16(res, DMFONT_VERSION);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
185 dmf_write_le16(res, nglyphs);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
186 dmf_write_le16(res, maxglyph + 1);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
187 dmfputc(font->width, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
188 dmfputc(font->height, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
189
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
190 if (nglyphs > 0)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
191 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
192 int i;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
193 SDL_Surface *glyph = font->glyphs[maxglyph];
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
194
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
195 // If there are actual glyphs stored, save this
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
196 dmfputc(glyph->format->BitsPerPixel, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
197 dmf_write_le32(res, glyph->format->Rmask);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
198 dmf_write_le32(res, glyph->format->Gmask);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
199 dmf_write_le32(res, glyph->format->Bmask);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
200 dmf_write_le32(res, glyph->format->Amask);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
201
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
202 for (i = 0; i < font->nglyphs; i++)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
203 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
204 glyph = font->glyphs[i];
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
205 if (glyph != NULL)
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 int y;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
208 Uint8 *pixels = glyph->pixels;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
209
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
210 if (glyph->w < DMFONT_MIN_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
211 glyph->h < DMFONT_MIN_HEIGHT ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
212 glyph->w > DMFONT_MAX_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
213 glyph->h > DMFONT_MAX_HEIGHT)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
214 continue;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
215
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
216 // Each glyph has its table index and w/h stored
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
217 dmf_write_le16(res, i);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
218 dmfputc(glyph->w, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
219 dmfputc(glyph->h, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
220
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
221 // Write the pixel data
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
222 for (y = 0; y < glyph->h; y++)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
223 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
224 if (dmfwrite(pixels, glyph->format->BytesPerPixel, glyph->w, res) != (size_t) glyph->w)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
225 return DMERR_FWRITE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
226 pixels += glyph->pitch;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
227 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
228 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
229 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
230 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
231
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
232 return DMERR_OK;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
233 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
234
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
235
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 int main(int argc, char *argv[])
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 {
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
238 DMResource *inFile = NULL, *outFile = NULL;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 DMBitmapFont *font = NULL;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
240 SDL_Surface *fontbmap = NULL;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 int res;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
242 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
243 BOOL initTTF = FALSE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
244 TTF_Font *ttf = NULL;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
245 #endif
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 dmInitProg("fontconv", "Bitmap font converter", "0.2", NULL, NULL);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 dmVerbosity = 1;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 // Parse arguments
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 if (!dmArgsProcess(argc, argv, optList, optListN,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 argHandleOpt, argHandleFile, TRUE))
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 exit(1);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 // Check arguments
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 if (!optInFilename)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 dmError("Input or output file not specified!\n");
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 return 1;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 }
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
261
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
262 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
263 if (TTF_Init() < 0)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
264 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
265 dmError("Could not initialize FreeType/TTF: %s\n", SDL_GetError());
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
266 goto error_exit;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
267 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
268 initTTF = TRUE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
269 #endif
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 // Open the source file
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 if ((inFile = dmf_create_stdio(optInFilename, "rb")) == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 dmError("Error opening input file '%s', %d: %s\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275 optInFilename, errno, strerror(errno));
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 return 1;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
279
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
280 if ((res = dmLoadBitmapFont(inFile, &font)) == DMERR_OK)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
281 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
282 dmMsg(1, "Input is a TSFONT/DMFONT font file.\n");
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
283 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
284 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
285 else
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
286 if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth)) != NULL)
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 {
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
288 int i;
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
289 SDL_Color col = { 255, 255, 255, 100 };
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
290 dmMsg(1, "Input is a TTF TrueType font, rendering at %d x %d.\n",
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
291 optSplitWidth, optSplitHeight);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
292
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
293 TTF_SetFontStyle(ttf, TTF_STYLE_NORMAL);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
294
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
295 if ((font = dmNewBitmapFont(256, optSplitWidth - 1, optSplitHeight+4)) == NULL)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
296 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
297 goto error_exit;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
298 }
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
300 for (i = 0; i < 255; i++)
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 char str[2];
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
303 str[0] = i;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
304 str[1] = 0;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
305 font->glyphs[i] = TTF_RenderText_Blended(ttf, str, col);
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 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
308 #endif
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
309 else
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
310 {
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 dmfseek(inFile, 0L, SEEK_SET);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 if ((fontbmap = dmLoadImage(inFile)) == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 dmError("Could not load image file '%s'.\n", optInFilename);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 goto error_exit;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
319 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
320 fontbmap->w, fontbmap->h, fontbmap->format->BitsPerPixel,
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
321 optSplitWidth, optSplitHeight);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
322
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 if ((res = dmCreateBitmapFontFromImage(fontbmap, optSplitWidth, optSplitHeight, &font)) != DMERR_OK)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325 dmError("Could not create a font from image, %d: %s\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 res, dmErrorStr(res));
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 goto error_exit;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 if (font == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 dmError("No font loaded.\n");
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 goto error_exit;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336
579
f87446a81887 Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents: 362
diff changeset
337 dmMsg(1, "Outputting a DMFONT format bitmap font.\n");
f87446a81887 Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents: 362
diff changeset
338 if (optOutFilename == NULL)
f87446a81887 Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents: 362
diff changeset
339 outFile = dmf_create_stdio_stream(stdout);
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
340 else
579
f87446a81887 Remove C source output mode, it is useless.
Matti Hamalainen <ccr@tnsp.org>
parents: 362
diff changeset
341 outFile = dmf_create_stdio(optOutFilename, "wb");
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342
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
343 if (outFile == NULL)
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
344 {
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
345 dmError("Error creating file '%s', %d: %s\n",
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
346 optInFilename, errno, strerror(errno));
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
347 goto error_exit;
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
348 }
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349
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
350 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
351 dmf_close(outFile);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 if (res != DMERR_OK)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 dmError("Error saving font, %d: %s\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 res, dmErrorStr(res));
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 error_exit:
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
360
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
361 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
362 if (initTTF)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
363 TTF_Quit();
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
364 #endif
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 dmf_close(inFile);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 dmFreeBitmapFont(font);
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
368 SDL_FreeSurface(fontbmap);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 return 0;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 }