aboutsummaryrefslogtreecommitdiff
path: root/engines/parallaction/font.cpp
blob: bc67acf644ebdf5572a3ba706a4d55100389cef0 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/* 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$
 *
 */

#include "common/endian.h"
#include "common/stream.h"

#include "parallaction/parallaction.h"

namespace Parallaction {


extern byte _amigaTopazFont[];

class BraFont : public Font {

	byte	*_cp;
	uint	_bufPitch;

	uint32	_height;
	byte	_numGlyphs;

	byte	*_widths;
	uint	*_offsets;

	byte	*_data;

	static  byte _charMap[];

	byte mapChar(byte c) {
		return _charMap[c];
	}

public:
	BraFont(Common::ReadStream &stream) {

		_numGlyphs = stream.readByte();
		_height = stream.readUint32BE();

		_widths = (byte*)malloc(_numGlyphs);
		stream.read(_widths, _numGlyphs);

		_offsets = (uint*)malloc(_numGlyphs * sizeof(uint));
		_offsets[0] = 0;
		for (uint i = 1; i < _numGlyphs; i++)
			_offsets[i] = _offsets[i-1] + _widths[i-1] * _height;

		uint size = _offsets[_numGlyphs-1] + _widths[_numGlyphs-1] * _height;

		_data = (byte*)malloc(size);
		stream.read(_data, size);

	}

	~BraFont() {
		free(_widths);
		free(_offsets);
		free(_data);
	}


	uint32 getStringWidth(const char *s) {
		uint32 len = 0;

		while (*s) {
			byte c = mapChar(*s);
			len += (_widths[c] + 2);
			s++;
		}

		return len;
	}

	uint16 height() {
		return (uint16)_height;
	}

	uint16 drawChar(char c) {
		assert(c < _numGlyphs);

		byte *src = _data + _offsets[c];
		byte *dst = _cp;
		uint16 w = _widths[c];

		for (uint16 j = 0; j < height(); j++) {
			for (uint16 k = 0; k < w; k++) {

				if (*src) {
					*dst = (_color) ? _color : *src;
				}

				dst++;
				src++;
			}

			dst += (_bufPitch - w);
		}

		return w + 2;

	}

	void drawString(byte* buffer, uint32 pitch, const char *s) {
		if (s == NULL)
			return;

		_bufPitch = pitch;

		_cp = buffer;
		while (*s) {
			byte c = mapChar(*s);
			_cp += drawChar(c);
			s++;
		}
	}

};

byte BraFont::_charMap[] = {
// 0
	0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34,
// 1
	0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34,
// 2
	0x34, 0x49, 0x48, 0x34, 0x34, 0x34, 0x34, 0x47, 0x34, 0x34, 0x34, 0x34, 0x40, 0x34, 0x3F, 0x34,
// 3
	0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x46, 0x45, 0x34, 0x34, 0x34, 0x42,
// 4
	0x34, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
// 5
	0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x34, 0x34, 0x34, 0x34, 0x34,
// 6
	0x34, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
// 7
	0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x34, 0x34, 0x34, 0x34,
// 8
	0x5E, 0x5D, 0x4E, 0x4B, 0x4D, 0x4C, 0x34, 0x5E, 0x4F, 0x51, 0x50, 0x34, 0x34, 0x34, 0x34, 0x34,
// 9
	0x34, 0x34, 0x34, 0x57, 0x59, 0x58, 0x5B, 0x5C, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34,
// A
	0x4A, 0x52, 0x34, 0x5A, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34,
// B
	0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34,
// C
	0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34,
// D
	0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34,
// E
	0x34, 0x5F, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34,
// F
	0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34, 0x34
};


class DosFont : public Font {

protected:
	// drawing properties
	byte		*_cp;

	Cnv			*_data;
	byte		_pitch;
	uint32		_bufPitch;

protected:
	virtual uint16 drawChar(char c) = 0;
	virtual uint16 width(byte c) = 0;
	virtual uint16 height() = 0;

	byte mapChar(byte c) {
		if (c == 0xA5) return 0x5F;
		if (c == 0xDF) return 0x60;

		if (c > 0x7F) return c - 0x7F;

		return c - 0x20;
	}

public:
	DosFont(Cnv *cnv) : _data(cnv), _pitch(cnv->_width) {
	}

	~DosFont() {
		delete _data;
	}

	void setData() {

	}

	uint32 getStringWidth(const char *s) {
		uint32 len = 0;

		while (*s) {
			byte c = mapChar(*s);
			len += width(c);
			s++;
		}

		return len;
	}

