2004-04-12 21:40:49 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2005-01-01 16:20:17 +00:00
|
|
|
* Copyright (C) 2004-2005 The ScummVM project
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
|
|
|
* The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
|
|
|
|
*
|
|
|
|
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Font management and font drawing module
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/saga.h"
|
|
|
|
#include "saga/gfx.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/rscfile_mod.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;
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Load font module resource context
|
2004-12-22 13:09:47 +00:00
|
|
|
_fontContext = _vm->getFileContext(GAME_RESOURCEFILE, 0);
|
2004-11-15 03:03:48 +00:00
|
|
|
if (_fontContext == NULL) {
|
2004-08-03 00:06:18 +00:00
|
|
|
error("Font::Font(): Couldn't get 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-01-11 21:10:36 +00:00
|
|
|
_nFonts = 0;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-11 21:10:36 +00:00
|
|
|
_fonts = (FONT **)malloc(_vm->getFontsCount() * sizeof(*_fonts));
|
2004-08-03 00:06:18 +00:00
|
|
|
if (_fonts == NULL) {
|
2005-01-21 21:55:54 +00:00
|
|
|
memoryError("Font::Font");
|
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) {
|
2004-04-25 14:42:14 +00:00
|
|
|
// int i;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-03 00:06:18 +00:00
|
|
|
debug(0, "Font::~Font(): Freeing fonts.");
|
2004-04-12 21:40:49 +00:00
|
|
|
/*
|
2004-12-15 00:24:12 +00:00
|
|
|
for (i = 0 ; i < FONT_COUNT ; i++) {
|
|
|
|
if (_fonts[i] != NULL) {
|
|
|
|
if (_fonts[i]->normal_loaded) {
|
|
|
|
free(_fonts[i]->normal->font_free_p);
|
|
|
|
free(_fonts[i]->normal);
|
2004-05-01 08:44:00 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
if (_fonts[i]->outline_loaded) {
|
|
|
|
free(_fonts[i]->outline->font_free_p);
|
|
|
|
free(_fonts[i]->outline);
|
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-01-11 21:10:36 +00:00
|
|
|
int Font::loadFont(uint32 fontResourceId) {
|
2004-10-27 21:32:28 +00:00
|
|
|
FONT_HEADER fh;
|
|
|
|
FONT *font;
|
|
|
|
FONT_STYLE *normal_font;
|
2004-04-30 23:02:23 +00:00
|
|
|
byte *fontres_p;
|
2004-04-12 21:40:49 +00:00
|
|
|
size_t fontres_len;
|
|
|
|
int nbits;
|
|
|
|
int c;
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Load font resource
|
2005-01-11 21:10:36 +00:00
|
|
|
if (RSC_LoadResource(_fontContext, fontResourceId, &fontres_p, &fontres_len) != SUCCESS) {
|
2004-08-03 00:06:18 +00:00
|
|
|
error("Font::loadFont(): Couldn't load font resource.");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
if (fontres_len < FONT_DESCSIZE) {
|
2004-11-15 03:03:48 +00:00
|
|
|
warning("Font::loadFont(): Invalid font length (%d < %d)", fontres_len, FONT_DESCSIZE);
|
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
MemoryReadStreamEndian readS(fontres_p, fontres_len, IS_BIG_ENDIAN);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Create new font structure
|
2005-01-02 14:52:11 +00:00
|
|
|
font = (FONT *)malloc(sizeof(*font));
|
2004-04-12 21:40:49 +00:00
|
|
|
if (font == NULL) {
|
2005-01-21 21:55:54 +00:00
|
|
|
memoryError("Font::loadFont");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Read font header
|
2004-12-15 00:24:12 +00:00
|
|
|
fh.c_height = readS.readUint16();
|
|
|
|
fh.c_width = readS.readUint16();
|
|
|
|
fh.row_length = readS.readUint16();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-11 21:10:36 +00:00
|
|
|
debug(1, "Font::loadFont(): Reading fontResourceId %d...", fontResourceId);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
debug(2, "Character width: %d", fh.c_width);
|
|
|
|
debug(2, "Character height: %d", fh.c_height);
|
|
|
|
debug(2, "Row padding: %d", fh.row_length);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Create normal font style
|
2005-01-02 14:52:11 +00:00
|
|
|
normal_font = (FONT_STYLE *)malloc(sizeof(*normal_font));
|
2004-04-12 21:40:49 +00:00
|
|
|
if (normal_font == NULL) {
|
2005-01-21 21:55:54 +00:00
|
|
|
memoryError("Font::loadFont");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
normal_font->font_free_p = fontres_p;
|
|
|
|
normal_font->hdr.c_height = fh.c_height;
|
|
|
|
normal_font->hdr.c_width = fh.c_width;
|
|
|
|
normal_font->hdr.row_length = fh.row_length;
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
for (c = 0; c < FONT_CHARCOUNT; c++) {
|
2004-12-15 00:24:12 +00:00
|
|
|
normal_font->fce[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++) {
|
2004-08-01 10:02:41 +00:00
|
|
|
nbits = normal_font->fce[c].width = readS.readByte();
|
2004-08-03 00:06:18 +00:00
|
|
|
normal_font->fce[c].byte_width = getByteLen(nbits);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
for (c = 0; c < FONT_CHARCOUNT; c++) {
|
2004-08-01 10:02:41 +00:00
|
|
|
normal_font->fce[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++) {
|
2004-08-01 10:02:41 +00:00
|
|
|
normal_font->fce[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) {
|
2004-05-05 13:05:45 +00:00
|
|
|
warning("Invalid font resource size.");
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
normal_font->font_p = fontres_p + FONT_DESCSIZE;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
font->normal = normal_font;
|
|
|
|
font->normal_loaded = 1;
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Create outline font style
|
2004-08-03 00:06:18 +00:00
|
|
|
font->outline = createOutline(normal_font);
|
2004-04-12 21:40:49 +00:00
|
|
|
font->outline_loaded = 1;
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Set font data
|
2005-01-11 21:10:36 +00:00
|
|
|
_fonts[_nFonts++] = font;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-03 00:06:18 +00:00
|
|
|
int Font::getHeight(int font_id) {
|
2004-10-27 21:32:28 +00:00
|
|
|
FONT *font;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-03 00:06:18 +00:00
|
|
|
if (!_initialized) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-03 01:12:08 +00:00
|
|
|
if ((font_id < 0) || (font_id >= _nFonts) || (_fonts[font_id] == NULL)) {
|
2004-08-03 00:06:18 +00:00
|
|
|
error("Font::getHeight(): Invalid font id.");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-03 00:06:18 +00:00
|
|
|
font = _fonts[font_id];
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
return font->normal->hdr.c_height;
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
FONT_STYLE *Font::createOutline(FONT_STYLE *src_font) {
|
|
|
|
FONT_STYLE *new_font;
|
2004-04-12 21:40:49 +00:00
|
|
|
unsigned char *new_font_data;
|
|
|
|
size_t new_font_data_len;
|
|
|
|
int s_width = src_font->hdr.c_width;
|
|
|
|
int s_height = src_font->hdr.c_height;
|
|
|
|
int new_row_len = 0;
|
|
|
|
int row;
|
|
|
|
int i;
|
|
|
|
int index;
|
|
|
|
size_t index_offset = 0;
|
|
|
|
int new_byte_width;
|
|
|
|
int old_byte_width;
|
|
|
|
int current_byte;
|
|
|
|
unsigned char *base_ptr;
|
|
|
|
unsigned char *src_ptr;
|
|
|
|
unsigned char *dest_ptr1;
|
|
|
|
unsigned char *dest_ptr2;
|
|
|
|
unsigned char *dest_ptr3;
|
|
|
|
unsigned char c_rep;
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Create new font style structure
|
2005-01-02 14:52:11 +00:00
|
|
|
new_font = (FONT_STYLE *)malloc(sizeof(*new_font));
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
if (new_font == NULL) {
|
2005-01-21 21:55:54 +00:00
|
|
|
memoryError("Font::createOutline");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-01-02 14:52:11 +00:00
|
|
|
memset(new_font, 0, sizeof(*new_font));
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Populate new font style character data
|
2004-10-27 21:32:28 +00:00
|
|
|
for (i = 0; i < FONT_CHARCOUNT; i++) {
|
2004-04-12 21:40:49 +00:00
|
|
|
new_byte_width = 0;
|
|
|
|
old_byte_width = 0;
|
|
|
|
index = src_font->fce[i].index;
|
2004-10-27 21:32:28 +00:00
|
|
|
if ((index > 0) || (i == FONT_FIRSTCHAR)) {
|
2004-04-12 21:40:49 +00:00
|
|
|
index += index_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_font->fce[i].index = index;
|
|
|
|
new_font->fce[i].tracking = src_font->fce[i].tracking;
|
|
|
|
new_font->fce[i].flag = src_font->fce[i].flag;
|
|
|
|
|
|
|
|
if (src_font->fce[i].width != 0) {
|
2004-08-03 00:06:18 +00:00
|
|
|
new_byte_width = getByteLen(src_font->fce[i].width + 2);
|
|
|
|
old_byte_width = getByteLen(src_font->fce[i].width);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
if (new_byte_width > old_byte_width) {
|
|
|
|
index_offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new_font->fce[i].width = src_font->fce[i].width + 2;
|
|
|
|
new_font->fce[i].byte_width = new_byte_width;
|
|
|
|
new_row_len += new_byte_width;
|
|
|
|
}
|
|
|
|
|
2004-05-05 13:05:45 +00:00
|
|
|
debug(2, "New row length: %d", new_row_len);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
new_font->hdr.c_width = s_width + 2;
|
|
|
|
new_font->hdr.c_height = s_height + 2;
|
|
|
|
new_font->hdr.row_length = new_row_len;
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Allocate new font representation storage
|
2004-04-12 21:40:49 +00:00
|
|
|
new_font_data_len = new_row_len * (s_height + 2);
|
|
|
|
new_font_data = (unsigned char *)malloc(new_font_data_len);
|
|
|
|
|
|
|
|
if (new_font_data == NULL) {
|
2005-01-21 21:55:54 +00:00
|
|
|
memoryError("Font::createOutline");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(new_font_data, 0, new_font_data_len);
|
|
|
|
|
|
|
|
new_font->font_free_p = new_font_data;
|
|
|
|
new_font->font_p = new_font_data;
|
|
|
|
|
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++) {
|
2004-04-12 21:40:49 +00:00
|
|
|
for (row = 0; row < s_height; row++) {
|
2004-05-01 08:44:00 +00:00
|
|
|
for (current_byte = 0; current_byte < new_font->fce[i].byte_width; current_byte++) {
|
|
|
|
base_ptr = new_font->font_p + new_font->fce[i].index + current_byte;
|
|
|
|
dest_ptr1 = base_ptr + new_font->hdr.row_length * row;
|
|
|
|
dest_ptr2 = base_ptr + new_font->hdr.row_length * (row + 1);
|
|
|
|
dest_ptr3 = base_ptr + new_font->hdr.row_length * (row + 2);
|
2004-04-12 21:40:49 +00:00
|
|
|
if (current_byte > 0) {
|
2004-05-01 08:44:00 +00:00
|
|
|
// Get last two columns from previous byte
|
|
|
|
src_ptr = src_font->font_p + src_font->hdr.row_length * row + src_font->fce[i].index +
|
|
|
|
(current_byte - 1);
|
2004-04-12 21:40:49 +00:00
|
|
|
c_rep = *src_ptr;
|
2004-05-01 08:44:00 +00:00
|
|
|
*dest_ptr1 |= ((c_rep << 6) | (c_rep << 7));
|
|
|
|
*dest_ptr2 |= ((c_rep << 6) | (c_rep << 7));
|
|
|
|
*dest_ptr3 |= ((c_rep << 6) | (c_rep << 7));
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (current_byte < src_font->fce[i].byte_width) {
|
2004-05-01 08:44:00 +00:00
|
|
|
src_ptr = src_font->font_p + src_font->hdr.row_length * row + src_font->fce[i].index +
|
|
|
|
current_byte;
|
2004-04-12 21:40:49 +00:00
|
|
|
c_rep = *src_ptr;
|
2004-05-01 08:44:00 +00:00
|
|
|
*dest_ptr1 |= c_rep | (c_rep >> 1) | (c_rep >> 2);
|
|
|
|
*dest_ptr2 |= c_rep | (c_rep >> 1) | (c_rep >> 2);
|
|
|
|
*dest_ptr3 |= c_rep | (c_rep >> 1) | (c_rep >> 2);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// "Hollow out" character to prevent overdraw
|
2004-04-12 21:40:49 +00:00
|
|
|
for (row = 0; row < s_height; row++) {
|
2004-05-01 08:44:00 +00:00
|
|
|
for (current_byte = 0; current_byte < new_font->fce[i].byte_width; current_byte++) {
|
|
|
|
dest_ptr2 = new_font->font_p + new_font->hdr.row_length * (row + 1) + new_font->fce[i].index + current_byte;
|
2004-04-12 21:40:49 +00:00
|
|
|
if (current_byte > 0) {
|
2004-05-01 08:44:00 +00:00
|
|
|
// Get last two columns from previous byte
|
|
|
|
src_ptr = src_font->font_p + src_font->hdr.row_length * row + src_font->fce[i].index +
|
|
|
|
(current_byte - 1);
|
|
|
|
*dest_ptr2 &= ((*src_ptr << 7) ^ 0xFFU);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (current_byte < src_font->fce[i].byte_width) {
|
2004-05-01 08:44:00 +00:00
|
|
|
src_ptr = src_font->font_p + src_font->hdr.row_length * row + src_font->fce[i].index +
|
|
|
|
current_byte;
|
|
|
|
*dest_ptr2 &= ((*src_ptr >> 1) ^ 0xFFU);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_font;
|
|
|
|
}
|
|
|
|
|
2004-08-03 00:06:18 +00:00
|
|
|
int Font::getByteLen(int num_bits) {
|
2004-04-12 21:40:49 +00:00
|
|
|
int byte_len;
|
|
|
|
byte_len = num_bits / 8;
|
|
|
|
|
|
|
|
if (num_bits % 8) {
|
|
|
|
byte_len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return byte_len;
|
|
|
|
}
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Returns the horizontal length in pixels of the graphical representation
|
|
|
|
// of at most 'test_str_ct' characters of the string 'test_str', taking
|
|
|
|
// into account any formatting options specified by 'flags'.
|
|
|
|
// If 'test_str_ct' is 0, all characters of 'test_str' are counted.
|
2004-08-03 00:06:18 +00:00
|
|
|
int Font::getStringWidth(int font_id, const char *test_str, size_t test_str_ct, int flags) {
|
2004-10-27 21:32:28 +00:00
|
|
|
FONT *font;
|
2004-04-12 21:40:49 +00:00
|
|
|
size_t ct;
|
|
|
|
int width = 0;
|
|
|
|
int ch;
|
2004-04-30 23:02:23 +00:00
|
|
|
const byte *txt_p;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-03 00:06:18 +00:00
|
|
|
if (!_initialized) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-03 01:12:08 +00:00
|
|
|
if ((font_id < 0) || (font_id >= _nFonts) || (_fonts[font_id] == NULL)) {
|
2004-08-03 00:06:18 +00:00
|
|
|
error("Font::getStringWidth(): Invalid font id.");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-03 00:06:18 +00:00
|
|
|
font = _fonts[font_id];
|
2004-04-12 21:40:49 +00:00
|
|
|
assert(font != NULL);
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
txt_p = (const byte *) test_str;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
for (ct = test_str_ct; *txt_p && (!test_str_ct || ct > 0); txt_p++, ct--) {
|
2004-04-12 21:40:49 +00:00
|
|
|
ch = *txt_p & 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);
|
2004-04-12 21:40:49 +00:00
|
|
|
width += font->normal->fce[ch].tracking;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & FONT_BOLD) || (flags & FONT_OUTLINE)) {
|
|
|
|
width += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int Font::draw(int font_id, SURFACE *ds, const char *draw_str, size_t draw_str_ct,
|
2004-05-01 08:44:00 +00:00
|
|
|
int text_x, int text_y, int color, int effect_color, int flags) {
|
2004-10-27 21:32:28 +00:00
|
|
|
FONT *font;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-03 00:06:18 +00:00
|
|
|
if (!_initialized) {
|
|
|
|
error("Font::draw(): Font Module not initialized.");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-03 01:12:08 +00:00
|
|
|
if ((font_id < 0) || (font_id >= _nFonts) || (_fonts[font_id] == NULL)) {
|
2004-08-03 00:06:18 +00:00
|
|
|
error("Font::draw(): Invalid font id.");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-03 00:06:18 +00:00
|
|
|
font = _fonts[font_id];
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
if (flags & FONT_OUTLINE) {
|
2005-01-11 00:51:58 +00:00
|
|
|
outFont(font->outline, ds, draw_str, draw_str_ct, text_x - 1, text_y - 1, effect_color, flags);
|
|
|
|
outFont(font->normal, ds, draw_str, draw_str_ct, text_x, text_y, color, flags);
|
2004-04-12 21:40:49 +00:00
|
|
|
} else if (flags & FONT_SHADOW) {
|
2005-01-11 00:51:58 +00:00
|
|
|
outFont(font->normal, ds, draw_str, draw_str_ct, text_x - 1, text_y + 1, effect_color, flags);
|
|
|
|
outFont(font->normal, ds, draw_str, draw_str_ct, text_x, text_y, color, flags);
|
2004-05-01 08:44:00 +00:00
|
|
|
} else { // FONT_NORMAL
|
2005-01-11 00:51:58 +00:00
|
|
|
outFont(font->normal, ds, draw_str, draw_str_ct, text_x, text_y, color, flags);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int Font::outFont(FONT_STYLE * draw_font, SURFACE * ds, const char *draw_str, size_t draw_str_ct,
|
2005-01-11 00:51:58 +00:00
|
|
|
int text_x, int text_y, int color, int flags) {
|
2004-04-30 23:02:23 +00:00
|
|
|
const byte *draw_str_p;
|
|
|
|
byte *c_data_ptr;
|
2004-04-12 21:40:49 +00:00
|
|
|
int c_code;
|
|
|
|
int char_row;
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
byte *output_ptr;
|
|
|
|
byte *output_ptr_min;
|
|
|
|
byte *output_ptr_max;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
int row;
|
|
|
|
int row_limit;
|
|
|
|
|
|
|
|
int c_byte_len;
|
|
|
|
int c_byte;
|
|
|
|
int c_bit;
|
|
|
|
int ct;
|
|
|
|
|
2004-10-30 22:13:48 +00:00
|
|
|
if ((text_x > ds->w) || (text_y > ds->h)) {
|
2004-05-01 08:44:00 +00:00
|
|
|
// Output string can't be visible
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
draw_str_p = (const byte *) draw_str;
|
2004-04-12 21:40:49 +00:00
|
|
|
ct = draw_str_ct;
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Draw string one character at a time, maximum of 'draw_str'_ct
|
|
|
|
// characters, or no limit if 'draw_str_ct' is 0
|
2004-04-12 21:40:49 +00:00
|
|
|
for (; *draw_str_p && (!draw_str_ct || ct); draw_str_p++, ct--) {
|
|
|
|
c_code = *draw_str_p & 0xFFU;
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Translate character
|
2005-01-11 00:51:58 +00:00
|
|
|
if (!(flags & FONT_DONTMAP))
|
|
|
|
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
|
2004-10-27 21:32:28 +00:00
|
|
|
if ((draw_font->fce[c_code].index == 0) && (c_code != FONT_FIRSTCHAR)) {
|
|
|
|
#if FONT_SHOWUNDEFINED
|
|
|
|
if (c_code == FONT_CH_SPACE) {
|
2004-04-12 21:40:49 +00:00
|
|
|
text_x += draw_font->fce[c_code].tracking;
|
|
|
|
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 )
|
2004-04-12 21:40:49 +00:00
|
|
|
text_x += draw_font->fce[c_code].tracking;
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
// Get length of character in bytes
|
2004-04-12 21:40:49 +00:00
|
|
|
c_byte_len = ((draw_font->fce[c_code].width - 1) / 8) + 1;
|
2004-10-30 22:13:48 +00:00
|
|
|
row_limit = (ds->h < (text_y + draw_font->hdr.c_height)) ? ds->h : text_y + draw_font->hdr.c_height;
|
2004-04-12 21:40:49 +00:00
|
|
|
char_row = 0;
|
|
|
|
|
|
|
|
for (row = text_y; row < row_limit; row++, char_row++) {
|
2004-05-01 08:44:00 +00:00
|
|
|
// Clip negative rows */
|
2004-04-12 21:40:49 +00:00
|
|
|
if (row < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-10-30 22:13:48 +00:00
|
|
|
output_ptr = (byte *)ds->pixels + (ds->pitch * row) + text_x;
|
|
|
|
output_ptr_min = (byte *)ds->pixels + (ds->pitch * row) + (text_x > 0 ? text_x : 0);
|
|
|
|
output_ptr_max = output_ptr + (ds->pitch - text_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
|
2004-04-12 21:40:49 +00:00
|
|
|
if (output_ptr < output_ptr_min) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
c_data_ptr = draw_font->font_p + char_row * draw_font->hdr.row_length + draw_font->fce[c_code].index;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 08:44:00 +00:00
|
|
|
for (c_byte = 0; c_byte < c_byte_len; c_byte++, c_data_ptr++) {
|
|
|
|
// Check each bit, draw pixel if bit is set
|
|
|
|
for (c_bit = 7; c_bit >= 0 && (output_ptr < output_ptr_max); c_bit--) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if ((*c_data_ptr >> c_bit) & 0x01) {
|
2004-04-30 23:02:23 +00:00
|
|
|
*output_ptr = (byte) color;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
output_ptr++;
|
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
|
2004-04-12 21:40:49 +00:00
|
|
|
text_x += draw_font->fce[c_code].tracking;
|
2004-05-01 08:44:00 +00:00
|
|
|
} // end per-character processing
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Saga
|