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

195 lines
7.3 KiB
C++
Raw Normal View History

/*****************************************************************************
2018-06-15 14:07:34 +02:00
* Copyright (c) 2014-2018 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"
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", 9, 1, 1, 9, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "msgothic.ttc", "MS PGothic", 12, 1, 0, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "msgothic.ttc", "MS PGothic", 12, 1, 0, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
TTFFontSetDescriptor TTFFontHiragano = { {
{ u8"ヒラギノ丸ゴ ProN W4.ttc", "Hiragino Maru Gothic ProN", 9, 1, 1, 9, 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", 11, 1, 0, 13, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
2017-10-09 10:53:09 +02:00
TTFFontSetDescriptor TTFFontMingLiu = { {
{ "msjh.ttc", "JhengHei", 9, -1, -1, 9, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "mingliu.ttc", "MingLiU", 11, 1, 1, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "mingliu.ttc", "MingLiU", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
TTFFontSetDescriptor TTFFontHeiti = { {
{ u8"华文黑体.ttf", "STHeiti", 9, -1, -1, 9, HINTING_THRESHOLD_MEDIUM, nullptr },
{ u8"华文黑体.ttf", "STHeiti", 11, 1, 1, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ u8"华文黑体.ttf", "STHeiti", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
2017-10-09 10:53:09 +02:00
TTFFontSetDescriptor TTFFontSimSun = { {
{ "msyh.ttc", "YaHei", 9, -1, -1, 9, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "simsun.ttc", "SimSun", 11, 1, -1, 14, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "simsun.ttc", "SimSun", 12, 1, -2, 15, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
TTFFontSetDescriptor TTFFontLiHeiPro = { {
{ u8"儷黑 Pro.ttf", "LiHei Pro", 9, 1, -1, 9, HINTING_THRESHOLD_MEDIUM, nullptr },
{ 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 },
} };
2017-10-09 10:53:09 +02:00
TTFFontSetDescriptor TTFFontGulim = { {
{ "gulim.ttc", "Gulim", 10, 1, 0, 10, HINTING_DISABLED, nullptr },
{ "gulim.ttc", "Gulim", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr },
{ "gulim.ttc", "Gulim", 12, 1, 0, 15, HINTING_THRESHOLD_MEDIUM, nullptr },
} };
TTFFontSetDescriptor TTFFontNanum = { {
{ "NanumGothic.ttc", "Nanum Gothic", 10, 1, 0, 10, HINTING_DISABLED, nullptr },
{ "NanumGothic.ttc", "Nanum Gothic", 12, 1, 0, 15, HINTING_THRESHOLD_LOW, nullptr },
{ "NanumGothic.ttc", "Nanum Gothic", 12, 1, 0, 15, HINTING_THRESHOLD_LOW, nullptr },
} };
2017-10-09 10:53:09 +02:00
TTFFontSetDescriptor TTFFontArial = { {
{ "arial.ttf", "Arial", 9, 0, -1, 9, HINTING_THRESHOLD_LOW, nullptr },
{ "arial.ttf", "Arial", 10, 0, -1, 12, HINTING_THRESHOLD_LOW, nullptr },
{ "arial.ttf", "Arial", 11, 0, -1, 12, HINTING_THRESHOLD_LOW, nullptr },
} };
TTFFontSetDescriptor TTFFontArialUnicode = { {
{ "arialuni.ttf", "Arial Unicode MS", 9, 0, -1, 9, HINTING_THRESHOLD_LOW, nullptr },
{ "arialuni.ttf", "Arial Unicode MS", 10, 0, -1, 12, HINTING_THRESHOLD_LOW, nullptr },
{ "arialuni.ttf", "Arial Unicode MS", 11, 0, -1, 12, HINTING_THRESHOLD_LOW, 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)
{
ttf_dispose();
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;
ttf_dispose();
bool fontInitialised = ttf_initialise();
return fontInitialised;
}
2018-04-26 23:03:02 +01:00
static bool LoadCustomConfigFont(LocalisationService& localisationService)
{
static TTFFontSetDescriptor TTFFontCustom = { {
2018-06-22 22:59:46 +02:00
{ gConfigFonts.file_name,
gConfigFonts.font_name,
gConfigFonts.size_tiny,
gConfigFonts.x_offset,
gConfigFonts.y_offset,
gConfigFonts.height_tiny,
gConfigFonts.hinting_threshold,
nullptr },
{ gConfigFonts.file_name,
gConfigFonts.font_name,
gConfigFonts.size_small,
gConfigFonts.x_offset,
gConfigFonts.y_offset,
gConfigFonts.height_small,
gConfigFonts.hinting_threshold,
nullptr },
{ gConfigFonts.file_name,
gConfigFonts.font_name,
gConfigFonts.size_medium,
gConfigFonts.x_offset,
gConfigFonts.y_offset,
gConfigFonts.height_medium,
gConfigFonts.hinting_threshold,
nullptr },
} };
ttf_dispose();
2018-04-26 23:03:02 +01:00
localisationService.UseTrueTypeFont(true);
gCurrentTTFFontSet = &TTFFontCustom;
bool fontInitialised = ttf_initialise();
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 (!String::IsNullOrEmpty(gConfigFonts.file_name))
{
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;
}
2017-10-26 23:23:24 +02:00
TTFFontDescriptor smallFont = font->size[FONT_SIZE_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[FONT_SIZE_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);
}