font.cpp 9.99 KB
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

/*
 *
 * font.cpp
 *
 * 23rd August 2005: Created font.c
 * 3rd February 2009: Renamed font.c to font.cpp
 *
 * Part of the OpenJazz project
 *
 *
 * Copyright (c) 2005-2010 Alister Thomson
 *
 * OpenJazz is distributed under the terms of
 * the GNU General Public License, version 2.0
 *
 * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/*
 * Deals with the loading, displaying and freeing of screen fonts.
 *
 */


#include "../file.h"
#include "font.h"
#include "video.h"

#include <string.h>


Font::Font (const char* fileName) {

	File* file;
	unsigned char* pixels;
	unsigned char* blank;
	int fileSize;
	int count, size, width, height;

	// Load font from a font file

	try {

		file = new File(fileName, false);

	} catch (int e) {

		throw e;

	}

	fileSize = file->getSize();

	nCharacters = 128;


	file->seek(20, true);
	lineHeight = file->loadChar() << 1;


	// Create blank character data

	blank = new unsigned char[3];
	memset(blank, 0, 3);


	// Load characters

	for (count = 0; count < 128; count++) {

		if (file->tell() >= fileSize) {

			nCharacters = count;

			break;

		}

		size = file->loadShort();

		if (size) {

			pixels = file->loadRLE(size);

			width = pixels[0];
			width += pixels[1] << 8;
			height = pixels[2];
			height += pixels[3] << 8;

			characters[count] = createSurface(pixels + 4, width, height);

			delete[] pixels;

		} else characters[count] = createSurface(blank, 3, 1);

		SDL_SetColorKey(characters[count], SDL_SRCCOLORKEY, 0);

	}

	delete[] blank;

	delete file;


	// Create ASCII->font map

	for (count = 0; count < 33; count++) map[count] = 0;
	map[33] = 107; // !
	map[34] = 116; // "
	map[35] = 0; // #
	map[36] = 63; // $
	map[37] = 0; // %
	map[38] = 0; // &
	map[39] = 115; // '
	map[40] = 111; // (
	map[41] = 112; // )
	map[42] = 0; // *
	map[43] = 105; // +
	map[44] = 101; // ,
	map[45] = 104; // -
	map[46] = 102; // .
	map[47] = 108; // /
	for (count = 48; count < 58; count++) map[count] = count + 5;  // Numbers
	map[58] = 114; // :
	map[59] = 113; // ;
	map[60] = 0; // <
	map[61] = 106; // =
	map[62] = 0; // >
	map[63] = 103; // ?
	map[64] = 0; // @
	for (count = 65; count < 91; count++) map[count] = count - 38; // Upper-case letters
	for (; count < 97; count++) map[count] = 0;
	for (; count < 123; count++) map[count] = count - 96; // Lower-case letters
	for (; count < 128; count++) map[count] = 0;

	for (count = 0; count < 128; count++) {

		if (map[count] >= nCharacters) map[count] = 0;

	}

	return;

}


Font::Font (unsigned char* pixels, bool big) {

	unsigned char* chrPixels;
	int count, y;

	if (big) lineHeight = 8;
	else lineHeight = 7;

	chrPixels = new unsigned char[8 * lineHeight];

	for (count = 0; count < 40; count++) {

		for (y = 0; y < lineHeight; y++)
			memcpy(chrPixels + (y * 8), pixels + (count * 8) + (y * SW), 8);

		characters[count] = createSurface(chrPixels, 8, lineHeight);

		if (big) SDL_SetColorKey(characters[count], SDL_SRCCOLORKEY, 0);

	}

	nCharacters= 40;

	delete[] chrPixels;


	// Create ASCII->font map

	if (big) {

		// Goes " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-:."

		for (count = 0; count < 45; count++) map[count] = 0;
		map[count++] = 37;
		map[count++] = 39;
		for (; count < 48; count++) map[count] = 0;
		for (; count < 58; count++) map[count] = count - 47;
		map[count++] = 38;
		for (; count < 65; count++) map[count] = 0;
		for (; count < 91; count++) map[count] = count - 54;
		for (; count < 97; count++) map[count] = 0;
		for (; count < 123; count++) map[count] = count - 86;
		for (; count < 128; count++) map[count] = 0;

	} else {

		// Goes " 0123456789oo" (where oo = infinity)
		// Use :; to represent the infinity symbol

		for (count = 0; count < 48; count++) map[count] = 0;
		for (; count < 60; count++) map[count] = count - 47;
		for (; count < 128; count++) map[count] = 0;

	}

	return;

}


