Home | History | Annotate | Download | only in fxfa
      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/font/cpdf_font.h"
     14 #include "core/fpdfapi/parser/cpdf_dictionary.h"
     15 #include "core/fpdfapi/parser/cpdf_document.h"
     16 #include "third_party/base/ptr_util.h"
     17 #include "xfa/fgas/font/cfgas_gefont.h"
     18 #include "xfa/fgas/font/fgas_fontutils.h"
     19 #include "xfa/fxfa/cxfa_ffapp.h"
     20 #include "xfa/fxfa/cxfa_ffdoc.h"
     21 
     22 CXFA_FontMgr::CXFA_FontMgr() {}
     23 
     24 CXFA_FontMgr::~CXFA_FontMgr() {}
     25 
     26 RetainPtr<CFGAS_GEFont> CXFA_FontMgr::GetFont(
     27     CXFA_FFDoc* hDoc,
     28     const 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 
     38   CFGAS_PDFFontMgr* pMgr = hDoc->GetPDFFontMgr();
     39   CPDF_Font* pPDFFont = nullptr;
     40   RetainPtr<CFGAS_GEFont> pFont;
     41   if (pMgr) {
     42     pFont = pMgr->GetFont(wsEnglishName.AsStringView(), dwFontStyles, &pPDFFont,
     43                           true);
     44     if (pFont)
     45       return pFont;
     46   }
     47   if (!pFont && m_pDefFontMgr)
     48     pFont = m_pDefFontMgr->GetFont(hDoc->GetApp()->GetFDEFontMgr(),
     49                                    wsFontFamily, dwFontStyles);
     50 
     51   if (!pFont && pMgr) {
     52     pPDFFont = nullptr;
     53     pFont = pMgr->GetFont(wsEnglishName.AsStringView(), dwFontStyles, &pPDFFont,
     54                           false);
     55     if (pFont)
     56       return pFont;
     57   }
     58   if (!pFont && m_pDefFontMgr) {
     59     pFont = m_pDefFontMgr->GetDefaultFont(hDoc->GetApp()->GetFDEFontMgr(),
     60                                           wsFontFamily, dwFontStyles);
     61   }
     62 
     63   if (pFont) {
     64     if (pPDFFont) {
     65       pMgr->SetFont(pFont, pPDFFont);
     66       pFont->SetFontProvider(pMgr);
     67     }
     68     m_FontMap[bsKey] = pFont;
     69   }
     70   return pFont;
     71 }
     72 
     73 void CXFA_FontMgr::SetDefFontMgr(
     74     std::unique_ptr<CFGAS_DefaultFontManager> pFontMgr) {
     75   m_pDefFontMgr = std::move(pFontMgr);
     76 }
     77