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/fxfa/cxfa_fontmgr.h"
8
9 #include "core/fpdfapi/parser/cpdf_dictionary.h"
10 #include "core/fxge/cfx_fontmgr.h"
11 #include "core/fxge/cfx_gemodule.h"
12 #include "xfa/fgas/font/cfgas_defaultfontmanager.h"
13 #include "xfa/fgas/font/cfgas_gefont.h"
14 #include "xfa/fgas/font/fgas_fontutils.h"
15 #include "xfa/fxfa/cxfa_ffapp.h"
16 #include "xfa/fxfa/cxfa_ffdoc.h"
17
18 CXFA_FontMgr::CXFA_FontMgr() = default;
19
20 CXFA_FontMgr::~CXFA_FontMgr() = default;
21
Trace(cppgc::Visitor * visitor) const22 void CXFA_FontMgr::Trace(cppgc::Visitor* visitor) const {}
23
GetFont(CXFA_FFDoc * pDoc,const WideString & wsFontFamily,uint32_t dwFontStyles)24 RetainPtr<CFGAS_GEFont> CXFA_FontMgr::GetFont(CXFA_FFDoc* pDoc,
25 const WideString& wsFontFamily,
26 uint32_t dwFontStyles) {
27 auto key = std::make_pair(wsFontFamily, dwFontStyles);
28 auto iter = m_FontMap.find(key);
29 if (iter != m_FontMap.end())
30 return iter->second;
31
32 WideString wsEnglishName = FGAS_FontNameToEnglishName(wsFontFamily);
33 RetainPtr<CFGAS_GEFont> pFont =
34 pDoc->GetPDFFont(wsEnglishName, dwFontStyles, true);
35 if (pFont)
36 return pFont;
37
38 pFont = CFGAS_DefaultFontManager::GetFont(wsFontFamily, dwFontStyles);
39 if (!pFont) {
40 pFont = pDoc->GetPDFFont(wsEnglishName, dwFontStyles, false);
41 if (pFont)
42 return pFont;
43 }
44 if (!pFont) {
45 pFont = CFGAS_DefaultFontManager::GetDefaultFont(dwFontStyles);
46 }
47 if (!pFont) {
48 pFont = CFGAS_GEFont::LoadStockFont(
49 pDoc->GetPDFDoc(), ByteString::Format("%ls", wsFontFamily.c_str()));
50 }
51 if (!pFont)
52 return nullptr;
53
54 m_FontMap[key] = pFont;
55 return pFont;
56 }
57