1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/*
* GraphLCD tool crtfont
*
* crtfont.c - a tool to create *.fnt files for use with the GraphLCD
* graphics library
*
* based on graphlcd plugin 0.1.1 for the Video Disc Recorder
* (c) 2001-2004 Carsten Siebholz <c.siebholz AT t-online.de>
*
* This file is released under the GNU General Public License. Refer
* to the COPYING file distributed with this package.
*
* (c) 2004 Andreas Regel <andreas.regel AT powarman.de>
*/
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glcdgraphics/bitmap.h>
#include <glcdgraphics/font.h>
static const char *prgname = "crtfont";
static const char *version = "0.1.6";
const int kMaxLineLength = 1024;
enum ePicFormat
{
undefined,
PBM
};
void usage(void);
char * trimleft(char * str)
{
char * s = str;
while (*s == ' ' || *s == '\t')
s++;
strcpy(str, s);
return str;
}
char * trimright(char * str)
{
char * s = str + strlen(str) - 1;
while (s >= str && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
*s-- = 0;
return str;
}
char * trim(char * str)
{
return trimleft(trimright(str));
}
int main(int argc, char *argv[])
{
ePicFormat picFormat = undefined;
GLCD::cFont font;
char * picName = NULL;
char * descName = NULL;
char * fontName = NULL;
FILE * descFile;
bool error = false;
GLCD::cBitmap * bitmap = NULL;
GLCD::cBitmap * tmpBitmap = NULL;
GLCD::cBitmap * charBitmap = NULL;
char line[kMaxLineLength];
int l = 0;
char * token;
int startOffset, endOffset;
int spaceWidth;
int version;
char * ptr;
static struct option long_options[] =
{
{ "format", required_argument, NULL, 'f'},
{ "bmpfile", required_argument, NULL, 'b'},
{ "descfile", required_argument, NULL, 'd'},
{ "outfile", required_argument, NULL, 'o'},
{ NULL}
};
int c, option_index = 0;
while ((c = getopt_long(argc, argv, "f:b:d:o:", long_options, &option_index)) != -1)
{
switch (c)
{
case 'f':
if (strcasecmp(optarg, "PBM") == 0)
picFormat = PBM;
break;
case 'b':
picName = strdup(optarg);
break;
case 'd':
descName = strdup(optarg);
break;
case 'o':
fontName = strdup(optarg);
break;
default:
return 1;
}
}
if (picFormat == undefined)
{
fprintf(stderr, "ERROR: You have to specify the format (-f <format>)\n");
error = true;
}
if (!picName)
{
fprintf(stderr, "ERROR: You have to specify the bmpfile (-b bmpfile)\n");
error = true;
}
if (!descName)
{
fprintf(stderr, "ERROR: You have to specify the descfile (-d descfile)\n");
error = true;
}
if (!fontName)
{
fprintf(stderr, "ERROR: You have to specify the outfile (-o outfile)\n");
error = true;
}
if (error)
{
usage();
return 1;
}
descFile = fopen(descName,"r");
if (!descFile)
{
fprintf(stderr, "Cannot open file: %s\n",descName);
return 2;
}
// Load Picture
switch (picFormat)
{
case PBM:
bitmap = new GLCD::cBitmap(0, 0);
bitmap->LoadPBM(picName);
if (!bitmap)
{
fprintf(stderr, "Cannot open file: %s\n",picName);
return 2;
}
break;
default:
return 2;
}
if (!bitmap)
return 3;
spaceWidth = 0;
version = 0;
fgets(line, sizeof(line), descFile);
trim(line);
if (strstr(line, "version") != NULL)
{
ptr = strstr(line, ":");
version = atoi(ptr + 1);
}
if (version != 1)
{
fprintf(stderr, "Wrong description file format version (found %d, expected 1)!\n", version);
return 2;
}
while (!feof(descFile))
{
fgets(line, sizeof(line), descFile);
trim(line);
if (strstr(line, "fontheight") != NULL)
{
ptr = strstr(line, ":");
font.SetTotalHeight(atoi(ptr + 1));
}
else if (strstr(line, "fontascent") != NULL)
{
ptr = strstr(line, ":");
font.SetTotalAscent(atoi(ptr + 1));
}
else if (strstr(line, "lineheight") != NULL)
{
ptr = strstr(line, ":");
font.SetLineHeight(atoi(ptr + 1));
}
else if (strstr(line, "spacebetween") != NULL)
{
ptr = strstr(line, ":");
font.SetSpaceBetween(atoi(ptr + 1));
}
else if (strstr(line, "spacewidth") != NULL)
{
ptr = strstr(line, ":");
spaceWidth = atoi(ptr + 1);
}
else
{
token = strtok(line, " ");
if (token)
{
startOffset = atoi(token);
// get character
token = strtok(NULL, " ");
while (token)
{
uint16_t character;
if (strlen(token) == 1)
character = (uint8_t) token[0];
else
character = atoi(token);
// get EndOffset
token = strtok(NULL, " ");
endOffset = atoi(token);
tmpBitmap = bitmap->SubBitmap(startOffset, l * font.TotalHeight(), endOffset - 1, (l + 1) * font.TotalHeight() - 1);
if (spaceWidth > 0)
{
// calculate width of this character
int x;
int y;
int left = 255;
int right = 0;
for (y = 0; y < tmpBitmap->Height(); y++)
{
for (x = 0; x < tmpBitmap->Width(); x++)
{
if (tmpBitmap->GetPixel(x, y))
break;
}
if (x < tmpBitmap->Width() && x < left)
left = x;
for (x = tmpBitmap->Width() - 1; x >= 0; x--)
{
if (tmpBitmap->GetPixel(x, y))
break;
}
if (x >= 0 && x > right)
right = x;
}
if (left > right)
{
left = 0;
right = spaceWidth - 1;
}
charBitmap = tmpBitmap->SubBitmap(left, 0, right, font.TotalHeight() - 1);
font.SetCharacter(character, charBitmap);
delete tmpBitmap;
}
else
{
font.SetCharacter(character, tmpBitmap);
}
startOffset = endOffset;
// get next character
token = strtok(NULL, " ");
}
}
l++;
}
}
fclose(descFile);
delete bitmap;
if (font.SaveFNT(fontName))
fprintf(stdout,"Font '%s' created successfully\n", fontName);
return 0;
}
void usage(void)
{
fprintf(stdout, "\n");
fprintf(stdout, "%s v%s\n", prgname, version);
fprintf(stdout, "%s is a tool to create *.fnt files that are used by the\n", prgname);
fprintf(stdout, " graphlcd plugin for VDR.\n\n");
fprintf(stdout, " Usage: %s -f <format> -b bmpfile -d descfile -o outfile\n\n", prgname);
fprintf(stdout, " -f --format specifies the format of the bitmap. Possible values are:\n");
fprintf(stdout, " PBM : file is an binary PBM file\n" );
fprintf(stdout, " -b --bmpfile specifies the name of the bitmap file (*.pbm)\n");
fprintf(stdout, " -d --descfile specifies the name of the description file (*.desc)\n");
fprintf(stdout, " -o --outfile specifies the name of the output file (*.fnt)\n");
fprintf(stdout, "\n" );
fprintf(stdout, " example: %s -f PBM -b f12.pbm -d f12.desc -o f12.fnt\n", prgname);
}
|