28#include <wx/tokenzr.h>
32#include "OCPNPlatform.h"
38 wxFontStyle style_req;
39 wxFontWeight weight_req;
46 wxFont *FindOrCreateFont(
int pointSize, wxFontFamily family,
47 wxFontStyle style, wxFontWeight weight,
48 bool underline =
false,
49 const wxString &face = wxEmptyString,
50 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
55 wxFontStyle style, wxFontWeight weight,
bool underline,
56 const wxString &facename, wxFontEncoding encoding);
58 std::vector<font_cache_record> m_fontVector;
61extern wxString g_locale;
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;
102 pDefFont = FindOrCreateFont(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
103 wxFONTWEIGHT_BOLD, FALSE, wxString(_T (
"" )),
104 wxFONTENCODING_SYSTEM);
108 for (
auto it = m_fontlist->begin(); it != m_fontlist->end(); it++)
delete *it;
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(
"-");
143 const collate<char> &coll = use_facet<collate<char> >(loc);
148 wxCharBuffer abuf = description.ToUTF8();
150 int fdLen = strlen(abuf);
153 wxString::Format(
"%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 {
187wxFont *FontMgr::GetFontLegacy(
const wxString &TextElement,
188 int user_default_size) {
190 for (
auto node = m_fontlist->begin(); node != m_fontlist->end(); ++node) {
197 return GetFont(TextElement, user_default_size);
202 for (
auto node = m_fontlist->begin(); node != m_fontlist->end(); ++node) {
207 if (requested_font_size == 0 && IsDefaultFontEntry(pmfd)) {
212 }
else if (requested_font_size != 0 &&
213 pmfd->
m_font->GetPointSize() == requested_font_size) {
226 if (0 == requested_font_size) {
227 new_size = g_default_font_size ? g_default_font_size : GetSystemFontSize();
229 new_size = requested_font_size;
231 wxString face_name = g_default_font_facename.Length()
232 ? g_default_font_facename
233 : GetSystemFontFaceName();
235 wxString nativefont = GetSimpleNativeFont(new_size, face_name);
236 wxFont *nf = wxFont::New(nativefont);
240 bool is_default = (requested_font_size == 0);
242 new MyFontDesc(TextElement, configkey, nf, color, is_default);
243 m_fontlist->push_back(pnewfd);
248MyFontDesc *FontMgr::GetFontDesc(
const wxString &TextElement)
const {
250 for (
auto node = m_fontlist->begin(); node != m_fontlist->end(); ++node) {
259 for (
auto node = m_fontlist->begin(); node != m_fontlist->end(); ++node) {
270 wxColor defaultColor = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
273 if (TextElement.IsSameAs(_(
"Console Legend")))
274 defaultColor = wxColour(0, 255, 0);
275 else if (TextElement.IsSameAs(_(
"Console Value")))
276 defaultColor = wxColour(0, 255, 0);
277 else if (TextElement.IsSameAs(_(
"Marks")))
278 defaultColor = wxColour(0, 0, 0);
279 else if (TextElement.IsSameAs(_(
"RouteLegInfoRollover")))
280 defaultColor = wxColour(0, 0, 0);
281 else if (TextElement.IsSameAs(_(
"AISRollover")))
282 defaultColor = wxColour(0, 0, 0);
283 else if (TextElement.IsSameAs(_(
"ExtendedTideIcon")))
284 defaultColor = wxColour(0, 0, 0);
285 else if (TextElement.IsSameAs(_(
"ChartTexts")))
286 defaultColor = wxColour(0, 0, 0);
287 else if (TextElement.IsSameAs(_(
"AIS Target Name")))
288 defaultColor = wxColour(0, 0, 0);
291 return wxColour(0, 0, 0);
297wxString FontMgr::GetSimpleNativeFont(
int size, wxString face) {
302 nativefont = wxFont(size, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
303 wxFONTWEIGHT_NORMAL,
false, face)
304 .GetNativeFontInfoDesc();
332 auto it = m_fontlist->begin();
334 return (*it)->m_configstring;
338 auto it = m_fontlist->begin();
340 return (*it)->m_dialogstring;
344 std::set<wxString> uniqueStrings;
346 for (
auto node = m_fontlist->begin(); node != m_fontlist->end(); ++node) {
348 if (locale.IsEmpty() || pmfd->
m_configstring.BeforeFirst(
'-') == locale) {
352 wxArrayString strings;
353 strings.reserve(uniqueStrings.size());
354 for (
const auto &str : uniqueStrings) {
362 auto it = m_fontlist->begin();
364 return (*it)->m_nativeInfo;
368 auto it = m_fontlist->begin();
372 ret.Append(_T (
":" ));
374 ret.Append(_T (
":" ));
376 wxString cols(
"rgb(0,0,0)");
377 if (pfd->
m_color.IsOk()) cols = pfd->
m_color.GetAsString(wxC2S_CSS_SYNTAX);
385 for (
auto node = m_fontlist->begin(); node != m_fontlist->end(); ++node) {
397 wxStringTokenizer tk(*pNativeDesc, _T (
":" ));
398 wxString dialogstring = tk.GetNextToken();
399 wxString nativefont = tk.GetNextToken();
401 wxString c = tk.GetNextToken();
405 auto node = m_fontlist->begin();
406 for (; node != m_fontlist->end(); ++node) {
419 if (node == m_fontlist->end()) {
420 wxFont *nf0 =
new wxFont();
422#ifdef __OCPN__ANDROID__
423 wxFont *nf =
new wxFont(nativefont);
425 wxFont *nf = nf0->New(nativefont);
428 double font_size = nf->GetPointSize();
429 wxString s = nf->GetNativeFontInfoDesc();
433 wxString face = nf->GetFaceName();
434 const wxChar *t = face.c_str();
437 wxString substitute_native = GetSimpleNativeFont(12,
"");
438 nf = nf0->New(substitute_native);
444 new MyFontDesc(dialogstring, *pConfigString, nf, color);
445 m_fontlist->push_back(pnewfd);
450 wxFontStyle style, wxFontWeight weight,
451 bool underline,
const wxString &facename,
452 wxFontEncoding encoding) {
454 return m_wxFontCache->FindOrCreateFont(point_size, family, style, weight,
455 underline, facename, encoding);
459 wxFontFamily family, wxFontStyle style,
460 wxFontWeight weight,
bool underline,
461 const wxString &facename,
462 wxFontEncoding encoding) {
463 if (record.pointsize_req == pointSize && record.style_req == style &&
464 record.weight_req == weight && record.underline_req == underline) {
467 wxFont *font = record.font;
473 if (!facename.empty()) {
474 const wxString &fontFace = font->GetFaceName();
477 same = !fontFace || fontFace == facename;
479 same = font->GetFamily() == family;
481 if (same && (encoding != wxFONTENCODING_DEFAULT)) {
483 same = font->GetEncoding() == encoding;
490wxFont *OCPNwxFontList::FindOrCreateFont(
int pointSize, wxFontFamily family,
491 wxFontStyle style, wxFontWeight weight,
493 const wxString &facename,
494 wxFontEncoding encoding) {
503 if (family == wxFONTFAMILY_DEFAULT) family = wxFONTFAMILY_SWISS;
507 for (
size_t i = 0; i < m_fontVector.size(); i++) {
509 if (isCached(record, pointSize, family, style, weight, underline, facename,
519 weight, underline, facename, encoding);
520 if (fontTmp.IsOk()) {
521 font =
new wxFont(fontTmp);
524 record.pointsize_req = pointSize;
525 record.style_req = style;
526 record.weight_req = weight;
527 record.underline_req = underline;
528 m_fontVector.push_back(record);
534void OCPNwxFontList::FreeAll(
void) {
536 for (
size_t i = 0; i < m_fontVector.size(); i++) {
541 m_fontVector.clear();
544static wxString FontCandidates[] = {_(
"AISTargetAlert"),
547 _(
"AIS Target Name"),
549 _(
"RouteLegInfoRollover"),
550 _(
"ExtendedTideIcon"),
555 _(
"TideCurrentGraphRollover"),
565 wxString now_locale = g_locale;
566 wxArrayString string_array;
569 wxArrayString candidateArray;
574 wxString candidate = FontCandidates[i];
575 if (candidate ==
"END_OF_LIST") {
579 candidateArray.Add(candidate);
584 for (
unsigned int i = 0; i < m_AuxKeyArray.GetCount(); i++) {
585 candidateArray.Add(m_AuxKeyArray[i]);
588 for (
unsigned int i = 0; i < candidateArray.GetCount(); i++) {
589 wxString candidate = candidateArray[i];
597 wxString trans = wxGetTranslation(candidate);
599 for (
auto node = m_fontlist->begin(); node != m_fontlist->end(); node++) {
602 if (tlocale == now_locale) {
615 for (
auto node = m_fontlist->begin(); node != m_fontlist->end(); node++) {
618 if (tlocale == now_locale) {
620 for (
unsigned int i = 0; i < string_array.GetCount(); i++) {
634 auto node = m_fontlist->begin();
635 while (node != m_fontlist->end()) {
638 auto found = std::find(m_fontlist->begin(), m_fontlist->end(), pmfd);
639 if (found != m_fontlist->end()) m_fontlist->erase(found);
640 node = m_fontlist->begin();
650 wxString candidate = FontCandidates[i];
651 if (candidate ==
"END_OF_LIST") {
655 GetFont(wxGetTranslation(candidate));
661 for (
unsigned int i = 0; i < m_AuxKeyArray.GetCount(); i++) {
662 if (m_AuxKeyArray[i] == key)
return false;
664 m_AuxKeyArray.Add(key);
670 int size = g_default_font_size ? g_default_font_size : GetSystemFontSize();
671 wxString face = g_default_font_facename.Length() ? g_default_font_facename
672 : GetSystemFontFaceName();
673 wxString native = GetSimpleNativeFont(size, face);
674 wxFont *defaultFont = wxFont::New(native);
680 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.
Global variables stored in configuration file.
PlugIn Object Definition/API.
double OCPN_GetDisplayContentScaleFactor()
Gets content scaling factor for current display.