From f0ce0b498fe62fa97428ebcd36caa2ae883eb775 Mon Sep 17 00:00:00 2001 From: Filippos Karapetis Date: Wed, 3 Dec 2014 02:29:08 +0200 Subject: ZVISION: Move the TruetypeFont class --- engines/zvision/fonts/truetype_font.cpp | 344 ------------------------ engines/zvision/fonts/truetype_font.h | 123 --------- engines/zvision/graphics/render_manager.h | 2 +- engines/zvision/graphics/truetype_font.cpp | 341 +++++++++++++++++++++++ engines/zvision/graphics/truetype_font.h | 123 +++++++++ engines/zvision/module.mk | 2 +- engines/zvision/scripting/sidefx/ttytext_node.h | 2 +- engines/zvision/text/string_manager.cpp | 13 +- engines/zvision/text/string_manager.h | 2 +- engines/zvision/text/text.cpp | 8 +- engines/zvision/text/text.h | 2 +- engines/zvision/zvision.cpp | 2 +- 12 files changed, 477 insertions(+), 487 deletions(-) delete mode 100644 engines/zvision/fonts/truetype_font.cpp delete mode 100644 engines/zvision/fonts/truetype_font.h create mode 100644 engines/zvision/graphics/truetype_font.cpp create mode 100644 engines/zvision/graphics/truetype_font.h diff --git a/engines/zvision/fonts/truetype_font.cpp b/engines/zvision/fonts/truetype_font.cpp deleted file mode 100644 index a9363d8923..0000000000 --- a/engines/zvision/fonts/truetype_font.cpp +++ /dev/null @@ -1,344 +0,0 @@ -/* 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/fonts/truetype_font.h" - -#include "zvision/zvision.h" -#include "zvision/graphics/render_manager.h" - -#include "common/config-manager.h" -#include "common/debug.h" -#include "common/file.h" -#include "common/system.h" -#include "common/unzip.h" - -#include "graphics/font.h" -#include "graphics/fonts/ttf.h" -#include "graphics/surface.h" - -namespace ZVision { - -TruetypeFont::TruetypeFont(ZVision *engine, int32 fontHeight) - : _engine(engine), - _fontHeight(fontHeight), - _font(0), - _lineHeight(0), - _maxCharWidth(0), - _maxCharHeight(0) { -} - -TruetypeFont::~TruetypeFont(void) { - delete _font; -} - -bool TruetypeFont::loadFile(const Common::String &filename) { - Common::File file; - - bool fileOpened = false; - if (!Common::File::exists(filename)) { - debug("TTF font file %s was not found. Reverting to arial.ttf", filename.c_str()); - fileOpened = file.open("arial.ttf"); - } else { - fileOpened = file.open(filename); - } - - if (!fileOpened) { - debug("TTF file could not be opened"); - return false; - } - - _font = Graphics::loadTTFFont(file, _fontHeight); - _lineHeight = _font->getFontHeight(); - - return true; -} - -Graphics::Surface *TruetypeFont::drawTextToSurface(const Common::String &text, uint16 textColor, int maxWidth, int maxHeight, Graphics::TextAlign align, bool wrap) { - if (text.equals("")) { - return nullptr; - } - - Graphics::Surface *surface = new Graphics::Surface(); - - if (!wrap) { - int width = MIN(_font->getStringWidth(text), maxWidth); - surface->create(width, _lineHeight, Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0)); - // TODO: Add better alpha support by getting the pixels from the backbuffer. - // However doing that requires some kind of caching system so future text doesn't try to use this text as it's alpha background. - surface->fillRect(Common::Rect(0, 0, surface->w, surface->h), 0); - - _font->drawString(surface, text, 0, 0, maxWidth, textColor, align); - return surface; - } - - Common::Array lines; - _font->wordWrapText(text, maxWidth, lines); - - while (maxHeight > 0 && (int)lines.size() * _lineHeight > maxHeight) { - lines.pop_back(); - } - if (lines.size() == 0) { - delete surface; - return nullptr; - } - - surface->create(maxWidth, lines.size() * _lineHeight, Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0)); - surface->fillRect(Common::Rect(0, 0, surface->w, surface->h), 0); - - int heightOffset = 0; - for (Common::Array::iterator it = lines.begin(); it != lines.end(); it++) { - _font->drawString(surface, *it, 0, 0 + heightOffset, maxWidth, textColor, align); - heightOffset += _lineHeight; - } - - return surface; -} - -StyledTTFont::StyledTTFont(ZVision *engine) { - _engine = engine; - _style = 0; - _font = NULL; - _lineHeight = 0; -} - -StyledTTFont::~StyledTTFont() { - if (_font) - delete _font; -} - -bool StyledTTFont::loadFont(const Common::String &fontName, int32 point, uint style) { - _style = style; - return loadFont(fontName, point); -} - -bool StyledTTFont::loadFont(const Common::String &fontName, int32 point) { - Common::String newFontName; - if (fontName.matchString("*times new roman*", true) || fontName.matchString("*times*", true)) { - if ((_style & (STTF_BOLD | STTF_ITALIC)) == (STTF_BOLD | STTF_ITALIC)) - newFontName = "timesbi.ttf"; - else if (_style & STTF_BOLD) - newFontName = "timesbd.ttf"; - else if (_style & STTF_ITALIC) - newFontName = "timesi.ttf"; - else - newFontName = "times.ttf"; - - } else if (fontName.matchString("*courier new*", true) || fontName.matchString("*courier*", true) || fontName.matchString("*ZorkDeath*", true)) { - if ((_style & (STTF_BOLD | STTF_ITALIC)) == (STTF_BOLD | STTF_ITALIC)) - newFontName = "courbi.ttf"; - else if (_style & STTF_BOLD) - newFontName = "courbd.ttf"; - else if (_style & STTF_ITALIC) - newFontName = "couri.ttf"; - else - newFontName = "cour.ttf"; - - } else if (fontName.matchString("*century schoolbook*", true)) { - if ((_style & (STTF_BOLD | STTF_ITALIC)) == (STTF_BOLD | STTF_ITALIC)) - newFontName = "censcbkbi.ttf"; - else if (_style & STTF_BOLD) - newFontName = "censcbkbd.ttf"; - else if (_style & STTF_ITALIC) - newFontName = "censcbki.ttf"; - else - newFontName = "censcbk.ttf"; - - } else if (fontName.matchString("*garamond*", true)) { - if ((_style & (STTF_BOLD | STTF_ITALIC)) == (STTF_BOLD | STTF_ITALIC)) - newFontName = "garabi.ttf"; - else if (_style & STTF_BOLD) - newFontName = "garabd.ttf"; - else if (_style & STTF_ITALIC) - newFontName = "garai.ttf"; - else - newFontName = "gara.ttf"; - - } else if (fontName.matchString("*arial*", true) || fontName.matchString("*ZorkNormal*", true)) { - if ((_style & (STTF_BOLD | STTF_ITALIC)) == (STTF_BOLD | STTF_ITALIC)) - newFontName = "arialbi.ttf"; - else if (_style & STTF_BOLD) - newFontName = "arialbd.ttf"; - else if (_style & STTF_ITALIC) - newFontName = "ariali.ttf"; - else - newFontName = "arial.ttf"; - - } else { - debug("Could not identify font: %s. Reverting to Arial", fontName.c_str()); - newFontName = "arial.ttf"; - } - - bool sharp = (_style & STTF_SHARP) == STTF_SHARP; - - Common::File *file = _engine->getSearchManager()->openFile(newFontName); - - if (!file) { - Common::SeekableReadStream *themeFile = nullptr; - if (ConfMan.hasKey("themepath")) { - Common::FSNode themePath(ConfMan.get("themepath")); - if (themePath.exists()) { - Common::FSNode scummModern = themePath.getChild("scummmodern.zip"); - if (scummModern.exists()) { - themeFile = scummModern.createReadStream(); - } - } - } - if (!themeFile) { // Fallback : Search for ScummModern.zip in SearchMan. - themeFile = SearchMan.createReadStreamForMember("scummmodern.zip"); - } - if (themeFile) { - Common::Archive *themeArchive = Common::makeZipArchive(themeFile); - if (themeArchive->hasFile("FreeSans.ttf")) { - Common::SeekableReadStream *stream = nullptr; - stream = themeArchive->createReadStreamForMember("FreeSans.ttf"); - Graphics::Font *_newFont = Graphics::loadTTFFont(*stream, point, 60, (sharp ? Graphics::kTTFRenderModeMonochrome : Graphics::kTTFRenderModeNormal)); // 66 dpi for 640 x 480 on 14" display - if (_newFont) { - if (!_font) - delete _font; - _font = _newFont; - } - if (stream) - delete stream; - } - delete themeArchive; - themeArchive = nullptr; - } - } else { - Graphics::Font *_newFont = Graphics::loadTTFFont(*file, point, 60, (sharp ? Graphics::kTTFRenderModeMonochrome : Graphics::kTTFRenderModeNormal)); // 66 dpi for 640 x 480 on 14" display - if (_newFont) { - if (!_font) - delete _font; - _font = _newFont; - } - delete file; - } - - _fntName = fontName; - _lineHeight = point; - - if (_font) - return true; - return false; -} - -void StyledTTFont::setStyle(uint newStyle) { - if ((_style & (STTF_BOLD | STTF_ITALIC | STTF_SHARP)) != (newStyle & (STTF_BOLD | STTF_ITALIC | STTF_SHARP))) { - _style = newStyle; - loadFont(_fntName, _lineHeight); - } else { - _style = newStyle; - } -} - -int StyledTTFont::getFontHeight() { - if (_font) - return _font->getFontHeight(); - return 0; -} - -int StyledTTFont::getMaxCharWidth() { - if (_font) - return _font->getMaxCharWidth(); - return 0; -} - -int StyledTTFont::getCharWidth(byte chr) { - if (_font) - return _font->getCharWidth(chr); - return 0; -} - -int StyledTTFont::getKerningOffset(byte left, byte right) { - if (_font) - return _font->getKerningOffset(left, right); - return 0; -} - -void StyledTTFont::drawChar(Graphics::Surface *dst, byte chr, int x, int y, uint32 color) { - if (_font) { - _font->drawChar(dst, chr, x, y, color); - if (_style & STTF_UNDERLINE) { - int16 pos = floor(_font->getFontHeight() * 0.87); - int thk = MAX((int)(_font->getFontHeight() * 0.05), 1); - dst->fillRect(Common::Rect(x, y + pos, x + _font->getCharWidth(chr), y + pos + thk), color); - } - if (_style & STTF_STRIKEOUT) { - int16 pos = floor(_font->getFontHeight() * 0.60); - int thk = MAX((int)(_font->getFontHeight() * 0.05), 1); - dst->fillRect(Common::Rect(x, y + pos, x + _font->getCharWidth(chr), y + pos + thk), color); - } - } -} - -void StyledTTFont::drawString(Graphics::Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, Graphics::TextAlign align) { - if (_font) { - _font->drawString(dst, str, x, y, w, color, align); - if (_style & STTF_UNDERLINE) { - int16 pos = floor(_font->getFontHeight() * 0.87); - int16 wd = MIN(_font->getStringWidth(str), w); - int16 stX = x; - if (align == Graphics::kTextAlignCenter) - stX += (w - wd) / 2; - else if (align == Graphics::kTextAlignRight) - stX += (w - wd); - - int thk = MAX((int)(_font->getFontHeight() * 0.05), 1); - - dst->fillRect(Common::Rect(stX, y + pos, stX + wd, y + pos + thk), color); - } - if (_style & STTF_STRIKEOUT) { - int16 pos = floor(_font->getFontHeight() * 0.60); - int16 wd = MIN(_font->getStringWidth(str), w); - int16 stX = x; - if (align == Graphics::kTextAlignCenter) - stX += (w - wd) / 2; - else if (align == Graphics::kTextAlignRight) - stX += (w - wd); - - int thk = MAX((int)(_font->getFontHeight() * 0.05), 1); - - dst->fillRect(Common::Rect(stX, y + pos, stX + wd, y + pos + thk), color); - } - } -} - -int StyledTTFont::getStringWidth(const Common::String &str) { - if (_font) - return _font->getStringWidth(str); - return 0; -} - -Graphics::Surface *StyledTTFont::renderSolidText(const Common::String &str, uint32 color) { - Graphics::Surface *tmp = new Graphics::Surface; - if (_font) { - int16 w = _font->getStringWidth(str); - if (w && w < 1024) { - tmp->create(w, _font->getFontHeight(), _engine->_pixelFormat); - drawString(tmp, str, 0, 0, w, color); - } - } - return tmp; -} - -} // End of namespace ZVision diff --git a/engines/zvision/fonts/truetype_font.h b/engines/zvision/fonts/truetype_font.h deleted file mode 100644 index 30ef1c73a3..0000000000 --- a/engines/zvision/fonts/truetype_font.h +++ /dev/null @@ -1,123 +0,0 @@ -/* 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. - * - */ - -// This file is based on engines/wintermute/base/fonts/base_font_truetype.h/.cpp - -#ifndef ZVISION_TRUETYPE_FONT_H -#define ZVISION_TRUETYPE_FONT_H - -#include "graphics/font.h" -#include "graphics/pixelformat.h" - -namespace Graphics { -struct Surface; -} - -namespace ZVision { - -class ZVision; - -class TruetypeFont { -public: - TruetypeFont(ZVision *engine, int32 fontHeight); - ~TruetypeFont(); - -private: - ZVision *_engine; - Graphics::Font *_font; - int _lineHeight; - - size_t _maxCharWidth; - size_t _maxCharHeight; - -public: - int32 _fontHeight; - -public: - /** - * Loads a .ttf file into memory. This must be called - * before any calls to drawTextToSurface - * - * @param filename The file name of the .ttf file to load - */ - bool loadFile(const Common::String &filename); - /** - * Renders the supplied text to a Surface using 0x0 as the - * background color. - * - * @param text The to render - * @param textColor The color to render the text with - * @param maxWidth The max width the text should take up. - * @param maxHeight The max height the text should take up. - * @param align The alignment of the text within the bounds of maxWidth - * @param wrap If true, any words extending past maxWidth will wrap to a new line. If false, ellipses will be rendered to show that the text didn't fit - * @return A Surface containing the rendered text - */ - Graphics::Surface *drawTextToSurface(const Common::String &text, uint16 textColor, int maxWidth, int maxHeight, Graphics::TextAlign align, bool wrap); -}; - -// Styled TTF -class StyledTTFont { -public: - StyledTTFont(ZVision *engine); - ~StyledTTFont(); - - enum { - STTF_BOLD = 1, - STTF_ITALIC = 2, - STTF_UNDERLINE = 4, - STTF_STRIKEOUT = 8, - STTF_SHARP = 16 - }; - -private: - ZVision *_engine; - Graphics::Font *_font; - int _lineHeight; - uint _style; - Common::String _fntName; - -public: - bool loadFont(const Common::String &fontName, int32 point); - bool loadFont(const Common::String &fontName, int32 point, uint style); - void setStyle(uint newStyle); - - int getFontHeight(); - int getMaxCharWidth(); - int getCharWidth(byte chr); - int getKerningOffset(byte left, byte right); - - void drawChar(Graphics::Surface *dst, byte chr, int x, int y, uint32 color); - - void drawString(Graphics::Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, Graphics::TextAlign align = Graphics::kTextAlignLeft); - int getStringWidth(const Common::String &str); - - Graphics::Surface *renderSolidText(const Common::String &str, uint32 color); - - bool isLoaded() { - return _font != NULL; - }; -}; - -} // End of namespace ZVision - -#endif diff --git a/engines/zvision/graphics/render_manager.h b/engines/zvision/graphics/render_manager.h index 39809c65a4..879a8643ce 100644 --- a/engines/zvision/graphics/render_manager.h +++ b/engines/zvision/graphics/render_manager.h @@ -24,7 +24,7 @@ #define ZVISION_RENDER_MANAGER_H #include "zvision/graphics/render_table.h" -#include "zvision/fonts/truetype_font.h" +#include "zvision/graphics/truetype_font.h" #include "common/rect.h" #include "common/hashmap.h" diff --git a/engines/zvision/graphics/truetype_font.cpp b/engines/zvision/graphics/truetype_font.cpp new file mode 100644 index 0000000000..1a0e92087c --- /dev/null +++ b/engines/zvision/graphics/truetype_font.cpp @@ -0,0 +1,341 @@ +/* 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 "common/config-manager.h" +#include "common/debug.h" +#include "common/file.h" +#include "common/system.h" +#include "common/unzip.h" +#include "graphics/font.h" +#include "graphics/fonts/ttf.h" +#include "graphics/surface.h" + +#include "zvision/zvision.h" +#include "zvision/graphics/render_manager.h" +#include "zvision/graphics/truetype_font.h" + +namespace ZVision { + +TruetypeFont::TruetypeFont(ZVision *engine, int32 fontHeight) + : _engine(engine), + _fontHeight(fontHeight), + _font(0), + _lineHeight(0), + _maxCharWidth(0), + _maxCharHeight(0) { +} + +TruetypeFont::~TruetypeFont(void) { + delete _font; +} + +bool TruetypeFont::loadFile(const Common::String &filename) { + Common::File file; + + bool fileOpened = false; + if (!Common::File::exists(filename)) { + debug("TTF font file %s was not found. Reverting to arial.ttf", filename.c_str()); + fileOpened = file.open("arial.ttf"); + } else { + fileOpened = file.open(filename); + } + + if (!fileOpened) { + debug("TTF file could not be opened"); + return false; + } + + _font = Graphics::loadTTFFont(file, _fontHeight); + _lineHeight = _font->getFontHeight(); + + return true; +} + +Graphics::Surface *TruetypeFont::drawTextToSurface(const Common::String &text, uint16 textColor, int maxWidth, int maxHeight, Graphics::TextAlign align, bool wrap) { + if (text.equals("")) { + return nullptr; + } + + Graphics::Surface *surface = new Graphics::Surface(); + + if (!wrap) { + int width = MIN(_font->getStringWidth(text), maxWidth); + surface->create(width, _lineHeight, Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0)); + // TODO: Add better alpha support by getting the pixels from the backbuffer. + // However doing that requires some kind of caching system so future text doesn't try to use this text as it's alpha background. + surface->fillRect(Common::Rect(0, 0, surface->w, surface->h), 0); + + _font->drawString(surface, text, 0, 0, maxWidth, textColor, align); + return surface; + } + + Common::Array lines; + _font->wordWrapText(text, maxWidth, lines); + + while (maxHeight > 0 && (int)lines.size() * _lineHeight > maxHeight) { + lines.pop_back(); + } + if (lines.size() == 0) { + delete surface; + return nullptr; + } + + surface->create(maxWidth, lines.size() * _lineHeight, Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0)); + surface->fillRect(Common::Rect(0, 0, surface->w, surface->h), 0); + + int heightOffset = 0; + for (Common::Array::iterator it = lines.begin(); it != lines.end(); it++) { + _font->drawString(surface, *it, 0, 0 + heightOffset, maxWidth, textColor, align); + heightOffset += _lineHeight; + } + + return surface; +} + +StyledTTFont::StyledTTFont(ZVision *engine) { + _engine = engine; + _style = 0; + _font = NULL; + _lineHeight = 0; +} + +StyledTTFont::~StyledTTFont() { + if (_font) + delete _font; +} + +bool StyledTTFont::loadFont(const Common::String &fontName, int32 point, uint style) { + _style = style; + return loadFont(fontName, point); +} + +bool StyledTTFont::loadFont(const Common::String &fontName, int32 point) { + Common::String newFontName; + if (fontName.matchString("*times new roman*", true) || fontName.matchString("*times*", true)) { + if ((_style & (STTF_BOLD | STTF_ITALIC)) == (STTF_BOLD | STTF_ITALIC)) + newFontName = "timesbi.ttf"; + else if (_style & STTF_BOLD) + newFontName = "timesbd.ttf"; + else if (_style & STTF_ITALIC) + newFontName = "timesi.ttf"; + else + newFontName = "times.ttf"; + + } else if (fontName.matchString("*courier new*", true) || fontName.matchString("*courier*", true) || fontName.matchString("*ZorkDeath*", true)) { + if ((_style & (STTF_BOLD | STTF_ITALIC)) == (STTF_BOLD | STTF_ITALIC)) + newFontName = "courbi.ttf"; + else if (_style & STTF_BOLD) + newFontName = "courbd.ttf"; + else if (_style & STTF_ITALIC) + newFontName = "couri.ttf"; + else + newFontName = "cour.ttf"; + + } else if (fontName.matchString("*century schoolbook*", true)) { + if ((_style & (STTF_BOLD | STTF_ITALIC)) == (STTF_BOLD | STTF_ITALIC)) + newFontName = "censcbkbi.ttf"; + else if (_style & STTF_BOLD) + newFontName = "censcbkbd.ttf"; + else if (_style & STTF_ITALIC) + newFontName = "censcbki.ttf"; + else + newFontName = "censcbk.ttf"; + + } else if (fontName.matchString("*garamond*", true)) { + if ((_style & (STTF_BOLD | STTF_ITALIC)) == (STTF_BOLD | STTF_ITALIC)) + newFontName = "garabi.ttf"; + else if (_style & STTF_BOLD) + newFontName = "garabd.ttf"; + else if (_style & STTF_ITALIC) + newFontName = "garai.ttf"; + else + newFontName = "gara.ttf"; + + } else if (fontName.matchString("*arial*", true) || fontName.matchString("*ZorkNormal*", true)) { + if ((_style & (STTF_BOLD | STTF_ITALIC)) == (STTF_BOLD | STTF_ITALIC)) + newFontName = "arialbi.ttf"; + else if (_style & STTF_BOLD) + newFontName = "arialbd.ttf"; + else if (_style & STTF_ITALIC) + newFontName = "ariali.ttf"; + else + newFontName = "arial.ttf"; + + } else { + debug("Could not identify font: %s. Reverting to Arial", fontName.c_str()); + newFontName = "arial.ttf"; + } + + bool sharp = (_style & STTF_SHARP) == STTF_SHARP; + + Common::File *file = _engine->getSearchManager()->openFile(newFontName); + + if (!file) { + Common::SeekableReadStream *themeFile = nullptr; + if (ConfMan.hasKey("themepath")) { + Common::FSNode themePath(ConfMan.get("themepath")); + if (themePath.exists()) { + Common::FSNode scummModern = themePath.getChild("scummmodern.zip"); + if (scummModern.exists()) { + themeFile = scummModern.createReadStream(); + } + } + } + if (!themeFile) { // Fallback : Search for ScummModern.zip in SearchMan. + themeFile = SearchMan.createReadStreamForMember("scummmodern.zip"); + } + if (themeFile) { + Common::Archive *themeArchive = Common::makeZipArchive(themeFile); + if (themeArchive->hasFile("FreeSans.ttf")) { + Common::SeekableReadStream *stream = nullptr; + stream = themeArchive->createReadStreamForMember("FreeSans.ttf"); + Graphics::Font *_newFont = Graphics::loadTTFFont(*stream, point, 60, (sharp ? Graphics::kTTFRenderModeMonochrome : Graphics::kTTFRenderModeNormal)); // 66 dpi for 640 x 480 on 14" display + if (_newFont) { + if (!_font) + delete _font; + _font = _newFont; + } + if (stream) + delete stream; + } + delete themeArchive; + themeArchive = nullptr; + } + } else { + Graphics::Font *_newFont = Graphics::loadTTFFont(*file, point, 60, (sharp ? Graphics::kTTFRenderModeMonochrome : Graphics::kTTFRenderModeNormal)); // 66 dpi for 640 x 480 on 14" display + if (_newFont) { + if (!_font) + delete _font; + _font = _newFont; + } + delete file; + } + + _fntName = fontName; + _lineHeight = point; + + if (_font) + return true; + return false; +} + +void StyledTTFont::setStyle(uint newStyle) { + if ((_style & (STTF_BOLD | STTF_ITALIC | STTF_SHARP)) != (newStyle & (STTF_BOLD | STTF_ITALIC | STTF_SHARP))) { + _style = newStyle; + loadFont(_fntName, _lineHeight); + } else { + _style = newStyle; + } +} + +int StyledTTFont::getFontHeight() { + if (_font) + return _font->getFontHeight(); + return 0; +} + +int StyledTTFont::getMaxCharWidth() { + if (_font) + return _font->getMaxCharWidth(); + return 0; +} + +int StyledTTFont::getCharWidth(byte chr) { + if (_font) + return _font->getCharWidth(chr); + return 0; +} + +int StyledTTFont::getKerningOffset(byte left, byte right) { + if (_font) + return _font->getKerningOffset(left, right); + return 0; +} + +void StyledTTFont::drawChar(Graphics::Surface *dst, byte chr, int x, int y, uint32 color) { + if (_font) { + _font->drawChar(dst, chr, x, y, color); + if (_style & STTF_UNDERLINE) { + int16 pos = floor(_font->getFontHeight() * 0.87); + int thk = MAX((int)(_font->getFontHeight() * 0.05), 1); + dst->fillRect(Common::Rect(x, y + pos, x + _font->getCharWidth(chr), y + pos + thk), color); + } + if (_style & STTF_STRIKEOUT) { + int16 pos = floor(_font->getFontHeight() * 0.60); + int thk = MAX((int)(_font->getFontHeight() * 0.05), 1); + dst->fillRect(Common::Rect(x, y + pos, x + _font->getCharWidth(chr), y + pos + thk), color); + } + } +} + +void StyledTTFont::drawString(Graphics::Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, Graphics::TextAlign align) { + if (_font) { + _font->drawString(dst, str, x, y, w, color, align); + if (_style & STTF_UNDERLINE) { + int16 pos = floor(_font->getFontHeight() * 0.87); + int16 wd = MIN(_font->getStringWidth(str), w); + int16 stX = x; + if (align == Graphics::kTextAlignCenter) + stX += (w - wd) / 2; + else if (align == Graphics::kTextAlignRight) + stX += (w - wd); + + int thk = MAX((int)(_font->getFontHeight() * 0.05), 1); + + dst->fillRect(Common::Rect(stX, y + pos, stX + wd, y + pos + thk), color); + } + if (_style & STTF_STRIKEOUT) { + int16 pos = floor(_font->getFontHeight() * 0.60); + int16 wd = MIN(_font->getStringWidth(str), w); + int16 stX = x; + if (align == Graphics::kTextAlignCenter) + stX += (w - wd) / 2; + else if (align == Graphics::kTextAlignRight) + stX += (w - wd); + + int thk = MAX((int)(_font->getFontHeight() * 0.05), 1); + + dst->fillRect(Common::Rect(stX, y + pos, stX + wd, y + pos + thk), color); + } + } +} + +int StyledTTFont::getStringWidth(const Common::String &str) { + if (_font) + return _font->getStringWidth(str); + return 0; +} + +Graphics::Surface *StyledTTFont::renderSolidText(const Common::String &str, uint32 color) { + Graphics::Surface *tmp = new Graphics::Surface; + if (_font) { + int16 w = _font->getStringWidth(str); + if (w && w < 1024) { + tmp->create(w, _font->getFontHeight(), _engine->_pixelFormat); + drawString(tmp, str, 0, 0, w, color); + } + } + return tmp; +} + +} // End of namespace ZVision diff --git a/engines/zvision/graphics/truetype_font.h b/engines/zvision/graphics/truetype_font.h new file mode 100644 index 0000000000..30ef1c73a3 --- /dev/null +++ b/engines/zvision/graphics/truetype_font.h @@ -0,0 +1,123 @@ +/* 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. + * + */ + +// This file is based on engines/wintermute/base/fonts/base_font_truetype.h/.cpp + +#ifndef ZVISION_TRUETYPE_FONT_H +#define ZVISION_TRUETYPE_FONT_H + +#include "graphics/font.h" +#include "graphics/pixelformat.h" + +namespace Graphics { +struct Surface; +} + +namespace ZVision { + +class ZVision; + +class TruetypeFont { +public: + TruetypeFont(ZVision *engine, int32 fontHeight); + ~TruetypeFont(); + +private: + ZVision *_engine; + Graphics::Font *_font; + int _lineHeight; + + size_t _maxCharWidth; + size_t _maxCharHeight; + +public: + int32 _fontHeight; + +public: + /** + * Loads a .ttf file into memory. This must be called + * before any calls to drawTextToSurface + * + * @param filename The file name of the .ttf file to load + */ + bool loadFile(const Common::String &filename); + /** + * Renders the supplied text to a Surface using 0x0 as the + * background color. + * + * @param text The to render + * @param textColor The color to render the text with + * @param maxWidth The max width the text should take up. + * @param maxHeight The max height the text should take up. + * @param align The alignment of the text within the bounds of maxWidth + * @param wrap If true, any words extending past maxWidth will wrap to a new line. If false, ellipses will be rendered to show that the text didn't fit + * @return A Surface containing the rendered text + */ + Graphics::Surface *drawTextToSurface(const Common::String &text, uint16 textColor, int maxWidth, int maxHeight, Graphics::TextAlign align, bool wrap); +}; + +// Styled TTF +class StyledTTFont { +public: + StyledTTFont(ZVision *engine); + ~StyledTTFont(); + + enum { + STTF_BOLD = 1, + STTF_ITALIC = 2, + STTF_UNDERLINE = 4, + STTF_STRIKEOUT = 8, + STTF_SHARP = 16 + }; + +private: + ZVision *_engine; + Graphics::Font *_font; + int _lineHeight; + uint _style; + Common::String _fntName; + +public: + bool loadFont(const Common::String &fontName, int32 point); + bool loadFont(const Common::String &fontName, int32 point, uint style); + void setStyle(uint newStyle); + + int getFontHeight(); + int getMaxCharWidth(); + int getCharWidth(byte chr); + int getKerningOffset(byte left, byte right); + + void drawChar(Graphics::Surface *dst, byte chr, int x, int y, uint32 color); + + void drawString(Graphics::Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, Graphics::TextAlign align = Graphics::kTextAlignLeft); + int getStringWidth(const Common::String &str); + + Graphics::Surface *renderSolidText(const Common::String &str, uint32 color); + + bool isLoaded() { + return _font != NULL; + }; +}; + +} // End of namespace ZVision + +#endif diff --git a/engines/zvision/module.mk b/engines/zvision/module.mk index 607a9be4ed..e2beeb593d 100644 --- a/engines/zvision/module.mk +++ b/engines/zvision/module.mk @@ -12,12 +12,12 @@ MODULE_OBJS := \ cursors/cursor_manager.o \ cursors/cursor.o \ detection.o \ - fonts/truetype_font.o \ graphics/effects/fog.o \ graphics/effects/light.o \ graphics/effects/wave.o \ graphics/render_manager.o \ graphics/render_table.o \ + graphics/truetype_font.o \ scripting/actions.o \ scripting/control.o \ scripting/controls/fist_control.o \ diff --git a/engines/zvision/scripting/sidefx/ttytext_node.h b/engines/zvision/scripting/sidefx/ttytext_node.h index a229129b9d..b6cbed3e34 100644 --- a/engines/zvision/scripting/sidefx/ttytext_node.h +++ b/engines/zvision/scripting/sidefx/ttytext_node.h @@ -28,7 +28,7 @@ #include "zvision/scripting/sidefx.h" #include "zvision/text/text.h" -#include "zvision/fonts/truetype_font.h" +#include "zvision/graphics/truetype_font.h" namespace Common { class String; diff --git a/engines/zvision/text/string_manager.cpp b/engines/zvision/text/string_manager.cpp index f88f9eaddd..1a04c67988 100644 --- a/engines/zvision/text/string_manager.cpp +++ b/engines/zvision/text/string_manager.cpp @@ -21,20 +21,17 @@ */ #include "common/scummsys.h" - -#include "zvision/zvision.h" -#include "zvision/core/search_manager.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" +#include "zvision/zvision.h" +#include "zvision/core/search_manager.h" +#include "zvision/text/string_manager.h" +#include "zvision/graphics/truetype_font.h" + namespace ZVision { StringManager::StringManager(ZVision *engine) diff --git a/engines/zvision/text/string_manager.h b/engines/zvision/text/string_manager.h index 61c0d95f51..8d6fbe67a6 100644 --- a/engines/zvision/text/string_manager.h +++ b/engines/zvision/text/string_manager.h @@ -24,7 +24,7 @@ #define ZVISION_STRING_MANAGER_H #include "zvision/detection.h" -#include "zvision/fonts/truetype_font.h" +#include "zvision/graphics/truetype_font.h" namespace Graphics { class FontManager; diff --git a/engines/zvision/text/text.cpp b/engines/zvision/text/text.cpp index 6f41416015..0ccca214d7 100644 --- a/engines/zvision/text/text.cpp +++ b/engines/zvision/text/text.cpp @@ -21,23 +21,19 @@ */ #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/text/text.h" #include "zvision/graphics/render_manager.h" +#include "zvision/graphics/truetype_font.h" #include "zvision/scripting/script_manager.h" namespace ZVision { diff --git a/engines/zvision/text/text.h b/engines/zvision/text/text.h index 6b916860f0..01c3fd760c 100644 --- a/engines/zvision/text/text.h +++ b/engines/zvision/text/text.h @@ -25,7 +25,7 @@ #define ZVISION_TEXT_H #include "zvision/detection.h" -#include "zvision/fonts/truetype_font.h" +#include "zvision/graphics/truetype_font.h" #include "zvision/zvision.h" namespace Graphics { diff --git a/engines/zvision/zvision.cpp b/engines/zvision/zvision.cpp index d981d14838..af9d26a350 100644 --- a/engines/zvision/zvision.cpp +++ b/engines/zvision/zvision.cpp @@ -33,7 +33,7 @@ #include "zvision/core/menu.h" #include "zvision/core/search_manager.h" #include "zvision/text/text.h" -#include "zvision/fonts/truetype_font.h" +#include "zvision/graphics/truetype_font.h" #include "zvision/core/midi.h" #include "zvision/utility/zfs_archive.h" -- cgit v1.2.3