Speech bubbles can point down right.
[scummvm-innocent.git] / graphics / sjis.cpp
blob405d8622c29d3cc5af18b49e755af0dd655419cc
1 /* ScummVM - Graphic Adventure Engine
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * $URL$
22 * $Id$
25 #include "graphics/sjis.h"
27 #ifdef GRAPHICS_SJIS_H
29 #include "common/debug.h"
30 #include "common/archive.h"
31 #include "common/endian.h"
33 namespace Graphics {
35 FontSJIS *FontSJIS::createFont(const Common::Platform platform) {
36 FontSJIS *ret = 0;
38 // Try the font ROM of the specified platform
39 if (platform == Common::kPlatformFMTowns) {
40 ret = new FontTowns();
41 if (ret && ret->loadData())
42 return ret;
43 delete ret;
46 // Try ScummVM's font.
47 ret = new FontSjisSVM();
48 if (ret && ret->loadData())
49 return ret;
50 delete ret;
52 return 0;
55 template<typename Color>
56 void FontSJIS16x16::drawCharInternOutline(const uint16 *glyph, uint8 *dst, int pitch, Color c1, Color c2) const {
57 uint32 outlineGlyph[18];
58 memset(outlineGlyph, 0, sizeof(outlineGlyph));
60 // Create an outline map including the original character
61 const uint16 *src = glyph;
62 for (int i = 0; i < 16; ++i) {
63 uint32 line = *src++;
64 line = (line << 2) | (line << 1) | (line << 0);
66 outlineGlyph[i + 0] |= line;
67 outlineGlyph[i + 1] |= line;
68 outlineGlyph[i + 2] |= line;
71 uint8 *dstLine = dst;
72 for (int y = 0; y < 18; ++y) {
73 Color *lineBuf = (Color *)dstLine;
74 uint32 line = outlineGlyph[y];
76 for (int x = 0; x < 18; ++x) {
77 if (line & 0x20000)
78 *lineBuf = c2;
79 line <<= 1;
80 ++lineBuf;
83 dstLine += pitch;
86 // draw the original char
87 drawCharIntern<Color>(glyph, dst + pitch + 1, pitch, c1);
90 template<typename Color>
91 void FontSJIS16x16::drawCharIntern(const uint16 *glyph, uint8 *dst, int pitch, Color c1) const {
92 for (int y = 0; y < 16; ++y) {
93 Color *lineBuf = (Color *)dst;
94 uint16 line = *glyph++;
96 for (int x = 0; x < 16; ++x) {
97 if (line & 0x8000)
98 *lineBuf = c1;
99 line <<= 1;
100 ++lineBuf;
103 dst += pitch;
107 void FontSJIS16x16::drawChar(void *dst, uint16 ch, int pitch, int bpp, uint32 c1, uint32 c2) const {
108 const uint16 *glyphSource = getCharData(ch);
109 if (!glyphSource) {
110 warning("FontSJIS16x16::drawChar: Font does not offer data for %02X %02X", ch & 0xFF, ch >> 8);
111 return;
114 if (bpp == 1) {
115 if (!_outlineEnabled)
116 drawCharIntern<uint8>(glyphSource, (uint8 *)dst, pitch, c1);
117 else
118 drawCharInternOutline<uint8>(glyphSource, (uint8 *)dst, pitch, c1, c2);
119 } else if (bpp == 2) {
120 if (!_outlineEnabled)
121 drawCharIntern<uint16>(glyphSource, (uint8 *)dst, pitch, c1);
122 else
123 drawCharInternOutline<uint16>(glyphSource, (uint8 *)dst, pitch, c1, c2);
124 } else {
125 error("FontTowns::drawChar: unsupported bpp: %d", bpp);
129 // FM-TOWNS ROM font
131 bool FontTowns::loadData() {
132 Common::SeekableReadStream *data = SearchMan.createReadStreamForMember("FMT_FNT.ROM");
133 if (!data)
134 return false;
136 for (uint i = 0; i < (kFontRomSize / 2); ++i)
137 _fontData[i] = data->readUint16BE();
139 bool retValue = !data->err();
140 delete data;
141 return retValue;
144 const uint16 *FontTowns::getCharData(uint16 ch) const {
145 uint8 f = ch & 0xFF;
146 uint8 s = ch >> 8;
148 // copied from scumm\charset.cpp
149 enum {
150 KANA = 0,
151 KANJI = 1,
152 EKANJI = 2
155 int base = s - ((s + 1) % 32);
156 int c = 0, p = 0, chunk_f = 0, chunk = 0, cr = 0, kanjiType = KANA;
158 if (f >= 0x81 && f <= 0x84) kanjiType = KANA;
159 if (f >= 0x88 && f <= 0x9f) kanjiType = KANJI;
160 if (f >= 0xe0 && f <= 0xea) kanjiType = EKANJI;
162 if ((f > 0xe8 || (f == 0xe8 && base >= 0x9f)) || (f > 0x90 || (f == 0x90 && base >= 0x9f))) {
163 c = 48; //correction
164 p = -8; //correction
167 if (kanjiType == KANA) {//Kana
168 chunk_f = (f - 0x81) * 2;
169 } else if (kanjiType == KANJI) {//Standard Kanji
170 p += f - 0x88;
171 chunk_f = c + 2 * p;
172 } else if (kanjiType == EKANJI) {//Enhanced Kanji
173 p += f - 0xe0;
174 chunk_f = c + 2 * p;
177 // Base corrections
178 if (base == 0x7f && s == 0x7f)
179 base -= 0x20;
180 if (base == 0x9f && s == 0xbe)
181 base += 0x20;
182 if (base == 0xbf && s == 0xde)
183 base += 0x20;
184 //if (base == 0x7f && s == 0x9e)
185 // base += 0x20;
187 switch (base) {
188 case 0x3f:
189 cr = 0; //3f
190 if (kanjiType == KANA) chunk = 1;
191 else if (kanjiType == KANJI) chunk = 31;
192 else if (kanjiType == EKANJI) chunk = 111;
193 break;
194 case 0x5f:
195 cr = 0; //5f
196 if (kanjiType == KANA) chunk = 17;
197 else if (kanjiType == KANJI) chunk = 47;
198 else if (kanjiType == EKANJI) chunk = 127;
199 break;
200 case 0x7f:
201 cr = -1; //80
202 if (kanjiType == KANA) chunk = 9;
203 else if (kanjiType == KANJI) chunk = 63;
204 else if (kanjiType == EKANJI) chunk = 143;
205 break;
206 case 0x9f:
207 cr = 1; //9e
208 if (kanjiType == KANA) chunk = 2;
209 else if (kanjiType == KANJI) chunk = 32;
210 else if (kanjiType == EKANJI) chunk = 112;
211 break;
212 case 0xbf:
213 cr = 1; //be
214 if (kanjiType == KANA) chunk = 18;
215 else if (kanjiType == KANJI) chunk = 48;
216 else if (kanjiType == EKANJI) chunk = 128;
217 break;
218 case 0xdf:
219 cr = 1; //de
220 if (kanjiType == KANA) chunk = 10;
221 else if (kanjiType == KANJI) chunk = 64;
222 else if (kanjiType == EKANJI) chunk = 144;
223 break;
224 default:
225 debug(4, "Invalid Char! f %x s %x base %x c %d p %d", f, s, base, c, p);
228 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);
229 return _fontData + (((chunk_f + chunk) * 32 + (s - base)) + cr) * 16;
232 // ScummVM SJIS font
234 bool FontSjisSVM::loadData() {
235 Common::SeekableReadStream *data = SearchMan.createReadStreamForMember("SJIS.FNT");
236 if (!data)
237 return false;
239 uint32 magic1 = data->readUint32BE();
240 uint32 magic2 = data->readUint32BE();
242 if (magic1 != MKID_BE('SCVM') || magic2 != MKID_BE('SJIS')) {
243 delete data;
244 return false;
247 uint32 version = data->readUint32BE();
248 if (version != 1) {
249 delete data;
250 return false;
252 uint numChars = data->readUint16BE();
254 _fontData = new uint16[numChars * 16];
255 assert(_fontData);
257 for (uint i = 0; i < numChars * 16; ++i)
258 _fontData[i] = data->readUint16BE();
260 bool retValue = !data->err();
261 delete data;
262 return retValue;
265 const uint16 *FontSjisSVM::getCharData(uint16 c) const {
266 const uint8 fB = c & 0xFF;
267 const uint8 sB = c >> 8;
269 // We only allow 2 byte SJIS characters.
270 if (fB <= 0x80 || fB >= 0xF0 || (fB >= 0xA0 && fB <= 0xDF) || sB == 0x7F)
271 return 0;
273 int base = fB;
274 base -= 0x81;
275 if (base >= 0x5F)
276 base -= 0x40;
278 int index = sB;
279 index -= 0x40;
280 if (index >= 0x3F)
281 --index;
283 return _fontData + (base * 0xBC + index) * 16;
286 } // end of namespace Graphics
288 #endif // defined(GRAPHICS_SJIS_H)