Skip to content

Commit fa88c95

Browse files
committed
Improve font lookup in wxPdfFontManager
On the [wxWidgets forum](https://forums.wxwidgets.org/viewtopic.php?f=30&t=51732) a problem was reported about selecting the wrong font. The user had registered a font with regular style under the font family name "Arial" (which is also an alias for the font family "Helvetica"). Thereafter the user tried to select the font "Arial" with bold style. However, the registered font with regular style was selected. This happened, because registering the font "Arial" with regular style created the font family "Arial", hiding the equivalent font family "Helvetica" under its alias family name "Arial". The font lookup was improved to search also the font family registered under the font family alias.
1 parent 3d43def commit fa88c95

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and from version 0.9.0 onwards this project adheres to [Semantic Versioning](htt
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Improved font lookup in wxPdfFontManager to search also under the font family alias, if given
13+
1014
## [1.2.0] - 2024-02-18
1115

1216
**wxPdfDocument** is compatible with wxWidgets versions 3.2.x and above.

src/pdffontmanager.cpp

+28-10
Original file line numberDiff line numberDiff line change
@@ -1086,31 +1086,49 @@ wxPdfFontManagerBase::GetFont(const wxString& fontName, int fontStyle) const
10861086
int searchStyle = (fontStyle & ~wxPDF_FONTSTYLE_DECORATION_MASK) & wxPDF_FONTSTYLE_MASK;
10871087
wxPdfFontData* fontData = NULL;
10881088

1089+
// 0. Determine whether a family alias exists
1090+
wxPdfFontFamilyMap::const_iterator familyAliasIter = m_fontFamilyMap.end();
1091+
wxPdfFontAliasMap::const_iterator aliasIter = m_fontAliasMap.find(lcFontName);
1092+
if (aliasIter != m_fontAliasMap.end())
1093+
{
1094+
familyAliasIter = m_fontFamilyMap.find(aliasIter->second);
1095+
}
1096+
10891097
// Check whether font name equals font family
10901098
wxPdfFontFamilyMap::const_iterator familyIter = m_fontFamilyMap.find(lcFontName);
10911099
if (familyIter == m_fontFamilyMap.end())
10921100
{
1093-
// 1. Check family alias if given name was not a family name
1094-
wxPdfFontAliasMap::const_iterator aliasIter = m_fontAliasMap.find(lcFontName);
1095-
if (aliasIter != m_fontAliasMap.end())
1096-
{
1097-
familyIter = m_fontFamilyMap.find(aliasIter->second);
1098-
}
1101+
// 1. Use family alias (may be empty)
1102+
familyIter = familyAliasIter;
10991103
}
11001104

11011105
if (familyIter != m_fontFamilyMap.end())
11021106
{
11031107
// 2. Check whether the family contains a font with the requested style
11041108
size_t n = familyIter->second.GetCount();
11051109
size_t j;
1106-
for (j = 0; j < n && fontData == NULL; ++j)
1110+
do
11071111
{
1108-
fontData = m_fontList[familyIter->second[j]]->GetFontData();
1109-
if (fontData->GetStyle() != searchStyle)
1112+
for (j = 0; j < n && fontData == NULL; ++j)
11101113
{
1111-
fontData = NULL;
1114+
fontData = m_fontList[familyIter->second[j]]->GetFontData();
1115+
if (fontData->GetStyle() != searchStyle)
1116+
{
1117+
fontData = NULL;
1118+
}
1119+
}
1120+
n = 0;
1121+
// Search in alias font family if not already searched
1122+
if (fontData == NULL && familyAliasIter != familyIter)
1123+
{
1124+
if (familyAliasIter != m_fontFamilyMap.end())
1125+
{
1126+
familyIter = familyAliasIter;
1127+
n = familyIter->second.GetCount();
1128+
}
11121129
}
11131130
}
1131+
while (n > 0);
11141132
}
11151133

11161134
if (fontData == NULL)

0 commit comments

Comments
 (0)