blob: 83e45791e20f7f7a5a4eaa317be4a840829970f3 [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
dsinclairbc5e6d22016-10-04 11:08:49 -07009#include "core/fpdfapi/font/cpdf_font.h"
dsinclair41872fa2016-10-04 11:29:35 -070010#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070011#include "core/fpdfapi/parser/cpdf_document.h"
tsepez0e606b52016-11-18 16:22:41 -080012#include "core/fpdfapi/parser/cpdf_reference.h"
dsinclair488b7ad2016-10-04 11:55:50 -070013#include "core/fpdfapi/parser/cpdf_simple_parser.h"
14#include "core/fpdfapi/parser/cpdf_stream.h"
15#include "core/fpdfapi/parser/fpdf_parser_decode.h"
dsinclair1727aee2016-09-29 13:12:56 -070016#include "core/fpdfdoc/cpdf_formfield.h"
dsinclair74a34fc2016-09-29 16:41:42 -070017#include "core/fxge/cfx_substfont.h"
dsinclair114e46a2016-09-29 17:18:21 -070018#include "fpdfsdk/cpdfsdk_annot.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020CBA_FontMap::CBA_FontMap(CPDFSDK_Annot* pAnnot,
dsinclairb9590102016-04-27 06:38:59 -070021 CFX_SystemHandler* pSystemHandler)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022 : CPWL_FontMap(pSystemHandler),
thestig1cd352e2016-06-07 17:53:06 -070023 m_pDocument(nullptr),
24 m_pAnnotDict(nullptr),
25 m_pDefaultFont(nullptr),
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 m_sAPType("N") {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 CPDF_Page* pPage = pAnnot->GetPDFPage();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029 m_pDocument = pPage->m_pDocument;
30 m_pAnnotDict = pAnnot->GetPDFAnnot()->GetAnnotDict();
Lei Zhangfcfa3b82015-12-24 21:07:28 -080031 Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032}
33
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034CBA_FontMap::~CBA_FontMap() {}
35
36void CBA_FontMap::Reset() {
37 Empty();
thestig1cd352e2016-06-07 17:53:06 -070038 m_pDefaultFont = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 m_sDefaultFontName = "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040}
41
Lei Zhangfcfa3b82015-12-24 21:07:28 -080042void CBA_FontMap::Initialize() {
npmea3c3be2016-09-19 07:24:33 -070043 int32_t nCharset = FXFONT_DEFAULT_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044
45 if (!m_pDefaultFont) {
46 m_pDefaultFont = GetAnnotDefaultFont(m_sDefaultFontName);
47 if (m_pDefaultFont) {
Lei Zhangc2fb35f2016-01-05 16:46:58 -080048 if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 nCharset = pSubstFont->m_Charset;
Lei Zhangc2fb35f2016-01-05 16:46:58 -080050 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 if (m_sDefaultFontName == "Wingdings" ||
52 m_sDefaultFontName == "Wingdings2" ||
53 m_sDefaultFontName == "Wingdings3" ||
54 m_sDefaultFontName == "Webdings")
npmea3c3be2016-09-19 07:24:33 -070055 nCharset = FXFONT_SYMBOL_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 else
npmea3c3be2016-09-19 07:24:33 -070057 nCharset = FXFONT_ANSI_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 }
59 AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);
60 AddFontToAnnotDict(m_pDefaultFont, m_sDefaultFontName);
61 }
62 }
63
npmea3c3be2016-09-19 07:24:33 -070064 if (nCharset != FXFONT_ANSI_CHARSET)
Lei Zhangfcfa3b82015-12-24 21:07:28 -080065 CPWL_FontMap::Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066}
67
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068void CBA_FontMap::SetDefaultFont(CPDF_Font* pFont,
69 const CFX_ByteString& sFontName) {
Lei Zhang5eca3052016-02-22 20:32:21 -080070 ASSERT(pFont);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071
72 if (m_pDefaultFont)
73 return;
74
75 m_pDefaultFont = pFont;
76 m_sDefaultFontName = sFontName;
77
npmea3c3be2016-09-19 07:24:33 -070078 int32_t nCharset = FXFONT_DEFAULT_CHARSET;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont())
80 nCharset = pSubstFont->m_Charset;
81 AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082}
83
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084CPDF_Font* CBA_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias,
85 int32_t nCharset) {
dsinclair38fd8442016-09-15 10:15:32 -070086 if (m_pAnnotDict->GetStringFor("Subtype") != "Widget")
thestig907a5222016-06-21 14:38:27 -070087 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088
thestig907a5222016-06-21 14:38:27 -070089 CPDF_Document* pDocument = GetDocument();
90 CPDF_Dictionary* pRootDict = pDocument->GetRoot();
91 if (!pRootDict)
92 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093
dsinclair38fd8442016-09-15 10:15:32 -070094 CPDF_Dictionary* pAcroFormDict = pRootDict->GetDictFor("AcroForm");
thestig907a5222016-06-21 14:38:27 -070095 if (!pAcroFormDict)
96 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097
dsinclair38fd8442016-09-15 10:15:32 -070098 CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR");
thestig907a5222016-06-21 14:38:27 -070099 if (!pDRDict)
100 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101
thestig907a5222016-06-21 14:38:27 -0700102 return FindResFontSameCharset(pDRDict, sFontAlias, nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700103}
104
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105CPDF_Document* CBA_FontMap::GetDocument() {
106 return m_pDocument;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700107}
108
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109CPDF_Font* CBA_FontMap::FindResFontSameCharset(CPDF_Dictionary* pResDict,
110 CFX_ByteString& sFontAlias,
111 int32_t nCharset) {
112 if (!pResDict)
thestig1cd352e2016-06-07 17:53:06 -0700113 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114
dsinclair38fd8442016-09-15 10:15:32 -0700115 CPDF_Dictionary* pFonts = pResDict->GetDictFor("Font");
Lei Zhang412e9082015-12-14 18:34:00 -0800116 if (!pFonts)
thestig1cd352e2016-06-07 17:53:06 -0700117 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700118
Lei Zhang5eca3052016-02-22 20:32:21 -0800119 CPDF_Document* pDocument = GetDocument();
thestig1cd352e2016-06-07 17:53:06 -0700120 CPDF_Font* pFind = nullptr;
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800121 for (const auto& it : *pFonts) {
122 const CFX_ByteString& csKey = it.first;
tsepez0e606b52016-11-18 16:22:41 -0800123 if (!it.second)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125
tsepez0e606b52016-11-18 16:22:41 -0800126 CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
Dan Sinclairf1251c12015-10-20 16:24:45 -0400127 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");
tsepez0e606b52016-11-18 16:22:41 -0800157 if (!pAPDict)
158 pAPDict = m_pAnnotDict->SetNewFor<CPDF_Dictionary>("AP");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 // to avoid checkbox and radiobutton
dsinclair38fd8442016-09-15 10:15:32 -0700161 CPDF_Object* pObject = pAPDict->GetObjectFor(m_sAPType);
Dan Sinclairf1251c12015-10-20 16:24:45 -0400162 if (ToDictionary(pObject))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164
dsinclair38fd8442016-09-15 10:15:32 -0700165 CPDF_Stream* pStream = pAPDict->GetStreamFor(m_sAPType);
Lei Zhang412e9082015-12-14 18:34:00 -0800166 if (!pStream) {
tsepez70c4afd2016-11-15 11:33:44 -0800167 pStream = m_pDocument->NewIndirect<CPDF_Stream>();
tsepez0e606b52016-11-18 16:22:41 -0800168 pAPDict->SetNewFor<CPDF_Reference>(m_sAPType, m_pDocument,
169 pStream->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 CPDF_Dictionary* pStreamDict = pStream->GetDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173 if (!pStreamDict) {
tsepez698c5712016-09-28 16:47:07 -0700174 pStreamDict = new CPDF_Dictionary(m_pDocument->GetByteStringPool());
thestig1cd352e2016-06-07 17:53:06 -0700175 pStream->InitStream(nullptr, 0, pStreamDict);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 }
177
178 if (pStreamDict) {
dsinclair38fd8442016-09-15 10:15:32 -0700179 CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources");
tsepez0e606b52016-11-18 16:22:41 -0800180 if (!pStreamResList)
181 pStreamResList = pStreamDict->SetNewFor<CPDF_Dictionary>("Resources");
tsepezbb577af2016-09-21 19:10:19 -0700182 CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font");
183 if (!pStreamResFontList) {
tsepez5913a6c2016-11-16 17:31:18 -0800184 pStreamResFontList = m_pDocument->NewIndirect<CPDF_Dictionary>();
tsepez0e606b52016-11-18 16:22:41 -0800185 pStreamResList->SetNewFor<CPDF_Reference>(
186 "Font", m_pDocument, pStreamResFontList->GetObjNum());
tsepezbb577af2016-09-21 19:10:19 -0700187 }
188 if (!pStreamResFontList->KeyExist(sAlias)) {
tsepez0e606b52016-11-18 16:22:41 -0800189 pStreamResFontList->SetNewFor<CPDF_Reference>(
190 sAlias, m_pDocument, pFont->GetFontDict()->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 }
192 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193}
194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195CPDF_Font* CBA_FontMap::GetAnnotDefaultFont(CFX_ByteString& sAlias) {
thestig1cd352e2016-06-07 17:53:06 -0700196 CPDF_Dictionary* pAcroFormDict = nullptr;
dsinclair38fd8442016-09-15 10:15:32 -0700197 const bool bWidget = (m_pAnnotDict->GetStringFor("Subtype") == "Widget");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 if (bWidget) {
199 if (CPDF_Dictionary* pRootDict = m_pDocument->GetRoot())
dsinclair38fd8442016-09-15 10:15:32 -0700200 pAcroFormDict = pRootDict->GetDictFor("AcroForm");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 }
202
203 CFX_ByteString sDA;
Wei Lid4e8f122016-03-21 11:20:44 -0700204 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pAnnotDict, "DA");
205 if (pObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 sDA = pObj->GetString();
207
208 if (bWidget) {
209 if (sDA.IsEmpty()) {
210 pObj = FPDF_GetFieldAttr(pAcroFormDict, "DA");
211 sDA = pObj ? pObj->GetString() : CFX_ByteString();
212 }
213 }
npm860a2d02016-09-12 15:02:28 -0700214 if (sDA.IsEmpty())
215 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216
npm860a2d02016-09-12 15:02:28 -0700217 CPDF_SimpleParser syntax(sDA.AsStringC());
218 syntax.FindTagParamFromStart("Tf", 2);
219 CFX_ByteString sFontName(syntax.GetWord());
220 sAlias = PDF_NameDecode(sFontName).Mid(1);
thestig1cd352e2016-06-07 17:53:06 -0700221 CPDF_Dictionary* pFontDict = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222
dsinclair38fd8442016-09-15 10:15:32 -0700223 if (CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP")) {
224 if (CPDF_Dictionary* pNormalDict = pAPDict->GetDictFor("N")) {
npm860a2d02016-09-12 15:02:28 -0700225 if (CPDF_Dictionary* pNormalResDict =
dsinclair38fd8442016-09-15 10:15:32 -0700226 pNormalDict->GetDictFor("Resources")) {
227 if (CPDF_Dictionary* pResFontDict = pNormalResDict->GetDictFor("Font"))
228 pFontDict = pResFontDict->GetDictFor(sAlias);
npm860a2d02016-09-12 15:02:28 -0700229 }
230 }
231 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232
npm860a2d02016-09-12 15:02:28 -0700233 if (bWidget && !pFontDict && pAcroFormDict) {
dsinclair38fd8442016-09-15 10:15:32 -0700234 if (CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR")) {
235 if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDictFor("Font"))
236 pFontDict = pDRFontDict->GetDictFor(sAlias);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 }
238 }
239
240 return pFontDict ? m_pDocument->LoadFont(pFontDict) : nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241}
242
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243void CBA_FontMap::SetAPType(const CFX_ByteString& sAPType) {
244 m_sAPType = sAPType;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700245
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 Reset();
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800247 Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700248}