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
|
/* 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 "graphics/sjis.h"
#ifdef GRAPHICS_SJIS_H
#include "common/debug.h"
#include "common/archive.h"
#include "common/endian.h"
namespace Graphics {
FontSJIS *FontSJIS::createFont(const Common::Platform platform) {
FontSJIS *ret = 0;
// Try the font ROM of the specified platform
if (platform == Common::kPlatformFMTowns) {
ret = new FontTowns();
if (ret && ret->loadData())
return ret;
delete ret;
}
// Try ScummVM's font.
ret = new FontSjisSVM();
if (ret && ret->loadData())
return ret;
delete ret;
return 0;
}
template<typename Color>
void FontSJIS16x16::drawCharInternOutline(const uint16 *glyph, uint8 *dst, int pitch, Color c1, Color c2) const {
uint32 outlineGlyph[18];
memset(outlineGlyph, 0, sizeof(outlineGlyph));
// Create an outline map including the original character
const uint16 *src = glyph;
for (int i = 0; i < 16; ++i) {
uint32 line = *src++;
line = (line << 2) | (line << 1) | (line << 0);
outlineGlyph[i + 0] |= line;
outlineGlyph[i + 1] |= line;
outlineGlyph[i + 2] |= line;
}
uint8 *dstLine = dst;
for (int y = 0; y < 18; ++y) {
Color *lineBuf = (Color *)dstLine;
uint32 line = outlineGlyph[y];
for (int x = 0; x < 18; ++x) {
if (line & 0x20000)
*lineBuf = c2;
line <<= 1;
++lineBuf;
}
dstLine += pitch;
}
// draw the original char
drawCharIntern<Color>(glyph, dst + pitch + 1, pitch, c1);
}
template<typename Color>
void FontSJIS16x16::drawCharIntern(const uint16 *glyph, uint8 *dst, int pitch, Color c1) const {
for (int y = 0; y < 16; ++y) {
Color *lineBuf = (Color *)dst;
uint16 line = *glyph++;
for (int x = 0; x < 16; ++x) {
if (line & 0x8000)
*lineBuf = c1;
line <<= 1;
++lineBuf;
}
dst += pitch;
}
}
void FontSJIS16x16::drawChar(void *dst, uint16 ch, int pitch, int bpp, uint32 c1, uint32 c2) const {
const uint16 *glyphSource = getCharData(ch);
if (!glyphSource) {
warning("FontSJIS16x16::drawChar: Font does not offer data for %02X %02X", ch & 0xFF, ch >> 8);
return;
}
if (bpp == 1) {
if (!_outlineEnabled)
drawCharIntern<uint8>(glyphSource, (uint8 *)dst, pitch, c1);
else
drawCharInternOutline<uint8>(glyphSource, (uint8 *)dst, pitch, c1, c2);
} else if (bpp == 2) {
if (!_outlineEnabled)
drawCharIntern<uint16>(glyphSource, (uint8 *)dst, pitch, c1);
else
drawCharInternOutline<uint16>(glyphSource, (uint8 *)dst, pitch, c1, c2);
} else {
error("FontSJIS16x16::drawChar: unsupported bpp: %d", bpp);
}
}
// FM-TOWNS ROM font
bool FontTowns::loadData() {
Common::SeekableReadStream *data = SearchMan.createReadStreamForMember("FMT_FNT.ROM");
if (!data)
return false;
for (uint i = 0; i < (kFontRomSize / 2); ++i)
_fontData[i] = data->readUint16BE();
bool retValue = !data->err();
delete data;
return retValue;
}
const uint16 *FontTowns::getCharData(uint16 ch) const {
uint8 f = ch & 0xFF;
uint8 s = ch >> 8;
// copied from scumm\charset.cpp
enum {
KANA = 0,
KANJI = 1,
EKANJI = 2
};
int base = s - ((s + 1) % 32);
int c = 0, p = 0, chunk_f = 0, chunk = 0, cr = 0, kanjiType = KANA;
if (f >= 0x81 && f <= 0x84) kanjiType = KANA;
if (f >= 0x88 && f <= 0x9f) kanjiType = KANJI;
if (f >= 0xe0 && f <= 0xea) kanjiType = EKANJI;
if ((f > 0xe8 || (f == 0xe8 && base >= 0x9f)) || (f > 0x90 || (f == 0x90 && base >= 0x9f))) {
c = 48; //correction
p = -8; //correction
}
if (kanjiType == KANA) {//Kana
chunk_f = (f - 0x81) * 2;
} else if (kanjiType == KANJI) {//Standard Kanji
p += f - 0x88;
chunk_f = c + 2 * p;
} else if (kanjiType == EKANJI) {//Enhanced Kanji
p += f - 0xe0;
chunk_f = c + 2 * p;
}
// Base corrections
if (base == 0x7f && s == 0x7f)
base -= 0x20;
if (base == 0x9f && s == 0xbe)
base += 0x20;
if (base == 0xbf && s == 0xde)
base += 0x20;
//if (base == 0x7f && s == 0x9e)
// base += 0x20;
switch (base) {
case 0x3f:
cr = 0; //3f
if (kanjiType == KANA) chunk = 1;
else if (kanjiType == KANJI) chunk = 31;
else if (kanjiType == EKANJI) chunk = 111;
break;
case 0x5f:
cr = 0; //5f
if (kanjiType == KANA) chunk = 17;
else if (kanjiType == KANJI) chunk = 47;
else if (kanjiType == EKANJI) chunk = 127;
break;
case 0x7f:
cr = -1; //80
if (kanjiType == KANA) chunk = 9;
else if (kanjiType == KANJI) chunk = 63;
else if (kanjiType == EKANJI) chunk = 143;
break;
case 0x9f:
cr = 1; //9e
if (kanjiType == KANA) chunk = 2;
else if (kanjiType == KANJI) chunk = 32;
else if (kanjiType == EKANJI) chunk = 112;
break;
case 0xbf:
cr = 1; //be
if (kanjiType == KANA) chunk = 18;
else if (kanjiType == KANJI) chunk = 48;
else if (kanjiType == EKANJI) chunk = 128;
break;
case 0xdf:
cr = 1; //de
if (kanjiType == KANA) chunk = 10;
else if (kanjiType == KANJI) chunk = 64;
else if (kanjiType == EKANJI) chunk = 144;
break;
default:
debug(4, "Invalid Char! f %x s %x base %x c %d p %d", f, s, base, c, p);
}
debug(6, "Kanji: %c%c f 0x%x s 0x%x base 0x%x c %d p %d chunk %d cr %d index %d", f, s, f, s, base, c, p, chunk, cr, ((chunk_f + chunk) * 32 + (s - base)) + cr);
return _fontData + (((chunk_f + chunk) * 32 + (s - base)) + cr) * 16;
}
// ScummVM SJIS font
bool FontSjisSVM::loadData() {
Common::SeekableReadStream *data = SearchMan.createReadStreamForMember("SJIS.FNT");
if (!data)
return false;
uint32 magic1 = data->readUint32BE();
uint32 magic2 = data->readUint32BE();
if (magic1 != MKID_BE('SCVM') || magic2 != MKID_BE('SJIS')) {
delete data;
return false;
}
uint32 version = data->readUint32BE();
if (version != 2) {
delete data;
return false;
}
uint numChars16x16 = data->readUint16BE();
uint numChars8x16 = data->readUint16BE();
_fontData16x16Size = numChars16x16 * 16;
_fontData16x16 = new uint16[_fontData16x16Size];
assert(_fontData16x16);
for (uint i = 0; i < _fontData16x16Size; ++i)
_fontData16x16[i] = data->readUint16BE();
_fontData8x16Size = numChars8x16 * 16;
_fontData8x16 = new uint8[numChars8x16 * 16];
assert(_fontData8x16);
data->read(_fontData8x16, _fontData8x16Size);
bool retValue = !data->err();
delete data;
return retValue;
}
void FontSjisSVM::drawChar(void *dst, uint16 ch, int pitch, int bpp, uint32 c1, uint32 c2) const {
if (!is8x16(ch))
return FontSJIS16x16::drawChar(dst, ch, pitch, bpp, c1, c2);
const uint8 *glyphSource = getCharData8x16(ch);
if (!glyphSource)
return;
if (bpp == 1) {
if (!_outlineEnabled)
drawCharIntern<uint8>(glyphSource, (uint8 *)dst, pitch, c1);
else
drawCharInternOutline<uint8>(glyphSource, (uint8 *)dst, pitch, c1, c2);
} else if (bpp == 2) {
if (!_outlineEnabled)
drawCharIntern<uint16>(glyphSource, (uint8 *)dst, pitch, c1);
else
drawCharInternOutline<uint16>(glyphSource, (uint8 *)dst, pitch, c1, c2);
} else {
error("FontSjisSVM::drawChar: unsupported bpp: %d", bpp);
}
}
// TODO: Consider merging these with FontSjis16x16
template<typename Color>
void FontSjisSVM::drawCharInternOutline(const uint8 *glyph, uint8 *dst, int pitch, Color c1, Color c2) const {
uint16 outlineGlyph[18];
memset(outlineGlyph, 0, sizeof(outlineGlyph));
// Create an outline map including the original character
const uint8 *src = glyph;
for (int i = 0; i < 16; ++i) {
uint16 line = *src++;
line = (line << 2) | (line << 1) | (line << 0);
outlineGlyph[i + 0] |= line;
outlineGlyph[i + 1] |= line;
outlineGlyph[i + 2] |= line;
}
uint8 *dstLine = dst;
for (int y = 0; y < 18; ++y) {
Color *lineBuf = (Color *)dstLine;
uint16 line = outlineGlyph[y];
for (int x = 0; x < 10; ++x) {
if (line & 0x200)
*lineBuf = c2;
line <<= 1;
++lineBuf;
}
dstLine += pitch;
}
// draw the original char
drawCharIntern<Color>(glyph, dst + pitch + 1, pitch, c1);
}
template<typename Color>
void FontSjisSVM::drawCharIntern(const uint8 *glyph, uint8 *dst, int pitch, Color c1) const {
for (int y = 0; y < 16; ++y) {
Color *lineBuf = (Color *)dst;
uint8 line = *glyph++;
for (int x = 0; x < 8; ++x) {
if (line & 0x80)
*lineBuf = c1;
line <<= 1;
++lineBuf;
}
dst += pitch;
}
}
uint FontSjisSVM::getCharWidth(uint16 ch) const {
if (is8x16(ch))
return _outlineEnabled ? 10 : 8;
else
return FontSJIS16x16::getMaxFontWidth();
}
bool FontSjisSVM::is8x16(uint16 ch) const {
if (ch >= 0xFF)
return false;
else if (ch <= 0x7F || (ch >= 0xA1 && ch <= 0xDF))
return true;
else
return false;
}
const uint16 *FontSjisSVM::getCharData(uint16 c) const {
const uint8 fB = c & 0xFF;
const uint8 sB = c >> 8;
// We only allow 2 byte SJIS characters.
if (fB <= 0x80 || fB >= 0xF0 || (fB >= 0xA0 && fB <= 0xDF) || sB == 0x7F)
return 0;
int base = fB;
base -= 0x81;
if (base >= 0x5F)
base -= 0x40;
int index = sB;
index -= 0x40;
if (index >= 0x3F)
--index;
// Another check if the passed character was an
// correctly encoded SJIS character.
if (index < 0 || index >= 0xBC || base < 0)
return 0;
const uint offset = (base * 0xBC + index) * 16;
assert(offset + 16 <= _fontData16x16Size);
return _fontData16x16 + offset;
}
const uint8 *FontSjisSVM::getCharData8x16(uint16 c) const {
const uint8 fB = c & 0xFF;
const uint8 sB = c >> 8;
if (!is8x16(c) || sB)
return 0;
int index = fB;
// half-width katakana
if (fB >= 0xA1 && fB <= 0xDF)
index -= 0x21;
const uint offset = index * 16;
assert(offset <= _fontData8x16Size);
return _fontData8x16 + offset;
}
} // End of namespace Graphics
#endif // defined(GRAPHICS_SJIS_H)
|