tibia-client/src/framework/graphics/bitmapfont.cpp

338 lines
12 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
2012-01-02 17:58:37 +01:00
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
2011-08-28 15:17:58 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "bitmapfont.h"
2011-08-14 04:09:11 +02:00
#include "texturemanager.h"
#include "graphics.h"
#include "image.h"
2011-04-10 00:55:58 +02:00
2011-08-15 16:06:15 +02:00
#include <framework/otml/otml.h>
2011-04-10 00:55:58 +02:00
void BitmapFont::load(const OTMLNodePtr& fontNode)
{
2012-01-05 02:28:29 +01:00
OTMLNodePtr textureNode = fontNode->at("texture");
std::string textureFile = stdext::resolve_path(textureNode->value(), textureNode->source());
2011-11-17 22:41:02 +01:00
Size glyphSize = fontNode->valueAt<Size>("glyph-size");
m_glyphHeight = fontNode->valueAt<int>("height");
2011-11-17 22:41:02 +01:00
m_yOffset = fontNode->valueAt("y-offset", 0);
m_firstGlyph = fontNode->valueAt("first-glyph", 32);
m_glyphSpacing = fontNode->valueAt("spacing", Size(0,0));
int spaceWidth = fontNode->valueAt("space-width", glyphSize.width());
2011-08-14 04:09:11 +02:00
// load font texture
2012-01-05 02:28:29 +01:00
m_texture = g_textures.getTexture(textureFile);
2011-08-14 04:09:11 +02:00
2011-11-17 22:41:02 +01:00
if(OTMLNodePtr node = fontNode->get("fixed-glyph-width")) {
2011-08-20 22:30:41 +02:00
for(int glyph = m_firstGlyph; glyph < 256; ++glyph)
m_glyphsSize[glyph] = Size(node->value<int>(), m_glyphHeight);
} else {
calculateGlyphsWidthsAutomatically(Image::load(textureFile), glyphSize);
}
2011-08-14 04:09:11 +02:00
// 32 and 160 are spaces (&nbsp;)
m_glyphsSize[32].setWidth(spaceWidth);
m_glyphsSize[160].setWidth(spaceWidth);
// new line actually has a size that will be useful in multiline algorithm
m_glyphsSize[(uchar)'\n'] = Size(1, m_glyphHeight);
2011-08-14 04:09:11 +02:00
// read custom widths
/*
2011-11-17 22:41:02 +01:00
if(OTMLNodePtr node = fontNode->get("glyph-widths")) {
for(const OTMLNodePtr& child : node->children())
m_glyphsSize[stdext::safe_cast<int>(child->tag())].setWidth(child->value<int>());
2011-04-07 02:58:36 +02:00
}
*/
2011-04-07 02:58:36 +02:00
2011-08-14 04:09:11 +02:00
// calculate glyphs texture coords
int numHorizontalGlyphs = m_texture->getSize().width() / glyphSize.width();
for(int glyph = m_firstGlyph; glyph < 256; ++glyph) {
m_glyphsTextureCoords[glyph].setRect(((glyph - m_firstGlyph) % numHorizontalGlyphs) * glyphSize.width(),
((glyph - m_firstGlyph) / numHorizontalGlyphs) * glyphSize.height(),
m_glyphsSize[glyph].width(),
m_glyphHeight);
2011-04-07 02:58:36 +02:00
}
}
void BitmapFont::drawText(const std::string& text, const Point& startPos)
2011-04-07 20:49:35 +02:00
{
2012-06-14 20:26:55 +02:00
Size boxSize = g_painter->getResolution() - startPos.toSize();
2011-04-07 22:36:40 +02:00
Rect screenCoords(startPos, boxSize);
drawText(text, screenCoords, Fw::AlignTopLeft);
2011-04-07 20:49:35 +02:00
}
void BitmapFont::drawText(const std::string& text, const Rect& screenCoords, Fw::AlignmentFlag align)
{
static CoordsBuffer coordsBuffer;
coordsBuffer.clear();
calculateDrawTextCoords(coordsBuffer, text, screenCoords, align);
g_painter->drawTextureCoords(coordsBuffer, m_texture);
}
2011-04-15 04:13:53 +02:00
void BitmapFont::calculateDrawTextCoords(CoordsBuffer& coordsBuffer, const std::string& text, const Rect& screenCoords, Fw::AlignmentFlag align)
2011-04-07 02:58:36 +02:00
{
2011-04-16 18:08:55 +02:00
// prevent glitches from invalid rects
2011-12-05 19:27:07 +01:00
if(!screenCoords.isValid() || !m_texture)
2011-04-16 18:08:55 +02:00
return;
int textLenght = text.length();
// map glyphs positions
Size textBoxSize;
const std::vector<Point>& glyphsPositions = calculateGlyphsPositions(text, align, &textBoxSize);
for(int i = 0; i < textLenght; ++i) {
int glyph = (uchar)text[i];
// skip invalid glyphs
if(glyph < 32)
continue;
// calculate initial glyph rect and texture coords
Rect glyphScreenCoords(glyphsPositions[i], m_glyphsSize[glyph]);
Rect glyphTextureCoords = m_glyphsTextureCoords[glyph];
// first translate to align position
if(align & Fw::AlignBottom) {
2011-04-16 18:08:55 +02:00
glyphScreenCoords.translate(0, screenCoords.height() - textBoxSize.height());
} else if(align & Fw::AlignVerticalCenter) {
2011-04-16 18:08:55 +02:00
glyphScreenCoords.translate(0, (screenCoords.height() - textBoxSize.height()) / 2);
2011-05-12 00:16:11 +02:00
} else { // AlignTop
2011-04-16 18:08:55 +02:00
// nothing to do
}
if(align & Fw::AlignRight) {
2011-04-16 18:08:55 +02:00
glyphScreenCoords.translate(screenCoords.width() - textBoxSize.width(), 0);
} else if(align & Fw::AlignHorizontalCenter) {
2011-04-16 18:08:55 +02:00
glyphScreenCoords.translate((screenCoords.width() - textBoxSize.width()) / 2, 0);
2011-05-12 00:16:11 +02:00
} else { // AlignLeft
2011-04-16 18:08:55 +02:00
// nothing to do
}
2011-05-13 01:24:57 +02:00
// only render glyphs that are after 0, 0
if(glyphScreenCoords.bottom() < 0 || glyphScreenCoords.right() < 0)
continue;
// bound glyph topLeft to 0,0 if needed
if(glyphScreenCoords.top() < 0) {
glyphTextureCoords.setTop(glyphTextureCoords.top() - glyphScreenCoords.top());
glyphScreenCoords.setTop(0);
}
if(glyphScreenCoords.left() < 0) {
glyphTextureCoords.setLeft(glyphTextureCoords.left() - glyphScreenCoords.left());
glyphScreenCoords.setLeft(0);
}
2011-04-16 18:08:55 +02:00
// translate rect to screen coords
glyphScreenCoords.translate(screenCoords.topLeft());
// only render if glyph rect is visible on screenCoords
if(!screenCoords.intersects(glyphScreenCoords))
continue;
// bound glyph bottomRight to screenCoords bottomRight
if(glyphScreenCoords.bottom() > screenCoords.bottom()) {
glyphTextureCoords.setBottom(glyphTextureCoords.bottom() + (screenCoords.bottom() - glyphScreenCoords.bottom()));
glyphScreenCoords.setBottom(screenCoords.bottom());
}
if(glyphScreenCoords.right() > screenCoords.right()) {
glyphTextureCoords.setRight(glyphTextureCoords.right() + (screenCoords.right() - glyphScreenCoords.right()));
glyphScreenCoords.setRight(screenCoords.right());
}
// render glyph
coordsBuffer.addRect(glyphScreenCoords, glyphTextureCoords);
2011-04-16 18:08:55 +02:00
}
2011-04-07 02:58:36 +02:00
}
const std::vector<Point>& BitmapFont::calculateGlyphsPositions(const std::string& text,
Fw::AlignmentFlag align,
Size *textBoxSize)
2011-04-07 20:49:35 +02:00
{
2011-04-16 18:08:55 +02:00
// for performance reasons we use statics vectors that are allocated on demand
static std::vector<Point> glyphsPositions(1);
static std::vector<int> lineWidths(1);
2011-04-15 04:13:53 +02:00
2011-04-16 18:08:55 +02:00
int textLength = text.length();
2011-04-07 22:36:40 +02:00
int maxLineWidth = 0;
int lines = 0;
int glyph;
int i;
2011-04-07 20:49:35 +02:00
2011-04-16 18:08:55 +02:00
// return if there is no text
if(textLength == 0) {
if(textBoxSize)
2011-12-07 01:31:55 +01:00
textBoxSize->resize(0,m_glyphHeight);
2011-04-16 18:08:55 +02:00
return glyphsPositions;
}
2011-04-17 02:36:58 +02:00
// resize glyphsPositions vector when needed
2011-04-16 18:08:55 +02:00
if(textLength > (int)glyphsPositions.size())
glyphsPositions.resize(textLength);
2011-04-07 22:36:40 +02:00
// calculate lines width
if((align & Fw::AlignRight || align & Fw::AlignHorizontalCenter) || textBoxSize) {
2011-04-07 22:36:40 +02:00
lineWidths[0] = 0;
2011-04-16 18:08:55 +02:00
for(i = 0; i< textLength; ++i) {
2011-04-11 22:06:03 +02:00
glyph = (uchar)text[i];
2011-04-07 22:36:40 +02:00
if(glyph == (uchar)'\n') {
2011-04-16 18:08:55 +02:00
lines++;
if(lines+1 > (int)lineWidths.size())
lineWidths.resize(lines+1);
lineWidths[lines] = 0;
2011-04-07 22:36:40 +02:00
} else if(glyph >= 32) {
2011-12-21 05:43:51 +01:00
lineWidths[lines] += m_glyphsSize[glyph].width() ;
if((i+1 != textLength && text[i+1] != '\n')) // only add space if letter is not the last or before a \n.
lineWidths[lines] += m_glyphSpacing.width();
2011-04-07 22:36:40 +02:00
maxLineWidth = std::max(maxLineWidth, lineWidths[lines]);
}
}
}
2011-11-17 22:41:02 +01:00
Point virtualPos(0, m_yOffset);
2011-04-07 22:36:40 +02:00
lines = 0;
2011-04-16 18:08:55 +02:00
for(i = 0; i < textLength; ++i) {
2011-04-11 22:06:03 +02:00
glyph = (uchar)text[i];
2011-04-07 02:58:36 +02:00
2011-04-07 22:36:40 +02:00
// new line or first glyph
if(glyph == (uchar)'\n' || i == 0) {
if(glyph == (uchar)'\n') {
2011-04-10 02:51:35 +02:00
virtualPos.y += m_glyphHeight + m_glyphSpacing.height();
2011-04-07 22:36:40 +02:00
lines++;
}
// calculate start x pos
if(align & Fw::AlignRight) {
2011-04-07 22:36:40 +02:00
virtualPos.x = (maxLineWidth - lineWidths[lines]);
} else if(align & Fw::AlignHorizontalCenter) {
2011-04-07 22:36:40 +02:00
virtualPos.x = (maxLineWidth - lineWidths[lines]) / 2;
2011-05-12 00:16:11 +02:00
} else { // AlignLeft
2011-04-07 22:36:40 +02:00
virtualPos.x = 0;
}
2011-04-07 10:38:29 +02:00
}
2011-04-07 22:36:40 +02:00
2011-04-17 02:36:58 +02:00
// store current glyph topLeft
glyphsPositions[i] = virtualPos;
2011-04-07 20:49:35 +02:00
// render only if the glyph is valid
2011-04-07 22:36:40 +02:00
if(glyph >= 32 && glyph != (uchar)'\n') {
2011-12-21 05:43:51 +01:00
virtualPos.x += m_glyphsSize[glyph].width() + m_glyphSpacing.width();
2011-04-07 10:38:29 +02:00
}
}
2011-04-07 20:49:35 +02:00
2011-04-07 22:36:40 +02:00
if(textBoxSize) {
textBoxSize->setWidth(maxLineWidth);
textBoxSize->setHeight(virtualPos.y + m_glyphHeight);
2011-04-07 22:36:40 +02:00
}
2011-04-15 04:13:53 +02:00
return glyphsPositions;
2011-04-06 21:46:58 +02:00
}
2011-04-07 22:36:40 +02:00
Size BitmapFont::calculateTextRectSize(const std::string& text)
2011-04-07 22:36:40 +02:00
{
Size size;
calculateGlyphsPositions(text, Fw::AlignTopLeft, &size);
2011-04-07 22:36:40 +02:00
return size;
}
2011-04-15 04:13:53 +02:00
void BitmapFont::calculateGlyphsWidthsAutomatically(const ImagePtr& image, const Size& glyphSize)
2011-08-14 04:09:11 +02:00
{
int numHorizontalGlyphs = image->getSize().width() / glyphSize.width();
auto texturePixels = image->getPixels();
2011-08-14 04:09:11 +02:00
// small AI to auto calculate pixels widths
for(int glyph = m_firstGlyph; glyph< 256; ++glyph) {
Rect glyphCoords(((glyph - m_firstGlyph) % numHorizontalGlyphs) * glyphSize.width(),
((glyph - m_firstGlyph) / numHorizontalGlyphs) * glyphSize.height(),
glyphSize.width(),
m_glyphHeight);
int width = glyphSize.width();
2011-11-02 06:59:04 +01:00
for(int x = glyphCoords.left(); x <= glyphCoords.right(); ++x) {
2012-04-26 03:58:36 +02:00
int filledPixels = 0;
2011-08-14 04:09:11 +02:00
// check if all vertical pixels are alpha
for(int y = glyphCoords.top(); y <= glyphCoords.bottom(); ++y) {
2012-04-26 03:58:36 +02:00
if(texturePixels[(y * image->getSize().width() * 4) + (x*4) + 3] != 0)
filledPixels++;
2011-08-14 04:09:11 +02:00
}
if(filledPixels > 0)
2012-04-26 03:58:36 +02:00
width = x - glyphCoords.left() + 1;
2011-08-14 04:09:11 +02:00
}
// store glyph size
2011-12-07 01:31:55 +01:00
m_glyphsSize[glyph].resize(width, m_glyphHeight);
2011-08-14 04:09:11 +02:00
}
}
2012-01-25 01:50:30 +01:00
std::string BitmapFont::wrapText(const std::string& text, int maxWidth)
2012-01-25 01:50:30 +01:00
{
std::string outText;
std::string line;
std::vector<std::string> words;
std::vector<std::string> wordsSplit = stdext::split(text);
2012-01-25 01:50:30 +01:00
// break huge words into small ones
for(uint i=0;i<wordsSplit.size();++i) {
const std::string& word = wordsSplit[i];
int wordWidth = calculateTextRectSize(word).width();
if(wordWidth > maxWidth) {
std::string newWord;
for(uint j=0;j<word.length();++j) {
std::string candidate = newWord + word[j];
if(j != word.length() - 1)
candidate += "-";
int candidateWidth = calculateTextRectSize(candidate).width();
if(candidateWidth > maxWidth) {
newWord += "-";
words.push_back(newWord);
newWord = "";
}
newWord += word[j];
}
words.push_back(newWord);
} else
words.push_back(word);
}
// compose lines
for(uint i=0;i<words.size();++i) {
std::string candidate = line + words[i];
int candidateWidth = calculateTextRectSize(candidate).width();
if(candidateWidth > maxWidth) {
if(!line.empty())
outText += line.substr(0, line.length()-1) + "\n";
line = "";
}
line += words[i] + " ";
}
outText += line;
outText = outText.substr(0, outText.length()-1);
return outText;
}