File: mactext-canvas.h

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 (211 lines) | stat: -rw-r--r-- 6,262 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
/* 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/>.
 *
 */

#ifndef GRAPHICS_MACGUI_MACTEXTCANVAS_H
#define GRAPHICS_MACGUI_MACTEXTCANVAS_H

#include "graphics/macgui/macwindowmanager.h"
#include "graphics/image-archive.h"

namespace Graphics {

class MacText;

struct MacFontRun {
	Common::U32String text;

	uint16 fontId;
	byte textSlant;
	uint16 fontSize;
	uint16 palinfo1;
	uint16 palinfo2;
	uint16 palinfo3;
	uint32 fgcolor;
	const Font *font;
	MacWindowManager *wm;
	Common::String link;  // Substitute to return when hover or click

	MacFontRun() {
		wm = nullptr;
		fontId = textSlant = fontSize = 0;
		palinfo1 = palinfo2 = palinfo3 = 0;
		fgcolor = 0;
		font = nullptr;
	}

	MacFontRun(MacWindowManager *wm_) {
		wm = wm_;
		fontId = textSlant = fontSize = 0;
		palinfo1 = palinfo2 = palinfo3 = 0;
		fgcolor = 0;
		font = nullptr;
	}

	MacFontRun(MacWindowManager *wm_, uint16 fontId_, byte textSlant_, uint16 fontSize_,
			uint16 palinfo1_, uint16 palinfo2_, uint16 palinfo3_) {
		setValues(wm_, fontId_, textSlant_, fontSize_, palinfo1_, palinfo2_, palinfo3_);
	}

	MacFontRun(MacWindowManager *wm_, const Font *font_, byte textSlant_, uint16 fontSize_,
			uint16 palinfo1_, uint16 palinfo2_, uint16 palinfo3_) {
		setValues(wm_, 0, textSlant_, fontSize_, palinfo1_, palinfo2_, palinfo3_);
		font = font_;
	}

	void setValues(MacWindowManager *wm_, uint16 fontId_, byte textSlant_, uint16 fontSize_,
			uint16 palinfo1_, uint16 palinfo2_, uint16 palinfo3_) {
		wm        = wm_;
		fontId    = fontId_;
		textSlant = textSlant_;
		fontSize  = fontSize_;
		palinfo1  = palinfo1_;
		palinfo2  = palinfo2_;
		palinfo3  = palinfo3_;
		fgcolor   = wm_->findBestColor(palinfo1_ & 0xff, palinfo2_ & 0xff, palinfo3_ & 0xff);
		font      = nullptr;
	}

	const Font *getFont();

	const Common::String toString();
	bool equals(MacFontRun &to);

	Common::CodePage getEncoding();
	bool plainByteMode();
	Common::String getEncodedText();

	bool equals(const MacFontRun *y) {
		return (fontId    == y->fontId &&
				textSlant == y->textSlant &&
				fontSize  == y->fontSize &&
				palinfo1  == y->palinfo1 &&
				palinfo2  == y->palinfo2 &&
				palinfo3  == y->palinfo3 &&
				fgcolor   == y->fgcolor);
	}

	void debugPrint();
};

struct MacTextLine;

class MacTextCanvas {
public:
	Common::Array<MacTextLine> _text;
	ManagedSurface *_surface = nullptr, *_shadowSurface = nullptr;
	int _maxWidth = 0;
	int _textMaxWidth = 0;
	int _textMaxHeight = 0;
	TextAlign _textAlignment = kTextAlignLeft;
	int _interLinear = 0;
	int _textShadow = 0;
	MacWindowManager *_wm = nullptr;
	uint32 _tfgcolor = 0;
	uint32 _tbgcolor = 0;
	bool _macFontMode = true;
	MacText *_macText;
	MacFontRun _defaultFormatting;
	ImageArchive _imageArchive;

public:
	~MacTextCanvas();

	void recalcDims();
	void reallocSurface();
	void render(int from, int to);
	void render(int from, int to, int shadow);
	int getAlignOffset(int row);

	/**
	 * Returns line width in pixels. This takes into account chunks.
	 * The result is cached for faster subsequent calls.
	 *
	 * @param line Line number
	 * @param enforce Flag for indicating skipping the cache and computing the width,
	 *                must be called when text gets changed
	 * @param col Compute line width up to specified column, including this column
	 * @return line width in pixels, or 0 for non-existent lines
	 */
	int getLineWidth(int line, bool enforce = false, int col = -1);
	int getLineHeight(int line);
	int getLineCharWidth(int line, bool enforce = false);

	void splitString(const Common::U32String &str, int curLine, MacFontRun &defaultFormatting);
	const Common::U32String::value_type *splitString(const Common::U32String::value_type *s, int curLine, MacFontRun &defaultFormatting);

	void chopChunk(const Common::U32String &str, int *curLinePtr, int indent, int maxWidth);
	Common::U32String getTextChunk(int startRow, int startCol, int endRow, int endCol, bool formatted = false, bool newlines = true);

	/**
	 * Rewraps paragraph containing given text row.
	 * When text is modified, we redo whole thing again without touching
	 * other paragraphs. Also, cursor position is returned in the arguments
	 */
	void reshuffleParagraph(int *row, int *col, MacFontRun &defaultFormatting);
	void setMaxWidth(int maxWidth, MacFontRun &defaultFormatting);

	void debugPrint(const char *prefix = nullptr);

private:
	void processTable(int line, int maxWidth);
	void parsePicExt(const Common::U32String &ext, uint16 &w, uint16 &h, int defpercent);
};

struct MacTextTableRow {
	Common::Array<MacTextCanvas> cells;
	int heght = -1;
};

struct MacTextLine {
	int width = -1;
	int height = -1;
	int minWidth = -1;
	int y = 0;
	int charwidth = -1;
	bool paragraphEnd = false;
	bool wordContinuation = false;
	int indent = 0; // in units
	int firstLineIndent = 0; // in pixels
	Common::Path picfname;
	Common::U32String picalt, pictitle, picext;
	uint16 picpercent = 50;
	Common::Array<MacTextTableRow> *table = nullptr;
	ManagedSurface *tableSurface = nullptr;

	Common::Array<MacFontRun> chunks;

	MacFontRun &firstChunk() { return chunks[0]; }
	MacFontRun &lastChunk() { return chunks[chunks.size() - 1]; }

	/**
	 * Search for a chunk at given char column.
	 *
	 * @param col Requested column, gets modified with in-chunk column
	 * @returns Chunk number
	 *
	 * @note If requested column is too big, returns last character in the line
	 */
	uint getChunkNum(int *col);
};

} // End of namespace Graphics

#endif