1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "bladerunner/font.h"
24 
25 #include "bladerunner/bladerunner.h"
26 
27 #include "common/debug.h"
28 
29 namespace BladeRunner {
30 
Font()31 Font::Font() {
32 	reset();
33 }
34 
~Font()35 Font::~Font() {
36 	close();
37 }
38 
load(BladeRunnerEngine * vm,const Common::String & fileName,int spacing,bool useFontColor)39 Font* Font::load(BladeRunnerEngine *vm, const Common::String &fileName, int spacing, bool useFontColor) {
40 	Font *font = new Font();
41 	font->_spacing = spacing;
42 	font->_useFontColor = useFontColor;
43 
44 	Common::ScopedPtr<Common::SeekableReadStream> stream(vm->getResourceStream(fileName));
45 	if (!stream) {
46 		warning("Font::open failed to open '%s'", fileName.c_str());
47 		delete font;
48 		return nullptr;
49 	}
50 
51 	font->_characterCount = stream->readUint32LE();
52 	font->_maxWidth = stream->readUint32LE();
53 	font->_maxHeight = stream->readUint32LE();
54 	font->_dataSize = stream->readUint32LE();
55 	font->_data = new uint16[font->_dataSize];
56 	if (!font->_data) {
57 		warning("Font::open failed to allocate font buffer");
58 		delete font;
59 		return nullptr;
60 	}
61 
62 	font->_characters.resize(font->_characterCount);
63 	for (uint32 i = 0; i < font->_characterCount; ++i) {
64 		font->_characters[i].x = stream->readUint32LE();
65 		font->_characters[i].y = stream->readUint32LE();
66 		font->_characters[i].width = stream->readUint32LE();
67 		font->_characters[i].height = stream->readUint32LE();
68 		font->_characters[i].dataOffset = stream->readUint32LE();
69 	}
70 
71 	for (int i = 0; i < font->_dataSize; ++i) {
72 		font->_data[i] = stream->readUint16LE();
73 	}
74 
75 	return font;
76 }
77 
close()78 void Font::close() {
79 	if (_data != nullptr) {
80 		delete[] _data;
81 	}
82 	reset();
83 }
84 
reset()85 void Font::reset() {
86 	_maxWidth = 0;
87 	_maxHeight = 0;
88 	_characterCount = 0;
89 	_data = nullptr;
90 	_dataSize = 0;
91 	_screenWidth = 0;
92 	_screenHeight = 0;
93 	_spacing = 0;
94 	_useFontColor = false;
95 
96 	_characters.clear();
97 }
98 
getFontHeight() const99 int Font::getFontHeight() const {
100 	return _maxHeight;
101 }
102 
getMaxCharWidth() const103 int Font::getMaxCharWidth() const {
104 	return _maxWidth;
105 }
106 
getCharWidth(uint32 chr) const107 int Font::getCharWidth(uint32 chr) const {
108 	if (chr >= _characterCount) {
109 		return 0;
110 	}
111 	return _characters[chr + 1].width + _spacing;
112 }
113 
drawChar(Graphics::Surface * dst,uint32 chr,int x,int y,uint32 color) const114 void Font::drawChar(Graphics::Surface *dst, uint32 chr, int x, int y, uint32 color) const {
115 	uint32 characterIndex = chr + 1;
116 	if (x < 0 || x >= dst->w || y < 0 || y >= dst->h || !_data || characterIndex >= _characterCount) {
117 		return;
118 	}
119 
120 	uint16 *srcPtr = &_data[_characters[characterIndex].dataOffset];
121 	int width = _characters[characterIndex].width;
122 	int height = _characters[characterIndex].height;
123 
124 	int endY = height + y - 1;
125 	int currentY = y;
126 
127 	// FIXME/TODO
128 	// This width and height check were added as a temporary bug fix -- a sanity check which is only needed for the internal TAHOMA18.FON font.
129 	// That font's glyph properties table is corrupted - the start of the file states that there are 0xF7 (=247) entries in the char properties table
130 	// but that table get corrupted past the 176th entry. The image data glyph part of the FON file also only covers the 176 entries.
131 	// So the following if clause-check will return here if the width and height values are unnaturally big.
132 	// The bug only affects debug cases where all character glyph need to be displayed...
133 	// ...or potential custom dialogue / translations that reference characters that are not within the range of ASCII values for the normal Latin characters.
134 	if (width > 100 || height > 100) {
135 		return;
136 	}
137 
138 	while (currentY <= endY && currentY < dst->h) {
139 		int currentX = x;
140 		int endX = width + x - 1;
141 		while (currentX <= endX && currentX < dst->w) {
142 			uint8 a, r, g, b;
143 			getGameDataColor(*srcPtr, a, r, g, b);
144 			if (!a) { // Alpha is inversed
145 				uint32 outColor = color;
146 				if (_useFontColor) {
147 					// Ignore the alpha in the output as it is inversed in the input
148 					outColor = dst->format.RGBToColor(r, g, b);
149 				}
150 				void *dstPtr = dst->getBasePtr(CLIP(currentX + _characters[characterIndex].x, 0, dst->w - 1), CLIP(currentY + _characters[characterIndex].y, 0, dst->h - 1));
151 				drawPixel(*dst, dstPtr, outColor);
152 			}
153 			++srcPtr;
154 			++currentX;
155 		}
156 		++currentY;
157 	}
158 }
159 
160 } // End of namespace BladeRunner
161