summaryrefslogtreecommitdiff
path: root/xfa/fxfa/cxfa_fontmgr.cpp
blob: d5ba2ea8072a1e9b70f05d4b81fea433634bf1d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "xfa/fxfa/cxfa_fontmgr.h"

#include <algorithm>
#include <memory>
#include <utility>

#include "core/fpdfapi/font/cpdf_font.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_document.h"
#include "third_party/base/ptr_util.h"
#include "xfa/fgas/font/cfgas_gefont.h"
#include "xfa/fgas/font/fgas_fontutils.h"
#include "xfa/fxfa/cxfa_ffapp.h"
#include "xfa/fxfa/cxfa_ffdoc.h"

CXFA_FontMgr::CXFA_FontMgr() {}

CXFA_FontMgr::~CXFA_FontMgr() {}

CFX_RetainPtr<CFGAS_GEFont> CXFA_FontMgr::GetFont(
    CXFA_FFDoc* hDoc,
    const WideStringView& wsFontFamily,
    uint32_t dwFontStyles,
    uint16_t wCodePage) {
  uint32_t dwHash = FX_HashCode_GetW(wsFontFamily, false);
  ByteString bsKey;
  bsKey.Format("%u%u%u", dwHash, dwFontStyles, wCodePage);
  auto iter = m_FontMap.find(bsKey);
  if (iter != m_FontMap.end())
    return iter->second;

  WideString wsEnglishName = FGAS_FontNameToEnglishName(wsFontFamily);
  auto it = m_PDFFontMgrMap.find(hDoc);
  CFGAS_PDFFontMgr* pMgr =
      it != m_PDFFontMgrMap.end() ? it->second.get() : nullptr;
  CPDF_Font* pPDFFont = nullptr;
  CFX_RetainPtr<CFGAS_GEFont> pFont;
  if (pMgr) {
    pFont = pMgr->GetFont(wsEnglishName.AsStringView(), dwFontStyles, &pPDFFont,
                          true);
    if (pFont)
      return pFont;
  }
  if (!pFont && m_pDefFontMgr)
    pFont = m_pDefFontMgr->GetFont(hDoc->GetApp()->GetFDEFontMgr(),
                                   wsFontFamily, dwFontStyles, wCodePage);

  if (!pFont && pMgr) {
    pPDFFont = nullptr;
    pFont = pMgr->GetFont(wsEnglishName.AsStringView(), dwFontStyles, &pPDFFont,
                          false);
    if (pFont)
      return pFont;
  }
  if (!pFont && m_pDefFontMgr) {
    pFont = m_pDefFontMgr->GetDefaultFont(
        hDoc->GetApp()->GetFDEFontMgr(), wsFontFamily, dwFontStyles, wCodePage);
  }
  if (pFont) {
    if (pPDFFont) {
      pMgr->SetFont(pFont, pPDFFont);
      pFont->SetFontProvider(pMgr);
    }
    m_FontMap[bsKey] = pFont;
  }
  return pFont;
}

void CXFA_FontMgr::LoadDocFonts(CXFA_FFDoc* hDoc) {
  if (!m_PDFFontMgrMap[hDoc])
    m_PDFFontMgrMap[hDoc] = pdfium::MakeUnique<CFGAS_PDFFontMgr>(
        hDoc->GetPDFDoc(), hDoc->GetApp()->GetFDEFontMgr());
}

void CXFA_FontMgr::ReleaseDocFonts(CXFA_FFDoc* hDoc) {
  m_PDFFontMgrMap.erase(hDoc);
}

void CXFA_FontMgr::SetDefFontMgr(
    std::unique_ptr<CFGAS_DefaultFontManager> pFontMgr) {
  m_pDefFontMgr = std::move(pFontMgr);
}