27#include <wx/tokenzr.h>
30#include "OCPNPlatform.h"
31#include "ocpn_plugin.h"
36 wxFontStyle style_req;
37 wxFontWeight weight_req;
43 wxFont *FindOrCreateFont(
int pointSize, wxFontFamily family,
44 wxFontStyle style, wxFontWeight weight,
45 bool underline =
false,
46 const wxString &face = wxEmptyString,
47 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
52 wxFontStyle style, wxFontWeight weight,
53 bool underline,
const wxString &facename,
54 wxFontEncoding encoding);
56 std::vector<font_cache_record> m_fontVector;
59extern wxString g_locale;
62int g_default_font_size;
63wxString g_default_font_facename;
65FontMgr *FontMgr::instance = NULL;
68 if (!instance) instance =
new FontMgr;
72void FontMgr::Shutdown() {
79FontMgr::FontMgr() : m_wxFontCache(NULL), m_fontlist(NULL), pDefFont(NULL) {
81 m_fontlist =
new FontList;
82 m_fontlist->DeleteContents(
true);
87 pDefFont = FindOrCreateFont(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
88 wxFONTWEIGHT_BOLD, FALSE, wxString(_T (
"" )),
89 wxFONTENCODING_SYSTEM);
99void FontMgr::SetLocale(wxString &newLocale) { s_locale = newLocale; }
101wxColour FontMgr::GetFontColor(
const wxString &TextElement)
const {
105 auto node = m_fontlist->GetFirst();
107 pmfd = node->GetData();
108 if (pmfd->m_dialogstring == TextElement) {
109 if (pmfd->m_configstring.BeforeFirst(
'-') == s_locale)
110 return pmfd->m_color;
112 node = node->GetNext();
115 return wxColour(0, 0, 0);
118bool FontMgr::SetFontColor(
const wxString &TextElement,
119 const wxColour color)
const {
122 auto node = m_fontlist->GetFirst();
124 pmfd = node->GetData();
125 if (pmfd->m_dialogstring == TextElement) {
126 if (pmfd->m_configstring.BeforeFirst(
'-') == s_locale) {
127 pmfd->m_color = color;
131 node = node->GetNext();
137wxString FontMgr::GetFontConfigKey(
const wxString &description) {
143 configkey = s_locale;
144 configkey.Append(_T(
"-"));
148 const collate<char> &coll = use_facet<collate<char> >(loc);
153 wxCharBuffer abuf = description.ToUTF8();
155 int fdLen = strlen(abuf);
157 configkey.Append(wxString::Format(
158 _T(
"%08lx"), coll.hash(abuf.data(), abuf.data() + fdLen)));
162wxFont *FontMgr::GetFont(
const wxString &TextElement,
int user_default_size) {
165 auto node = m_fontlist->GetFirst();
167 pmfd = node->GetData();
168 if (pmfd->m_dialogstring == TextElement) {
169 if (pmfd->m_configstring.BeforeFirst(
'-') == s_locale)
172 node = node->GetNext();
176 wxString configkey = GetFontConfigKey(TextElement);
182 wxFont sys_font = *wxNORMAL_FONT;
183 int sys_font_size = sys_font.GetPointSize();
184 wxString FaceName = sys_font.GetFaceName();
187 if (0 == user_default_size) {
188 if (g_default_font_size)
189 new_size = g_default_font_size;
191 new_size = sys_font_size;
193 new_size = user_default_size;
195 if (g_default_font_facename.Length()) FaceName = g_default_font_facename;
197 wxString nativefont = GetSimpleNativeFont(new_size, FaceName);
198 wxFont *nf = wxFont::New(nativefont);
200 wxColor color = GetDefaultFontColor( TextElement);
203 m_fontlist->Append(pnewfd);
205 return pnewfd->m_font;
208wxColour FontMgr::GetDefaultFontColor(
const wxString &TextElement ){
209 wxColor defaultColor = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
212 if(TextElement.IsSameAs(
"Console Legend") )
213 defaultColor = wxColour( 0, 255, 0);
214 if(TextElement.IsSameAs(
"Console Value") )
215 defaultColor = wxColour( 0, 255, 0);
219 return wxColour(0,0,0);
225wxString FontMgr::GetSimpleNativeFont(
int size, wxString face) {
230 nativefont = wxFont(size, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
231 wxFONTWEIGHT_NORMAL,
false, face)
232 .GetNativeFontInfoDesc();
237bool FontMgr::SetFont(
const wxString &TextElement, wxFont *pFont,
241 auto node = m_fontlist->GetFirst();
243 pmfd = node->GetData();
244 if (pmfd->m_dialogstring == TextElement) {
245 if (pmfd->m_configstring.BeforeFirst(
'-') == s_locale) {
255 pmfd->m_font = pFont;
256 pmfd->m_nativeInfo = pFont->GetNativeFontInfoDesc();
257 pmfd->m_color = color;
262 node = node->GetNext();
268int FontMgr::GetNumFonts(
void)
const {
return m_fontlist->GetCount(); }
270const wxString &FontMgr::GetConfigString(
int i)
const {
271 MyFontDesc *pfd = m_fontlist->Item(i)->GetData();
272 return pfd->m_configstring;
275const wxString &FontMgr::GetDialogString(
int i)
const {
276 MyFontDesc *pfd = m_fontlist->Item(i)->GetData();
277 return pfd->m_dialogstring;
280const wxString &FontMgr::GetNativeDesc(
int i)
const {
281 MyFontDesc *pfd = m_fontlist->Item(i)->GetData();
282 return pfd->m_nativeInfo;
285wxString FontMgr::GetFullConfigDesc(
int i)
const {
286 MyFontDesc *pfd = m_fontlist->Item(i)->GetData();
287 wxString ret = pfd->m_dialogstring;
288 ret.Append(_T (
":" ));
289 ret.Append(pfd->m_nativeInfo);
290 ret.Append(_T (
":" ));
292 wxString cols(_T(
"rgb(0,0,0)"));
293 if (pfd->m_color.IsOk()) cols = pfd->m_color.GetAsString(wxC2S_CSS_SYNTAX);
299MyFontDesc *FontMgr::FindFontByConfigString(wxString pConfigString) {
302 auto node = m_fontlist->GetFirst();
305 pmfd = node->GetData();
306 if (pmfd->m_configstring == pConfigString) {
309 node = node->GetNext();
315void FontMgr::LoadFontNative(wxString *pConfigString, wxString *pNativeDesc) {
318 wxStringTokenizer tk(*pNativeDesc, _T (
":" ));
319 wxString dialogstring = tk.GetNextToken();
320 wxString nativefont = tk.GetNextToken();
322 wxString c = tk.GetNextToken();
327 auto node = m_fontlist->GetFirst();
330 pmfd = node->GetData();
331 if (pmfd->m_configstring == *pConfigString) {
332 if (pmfd->m_configstring.BeforeFirst(
'-') == g_locale) {
333 pmfd->m_nativeInfo = nativefont;
334 wxFont *nf = pmfd->m_font->New(pmfd->m_nativeInfo);
339 node = node->GetNext();
344 wxFont *nf0 =
new wxFont();
346#ifdef __OCPN__ANDROID__
347 wxFont *nf =
new wxFont(nativefont);
349 wxFont *nf = nf0->New(nativefont);
352 double font_size = nf->GetPointSize();
353 wxString s = nf->GetNativeFontInfoDesc();
357 wxString face = nf->GetFaceName();
358 const wxChar *t = face.c_str();
361 wxString substitute_native = GetSimpleNativeFont(12, _T(
""));
362 nf = nf0->New(substitute_native);
368 new MyFontDesc(dialogstring, *pConfigString, nf, color);
369 m_fontlist->Append(pnewfd);
373wxFont *FontMgr::FindOrCreateFont(
int point_size, wxFontFamily family,
374 wxFontStyle style, wxFontWeight weight,
375 bool underline,
const wxString &facename,
376 wxFontEncoding encoding) {
378 return m_wxFontCache->FindOrCreateFont(point_size, family, style, weight,
379 underline, facename, encoding);
382bool OCPNwxFontList::isCached(
font_cache_record& record,
int pointSize, wxFontFamily family,
383 wxFontStyle style, wxFontWeight weight,
384 bool underline,
const wxString &facename,
385 wxFontEncoding encoding) {
386 if (record.pointsize_req == pointSize && record.style_req == style &&
387 record.weight_req == weight && record.underline_req == underline) {
390 wxFont *font = record.font;
396 if (!facename.empty()) {
397 const wxString &fontFace = font->GetFaceName();
400 same = !fontFace || fontFace == facename;
402 same = font->GetFamily() == family;
404 if (same && (encoding != wxFONTENCODING_DEFAULT)) {
406 same = font->GetEncoding() == encoding;
413wxFont *OCPNwxFontList::FindOrCreateFont(
int pointSize, wxFontFamily family,
414 wxFontStyle style, wxFontWeight weight,
416 const wxString &facename,
417 wxFontEncoding encoding) {
426 if (family == wxFONTFAMILY_DEFAULT) family = wxFONTFAMILY_SWISS;
430 for (
size_t i=0; i < m_fontVector.size() ; i++){
432 if (isCached(record, pointSize, family, style, weight, underline, facename,
441 wxFont fontTmp(OCPN_GetDisplayContentScaleFactor() * pointSize,
442 family, style, weight, underline, facename, encoding);
443 if (fontTmp.IsOk()) {
444 font =
new wxFont(fontTmp);
447 record.pointsize_req = pointSize;
448 record.style_req = style;
449 record.weight_req = weight;
450 record.underline_req = underline;
451 m_fontVector.push_back(record);
457void OCPNwxFontList::FreeAll(
void) {
459 for (
size_t i=0; i < m_fontVector.size() ; i++){
464 m_fontVector.clear();
467static wxString FontCandidates[] = {_T(
"AISTargetAlert"),
468 _T(
"AISTargetQuery"),
470 _T(
"AIS Target Name" ),
472 _T(
"RouteLegInfoRollover"),
473 _T(
"ExtendedTideIcon"),
475 _T(
"Console Legend"),
478 _T(
"TideCurrentGraphRollover"),
487void FontMgr::ScrubList() {
488 wxString now_locale = g_locale;
489 wxArrayString string_array;
492 wxArrayString candidateArray;
497 wxString candidate = FontCandidates[i];
498 if (candidate == _T(
"END_OF_LIST")) {
502 candidateArray.Add(candidate);
507 for (
unsigned int i = 0; i < m_AuxKeyArray.GetCount(); i++) {
508 candidateArray.Add(m_AuxKeyArray[i]);
511 for (
unsigned int i = 0; i < candidateArray.GetCount(); i++) {
512 wxString candidate = candidateArray[i];
520 wxString trans = wxGetTranslation(candidate);
523 auto node = m_fontlist->GetFirst();
525 pmfd = node->GetData();
526 wxString tlocale = pmfd->m_configstring.BeforeFirst(
'-');
527 if (tlocale == now_locale) {
528 if (trans == pmfd->m_dialogstring) {
529 string_array.Add(pmfd->m_dialogstring);
533 node = node->GetNext();
543 auto node = m_fontlist->GetFirst();
545 pmfd = node->GetData();
546 wxString tlocale = pmfd->m_configstring.BeforeFirst(
'-');
547 if (tlocale == now_locale) {
549 for (
unsigned int i = 0; i < string_array.GetCount(); i++) {
550 if (string_array[i] == pmfd->m_dialogstring) {
556 pmfd->m_dialogstring = _T(
"");
557 pmfd->m_configstring = _T(
"");
561 node = node->GetNext();
565 node = m_fontlist->GetFirst();
567 pmfd = node->GetData();
568 if (pmfd->m_dialogstring == _T(
"")) {
569 bool bd = m_fontlist->DeleteObject(pmfd);
570 if (bd) node = m_fontlist->GetFirst();
572 node = node->GetNext();
579 wxString candidate = FontCandidates[i];
580 if (candidate == _T(
"END_OF_LIST")) {
584 GetFont(wxGetTranslation(candidate), g_default_font_size);
590bool FontMgr::AddAuxKey(wxString key) {
591 for (
unsigned int i = 0; i < m_AuxKeyArray.GetCount(); i++) {
592 if (m_AuxKeyArray[i] == key)
return false;
594 m_AuxKeyArray.Add(key);