aboutsummaryrefslogtreecommitdiff
path: root/engines/zvision/text/text.cpp
blob: 4af3b967f323a615eac8389dcac0f550fbe89787 (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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/* 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 "common/scummsys.h"
#include "common/file.h"
#include "common/tokenizer.h"
#include "common/debug.h"
#include "common/rect.h"
#include "graphics/fontman.h"
#include "graphics/colormasks.h"
#include "graphics/surface.h"
#include "graphics/font.h"
#include "graphics/fonts/ttf.h"

#include "zvision/text/text.h"
#include "zvision/graphics/render_manager.h"
#include "zvision/text/truetype_font.h"
#include "zvision/scripting/script_manager.h"

namespace ZVision {

cTxtStyle::cTxtStyle() {
	_fontname = "Arial";
	_blue = 255;
	_green = 255;
	_red = 255;
	_bold = false;
#if 0
	_newline = false;
	_escapement = 0;
#endif
	_italic = false;
	_justify = TXT_JUSTIFY_LEFT;
	_size = 12;
#if 0
	_skipcolor = false;
#endif
	_strikeout = false;
	_underline = false;
	_statebox = 0;
	_sharp = false;
}

txtReturn cTxtStyle::parseStyle(const Common::String &strin, int16 ln) {
	Common::String buf = Common::String(strin.c_str(), ln);

	int8 retval = TXT_RET_NOTHING;

	Common::StringTokenizer tokenizer(buf, " ");
	Common::String token;

	while (!tokenizer.empty()) {
		token = tokenizer.nextToken();

		if (token.matchString("font", true)) {
			token = tokenizer.nextToken();
			if (token[0] == '"') {
				Common::String _tmp = Common::String(token.c_str() + 1);

				while (token.lastChar() != '"' && !tokenizer.empty()) {
					token = tokenizer.nextToken();
					_tmp += " " + token;
				}

				if (_tmp.lastChar() == '"')
					_tmp.deleteLastChar();

				_fontname = _tmp;
			} else {
				if (!tokenizer.empty())
					_fontname = token;
			}
			retval |= TXT_RET_FNTCHG;

		} else if (token.matchString("blue", true)) {
			if (!tokenizer.empty()) {
				token = tokenizer.nextToken();
				int32 tmp = atoi(token.c_str());
				if (_blue != tmp) {
					_blue = tmp;
					retval |= TXT_RET_FNTSTL;
				}
			}
		} else if (token.matchString("red", true)) {
			if (!tokenizer.empty()) {
				token = tokenizer.nextToken();
				int32 tmp = atoi(token.c_str());
				if (_red != tmp) {
					_red = tmp;
					retval |= TXT_RET_FNTSTL;
				}
			}
		} else if (token.matchString("green", true)) {
			if (!tokenizer.empty()) {
				token = tokenizer.nextToken();
				int32 tmp = atoi(token.c_str());
				if (_green != tmp) {
					_green = tmp;
					retval |= TXT_RET_FNTSTL;
				}
			}
		} else if (token.matchString("newline", true)) {
#if 0
			if ((retval & TXT_RET_NEWLN) == 0)
				_newline = 0;

			_newline++;
#endif
			retval |= TXT_RET_NEWLN;
		} else if (token.matchString("point", true)) {
			if (!tokenizer.empty()) {
				token = tokenizer.nextToken();
				int32 tmp = atoi(token.c_str());
				if (_size != tmp) {
					_size = tmp;
					retval |= TXT_RET_FNTCHG;
				}
			}
		} else if (token.matchString("escapement", true)) {
			if (!tokenizer.empty()) {
				token = tokenizer.nextToken();
#if 0
				int32 tmp = atoi(token.c_str());
				_escapement = tmp;
#endif
			}
		} else if (token.matchString("italic", true)) {
			if (!tokenizer.empty()) {
				token = tokenizer.nextToken();
				if (token.matchString("on", true)) {
					if (_italic != true) {
						_italic = true;
						retval |= TXT_RET_FNTSTL;
					}
				} else if (token.matchString("off", true)) {
					if (_italic != false) {
						_italic = false;
						retval |= TXT_RET_FNTSTL;
					}
				}
			}
		} else if (token.matchString("underline", true)) {
			if (!tokenizer.empty()) {
				token = tokenizer.nextToken();
				if (token.matchString("on", true)) {
					if (_underline != true) {
						_underline = true;
						retval |= TXT_RET_FNTSTL;
					}
				} else if (token.matchString("off", true)) {
					if (_underline != false) {
						_underline = false;
						retval |= TXT_RET_FNTSTL;
					}
				}
			}
		} else if (token.matchString("strikeout", true)) {
			if (!tokenizer.empty()) {
				token = tokenizer.nextToken();
				if (token.matchString("on", true)) {
					if (_strikeout != true) {
						_strikeout = true;
						retval |= TXT_RET_FNTSTL;
					}
				} else if (token.matchString("off", true)) {
					if (_strikeout != false) {
						_strikeout = false;
						retval |= TXT_RET_FNTSTL;
					}
				}
			}
		} else if (token.matchString("bold", true)) {
			if (!tokenizer.empty()) {
				token = tokenizer.nextToken();
				if (token.matchString("on", true)) {
					if (_bold != true) {
						_bold = true;
						retval |= TXT_RET_FNTSTL;
					}
				} else if (token.matchString("off", true)) {
					if (_bold != false) {
						_bold = false;
						retval |= TXT_RET_FNTSTL;
					}
				}
			}
		} else if (token.matchString("skipcolor", true)) {
			if (!tokenizer.empty()) {
				token = tokenizer.nextToken();
#if 0
				if (token.matchString("on", true)) {
					_skipcolor = true;
				} else if (token.matchString("off", true)) {
					_skipcolor = false;
				}
#endif
			}
		} else if (token.matchString("image", true)) {
			// Not used
		} else if (token.matchString("statebox", true)) {
			if (!tokenizer.empty()) {
				token = tokenizer.nextToken();
				_statebox = atoi(token.c_str());
				retval |= TXT_RET_HASSTBOX;
			}
		} else if (token.matchString("justify", true)) {
			if (!tokenizer.empty()) {
				token = tokenizer.nextToken();
				if (token.matchString("center", true))
					_justify = TXT_JUSTIFY_CENTER;
				else if (token.matchString("left", true))
					_justify = TXT_JUSTIFY_LEFT;
				else if (token.matchString("right", true))
					_justify = TXT_JUSTIFY_RIGHT;
			}
		}
	}
	return (txtReturn)retval;
}

void cTxtStyle::readAllStyle(const Common::String &txt) {
	int16 startTextPosition = -1;
	int16 endTextPosition = -1;

	for (uint16 i = 0; i < txt.size(); i++) {
		if (txt[i] == '<')
			startTextPosition = i;
		else if (txt[i] == '>') {
			endTextPosition = i;
			if (startTextPosition != -1)
				if ((endTextPosition - startTextPosition - 1) > 0)
					parseStyle(Common::String(txt.c_str() + startTextPosition + 1), endTextPosition - startTextPosition - 1);
		}

	}
}

void cTxtStyle::setFontStyle(StyledTTFont &font) {
	uint tempStyle = 0;

	if (_bold)
		tempStyle |= StyledTTFont::STTF_BOLD;

	if (_italic)
		tempStyle |= StyledTTFont::STTF_ITALIC;

	if (_underline)
		tempStyle |= StyledTTFont::STTF_UNDERLINE;

	if (_strikeout)
		tempStyle |= StyledTTFont::STTF_STRIKEOUT;

	if (_sharp)
		tempStyle |= StyledTTFont::STTF_SHARP;

	font.setStyle(tempStyle);
}

void cTxtStyle::setFont(StyledTTFont &font) {
	uint tempStyle = 0;

	if (_bold)
		tempStyle |= StyledTTFont::STTF_BOLD;

	if (_italic)
		tempStyle |= StyledTTFont::STTF_ITALIC;

	if (_underline)
		tempStyle |= StyledTTFont::STTF_UNDERLINE;

	if (_strikeout)
		tempStyle |= StyledTTFont::STTF_STRIKEOUT;

	if (_sharp)
		tempStyle |= StyledTTFont::STTF_SHARP;

	font.loadFont(_fontname, _size, tempStyle);
}

Graphics::Surface *TextRenderer::render(StyledTTFont &fnt, const Common::String &txt, cTxtStyle &style) {
	style.setFontStyle(fnt);
	uint32 clr = _engine->_resourcePixelFormat.RGBToColor(style._red, style._green, style._blue);
	return fnt.renderSolidText(txt, clr);
}

void TextRenderer::drawTxtWithJustify(const Common::String &txt, StyledTTFont &fnt, uint32 color, Graphics::Surface &dst, int lineY, txtJustify justify) {
	if (justify == TXT_JUSTIFY_LEFT)
		fnt.drawString(&dst, txt, 0, lineY, dst.w, color, Graphics::kTextAlignLeft);
	else if (justify == TXT_JUSTIFY_CENTER)
		fnt.drawString(&dst, txt, 0, lineY, dst.w, color, Graphics::kTextAlignCenter);
	else if (justify == TXT_JUSTIFY_RIGHT)
		fnt.drawString(&dst, txt, 0, lineY, dst.w, color, Graphics::kTextAlignRight);
}

int32 TextRenderer::drawTxt(const Common::String &txt, cTxtStyle &fontStyle, Graphics::Surface &dst) {
	StyledTTFont font(_engine);
	fontStyle.setFont(font);

	uint32 clr = _engine->_resourcePixelFormat.RGBToColor(fontStyle._red, fontStyle._green, fontStyle._blue);

	int16 w;

	w = font.getStringWidth(txt);

	drawTxtWithJustify(txt, font, clr, dst, 0, fontStyle._justify);

	return w;
}

void TextRenderer::drawTxtInOneLine(const Common::String &text, Graphics::Surface &dst) {
	const int16 TXT_CFG_TEXTURES_LINES = 256; // For now I don't want remake it
	const int TXT_CFG_TEXTURES_PER_LINE = 6;
	cTxtStyle style, style2;
	int16 startTextPosition = -1;
	int16 endTextPosition = -1;
	int16 i = 0;
	int16 dx = 0, dy = 0;
	int16 textPixelWidth;
	int16 textPosition = 0;
	Common::String buf;
	Common::String buf2;

	Graphics::Surface *TxtSurfaces[TXT_CFG_TEXTURES_LINES][TXT_CFG_TEXTURES_PER_LINE];
	int16 currentline = 0, currentlineitm = 0;

	int TxtJustify[TXT_CFG_TEXTURES_LINES];
	int TxtPoint[TXT_CFG_TEXTURES_LINES];

	for (int16 k = 0; k < TXT_CFG_TEXTURES_LINES; k++) {
		TxtPoint[k] = 0;
		for (int j = 0; j < TXT_CFG_TEXTURES_PER_LINE; j++)
			TxtSurfaces[k][j] = NULL;
	}

	int16 stringlen = text.size();

	StyledTTFont font(_engine);

	style.setFont(font);

	int16 prevbufspace = 0, prevtxtspace = 0;

	while (i < stringlen) {
		TxtJustify[currentline] = style._justify;
		if (text[i] == '<') {
			int16 ret = 0;

			startTextPosition = i;
			while (i < stringlen && text[i] != '>')
				i++;
			endTextPosition = i;
			if (startTextPosition != -1)
				if ((endTextPosition - startTextPosition - 1) > 0) {
					style2 = style;
					ret = style.parseStyle(Common::String(text.c_str() + startTextPosition + 1), endTextPosition - startTextPosition - 1);
				}

			if (ret & (TXT_RET_FNTCHG | TXT_RET_FNTSTL | TXT_RET_NEWLN)) {
				if (buf.size() > 0) {
					textPixelWidth = font.getStringWidth(buf);

					TxtSurfaces[currentline][currentlineitm] = render(font, buf, style2);
					TxtPoint[currentline] = MAX(font.getFontHeight(), TxtPoint[currentline]);

					currentlineitm++;

					buf.clear();
					prevbufspace = 0;
					textPosition = 0;
					dx += textPixelWidth;

				}
				if (ret & TXT_RET_FNTCHG) {
					style.setFont(font);
				}
				if (ret & TXT_RET_FNTSTL)
					style.setFontStyle(font);

				if (ret & TXT_RET_NEWLN) {
					currentline++;
					currentlineitm = 0;
					dx = 0;
				}
			}

			if (ret & TXT_RET_HASSTBOX) {
				Common::String buf3;
				buf3 = Common::String::format("%d", _engine->getScriptManager()->getStateValue(style._statebox));
				buf += buf3;
				textPosition += buf3.size();
			}

		} else {

			buf += text[i];
			textPosition++;

			if (text[i] == ' ') {
				prevbufspace = textPosition - 1;
				prevtxtspace = i;
			}

			if (font.isLoaded()) {
				textPixelWidth = font.getStringWidth(buf);
				if (textPixelWidth + dx > dst.w) {
					if (prevbufspace == 0) {
						prevtxtspace = i;
						prevbufspace = textPosition - 1;
					}
					buf2 = Common::String(buf.c_str(), prevbufspace + 1);

					if (buf2.size() > 0) {
						TxtSurfaces[currentline][currentlineitm] = render(font, buf2, style);
						TxtPoint[currentline] = MAX(font.getFontHeight(), TxtPoint[currentline]);
					}

					buf.clear();
					i = prevtxtspace;
					prevbufspace = 0;
					textPosition = 0;
					currentline++;
					currentlineitm = 0;
					dx = 0;
				}
			}
		}
		i++;
	}

	if (buf.size() > 0) {
		TxtSurfaces[currentline][currentlineitm] = render(font, buf, style);
		TxtPoint[currentline] = MAX(font.getFontHeight(), TxtPoint[currentline]);
	}

	dy = 0;
	for (i = 0; i <= currentline; i++) {
		int16 j = 0;
		int16 width = 0;
		while (TxtSurfaces[i][j] != NULL) {
			width += TxtSurfaces[i][j]->w;
			j++;
		}
		dx = 0;
		Common::Rect empty;
		for (int32 jj = 0; jj < j; jj++) {
			if (TxtJustify[i] == TXT_JUSTIFY_LEFT)
				_engine->getRenderManager()->blitSurfaceToSurface(*TxtSurfaces[i][jj], empty, dst, dx, dy + TxtPoint[i] - TxtSurfaces[i][jj]->h, 0);

			else if (TxtJustify[i] == TXT_JUSTIFY_CENTER)
				_engine->getRenderManager()->blitSurfaceToSurface(*TxtSurfaces[i][jj], empty, dst, ((dst.w - width) / 2) + dx,  dy + TxtPoint[i] - TxtSurfaces[i][jj]->h, 0);

			else if (TxtJustify[i] == TXT_JUSTIFY_RIGHT)
				_engine->getRenderManager()->blitSurfaceToSurface(*TxtSurfaces[i][jj], empty, dst, dst.w - width + dx, dy + TxtPoint[i] - TxtSurfaces[i][jj]->h, 0);

			dx += TxtSurfaces[i][jj]->w;
		}

		dy += TxtPoint[i];
	}

	for (i = 0; i < TXT_CFG_TEXTURES_LINES; i++)
		for (int32 j = 0; j < TXT_CFG_TEXTURES_PER_LINE; j++)
			if (TxtSurfaces[i][j] != NULL) {
				TxtSurfaces[i][j]->free();
				delete TxtSurfaces[i][j];
			}
}

Common::String readWideLine(Common::SeekableReadStream &stream) {
	Common::String asciiString;

	while (true) {
		uint32 value = stream.readUint16LE();
		if (stream.eos())
			break;
		// Check for CRLF
		if (value == 0x0A0D) {
			// Read in the extra NULL char
			stream.readByte(); // \0
			// End of the line. Break
			break;
		}

		// Crush each octet pair to a UTF-8 sequence
		if (value < 0x80) {
			asciiString += (char)(value & 0x7F);
		} else if (value >= 0x80 && value < 0x800) {
			asciiString += (char)(0xC0 | ((value >> 6) & 0x1F));
			asciiString += (char)(0x80 | (value & 0x3F));
		} else if (value >= 0x800 && value < 0x10000 && value != 0xCCCC) {
			asciiString += (char)(0xE0 | ((value >> 12) & 0xF));
			asciiString += (char)(0x80 | ((value >> 6) & 0x3F));
			asciiString += (char)(0x80 | (value & 0x3F));
		} else if (value == 0xCCCC) {
			// Ignore, this character is used as newline sometimes
		} else if (value >= 0x10000 && value < 0x200000) {
			asciiString += (char)(0xF0);
			asciiString += (char)(0x80 | ((value >> 12) & 0x3F));
			asciiString += (char)(0x80 | ((value >> 6) & 0x3F));
			asciiString += (char)(0x80 | (value & 0x3F));
		}
	}

	return asciiString;
}

int8 getUtf8CharSize(char chr) {
	if ((chr & 0x80) == 0)
		return 1;
	else if ((chr & 0xE0) == 0xC0)
		return 2;
	else if ((chr & 0xF0) == 0xE0)
		return 3;
	else if ((chr & 0xF8) == 0xF0)
		return 4;
	else if ((chr & 0xFC) == 0xF8)
		return 5;
	else if ((chr & 0xFE) == 0xFC)
		return 6;

	return 1;
}

uint16 readUtf8Char(const char *chr) {
	uint16 result = 0;
	if ((chr[0] & 0x80) == 0)
		result = chr[0];
	else if ((chr[0] & 0xE0) == 0xC0)
		result = ((chr[0] & 0x1F) << 6) | (chr[1] & 0x3F);
	else if ((chr[0] & 0xF0) == 0xE0)
		result = ((chr[0] & 0x0F) << 12) | ((chr[1] & 0x3F) << 6) | (chr[2] & 0x3F);
	else
		result = chr[0];

	return result;
}

} // End of namespace ZVision