• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The PDFium Authors
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 "core/fpdfapi/font/cpdf_type3font.h"
8 
9 #include <algorithm>
10 #include <iterator>
11 #include <type_traits>
12 #include <utility>
13 
14 #include "core/fpdfapi/font/cpdf_type3char.h"
15 #include "core/fpdfapi/parser/cpdf_array.h"
16 #include "core/fpdfapi/parser/cpdf_dictionary.h"
17 #include "core/fpdfapi/parser/cpdf_stream.h"
18 #include "core/fxcrt/autorestorer.h"
19 #include "core/fxcrt/check.h"
20 #include "core/fxcrt/fx_system.h"
21 
22 namespace {
23 
24 constexpr int kMaxType3FormLevel = 4;
25 
26 }  // namespace
27 
CPDF_Type3Font(CPDF_Document * pDocument,RetainPtr<CPDF_Dictionary> pFontDict,FormFactoryIface * pFormFactory)28 CPDF_Type3Font::CPDF_Type3Font(CPDF_Document* pDocument,
29                                RetainPtr<CPDF_Dictionary> pFontDict,
30                                FormFactoryIface* pFormFactory)
31     : CPDF_SimpleFont(pDocument, std::move(pFontDict)),
32       m_pFormFactory(pFormFactory) {
33   DCHECK(GetDocument());
34 }
35 
36 CPDF_Type3Font::~CPDF_Type3Font() = default;
37 
IsType3Font() const38 bool CPDF_Type3Font::IsType3Font() const {
39   return true;
40 }
41 
AsType3Font() const42 const CPDF_Type3Font* CPDF_Type3Font::AsType3Font() const {
43   return this;
44 }
45 
AsType3Font()46 CPDF_Type3Font* CPDF_Type3Font::AsType3Font() {
47   return this;
48 }
49 
WillBeDestroyed()50 void CPDF_Type3Font::WillBeDestroyed() {
51   m_bWillBeDestroyed = true;
52 
53   // Last reference to |this| may be through one of its CPDF_Type3Chars.
54   RetainPtr<CPDF_Font> protector(this);
55   for (const auto& item : m_CacheMap) {
56     if (item.second) {
57       item.second->WillBeDestroyed();
58     }
59   }
60 }
61 
Load()62 bool CPDF_Type3Font::Load() {
63   m_pFontResources = m_pFontDict->GetMutableDictFor("Resources");
64   RetainPtr<const CPDF_Array> pMatrix = m_pFontDict->GetArrayFor("FontMatrix");
65   float xscale = 1.0f;
66   float yscale = 1.0f;
67   if (pMatrix) {
68     m_FontMatrix = pMatrix->GetMatrix();
69     xscale = m_FontMatrix.a;
70     yscale = m_FontMatrix.d;
71   }
72 
73   RetainPtr<const CPDF_Array> pBBox = m_pFontDict->GetArrayFor("FontBBox");
74   if (pBBox) {
75     CFX_FloatRect box(
76         pBBox->GetFloatAt(0) * xscale, pBBox->GetFloatAt(1) * yscale,
77         pBBox->GetFloatAt(2) * xscale, pBBox->GetFloatAt(3) * yscale);
78     CPDF_Type3Char::TextUnitRectToGlyphUnitRect(&box);
79     m_FontBBox = box.ToFxRect();
80   }
81 
82   const size_t kCharLimit = m_CharWidthL.size();
83   int StartChar = m_pFontDict->GetIntegerFor("FirstChar");
84   if (StartChar >= 0 && static_cast<size_t>(StartChar) < kCharLimit) {
85     RetainPtr<const CPDF_Array> pWidthArray =
86         m_pFontDict->GetArrayFor("Widths");
87     if (pWidthArray) {
88       size_t count = std::min(pWidthArray->size(), kCharLimit);
89       count = std::min(count, kCharLimit - StartChar);
90       for (size_t i = 0; i < count; i++) {
91         m_CharWidthL[StartChar + i] =
92             FXSYS_roundf(CPDF_Type3Char::TextUnitToGlyphUnit(
93                 pWidthArray->GetFloatAt(i) * xscale));
94       }
95     }
96   }
97   m_pCharProcs = m_pFontDict->GetMutableDictFor("CharProcs");
98   if (m_pFontDict->GetDirectObjectFor("Encoding"))
99     LoadPDFEncoding(false, false);
100   return true;
101 }
102 
LoadGlyphMap()103 void CPDF_Type3Font::LoadGlyphMap() {}
104 
CheckType3FontMetrics()105 void CPDF_Type3Font::CheckType3FontMetrics() {
106   CheckFontMetrics();
107 }
108 
LoadChar(uint32_t charcode)109 CPDF_Type3Char* CPDF_Type3Font::LoadChar(uint32_t charcode) {
110   if (m_CharLoadingDepth >= kMaxType3FormLevel)
111     return nullptr;
112 
113   auto it = m_CacheMap.find(charcode);
114   if (it != m_CacheMap.end())
115     return it->second.get();
116 
117   const char* name = GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode);
118   if (!name)
119     return nullptr;
120 
121   if (!m_pCharProcs)
122     return nullptr;
123 
124   RetainPtr<CPDF_Stream> pStream =
125       ToStream(m_pCharProcs->GetMutableDirectObjectFor(name));
126   if (!pStream)
127     return nullptr;
128 
129   std::unique_ptr<CPDF_Font::FormIface> pForm = m_pFormFactory->CreateForm(
130       m_pDocument, m_pFontResources ? m_pFontResources : m_pPageResources,
131       pStream);
132 
133   auto pNewChar = std::make_unique<CPDF_Type3Char>();
134 
135   // This can trigger recursion into this method. The content of |m_CacheMap|
136   // can change as a result. Thus after it returns, check the cache again for
137   // a cache hit.
138   {
139     AutoRestorer<int> restorer(&m_CharLoadingDepth);
140     m_CharLoadingDepth++;
141     pForm->ParseContentForType3Char(pNewChar.get());
142   }
143   it = m_CacheMap.find(charcode);
144   if (it != m_CacheMap.end())
145     return it->second.get();
146 
147   pNewChar->Transform(pForm.get(), m_FontMatrix);
148   if (pForm->HasPageObjects())
149     pNewChar->SetForm(std::move(pForm));
150 
151   CPDF_Type3Char* pCachedChar = pNewChar.get();
152   m_CacheMap[charcode] = std::move(pNewChar);
153   return pCachedChar;
154 }
155 
GetCharWidthF(uint32_t charcode)156 int CPDF_Type3Font::GetCharWidthF(uint32_t charcode) {
157   if (charcode >= std::size(m_CharWidthL))
158     charcode = 0;
159 
160   if (m_CharWidthL[charcode])
161     return m_CharWidthL[charcode];
162 
163   const CPDF_Type3Char* pChar = LoadChar(charcode);
164   return pChar ? pChar->width() : 0;
165 }
166 
GetCharBBox(uint32_t charcode)167 FX_RECT CPDF_Type3Font::GetCharBBox(uint32_t charcode) {
168   FX_RECT ret;
169   const CPDF_Type3Char* pChar = LoadChar(charcode);
170   if (pChar)
171     ret = pChar->bbox();
172   return ret;
173 }
174