File: te_text_layout.cpp

package info (click to toggle)
scummvm 2.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 363,784 kB
  • sloc: cpp: 3,622,060; asm: 27,410; python: 10,528; sh: 10,241; xml: 6,752; java: 5,579; perl: 2,570; yacc: 1,635; javascript: 1,016; lex: 539; makefile: 398; ansic: 378; awk: 275; objc: 82; sed: 11; php: 1
file content (217 lines) | stat: -rw-r--r-- 6,294 bytes parent folder | download
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
/* 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 "tetraedge/tetraedge.h"
#include "tetraedge/te/te_core.h"
#include "tetraedge/te/te_i_loc.h"
#include "tetraedge/te/te_resource_manager.h"
#include "tetraedge/te/te_renderer.h"
#include "tetraedge/te/te_text_layout.h"
#include "tetraedge/te/te_text_layout_xml_parser.h"

namespace Tetraedge {

TeTextLayout::TeTextLayout(): _textSizeProportionalToWidth(0),
_textSizeType(0), _baseFontSize(10) {
}

TeTextLayout::~TeTextLayout() {
}

void TeTextLayout::setInterLine(float val) {
	_base.setInterLine(val);
}

void TeTextLayout::draw() {
	if (!worldVisible())
		return;

	updateSize();
	TeMatrix4x4 matrix = worldTransformationMatrix();
	TeRenderer *renderer = g_engine->getRenderer();
	renderer->pushMatrix();
	renderer->loadMatrix(matrix);
	_base.draw();
	renderer->popMatrix();
	TeLayout::draw();

}

static TeFont3::AlignStyle _alignNameToEnum(const Common::String &name) {
	if (name == "left")
		return TeFont3::AlignLeft;
	else if (name == "right")
		return TeFont3::AlignRight;
	else if (name == "justify")
		return TeFont3::AlignJustify;
	else if (name =="center")
		return TeFont3::AlignCenter;
	warning("Unknown text align style: %s", name.c_str());
	return TeFont3::AlignLeft;
}

void TeTextLayout::setText(const Common::String &val) {
	if (!val.size()) {
		clear();
		_sizeChanged = true;
		return;
	}

	TeILoc *loc = g_engine->getCore()->loc();

	Common::String replaced = val;
	size_t bstart = replaced.find("$(");
	while (bstart != Common::String::npos) {
		size_t bend = replaced.find(")", bstart);
		if (bend == Common::String::npos)
			break;
		const Common::String *replacement = loc->text(replaced.substr(bstart + 2, bend - bstart - 2));
		if (replacement) {
			replaced.replace(bstart, bend - bstart + 1, *replacement);
		}
		bstart = replaced.find("$(", bstart + 1);
	}

	//
	// WORKAROUND: The Syberia credits xml has an unmatched "</t>" at the end..
	// just delete it.
	//
	// Note there is another workaround for the credits xml in the parser.
	//
	size_t tagstart = replaced.find("</t>");
	if (tagstart != Common::String::npos)
		replaced.replace(tagstart, 4, "    ");

	const Common::String xmlDocStr = Common::String::format("<?xml version=\"1.0\" encoding=\"UTF-8\"?><document>%s</document>", replaced.c_str());

	TeTextLayoutXmlParser parser;
	parser.setAllowText();

	if (!parser.loadBuffer((const byte *)(xmlDocStr.c_str()), xmlDocStr.size()))
		error("TeTextLayout::load: failed to load xml.");

	if (!parser.parse())
		error("TeTextLayout::load: failed to parse xml.");

	_base.setText(parser.textContent());
	_base.setGlobalColor(parser.color());
	if (parser.fontSize())
		_baseFontSize = parser.fontSize();

	if (parser.fontFile().size()) {
		Common::Path fontPath(parser.fontFile());
		fontPath = g_engine->getCore()->findFile(fontPath);
		TeIntrusivePtr<TeFont3> font = g_engine->getResourceManager()->getResource<TeFont3>(fontPath);
		//font->load(fontPath); // lazy load this later.
		_base.setFont(0, font);
	}
	if (parser.style().size())
		_base.setAlignStyle(_alignNameToEnum(parser.style()));
	for (uint offset : parser.lineBreaks())
		_base.insertNewLine(offset);
	_sizeChanged = true;
}

void TeTextLayout::setTextSizeType(int type) {
	if (_textSizeType != type) {
		_textSizeType = type;
		_sizeChanged = true;
	}
}

void TeTextLayout::setTextSizeProportionalToWidth(int val) {
	if (_textSizeProportionalToWidth != val) {
		_textSizeProportionalToWidth = val;
		_sizeChanged = true;
	}
}

void TeTextLayout::setWrapMode(TeTextBase2::WrapMode mode) {
	_base.setWrapMode(mode);
}

void TeTextLayout::strikethrough(bool val) {
	_base.strikethrough(val);
}

bool TeTextLayout::strikethrough() const {
	return _base.strikethrough();
}

const Common::String &TeTextLayout::text() const {
	return _base.text();
}

const TeVector2s32 &TeTextLayout::textSize() const {
	return _base.size();
}

void TeTextLayout::updateSize() {
	if (!_sizeChanged)
		return;

	TeLayout::updateSize();

	const TeMatrix4x4 transform = worldTransformationMatrix();

	const TeVector3f32 v1 = transform * TeVector3f32(0, 0, 0);
	const TeVector3f32 v2 = transform * TeVector3f32(1, 0, 0);
	const TeVector3f32 v3 = transform * TeVector3f32(0, 1, 0);

	const TeVector3f32 transformVec((v2 - v1).length(), (v3 - v1).length(), 1.0);
	const TeVector3f32 thisSize = size();
	const TeVector3f32 textSize = thisSize * transformVec;
	const TeVector2s32 textSizeI(textSize.x(), textSize.y());
	_base.setRect(textSizeI);

	float newFontSize = 0;
	if (_textSizeType == 0 || (_textSizeType == 1 && _textSizeProportionalToWidth == 0)) {
		newFontSize = _baseFontSize;
	} else if (_textSizeType == 1) {
		newFontSize = (thisSize.x() / _textSizeProportionalToWidth) * _baseFontSize;
	}

	newFontSize *= transformVec.y();

	_base.setFontSize(newFontSize);
	_base.build();

	TeVector3f32 userSz = userSize();
	const TeVector2s32 baseSz = _base.size();

	if (sizeType() == RELATIVE_TO_PARENT && parent()) {
		if (wrapMode() != TeTextBase2::WrapModeFixed) {
			if (parent()->xSize() != 0.0)
				userSz.x() = ((float)baseSz._x / transformVec.y()) / parent()->xSize();
		}
		if (parent()->ySize() != 0.0)
			userSz.y() = ((float)baseSz._y / transformVec.y()) / parent()->ySize();
	} else if (sizeType() == ABSOLUTE) {
		if (wrapMode() != TeTextBase2::WrapModeFixed) {
			userSz.x() = baseSz._x / transformVec.y();
		}
		userSz.y() = baseSz._y / transformVec.y();
	}

	setSize(userSz);
}

} // end namespace Tetraedge