File: TextManager.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (252 lines) | stat: -rw-r--r-- 7,747 bytes parent folder | download | duplicates (2)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * This code is based on the CRAB engine
 *
 * Copyright (c) Arvind Raja Yadav
 *
 * Licensed under MIT
 *
 */

#include "common/file.h"
#include "graphics/font.h"
#include "graphics/screen.h"
#include "graphics/fonts/ttf.h"
#include "crab/crab.h"
#include "crab/GameParam.h"
#include "crab/XMLDoc.h"
#include "crab/text/TextManager.h"

namespace Crab {

using namespace pyrodactyl::text;

//------------------------------------------------------------------------
// Purpose: Initialize, set cache etc
//------------------------------------------------------------------------
void TextManager::init() {
	// First, delete everything that exists
	quit();

	// Load the list of fonts
	XMLDoc fontList(g_engine->_filePath->_font);
	if (fontList.ready()) {
		rapidxml::xml_node<char> *node = fontList.doc()->first_node("fonts");

		loadNum(_cacheSize, "cache_size", node);
		_cache.resize(_cacheSize);
		_oldest = 0;

		if (nodeValid(node->first_node("padding")))
			_padBg.load(node->first_node("padding"));

		for (auto n = node->first_node("font"); n != nullptr; n = n->next_sibling("font")) {
			rapidxml::xml_attribute<char> *id, *path, *size;
			id = n->first_attribute("id");
			path = n->first_attribute("path");
			size = n->first_attribute("size");

			if (id != nullptr && path != nullptr && size != nullptr) {
				uint pos = stringToNumber<uint>(id->value());
				if (_font.size() <= pos)
					_font.resize(pos + 1);
				Common::File *file = new Common::File();
				fileOpen(path->value(), file);
				_font[pos] = Graphics::loadTTFFont(file, DisposeAfterUse::YES, stringToNumber<int>(size->value()));
			}
		}
	}

	_colpool.load(g_engine->_filePath->_colors);
}

void TextManager::reset() {
	_cache.clear();
	_cache.resize(_cacheSize);
}

//------------------------------------------------------------------------
// Purpose: Search cache for rendered text
//------------------------------------------------------------------------
int TextManager::search(const Common::String &text, int col, FontKey fontid) {
	int pos = 0;
	for (auto i = _cache.begin(); i != _cache.end(); ++i, ++pos)
		if (i->_empty == false && i->_text == text && i->EqualCol(col) && i->_font == fontid)
			return pos;

	return -1;
}

int TextManager::findFreeSlot() {
	int pos = 0;
	for (auto i = _cache.begin(); i != _cache.end(); ++i, ++pos)
		if (i->_empty)
			return pos;

	int ret = _oldest;
	_oldest = (_oldest + 1) % _cache.size();
	return ret;
}

//------------------------------------------------------------------------
// Purpose: Render the SDL surface for text
//------------------------------------------------------------------------
Graphics::ManagedSurface *TextManager::renderTextBlended(const FontKey &fKey, const Common::String &text, const int &color) {
	Color pooledColor = _colpool.get(color);
	uint32 col = g_engine->_format->ARGBToColor(255, pooledColor.r, pooledColor.g, pooledColor.b);

	Graphics::ManagedSurface *surf = nullptr;

	if (text.empty()) {
		Common::Rect rec = getFont(fKey)->getBoundingBox(" ");
		int h = rec.height();
		surf = new Graphics::ManagedSurface(rec.width(), h + (h / 2), *g_engine->_format);
		getFont(fKey)->drawString(surf, " ", 0, 0, rec.width(), col);
	} else {
		Common::Rect rec = getFont(fKey)->getBoundingBox(text);
		int h = rec.height();
		surf = new Graphics::ManagedSurface(rec.width(), h + (h / 2), *g_engine->_format);
		getFont(fKey)->drawString(surf, text, 0, 0, rec.width(), col);
	}

	return surf;
}

//------------------------------------------------------------------------
// Purpose: Draw text
//------------------------------------------------------------------------
void TextManager::draw(const int &x, const int &y, const Common::String &text, const int &color,
					   const FontKey &fontk, const Align &align, const bool &background) {
	if (text == " ") return;

	int pos = search(text, color, fontk);
	if (pos == -1) {
		pos = findFreeSlot();
		Graphics::ManagedSurface *surf = renderTextBlended(fontk, text, color);
		_cache[pos]._img.deleteImage();
		_cache[pos]._empty = false;

		_cache[pos]._text = text;
		_cache[pos]._col = color;
		_cache[pos]._font = fontk;

		_cache[pos]._img.load(surf);

		delete surf;
	}

	if (background) {
		_rect.w = _cache[pos]._img.w() + (2 * _padBg.x);
		_rect.h = _cache[pos]._img.h() + (2 * _padBg.y);

		uint32 col = g_engine->_format->ARGBToColor(128, 0, 0, 0);
		Graphics::Surface surf;
		surf.create(_rect.w, _rect.h, *g_engine->_format);
		surf.fillRect(Common::Rect(_rect.w, _rect.h), col);

		if (align == ALIGN_LEFT) {
			_rect.x = x - _padBg.x;
			_rect.y = y - _padBg.y;

			g_engine->_screen->blitFrom(surf, Common::Point(_rect.x, _rect.y));

			_cache[pos]._img.draw(x, y);
		} else if (align == ALIGN_CENTER) {
			_rect.x = x - _cache[pos]._img.w() / 2 - _padBg.x;
			_rect.y = y - _cache[pos]._img.h() / 2 - _padBg.y;

			g_engine->_screen->blitFrom(surf, Common::Point(_rect.x, _rect.y));

			_cache[pos]._img.draw(x - _cache[pos]._img.w() / 2, y - _cache[pos]._img.h() / 2);
		} else {
			_rect.x = x - _cache[pos]._img.w() - _padBg.x;
			_rect.y = y - _padBg.y;

			g_engine->_screen->blitFrom(surf, Common::Point(_rect.x, _rect.y));

			_cache[pos]._img.draw(x - _cache[pos]._img.w(), y);
		}

		surf.free();

	} else {
		if (align == ALIGN_LEFT)
			_cache[pos]._img.draw(x, y);
		else if (align == ALIGN_CENTER)
			_cache[pos]._img.draw(x - _cache[pos]._img.w() / 2, y - _cache[pos]._img.h() / 2);
		else
			_cache[pos]._img.draw(x - _cache[pos]._img.w(), y);
	}
}

void TextManager::draw(const int &x, int y, const Common::String &text, const int &color, const FontKey &fKey, const Align &align,
					   const uint &lineWidth, const uint &lineHeight, const bool &background) {
	for (uint startPos = 0, len = text.size(); startPos < len; y += lineHeight) {
		uint endPos = startPos + 1;
		int lastInterrupt = -1;
		Common::String word;

		while (endPos - startPos <= lineWidth) {
			if (endPos == len || text[endPos] == '`') {
				lastInterrupt = endPos;
				break;
			}

			if (text[endPos] == ' ' || text[endPos] == ',' || text[endPos] == '.')
				lastInterrupt = endPos;

			endPos++;
		}

		if (lastInterrupt >= 0) { // wrap a word around
			for (uint i = 0; i < lastInterrupt - startPos; i++)
				word += text[startPos + i];

			startPos = lastInterrupt + 1;
		} else { // word bigger than line, just thunk
			for (uint i = 0; i < endPos - startPos; i++)
				word += text[startPos + i];

			startPos += lineWidth;
		}

		draw(x, y, word, color, fKey, align, background);
	}
}

//------------------------------------------------------------------------
// Purpose: Quit
//------------------------------------------------------------------------
void TextManager::quit() {
	for (auto &i : _font)
		delete i;

	for (auto &i : _cache) {
		if (i._empty == false) {
			i._img.deleteImage();
			i._empty = true;
		}
	}
}

} // End of namespace Crab