	void drawString(byte* buffer, uint32 pitch, const char *s) {
		if (s == NULL)
			return;

		_bufPitch = pitch;

		_cp = buffer;
		while (*s) {
			byte c = mapChar(*s);
			_cp += drawChar(c);
			s++;
		}
	}
};

class DosDialogueFont : public DosFont {

private:
	static const byte	_glyphWidths[126];

protected:
	uint16 width(byte c) {
		return _glyphWidths[c];
	}

	uint16 height() {
		return _data->_height;
	}

public:
	DosDialogueFont(Cnv *cnv) : DosFont(cnv) {
	}

protected:
	uint16 drawChar(char c) {

		byte *src = _data->getFramePtr(c);
		byte *dst = _cp;
		uint16 w = width(c);

		for (uint16 j = 0; j < height(); j++) {
			for (uint16 k = 0; k < w; k++) {

				if (!*src)
					*dst = _color;

				dst++;
				src++;
			}

			src += (_pitch - w);
			dst += (_bufPitch - w);
		}

		return w;

	}

};

const byte DosDialogueFont::_glyphWidths[126] = {
  0x04, 0x03, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x04, 0x04, 0x06, 0x06, 0x03, 0x05, 0x03, 0x05,
  0x06, 0x06, 0x06, 0x06, 0x07, 0x06, 0x06, 0x06, 0x06, 0x06, 0x03, 0x03, 0x05, 0x04, 0x05, 0x05,
  0x03, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x03, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x08, 0x07, 0x07, 0x07, 0x05, 0x06, 0x05, 0x08, 0x07,
  0x04, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x03, 0x04, 0x05, 0x05, 0x06, 0x06, 0x05,
  0x05, 0x06, 0x05, 0x05, 0x05, 0x05, 0x06, 0x07, 0x05, 0x05, 0x05, 0x05, 0x02, 0x05, 0x05, 0x07,
  0x08, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04,
  0x05, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04, 0x06, 0x05, 0x05, 0x05, 0x05
};


class DosMonospacedFont : public DosFont {

protected:
	uint16	_width;

protected:
	uint16 width(byte c) {
		return _width;
	}

	uint16 height() {
		return _data->_height;
	}

	uint16 drawChar(char c) {

		byte *src = _data->getFramePtr(c);
		byte *dst = _cp;

		for (uint16 i = 0; i < height(); i++) {
			for (uint16 j = 0; j < _width; j++) {
				if (*src)
					*dst = *src;
				src++;
				dst++;
			}

			dst += (_bufPitch - _width);
			src += (_pitch - _width);
		}

		return _width;
	}

public:
	DosMonospacedFont(Cnv *cnv) : DosFont(cnv) {
		_width = 8;
	}

};



class AmigaFont : public Font {

#include "common/pack-start.h"
	struct CharLoc {
		uint16	_offset;
		uint16	_length;
	};

	struct AmigaDiskFont {
		uint16	_ySize;
		byte	_style;
		byte	_flags;
		uint16	_xSize;
		uint16	_baseline;
		uint16	_boldSmear;
		uint16	_accessors;	// unused
		byte	_loChar;
		byte	_hiChar;
		uint32	_charData;
		uint16	_modulo;
		uint32	_charLoc;
		uint32	_charSpace;
		uint32	_charKern;
	};
#include "common/pack-end.h"

	AmigaDiskFont	*_font;
	uint32		_dataSize;
	byte		*_data;
	byte		*_charData;
	CharLoc		*_charLoc;
	uint16		*_charSpace;
	uint16		*_charKern;

	byte			*_cp;
	uint32		_pitch;

protected:
	uint16 getSpacing(byte c);
	void blitData(byte c);
	uint16 getKerning(byte c);
	uint16 getPixels(byte c);
	uint16 getOffset(byte c);
	uint16 width(byte c);
	uint16 height();

	byte	mapChar(byte c);

public:
	AmigaFont(Common::SeekableReadStream &stream);
	~AmigaFont();

