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 22CBA_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.Get(); 32 m_pAnnotDict = pAnnot->GetPDFAnnot()->GetAnnotDict(); 33 Initialize(); 34} 35 36CBA_FontMap::~CBA_FontMap() {} 37 38void CBA_FontMap::Reset() { 39 Empty(); 40 m_pDefaultFont = nullptr; 41 m_sDefaultFontName.clear(); 42} 43 44void CBA_FontMap::Initialize() { 45 int32_t nCharset = FX_CHARSET_Default; 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 = FX_CHARSET_Symbol; 58 else 59 nCharset = FX_CHARSET_ANSI; 60 } 61 AddFontData(m_pDefaultFont.Get(), m_sDefaultFontName, nCharset); 62 AddFontToAnnotDict(m_pDefaultFont.Get(), m_sDefaultFontName); 63 } 64 } 65 66 if (nCharset != FX_CHARSET_ANSI) 67 CPWL_FontMap::Initialize(); 68} 69 70void CBA_FontMap::SetDefaultFont(CPDF_Font* pFont, 71 const 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 = FX_CHARSET_Default; 81 if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont()) 82 nCharset = pSubstFont->m_Charset; 83 AddFontData(m_pDefaultFont.Get(), m_sDefaultFontName, nCharset); 84} 85 86CPDF_Font* CBA_FontMap::FindFontSameCharset(ByteString* sFontAlias, 87 int32_t nCharset) { 88 if (m_pAnnotDict->GetStringFor("Subtype") != "Widget") 89 return nullptr; 90 91 CPDF_Document* pDocument = GetDocument(); 92 const 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 107CPDF_Document* CBA_FontMap::GetDocument() { 108 return m_pDocument.Get(); 109} 110 111CPDF_Font* CBA_FontMap::FindResFontSameCharset(CPDF_Dictionary* pResDict, 112 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 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 148void CBA_FontMap::AddedFont(CPDF_Font* pFont, const ByteString& sFontAlias) { 149 AddFontToAnnotDict(pFont, sFontAlias); 150} 151 152void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont, 153 const ByteString& sAlias) { 154 if (!pFont) 155 return; 156 157 CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP"); 158 if (!pAPDict) 159 pAPDict = m_pAnnotDict->SetNewFor<CPDF_Dictionary>("AP"); 160 161 // to avoid checkbox and radiobutton 162 CPDF_Object* pObject = pAPDict->GetObjectFor(m_sAPType); 163 if (ToDictionary(pObject)) 164 return; 165 166 CPDF_Stream* pStream = pAPDict->GetStreamFor(m_sAPType); 167 if (!pStream) { 168 pStream = m_pDocument->NewIndirect<CPDF_Stream>(); 169 pAPDict->SetNewFor<CPDF_Reference>(m_sAPType, m_pDocument.Get(), 170 pStream->GetObjNum()); 171 } 172 173 CPDF_Dictionary* pStreamDict = pStream->GetDict(); 174 if (!pStreamDict) { 175 auto pOwnedDict = 176 pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool()); 177 pStreamDict = pOwnedDict.get(); 178 pStream->InitStream(nullptr, 0, std::move(pOwnedDict)); 179 } 180 181 CPDF_Dictionary* pStreamResList = pStreamDict->GetDictFor("Resources"); 182 if (!pStreamResList) 183 pStreamResList = pStreamDict->SetNewFor<CPDF_Dictionary>("Resources"); 184 CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font"); 185 if (!pStreamResFontList) { 186 pStreamResFontList = m_pDocument->NewIndirect<CPDF_Dictionary>(); 187 pStreamResList->SetNewFor<CPDF_Reference>("Font", m_pDocument.Get(), 188 pStreamResFontList->GetObjNum()); 189 } 190 if (!pStreamResFontList->KeyExist(sAlias)) { 191 pStreamResFontList->SetNewFor<CPDF_Reference>( 192 sAlias, m_pDocument.Get(), pFont->GetFontDict()->GetObjNum()); 193 } 194} 195 196CPDF_Font* CBA_FontMap::GetAnnotDefaultFont(ByteString* sAlias) { 197 CPDF_Dictionary* pAcroFormDict = nullptr; 198 const bool bWidget = (m_pAnnotDict->GetStringFor("Subtype") == "Widget"); 199 if (bWidget) { 200 const CPDF_Dictionary* pRootDict = m_pDocument->GetRoot(); 201 if (pRootDict) 202 pAcroFormDict = pRootDict->GetDictFor("AcroForm"); 203 } 204 205 ByteString sDA; 206 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pAnnotDict.Get(), "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() : ByteString(); 214 } 215 } 216 if (sDA.IsEmpty()) 217 return nullptr; 218 219 CPDF_SimpleParser syntax(sDA.AsStringView()); 220 syntax.FindTagParamFromStart("Tf", 2); 221 222 ByteString sFontName(syntax.GetWord()); 223 ByteString sDecodedFontName = PDF_NameDecode(sFontName); 224 *sAlias = sDecodedFontName.Right(sDecodedFontName.GetLength() - 1); 225 226 CPDF_Dictionary* pFontDict = nullptr; 227 if (CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDictFor("AP")) { 228 if (CPDF_Dictionary* pNormalDict = pAPDict->GetDictFor("N")) { 229 if (CPDF_Dictionary* pNormalResDict = 230 pNormalDict->GetDictFor("Resources")) { 231 if (CPDF_Dictionary* pResFontDict = pNormalResDict->GetDictFor("Font")) 232 pFontDict = pResFontDict->GetDictFor(*sAlias); 233 } 234 } 235 } 236 if (bWidget && !pFontDict && pAcroFormDict) { 237 if (CPDF_Dictionary* pDRDict = pAcroFormDict->GetDictFor("DR")) { 238 if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDictFor("Font")) 239 pFontDict = pDRFontDict->GetDictFor(*sAlias); 240 } 241 } 242 return pFontDict ? m_pDocument->LoadFont(pFontDict) : nullptr; 243} 244 245void CBA_FontMap::SetAPType(const ByteString& sAPType) { 246 m_sAPType = sAPType; 247 248 Reset(); 249 Initialize(); 250} 251