7#include "xfa/fgas/font/cfgas_gefont.h"
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"
25 uint32_t dwFontStyles,
28 auto pFont = pdfium::MakeRetain<CFGAS_GEFont>();
29 if (!pFont->LoadFontInternal(pszFontFamily, dwFontStyles, wCodePage))
34 wCodePage, dwFontStyles, pszFontFamily);
40 auto pFont =
pdfium::MakeRetain<CFGAS_GEFont>();
41 if (!pFont->LoadFontInternal(
std::move(pPDFFont)))
49 std::unique_ptr<CFX_Font> pInternalFont) {
50 auto pFont =
pdfium::MakeRetain<CFGAS_GEFont>();
51 if (!pFont->LoadFontInternal(
std::move(pInternalFont)))
62 CPDF_Font::GetStockFont(pDoc, font_family.AsStringView());
63 return stock_font ? CFGAS_GEFont::LoadFont(
std::move(stock_font)) :
nullptr;
66CFGAS_GEFont::CFGAS_GEFont() =
default;
71bool CFGAS_GEFont::LoadFontInternal(
const wchar_t* pszFontFamily,
72 uint32_t dwFontStyles,
73 FX_CodePage wCodePage) {
76 ByteString csFontFamily;
78 csFontFamily = WideString(pszFontFamily).ToDefANSI();
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";
90 m_pFont->LoadSubst(csFontFamily,
true, dwFontStyles, iWeight, 0, wCodePage,
92 return m_pFont->GetFace() && InitFont();
102 m_pPDFFont = std::move(pPDFFont);
103 m_pFont = m_pPDFFont->GetFont();
107bool CFGAS_GEFont::LoadFontInternal(std::unique_ptr<CFX_Font> pInternalFont) {
108 if (m_pFont || !pInternalFont)
111 m_pFont = std::move(pInternalFont);
115bool CFGAS_GEFont::InitFont() {
122 m_pFontEncoding = FX_CreateFontEncodingEx(m_pFont.Get());
123 return !!m_pFontEncoding;
126WideString CFGAS_GEFont::GetFamilyName()
const {
128 ByteString family_name = subst_font && !subst_font->m_Family.IsEmpty()
129 ? subst_font->m_Family
130 : m_pFont->GetFamilyName();
136 if (m_dwLogFontStyle.has_value())
137 return m_dwLogFontStyle.value();
139 uint32_t dwStyles = 0;
140 auto* pSubstFont = m_pFont->GetSubstFont();
145 if (m_pFont->IsBold())
147 if (m_pFont->IsItalic())
154 auto it = m_CharWidthMap.find(wUnicode);
155 if (it != m_CharWidthMap.end())
158 auto [glyph, pFont] = GetGlyphIndexAndFont(wUnicode,
true);
159 if (!pFont || glyph == 0xffff) {
160 m_CharWidthMap[wUnicode] = std::nullopt;
164 return pFont->GetCharWidth(wUnicode);
166 int32_t width_from_cfx_font = m_pFont->GetGlyphWidth(glyph);
167 if (width_from_cfx_font < 0) {
168 m_CharWidthMap[wUnicode] = std::nullopt;
171 uint16_t width =
static_cast<uint16_t>(width_from_cfx_font);
172 m_CharWidthMap[wUnicode] = width;
177 auto it = m_BBoxMap.find(wUnicode);
178 if (it != m_BBoxMap.end())
181 auto [iGlyph, pFont] = GetGlyphIndexAndFont(wUnicode,
true);
182 if (!pFont || iGlyph == 0xFFFF)
185 if (pFont.Get() !=
this)
186 return pFont->GetCharBBox(wUnicode);
188 std::optional<
FX_RECT> rtBBox = m_pFont->GetGlyphBBox(iGlyph);
189 if (rtBBox.has_value())
190 m_BBoxMap[wUnicode] = rtBBox.value();
196 return GetGlyphIndexAndFont(wUnicode,
true).first;
199std::pair<int32_t,
RetainPtr<CFGAS_GEFont>> CFGAS_GEFont::GetGlyphIndexAndFont(
202 int32_t iGlyphIndex = m_pFontEncoding->GlyphFromCharCode(wUnicode);
204 return {iGlyphIndex,
pdfium::WrapRetain(
this)};
208 return {0xFFFF,
nullptr};
211 if (wBitField >= 128)
212 return {0xFFFF,
nullptr};
214 auto it = m_FontMapper.find(wUnicode);
215 if (it != m_FontMapper.end() && it->second && it->second.Get() !=
this) {
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};
227 return {0xFFFF,
nullptr};
232 pFontMgr->GetFontByUnicode(wUnicode, GetFontStyles(), wsFamily.c_str());
233#if !BUILDFLAG(IS_WIN)
235 pFont = pFontMgr->GetFontByUnicode(wUnicode, GetFontStyles(),
nullptr);
237 if (!pFont || pFont ==
this)
238 return {0xFFFF,
nullptr};
240 m_FontMapper[wUnicode] = pFont;
241 m_SubstFonts.push_back(pFont);
244 std::tie(iGlyphIndex, font) = pFont->GetGlyphIndexAndFont(wUnicode,
false);
245 if (iGlyphIndex == 0xFFFF)
246 return {0xFFFF,
nullptr};
248 return {(iGlyphIndex | (m_SubstFonts.size() << 24)), pFont};
252 return m_pFont->GetAscent();
256 return m_pFont->GetDescent();
260 iGlyphIndex =
static_cast<uint32_t>(iGlyphIndex) >> 24;
261 if (iGlyphIndex == 0)
262 return pdfium::WrapRetain(
this);
263 return m_SubstFonts[iGlyphIndex - 1];
fxcrt::ByteString ByteString
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
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)
#define FXFONT_FORCE_BOLD
fxcrt::WideString WideString