diff options
author | Marisa-Chan | 2014-02-24 22:31:06 +0700 |
---|---|---|
committer | Marisa-Chan | 2014-02-24 22:31:06 +0700 |
commit | cf08849147905ffd12cd9571b617033ded5a95fb (patch) | |
tree | f61cdf0a394b2416a9452c87cb1c7c677ccbfd0f /engines | |
parent | dcfe35c8ac1cd2c9139f5d7a59f0f554e3c5082b (diff) | |
download | scummvm-rg350-cf08849147905ffd12cd9571b617033ded5a95fb.tar.gz scummvm-rg350-cf08849147905ffd12cd9571b617033ded5a95fb.tar.bz2 scummvm-rg350-cf08849147905ffd12cd9571b617033ded5a95fb.zip |
ZVISION: Implement textRender with tags parser.
Diffstat (limited to 'engines')
-rw-r--r-- | engines/zvision/module.mk | 3 | ||||
-rw-r--r-- | engines/zvision/text.cpp | 512 | ||||
-rw-r--r-- | engines/zvision/text.h | 97 | ||||
-rw-r--r-- | engines/zvision/zvision.cpp | 3 | ||||
-rw-r--r-- | engines/zvision/zvision.h | 5 |
5 files changed, 619 insertions, 1 deletions
diff --git a/engines/zvision/module.mk b/engines/zvision/module.mk index b8dca9e63a..ea70b4b831 100644 --- a/engines/zvision/module.mk +++ b/engines/zvision/module.mk @@ -36,7 +36,8 @@ MODULE_OBJS := \ slot_control.o \ menu.o \ meta_animation.o \ - search_manager.o + search_manager.o \ + text.o MODULE_DIRS += \ engines/zvision diff --git a/engines/zvision/text.cpp b/engines/zvision/text.cpp new file mode 100644 index 0000000000..575ca03d10 --- /dev/null +++ b/engines/zvision/text.cpp @@ -0,0 +1,512 @@ +/* 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/scummsys.h" + +#include "zvision/text.h" + +#include "zvision/truetype_font.h" + +#include "common/file.h" +#include "common/tokenizer.h" +#include "common/debug.h" +#include "common/rect.h" + +#include "graphics/fontman.h" +#include "graphics/colormasks.h" +#include "graphics/surface.h" +#include "graphics/font.h" +#include "graphics/fonts/ttf.h" + +#include "zvision/render_manager.h" +#include "zvision/script_manager.h" + + +namespace ZVision { + +cTxtStyle::cTxtStyle() { + fontname = "Arial"; + blue = 255; + green = 255; + red = 255; + bold = false; + escapement = 0; + italic = false; + justify = TXT_JUSTIFY_LEFT; + newline = false; + size = 12; + skipcolor = false; + strikeout = false; + underline = false; + statebox = 0; +} + +txtReturn cTxtStyle::parseStyle(const Common::String &strin, int16 ln) { + Common::String buf = Common::String(strin.c_str(), ln); + + int8 retval = TXT_RET_NOTHING; + + Common::StringTokenizer tokenizer(buf, " "); + Common::String token; + + while (!tokenizer.empty()) { + token = tokenizer.nextToken(); + + if (token.matchString("font", true)) { + token = tokenizer.nextToken(); + if (token[0] == '"') { + Common::String _tmp = Common::String(token.c_str() + 1); + + while (token.lastChar() != '"' && !tokenizer.empty()) { + token = tokenizer.nextToken(); + _tmp += " " + token; + } + + if (_tmp.lastChar() == '"') + _tmp.deleteLastChar(); + + fontname = _tmp; + } else { + if (!tokenizer.empty()) + fontname = token; + } + retval |= TXT_RET_FNTCHG; + + } else if (token.matchString("blue", true)) { + if (!tokenizer.empty()) { + token = tokenizer.nextToken(); + int32 tmp = atoi(token.c_str()); + if (blue != tmp) { + blue = tmp; + retval |= TXT_RET_FNTSTL; + } + } + } else if (token.matchString("red", true)) { + if (!tokenizer.empty()) { + token = tokenizer.nextToken(); + int32 tmp = atoi(token.c_str()); + if (red != tmp) { + red = tmp; + retval |= TXT_RET_FNTSTL; + } + } + } else if (token.matchString("green", true)) { + if (!tokenizer.empty()) { + token = tokenizer.nextToken(); + int32 tmp = atoi(token.c_str()); + if (green != tmp) { + green = tmp; + retval |= TXT_RET_FNTSTL; + } + } + } else if (token.matchString("newline", true)) { + if ((retval & TXT_RET_NEWLN) == 0) + newline = 0; + + newline++; + retval |= TXT_RET_NEWLN; + } else if (token.matchString("point", true)) { + if (!tokenizer.empty()) { + token = tokenizer.nextToken(); + int32 tmp = atoi(token.c_str()); + if (size != tmp) { + size = tmp; + retval |= TXT_RET_FNTCHG; + } + } + } else if (token.matchString("escapement", true)) { + if (!tokenizer.empty()) { + token = tokenizer.nextToken(); + int32 tmp = atoi(token.c_str()); + escapement = tmp; + } + } else if (token.matchString("italic", true)) { + if (!tokenizer.empty()) { + token = tokenizer.nextToken(); + if (token.matchString("on", true)) { + if (italic != true) { + italic = true; + retval |= TXT_RET_FNTSTL; + } + } else if (token.matchString("off", true)) { + if (italic != false) { + italic = false; + retval |= TXT_RET_FNTSTL; + } + } + } + } else if (token.matchString("underline", true)) { + if (!tokenizer.empty()) { + token = tokenizer.nextToken(); + if (token.matchString("on", true)) { + if (underline != true) { + underline = true; + retval |= TXT_RET_FNTSTL; + } + } else if (token.matchString("off", true)) { + if (underline != false) { + underline = false; + retval |= TXT_RET_FNTSTL; + } + } + } + } else if (token.matchString("strikeout", true)) { + if (!tokenizer.empty()) { + token = tokenizer.nextToken(); + if (token.matchString("on", true)) { + if (strikeout != true) { + strikeout = true; + retval |= TXT_RET_FNTSTL; + } + } else if (token.matchString("off", true)) { + if (strikeout != false) { + strikeout = false; + retval |= TXT_RET_FNTSTL; + } + } + } + } else if (token.matchString("bold", true)) { + if (!tokenizer.empty()) { + token = tokenizer.nextToken(); + if (token.matchString("on", true)) { + if (bold != true) { + bold = true; + retval |= TXT_RET_FNTSTL; + } + } else if (token.matchString("off", true)) { + if (bold != false) { + bold = false; + retval |= TXT_RET_FNTSTL; + } + } + } + } else if (token.matchString("skipcolor", true)) { + if (!tokenizer.empty()) { + token = tokenizer.nextToken(); + if (token.matchString("on", true)) { + skipcolor = true; + } else if (token.matchString("off", true)) { + skipcolor = false; + } + } + } else if (token.matchString("image", true)) { + // Not used + } else if (token.matchString("statebox", true)) { + if (!tokenizer.empty()) { + token = tokenizer.nextToken(); + statebox = atoi(token.c_str()); + retval |= TXT_RET_HASSTBOX; + } + } else if (token.matchString("justify", true)) { + if (!tokenizer.empty()) { + token = tokenizer.nextToken(); + if (token.matchString("center", true)) + justify = TXT_JUSTIFY_CENTER; + else if (token.matchString("left", true)) + justify = TXT_JUSTIFY_LEFT; + else if (token.matchString("right", true)) + justify = TXT_JUSTIFY_RIGHT; + } + } + } + return (txtReturn)retval; +} + +void cTxtStyle::readAllStyle(const Common::String &txt) { + int16 strt = -1; + int16 endt = -1; + + for (uint16 i = 0; i < txt.size(); i++) { + if (txt[i] == '<') + strt = i; + else if (txt[i] == '>') { + endt = i; + if (strt != -1) + if ((endt - strt - 1) > 0) + parseStyle(Common::String(txt.c_str() + strt + 1), endt - strt - 1); + } + + } +} + +void cTxtStyle::setFontStyle(sTTFont &font) { + uint temp_stl = 0; + + if (bold) + temp_stl |= sTTFont::STTF_BOLD; + + if (italic) + temp_stl |= sTTFont::STTF_ITALIC; + + if (underline) + temp_stl |= sTTFont::STTF_UNDERLINE; + + if (strikeout) + temp_stl |= sTTFont::STTF_STRIKEOUT; + + font.setStyle(temp_stl); +} + +void cTxtStyle::setFont(sTTFont &font) { + uint temp_stl = 0; + + if (bold) + temp_stl |= sTTFont::STTF_BOLD; + + if (italic) + temp_stl |= sTTFont::STTF_ITALIC; + + if (underline) + temp_stl |= sTTFont::STTF_UNDERLINE; + + if (strikeout) + temp_stl |= sTTFont::STTF_STRIKEOUT; + + font.loadFont(fontname, size, temp_stl); +} + +Graphics::Surface *textRenderer::render(sTTFont &fnt, const Common::String &txt, cTxtStyle &style) { + style.setFontStyle(fnt); + uint32 clr = _engine->_pixelFormat.RGBToColor(style.red, style.green, style.blue); + return fnt.renderSolidText(txt, clr); +} + +void textRenderer::drawTxtWithJustify(const Common::String &txt, sTTFont &fnt, uint32 color, Graphics::Surface &dst, int lineY, txtJustify justify) { + if (justify == TXT_JUSTIFY_LEFT) + fnt.drawString(&dst, txt, 0, lineY, dst.w, color, Graphics::kTextAlignLeft); + else if (justify == TXT_JUSTIFY_CENTER) + fnt.drawString(&dst, txt, 0, lineY, dst.w, color, Graphics::kTextAlignCenter); + else if (justify == TXT_JUSTIFY_RIGHT) + fnt.drawString(&dst, txt, 0, lineY, dst.w, color, Graphics::kTextAlignRight); +} + +int32 textRenderer::drawTxt(const Common::String &txt, cTxtStyle &fnt_stl, Graphics::Surface &dst) { + sTTFont font(_engine); + fnt_stl.setFont(font); + + dst.fillRect(Common::Rect(dst.w, dst.h), 0); + + uint32 clr = _engine->_pixelFormat.RGBToColor(fnt_stl.red, fnt_stl.green, fnt_stl.blue); + + int16 w; + + w = font.getStringWidth(txt); + + drawTxtWithJustify(txt, font, clr, dst, fnt_stl.size, fnt_stl.justify); + + return w; +} + +void textRenderer::drawTxtInOneLine(const Common::String &text, Graphics::Surface &dst) { + const int16 TXT_CFG_TEXTURES_LINES = 256; // For now I don't want remake it + const int TXT_CFG_TEXTURES_PER_LINE = 6; + cTxtStyle style, style2; + int16 strt = -1; + int16 endt = -1; + int16 i = 0; + int16 dx = 0, dy = 0; + int16 txt_w; + int16 txtpos = 0; + Common::String buf; + Common::String buf2; + + Graphics::Surface *TxtSurfaces[TXT_CFG_TEXTURES_LINES][TXT_CFG_TEXTURES_PER_LINE]; + int16 currentline = 0, currentlineitm = 0; + + int TxtJustify[TXT_CFG_TEXTURES_LINES]; + int TxtPoint[TXT_CFG_TEXTURES_LINES]; + + for (int16 k = 0; k < TXT_CFG_TEXTURES_LINES; k++) { + TxtPoint[k] = 0; + for (int j = 0; j < TXT_CFG_TEXTURES_PER_LINE; j++) + TxtSurfaces[k][j] = NULL; + } + + int16 stringlen = text.size(); + + sTTFont font(_engine); + + style.setFont(font); + + int16 prevbufspace = 0, prevtxtspace = 0; + + while (i < stringlen) { + TxtJustify[currentline] = style.justify; + if (text[i] == '<') { + int16 ret = 0; + + strt = i; + while (i < stringlen && text[i] != '>') + i++; + endt = i; + if (strt != -1) + if ((endt - strt - 1) > 0) { + style2 = style; + ret = style.parseStyle(Common::String(text.c_str() + strt + 1), endt - strt - 1); + } + + if (ret & (TXT_RET_FNTCHG | TXT_RET_FNTSTL | TXT_RET_NEWLN)) { + if (buf.size() > 0) { + txt_w = font.getStringWidth(buf); + + TxtSurfaces[currentline][currentlineitm] = render(font, buf, style2); + TxtPoint[currentline] = MAX(font.getFontHeight(), TxtPoint[currentline]); + + currentlineitm++; + + buf.clear(); + prevbufspace = 0; + txtpos = 0; + dx += txt_w; + + } + if (ret & TXT_RET_FNTCHG) { + style.setFont(font); + } + if (ret & TXT_RET_FNTSTL) + style.setFontStyle(font); + + if (ret & TXT_RET_NEWLN) { + currentline++; + currentlineitm = 0; + dx = 0; + } + } + + if (ret & TXT_RET_HASSTBOX) { + Common::String buf3; + buf3.format("%d", _engine->getScriptManager()->getStateValue(style.statebox)); + buf += buf3; + txtpos += buf3.size(); + } + + } else { + + buf += text[i]; + txtpos++; + + if (text[i] == ' ') { + prevbufspace = txtpos - 1; + prevtxtspace = i; + } + + if (font.isLoaded()) { + txt_w = font.getStringWidth(buf); + if (txt_w + dx > dst.w) { + if (prevbufspace == 0) { + prevtxtspace = i; + prevbufspace = txtpos - 1; + } + buf2 = Common::String(buf.c_str(), prevbufspace + 1); + + if (buf2.size() > 0) { + TxtSurfaces[currentline][currentlineitm] = render(font, buf2, style); + TxtPoint[currentline] = MAX(font.getFontHeight(), TxtPoint[currentline]); + } + + buf.clear(); + i = prevtxtspace; + prevbufspace = 0; + txtpos = 0; + currentline++; + currentlineitm = 0; + dx = 0; + } + } + } + i++; + } + + if (buf.size() > 0) { + TxtSurfaces[currentline][currentlineitm] = render(font, buf, style); + TxtPoint[currentline] = MAX(font.getFontHeight(), TxtPoint[currentline]); + } + + dy = 0; + for (i = 0; i <= currentline; i++) { + int16 j = 0; + int16 width = 0; + while (TxtSurfaces[i][j] != NULL) { + width += TxtSurfaces[i][j]->w; + j++; + } + dx = 0; + for (int32_t jj = 0; jj < j; jj++) { + if (TxtJustify[i] == TXT_JUSTIFY_LEFT) + _engine->getRenderManager()->blitSurfaceToSurface(*TxtSurfaces[i][jj], dst, dx, dy + TxtPoint[i] - TxtSurfaces[i][jj]->h, 0); + + else if (TxtJustify[i] == TXT_JUSTIFY_CENTER) + _engine->getRenderManager()->blitSurfaceToSurface(*TxtSurfaces[i][jj], dst, ((dst.w - width) / 2) + dx, dy + TxtPoint[i] - TxtSurfaces[i][jj]->h, 0); + + else if (TxtJustify[i] == TXT_JUSTIFY_RIGHT) + _engine->getRenderManager()->blitSurfaceToSurface(*TxtSurfaces[i][jj], dst, dst.w - width + dx, dy + TxtPoint[i] - TxtSurfaces[i][jj]->h, 0); + + dx += TxtSurfaces[i][jj]->w; + } + + dy += TxtPoint[i]; + } + + for (i = 0; i < TXT_CFG_TEXTURES_LINES; i++) + for (int32_t j = 0; j < TXT_CFG_TEXTURES_PER_LINE; j++) + if (TxtSurfaces[i][j] != NULL) { + TxtSurfaces[i][j]->free(); + delete TxtSurfaces[i][j]; + } +} + +Common::String readWideLine(Common::SeekableReadStream &stream) { + Common::String asciiString; + + while (!stream.eos()) { + uint32 value = stream.readUint16LE(); + // Check for CRLF + if (value == 0x0A0D) { + // Read in the extra NULL char + stream.readByte(); // \0 + // End of the line. Break + break; + } + + // Crush each octet pair to a single octet with a simple cast + if (value < 0x80) { + asciiString += (char)(value & 0x7F); + } else if (value >= 0x80 && value < 0x800) { + asciiString += (char)(0xC0 | ((value >> 6) & 0x1F)); + asciiString += (char)(0x80 | (value & 0x3F)); + } else if (value >= 0x800 && value < 0x10000) { + asciiString += (char)(0xE0 | ((value >> 12) & 0xF)); + asciiString += (char)(0x80 | ((value >> 6) & 0x3F)); + asciiString += (char)(0x80 | (value & 0x3F)); + } else if (value >= 0x10000 && value < 0x200000) { + asciiString += (char)(0xF0); + asciiString += (char)(0x80 | ((value >> 12) & 0x3F)); + asciiString += (char)(0x80 | ((value >> 6) & 0x3F)); + asciiString += (char)(0x80 | (value & 0x3F)); + } + } + + return asciiString; +} + + +} // End of namespace ZVision diff --git a/engines/zvision/text.h b/engines/zvision/text.h new file mode 100644 index 0000000000..40244f7f0c --- /dev/null +++ b/engines/zvision/text.h @@ -0,0 +1,97 @@ +/* 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. + * + * + */ + +#ifndef ZVISION_TEXT_H +#define ZVISION_TEXT_H + +#include "zvision/detection.h" +#include "zvision/truetype_font.h" +#include "zvision/zvision.h" + + +namespace Graphics { +class FontManager; +} + +namespace ZVision { + +class ZVision; + +enum txtJustify { + TXT_JUSTIFY_CENTER = 0, + TXT_JUSTIFY_LEFT = 1, + TXT_JUSTIFY_RIGHT = 2 +}; + +enum txtReturn { + TXT_RET_NOTHING = 0x0, + TXT_RET_FNTCHG = 0x1, + TXT_RET_FNTSTL = 0x2, + TXT_RET_NEWLN = 0x4, + TXT_RET_HASSTBOX = 0x8 +}; + +class cTxtStyle { +public: + cTxtStyle(); + txtReturn parseStyle(const Common::String &strin, int16 len); + void readAllStyle(const Common::String &txt); + void setFontStyle(sTTFont &font); + void setFont(sTTFont &font); + +public: + Common::String fontname; + txtJustify justify; // 0 - center, 1-left, 2-right + int16 size; + uint8 red; // 0-255 + uint8 green; // 0-255 + uint8 blue; // 0-255 + int8 newline; + int8 escapement; + bool italic; + bool bold; + bool underline; + bool strikeout; + bool skipcolor; + int32 statebox; + // char image ?? +}; + +class textRenderer { +public: + textRenderer(ZVision *engine): _engine(engine) {}; + + void drawTxtWithJustify(const Common::String &txt, sTTFont &fnt, uint32 color, Graphics::Surface &dst, int lineY, txtJustify justify); + int32 drawTxt(const Common::String &txt, cTxtStyle &fnt_stl, Graphics::Surface &dst); + Graphics::Surface *render(sTTFont &fnt, const Common::String &txt, cTxtStyle &style); + void drawTxtInOneLine(const Common::String &txt, Graphics::Surface &dst); + +private: + ZVision *_engine; +}; + +Common::String readWideLine(Common::SeekableReadStream &stream); + +} // End of namespace ZVision + +#endif diff --git a/engines/zvision/zvision.cpp b/engines/zvision/zvision.cpp index bf945f859a..178c7978ae 100644 --- a/engines/zvision/zvision.cpp +++ b/engines/zvision/zvision.cpp @@ -34,6 +34,7 @@ #include "zvision/detection.h" #include "zvision/menu.h" #include "zvision/search_manager.h" +#include "zvision/text.h" #include "common/config-manager.h" #include "common/str.h" @@ -93,6 +94,7 @@ void ZVision::initialize() { _searchManager = new sManager(ConfMan.get("path"), 6); + _searchManager->addDir("FONTS"); _searchManager->addDir("addon"); if (_gameDescription->gameId == GID_GRANDINQUISITOR) { @@ -152,6 +154,7 @@ void ZVision::initialize() { _saveManager = new SaveManager(this); _stringManager = new StringManager(this); _cursorManager = new CursorManager(this, &_pixelFormat); + _textRenderer = new textRenderer(this); if (_gameDescription->gameId == GID_GRANDINQUISITOR) _menu = new menuZgi(this); diff --git a/engines/zvision/zvision.h b/engines/zvision/zvision.h index 983d5a56eb..bbcaf3c952 100644 --- a/engines/zvision/zvision.h +++ b/engines/zvision/zvision.h @@ -53,6 +53,7 @@ class StringManager; class SaveManager; class RlfAnimation; class menuHandler; +class textRenderer; class ZVision : public Engine { public: @@ -101,6 +102,7 @@ private: StringManager *_stringManager; menuHandler *_menu; sManager *_searchManager; + textRenderer *_textRenderer; // Clock Clock _clock; @@ -140,6 +142,9 @@ public: sManager *getSearchManager() const { return _searchManager; } + textRenderer *getTextRenderer() const { + return _textRenderer; + } Common::RandomSource *getRandomSource() const { return _rnd; } |