File: stxt.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (153 lines) | stat: -rw-r--r-- 5,007 bytes parent folder | download | duplicates (3)
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
/* 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 "common/substream.h"

#include "director/director.h"
#include "director/cast.h"
#include "director/stxt.h"

namespace Director {

Stxt::Stxt(Cast *cast, Common::SeekableReadStreamEndian &textStream) : _cast(cast) {
	// TODO: Side effects on textStream make this a little hard to understand in context?

	_textType = kTextTypeFixed;
	_textAlign = kTextAlignLeft;
	_textShadow = kSizeNone;
	_unk1f = _unk2f = 0;
	_unk3f = 0;
	_size = textStream.size();

	// D4+ variant
	if (textStream.size() == 0)
		return;

	uint32 offset = textStream.readUint32();
	if (offset != 12) {
		error("Stxt init: unhandled offset");
		return;
	}
	uint32 strLen = textStream.readUint32();
	uint32 dataLen = textStream.readUint32();
	Common::String text = textStream.readString(0, strLen);
	debugC(3, kDebugText, "Stxt init: offset: %d strLen: %d dataLen: %d textlen: %u", offset, strLen, dataLen, text.size());

	// TODO: Before applying formatting and decoding the text to a U32String,
	// check if the following hold true:
	// - The engine platform doesn't match the file platform
	// - The movie has a valid font table (FXmp)
	// - The font table has a mapping between the two platforms
	// - The text has sections written in a font without a Map None qualifier
	// If yes, then just those sections should be preprocessed by churning the
	// bytes through the appropriate mapping (e.g. if running a Mac file on
	// Windows, use the "Mac: => Win:" rules).
	// Confirmed in real Director 4 that these sections are preprocessed in
	// the cast member on startup and not faked at text render time.

	uint16 formattingCount = textStream.readUint16();
	uint32 prevPos = 0;

	debugC(3, kDebugText, "Stxt init: formattingCount: %u", formattingCount);

	Common::U32String logText;

	while (formattingCount) {
		uint16 currentFont = _style.fontId;
		_style.read(textStream, _cast);

		assert(prevPos <= _style.formatStartOffset);  // If this is triggered, we have to implement sorting

		Common::String textPart;
		while (prevPos != _style.formatStartOffset) {
			char f = text.firstChar();
			textPart += f;
			text.deleteChar(0);

			if (f == '\001')	// Insert two \001s as a replacement
				_ftext += '\001';

			prevPos++;
		}
		_rtext += textPart;
		Common::CodePage encoding = detectFontEncoding(cast->_platform, currentFont);
		Common::U32String u32TextPart(textPart, encoding);
		_ptext += u32TextPart;
		_ftext += u32TextPart;
		logText += Common::toPrintable(u32TextPart);

		Common::String format = Common::String::format("\001\016%04x%02x%04x%04x%04x%04x", _style.fontId, _style.textSlant, _style.fontSize, _style.r, _style.g, _style.b);
		_ftext += format;
		logText += Common::toPrintable(format);

		formattingCount--;
	}

	_rtext += text;
	Common::CodePage encoding = detectFontEncoding(cast->_platform, _style.fontId);
	Common::U32String u32Text(text, encoding);
	_ptext += u32Text;
	_ftext += u32Text;
	logText += Common::toPrintable(u32Text);

	debugC(4, kDebugText, "#### text:\n%s\n####", logText.encode(Common::kUtf8).c_str());
}

FontStyle::FontStyle() {
	formatStartOffset = 0;
	height = 0;
	ascent = 0;

	fontId = 0;
	textSlant = 0;

	fontSize = 12;

	r = g = b = 0;
}

void FontStyle::read(Common::ReadStreamEndian &stream, Cast *cast) {
	formatStartOffset = stream.readUint32();
	uint16 originalHeight = height = stream.readUint16();
	ascent = stream.readUint16();

	uint16 originalFontId = fontId = stream.readUint16();
	textSlant = stream.readByte();
	stream.readByte(); // padding
	fontSize = stream.readUint16();

	r = stream.readUint16();
	g = stream.readUint16();
	b = stream.readUint16();

	if (cast->_fontMap.contains(originalFontId)) {
		FontMapEntry *info = cast->_fontMap[originalFontId];
		fontId = info->toFont;
		if (info->sizeMap.contains(originalHeight)) {
			height = info->sizeMap[height];
		}
	}

	debugC(3, kDebugLoading, "FontStyle::read(): formatStartOffset: %d, height: %d -> %d ascent: %d, fontId: %d -> %d, textSlant: %d, fontSize: %d, r: %x g: %x b: %x",
			formatStartOffset, originalHeight, height, ascent, originalFontId, fontId, textSlant, fontSize, r, g, b);
}

} // End of namespace Director