| 1 |
#include "FontUtils.hpp" |
| 2 |
|
| 3 |
#include "wxStringHash.h" |
| 4 |
|
| 5 |
namespace FontUtils |
| 6 |
{ |
| 7 |
#ifdef __WXMSW__ |
| 8 |
const wxString DEFAULT_FACE_NAME = "Consolas"; |
| 9 |
constexpr int DEFAULT_FONT_SIZE = 14; |
| 10 |
#elif defined(__WXMAC__) |
| 11 |
const wxString DEFAULT_FACE_NAME = "monaco"; |
| 12 |
constexpr int DEFAULT_FONT_SIZE = 14; |
| 13 |
#else // GTK, FreeBSD etc |
| 14 |
const wxString DEFAULT_FACE_NAME = "Monospace"; |
| 15 |
constexpr int DEFAULT_FONT_SIZE = 14; |
| 16 |
#endif |
| 17 |
|
| 18 |
#ifdef __WXMSW__ |
| 19 |
const std::unordered_set<wxString> words = { "SemiBold", "Semibold", "Extended", "Semi Bold", "Semi bold" }; |
| 20 |
#endif |
| 21 |
|
| 22 |
std::unordered_map<wxString, wxString> fixed_fonts_cache; |
| 23 |
|
| 24 |
const wxString& GetFontInfo(const wxFont& font) { return GetFontInfo(font.GetNativeFontInfoDesc()); } |
| 25 |
|
| 26 |
const wxString& GetFontInfo(const wxString& font_desc) |
| 27 |
{ |
| 28 |
// check the cache first |
| 29 |
if(fixed_fonts_cache.count(font_desc) != 0) { |
| 30 |
return fixed_fonts_cache[font_desc]; |
| 31 |
} |
| 32 |
|
| 33 |
wxString desc = font_desc; |
| 34 |
#ifdef __WXMSW__ |
| 35 |
// on MSW, we need to manipulate the info by remiving |
| 36 |
// "Semi Bold" (on all its variants) from teh font's info |
| 37 |
for(const wxString& word : words) { |
| 38 |
desc.Replace(word, wxEmptyString); |
| 39 |
} |
| 40 |
|
| 41 |
// replace all double spaces with a single one |
| 42 |
while(desc.Replace(" ", " ")) |
| 43 |
; |
| 44 |
desc.Trim(); |
| 45 |
#endif |
| 46 |
|
| 47 |
// update the cache |
| 48 |
fixed_fonts_cache.insert({ font_desc, desc }); |
| 49 |
// return the cached entry |
| 50 |
return fixed_fonts_cache[font_desc]; |
| 51 |
} |
| 52 |
|
| 53 |
wxFont GetDefaultMonospacedFont() |
| 54 |
{ |
| 55 |
wxFontInfo fontInfo = wxFontInfo(DEFAULT_FONT_SIZE).Family(wxFONTFAMILY_MODERN).FaceName(DEFAULT_FACE_NAME); |
| 56 |
wxFont font(fontInfo); |
| 57 |
return font; |
| 58 |
} |
| 59 |
} // namespace FontUtils |