@ -317,35 +317,12 @@ void Win32FontCache::ClearFontCache()
}
/**
* Loads the GDI font .
* If a GDI font description is present , e . g . from the automatic font
* fallback search , use it . Otherwise , try to resolve it by font name .
* @ param fs The font size to load .
*/
void LoadWin32Font ( FontSize fs )
static bool TryLoadFontFromFile ( const std : : string & font_name , LOGFONT & logfont )
{
FontCacheSubSetting * settings = GetFontCacheSubSetting ( fs ) ;
if ( settings - > font . empty ( ) ) return ;
const char * font_name = settings - > font . c_str ( ) ;
LOGFONT logfont ;
MemSetT ( & logfont , 0 ) ;
logfont . lfPitchAndFamily = fs = = FS_MONO ? FIXED_PITCH : VARIABLE_PITCH ;
logfont . lfCharSet = DEFAULT_CHARSET ;
logfont . lfOutPrecision = OUT_OUTLINE_PRECIS ;
logfont . lfClipPrecision = CLIP_DEFAULT_PRECIS ;
if ( settings - > os_handle ! = nullptr ) {
logfont = * ( const LOGFONT * ) settings - > os_handle ;
} else if ( strchr ( font_name , ' . ' ) ! = nullptr ) {
/* Might be a font file name, try load it. */
wchar_t fontPath [ MAX_PATH ] = { } ;
/* See if this is an absolute path. */
if ( FileExists ( settings- > font) ) {
if ( FileExists ( font_name ) ) {
convert_to_fs ( font_name , fontPath , lengthof ( fontPath ) ) ;
} else {
/* Scan the search-paths to see if it can be found. */
@ -381,19 +358,16 @@ void LoadWin32Font(FontSize fs)
_wsplitpath ( fontPath , nullptr , nullptr , fname , nullptr ) ;
wcsncpy_s ( logfont . lfFaceName , lengthof ( logfont . lfFaceName ) , fname , _TRUNCATE ) ;
logfont . lfWeight = strcasestr ( font_name , " bold " ) ! = nullptr | | strcasestr ( font_name , " -bold " ) ! = nullptr ? FW_BOLD : FW_NORMAL ; // Poor man's way to allow selecting bold fonts.
}
} else {
ShowInfo ( " Unable to load file '{}' for {} font, using default windows font selection instead " , font_name , FontSizeToName ( fs ) ) ;
logfont . lfWeight = strcasestr ( font_name . c_str ( ) , " bold " ) ! = nullptr | | strcasestr ( font_name . c_str ( ) , " -bold " ) ! = nullptr ? FW_BOLD : FW_NORMAL ; // Poor man's way to allow selecting bold fonts.
}
}
}
if ( logfont . lfFaceName [ 0 ] = = 0 ) {
logfont . lfWeight = strcasestr ( font_name , " bold " ) ! = nullptr ? FW_BOLD : FW_NORMAL ; // Poor man's way to allow selecting bold fonts.
convert_to_fs ( font_name , logfont . lfFaceName , lengthof ( logfont . lfFaceName ) ) ;
return logfont . lfFaceName [ 0 ] ! = 0 ;
}
static void LoadWin32Font ( FontSize fs , const LOGFONT & logfont , uint size , const char * font_name )
{
HFONT font = CreateFontIndirect ( & logfont ) ;
if ( font = = nullptr ) {
ShowInfo ( " Unable to use '{}' for {} font, Win32 reported error 0x{:X}, using sprite font instead " , font_name , FontSizeToName ( fs ) , GetLastError ( ) ) ;
@ -401,5 +375,61 @@ void LoadWin32Font(FontSize fs)
}
DeleteObject ( font ) ;
new Win32FontCache ( fs , logfont , settings - > size ) ;
new Win32FontCache ( fs , logfont , size ) ;
}
/**
* Loads the GDI font .
* If a GDI font description is present , e . g . from the automatic font
* fallback search , use it . Otherwise , try to resolve it by font name .
* @ param fs The font size to load .
*/
void LoadWin32Font ( FontSize fs )
{
FontCacheSubSetting * settings = GetFontCacheSubSetting ( fs ) ;
if ( settings - > font . empty ( ) ) return ;
const char * font_name = settings - > font . c_str ( ) ;
LOGFONT logfont ;
MemSetT ( & logfont , 0 ) ;
logfont . lfPitchAndFamily = fs = = FS_MONO ? FIXED_PITCH : VARIABLE_PITCH ;
logfont . lfCharSet = DEFAULT_CHARSET ;
logfont . lfOutPrecision = OUT_OUTLINE_PRECIS ;
logfont . lfClipPrecision = CLIP_DEFAULT_PRECIS ;
if ( settings - > os_handle ! = nullptr ) {
logfont = * ( const LOGFONT * ) settings - > os_handle ;
} else if ( strchr ( font_name , ' . ' ) ! = nullptr ) {
/* Might be a font file name, try load it. */
if ( ! TryLoadFontFromFile ( settings - > font , logfont ) ) {
ShowInfo ( " Unable to load file '{}' for {} font, using default windows font selection instead " , font_name , FontSizeToName ( fs ) ) ;
}
}
if ( logfont . lfFaceName [ 0 ] = = 0 ) {
logfont . lfWeight = strcasestr ( font_name , " bold " ) ! = nullptr ? FW_BOLD : FW_NORMAL ; // Poor man's way to allow selecting bold fonts.
convert_to_fs ( font_name , logfont . lfFaceName , lengthof ( logfont . lfFaceName ) ) ;
}
LoadWin32Font ( fs , logfont , settings - > size , font_name ) ;
}
/**
* Load a TrueType font from a file .
* @ param fs The font size to load .
* @ param file_name Path to the font file .
* @ param size Requested font size .
*/
void LoadWin32Font ( FontSize fs , const std : : string & file_name , uint size )
{
LOGFONT logfont ;
MemSetT ( & logfont , 0 ) ;
logfont . lfPitchAndFamily = fs = = FS_MONO ? FIXED_PITCH : VARIABLE_PITCH ;
logfont . lfCharSet = DEFAULT_CHARSET ;
logfont . lfOutPrecision = OUT_OUTLINE_PRECIS ;
logfont . lfClipPrecision = CLIP_DEFAULT_PRECIS ;
if ( TryLoadFontFromFile ( file_name , logfont ) ) {
LoadWin32Font ( fs , logfont , size , file_name . c_str ( ) ) ;
}
}