1 // Copyright 2014 PDFium Authors. All rights reserved.
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 <algorithm>
10 #include <memory>
11 #include <utility>
12 
13 #include "core/fpdfapi/parser/cpdf_dictionary.h"
14 #include "core/fpdfapi/parser/cpdf_document.h"
15 #include "core/fxge/cfx_fontmgr.h"
16 #include "core/fxge/cfx_gemodule.h"
17 #include "xfa/fgas/font/cfgas_defaultfontmanager.h"
18 #include "xfa/fgas/font/cfgas_gefont.h"
19 #include "xfa/fgas/font/fgas_fontutils.h"
20 #include "xfa/fxfa/cxfa_ffapp.h"
21 #include "xfa/fxfa/cxfa_ffdoc.h"
22 
23 CXFA_FontMgr::CXFA_FontMgr() = default;
24 
25 CXFA_FontMgr::~CXFA_FontMgr() = default;
26 
GetFont(CXFA_FFDoc * hDoc,WideStringView wsFontFamily,uint32_t dwFontStyles)27 RetainPtr<CFGAS_GEFont> CXFA_FontMgr::GetFont(CXFA_FFDoc* hDoc,
28                                               WideStringView wsFontFamily,
29                                               uint32_t dwFontStyles) {
30   uint32_t dwHash = FX_HashCode_GetW(wsFontFamily, false);
31   ByteString bsKey = ByteString::Format("%u%u%u", dwHash, dwFontStyles, 0xFFFF);
32   auto iter = m_FontMap.find(bsKey);
33   if (iter != m_FontMap.end())
34     return iter->second;
35 
36   WideString wsEnglishName = FGAS_FontNameToEnglishName(wsFontFamily);
37   CFGAS_PDFFontMgr* pMgr = hDoc->GetPDFFontMgr();
38   RetainPtr<CFGAS_GEFont> pFont;
39   if (pMgr) {
40     pFont = pMgr->GetFont(wsEnglishName.AsStringView(), dwFontStyles, true);
41     if (pFont)
42       return pFont;
43   }
44   if (!pFont) {
45     pFont = CFGAS_DefaultFontManager::GetFont(hDoc->GetApp()->GetFDEFontMgr(),
46                                               wsFontFamily, dwFontStyles);
47   }
48   if (!pFont && pMgr) {
49     pFont = pMgr->GetFont(wsEnglishName.AsStringView(), dwFontStyles, false);
50     if (pFont)
51       return pFont;
52   }
53   if (!pFont) {
54     pFont = CFGAS_DefaultFontManager::GetDefaultFont(
55         hDoc->GetApp()->GetFDEFontMgr(), wsFontFamily, dwFontStyles);
56   }
57   if (!pFont) {
58     pFont = CFGAS_GEFont::LoadStockFont(
59         hDoc->GetPDFDoc(), hDoc->GetApp()->GetFDEFontMgr(),
60         ByteString::Format("%ls", WideString(wsFontFamily).c_str()));
61   }
62   if (pFont)
63     m_FontMap[bsKey] = pFont;
64 
65   return pFont;
66 }
67