Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | |
Dan Sinclair | 80c4878 | 2017-03-23 12:11:20 -0400 | [diff] [blame] | 7 | #include "xfa/fxfa/cxfa_fontmgr.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 8 | |
| 9 | #include <algorithm> |
thestig | 2c06532 | 2016-09-26 14:16:43 -0700 | [diff] [blame] | 10 | #include <memory> |
thestig | 4dce6d4 | 2016-05-31 05:46:52 -0700 | [diff] [blame] | 11 | #include <utility> |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 12 | |
dsinclair | bc5e6d2 | 2016-10-04 11:08:49 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/font/cpdf_font.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 14 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
| 15 | #include "core/fpdfapi/parser/cpdf_document.h" |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 16 | #include "third_party/base/ptr_util.h" |
npm | 4b91a2d | 2016-11-21 15:19:44 -0800 | [diff] [blame] | 17 | #include "xfa/fgas/font/cfgas_gefont.h" |
Dan Sinclair | 959c2be | 2017-09-21 10:13:40 -0400 | [diff] [blame] | 18 | #include "xfa/fgas/font/fgas_fontutils.h" |
Dan Sinclair | 80c4878 | 2017-03-23 12:11:20 -0400 | [diff] [blame] | 19 | #include "xfa/fxfa/cxfa_ffapp.h" |
| 20 | #include "xfa/fxfa/cxfa_ffdoc.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 21 | |
thestig | 4dce6d4 | 2016-05-31 05:46:52 -0700 | [diff] [blame] | 22 | CXFA_FontMgr::CXFA_FontMgr() {} |
dsinclair | 303e525 | 2016-04-27 12:47:01 -0700 | [diff] [blame] | 23 | |
tsepez | 5a5180e | 2016-05-20 13:24:30 -0700 | [diff] [blame] | 24 | CXFA_FontMgr::~CXFA_FontMgr() {} |
dsinclair | 303e525 | 2016-04-27 12:47:01 -0700 | [diff] [blame] | 25 | |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 26 | RetainPtr<CFGAS_GEFont> CXFA_FontMgr::GetFont( |
tsepez | e647799 | 2017-01-05 12:57:00 -0800 | [diff] [blame] | 27 | CXFA_FFDoc* hDoc, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 28 | const WideStringView& wsFontFamily, |
Dan Sinclair | 1a43b30 | 2017-09-26 11:09:35 -0400 | [diff] [blame] | 29 | uint32_t dwFontStyles) { |
tsepez | b6853cf | 2016-04-25 11:23:43 -0700 | [diff] [blame] | 30 | uint32_t dwHash = FX_HashCode_GetW(wsFontFamily, false); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 31 | ByteString bsKey; |
Dan Sinclair | 1a43b30 | 2017-09-26 11:09:35 -0400 | [diff] [blame] | 32 | bsKey.Format("%u%u%u", dwHash, dwFontStyles, 0xFFFF); |
tsepez | 5a5180e | 2016-05-20 13:24:30 -0700 | [diff] [blame] | 33 | auto iter = m_FontMap.find(bsKey); |
| 34 | if (iter != m_FontMap.end()) |
| 35 | return iter->second; |
thestig | 2c06532 | 2016-09-26 14:16:43 -0700 | [diff] [blame] | 36 | |
Dan Sinclair | 959c2be | 2017-09-21 10:13:40 -0400 | [diff] [blame] | 37 | WideString wsEnglishName = FGAS_FontNameToEnglishName(wsFontFamily); |
Dan Sinclair | a44a480 | 2017-09-21 10:45:18 -0400 | [diff] [blame] | 38 | |
| 39 | CFGAS_PDFFontMgr* pMgr = hDoc->GetPDFFontMgr(); |
tsepez | 5a5180e | 2016-05-20 13:24:30 -0700 | [diff] [blame] | 40 | CPDF_Font* pPDFFont = nullptr; |
Dan Sinclair | 0b95042 | 2017-09-21 15:49:49 -0400 | [diff] [blame] | 41 | RetainPtr<CFGAS_GEFont> pFont; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 42 | if (pMgr) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 43 | pFont = pMgr->GetFont(wsEnglishName.AsStringView(), dwFontStyles, &pPDFFont, |
| 44 | true); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 45 | if (pFont) |
| 46 | return pFont; |
| 47 | } |
thestig | 4dce6d4 | 2016-05-31 05:46:52 -0700 | [diff] [blame] | 48 | if (!pFont && m_pDefFontMgr) |
Dan Sinclair | 959c2be | 2017-09-21 10:13:40 -0400 | [diff] [blame] | 49 | pFont = m_pDefFontMgr->GetFont(hDoc->GetApp()->GetFDEFontMgr(), |
Dan Sinclair | 1a43b30 | 2017-09-26 11:09:35 -0400 | [diff] [blame] | 50 | wsFontFamily, dwFontStyles); |
thestig | 4dce6d4 | 2016-05-31 05:46:52 -0700 | [diff] [blame] | 51 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 52 | if (!pFont && pMgr) { |
tsepez | 5a5180e | 2016-05-20 13:24:30 -0700 | [diff] [blame] | 53 | pPDFFont = nullptr; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 54 | pFont = pMgr->GetFont(wsEnglishName.AsStringView(), dwFontStyles, &pPDFFont, |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 55 | false); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 56 | if (pFont) |
| 57 | return pFont; |
| 58 | } |
| 59 | if (!pFont && m_pDefFontMgr) { |
Dan Sinclair | 1a43b30 | 2017-09-26 11:09:35 -0400 | [diff] [blame] | 60 | pFont = m_pDefFontMgr->GetDefaultFont(hDoc->GetApp()->GetFDEFontMgr(), |
| 61 | wsFontFamily, dwFontStyles); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 62 | } |
Dan Sinclair | a44a480 | 2017-09-21 10:45:18 -0400 | [diff] [blame] | 63 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 64 | if (pFont) { |
| 65 | if (pPDFFont) { |
thestig | 2c06532 | 2016-09-26 14:16:43 -0700 | [diff] [blame] | 66 | pMgr->SetFont(pFont, pPDFFont); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 67 | pFont->SetFontProvider(pMgr); |
| 68 | } |
| 69 | m_FontMap[bsKey] = pFont; |
| 70 | } |
| 71 | return pFont; |
| 72 | } |
tsepez | 5a5180e | 2016-05-20 13:24:30 -0700 | [diff] [blame] | 73 | |
Dan Sinclair | 959c2be | 2017-09-21 10:13:40 -0400 | [diff] [blame] | 74 | void CXFA_FontMgr::SetDefFontMgr( |
| 75 | std::unique_ptr<CFGAS_DefaultFontManager> pFontMgr) { |
thestig | 4dce6d4 | 2016-05-31 05:46:52 -0700 | [diff] [blame] | 76 | m_pDefFontMgr = std::move(pFontMgr); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 77 | } |