• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "fpdfsdk/formfiller/cba_fontmap.h"
8 
9 #include <utility>
10 
11 #include "core/fpdfapi/font/cpdf_font.h"
12 #include "core/fpdfapi/page/cpdf_page.h"
13 #include "core/fpdfapi/parser/cpdf_document.h"
14 #include "core/fpdfapi/parser/cpdf_reference.h"
15 #include "core/fpdfapi/parser/cpdf_simple_parser.h"
16 #include "core/fpdfapi/parser/cpdf_stream.h"
17 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
18 #include "core/fpdfdoc/cpdf_formfield.h"
19 #include "core/fxge/cfx_substfont.h"
20 #include "fpdfsdk/cpdfsdk_annot.h"
21 
CBA_FontMap(CPDFSDK_Annot * pAnnot,CFX_SystemHandler * pSystemHandler)22 CBA_FontMap::CBA_FontMap(CPDFSDK_Annot* pAnnot,
23                          CFX_SystemHandler* pSystemHandler)
24     : CPWL_FontMap(pSystemHandler),
25       m_pDocument(nullptr),
26       m_pAnnotDict(nullptr),
27       m_pDefaultFont(nullptr),
28       m_sAPType("N") {
29   CPDF_Page* pPage = pAnnot->GetPDFPage();
30 
31   m_pDocument = pPage->m_pDocument;
32   m_pAnnotDict = pAnnot->GetPDFAnnot()->GetAnnotDict();
33   Initialize();
34 }
35 
~CBA_FontMap()36 CBA_FontMap::~CBA_FontMap() {}
37 
Reset()38 void CBA_FontMap::Reset() {
39   Empty();
40   m_pDefaultFont = nullptr;
41   m_sDefaultFontName = "";
42 }
43 
Initialize()44 void CBA_FontMap::Initialize() {
45   int32_t nCharset = FXFONT_DEFAULT_CHARSET;
46 
47   if (!m_pDefaultFont) {
48     m_pDefaultFont = GetAnnotDefaultFont(m_sDefaultFontName);
49     if (m_pDefaultFont) {
50       if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont()) {
51         nCharset = pSubstFont->m_Charset;
52       } else {
53         if (m_sDefaultFontName == "Wingdings" ||
54             m_sDefaultFontName == "Wingdings2" ||
55             m_sDefaultFontName == "Wingdings3" ||
56             m_sDefaultFontName == "Webdings")
57           nCharset = FXFONT_SYMBOL_CHARSET;
58         else
59           nCharset = FXFONT_ANSI_CHARSET;
60       }
61       AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);
62       AddFontToAnnotDict(m_pDefaultFont, m_sDefaultFontName);
63     }
64   }
65 
66   if (nCharset != FXFONT_ANSI_CHARSET)
67     CPWL_FontMap::Initialize();
68 }
69 
SetDefaultFont(CPDF_Font * pFont,const CFX_ByteString & sFontName)70 void CBA_FontMap::SetDefaultFont(CPDF_Font* pFont,
71                                  const CFX_ByteString& sFontName) {
72   ASSERT(pFont);
73 
74   if (m_pDefaultFont)
75     return;
76 
77   m_pDefaultFont = pFont;
78   m_sDefaultFontName = sFontName;
79 
80   int32_t nCharset = FXFONT_DEFAULT_CHARSET;
81   if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont())
82     nCharset = pSubstFont->m_Charset;
83   AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);
84 }
85 
FindFontSameCharset(CFX_ByteString & sFontAlias,int32_t nCharset)86 CPDF_Font* CBA_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias,
87                                             int32_t nCharset) {
88   if (m_pAnnotDict->GetStringFor("Subtype") != "Widget")
89     return nullptr;
90 
91   CPDF_Document* pDocument = GetDocument();
92   CPDF_Dictionary* pRootDict = pDocument->GetRoot();
93   if (!pRootDict)
94     return nullptr;
95 
96   CPDF_Dictionary* pAcroFormDict = pRootDict->GetDictFor("AcroForm");
97   if (!pAcroFormDict)
98     return nullptr;
99 
100   CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR");
101   if (!pDRDict)
102     return nullptr;
103 
104   return FindResFontSameCharset(pDRDict, sFontAlias, nCharset);
105 }
106 
GetDocument()107 CPDF_Document* CBA_FontMap::GetDocument() {
108   return m_pDocument;
109 }
110 
FindResFontSameCharset(CPDF_Dictionary * pResDict,CFX_ByteString & sFontAlias,int32_t nCharset)111 CPDF_Font* CBA_FontMap::FindResFontSameCharset(CPDF_Dictionary* pResDict,
112                                                CFX_ByteString& sFontAlias,
113                                                int32_t nCharset) {
114   if (!pResDict)
115     return nullptr;
116 
117   CPDF_Dictionary* pFonts = pResDict->GetDictFor("Font");
118   if (!pFonts)
119     return nullptr;
120 
121   CPDF_Document* pDocument = GetDocument();
122   CPDF_Font* pFind = nullptr;
123   for (const auto& it : *pFonts) {
124     const CFX_ByteString& csKey = it.first;
125     if (!it.second)
126       continue;
127 
128     CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
129     if (!pElement)
130       continue;
131     if (pElement->GetStringFor("Type") != "Font")
132       continue;
133 
134     CPDF_Font* pFont = pDocument->LoadFont(pElement);
135     if (!pFont)
136       continue;
137     const CFX_SubstFont* pSubst = pFont->GetSubstFont();
138     if (!pSubst)
139       continue;
140     if (pSubst->m_Charset == nCharset) {
141       sFontAlias = csKey;
142       pFind = pFont;
143     }
144   }
145   return pFind;
146 }
147 
AddedFont(CPDF_Font * pFont,const CFX_ByteString & sFontAlias)148 void CBA_FontMap::AddedFont(CPDF_Font* pFont,
149                             const CFX_ByteString& sFontAlias) {
150   AddFontToAnnotDict(pFont, sFontAlias);
151 }
152 
AddFontToAnnotDict(CPDF_Font * pFont,const CFX_ByteString & sAlias)153 void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont,
154                                      const CFX_ByteString& sAlias) {
155   if (!pFont)
156     return;
157 
158   CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP");
159   if (!pAPDict)
160     pAPDict = m_pAnnotDict->SetNewFor<CPDF_Dictionary>("AP");
161 
162   // to avoid checkbox and radiobutton
163   CPDF_Object* pObject = pAPDict->GetObjectFor(m_sAPType);
164   if (ToDictionary(pObject))
165     return;
166 
167   CPDF_Stream* pStream = pAPDict->GetStreamFor(m_sAPType);
168   if (!pStream) {
169     pStream = m_pDocument->NewIndirect<CPDF_Stream>();
170     pAPDict->SetNewFor<CPDF_Reference>(m_sAPType, m_pDocument,
171                                        pStream->GetObjNum());
172   }
173 
174   CPDF_Dictionary* pStreamDict = pStream->GetDict();
175   if (!pStreamDict) {
176     auto pOwnedDict =
177         pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool());
178     pStreamDict = pOwnedDict.get();
179     pStream->InitStream(nullptr, 0, std::move(pOwnedDict));
180   }
181 
182   CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources");
183   if (!pStreamResList)
184     pStreamResList = pStreamDict->SetNewFor<CPDF_Dictionary>("Resources");
185   CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font");
186   if (!pStreamResFontList) {
187     pStreamResFontList = m_pDocument->NewIndirect<CPDF_Dictionary>();
188     pStreamResList->SetNewFor<CPDF_Reference>("Font", m_pDocument,
189                                               pStreamResFontList->GetObjNum());
190   }
191   if (!pStreamResFontList->KeyExist(sAlias)) {
192     pStreamResFontList->SetNewFor<CPDF_Reference>(
193         sAlias, m_pDocument, pFont->GetFontDict()->GetObjNum());
194   }
195 }
196 
GetAnnotDefaultFont(CFX_ByteString & sAlias)197 CPDF_Font* CBA_FontMap::GetAnnotDefaultFont(CFX_ByteString& sAlias) {
198   CPDF_Dictionary* pAcroFormDict = nullptr;
199   const bool bWidget = (m_pAnnotDict->GetStringFor("Subtype") == "Widget");
200   if (bWidget) {
201     if (CPDF_Dictionary* pRootDict = m_pDocument->GetRoot())
202       pAcroFormDict = pRootDict->GetDictFor("AcroForm");
203   }
204 
205   CFX_ByteString sDA;
206   CPDF_Object* pObj = FPDF_GetFieldAttr(m_pAnnotDict, "DA");
207   if (pObj)
208     sDA = pObj->GetString();
209 
210   if (bWidget) {
211     if (sDA.IsEmpty()) {
212       pObj = FPDF_GetFieldAttr(pAcroFormDict, "DA");
213       sDA = pObj ? pObj->GetString() : CFX_ByteString();
214     }
215   }
216   if (sDA.IsEmpty())
217     return nullptr;
218 
219   CPDF_SimpleParser syntax(sDA.AsStringC());
220   syntax.FindTagParamFromStart("Tf", 2);
221   CFX_ByteString sFontName(syntax.GetWord());
222   sAlias = PDF_NameDecode(sFontName).Mid(1);
223   CPDF_Dictionary* pFontDict = nullptr;
224 
225   if (CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP")) {
226     if (CPDF_Dictionary* pNormalDict = pAPDict->GetDictFor("N")) {
227       if (CPDF_Dictionary* pNormalResDict =
228               pNormalDict->GetDictFor("Resources")) {
229         if (CPDF_Dictionary* pResFontDict = pNormalResDict->GetDictFor("Font"))
230           pFontDict = pResFontDict->GetDictFor(sAlias);
231       }
232     }
233   }
234 
235   if (bWidget && !pFontDict && pAcroFormDict) {
236     if (CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR")) {
237       if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDictFor("Font"))
238         pFontDict = pDRFontDict->GetDictFor(sAlias);
239     }
240   }
241 
242   return pFontDict ? m_pDocument->LoadFont(pFontDict) : nullptr;
243 }
244 
SetAPType(const CFX_ByteString & sAPType)245 void CBA_FontMap::SetAPType(const CFX_ByteString& sAPType) {
246   m_sAPType = sAPType;
247 
248   Reset();
249   Initialize();
250 }
251