28#include <wx/tokenzr.h>
31#include "OCPNPlatform.h"
37 wxFontStyle style_req;
38 wxFontWeight weight_req;
45 wxFont *FindOrCreateFont(
int pointSize, wxFontFamily family,
46 wxFontStyle style, wxFontWeight weight,
47 bool underline =
false,
48 const wxString &face = wxEmptyString,
49 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
54 wxFontStyle style, wxFontWeight weight,
bool underline,
55 const wxString &facename, wxFontEncoding encoding);
57 std::vector<font_cache_record> m_fontVector;
60extern wxString g_locale;
78int g_default_font_size;
79wxString g_default_font_facename;
81FontMgr *FontMgr::instance = NULL;
84 if (!instance) instance =
new FontMgr;
88void FontMgr::Shutdown() {
95FontMgr::FontMgr() : m_wxFontCache(NULL), m_fontlist(NULL), pDefFont(NULL) {
97 m_fontlist =
new FontList;
98 m_fontlist->DeleteContents(
true);
103 pDefFont = FindOrCreateFont(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
104 wxFONTWEIGHT_BOLD, FALSE, wxString(_T (
"" )),
105 wxFONTENCODING_SYSTEM);
112 delete m_wxFontCache;
115void FontMgr::SetLocale(wxString &newLocale) { s_locale = newLocale; }
119 return pmfd ? pmfd->
m_color : wxColour(0, 0, 0);
123 const wxColour color)
const {
138 configkey = s_locale;
139 configkey.Append(_T(
"-"));
143 const collate<char> &coll = use_facet<collate<char> >(loc);
148 wxCharBuffer abuf = description.ToUTF8();
150 int fdLen = strlen(abuf);
152 configkey.Append(wxString::Format(
153 _T(
"%08lx"), coll.hash(abuf.data(), abuf.data() + fdLen)));
157int FontMgr::GetSystemFontSize() {
158 static int sys_font_size = 0;
159 if (!sys_font_size) {
160 wxFont sys_font = *wxNORMAL_FONT;
161 sys_font_size = sys_font.GetPointSize();
163 return sys_font_size;
166wxString FontMgr::GetSystemFontFaceName() {
167 static wxString sys_font_facename;
168 if (sys_font_facename.IsEmpty()) {
169 wxFont sys_font = *wxNORMAL_FONT;
170 sys_font_facename = sys_font.GetFaceName();
172 return sys_font_facename;
175bool FontMgr::IsDefaultFontEntry(
const MyFontDesc *font_desc)
const {
189 auto node = m_fontlist->GetFirst();
192 pmfd = node->GetData();
196 if (requested_font_size == 0 && IsDefaultFontEntry(pmfd)) {
201 }
else if (requested_font_size != 0 &&
202 pmfd->
m_font->GetPointSize() == requested_font_size) {
206 node = node->GetNext();
216 if (0 == requested_font_size) {
217 new_size = g_default_font_size ? g_default_font_size : GetSystemFontSize();
219 new_size = requested_font_size;
221 wxString face_name = g_default_font_facename.Length()
222 ? g_default_font_facename
223 : GetSystemFontFaceName();
225 wxString nativefont = GetSimpleNativeFont(new_size, face_name);
226 wxFont *nf = wxFont::New(nativefont);
230 bool is_default = (requested_font_size == 0);
232 new MyFontDesc(TextElement, configkey, nf, color, is_default);
233 m_fontlist->Append(pnewfd);
238MyFontDesc *FontMgr::GetFontDesc(
const wxString &TextElement)
const {
241 auto node = m_fontlist->GetFirst();
243 pmfd = node->GetData();
248 node = node->GetNext();
252 node = m_fontlist->GetFirst();
254 pmfd = node->GetData();
259 node = node->GetNext();
266 wxColor defaultColor = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
269 if (TextElement.IsSameAs(_(
"Console Legend")))
270 defaultColor = wxColour(0, 255, 0);
271 else if (TextElement.IsSameAs(_(
"Console Value")))
272 defaultColor = wxColour(0, 255, 0);
273 else if (TextElement.IsSameAs(_(
"Marks")))
274 defaultColor = wxColour(0, 0, 0);
275 else if (TextElement.IsSameAs(_(
"RouteLegInfoRollover")))
276 defaultColor = wxColour(0, 0, 0);
277 else if (TextElement.IsSameAs(_(
"AISRollover")))
278 defaultColor = wxColour(0, 0, 0);
279 else if (TextElement.IsSameAs(_(
"ExtendedTideIcon")))
280 defaultColor = wxColour(0, 0, 0);
281 else if (TextElement.IsSameAs(_(
"ChartTexts")))
282 defaultColor = wxColour(0, 0, 0);
283 else if (TextElement.IsSameAs(_(
"AIS Target Name")))
284 defaultColor = wxColour(0, 0, 0);
287 return wxColour(0, 0, 0);
293wxString FontMgr::GetSimpleNativeFont(
int size, wxString face) {
298 nativefont = wxFont(size, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
299 wxFONTWEIGHT_NORMAL,
false, face)
300 .GetNativeFontInfoDesc();
328 MyFontDesc *pfd = m_fontlist->Item(i)->GetData();
333 MyFontDesc *pfd = m_fontlist->Item(i)->GetData();
338 std::set<wxString> uniqueStrings;
340 auto node = m_fontlist->GetFirst();
343 if (locale.IsEmpty() || pmfd->
m_configstring.BeforeFirst(
'-') == locale) {
346 node = node->GetNext();
348 wxArrayString strings;
349 strings.reserve(uniqueStrings.size());
350 for (
const auto &str : uniqueStrings) {
358 MyFontDesc *pfd = m_fontlist->Item(i)->GetData();
363 MyFontDesc *pfd = m_fontlist->Item(i)->GetData();
365 ret.Append(_T (
":" ));
367 ret.Append(_T (
":" ));
369 wxString cols(_T(
"rgb(0,0,0)"));
370 if (pfd->
m_color.IsOk()) cols = pfd->
m_color.GetAsString(wxC2S_CSS_SYNTAX);
379 auto node = m_fontlist->GetFirst();
382 pmfd = node->GetData();
386 node = node->GetNext();
395 wxStringTokenizer tk(*pNativeDesc, _T (
":" ));
396 wxString dialogstring = tk.GetNextToken();
397 wxString nativefont = tk.GetNextToken();
399 wxString c = tk.GetNextToken();
404 auto node = m_fontlist->GetFirst();
407 pmfd = node->GetData();
416 node = node->GetNext();
421 wxFont *nf0 =
new wxFont();
423#ifdef __OCPN__ANDROID__
424 wxFont *nf =
new wxFont(nativefont);
426 wxFont *nf = nf0->New(nativefont);
429 double font_size = nf->GetPointSize();
430 wxString s = nf->GetNativeFontInfoDesc();
434 wxString face = nf->GetFaceName();
435 const wxChar *t = face.c_str();
438 wxString substitute_native = GetSimpleNativeFont(12, _T(
""));
439 nf = nf0->New(substitute_native);
445 new MyFontDesc(dialogstring, *pConfigString, nf, color);
446 m_fontlist->Append(pnewfd);
451 wxFontStyle style, wxFontWeight weight,
452 bool underline,
const wxString &facename,
453 wxFontEncoding encoding) {
455 return m_wxFontCache->FindOrCreateFont(point_size, family, style, weight,
456 underline, facename, encoding);
460 wxFontFamily family, wxFontStyle style,
461 wxFontWeight weight,
bool underline,
462 const wxString &facename,
463 wxFontEncoding encoding) {
464 if (record.pointsize_req == pointSize && record.style_req == style &&
465 record.weight_req == weight && record.underline_req == underline) {
468 wxFont *font = record.font;
474 if (!facename.empty()) {
475 const wxString &fontFace = font->GetFaceName();
478 same = !fontFace || fontFace == facename;
480 same = font->GetFamily() == family;
482 if (same && (encoding != wxFONTENCODING_DEFAULT)) {
484 same = font->GetEncoding() == encoding;
491wxFont *OCPNwxFontList::FindOrCreateFont(
int pointSize, wxFontFamily family,
492 wxFontStyle style, wxFontWeight weight,
494 const wxString &facename,
495 wxFontEncoding encoding) {
504 if (family == wxFONTFAMILY_DEFAULT) family = wxFONTFAMILY_SWISS;
508 for (
size_t i = 0; i < m_fontVector.size(); i++) {
510 if (isCached(record, pointSize, family, style, weight, underline, facename,
519 wxFont fontTmp(OCPN_GetDisplayContentScaleFactor() * pointSize, family, style,
520 weight, underline, facename, encoding);
521 if (fontTmp.IsOk()) {
522 font =
new wxFont(fontTmp);
525 record.pointsize_req = pointSize;
526 record.style_req = style;
527 record.weight_req = weight;
528 record.underline_req = underline;
529 m_fontVector.push_back(record);
535void OCPNwxFontList::FreeAll(
void) {
537 for (
size_t i = 0; i < m_fontVector.size(); i++) {
542 m_fontVector.clear();
545static wxString FontCandidates[] = {_(
"AISTargetAlert"),
548 _(
"AIS Target Name"),
550 _(
"RouteLegInfoRollover"),
551 _(
"ExtendedTideIcon"),
556 _(
"TideCurrentGraphRollover"),
566 wxString now_locale = g_locale;
567 wxArrayString string_array;
570 wxArrayString candidateArray;
575 wxString candidate = FontCandidates[i];
576 if (candidate == _T(
"END_OF_LIST")) {
580 candidateArray.Add(candidate);
585 for (
unsigned int i = 0; i < m_AuxKeyArray.GetCount(); i++) {
586 candidateArray.Add(m_AuxKeyArray[i]);
589 for (
unsigned int i = 0; i < candidateArray.GetCount(); i++) {
590 wxString candidate = candidateArray[i];
598 wxString trans = wxGetTranslation(candidate);
601 auto node = m_fontlist->GetFirst();
603 pmfd = node->GetData();
605 if (tlocale == now_locale) {
611 node = node->GetNext();
621 auto node = m_fontlist->GetFirst();
623 pmfd = node->GetData();
625 if (tlocale == now_locale) {
627 for (
unsigned int i = 0; i < string_array.GetCount(); i++) {
639 node = node->GetNext();
643 node = m_fontlist->GetFirst();
645 pmfd = node->GetData();
647 bool bd = m_fontlist->DeleteObject(pmfd);
648 if (bd) node = m_fontlist->GetFirst();
650 node = node->GetNext();
657 wxString candidate = FontCandidates[i];
658 if (candidate == _T(
"END_OF_LIST")) {
662 GetFont(wxGetTranslation(candidate), g_default_font_size);
669 for (
unsigned int i = 0; i < m_AuxKeyArray.GetCount(); i++) {
670 if (m_AuxKeyArray[i] == key)
return false;
672 m_AuxKeyArray.Add(key);
678 int size = g_default_font_size ? g_default_font_size : GetSystemFontSize();
679 wxString face = g_default_font_facename.Length() ? g_default_font_facename
680 : GetSystemFontFaceName();
681 wxString native = GetSimpleNativeFont(size, face);
682 wxFont *defaultFont = wxFont::New(native);
688 desc->
m_font = defaultFont;
wxFont * FindOrCreateFont(int point_size, wxFontFamily family, wxFontStyle style, wxFontWeight weight, bool underline=false, const wxString &facename=wxEmptyString, wxFontEncoding encoding=wxFONTENCODING_DEFAULT)
Creates or finds a matching font in the font cache.
wxString GetFullConfigDesc(int i) const
Gets description of font at index i.
wxColour GetFontColor(const wxString &TextElement) const
Gets the text color for a UI element.
bool AddAuxKey(wxString key)
Adds new plugin-defined font configuration key.
bool ResetFontToDefault(const wxString &TextElement)
Resets the font configuration for a UI element back to system defaults.
void LoadFontNative(wxString *pConfigString, wxString *pNativeDesc)
Loads font settings from a string descriptor.
bool SetFontColor(const wxString &TextElement, const wxColour color) const
Sets the text color for a UI element.
static wxString GetFontConfigKey(const wxString &description)
Creates configuration key from UI element name by combining locale with hash.
bool SetFont(const wxString &TextElement, wxFont *pFont, wxColour color)
Sets the default font properties for a UI element.
int GetNumFonts(void) const
Gets the total number of font configurations currently loaded.
wxColour GetDefaultFontColor(const wxString &TextElement)
Gets the default text color for a UI element.
void ScrubList()
Cleans up stale font entries after a locale change.
wxArrayString GetDialogStrings(const wxString &locale=wxEmptyString) const
Gets the list of unique dialog strings.
MyFontDesc * FindFontByConfigString(wxString pConfigString)
Finds font descriptor by its configuration key.
const wxString & GetNativeDesc(int i) const
Gets the native font descriptor string for the font at index i.
const wxString & GetConfigString(int i) const
Gets the locale-specific configuration key for a font at index i.
wxFont * GetFont(const wxString &TextElement, int requested_font_size=0)
Gets a font object for a UI element.
const wxString & GetDialogString(int i) const
Gets the UI element identifier string for the font at index i.
wxString m_nativeInfo
Platform-specific font descriptor string.
wxString m_dialogstring
UI element identifier, e.g., "AISTargetAlert", "StatusBar".
wxString m_configstring
Configuration key in "locale-hash" format.
wxFont * m_font
Font object.
wxColour m_color
Text color.
bool m_is_default
Indicates if this is the default font entry for the TextElement.
PlugIn Object Definition/API.