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
|
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/debug.h"
#include "common/file.h"
#include "common/system.h"
#include "common/ustr.h"
#include "common/unicode-bidi.h"
#include "graphics/fonts/ttf.h"
#include "graphics/font.h"
#include "graphics/fontman.h"
#include "graphics/surface.h"
#include "video/subtitles.h"
#include "common/config-manager.h"
namespace Video {
SRTParser::SRTParser() {
}
SRTParser::~SRTParser() {
cleanup();
}
void SRTParser::cleanup() {
for (Common::Array<SRTEntry *>::const_iterator item = _entries.begin(); item != _entries.end(); ++item)
delete *item;
_entries.clear();
}
bool parseTime(const char **pptr, uint32 *res) {
int hours, mins, secs, msecs;
const char *ptr = *pptr;
hours = (*ptr++ - '0') * 10;
hours += *ptr++ - '0';
if (hours > 24 || hours < 0)
return false;
if (*ptr++ != ':')
return false;
mins = (*ptr++ - '0') * 10;
mins += *ptr++ - '0';
if (mins > 60 || mins < 0)
return false;
if (*ptr++ != ':')
return false;
secs = (*ptr++ - '0') * 10;
secs += *ptr++ - '0';
if (secs > 60 || secs < 0)
return false;
if (*ptr++ != ',')
return false;
msecs = (*ptr++ - '0') * 100;
msecs += (*ptr++ - '0') * 10;
msecs += *ptr++ - '0';
if (msecs > 1000 || msecs < 0)
return false;
*res = 1000 * (3600 * hours + 60 * mins + secs) + msecs;
*pptr = ptr;
return true;
}
int SRTEntryComparator(const void *item1, const void *item2) {
const SRTEntry *l = *(const SRTEntry * const *)item1;
const SRTEntry *r = *(const SRTEntry * const *)item2;
return l->start - r->start;
}
int SRTEntryComparatorBSearch(const void *item1, const void *item2) {
const SRTEntry *k = *(const SRTEntry * const *)item1;
const SRTEntry *i = *(const SRTEntry * const *)item2;
if (k->start < i->start)
return -1;
if (k->start > i->end - 1)
return 1;
return 0;
}
bool SRTParser::parseFile(const char *fname) {
Common::File f;
cleanup();
if (!f.open(fname)) {
return false;
}
byte buf[3];
f.read(buf, 3);
int line = 0;
// Skip UTF header if present
if (buf[0] != 0xef || buf[1] != 0xbb || buf[2] != 0xbf)
f.seek(0);
while (!f.eos()) {
Common::String sseq, stimespec, stmp, text;
sseq = f.readLine(); line++;
stimespec = f.readLine(); line++;
text = f.readLine(); line++;
if (sseq.empty()) {
if (f.eos()) {
// Normal end of stream
break;
} else {
warning("Bad SRT file format (spec): %s at line %d", fname, line);
break;
}
}
if (stimespec.empty() || text.empty()) {
warning("Bad SRT file format (spec): %s at line %d", fname, line);
break;
}
// Read all multiline text
while (!f.eos()) {
stmp = f.readLine(); line++;
if (!stmp.empty()) {
text += '\n';
text += stmp;
} else {
break;
}
}
uint32 seq = atol(sseq.c_str());
if (seq == 0) {
warning("Bad SRT file format (seq): %s at line %d", fname, line);
break;
}
// 00:20:41,150 --> 00:20:45,109
if (stimespec.size() < 29) {
warning("Bad SRT file format (timespec length %d): %s at line %d", stimespec.size(), fname, line);
break;
}
const char *ptr = stimespec.c_str();
uint32 start, end;
if (!parseTime(&ptr, &start)) {
warning("Bad SRT file format (timespec start): %s at line %d", fname, line);
break;
}
while (*ptr == ' ')
ptr++;
while (*ptr == '-')
ptr++;
if (*ptr != '>') {
warning("Bad SRT file format (timespec middle ('%c')): %s at line %d", *ptr, fname, line);
break;
}
ptr++;
while (*ptr == ' ')
ptr++;
if (!parseTime(&ptr, &end)) {
warning("Bad SRT file format (timespec end): %s at line %d", fname, line);
break;
}
_entries.push_back(new SRTEntry(seq, start, end, text));
}
qsort(_entries.data(), _entries.size(), sizeof(SRTEntry *), &SRTEntryComparator);
debug(6, "SRTParser: Loaded %d entries", _entries.size());
return true;
}
Common::String SRTParser::getSubtitle(uint32 timestamp) {
SRTEntry test(0, timestamp, 0, "");
SRTEntry *testptr = &test;
const SRTEntry **entry = (const SRTEntry **)bsearch(&testptr, _entries.data(), _entries.size(), sizeof(SRTEntry *), &SRTEntryComparatorBSearch);
if (entry == NULL)
return "";
return (*entry)->text;
}
#define SHADOW 1
Subtitles::Subtitles() : _loaded(false), _font(nullptr), _hPad(0), _vPad(0), _overlayHasAlpha(true),
_lastOverlayWidth(-1), _lastOverlayHeight(-1) {
_surface = new Graphics::Surface();
_subtitleDev = ConfMan.getBool("subtitle_dev");
}
Subtitles::~Subtitles() {
delete _surface;
}
void Subtitles::setFont(const char *fontname, int height) {
Common::File file;
_fontHeight = height;
#ifdef USE_FREETYPE2
if (file.open(fontname)) {
_font = Graphics::loadTTFFont(file, _fontHeight, Graphics::kTTFSizeModeCharacter, 96);
}
if (!_font) {
_font = Graphics::loadTTFFontFromArchive(fontname, _fontHeight, Graphics::kTTFSizeModeCharacter, 96);
}
#endif
if (!_font) {
debug(1, "Cannot load font %s directly", fontname);
_font = FontMan.getFontByName(fontname);
}
if (!_font) {
warning("Cannot load font %s", fontname);
_font = FontMan.getFontByUsage(Graphics::FontManager::kBigGUIFont);
}
}
void Subtitles::loadSRTFile(const char *fname) {
debug(1, "loadSRTFile('%s')", fname);
if (_subtitleDev) {
_fname = fname;
}
_loaded = _srtParser.parseFile(fname);
}
void Subtitles::setBBox(const Common::Rect bbox) {
_requestedBBox = bbox;
Graphics::PixelFormat overlayFormat = g_system->getOverlayFormat();
_overlayHasAlpha = overlayFormat.aBits() != 0;
_surface->create(_requestedBBox.width() + SHADOW * 2, _requestedBBox.height() + SHADOW * 2, overlayFormat);
// Force recalculation of real bounding box
_lastOverlayWidth = -1;
_lastOverlayHeight = -1;
}
void Subtitles::setColor(byte r, byte g, byte b) {
_color = _surface->format.ARGBToColor(255, r, g, b);
_blackColor = _surface->format.ARGBToColor(255, 0, 0, 0);
_transparentColor = _surface->format.ARGBToColor(0, 0, 0, 0);
}
void Subtitles::setPadding(uint16 horizontal, uint16 vertical) {
_hPad = horizontal;
_vPad = vertical;
}
bool Subtitles::drawSubtitle(uint32 timestamp, bool force) {
Common::String subtitle;
if (_loaded) {
subtitle = _srtParser.getSubtitle(timestamp);
} else if (_subtitleDev) {
subtitle = _fname;
uint32 hours, mins, secs, msecs;
secs = timestamp / 1000;
hours = secs / 3600;
mins = (secs / 60) % 60;
secs %= 60;
msecs = timestamp % 1000;
subtitle += " " + Common::String::format("%02u:%02u:%02u,%03u", hours, mins, secs, msecs);
} else {
return false;
}
int16 width = g_system->getOverlayWidth(),
height = g_system->getOverlayHeight();
if (width != _lastOverlayWidth ||
height != _lastOverlayHeight) {
_lastOverlayWidth = width;
_lastOverlayHeight = height;
// Recalculate the real bounding box to use
_realBBox = _requestedBBox;
if (_realBBox.bottom > height) {
// First try to move the bounding box
_realBBox.top -= _realBBox.bottom - height;
_realBBox.bottom = height;
}
if (_realBBox.top < 0) {
// Not enough space
_realBBox.top = 0;
}
if (_realBBox.right > width) {
// First try to move the bounding box
_realBBox.left -= _realBBox.right - width;
_realBBox.right = width;
}
if (_realBBox.left < 0) {
// Not enough space
_realBBox.left = 0;
}
force = true;
}
if (!force && _overlayHasAlpha && subtitle == _subtitle)
return false;
if (force || subtitle != _subtitle) {
debug(1, "%d: %s", timestamp, subtitle.c_str());
_subtitle = subtitle;
renderSubtitle();
}
if (_overlayHasAlpha) {
// When we have alpha, draw the whole surface without thinking it more
g_system->copyRectToOverlay(_surface->getPixels(), _surface->pitch, _realBBox.left, _realBBox.top, _realBBox.width(), _realBBox.height());
} else {
// When overlay doesn't have alpha, showing it hides the underlying game screen
// We force a copy of the game screen to the overlay by clearing it
// We then draw the smallest possible surface to minimize black rectangle behind text
g_system->clearOverlay();
g_system->copyRectToOverlay((byte *)_surface->getPixels() + _drawRect.top * _surface->pitch + _drawRect.left * _surface->format.bytesPerPixel, _surface->pitch,
_realBBox.left + _drawRect.left, _realBBox.top + _drawRect.top, _drawRect.width(), _drawRect.height());
}
return true;
}
void Subtitles::renderSubtitle() {
_surface->fillRect(Common::Rect(0, 0, _surface->w, _surface->h), _transparentColor);
Common::Array<Common::U32String> lines;
_font->wordWrapText(convertUtf8ToUtf32(_subtitle), _realBBox.width(), lines);
if (lines.empty()) {
_drawRect.left = 0;
_drawRect.top = 0;
_drawRect.right = 0;
_drawRect.bottom = 0;
return;
}
int height = _vPad;
int width = 0;
for (uint i = 0; i < lines.size(); i++)
width = MAX(_font->getStringWidth(lines[i]), width);
width = MIN(width + 2 * _hPad, (int)_realBBox.width());
int originX = (_realBBox.width() - width) / 2;
for (uint i = 0; i < lines.size(); i++) {
Common::U32String line = convertBiDiU32String(lines[i]).visual;
_font->drawString(_surface, line, originX, height, width, _blackColor, Graphics::kTextAlignCenter);
_font->drawString(_surface, line, originX + SHADOW * 2, height, width, _blackColor, Graphics::kTextAlignCenter);
_font->drawString(_surface, line, originX, height + SHADOW * 2, width, _blackColor, Graphics::kTextAlignCenter);
_font->drawString(_surface, line, originX + SHADOW * 2, height + SHADOW * 2, width, _blackColor, Graphics::kTextAlignCenter);
_font->drawString(_surface, line, originX + SHADOW, height + SHADOW, width, _color, Graphics::kTextAlignCenter);
height += _font->getFontHeight();
if (height + _vPad > _realBBox.bottom)
break;
}
height += _vPad;
_drawRect.left = originX;
_drawRect.top = 0;
_drawRect.setWidth(width + SHADOW * 2);
_drawRect.setHeight(height + SHADOW * 2);
}
} // End of namespace Video
|