File: text.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 (331 lines) | stat: -rw-r--r-- 8,707 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* 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/>.
 *
 */

#include "engines/stark/visual/text.h"

#include "graphics/font.h"
#include "graphics/pixelformat.h"
#include "graphics/surface.h"

#include "engines/stark/debug.h"
#include "engines/stark/gfx/driver.h"
#include "engines/stark/gfx/surfacerenderer.h"
#include "engines/stark/gfx/bitmap.h"
#include "engines/stark/scene.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/settings.h"

#include "common/util.h"

namespace Stark {

VisualText::VisualText(Gfx::Driver *gfx) :
		Visual(TYPE),
		_gfx(gfx),
		_bitmap(nullptr),
		_color(Gfx::Color(0, 0, 0)),
		_backgroundColor(Gfx::Color(0, 0, 0, 0)),
		_align(Graphics::kTextAlignLeft),
		_targetWidth(600),
		_targetHeight(600),
		_fontType(FontProvider::kBigFont),
		_fontCustomIndex(-1) {
	_surfaceRenderer = _gfx->createSurfaceRenderer();
	_surfaceRenderer->setNoScalingOverride(true);
	_surfaceRenderer->setSnapToGrid(true);
}

VisualText::~VisualText() {
	freeBitmap();
	delete _surfaceRenderer;
}

Common::Rect VisualText::getRect() {
	if (!_bitmap) {
		createBitmap();
	}
	return _originalRect;
}

void VisualText::setText(const Common::String &text) {
	if (_text != text) {
		freeBitmap();
		_text = text;
	}
}

void VisualText::setColor(const Gfx::Color &color) {
	if (_color == color) {
		return;
	}

	freeBitmap();
	_color = color;
}

void VisualText::setBackgroundColor(const Gfx::Color &color) {
	if (color == _backgroundColor) {
		return;
	}

	freeBitmap();
	_backgroundColor = color;
}

void VisualText::setAlign(Graphics::TextAlign align) {
	if (align != _align) {
		freeBitmap();
		_align = align;
	}
}

void VisualText::setTargetWidth(uint32 width) {
	if (width != _targetWidth) {
		freeBitmap();
		_targetWidth = width;
	}
}

void VisualText::setTargetHeight(uint32 height) {
	if (height != _targetHeight) {
		freeBitmap();
		_targetHeight = height;
	}
}

void VisualText::setFont(FontProvider::FontType type, int32 customFontIndex) {
	if (type != _fontType || customFontIndex != _fontCustomIndex) {
		freeBitmap();
		_fontType = type;
		_fontCustomIndex = customFontIndex;
	}
}

/**
 * Convert a color component from the perceptual to the linear color space
 *
 * Gamma 1.8 seems to be an accepted value in the font rendering domain.
 */
static float srgbToLinear(float x) {
	if (x <= 0.0f)
		return 0.0f;
	else if (x >= 1.0f)
		return 1.0f;
	else
		return powf(x, 1.8f);
}

/** Convert a color component from the linear to the perceptual color space */
static float linearToSrgb(float x) {
	if (x <= 0.0f)
		return 0.0f;
	else if (x >= 1.0f)
		return 1.0f;
	else
		return powf(x, 1.0f / 1.8f);
}

/**
 * Multiply the color components of a surface with the alpha component
 *
 * Linear colorspace aware variant. Anti-aliased fonts at small sized
 * have a very few solid pixels. It's important the semi-transparent
 * pixels have the right value after the pre-multiplication.
 */
static void multiplyColorWithAlpha(Graphics::Surface *source) {
	assert(source->format == Gfx::Driver::getRGBAPixelFormat());

	for (int y = 0; y < source->h; y++) {
		const uint8 *src = (const uint8 *) source->getBasePtr(0, y);
		uint8 *dst = (uint8 *) source->getBasePtr(0, y);

		for (int x = 0; x < source->w; x++) {
			uint8 a, r, g, b;
			r = *src++;
			g = *src++;
			b = *src++;
			a = *src++;

			if (a == 0) {
				r = 0;
				g = 0;
				b = 0;

			} else if (a != 0xFF) {
				float aFloat = a / 255.f;
				float linearR = srgbToLinear(r / 255.f);
				float linearG = srgbToLinear(g / 255.f);
				float linearB = srgbToLinear(b / 255.f);

				linearR *= aFloat;
				linearG *= aFloat;
				linearB *= aFloat;

				r = linearToSrgb(linearR) * 255.f;
				g = linearToSrgb(linearG) * 255.f;
				b = linearToSrgb(linearB) * 255.f;
			}

			*dst++ = r;
			*dst++ = g;
			*dst++ = b;
			*dst++ = a;
		}
	}
}

/**
 * Blend a grayscale surface with a color
 *
 * Color space aware version.
 */
static void blendWithColor(Graphics::Surface *source, const Gfx::Color &color) {
	assert(source->format == Gfx::Driver::getRGBAPixelFormat());

	float sRL = srgbToLinear(color.r / 255.f);
	float sGL = srgbToLinear(color.g / 255.f);
	float sBL = srgbToLinear(color.b / 255.f);

	for (int y = 0; y < source->h; y++) {
		const uint8 *src = (const uint8 *) source->getBasePtr(0, y);
		uint8 *dst = (uint8 *) source->getBasePtr(0, y);

		for (int x = 0; x < source->w; x++) {
			uint8 a, r, g, b;
			r = *src++;
			g = *src++;
			b = *src++;
			src++;

			a = r;
			if (a != 255) {
				float aFloat = a / 255.f;
				r = linearToSrgb(sRL * aFloat) * 255.f;
				g = linearToSrgb(sGL * aFloat) * 255.f;
				b = linearToSrgb(sBL * aFloat) * 255.f;

			} else {
				r = color.r;
				g = color.g;
				b = color.b;
			}

			*dst++ = r;
			*dst++ = g;
			*dst++ = b;
			*dst++ = a;
		}
	}
}

void VisualText::createBitmap() {
	Common::CodePage codePage = StarkSettings->getTextCodePage();
	Common::U32String unicodeText = Common::convertToU32String(_text.c_str(), codePage);

	// Get the font and required metrics
	const Graphics::Font *font = StarkFontProvider->getScaledFont(_fontType, _fontCustomIndex);
	uint scaledLineHeight = StarkFontProvider->getScaledFontHeight(_fontType, _fontCustomIndex);
	uint originalLineHeight = StarkFontProvider->getOriginalFontHeight(_fontType, _fontCustomIndex);
	uint maxScaledLineWidth = StarkGfx->scaleWidthOriginalToCurrent(_targetWidth);

	// Word wrap the text
	Common::Array<Common::U32String> lines;
	font->wordWrapText(unicodeText, maxScaledLineWidth, lines);

	// Use the actual font bounding box to prevent text from being cut off
	Common::Rect scaledRect;
	if (!lines.empty()) {
		scaledRect = font->getBoundingBox(lines[0]);
		for (uint i = 1; i < lines.size(); i++) {
			scaledRect.extend(font->getBoundingBox(lines[i], 0, scaledLineHeight * i));
		}
	}

	// Make sure lines have approximately consistent height regardless of the characters they use
	scaledRect.bottom = MAX<int16>(scaledRect.bottom, scaledLineHeight * lines.size());

	if (!isBlank()) {
		_originalRect.right = StarkGfx->scaleWidthCurrentToOriginal(scaledRect.right);
		_originalRect.bottom = originalLineHeight * lines.size();
	} else {
		// For Empty text, preserve the original width and height for being used as clicking area
		_originalRect.right = _targetWidth;
		_originalRect.bottom = _targetHeight;
	}

	// Create a surface to render to
	Graphics::Surface surface;
	surface.create(scaledRect.right, scaledRect.bottom, Gfx::Driver::getRGBAPixelFormat());

	// First render the text as white on a black background to produce an alpha mask
	uint32 black = surface.format.ARGBToColor(0xFF, 0, 0, 0);
	uint32 white = surface.format.ARGBToColor(0xFF, 0xFF, 0xFF, 0xFF);

	surface.fillRect(Common::Rect(surface.w, surface.h), black);

	// Render the lines to the surface
	for (uint i = 0; i < lines.size(); i++) {
		font->drawString(&surface, lines[i], 0, scaledLineHeight * i, surface.w, white, _align, 0, false);
	}

	// Blend the text color with the alpha mask to produce an image of the text
	// of the correct color. Anti-aliased pixels use the full alpha range.
	blendWithColor(&surface, _color);
	multiplyColorWithAlpha(&surface);

	// Create a bitmap from the surface
	_bitmap = _gfx->createBitmap(&surface);
	_bitmap->setSamplingFilter(Gfx::Bitmap::kNearest);

	surface.free();
}

void VisualText::freeBitmap() {
	delete _bitmap;
	_bitmap = nullptr;
}

void VisualText::render(const Common::Point &position) {
	if (!_bitmap) {
		createBitmap();
	}

	if (_backgroundColor.a != 0) {
		_surfaceRenderer->fill(_backgroundColor, position, _bitmap->width(), _bitmap->height());
	}

	_surfaceRenderer->render(_bitmap, position);
}

void VisualText::reset() {
	freeBitmap();
}

bool VisualText::isBlank() {
	for (uint i = 0; i < _text.size(); ++i) {
		if (!Common::isSpace(_text[i])) {
			return false;
		}
	}
	return true;
}

} // End of namespace Stark