diff options
Diffstat (limited to 'engines/zvision/text')
-rw-r--r-- | engines/zvision/text/string_manager.cpp | 255 | ||||
-rw-r--r-- | engines/zvision/text/string_manager.h | 84 | ||||
-rw-r--r-- | engines/zvision/text/text.cpp | 549 | ||||
-rw-r--r-- | engines/zvision/text/text.h | 100 |
4 files changed, 988 insertions, 0 deletions
diff --git a/engines/zvision/text/string_manager.cpp b/engines/zvision/text/string_manager.cpp new file mode 100644 index 0000000000..cadfbf1f49 --- /dev/null +++ b/engines/zvision/text/string_manager.cpp @@ -0,0 +1,255 @@ +/* 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/string_manager.h" + +#include "zvision/fonts/truetype_font.h" + +#include "common/file.h" +#include "common/tokenizer.h" +#include "common/debug.h" + +#include "graphics/fontman.h" +#include "graphics/colormasks.h" + + +namespace ZVision { + +StringManager::StringManager(ZVision *engine) + : _engine(engine) { +} + +StringManager::~StringManager() { + for (Common::HashMap<Common::String, TruetypeFont *>::iterator iter = _fonts.begin(); iter != _fonts.end(); ++iter) { + delete iter->_value; + } +} + +void StringManager::initialize(ZVisionGameId gameId) { + if (gameId == GID_NEMESIS) { + // TODO: Check this hardcoded filename against all versions of Nemesis + parseStrFile("nemesis.str"); + } else if (gameId == GID_GRANDINQUISITOR) { + // TODO: Check this hardcoded filename against all versions of Grand Inquisitor + parseStrFile("inquis.str"); + } +} + +void StringManager::parseStrFile(const Common::String &fileName) { + Common::File file; + if (!file.open(fileName)) { + warning("%s does not exist. String parsing failed", fileName.c_str()); + return; + } + + uint lineNumber = 0; + while (!file.eos()) { + _lastStyle.align = Graphics::kTextAlignLeft; + _lastStyle.color = 0; + _lastStyle.font = nullptr; + + Common::String asciiLine = readWideLine(file); + if (asciiLine.empty()) { + continue; + } + + char tagString[150]; + uint tagStringCursor = 0; + char textString[150]; + uint textStringCursor = 0; + bool inTag = false; + + for (uint i = 0; i < asciiLine.size(); ++i) { + switch (asciiLine[i]) { + case '<': + inTag = true; + if (!_inGameText[lineNumber].fragments.empty()) { + _inGameText[lineNumber].fragments.back().text = Common::String(textString, textStringCursor); + textStringCursor = 0; + } + break; + case '>': + inTag = false; + parseTag(Common::String(tagString, tagStringCursor), lineNumber); + tagStringCursor = 0; + break; + default: + if (inTag) { + tagString[tagStringCursor] = asciiLine[i]; + tagStringCursor++; + } else { + textString[textStringCursor] = asciiLine[i]; + textStringCursor++; + } + break; + } + } + + if (textStringCursor > 0) { + _inGameText[lineNumber].fragments.back().text = Common::String(textString, textStringCursor); + } + + lineNumber++; + assert(lineNumber <= NUM_TEXT_LINES); + } +} + +void StringManager::parseTag(const Common::String &tagString, uint lineNumber) { + Common::StringTokenizer tokenizer(tagString); + + Common::String token = tokenizer.nextToken(); + + Common::String fontName; + bool bold = false; + Graphics::TextAlign align = _lastStyle.align; + int point = _lastStyle.font != nullptr ? _lastStyle.font->_fontHeight : 12; + int red = 0; + int green = 0; + int blue = 0; + + while (!token.empty()) { + if (token.matchString("font", true)) { + fontName = tokenizer.nextToken(); + } else if (token.matchString("bold", true)) { + token = tokenizer.nextToken(); + if (token.matchString("on", false)) { + bold = true; + } + } else if (token.matchString("justify", true)) { + token = tokenizer.nextToken(); + if (token.matchString("center", false)) { + align = Graphics::kTextAlignCenter; + } else if (token.matchString("right", false)) { + align = Graphics::kTextAlignRight; + } + } else if (token.matchString("point", true)) { + point = atoi(tokenizer.nextToken().c_str()); + } else if (token.matchString("red", true)) { + red = atoi(tokenizer.nextToken().c_str()); + } else if (token.matchString("green", true)) { + green = atoi(tokenizer.nextToken().c_str()); + } else if (token.matchString("blue", true)) { + blue = atoi(tokenizer.nextToken().c_str()); + } + + token = tokenizer.nextToken(); + } + + TextFragment fragment; + + if (fontName.empty()) { + fragment.style.font = _lastStyle.font; + } else { + Common::String newFontName; + if (fontName.matchString("*times new roman*", true)) { + if (bold) { + newFontName = "timesbd.ttf"; + } else { + newFontName = "times.ttf"; + } + } else if (fontName.matchString("*courier new*", true)) { + if (bold) { + newFontName = "courbd.ttf"; + } else { + newFontName = "cour.ttf"; + } + } else if (fontName.matchString("*century schoolbook*", true)) { + if (bold) { + newFontName = "censcbkbd.ttf"; + } else { + newFontName = "censcbk.ttf"; + } + } else if (fontName.matchString("*garamond*", true)) { + if (bold) { + newFontName = "garabd.ttf"; + } else { + newFontName = "gara.ttf"; + } + } else { + debug("Could not identify font: %s. Reverting to Arial", fontName.c_str()); + if (bold) { + newFontName = "zorknorm.ttf"; + } else { + newFontName = "arial.ttf"; + } + } + + Common::String fontKey = Common::String::format("%s-%d", newFontName.c_str(), point); + if (_fonts.contains(fontKey)) { + fragment.style.font = _fonts[fontKey]; + } else { + fragment.style.font = new TruetypeFont(_engine, point); + fragment.style.font->loadFile(newFontName); + _fonts[fontKey] = fragment.style.font; + } + } + + fragment.style.align = align; + fragment.style.color = Graphics::ARGBToColor<Graphics::ColorMasks<565> >(0, red, green, blue); + _inGameText[lineNumber].fragments.push_back(fragment); + + _lastStyle = fragment.style; +} + +Common::String StringManager::readWideLine(Common::SeekableReadStream &stream) { + Common::String asciiString; + + // Don't spam the user with warnings about UTF-16 support. + // Just do one warning per String + bool charOverflowWarning = false; + + uint16 value = stream.readUint16LE(); + while (!stream.eos()) { + // 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 > 255) { + charOverflowWarning = true; + value = '?'; + } + char charValue = (char)value; + + asciiString += charValue; + + value = stream.readUint16LE(); + } + + if (charOverflowWarning) { + warning("UTF-16 is not supported. Characters greater than 255 are replaced with '?'"); + } + + return asciiString; +} + +StringManager::TextStyle StringManager::getTextStyle(uint stringNumber) { + return _inGameText[stringNumber].fragments.front().style; +} + +} // End of namespace ZVision diff --git a/engines/zvision/text/string_manager.h b/engines/zvision/text/string_manager.h new file mode 100644 index 0000000000..af8324b890 --- /dev/null +++ b/engines/zvision/text/string_manager.h @@ -0,0 +1,84 @@ +/* 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_STRING_MANAGER_H +#define ZVISION_STRING_MANAGER_H + +#include "zvision/detection.h" +#include "zvision/fonts/truetype_font.h" + + +namespace Graphics { +class FontManager; +} + +namespace ZVision { + +class ZVision; + +class StringManager { +public: + StringManager(ZVision *engine); + ~StringManager(); + +public: + struct TextStyle { + TruetypeFont *font; + uint16 color; // In RBG 565 + Graphics::TextAlign align; + }; + + struct TextFragment { + TextStyle style; + Common::String text; + }; + +private: + struct InGameText { + Common::List<TextFragment> fragments; + }; + + enum { + NUM_TEXT_LINES = 56 // Max number of lines in a .str file. We hardcode this number because we know ZNem uses 42 strings and ZGI uses 56 + }; + +private: + ZVision *_engine; + InGameText _inGameText[NUM_TEXT_LINES]; + Common::HashMap<Common::String, TruetypeFont *> _fonts; + + TextStyle _lastStyle; + +public: + void initialize(ZVisionGameId gameId); + StringManager::TextStyle getTextStyle(uint stringNumber); + +private: + void parseStrFile(const Common::String &fileName); + void parseTag(const Common::String &tagString, uint lineNumber); + + static Common::String readWideLine(Common::SeekableReadStream &stream); +}; + +} // End of namespace ZVision + +#endif diff --git a/engines/zvision/text/text.cpp b/engines/zvision/text/text.cpp new file mode 100644 index 0000000000..cad2723d13 --- /dev/null +++ b/engines/zvision/text/text.cpp @@ -0,0 +1,549 @@ +/* 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/text.h" + +#include "zvision/fonts/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/graphics/render_manager.h" +#include "zvision/scripting/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; + sharp = false; +} + +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; + + if (sharp) + temp_stl |= sTTFont::STTF_SHARP; + + 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; + + if (sharp) + temp_stl |= sTTFont::STTF_SHARP; + + 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; +} + +int8 getUtf8CharSize(char chr) { + if ((chr & 0x80) == 0) + return 1; + else if ((chr & 0xE0) == 0xC0) + return 2; + else if ((chr & 0xF0) == 0xE0) + return 3; + else if ((chr & 0xF8) == 0xF0) + return 4; + else if ((chr & 0xFC) == 0xF8) + return 5; + else if ((chr & 0xFE) == 0xFC) + return 6; + + return 1; +} + +uint16 readUtf8Char(const char *chr) { + uint16 result = 0; + if ((chr[0] & 0x80) == 0) + result = chr[0]; + else if ((chr[0] & 0xE0) == 0xC0) + result = ((chr[0] & 0x1F) << 6) | (chr[1] & 0x3F); + else if ((chr[0] & 0xF0) == 0xE0) + result = ((chr[0] & 0x0F) << 12) | ((chr[1] & 0x3F) << 6) | (chr[2] & 0x3F); + else + result = chr[0]; + + return result; +} + +} // End of namespace ZVision diff --git a/engines/zvision/text/text.h b/engines/zvision/text/text.h new file mode 100644 index 0000000000..4b318fdef0 --- /dev/null +++ b/engines/zvision/text/text.h @@ -0,0 +1,100 @@ +/* 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/fonts/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; + bool sharp; + // 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); +int8 getUtf8CharSize(char chr); +uint16 readUtf8Char(const char *chr); + +} // End of namespace ZVision + +#endif |