Font::Font () {

	File* file;
	unsigned char* pixels;
	unsigned char* sorted;
	int fileSize;
	int count, width, height;

	// Load font from FONTS.000

	try {

		file = new File(F_FONTS, false);

	} catch (int e) {

		throw e;

	}


	fileSize = file->getSize();

	nCharacters = file->loadShort();


	// Load characters

	for (count = 0; count < nCharacters; count++) {

		if (file->tell() >= fileSize) {

			nCharacters = count;

			break;

		}

		width = file->loadShort() << 2;
		height = file->loadShort();

		file->seek(4, false);

		pixels = file->loadBlock(width * height);
		sorted = sortPixels(pixels, width * height);

		characters[count] = createSurface(sorted, width, height);
		SDL_SetColorKey(characters[count], SDL_SRCCOLORKEY, 254);

		delete[] sorted;
		delete[] pixels;

	}

	delete file;

	lineHeight = characters[0]->h;


	// Create blank character data

	pixels = new unsigned char[3];
	memset(pixels, 254, 3);
	characters[nCharacters] = createSurface(pixels, 3, 1);
	SDL_SetColorKey(characters[nCharacters], SDL_SRCCOLORKEY, 254);
	delete[] pixels;


	// Create ASCII->font map

	for (count = 0; count < 37; count++) map[count] = nCharacters;
	map[count++] = 36;
	for (; count < 48; count++) map[count] = nCharacters;
	for (; count < 58; count++) map[count] = count - 22;
	for (; count < 65; count++) map[count] = nCharacters;
	for (; count < 91; count++) map[count] = count - 65;
	for (; count < 97; count++) map[count] = nCharacters;
	for (; count < 123; count++) map[count] = count - 97;
	for (; count < 128; count++) map[count] = nCharacters;

	nCharacters++;

	for (count = 0; count < 128; count++) {

		if (map[count] >= nCharacters) map[count] = 0;

	}

	return;

}


Font::~Font () {

	int count;

	for (count = 0; count < nCharacters; count++) SDL_FreeSurface(characters[count]);

	return;

}


int Font::showString (const char* string, int x, int y) {

	SDL_Surface* surface;
	SDL_Rect dst;
	unsigned int count;
	int xOffset, yOffset;

	// Determine the position at which to draw the first character
	xOffset = x;
	yOffset = y;

	// Go through each character of the string
	for (count = 0; string[count]; count++) {

		if (string[count] == '\n') {

			xOffset = x;
			yOffset += lineHeight;

		} else {

			// Determine the character's position on the screen
			dst.y = yOffset;
			dst.x = xOffset;

			// Determine the character's surface
			surface = characters[int(map[int(string[count])])];

			// Draw the character to the screen
			SDL_BlitSurface(surface, NULL, canvas, &dst);

			xOffset += surface->w + 2;

		}

	}

	return xOffset;

}


int Font::showSceneString (const unsigned char* string, int x, int y) {

	SDL_Surface* surface;
	SDL_Rect dst;
	unsigned int count;
	int offset;

	// Determine the position at which to draw the first character
	offset = x;

	// Go through each character of the string
	for (count = 0; string[count]; count++) {

		// Determine the character's position on the screen
		dst.y = y;
		dst.x = offset;

		// Determine the character's surface
		if (string[count] < nCharacters) surface = characters[int(string[count])];
		else surface = characters[0];

		// Draw the character to the screen
		SDL_BlitSurface(surface, NULL, canvas, &dst);

		offset += surface->w + 1;

	}

	return offset;

}


void Font::showNumber (int n, int x, int y) {

	SDL_Surface *surface;
	SDL_Rect dst;
	int count, offset;

	// n being 0 is a special case. It must not be considered to be a trailing
	// zero, as these are not displayed.
	if (!n) {

		// Determine 0's surface
		surface = characters[int(map[int('0')])];

		// Determine 0's position on the screen
		dst.y = y;
		dst.x = x - surface->w;

		// Draw 0 to the screen
		SDL_BlitSurface(surface, NULL, canvas, &dst);

		return;

	}

	// Determine the length of the number to be drawn
	if (n > 0) count = n;
	else count = -n;

	// Determine the position at which to draw the lowest digit
	offset = x;

	while (count) {

		// Determine the digit's surface
		surface = characters[int(map['0' + (count % 10)])];

		offset -= surface->w;

		// Determine the digit's position on the screen
		dst.y = y;
		dst.x = offset;

		// Draw the digit to the screen
		SDL_BlitSurface(surface, NULL, canvas, &dst);

		count /= 10;

	}

	// If needed, draw the negative sign
	if (n < 0) {

		// Determine the negative sign's surface
		surface = characters[int(map[int('-')])];

		// Determine the negative sign's position on the screen
		dst.y = y;
		dst.x = offset - surface->w;

		// Draw the negative sign to the screen
		SDL_BlitSurface(surface, NULL, canvas, &dst);

	}

	return;

}


void Font::mapPalette (int start, int length, int newStart, int newLength) {

	SDL_Color palette[256];
	int count;

	// Map a range of palette indices to another range

	for (count = 0; count < length; count++)
		palette[count].r = palette[count].g = palette[count].b =
			(count * newLength / length) + newStart;

	for (count = 0; count < nCharacters; count++)
		SDL_SetPalette(characters[count], SDL_LOGPAL, palette, start, length);

	return;

}


void Font::restorePalette () {

	int count;

	for (count = 0; count < nCharacters; count++)
		video.restoreSurfacePalette(characters[count]);

	return;

}


int Font::getHeight () {

	return lineHeight;

}


int Font::getStringWidth (const char *string) {

	int count;
	int stringWidth = 0;

	// Go through each character of the string
	for (count = 0; string[count]; count++) {

		// Only get the width of the first line
		if (string[count] == '\n') return stringWidth;

		stringWidth += characters[int(map[int(string[count])])]->w + 2;

	}

	return stringWidth;

}


int Font::getSceneStringWidth (const unsigned char *string) {

	int count;
	int stringWidth = 0;

	// Go through each character of the string
	for (count = 0; string[count]; count++) {

		if (string[count] < nCharacters) stringWidth += characters[int(string[count])]->w + 1;
		else stringWidth += characters[0]->w + 1;

	}

	return stringWidth;

}