26 #include <wx/gdicmn.h>
27 #include <wx/tokenzr.h>
30 #include "OCPNPlatform.h"
36 wxFontStyle style_req;
37 wxFontWeight weight_req;
44 wxFont *FindOrCreateFont(
int pointSize, wxFontFamily family,
45 wxFontStyle style, wxFontWeight weight,
46 bool underline =
false,
47 const wxString &face = wxEmptyString,
48 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
53 wxFontStyle style, wxFontWeight weight,
54 bool underline,
const wxString &facename,
55 wxFontEncoding encoding);
57 std::vector<font_cache_record> m_fontVector;
60 extern wxString g_locale;
63 int g_default_font_size;
64 wxString g_default_font_facename;
66 FontMgr *FontMgr::instance = NULL;
69 if (!instance) instance =
new FontMgr;
73 void FontMgr::Shutdown() {
80 FontMgr::FontMgr() : m_wxFontCache(NULL), m_fontlist(NULL), pDefFont(NULL) {
82 m_fontlist =
new FontList;
83 m_fontlist->DeleteContents(
true);
88 pDefFont = FindOrCreateFont(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
89 wxFONTWEIGHT_BOLD, FALSE, wxString(_T (
"" )),
90 wxFONTENCODING_SYSTEM);
100 void FontMgr::SetLocale(wxString &newLocale) { s_locale = newLocale; }
102 wxColour FontMgr::GetFontColor(
const wxString &TextElement)
const {
106 auto node = m_fontlist->GetFirst();
108 pmfd = node->GetData();
109 if (pmfd->m_dialogstring == TextElement) {
110 if (pmfd->m_configstring.BeforeFirst(
'-') == s_locale)
111 return pmfd->m_color;
113 node = node->GetNext();
116 return wxColour(0, 0, 0);
119 bool FontMgr::SetFontColor(
const wxString &TextElement,
120 const wxColour color)
const {
123 auto node = m_fontlist->GetFirst();
125 pmfd = node->GetData();
126 if (pmfd->m_dialogstring == TextElement) {
127 if (pmfd->m_configstring.BeforeFirst(
'-') == s_locale) {
128 pmfd->m_color = color;
132 node = node->GetNext();
138 wxString FontMgr::GetFontConfigKey(
const wxString &description) {
144 configkey = s_locale;
145 configkey.Append(_T(
"-"));
149 const collate<char> &coll = use_facet<collate<char> >(loc);
154 wxCharBuffer abuf = description.ToUTF8();
156 int fdLen = strlen(abuf);
158 configkey.Append(wxString::Format(
159 _T(
"%08lx"), coll.hash(abuf.data(), abuf.data() + fdLen)));
163 wxFont *FontMgr::GetFont(
const wxString &TextElement,
int user_default_size) {
166 auto node = m_fontlist->GetFirst();
168 pmfd = node->GetData();
169 if (pmfd->m_dialogstring == TextElement) {
170 if (pmfd->m_configstring.BeforeFirst(
'-') == s_locale)
173 node = node->GetNext();
177 wxString configkey = GetFontConfigKey(TextElement);
183 wxFont sys_font = *wxNORMAL_FONT;
184 int sys_font_size = sys_font.GetPointSize();
185 wxString FaceName = sys_font.GetFaceName();
188 if (0 == user_default_size) {
189 if (g_default_font_size)
190 new_size = g_default_font_size;
192 new_size = sys_font_size;
194 new_size = user_default_size;
196 if (g_default_font_facename.Length()) FaceName = g_default_font_facename;
198 wxString nativefont = GetSimpleNativeFont(new_size, FaceName);
199 wxFont *nf = wxFont::New(nativefont);
201 wxColor color = GetDefaultFontColor( TextElement);
204 m_fontlist->Append(pnewfd);
206 return pnewfd->m_font;
209 wxColour FontMgr::GetDefaultFontColor(
const wxString &TextElement ){
210 wxColor defaultColor = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
213 if(TextElement.IsSameAs( _(
"Console Legend")) )
214 defaultColor = wxColour( 0, 255, 0);
215 else if(TextElement.IsSameAs( _(
"Console Value")) )
216 defaultColor = wxColour( 0, 255, 0);
217 else if(TextElement.IsSameAs( _(
"Marks")) )
218 defaultColor = wxColour( 0, 0, 0);
219 else if(TextElement.IsSameAs( _(
"RouteLegInfoRollover")) )
220 defaultColor = wxColour( 0, 0, 0);
221 else if(TextElement.IsSameAs( _(
"AISRollover")) )
222 defaultColor = wxColour( 0, 0, 0);
223 else if(TextElement.IsSameAs( _(
"ExtendedTideIcon")) )
224 defaultColor = wxColour( 0, 0, 0);
225 else if(TextElement.IsSameAs( _(
"ChartTexts")) )
226 defaultColor = wxColour( 0, 0, 0);
227 else if(TextElement.IsSameAs( _(
"AIS Target Name")) )
228 defaultColor = wxColour( 0, 0, 0);
231 return wxColour(0,0,0);
237 wxString FontMgr::GetSimpleNativeFont(
int size, wxString face) {
242 nativefont = wxFont(size, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
243 wxFONTWEIGHT_NORMAL,
false, face)
244 .GetNativeFontInfoDesc();
249 bool FontMgr::SetFont(
const wxString &TextElement, wxFont *pFont,
253 auto node = m_fontlist->GetFirst();
255 pmfd = node->GetData();
256 if (pmfd->m_dialogstring == TextElement) {
257 if (pmfd->m_configstring.BeforeFirst(
'-') == s_locale) {
267 pmfd->m_font = pFont;
268 pmfd->m_nativeInfo = pFont->GetNativeFontInfoDesc();
269 pmfd->m_color = color;
274 node = node->GetNext();
280 int FontMgr::GetNumFonts(
void)
const {
return m_fontlist->GetCount(); }
282 const wxString &FontMgr::GetConfigString(
int i)
const {
283 MyFontDesc *pfd = m_fontlist->Item(i)->GetData();
284 return pfd->m_configstring;
287 const wxString &FontMgr::GetDialogString(
int i)
const {
288 MyFontDesc *pfd = m_fontlist->Item(i)->GetData();
289 return pfd->m_dialogstring;
292 const wxString &FontMgr::GetNativeDesc(
int i)
const {
293 MyFontDesc *pfd = m_fontlist->Item(i)->GetData();
294 return pfd->m_nativeInfo;
297 wxString FontMgr::GetFullConfigDesc(
int i)
const {
298 MyFontDesc *pfd = m_fontlist->Item(i)->GetData();
299 wxString ret = pfd->m_dialogstring;
300 ret.Append(_T (
":" ));
301 ret.Append(pfd->m_nativeInfo);
302 ret.Append(_T (
":" ));
304 wxString cols(_T(
"rgb(0,0,0)"));
305 if (pfd->m_color.IsOk()) cols = pfd->m_color.GetAsString(wxC2S_CSS_SYNTAX);
311 MyFontDesc *FontMgr::FindFontByConfigString(wxString pConfigString) {
314 auto node = m_fontlist->GetFirst();
317 pmfd = node->GetData();
318 if (pmfd->m_configstring == pConfigString) {
321 node = node->GetNext();
327 void FontMgr::LoadFontNative(wxString *pConfigString, wxString *pNativeDesc) {
330 wxStringTokenizer tk(*pNativeDesc, _T (
":" ));
331 wxString dialogstring = tk.GetNextToken();
332 wxString nativefont = tk.GetNextToken();
334 wxString c = tk.GetNextToken();
339 auto node = m_fontlist->GetFirst();
342 pmfd = node->GetData();
343 if (pmfd->m_configstring == *pConfigString) {
344 if (pmfd->m_configstring.BeforeFirst(
'-') == g_locale) {
345 pmfd->m_nativeInfo = nativefont;
346 wxFont *nf = pmfd->m_font->New(pmfd->m_nativeInfo);
351 node = node->GetNext();
356 wxFont *nf0 =
new wxFont();
358 #ifdef __OCPN__ANDROID__
359 wxFont *nf =
new wxFont(nativefont);
361 wxFont *nf = nf0->New(nativefont);
364 double font_size = nf->GetPointSize();
365 wxString s = nf->GetNativeFontInfoDesc();
369 wxString face = nf->GetFaceName();
370 const wxChar *t = face.c_str();
373 wxString substitute_native = GetSimpleNativeFont(12, _T(
""));
374 nf = nf0->New(substitute_native);
380 new MyFontDesc(dialogstring, *pConfigString, nf, color);
381 m_fontlist->Append(pnewfd);
385 wxFont *FontMgr::FindOrCreateFont(
int point_size, wxFontFamily family,
386 wxFontStyle style, wxFontWeight weight,
387 bool underline,
const wxString &facename,
388 wxFontEncoding encoding) {
390 return m_wxFontCache->FindOrCreateFont(point_size, family, style, weight,
391 underline, facename, encoding);
394 bool OCPNwxFontList::isCached(
font_cache_record& record,
int pointSize, wxFontFamily family,
395 wxFontStyle style, wxFontWeight weight,
396 bool underline,
const wxString &facename,
397 wxFontEncoding encoding) {
398 if (record.pointsize_req == pointSize && record.style_req == style &&
399 record.weight_req == weight && record.underline_req == underline) {
402 wxFont *font = record.font;
408 if (!facename.empty()) {
409 const wxString &fontFace = font->GetFaceName();
412 same = !fontFace || fontFace == facename;
414 same = font->GetFamily() == family;
416 if (same && (encoding != wxFONTENCODING_DEFAULT)) {
418 same = font->GetEncoding() == encoding;
425 wxFont *OCPNwxFontList::FindOrCreateFont(
int pointSize, wxFontFamily family,
426 wxFontStyle style, wxFontWeight weight,
428 const wxString &facename,
429 wxFontEncoding encoding) {
438 if (family == wxFONTFAMILY_DEFAULT) family = wxFONTFAMILY_SWISS;
442 for (
size_t i=0; i < m_fontVector.size() ; i++){
444 if (isCached(record, pointSize, family, style, weight, underline, facename,
453 wxFont fontTmp(OCPN_GetDisplayContentScaleFactor() * pointSize,
454 family, style, weight, underline, facename, encoding);
455 if (fontTmp.IsOk()) {
456 font =
new wxFont(fontTmp);
459 record.pointsize_req = pointSize;
460 record.style_req = style;
461 record.weight_req = weight;
462 record.underline_req = underline;
463 m_fontVector.push_back(record);
469 void OCPNwxFontList::FreeAll(
void) {
471 for (
size_t i=0; i < m_fontVector.size() ; i++){
476 m_fontVector.clear();
479 static wxString FontCandidates[] = {_T(
"AISTargetAlert"),
480 _T(
"AISTargetQuery"),
482 _T(
"AIS Target Name" ),
484 _T(
"RouteLegInfoRollover"),
485 _T(
"ExtendedTideIcon"),
487 _T(
"Console Legend"),
490 _T(
"TideCurrentGraphRollover"),
499 void FontMgr::ScrubList() {
500 wxString now_locale = g_locale;
501 wxArrayString string_array;
504 wxArrayString candidateArray;
509 wxString candidate = FontCandidates[i];
510 if (candidate == _T(
"END_OF_LIST")) {
514 candidateArray.Add(candidate);
519 for (
unsigned int i = 0; i < m_AuxKeyArray.GetCount(); i++) {
520 candidateArray.Add(m_AuxKeyArray[i]);
523 for (
unsigned int i = 0; i < candidateArray.GetCount(); i++) {
524 wxString candidate = candidateArray[i];
532 wxString trans = wxGetTranslation(candidate);
535 auto node = m_fontlist->GetFirst();
537 pmfd = node->GetData();
538 wxString tlocale = pmfd->m_configstring.BeforeFirst(
'-');
539 if (tlocale == now_locale) {
540 if (trans == pmfd->m_dialogstring) {
541 string_array.Add(pmfd->m_dialogstring);
545 node = node->GetNext();
555 auto node = m_fontlist->GetFirst();
557 pmfd = node->GetData();
558 wxString tlocale = pmfd->m_configstring.BeforeFirst(
'-');
559 if (tlocale == now_locale) {
561 for (
unsigned int i = 0; i < string_array.GetCount(); i++) {
562 if (string_array[i] == pmfd->m_dialogstring) {
568 pmfd->m_dialogstring = _T(
"");
569 pmfd->m_configstring = _T(
"");
573 node = node->GetNext();
577 node = m_fontlist->GetFirst();
579 pmfd = node->GetData();
580 if (pmfd->m_dialogstring == _T(
"")) {
581 bool bd = m_fontlist->DeleteObject(pmfd);
582 if (bd) node = m_fontlist->GetFirst();
584 node = node->GetNext();
591 wxString candidate = FontCandidates[i];
592 if (candidate == _T(
"END_OF_LIST")) {
596 GetFont(wxGetTranslation(candidate), g_default_font_size);
602 bool FontMgr::AddAuxKey(wxString key) {
603 for (
unsigned int i = 0; i < m_AuxKeyArray.GetCount(); i++) {
604 if (m_AuxKeyArray[i] == key)
return false;
606 m_AuxKeyArray.Add(key);