scummvm/engines/bladerunner/font.cpp

304 lines
8.2 KiB
C++
Raw Normal View History

2016-10-06 21:23:46 +02:00
/* 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.
*
*/
#include "bladerunner/font.h"
#include "bladerunner/bladerunner.h"
#include "common/debug.h"
namespace BladeRunner {
Font::Font(BladeRunnerEngine *vm) {
_vm = vm;
2016-10-06 21:23:46 +02:00
reset();
}
Font::~Font() {
close();
}
#if SUBTITLES_SUPPORT
#if SUBTITLES_EXTERNAL_FONT
// for external FON font file / subtitles support
bool Font::openFromStream(Common::ScopedPtr<Common::SeekableReadStream> &stream, int screenWidth, int screenHeight, int spacing1, int spacing2, uint16 color) {
reset();
_screenWidth = screenWidth;
_screenHeight = screenHeight;
_spacing1 = spacing1;
_spacing2 = spacing2;
_color = color;
if (!stream) {
return false;
}
_characterCount = stream->readUint32LE();
debug("Font's character count: %d", _characterCount);
_maxWidth = stream->readUint32LE();
_maxHeight = stream->readUint32LE();
_dataSize = stream->readUint32LE();
_data = new uint16[_dataSize];
if (!_data) {
debug("Font::open failed to allocate font buffer");
return false;
}
for (int i = 0; i < _characterCount; i++) {
_characters[i].x = stream->readUint32LE();
_characters[i].y = stream->readUint32LE();
_characters[i].width = stream->readUint32LE();
_characters[i].height = stream->readUint32LE();
_characters[i].dataOffset = stream->readUint32LE();
debug("char::%d character x: %d, y: %d, w: %d, h:%d, do: %d", i, _characters[i].x, _characters[i].y, _characters[i].width, _characters[i].height, _characters[i].dataOffset);
}
for (int i = 0; i < _dataSize; i++) {
_data[i] = stream->readUint16LE();
}
return true;
}
#endif // SUBTITLES_EXTERNAL_FONT
#endif // SUBTITLES_SUPPORT
2016-10-06 21:23:46 +02:00
bool Font::open(const Common::String &fileName, int screenWidth, int screenHeight, int spacing1, int spacing2, uint16 color) {
reset();
_screenWidth = screenWidth;
_screenHeight = screenHeight;
_spacing1 = spacing1;
_spacing2 = spacing2;
_color = color;
Common::ScopedPtr<Common::SeekableReadStream> stream(_vm->getResourceStream(fileName));
if (!stream) {
debug("Font::open failed to open '%s'", fileName.c_str());
return false;
}
_characterCount = stream->readUint32LE();
_maxWidth = stream->readUint32LE();
_maxHeight = stream->readUint32LE();
_dataSize = stream->readUint32LE();
_data = new uint16[_dataSize];
if (!_data) {
debug("Font::open failed to allocate font buffer");
return false;
}
for (int i = 0; i < _characterCount; i++) {
_characters[i].x = stream->readUint32LE();
_characters[i].y = stream->readUint32LE();
_characters[i].width = stream->readUint32LE();
_characters[i].height = stream->readUint32LE();
_characters[i].dataOffset = stream->readUint32LE();
#if SUBTITLES_SUPPORT
#if !SUBTITLES_EXTERNAL_FONT
// special explicit alignment fixes for TAHOMA18 (INTERNAL) font
if(fileName == "TAHOMA18.FON") {
// fix P -> i = 81 (ascii code 80 + 1)
if(i == 81 || i == 72 || i == 74 || i == 75 // P, G, I, J
|| i == 46 // '-'
)
{
_characters[i].x = 0;// from 1
}
if(i == 81 // P
|| i == 83 || i == 84 // R, S,
|| i == 86 // U
|| i == 87 || i == 88 || i == 89 || i == 90 || i == 91 // V, W, X, Y ,Z
) {
_characters[i].y = 7;// from 6 // bring down a pixel
}
}
//debug("char::%d character x: %d, y: %d, w: %d, h:%d, do: %d", i, _characters[i].x, _characters[i].y, _characters[i].width, _characters[i].height, _characters[i].dataOffset);
#endif // SUBTITLES_EXTERNAL_FONT
#endif // SUBTITLES_SUPPORT
}
for (int i = 0; i < _dataSize; i++) {
_data[i] = stream->readUint16LE();
}
2016-10-06 21:23:46 +02:00
return true;
}
void Font::close() {
if (_data != nullptr) {
delete[] _data;
}
reset();
}
void Font::setSpacing(int spacing1, int spacing2) {
if (_data) {
_spacing1 = spacing1;
_spacing2 = spacing2;
}
}
void Font::setColor(uint16 color) {
if (_data && _color != color) {
replaceColor(_color, color);
_color = color;
}
}
void Font::draw(const Common::String &text, Graphics::Surface &surface, int x, int y) const {
if (!_data) {
return;
}
2016-10-06 21:23:46 +02:00
x = CLIP(x, 0, _screenWidth - getTextWidth(text) + 1);
y = CLIP(y, 0, _screenHeight - _maxHeight);
const uint8 *character = (const uint8 *)text.c_str();
while (*character != 0) {
drawCharacter(*character, surface, x, y);
x += _spacing1 + _characters[*character + 1].width;
character++;
2016-10-06 21:23:46 +02:00
}
}
void Font::drawColor(const Common::String &text, Graphics::Surface &surface, int x, int y, uint16 color) {
if (_color != color) {
setColor(color);
}
draw(text, surface, x, y);
}
2018-03-26 23:54:58 +02:00
void Font::drawNumber(int num, Graphics::Surface &surface, int x, int y) const {
char buffer[20];
snprintf(buffer, 20, "%d", num);
draw(buffer, surface, x, y);
}
int Font::getTextWidth(const Common::String &text) const {
const uint8 *character = (const uint8 *)text.c_str();
2016-10-06 21:23:46 +02:00
if (!_data) {
return 0;
}
int totalWidth = 0;
if (*character == 0) {
return 0;
}
while (*character != 0) {
totalWidth += _spacing1 + _characters[*character + 1].width;
2016-10-06 21:23:46 +02:00
character++;
}
return totalWidth - _spacing1;
}
int Font::getTextHeight(const Common::String &text) const {
return _maxHeight;
}
2016-10-06 21:23:46 +02:00
void Font::reset() {
_maxWidth = 0;
_maxHeight = 0;
_characterCount = 0;
_data = nullptr;
_dataSize = 0;
_screenWidth = 0;
_screenHeight = 0;
_spacing1 = 0;
_spacing2 = 0;
_color = 0x7FFF;
_intersperse = 0;
memset(_characters, 0, 256 * sizeof(Character));
2016-10-06 21:23:46 +02:00
}
void Font::replaceColor(uint16 oldColor, uint16 newColor) {
if (!_data || !_dataSize) {
return;
}
for (int i = 0; i < _dataSize; i++) {
if (_data[i] == oldColor) {
_data[i] = newColor;
}
}
}
#if SUBTITLES_SUPPORT
void Font::setBlackColor() {
// to create a font that can be used as a shadow effect
if (!_data || !_dataSize) {
return;
}
for (int i = 0; i < _dataSize; i++) {
//debug("COLOR EXISTING: %d", _data[i]);
if(_data[i] != 32768) { // 0x8000 transparent
_data[i] = 0x0000; // black
}
}
}
#endif // SUBTITLES_SUPPORT
void Font::drawCharacter(const uint8 character, Graphics::Surface &surface, int x, int y) const {
uint8 characterIndex = character + 1;
if (x < 0 || x >= _screenWidth || y < 0 || y >= _screenHeight || !_data || characterIndex >= _characterCount) {
2016-10-06 21:23:46 +02:00
return;
}
uint16 *dstPtr = (uint16 *)surface.getBasePtr(x + _characters[characterIndex].x, y + _characters[characterIndex].y);
uint16 *srcPtr = &_data[_characters[characterIndex].dataOffset];
int width = _characters[characterIndex].width;
int height = _characters[characterIndex].height;
2016-10-06 21:23:46 +02:00
if (_intersperse && y & 1) {
dstPtr += surface.pitch / 2;
2016-10-06 21:23:46 +02:00
}
int endY = height + y - 1;
int currentY = y;
#if BLADERUNNER_RESTORED_CONTENT_GAME
// Temp Bug fix - Return if w h unnaturally big: -- the INTERNAL tahoma18 font is corrupted so it could cause crashes
if(width > 100 || height > 100) {
return;
}
#endif
2016-10-06 21:23:46 +02:00
while (currentY <= endY && currentY < _screenHeight) {
int currentX = x;
int endX = width + x - 1;
while (currentX <= endX && currentX < _screenWidth) {
if ((*srcPtr & 0x8000) == 0) {
2016-10-06 21:23:46 +02:00
*dstPtr = *srcPtr;
}
dstPtr++;
srcPtr++;
currentX++;
}
dstPtr += surface.pitch / 2 - width;
2016-10-06 21:23:46 +02:00
if (_intersperse) {
srcPtr += width;
dstPtr += surface.pitch / 2;
2016-10-06 21:23:46 +02:00
currentY++;
}
currentY++;
}
}
2016-10-06 21:23:46 +02:00
} // End of namespace BladeRunner