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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef __has_feature // Optional of course.
#define __has_feature(x) 0 // Compatibility with non-clang compilers.
#endif
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
struct BdfBoundingBox {
int width, height;
int xOffset, yOffset;
};
struct BdfFont {
char *familyName;
char *slant;
int maxAdvance;
int size;
int height;
BdfBoundingBox defaultBox;
int ascent;
int firstCharacter;
int defaultCharacter;
int numCharacters;
unsigned char **bitmaps;
unsigned char *advances;
BdfBoundingBox *boxes;
BdfFont() : bitmaps(0), advances(0), boxes(0), familyName(0), slant(0) {
}
~BdfFont() {
if (bitmaps) {
for (int i = 0; i < numCharacters; ++i)
delete[] bitmaps[i];
}
delete[] bitmaps;
delete[] advances;
delete[] boxes;
delete[] familyName;
delete[] slant;
}
};
void error(const char *s, ...) {
char buf[1024];
va_list va;
va_start(va, s);
vsnprintf(buf, 1024, s, va);
va_end(va);
fprintf(stderr, "ERROR: %s!\n", buf);
exit(1);
}
void warning(const char *s, ...) {
char buf[1024];
va_list va;
va_start(va, s);
vsnprintf(buf, 1024, s, va);
va_end(va);
fprintf(stderr, "WARNING: %s!\n", buf);
}
bool hasPrefix(const std::string &str, const std::string &prefix) {
return str.compare(0, prefix.size(), prefix) == 0;
}
inline void hexToInt(unsigned char &h) {
if (h >= '0' && h <= '9')
h -= '0';
else if (h >= 'A' && h <= 'F')
h -= 'A' - 10;
else if (h >= 'a' && h <= 'f')
h -= 'a' - 10;
else
h = 0;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: convbdf [input] [fontname]\n");
return 1;
}
std::ifstream in(argv[1]);
std::string line;
std::getline(in, line);
if (in.fail() || in.eof())
error("Premature end of file");
int verMajor, verMinor;
if (sscanf(line.c_str(), "STARTFONT %d.%d", &verMajor, &verMinor) != 2)
error("File '%s' is no BDF file", argv[1]);
if (verMajor != 2 || (verMinor != 1 && verMinor != 2))
error("File '%s' does not use a supported BDF version (%d.%d)", argv[1], verMajor, verMinor);
std::string fontName;
std::string copyright;
BdfFont font;
memset(&font, 0, sizeof(font));
font.ascent = -1;
font.defaultCharacter = -1;
int charsProcessed = 0, charsAvailable = 0, descent = -1;
while (true) {
std::getline(in, line);
if (in.fail() || in.eof())
error("Premature end of file");
if (hasPrefix(line, "PIXEL_SIZE ")) {
if (sscanf(line.c_str(), "PIXEL_SIZE %d", &font.size) != 1)
error("Invalid PIXEL_SIZE");
} else if (hasPrefix(line, "FONT ")) {
fontName = line.substr(5);
} else if (hasPrefix(line, "COPYRIGHT ")) {
copyright = line.substr(10);
} else if (hasPrefix(line, "COMMENT ")) {
// Ignore
} else if (hasPrefix(line, "FONTBOUNDINGBOX ")) {
if (sscanf(line.c_str(), "FONTBOUNDINGBOX %d %d %d %d",
&font.defaultBox.width, &font.defaultBox.height,
&font.defaultBox.xOffset, &font.defaultBox.yOffset) != 4)
error("Invalid FONTBOUNDINGBOX");
} else if (hasPrefix(line, "CHARS ")) {
if (sscanf(line.c_str(), "CHARS %d", &charsAvailable) != 1)
error("Invalid CHARS");
font.numCharacters = 256;
font.bitmaps = new unsigned char *[font.numCharacters];
memset(font.bitmaps, 0, sizeof(unsigned char *) * font.numCharacters);
font.advances = new unsigned char[font.numCharacters];
font.boxes = new BdfBoundingBox[font.numCharacters];
} else if (hasPrefix(line, "FAMILY_NAME \"")) {
font.familyName = new char[line.size()]; // We will definitely fit here
strncpy(font.familyName, &line.c_str()[13], line.size() - 1);
char *p = &font.familyName[strlen(font.familyName)];
while (p != font.familyName && *p != '"')
p--;
if (p == font.familyName)
error("Invalid FAMILY_NAME");
*p = '\0'; // Remove last quote
} else if (hasPrefix(line, "SLANT \"")) {
font.familyName = new char[line.size()]; // We will definitely fit here
strncpy(font.familyName, &line.c_str()[7], line.size() - 1);
char *p = &font.slant[strlen(font.slant)];
while (p != font.slant && *p != '"')
p--;
if (p == font.slant)
error("Invalid SLANT");
*p = '\0'; // Remove last quote
} else if (hasPrefix(line, "FONT_ASCENT ")) {
if (sscanf(line.c_str(), "FONT_ASCENT %d", &font.ascent) != 1)
error("Invalid FONT_ASCENT");
} else if (hasPrefix(line, "FONT_DESCENT ")) {
if (sscanf(line.c_str(), "FONT_DESCENT %d", &descent) != 1)
error("Invalid FONT_ASCENT");
} else if (hasPrefix(line, "DEFAULT_CHAR ")) {
if (sscanf(line.c_str(), "DEFAULT_CHAR %d", &font.defaultCharacter) != 1)
error("Invalid DEFAULT_CHAR");
} else if (hasPrefix(line, "STARTCHAR ")) {
++charsProcessed;
if (charsProcessed > charsAvailable)
error("Too many characters defined (only %d specified, but %d existing already)",
charsAvailable, charsProcessed);
bool hasWidth = false, hasBitmap = false;
int encoding = -1;
int xAdvance;
unsigned char *bitmap = 0;
BdfBoundingBox bbox = font.defaultBox;
while (true) {
std::getline(in, line);
if (in.fail() || in.eof())
error("Premature end of file");
if (hasPrefix(line, "ENCODING ")) {
if (sscanf(line.c_str(), "ENCODING %d", &encoding) != 1)
error("Invalid ENCODING");
} else if (hasPrefix(line, "DWIDTH ")) {
int yAdvance;
if (sscanf(line.c_str(), "DWIDTH %d %d", &xAdvance, &yAdvance) != 2)
error("Invalid DWIDTH");
if (yAdvance != 0)
error("Character %d uses a DWIDTH y advance of %d", encoding, yAdvance);
if (xAdvance < 0)
error("Character %d has a negative x advance", encoding);
if (xAdvance > font.maxAdvance)
font.maxAdvance = xAdvance;
hasWidth = true;
} else if (hasPrefix(line, "BBX" )) {
if (sscanf(line.c_str(), "BBX %d %d %d %d",
&bbox.width, &bbox.height,
&bbox.xOffset, &bbox.yOffset) != 4)
error("Invalid BBX");
} else if (line == "BITMAP") {
hasBitmap = true;
const size_t bytesPerRow = ((bbox.width + 7) / 8);
// Since we do not load all characters, we only create a
// buffer for the ones we actually load.
if (encoding < font.numCharacters)
bitmap = new unsigned char[bbox.height * bytesPerRow];
for (int i = 0; i < bbox.height; ++i) {
std::getline(in, line);
if (in.fail() || in.eof())
error("Premature end of file");
if (line.size() != bytesPerRow * 2)
error("Glyph bitmap line too short");
if (!bitmap)
continue;
for (size_t j = 0; j < bytesPerRow; ++j) {
unsigned char nibble1 = line[j * 2 + 0];
hexToInt(nibble1);
unsigned char nibble2 = line[j * 2 + 1];
hexToInt(nibble2);
bitmap[i * bytesPerRow + j] = (nibble1 << 4) | nibble2;
}
}
} else if (line == "ENDCHAR") {
if (encoding == -1 || !hasWidth || !hasBitmap)
error("Character not completly defined");
if (encoding < font.numCharacters) {
font.advances[encoding] = xAdvance;
font.boxes[encoding] = bbox;
font.bitmaps[encoding] = bitmap;
}
break;
}
}
} else if (line == "ENDFONT") {
break;
} else {
// Silently ignore other declarations
//warning("Unsupported declaration: \"%s\"", line.c_str());
}
}
if (font.ascent < 0)
error("No ascent specified");
if (descent < 0)
error("No descent specified");
font.height = font.ascent + descent;
int firstCharacter = font.numCharacters;
int lastCharacter = -1;
bool hasFixedBBox = true;
bool hasFixedAdvance = true;
for (int i = 0; i < font.numCharacters; ++i) {
if (!font.bitmaps[i])
continue;
if (i < firstCharacter)
firstCharacter = i;
if (i > lastCharacter)
lastCharacter = i;
if (font.advances[i] != font.maxAdvance)
hasFixedAdvance = false;
const BdfBoundingBox &bbox = font.boxes[i];
if (bbox.width != font.defaultBox.width
|| bbox.height != font.defaultBox.height
|| bbox.xOffset != font.defaultBox.xOffset
|| bbox.yOffset != font.defaultBox.yOffset)
hasFixedBBox = false;
}
if (lastCharacter == -1)
error("No glyphs found");
// Free the advance table, in case all glyphs use the same advance
if (hasFixedAdvance) {
delete[] font.advances;
font.advances = 0;
}
// Free the box table, in case all glyphs use the same box
if (hasFixedBBox) {
delete[] font.boxes;
font.boxes = 0;
}
// Adapt for the fact that we never use encoding 0.
if (font.defaultCharacter < firstCharacter
|| font.defaultCharacter > lastCharacter)
font.defaultCharacter = -1;
font.firstCharacter = firstCharacter;
charsAvailable = lastCharacter - firstCharacter + 1;
// Try to compact the tables
if (charsAvailable < font.numCharacters) {
unsigned char **bitmaps = new unsigned char *[charsAvailable];
BdfBoundingBox *boxes = 0;
if (!hasFixedBBox)
boxes = new BdfBoundingBox[charsAvailable];
unsigned char *advances = 0;
if (!hasFixedAdvance)
advances = new unsigned char[charsAvailable];
for (int i = 0; i < charsAvailable; ++i) {
const int encoding = i + firstCharacter;
if (font.bitmaps[encoding]) {
bitmaps[i] = font.bitmaps[encoding];
if (!hasFixedBBox)
boxes[i] = font.boxes[encoding];
if (!hasFixedAdvance)
advances[i] = font.advances[encoding];
} else {
bitmaps[i] = 0;
}
}
delete[] font.bitmaps;
font.bitmaps = bitmaps;
delete[] font.advances;
font.advances = advances;
delete[] font.boxes;
font.boxes = boxes;
font.numCharacters = charsAvailable;
}
char dateBuffer[256];
time_t curTime = time(0);
snprintf(dateBuffer, sizeof(dateBuffer), "%s", ctime(&curTime));
// Finally output the cpp source file to stdout
printf("// Generated by convbdf on %s"
"#include \"graphics/fonts/bdf.h\"\n"
"\n"
"// Font information:\n"
"// Name: %s\n"
"// Size: %dx%d\n"
"// Box: %d %d %d %d\n"
"// Ascent: %d\n"
"// First character: %d\n"
"// Default character: %d\n"
"// Characters: %d\n"
"// Copyright: %s\n"
"\n",
dateBuffer, fontName.c_str(), font.maxAdvance, font.height,
font.defaultBox.width, font.defaultBox.height, font.defaultBox.xOffset,
font.defaultBox.yOffset, font.ascent, font.firstCharacter,
font.defaultCharacter, font.numCharacters, copyright.c_str());
printf("namespace Graphics {\n"
"\n");
for (int i = 0; i < font.numCharacters; ++i) {
if (!font.bitmaps[i])
continue;
BdfBoundingBox box = hasFixedBBox ? font.defaultBox : font.boxes[i];
printf("// Character %d (0x%02X)\n"
"// Box: %d %d %d %d\n"
"// Advance: %d\n"
"//\n", i + font.firstCharacter, i + font.firstCharacter,
box.width, box.height, box.xOffset, box.yOffset,
hasFixedAdvance ? font.maxAdvance : font.advances[i]);
printf("// +");
for (int x = 0; x < box.width; ++x)
printf("-");
printf("+\n");
const unsigned char *bitmap = font.bitmaps[i];
for (int y = 0; y < box.height; ++y) {
printf("// |");
unsigned char data = 0;
for (int x = 0; x < box.width; ++x) {
if (!(x % 8))
data = *bitmap++;
printf("%c", (data & 0x80) ? '*' : ' ');
data <<= 1;
}
printf("|\n");
}
printf("// +");
for (int x = 0; x < box.width; ++x)
printf("-");
printf("+\n");
const int bytesPerRow = (box.width + 7) / 8;
bitmap = font.bitmaps[i];
printf("static const byte glyph%d[] = {\n", i);
for (int y = 0; y < box.height; ++y) {
printf("\t");
for (int x = 0; x < bytesPerRow; ++x) {
if (x)
printf(", ");
printf("0x%02X", *bitmap++);
}
if (y != box.height - 1)
printf(",");
printf("\n");
}
printf("};\n"
"\n");
}
printf("// Bitmap pointer table\n"
"const byte *const bitmapTable[] = {\n");
for (int i = 0; i < font.numCharacters; ++i) {
if (font.bitmaps[i])
printf("\tglyph%d", i);
else
printf("\t0");
if (i != font.numCharacters - 1)
printf(",");
printf("\n");
}
printf("};\n"
"\n");
if (!hasFixedAdvance) {
printf("// Advance table\n"
"static const byte advances[] = {\n");
for (int i = 0; i < font.numCharacters; ++i) {
if (font.bitmaps[i])
printf("\t%d", font.advances[i]);
else
printf("\t0");
if (i != font.numCharacters - 1)
printf(",");
printf("\n");
}
printf("};\n"
"\n");
}
if (!hasFixedBBox) {
printf("// Bounding box table\n"
"static const BdfBoundingBox boxes[] = {\n");
for (int i = 0; i < font.numCharacters; ++i) {
if (font.bitmaps[i]) {
const BdfBoundingBox &box = font.boxes[i];
printf("\t{ %d, %d, %d, %d }", box.width, box.height, box.xOffset, box.yOffset);
} else {
printf("\t{ 0, 0, 0, 0 }");
}
if (i != font.numCharacters - 1)
printf(",");
printf("\n");
}
printf("};\n"
"\n");
}
printf("// Font structure\n"
"static const BdfFontData desc = {\n"
"\t\"%s\", // Family name\n"
"\t\"%s\", // Slant\n"
"\t%d, // Max advance\n"
"\t%d, // Height\n"
"\t%d, // Size\n"
"\t{ %d, %d, %d, %d }, // Bounding box\n"
"\t%d, // Ascent\n"
"\n"
"\t%d, // First character\n"
"\t%d, // Default character\n"
"\t%d, // Characters\n"
"\n"
"\tbitmapTable, // Bitmaps\n",
font.familyName, font.slant, font.maxAdvance, font.size, font.height, font.defaultBox.width,
font.defaultBox.height, font.defaultBox.xOffset, font.defaultBox.yOffset,
font.ascent, font.firstCharacter, font.defaultCharacter, font.numCharacters);
if (hasFixedAdvance)
printf("\t0, // Advances\n");
else
printf("\tadvances, // Advances\n");
if (hasFixedBBox)
printf("\t0 // Boxes\n");
else
printf("\tboxes // Boxes\n");
printf("};\n"
"\n"
"DEFINE_FONT(%s)\n"
"\n"
"} // End of namespace Graphics\n",
argv[2]);
}
|