Adjust null object check
[pdfium.git] / core / src / fpdfdoc / doc_ap.cpp
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 "../../include/fpdfdoc/fpdf_doc.h"
8 #include "../../include/fpdfdoc/fpdf_vt.h"
9 #include "pdf_vt.h"
10 #include "../../include/fpdfdoc/fpdf_ap.h"
11 FX_BOOL FPDF_GenerateAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict)
12 {
13     if (!pAnnotDict || pAnnotDict->GetConstString("Subtype") != FX_BSTRC("Widget")) {
14         return FALSE;
15     }
16     CFX_ByteString field_type = FPDF_GetFieldAttr(pAnnotDict, "FT")->GetString();
17     FX_DWORD flags = FPDF_GetFieldAttr(pAnnotDict, "Ff")? FPDF_GetFieldAttr(pAnnotDict, "Ff")->GetInteger() : 0;
18     if (field_type == "Tx") {
19         return CPVT_GenerateAP::GenerateTextFieldAP(pDoc, pAnnotDict);
20     } else if (field_type == "Ch") {
21         if (flags & (1 << 17)) {
22             return CPVT_GenerateAP::GenerateComboBoxAP(pDoc, pAnnotDict);
23         } else {
24             return CPVT_GenerateAP::GenerateListBoxAP(pDoc, pAnnotDict);
25         }
26     } else if (field_type == "Btn") {
27         if (!(flags & (1 << 16))) {
28             if (!pAnnotDict->KeyExist("AS")) {
29                 if (CPDF_Dictionary* pParentDict = pAnnotDict->GetDict("Parent")) {
30                     if (pParentDict->KeyExist("AS")) {
31                         pAnnotDict->SetAtString("AS", pParentDict->GetString("AS"));
32                     }
33                 }
34             }
35         }
36     }
37     return FALSE;
38 }
39 class CPVT_FontMap : public IPVT_FontMap
40 {
41 public:
42     CPVT_FontMap(CPDF_Document * pDoc, CPDF_Dictionary * pResDict, CPDF_Font * pDefFont,
43                  const CFX_ByteString & sDefFontAlias);
44     virtual ~CPVT_FontMap();
45     CPDF_Font*                                          GetPDFFont(FX_INT32 nFontIndex);
46     CFX_ByteString                                      GetPDFFontAlias(FX_INT32 nFontIndex);
47     static void                                         GetAnnotSysPDFFont(CPDF_Document * pDoc, CPDF_Dictionary * pResDict,
48             CPDF_Font * & pSysFont, CFX_ByteString & sSysFontAlias);
49 private:
50     CPDF_Document*                                      m_pDocument;
51     CPDF_Dictionary*                            m_pResDict;
52     CPDF_Font*                                          m_pDefFont;
53     CFX_ByteString                                      m_sDefFontAlias;
54     CPDF_Font*                                          m_pSysFont;
55     CFX_ByteString                                      m_sSysFontAlias;
56 };
57 CPVT_FontMap::CPVT_FontMap(CPDF_Document * pDoc, CPDF_Dictionary * pResDict, CPDF_Font * pDefFont,
58                            const CFX_ByteString & sDefFontAlias) :
59     m_pDocument(pDoc),
60     m_pResDict(pResDict),
61     m_pDefFont(pDefFont),
62     m_sDefFontAlias(sDefFontAlias),
63     m_pSysFont(NULL),
64     m_sSysFontAlias()
65 {
66 }
67 CPVT_FontMap::~CPVT_FontMap()
68 {
69 }
70 extern CPDF_Font*               AddNativeInterFormFont(CPDF_Dictionary*& pFormDict, CPDF_Document* pDocument, CFX_ByteString& csNameTag);
71 void CPVT_FontMap::GetAnnotSysPDFFont(CPDF_Document * pDoc, CPDF_Dictionary * pResDict,
72                                       CPDF_Font * & pSysFont, CFX_ByteString & sSysFontAlias)
73 {
74     if (pDoc && pResDict) {
75         CFX_ByteString sFontAlias;
76         CPDF_Dictionary* pFormDict = pDoc->GetRoot()->GetDict("AcroForm");
77         if (CPDF_Font * pPDFFont = AddNativeInterFormFont(pFormDict, pDoc, sSysFontAlias)) {
78             if (CPDF_Dictionary * pFontList = pResDict->GetDict("Font")) {
79                 if (!pFontList->KeyExist(sSysFontAlias)) {
80                     pFontList->SetAtReference(sSysFontAlias, pDoc, pPDFFont->GetFontDict());
81                 }
82             }
83             pSysFont = pPDFFont;
84         }
85     }
86 }
87 CPDF_Font* CPVT_FontMap::GetPDFFont(FX_INT32 nFontIndex)
88 {
89     switch (nFontIndex) {
90         case 0:
91             return m_pDefFont;
92         case 1:
93             if (!m_pSysFont) {
94                 GetAnnotSysPDFFont(m_pDocument, m_pResDict, m_pSysFont, m_sSysFontAlias);
95             }
96             return m_pSysFont;
97     }
98     return NULL;
99 }
100 CFX_ByteString CPVT_FontMap::GetPDFFontAlias(FX_INT32 nFontIndex)
101 {
102     switch (nFontIndex) {
103         case 0:
104             return m_sDefFontAlias;
105         case 1:
106             if (!m_pSysFont) {
107                 GetAnnotSysPDFFont(m_pDocument, m_pResDict, m_pSysFont, m_sSysFontAlias);
108             }
109             return m_sSysFontAlias;
110     }
111     return "";
112 }
113 CPVT_Provider::CPVT_Provider(IPVT_FontMap * pFontMap) : m_pFontMap(pFontMap)
114 {
115     ASSERT (m_pFontMap != NULL);
116 }
117 CPVT_Provider::~CPVT_Provider()
118 {
119 }
120 FX_INT32 CPVT_Provider::GetCharWidth(FX_INT32 nFontIndex, FX_WORD word, FX_INT32 nWordStyle)
121 {
122     if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) {
123         FX_DWORD charcode = pPDFFont->CharCodeFromUnicode(word);
124         if (charcode != -1) {
125             return pPDFFont->GetCharWidthF(charcode);
126         }
127     }
128     return 0;
129 }
130 FX_INT32 CPVT_Provider::GetTypeAscent(FX_INT32 nFontIndex)
131 {
132     if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) {
133         return pPDFFont->GetTypeAscent();
134     }
135     return 0;
136 }
137 FX_INT32 CPVT_Provider::GetTypeDescent(FX_INT32 nFontIndex)
138 {
139     if (CPDF_Font* pPDFFont = m_pFontMap->GetPDFFont(nFontIndex)) {
140         return pPDFFont->GetTypeDescent();
141     }
142     return 0;
143 }
144 FX_INT32 CPVT_Provider::GetWordFontIndex(FX_WORD word, FX_INT32 charset, FX_INT32 nFontIndex)
145 {
146     if (CPDF_Font* pDefFont = m_pFontMap->GetPDFFont(0)) {
147         if (pDefFont->CharCodeFromUnicode(word) != -1) {
148             return 0;
149         }
150     }
151     if (CPDF_Font* pSysFont = m_pFontMap->GetPDFFont(1))
152         if (pSysFont->CharCodeFromUnicode(word) != -1) {
153             return 1;
154         }
155     return -1;
156 }
157 FX_BOOL CPVT_Provider::IsLatinWord(FX_WORD word)
158 {
159     if ((word >= 0x61 && word <= 0x7A) || (word >= 0x41 && word <= 0x5A) || word == 0x2D || word == 0x27) {
160         return TRUE;
161     }
162     return FALSE;
163 }
164 FX_INT32 CPVT_Provider::GetDefaultFontIndex()
165 {
166     return 0;
167 }
168 static CFX_ByteString GetPDFWordString(IPVT_FontMap * pFontMap, FX_INT32 nFontIndex, FX_WORD Word, FX_WORD SubWord)
169 {
170     CFX_ByteString sWord;
171     if (SubWord > 0) {
172         sWord.Format("%c", SubWord);
173     } else {
174         if (pFontMap) {
175             if (CPDF_Font * pPDFFont = pFontMap->GetPDFFont(nFontIndex)) {
176                 if (pPDFFont->GetBaseFont().Compare("Symbol") == 0 || pPDFFont->GetBaseFont().Compare("ZapfDingbats") == 0) {
177                     sWord.Format("%c", Word);
178                 } else {
179                     FX_DWORD dwCharCode = pPDFFont->CharCodeFromUnicode(Word);
180                     if (dwCharCode != -1) {
181                         pPDFFont->AppendChar(sWord, dwCharCode);
182                     }
183                 }
184             }
185         }
186     }
187     return sWord;
188 }
189 static CFX_ByteString GetWordRenderString(const CFX_ByteString & strWords)
190 {
191     if (strWords.GetLength() > 0) {
192         return PDF_EncodeString(strWords) + " Tj\n";
193     }
194     return "";
195 }
196 static CFX_ByteString GetFontSetString(IPVT_FontMap * pFontMap, FX_INT32 nFontIndex, FX_FLOAT fFontSize)
197 {
198     CFX_ByteTextBuf sRet;
199     if (pFontMap) {
200         CFX_ByteString sFontAlias = pFontMap->GetPDFFontAlias(nFontIndex);
201         if (sFontAlias.GetLength() > 0 && fFontSize > 0 ) {
202             sRet << "/" << sFontAlias << " " << fFontSize << " Tf\n";
203         }
204     }
205     return sRet.GetByteString();
206 }
207 static CPVT_Color ParseColor(const CFX_ByteString & str)
208 {
209     CPDF_SimpleParser syntax(str);
210     syntax.SetPos(0);
211     if (syntax.FindTagParam("g", 1)) {
212         return CPVT_Color(CT_GRAY, FX_atof(syntax.GetWord()));
213     }
214     syntax.SetPos(0);
215     if (syntax.FindTagParam("rg", 3)) {
216         FX_FLOAT f1 = FX_atof(syntax.GetWord());
217         FX_FLOAT f2 = FX_atof(syntax.GetWord());
218         FX_FLOAT f3 = FX_atof(syntax.GetWord());
219         return CPVT_Color(CT_RGB, f1, f2, f3);
220     }
221     syntax.SetPos(0);
222     if (syntax.FindTagParam("k", 4)) {
223         FX_FLOAT f1 = FX_atof(syntax.GetWord());
224         FX_FLOAT f2 = FX_atof(syntax.GetWord());
225         FX_FLOAT f3 = FX_atof(syntax.GetWord());
226         FX_FLOAT f4 = FX_atof(syntax.GetWord());
227         return CPVT_Color(CT_CMYK, f1, f2, f3, f4);
228     }
229     return CPVT_Color(CT_TRANSPARENT);
230 }
231 static CPVT_Color ParseColor(const CPDF_Array & array)
232 {
233     CPVT_Color rt;
234     switch (array.GetCount()) {
235         case 1:
236             rt = CPVT_Color(CT_GRAY, array.GetFloat(0));
237             break;
238         case 3:
239             rt = CPVT_Color(CT_RGB, array.GetFloat(0), array.GetFloat(1), array.GetFloat(2));
240             break;
241         case 4:
242             rt = CPVT_Color(CT_CMYK, array.GetFloat(0), array.GetFloat(1), array.GetFloat(2), array.GetFloat(3));
243             break;
244     }
245     return rt;
246 }
247 static FX_BOOL GenerateWidgetAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict, const FX_INT32 & nWidgetType)
248 {
249     CPDF_Dictionary* pFormDict = NULL;
250     if (CPDF_Dictionary * pRootDict = pDoc->GetRoot()) {
251         pFormDict = pRootDict->GetDict("AcroForm");
252     }
253     if (!pFormDict) {
254         return FALSE;
255     }
256     CFX_ByteString DA;
257     if (CPDF_Object* pDAObj = FPDF_GetFieldAttr(pAnnotDict, "DA")) {
258         DA = pDAObj->GetString();
259     }
260     if (DA.IsEmpty()) {
261         DA = pFormDict->GetString("DA");
262     }
263     if (DA.IsEmpty()) {
264         return FALSE;
265     }
266     CPDF_SimpleParser syntax(DA);
267     syntax.FindTagParam("Tf", 2);
268     CFX_ByteString sFontName = syntax.GetWord();
269     sFontName = PDF_NameDecode(sFontName);
270     if (sFontName.IsEmpty()) {
271         return FALSE;
272     }
273     FX_FLOAT fFontSize = FX_atof(syntax.GetWord());
274     CPVT_Color crText = ParseColor(DA);
275     FX_BOOL bUseFormRes = FALSE;
276     CPDF_Dictionary * pFontDict = NULL;
277     CPDF_Dictionary* pDRDict = pAnnotDict->GetDict(FX_BSTRC("DR"));
278     if (pDRDict == NULL) {
279         pDRDict = pFormDict->GetDict(FX_BSTRC("DR"));
280         bUseFormRes = TRUE;
281     }
282     CPDF_Dictionary * pDRFontDict = NULL;
283     if (pDRDict && (pDRFontDict = pDRDict->GetDict("Font"))) {
284         pFontDict = pDRFontDict->GetDict(sFontName.Mid(1));
285         if (!pFontDict && !bUseFormRes) {
286             pDRDict = pFormDict->GetDict(FX_BSTRC("DR"));
287             pDRFontDict = pDRDict->GetDict("Font");
288             if (pDRFontDict) {
289                 pFontDict = pDRFontDict->GetDict(sFontName.Mid(1));
290             }
291         }
292     }
293     if (!pDRFontDict) {
294         return FALSE;
295     }
296     if (!pFontDict) {
297         pFontDict = CPDF_Dictionary::Create();
298         if (pFontDict == NULL) {
299             return FALSE;
300         }
301         pFontDict->SetAtName(FX_BSTRC("Type"), "Font");
302         pFontDict->SetAtName(FX_BSTRC("Subtype"), "Type1");
303         pFontDict->SetAtName(FX_BSTRC("BaseFont"), "Helvetica");
304         pFontDict->SetAtName(FX_BSTRC("Encoding"), "WinAnsiEncoding");
305         pDoc->AddIndirectObject(pFontDict);
306         pDRFontDict->SetAtReference(sFontName.Mid(1), pDoc, pFontDict);
307     }
308     CPDF_Font* pDefFont = pDoc->LoadFont(pFontDict);
309     if (!pDefFont) {
310         return FALSE;
311     }
312     CFX_CharMap* pCharMap = pDefFont->GetCharMap();
313     CPDF_Rect rcAnnot = pAnnotDict->GetRect("Rect");
314     FX_INT32 nRotate = 0;
315     if (CPDF_Dictionary * pMKDict = pAnnotDict->GetDict("MK")) {
316         nRotate = pMKDict->GetInteger("R");
317     }
318     CPDF_Rect rcBBox;
319     CPDF_Matrix matrix;
320     switch (nRotate % 360) {
321         case 0:
322             rcBBox = CPDF_Rect(0, 0, rcAnnot.right - rcAnnot.left, rcAnnot.top - rcAnnot.bottom);
323             break;
324         case 90:
325             matrix = CPDF_Matrix(0, 1, -1, 0, rcAnnot.right - rcAnnot.left, 0);
326             rcBBox = CPDF_Rect(0, 0, rcAnnot.top - rcAnnot.bottom, rcAnnot.right - rcAnnot.left);
327             break;
328         case 180:
329             matrix = CPDF_Matrix(-1, 0, 0, -1, rcAnnot.right - rcAnnot.left, rcAnnot.top - rcAnnot.bottom);
330             rcBBox = CPDF_Rect(0, 0, rcAnnot.right - rcAnnot.left, rcAnnot.top - rcAnnot.bottom);
331             break;
332         case 270:
333             matrix = CPDF_Matrix(0, -1, 1, 0, 0, rcAnnot.top - rcAnnot.bottom);
334             rcBBox = CPDF_Rect(0, 0, rcAnnot.top - rcAnnot.bottom, rcAnnot.right - rcAnnot.left);
335             break;
336     }
337     FX_INT32 nBorderStyle = PBS_SOLID;
338     FX_FLOAT fBorderWidth = 1;
339     CPVT_Dash dsBorder(3, 0, 0);
340     CPVT_Color crLeftTop, crRightBottom;
341     if (CPDF_Dictionary * pBSDict = pAnnotDict->GetDict("BS")) {
342         if (pBSDict->KeyExist("W")) {
343             fBorderWidth = pBSDict->GetNumber("W");
344         }
345         if (CPDF_Array * pArray = pBSDict->GetArray("D")) {
346             dsBorder = CPVT_Dash(pArray->GetInteger(0), pArray->GetInteger(1), pArray->GetInteger(2));
347         }
348         switch (pBSDict->GetString("S").GetAt(0)) {
349             case 'S':
350                 nBorderStyle = PBS_SOLID;
351                 break;
352             case 'D':
353                 nBorderStyle = PBS_DASH;
354                 break;
355             case 'B':
356                 nBorderStyle = PBS_BEVELED;
357                 fBorderWidth *= 2;
358                 crLeftTop = CPVT_Color(CT_GRAY, 1);
359                 crRightBottom = CPVT_Color(CT_GRAY, 0.5);
360                 break;
361             case 'I':
362                 nBorderStyle = PBS_INSET;
363                 fBorderWidth *= 2;
364                 crLeftTop = CPVT_Color(CT_GRAY, 0.5);
365                 crRightBottom = CPVT_Color(CT_GRAY, 0.75);
366                 break;
367             case 'U':
368                 nBorderStyle = PBS_UNDERLINED;
369                 break;
370         }
371     }
372     CPVT_Color crBorder, crBG;
373     if (CPDF_Dictionary * pMKDict = pAnnotDict->GetDict("MK")) {
374         if (CPDF_Array * pArray = pMKDict->GetArray("BC")) {
375             crBorder = ParseColor(*pArray);
376         }
377         if (CPDF_Array * pArray = pMKDict->GetArray("BG")) {
378             crBG = ParseColor(*pArray);
379         }
380     }
381     CFX_ByteTextBuf sAppStream;
382     CFX_ByteString sBG = CPVT_GenerateAP::GenerateColorAP(crBG, TRUE);
383     if (sBG.GetLength() > 0) {
384         sAppStream << "q\n" << sBG << rcBBox.left << " " << rcBBox.bottom << " "
385                    << rcBBox.Width() << " " << rcBBox.Height() << " re f\n" << "Q\n";
386     }
387     CFX_ByteString sBorderStream = CPVT_GenerateAP::GenerateBorderAP(rcBBox, fBorderWidth,
388                                    crBorder, crLeftTop, crRightBottom, nBorderStyle, dsBorder);
389     if (sBorderStream.GetLength() > 0) {
390         sAppStream << "q\n" << sBorderStream << "Q\n";
391     }
392     CPDF_Rect rcBody = CPDF_Rect(rcBBox.left + fBorderWidth, rcBBox.bottom + fBorderWidth,
393                                  rcBBox.right - fBorderWidth, rcBBox.top - fBorderWidth);
394     rcBody.Normalize();
395     CPDF_Dictionary* pAPDict = pAnnotDict->GetDict("AP");
396     if (pAPDict == NULL) {
397         pAPDict = CPDF_Dictionary::Create();
398         if (pAPDict == NULL) {
399             return FALSE;
400         }
401         pAnnotDict->SetAt("AP", pAPDict);
402     }
403     CPDF_Stream* pNormalStream = pAPDict->GetStream("N");
404     if (pNormalStream == NULL) {
405         pNormalStream = CPDF_Stream::Create(NULL, 0, NULL);
406         if (pNormalStream == NULL) {
407             return FALSE;
408         }
409         FX_INT32 objnum = pDoc->AddIndirectObject(pNormalStream);
410         pAnnotDict->GetDict("AP")->SetAtReference("N", pDoc, objnum);
411     }
412     CPDF_Dictionary * pStreamDict = pNormalStream->GetDict();
413     if (pStreamDict) {
414         pStreamDict->SetAtMatrix("Matrix", matrix);
415         pStreamDict->SetAtRect("BBox", rcBBox);
416         CPDF_Dictionary* pStreamResList = pStreamDict->GetDict("Resources");
417         if (pStreamResList) {
418             CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDict("Font");
419             if (!pStreamResFontList) {
420                 pStreamResFontList = CPDF_Dictionary::Create();
421                 if (pStreamResFontList == NULL) {
422                     return FALSE;
423                 }
424                 pStreamResList->SetAt("Font", pStreamResFontList);
425             }
426             if (!pStreamResFontList->KeyExist(sFontName)) {
427                 pStreamResFontList->SetAtReference(sFontName, pDoc, pFontDict);
428             }
429         } else {
430             pStreamDict->SetAt("Resources", pFormDict->GetDict("DR")->Clone());
431             pStreamResList = pStreamDict->GetDict("Resources");
432         }
433     }
434     switch (nWidgetType) {
435         case 0: {
436                 CFX_WideString swValue = FPDF_GetFieldAttr(pAnnotDict, "V")? FPDF_GetFieldAttr(pAnnotDict, "V")->GetUnicodeText() : CFX_WideString();
437                 FX_INT32 nAlign = FPDF_GetFieldAttr(pAnnotDict, "Q")? FPDF_GetFieldAttr(pAnnotDict, "Q")->GetInteger() : 0;
438                 FX_DWORD dwFlags = FPDF_GetFieldAttr(pAnnotDict, "Ff")? FPDF_GetFieldAttr(pAnnotDict, "Ff")->GetInteger() : 0;
439                 FX_DWORD dwMaxLen = FPDF_GetFieldAttr(pAnnotDict, "MaxLen") ? FPDF_GetFieldAttr(pAnnotDict, "MaxLen")->GetInteger() : 0;
440                 CPVT_FontMap map(pDoc, pStreamDict ? pStreamDict->GetDict("Resources") : NULL , pDefFont, sFontName.Right(sFontName.GetLength() - 1));
441                 CPVT_Provider prd(&map);
442                 CPDF_VariableText vt;
443                 vt.SetProvider(&prd);
444                 vt.SetPlateRect(rcBody);
445                 vt.SetAlignment(nAlign);
446                 if (IsFloatZero(fFontSize)) {
447                     vt.SetAutoFontSize(TRUE);
448                 } else {
449                     vt.SetFontSize(fFontSize);
450                 }
451                 FX_BOOL bMultiLine = (dwFlags >> 12) & 1;
452                 if (bMultiLine) {
453                     vt.SetMultiLine(TRUE);
454                     vt.SetAutoReturn(TRUE);
455                 }
456                 FX_WORD subWord = 0;
457                 if ((dwFlags >> 13) & 1) {
458                     subWord = '*';
459                     vt.SetPasswordChar(subWord);
460                 }
461                 FX_BOOL bCharArray = (dwFlags >> 24) & 1;
462                 if (bCharArray) {
463                     vt.SetCharArray(dwMaxLen);
464                 } else {
465                     vt.SetLimitChar(dwMaxLen);
466                 }
467                 vt.Initialize();
468                 vt.SetText(swValue);
469                 vt.RearrangeAll();
470                 CPDF_Rect rcContent = vt.GetContentRect();
471                 CPDF_Point ptOffset(0.0f, 0.0f);
472                 if (!bMultiLine) {
473                     ptOffset = CPDF_Point(0.0f, (rcContent.Height() - rcBody.Height()) / 2.0f);
474                 }
475                 CFX_ByteString sBody = CPVT_GenerateAP::GenerateEditAP(&map, vt.GetIterator(), ptOffset, !bCharArray, subWord);
476                 if (sBody.GetLength() > 0) {
477                     sAppStream << "/Tx BMC\n" << "q\n";
478                     if (rcContent.Width() > rcBody.Width() ||
479                             rcContent.Height() > rcBody.Height()) {
480                         sAppStream << rcBody.left << " " << rcBody.bottom << " "
481                                    << rcBody.Width() << " " << rcBody.Height() << " re\nW\nn\n";
482                     }
483                     sAppStream << "BT\n" << CPVT_GenerateAP::GenerateColorAP(crText, TRUE) << sBody << "ET\n" << "Q\nEMC\n";
484                 }
485             }
486             break;
487         case 1: {
488                 CFX_WideString swValue = FPDF_GetFieldAttr(pAnnotDict, "V") ? FPDF_GetFieldAttr(pAnnotDict, "V")->GetUnicodeText() : CFX_WideString();
489                 CPVT_FontMap map(pDoc, pStreamDict ? pStreamDict->GetDict("Resources"):NULL, pDefFont, sFontName.Right(sFontName.GetLength() - 1));
490                 CPVT_Provider prd(&map);
491                 CPDF_VariableText vt;
492                 vt.SetProvider(&prd);
493                 CPDF_Rect rcButton = rcBody;
494                 rcButton.left = rcButton.right - 13;
495                 rcButton.Normalize();
496                 CPDF_Rect rcEdit = rcBody;
497                 rcEdit.right = rcButton.left;
498                 rcEdit.Normalize();
499                 vt.SetPlateRect(rcEdit);
500                 if (IsFloatZero(fFontSize)) {
501                     vt.SetAutoFontSize(TRUE);
502                 } else {
503                     vt.SetFontSize(fFontSize);
504                 }
505                 vt.Initialize();
506                 vt.SetText(swValue);
507                 vt.RearrangeAll();
508                 CPDF_Rect rcContent = vt.GetContentRect();
509                 CPDF_Point ptOffset = CPDF_Point(0.0f, (rcContent.Height() - rcEdit.Height()) / 2.0f);
510                 CFX_ByteString sEdit = CPVT_GenerateAP::GenerateEditAP(&map, vt.GetIterator(), ptOffset, TRUE, 0);
511                 if (sEdit.GetLength() > 0) {
512                     sAppStream << "/Tx BMC\n" << "q\n";
513                     sAppStream << rcEdit.left << " " << rcEdit.bottom << " "
514                                << rcEdit.Width() << " " << rcEdit.Height() << " re\nW\nn\n";
515                     sAppStream << "BT\n" << CPVT_GenerateAP::GenerateColorAP(crText, TRUE) << sEdit << "ET\n" << "Q\nEMC\n";
516                 }
517                 CFX_ByteString sButton = CPVT_GenerateAP::GenerateColorAP(CPVT_Color(CT_RGB, 220.0f / 255.0f, 220.0f / 255.0f, 220.0f / 255.0f), TRUE);
518                 if (sButton.GetLength() > 0 && !rcButton.IsEmpty()) {
519                     sAppStream << "q\n" << sButton;
520                     sAppStream << rcButton.left << " " << rcButton.bottom << " "
521                                << rcButton.Width() << " " << rcButton.Height() << " re f\n";
522                     sAppStream << "Q\n";
523                     CFX_ByteString sButtonBorder = CPVT_GenerateAP::GenerateBorderAP(rcButton, 2, CPVT_Color(CT_GRAY, 0), CPVT_Color(CT_GRAY, 1), CPVT_Color(CT_GRAY, 0.5), PBS_BEVELED, CPVT_Dash(3, 0, 0));
524                     if (sButtonBorder.GetLength() > 0) {
525                         sAppStream << "q\n" << sButtonBorder << "Q\n";
526                     }
527                     CPDF_Point ptCenter = CPDF_Point((rcButton.left + rcButton.right) / 2, (rcButton.top + rcButton.bottom) / 2);
528                     if (IsFloatBigger(rcButton.Width(), 6) && IsFloatBigger(rcButton.Height(), 6)) {
529                         sAppStream << "q\n" << " 0 g\n";
530                         sAppStream << ptCenter.x - 3 << " " << ptCenter.y + 1.5f << " m\n";
531                         sAppStream << ptCenter.x + 3 << " " << ptCenter.y + 1.5f << " l\n";
532                         sAppStream << ptCenter.x << " " << ptCenter.y - 1.5f << " l\n";
533                         sAppStream << ptCenter.x - 3 << " " << ptCenter.y + 1.5f << " l f\n";
534                         sAppStream << sButton << "Q\n";
535                     }
536                 }
537             }
538             break;
539         case 2: {
540                 CPVT_FontMap map(pDoc, pStreamDict ? pStreamDict->GetDict("Resources"):NULL, pDefFont, sFontName.Right(sFontName.GetLength() - 1));
541                 CPVT_Provider prd(&map);
542                 CPDF_Array * pOpts = FPDF_GetFieldAttr(pAnnotDict, "Opt") ? FPDF_GetFieldAttr(pAnnotDict, "Opt")->GetArray() : NULL;
543                 CPDF_Array * pSels = FPDF_GetFieldAttr(pAnnotDict, "I") ? FPDF_GetFieldAttr(pAnnotDict, "I")->GetArray() : NULL;
544                 FX_INT32 nTop = FPDF_GetFieldAttr(pAnnotDict, "TI") ? FPDF_GetFieldAttr(pAnnotDict, "TI")->GetInteger() : 0;
545                 CFX_ByteTextBuf sBody;
546                 if (pOpts) {
547                     FX_FLOAT fy = rcBody.top;
548                     for (FX_INT32 i = nTop, sz = pOpts->GetCount(); i < sz; i++) {
549                         if (IsFloatSmaller(fy, rcBody.bottom)) {
550                             break;
551                         }
552                         if (CPDF_Object* pOpt = pOpts->GetElementValue(i)) {
553                             CFX_WideString swItem;
554                             if (pOpt->GetType() == PDFOBJ_STRING) {
555                                 swItem = pOpt->GetUnicodeText();
556                             } else if (pOpt->GetType() == PDFOBJ_ARRAY) {
557                                 swItem = ((CPDF_Array*)pOpt)->GetElementValue(1)->GetUnicodeText();
558                             }
559                             FX_BOOL bSelected = FALSE;
560                             if (pSels) {
561                                 for (FX_DWORD s = 0, ssz = pSels->GetCount(); s < ssz; s++) {
562                                     if (i == pSels->GetInteger(s)) {
563                                         bSelected = TRUE;
564                                         break;
565                                     }
566                                 }
567                             }
568                             CPDF_VariableText vt;
569                             vt.SetProvider(&prd);
570                             vt.SetPlateRect(CPDF_Rect(rcBody.left, 0.0f, rcBody.right, 0.0f));
571                             if (IsFloatZero(fFontSize)) {
572                                 vt.SetFontSize(12.0f);
573                             } else {
574                                 vt.SetFontSize(fFontSize);
575                             }
576                             vt.Initialize();
577                             vt.SetText(swItem);
578                             vt.RearrangeAll();
579                             FX_FLOAT fItemHeight = vt.GetContentRect().Height();
580                             if (bSelected) {
581                                 CPDF_Rect rcItem = CPDF_Rect(rcBody.left, fy - fItemHeight, rcBody.right, fy);
582                                 sBody << "q\n" << CPVT_GenerateAP::GenerateColorAP(CPVT_Color(CT_RGB, 0, 51.0f / 255.0f, 113.0f / 255.0f), TRUE)
583                                       << rcItem.left << " " << rcItem.bottom << " " << rcItem.Width() << " " << rcItem.Height() << " re f\n" << "Q\n";
584                                 sBody << "BT\n" << CPVT_GenerateAP::GenerateColorAP(CPVT_Color(CT_GRAY, 1), TRUE) << CPVT_GenerateAP::GenerateEditAP(&map, vt.GetIterator(), CPDF_Point(0.0f, fy), TRUE, 0) << "ET\n";
585                             } else {
586                                 sBody << "BT\n" << CPVT_GenerateAP::GenerateColorAP(crText, TRUE) << CPVT_GenerateAP::GenerateEditAP(&map, vt.GetIterator(), CPDF_Point(0.0f, fy), TRUE, 0) << "ET\n";
587                             }
588                             fy -= fItemHeight;
589                         }
590                     }
591                 }
592                 if (sBody.GetSize() > 0) {
593                     sAppStream << "/Tx BMC\n" << "q\n";
594                     sAppStream << rcBody.left << " " << rcBody.bottom << " "
595                                << rcBody.Width() << " " << rcBody.Height() << " re\nW\nn\n";
596                     sAppStream << sBody.GetByteString() << "Q\nEMC\n";
597                 }
598             }
599             break;
600     }
601     if (pNormalStream) {
602         pNormalStream->SetData((FX_BYTE*)sAppStream.GetBuffer(), sAppStream.GetSize(), FALSE, FALSE);
603         pStreamDict = pNormalStream->GetDict();
604         if (pStreamDict) {
605             pStreamDict->SetAtMatrix("Matrix", matrix);
606             pStreamDict->SetAtRect("BBox", rcBBox);
607             CPDF_Dictionary* pStreamResList = pStreamDict->GetDict("Resources");
608             if (pStreamResList) {
609                 CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDict("Font");
610                 if (!pStreamResFontList) {
611                     pStreamResFontList = CPDF_Dictionary::Create();
612                     if (pStreamResFontList == NULL) {
613                         return FALSE;
614                     }
615                     pStreamResList->SetAt("Font", pStreamResFontList);
616                 }
617                 if (!pStreamResFontList->KeyExist(sFontName)) {
618                     pStreamResFontList->SetAtReference(sFontName, pDoc, pFontDict);
619                 }
620             } else {
621                 pStreamDict->SetAt("Resources", pFormDict->GetDict("DR")->Clone());
622                 pStreamResList = pStreamDict->GetDict("Resources");
623             }
624         }
625     }
626     return TRUE;
627 }
628 FX_BOOL CPVT_GenerateAP::GenerateTextFieldAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict)
629 {
630     return GenerateWidgetAP(pDoc, pAnnotDict, 0);
631 }
632 FX_BOOL CPVT_GenerateAP::GenerateComboBoxAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict)
633 {
634     return GenerateWidgetAP(pDoc, pAnnotDict, 1);
635 }
636 FX_BOOL CPVT_GenerateAP::GenerateListBoxAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict)
637 {
638     return GenerateWidgetAP(pDoc, pAnnotDict, 2);
639 }
640 CFX_ByteString CPVT_GenerateAP::GenerateEditAP(IPVT_FontMap * pFontMap, IPDF_VariableText_Iterator* pIterator, const CPDF_Point & ptOffset, FX_BOOL bContinuous, FX_WORD SubWord, const CPVT_WordRange * pVisible)
641 {
642     CFX_ByteTextBuf sEditStream, sLineStream, sWords;
643     CPDF_Point ptOld(0.0f, 0.0f), ptNew(0.0f, 0.0f);
644     FX_INT32 nCurFontIndex = -1;
645     if (pIterator) {
646         if (pVisible) {
647             pIterator->SetAt(pVisible->BeginPos);
648         } else {
649             pIterator->SetAt(0);
650         }
651         CPVT_WordPlace oldplace;
652         while (pIterator->NextWord()) {
653             CPVT_WordPlace place = pIterator->GetAt();
654             if (pVisible && place.WordCmp(pVisible->EndPos) > 0) {
655                 break;
656             }
657             if (bContinuous) {
658                 if (place.LineCmp(oldplace) != 0) {
659                     if (sWords.GetSize() > 0) {
660                         sLineStream << GetWordRenderString(sWords.GetByteString());
661                         sEditStream << sLineStream;
662                         sLineStream.Clear();
663                         sWords.Clear();
664                     }
665                     CPVT_Word word;
666                     if (pIterator->GetWord(word)) {
667                         ptNew = CPDF_Point(word.ptWord.x + ptOffset.x, word.ptWord.y + ptOffset.y);
668                     } else {
669                         CPVT_Line line;
670                         pIterator->GetLine(line);
671                         ptNew = CPDF_Point(line.ptLine.x + ptOffset.x, line.ptLine.y + ptOffset.y);
672                     }
673                     if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) {
674                         sLineStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y << " Td\n";
675                         ptOld = ptNew;
676                     }
677                 }
678                 CPVT_Word word;
679                 if (pIterator->GetWord(word)) {
680                     if (word.nFontIndex != nCurFontIndex) {
681                         if (sWords.GetSize() > 0) {
682                             sLineStream << GetWordRenderString(sWords.GetByteString());
683                             sWords.Clear();
684                         }
685                         sLineStream << GetFontSetString(pFontMap, word.nFontIndex, word.fFontSize);
686                         nCurFontIndex = word.nFontIndex;
687                     }
688                     sWords << GetPDFWordString(pFontMap, nCurFontIndex, word.Word, SubWord);
689                 }
690                 oldplace = place;
691             } else {
692                 CPVT_Word word;
693                 if (pIterator->GetWord(word)) {
694                     ptNew = CPDF_Point(word.ptWord.x + ptOffset.x, word.ptWord.y + ptOffset.y);
695                     if (ptNew.x != ptOld.x || ptNew.y != ptOld.y) {
696                         sEditStream << ptNew.x - ptOld.x << " " << ptNew.y - ptOld.y << " Td\n";
697                         ptOld = ptNew;
698                     }
699                     if (word.nFontIndex != nCurFontIndex) {
700                         sEditStream << GetFontSetString(pFontMap, word.nFontIndex, word.fFontSize);
701                         nCurFontIndex = word.nFontIndex;
702                     }
703                     sEditStream << GetWordRenderString(GetPDFWordString(pFontMap, nCurFontIndex, word.Word, SubWord));
704                 }
705             }
706         }
707         if (sWords.GetSize() > 0) {
708             sLineStream << GetWordRenderString(sWords.GetByteString());
709             sEditStream << sLineStream;
710             sWords.Clear();
711         }
712     }
713     return sEditStream.GetByteString();
714 }
715 CFX_ByteString CPVT_GenerateAP::GenerateBorderAP(const CPDF_Rect & rect, FX_FLOAT fWidth,
716         const CPVT_Color & color, const CPVT_Color & crLeftTop, const CPVT_Color & crRightBottom,
717         FX_INT32 nStyle, const CPVT_Dash & dash)
718 {
719     CFX_ByteTextBuf sAppStream;
720     CFX_ByteString sColor;
721     FX_FLOAT fLeft = rect.left;
722     FX_FLOAT fRight = rect.right;
723     FX_FLOAT fTop = rect.top;
724     FX_FLOAT fBottom = rect.bottom;
725     if (fWidth > 0.0f) {
726         FX_FLOAT fHalfWidth = fWidth / 2.0f;
727         switch (nStyle) {
728             default:
729             case PBS_SOLID:
730                 sColor = GenerateColorAP(color, TRUE);
731                 if (sColor.GetLength() > 0) {
732                     sAppStream << sColor;
733                     sAppStream << fLeft << " " << fBottom << " " << fRight - fLeft << " " << fTop - fBottom << " re\n";
734                     sAppStream << fLeft + fWidth << " " << fBottom + fWidth << " "
735                                << fRight - fLeft - fWidth * 2 << " " << fTop - fBottom - fWidth * 2 << " re\n";
736                     sAppStream << "f*\n";
737                 }
738                 break;
739             case PBS_DASH:
740                 sColor = GenerateColorAP(color, FALSE);
741                 if (sColor.GetLength() > 0) {
742                     sAppStream << sColor;
743                     sAppStream << fWidth << " w" << " [" << dash.nDash << " " << dash.nGap << "] " << dash.nPhase << " d\n";
744                     sAppStream << fLeft + fWidth / 2 << " " << fBottom + fWidth / 2 << " m\n";
745                     sAppStream << fLeft + fWidth / 2 << " " << fTop - fWidth / 2 << " l\n";
746                     sAppStream << fRight - fWidth / 2 << " " << fTop - fWidth / 2 << " l\n";
747                     sAppStream << fRight - fWidth / 2 << " " << fBottom + fWidth / 2 << " l\n";
748                     sAppStream << fLeft + fWidth / 2 << " " << fBottom + fWidth / 2 << " l S\n";
749                 }
750                 break;
751             case PBS_BEVELED:
752             case PBS_INSET:
753                 sColor = GenerateColorAP(crLeftTop, TRUE);
754                 if (sColor.GetLength() > 0) {
755                     sAppStream << sColor;
756                     sAppStream << fLeft + fHalfWidth << " " << fBottom + fHalfWidth << " m\n";
757                     sAppStream << fLeft + fHalfWidth << " " << fTop - fHalfWidth << " l\n";
758                     sAppStream << fRight - fHalfWidth << " " << fTop - fHalfWidth << " l\n";
759                     sAppStream << fRight - fHalfWidth * 2 << " " << fTop - fHalfWidth * 2 << " l\n";
760                     sAppStream << fLeft + fHalfWidth * 2 << " " << fTop - fHalfWidth * 2 << " l\n";
761                     sAppStream << fLeft + fHalfWidth * 2 << " " << fBottom + fHalfWidth * 2 << " l f\n";
762                 }
763                 sColor = GenerateColorAP(crRightBottom, TRUE);
764                 if (sColor.GetLength() > 0) {
765                     sAppStream << sColor;
766                     sAppStream << fRight - fHalfWidth << " " << fTop - fHalfWidth << " m\n";
767                     sAppStream << fRight - fHalfWidth << " " << fBottom + fHalfWidth << " l\n";
768                     sAppStream << fLeft + fHalfWidth << " " <<  fBottom + fHalfWidth << " l\n";
769                     sAppStream << fLeft + fHalfWidth * 2 << " " << fBottom + fHalfWidth * 2 << " l\n";
770                     sAppStream << fRight - fHalfWidth * 2 << " " << fBottom + fHalfWidth * 2 << " l\n";
771                     sAppStream << fRight - fHalfWidth * 2 << " " << fTop - fHalfWidth * 2 << " l f\n";
772                 }
773                 sColor = GenerateColorAP(color, TRUE);
774                 if (sColor.GetLength() > 0) {
775                     sAppStream << sColor;
776                     sAppStream << fLeft << " " << fBottom << " " <<     fRight - fLeft << " " << fTop - fBottom << " re\n";
777                     sAppStream << fLeft + fHalfWidth << " " << fBottom + fHalfWidth << " "
778                                << fRight - fLeft - fHalfWidth * 2 << " " << fTop - fBottom - fHalfWidth * 2 << " re f*\n";
779                 }
780                 break;
781             case PBS_UNDERLINED:
782                 sColor = GenerateColorAP(color, FALSE);
783                 if (sColor.GetLength() > 0) {
784                     sAppStream << sColor;
785                     sAppStream << fWidth << " w\n";
786                     sAppStream << fLeft << " " << fBottom + fWidth / 2 << " m\n";
787                     sAppStream << fRight << " " << fBottom + fWidth / 2 << " l S\n";
788                 }
789                 break;
790         }
791     }
792     return sAppStream.GetByteString();
793 }
794 CFX_ByteString CPVT_GenerateAP::GenerateColorAP(const CPVT_Color & color, const FX_BOOL & bFillOrStroke)
795 {
796     CFX_ByteTextBuf sColorStream;
797     switch (color.nColorType) {
798         case CT_RGB:
799             sColorStream << color.fColor1 << " " << color.fColor2 << " " << color.fColor3 << " "
800                          << (bFillOrStroke ? "rg" : "RG") << "\n";
801             break;
802         case CT_GRAY:
803             sColorStream << color.fColor1 << " " << (bFillOrStroke ? "g" : "G") << "\n";
804             break;
805         case CT_CMYK:
806             sColorStream << color.fColor1 << " " << color.fColor2 << " " << color.fColor3 << " " << color.fColor4 << " "
807                          << (bFillOrStroke ? "k" : "K") << "\n";
808             break;
809     }
810     return sColorStream.GetByteString();
811 }