blob: 750b41627e798fd790977191d05baf03f98b4be8 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairedbb3192016-03-21 09:08:24 -04007#include "fpdfsdk/formfiller/cba_fontmap.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -08008
tsepez9e05ee12016-11-21 13:19:10 -08009#include <utility>
10
dsinclairbc5e6d22016-10-04 11:08:49 -070011#include "core/fpdfapi/font/cpdf_font.h"
dsinclair41872fa2016-10-04 11:29:35 -070012#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070013#include "core/fpdfapi/parser/cpdf_document.h"
tsepez0e606b52016-11-18 16:22:41 -080014#include "core/fpdfapi/parser/cpdf_reference.h"
dsinclair488b7ad2016-10-04 11:55:50 -070015#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"
dsinclair1727aee2016-09-29 13:12:56 -070018#include "core/fpdfdoc/cpdf_formfield.h"
dsinclair74a34fc2016-09-29 16:41:42 -070019#include "core/fxge/cfx_substfont.h"
dsinclair114e46a2016-09-29 17:18:21 -070020#include "fpdfsdk/cpdfsdk_annot.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022CBA_FontMap::CBA_FontMap(CPDFSDK_Annot* pAnnot,
dsinclairb9590102016-04-27 06:38:59 -070023 CFX_SystemHandler* pSystemHandler)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 : CPWL_FontMap(pSystemHandler),
thestig1cd352e2016-06-07 17:53:06 -070025 m_pDocument(nullptr),
26 m_pAnnotDict(nullptr),
27 m_pDefaultFont(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 m_sAPType("N") {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029 CPDF_Page* pPage = pAnnot->GetPDFPage();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031 m_pDocument = pPage->m_pDocument;
32 m_pAnnotDict = pAnnot->GetPDFAnnot()->GetAnnotDict();
Lei Zhangfcfa3b82015-12-24 21:07:28 -080033 Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034}
35
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036CBA_FontMap::~CBA_FontMap() {}
37
38void CBA_FontMap::Reset() {
39 Empty();
thestig1cd352e2016-06-07 17:53:06 -070040 m_pDefaultFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 m_sDefaultFontName = "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042}
43
Lei Zhangfcfa3b82015-12-24 21:07:28 -080044void CBA_FontMap::Initialize() {
npmea3c3be2016-09-19 07:24:33 -070045 int32_t nCharset = FXFONT_DEFAULT_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046
47 if (!m_pDefaultFont) {
48 m_pDefaultFont = GetAnnotDefaultFont(m_sDefaultFontName);
49 if (m_pDefaultFont) {
Lei Zhangc2fb35f2016-01-05 16:46:58 -080050 if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 nCharset = pSubstFont->m_Charset;
Lei Zhangc2fb35f2016-01-05 16:46:58 -080052 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 if (m_sDefaultFontName == "Wingdings" ||
54 m_sDefaultFontName == "Wingdings2" ||
55 m_sDefaultFontName == "Wingdings3" ||
56 m_sDefaultFontName == "Webdings")
npmea3c3be2016-09-19 07:24:33 -070057 nCharset = FXFONT_SYMBOL_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 else
npmea3c3be2016-09-19 07:24:33 -070059 nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 }
61 AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);
62 AddFontToAnnotDict(m_pDefaultFont, m_sDefaultFontName);
63 }
64 }
65
npmea3c3be2016-09-19 07:24:33 -070066 if (nCharset != FXFONT_ANSI_CHARSET)
Lei Zhangfcfa3b82015-12-24 21:07:28 -080067 CPWL_FontMap::Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068}
69
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070void CBA_FontMap::SetDefaultFont(CPDF_Font* pFont,
71 const CFX_ByteString& sFontName) {
Lei Zhang5eca3052016-02-22 20:32:21 -080072 ASSERT(pFont);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073
74 if (m_pDefaultFont)
75 return;
76
77 m_pDefaultFont = pFont;
78 m_sDefaultFontName = sFontName;
79
npmea3c3be2016-09-19 07:24:33 -070080 int32_t nCharset = FXFONT_DEFAULT_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont())
82 nCharset = pSubstFont->m_Charset;
83 AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084}
85
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086CPDF_Font* CBA_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias,
87 int32_t nCharset) {
dsinclair38fd8442016-09-15 10:15:32 -070088 if (m_pAnnotDict->GetStringFor("Subtype") != "Widget")
thestig907a5222016-06-21 14:38:27 -070089 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090
thestig907a5222016-06-21 14:38:27 -070091 CPDF_Document* pDocument = GetDocument();
92 CPDF_Dictionary* pRootDict = pDocument->GetRoot();
93 if (!pRootDict)
94 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095
dsinclair38fd8442016-09-15 10:15:32 -070096 CPDF_Dictionary* pAcroFormDict = pRootDict->GetDictFor("AcroForm");
thestig907a5222016-06-21 14:38:27 -070097 if (!pAcroFormDict)
98 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099
dsinclair38fd8442016-09-15 10:15:32 -0700100 CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR");
thestig907a5222016-06-21 14:38:27 -0700101 if (!pDRDict)
102 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103
thestig907a5222016-06-21 14:38:27 -0700104 return FindResFontSameCharset(pDRDict, sFontAlias, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700105}
106
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107CPDF_Document* CBA_FontMap::GetDocument() {
108 return m_pDocument;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700109}
110
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111CPDF_Font* CBA_FontMap::FindResFontSameCharset(CPDF_Dictionary* pResDict,
112 CFX_ByteString& sFontAlias,
113 int32_t nCharset) {
114 if (!pResDict)
thestig1cd352e2016-06-07 17:53:06 -0700115 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116
dsinclair38fd8442016-09-15 10:15:32 -0700117 CPDF_Dictionary* pFonts = pResDict->GetDictFor("Font");
Lei Zhang412e9082015-12-14 18:34:00 -0800118 if (!pFonts)
thestig1cd352e2016-06-07 17:53:06 -0700119 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120
Lei Zhang5eca3052016-02-22 20:32:21 -0800121 CPDF_Document* pDocument = GetDocument();
thestig1cd352e2016-06-07 17:53:06 -0700122 CPDF_Font* pFind = nullptr;
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800123 for (const auto& it : *pFonts) {
124 const CFX_ByteString& csKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800125 if (!it.second)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127
tsepez0e606b52016-11-18 16:22:41 -0800128 CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
Dan Sinclairf1251c12015-10-20 16:24:45 -0400129 if (!pElement)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 continue;
dsinclair38fd8442016-09-15 10:15:32 -0700131 if (pElement->GetStringFor("Type") != "Font")
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 continue;
133
134 CPDF_Font* pFont = pDocument->LoadFont(pElement);
Lei Zhang412e9082015-12-14 18:34:00 -0800135 if (!pFont)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 continue;
137 const CFX_SubstFont* pSubst = pFont->GetSubstFont();
Lei Zhang412e9082015-12-14 18:34:00 -0800138 if (!pSubst)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 continue;
140 if (pSubst->m_Charset == nCharset) {
141 sFontAlias = csKey;
142 pFind = pFont;
143 }
144 }
145 return pFind;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146}
147
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148void CBA_FontMap::AddedFont(CPDF_Font* pFont,
149 const CFX_ByteString& sFontAlias) {
150 AddFontToAnnotDict(pFont, sFontAlias);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700151}
152
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont,
154 const CFX_ByteString& sAlias) {
155 if (!pFont)
156 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700157
dsinclair38fd8442016-09-15 10:15:32 -0700158 CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP");
tsepez0e606b52016-11-18 16:22:41 -0800159 if (!pAPDict)
160 pAPDict = m_pAnnotDict->SetNewFor<CPDF_Dictionary>("AP");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 // to avoid checkbox and radiobutton
dsinclair38fd8442016-09-15 10:15:32 -0700163 CPDF_Object* pObject = pAPDict->GetObjectFor(m_sAPType);
Dan Sinclairf1251c12015-10-20 16:24:45 -0400164 if (ToDictionary(pObject))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700166
dsinclair38fd8442016-09-15 10:15:32 -0700167 CPDF_Stream* pStream = pAPDict->GetStreamFor(m_sAPType);
Lei Zhang412e9082015-12-14 18:34:00 -0800168 if (!pStream) {
tsepez70c4afd2016-11-15 11:33:44 -0800169 pStream = m_pDocument->NewIndirect<CPDF_Stream>();
tsepez0e606b52016-11-18 16:22:41 -0800170 pAPDict->SetNewFor<CPDF_Reference>(m_sAPType, m_pDocument,
171 pStream->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700173
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 CPDF_Dictionary* pStreamDict = pStream->GetDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 if (!pStreamDict) {
tsepez9e05ee12016-11-21 13:19:10 -0800176 auto pOwnedDict =
177 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool());
178 pStreamDict = pOwnedDict.get();
179 pStream->InitStream(nullptr, 0, std::move(pOwnedDict));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 }
181
tsepez9e05ee12016-11-21 13:19:10 -0800182 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());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195}
196
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197CPDF_Font* CBA_FontMap::GetAnnotDefaultFont(CFX_ByteString& sAlias) {
thestig1cd352e2016-06-07 17:53:06 -0700198 CPDF_Dictionary* pAcroFormDict = nullptr;
dsinclair38fd8442016-09-15 10:15:32 -0700199 const bool bWidget = (m_pAnnotDict->GetStringFor("Subtype") == "Widget");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 if (bWidget) {
201 if (CPDF_Dictionary* pRootDict = m_pDocument->GetRoot())
dsinclair38fd8442016-09-15 10:15:32 -0700202 pAcroFormDict = pRootDict->GetDictFor("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 }
204
205 CFX_ByteString sDA;
Wei Lid4e8f122016-03-21 11:20:44 -0700206 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pAnnotDict, "DA");
207 if (pObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 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 }
npm860a2d02016-09-12 15:02:28 -0700216 if (sDA.IsEmpty())
217 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218
npm860a2d02016-09-12 15:02:28 -0700219 CPDF_SimpleParser syntax(sDA.AsStringC());
220 syntax.FindTagParamFromStart("Tf", 2);
221 CFX_ByteString sFontName(syntax.GetWord());
222 sAlias = PDF_NameDecode(sFontName).Mid(1);
thestig1cd352e2016-06-07 17:53:06 -0700223 CPDF_Dictionary* pFontDict = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224
dsinclair38fd8442016-09-15 10:15:32 -0700225 if (CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP")) {
226 if (CPDF_Dictionary* pNormalDict = pAPDict->GetDictFor("N")) {
npm860a2d02016-09-12 15:02:28 -0700227 if (CPDF_Dictionary* pNormalResDict =
dsinclair38fd8442016-09-15 10:15:32 -0700228 pNormalDict->GetDictFor("Resources")) {
229 if (CPDF_Dictionary* pResFontDict = pNormalResDict->GetDictFor("Font"))
230 pFontDict = pResFontDict->GetDictFor(sAlias);
npm860a2d02016-09-12 15:02:28 -0700231 }
232 }
233 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234
npm860a2d02016-09-12 15:02:28 -0700235 if (bWidget && !pFontDict && pAcroFormDict) {
dsinclair38fd8442016-09-15 10:15:32 -0700236 if (CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR")) {
237 if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDictFor("Font"))
238 pFontDict = pDRFontDict->GetDictFor(sAlias);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239 }
240 }
241
242 return pFontDict ? m_pDocument->LoadFont(pFontDict) : nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700243}
244
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245void CBA_FontMap::SetAPType(const CFX_ByteString& sAPType) {
246 m_sAPType = sAPType;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 Reset();
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800249 Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250}