OpenTTD Source 20250524-master-gc366e6a48e
font_win32.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "../../stdafx.h"
11#include "../../debug.h"
12#include "../../blitter/factory.hpp"
13#include "../../core/math_func.hpp"
14#include "../../error_func.h"
15#include "../../fileio_func.h"
16#include "../../fontcache.h"
17#include "../../fontcache/truetypefontcache.h"
18#include "../../fontdetection.h"
19#include "../../library_loader.h"
20#include "../../string_func.h"
21#include "../../strings_func.h"
22#include "../../zoom_func.h"
23#include "font_win32.h"
24
25#include "../../table/control_codes.h"
26
27#include <windows.h>
28#include <shlobj.h> /* SHGetFolderPath */
29#include "win32.h"
30#undef small // Say what, Windows?
31
32#include "../../safeguards.h"
33
34struct EFCParam {
35 FontCacheSettings *settings;
36 LOCALESIGNATURE locale;
37 MissingGlyphSearcher *callback;
38 std::vector<std::wstring> fonts;
39
40 bool Add(const std::wstring_view &font)
41 {
42 for (const auto &entry : this->fonts) {
43 if (font.compare(entry) == 0) return false;
44 }
45
46 this->fonts.emplace_back(font);
47
48 return true;
49 }
50};
51
52static int CALLBACK EnumFontCallback(const ENUMLOGFONTEX *logfont, const NEWTEXTMETRICEX *metric, DWORD type, LPARAM lParam)
53{
54 EFCParam *info = (EFCParam *)lParam;
55
56 /* Skip duplicates */
57 if (!info->Add(logfont->elfFullName)) return 1;
58 /* Only use TrueType fonts */
59 if (!(type & TRUETYPE_FONTTYPE)) return 1;
60 /* Don't use SYMBOL fonts */
61 if (logfont->elfLogFont.lfCharSet == SYMBOL_CHARSET) return 1;
62 /* Use monospaced fonts when asked for it. */
63 if (info->callback->Monospace() && (logfont->elfLogFont.lfPitchAndFamily & (FF_MODERN | FIXED_PITCH)) != (FF_MODERN | FIXED_PITCH)) return 1;
64
65 /* The font has to have at least one of the supported locales to be usable. */
66 auto check_bitfields = [&]() {
67 /* First try Unicode Subset Bitfield. */
68 for (uint8_t i = 0; i < 4; i++) {
69 if ((metric->ntmFontSig.fsUsb[i] & info->locale.lsUsb[i]) != 0) return true;
70 }
71 /* Keep Code Page Bitfield as a fallback. */
72 for (uint8_t i = 0; i < 2; i++) {
73 if ((metric->ntmFontSig.fsCsb[i] & info->locale.lsCsbSupported[i]) != 0) return true;
74 }
75 return false;
76 };
77 if (!check_bitfields()) return 1;
78
79 char font_name[MAX_PATH];
80 convert_from_fs(logfont->elfFullName, font_name);
81
82 info->callback->SetFontNames(info->settings, font_name, &logfont->elfLogFont);
83 if (info->callback->FindMissingGlyphs()) return 1;
84 Debug(fontcache, 1, "Fallback font: {}", font_name);
85 return 0; // stop enumerating
86}
87
88bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, MissingGlyphSearcher *callback)
89{
90 Debug(fontcache, 1, "Trying fallback fonts");
91 EFCParam langInfo;
92 std::wstring lang = OTTD2FS(language_isocode.substr(0, language_isocode.find('_')));
93 if (GetLocaleInfoEx(lang.c_str(), LOCALE_FONTSIGNATURE, reinterpret_cast<LPWSTR>(&langInfo.locale), sizeof(langInfo.locale) / sizeof(wchar_t)) == 0) {
94 /* Invalid isocode or some other mysterious error, can't determine fallback font. */
95 Debug(fontcache, 1, "Can't get locale info for fallback font (isocode={})", language_isocode);
96 return false;
97 }
98 langInfo.settings = settings;
99 langInfo.callback = callback;
100
101 LOGFONT font;
102 /* Enumerate all fonts. */
103 font.lfCharSet = DEFAULT_CHARSET;
104 font.lfFaceName[0] = '\0';
105 font.lfPitchAndFamily = 0;
106
107 HDC dc = GetDC(nullptr);
108 int ret = EnumFontFamiliesEx(dc, &font, (FONTENUMPROC)&EnumFontCallback, (LPARAM)&langInfo, 0);
109 ReleaseDC(nullptr, dc);
110 return ret == 0;
111}
112
113
120Win32FontCache::Win32FontCache(FontSize fs, const LOGFONT &logfont, int pixels) : TrueTypeFontCache(fs, pixels), logfont(logfont)
121{
122 this->dc = CreateCompatibleDC(nullptr);
123 this->SetFontSize(pixels);
124}
125
126Win32FontCache::~Win32FontCache()
127{
128 this->ClearFontCache();
129 DeleteDC(this->dc);
130 DeleteObject(this->font);
131}
132
133void Win32FontCache::SetFontSize(int pixels)
134{
135 if (pixels == 0) {
136 /* Try to determine a good height based on the minimal height recommended by the font. */
137 int scaled_height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
138 pixels = scaled_height;
139
140 HFONT temp = CreateFontIndirect(&this->logfont);
141 if (temp != nullptr) {
142 HGDIOBJ old = SelectObject(this->dc, temp);
143
144 UINT size = GetOutlineTextMetrics(this->dc, 0, nullptr);
145 LPOUTLINETEXTMETRIC otm = (LPOUTLINETEXTMETRIC)new BYTE[size];
146 GetOutlineTextMetrics(this->dc, size, otm);
147
148 /* Font height is minimum height plus the difference between the default
149 * height for this font size and the small size. */
150 int diff = scaled_height - ScaleGUITrad(FontCache::GetDefaultFontHeight(FS_SMALL));
151 /* Clamp() is not used as scaled_height could be greater than MAX_FONT_SIZE, which is not permitted in Clamp(). */
152 pixels = std::min(std::max(std::min<int>(otm->otmusMinimumPPEM, MAX_FONT_MIN_REC_SIZE) + diff, scaled_height), MAX_FONT_SIZE);
153
154 delete[] (BYTE*)otm;
155 SelectObject(dc, old);
156 DeleteObject(temp);
157 }
158 } else {
159 pixels = ScaleGUITrad(pixels);
160 }
161 this->used_size = pixels;
162
163 /* Create GDI font handle. */
164 this->logfont.lfHeight = -pixels;
165 this->logfont.lfWidth = 0;
166 this->logfont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
167 this->logfont.lfQuality = ANTIALIASED_QUALITY;
168
169 if (this->font != nullptr) {
170 SelectObject(dc, this->old_font);
171 DeleteObject(this->font);
172 }
173 this->font = CreateFontIndirect(&this->logfont);
174 this->old_font = SelectObject(this->dc, this->font);
175
176 /* Query the font metrics we needed. */
177 UINT otmSize = GetOutlineTextMetrics(this->dc, 0, nullptr);
178 POUTLINETEXTMETRIC otm = (POUTLINETEXTMETRIC)new BYTE[otmSize];
179 GetOutlineTextMetrics(this->dc, otmSize, otm);
180
181 this->ascender = otm->otmTextMetrics.tmAscent;
182 this->descender = otm->otmTextMetrics.tmDescent;
183 this->height = this->ascender + this->descender;
184 this->glyph_size.cx = otm->otmTextMetrics.tmMaxCharWidth;
185 this->glyph_size.cy = otm->otmTextMetrics.tmHeight;
186
187 this->fontname = FS2OTTD((LPWSTR)((BYTE *)otm + (ptrdiff_t)otm->otmpFaceName));
188
189 Debug(fontcache, 2, "Loaded font '{}' with size {}", this->fontname, pixels);
190 delete[] (BYTE*)otm;
191}
192
197{
198 /* GUI scaling might have changed, determine font size anew if it was automatically selected. */
199 if (this->font != nullptr) this->SetFontSize(this->req_size);
200
202}
203
204/* virtual */ const Sprite *Win32FontCache::InternalGetGlyph(GlyphID key, bool aa)
205{
206 GLYPHMETRICS gm;
207 MAT2 mat = { {0, 1}, {0, 0}, {0, 0}, {0, 1} };
208
209 /* Call GetGlyphOutline with zero size initially to get required memory size. */
210 DWORD size = GetGlyphOutline(this->dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, 0, nullptr, &mat);
211 if (size == GDI_ERROR) UserError("Unable to render font glyph");
212
213 /* Add 1 scaled pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel. */
214 uint shadow = (this->fs == FS_NORMAL) ? ScaleGUITrad(1) : 0;
215 uint width = std::max(1U, (uint)gm.gmBlackBoxX + shadow);
216 uint height = std::max(1U, (uint)gm.gmBlackBoxY + shadow);
217
218 /* Limit glyph size to prevent overflows later on. */
219 if (width > MAX_GLYPH_DIM || height > MAX_GLYPH_DIM) UserError("Font glyph is too large");
220
221 /* Call GetGlyphOutline again with size to actually render the glyph. */
222 uint8_t *bmp = this->render_buffer.Allocate(size);
223 GetGlyphOutline(this->dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, size, bmp, &mat);
224
225 /* GDI has rendered the glyph, now we allocate a sprite and copy the image into it. */
226 SpriteLoader::SpriteCollection spritecollection;
227 SpriteLoader::Sprite &sprite = spritecollection[ZoomLevel::Min];
228 sprite.AllocateData(ZoomLevel::Min, width * height);
230 if (aa) sprite.colours.Set(SpriteComponent::Alpha);
231 sprite.width = width;
232 sprite.height = height;
233 sprite.x_offs = gm.gmptGlyphOrigin.x;
234 sprite.y_offs = this->ascender - gm.gmptGlyphOrigin.y;
235
236 if (size > 0) {
237 /* All pixel data returned by GDI is in the form of DWORD-aligned rows.
238 * For a non anti-aliased glyph, the returned bitmap has one bit per pixel.
239 * For anti-aliased rendering, GDI uses the strange value range of 0 to 64,
240 * inclusively. To map this to 0 to 255, we shift left by two and then
241 * subtract one. */
242 uint pitch = Align(aa ? gm.gmBlackBoxX : std::max((gm.gmBlackBoxX + 7u) / 8u, 1u), 4);
243
244 /* Draw shadow for medium size. */
245 if (this->fs == FS_NORMAL && !aa) {
246 for (uint y = 0; y < gm.gmBlackBoxY; y++) {
247 for (uint x = 0; x < gm.gmBlackBoxX; x++) {
248 if (aa ? (bmp[x + y * pitch] > 0) : HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
249 sprite.data[shadow + x + (shadow + y) * sprite.width].m = SHADOW_COLOUR;
250 sprite.data[shadow + x + (shadow + y) * sprite.width].a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
251 }
252 }
253 }
254 }
255
256 for (uint y = 0; y < gm.gmBlackBoxY; y++) {
257 for (uint x = 0; x < gm.gmBlackBoxX; x++) {
258 if (aa ? (bmp[x + y * pitch] > 0) : HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
259 sprite.data[x + y * sprite.width].m = FACE_COLOUR;
260 sprite.data[x + y * sprite.width].a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
261 }
262 }
263 }
264 }
265
266 UniquePtrSpriteAllocator allocator;
267 BlitterFactory::GetCurrentBlitter()->Encode(SpriteType::Font, spritecollection, allocator);
268
269 GlyphEntry new_glyph;
270 new_glyph.data = std::move(allocator.data);
271 new_glyph.width = gm.gmCellIncX;
272
273 return this->SetGlyphPtr(key, std::move(new_glyph)).GetSprite();
274}
275
276/* virtual */ GlyphID Win32FontCache::MapCharToGlyph(char32_t key, bool allow_fallback)
277{
278 assert(IsPrintable(key));
279
280 /* Convert characters outside of the BMP into surrogate pairs. */
281 WCHAR chars[2];
282 if (key >= 0x010000U) {
283 chars[0] = (wchar_t)(((key - 0x010000U) >> 10) + 0xD800);
284 chars[1] = (wchar_t)(((key - 0x010000U) & 0x3FF) + 0xDC00);
285 } else {
286 chars[0] = (wchar_t)(key & 0xFFFF);
287 }
288
289 WORD glyphs[2] = { 0, 0 };
290 GetGlyphIndicesW(this->dc, chars, key >= 0x010000U ? 2 : 1, glyphs, GGI_MARK_NONEXISTING_GLYPHS);
291
292 if (glyphs[0] != 0xFFFF) return glyphs[0];
293 return allow_fallback && key >= SCC_SPRITE_START && key <= SCC_SPRITE_END ? this->parent->MapCharToGlyph(key) : 0;
294}
295
296
297static bool TryLoadFontFromFile(const std::string &font_name, LOGFONT &logfont)
298{
299 wchar_t fontPath[MAX_PATH] = {};
300
301 /* See if this is an absolute path. */
302 if (FileExists(font_name)) {
303 convert_to_fs(font_name, fontPath);
304 } else {
305 /* Scan the search-paths to see if it can be found. */
306 std::string full_font = FioFindFullPath(BASE_DIR, font_name);
307 if (!full_font.empty()) {
308 convert_to_fs(font_name, fontPath);
309 }
310 }
311
312 if (fontPath[0] != 0) {
313 if (AddFontResourceEx(fontPath, FR_PRIVATE, 0) != 0) {
314 /* Try a nice little undocumented function first for getting the internal font name.
315 * Some documentation is found at: http://www.undocprint.org/winspool/getfontresourceinfo */
316 static LibraryLoader _gdi32("gdi32.dll");
317 typedef BOOL(WINAPI *PFNGETFONTRESOURCEINFO)(LPCTSTR, LPDWORD, LPVOID, DWORD);
318 static PFNGETFONTRESOURCEINFO GetFontResourceInfo = _gdi32.GetFunction("GetFontResourceInfoW");
319
320 if (GetFontResourceInfo != nullptr) {
321 /* Try to query an array of LOGFONTs that describe the file. */
322 DWORD len = 0;
323 if (GetFontResourceInfo(fontPath, &len, nullptr, 2) && len >= sizeof(LOGFONT)) {
324 LOGFONT *buf = (LOGFONT *)new uint8_t[len];
325 if (GetFontResourceInfo(fontPath, &len, buf, 2)) {
326 logfont = *buf; // Just use first entry.
327 }
328 delete[](uint8_t *)buf;
329 }
330 }
331
332 /* No dice yet. Use the file name as the font face name, hoping it matches. */
333 if (logfont.lfFaceName[0] == 0) {
334 wchar_t fname[_MAX_FNAME];
335 _wsplitpath(fontPath, nullptr, nullptr, fname, nullptr);
336
337 wcsncpy_s(logfont.lfFaceName, lengthof(logfont.lfFaceName), fname, _TRUNCATE);
338 logfont.lfWeight = StrContainsIgnoreCase(font_name, " bold") || StrContainsIgnoreCase(font_name, "-bold") ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts.
339 }
340 }
341 }
342
343 return logfont.lfFaceName[0] != 0;
344}
345
346static void LoadWin32Font(FontSize fs, const LOGFONT &logfont, uint size, std::string_view font_name)
347{
348 HFONT font = CreateFontIndirect(&logfont);
349 if (font == nullptr) {
350 ShowInfo("Unable to use '{}' for {} font, Win32 reported error 0x{:X}, using sprite font instead", font_name, FontSizeToName(fs), GetLastError());
351 return;
352 }
353 DeleteObject(font);
354
355 new Win32FontCache(fs, logfont, size);
356}
363void LoadWin32Font(FontSize fs)
364{
366
367 std::string font = GetFontCacheFontName(fs);
368 if (font.empty()) return;
369
370 LOGFONT logfont{};
371 logfont.lfPitchAndFamily = fs == FS_MONO ? FIXED_PITCH : VARIABLE_PITCH;
372 logfont.lfCharSet = DEFAULT_CHARSET;
373 logfont.lfOutPrecision = OUT_OUTLINE_PRECIS;
374 logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
375
376 if (settings->os_handle != nullptr) {
377 logfont = *(const LOGFONT *)settings->os_handle;
378 } else if (font.find('.') != std::string::npos) {
379 /* Might be a font file name, try load it. */
380 if (!TryLoadFontFromFile(font, logfont)) {
381 ShowInfo("Unable to load file '{}' for {} font, using default windows font selection instead", font, FontSizeToName(fs));
382 }
383 }
384
385 if (logfont.lfFaceName[0] == 0) {
386 logfont.lfWeight = StrContainsIgnoreCase(font, " bold") ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts.
387 convert_to_fs(font, logfont.lfFaceName);
388 }
389
390 LoadWin32Font(fs, logfont, GetFontCacheFontSize(fs), font);
391}
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr Timpl & Set()
Set all bits.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:136
int height
The height of the font.
Definition fontcache.h:26
FontCache * parent
The parent of this font cache.
Definition fontcache.h:24
virtual GlyphID MapCharToGlyph(char32_t key, bool fallback=true)=0
Map a character into a glyph.
const FontSize fs
The size of the font.
Definition fontcache.h:25
int descender
The descender value of the font.
Definition fontcache.h:28
int ascender
The ascender value of the font.
Definition fontcache.h:27
A searcher for missing glyphs.
bool FindMissingGlyphs()
Check whether there are glyphs missing in the current language.
Definition strings.cpp:2258
virtual void SetFontNames(struct FontCacheSettings *settings, std::string_view font_name, const void *os_data=nullptr)=0
Set the right font names.
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.
Map zoom level to data.
virtual Sprite * Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)=0
Convert a sprite from the loader to our own format.
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.
Definition spritecache.h:35
Font cache for fonts that are based on a Win32 font.
Definition font_win32.h:20
HGDIOBJ old_font
Old font selected into the GDI context.
Definition font_win32.h:25
std::string fontname
Cached copy of loaded font facename.
Definition font_win32.h:27
HFONT font
The font face associated with this font.
Definition font_win32.h:23
GlyphID MapCharToGlyph(char32_t key, bool allow_fallback=true) override
Map a character into a glyph.
HDC dc
Cached GDI device context.
Definition font_win32.h:24
void ClearFontCache() override
Reset cached glyphs.
SIZE glyph_size
Maximum size of regular glyphs.
Definition font_win32.h:26
Win32FontCache(FontSize fs, const LOGFONT &logfont, int pixels)
Create a new Win32FontCache.
ReusableBuffer< uint8_t > render_buffer
Temporary buffer for rendering glyphs.
Definition font_win32.h:29
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
bool FileExists(std::string_view filename)
Test whether the given filename exists.
Definition fileio.cpp:132
std::string FioFindFullPath(Subdirectory subdir, std::string_view filename)
Find a path to the filename in one of the search directories.
Definition fileio.cpp:144
@ BASE_DIR
Base directory for all subdirectories.
Definition fileio_type.h:88
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.
Definition fontcache.h:216
uint32_t GlyphID
Glyphs are characters from a font.
Definition fontcache.h:17
@ Font
A sprite used for fonts.
FontSize
Available font sizes.
Definition gfx_type.h:250
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:254
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:252
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:251
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition math_func.hpp:37
@ Palette
Sprite has palette data.
@ Alpha
Sprite has alpha.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
bool StrContainsIgnoreCase(std::string_view str, std::string_view value)
Checks if a string is contained in another string, while ignoring the case of the characters.
Definition string.cpp:334
Settings for the four different fonts.
Definition fontcache.h:200
Settings for a single font.
Definition fontcache.h:192
uint8_t m
Remap-channel.
uint8_t a
Alpha-channel.
Structure for passing information from the sprite loader to the blitter.
SpriteComponents colours
The colour components of the sprite with useful information.
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
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.
int16_t y_offs
The y-offset of where the sprite will be drawn.
Data structure describing a sprite.
Definition spritecache.h:17
std::byte data[]
Sprite data.
Definition spritecache.h:22
static const int MAX_FONT_SIZE
Maximum font size.
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:49
std::string_view 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.
Definition win32.cpp:372
std::wstring OTTD2FS(std::string_view name)
Convert from OpenTTD's encoding to a wide string.
Definition win32.cpp:354
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:337
wchar_t * convert_to_fs(std::string_view src, std::span< wchar_t > dst_buf)
Convert from OpenTTD's encoding to that of the environment in UNICODE.
Definition win32.cpp:389
declarations of functions for MS windows systems
@ Min
Minimum zoom level.