1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/client/render/IFont.cpp

89 lines
2.5 KiB
C++
Raw Normal View History

2023-02-01 15:42:21 +02:00
/*
2023-02-01 16:42:03 +02:00
* IFont.cpp, part of VCMI engine
2023-02-01 15:42:21 +02:00
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "IFont.h"
2023-02-01 15:42:21 +02:00
#include "../gui/CGuiHandler.h"
#include "../render/IScreenHandler.h"
#include "../../lib/Point.h"
#include "../../lib/texts/TextOperations.h"
2023-02-01 15:42:21 +02:00
int IFont::getScalingFactor() const
{
return GH.screenHandler().getScalingFactor();
}
2023-02-01 15:42:21 +02:00
size_t IFont::getStringWidth(const std::string & data) const
{
size_t width = 0;
for(size_t i=0; i<data.size(); i += TextOperations::getUnicodeCharacterSize(data[i]))
2023-02-01 15:42:21 +02:00
{
width += getGlyphWidth(data.data() + i);
}
return width;
}
void IFont::renderTextLeft(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
2023-02-01 15:42:21 +02:00
{
renderText(surface, data, color, pos);
}
void IFont::renderTextRight(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
2023-02-01 15:42:21 +02:00
{
2024-08-17 22:54:29 +02:00
Point size = Point(getStringWidth(data), getLineHeight()) * getScalingFactor();
2023-02-01 15:42:21 +02:00
renderText(surface, data, color, pos - size);
}
void IFont::renderTextCenter(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
2023-02-01 15:42:21 +02:00
{
2024-08-17 22:54:29 +02:00
Point size = Point(getStringWidth(data), getLineHeight()) * getScalingFactor();
2023-02-01 15:42:21 +02:00
renderText(surface, data, color, pos - size / 2);
}
void IFont::renderTextLinesLeft(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
2023-02-01 15:42:21 +02:00
{
Point currPos = pos;
for(const std::string & line : data)
{
renderTextLeft(surface, line, color, currPos);
currPos.y += getLineHeight() * getScalingFactor();
2023-02-01 15:42:21 +02:00
}
}
void IFont::renderTextLinesRight(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
2023-02-01 15:42:21 +02:00
{
Point currPos = pos;
2024-08-17 22:54:29 +02:00
currPos.y -= data.size() * getLineHeight() * getScalingFactor();
2023-02-01 15:42:21 +02:00
for(const std::string & line : data)
{
renderTextRight(surface, line, color, currPos);
currPos.y += getLineHeight() * getScalingFactor();
2023-02-01 15:42:21 +02:00
}
}
void IFont::renderTextLinesCenter(SDL_Surface * surface, const std::vector<std::string> & data, const ColorRGBA & color, const Point & pos) const
2023-02-01 15:42:21 +02:00
{
Point currPos = pos;
2024-08-17 22:54:29 +02:00
currPos.y -= data.size() * getLineHeight() / 2 * getScalingFactor();
2023-02-01 15:42:21 +02:00
for(const std::string & line : data)
{
renderTextCenter(surface, line, color, currPos);
currPos.y += getLineHeight() * getScalingFactor();
2023-02-01 15:42:21 +02:00
}
}