blob: 04d6a72ec557527e99861ac9eac4d0c4019976ee [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
dan sinclair61b2fc72016-03-23 19:21:44 -04009#include "core/fpdfapi/fpdf_font/include/cpdf_font.h"
Dan Sinclair455a4192016-03-16 09:48:56 -040010#include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040011#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
12#include "core/fpdfapi/fpdf_parser/include/cpdf_simple_parser.h"
dan sinclair61b2fc72016-03-23 19:21:44 -040013#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h"
dsinclaircac704d2016-07-28 12:59:09 -070014#include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h"
15#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) {
Tom Sepezae51c812015-08-05 12:34:06 -0700158 pAPDict = new CPDF_Dictionary;
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) {
Tom Sepezae51c812015-08-05 12:34:06 -0700176 pStreamDict = new CPDF_Dictionary;
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) {
Tom Sepezae51c812015-08-05 12:34:06 -0700183 pStreamResList = new CPDF_Dictionary();
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) {
188 pStreamResFontList = new CPDF_Dictionary;
189 pStreamResList->SetReferenceFor(
190 "Font", m_pDocument,
191 m_pDocument->AddIndirectObject(pStreamResFontList));
192 }
193 if (!pStreamResFontList->KeyExist(sAlias)) {
194 pStreamResFontList->SetReferenceFor(sAlias, m_pDocument,
195 pFont->GetFontDict()->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 }
197 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200CPDF_Font* CBA_FontMap::GetAnnotDefaultFont(CFX_ByteString& sAlias) {
thestig1cd352e2016-06-07 17:53:06 -0700201 CPDF_Dictionary* pAcroFormDict = nullptr;
dsinclair38fd8442016-09-15 10:15:32 -0700202 const bool bWidget = (m_pAnnotDict->GetStringFor("Subtype") == "Widget");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 if (bWidget) {
204 if (CPDF_Dictionary* pRootDict = m_pDocument->GetRoot())
dsinclair38fd8442016-09-15 10:15:32 -0700205 pAcroFormDict = pRootDict->GetDictFor("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 }
207
208 CFX_ByteString sDA;
Wei Lid4e8f122016-03-21 11:20:44 -0700209 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pAnnotDict, "DA");
210 if (pObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 sDA = pObj->GetString();
212
213 if (bWidget) {
214 if (sDA.IsEmpty()) {
215 pObj = FPDF_GetFieldAttr(pAcroFormDict, "DA");
216 sDA = pObj ? pObj->GetString() : CFX_ByteString();
217 }
218 }
npm860a2d02016-09-12 15:02:28 -0700219 if (sDA.IsEmpty())
220 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221
npm860a2d02016-09-12 15:02:28 -0700222 CPDF_SimpleParser syntax(sDA.AsStringC());
223 syntax.FindTagParamFromStart("Tf", 2);
224 CFX_ByteString sFontName(syntax.GetWord());
225 sAlias = PDF_NameDecode(sFontName).Mid(1);
thestig1cd352e2016-06-07 17:53:06 -0700226 CPDF_Dictionary* pFontDict = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227
dsinclair38fd8442016-09-15 10:15:32 -0700228 if (CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP")) {
229 if (CPDF_Dictionary* pNormalDict = pAPDict->GetDictFor("N")) {
npm860a2d02016-09-12 15:02:28 -0700230 if (CPDF_Dictionary* pNormalResDict =
dsinclair38fd8442016-09-15 10:15:32 -0700231 pNormalDict->GetDictFor("Resources")) {
232 if (CPDF_Dictionary* pResFontDict = pNormalResDict->GetDictFor("Font"))
233 pFontDict = pResFontDict->GetDictFor(sAlias);
npm860a2d02016-09-12 15:02:28 -0700234 }
235 }
236 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237
npm860a2d02016-09-12 15:02:28 -0700238 if (bWidget && !pFontDict && pAcroFormDict) {
dsinclair38fd8442016-09-15 10:15:32 -0700239 if (CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR")) {
240 if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDictFor("Font"))
241 pFontDict = pDRFontDict->GetDictFor(sAlias);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 }
243 }
244
245 return pFontDict ? m_pDocument->LoadFont(pFontDict) : nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246}
247
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248void CBA_FontMap::SetAPType(const CFX_ByteString& sAPType) {
249 m_sAPType = sAPType;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 Reset();
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800252 Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253}