-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.cpp
181 lines (151 loc) · 4.26 KB
/
font.cpp
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
/* 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 "engines/express/font.h"
//#include "engines/express/debug.h"
#include "common/stream.h"
#include "common/system.h"
namespace Express {
Font::Font(Common::Archive *arc) : _glyphs(NULL), _glyphWidths(NULL) {
Common::SeekableReadStream *fontFile;
fontFile = arc->createReadStreamForMember("FONT.DAT");
if (fontFile) {
// Read the palette
for (int i = 0; i < 0x10; i++) {
_palette[i] = fontFile->readUint16LE();
}
// Read the character map
fontFile->read(_charMap, 0x200);
// Read the glyphs
_numGlyphs = fontFile->readUint16LE();
_glyphs = new byte[_numGlyphs * 18 * 8];
fontFile->read(_glyphs, _numGlyphs * 18 * 8);
// TODO: Read something else?
//uint16 unknown = fontFile->readByte();
//warning("unknown = %d", unknown);
//warning("pos = %d", fontFile->pos());
//warning("left = %d", fontFile->size() - fontFile->pos());
//while (!fontFile->eos()) {
//unknown = fontFile->readByte();
//warning("val = %d", unknown);
//}
// Precalculate glyph widths
_glyphWidths = new byte[_numGlyphs];
for (int i = 0; i < _numGlyphs; i++) {
_glyphWidths[i] = getGlyphWidth(i);
}
}
}
Font::~Font() {
delete[] _glyphs;
delete[] _glyphWidths;
}
uint16 Font::getCharGlyph(uint16 c) {
warning("%c", c);
if (c >= 0x200)
error("Express::Font: Invalid character %d", c);
return _charMap[c];
}
byte *Font::getGlyphImg(uint16 g) {
if (g >= _numGlyphs)
error("Express::Font: Invalid glyph %d (%d available)",
g, _numGlyphs);
return _glyphs + g * 18 * 8;
}
uint8 Font::getGlyphWidth(uint16 g) {
byte *p = getGlyphImg(g);
uint8 maxLineWidth = 0;
for (int j = 0; j < 18; j++) {
uint8 currentLineWidth = 0;
for (int i = 0; i < 16; i++) {
byte index;
if (i % 2)
index = *p & 0xf;
else
index = *p >> 4;
uint16 color = _palette[index];
if (color != 0x1f)
currentLineWidth = i;
if (i % 2)
p++;
}
if (currentLineWidth > maxLineWidth)
maxLineWidth = currentLineWidth;
}
return maxLineWidth;
}
byte *Font::getCharImg(uint16 c) {
return getGlyphImg(getCharGlyph(c));
}
uint8 Font::getCharWidth(uint16 c) {
if (c == 0x20) {
// Space is a special case
// TODO: this is an arbitrary value
return 10;
} else {
return _glyphWidths[getCharGlyph(c)];
}
}
uint16 Font::getStringWidth(Common::String str) {
uint16 width = 0;
for (uint i = 0; i < str.size(); i++)
width += getCharWidth(str[i]);
return width;
}
uint16 Font::getStringWidth(uint16 *str, uint16 length) {
uint16 width = 0;
for (uint i = 0; i < length; i++)
width += getCharWidth(str[i]);
return width;
}
void Font::drawChar(int x, int y, uint16 c) {
byte *p = getCharImg(c);
for (int j = 0; j < 18; j++) {
for (int i = 0; i < 16; i++) {
byte index;
if (i % 2)
index = *p & 0xf;
else
index = *p >> 4;
uint16 color = _palette[index];
if (color != 0x1f)
g_system->copyRectToScreen((byte *)&color, 2, x + i, y + j, 1, 1);
if (i % 2)
p++;
}
}
}
void Font::drawString(int x, int y, Common::String str) {
int currentX = x;
for (uint i = 0; i < str.size(); i++) {
drawChar(currentX, y, str[i]);
currentX += getCharWidth(str[i]);
}
}
void Font::drawString(int x, int y, uint16 *str, uint16 length) {
int currentX = x;
for (uint i = 0; i < length; i++) {
drawChar(currentX, y, str[i]);
currentX += getCharWidth(str[i]);
}
}
} // End of Express namespace