From 07c9f62e40851968c9166a2cbb1504653c561cb5 Mon Sep 17 00:00:00 2001 From: spx Date: Sun, 11 Oct 2009 13:03:20 +0000 Subject: [PATCH] Fixed: Wrong font is used due to the font caching problem. --- Library/MeterString.cpp | 26 ++++++++++++++++++-------- Library/MeterString.h | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Library/MeterString.cpp b/Library/MeterString.cpp index 6183097b..a0bba4e0 100644 --- a/Library/MeterString.cpp +++ b/Library/MeterString.cpp @@ -164,7 +164,8 @@ void CMeterString::Initialize() REAL size = (REAL)m_FontSize * (96.0f / (REAL)dpi); - std::map::iterator iter2 = c_Fonts.find(FontPropertiesToString(size, style)); + std::wstring properties = FontPropertiesToString(m_FontFamily, size, style); + std::map::iterator iter2 = c_Fonts.find(properties); if (iter2 != c_Fonts.end()) { m_Font = (*iter2).second; @@ -179,13 +180,13 @@ void CMeterString::Initialize() { m_Font = new Gdiplus::Font(FontFamily::GenericSansSerif(), size, style); } - c_Fonts[FontPropertiesToString(size, style)] = m_Font; - } + c_Fonts[properties] = m_Font; - Status status = m_Font->GetLastStatus(); - if(Ok != status) - { - throw CError(std::wstring(L"Unable to create font: ") + m_FontFace, __LINE__, __FILE__); + Status status = m_Font->GetLastStatus(); + if(Ok != status) + { + throw CError(std::wstring(L"Unable to create font: ") + m_FontFace, __LINE__, __FILE__); + } } } @@ -579,10 +580,19 @@ void CMeterString::FreeFontCache() ** Static helper to convert font properties to a string so it can be used as a key for the cache map. ** */ -std::wstring CMeterString::FontPropertiesToString(REAL size, FontStyle style) +std::wstring CMeterString::FontPropertiesToString(FontFamily* fontFamily, REAL size, FontStyle style) { std::wstringstream stream; stream << size << L"-" << (int)style; + + if (fontFamily) + { + WCHAR familyName[LF_FACESIZE]; + if (Ok == fontFamily->GetFamilyName(familyName)) + { + return std::wstring(familyName) + L"-" + stream.str(); + } + } return stream.str(); } diff --git a/Library/MeterString.h b/Library/MeterString.h index 1d666a65..fb7ced42 100644 --- a/Library/MeterString.h +++ b/Library/MeterString.h @@ -86,7 +86,7 @@ private: std::vector m_MeasureNames; std::vector m_Measures; - static std::wstring FontPropertiesToString(Gdiplus::REAL size, Gdiplus::FontStyle style); + static std::wstring FontPropertiesToString(Gdiplus::FontFamily* fontFamily, Gdiplus::REAL size, Gdiplus::FontStyle style); static std::map c_FontFamilies; // Cache for the font families static std::map c_Fonts; // Cache for the fonts };