aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engines/zvision/truetype_font.cpp128
-rw-r--r--engines/zvision/truetype_font.h65
2 files changed, 26 insertions, 167 deletions
diff --git a/engines/zvision/truetype_font.cpp b/engines/zvision/truetype_font.cpp
index dacd7967d3..d88f22a727 100644
--- a/engines/zvision/truetype_font.cpp
+++ b/engines/zvision/truetype_font.cpp
@@ -26,6 +26,7 @@
#include "common/file.h"
#include "common/system.h"
+#include "graphics/font.h"
#include "graphics/fonts/ttf.h"
#include "graphics/surface.h"
@@ -36,43 +37,27 @@
namespace ZVision {
-TruetypeFont::TruetypeFont(ZVision *engine, int32 fontHeight, const Graphics::PixelFormat pixelFormat)
+TruetypeFont::TruetypeFont(ZVision *engine, int32 fontHeight)
: _engine(engine),
- _pixelFormat(pixelFormat),
- _isBold(false),
- _isItalic(false),
- _isStriked(false),
- _isUnderline(false),
_fontHeight(fontHeight),
_font(0),
_lineHeight(0),
_maxCharWidth(0),
_maxCharHeight(0) {
- for (int i = 0; i < NUM_CACHED_TEXTS; i++) {
- _cachedTexts[i] = 0;
- }
}
TruetypeFont::~TruetypeFont(void) {
- clearCache();
-
delete _font;
}
-void TruetypeFont::clearCache() {
- for (int i = 0; i < NUM_CACHED_TEXTS; i++) {
- if (_cachedTexts[i]) {
- delete _cachedTexts[i];
- }
- _cachedTexts[i] = 0;
- }
-}
-
bool TruetypeFont::loadFile(const Common::String &filename) {
Common::File file;
- if (file.open(filename)) {
- debug("TTF font file %s could not be opened", filename.c_str());
- return false;
+
+ if (!Common::File::exists(filename)) {
+ debug("TTF font file %s was not found. Reverting to arial.ttf", filename.c_str());
+ file.open("arial.ttf");
+ } else {
+ file.open(filename);
}
_font = Graphics::loadTTFFont(file, _fontHeight);
@@ -81,65 +66,25 @@ bool TruetypeFont::loadFile(const Common::String &filename) {
return true;
}
-void TruetypeFont::drawText(const Common::String &text, int x, int y, int width, TextAlign align, int maxHeight) {
+Graphics::Surface *TruetypeFont::drawTextToSurface(const Common::String &text, int destX, int destY, uint16 textColor, int maxWidth, int maxHeight, Graphics::TextAlign align, bool wrap) {
if (text.equals("")) {
- return;
- }
-
- // Find the cached surface, if it exists
- uint32 minUseTime = UINT_MAX;
- int minIndex = -1;
- Graphics::Surface *surface = nullptr;
-
- for (int i = 0; i < NUM_CACHED_TEXTS; i++) {
- if (_cachedTexts[i] == nullptr) {
- minUseTime = 0;
- minIndex = i;
- } else {
- if (_cachedTexts[i]->_text == text && _cachedTexts[i]->_align == align && _cachedTexts[i]->_width == width && _cachedTexts[i]->_maxHeight == maxHeight) {
- surface = _cachedTexts[i]->_surface;
- _cachedTexts[i]->_marked = true;
- _cachedTexts[i]->_lastUsed = g_system->getMillis();
- break;
- } else {
- if (_cachedTexts[i]->_lastUsed < minUseTime) {
- minUseTime = _cachedTexts[i]->_lastUsed;
- minIndex = i;
- }
- }
- }
+ return nullptr;
}
- // It's not cached, so create one
- if (!surface) {
- surface = renderTextToTexture(text, width, align, maxHeight);
- if (surface) {
- // Write the new surface to cache
- if (_cachedTexts[minIndex] != nullptr) {
- delete _cachedTexts[minIndex];
- }
- _cachedTexts[minIndex] = new CachedText;
-
- _cachedTexts[minIndex]->_surface = surface;
- _cachedTexts[minIndex]->_align = align;
- _cachedTexts[minIndex]->_width = width;
- _cachedTexts[minIndex]->_maxHeight = maxHeight;
- _cachedTexts[minIndex]->_text = text;
- _cachedTexts[minIndex]->_marked = true;
- _cachedTexts[minIndex]->_lastUsed = _engine->_system->getMillis();
- }
- }
+ 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));
+ // Copy the pixels from the RenderManager::_workingWindow, so we can get nice antialiasing
+ _engine->getRenderManager()->copyWorkingWindowSubRectToSurface(surface, 0, 0, Common::Rect(destX, destY, destX + width, destY + _lineHeight));
- // Render it to the working window
- if (surface) {
- _engine->getRenderManager()->copyRectToWorkingWindow((uint16 *)surface->getPixels(), x, y, surface->w, surface->w, surface->h);
+ _font->drawString(surface, text, destX, destY, maxWidth, textColor, align);
+ return surface;
}
-}
-Graphics::Surface *TruetypeFont::renderTextToTexture(const Common::String &text, int width, TextAlign align, int maxHeight) {
Common::Array<Common::String> lines;
- _font->wordWrapText(text, width, lines);
+ _font->wordWrapText(text, maxWidth, lines);
while (maxHeight > 0 && lines.size() * _lineHeight > maxHeight) {
lines.pop_back();
@@ -148,44 +93,17 @@ Graphics::Surface *TruetypeFont::renderTextToTexture(const Common::String &text,
return nullptr;
}
- Graphics::TextAlign alignment = Graphics::kTextAlignInvalid;
- if (align == ALIGN_LEFT) {
- alignment = Graphics::kTextAlignLeft;
- } else if (align == ALIGN_CENTER) {
- alignment = Graphics::kTextAlignCenter;
- } else if (align == ALIGN_RIGHT) {
- alignment = Graphics::kTextAlignRight;
- }
-
- Graphics::Surface *surface = new Graphics::Surface();
- surface->create((uint16)width, (uint16)(_lineHeight * lines.size()), _pixelFormat);
+ surface->create(maxWidth, lines.size() * _lineHeight, Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0));
+ // Copy the pixels from the RenderManager::_workingWindow, so we can get nice antialiasing
+ _engine->getRenderManager()->copyWorkingWindowSubRectToSurface(surface, 0, 0, Common::Rect(destX, destY, destX + maxWidth, destY + lines.size() * _lineHeight));
- uint32 alphaColor = 0xffffffff;
int heightOffset = 0;
for (Common::Array<Common::String>::iterator it = lines.begin(); it != lines.end(); it++) {
- _font->drawString(surface, *it, 0, heightOffset, width, alphaColor, alignment);
+ _font->drawString(surface, *it, destX, destY + heightOffset, maxWidth, textColor, align);
heightOffset += (int)_lineHeight;
}
return surface;
- // TODO: _isUnderline, _isBold, _isItalic, _isStriked
-}
-
-void TruetypeFont::measureText(const Common::String &text, int maxWidth, int maxHeight, int &textWidthOut, int &textHeightOut) {
- if (maxWidth >= 0) {
- Common::Array<Common::String> lines;
- _font->wordWrapText(text, maxWidth, lines);
- Common::Array<Common::String>::iterator it;
- textWidthOut = 0;
- for (it = lines.begin(); it != lines.end(); ++it) {
- textWidthOut = MAX(textWidthOut, _font->getStringWidth(*it));
- }
-
- textHeightOut = (int)(lines.size() * getLineHeight());
- } else {
- textWidthOut = _font->getStringWidth(text);
- textHeightOut = _fontHeight;
- }
}
} // End of namespace ZVision
diff --git a/engines/zvision/truetype_font.h b/engines/zvision/truetype_font.h
index 48657417a2..c8d0a95b28 100644
--- a/engines/zvision/truetype_font.h
+++ b/engines/zvision/truetype_font.h
@@ -28,7 +28,7 @@
#include "common/types.h"
#include "graphics/font.h"
-
+#include "graphics/pixelformat.h"
namespace Graphics {
struct Surface;
@@ -40,83 +40,24 @@ class ZVision;
class TruetypeFont {
public:
- TruetypeFont(ZVision *engine, int32 fontHeight, const Graphics::PixelFormat pixelFormat);
+ TruetypeFont(ZVision *engine, int32 fontHeight);
~TruetypeFont();
-public:
- enum {
- NUM_CACHED_TEXTS = 30
- };
-
- enum TextAlign {
- ALIGN_LEFT = 0,
- ALIGN_RIGHT,
- ALIGN_CENTER
- };
-
- class CachedText {
- public:
- Common::String _text;
- int32 _width;
- TextAlign _align;
- int32 _maxHeight;
- Graphics::Surface *_surface;
- int32 _textOffset;
- bool _marked;
- uint32 _lastUsed;
-
- CachedText() {
- _width = _maxHeight = -1;
- _align = ALIGN_LEFT;
- _surface = 0;
- _lastUsed = 0;
- _marked = false;
- }
-
- virtual ~CachedText() {
- delete _surface;
- }
- };
-
private:
ZVision *_engine;
- const Graphics::PixelFormat _pixelFormat;
-
Graphics::Font *_font;
-
float _lineHeight;
size_t _maxCharWidth;
size_t _maxCharHeight;
- CachedText *_cachedTexts[NUM_CACHED_TEXTS];
-
public:
- bool _isBold;
- bool _isItalic;
- bool _isUnderline;
- bool _isStriked;
int32 _fontHeight;
- //BaseArray<BaseTTFontLayer *> _layers;
-
public:
- int getTextWidth(const byte *text, int maxLength = -1);
- int getTextHeight(const byte *text, int width);
- void drawText(const Common::String &text, int x, int y, int width, TextAlign align = ALIGN_LEFT, int max_height = -1);
- int getLetterHeight();
-
bool loadFile(const Common::String &filename);
-
- float getLineHeight() const { return _lineHeight; }
- void clearCache();
-
- static TruetypeFont *createFromFile(ZVision *game, const Common::String &filename);
-
-private:
- void measureText(const Common::String &text, int maxWidth, int maxHeight, int &textWidthOut, int &textHeightOut);
- Graphics::Surface *renderTextToTexture(const Common::String &text, int width, TextAlign align, int maxHeight);
+ Graphics::Surface *drawTextToSurface(const Common::String &text, int destX, int destY, uint16 textColor, int maxWidth, int maxHeight, Graphics::TextAlign align, bool wrap);
};
} // End of namespace ZVision