2 * GraphLCD tool crtfont
4 * crtfont.c - a tool to create *.fnt files for use with the GraphLCD
7 * based on graphlcd plugin 0.1.1 for the Video Disc Recorder
8 * (c) 2001-2004 Carsten Siebholz <c.siebholz AT t-online.de>
10 * This file is released under the GNU General Public License. Refer
11 * to the COPYING file distributed with this package.
13 * (c) 2004 Andreas Regel <andreas.regel AT powarman.de>
21 #include <glcdgraphics/bitmap.h>
22 #include <glcdgraphics/font.h>
24 static const char *prgname = "crtfont";
25 static const char *version = "0.1.6";
27 const int kMaxLineLength = 1024;
38 char * trimleft(char * str)
41 while (*s == ' ' || *s == '\t')
47 char * trimright(char * str)
49 char * s = str + strlen(str) - 1;
50 while (s >= str && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
55 char * trim(char * str)
57 return trimleft(trimright(str));
61 int main(int argc, char *argv[])
63 ePicFormat picFormat = undefined;
65 char * picName = NULL;
66 char * descName = NULL;
67 char * fontName = NULL;
70 GLCD::cBitmap * bitmap = NULL;
71 GLCD::cBitmap * tmpBitmap = NULL;
72 GLCD::cBitmap * charBitmap = NULL;
73 char line[kMaxLineLength];
76 int startOffset, endOffset;
81 static struct option long_options[] =
83 { "format", required_argument, NULL, 'f'},
84 { "bmpfile", required_argument, NULL, 'b'},
85 { "descfile", required_argument, NULL, 'd'},
86 { "outfile", required_argument, NULL, 'o'},
90 int c, option_index = 0;
91 while ((c = getopt_long(argc, argv, "f:b:d:o:", long_options, &option_index)) != -1)
96 if (strcasecmp(optarg, "PBM") == 0)
101 picName = strdup(optarg);
105 descName = strdup(optarg);
109 fontName = strdup(optarg);
117 if (picFormat == undefined)
119 fprintf(stderr, "ERROR: You have to specify the format (-f <format>)\n");
124 fprintf(stderr, "ERROR: You have to specify the bmpfile (-b bmpfile)\n");
129 fprintf(stderr, "ERROR: You have to specify the descfile (-d descfile)\n");
134 fprintf(stderr, "ERROR: You have to specify the outfile (-o outfile)\n");
144 descFile = fopen(descName,"r");
147 fprintf(stderr, "Cannot open file: %s\n",descName);
155 bitmap = new GLCD::cBitmap(0, 0);
156 bitmap->LoadPBM(picName);
159 fprintf(stderr, "Cannot open file: %s\n",picName);
174 fgets(line, sizeof(line), descFile);
176 if (strstr(line, "version") != NULL)
178 ptr = strstr(line, ":");
179 version = atoi(ptr + 1);
183 fprintf(stderr, "Wrong description file format version (found %d, expected 1)!\n", version);
186 while (!feof(descFile))
188 fgets(line, sizeof(line), descFile);
190 if (strstr(line, "fontheight") != NULL)
192 ptr = strstr(line, ":");
193 font.SetTotalHeight(atoi(ptr + 1));
195 else if (strstr(line, "fontascent") != NULL)
197 ptr = strstr(line, ":");
198 font.SetTotalAscent(atoi(ptr + 1));
200 else if (strstr(line, "lineheight") != NULL)
202 ptr = strstr(line, ":");
203 font.SetLineHeight(atoi(ptr + 1));
205 else if (strstr(line, "spacebetween") != NULL)
207 ptr = strstr(line, ":");
208 font.SetSpaceBetween(atoi(ptr + 1));
210 else if (strstr(line, "spacewidth") != NULL)
212 ptr = strstr(line, ":");
213 spaceWidth = atoi(ptr + 1);
217 token = strtok(line, " ");
220 startOffset = atoi(token);
223 token = strtok(NULL, " ");
227 if (strlen(token) == 1)
228 character = (uint8_t) token[0];
230 character = atoi(token);
233 token = strtok(NULL, " ");
234 endOffset = atoi(token);
235 tmpBitmap = bitmap->SubBitmap(startOffset, l * font.TotalHeight(), endOffset - 1, (l + 1) * font.TotalHeight() - 1);
238 // calculate width of this character
243 for (y = 0; y < tmpBitmap->Height(); y++)
245 for (x = 0; x < tmpBitmap->Width(); x++)
247 if (tmpBitmap->GetPixel(x, y))
250 if (x < tmpBitmap->Width() && x < left)
252 for (x = tmpBitmap->Width() - 1; x >= 0; x--)
254 if (tmpBitmap->GetPixel(x, y))
257 if (x >= 0 && x > right)
263 right = spaceWidth - 1;
265 charBitmap = tmpBitmap->SubBitmap(left, 0, right, font.TotalHeight() - 1);
266 font.SetCharacter(character, charBitmap);
271 font.SetCharacter(character, tmpBitmap);
273 startOffset = endOffset;
275 // get next character
276 token = strtok(NULL, " ");
285 if (font.SaveFNT(fontName))
286 fprintf(stdout,"Font '%s' created successfully\n", fontName);
293 fprintf(stdout, "\n");
294 fprintf(stdout, "%s v%s\n", prgname, version);
295 fprintf(stdout, "%s is a tool to create *.fnt files that are used by the\n", prgname);
296 fprintf(stdout, " graphlcd plugin for VDR.\n\n");
297 fprintf(stdout, " Usage: %s -f <format> -b bmpfile -d descfile -o outfile\n\n", prgname);
298 fprintf(stdout, " -f --format specifies the format of the bitmap. Possible values are:\n");
299 fprintf(stdout, " PBM : file is an binary PBM file\n" );
300 fprintf(stdout, " -b --bmpfile specifies the name of the bitmap file (*.pbm)\n");
301 fprintf(stdout, " -d --descfile specifies the name of the description file (*.desc)\n");
302 fprintf(stdout, " -o --outfile specifies the name of the output file (*.fnt)\n");
303 fprintf(stdout, "\n" );
304 fprintf(stdout, " example: %s -f PBM -b f12.pbm -d f12.desc -o f12.fnt\n", prgname);