2011-07-13 23:12:36 +02:00
|
|
|
#include <global.h>
|
2011-04-17 21:14:24 +02:00
|
|
|
#include <core/resources.h>
|
|
|
|
#include <graphics/font.h>
|
|
|
|
#include <graphics/textures.h>
|
|
|
|
#include <graphics/graphics.h>
|
2011-07-13 23:12:36 +02:00
|
|
|
#include <otml/otml.h>
|
2011-04-07 02:58:36 +02:00
|
|
|
|
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
|
2011-05-12 00:16:11 +02:00
|
|
|
for(int glyph = m_firstGlyph; glyph< 256; ++glyph) {
|
|
|
|
Rect glyphCoords(((glyph - m_firstGlyph) % numHorizontalGlyphs) * glyphSize.width(),
|
|
|
|
((glyph - m_firstGlyph) / numHorizontalGlyphs) * glyphSize.height(),
|
2011-04-10 00:55:58 +02:00
|
|
|
glyphSize.width(),
|
|
|
|
m_glyphHeight);
|
|
|
|
int width = glyphSize.width();
|
2011-05-13 01:24:57 +02:00
|
|
|
int lastColumnFilledPixels = 0;
|
|
|
|
for(int x = glyphCoords.left() + 1; x <= glyphCoords.right(); ++x) {
|
|
|
|
int columnFilledPixels = 0;
|
2011-04-10 00:55:58 +02:00
|
|
|
|
|
|
|
// check if all vertical pixels are alpha
|
|
|
|
for(int y = glyphCoords.top(); y <= glyphCoords.bottom(); ++y) {
|
2011-05-13 01:24:57 +02:00
|
|
|
if(texturePixels[(y * m_texture->getSize().width() * 4) + (x*4) + 3] != 0)
|
|
|
|
columnFilledPixels++;
|
2011-04-10 00:55:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// if all pixels were alpha we found the width
|
2011-05-13 01:24:57 +02:00
|
|
|
if(columnFilledPixels == 0) {
|
2011-04-10 00:55:58 +02:00
|
|
|
width = x - glyphCoords.left();
|
2011-07-27 01:13:27 +02:00
|
|
|
width += m_glyphSpacing.width();
|
2011-05-13 01:24:57 +02:00
|
|
|
if(m_glyphHeight >= 16 && lastColumnFilledPixels >= m_glyphHeight/3)
|
|
|
|
width += 1;
|
2011-04-10 00:55:58 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-05-13 01:24:57 +02:00
|
|
|
lastColumnFilledPixels = columnFilledPixels;
|
2011-04-10 00:55:58 +02:00
|
|
|
}
|
|
|
|
// 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-05-19 19:11:05 +02:00
|
|
|
std::stringstream fin;
|
|
|
|
if(!g_resources.loadFile(file, fin)) {
|
2011-07-17 13:52:20 +02:00
|
|
|
logError("ERROR: Coult not load font file '",file,"'");
|
2011-04-07 02:58:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string textureName;
|
2011-04-07 09:30:32 +02:00
|
|
|
Size glyphSize;
|
2011-04-07 02:58:36 +02:00
|
|
|
|
2011-05-22 00:24:10 +02:00
|
|
|
try {
|
2011-07-13 23:12:36 +02:00
|
|
|
OTMLParser parser(fin, file);
|
|
|
|
OTMLNode* doc = parser.getDocument();
|
2011-04-07 02:58:36 +02:00
|
|
|
|
2011-04-10 22:40:44 +02:00
|
|
|
// required values
|
2011-05-21 20:15:46 +02:00
|
|
|
textureName = doc->valueAt("image");
|
2011-07-27 01:13:27 +02:00
|
|
|
glyphSize = doc->readAt("image-glyph-size", Size(16, 16));
|
|
|
|
m_glyphHeight = doc->readAt("glyph-height", 11);
|
|
|
|
m_firstGlyph = doc->readAt("first-glyph", 32);
|
|
|
|
m_topMargin = doc->readAt("top-margin", 0);
|
|
|
|
m_glyphSpacing = doc->readAt("glyph-spacing", Size(0,0));
|
2011-04-10 22:40:44 +02:00
|
|
|
|
|
|
|
// load texture
|
2011-05-19 19:11:05 +02:00
|
|
|
m_texture = g_textures.get(textureName);
|
2011-04-07 09:30:32 +02:00
|
|
|
if(!m_texture) {
|
2011-07-17 13:52:20 +02:00
|
|
|
logError("ERROR: Failed to load image for font file '",file,"'");
|
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
|
2011-07-27 01:13:27 +02:00
|
|
|
if(doc->hasChild("glyph-widths")) {
|
2011-05-21 20:15:46 +02:00
|
|
|
std::map<int, int> glyphWidths;
|
2011-07-27 01:13:27 +02:00
|
|
|
doc->readAt("glyph-widths", &glyphWidths);
|
2011-05-21 20:15:46 +02:00
|
|
|
foreach(const auto& pair, glyphWidths)
|
|
|
|
m_glyphsSize[pair.first].setWidth(pair.second);
|
|
|
|
}
|
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();
|
2011-07-27 01:13:27 +02:00
|
|
|
for(int glyph = m_firstGlyph; glyph < 256; ++glyph) {
|
2011-05-12 00:16:11 +02:00
|
|
|
m_glyphsTextureCoords[glyph].setRect(((glyph - m_firstGlyph) % numHorizontalGlyphs) * glyphSize.width(),
|
|
|
|
((glyph - m_firstGlyph) / numHorizontalGlyphs) * glyphSize.height(),
|
2011-04-10 22:40:44 +02:00
|
|
|
m_glyphsSize[glyph].width(),
|
|
|
|
m_glyphHeight);
|
2011-04-07 02:58:36 +02:00
|
|
|
}
|
2011-07-13 23:12:36 +02:00
|
|
|
} catch(OTMLException e) {
|
2011-07-17 13:52:20 +02:00
|
|
|
logError("ERROR: Malformed font file \"", file.c_str(), "\":\n ", 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-05-12 00:16:11 +02:00
|
|
|
renderText(text, screenCoords, AlignTopLeft, color);
|
2011-04-07 20:49:35 +02:00
|
|
|
}
|
|
|
|
|
2011-04-15 04:13:53 +02:00
|
|
|
|
2011-04-07 22:36:40 +02:00
|
|
|
void Font::renderText(const std::string& text,
|
|
|
|
const Rect& screenCoords,
|
2011-05-12 00:16:11 +02:00
|
|
|
AlignmentFlag align,
|
2011-04-15 04:13:53 +02:00
|
|
|
const Color& color)
|
2011-04-07 02:58:36 +02:00
|
|
|
{
|
2011-04-16 18:08:55 +02:00
|
|
|
// prevent glitches from invalid rects
|
|
|
|
if(!screenCoords.isValid())
|
|
|
|
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
|
2011-05-12 00:16:11 +02:00
|
|
|
if(align & AlignBottom) {
|
2011-04-16 18:08:55 +02:00
|
|
|
glyphScreenCoords.translate(0, screenCoords.height() - textBoxSize.height());
|
2011-05-12 00:16:11 +02:00
|
|
|
} else if(align & 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
|
|
|
|
}
|
|
|
|
|
2011-05-12 00:16:11 +02:00
|
|
|
if(align & AlignRight) {
|
2011-04-16 18:08:55 +02:00
|
|
|
glyphScreenCoords.translate(screenCoords.width() - textBoxSize.width(), 0);
|
2011-05-12 00:16:11 +02:00
|
|
|
} else if(align & 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
|
|
|
|
g_graphics.drawTexturedRect(glyphScreenCoords, m_texture, glyphTextureCoords, color);
|
|
|
|
}
|
2011-04-07 02:58:36 +02:00
|
|
|
}
|
|
|
|
|
2011-05-12 00:16:11 +02:00
|
|
|
const std::vector<Point>& Font::calculateGlyphsPositions(const std::string& text, AlignmentFlag align, Size *textBoxSize) const
|
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-05-13 01:24:57 +02:00
|
|
|
textBoxSize->setSize(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
|
2011-05-12 00:16:11 +02:00
|
|
|
if((align & AlignRight || align & 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-07-27 01:13:27 +02:00
|
|
|
lineWidths[lines] += m_glyphsSize[glyph].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;
|
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
|
2011-05-12 00:16:11 +02:00
|
|
|
if(align & AlignRight) {
|
2011-04-07 22:36:40 +02:00
|
|
|
virtualPos.x = (maxLineWidth - lineWidths[lines]);
|
2011-05-12 00:16:11 +02:00
|
|
|
} else if(align & 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-07-27 01:13:27 +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);
|
2011-04-19 04:10:08 +02:00
|
|
|
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
|
|
|
|
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;
|
2011-05-12 00:16:11 +02:00
|
|
|
calculateGlyphsPositions(text, AlignTopLeft, &size);
|
2011-04-07 22:36:40 +02:00
|
|
|
return size;
|
|
|
|
}
|
2011-04-15 04:13:53 +02:00
|
|
|
|