diff options
author | Marisa-Chan | 2014-02-24 20:44:22 +0700 |
---|---|---|
committer | Marisa-Chan | 2014-02-24 20:44:22 +0700 |
commit | dcfe35c8ac1cd2c9139f5d7a59f0f554e3c5082b (patch) | |
tree | 683322b79579f729baff00950ed1e2a771e27c3c /engines/zvision | |
parent | 226ccb0de7ed702d4b41625c8a2211fac9cc0787 (diff) | |
download | scummvm-rg350-dcfe35c8ac1cd2c9139f5d7a59f0f554e3c5082b.tar.gz scummvm-rg350-dcfe35c8ac1cd2c9139f5d7a59f0f554e3c5082b.tar.bz2 scummvm-rg350-dcfe35c8ac1cd2c9139f5d7a59f0f554e3c5082b.zip |
ZVISION: Implement sTTFont class for handle ttf with styles.
Diffstat (limited to 'engines/zvision')
-rw-r--r-- | engines/zvision/truetype_font.cpp | 226 | ||||
-rw-r--r-- | engines/zvision/truetype_font.h | 42 |
2 files changed, 268 insertions, 0 deletions
diff --git a/engines/zvision/truetype_font.cpp b/engines/zvision/truetype_font.cpp index dde3b2dee5..5e1bc7a81d 100644 --- a/engines/zvision/truetype_font.cpp +++ b/engines/zvision/truetype_font.cpp @@ -27,9 +27,11 @@ #include "zvision/zvision.h" #include "zvision/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" @@ -113,4 +115,228 @@ Graphics::Surface *TruetypeFont::drawTextToSurface(const Common::String &text, u return surface; } +sTTFont::sTTFont(ZVision *engine) { + _engine = engine; + _style = 0; + _font = NULL; + _lineHeight = 0; +} + +sTTFont::~sTTFont() { + if (_font) + delete _font; +} + +bool sTTFont::loadFont(const Common::String &fontName, int32 point, uint style) { + _style = style; + return loadFont(fontName, point); +} + +bool sTTFont::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"; + } + + 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 2.5: 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); // 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); // 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 sTTFont::setStyle(uint newStyle) { + if ((_style & (STTF_BOLD | STTF_ITALIC)) != (newStyle & (STTF_BOLD | STTF_ITALIC))) { + _style = newStyle; + loadFont(_fntName, _lineHeight); + } else { + _style = newStyle; + } +} + +int sTTFont::getFontHeight() { + if (_font) + return _font->getFontHeight(); + return 0; +} + +int sTTFont::getMaxCharWidth() { + if (_font) + return _font->getMaxCharWidth(); + return 0; +} + +int sTTFont::getCharWidth(byte chr) { + if (_font) + return _font->getCharWidth(chr); + return 0; +} + +int sTTFont::getKerningOffset(byte left, byte right) { + if (_font) + return _font->getKerningOffset(left, right); + return 0; +} + +void sTTFont::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 sTTFont::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 sTTFont::getStringWidth(const Common::String &str) { + if (_font) + return _font->getStringWidth(str); + return 0; +} + +Graphics::Surface *sTTFont::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/truetype_font.h b/engines/zvision/truetype_font.h index 762657a5fb..0462f35aff 100644 --- a/engines/zvision/truetype_font.h +++ b/engines/zvision/truetype_font.h @@ -76,6 +76,48 @@ public: Graphics::Surface *drawTextToSurface(const Common::String &text, uint16 textColor, int maxWidth, int maxHeight, Graphics::TextAlign align, bool wrap); }; +// Styled TTF +class sTTFont { +public: + sTTFont(ZVision *engine); + ~sTTFont(); + + enum { + STTF_BOLD = 1, + STTF_ITALIC = 2, + STTF_UNDERLINE = 4, + STTF_STRIKEOUT = 8 + }; + +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 |