10#include "../../stdafx.h"
11#include "../../debug.h"
12#include "../../blitter/factory.hpp"
13#include "../../core/math_func.hpp"
14#include "../../core/mem_func.hpp"
15#include "../../error_func.h"
16#include "../../fileio_func.h"
17#include "../../fontcache.h"
18#include "../../fontcache/truetypefontcache.h"
19#include "../../fontdetection.h"
20#include "../../library_loader.h"
21#include "../../string_func.h"
22#include "../../strings_func.h"
23#include "../../zoom_func.h"
26#include "../../table/control_codes.h"
37 LOCALESIGNATURE locale;
39 std::vector<std::wstring> fonts;
41 bool Add(
const std::wstring_view &font)
43 for (
const auto &entry : this->fonts) {
44 if (font.compare(entry) == 0)
return false;
47 this->fonts.emplace_back(font);
53static int CALLBACK EnumFontCallback(
const ENUMLOGFONTEX *logfont,
const NEWTEXTMETRICEX *metric, DWORD type, LPARAM lParam)
58 if (!info->Add(logfont->elfFullName))
return 1;
60 if (!(type & TRUETYPE_FONTTYPE))
return 1;
62 if (logfont->elfLogFont.lfCharSet == SYMBOL_CHARSET)
return 1;
64 if (info->callback->
Monospace() && (logfont->elfLogFont.lfPitchAndFamily & (FF_MODERN | FIXED_PITCH)) != (FF_MODERN | FIXED_PITCH))
return 1;
67 auto check_bitfields = [&]() {
69 for (uint8_t i = 0; i < 4; i++) {
70 if ((metric->ntmFontSig.fsUsb[i] & info->locale.lsUsb[i]) != 0)
return true;
73 for (uint8_t i = 0; i < 2; i++) {
74 if ((metric->ntmFontSig.fsCsb[i] & info->locale.lsCsbSupported[i]) != 0)
return true;
78 if (!check_bitfields())
return 1;
80 char font_name[MAX_PATH];
83 info->callback->
SetFontNames(info->settings, font_name, &logfont->elfLogFont);
85 Debug(fontcache, 1,
"Fallback font: {}", font_name);
91 Debug(fontcache, 1,
"Trying fallback fonts");
93 std::wstring lang =
OTTD2FS(language_isocode.substr(0, language_isocode.find(
'_')));
94 if (GetLocaleInfoEx(lang.c_str(), LOCALE_FONTSIGNATURE,
reinterpret_cast<LPWSTR
>(&langInfo.locale),
sizeof(langInfo.locale) /
sizeof(
wchar_t)) == 0) {
96 Debug(fontcache, 1,
"Can't get locale info for fallback font (isocode={})", language_isocode);
100 langInfo.callback = callback;
104 font.lfCharSet = DEFAULT_CHARSET;
105 font.lfFaceName[0] =
'\0';
106 font.lfPitchAndFamily = 0;
108 HDC dc = GetDC(
nullptr);
109 int ret = EnumFontFamiliesEx(dc, &font, (FONTENUMPROC)&EnumFontCallback, (LPARAM)&langInfo, 0);
110 ReleaseDC(
nullptr, dc);
123 this->
dc = CreateCompatibleDC(
nullptr);
124 this->SetFontSize(pixels);
127Win32FontCache::~Win32FontCache()
131 DeleteObject(this->
font);
134void Win32FontCache::SetFontSize(
int pixels)
138 int scaled_height =
ScaleGUITrad(FontCache::GetDefaultFontHeight(this->
fs));
139 pixels = scaled_height;
141 HFONT temp = CreateFontIndirect(&this->logfont);
142 if (temp !=
nullptr) {
143 HGDIOBJ old = SelectObject(this->
dc, temp);
145 UINT size = GetOutlineTextMetrics(this->
dc, 0,
nullptr);
146 LPOUTLINETEXTMETRIC otm = (LPOUTLINETEXTMETRIC)
new BYTE[size];
147 GetOutlineTextMetrics(this->
dc, size, otm);
156 SelectObject(
dc, old);
165 this->logfont.lfHeight = -pixels;
166 this->logfont.lfWidth = 0;
167 this->logfont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
168 this->logfont.lfQuality = ANTIALIASED_QUALITY;
170 if (this->
font !=
nullptr) {
172 DeleteObject(this->
font);
174 this->
font = CreateFontIndirect(&this->logfont);
178 UINT otmSize = GetOutlineTextMetrics(this->
dc, 0,
nullptr);
179 POUTLINETEXTMETRIC otm = (POUTLINETEXTMETRIC)
new BYTE[otmSize];
180 GetOutlineTextMetrics(this->
dc, otmSize, otm);
182 this->
ascender = otm->otmTextMetrics.tmAscent;
183 this->
descender = otm->otmTextMetrics.tmDescent;
185 this->
glyph_size.cx = otm->otmTextMetrics.tmMaxCharWidth;
186 this->
glyph_size.cy = otm->otmTextMetrics.tmHeight;
188 this->
fontname =
FS2OTTD((LPWSTR)((BYTE *)otm + (ptrdiff_t)otm->otmpFaceName));
190 Debug(fontcache, 2,
"Loaded font '{}' with size {}", this->
fontname, pixels);
200 if (this->
font !=
nullptr) this->SetFontSize(this->
req_size);
205 const Sprite *Win32FontCache::InternalGetGlyph(
GlyphID key,
bool aa)
208 MAT2 mat = { {0, 1}, {0, 0}, {0, 0}, {0, 1} };
211 DWORD size = GetGlyphOutline(this->
dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, 0, nullptr, &mat);
212 if (size == GDI_ERROR) UserError(
"Unable to render font glyph");
216 uint width = std::max(1U, (uint)gm.gmBlackBoxX + shadow);
217 uint
height = std::max(1U, (uint)gm.gmBlackBoxY + shadow);
224 GetGlyphOutline(this->
dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, size, bmp, &mat);
232 sprite.
width = width;
234 sprite.
x_offs = gm.gmptGlyphOrigin.x;
243 uint pitch =
Align(aa ? gm.gmBlackBoxX : std::max((gm.gmBlackBoxX + 7u) / 8u, 1u), 4);
247 for (uint y = 0; y < gm.gmBlackBoxY; y++) {
248 for (uint x = 0; x < gm.gmBlackBoxX; x++) {
249 if (aa ? (bmp[x + y * pitch] > 0) :
HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
250 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
m = SHADOW_COLOUR;
251 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
257 for (uint y = 0; y < gm.gmBlackBoxY; y++) {
258 for (uint x = 0; x < gm.gmBlackBoxX; x++) {
259 if (aa ? (bmp[x + y * pitch] > 0) :
HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
260 sprite.
data[x + y * sprite.
width].
m = FACE_COLOUR;
261 sprite.
data[x + y * sprite.
width].
a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
270 GlyphEntry new_glyph;
271 new_glyph.
data = std::move(allocator.data);
272 new_glyph.width = gm.gmCellIncX;
274 return this->SetGlyphPtr(key, std::move(new_glyph)).GetSprite();
279 assert(IsPrintable(key));
283 if (key >= 0x010000U) {
284 chars[0] = (wchar_t)(((key - 0x010000U) >> 10) + 0xD800);
285 chars[1] = (wchar_t)(((key - 0x010000U) & 0x3FF) + 0xDC00);
287 chars[0] = (wchar_t)(key & 0xFFFF);
290 WORD glyphs[2] = { 0, 0 };
291 GetGlyphIndicesW(this->
dc, chars, key >= 0x010000U ? 2 : 1, glyphs, GGI_MARK_NONEXISTING_GLYPHS);
293 if (glyphs[0] != 0xFFFF)
return glyphs[0];
294 return allow_fallback && key >= SCC_SPRITE_START && key <= SCC_SPRITE_END ? this->
parent->
MapCharToGlyph(key) : 0;
298static bool TryLoadFontFromFile(
const std::string &font_name, LOGFONT &logfont)
300 wchar_t fontPath[MAX_PATH] = {};
308 if (!full_font.empty()) {
313 if (fontPath[0] != 0) {
314 if (AddFontResourceEx(fontPath, FR_PRIVATE, 0) != 0) {
318 typedef BOOL(WINAPI *PFNGETFONTRESOURCEINFO)(LPCTSTR, LPDWORD, LPVOID, DWORD);
319 static PFNGETFONTRESOURCEINFO GetFontResourceInfo = _gdi32.GetFunction(
"GetFontResourceInfoW");
321 if (GetFontResourceInfo !=
nullptr) {
324 if (GetFontResourceInfo(fontPath, &len,
nullptr, 2) && len >=
sizeof(LOGFONT)) {
325 LOGFONT *buf = (LOGFONT *)
new uint8_t[len];
326 if (GetFontResourceInfo(fontPath, &len, buf, 2)) {
329 delete[](uint8_t *)buf;
334 if (logfont.lfFaceName[0] == 0) {
335 wchar_t fname[_MAX_FNAME];
336 _wsplitpath(fontPath,
nullptr,
nullptr, fname,
nullptr);
338 wcsncpy_s(logfont.lfFaceName,
lengthof(logfont.lfFaceName), fname, _TRUNCATE);
339 logfont.lfWeight = strcasestr(font_name.c_str(),
" bold") !=
nullptr || strcasestr(font_name.c_str(),
"-bold") !=
nullptr ? FW_BOLD : FW_NORMAL;
344 return logfont.lfFaceName[0] != 0;
347static void LoadWin32Font(
FontSize fs,
const LOGFONT &logfont, uint size,
const char *font_name)
349 HFONT font = CreateFontIndirect(&logfont);
350 if (font ==
nullptr) {
351 ShowInfo(
"Unable to use '{}' for {} font, Win32 reported error 0x{:X}, using sprite font instead", font_name, FontSizeToName(fs), GetLastError());
369 if (font.empty())
return;
371 const char *font_name = font.c_str();
374 logfont.lfPitchAndFamily = fs ==
FS_MONO ? FIXED_PITCH : VARIABLE_PITCH;
375 logfont.lfCharSet = DEFAULT_CHARSET;
376 logfont.lfOutPrecision = OUT_OUTLINE_PRECIS;
377 logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
379 if (
settings->os_handle !=
nullptr) {
380 logfont = *(
const LOGFONT *)
settings->os_handle;
381 }
else if (strchr(font_name,
'.') !=
nullptr) {
383 if (!TryLoadFontFromFile(font, logfont)) {
384 ShowInfo(
"Unable to load file '{}' for {} font, using default windows font selection instead", font, FontSizeToName(fs));
388 if (logfont.lfFaceName[0] == 0) {
389 logfont.lfWeight = strcasestr(font_name,
" bold") !=
nullptr ? FW_BOLD : FW_NORMAL;
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
int height
The height of the font.
FontCache * parent
The parent of this font cache.
virtual GlyphID MapCharToGlyph(char32_t key, bool fallback=true)=0
Map a character into a glyph.
const FontSize fs
The size of the font.
int descender
The descender value of the font.
int ascender
The ascender value of the font.
A searcher for missing glyphs.
virtual void SetFontNames(struct FontCacheSettings *settings, const char *font_name, const void *os_data=nullptr)=0
Set the right font names.
bool FindMissingGlyphs()
Check whether there are glyphs missing in the current language.
virtual bool Monospace()=0
Whether to search for a monospace font or not.
T * Allocate(size_t count)
Get buffer of at least count times T.
virtual Sprite * Encode(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)=0
Convert a sprite from the loader to our own format.
std::array< Sprite, ZOOM_LVL_END > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
Font cache for fonts that are based on a TrueType font.
static constexpr int MAX_GLYPH_DIM
Maximum glyph dimensions.
int used_size
Used font size.
int req_size
Requested font size.
void ClearFontCache() override
Reset cached glyphs.
static constexpr uint MAX_FONT_MIN_REC_SIZE
Upper limit for the recommended font size in case a font file contains nonsensical values.
SpriteAllocator that allocates memory via a unique_ptr array.
Font cache for fonts that are based on a Win32 font.
HGDIOBJ old_font
Old font selected into the GDI context.
std::string fontname
Cached copy of loaded font facename.
HFONT font
The font face associated with this font.
GlyphID MapCharToGlyph(char32_t key, bool allow_fallback=true) override
Map a character into a glyph.
HDC dc
Cached GDI device context.
void ClearFontCache() override
Reset cached glyphs.
SIZE glyph_size
Maximum size of regular glyphs.
Win32FontCache(FontSize fs, const LOGFONT &logfont, int pixels)
Create a new Win32FontCache.
ReusableBuffer< uint8_t > render_buffer
Temporary buffer for rendering glyphs.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
std::string FioFindFullPath(Subdirectory subdir, const std::string &filename)
Find a path to the filename in one of the search directories.
bool FileExists(const std::string &filename)
Test whether the given filename exists.
@ BASE_DIR
Base directory for all subdirectories.
fluid_settings_t * settings
FluidSynth settings handle.
bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, MissingGlyphSearcher *callback)
We would like to have a fallback font as the current one doesn't contain all characters we need.
Functions related to font handling on Win32.
uint GetFontCacheFontSize(FontSize fs)
Get the scalable font size to use for a FontSize.
std::string GetFontCacheFontName(FontSize fs)
Get font to use for a given font size.
FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
uint32_t GlyphID
Glyphs are characters from a font.
@ Font
A sprite used for fonts.
FontSize
Available font sizes.
@ FS_MONO
Index of the monospaced font in the font tables.
@ FS_SMALL
Index of the small font in the font tables.
@ FS_NORMAL
Index of the normal font in the font tables.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
void MemSetT(T *ptr, uint8_t value, size_t num=1)
Type-safe version of memset().
A number of safeguards to prevent using unsafe methods.
@ SCC_ALPHA
Sprite has alpha.
@ SCC_PAL
Sprite has palette data.
#define lengthof(array)
Return the length of an fixed size array.
Settings for the four different fonts.
Settings for a single font.
Structure for passing information from the sprite loader to the blitter.
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
SpriteColourComponent colours
The colour components of the sprite with useful information.
uint16_t width
Width of the sprite.
int16_t x_offs
The x-offset of where the sprite will be drawn.
SpriteLoader::CommonPixel * data
The sprite itself.
uint16_t height
Height of the sprite.
SpriteType type
The sprite type.
int16_t y_offs
The y-offset of where the sprite will be drawn.
Data structure describing a sprite.
uint8_t data[]
Sprite data.
static const int MAX_FONT_SIZE
Maximum font size.
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.
char * convert_from_fs(const std::wstring_view src, std::span< char > dst_buf)
Convert to OpenTTD's encoding from that of the environment in UNICODE.
std::string FS2OTTD(const std::wstring &name)
Convert to OpenTTD's encoding from a wide string.
wchar_t * convert_to_fs(const std::string_view src, std::span< wchar_t > dst_buf)
Convert from OpenTTD's encoding to that of the environment in UNICODE.
declarations of functions for MS windows systems
@ ZOOM_LVL_MIN
Minimum zoom level.