blob: d144bd2cb5f2d580db58a62a04a5657a5cbaee0b [file] [log] [blame]
Dan Sinclair1770c022016-03-14 14:14:16 -04001// 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
Dan Sinclair80c48782017-03-23 12:11:20 -04007#include "xfa/fxfa/cxfa_fontmgr.h"
Dan Sinclair1770c022016-03-14 14:14:16 -04008
9#include <algorithm>
thestig2c065322016-09-26 14:16:43 -070010#include <memory>
thestig4dce6d42016-05-31 05:46:52 -070011#include <utility>
Dan Sinclair1770c022016-03-14 14:14:16 -040012
dsinclairbc5e6d22016-10-04 11:08:49 -070013#include "core/fpdfapi/font/cpdf_font.h"
dsinclair488b7ad2016-10-04 11:55:50 -070014#include "core/fpdfapi/parser/cpdf_dictionary.h"
15#include "core/fpdfapi/parser/cpdf_document.h"
tsepeza9caab92016-12-14 05:57:10 -080016#include "third_party/base/ptr_util.h"
npm4b91a2d2016-11-21 15:19:44 -080017#include "xfa/fgas/font/cfgas_gefont.h"
Dan Sinclair959c2be2017-09-21 10:13:40 -040018#include "xfa/fgas/font/fgas_fontutils.h"
Dan Sinclair80c48782017-03-23 12:11:20 -040019#include "xfa/fxfa/cxfa_ffapp.h"
20#include "xfa/fxfa/cxfa_ffdoc.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040021
thestig4dce6d42016-05-31 05:46:52 -070022CXFA_FontMgr::CXFA_FontMgr() {}
dsinclair303e5252016-04-27 12:47:01 -070023
tsepez5a5180e2016-05-20 13:24:30 -070024CXFA_FontMgr::~CXFA_FontMgr() {}
dsinclair303e5252016-04-27 12:47:01 -070025
Dan Sinclair0b950422017-09-21 15:49:49 -040026RetainPtr<CFGAS_GEFont> CXFA_FontMgr::GetFont(
tsepeze6477992017-01-05 12:57:00 -080027 CXFA_FFDoc* hDoc,
Ryan Harrison275e2602017-09-18 14:23:18 -040028 const WideStringView& wsFontFamily,
Dan Sinclair1a43b302017-09-26 11:09:35 -040029 uint32_t dwFontStyles) {
tsepezb6853cf2016-04-25 11:23:43 -070030 uint32_t dwHash = FX_HashCode_GetW(wsFontFamily, false);
Ryan Harrison275e2602017-09-18 14:23:18 -040031 ByteString bsKey;
Dan Sinclair1a43b302017-09-26 11:09:35 -040032 bsKey.Format("%u%u%u", dwHash, dwFontStyles, 0xFFFF);
tsepez5a5180e2016-05-20 13:24:30 -070033 auto iter = m_FontMap.find(bsKey);
34 if (iter != m_FontMap.end())
35 return iter->second;
thestig2c065322016-09-26 14:16:43 -070036
Dan Sinclair959c2be2017-09-21 10:13:40 -040037 WideString wsEnglishName = FGAS_FontNameToEnglishName(wsFontFamily);
Dan Sinclaira44a4802017-09-21 10:45:18 -040038
39 CFGAS_PDFFontMgr* pMgr = hDoc->GetPDFFontMgr();
tsepez5a5180e2016-05-20 13:24:30 -070040 CPDF_Font* pPDFFont = nullptr;
Dan Sinclair0b950422017-09-21 15:49:49 -040041 RetainPtr<CFGAS_GEFont> pFont;
Dan Sinclair1770c022016-03-14 14:14:16 -040042 if (pMgr) {
Ryan Harrison275e2602017-09-18 14:23:18 -040043 pFont = pMgr->GetFont(wsEnglishName.AsStringView(), dwFontStyles, &pPDFFont,
44 true);
Dan Sinclair1770c022016-03-14 14:14:16 -040045 if (pFont)
46 return pFont;
47 }
thestig4dce6d42016-05-31 05:46:52 -070048 if (!pFont && m_pDefFontMgr)
Dan Sinclair959c2be2017-09-21 10:13:40 -040049 pFont = m_pDefFontMgr->GetFont(hDoc->GetApp()->GetFDEFontMgr(),
Dan Sinclair1a43b302017-09-26 11:09:35 -040050 wsFontFamily, dwFontStyles);
thestig4dce6d42016-05-31 05:46:52 -070051
Dan Sinclair1770c022016-03-14 14:14:16 -040052 if (!pFont && pMgr) {
tsepez5a5180e2016-05-20 13:24:30 -070053 pPDFFont = nullptr;
Ryan Harrison275e2602017-09-18 14:23:18 -040054 pFont = pMgr->GetFont(wsEnglishName.AsStringView(), dwFontStyles, &pPDFFont,
tsepezd19e9122016-11-02 15:43:18 -070055 false);
Dan Sinclair1770c022016-03-14 14:14:16 -040056 if (pFont)
57 return pFont;
58 }
59 if (!pFont && m_pDefFontMgr) {
Dan Sinclair1a43b302017-09-26 11:09:35 -040060 pFont = m_pDefFontMgr->GetDefaultFont(hDoc->GetApp()->GetFDEFontMgr(),
61 wsFontFamily, dwFontStyles);
Dan Sinclair1770c022016-03-14 14:14:16 -040062 }
Dan Sinclaira44a4802017-09-21 10:45:18 -040063
Dan Sinclair1770c022016-03-14 14:14:16 -040064 if (pFont) {
65 if (pPDFFont) {
thestig2c065322016-09-26 14:16:43 -070066 pMgr->SetFont(pFont, pPDFFont);
Dan Sinclair1770c022016-03-14 14:14:16 -040067 pFont->SetFontProvider(pMgr);
68 }
69 m_FontMap[bsKey] = pFont;
70 }
71 return pFont;
72}
tsepez5a5180e2016-05-20 13:24:30 -070073
Dan Sinclair959c2be2017-09-21 10:13:40 -040074void CXFA_FontMgr::SetDefFontMgr(
75 std::unique_ptr<CFGAS_DefaultFontManager> pFontMgr) {
thestig4dce6d42016-05-31 05:46:52 -070076 m_pDefFontMgr = std::move(pFontMgr);
Dan Sinclair1770c022016-03-14 14:14:16 -040077}