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