	uint32 getStringWidth(const char *s);
	void drawString(byte *buf, uint32 pitch, const char *s);



};

AmigaFont::AmigaFont(Common::SeekableReadStream &stream) {
	stream.seek(32);	// skips dummy header

	_dataSize = stream.size() - stream.pos();
	_data = (byte*)malloc(_dataSize);
	stream.read(_data, _dataSize);

	_font = (AmigaDiskFont*)(_data + 78);
	_font->_ySize = FROM_BE_16(_font->_ySize);
	_font->_xSize = FROM_BE_16(_font->_xSize);
	_font->_baseline = FROM_BE_16(_font->_baseline);
	_font->_modulo = FROM_BE_16(_font->_modulo);

	_charLoc = (CharLoc*)(_data + FROM_BE_32(_font->_charLoc));
	_charData = _data + FROM_BE_32(_font->_charData);

	_charSpace = 0;
	_charKern = 0;

	if (_font->_charSpace != 0)
		_charSpace = (uint16*)(_data + FROM_BE_32(_font->_charSpace));
	if (_font->_charKern != 0)
		_charKern = (uint16*)(_data + FROM_BE_32(_font->_charKern));

}

AmigaFont::~AmigaFont() {
	free(_data);
}

uint16 AmigaFont::getSpacing(byte c) {
	return (_charSpace == 0) ? _font->_xSize : FROM_BE_16(_charSpace[c]);
}

uint16 AmigaFont::getKerning(byte c) {
	return (_charKern == 0) ? 0 : FROM_BE_16(_charKern[c]);
}

uint16 AmigaFont::getPixels(byte c) {
	return FROM_BE_16(_charLoc[c]._length);
}

uint16 AmigaFont::getOffset(byte c) {
	return FROM_BE_16(_charLoc[c]._offset);
}

void AmigaFont::blitData(byte c) {

	int num = getPixels(c);
	int bitOffset = getOffset(c);

	byte *d = _cp;
	byte *s = _charData;

	for (int i = 0; i < _font->_ySize; i++) {

		for (int j = bitOffset; j < bitOffset + num; j++) {
			byte *b = s + (j >> 3);
			byte bit = *b & (0x80 >> (j & 7));

			if (bit)
				*d = _color;

			d++;
		}

		s += _font->_modulo;
		d += _pitch - num;
	}

}

uint16 AmigaFont::width(byte c) {
//	printf("kern(%i) = %i, space(%i) = %i\t", c, getKerning(c), c, getSpacing(c));
	return getKerning(c) + getSpacing(c);
}

uint16 AmigaFont::height() {
	return _font->_ySize;
}

byte AmigaFont::mapChar(byte c) {

	if (c < _font->_loChar || c > _font->_hiChar)
		error("character '%c (%x)' not supported by font", c, c);

	return c - _font->_loChar;
}

uint32 AmigaFont::getStringWidth(const char *s) {
	uint32 len = 0;

	while (*s) {
		byte c = mapChar(*s);
		len += width(c);
		s++;
	}

	return len;
}

void AmigaFont::drawString(byte *buffer, uint32 pitch, const char *s) {

	_cp = buffer;
	_pitch = pitch;

	byte c;

	while (*s) {
		c = mapChar(*s);
		_cp += getKerning(c);
		blitData(c);
		_cp += getSpacing(c);
		s++;
	}

}

Font *DosDisk_ns::createFont(const char *name, Cnv* cnv) {
	Font *f = 0;

	if (!scumm_stricmp(name, "comic"))
		f = new DosDialogueFont(cnv);
	else
	if (!scumm_stricmp(name, "topaz"))
		f = new DosMonospacedFont(cnv);
	else
	if (!scumm_stricmp(name, "slide"))
		f = new DosMonospacedFont(cnv);
	else
		error("unknown dos font '%s'", name);

	return f;
}

Font *AmigaDisk_ns::createFont(const char *name, Common::SeekableReadStream &stream) {
	// TODO: implement AmigaLabelFont for labels
	return new AmigaFont(stream);
}

Font *DosDisk_br::createFont(const char *name, Common::ReadStream &stream) {
//	printf("DosDisk_br::createFont(%s)\n", name);
	return new BraFont(stream);
}

Font *AmigaDisk_br::createFont(const char *name, Common::SeekableReadStream &stream) {
	// TODO: implement AmigaLabelFont for labels
	return new AmigaFont(stream);
}

void Parallaction_ns::initFonts() {

	if (getPlatform() == Common::kPlatformPC) {
		_dialogueFont = _disk->loadFont("comic");
		_labelFont = _disk->loadFont("topaz");
		_menuFont = _disk->loadFont("slide");
		_introFont = _disk->loadFont("slide");
	} else {
		_dialogueFont = _disk->loadFont("comic");
		Common::MemoryReadStream stream(_amigaTopazFont, 2600, false);
		_labelFont = new AmigaFont(stream);
		_menuFont = _disk->loadFont("slide");
		_introFont = _disk->loadFont("intro");
	}

}

void Parallaction_br::initFonts() {
	if (getPlatform() == Common::kPlatformPC) {
		_menuFont = _disk->loadFont("russia");
		_dialogueFont = _disk->loadFont("comic");
		_labelFont = _menuFont;
	} else {
		// TODO: Confirm fonts matches
		// fonts/natasha/16
		// fonts/sonya/18
		// fonts/vanya/16

		_menuFont = _disk->loadFont("fonts/natasha/16");
		_dialogueFont = _disk->loadFont("fonts/sonya/18");
		Common::MemoryReadStream stream(_amigaTopazFont, 2600, false);
		_labelFont = new AmigaFont(stream);
	}
}

}