aboutsummaryrefslogtreecommitdiff
path: root/engines/sludge/fonttext.cpp
blob: 6163c445acde2f76b3528ba5e3a7b07548c57ca9 (plain)
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
/* 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 "sludge/allfiles.h"
#include "sludge/fonttext.h"
#include "sludge/graphics.h"
#include "sludge/moreio.h"
#include "sludge/newfatal.h"
#include "sludge/sprites.h"
#include "sludge/sludge.h"
#include "sludge/version.h"

namespace Sludge {

TextManager::TextManager() {
	init();
}

TextManager::~TextManager() {
	kill();
}

void TextManager::init() {
	_theFont.total = 0;
	_theFont.sprites = nullptr;

	_fontHeight = 0;
	_numFontColours = 0;
	_loadedFontNum = 0;
	_fontSpace = -1;

	_pastePalette.init();
	_fontTable.clear();
}

void TextManager::kill() {
	GraphicsManager::forgetSpriteBank(_theFont);
	_pastePalette.kill();
}

bool TextManager::isInFont(const Common::String &theText) {
	if (_fontTable.empty())
		return 0;
	if (theText.empty())
		return 0;

	Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);

	// We don't want to compare strings. Only single characters allowed!
	if (str32.size() > 1)
		return false;

	uint32 c = str32[0];

	// check if font order contains the utf8 char
	return _fontOrder.getU32String().contains(c);
}

int TextManager::stringLength(const Common::String &theText) {
	Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);
	return str32.size();
}

int TextManager::stringWidth(const Common::String &theText) {
	int xOff = 0;

	if (_fontTable.empty())
		return 0;

	Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);

	for (uint i = 0; i < str32.size(); ++i) {
		uint32 c = str32[i];
		xOff += _theFont.sprites[fontInTable(c)].surface.w + _fontSpace;
	}

	return xOff;
}

void TextManager::pasteString(const Common::String &theText, int xOff, int y, SpritePalette &thePal) {
	if (_fontTable.empty())
		return;

	xOff += (int)((float)(_fontSpace >> 1) / g_sludge->_gfxMan->getCamZoom());

	Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);

	for (uint32 i = 0; i < str32.size(); ++i) {
		uint32 c = str32[i];
		Sprite *mySprite = &_theFont.sprites[fontInTable(c)];
		g_sludge->_gfxMan->fontSprite(xOff, y, *mySprite, thePal);
		xOff += (int)((double)(mySprite->surface.w + _fontSpace) / g_sludge->_gfxMan->getCamZoom());
	}
}

void TextManager::pasteStringToBackdrop(const Common::String &theText, int xOff, int y) {
	if (_fontTable.empty())
		return;

	Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);

	xOff += _fontSpace >> 1;
	for (uint32 i = 0; i < str32.size(); ++i) {
		uint32 c = str32[i];
		Sprite *mySprite = &_theFont.sprites[fontInTable(c)];
		g_sludge->_gfxMan->pasteSpriteToBackDrop(xOff, y, *mySprite, _pastePalette);
		xOff += mySprite->surface.w + _fontSpace;
	}
}

void TextManager::burnStringToBackdrop(const Common::String &theText, int xOff, int y) {
	if (_fontTable.empty())
		return;

	Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);

	xOff += _fontSpace >> 1;
	for (uint i = 0; i < str32.size(); ++i) {
		uint32 c = str32[i];
		Sprite *mySprite = &_theFont.sprites[fontInTable(c)];
		g_sludge->_gfxMan->burnSpriteToBackDrop(xOff, y, *mySprite, _pastePalette);
		xOff += mySprite->surface.w + _fontSpace;
	}
}

bool TextManager::loadFont(int filenum, const Common::String &charOrder, int h) {
	_fontOrder.setUTF8String(charOrder);

	g_sludge->_gfxMan->forgetSpriteBank(_theFont);

	_loadedFontNum = filenum;

	// get max value among all utf8 chars
	Common::U32String fontOrderString = _fontOrder.getU32String();

	// create an index table from utf8 char to the index
	if (!_fontTable.empty()) {
		_fontTable.clear();
	}

	for (uint i = 0; i < fontOrderString.size(); ++i) {
		uint32 c = fontOrderString[i];
		_fontTable[c] = i;
	}

	if (!g_sludge->_gfxMan->loadSpriteBank(filenum, _theFont, true)) {
		fatal("Can't load font");
		return false;
	}

	_numFontColours = _theFont.myPalette.total;
	_fontHeight = h;
	return true;
}

// load & save
void TextManager::saveFont(Common::WriteStream *stream) {
	stream->writeByte(!_fontTable.empty());
	if (!_fontTable.empty()) {
		stream->writeUint16BE(_loadedFontNum);
		stream->writeUint16BE(_fontHeight);
		writeString(_fontOrder.getUTF8String(), stream);
	}
	stream->writeSint16LE(_fontSpace);
}

void TextManager::loadFont(int ssgVersion, Common::SeekableReadStream *stream) {
	bool fontLoaded = stream->readByte();
	int fontNum = 0;
	Common::String charOrder = "";
	if (fontLoaded) {
		fontNum = stream->readUint16BE();
		_fontHeight = stream->readUint16BE();

		if (ssgVersion < VERSION(2, 2)) {
			char *tmp = new char[257];
			for (int a = 0; a < 256; a++) {
				int x = stream->readByte();
				tmp[x] = a;
			}
			tmp[256] = 0;
			charOrder = tmp;
			delete []tmp;
		} else {
			charOrder = readString(stream);
		}
	}
	loadFont(fontNum, charOrder, _fontHeight);

	_fontSpace = stream->readSint16LE();
}

} // End of namespace Sludge