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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../include/formfiller/FormFiller.h"
8 #include "../../include/formfiller/FFL_CBA_Fontmap.h"
10 CBA_FontMap::CBA_FontMap(CPDFSDK_Annot* pAnnot, IFX_SystemHandler* pSystemHandler) :
11 CPWL_FontMap(pSystemHandler),
17 ASSERT(pAnnot != NULL);
19 CPDF_Page* pPage = pAnnot->GetPDFPage();
21 m_pDocument = pPage->m_pDocument;
22 m_pAnnotDict = pAnnot->GetPDFAnnot()->m_pAnnotDict;
25 CBA_FontMap::CBA_FontMap(CPDF_Document* pDocument, CPDF_Dictionary* pAnnotDict,
26 IFX_SystemHandler* pSystemHandler) :
27 CPWL_FontMap(pSystemHandler),
28 m_pDocument(pDocument),
29 m_pAnnotDict(pAnnotDict),
35 CBA_FontMap::~CBA_FontMap()
39 void CBA_FontMap::Reset()
42 m_pDefaultFont = NULL;
43 m_sDefaultFontName = "";
46 void CBA_FontMap::Initial(FX_LPCSTR fontname)
48 FX_INT32 nCharset = DEFAULT_CHARSET;
52 m_pDefaultFont = GetAnnotDefaultFont(m_sDefaultFontName);
55 if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont())
56 nCharset = pSubstFont->m_Charset;
59 if (m_sDefaultFontName == "Wingdings" || m_sDefaultFontName == "Wingdings2" ||
60 m_sDefaultFontName == "Wingdings3" || m_sDefaultFontName == "Webdings")
61 nCharset = SYMBOL_CHARSET;
63 nCharset = ANSI_CHARSET;
65 AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);
66 AddFontToAnnotDict(m_pDefaultFont, m_sDefaultFontName);
70 if (nCharset != ANSI_CHARSET)
71 CPWL_FontMap::Initial(fontname);
74 void CBA_FontMap::SetDefaultFont(CPDF_Font * pFont, const CFX_ByteString & sFontName)
76 ASSERT(pFont != NULL);
78 if (m_pDefaultFont) return;
80 m_pDefaultFont = pFont;
81 m_sDefaultFontName = sFontName;
83 // if (m_sDefaultFontName.IsEmpty())
84 // m_sDefaultFontName = pFont->GetFontTypeName();
86 FX_INT32 nCharset = DEFAULT_CHARSET;
87 if (const CFX_SubstFont* pSubstFont = m_pDefaultFont->GetSubstFont())
88 nCharset = pSubstFont->m_Charset;
89 AddFontData(m_pDefaultFont, m_sDefaultFontName, nCharset);
92 CPDF_Font* CBA_FontMap::FindFontSameCharset(CFX_ByteString& sFontAlias, FX_INT32 nCharset)
94 ASSERT(m_pAnnotDict != NULL);
96 if (m_pAnnotDict->GetString("Subtype") == "Widget")
98 CPDF_Document* pDocument = GetDocument();
99 ASSERT(pDocument != NULL);
101 CPDF_Dictionary * pRootDict = pDocument->GetRoot();
102 if (!pRootDict) return NULL;
104 CPDF_Dictionary* pAcroFormDict = pRootDict->GetDict("AcroForm");
105 if (!pAcroFormDict) return NULL;
107 CPDF_Dictionary * pDRDict = pAcroFormDict->GetDict("DR");
108 if (!pDRDict) return NULL;
110 return FindResFontSameCharset(pDRDict, sFontAlias, nCharset);
116 CPDF_Document* CBA_FontMap::GetDocument()
121 CPDF_Font* CBA_FontMap::FindResFontSameCharset(CPDF_Dictionary* pResDict, CFX_ByteString& sFontAlias,
124 if (!pResDict) return NULL;
126 CPDF_Document* pDocument = GetDocument();
127 ASSERT(pDocument != NULL);
129 CPDF_Dictionary* pFonts = pResDict->GetDict("Font");
130 if (pFonts == NULL) return NULL;
132 CPDF_Font* pFind = NULL;
134 FX_POSITION pos = pFonts->GetStartPos();
137 CPDF_Object* pObj = NULL;
138 CFX_ByteString csKey;
139 pObj = pFonts->GetNextElement(pos, csKey);
140 if (pObj == NULL) continue;
142 CPDF_Object* pDirect = pObj->GetDirect();
143 if (pDirect == NULL || pDirect->GetType() != PDFOBJ_DICTIONARY) continue;
145 CPDF_Dictionary* pElement = (CPDF_Dictionary*)pDirect;
146 if (pElement->GetString("Type") != "Font") continue;
148 CPDF_Font* pFont = pDocument->LoadFont(pElement);
149 if (pFont == NULL) continue;
150 const CFX_SubstFont* pSubst = pFont->GetSubstFont();
151 if (pSubst == NULL) continue;
152 if (pSubst->m_Charset == nCharset)
161 void CBA_FontMap::AddedFont(CPDF_Font* pFont, const CFX_ByteString& sFontAlias)
163 AddFontToAnnotDict(pFont, sFontAlias);
166 void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont, const CFX_ByteString& sAlias)
170 ASSERT(m_pAnnotDict != NULL);
171 ASSERT(m_pDocument != NULL);
173 CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDict("AP");
177 pAPDict = FX_NEW CPDF_Dictionary;
178 m_pAnnotDict->SetAt("AP", pAPDict);
181 //to avoid checkbox and radiobutton
182 CPDF_Object* pObject = pAPDict->GetElement(m_sAPType);
183 if (pObject && pObject->GetType() == PDFOBJ_DICTIONARY)
186 CPDF_Stream* pStream = pAPDict->GetStream(m_sAPType);
189 pStream = FX_NEW CPDF_Stream(NULL, 0, NULL);
190 FX_INT32 objnum = m_pDocument->AddIndirectObject(pStream);
191 pAPDict->SetAtReference(m_sAPType, m_pDocument, objnum);
194 CPDF_Dictionary * pStreamDict = pStream->GetDict();
198 pStreamDict = FX_NEW CPDF_Dictionary;
199 pStream->InitStream(NULL, 0, pStreamDict);
204 CPDF_Dictionary* pStreamResList = pStreamDict->GetDict("Resources");
207 pStreamResList = FX_NEW CPDF_Dictionary();
208 pStreamDict->SetAt("Resources", pStreamResList);
213 CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDict("Font");
214 if (!pStreamResFontList)
216 pStreamResFontList = FX_NEW CPDF_Dictionary;
217 FX_INT32 objnum = m_pDocument->AddIndirectObject(pStreamResFontList);
218 pStreamResList->SetAtReference("Font", m_pDocument, objnum);
220 if (!pStreamResFontList->KeyExist(sAlias))
221 pStreamResFontList->SetAtReference(sAlias, m_pDocument, pFont->GetFontDict());
226 CPDF_Font* CBA_FontMap::GetAnnotDefaultFont(CFX_ByteString &sAlias)
228 ASSERT(m_pAnnotDict != NULL);
229 ASSERT(m_pDocument != NULL);
231 CPDF_Dictionary* pAcroFormDict = NULL;
233 FX_BOOL bWidget = (m_pAnnotDict->GetString("Subtype") == "Widget");
237 if (CPDF_Dictionary * pRootDict = m_pDocument->GetRoot())
238 pAcroFormDict = pRootDict->GetDict("AcroForm");
243 if ((pObj = FPDF_GetFieldAttr(m_pAnnotDict, "DA")))
244 sDA = pObj->GetString();
250 pObj = FPDF_GetFieldAttr(pAcroFormDict, "DA");
251 sDA = pObj ? pObj->GetString() : CFX_ByteString();
255 CPDF_Dictionary * pFontDict = NULL;
259 CPDF_SimpleParser syntax(sDA);
260 syntax.FindTagParam("Tf", 2);
261 CFX_ByteString sFontName = syntax.GetWord();
262 sAlias = PDF_NameDecode(sFontName).Mid(1);
264 if (CPDF_Dictionary * pDRDict = m_pAnnotDict->GetDict("DR"))
265 if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDict("Font"))
266 pFontDict = pDRFontDict->GetDict(sAlias);
269 if (CPDF_Dictionary* pAPDict = m_pAnnotDict->GetDict("AP"))
270 if (CPDF_Dictionary* pNormalDict = pAPDict->GetDict("N"))
271 if (CPDF_Dictionary* pNormalResDict = pNormalDict->GetDict("Resources"))
272 if (CPDF_Dictionary* pResFontDict = pNormalResDict->GetDict("Font"))
273 pFontDict = pResFontDict->GetDict(sAlias);
281 if (CPDF_Dictionary * pDRDict = pAcroFormDict->GetDict("DR"))
282 if (CPDF_Dictionary* pDRFontDict = pDRDict->GetDict("Font"))
283 pFontDict = pDRFontDict->GetDict(sAlias);
290 return m_pDocument->LoadFont(pFontDict);
295 void CBA_FontMap::SetAPType(const CFX_ByteString& sAPType)