1 // Copyright 2014 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "xfa/fgas/font/cfgas_gefont.h"
8
9 #include <memory>
10 #include <utility>
11
12 #include "build/build_config.h"
13 #include "core/fpdfapi/font/cpdf_font.h"
14 #include "core/fxcrt/check.h"
15 #include "core/fxge/cfx_font.h"
16 #include "core/fxge/cfx_substfont.h"
17 #include "core/fxge/cfx_unicodeencodingex.h"
18 #include "core/fxge/fx_font.h"
19 #include "xfa/fgas/font/cfgas_fontmgr.h"
20 #include "xfa/fgas/font/cfgas_gemodule.h"
21 #include "xfa/fgas/font/fgas_fontutils.h"
22
23 // static
LoadFont(const wchar_t * pszFontFamily,uint32_t dwFontStyles,FX_CodePage wCodePage)24 RetainPtr<CFGAS_GEFont> CFGAS_GEFont::LoadFont(const wchar_t* pszFontFamily,
25 uint32_t dwFontStyles,
26 FX_CodePage wCodePage) {
27 #if BUILDFLAG(IS_WIN)
28 auto pFont = pdfium::MakeRetain<CFGAS_GEFont>();
29 if (!pFont->LoadFontInternal(pszFontFamily, dwFontStyles, wCodePage))
30 return nullptr;
31 return pFont;
32 #else
33 return CFGAS_GEModule::Get()->GetFontMgr()->GetFontByCodePage(
34 wCodePage, dwFontStyles, pszFontFamily);
35 #endif
36 }
37
38 // static
LoadFont(RetainPtr<CPDF_Font> pPDFFont)39 RetainPtr<CFGAS_GEFont> CFGAS_GEFont::LoadFont(RetainPtr<CPDF_Font> pPDFFont) {
40 auto pFont = pdfium::MakeRetain<CFGAS_GEFont>();
41 if (!pFont->LoadFontInternal(std::move(pPDFFont)))
42 return nullptr;
43
44 return pFont;
45 }
46
47 // static
LoadFont(std::unique_ptr<CFX_Font> pInternalFont)48 RetainPtr<CFGAS_GEFont> CFGAS_GEFont::LoadFont(
49 std::unique_ptr<CFX_Font> pInternalFont) {
50 auto pFont = pdfium::MakeRetain<CFGAS_GEFont>();
51 if (!pFont->LoadFontInternal(std::move(pInternalFont)))
52 return nullptr;
53
54 return pFont;
55 }
56
57 // static
LoadStockFont(CPDF_Document * pDoc,const ByteString & font_family)58 RetainPtr<CFGAS_GEFont> CFGAS_GEFont::LoadStockFont(
59 CPDF_Document* pDoc,
60 const ByteString& font_family) {
61 RetainPtr<CPDF_Font> stock_font =
62 CPDF_Font::GetStockFont(pDoc, font_family.AsStringView());
63 return stock_font ? CFGAS_GEFont::LoadFont(std::move(stock_font)) : nullptr;
64 }
65
66 CFGAS_GEFont::CFGAS_GEFont() = default;
67
68 CFGAS_GEFont::~CFGAS_GEFont() = default;
69
70 #if BUILDFLAG(IS_WIN)
LoadFontInternal(const wchar_t * pszFontFamily,uint32_t dwFontStyles,FX_CodePage wCodePage)71 bool CFGAS_GEFont::LoadFontInternal(const wchar_t* pszFontFamily,
72 uint32_t dwFontStyles,
73 FX_CodePage wCodePage) {
74 if (m_pFont)
75 return false;
76 ByteString csFontFamily;
77 if (pszFontFamily)
78 csFontFamily = WideString(pszFontFamily).ToDefANSI();
79
80 int32_t iWeight =
81 FontStyleIsForceBold(dwFontStyles) ? FXFONT_FW_BOLD : FXFONT_FW_NORMAL;
82 m_pFont = std::make_unique<CFX_Font>();
83 if (FontStyleIsItalic(dwFontStyles) && FontStyleIsForceBold(dwFontStyles))
84 csFontFamily += ",BoldItalic";
85 else if (FontStyleIsForceBold(dwFontStyles))
86 csFontFamily += ",Bold";
87 else if (FontStyleIsItalic(dwFontStyles))
88 csFontFamily += ",Italic";
89
90 m_pFont->LoadSubst(csFontFamily, true, dwFontStyles, iWeight, 0, wCodePage,
91 false);
92 return m_pFont->GetFace() && InitFont();
93 }
94 #endif // BUILDFLAG(IS_WIN)
95
LoadFontInternal(RetainPtr<CPDF_Font> pPDFFont)96 bool CFGAS_GEFont::LoadFontInternal(RetainPtr<CPDF_Font> pPDFFont) {
97 DCHECK(pPDFFont);
98
99 if (m_pFont)
100 return false;
101
102 m_pPDFFont = std::move(pPDFFont); // Keep `pPDFFont` alive for the duration.
103 m_pFont = m_pPDFFont->GetFont();
104 return InitFont();
105 }
106
LoadFontInternal(std::unique_ptr<CFX_Font> pInternalFont)107 bool CFGAS_GEFont::LoadFontInternal(std::unique_ptr<CFX_Font> pInternalFont) {
108 if (m_pFont || !pInternalFont)
109 return false;
110
111 m_pFont = std::move(pInternalFont);
112 return InitFont();
113 }
114
InitFont()115 bool CFGAS_GEFont::InitFont() {
116 if (!m_pFont)
117 return false;
118
119 if (m_pFontEncoding)
120 return true;
121
122 m_pFontEncoding = FX_CreateFontEncodingEx(m_pFont.Get());
123 return !!m_pFontEncoding;
124 }
125
GetFamilyName() const126 WideString CFGAS_GEFont::GetFamilyName() const {
127 CFX_SubstFont* subst_font = m_pFont->GetSubstFont();
128 ByteString family_name = subst_font && !subst_font->m_Family.IsEmpty()
129 ? subst_font->m_Family
130 : m_pFont->GetFamilyName();
131 return WideString::FromDefANSI(family_name.AsStringView());
132 }
133
GetFontStyles() const134 uint32_t CFGAS_GEFont::GetFontStyles() const {
135 DCHECK(m_pFont);
136 if (m_dwLogFontStyle.has_value())
137 return m_dwLogFontStyle.value();
138
139 uint32_t dwStyles = 0;
140 auto* pSubstFont = m_pFont->GetSubstFont();
141 if (pSubstFont) {
142 if (pSubstFont->m_Weight == FXFONT_FW_BOLD)
143 dwStyles |= FXFONT_FORCE_BOLD;
144 } else {
145 if (m_pFont->IsBold())
146 dwStyles |= FXFONT_FORCE_BOLD;
147 if (m_pFont->IsItalic())
148 dwStyles |= FXFONT_ITALIC;
149 }
150 return dwStyles;
151 }
152
GetCharWidth(wchar_t wUnicode)153 std::optional<uint16_t> CFGAS_GEFont::GetCharWidth(wchar_t wUnicode) {
154 auto it = m_CharWidthMap.find(wUnicode);
155 if (it != m_CharWidthMap.end())
156 return it->second;
157
158 auto [glyph, pFont] = GetGlyphIndexAndFont(wUnicode, true);
159 if (!pFont || glyph == 0xffff) {
160 m_CharWidthMap[wUnicode] = std::nullopt;
161 return std::nullopt;
162 }
163 if (pFont != this)
164 return pFont->GetCharWidth(wUnicode);
165
166 int32_t width_from_cfx_font = m_pFont->GetGlyphWidth(glyph);
167 if (width_from_cfx_font < 0) {
168 m_CharWidthMap[wUnicode] = std::nullopt;
169 return std::nullopt;
170 }
171 uint16_t width = static_cast<uint16_t>(width_from_cfx_font);
172 m_CharWidthMap[wUnicode] = width;
173 return width;
174 }
175
GetCharBBox(wchar_t wUnicode)176 std::optional<FX_RECT> CFGAS_GEFont::GetCharBBox(wchar_t wUnicode) {
177 auto it = m_BBoxMap.find(wUnicode);
178 if (it != m_BBoxMap.end())
179 return it->second;
180
181 auto [iGlyph, pFont] = GetGlyphIndexAndFont(wUnicode, true);
182 if (!pFont || iGlyph == 0xFFFF)
183 return std::nullopt;
184
185 if (pFont.Get() != this)
186 return pFont->GetCharBBox(wUnicode);
187
188 std::optional<FX_RECT> rtBBox = m_pFont->GetGlyphBBox(iGlyph);
189 if (rtBBox.has_value())
190 m_BBoxMap[wUnicode] = rtBBox.value();
191
192 return rtBBox;
193 }
194
GetGlyphIndex(wchar_t wUnicode)195 int32_t CFGAS_GEFont::GetGlyphIndex(wchar_t wUnicode) {
196 return GetGlyphIndexAndFont(wUnicode, true).first;
197 }
198
GetGlyphIndexAndFont(wchar_t wUnicode,bool bRecursive)199 std::pair<int32_t, RetainPtr<CFGAS_GEFont>> CFGAS_GEFont::GetGlyphIndexAndFont(
200 wchar_t wUnicode,
201 bool bRecursive) {
202 int32_t iGlyphIndex = m_pFontEncoding->GlyphFromCharCode(wUnicode);
203 if (iGlyphIndex > 0)
204 return {iGlyphIndex, pdfium::WrapRetain(this)};
205
206 const FGAS_FONTUSB* pFontUSB = FGAS_GetUnicodeBitField(wUnicode);
207 if (!pFontUSB)
208 return {0xFFFF, nullptr};
209
210 uint16_t wBitField = pFontUSB->wBitField;
211 if (wBitField >= 128)
212 return {0xFFFF, nullptr};
213
214 auto it = m_FontMapper.find(wUnicode);
215 if (it != m_FontMapper.end() && it->second && it->second.Get() != this) {
216 RetainPtr<CFGAS_GEFont> font;
217 std::tie(iGlyphIndex, font) =
218 it->second->GetGlyphIndexAndFont(wUnicode, false);
219 if (iGlyphIndex != 0xFFFF) {
220 for (size_t i = 0; i < m_SubstFonts.size(); ++i) {
221 if (m_SubstFonts[i] == it->second)
222 return {(iGlyphIndex | ((i + 1) << 24)), it->second};
223 }
224 }
225 }
226 if (!bRecursive)
227 return {0xFFFF, nullptr};
228
229 CFGAS_FontMgr* pFontMgr = CFGAS_GEModule::Get()->GetFontMgr();
230 WideString wsFamily = GetFamilyName();
231 RetainPtr<CFGAS_GEFont> pFont =
232 pFontMgr->GetFontByUnicode(wUnicode, GetFontStyles(), wsFamily.c_str());
233 #if !BUILDFLAG(IS_WIN)
234 if (!pFont)
235 pFont = pFontMgr->GetFontByUnicode(wUnicode, GetFontStyles(), nullptr);
236 #endif
237 if (!pFont || pFont == this) // Avoids direct cycles below.
238 return {0xFFFF, nullptr};
239
240 m_FontMapper[wUnicode] = pFont;
241 m_SubstFonts.push_back(pFont);
242
243 RetainPtr<CFGAS_GEFont> font;
244 std::tie(iGlyphIndex, font) = pFont->GetGlyphIndexAndFont(wUnicode, false);
245 if (iGlyphIndex == 0xFFFF)
246 return {0xFFFF, nullptr};
247
248 return {(iGlyphIndex | (m_SubstFonts.size() << 24)), pFont};
249 }
250
GetAscent() const251 int32_t CFGAS_GEFont::GetAscent() const {
252 return m_pFont->GetAscent();
253 }
254
GetDescent() const255 int32_t CFGAS_GEFont::GetDescent() const {
256 return m_pFont->GetDescent();
257 }
258
GetSubstFont(int32_t iGlyphIndex)259 RetainPtr<CFGAS_GEFont> CFGAS_GEFont::GetSubstFont(int32_t iGlyphIndex) {
260 iGlyphIndex = static_cast<uint32_t>(iGlyphIndex) >> 24;
261 if (iGlyphIndex == 0)
262 return pdfium::WrapRetain(this);
263 return m_SubstFonts[iGlyphIndex - 1];
264 }
265