aboutsummaryrefslogtreecommitdiff
path: root/engines/tinsel/text.cpp
blob: 3652d6ed3e1b396f71109a995fc74bd6de9f48a2 (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 * Text utilities.
 */

#include "tinsel/dw.h"
#include "tinsel/graphics.h"	// object plotting
#include "tinsel/handle.h"
#include "tinsel/sched.h"	// process scheduler defines
#include "tinsel/strres.h"	// bMultiByte
#include "tinsel/text.h"	// text defines

namespace Tinsel {

/**
 * Returns the length of one line of a string in pixels.
 * @param szStr			String
 * @param pFont			Which font to use for dimensions
 */
int StringLengthPix(char *szStr, const FONT *pFont) {
	int strLen;	// accumulated length of string
	byte	c;
	SCNHANDLE	hImg;

	// while not end of string or end of line
	for (strLen = 0; (c = *szStr) != EOS_CHAR && c != LF_CHAR; szStr++) {
		if (bMultiByte) {
			if (c & 0x80)
				c = ((c & ~0x80) << 8) + *++szStr;
		}
		hImg = FROM_LE_32(pFont->fontDef[c]);

		if (hImg) {
			// there is a IMAGE for this character
			const IMAGE *pChar = (const IMAGE *)LockMem(hImg);

			// add width of font bitmap
			strLen += FROM_LE_16(pChar->imgWidth);
		} else
			// use width of space character
			strLen += FROM_LE_32(pFont->spaceSize);

		// finally add the inter-character spacing
		strLen += FROM_LE_32(pFont->xSpacing);
	}

	// return length of line in pixels - minus inter-char spacing for last character
	strLen -= FROM_LE_32(pFont->xSpacing);
	return (strLen > 0) ? strLen : 0;
}

/**
 * Returns the justified x start position of a line of text.
 * @param szStr			String to output
 * @param xPos			X position of string
 * @param pFont			Which font to use
 * @param mode			Mode flags for the string
 */
int JustifyText(char *szStr, int xPos, const FONT *pFont, int mode) {
	if (mode & TXT_CENTER) {
		// center justify the text

		// adjust x positioning by half the length of line in pixels
		xPos -= StringLengthPix(szStr, pFont) / 2;
	} else if (mode & TXT_RIGHT) {
		// right justify the text

		// adjust x positioning by length of line in pixels
		xPos -= StringLengthPix(szStr, pFont);
	}

	// return text line x start position
	return xPos;
}

/**
 * Main text outputting routine. If a object list is specified a
 * multi-object is created for the whole text and a pointer to the head
 * of the list is returned.
 * @param pList			Object list to add text to
 * @param szStr			String to output
 * @param color		Color for monochrome text
 * @param xPos			X position of string
 * @param yPos			Y position of string
 * @param hFont			Which font to use
 * @param mode			Mode flags for the string
 * @param sleepTime		Sleep time between each character (if non-zero)
 */
OBJECT *ObjectTextOut(OBJECT *pList, char *szStr, int color,
					  int xPos, int yPos, SCNHANDLE hFont, int mode, int sleepTime) {
	int xJustify;	// x position of text after justification
	int yOffset;	// offset to next line of text
	OBJECT *pFirst;	// head of multi-object text list
	OBJECT *pChar = 0;	// object ptr for the character
	byte c;
	SCNHANDLE hImg;
	const IMAGE *pImg;

	// make sure there is a linked list to add text to
	assert(pList);

	// get font pointer
	const FONT *pFont = (const FONT *)LockMem(hFont);

	// init head of text list
	pFirst = NULL;

	// get image for capital W
	assert(pFont->fontDef[(int)'W']);
	pImg = (const IMAGE *)LockMem(FROM_LE_32(pFont->fontDef[(int)'W']));

	// get height of capital W for offset to next line
	yOffset = FROM_LE_16(pImg->imgHeight) & ~C16_FLAG_MASK;

	while (*szStr) {
		// x justify the text according to the mode flags
		xJustify = JustifyText(szStr, xPos, pFont, mode);

		// repeat until end of string or end of line
		while ((c = *szStr) != EOS_CHAR && c != LF_CHAR) {
			if (bMultiByte) {
				if (c & 0x80)
					c = ((c & ~0x80) << 8) + *++szStr;
			}
			hImg = FROM_LE_32(pFont->fontDef[c]);

			if (hImg == 0) {
				// no image for this character

				// add font spacing for a space character
				xJustify += FROM_LE_32(pFont->spaceSize);
			} else {	// printable character

				int aniX, aniY;		// char image animation offsets

				OBJ_INIT oi;
				oi.hObjImg  = FROM_LE_32(pFont->fontInit.hObjImg);
				oi.objFlags = FROM_LE_32(pFont->fontInit.objFlags);
				oi.objID    = FROM_LE_32(pFont->fontInit.objID);
				oi.objX     = FROM_LE_32(pFont->fontInit.objX);
				oi.objY     = FROM_LE_32(pFont->fontInit.objY);
				oi.objZ     = FROM_LE_32(pFont->fontInit.objZ);

				// allocate and init a character object
				if (pFirst == NULL)
					// first time - init head of list
					pFirst = pChar = InitObject(&oi);	// FIXME: endian issue using fontInit!!!
				else
					// chain to multi-char list
					pChar = pChar->pSlave = InitObject(&oi);	// FIXME: endian issue using fontInit!!!

				// convert image handle to pointer
				pImg = (const IMAGE *)LockMem(hImg);

				// fill in character object
				pChar->hImg   = hImg;			// image def
				pChar->width  = FROM_LE_16(pImg->imgWidth);		// width of chars bitmap
				pChar->height = FROM_LE_16(pImg->imgHeight) & ~C16_FLAG_MASK;	// height of chars bitmap
				pChar->hBits  = FROM_LE_32(pImg->hImgBits);		// bitmap

				// check for absolute positioning
				if (mode & TXT_ABSOLUTE)
					pChar->flags |= DMA_ABS;

				// set characters color - only effective for mono fonts
				pChar->constant = color;

				// get Y animation offset
				GetAniOffset(hImg, pChar->flags, &aniX, &aniY);

				// set x position - ignore animation point
				pChar->xPos = intToFrac(xJustify);

				// set y position - adjust for animation point
				pChar->yPos = intToFrac(yPos - aniY);

				if (mode & TXT_SHADOW) {
					// we want to shadow the character
					OBJECT *pShad;

					// allocate a object for the shadow and chain to multi-char list
					pShad = pChar->pSlave = AllocObject();

					// copy the character for a shadow
					CopyObject(pShad, pChar);

					// add shadow offsets to characters position
					pShad->xPos += intToFrac(FROM_LE_32(pFont->xShadow));
					pShad->yPos += intToFrac(FROM_LE_32(pFont->yShadow));

					// shadow is behind the character
					pShad->zPos--;

					// shadow is always mono
					pShad->flags = DMA_CNZ | DMA_CHANGED;

					// check for absolute positioning
					if (mode & TXT_ABSOLUTE)
						pShad->flags |= DMA_ABS;

					// shadow always uses first palette entry
					// should really alloc a palette here also ????
					pShad->constant = 1;

					// add shadow to object list
					InsertObject(pList, pShad);
				}

				// add character to object list
				InsertObject(pList, pChar);

				// move to end of list
				if (pChar->pSlave)
					pChar = pChar->pSlave;

				// add character spacing
				xJustify += FROM_LE_16(pImg->imgWidth);
			}

			// finally add the inter-character spacing
			xJustify += FROM_LE_32(pFont->xSpacing);

			// next character in string
			++szStr;
		}

		// adjust the text y position and add the inter-line spacing
		yPos += yOffset + FROM_LE_32(pFont->ySpacing);

		// check for newline
		if (c == LF_CHAR)
			// next character in string
			++szStr;
	}

	// return head of list
	return pFirst;
}

/**
 * Is there an image for this character in this font?
 * @param hFont	which font to use
 * @param c		character to test
 */
bool IsCharImage(SCNHANDLE hFont, char c) {
	byte c2 = (byte)c;

	// Inventory save game name editor needs to be more clever for
	// multi-byte characters. This bodge will stop it erring.
	if (bMultiByte && (c2 & 0x80))
		return false;

	// get font pointer
	const FONT *pFont = (const FONT *)LockMem(hFont);

	return pFont->fontDef[c2] != 0;
}

} // End of namespace Tinsel