annotate fontconv.c @ 178:63ff0fb944cd

Implement TTF to bitmap font conversion (crude).
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 06 Oct 2012 12:21:31 +0300
parents 67d2cba58a87
children 17d4cc4c3ed1
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
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 enum
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 OFMT_DMFONT,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 };
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 char *optInFilename = NULL, *optOutFilename = NULL;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 int optOutFormat = OFMT_DMFONT,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 optSplitWidth = 8,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 optSplitHeight = 8;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 DMOptArg optList[] =
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 { 0, '?', "help", "Show this help", OPT_NONE },
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 { 2, 'o', "output", "Output file (default stdout)", OPT_ARGREQ },
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 { 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
34 };
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 const int optListN = sizeof(optList) / sizeof(optList[0]);
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
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 switch (optN)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 case 0:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 dmPrintBanner(stdout, dmProgName,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 "[options] [sourcefile]");
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 dmArgsPrintHelp(stdout, optList, optListN);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 exit(0);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 break;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 case 1:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 dmVerbosity++;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 break;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 case 2:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 optOutFilename = optArg;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 break;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 case 3:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 int w, h;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 if (sscanf(optArg, "%dx%d", &w, &h) != 2)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 dmError("Invalid argument for -s option, '%s'.\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 optArg);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 return FALSE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 if (w < DMFONT_MIN_WIDTH || w > DMFONT_MAX_WIDTH ||
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 h < DMFONT_MIN_HEIGHT || h > DMFONT_MAX_HEIGHT)
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 dmError("Invalid dimensions, must be %d < W %d, %d < H < %d.\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 DMFONT_MIN_WIDTH , DMFONT_MAX_WIDTH,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 DMFONT_MIN_HEIGHT , DMFONT_MAX_HEIGHT);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 return FALSE;
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 optSplitWidth = w;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 optSplitHeight = h;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 break;
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 default:
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 dmError("Unknown argument '%s'.\n", currArg);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 return FALSE;
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
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 return TRUE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 BOOL argHandleFile(char *currArg)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 if (!optInFilename)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 optInFilename = currArg;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 else
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 dmError("Too many filename arguments, '%s'\n", currArg);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 return FALSE;
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
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 return TRUE;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
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 int dmCreateBitmapFontFromImage(SDL_Surface *image, int width, int height, DMBitmapFont **pfont)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 int nglyph, xc, yc, xglyphs, yglyphs;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 DMBitmapFont *font;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 if (image->w < width || width < 4 || image->h < height || height < 4)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 return DMERR_INVALID_ARGS;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 xglyphs = image->w / width;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 yglyphs = image->h / height;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 if ((font = dmNewBitmapFont(xglyphs * yglyphs, width, height)) == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 return DMERR_MALLOC;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
118 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
119 image->w, image->h,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 width, height,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 xglyphs, yglyphs, xglyphs * yglyphs);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 nglyph = 0;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 for (yc = 0; yc < yglyphs; yc++)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 for (xc = 0; xc < xglyphs; xc++)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 SDL_Surface *glyph = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 image->format->BitsPerPixel,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 image->format->Rmask,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 image->format->Gmask,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 image->format->Bmask,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 image->format->Amask);
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 if (glyph == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 dmFreeBitmapFont(font);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 return DMERR_MALLOC;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 SDL_Rect r;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 r.x = xc * width;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 r.y = yc * height;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 r.w = width;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 r.h = height;
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 SDL_BlitSurface(image, &r, glyph, NULL);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 font->glyphs[nglyph++] = glyph;
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
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 *pfont = font;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 return DMERR_OK;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
156 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
157 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
158 int maxglyph, nglyphs, n;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
159 if (font == NULL)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
160 return DMERR_NULLPTR;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
161
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
162 if (font->nglyphs > DMFONT_MAX_GLYPHS ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
163 font->width > DMFONT_MAX_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
164 font->height > DMFONT_MAX_HEIGHT ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
165 font->width < DMFONT_MIN_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
166 font->height < DMFONT_MIN_HEIGHT)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
167 return DMERR_INVALID_DATA;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
168
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
169 // Count number of actually existing glyphs
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
170 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
171 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
172 SDL_Surface *glyph = font->glyphs[n];
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
173 if (glyph != NULL)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
174 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
175 maxglyph = n;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
176 if (glyph->w < DMFONT_MIN_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
177 glyph->h < DMFONT_MIN_HEIGHT ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
178 glyph->w > DMFONT_MAX_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
179 glyph->h > DMFONT_MAX_HEIGHT)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
180 continue;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
181 nglyphs++;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
182 }
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
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
185 // Write the DMFONT header
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
186 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
187 return DMERR_FWRITE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
188
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
189 dmf_write_le16(res, DMFONT_VERSION);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
190 dmf_write_le16(res, nglyphs);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
191 dmf_write_le16(res, maxglyph + 1);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
192 dmfputc(font->width, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
193 dmfputc(font->height, res);
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 (nglyphs > 0)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
196 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
197 int i;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
198 SDL_Surface *glyph = font->glyphs[maxglyph];
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
199
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
200 // 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
201 dmfputc(glyph->format->BitsPerPixel, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
202 dmf_write_le32(res, glyph->format->Rmask);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
203 dmf_write_le32(res, glyph->format->Gmask);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
204 dmf_write_le32(res, glyph->format->Bmask);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
205 dmf_write_le32(res, glyph->format->Amask);
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 for (i = 0; i < font->nglyphs; i++)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
208 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
209 glyph = font->glyphs[i];
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
210 if (glyph != NULL)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
211 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
212 int y;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
213 Uint8 *pixels = glyph->pixels;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
214
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
215 if (glyph->w < DMFONT_MIN_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
216 glyph->h < DMFONT_MIN_HEIGHT ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
217 glyph->w > DMFONT_MAX_WIDTH ||
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
218 glyph->h > DMFONT_MAX_HEIGHT)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
219 continue;
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 // 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
222 dmf_write_le16(res, i);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
223 dmfputc(glyph->w, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
224 dmfputc(glyph->h, res);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
225
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
226 // Write the pixel data
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
227 for (y = 0; y < glyph->h; y++)
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 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
230 return DMERR_FWRITE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
231 pixels += glyph->pitch;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
232 }
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 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
236
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
237 return DMERR_OK;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
238 }
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
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 int main(int argc, char *argv[])
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 DMResource *inFile = NULL;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 DMBitmapFont *font = NULL;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
245 SDL_Surface *fontbmap = NULL;
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 int res;
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
247 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
248 BOOL initTTF = FALSE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
249 TTF_Font *ttf = NULL;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
250 #endif
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 dmInitProg("fontconv", "Bitmap font converter", "0.2", NULL, NULL);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 dmVerbosity = 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 // Parse arguments
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 if (!dmArgsProcess(argc, argv, optList, optListN,
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 argHandleOpt, argHandleFile, TRUE))
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 exit(1);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 // Check arguments
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 if (!optInFilename)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 dmError("Input or output file not specified!\n");
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 return 1;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 }
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
266
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
267 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
268 if (TTF_Init() < 0)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
269 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
270 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
271 goto error_exit;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
272 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
273 initTTF = TRUE;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
274 #endif
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 // Open the source file
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 if ((inFile = dmf_create_stdio(optInFilename, "rb")) == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 dmError("Error opening input file '%s', %d: %s\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280 optInFilename, errno, strerror(errno));
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 return 1;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
284
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
285 if ((res = dmLoadBitmapFont(inFile, &font)) == DMERR_OK)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
286 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
287 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
288 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
289 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
290 else
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
291 if ((ttf = TTF_OpenFont(optInFilename, optSplitWidth)) != NULL)
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 {
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
293 int i;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
294 SDL_Color col = { 255, 255, 255, 100 }; //255, 255, 255, 100 };
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
295 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
296 optSplitWidth, optSplitHeight);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
297
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
298 TTF_SetFontStyle(ttf, TTF_STYLE_NORMAL);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
299
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
300 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
301 {
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
302 goto error_exit;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
303 }
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
305 for (i = 0; i < 255; i++)
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 char str[2];
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
308 str[0] = i;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
309 str[1] = 0;
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
310 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
311 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
312 }
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
313 #endif
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
314 else
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
315 {
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 dmfseek(inFile, 0L, SEEK_SET);
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 if ((fontbmap = dmLoadImage(inFile)) == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 dmError("Could not load image file '%s'.\n", optInFilename);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 goto error_exit;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
324 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
325 fontbmap->w, fontbmap->h, fontbmap->format->BitsPerPixel,
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
326 optSplitWidth, optSplitHeight);
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
327
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 if ((res = dmCreateBitmapFontFromImage(fontbmap, optSplitWidth, optSplitHeight, &font)) != DMERR_OK)
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 dmError("Could not create a font from image, %d: %s\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 res, dmErrorStr(res));
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 goto error_exit;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 }
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 if (font == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338 dmError("No font loaded.\n");
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 goto error_exit;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 if (optOutFormat == OFMT_DMFONT)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 {
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 DMResource *file;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
346 dmMsg(1, "Outputting a DMFONT format bitmap font.\n");
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
347
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348 if (optOutFilename == NULL)
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 file = dmf_create_stdio_stream(stdout);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350 else
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 file = dmf_create_stdio(optOutFilename, "wb");
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 (file == NULL)
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 creating file '%s', %d: %s\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 optInFilename, errno, strerror(errno));
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 goto error_exit;
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
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 res = dmSaveBitmapFont(file, font);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 dmf_close(file);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 }
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 if (res != DMERR_OK)
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 dmError("Error saving font, %d: %s\n",
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 res, dmErrorStr(res));
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368 }
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 error_exit:
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
371
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
372 #ifdef DM_GFX_TTF_TEXT
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
373 if (initTTF)
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
374 TTF_Quit();
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
375 #endif
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 dmf_close(inFile);
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378 dmFreeBitmapFont(font);
178
63ff0fb944cd Implement TTF to bitmap font conversion (crude).
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
379 SDL_FreeSurface(fontbmap);
160
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 return 0;
67d2cba58a87 Add fontconv tool.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 }