blob: 9e2a53f4961fa9e125fc7a9a28f83708057e214e [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
dsinclair166bc162016-09-29 11:58:38 -07009#include "core/fpdfapi/fpdf_font/cpdf_font.h"
dsinclair8a752ab2016-09-29 11:59:54 -070010#include "core/fpdfapi/fpdf_page/cpdf_page.h"
dsinclairc6c425a2016-09-29 12:01:30 -070011#include "core/fpdfapi/fpdf_parser/cpdf_document.h"
12#include "core/fpdfapi/fpdf_parser/cpdf_simple_parser.h"
13#include "core/fpdfapi/fpdf_parser/cpdf_stream.h"
14#include "core/fpdfapi/fpdf_parser/fpdf_parser_decode.h"
dsinclaircac704d2016-07-28 12:59:09 -070015#include "core/fpdfdoc/include/cpdf_formfield.h"
npm81ee14d2016-08-29 09:35:12 -070016#include "core/fxge/include/cfx_substfont.h"
jaepark27362762016-08-11 13:10:39 -070017#include "fpdfsdk/include/cpdfsdk_annot.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019CBA_FontMap::CBA_FontMap(CPDFSDK_Annot* pAnnot,
dsinclairb9590102016-04-27 06:38:59 -070020 CFX_SystemHandler* pSystemHandler)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021 : CPWL_FontMap(pSystemHandler),
thestig1cd352e2016-06-07 17:53:06 -070022 m_pDocument(nullptr),
23 m_pAnnotDict(nullptr),
24 m_pDefaultFont(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025 m_sAPType("N") {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 CPDF_Page* pPage = pAnnot->GetPDFPage();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 m_pDocument = pPage->m_pDocument;
29 m_pAnnotDict = pAnnot->GetPDFAnnot()->GetAnnotDict();
Lei Zhangfcfa3b82015-12-24 21:07:28 -080030 Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031}
32
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033CBA_FontMap::~CBA_FontMap() {}
34
35void CBA_FontMap::Reset() {
36 Empty();
thestig1cd352e2016-06-07 17:53:06 -070037 m_pDefaultFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038 m_sDefaultFontName = "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039}
40
Lei Zhangfcfa3b82015-12-24 21:07:28 -080041void CBA_FontMap::Initialize() {
npmea3c3be2016-09-19 07:24:33 -070042 int32_t nCharset = FXFONT_DEFAULT_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043
44 if (!m_pDefaultFont) {
45 m_pDefaultFont = GetAnnotDefaultFont(m_sDefaultFontName);
46 if (m_pDefaultFont) {
Lei Zhangc2fb35f2016-01-05 16:46:58 -080047 if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 nCharset = pSubstFont->m_Charset;
Lei Zhangc2fb35f2016-01-05 16:46:58 -080049 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 if (m_sDefaultFontName == "Wingdings" ||
51 m_sDefaultFontName == "Wingdings2" ||
52 m_sDefaultFontName == "Wingdings3" ||
53 m_sDefaultFontName == "Webdings")
npmea3c3be2016-09-19 07:24:33 -070054 nCharset = FXFONT_SYMBOL_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 else
npmea3c3be2016-09-19 07:24:33 -070056 nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 }
58 AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);
59 AddFontToAnnotDict(m_pDefaultFont, m_sDefaultFontName);
60 }
61 }
62
npmea3c3be2016-09-19 07:24:33 -070063 if (nCharset != FXFONT_ANSI_CHARSET)
Lei Zhangfcfa3b82015-12-24 21:07:28 -080064 CPWL_FontMap::Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065}
66
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067void CBA_FontMap::SetDefaultFont(CPDF_Font* pFont,
68 const CFX_ByteString& sFontName) {
Lei Zhang5eca3052016-02-22 20:32:21 -080069 ASSERT(pFont);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070
71 if (m_pDefaultFont)
72 return;
73
74 m_pDefaultFont = pFont;
75 m_sDefaultFontName = sFontName;
76
npmea3c3be2016-09-19 07:24:33 -070077 int32_t nCharset = FXFONT_DEFAULT_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont())
79 nCharset = pSubstFont->m_Charset;
80 AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081}
82
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083CPDF_Font* CBA_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias,
84 int32_t nCharset) {
dsinclair38fd8442016-09-15 10:15:32 -070085 if (m_pAnnotDict->GetStringFor("Subtype") != "Widget")
thestig907a5222016-06-21 14:38:27 -070086 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087
thestig907a5222016-06-21 14:38:27 -070088 CPDF_Document* pDocument = GetDocument();
89 CPDF_Dictionary* pRootDict = pDocument->GetRoot();
90 if (!pRootDict)
91 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092
dsinclair38fd8442016-09-15 10:15:32 -070093 CPDF_Dictionary* pAcroFormDict = pRootDict->GetDictFor("AcroForm");
thestig907a5222016-06-21 14:38:27 -070094 if (!pAcroFormDict)
95 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096
dsinclair38fd8442016-09-15 10:15:32 -070097 CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR");
thestig907a5222016-06-21 14:38:27 -070098 if (!pDRDict)
99 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100
thestig907a5222016-06-21 14:38:27 -0700101 return FindResFontSameCharset(pDRDict, sFontAlias, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102}
103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104CPDF_Document* CBA_FontMap::GetDocument() {
105 return m_pDocument;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106}
107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108CPDF_Font* CBA_FontMap::FindResFontSameCharset(CPDF_Dictionary* pResDict,
109 CFX_ByteString& sFontAlias,
110 int32_t nCharset) {
111 if (!pResDict)
thestig1cd352e2016-06-07 17:53:06 -0700112 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700113
dsinclair38fd8442016-09-15 10:15:32 -0700114 CPDF_Dictionary* pFonts = pResDict->GetDictFor("Font");
Lei Zhang412e9082015-12-14 18:34:00 -0800115 if (!pFonts)
thestig1cd352e2016-06-07 17:53:06 -0700116 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117
Lei Zhang5eca3052016-02-22 20:32:21 -0800118 CPDF_Document* pDocument = GetDocument();
thestig1cd352e2016-06-07 17:53:06 -0700119 CPDF_Font* pFind = nullptr;
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800120 for (const auto& it : *pFonts) {
121 const CFX_ByteString& csKey = it.first;
122 CPDF_Object* pObj = it.second;
Lei Zhang412e9082015-12-14 18:34:00 -0800123 if (!pObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125
Dan Sinclairf1251c12015-10-20 16:24:45 -0400126 CPDF_Dictionary* pElement = ToDictionary(pObj->GetDirect());
127 if (!pElement)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 continue;
dsinclair38fd8442016-09-15 10:15:32 -0700129 if (pElement->GetStringFor("Type") != "Font")
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 continue;
131
132 CPDF_Font* pFont = pDocument->LoadFont(pElement);
Lei Zhang412e9082015-12-14 18:34:00 -0800133 if (!pFont)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 continue;
135 const CFX_SubstFont* pSubst = pFont->GetSubstFont();
Lei Zhang412e9082015-12-14 18:34:00 -0800136 if (!pSubst)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 continue;
138 if (pSubst->m_Charset == nCharset) {
139 sFontAlias = csKey;
140 pFind = pFont;
141 }
142 }
143 return pFind;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144}
145
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146void CBA_FontMap::AddedFont(CPDF_Font* pFont,
147 const CFX_ByteString& sFontAlias) {
148 AddFontToAnnotDict(pFont, sFontAlias);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700149}
150
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont,
152 const CFX_ByteString& sAlias) {
153 if (!pFont)
154 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700155
dsinclair38fd8442016-09-15 10:15:32 -0700156 CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP");
Lei Zhang412e9082015-12-14 18:34:00 -0800157 if (!pAPDict) {
tsepez698c5712016-09-28 16:47:07 -0700158 pAPDict = new CPDF_Dictionary(m_pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700159 m_pAnnotDict->SetFor("AP", pAPDict);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 }
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) {
tsepeze6db16e2016-09-19 10:45:09 -0700169 pStream = new CPDF_Stream;
tsepezbb577af2016-09-21 19:10:19 -0700170 pAPDict->SetReferenceFor(m_sAPType, m_pDocument,
171 m_pDocument->AddIndirectObject(pStream));
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) {
tsepez698c5712016-09-28 16:47:07 -0700176 pStreamDict = new CPDF_Dictionary(m_pDocument->GetByteStringPool());
thestig1cd352e2016-06-07 17:53:06 -0700177 pStream->InitStream(nullptr, 0, pStreamDict);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 }
179
180 if (pStreamDict) {
dsinclair38fd8442016-09-15 10:15:32 -0700181 CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 if (!pStreamResList) {
tsepez698c5712016-09-28 16:47:07 -0700183 pStreamResList = new CPDF_Dictionary(m_pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700184 pStreamDict->SetFor("Resources", pStreamResList);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 }
tsepezbb577af2016-09-21 19:10:19 -0700186 CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font");
187 if (!pStreamResFontList) {
tsepez698c5712016-09-28 16:47:07 -0700188 pStreamResFontList =
189 new CPDF_Dictionary(m_pDocument->GetByteStringPool());
tsepezbb577af2016-09-21 19:10:19 -0700190 pStreamResList->SetReferenceFor(
191 "Font", m_pDocument,
192 m_pDocument->AddIndirectObject(pStreamResFontList));
193 }
194 if (!pStreamResFontList->KeyExist(sAlias)) {
195 pStreamResFontList->SetReferenceFor(sAlias, m_pDocument,
196 pFont->GetFontDict()->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 }
198 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201CPDF_Font* CBA_FontMap::GetAnnotDefaultFont(CFX_ByteString& sAlias) {
thestig1cd352e2016-06-07 17:53:06 -0700202 CPDF_Dictionary* pAcroFormDict = nullptr;
dsinclair38fd8442016-09-15 10:15:32 -0700203 const bool bWidget = (m_pAnnotDict->GetStringFor("Subtype") == "Widget");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 if (bWidget) {
205 if (CPDF_Dictionary* pRootDict = m_pDocument->GetRoot())
dsinclair38fd8442016-09-15 10:15:32 -0700206 pAcroFormDict = pRootDict->GetDictFor("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 }
208
209 CFX_ByteString sDA;
Wei Lid4e8f122016-03-21 11:20:44 -0700210 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pAnnotDict, "DA");
211 if (pObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 sDA = pObj->GetString();
213
214 if (bWidget) {
215 if (sDA.IsEmpty()) {
216 pObj = FPDF_GetFieldAttr(pAcroFormDict, "DA");
217 sDA = pObj ? pObj->GetString() : CFX_ByteString();
218 }
219 }
npm860a2d02016-09-12 15:02:28 -0700220 if (sDA.IsEmpty())
221 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222
npm860a2d02016-09-12 15:02:28 -0700223 CPDF_SimpleParser syntax(sDA.AsStringC());
224 syntax.FindTagParamFromStart("Tf", 2);
225 CFX_ByteString sFontName(syntax.GetWord());
226 sAlias = PDF_NameDecode(sFontName).Mid(1);
thestig1cd352e2016-06-07 17:53:06 -0700227 CPDF_Dictionary* pFontDict = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228
dsinclair38fd8442016-09-15 10:15:32 -0700229 if (CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP")) {
230 if (CPDF_Dictionary* pNormalDict = pAPDict->GetDictFor("N")) {
npm860a2d02016-09-12 15:02:28 -0700231 if (CPDF_Dictionary* pNormalResDict =
dsinclair38fd8442016-09-15 10:15:32 -0700232 pNormalDict->GetDictFor("Resources")) {
233 if (CPDF_Dictionary* pResFontDict = pNormalResDict->GetDictFor("Font"))
234 pFontDict = pResFontDict->GetDictFor(sAlias);
npm860a2d02016-09-12 15:02:28 -0700235 }
236 }
237 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238
npm860a2d02016-09-12 15:02:28 -0700239 if (bWidget && !pFontDict && pAcroFormDict) {
dsinclair38fd8442016-09-15 10:15:32 -0700240 if (CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR")) {
241 if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDictFor("Font"))
242 pFontDict = pDRFontDict->GetDictFor(sAlias);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 }
244 }
245
246 return pFontDict ? m_pDocument->LoadFont(pFontDict) : nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247}
248
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249void CBA_FontMap::SetAPType(const CFX_ByteString& sAPType) {
250 m_sAPType = sAPType;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 Reset();
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800253 Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700254}