From a0e915acb1086756023672317d5344c460d7b190 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Sat, 1 Mar 2014 16:48:58 +0000 Subject: [PATCH] using a lazy translation hash map for conversion between user friendly names and postscript names, so that we get only one warning per font family. The only way to completely get rid of this would be to pre-populate the map during startup which would mean to completely iterate through all installed fonts, so it's a compromise. see #15999 git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76036 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/fontutil.h | 3 +++ src/osx/carbon/font.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/include/wx/fontutil.h b/include/wx/fontutil.h index a4e0a7d4d5..0915e63dc0 100644 --- a/include/wx/fontutil.h +++ b/include/wx/fontutil.h @@ -154,6 +154,9 @@ public: void Free(); void EnsureValid(); + + static void UpdateNamesMap(const wxString& familyname, CTFontDescriptorRef descr); + static void UpdateNamesMap(const wxString& familyname, CTFontRef font); bool m_descriptorValid; diff --git a/src/osx/carbon/font.cpp b/src/osx/carbon/font.cpp index 7dd90920d1..8b5a518688 100644 --- a/src/osx/carbon/font.cpp +++ b/src/osx/carbon/font.cpp @@ -199,6 +199,8 @@ wxFontRefData::wxFontRefData(const wxFontRefData& data) : wxGDIRefData() // implementation // ============================================================================ +wxStringToStringHashMap gs_FontFamilyToPSName; + // ---------------------------------------------------------------------------- // wxFontRefData // ---------------------------------------------------------------------------- @@ -466,7 +468,16 @@ void wxFontRefData::MacFindFont() m_ctFont = fontcache[ std::wstring(lookupnameWithSize.wc_str()) ]; if ( !m_ctFont ) { - m_ctFont.reset(CTFontCreateWithName( wxCFStringRef(m_info.m_faceName), m_info.m_pointSize , NULL )); + wxCFStringRef fontname(m_info.m_faceName); + + wxStringToStringHashMap::const_iterator it = gs_FontFamilyToPSName.find(m_info.m_faceName); + + if ( it != gs_FontFamilyToPSName.end() ) + fontname = it->second; + else + fontname = m_info.m_faceName; + + m_ctFont.reset(CTFontCreateWithName( fontname, m_info.m_pointSize , NULL )); if ( m_ctFont.get() == NULL ) { // TODO try fallbacks according to font type @@ -474,6 +485,10 @@ void wxFontRefData::MacFindFont() } else { + if ( it == gs_FontFamilyToPSName.end() ) + { + m_info.UpdateNamesMap(m_info.m_faceName, m_ctFont); + } if ( traits != 0 ) { // attempt native font variant, if not available, fallback to italic emulation mode and remove bold @@ -501,7 +516,7 @@ void wxFontRefData::MacFindFont() if ( fontWithTraits == NULL ) { - fontWithTraits = CTFontCreateWithName( wxCFStringRef(m_info.m_faceName), m_info.m_pointSize, remainingTransform ); + fontWithTraits = CTFontCreateWithName( fontname, m_info.m_pointSize, remainingTransform ); } } @@ -1014,6 +1029,8 @@ void wxNativeFontInfo::Init(CTFontDescriptorRef descr) wxCFStringRef familyName( (CFStringRef) CTFontDescriptorCopyAttribute(descr, kCTFontFamilyNameAttribute)); m_faceName = familyName.AsString(); + + UpdateNamesMap(m_faceName, descr); } void wxNativeFontInfo::EnsureValid() @@ -1306,4 +1323,23 @@ void wxNativeFontInfo::SetStrikethrough(bool WXUNUSED(strikethrough)) { } +void wxNativeFontInfo::UpdateNamesMap(const wxString& familyName, CTFontDescriptorRef descr) +{ + if ( gs_FontFamilyToPSName.find(familyName) == gs_FontFamilyToPSName.end() ) + { + wxCFStringRef psName( (CFStringRef) CTFontDescriptorCopyAttribute(descr, kCTFontNameAttribute)); + gs_FontFamilyToPSName[familyName] = psName.AsString(); + } +} + +void wxNativeFontInfo::UpdateNamesMap(const wxString& familyName, CTFontRef font) +{ + if ( gs_FontFamilyToPSName.find(familyName) == gs_FontFamilyToPSName.end() ) + { + wxCFRef descr(CTFontCopyFontDescriptor( font )); + UpdateNamesMap(familyName, descr); + } +} + +