OLD | NEW |
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fpdfdoc/cpvt_generateap.h" | 7 #include "core/fpdfdoc/cpvt_generateap.h" |
8 | 8 |
9 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" | 9 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h" |
10 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" | 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" |
11 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | 11 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
12 #include "core/fpdfapi/fpdf_parser/include/cpdf_simple_parser.h" | 12 #include "core/fpdfapi/fpdf_parser/include/cpdf_simple_parser.h" |
13 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" | 13 #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" |
14 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" | 14 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" |
15 #include "core/fpdfdoc/cpvt_color.h" | 15 #include "core/fpdfdoc/cpvt_color.h" |
16 #include "core/fpdfdoc/cpvt_fontmap.h" | 16 #include "core/fpdfdoc/cpvt_fontmap.h" |
17 #include "core/fpdfdoc/include/cpdf_formfield.h" | 17 #include "core/fpdfdoc/include/cpdf_formfield.h" |
18 #include "core/fpdfdoc/include/cpvt_word.h" | 18 #include "core/fpdfdoc/include/cpvt_word.h" |
19 #include "core/fpdfdoc/pdf_vt.h" | 19 #include "core/fpdfdoc/pdf_vt.h" |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 FX_BOOL GenerateWidgetAP(CPDF_Document* pDoc, | 23 bool GenerateWidgetAP(CPDF_Document* pDoc, |
24 CPDF_Dictionary* pAnnotDict, | 24 CPDF_Dictionary* pAnnotDict, |
25 const int32_t& nWidgetType) { | 25 const int32_t& nWidgetType) { |
26 CPDF_Dictionary* pFormDict = nullptr; | 26 CPDF_Dictionary* pFormDict = nullptr; |
27 if (CPDF_Dictionary* pRootDict = pDoc->GetRoot()) | 27 if (CPDF_Dictionary* pRootDict = pDoc->GetRoot()) |
28 pFormDict = pRootDict->GetDictBy("AcroForm"); | 28 pFormDict = pRootDict->GetDictBy("AcroForm"); |
29 if (!pFormDict) | 29 if (!pFormDict) |
30 return FALSE; | 30 return false; |
31 | 31 |
32 CFX_ByteString DA; | 32 CFX_ByteString DA; |
33 if (CPDF_Object* pDAObj = FPDF_GetFieldAttr(pAnnotDict, "DA")) | 33 if (CPDF_Object* pDAObj = FPDF_GetFieldAttr(pAnnotDict, "DA")) |
34 DA = pDAObj->GetString(); | 34 DA = pDAObj->GetString(); |
35 if (DA.IsEmpty()) | 35 if (DA.IsEmpty()) |
36 DA = pFormDict->GetStringBy("DA"); | 36 DA = pFormDict->GetStringBy("DA"); |
37 if (DA.IsEmpty()) | 37 if (DA.IsEmpty()) |
38 return FALSE; | 38 return false; |
39 | 39 |
40 CPDF_SimpleParser syntax(DA.AsStringC()); | 40 CPDF_SimpleParser syntax(DA.AsStringC()); |
41 syntax.FindTagParamFromStart("Tf", 2); | 41 syntax.FindTagParamFromStart("Tf", 2); |
42 CFX_ByteString sFontName(syntax.GetWord()); | 42 CFX_ByteString sFontName(syntax.GetWord()); |
43 sFontName = PDF_NameDecode(sFontName); | 43 sFontName = PDF_NameDecode(sFontName); |
44 if (sFontName.IsEmpty()) | 44 if (sFontName.IsEmpty()) |
45 return FALSE; | 45 return false; |
46 | 46 |
47 FX_FLOAT fFontSize = FX_atof(syntax.GetWord()); | 47 FX_FLOAT fFontSize = FX_atof(syntax.GetWord()); |
48 CPVT_Color crText = CPVT_Color::ParseColor(DA); | 48 CPVT_Color crText = CPVT_Color::ParseColor(DA); |
49 FX_BOOL bUseFormRes = FALSE; | 49 FX_BOOL bUseFormRes = FALSE; |
50 CPDF_Dictionary* pFontDict = nullptr; | 50 CPDF_Dictionary* pFontDict = nullptr; |
51 CPDF_Dictionary* pDRDict = pAnnotDict->GetDictBy("DR"); | 51 CPDF_Dictionary* pDRDict = pAnnotDict->GetDictBy("DR"); |
52 if (!pDRDict) { | 52 if (!pDRDict) { |
53 pDRDict = pFormDict->GetDictBy("DR"); | 53 pDRDict = pFormDict->GetDictBy("DR"); |
54 bUseFormRes = TRUE; | 54 bUseFormRes = TRUE; |
55 } | 55 } |
56 CPDF_Dictionary* pDRFontDict = pDRDict ? pDRDict->GetDictBy("Font") : nullptr; | 56 CPDF_Dictionary* pDRFontDict = pDRDict ? pDRDict->GetDictBy("Font") : nullptr; |
57 if (pDRFontDict) { | 57 if (pDRFontDict) { |
58 pFontDict = pDRFontDict->GetDictBy(sFontName.Mid(1)); | 58 pFontDict = pDRFontDict->GetDictBy(sFontName.Mid(1)); |
59 if (!pFontDict && !bUseFormRes) { | 59 if (!pFontDict && !bUseFormRes) { |
60 pDRDict = pFormDict->GetDictBy("DR"); | 60 pDRDict = pFormDict->GetDictBy("DR"); |
61 pDRFontDict = pDRDict->GetDictBy("Font"); | 61 pDRFontDict = pDRDict->GetDictBy("Font"); |
62 if (pDRFontDict) | 62 if (pDRFontDict) |
63 pFontDict = pDRFontDict->GetDictBy(sFontName.Mid(1)); | 63 pFontDict = pDRFontDict->GetDictBy(sFontName.Mid(1)); |
64 } | 64 } |
65 } | 65 } |
66 if (!pDRFontDict) | 66 if (!pDRFontDict) |
67 return FALSE; | 67 return false; |
68 | 68 |
69 if (!pFontDict) { | 69 if (!pFontDict) { |
70 pFontDict = new CPDF_Dictionary; | 70 pFontDict = new CPDF_Dictionary; |
71 pFontDict->SetAtName("Type", "Font"); | 71 pFontDict->SetAtName("Type", "Font"); |
72 pFontDict->SetAtName("Subtype", "Type1"); | 72 pFontDict->SetAtName("Subtype", "Type1"); |
73 pFontDict->SetAtName("BaseFont", "Helvetica"); | 73 pFontDict->SetAtName("BaseFont", "Helvetica"); |
74 pFontDict->SetAtName("Encoding", "WinAnsiEncoding"); | 74 pFontDict->SetAtName("Encoding", "WinAnsiEncoding"); |
75 pDoc->AddIndirectObject(pFontDict); | 75 pDoc->AddIndirectObject(pFontDict); |
76 pDRFontDict->SetAtReference(sFontName.Mid(1), pDoc, pFontDict); | 76 pDRFontDict->SetAtReference(sFontName.Mid(1), pDoc, pFontDict); |
77 } | 77 } |
78 CPDF_Font* pDefFont = pDoc->LoadFont(pFontDict); | 78 CPDF_Font* pDefFont = pDoc->LoadFont(pFontDict); |
79 if (!pDefFont) | 79 if (!pDefFont) |
80 return FALSE; | 80 return false; |
81 | 81 |
82 CFX_FloatRect rcAnnot = pAnnotDict->GetRectBy("Rect"); | 82 CFX_FloatRect rcAnnot = pAnnotDict->GetRectBy("Rect"); |
83 int32_t nRotate = 0; | 83 int32_t nRotate = 0; |
84 if (CPDF_Dictionary* pMKDict = pAnnotDict->GetDictBy("MK")) | 84 if (CPDF_Dictionary* pMKDict = pAnnotDict->GetDictBy("MK")) |
85 nRotate = pMKDict->GetIntegerBy("R"); | 85 nRotate = pMKDict->GetIntegerBy("R"); |
86 | 86 |
87 CFX_FloatRect rcBBox; | 87 CFX_FloatRect rcBBox; |
88 CFX_Matrix matrix; | 88 CFX_Matrix matrix; |
89 switch (nRotate % 360) { | 89 switch (nRotate % 360) { |
90 case 0: | 90 case 0: |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 pStreamResList->SetAt("Font", pStreamResFontList); | 433 pStreamResList->SetAt("Font", pStreamResFontList); |
434 } | 434 } |
435 if (!pStreamResFontList->KeyExist(sFontName)) | 435 if (!pStreamResFontList->KeyExist(sFontName)) |
436 pStreamResFontList->SetAtReference(sFontName, pDoc, pFontDict); | 436 pStreamResFontList->SetAtReference(sFontName, pDoc, pFontDict); |
437 } else { | 437 } else { |
438 pStreamDict->SetAt("Resources", pFormDict->GetDictBy("DR")->Clone()); | 438 pStreamDict->SetAt("Resources", pFormDict->GetDictBy("DR")->Clone()); |
439 pStreamResList = pStreamDict->GetDictBy("Resources"); | 439 pStreamResList = pStreamDict->GetDictBy("Resources"); |
440 } | 440 } |
441 } | 441 } |
442 } | 442 } |
443 return TRUE; | 443 return true; |
444 } | 444 } |
445 | 445 |
446 } // namespace | 446 } // namespace |
447 | 447 |
448 FX_BOOL FPDF_GenerateAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict) { | 448 bool FPDF_GenerateAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict) { |
449 if (!pAnnotDict || pAnnotDict->GetStringBy("Subtype") != "Widget") { | 449 if (!pAnnotDict || pAnnotDict->GetStringBy("Subtype") != "Widget") |
450 return FALSE; | 450 return false; |
451 } | 451 |
452 CFX_ByteString field_type = FPDF_GetFieldAttr(pAnnotDict, "FT")->GetString(); | 452 CFX_ByteString field_type = FPDF_GetFieldAttr(pAnnotDict, "FT")->GetString(); |
453 uint32_t flags = FPDF_GetFieldAttr(pAnnotDict, "Ff") | 453 uint32_t flags = FPDF_GetFieldAttr(pAnnotDict, "Ff") |
454 ? FPDF_GetFieldAttr(pAnnotDict, "Ff")->GetInteger() | 454 ? FPDF_GetFieldAttr(pAnnotDict, "Ff")->GetInteger() |
455 : 0; | 455 : 0; |
456 if (field_type == "Tx") { | 456 if (field_type == "Tx") { |
457 return CPVT_GenerateAP::GenerateTextFieldAP(pDoc, pAnnotDict); | 457 return CPVT_GenerateAP::GenerateTextFieldAP(pDoc, pAnnotDict); |
458 } | 458 } |
459 if (field_type == "Ch") { | 459 if (field_type == "Ch") { |
460 return (flags & (1 << 17)) | 460 return (flags & (1 << 17)) |
461 ? CPVT_GenerateAP::GenerateComboBoxAP(pDoc, pAnnotDict) | 461 ? CPVT_GenerateAP::GenerateComboBoxAP(pDoc, pAnnotDict) |
462 : CPVT_GenerateAP::GenerateListBoxAP(pDoc, pAnnotDict); | 462 : CPVT_GenerateAP::GenerateListBoxAP(pDoc, pAnnotDict); |
463 } | 463 } |
464 if (field_type == "Btn") { | 464 if (field_type == "Btn") { |
465 if (!(flags & (1 << 16))) { | 465 if (!(flags & (1 << 16))) { |
466 if (!pAnnotDict->KeyExist("AS")) { | 466 if (!pAnnotDict->KeyExist("AS")) { |
467 if (CPDF_Dictionary* pParentDict = pAnnotDict->GetDictBy("Parent")) { | 467 if (CPDF_Dictionary* pParentDict = pAnnotDict->GetDictBy("Parent")) { |
468 if (pParentDict->KeyExist("AS")) { | 468 if (pParentDict->KeyExist("AS")) { |
469 pAnnotDict->SetAtString("AS", pParentDict->GetStringBy("AS")); | 469 pAnnotDict->SetAtString("AS", pParentDict->GetStringBy("AS")); |
470 } | 470 } |
471 } | 471 } |
472 } | 472 } |
473 } | 473 } |
474 } | 474 } |
475 return FALSE; | 475 return false; |
476 } | 476 } |
477 | 477 |
478 // Static. | 478 // Static. |
479 FX_BOOL CPVT_GenerateAP::GenerateTextFieldAP(CPDF_Document* pDoc, | 479 bool CPVT_GenerateAP::GenerateComboBoxAP(CPDF_Document* pDoc, |
480 CPDF_Dictionary* pAnnotDict) { | 480 CPDF_Dictionary* pAnnotDict) { |
| 481 return GenerateWidgetAP(pDoc, pAnnotDict, 1); |
| 482 } |
| 483 |
| 484 // Static. |
| 485 bool CPVT_GenerateAP::GenerateListBoxAP(CPDF_Document* pDoc, |
| 486 CPDF_Dictionary* pAnnotDict) { |
| 487 return GenerateWidgetAP(pDoc, pAnnotDict, 2); |
| 488 } |
| 489 |
| 490 // Static. |
| 491 bool CPVT_GenerateAP::GenerateTextFieldAP(CPDF_Document* pDoc, |
| 492 CPDF_Dictionary* pAnnotDict) { |
481 return GenerateWidgetAP(pDoc, pAnnotDict, 0); | 493 return GenerateWidgetAP(pDoc, pAnnotDict, 0); |
482 } | 494 } |
483 | 495 |
484 // Static. | 496 // Static. |
485 FX_BOOL CPVT_GenerateAP::GenerateComboBoxAP(CPDF_Document* pDoc, | |
486 CPDF_Dictionary* pAnnotDict) { | |
487 return GenerateWidgetAP(pDoc, pAnnotDict, 1); | |
488 } | |
489 | |
490 // Static. | |
491 FX_BOOL CPVT_GenerateAP::GenerateListBoxAP(CPDF_Document* pDoc, | |
492 CPDF_Dictionary* pAnnotDict) { | |
493 return GenerateWidgetAP(pDoc, pAnnotDict, 2); | |
494 } | |
495 | |
496 // Static. | |
497 CFX_ByteString CPVT_GenerateAP::GenerateEditAP( | 497 CFX_ByteString CPVT_GenerateAP::GenerateEditAP( |
498 IPVT_FontMap* pFontMap, | 498 IPVT_FontMap* pFontMap, |
499 CPDF_VariableText::Iterator* pIterator, | 499 CPDF_VariableText::Iterator* pIterator, |
500 const CFX_FloatPoint& ptOffset, | 500 const CFX_FloatPoint& ptOffset, |
501 FX_BOOL bContinuous, | 501 FX_BOOL bContinuous, |
502 uint16_t SubWord) { | 502 uint16_t SubWord) { |
503 CFX_ByteTextBuf sEditStream; | 503 CFX_ByteTextBuf sEditStream; |
504 CFX_ByteTextBuf sLineStream; | 504 CFX_ByteTextBuf sLineStream; |
505 CFX_ByteTextBuf sWords; | 505 CFX_ByteTextBuf sWords; |
506 CFX_FloatPoint ptOld(0.0f, 0.0f); | 506 CFX_FloatPoint ptOld(0.0f, 0.0f); |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
751 int32_t nFontIndex, | 751 int32_t nFontIndex, |
752 FX_FLOAT fFontSize) { | 752 FX_FLOAT fFontSize) { |
753 CFX_ByteTextBuf sRet; | 753 CFX_ByteTextBuf sRet; |
754 if (pFontMap) { | 754 if (pFontMap) { |
755 CFX_ByteString sFontAlias = pFontMap->GetPDFFontAlias(nFontIndex); | 755 CFX_ByteString sFontAlias = pFontMap->GetPDFFontAlias(nFontIndex); |
756 if (sFontAlias.GetLength() > 0 && fFontSize > 0) | 756 if (sFontAlias.GetLength() > 0 && fFontSize > 0) |
757 sRet << "/" << sFontAlias << " " << fFontSize << " Tf\n"; | 757 sRet << "/" << sFontAlias << " " << fFontSize << " Tf\n"; |
758 } | 758 } |
759 return sRet.MakeString(); | 759 return sRet.MakeString(); |
760 } | 760 } |
OLD | NEW |