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/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"
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)))
60 const ByteString& font_family) {
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())
160 std::tie(glyph, pFont) = GetGlyphIndexAndFont(wUnicode,
true);
161 if (!pFont || glyph == 0xffff) {
162 m_CharWidthMap[wUnicode] = absl::nullopt;
163 return absl::nullopt;
166 return pFont->GetCharWidth(wUnicode);
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;
173 uint16_t width =
static_cast<uint16_t>(width_from_cfx_font);
174 m_CharWidthMap[wUnicode] = width;
179 auto it = m_BBoxMap.find(wUnicode);
180 if (it != m_BBoxMap.end())
185 std::tie(iGlyph, pFont) = GetGlyphIndexAndFont(wUnicode,
true);
186 if (!pFont || iGlyph == 0xFFFF)
187 return absl::nullopt;
189 if (pFont.Get() !=
this)
190 return pFont->GetCharBBox(wUnicode);
192 absl::optional<FX_RECT> rtBBox = m_pFont->GetGlyphBBox(iGlyph);
193 if (rtBBox.has_value())
194 m_BBoxMap[wUnicode] = rtBBox.value();
202 std::tie(glyph, font) = GetGlyphIndexAndFont(wUnicode,
true);
206std::pair<int32_t,
RetainPtr<CFGAS_GEFont>> CFGAS_GEFont::GetGlyphIndexAndFont(
209 int32_t iGlyphIndex = m_pFontEncoding->GlyphFromCharCode(wUnicode);
211 return {iGlyphIndex, pdfium::WrapRetain(
this)};
215 return {0xFFFF,
nullptr};
218 if (wBitField >= 128)
219 return {0xFFFF,
nullptr};
221 auto it = m_FontMapper.find(wUnicode);
222 if (it != m_FontMapper.end() && it->second && it->second.Get() !=
this) {
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};
234 return {0xFFFF,
nullptr};
237 WideString wsFamily = GetFamilyName();
239 pFontMgr->GetFontByUnicode(wUnicode, GetFontStyles(), wsFamily.c_str());
240#if !BUILDFLAG(IS_WIN)
242 pFont = pFontMgr->GetFontByUnicode(wUnicode, GetFontStyles(),
nullptr);
244 if (!pFont || pFont ==
this)
245 return {0xFFFF,
nullptr};
247 m_FontMapper[wUnicode] = pFont;
248 m_SubstFonts.push_back(pFont);
251 std::tie(iGlyphIndex, font) = pFont->GetGlyphIndexAndFont(wUnicode,
false);
252 if (iGlyphIndex == 0xFFFF)
253 return {0xFFFF,
nullptr};
255 return {(iGlyphIndex | (m_SubstFonts.size() << 24)), pFont};
259 return m_pFont->GetAscent();
263 return m_pFont->GetDescent();
267 iGlyphIndex =
static_cast<uint32_t>(iGlyphIndex) >> 24;
268 if (iGlyphIndex == 0)
269 return pdfium::WrapRetain(
this);
270 return m_SubstFonts[iGlyphIndex - 1];
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
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)
#define FXFONT_FORCE_BOLD