From fb7dbffd59b797ed00785a2d3762355f9dbb9f7d Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Sun, 25 Nov 2018 11:42:00 -0800 Subject: GLK: Merge Fonts class into Screen class --- engines/glk/conf.cpp | 7 +- engines/glk/conf.h | 3 - engines/glk/fonts.cpp | 170 -------------------------------------- engines/glk/fonts.h | 118 -------------------------- engines/glk/frotz/bitmap_font.cpp | 60 -------------- engines/glk/frotz/bitmap_font.h | 81 ------------------ engines/glk/frotz/screen.cpp | 60 ++++++++++++++ engines/glk/frotz/screen.h | 81 ++++++++++++++++++ engines/glk/module.mk | 3 +- engines/glk/screen.cpp | 129 +++++++++++++++++++++++++++++ engines/glk/screen.h | 76 ++++++++++++++++- engines/glk/windows.h | 2 +- 12 files changed, 348 insertions(+), 442 deletions(-) delete mode 100644 engines/glk/fonts.cpp delete mode 100644 engines/glk/fonts.h delete mode 100644 engines/glk/frotz/bitmap_font.cpp delete mode 100644 engines/glk/frotz/bitmap_font.h create mode 100644 engines/glk/frotz/screen.cpp create mode 100644 engines/glk/frotz/screen.h diff --git a/engines/glk/conf.cpp b/engines/glk/conf.cpp index 4775f26621..ee2fd9bc21 100644 --- a/engines/glk/conf.cpp +++ b/engines/glk/conf.cpp @@ -21,7 +21,6 @@ */ #include "glk/conf.h" -#include "glk/fonts.h" #include "glk/utils.h" #include "glk/windows.h" #include "common/config-manager.h" @@ -197,9 +196,9 @@ Conf::Conf(InterpreterType interpType) { char *font = strtok(buffer, "\r\n\t "); if (tg == 0) - _tStyles[style].font = Fonts::getId(font); + _tStyles[style].font = Screen::getFontId(font); else - _gStyles[style].font = Fonts::getId(font); + _gStyles[style].font = Screen::getFontId(font); } } @@ -231,7 +230,7 @@ void Conf::get(const Common::String &key, bool &field, bool defaultVal) { } void Conf::get(const Common::String &key, FACES &field, FACES defaultFont) { - field = ConfMan.hasKey(key) ? Fonts::getId(ConfMan.get(key)) : defaultFont; + field = ConfMan.hasKey(key) ? Screen::getFontId(ConfMan.get(key)) : defaultFont; } void Conf::get(const Common::String &key, double &field, double defaultVal) { diff --git a/engines/glk/conf.h b/engines/glk/conf.h index bf68e343f9..ca9de2ed08 100644 --- a/engines/glk/conf.h +++ b/engines/glk/conf.h @@ -24,13 +24,10 @@ #define GLK_CONF_H #include "glk/glk_types.h" -#include "glk/fonts.h" #include "glk/windows.h" namespace Glk { - - /** * Engine configuration */ diff --git a/engines/glk/fonts.cpp b/engines/glk/fonts.cpp deleted file mode 100644 index 416496670c..0000000000 --- a/engines/glk/fonts.cpp +++ /dev/null @@ -1,170 +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 "glk/fonts.h" -#include "glk/glk_types.h" -#include "glk/conf.h" -#include "glk/glk.h" -#include "glk/frotz/bitmap_font.h" -#include "common/memstream.h" -#include "common/unzip.h" -#include "graphics/fonts/ttf.h" -#include "graphics/fontman.h" -#include "image/bmp.h" - -namespace Glk { - -#define FONTS_VERSION 1.0 -#define FONTS_FILENAME "fonts.dat" - -Fonts::Fonts(Graphics::ManagedSurface *surface) : _surface(surface), _fontsMissing(false) { - if (!loadFonts()) - error("Could not load data file"); - - // TODO: See if there's any better way for getting the leading and baseline - Common::Rect r1 = _fontTable[7]->getBoundingBox('o'); - Common::Rect r2 = _fontTable[7]->getBoundingBox('y'); - double baseLine = (double)r1.bottom; - double leading = (double)r2.bottom + 2; - - g_conf->_leading = MAX((double)g_conf->_leading, leading); - g_conf->_baseLine = MAX((double)g_conf->_baseLine, baseLine); - g_conf->_cellW = _fontTable[0]->getStringWidth("0"); - g_conf->_cellH = g_conf->_leading; -} - -Fonts::~Fonts() { - for (int idx = 0; idx < FONTS_TOTAL; ++idx) - delete _fontTable[idx]; -} - -bool Fonts::loadFonts() { - Common::Archive *archive = nullptr; - - if (!Common::File::exists(FONTS_FILENAME) || (archive = Common::makeZipArchive(FONTS_FILENAME)) == nullptr) - return false; - - // Open the version.txt file within it to validate the version - Common::File f; - if (!f.open("version.txt", *archive)) { - delete archive; - return false; - } - - // Validate the version - char buffer[4]; - f.read(buffer, 3); - buffer[3] = '\0'; - - if (Common::String(buffer) != "1.0") { - delete archive; - return false; - } - - // R ead in the fonts - double monoAspect = g_conf->_monoAspect; - double propAspect = g_conf->_propAspect; - double monoSize = g_conf->_monoSize; - double propSize = g_conf->_propSize; - - _fontTable[0] = loadFont(MONOR, archive, monoSize, monoAspect, FONTR); - _fontTable[1] = loadFont(MONOB, archive, monoSize, monoAspect, FONTB); - _fontTable[2] = loadFont(MONOI, archive, monoSize, monoAspect, FONTI); - _fontTable[3] = loadFont(MONOZ, archive, monoSize, monoAspect, FONTZ); - - _fontTable[4] = loadFont(PROPR, archive, propSize, propAspect, FONTR); - _fontTable[5] = loadFont(PROPB, archive, propSize, propAspect, FONTB); - _fontTable[6] = loadFont(PROPI, archive, propSize, propAspect, FONTI); - _fontTable[7] = loadFont(PROPZ, archive, propSize, propAspect, FONTZ); - - delete archive; - return true; -} - -const Graphics::Font *Fonts::loadFont(FACES face, Common::Archive *archive, double size, double aspect, int - style) { - Common::File f; - const char *const FILENAMES[8] = { - "GoMono-Regular.ttf", "GoMono-Bold.ttf", "GoMono-Italic.ttf", "GoMono-Bold-Italic.ttf", - "NotoSerif-Regular.ttf", "NotoSerif-Bold.ttf", "NotoSerif-Italic.ttf", "NotoSerif-Bold-Italic.ttf" - }; - - // TODO: Properly create a derived Fonts manager for the Frotz sub-engine - if (face == MONOZ && g_vm->getInterpreterType() == INTERPRETER_FROTZ) { - if (!f.open("infocom_graphics.bmp", *archive)) - error("Could not load font"); - - Image::BitmapDecoder decoder; - decoder.loadStream(f); - return new Frotz::BitmapFont(*decoder.getSurface()); - - } else { - if (!f.open(FILENAMES[face], *archive)) - error("Could not load font"); - - return Graphics::loadTTFFont(f, size, Graphics::kTTFSizeModeCharacter); - } -} - -FACES Fonts::getId(const Common::String &name) { - if (name == "monor") return MONOR; - if (name == "monob") return MONOB; - if (name == "monoi") return MONOI; - if (name == "monoz") return MONOZ; - if (name == "propr") return PROPR; - if (name == "propb") return PROPB; - if (name == "propi") return PROPI; - if (name == "propz") return PROPZ; - return MONOR; -} - -int Fonts::drawString(const Point &pos, int fontIdx, const byte *rgb, const Common::String &text, int spw) { - Point pt(pos.x / GLI_SUBPIX, pos.y - g_conf->_baseLine); - const Graphics::Font *font = _fontTable[fontIdx]; - const uint32 color = _surface->format.RGBToColor(rgb[0], rgb[1], rgb[2]); - font->drawString(_surface, text, pt.x, pt.y, _surface->w - pt.x, color); - - pt.x += font->getStringWidth(text); - return MIN((int)pt.x, (int)_surface->w) * GLI_SUBPIX; -} - -int Fonts::drawStringUni(const Point &pos, int fontIdx, const byte *rgb, const Common::U32String &text, int spw) { - Point pt(pos.x / GLI_SUBPIX, pos.y - g_conf->_baseLine); - const Graphics::Font *font = _fontTable[fontIdx]; - const uint32 color = _surface->format.RGBToColor(rgb[0], rgb[1], rgb[2]); - font->drawString(_surface, text, pt.x, pt.y, _surface->w - pt.x, color); - - pt.x += font->getStringWidth(text); - return MIN((int)pt.x, (int)_surface->w) * GLI_SUBPIX; -} - -size_t Fonts::stringWidth(int fontIdx, const Common::String &text, int spw) { - const Graphics::Font *font = _fontTable[fontIdx]; - return font->getStringWidth(text) * GLI_SUBPIX; -} - -size_t Fonts::stringWidthUni(int fontIdx, const Common::U32String &text, int spw) { - const Graphics::Font *font = _fontTable[fontIdx]; - return font->getStringWidth(text) * GLI_SUBPIX; -} - -} // End of namespace Glk diff --git a/engines/glk/fonts.h b/engines/glk/fonts.h deleted file mode 100644 index 364b0239be..0000000000 --- a/engines/glk/fonts.h +++ /dev/null @@ -1,118 +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. - * - */ - -#ifndef GLK_FONTS_H -#define GLK_FONTS_H - -#include "glk/glk_types.h" -#include "glk/utils.h" -#include "common/archive.h" -#include "common/array.h" -#include "common/file.h" -#include "common/str.h" -#include "common/ustr.h" -#include "graphics/font.h" - -namespace Glk { - -#define FONTS_TOTAL 8 - -enum FACES { MONOR, MONOB, MONOI, MONOZ, PROPR, PROPB, PROPI, PROPZ }; -enum TYPES { MONOF, PROPF }; -enum STYLES { FONTR, FONTB, FONTI, FONTZ }; - -/** - * Fonts manager - */ -class Fonts { -private: - Graphics::ManagedSurface *_surface; - const Graphics::Font *_fontTable[FONTS_TOTAL]; - bool _fontsMissing; -private: - /** - * Load all the fonts - */ - bool loadFonts(); - - /** - * Load a single font - */ - const Graphics::Font *loadFont(FACES face, Common::Archive *archive, double size, double aspect, int style); -public: - /** - * Get the index/id of a font by name - */ - static FACES getId(const Common::String &name); -public: - /** - * Constructor - */ - Fonts(Graphics::ManagedSurface *surface); - - /** - * Destructor - */ - virtual ~Fonts(); - - /** - * Draws a string using the specified font at the given co-ordinates - * @param pos Position for the bottom-left corner the text will be drawn with - * @param fontIdx Which font to use - * @param rgb RGB tuplet specifying the text color - * @param text The text to draw - * @param spw ?? - */ - int drawString(const Point &pos, int fontIdx, const byte *rgb, const Common::String &text, int spw = 0); - - /** - * Draws a unicode string using the specified font at the given co-ordinates - * @param pos Position for the bottom-left corner the text will be drawn with - * @param fontIdx Which font to use - * @param rgb RGB tuplet specifying the text color - * @param text The text to draw - * @param spw ?? - */ - int drawStringUni(const Point &pos, int fontIdx, const byte *rgb, const Common::U32String &text, int spw = 0); - - /** - * Get the width in pixels of a string - * @param fontIdx Which font to use - * @param text Text to get the width of - * @param spw Delta X - * @returns Width of string multiplied by GLI_SUBPIX - */ - size_t stringWidth(int fontIdx, const Common::String &text, int spw = 0); - - /** - * Get the width in pixels of a unicode string - * @param fontIdx Which font to use - * @param text Text to get the width of - * @param spw Delta X - * @returns Width of string multiplied by GLI_SUBPIX - */ - size_t stringWidthUni(int fontIdx, const Common::U32String &text, int spw = 0); -}; - -} // End of namespace Glk - -#endif diff --git a/engines/glk/frotz/bitmap_font.cpp b/engines/glk/frotz/bitmap_font.cpp deleted file mode 100644 index 91f44a9c0c..0000000000 --- a/engines/glk/frotz/bitmap_font.cpp +++ /dev/null @@ -1,60 +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 "glk/frotz/bitmap_font.h" - -namespace Glk { -namespace Frotz { - -BitmapFont::BitmapFont(const Graphics::Surface &src, uint charWidth, - uint charHeight, unsigned char startingChar) : _startingChar(startingChar) { - assert(src.format.bytesPerPixel == 1); - assert((src.w % charWidth) == 0); - assert((src.h % charHeight) == 0); - _surface.copyFrom(src); - - Common::Rect r(charWidth, charHeight); - for (uint y = 0; y < src.h; y += charHeight) { - r.moveTo(0, y); - for (uint x = 0; x < src.w; x += charWidth, r.translate(charWidth, 0)) - _chars.push_back(r); - } -} - -BitmapFont::~BitmapFont() { - _surface.free(); -} - -void BitmapFont::drawChar(Graphics::Surface *dst, uint32 chr, int x, int y, uint32 color) const { - const Common::Rect &r = _chars[chr - _startingChar]; - for (int yCtr = 0; yCtr < r.height(); ++yCtr) { - const byte *srcP = (const byte *)_surface.getBasePtr(r.left, r.top + yCtr); - - for (int xCtr = 0; xCtr < r.width(); ++xCtr, ++srcP) { - if (*srcP) - dst->hLine(x + xCtr, y + yCtr, x + xCtr, color); - } - } -} - -} // End of namespace Scott -} // End of namespace Glk diff --git a/engines/glk/frotz/bitmap_font.h b/engines/glk/frotz/bitmap_font.h deleted file mode 100644 index dac716c436..0000000000 --- a/engines/glk/frotz/bitmap_font.h +++ /dev/null @@ -1,81 +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. - * - */ - -#ifndef GLK_FROTZ_FONTS -#define GLK_FROTZ_FONTS - -#include "graphics/font.h" -#include "graphics/surface.h" -#include "common/archive.h" -#include "common/array.h" -#include "common/rect.h" - -namespace Glk { -namespace Frotz { - -/** - * Implements a fixed width font stored as a grid on a passed surface - */ -class BitmapFont : public Graphics::Font { -private: - Graphics::Surface _surface; - Common::Array _chars; - size_t _startingChar; -public: - /** - * Constructor - */ - BitmapFont(const Graphics::Surface &src, uint charWidth = 8, uint charHeight = 8, - unsigned char startingChar = ' '); - - /** - * Destructor - */ - ~BitmapFont(); - - /** - * Get the font height - */ - virtual int getFontHeight() const override { return _chars[0].height(); } - - /** - * Get the maximum character width - */ - virtual int getMaxCharWidth() const override { return _chars[0].width(); } - - /** - * Get the width of the given character - */ - virtual int getCharWidth(uint32 chr) const override { - return _chars[chr - _startingChar].width(); - } - - /** - * Draw a character - */ - virtual void drawChar(Graphics::Surface *dst, uint32 chr, int x, int y, uint32 color) const override; -}; - -} // End of namespace Frotz -} // End of namespace Glk - -#endif diff --git a/engines/glk/frotz/screen.cpp b/engines/glk/frotz/screen.cpp new file mode 100644 index 0000000000..eef6d6118d --- /dev/null +++ b/engines/glk/frotz/screen.cpp @@ -0,0 +1,60 @@ +/* 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 "glk/frotz/screen.h" + +namespace Glk { +namespace Frotz { + +BitmapFont::BitmapFont(const Graphics::Surface &src, uint charWidth, + uint charHeight, unsigned char startingChar) : _startingChar(startingChar) { + assert(src.format.bytesPerPixel == 1); + assert((src.w % charWidth) == 0); + assert((src.h % charHeight) == 0); + _surface.copyFrom(src); + + Common::Rect r(charWidth, charHeight); + for (uint y = 0; y < src.h; y += charHeight) { + r.moveTo(0, y); + for (uint x = 0; x < src.w; x += charWidth, r.translate(charWidth, 0)) + _chars.push_back(r); + } +} + +BitmapFont::~BitmapFont() { + _surface.free(); +} + +void BitmapFont::drawChar(Graphics::Surface *dst, uint32 chr, int x, int y, uint32 color) const { + const Common::Rect &r = _chars[chr - _startingChar]; + for (int yCtr = 0; yCtr < r.height(); ++yCtr) { + const byte *srcP = (const byte *)_surface.getBasePtr(r.left, r.top + yCtr); + + for (int xCtr = 0; xCtr < r.width(); ++xCtr, ++srcP) { + if (*srcP) + dst->hLine(x + xCtr, y + yCtr, x + xCtr, color); + } + } +} + +} // End of namespace Scott +} // End of namespace Glk diff --git a/engines/glk/frotz/screen.h b/engines/glk/frotz/screen.h new file mode 100644 index 0000000000..dac716c436 --- /dev/null +++ b/engines/glk/frotz/screen.h @@ -0,0 +1,81 @@ +/* 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 GLK_FROTZ_FONTS +#define GLK_FROTZ_FONTS + +#include "graphics/font.h" +#include "graphics/surface.h" +#include "common/archive.h" +#include "common/array.h" +#include "common/rect.h" + +namespace Glk { +namespace Frotz { + +/** + * Implements a fixed width font stored as a grid on a passed surface + */ +class BitmapFont : public Graphics::Font { +private: + Graphics::Surface _surface; + Common::Array _chars; + size_t _startingChar; +public: + /** + * Constructor + */ + BitmapFont(const Graphics::Surface &src, uint charWidth = 8, uint charHeight = 8, + unsigned char startingChar = ' '); + + /** + * Destructor + */ + ~BitmapFont(); + + /** + * Get the font height + */ + virtual int getFontHeight() const override { return _chars[0].height(); } + + /** + * Get the maximum character width + */ + virtual int getMaxCharWidth() const override { return _chars[0].width(); } + + /** + * Get the width of the given character + */ + virtual int getCharWidth(uint32 chr) const override { + return _chars[chr - _startingChar].width(); + } + + /** + * Draw a character + */ + virtual void drawChar(Graphics::Surface *dst, uint32 chr, int x, int y, uint32 color) const override; +}; + +} // End of namespace Frotz +} // End of namespace Glk + +#endif diff --git a/engines/glk/module.mk b/engines/glk/module.mk index 7c7a85bb91..4112a9f020 100644 --- a/engines/glk/module.mk +++ b/engines/glk/module.mk @@ -5,7 +5,6 @@ MODULE_OBJS := \ conf.o \ detection.o \ events.o \ - fonts.o \ glk.o \ glk_api.o \ picture.o \ @@ -22,7 +21,6 @@ MODULE_OBJS := \ window_pair.o \ window_text_buffer.o \ window_text_grid.o \ - frotz/bitmap_font.o \ frotz/config.o \ frotz/detection.o \ frotz/frotz.o \ @@ -42,6 +40,7 @@ MODULE_OBJS := \ frotz/processor_text.o \ frotz/processor_variables.o \ frotz/quetzal.o \ + frotz/screen.o \ scott/detection.o \ scott/scott.o diff --git a/engines/glk/screen.cpp b/engines/glk/screen.cpp index 1a76ac8382..cfe8cde84f 100644 --- a/engines/glk/screen.cpp +++ b/engines/glk/screen.cpp @@ -22,9 +22,38 @@ #include "glk/screen.h" #include "glk/conf.h" +#include "common/unzip.h" +#include "image/bmp.h" +#include "graphics/fonts/ttf.h" +#include "graphics/fontman.h" namespace Glk { + +#define FONTS_VERSION 1.0 +#define FONTS_FILENAME "fonts.dat" + +Screen::Screen() { + if (!loadFonts()) + error("Could not load data file"); + + // TODO: See if there's any better way for getting the leading and baseline + Common::Rect r1 = _fonts[7]->getBoundingBox('o'); + Common::Rect r2 = _fonts[7]->getBoundingBox('y'); + double baseLine = (double)r1.bottom; + double leading = (double)r2.bottom + 2; + + g_conf->_leading = MAX((double)g_conf->_leading, leading); + g_conf->_baseLine = MAX((double)g_conf->_baseLine, baseLine); + g_conf->_cellW = _fonts[0]->getStringWidth("0"); + g_conf->_cellH = g_conf->_leading; +} + +Screen::~Screen() { + for (int idx = 0; idx < FONTS_TOTAL; ++idx) + delete _fonts[idx]; +} + void Screen::fill(const byte *rgb) { uint color = format.RGBToColor(rgb[0], rgb[1], rgb[2]); clear(color); @@ -69,4 +98,104 @@ void Screen::drawCaret(const Point &pos) { } } +bool Screen::loadFonts() { + Common::Archive *archive = nullptr; + _fonts.resize(FONTS_TOTAL); + + if (!Common::File::exists(FONTS_FILENAME) || (archive = Common::makeZipArchive(FONTS_FILENAME)) == nullptr) + return false; + + // Open the version.txt file within it to validate the version + Common::File f; + if (!f.open("version.txt", *archive)) { + delete archive; + return false; + } + + // Validate the version + char buffer[4]; + f.read(buffer, 3); + buffer[3] = '\0'; + + if (Common::String(buffer) != "1.0") { + delete archive; + return false; + } + + // R ead in the fonts + double monoAspect = g_conf->_monoAspect; + double propAspect = g_conf->_propAspect; + double monoSize = g_conf->_monoSize; + double propSize = g_conf->_propSize; + + _fonts[0] = loadFont(MONOR, archive, monoSize, monoAspect, FONTR); + _fonts[1] = loadFont(MONOB, archive, monoSize, monoAspect, FONTB); + _fonts[2] = loadFont(MONOI, archive, monoSize, monoAspect, FONTI); + _fonts[3] = loadFont(MONOZ, archive, monoSize, monoAspect, FONTZ); + + _fonts[4] = loadFont(PROPR, archive, propSize, propAspect, FONTR); + _fonts[5] = loadFont(PROPB, archive, propSize, propAspect, FONTB); + _fonts[6] = loadFont(PROPI, archive, propSize, propAspect, FONTI); + _fonts[7] = loadFont(PROPZ, archive, propSize, propAspect, FONTZ); + + delete archive; + return true; +} + +const Graphics::Font *Screen::loadFont(FACES face, Common::Archive *archive, double size, double aspect, int + style) { + Common::File f; + const char *const FILENAMES[8] = { + "GoMono-Regular.ttf", "GoMono-Bold.ttf", "GoMono-Italic.ttf", "GoMono-Bold-Italic.ttf", + "NotoSerif-Regular.ttf", "NotoSerif-Bold.ttf", "NotoSerif-Italic.ttf", "NotoSerif-Bold-Italic.ttf" + }; + + if (!f.open(FILENAMES[face], *archive)) + error("Could not load font"); + + return Graphics::loadTTFFont(f, size, Graphics::kTTFSizeModeCharacter); +} + +FACES Screen::getFontId(const Common::String &name) { + if (name == "monor") return MONOR; + if (name == "monob") return MONOB; + if (name == "monoi") return MONOI; + if (name == "monoz") return MONOZ; + if (name == "propr") return PROPR; + if (name == "propb") return PROPB; + if (name == "propi") return PROPI; + if (name == "propz") return PROPZ; + return MONOR; +} + +int Screen::drawString(const Point &pos, int fontIdx, const byte *rgb, const Common::String &text, int spw) { + Point pt(pos.x / GLI_SUBPIX, pos.y - g_conf->_baseLine); + const Graphics::Font *font = _fonts[fontIdx]; + const uint32 color = format.RGBToColor(rgb[0], rgb[1], rgb[2]); + font->drawString(this, text, pt.x, pt.y, w - pt.x, color); + + pt.x += font->getStringWidth(text); + return MIN((int)pt.x, (int)w) * GLI_SUBPIX; +} + +int Screen::drawStringUni(const Point &pos, int fontIdx, const byte *rgb, const Common::U32String &text, int spw) { + Point pt(pos.x / GLI_SUBPIX, pos.y - g_conf->_baseLine); + const Graphics::Font *font = _fonts[fontIdx]; + const uint32 color = format.RGBToColor(rgb[0], rgb[1], rgb[2]); + font->drawString(this, text, pt.x, pt.y, w - pt.x, color); + + pt.x += font->getStringWidth(text); + return MIN((int)pt.x, (int)w) * GLI_SUBPIX; +} + +size_t Screen::stringWidth(int fontIdx, const Common::String &text, int spw) { + const Graphics::Font *font = _fonts[fontIdx]; + return font->getStringWidth(text) * GLI_SUBPIX; +} + +size_t Screen::stringWidthUni(int fontIdx, const Common::U32String &text, int spw) { + const Graphics::Font *font = _fonts[fontIdx]; + return font->getStringWidth(text) * GLI_SUBPIX; +} + } // End of namespace Glk diff --git a/engines/glk/screen.h b/engines/glk/screen.h index 0020d86f19..541e9e190e 100644 --- a/engines/glk/screen.h +++ b/engines/glk/screen.h @@ -23,24 +23,56 @@ #ifndef GLK_DRAW_H #define GLK_DRAW_H +#include "common/archive.h" +#include "common/array.h" #include "graphics/screen.h" -#include "glk/fonts.h" +#include "graphics/font.h" +#include "glk/utils.h" namespace Glk { +#define FONTS_TOTAL 8 + enum CaretShape { SMALL_DOT = 0, FAT_DOT = 1, THIN_LINE = 2, FAT_LINE = 3, BLOCK = 4 }; +enum FACES { MONOR, MONOB, MONOI, MONOZ, PROPR, PROPB, PROPI, PROPZ }; +enum TYPES { MONOF, PROPF }; +enum STYLES { FONTR, FONTB, FONTI, FONTZ }; + /** * Screen surface class */ -class Screen : public Graphics::Screen, public Fonts { +class Screen : public Graphics::Screen { +private: + Common::Array _fonts; +private: + /** + * Load all the fonts + */ + bool loadFonts(); + + /** + * Load a single font + */ + const Graphics::Font *loadFont(FACES face, Common::Archive *archive, + double size, double aspect, int style); +public: + /** + * Return the font Id for a given name + */ + static FACES getFontId(const Common::String &name); public: /** * Constructor */ - Screen() : Graphics::Screen(), Fonts(this) {} + Screen(); + + /** + * Destructor + */ + virtual ~Screen(); /** * Fills the screen with a given rgb color @@ -58,6 +90,44 @@ public: * and the X position is in multiples of GLI_SUBPIX */ void drawCaret(const Point &pos); + + /** + * Draws a string using the specified font at the given co-ordinates + * @param pos Position for the bottom-left corner the text will be drawn with + * @param fontIdx Which font to use + * @param rgb RGB tuplet specifying the text color + * @param text The text to draw + * @param spw ?? + */ + int drawString(const Point &pos, int fontIdx, const byte *rgb, const Common::String &text, int spw = 0); + + /** + * Draws a unicode string using the specified font at the given co-ordinates + * @param pos Position for the bottom-left corner the text will be drawn with + * @param fontIdx Which font to use + * @param rgb RGB tuplet specifying the text color + * @param text The text to draw + * @param spw ?? + */ + int drawStringUni(const Point &pos, int fontIdx, const byte *rgb, const Common::U32String &text, int spw = 0); + + /** + * Get the width in pixels of a string + * @param fontIdx Which font to use + * @param text Text to get the width of + * @param spw Delta X + * @returns Width of string multiplied by GLI_SUBPIX + */ + size_t stringWidth(int fontIdx, const Common::String &text, int spw = 0); + + /** + * Get the width in pixels of a unicode string + * @param fontIdx Which font to use + * @param text Text to get the width of + * @param spw Delta X + * @returns Width of string multiplied by GLI_SUBPIX + */ + size_t stringWidthUni(int fontIdx, const Common::U32String &text, int spw = 0); }; } // End of namespace Glk diff --git a/engines/glk/windows.h b/engines/glk/windows.h index 7abb9e5ff5..27380fa23e 100644 --- a/engines/glk/windows.h +++ b/engines/glk/windows.h @@ -29,7 +29,7 @@ #include "graphics/screen.h" #include "glk/events.h" #include "glk/glk_types.h" -#include "glk/fonts.h" +#include "glk/screen.h" #include "glk/selection.h" #include "glk/streams.h" -- cgit v1.2.3