2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
2007-05-30 21:56:52 +00:00
|
|
|
* 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.
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
|
|
|
* 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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
2006-02-11 12:44:16 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Font management and font drawing module
|
2007-07-31 18:08:40 +00:00
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/saga.h"
|
|
|
|
#include "saga/gfx.h"
|
2005-07-19 19:05:52 +00:00
|
|
|
#include "saga/rscfile.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/font.h"
|
2004-12-15 00:24:12 +00:00
|
|
|
#include "saga/stream.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
namespace Saga {
|
|
|
|
|
2004-08-03 00:06:18 +00:00
|
|
|
Font::Font(SagaEngine *vm) : _vm(vm), _initialized(false) {
|
2004-04-12 21:40:49 +00:00
|
|
|
int i;
|
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
// Load font module resource context
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-09 23:41:22 +00:00
|
|
|
assert(_vm->getFontsCount() > 0);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
_fonts = (FontData **)calloc(_vm->getFontsCount(), sizeof(*_fonts));
|
|
|
|
_loadedFonts = 0;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-11 21:10:36 +00:00
|
|
|
for (i = 0; i < _vm->getFontsCount(); i++) {
|
|
|
|
loadFont(_vm->getFontDescription(i)->fontResourceId);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-03 00:06:18 +00:00
|
|
|
_initialized = true;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-03 00:06:18 +00:00
|
|
|
Font::~Font(void) {
|
2005-07-05 16:58:36 +00:00
|
|
|
debug(8, "Font::~Font(): Freeing fonts.");
|
2005-07-14 17:46:21 +00:00
|
|
|
int i;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
for (i = 0 ; i < _loadedFonts ; i++) {
|
|
|
|
if (_fonts[i] != NULL) {
|
|
|
|
free(_fonts[i]->normal.font);
|
|
|
|
free(_fonts[i]->outline.font);
|
2004-05-01 08:44:00 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
free(_fonts[i]);
|
2004-05-01 08:44:00 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-09-28 06:33:13 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
void Font::loadFont(uint32 fontResourceId) {
|
|
|
|
FontData *font;
|
|
|
|
byte *fontResourcePointer;
|
|
|
|
size_t fontResourceLength;
|
|
|
|
int numBits;
|
2004-04-12 21:40:49 +00:00
|
|
|
int c;
|
2005-07-19 19:05:52 +00:00
|
|
|
ResourceContext *fontContext;
|
2005-07-14 17:46:21 +00:00
|
|
|
|
|
|
|
debug(1, "Font::loadFont(): Reading fontResourceId %d...", fontResourceId);
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
fontContext = _vm->_resource->getContext(GAME_RESOURCEFILE);
|
2005-07-14 17:46:21 +00:00
|
|
|
if (fontContext == NULL) {
|
2005-07-19 19:05:52 +00:00
|
|
|
error("Font::Font() resource context not found");
|
2005-07-14 17:46:21 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Load font resource
|
2005-07-19 19:05:52 +00:00
|
|
|
_vm->_resource->loadResource(fontContext, fontResourceId, fontResourcePointer, fontResourceLength);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
if (fontResourceLength < FONT_DESCSIZE) {
|
2006-03-08 11:37:25 +00:00
|
|
|
error("Font::loadFont() Invalid font length (%i < %i)", (int)fontResourceLength, FONT_DESCSIZE);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
MemoryReadStreamEndian readS(fontResourcePointer, fontResourceLength, fontContext->isBigEndian);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Create new font structure
|
2005-07-14 17:46:21 +00:00
|
|
|
font = (FontData *)malloc(sizeof(*font));
|
2005-07-29 17:58:00 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Read font header
|
2005-07-14 17:46:21 +00:00
|
|
|
font->normal.header.charHeight = readS.readUint16();
|
|
|
|
font->normal.header.charWidth = readS.readUint16();
|
|
|
|
font->normal.header.rowLength = readS.readUint16();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
debug(2, "Character width: %d", font->normal.header.charWidth);
|
|
|
|
debug(2, "Character height: %d", font->normal.header.charHeight);
|
|
|
|
debug(2, "Row padding: %d", font->normal.header.rowLength);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
for (c = 0; c < FONT_CHARCOUNT; c++) {
|
2005-07-14 17:46:21 +00:00
|
|
|
font->normal.fontCharEntry[c].index = readS.readUint16();
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
for (c = 0; c < FONT_CHARCOUNT; c++) {
|
2005-07-14 17:46:21 +00:00
|
|
|
numBits = font->normal.fontCharEntry[c].width = readS.readByte();
|
|
|
|
font->normal.fontCharEntry[c].byteWidth = getByteLen(numBits);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
for (c = 0; c < FONT_CHARCOUNT; c++) {
|
2005-07-14 17:46:21 +00:00
|
|
|
font->normal.fontCharEntry[c].flag = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
for (c = 0; c < FONT_CHARCOUNT; c++) {
|
2005-07-14 17:46:21 +00:00
|
|
|
font->normal.fontCharEntry[c].tracking = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
if (readS.pos() != FONT_DESCSIZE) {
|
2005-07-14 17:46:21 +00:00
|
|
|
error("Invalid font resource size.");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
font->normal.font = (byte*)malloc(fontResourceLength - FONT_DESCSIZE);
|
|
|
|
memcpy(font->normal.font, fontResourcePointer + FONT_DESCSIZE, fontResourceLength - FONT_DESCSIZE);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
free(fontResourcePointer);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
// Create outline font style
|
|
|
|
createOutline(font);
|
2005-07-29 17:58:00 +00:00
|
|
|
|
|
|
|
// Set font data
|
|
|
|
_fonts[_loadedFonts++] = font;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
void Font::createOutline(FontData *font) {
|
2004-04-12 21:40:49 +00:00
|
|
|
int i;
|
2005-07-14 17:46:21 +00:00
|
|
|
int row;
|
|
|
|
int newByteWidth;
|
|
|
|
int oldByteWidth;
|
|
|
|
int newRowLength = 0;
|
|
|
|
size_t indexOffset = 0;
|
2004-04-12 21:40:49 +00:00
|
|
|
int index;
|
2005-07-14 17:46:21 +00:00
|
|
|
int currentByte;
|
|
|
|
unsigned char *basePointer;
|
|
|
|
unsigned char *srcPointer;
|
|
|
|
unsigned char *destPointer1;
|
|
|
|
unsigned char *destPointer2;
|
|
|
|
unsigned char *destPointer3;
|
|
|
|
unsigned char charRep;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
// Populate new font style character data
|
2004-10-27 21:32:28 +00:00
|
|
|
for (i = 0; i < FONT_CHARCOUNT; i++) {
|
2005-07-14 17:46:21 +00:00
|
|
|
newByteWidth = 0;
|
|
|
|
oldByteWidth = 0;
|
|
|
|
index = font->normal.fontCharEntry[i].index;
|
2004-10-27 21:32:28 +00:00
|
|
|
if ((index > 0) || (i == FONT_FIRSTCHAR)) {
|
2005-07-14 17:46:21 +00:00
|
|
|
index += indexOffset;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
font->outline.fontCharEntry[i].index = index;
|
|
|
|
font->outline.fontCharEntry[i].tracking = font->normal.fontCharEntry[i].tracking;
|
|
|
|
font->outline.fontCharEntry[i].flag = font->normal.fontCharEntry[i].flag;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
if (font->normal.fontCharEntry[i].width != 0) {
|
|
|
|
newByteWidth = getByteLen(font->normal.fontCharEntry[i].width + 2);
|
|
|
|
oldByteWidth = getByteLen(font->normal.fontCharEntry[i].width);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
if (newByteWidth > oldByteWidth) {
|
|
|
|
indexOffset++;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
font->outline.fontCharEntry[i].width = font->normal.fontCharEntry[i].width + 2;
|
|
|
|
font->outline.fontCharEntry[i].byteWidth = newByteWidth;
|
|
|
|
newRowLength += newByteWidth;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
debug(2, "New row length: %d", newRowLength);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
font->outline.header = font->normal.header;
|
|
|
|
font->outline.header.charWidth += 2;
|
|
|
|
font->outline.header.charHeight += 2;
|
|
|
|
font->outline.header.rowLength = newRowLength;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
// Allocate new font representation storage
|
2005-07-14 17:46:21 +00:00
|
|
|
font->outline.font = (unsigned char *)calloc(newRowLength, font->outline.header.charHeight);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Generate outline font representation
|
2004-10-27 21:32:28 +00:00
|
|
|
for (i = 0; i < FONT_CHARCOUNT; i++) {
|
2005-07-14 17:46:21 +00:00
|
|
|
for (row = 0; row < font->normal.header.charHeight; row++) {
|
|
|
|
for (currentByte = 0; currentByte < font->outline.fontCharEntry[i].byteWidth; currentByte++) {
|
|
|
|
basePointer = font->outline.font + font->outline.fontCharEntry[i].index + currentByte;
|
|
|
|
destPointer1 = basePointer + newRowLength * row;
|
|
|
|
destPointer2 = basePointer + newRowLength * (row + 1);
|
|
|
|
destPointer3 = basePointer + newRowLength * (row + 2);
|
|
|
|
if (currentByte > 0) {
|
2004-05-01 08:44:00 +00:00
|
|
|
// Get last two columns from previous byte
|
2005-07-14 17:46:21 +00:00
|
|
|
srcPointer = font->normal.font + font->normal.header.rowLength * row + font->normal.fontCharEntry[i].index + (currentByte - 1);
|
|
|
|
charRep = *srcPointer;
|
|
|
|
*destPointer1 |= ((charRep << 6) | (charRep << 7));
|
|
|
|
*destPointer2 |= ((charRep << 6) | (charRep << 7));
|
|
|
|
*destPointer3 |= ((charRep << 6) | (charRep << 7));
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
if (currentByte < font->normal.fontCharEntry[i].byteWidth) {
|
|
|
|
srcPointer = font->normal.font + font->normal.header.rowLength * row + font->normal.fontCharEntry[i].index + currentByte;
|
|
|
|
charRep = *srcPointer;
|
|
|
|
*destPointer1 |= charRep | (charRep >> 1) | (charRep >> 2);
|
|
|
|
*destPointer2 |= charRep | (charRep >> 1) | (charRep >> 2);
|
|
|
|
*destPointer3 |= charRep | (charRep >> 1) | (charRep >> 2);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// "Hollow out" character to prevent overdraw
|
2005-07-14 17:46:21 +00:00
|
|
|
for (row = 0; row < font->normal.header.charHeight; row++) {
|
|
|
|
for (currentByte = 0; currentByte < font->outline.fontCharEntry[i].byteWidth; currentByte++) {
|
2005-10-11 17:39:31 +00:00
|
|
|
destPointer2 = font->outline.font + font->outline.header.rowLength * (row + 1) + font->outline.fontCharEntry[i].index + currentByte;
|
2005-07-14 17:46:21 +00:00
|
|
|
if (currentByte > 0) {
|
2004-05-01 08:44:00 +00:00
|
|
|
// Get last two columns from previous byte
|
2005-07-14 17:46:21 +00:00
|
|
|
srcPointer = font->normal.font + font->normal.header.rowLength * row + font->normal.fontCharEntry[i].index + (currentByte - 1);
|
|
|
|
*destPointer2 &= ((*srcPointer << 7) ^ 0xFFU);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
if (currentByte < font->normal.fontCharEntry[i].byteWidth) {
|
|
|
|
srcPointer = font->normal.font + font->normal.header.rowLength * row + font->normal.fontCharEntry[i].index + currentByte;
|
|
|
|
*destPointer2 &= ((*srcPointer >> 1) ^ 0xFFU);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-07-29 17:58:00 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Returns the horizontal length in pixels of the graphical representation
|
2005-11-03 18:20:12 +00:00
|
|
|
// of at most 'count' characters of the string 'text', taking
|
2004-05-01 08:44:00 +00:00
|
|
|
// into account any formatting options specified by 'flags'.
|
2005-11-03 18:20:12 +00:00
|
|
|
// If 'count' is 0, all characters of 'test' are counted.
|
2005-07-14 17:46:21 +00:00
|
|
|
int Font::getStringWidth(FontId fontId, const char *text, size_t count, FontEffectFlags flags) {
|
|
|
|
FontData *font;
|
2004-04-12 21:40:49 +00:00
|
|
|
size_t ct;
|
|
|
|
int width = 0;
|
|
|
|
int ch;
|
2005-07-14 17:46:21 +00:00
|
|
|
const byte *txt;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-11-03 18:20:12 +00:00
|
|
|
|
2005-09-28 06:33:13 +00:00
|
|
|
font = getFont(fontId);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
txt = (const byte *) text;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
for (ct = count; *txt && (!count || ct > 0); txt++, ct--) {
|
|
|
|
ch = *txt & 0xFFU;
|
2004-05-01 08:44:00 +00:00
|
|
|
// Translate character
|
2004-08-03 00:06:18 +00:00
|
|
|
ch = _charMap[ch];
|
2004-10-27 21:32:28 +00:00
|
|
|
assert(ch < FONT_CHARCOUNT);
|
2005-07-14 17:46:21 +00:00
|
|
|
width += font->normal.fontCharEntry[ch].tracking;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
if ((flags & kFontBold) || (flags & kFontOutline)) {
|
2004-04-12 21:40:49 +00:00
|
|
|
width += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
void Font::draw(FontId fontId, Surface *ds, const char *text, size_t count, const Common::Point &point,
|
|
|
|
int color, int effectColor, FontEffectFlags flags) {
|
|
|
|
FontData *font;
|
|
|
|
Point offsetPoint(point);
|
|
|
|
|
2005-09-28 06:33:13 +00:00
|
|
|
font = getFont(fontId);
|
2005-07-14 17:46:21 +00:00
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
if (flags & kFontOutline) {
|
2005-07-14 17:46:21 +00:00
|
|
|
offsetPoint.x--;
|
|
|
|
offsetPoint.y--;
|
|
|
|
outFont(font->outline, ds, text, count, offsetPoint, effectColor, flags);
|
|
|
|
outFont(font->normal, ds, text, count, point, color, flags);
|
|
|
|
} else if (flags & kFontShadow) {
|
|
|
|
offsetPoint.x--;
|
|
|
|
offsetPoint.y++;
|
|
|
|
outFont(font->normal, ds, text, count, offsetPoint, effectColor, flags);
|
|
|
|
outFont(font->normal, ds, text, count, point, color, flags);
|
2004-05-01 08:44:00 +00:00
|
|
|
} else { // FONT_NORMAL
|
2005-07-14 17:46:21 +00:00
|
|
|
outFont(font->normal, ds, text, count, point, color, flags);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-02 08:52:37 +00:00
|
|
|
void Font::outFont(const FontStyle &drawFont, Surface *ds, const char *text, size_t count, const Common::Point &point, int color, FontEffectFlags flags) {
|
2005-07-14 17:46:21 +00:00
|
|
|
const byte *textPointer;
|
|
|
|
byte *c_dataPointer;
|
2004-04-12 21:40:49 +00:00
|
|
|
int c_code;
|
2005-07-14 17:46:21 +00:00
|
|
|
int charRow;
|
|
|
|
Point textPoint(point);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
byte *outputPointer;
|
|
|
|
byte *outputPointer_min;
|
|
|
|
byte *outputPointer_max;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
int row;
|
2005-07-14 17:46:21 +00:00
|
|
|
int rowLimit;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
int c_byte_len;
|
|
|
|
int c_byte;
|
|
|
|
int c_bit;
|
|
|
|
int ct;
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
if ((point.x > ds->w) || (point.y > ds->h)) {
|
2004-05-01 08:44:00 +00:00
|
|
|
// Output string can't be visible
|
2005-07-14 17:46:21 +00:00
|
|
|
return;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-09-02 08:52:37 +00:00
|
|
|
textPointer = (const byte *)text;
|
2005-07-14 17:46:21 +00:00
|
|
|
ct = count;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
// Draw string one character at a time, maximum of 'draw_str'_ct
|
2004-05-01 08:44:00 +00:00
|
|
|
// characters, or no limit if 'draw_str_ct' is 0
|
2005-07-14 17:46:21 +00:00
|
|
|
for (; *textPointer && (!count || ct); textPointer++, ct--) {
|
|
|
|
c_code = *textPointer & 0xFFU;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Translate character
|
2005-07-14 17:46:21 +00:00
|
|
|
if (!(flags & kFontDontmap))
|
2005-01-11 00:51:58 +00:00
|
|
|
c_code = _charMap[c_code];
|
2004-10-27 21:32:28 +00:00
|
|
|
assert(c_code < FONT_CHARCOUNT);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Check if character is defined
|
2005-07-14 17:46:21 +00:00
|
|
|
if ((drawFont.fontCharEntry[c_code].index == 0) && (c_code != FONT_FIRSTCHAR)) {
|
2004-10-27 21:32:28 +00:00
|
|
|
#if FONT_SHOWUNDEFINED
|
2007-08-02 15:29:50 +00:00
|
|
|
if (c_code == FONT_CH_SPACE || c_code == 9) {
|
2005-07-14 17:46:21 +00:00
|
|
|
textPoint.x += drawFont.fontCharEntry[c_code].tracking;
|
2004-04-12 21:40:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-10-27 21:32:28 +00:00
|
|
|
c_code = FONT_CH_QMARK;
|
2004-05-01 08:44:00 +00:00
|
|
|
#else
|
|
|
|
// Character code is not defined, but advance tracking
|
|
|
|
// ( Not defined if offset is 0, except for 33 ('!') which
|
|
|
|
// is defined )
|
2005-07-14 17:46:21 +00:00
|
|
|
textPoint.x += drawFont.fontCharEntry[c_code].tracking;
|
2004-04-12 21:40:49 +00:00
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Get length of character in bytes
|
2005-07-14 17:46:21 +00:00
|
|
|
c_byte_len = ((drawFont.fontCharEntry[c_code].width - 1) / 8) + 1;
|
|
|
|
rowLimit = (ds->h < (textPoint.y + drawFont.header.charHeight)) ? ds->h : textPoint.y + drawFont.header.charHeight;
|
|
|
|
charRow = 0;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
for (row = textPoint.y; row < rowLimit; row++, charRow++) {
|
2004-05-01 08:44:00 +00:00
|
|
|
// Clip negative rows */
|
2004-04-12 21:40:49 +00:00
|
|
|
if (row < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
outputPointer = (byte *)ds->pixels + (ds->pitch * row) + textPoint.x;
|
|
|
|
outputPointer_min = (byte *)ds->pixels + (ds->pitch * row) + (textPoint.x > 0 ? textPoint.x : 0);
|
|
|
|
outputPointer_max = outputPointer + (ds->pitch - textPoint.x);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// If character starts off the screen, jump to next character
|
2005-07-14 17:46:21 +00:00
|
|
|
if (outputPointer < outputPointer_min) {
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
c_dataPointer = drawFont.font + charRow * drawFont.header.rowLength + drawFont.fontCharEntry[c_code].index;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
for (c_byte = 0; c_byte < c_byte_len; c_byte++, c_dataPointer++) {
|
2004-05-01 08:44:00 +00:00
|
|
|
// Check each bit, draw pixel if bit is set
|
2005-07-14 17:46:21 +00:00
|
|
|
for (c_bit = 7; c_bit >= 0 && (outputPointer < outputPointer_max); c_bit--) {
|
|
|
|
if ((*c_dataPointer >> c_bit) & 0x01) {
|
2005-09-02 08:52:37 +00:00
|
|
|
*outputPointer = (byte)color;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2005-07-14 17:46:21 +00:00
|
|
|
outputPointer++;
|
2004-05-01 08:44:00 +00:00
|
|
|
} // end per-bit processing
|
|
|
|
} // end per-byte processing
|
|
|
|
} // end per-row processing
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Advance tracking position
|
2005-07-14 17:46:21 +00:00
|
|
|
textPoint.x += drawFont.fontCharEntry[c_code].tracking;
|
2004-05-01 08:44:00 +00:00
|
|
|
} // end per-character processing
|
2005-07-14 17:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Font::textDraw(FontId fontId, Surface *ds, const char *text, const Common::Point &point, int color, int effectColor, FontEffectFlags flags) {
|
|
|
|
int textWidth;
|
|
|
|
int textLength;
|
|
|
|
int fitWidth;
|
|
|
|
Common::Point textPoint(point);
|
|
|
|
|
|
|
|
textLength = strlen(text);
|
|
|
|
|
|
|
|
if (!(flags & kFontCentered)) {
|
|
|
|
// Text is not centered; No formatting required
|
|
|
|
draw(fontId, ds, text, textLength, point, color, effectColor, flags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Text is centered... format output
|
|
|
|
// Enforce minimum and maximum center points for centered text
|
|
|
|
if (textPoint.x < TEXT_CENTERLIMIT) {
|
|
|
|
textPoint.x = TEXT_CENTERLIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (textPoint.x > ds->w - TEXT_CENTERLIMIT) {
|
|
|
|
textPoint.x = ds->w - TEXT_CENTERLIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (textPoint.x < (TEXT_MARGIN * 2)) {
|
|
|
|
// Text can't be centered if it's too close to the margin
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
textWidth = getStringWidth(fontId, text, textLength, flags);
|
|
|
|
|
|
|
|
if (textPoint.x < (ds->w / 2)) {
|
|
|
|
// Fit to right side
|
|
|
|
fitWidth = (textPoint.x - TEXT_MARGIN) * 2;
|
|
|
|
} else {
|
|
|
|
// Fit to left side
|
|
|
|
fitWidth = ((ds->w - TEXT_MARGIN) - textPoint.x) * 2;
|
|
|
|
}
|
|
|
|
|
2005-07-19 19:05:52 +00:00
|
|
|
if (fitWidth < textWidth) {
|
|
|
|
warning("text too long to be displayed in one line");
|
2005-10-17 03:28:21 +00:00
|
|
|
textWidth = fitWidth;
|
2005-07-14 17:46:21 +00:00
|
|
|
}
|
2005-07-19 19:05:52 +00:00
|
|
|
// Entire string fits, draw it
|
|
|
|
textPoint.x = textPoint.x - (textWidth / 2);
|
|
|
|
draw(fontId, ds, text, textLength, textPoint, color, effectColor, flags);
|
2005-07-14 17:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Font::getHeight(FontId fontId, const char *text, int width, FontEffectFlags flags) {
|
|
|
|
int textWidth;
|
|
|
|
int textLength;
|
|
|
|
int fitWidth;
|
|
|
|
const char *startPointer;
|
|
|
|
const char *searchPointer;
|
|
|
|
const char *measurePointer;
|
|
|
|
const char *foundPointer;
|
|
|
|
int len;
|
|
|
|
int w;
|
|
|
|
const char *endPointer;
|
|
|
|
int h;
|
|
|
|
int wc;
|
|
|
|
int w_total;
|
|
|
|
int len_total;
|
|
|
|
Common::Point textPoint;
|
|
|
|
Common::Point textPoint2;
|
|
|
|
|
|
|
|
textLength = strlen(text);
|
|
|
|
|
|
|
|
textWidth = getStringWidth(fontId, text, textLength, flags);
|
|
|
|
h = getHeight(fontId);
|
|
|
|
fitWidth = width;
|
|
|
|
|
|
|
|
textPoint.x = (fitWidth / 2);
|
|
|
|
textPoint.y = 0;
|
|
|
|
|
2005-07-29 17:58:00 +00:00
|
|
|
if (fitWidth >= textWidth) {
|
2005-07-14 17:46:21 +00:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
// String won't fit on one line
|
|
|
|
w_total = 0;
|
|
|
|
len_total = 0;
|
|
|
|
wc = 0;
|
|
|
|
|
|
|
|
startPointer = text;
|
|
|
|
measurePointer = text;
|
|
|
|
searchPointer = text;
|
|
|
|
endPointer = text + textLength;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
foundPointer = strchr(searchPointer, ' ');
|
|
|
|
if (foundPointer == NULL) {
|
|
|
|
// Ran to the end of the buffer
|
|
|
|
len = endPointer - measurePointer;
|
|
|
|
} else {
|
|
|
|
len = foundPointer - measurePointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
w = getStringWidth(fontId, measurePointer, len, flags);
|
|
|
|
measurePointer = foundPointer;
|
|
|
|
|
|
|
|
if ((w_total + w) > fitWidth) {
|
|
|
|
// This word won't fit
|
|
|
|
if (wc == 0) {
|
2005-09-02 08:52:37 +00:00
|
|
|
// The first word in the line didn't fit. Still print it
|
|
|
|
searchPointer = measurePointer + 1;
|
2005-07-14 17:46:21 +00:00
|
|
|
}
|
|
|
|
// Wrap what we've got and restart
|
|
|
|
textPoint.y += h + TEXT_LINESPACING;
|
2005-09-08 18:01:39 +00:00
|
|
|
if (foundPointer == NULL) {
|
|
|
|
// Since word hit NULL but fit, we are done
|
|
|
|
return textPoint.y + h;
|
|
|
|
}
|
2005-07-14 17:46:21 +00:00
|
|
|
w_total = 0;
|
|
|
|
len_total = 0;
|
|
|
|
wc = 0;
|
|
|
|
measurePointer = searchPointer;
|
|
|
|
startPointer = searchPointer;
|
|
|
|
} else {
|
|
|
|
// Word will fit ok
|
|
|
|
w_total += w;
|
|
|
|
len_total += len;
|
|
|
|
wc++;
|
|
|
|
if (foundPointer == NULL) {
|
|
|
|
// Since word hit NULL but fit, we are done
|
|
|
|
return textPoint.y + h;
|
|
|
|
}
|
|
|
|
searchPointer = measurePointer + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
void Font::textDrawRect(FontId fontId, Surface *ds, const char *text, const Common::Rect &rect, int color, int effectColor, FontEffectFlags flags) {
|
|
|
|
int textWidth;
|
|
|
|
int textLength;
|
|
|
|
int fitWidth;
|
|
|
|
const char *startPointer;
|
|
|
|
const char *searchPointer;
|
|
|
|
const char *measurePointer;
|
|
|
|
const char *foundPointer;
|
|
|
|
int len;
|
|
|
|
int w;
|
|
|
|
const char *endPointer;
|
|
|
|
int h;
|
|
|
|
int wc;
|
|
|
|
int w_total;
|
|
|
|
int len_total;
|
|
|
|
Common::Point textPoint;
|
|
|
|
Common::Point textPoint2;
|
|
|
|
|
|
|
|
textLength = strlen(text);
|
|
|
|
|
|
|
|
textWidth = getStringWidth(fontId, text, textLength, flags);
|
|
|
|
fitWidth = rect.width();
|
|
|
|
|
|
|
|
textPoint.x = rect.left + (fitWidth / 2);
|
|
|
|
textPoint.y = rect.top;
|
|
|
|
|
|
|
|
if (fitWidth >= textWidth) {
|
2005-09-28 06:33:13 +00:00
|
|
|
// Entire string fits, draw it
|
2005-07-14 17:46:21 +00:00
|
|
|
textPoint.x -= (textWidth / 2);
|
|
|
|
draw(fontId, ds, text, textLength, textPoint, color, effectColor, flags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// String won't fit on one line
|
|
|
|
h = getHeight(fontId);
|
|
|
|
w_total = 0;
|
|
|
|
len_total = 0;
|
|
|
|
wc = 0;
|
|
|
|
|
|
|
|
startPointer = text;
|
|
|
|
measurePointer = text;
|
|
|
|
searchPointer = text;
|
|
|
|
endPointer = text + textLength;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
foundPointer = strchr(searchPointer, ' ');
|
|
|
|
if (foundPointer == NULL) {
|
|
|
|
// Ran to the end of the buffer
|
|
|
|
len = endPointer - measurePointer;
|
|
|
|
} else {
|
|
|
|
len = foundPointer - measurePointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
w = getStringWidth(fontId, measurePointer, len, flags);
|
|
|
|
measurePointer = foundPointer;
|
|
|
|
|
|
|
|
if ((w_total + w) > fitWidth) {
|
|
|
|
// This word won't fit
|
|
|
|
if (wc == 0) {
|
2005-09-02 08:52:37 +00:00
|
|
|
w_total = fitWidth;
|
|
|
|
len_total = len;
|
2005-07-14 17:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap what we've got and restart
|
|
|
|
textPoint2.x = textPoint.x - (w_total / 2);
|
|
|
|
textPoint2.y = textPoint.y;
|
|
|
|
draw(fontId, ds, startPointer, len_total, textPoint2, color, effectColor, flags);
|
|
|
|
textPoint.y += h + TEXT_LINESPACING;
|
|
|
|
if (textPoint.y >= rect.bottom) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
w_total = 0;
|
|
|
|
len_total = 0;
|
2005-09-02 08:52:37 +00:00
|
|
|
if (wc == 0) {
|
|
|
|
searchPointer = measurePointer + 1;
|
|
|
|
}
|
2005-07-14 17:46:21 +00:00
|
|
|
wc = 0;
|
2005-09-28 06:33:13 +00:00
|
|
|
|
|
|
|
// Advance the search pointer to the next non-space.
|
|
|
|
// Otherwise, the first "word" to be measured will be
|
|
|
|
// an empty string. Measuring or drawing a string of
|
|
|
|
// length 0 is interpreted as measure/draw the entire
|
|
|
|
// buffer, which certainly is not what we want here.
|
|
|
|
//
|
|
|
|
// This happes because a string may contain several
|
|
|
|
// spaces in a row, e.g. after a period.
|
|
|
|
|
|
|
|
while (*searchPointer == ' ')
|
|
|
|
searchPointer++;
|
|
|
|
|
2005-07-14 17:46:21 +00:00
|
|
|
measurePointer = searchPointer;
|
|
|
|
startPointer = searchPointer;
|
|
|
|
} else {
|
|
|
|
// Word will fit ok
|
|
|
|
w_total += w;
|
|
|
|
len_total += len;
|
|
|
|
wc++;
|
|
|
|
if (foundPointer == NULL) {
|
|
|
|
// Since word hit NULL but fit, we are done
|
|
|
|
textPoint2.x = textPoint.x - (w_total / 2);
|
|
|
|
textPoint2.y = textPoint.y;
|
|
|
|
draw(fontId, ds, startPointer, len_total, textPoint2, color,
|
|
|
|
effectColor, flags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
searchPointer = measurePointer + 1;
|
|
|
|
}
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-11-19 12:41:23 +00:00
|
|
|
Font::FontId Font::knownFont2FontIdx(KnownFont font) {
|
2005-11-03 18:20:12 +00:00
|
|
|
FontId fontId = kSmallFont;
|
|
|
|
|
2007-07-26 14:39:05 +00:00
|
|
|
// The demo version of IHNM has 3 font types (like ITE), not 6 (like the full version of IHNM)
|
|
|
|
if (_vm->getGameType() == GType_ITE || _vm->getGameId() == GID_IHNM_DEMO) {
|
2005-11-03 18:20:12 +00:00
|
|
|
switch (font)
|
|
|
|
{
|
|
|
|
case (kKnownFontSmall):
|
|
|
|
fontId = kSmallFont;
|
|
|
|
break;
|
|
|
|
case (kKnownFontMedium):
|
|
|
|
fontId = kMediumFont;
|
|
|
|
break;
|
|
|
|
case (kKnownFontBig):
|
|
|
|
fontId = kBigFont;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (kKnownFontVerb):
|
|
|
|
fontId = kSmallFont;
|
|
|
|
break;
|
|
|
|
case (kKnownFontScript):
|
|
|
|
fontId = kMediumFont;
|
|
|
|
break;
|
|
|
|
case (kKnownFontPause):
|
|
|
|
fontId = _vm->_font->valid(kBigFont) ? kBigFont : kMediumFont;
|
|
|
|
break;
|
|
|
|
}
|
2007-07-26 14:39:05 +00:00
|
|
|
} else if (_vm->getGameType() == GType_IHNM && _vm->getGameId() != GID_IHNM_DEMO) {
|
2005-11-03 18:20:12 +00:00
|
|
|
switch (font)
|
|
|
|
{
|
|
|
|
case (kKnownFontSmall):
|
|
|
|
fontId = kSmallFont;
|
|
|
|
break;
|
|
|
|
case (kKnownFontMedium):
|
|
|
|
fontId = kMediumFont;
|
|
|
|
break;
|
|
|
|
case (kKnownFontBig):
|
|
|
|
fontId = kBigFont;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (kKnownFontVerb):
|
|
|
|
fontId = kIHNMFont8;
|
|
|
|
break;
|
|
|
|
case (kKnownFontScript):
|
|
|
|
fontId = kIHNMMainFont;
|
|
|
|
break;
|
|
|
|
case (kKnownFontPause):
|
|
|
|
fontId = kMediumFont; // unchecked
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fontId;
|
|
|
|
}
|
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
} // End of namespace Saga
|