tibia-client/src/framework/font.cpp

282 lines
9.4 KiB
C++
Raw Normal View History

/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* 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.
*/
2011-04-06 21:46:58 +02:00
#include "font.h"
2011-04-07 02:58:36 +02:00
#include "resources.h"
#include "textures.h"
#include "graphics.h"
Font::Font() :
m_lineHeight(14),
m_cursorSize(14),
2011-04-07 09:30:32 +02:00
m_color(0xFFFFFFFF)
2011-04-07 02:58:36 +02:00
{
}
2011-04-06 21:46:58 +02:00
bool Font::load(const std::string& file)
{
2011-04-07 02:58:36 +02:00
if(!g_resources.fileExists(file)) {
2011-04-07 11:36:02 +02:00
logError("Font file %s does not exists", file.c_str());
2011-04-07 02:58:36 +02:00
return false;
}
std::string fileContents = g_resources.loadTextFile(file);
if(!fileContents.size()) {
2011-04-07 11:36:02 +02:00
logError("Empty font file \"%s", file.c_str());
2011-04-07 02:58:36 +02:00
return false;
}
std::istringstream fin(fileContents);
std::string textureName;
2011-04-07 09:30:32 +02:00
int numHorizontalGlyphs;
int firstGlyph;
Size glyphSize;
Size textureSize;
2011-04-07 02:58:36 +02:00
try {
YAML::Parser parser(fin);
YAML::Node doc;
parser.GetNextDocument(doc);
doc["line height"] >> m_lineHeight;
doc["cursor size"] >> m_cursorSize;
doc["color"] >> m_color;
2011-04-07 09:30:32 +02:00
doc["first glyph"] >> firstGlyph;
doc["image glyph size"] >> glyphSize;
2011-04-07 02:58:36 +02:00
doc["image"] >> textureName;
2011-04-07 09:30:32 +02:00
m_texture = g_textures.get("fonts/" + textureName);
if(!m_texture) {
2011-04-07 11:36:02 +02:00
logError("Failed to load image for font \"%s\"", file.c_str());
2011-04-07 09:30:32 +02:00
return false;
}
textureSize = m_texture->getSize();
numHorizontalGlyphs = textureSize.width() / glyphSize.width();
2011-04-07 02:58:36 +02:00
2011-04-07 05:13:57 +02:00
const YAML::Node& widthsNode = doc["glyph widths"];
2011-04-07 02:58:36 +02:00
for(auto it = widthsNode.begin(); it != widthsNode.end(); ++it) {
2011-04-07 09:30:32 +02:00
int glyph, glyphWidth;
it.first() >> glyph;
it.second() >> glyphWidth;
// calculate glyph texture coords
m_glyphsTextureCoords[glyph].setRect(((glyph - firstGlyph) % numHorizontalGlyphs) * glyphSize.width(),
((glyph - firstGlyph) / numHorizontalGlyphs) * glyphSize.height(),
glyphWidth,
glyphSize.height());
// store glyph size
m_glyphsSize[glyph].setHeight(glyphSize.height());
m_glyphsSize[glyph].setWidth(glyphWidth);
2011-04-07 02:58:36 +02:00
}
} catch (YAML::ParserException& e) {
2011-04-07 11:36:02 +02:00
logError("Malformed font file \"%s\"", file.c_str());
2011-04-07 02:58:36 +02:00
return false;
}
return true;
}
2011-04-07 22:36:40 +02:00
void Font::renderText(const std::string& text,
const Point& startPos)
2011-04-07 20:49:35 +02:00
{
2011-04-07 22:36:40 +02:00
Size boxSize = g_graphics.getScreenSize() - startPos.toSize();
Rect screenCoords(startPos, boxSize);
Font::renderText(text, screenCoords);
2011-04-07 20:49:35 +02:00
}
2011-04-07 22:36:40 +02:00
void Font::renderText(const std::string& text,
const Rect& screenCoords,
int align,
const Point& startInternalPos,
bool debug)
2011-04-07 02:58:36 +02:00
{
2011-04-07 22:36:40 +02:00
// prevent glitches from invalid rects
if(!screenCoords.isValid())
return;
2011-04-07 09:30:32 +02:00
// begin texture rendering
g_graphics.setColor(m_color);
g_graphics._beginTextureRender(m_texture.get());
2011-04-07 02:58:36 +02:00
2011-04-07 10:38:29 +02:00
const Size& textureSize = m_texture->getSize();
2011-04-07 02:58:36 +02:00
int textLenght = text.length();
2011-04-07 20:49:35 +02:00
// map glyphs positions
2011-04-07 22:36:40 +02:00
Size textBoxSize;
Point *glyphsPositions = calculateGlyphsPositions(text, align, &textBoxSize);
2011-04-07 20:49:35 +02:00
2011-04-07 02:58:36 +02:00
for(int i = 0; i < textLenght; ++i) {
2011-04-07 10:38:29 +02:00
int glyph = (int)text[i];
2011-04-07 02:58:36 +02:00
2011-04-07 20:49:35 +02:00
// skip invalid glyphs
if(glyph < 32)
continue;
2011-04-07 02:58:36 +02:00
2011-04-07 22:36:40 +02:00
// calculate initial glyph rect and texture coords
2011-04-07 20:49:35 +02:00
Rect glyphScreenCoords(glyphsPositions[i], m_glyphsSize[glyph]);
Rect glyphTextureCoords = m_glyphsTextureCoords[glyph];
2011-04-07 22:36:40 +02:00
// first translate to align position
if(align & ALIGN_BOTTOM) {
glyphScreenCoords.translate(0, screenCoords.height() - textBoxSize.height());
} else if(align & ALIGN_VERTICAL_CENTER) {
glyphScreenCoords.translate(0, (screenCoords.height() - textBoxSize.height()) / 2);
} else { // ALIGN_TOP
// nothing to do
}
if(align & ALIGN_RIGHT) {
glyphScreenCoords.translate(screenCoords.width() - textBoxSize.width(), 0);
} else if(align & ALIGN_HORIZONTAL_CENTER) {
glyphScreenCoords.translate((screenCoords.width() - textBoxSize.width()) / 2, 0);
} else { // ALIGN_TOP
// nothing to do
}
// only render glyphs that are after startRenderPosition
if(glyphScreenCoords.bottom() < startInternalPos.y || glyphScreenCoords.right() < startInternalPos.x)
2011-04-07 20:49:35 +02:00
continue;
// bound glyph topLeft to startRenderPosition
2011-04-07 22:36:40 +02:00
if(glyphScreenCoords.top() < startInternalPos.y) {
glyphTextureCoords.setTop(glyphTextureCoords.top() + (startInternalPos.y - glyphScreenCoords.top()));
glyphScreenCoords.setTop(startInternalPos.y);
2011-04-07 02:58:36 +02:00
}
2011-04-07 22:36:40 +02:00
if(glyphScreenCoords.left() < startInternalPos.x) {
glyphTextureCoords.setLeft(glyphTextureCoords.left() + (startInternalPos.x - glyphScreenCoords.left()));
glyphScreenCoords.setLeft(startInternalPos.x);
2011-04-07 02:58:36 +02:00
}
2011-04-07 20:49:35 +02:00
2011-04-07 22:36:40 +02:00
// subtract startInternalPos
glyphScreenCoords.translate(-startInternalPos);
// translate rect to screen coords
2011-04-07 20:49:35 +02:00
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
g_graphics._drawTexturedRect(glyphScreenCoords, glyphTextureCoords, textureSize);
2011-04-07 02:58:36 +02:00
}
2011-04-07 09:30:32 +02:00
// end texture redering
g_graphics._endTextureRender();
g_graphics.resetColor();
2011-04-07 17:41:49 +02:00
2011-04-07 20:49:35 +02:00
if(debug)
g_graphics.drawBoundingRect(screenCoords.expanded(1), Color(0xFF00FF00), 1);
2011-04-07 02:58:36 +02:00
}
2011-04-07 22:36:40 +02:00
Point* Font::calculateGlyphsPositions(const std::string& text, int align, Size *textBoxSize)
2011-04-07 20:49:35 +02:00
{
static Point glyphsPositions[8192];
2011-04-07 22:36:40 +02:00
static int lineWidths[512];
int maxLineWidth = 0;
int lines = 0;
int glyph;
int i;
2011-04-07 20:49:35 +02:00
2011-04-07 22:36:40 +02:00
// protect buffer overflow on glyphsPostions
int numGlyphs = text.length();
2011-04-07 20:49:35 +02:00
if(numGlyphs > 8192)
logFatal("A text was too long to render!");
2011-04-07 22:36:40 +02:00
// calculate lines width
if((align & ALIGN_RIGHT || align & ALIGN_HORIZONTAL_CENTER) || textBoxSize) {
lineWidths[0] = 0;
for(i = 0; i< numGlyphs; ++i) {
glyph = (int)text[i];
if(glyph == (uchar)'\n') {
lineWidths[++lines] = 0;
} else if(glyph >= 32) {
lineWidths[lines] += m_glyphsSize[glyph].width();
maxLineWidth = std::max(maxLineWidth, lineWidths[lines]);
}
}
}
Point virtualPos;
lines = 0;
for(i = 0; i < numGlyphs; ++i) {
glyph = (int)text[i];
2011-04-07 02:58:36 +02:00
2011-04-07 20:49:35 +02:00
// store current glyph topLeft
glyphsPositions[i] = virtualPos;
2011-04-07 22:36:40 +02:00
// new line or first glyph
if(glyph == (uchar)'\n' || i == 0) {
if(glyph == (uchar)'\n') {
virtualPos.y += m_lineHeight;
lines++;
}
// calculate start x pos
if(align & ALIGN_RIGHT) {
virtualPos.x = (maxLineWidth - lineWidths[lines]);
} else if(align & ALIGN_HORIZONTAL_CENTER) {
virtualPos.x = (maxLineWidth - lineWidths[lines]) / 2;
} else { // ALIGN_LEFT
virtualPos.x = 0;
}
2011-04-07 10:38:29 +02:00
}
2011-04-07 22:36:40 +02:00
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-04-07 20:49:35 +02:00
virtualPos.x += m_glyphsSize[glyph].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_lineHeight);
}
2011-04-07 20:49:35 +02:00
return (Point *)glyphsPositions;
2011-04-06 21:46:58 +02:00
}
2011-04-07 22:36:40 +02:00
Size Font::calculateTextBoxSize(const std::string& text)
{
Size size;
calculateGlyphsPositions(text, ALIGN_TOP_LEFT, &size);
return size;
}