Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfgas_gefont.cpp
Go to the documentation of this file.
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/fxge/cfx_font.h"
15#include "core/fxge/cfx_substfont.h"
16#include "core/fxge/cfx_unicodeencodingex.h"
17#include "core/fxge/fx_font.h"
18#include "third_party/base/check.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
24RetainPtr<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
34 wCodePage, dwFontStyles, pszFontFamily);
35#endif
36}
37
38// static
39RetainPtr<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
48RetainPtr<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
58RetainPtr<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
66CFGAS_GEFont::CFGAS_GEFont() = default;
67
68CFGAS_GEFont::~CFGAS_GEFont() = default;
69
70#if BUILDFLAG(IS_WIN)
71bool 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
96bool 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
107bool 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
115bool 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
126WideString 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
134uint32_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
153absl::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 RetainPtr<CFGAS_GEFont> pFont;
159 int32_t glyph;
160 std::tie(glyph, pFont) = GetGlyphIndexAndFont(wUnicode, true);
161 if (!pFont || glyph == 0xffff) {
162 m_CharWidthMap[wUnicode] = absl::nullopt;
163 return absl::nullopt;
164 }
165 if (pFont != this)
166 return pFont->GetCharWidth(wUnicode);
167
168 int32_t width_from_cfx_font = m_pFont->GetGlyphWidth(glyph);
169 if (width_from_cfx_font < 0) {
170 m_CharWidthMap[wUnicode] = absl::nullopt;
171 return absl::nullopt;
172 }
173 uint16_t width = static_cast<uint16_t>(width_from_cfx_font);
174 m_CharWidthMap[wUnicode] = width;
175 return width;
176}
177
178absl::optional<FX_RECT> CFGAS_GEFont::GetCharBBox(wchar_t wUnicode) {
179 auto it = m_BBoxMap.find(wUnicode);
180 if (it != m_BBoxMap.end())
181 return it->second;
182
183 RetainPtr<CFGAS_GEFont> pFont;
184 int32_t iGlyph;
185 std::tie(iGlyph, pFont) = GetGlyphIndexAndFont(wUnicode, true);
186 if (!pFont || iGlyph == 0xFFFF)
187 return absl::nullopt;
188
189 if (pFont.Get() != this)
190 return pFont->GetCharBBox(wUnicode);
191
192 absl::optional<FX_RECT> rtBBox = m_pFont->GetGlyphBBox(iGlyph);
193 if (rtBBox.has_value())
194 m_BBoxMap[wUnicode] = rtBBox.value();
195
196 return rtBBox;
197}
198
199int32_t CFGAS_GEFont::GetGlyphIndex(wchar_t wUnicode) {
200 int32_t glyph;
201 RetainPtr<CFGAS_GEFont> font;
202 std::tie(glyph, font) = GetGlyphIndexAndFont(wUnicode, true);
203 return glyph;
204}
205
206std::pair<int32_t, RetainPtr<CFGAS_GEFont>> CFGAS_GEFont::GetGlyphIndexAndFont(
207 wchar_t wUnicode,
208 bool bRecursive) {
209 int32_t iGlyphIndex = m_pFontEncoding->GlyphFromCharCode(wUnicode);
210 if (iGlyphIndex > 0)
211 return {iGlyphIndex, pdfium::WrapRetain(this)};
212
213 const FGAS_FONTUSB* pFontUSB = FGAS_GetUnicodeBitField(wUnicode);
214 if (!pFontUSB)
215 return {0xFFFF, nullptr};
216
217 uint16_t wBitField = pFontUSB->wBitField;
218 if (wBitField >= 128)
219 return {0xFFFF, nullptr};
220
221 auto it = m_FontMapper.find(wUnicode);
222 if (it != m_FontMapper.end() && it->second && it->second.Get() != this) {
223 RetainPtr<CFGAS_GEFont> font;
224 std::tie(iGlyphIndex, font) =
225 it->second->GetGlyphIndexAndFont(wUnicode, false);
226 if (iGlyphIndex != 0xFFFF) {
227 for (size_t i = 0; i < m_SubstFonts.size(); ++i) {
228 if (m_SubstFonts[i] == it->second)
229 return {(iGlyphIndex | ((i + 1) << 24)), it->second};
230 }
231 }
232 }
233 if (!bRecursive)
234 return {0xFFFF, nullptr};
235
237 WideString wsFamily = GetFamilyName();
238 RetainPtr<CFGAS_GEFont> pFont =
239 pFontMgr->GetFontByUnicode(wUnicode, GetFontStyles(), wsFamily.c_str());
240#if !BUILDFLAG(IS_WIN)
241 if (!pFont)
242 pFont = pFontMgr->GetFontByUnicode(wUnicode, GetFontStyles(), nullptr);
243#endif
244 if (!pFont || pFont == this) // Avoids direct cycles below.
245 return {0xFFFF, nullptr};
246
247 m_FontMapper[wUnicode] = pFont;
248 m_SubstFonts.push_back(pFont);
249
250 RetainPtr<CFGAS_GEFont> font;
251 std::tie(iGlyphIndex, font) = pFont->GetGlyphIndexAndFont(wUnicode, false);
252 if (iGlyphIndex == 0xFFFF)
253 return {0xFFFF, nullptr};
254
255 return {(iGlyphIndex | (m_SubstFonts.size() << 24)), pFont};
256}
257
258int32_t CFGAS_GEFont::GetAscent() const {
259 return m_pFont->GetAscent();
260}
261
262int32_t CFGAS_GEFont::GetDescent() const {
263 return m_pFont->GetDescent();
264}
265
266RetainPtr<CFGAS_GEFont> CFGAS_GEFont::GetSubstFont(int32_t iGlyphIndex) {
267 iGlyphIndex = static_cast<uint32_t>(iGlyphIndex) >> 24;
268 if (iGlyphIndex == 0)
269 return pdfium::WrapRetain(this);
270 return m_SubstFonts[iGlyphIndex - 1];
271}
RetainPtr< CFGAS_GEFont > GetFontByCodePage(FX_CodePage wCodePage, uint32_t dwFontStyles, const wchar_t *pszFontFamily)
static RetainPtr< CFGAS_GEFont > LoadFont(const wchar_t *pszFontFamily, uint32_t dwFontStyles, FX_CodePage wCodePage)
absl::optional< uint16_t > GetCharWidth(wchar_t wUnicode)
RetainPtr< CFGAS_GEFont > GetSubstFont(int32_t iGlyphIndex)
uint32_t GetFontStyles() const
int32_t GetDescent() const
~CFGAS_GEFont() override
static RetainPtr< CFGAS_GEFont > LoadFont(RetainPtr< CPDF_Font > pFont)
int32_t GetGlyphIndex(wchar_t wUnicode)
int32_t GetAscent() const
static RetainPtr< CFGAS_GEFont > LoadStockFont(CPDF_Document *pDoc, const ByteString &font_family)
absl::optional< FX_RECT > GetCharBBox(wchar_t wUnicode)
static CFGAS_GEModule * Get()
CFGAS_FontMgr * GetFontMgr()
static WideString FromDefANSI(ByteStringView str)
const FGAS_FONTUSB * FGAS_GetUnicodeBitField(wchar_t wUnicode)
FX_CodePage
Definition fx_codepage.h:18
#define FXFONT_FW_BOLD
Definition fx_font.h:23
#define FXFONT_ITALIC
Definition fx_font.h:33
#define FXFONT_FORCE_BOLD
Definition fx_font.h:36
uint16_t wBitField