10 #include "../../stdafx.h"
11 #include "../../debug.h"
12 #include "../../blitter/factory.hpp"
13 #include "../../core/alloc_func.hpp"
14 #include "../../core/math_func.hpp"
15 #include "../../core/mem_func.hpp"
16 #include "../../error_func.h"
17 #include "../../fileio_func.h"
18 #include "../../fontcache.h"
19 #include "../../fontcache/truetypefontcache.h"
20 #include "../../fontdetection.h"
21 #include "../../library_loader.h"
22 #include "../../string_func.h"
23 #include "../../strings_func.h"
24 #include "../../zoom_func.h"
27 #include "../../table/control_codes.h"
32 #undef small // Say what, Windows?
38 LOCALESIGNATURE locale;
40 std::vector<std::wstring> fonts;
42 bool Add(
const std::wstring_view &font)
44 for (
const auto &entry : this->fonts) {
45 if (font.compare(entry) == 0)
return false;
48 this->fonts.emplace_back(font);
54 static int CALLBACK EnumFontCallback(
const ENUMLOGFONTEX *logfont,
const NEWTEXTMETRICEX *metric, DWORD type, LPARAM lParam)
59 if (!info->Add(logfont->elfFullName))
return 1;
61 if (!(type & TRUETYPE_FONTTYPE))
return 1;
63 if (logfont->elfLogFont.lfCharSet == SYMBOL_CHARSET)
return 1;
65 if (info->callback->
Monospace() && (logfont->elfLogFont.lfPitchAndFamily & (FF_MODERN | FIXED_PITCH)) != (FF_MODERN | FIXED_PITCH))
return 1;
68 if ((metric->ntmFontSig.fsCsb[0] & info->locale.lsCsbSupported[0]) == 0 && (metric->ntmFontSig.fsCsb[1] & info->locale.lsCsbSupported[1]) == 0) {
71 memset(&fs, 0,
sizeof(fs));
72 HFONT font = CreateFontIndirect(&logfont->elfLogFont);
73 if (font !=
nullptr) {
74 HDC dc = GetDC(
nullptr);
75 HGDIOBJ oldfont = SelectObject(dc, font);
76 GetTextCharsetInfo(dc, &fs, 0);
77 SelectObject(dc, oldfont);
78 ReleaseDC(
nullptr, dc);
81 if ((fs.fsCsb[0] & info->locale.lsCsbSupported[0]) == 0 && (fs.fsCsb[1] & info->locale.lsCsbSupported[1]) == 0)
return 1;
84 char font_name[MAX_PATH];
87 info->callback->
SetFontNames(info->settings, font_name, &logfont->elfLogFont);
89 Debug(fontcache, 1,
"Fallback font: {}", font_name);
95 Debug(fontcache, 1,
"Trying fallback fonts");
97 if (GetLocaleInfo(MAKELCID(winlangid, SORT_DEFAULT), LOCALE_FONTSIGNATURE, (LPTSTR)&langInfo.locale,
sizeof(langInfo.locale) /
sizeof(
wchar_t)) == 0) {
99 Debug(fontcache, 1,
"Can't get locale info for fallback font (langid=0x{:x})", winlangid);
103 langInfo.callback = callback;
107 font.lfCharSet = DEFAULT_CHARSET;
108 font.lfFaceName[0] =
'\0';
109 font.lfPitchAndFamily = 0;
111 HDC dc = GetDC(
nullptr);
112 int ret = EnumFontFamiliesEx(dc, &font, (FONTENUMPROC)&EnumFontCallback, (LPARAM)&langInfo, 0);
113 ReleaseDC(
nullptr, dc);
118 #ifndef ANTIALIASED_QUALITY
119 #define ANTIALIASED_QUALITY 4
130 this->
dc = CreateCompatibleDC(
nullptr);
131 this->SetFontSize(pixels);
134 Win32FontCache::~Win32FontCache()
138 DeleteObject(this->font);
141 void Win32FontCache::SetFontSize(
int pixels)
145 int scaled_height =
ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
146 pixels = scaled_height;
148 HFONT temp = CreateFontIndirect(&this->logfont);
149 if (temp !=
nullptr) {
150 HGDIOBJ old = SelectObject(this->
dc, temp);
152 UINT size = GetOutlineTextMetrics(this->
dc, 0,
nullptr);
153 LPOUTLINETEXTMETRIC otm = (LPOUTLINETEXTMETRIC)
new BYTE[size];
154 GetOutlineTextMetrics(this->
dc, size, otm);
163 SelectObject(
dc, old);
172 this->logfont.lfHeight = -pixels;
173 this->logfont.lfWidth = 0;
174 this->logfont.lfOutPrecision = ANTIALIASED_QUALITY;
176 if (this->font !=
nullptr) {
178 DeleteObject(this->font);
180 this->font = CreateFontIndirect(&this->logfont);
181 this->
old_font = SelectObject(this->
dc, this->font);
184 UINT otmSize = GetOutlineTextMetrics(this->
dc, 0,
nullptr);
185 POUTLINETEXTMETRIC otm = (POUTLINETEXTMETRIC)
new BYTE[otmSize];
186 GetOutlineTextMetrics(this->
dc, otmSize, otm);
189 this->
ascender = otm->otmTextMetrics.tmAscent;
190 this->
descender = otm->otmTextMetrics.tmDescent;
192 this->
glyph_size.cx = otm->otmTextMetrics.tmMaxCharWidth;
193 this->
glyph_size.cy = otm->otmTextMetrics.tmHeight;
195 this->
fontname =
FS2OTTD((LPWSTR)((BYTE *)otm + (ptrdiff_t)otm->otmpFaceName));
197 Debug(fontcache, 2,
"Loaded font '{}' with size {}", this->
fontname, pixels);
207 if (this->font !=
nullptr) this->SetFontSize(this->
req_size);
212 const Sprite *Win32FontCache::InternalGetGlyph(
GlyphID key,
bool aa)
215 MAT2 mat = { {0, 1}, {0, 0}, {0, 0}, {0, 1} };
218 DWORD size = GetGlyphOutline(this->
dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, 0,
nullptr, &mat);
219 if (size == GDI_ERROR) UserError(
"Unable to render font glyph");
223 uint width = std::max(1U, (uint)gm.gmBlackBoxX + shadow);
224 uint
height = std::max(1U, (uint)gm.gmBlackBoxY + shadow);
230 byte *bmp =
new byte[size];
231 GetGlyphOutline(this->
dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, size, bmp, &mat);
239 sprite.
width = width;
241 sprite.
x_offs = gm.gmptGlyphOrigin.x;
250 uint pitch =
Align(aa ? gm.gmBlackBoxX : std::max((gm.gmBlackBoxX + 7u) / 8u, 1u), 4);
254 for (uint y = 0; y < gm.gmBlackBoxY; y++) {
255 for (uint x = 0; x < gm.gmBlackBoxX; x++) {
256 if (aa ? (bmp[x + y * pitch] > 0) :
HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
257 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
m = SHADOW_COLOUR;
258 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
264 for (uint y = 0; y < gm.gmBlackBoxY; y++) {
265 for (uint x = 0; x < gm.gmBlackBoxX; x++) {
266 if (aa ? (bmp[x + y * pitch] > 0) :
HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
267 sprite.
data[x + y * sprite.
width].
m = FACE_COLOUR;
268 sprite.
data[x + y * sprite.
width].
a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
274 GlyphEntry new_glyph;
276 new_glyph.
width = gm.gmCellIncX;
278 this->SetGlyphPtr(key, &new_glyph);
282 return new_glyph.sprite;
287 assert(IsPrintable(key));
291 if (key >= 0x010000U) {
292 chars[0] = (wchar_t)(((key - 0x010000U) >> 10) + 0xD800);
293 chars[1] = (wchar_t)(((key - 0x010000U) & 0x3FF) + 0xDC00);
295 chars[0] = (wchar_t)(key & 0xFFFF);
298 WORD glyphs[2] = { 0, 0 };
299 GetGlyphIndicesW(this->
dc, chars, key >= 0x010000U ? 2 : 1, glyphs, GGI_MARK_NONEXISTING_GLYPHS);
301 if (glyphs[0] != 0xFFFF)
return glyphs[0];
302 return allow_fallback && key >= SCC_SPRITE_START && key <= SCC_SPRITE_END ? this->
parent->
MapCharToGlyph(key) : 0;
305 const void *Win32FontCache::InternalGetFontTable(uint32_t tag,
size_t &length)
307 DWORD len = GetFontData(this->
dc, tag, 0,
nullptr, 0);
309 void *result =
nullptr;
310 if (len != GDI_ERROR && len > 0) {
311 result = MallocT<BYTE>(len);
312 GetFontData(this->
dc, tag, 0, result, len);
320 static bool TryLoadFontFromFile(
const std::string &font_name, LOGFONT &logfont)
322 wchar_t fontPath[MAX_PATH] = {};
330 if (!full_font.empty()) {
335 if (fontPath[0] != 0) {
336 if (AddFontResourceEx(fontPath, FR_PRIVATE, 0) != 0) {
340 typedef BOOL(WINAPI *PFNGETFONTRESOURCEINFO)(LPCTSTR, LPDWORD, LPVOID, DWORD);
341 static PFNGETFONTRESOURCEINFO GetFontResourceInfo = _gdi32.GetFunction(
"GetFontResourceInfoW");
343 if (GetFontResourceInfo !=
nullptr) {
346 if (GetFontResourceInfo(fontPath, &len,
nullptr, 2) && len >=
sizeof(LOGFONT)) {
347 LOGFONT *buf = (LOGFONT *)
new byte[len];
348 if (GetFontResourceInfo(fontPath, &len, buf, 2)) {
356 if (logfont.lfFaceName[0] == 0) {
357 wchar_t fname[_MAX_FNAME];
358 _wsplitpath(fontPath,
nullptr,
nullptr, fname,
nullptr);
360 wcsncpy_s(logfont.lfFaceName,
lengthof(logfont.lfFaceName), fname, _TRUNCATE);
361 logfont.lfWeight = strcasestr(font_name.c_str(),
" bold") !=
nullptr || strcasestr(font_name.c_str(),
"-bold") !=
nullptr ? FW_BOLD : FW_NORMAL;
366 return logfont.lfFaceName[0] != 0;
371 HFONT font = CreateFontIndirect(&logfont);
372 if (font ==
nullptr) {
373 ShowInfo(
"Unable to use '{}' for {} font, Win32 reported error 0x{:X}, using sprite font instead", font_name, FontSizeToName(fs), GetLastError());
392 const char *font_name =
settings->font.c_str();
395 logfont.lfPitchAndFamily = fs ==
FS_MONO ? FIXED_PITCH : VARIABLE_PITCH;
396 logfont.lfCharSet = DEFAULT_CHARSET;
397 logfont.lfOutPrecision = OUT_OUTLINE_PRECIS;
398 logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
400 if (
settings->os_handle !=
nullptr) {
401 logfont = *(
const LOGFONT *)
settings->os_handle;
402 }
else if (strchr(font_name,
'.') !=
nullptr) {
404 if (!TryLoadFontFromFile(
settings->font, logfont)) {
405 ShowInfo(
"Unable to load file '{}' for {} font, using default windows font selection instead", font_name, FontSizeToName(fs));
409 if (logfont.lfFaceName[0] == 0) {
410 logfont.lfWeight = strcasestr(font_name,
" bold") !=
nullptr ? FW_BOLD : FW_NORMAL;
427 logfont.lfPitchAndFamily = fs ==
FS_MONO ? FIXED_PITCH : VARIABLE_PITCH;
428 logfont.lfCharSet = DEFAULT_CHARSET;
429 logfont.lfOutPrecision = OUT_OUTLINE_PRECIS;
430 logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
432 if (TryLoadFontFromFile(file_name, logfont)) {