aboutsummaryrefslogtreecommitdiff
path: root/engines/lab/text.cpp
blob: 92711acf5df88452fe339891b7f54b275644da5e (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
/* 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.
 *
 */

/*
 * This code is based on Labyrinth of Time code with assistance of
 *
 * Copyright (c) 1993 Terra Nova Development
 * Copyright (c) 2004 The Wyrmkeep Entertainment Co.
 *
 */

#include "lab/stddefines.h"
#include "lab/labfun.h"
#include "lab/text.h"
#include "lab/vga.h"

namespace Lab {

/*****************************************************************************/
/* Opens up a font from disk, but uses buffer memory to store it in.         */
/*****************************************************************************/
bool openFontMem(const char *TextFontPath, struct TextFont *tf, byte *fontbuffer) {
	byte **file = NULL;
	char header[5];
	int32 filesize, headersize = 4L + 2L + 256 * 3 + 4L;

	filesize = sizeOfFile(TextFontPath);
	file = g_music->newOpen(TextFontPath);

	if ((file != NULL) && (filesize > headersize)) {
		header[4] = 0;
		readBlock(&header, 4L, file);

		if (strcmp(header, "VGAF") == 0) {
			tf->DataLength = filesize - headersize;
			readBlock(&(tf->Height), 2L, file);
#if !defined(DOSCODE)
			swapUShortPtr(&(tf->Height), 1);
#endif
			readBlock(tf->Widths, 256L, file);
			readBlock(tf->Offsets, 256L * 2L, file);
#if !defined(DOSCODE)
			swapUShortPtr(tf->Offsets, 256);
#endif
			skip(file, 4L);
			tf->data = fontbuffer;
			readBlock(tf->data, tf->DataLength, file);
			return true;
		}
	}

	return false;
}


/*****************************************************************************/
/* Opens up a font from disk.                                                */
/*****************************************************************************/
bool openFont(const char *TextFontPath, struct TextFont **tf) {
	byte **file = NULL;
	char header[5];
	int32 filesize, headersize = 4L + 2L + 256 * 3 + 4L;

	if ((*tf = (TextFont *)calloc(sizeof(struct TextFont), 1))) {
		filesize = sizeOfFile(TextFontPath);
		file = g_music->newOpen(TextFontPath);

		if ((file != NULL) && (filesize > headersize)) {
			header[4] = 0;
			readBlock(&header, 4L, file);

			if (strcmp(header, "VGAF") == 0) {
				(*tf)->DataLength = filesize - headersize;
				readBlock(&((*tf)->Height), 2L, file);
#if !defined(DOSCODE)
				swapUShortPtr(&((*tf)->Height), 1);
#endif
				readBlock((*tf)->Widths, 256L, file);
				readBlock((*tf)->Offsets, 256L * 2L, file);
#if !defined(DOSCODE)
				swapUShortPtr((*tf)->Offsets, 256);
#endif
				skip(file, 4L);

				if (((*tf)->data = (byte *)calloc((*tf)->DataLength, 1))) {
					readBlock((*tf)->data, (*tf)->DataLength, file);
					return true;
				}
			}
		}

		free(*tf);
	}

	*tf = NULL;
	return false;
}


/*****************************************************************************/
/* Closes a font and frees all memory associated with it.                    */
/*****************************************************************************/
void closeFont(struct TextFont *tf) {
	if (tf) {
		if (tf->data && tf->DataLength)
			free(tf->data);

		free(tf);
	}
}



/*****************************************************************************/
/* Returns the length of a text in the specified font.                       */
/*****************************************************************************/
uint16 textLength(struct TextFont *tf, const char *text, uint16 numchars) {
	uint16 counter, length = 0;

	if (tf)
		for (counter = 0; counter < numchars; counter++) {
			length += tf->Widths[(uint)*text];
			text++;
		}

	return length;
}



/*****************************************************************************/
/* Returns the height of a specified font.                                   */
/*****************************************************************************/
uint16 textHeight(struct TextFont *tf) {
	if (tf)
		return tf->Height;
	else
		return 0;
}



extern uint32 VGAScreenWidth, VGABytesPerPage;


/*****************************************************************************/
/* Draws the text to the screen.                                             */
/*****************************************************************************/
void text(struct TextFont *tf, uint16 x, uint16 y, uint16 color, const char *text, uint16 numchars) {
	byte *VGATop, *VGACur, *VGATemp, *VGATempLine, *cdata;
	uint32 RealOffset, SegmentOffset;
	int32 templeft, LeftInSegment;
	uint16 counter, counterb, bwidth, mask, curpage, rows, cols, data;

	VGATop = getVGABaseAddr();

	for (counter = 0; counter < numchars; counter++) {
		RealOffset = (VGAScreenWidth * y) + x;
		curpage    = RealOffset / VGABytesPerPage;
		SegmentOffset = RealOffset - (curpage * VGABytesPerPage);
		LeftInSegment = VGABytesPerPage - SegmentOffset;
		VGACur = VGATop + SegmentOffset;
		setPage(curpage);

		if (tf->Widths[(uint)*text]) {
			cdata = tf->data + tf->Offsets[(uint)*text];
			bwidth = *cdata;
			cdata++;
			VGATemp = VGACur;
			VGATempLine = VGACur;

			for (rows = 0; rows < tf->Height; rows++) {
				VGATemp = VGATempLine;
				templeft = LeftInSegment;

				for (cols = 0; cols < bwidth; cols++) {
					data = *cdata;
					cdata++;

					if (data && (templeft >= 8)) {
						if (0x80 & data)
							*VGATemp = color;

						VGATemp++;

						if (0x40 & data)
							*VGATemp = color;

						VGATemp++;

						if (0x20 & data)
							*VGATemp = color;

						VGATemp++;

						if (0x10 & data)
							*VGATemp = color;

						VGATemp++;

						if (0x08 & data)
							*VGATemp = color;

						VGATemp++;

						if (0x04 & data)
							*VGATemp = color;

						VGATemp++;

						if (0x02 & data)
							*VGATemp = color;

						VGATemp++;

						if (0x01 & data)
							*VGATemp = color;

						VGATemp++;

						templeft -= 8;
					} else if (data) {
						mask = 0x80;
						templeft = LeftInSegment;

						for (counterb = 0; counterb < 8; counterb++) {
							if (templeft <= 0) {
								curpage++;
								setPage(curpage);
								VGATemp = (byte *)(VGATop - templeft);
								/* Set up VGATempLine for next line */
								VGATempLine -= VGABytesPerPage;
								/* Set up LeftInSegment for next line */
								LeftInSegment += VGABytesPerPage + templeft;
								templeft += VGABytesPerPage;
							}

							if (mask & data)
								*VGATemp = color;

							VGATemp++;

							mask = mask >> 1;
							templeft--;
						}
					} else {
						templeft -= 8;
						VGATemp += 8;
					}
				}

				VGATempLine += VGAScreenWidth;
				LeftInSegment -= VGAScreenWidth;

				if (LeftInSegment <= 0) {
					curpage++;
					setPage(curpage);
					VGATempLine -= VGABytesPerPage;
					LeftInSegment += VGABytesPerPage;
				}
			}
		}

		x += tf->Widths[(int)*text];
		text++;
	}

	ungetVGABaseAddr();
}

} // End of namespace Lab