2010-11-21 22:48:58 +01:00
|
|
|
/* 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-10 17:37:15 +02:00
|
|
|
#include "core/resources.h"
|
2011-04-07 02:58:36 +02:00
|
|
|
#include "textures.h"
|
|
|
|
#include "graphics.h"
|
|
|
|
|
2011-04-10 00:55:58 +02:00
|
|
|
void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
|
|
|
|
{
|
|
|
|
int numHorizontalGlyphs = m_texture->getSize().width() / glyphSize.width();
|
|
|
|
uchar *texturePixels = m_texture->getPixels();
|
|
|
|
|
|
|
|
// small AI to auto calculate pixels widths
|
|
|
|
for(int glyph = 32; glyph< 256; ++glyph) {
|
|
|
|
Rect glyphCoords(((glyph - 32) % numHorizontalGlyphs) * glyphSize.width(),
|
|
|
|
((glyph - 32) / numHorizontalGlyphs) * glyphSize.height(),
|
|
|
|
glyphSize.width(),
|
|
|
|
m_glyphHeight);
|
|
|
|
int width = glyphSize.width();
|
|
|
|
for(int x = glyphCoords.left() + 2; x <= glyphCoords.right(); ++x) {
|
|
|
|
bool allAlpha = true;
|
|
|
|
|
|
|
|
// check if all vertical pixels are alpha
|
|
|
|
for(int y = glyphCoords.top(); y <= glyphCoords.bottom(); ++y) {
|
|
|
|
if(texturePixels[(y * m_texture->getSize().width() * 4) + (x*4) + 3] != 0) {
|
|
|
|
allAlpha = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if all pixels were alpha we found the width
|
|
|
|
if(allAlpha) {
|
|
|
|
width = x - glyphCoords.left();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// store glyph size
|
2011-04-10 02:51:35 +02:00
|
|
|
m_glyphsSize[glyph].setSize(width, m_glyphHeight);
|
2011-04-10 00:55:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
delete[] texturePixels;
|
|
|
|
}
|
|
|
|
|
2011-04-06 21:46:58 +02:00
|
|
|
bool Font::load(const std::string& file)
|
2010-11-21 22:48:58 +01:00
|
|
|
{
|
2011-04-07 02:58:36 +02:00
|
|
|
std::string fileContents = g_resources.loadTextFile(file);
|
|
|
|
if(!fileContents.size()) {
|
2011-04-10 22:40:44 +02:00
|
|
|
logError("Coult not load 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
|
|
|
Size glyphSize;
|
2011-04-07 02:58:36 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
YAML::Parser parser(fin);
|
|
|
|
|
|
|
|
YAML::Node doc;
|
|
|
|
parser.GetNextDocument(doc);
|
|
|
|
|
2011-04-10 22:40:44 +02:00
|
|
|
// required values
|
2011-04-10 00:55:58 +02:00
|
|
|
doc["glyph height"] >> m_glyphHeight;
|
2011-04-07 09:30:32 +02:00
|
|
|
doc["image glyph size"] >> glyphSize;
|
2011-04-07 02:58:36 +02:00
|
|
|
doc["image"] >> textureName;
|
2011-04-07 09:30:32 +02:00
|
|
|
|
2011-04-10 22:40:44 +02:00
|
|
|
// optional values
|
|
|
|
if(doc.FindValue("glyph spacing"))
|
|
|
|
doc["glyph spacing"] >> m_glyphSpacing;
|
|
|
|
if(doc.FindValue("top margin"))
|
|
|
|
doc["top margin"] >> m_topMargin;
|
|
|
|
|
|
|
|
// load texture
|
2011-04-07 09:30:32 +02:00
|
|
|
m_texture = g_textures.get("fonts/" + textureName);
|
|
|
|
if(!m_texture) {
|
2011-04-10 22:40:44 +02:00
|
|
|
logError("Failed to load image for font file \"%s\"", file.c_str());
|
2011-04-07 09:30:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-10 22:40:44 +02:00
|
|
|
// auto calculate widths
|
2011-04-10 00:55:58 +02:00
|
|
|
calculateGlyphsWidthsAutomatically(glyphSize);
|
2011-04-07 09:30:32 +02:00
|
|
|
|
2011-04-10 00:55:58 +02:00
|
|
|
// read custom widths
|
|
|
|
if(doc.FindValue("glyph widths")) {
|
|
|
|
const YAML::Node& widthsNode = doc["glyph widths"];
|
|
|
|
for(auto it = widthsNode.begin(); it != widthsNode.end(); ++it) {
|
|
|
|
int glyph, glyphWidth;
|
|
|
|
it.first() >> glyph;
|
|
|
|
it.second() >> glyphWidth;
|
|
|
|
m_glyphsSize[glyph].setWidth(glyphWidth);
|
|
|
|
}
|
|
|
|
}
|
2011-04-07 09:30:32 +02:00
|
|
|
|
2011-04-10 00:55:58 +02:00
|
|
|
// calculate glyphs texture coords
|
|
|
|
int numHorizontalGlyphs = m_texture->getSize().width() / glyphSize.width();
|
|
|
|
for(int glyph = 32; glyph< 256; ++glyph) {
|
|
|
|
m_glyphsTextureCoords[glyph].setRect(((glyph - 32) % numHorizontalGlyphs) * glyphSize.width(),
|
2011-04-10 22:40:44 +02:00
|
|
|
((glyph - 32) / numHorizontalGlyphs) * glyphSize.height(),
|
|
|
|
m_glyphsSize[glyph].width(),
|
|
|
|
m_glyphHeight);
|
2011-04-07 02:58:36 +02:00
|
|
|
}
|
2011-04-10 22:40:44 +02:00
|
|
|
} catch (YAML::Exception& e) {
|
|
|
|
logError("Malformed font file \"%s\":\n %s", file.c_str(), e.what());
|
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,
|
2011-04-11 22:06:03 +02:00
|
|
|
const Point& startPos,
|
|
|
|
const Color& color)
|
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);
|
2011-04-11 22:06:03 +02:00
|
|
|
Font::renderText(text, screenCoords, ALIGN_TOP_LEFT, color);
|
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,
|
2011-04-10 00:55:58 +02:00
|
|
|
const Color& color,
|
2011-04-11 06:08:56 +02:00
|
|
|
const Point& startInternalPos,
|
|
|
|
int cursorPos,
|
|
|
|
const Color& cursorColor)
|
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 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-11 22:06:03 +02:00
|
|
|
int glyph = (uchar)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
|
2011-04-10 22:40:44 +02:00
|
|
|
g_graphics.drawTexturedRect(glyphScreenCoords, m_texture, glyphTextureCoords, color);
|
2011-04-11 06:08:56 +02:00
|
|
|
|
|
|
|
// render cursor
|
|
|
|
if(i == cursorPos) {
|
|
|
|
Rect cursorRect(glyphScreenCoords.left()-1, glyphScreenCoords.top(), 1, m_glyphHeight);
|
|
|
|
g_graphics.drawFilledRect(cursorRect, cursorColor);
|
|
|
|
}
|
|
|
|
// render cursor after last element
|
|
|
|
else if(cursorPos == textLenght && i == textLenght - 1) {
|
|
|
|
Rect cursorRect(glyphScreenCoords.right()+1, glyphScreenCoords.top(), 1, m_glyphHeight);
|
|
|
|
g_graphics.drawFilledRect(cursorRect, cursorColor);
|
|
|
|
}
|
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)
|
2011-04-10 02:51:35 +02:00
|
|
|
logFatal("could not calculate glyphs positions, text length is > 8192!");
|
2011-04-07 20:49:35 +02:00
|
|
|
|
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) {
|
2011-04-11 22:06:03 +02:00
|
|
|
glyph = (uchar)text[i];
|
2011-04-07 22:36:40 +02:00
|
|
|
|
|
|
|
if(glyph == (uchar)'\n') {
|
|
|
|
lineWidths[++lines] = 0;
|
|
|
|
} else if(glyph >= 32) {
|
2011-04-10 00:55:58 +02:00
|
|
|
lineWidths[lines] += m_glyphsSize[glyph].width() + m_glyphSpacing.width();
|
2011-04-07 22:36:40 +02:00
|
|
|
maxLineWidth = std::max(maxLineWidth, lineWidths[lines]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-10 00:55:58 +02:00
|
|
|
Point virtualPos(0, m_topMargin);
|
2011-04-07 22:36:40 +02:00
|
|
|
lines = 0;
|
|
|
|
for(i = 0; i < numGlyphs; ++i) {
|
2011-04-11 22:06:03 +02:00
|
|
|
glyph = (uchar)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') {
|
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 & 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-10 00:55:58 +02: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);
|
2011-04-10 00:55:58 +02:00
|
|
|
textBoxSize->setHeight(virtualPos.y + m_glyphHeight);
|
2011-04-07 22:36:40 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2011-04-08 07:10:00 +02:00
|
|
|
Size Font::calculateTextRectSize(const std::string& text)
|
2011-04-07 22:36:40 +02:00
|
|
|
{
|
|
|
|
Size size;
|
|
|
|
calculateGlyphsPositions(text, ALIGN_TOP_LEFT, &size);
|
|
|
|
return size;
|
|
|
|
}
|