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" |
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
445 pStreamResFontList->SetAtReference(sFontName, pDoc, pFontDict); | 445 pStreamResFontList->SetAtReference(sFontName, pDoc, pFontDict); |
446 } else { | 446 } else { |
447 pStreamDict->SetAt("Resources", pFormDict->GetDictBy("DR")->Clone()); | 447 pStreamDict->SetAt("Resources", pFormDict->GetDictBy("DR")->Clone()); |
448 pStreamResList = pStreamDict->GetDictBy("Resources"); | 448 pStreamResList = pStreamDict->GetDictBy("Resources"); |
449 } | 449 } |
450 } | 450 } |
451 } | 451 } |
452 return true; | 452 return true; |
453 } | 453 } |
454 | 454 |
455 CFX_ByteString GetColorStringWithDefault(CPDF_Dictionary* pAnnotDict, | 455 CFX_ByteString GetColorStringWithDefault(CPDF_Array* pColor, |
456 const CPVT_Color& crDefaultColor, | 456 const CPVT_Color& crDefaultColor, |
457 PaintOperation nOperation) { | 457 PaintOperation nOperation) { |
458 if (CPDF_Array* pColor = pAnnotDict->GetArrayBy("C")) { | 458 if (pColor) { |
459 CPVT_Color color = CPVT_Color::ParseColor(*pColor); | 459 CPVT_Color color = CPVT_Color::ParseColor(*pColor); |
460 return CPVT_GenerateAP::GenerateColorAP(color, nOperation); | 460 return CPVT_GenerateAP::GenerateColorAP(color, nOperation); |
461 } | 461 } |
462 | 462 |
463 return CPVT_GenerateAP::GenerateColorAP(crDefaultColor, nOperation); | 463 return CPVT_GenerateAP::GenerateColorAP(crDefaultColor, nOperation); |
464 } | 464 } |
465 | 465 |
466 FX_FLOAT GetBorderWidth(const CPDF_Dictionary& pAnnotDict) { | |
467 if (CPDF_Dictionary* pBorderStyleDict = pAnnotDict.GetDictBy("BS")) { | |
468 if (pBorderStyleDict->KeyExist("W")) | |
469 return pBorderStyleDict->GetNumberBy("W"); | |
470 } | |
471 | |
472 if (CPDF_Array* pBorderArray = pAnnotDict.GetArrayBy("Border")) { | |
473 if (pBorderArray->GetCount() > 2) | |
474 return pBorderArray->GetNumberAt(2); | |
475 } | |
476 | |
477 return 1; | |
478 } | |
479 | |
480 CFX_ByteString GetDashPatternString(const CPDF_Dictionary& pAnnotDict) { | |
481 CPDF_Array* pDashArray = nullptr; | |
482 | |
483 if (CPDF_Dictionary* pBorderStyleDict = pAnnotDict.GetDictBy("BS")) { | |
484 if (pBorderStyleDict->GetStringBy("S") == "D") | |
485 pDashArray = pBorderStyleDict->GetArrayBy("D"); | |
486 } else if (CPDF_Array* pBorderArray = pAnnotDict.GetArrayBy("Border")) { | |
Lei Zhang
2016/08/05 22:14:45
In GetBorderWidth() above, it checks the "BS" dict
jaepark
2016/08/05 23:07:39
Done.
| |
487 if (pBorderArray->GetCount() == 4) | |
488 pDashArray = pBorderArray->GetArrayAt(3); | |
489 } | |
490 | |
491 if (!pDashArray) | |
492 return CFX_ByteString(); | |
493 | |
494 size_t pDashArrayCount = pDashArray->GetCount(); | |
495 if (pDashArrayCount == 0 || pDashArrayCount > 10) | |
496 return CFX_ByteString(); | |
497 | |
498 CFX_ByteTextBuf sDashStream; | |
499 sDashStream << "["; | |
500 for (size_t i = 0; i < pDashArrayCount; ++i) | |
501 sDashStream << pDashArray->GetNumberAt(i) << " "; | |
502 sDashStream << "] 0 d\n"; | |
503 | |
504 return sDashStream.MakeString(); | |
505 } | |
506 | |
466 CPDF_Dictionary* GenerateExtGStateDict(const CPDF_Dictionary& pAnnotDict, | 507 CPDF_Dictionary* GenerateExtGStateDict(const CPDF_Dictionary& pAnnotDict, |
467 const CFX_ByteString& sExtGSDictName, | 508 const CFX_ByteString& sExtGSDictName, |
468 const CFX_ByteString& sBlendMode) { | 509 const CFX_ByteString& sBlendMode) { |
469 CPDF_Dictionary* pGSDict = new CPDF_Dictionary; | 510 CPDF_Dictionary* pGSDict = new CPDF_Dictionary; |
470 pGSDict->SetAtString("Type", "ExtGState"); | 511 pGSDict->SetAtString("Type", "ExtGState"); |
471 | 512 |
472 FX_FLOAT fOpacity = | 513 FX_FLOAT fOpacity = |
473 pAnnotDict.KeyExist("CA") ? pAnnotDict.GetNumberBy("CA") : 1; | 514 pAnnotDict.KeyExist("CA") ? pAnnotDict.GetNumberBy("CA") : 1; |
474 pGSDict->SetAtNumber("CA", fOpacity); | 515 pGSDict->SetAtNumber("CA", fOpacity); |
475 pGSDict->SetAtNumber("ca", fOpacity); | 516 pGSDict->SetAtNumber("ca", fOpacity); |
(...skipping 28 matching lines...) Expand all Loading... | |
504 | 545 |
505 CFX_FloatRect rect = pAnnotDict->GetRectBy("Rect"); | 546 CFX_FloatRect rect = pAnnotDict->GetRectBy("Rect"); |
506 pStreamDict->SetAtRect("BBox", rect); | 547 pStreamDict->SetAtRect("BBox", rect); |
507 | 548 |
508 CPDF_Dictionary* pResourceDict = new CPDF_Dictionary; | 549 CPDF_Dictionary* pResourceDict = new CPDF_Dictionary; |
509 pResourceDict->SetAt("ExtGState", pExtGStateDict); | 550 pResourceDict->SetAt("ExtGState", pExtGStateDict); |
510 | 551 |
511 pStreamDict->SetAt("Resources", pResourceDict); | 552 pStreamDict->SetAt("Resources", pResourceDict); |
512 } | 553 } |
513 | 554 |
555 CFX_ByteString GetPaintOperatorString(bool bIsStrokeRect, bool bIsFillRect) { | |
Lei Zhang
2016/08/05 22:14:45
If you want it to be more compact:
if (bIsStrokeR
jaepark
2016/08/05 23:07:39
Done.
| |
556 if (bIsStrokeRect && bIsFillRect) | |
557 return "b"; | |
558 else if (bIsStrokeRect) | |
Lei Zhang
2016/08/05 22:14:45
no else after return
jaepark
2016/08/05 23:07:39
Done.
| |
559 return "s"; | |
560 else if (bIsFillRect) | |
561 return "f"; | |
562 else | |
563 return "n"; | |
564 } | |
565 | |
514 } // namespace | 566 } // namespace |
515 | 567 |
516 bool FPDF_GenerateAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict) { | 568 bool FPDF_GenerateAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict) { |
517 if (!pAnnotDict || pAnnotDict->GetStringBy("Subtype") != "Widget") | 569 if (!pAnnotDict || pAnnotDict->GetStringBy("Subtype") != "Widget") |
518 return false; | 570 return false; |
519 | 571 |
520 CFX_ByteString field_type = FPDF_GetFieldAttr(pAnnotDict, "FT")->GetString(); | 572 CFX_ByteString field_type = FPDF_GetFieldAttr(pAnnotDict, "FT")->GetString(); |
521 uint32_t flags = FPDF_GetFieldAttr(pAnnotDict, "Ff") | 573 uint32_t flags = FPDF_GetFieldAttr(pAnnotDict, "Ff") |
522 ? FPDF_GetFieldAttr(pAnnotDict, "Ff")->GetInteger() | 574 ? FPDF_GetFieldAttr(pAnnotDict, "Ff")->GetInteger() |
523 : 0; | 575 : 0; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
565 CPDF_Dictionary* pAnnotDict) { | 617 CPDF_Dictionary* pAnnotDict) { |
566 // If AP dictionary exists, we use the appearance defined in the | 618 // If AP dictionary exists, we use the appearance defined in the |
567 // existing AP dictionary. | 619 // existing AP dictionary. |
568 if (pAnnotDict->KeyExist("AP")) | 620 if (pAnnotDict->KeyExist("AP")) |
569 return false; | 621 return false; |
570 | 622 |
571 CFX_ByteTextBuf sAppStream; | 623 CFX_ByteTextBuf sAppStream; |
572 CFX_ByteString sExtGSDictName = "GS"; | 624 CFX_ByteString sExtGSDictName = "GS"; |
573 sAppStream << "/" << sExtGSDictName << " gs "; | 625 sAppStream << "/" << sExtGSDictName << " gs "; |
574 | 626 |
575 sAppStream << GetColorStringWithDefault( | 627 sAppStream << GetColorStringWithDefault(pAnnotDict->GetArrayBy("C"), |
576 pAnnotDict, CPVT_Color(CPVT_Color::kRGB, 1, 1, 0), PaintOperation::FILL); | 628 CPVT_Color(CPVT_Color::kRGB, 1, 1, 0), |
629 PaintOperation::FILL); | |
577 | 630 |
578 CFX_FloatRect rect = pAnnotDict->GetRectBy("Rect"); | 631 CFX_FloatRect rect = pAnnotDict->GetRectBy("Rect"); |
579 rect.Normalize(); | 632 rect.Normalize(); |
580 | 633 |
581 sAppStream << rect.left << " " << rect.top << " m " << rect.right << " " | 634 sAppStream << rect.left << " " << rect.top << " m " << rect.right << " " |
582 << rect.top << " l " << rect.right << " " << rect.bottom << " l " | 635 << rect.top << " l " << rect.right << " " << rect.bottom << " l " |
583 << rect.left << " " << rect.bottom << " l " | 636 << rect.left << " " << rect.bottom << " l " |
584 << "h f\n"; | 637 << "h f\n"; |
585 | 638 |
586 CPDF_Dictionary* pExtGStateDict = | 639 CPDF_Dictionary* pExtGStateDict = |
587 GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Multiply"); | 640 GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Multiply"); |
588 GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pExtGStateDict); | 641 GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pExtGStateDict); |
589 | 642 |
590 return true; | 643 return true; |
591 } | 644 } |
592 | 645 |
593 bool CPVT_GenerateAP::GenerateUnderlineAP(CPDF_Document* pDoc, | 646 bool CPVT_GenerateAP::GenerateUnderlineAP(CPDF_Document* pDoc, |
594 CPDF_Dictionary* pAnnotDict) { | 647 CPDF_Dictionary* pAnnotDict) { |
595 // If AP dictionary exists, we use the appearance defined in the | 648 // If AP dictionary exists, we use the appearance defined in the |
596 // existing AP dictionary. | 649 // existing AP dictionary. |
597 if (pAnnotDict->KeyExist("AP")) | 650 if (pAnnotDict->KeyExist("AP")) |
598 return false; | 651 return false; |
599 | 652 |
600 CFX_ByteTextBuf sAppStream; | 653 CFX_ByteTextBuf sAppStream; |
601 CFX_ByteString sExtGSDictName = "GS"; | 654 CFX_ByteString sExtGSDictName = "GS"; |
602 sAppStream << "/" << sExtGSDictName << " gs "; | 655 sAppStream << "/" << sExtGSDictName << " gs "; |
603 | 656 |
604 sAppStream << GetColorStringWithDefault(pAnnotDict, | 657 sAppStream << GetColorStringWithDefault(pAnnotDict->GetArrayBy("C"), |
605 CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), | 658 CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), |
606 PaintOperation::STROKE); | 659 PaintOperation::STROKE); |
607 | 660 |
608 CFX_FloatRect rect = pAnnotDict->GetRectBy("Rect"); | 661 CFX_FloatRect rect = pAnnotDict->GetRectBy("Rect"); |
609 rect.Normalize(); | 662 rect.Normalize(); |
610 | 663 |
611 FX_FLOAT fLineWidth = 1.0; | 664 FX_FLOAT fLineWidth = 1.0; |
612 sAppStream << fLineWidth << " w " << rect.left << " " | 665 sAppStream << fLineWidth << " w " << rect.left << " " |
613 << rect.bottom + fLineWidth << " m " << rect.right << " " | 666 << rect.bottom + fLineWidth << " m " << rect.right << " " |
614 << rect.bottom + fLineWidth << " l S\n"; | 667 << rect.bottom + fLineWidth << " l S\n"; |
615 | 668 |
616 CPDF_Dictionary* pExtGStateDict = | 669 CPDF_Dictionary* pExtGStateDict = |
617 GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); | 670 GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); |
618 GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pExtGStateDict); | 671 GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pExtGStateDict); |
619 return true; | 672 return true; |
620 } | 673 } |
621 | 674 |
675 bool CPVT_GenerateAP::GenerateSquareAP(CPDF_Document* pDoc, | |
676 CPDF_Dictionary* pAnnotDict) { | |
677 // If AP dictionary exists, we use the appearance defined in the | |
678 // existing AP dictionary. | |
679 if (pAnnotDict->KeyExist("AP")) | |
680 return false; | |
681 | |
682 CFX_ByteTextBuf sAppStream; | |
683 CFX_ByteString sExtGSDictName = "GS"; | |
684 sAppStream << "/" << sExtGSDictName << " gs "; | |
685 | |
686 CPDF_Array* pInteriorColor = pAnnotDict->GetArrayBy("IC"); | |
687 sAppStream << GetColorStringWithDefault(pInteriorColor, | |
688 CPVT_Color(CPVT_Color::kTransparent), | |
689 PaintOperation::FILL); | |
690 | |
691 sAppStream << GetColorStringWithDefault(pAnnotDict->GetArrayBy("C"), | |
692 CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), | |
693 PaintOperation::STROKE); | |
694 | |
695 FX_FLOAT fBorderWidth = GetBorderWidth(*pAnnotDict); | |
696 sAppStream << fBorderWidth << " w "; | |
Lei Zhang
2016/08/05 22:14:45
So we write into the AP stream, even if the width
jaepark
2016/08/05 23:07:39
Whether to stroke or not depends on the paint oper
Lei Zhang
2016/08/06 00:10:37
I'm just want to make sure we render correctly if
jaepark
2016/08/08 17:17:27
On 2016/08/06 00:10:37, Lei Zhang wrote:
| |
697 | |
698 sAppStream << GetDashPatternString(*pAnnotDict); | |
699 | |
700 CFX_FloatRect rect = pAnnotDict->GetRectBy("Rect"); | |
701 rect.Normalize(); | |
702 | |
703 // Deflating rect because stroking a path entails painting all points whose | |
704 // perpendicular distance from the path in user space is less than or equal go | |
705 // half the line width. | |
706 rect.Deflate(fBorderWidth / 2, fBorderWidth / 2); | |
707 | |
708 bool bIsStrokeRect = fBorderWidth > 0; | |
709 bool bIsFillRect = pInteriorColor && (pInteriorColor->GetCount() > 0); | |
710 | |
711 sAppStream << rect.left << " " << rect.bottom << " " << rect.Width() << " " | |
712 << rect.Height() << " re " | |
713 << GetPaintOperatorString(bIsStrokeRect, bIsFillRect) << "\n"; | |
714 | |
715 CPDF_Dictionary* pExtGStateDict = | |
716 GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); | |
717 GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pExtGStateDict); | |
718 return true; | |
719 } | |
720 | |
622 bool CPVT_GenerateAP::GenerateSquigglyAP(CPDF_Document* pDoc, | 721 bool CPVT_GenerateAP::GenerateSquigglyAP(CPDF_Document* pDoc, |
623 CPDF_Dictionary* pAnnotDict) { | 722 CPDF_Dictionary* pAnnotDict) { |
624 // If AP dictionary exists, we use the appearance defined in the | 723 // If AP dictionary exists, we use the appearance defined in the |
625 // existing AP dictionary. | 724 // existing AP dictionary. |
626 if (pAnnotDict->KeyExist("AP")) | 725 if (pAnnotDict->KeyExist("AP")) |
627 return false; | 726 return false; |
628 | 727 |
629 CFX_ByteTextBuf sAppStream; | 728 CFX_ByteTextBuf sAppStream; |
630 CFX_ByteString sExtGSDictName = "GS"; | 729 CFX_ByteString sExtGSDictName = "GS"; |
631 sAppStream << "/" << sExtGSDictName << " gs "; | 730 sAppStream << "/" << sExtGSDictName << " gs "; |
632 | 731 |
633 sAppStream << GetColorStringWithDefault(pAnnotDict, | 732 sAppStream << GetColorStringWithDefault(pAnnotDict->GetArrayBy("C"), |
634 CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), | 733 CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), |
635 PaintOperation::STROKE); | 734 PaintOperation::STROKE); |
636 | 735 |
637 CFX_FloatRect rect = pAnnotDict->GetRectBy("Rect"); | 736 CFX_FloatRect rect = pAnnotDict->GetRectBy("Rect"); |
638 rect.Normalize(); | 737 rect.Normalize(); |
639 | 738 |
640 FX_FLOAT fLineWidth = 1.0; | 739 FX_FLOAT fLineWidth = 1.0; |
641 sAppStream << fLineWidth << " w "; | 740 sAppStream << fLineWidth << " w "; |
642 | 741 |
643 const FX_FLOAT fDelta = 2.0; | 742 const FX_FLOAT fDelta = 2.0; |
(...skipping 30 matching lines...) Expand all Loading... | |
674 CPDF_Dictionary* pAnnotDict) { | 773 CPDF_Dictionary* pAnnotDict) { |
675 // If AP dictionary exists, we use the appearance defined in the | 774 // If AP dictionary exists, we use the appearance defined in the |
676 // existing AP dictionary. | 775 // existing AP dictionary. |
677 if (pAnnotDict->KeyExist("AP")) | 776 if (pAnnotDict->KeyExist("AP")) |
678 return false; | 777 return false; |
679 | 778 |
680 CFX_ByteTextBuf sAppStream; | 779 CFX_ByteTextBuf sAppStream; |
681 CFX_ByteString sExtGSDictName = "GS"; | 780 CFX_ByteString sExtGSDictName = "GS"; |
682 sAppStream << "/" << sExtGSDictName << " gs "; | 781 sAppStream << "/" << sExtGSDictName << " gs "; |
683 | 782 |
684 sAppStream << GetColorStringWithDefault(pAnnotDict, | 783 sAppStream << GetColorStringWithDefault(pAnnotDict->GetArrayBy("C"), |
685 CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), | 784 CPVT_Color(CPVT_Color::kRGB, 0, 0, 0), |
686 PaintOperation::STROKE); | 785 PaintOperation::STROKE); |
687 | 786 |
688 CFX_FloatRect rect = pAnnotDict->GetRectBy("Rect"); | 787 CFX_FloatRect rect = pAnnotDict->GetRectBy("Rect"); |
689 rect.Normalize(); | 788 rect.Normalize(); |
690 | 789 |
691 FX_FLOAT fLineWidth = 1.0; | 790 FX_FLOAT fLineWidth = 1.0; |
692 FX_FLOAT fY = (rect.top + rect.bottom) / 2; | 791 FX_FLOAT fY = (rect.top + rect.bottom) / 2; |
693 sAppStream << fLineWidth << " w " << rect.left << " " << fY << " m " | 792 sAppStream << fLineWidth << " w " << rect.left << " " << fY << " m " |
694 << rect.right << " " << fY << " l S\n"; | 793 << rect.right << " " << fY << " l S\n"; |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
960 int32_t nFontIndex, | 1059 int32_t nFontIndex, |
961 FX_FLOAT fFontSize) { | 1060 FX_FLOAT fFontSize) { |
962 CFX_ByteTextBuf sRet; | 1061 CFX_ByteTextBuf sRet; |
963 if (pFontMap) { | 1062 if (pFontMap) { |
964 CFX_ByteString sFontAlias = pFontMap->GetPDFFontAlias(nFontIndex); | 1063 CFX_ByteString sFontAlias = pFontMap->GetPDFFontAlias(nFontIndex); |
965 if (sFontAlias.GetLength() > 0 && fFontSize > 0) | 1064 if (sFontAlias.GetLength() > 0 && fFontSize > 0) |
966 sRet << "/" << sFontAlias << " " << fFontSize << " Tf\n"; | 1065 sRet << "/" << sFontAlias << " " << fFontSize << " Tf\n"; |
967 } | 1066 } |
968 return sRet.MakeString(); | 1067 return sRet.MakeString(); |
969 } | 1068 } |
OLD | NEW |