Files
OpenRCT2-Web/src/openrct2/interface/Fonts.cpp
T

196 lines
8.2 KiB
C++
Raw Normal View History

/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-06-22 22:59:46 +02:00
#include "Fonts.h"
#include "../config/Config.h"
#include "../core/String.hpp"
2018-01-05 21:14:20 +00:00
#include "../drawing/TTF.h"
2018-01-06 18:32:25 +01:00
#include "../localisation/Language.h"
2018-04-26 23:03:02 +01:00
#include "../localisation/LocalisationService.h"
#include "../util/Util.h"
2018-06-22 22:59:46 +02:00
#include "FontFamilies.h"
2018-04-26 23:03:02 +01:00
using namespace OpenRCT2::Localisation;
2017-01-03 22:57:15 +01:00
#ifndef NO_TTF
2018-06-22 22:59:46 +02:00
uint8_t const HINTING_DISABLED = 0;
uint8_t const HINTING_THRESHOLD_LOW = 40;
uint8_t const HINTING_THRESHOLD_MEDIUM = 60;
2018-01-10 16:14:36 +01:00
// clang-format off
2017-10-09 10:53:09 +02:00
TTFFontSetDescriptor TTFFontMSGothic = { {
{ "msgothic.ttc", "MS PGothic", 12, 1, 0, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "msgothic.ttc", "MS PGothic", 12, 1, 0, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "msgothic.ttc", "MS PGothic", 9, 1, 1, 9, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
TTFFontSetDescriptor TTFFontNotoSansMono = { {
{ "NotoSansCJK-Regular.ttc", "Noto Sans Mono CJK JP", 12, 1, -3, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "NotoSansCJK-Regular.ttc", "Noto Sans Mono CJK JP", 12, 1, -3, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "NotoSansCJK-Regular.ttc", "Noto Sans Mono CJK JP", 9, 1, -2, 9, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
TTFFontSetDescriptor TTFFontHiragano = { {
{ u8"ヒラギノ丸ゴ ProN W4.ttc", "Hiragino Maru Gothic ProN", 11, 1, 0, 13, HINTING_THRESHOLD_MEDIUM, nullptr },
{ u8"ヒラギノ丸ゴ ProN W4.ttc", "Hiragino Maru Gothic ProN", 11, 1, 0, 13, HINTING_THRESHOLD_MEDIUM, nullptr },
{ u8"ヒラギノ丸ゴ ProN W4.ttc", "Hiragino Maru Gothic ProN", 9, 1, 1, 9, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
2017-10-09 10:53:09 +02:00
TTFFontSetDescriptor TTFFontMingLiu = { {
{ "mingliu.ttc", "MingLiU", 11, 1, 1, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "mingliu.ttc", "MingLiU", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "msjh.ttc", "JhengHei", 9, -1, -1, 9, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
TTFFontSetDescriptor TTFFontHeiti = { {
{ u8"华文黑体.ttf", "STHeiti", 11, 1, 1, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ u8"华文黑体.ttf", "STHeiti", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr },
{ u8"华文黑体.ttf", "STHeiti", 9, -1, -1, 9, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
2017-10-09 10:53:09 +02:00
TTFFontSetDescriptor TTFFontSimSun = { {
{ "simsun.ttc", "SimSun", 11, 1, -1, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "simsun.ttc", "SimSun", 12, 1, -2, 15, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "msyh.ttc", "Microsoft YaHei", 9, -1, -1, 9, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
TTFFontSetDescriptor TTFFontLiHeiPro = { {
{ u8"儷黑 Pro.ttf", "LiHei Pro", 11, 1, 0, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ u8"儷黑 Pro.ttf", "LiHei Pro", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr },
{ u8"儷黑 Pro.ttf", "LiHei Pro", 9, 1, -1, 9, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
2017-10-09 10:53:09 +02:00
TTFFontSetDescriptor TTFFontGulim = { {
{ "gulim.ttc", "Gulim", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "gulim.ttc", "Gulim", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "gulim.ttc", "Gulim", 10, 1, 0, 10, HINTING_DISABLED, nullptr },
} };
TTFFontSetDescriptor TTFFontMalgun = { {
{ "malgun.ttf", "Malgun Gothic", 12, 1, -3, 15, HINTING_THRESHOLD_LOW, nullptr },
{ "malgun.ttf", "Malgun Gothic", 12, 1, -3, 15, HINTING_THRESHOLD_LOW, nullptr },
{ "malgun.ttf", "Malgun Gothic", 10, 1, -3, 10, HINTING_THRESHOLD_LOW, nullptr },
} };
TTFFontSetDescriptor TTFFontNanum = { {
{ "NanumGothic.ttf", "Nanum Gothic", 12, 1, -2, 15, HINTING_THRESHOLD_LOW, nullptr },
{ "NanumGothic.ttf", "Nanum Gothic", 12, 1, -2, 15, HINTING_THRESHOLD_LOW, nullptr },
{ "NanumGothic.ttf", "Nanum Gothic", 10, 1, -2, 10, HINTING_DISABLED, nullptr },
} };
2017-10-09 10:53:09 +02:00
TTFFontSetDescriptor TTFFontArial = { {
{ "arial.ttf", "Arial", 12, 0, -1, 12, HINTING_THRESHOLD_LOW, nullptr },
{ "arial.ttf", "Arial", 12, 0, -1, 12, HINTING_THRESHOLD_LOW, nullptr },
2020-09-07 19:21:32 +02:00
{ "arial.ttf", "Arial", 10, 0, -1, 9, HINTING_THRESHOLD_LOW, nullptr },
} };
TTFFontSetDescriptor TTFFontArialUnicode = { {
{ "arialuni.ttf", "Arial Unicode MS", 12, 0, -1, 12, HINTING_THRESHOLD_LOW, nullptr },
{ "arialuni.ttf", "Arial Unicode MS", 12, 0, -1, 12, HINTING_THRESHOLD_LOW, nullptr },
2020-09-07 19:21:32 +02:00
{ "arialuni.ttf", "Arial Unicode MS", 10, 0, -1, 9, HINTING_THRESHOLD_LOW, nullptr },
} };
TTFFontSetDescriptor TTFFontMicroHei = { {
{ "wqy-microhei.ttc", "WenQuanYi Micro Hei", 11, 1, -1, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "wqy-microhei.ttc", "WenQuanYi Micro Hei", 11, 1, -2, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "wqy-microhei.ttc", "WenQuanYi Micro Hei", 10, -1, -1, 10, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
2018-01-10 16:14:36 +01:00
// clang-format on
2017-01-03 22:57:15 +01:00
#endif // NO_TTF
2018-04-26 23:03:02 +01:00
static void LoadSpriteFont(LocalisationService& localisationService)
{
TTFDispose();
2018-04-26 23:03:02 +01:00
localisationService.UseTrueTypeFont(false);
2017-01-04 13:51:12 +01:00
#ifndef NO_TTF
gCurrentTTFFontSet = nullptr;
2017-01-04 13:51:12 +01:00
#endif // NO_TTF
}
2017-01-03 22:57:15 +01:00
#ifndef NO_TTF
2018-06-22 22:59:46 +02:00
static bool LoadFont(LocalisationService& localisationService, TTFFontSetDescriptor* font)
{
2018-04-26 23:03:02 +01:00
localisationService.UseTrueTypeFont(true);
gCurrentTTFFontSet = font;
TTFDispose();
bool fontInitialised = TTFInitialise();
return fontInitialised;
}
2018-04-26 23:03:02 +01:00
static bool LoadCustomConfigFont(LocalisationService& localisationService)
{
static TTFFontSetDescriptor TTFFontCustom = { {
{ gConfigFonts.FileName.c_str(), gConfigFonts.FontName.c_str(), gConfigFonts.SizeTiny, gConfigFonts.OffsetX,
gConfigFonts.OffsetY, gConfigFonts.HeightTiny, gConfigFonts.HintingThreshold, nullptr },
{ gConfigFonts.FileName.c_str(), gConfigFonts.FontName.c_str(), gConfigFonts.SizeSmall, gConfigFonts.OffsetX,
gConfigFonts.OffsetY, gConfigFonts.HeightSmall, gConfigFonts.HintingThreshold, nullptr },
{ gConfigFonts.FileName.c_str(), gConfigFonts.FontName.c_str(), gConfigFonts.SizeMedium, gConfigFonts.OffsetX,
gConfigFonts.OffsetY, gConfigFonts.HeightMedium, gConfigFonts.HintingThreshold, nullptr },
} };
TTFDispose();
2018-04-26 23:03:02 +01:00
localisationService.UseTrueTypeFont(true);
gCurrentTTFFontSet = &TTFFontCustom;
bool fontInitialised = TTFInitialise();
return fontInitialised;
}
2017-01-03 22:57:15 +01:00
#endif // NO_TTF
2018-04-26 23:03:02 +01:00
void TryLoadFonts(LocalisationService& localisationService)
{
2017-01-03 22:57:15 +01:00
#ifndef NO_TTF
2018-04-26 23:48:25 +01:00
auto currentLanguage = localisationService.GetCurrentLanguage();
2018-06-22 22:59:46 +02:00
TTFontFamily const* fontFamily = LanguagesDescriptors[currentLanguage].font_family;
2017-10-26 23:23:24 +02:00
if (fontFamily != FAMILY_OPENRCT2_SPRITE)
{
if (!gConfigFonts.FileName.empty())
{
2018-04-26 23:03:02 +01:00
if (LoadCustomConfigFont(localisationService))
{
return;
}
2017-10-30 15:39:36 +01:00
log_verbose("Unable to initialise configured TrueType font -- falling back to the language's default.");
}
2018-06-22 22:59:46 +02:00
for (auto& font : *fontFamily)
{
2018-04-26 23:03:02 +01:00
if (LoadFont(localisationService, font))
2017-10-26 23:23:24 +02:00
{
return;
}
TTFFontDescriptor smallFont = font->size[EnumValue(FontStyle::Small)];
2017-10-30 15:39:36 +01:00
log_verbose("Unable to load TrueType font '%s' -- trying the next font in the family.", smallFont.font_name);
2017-10-26 23:23:24 +02:00
}
if (fontFamily != &TTFFamilySansSerif)
{
2017-10-30 15:39:36 +01:00
log_verbose("Unable to initialise any of the preferred TrueType fonts -- falling back to sans serif fonts.");
2017-10-26 23:23:24 +02:00
2018-06-22 22:59:46 +02:00
for (auto& font : TTFFamilySansSerif)
2017-10-26 23:23:24 +02:00
{
2018-04-26 23:03:02 +01:00
if (LoadFont(localisationService, font))
2017-10-26 23:23:24 +02:00
{
return;
}
TTFFontDescriptor smallFont = font->size[EnumValue(FontStyle::Small)];
2017-10-30 15:39:36 +01:00
log_verbose("Unable to load TrueType font '%s' -- trying the next font in the family.", smallFont.font_name);
2017-10-26 23:23:24 +02:00
}
2017-10-30 15:39:36 +01:00
log_verbose("Unable to initialise any of the preferred TrueType fonts -- falling back to sprite font.");
}
}
2017-01-03 22:57:15 +01:00
#endif // NO_TTF
2018-04-26 23:03:02 +01:00
LoadSpriteFont(localisationService);
}