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-04-04 21:23:32 +02:00
|
|
|
#include "CBitmapFont.h"
|
|
|
|
|
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
|
|
|
|
2024-09-24 12:41:39 +02:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
2024-02-11 23:09:01 +02:00
|
|
|
#include "../../lib/json/JsonNode.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../../lib/texts/TextOperations.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
|
|
|
|
#include <SDL_ttf.h>
|
|
|
|
|
|
|
|
std::pair<std::unique_ptr<ui8[]>, ui64> CTrueTypeFont::loadData(const JsonNode & config)
|
|
|
|
{
|
|
|
|
std::string filename = "Data/" + config["file"].String();
|
2023-08-23 14:07:50 +02:00
|
|
|
return CResourceHandler::get()->load(ResourcePath(filename, EResType::TTF_FONT))->readAll();
|
2023-02-01 20:42:06 +02:00
|
|
|
}
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2024-09-03 11:26:54 +02:00
|
|
|
int CTrueTypeFont::getPointSize(const JsonNode & config) const
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
2024-09-24 12:41:39 +02:00
|
|
|
float fontScale = settings["video"]["fontScalingFactor"].Float();
|
2024-07-22 11:52:33 +02:00
|
|
|
int scalingFactor = getScalingFactor();
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2024-09-03 11:26:54 +02:00
|
|
|
if (config.isNumber())
|
2024-09-24 12:41:39 +02:00
|
|
|
return std::round(config.Integer() * scalingFactor * fontScale);
|
2024-09-03 11:26:54 +02:00
|
|
|
else
|
2024-09-24 12:41:39 +02:00
|
|
|
return std::round(config[scalingFactor-1].Integer() * fontScale);
|
2024-09-03 11:26:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TTF_Font * CTrueTypeFont::loadFont(const JsonNode &config)
|
|
|
|
{
|
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");
|
|
|
|
|
2024-09-03 11:26:54 +02:00
|
|
|
return TTF_OpenFontRW(SDL_RWFromConstMem(data.first.get(), data.second), 1, getPointSize(config["size"]));
|
2012-12-19 20:24:53 +03:00
|
|
|
}
|
|
|
|
|
2024-09-03 11:26:54 +02:00
|
|
|
int CTrueTypeFont::getFontStyle(const JsonNode &config) const
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
|
|
|
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),
|
2024-10-10 22:31:11 +02:00
|
|
|
blended(true),
|
2024-09-23 20:15:30 +02:00
|
|
|
outline(fontConfig["outline"].Bool()),
|
2024-10-10 22:31:11 +02:00
|
|
|
dropShadow(!fontConfig["noShadow"].Bool())
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
|
|
|
assert(font);
|
|
|
|
|
|
|
|
TTF_SetFontStyle(font.get(), getFontStyle(fontConfig));
|
2024-09-23 20:15:30 +02:00
|
|
|
TTF_SetFontHinting(font.get(),TTF_HINTING_MONO);
|
2023-04-04 21:23:32 +02:00
|
|
|
|
2024-09-28 21:30:26 +02:00
|
|
|
logGlobal->debug("Loaded TTF font: '%s', point size %d, height %d, ascent %d, descent %d, line skip %d",
|
|
|
|
fontConfig["file"].String(),
|
|
|
|
getPointSize(fontConfig["size"]),
|
|
|
|
TTF_FontHeight(font.get()),
|
|
|
|
TTF_FontAscent(font.get()),
|
|
|
|
TTF_FontDescent(font.get()),
|
|
|
|
TTF_FontLineSkip(font.get())
|
|
|
|
);
|
2012-12-19 20:24:53 +03:00
|
|
|
}
|
|
|
|
|
2023-04-04 21:23:32 +02:00
|
|
|
CTrueTypeFont::~CTrueTypeFont() = default;
|
|
|
|
|
2024-09-24 12:41:39 +02:00
|
|
|
size_t CTrueTypeFont::getFontAscentScaled() const
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
2024-09-24 12:41:39 +02:00
|
|
|
return TTF_FontAscent(font.get());
|
2012-12-19 20:24:53 +03:00
|
|
|
}
|
|
|
|
|
2024-09-24 12:41:39 +02:00
|
|
|
size_t CTrueTypeFont::getLineHeightScaled() const
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
2024-09-24 12:41:39 +02:00
|
|
|
return TTF_FontHeight(font.get());
|
|
|
|
}
|
2023-04-04 21:23:32 +02:00
|
|
|
|
2024-10-10 22:31:11 +02:00
|
|
|
size_t CTrueTypeFont::getGlyphWidthScaled(const char *text) const
|
2024-09-24 12:41:39 +02:00
|
|
|
{
|
2024-10-10 22:31:11 +02:00
|
|
|
return getStringWidthScaled(std::string(text, TextOperations::getUnicodeCharacterSize(*text)));
|
2012-12-19 20:24:53 +03:00
|
|
|
}
|
|
|
|
|
2024-10-10 22:31:11 +02:00
|
|
|
bool CTrueTypeFont::canRepresentCharacter(const char * text) const
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
2024-10-10 22:31:11 +02:00
|
|
|
uint32_t codepoint = TextOperations::getUnicodeCodepoint(text, TextOperations::getUnicodeCharacterSize(*text));
|
2024-09-24 15:30:54 +02:00
|
|
|
#if SDL_TTF_VERSION_ATLEAST(2, 0, 18)
|
|
|
|
return TTF_GlyphIsProvided32(font.get(), codepoint);
|
|
|
|
#elif SDL_TTF_VERSION_ATLEAST(2, 0, 12)
|
|
|
|
if (codepoint <= 0xffff)
|
|
|
|
return TTF_GlyphIsProvided(font.get(), codepoint);
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
2024-09-24 12:41:39 +02:00
|
|
|
}
|
2023-04-04 21:23:32 +02:00
|
|
|
|
2024-10-10 22:31:11 +02:00
|
|
|
size_t CTrueTypeFont::getStringWidthScaled(const std::string & text) const
|
2024-09-24 12:41:39 +02:00
|
|
|
{
|
2012-12-19 20:24:53 +03:00
|
|
|
int width;
|
2024-10-10 22:31:11 +02:00
|
|
|
TTF_SizeUTF8(font.get(), text.c_str(), &width, nullptr);
|
2024-11-30 01:41:09 +02:00
|
|
|
|
|
|
|
if (outline)
|
|
|
|
width += getScalingFactor();
|
|
|
|
if (dropShadow || outline)
|
|
|
|
width += getScalingFactor();
|
|
|
|
|
2024-09-24 12:41:39 +02:00
|
|
|
return width;
|
2012-12-19 20:24:53 +03:00
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
void CTrueTypeFont::renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
2024-09-23 20:15:30 +02:00
|
|
|
if (color.r != 0 && color.g != 0 && color.b != 0) // not black - add shadow
|
|
|
|
{
|
|
|
|
if (outline)
|
|
|
|
renderText(surface, data, Colors::BLACK, pos - Point(1,1) * getScalingFactor());
|
|
|
|
|
|
|
|
if (dropShadow || outline)
|
|
|
|
renderText(surface, data, Colors::BLACK, pos + Point(1,1) * getScalingFactor());
|
|
|
|
}
|
2013-01-30 01:43:39 +03:00
|
|
|
|
2012-12-19 20:24:53 +03:00
|
|
|
if (!data.empty())
|
|
|
|
{
|
|
|
|
SDL_Surface * rendered;
|
|
|
|
if (blended)
|
2023-07-31 18:50:55 +02:00
|
|
|
rendered = TTF_RenderUTF8_Blended(font.get(), data.c_str(), CSDL_Ext::toSDL(color));
|
2013-01-30 01:43:39 +03:00
|
|
|
else
|
2023-07-31 18:50:55 +02:00
|
|
|
rendered = TTF_RenderUTF8_Solid(font.get(), data.c_str(), CSDL_Ext::toSDL(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
|
|
|
|