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/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
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
153std::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
176std::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
195int32_t CFGAS_GEFont::GetGlyphIndex(wchar_t wUnicode) {
196 return GetGlyphIndexAndFont(wUnicode, true).first;
197}
198
199std::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
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
251int32_t CFGAS_GEFont::GetAscent() const {
252 return m_pFont->GetAscent();
253}
254
255int32_t CFGAS_GEFont::GetDescent() const {
256 return m_pFont->GetDescent();
257}
258
259RetainPtr<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}
fxcrt::ByteString ByteString
Definition bytestring.h:180
#define DCHECK
Definition check.h:33
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)
std::optional< FX_RECT > GetCharBBox(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)
std::optional< uint16_t > GetCharWidth(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:19
#define FXFONT_FW_BOLD
Definition fx_font.h:25
#define FXFONT_ITALIC
Definition fx_font.h:35
#define FXFONT_FORCE_BOLD
Definition fx_font.h:38
uint16_t wBitField
fxcrt::WideString WideString
Definition widestring.h:207