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
|
/* 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.
*
*/
#include "common/file.h"
#include "scumm/scumm.h"
#include "scumm/util.h"
#include "scumm/smush/smush_font.h"
namespace Scumm {
SmushFont::SmushFont(ScummEngine *vm, const char *filename, bool use_original_colors, bool new_colors) :
NutRenderer(vm, filename),
_color(-1),
_new_colors(new_colors),
_original(use_original_colors) {
}
int SmushFont::getStringWidth(const char *str) {
assert(str);
int width = 0;
while (*str) {
if (*str & 0x80 && _vm->_useCJKMode) {
width += _vm->_2byteWidth + 1;
str += 2;
} else
width += getCharWidth(*str++);
}
return width;
}
int SmushFont::getStringHeight(const char *str) {
assert(str);
int height = 0;
while (*str) {
int charHeight = getCharHeight(*str++);
if (height < charHeight)
height = charHeight;
}
return height;
}
int SmushFont::drawChar(byte *buffer, int dst_width, int x, int y, byte chr) {
int w = _chars[chr].width;
int h = _chars[chr].height;
const byte *src = unpackChar(chr);
byte *dst = buffer + dst_width * y + x;
assert(dst_width == _vm->_screenWidth);
if (_original) {
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int8 value = *src++;
if (value != _chars[chr].transparency)
dst[i] = value;
}
dst += dst_width;
}
} else {
char color = (_color != -1) ? _color : 1;
if (_new_colors) {
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int8 value = *src++;
if (value == -color) {
dst[i] = 0xFF;
} else if (value == -31) {
dst[i] = 0;
} else if (value != _chars[chr].transparency) {
dst[i] = value;
}
}
dst += dst_width;
}
} else {
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int8 value = *src++;
if (value == 1) {
dst[i] = color;
} else if (value != _chars[chr].transparency) {
dst[i] = 0;
}
}
dst += dst_width;
}
}
}
return w;
}
int SmushFont::draw2byte(byte *buffer, int dst_width, int x, int y, int idx) {
int w = _vm->_2byteWidth;
int h = _vm->_2byteHeight;
const byte *src = _vm->get2byteCharPtr(idx);
byte bits = 0;
char color = (_color != -1) ? _color : 1;
if (_new_colors)
color = (char)0xff;
if (_vm->_game.id == GID_FT)
color = 1;
enum ShadowMode {
kNone,
kNormalShadowMode,
kKoreanV7ShadowMode,
kKoreanV8ShadowMode
};
ShadowMode shadowMode = kNone;
if (_vm->_language == Common::KO_KOR) {
if (_vm->_game.version == 8)
shadowMode = kKoreanV8ShadowMode;
else
shadowMode = kKoreanV7ShadowMode;
}
int shadowOffsetXTable[4] = {-1, 0, 1, 0};
int shadowOffsetYTable[4] = {0, 1, 0, 0};
int shadowOffsetColorTable[4] = {0, 0, 0, color};
int shadowIdx = 3;
if (shadowMode == kKoreanV8ShadowMode)
shadowIdx = 0;
else if (shadowMode == kKoreanV7ShadowMode)
shadowIdx = 2;
const byte *origSrc = src;
for (; shadowIdx < 4; shadowIdx++) {
int offX = x + shadowOffsetXTable[shadowIdx];
int offY = y + shadowOffsetYTable[shadowIdx];
byte drawColor = shadowOffsetColorTable[shadowIdx];
src = origSrc;
byte *dst = buffer + dst_width * (offY + (_vm->_game.id == GID_CMI ? 7 : (_vm->_game.id == GID_DIG ? 2 : 0))) + offX;
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
if (offX + i < 0)
continue;
if ((i % 8) == 0)
bits = *src++;
if (bits & revBitMask(i % 8)) {
if (shadowMode == kNormalShadowMode) {
dst[i + 1] = 0;
dst[dst_width + i] = 0;
dst[dst_width + i + 1] = 0;
}
dst[i] = drawColor;
}
}
dst += dst_width;
}
}
return w + 1;
}
void SmushFont::drawSubstring(const char *str, byte *buffer, int dst_width, int x, int y) {
// This happens in the Full Throttle intro. I don't know if our
// text-drawing functions are buggy, or if this function is supposed
// to have to check for it.
if (x < 0)
x = 0;
for (int i = 0; str[i] != 0; i++) {
if ((byte)str[i] >= 0x80 && _vm->_useCJKMode) {
x += draw2byte(buffer, dst_width, x, y, (byte)str[i] + 256 * (byte)str[i+1]);
i++;
} else
x += drawChar(buffer, dst_width, x, y, str[i]);
}
}
#define MAX_WORDS 60
void SmushFont::drawString(const char *str, byte *buffer, int dst_width, int dst_height, int x, int y, bool center) {
debugC(DEBUG_SMUSH, "SmushFont::drawString(%s, %d, %d, %d)", str, x, y, center);
while (str) {
char line[256];
const char *pos = strchr(str, '\n');
if (pos) {
memcpy(line, str, pos - str - 1);
line[pos - str - 1] = 0;
str = pos + 1;
} else {
strcpy(line, str);
str = 0;
}
drawSubstring(line, buffer, dst_width, center ? (x - getStringWidth(line) / 2) : x, y);
y += getStringHeight(line);
}
}
void SmushFont::drawStringWrap(const char *str, byte *buffer, int dst_width, int dst_height, int x, int y, int left, int right, bool center) {
debugC(DEBUG_SMUSH, "SmushFont::drawStringWrap(%s, %d, %d, %d, %d, %d)", str, x, y, left, right, center);
const int width = right - left;
char *s = strdup(str);
char *words[MAX_WORDS];
int word_count = 0;
char *tmp = s;
while (tmp) {
assert(word_count < MAX_WORDS);
words[word_count++] = tmp;
tmp = strpbrk(tmp, " \t\r\n");
if (tmp == 0)
break;
*tmp++ = 0;
}
int i = 0, max_width = 0, height = 0, line_count = 0;
char *substrings[MAX_WORDS];
int substr_widths[MAX_WORDS];
const int space_width = getCharWidth(' ');
i = 0;
while (i < word_count) {
char *substr = words[i++];
int substr_width = getStringWidth(substr);
while (i < word_count) {
int word_width = getStringWidth(words[i]);
if ((substr_width + space_width + word_width) >= width)
break;
substr_width += word_width + space_width;
*(words[i]-1) = ' '; // Convert 0 byte back to space
i++;
}
substrings[line_count] = substr;
substr_widths[line_count++] = substr_width;
if (max_width < substr_width)
max_width = substr_width;
height += getStringHeight(substr);
}
if (y > dst_height - height) {
y = dst_height - height;
}
if (center) {
max_width = (max_width + 1) / 2;
x = left + width / 2;
if (x < left + max_width)
x = left + max_width;
if (x > right - max_width)
x = right - max_width;
for (i = 0; i < line_count; i++) {
drawSubstring(substrings[i], buffer, dst_width, x - substr_widths[i] / 2, y);
y += getStringHeight(substrings[i]);
}
} else {
if (x > dst_width - max_width)
x = dst_width - max_width;
for (i = 0; i < line_count; i++) {
drawSubstring(substrings[i], buffer, dst_width, x, y);
y += getStringHeight(substrings[i]);
}
}
free(s);
}
} // End of namespace Scumm
|