2017-07-13 10:26:03 +02:00
|
|
|
/*
|
2023-02-01 16:42:03 +02:00
|
|
|
* CTrueTypeFont.cpp, part of VCMI engine
|
2017-07-13 10:26:03 +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
|
|
|
|
*
|
|
|
|
*/
|
2012-12-19 20:24:53 +03:00
|
|
|
#include "StdInc.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "CTrueTypeFont.h"
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2023-02-02 21:15:13 +02:00
|
|
|
#include "../render/Colors.h"
|
|
|
|
#include "../renderSDL/SDL_Extensions.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
|
2012-12-19 20:24:53 +03:00
|
|
|
#include "../../lib/JsonNode.h"
|
2023-02-12 23:00:56 +02:00
|
|
|
#include "../../lib/TextOperations.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
|
|
|
|
|
|
|
#include <SDL_ttf.h>
|
|
|
|
|
|
|
|
std::pair<std::unique_ptr<ui8[]>, ui64> CTrueTypeFont::loadData(const JsonNode & config)
|
|
|
|
{
|
|
|
|
std::string filename = "Data/" + config["file"].String();
|
|
|
|
return CResourceHandler::get()->load(ResourceID(filename, EResType::TTF_FONT))->readAll();
|
|
|
|
}
|
2012-12-19 20:24:53 +03:00
|
|
|
|
|
|
|
TTF_Font * CTrueTypeFont::loadFont(const JsonNode &config)
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
int pointSize = static_cast<int>(config["size"].Float());
|
2012-12-19 20:24:53 +03:00
|
|
|
|
|
|
|
if(!TTF_WasInit() && TTF_Init()==-1)
|
|
|
|
throw std::runtime_error(std::string("Failed to initialize true type support: ") + TTF_GetError() + "\n");
|
|
|
|
|
2020-10-01 10:38:06 +02:00
|
|
|
return TTF_OpenFontRW(SDL_RWFromConstMem(data.first.get(), (int)data.second), 1, pointSize);
|
2012-12-19 20:24:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CTrueTypeFont::getFontStyle(const JsonNode &config)
|
|
|
|
{
|
|
|
|
const JsonVector & names = config["style"].Vector();
|
|
|
|
int ret = 0;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const JsonNode & node : names)
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
|
|
|
if (node.String() == "bold")
|
|
|
|
ret |= TTF_STYLE_BOLD;
|
|
|
|
else if (node.String() == "italic")
|
|
|
|
ret |= TTF_STYLE_ITALIC;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CTrueTypeFont::CTrueTypeFont(const JsonNode & fontConfig):
|
2020-10-06 01:27:04 +02:00
|
|
|
data(loadData(fontConfig)),
|
|
|
|
font(loadFont(fontConfig), TTF_CloseFont),
|
|
|
|
blended(fontConfig["blend"].Bool())
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
|
|
|
assert(font);
|
|
|
|
|
|
|
|
TTF_SetFontStyle(font.get(), getFontStyle(fontConfig));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CTrueTypeFont::getLineHeight() const
|
|
|
|
{
|
|
|
|
return TTF_FontHeight(font.get());
|
|
|
|
}
|
|
|
|
|
2013-09-08 19:49:23 +03:00
|
|
|
size_t CTrueTypeFont::getGlyphWidth(const char *data) const
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
2023-02-12 23:52:35 +02:00
|
|
|
return getStringWidth(std::string(data, TextOperations::getUnicodeCharacterSize(*data)));
|
2013-10-26 00:45:14 +03:00
|
|
|
/*
|
2012-12-19 20:24:53 +03:00
|
|
|
int advance;
|
2013-09-08 19:49:23 +03:00
|
|
|
TTF_GlyphMetrics(font.get(), *data, nullptr, nullptr, nullptr, nullptr, &advance);
|
2012-12-19 20:24:53 +03:00
|
|
|
return advance;
|
2013-10-26 00:45:14 +03:00
|
|
|
*/
|
2012-12-19 20:24:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CTrueTypeFont::getStringWidth(const std::string & data) const
|
|
|
|
{
|
|
|
|
int width;
|
2013-10-26 00:45:14 +03:00
|
|
|
TTF_SizeUTF8(font.get(), data.c_str(), &width, nullptr);
|
2012-12-19 20:24:53 +03:00
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTrueTypeFont::renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const
|
|
|
|
{
|
2013-01-30 01:43:39 +03:00
|
|
|
if (color.r != 0 && color.g != 0 && color.b != 0) // not black - add shadow
|
2023-01-30 00:12:43 +02:00
|
|
|
renderText(surface, data, Colors::BLACK, pos + Point(1,1));
|
2013-01-30 01:43:39 +03:00
|
|
|
|
2012-12-19 20:24:53 +03:00
|
|
|
if (!data.empty())
|
|
|
|
{
|
|
|
|
SDL_Surface * rendered;
|
|
|
|
if (blended)
|
2013-09-21 21:29:26 +03:00
|
|
|
rendered = TTF_RenderUTF8_Blended(font.get(), data.c_str(), color);
|
2013-01-30 01:43:39 +03:00
|
|
|
else
|
2013-09-21 21:29:26 +03:00
|
|
|
rendered = TTF_RenderUTF8_Solid(font.get(), data.c_str(), color);
|
2012-12-19 20:24:53 +03:00
|
|
|
|
|
|
|
assert(rendered);
|
|
|
|
|
2023-01-17 22:01:35 +02:00
|
|
|
CSDL_Ext::blitSurface(rendered, surface, pos);
|
2012-12-19 20:24:53 +03:00
|
|
|
SDL_FreeSurface(rendered);
|
|
|
|
}
|
|
|
|
}
|
2013-09-08 19:49:23 +03:00
|
|
|
|