Commit a19264f0402b9f42b45e18a9d5bd0614eff33906

Authored by m-holger
1 parent bdcc697a

Refactor `QPDFFormFieldObjectHelper`: replace `getKey` with operator[], utilize …

…`Dictionary` and `Name` for improved clarity and consistency, and streamline `/Resources` and `/Font` handling.
libqpdf/QPDFFormFieldObjectHelper.cc
@@ -744,17 +744,18 @@ QPDFFormFieldObjectHelper::generateTextAppearance(QPDFAnnotationObjectHelper& ao @@ -744,17 +744,18 @@ QPDFFormFieldObjectHelper::generateTextAppearance(QPDFAnnotationObjectHelper& ao
744 { 744 {
745 QPDFObjectHandle AS = aoh.getAppearanceStream("/N"); 745 QPDFObjectHandle AS = aoh.getAppearanceStream("/N");
746 if (AS.null()) { 746 if (AS.null()) {
747 - QTC::TC("qpdf", "QPDFFormFieldObjectHelper create AS from scratch");  
748 QPDFObjectHandle::Rectangle rect = aoh.getRect(); 747 QPDFObjectHandle::Rectangle rect = aoh.getRect();
749 QPDFObjectHandle::Rectangle bbox(0, 0, rect.urx - rect.llx, rect.ury - rect.lly); 748 QPDFObjectHandle::Rectangle bbox(0, 0, rect.urx - rect.llx, rect.ury - rect.lly);
750 - QPDFObjectHandle dict = QPDFObjectHandle::parse(  
751 - "<< /Resources << /ProcSet [ /PDF /Text ] >> /Type /XObject /Subtype /Form >>");  
752 - dict.replaceKey("/BBox", QPDFObjectHandle::newFromRectangle(bbox)); 749 + auto dict = Dictionary(
  750 + {{"/BBox", QPDFObjectHandle::newFromRectangle(bbox)},
  751 + {"/Resources", Dictionary({{"/ProcSet", Array({Name("/PDF"), Name("/Text")})}})},
  752 + {"/Type", Name("/XObject")},
  753 + {"/Subtype", Name("/Form")}});
753 AS = QPDFObjectHandle::newStream(oh().getOwningQPDF(), "/Tx BMC\nEMC\n"); 754 AS = QPDFObjectHandle::newStream(oh().getOwningQPDF(), "/Tx BMC\nEMC\n");
754 AS.replaceDict(dict); 755 AS.replaceDict(dict);
755 - QPDFObjectHandle AP = aoh.getAppearanceDictionary();  
756 - if (AP.null()) {  
757 - aoh.getObjectHandle().replaceKey("/AP", QPDFObjectHandle::newDictionary()); 756 + Dictionary AP = aoh.getAppearanceDictionary();
  757 + if (!AP) {
  758 + aoh.getObjectHandle().replaceKey("/AP", Dictionary::empty());
758 AP = aoh.getAppearanceDictionary(); 759 AP = aoh.getAppearanceDictionary();
759 } 760 }
760 AP.replaceKey("/N", AS); 761 AP.replaceKey("/N", AS);
@@ -777,7 +778,7 @@ QPDFFormFieldObjectHelper::generateTextAppearance(QPDFAnnotationObjectHelper&amp; ao @@ -777,7 +778,7 @@ QPDFFormFieldObjectHelper::generateTextAppearance(QPDFAnnotationObjectHelper&amp; ao
777 std::string DA = getDefaultAppearance(); 778 std::string DA = getDefaultAppearance();
778 std::string V = getValueAsString(); 779 std::string V = getValueAsString();
779 std::vector<std::string> opt; 780 std::vector<std::string> opt;
780 - if (isChoice() && ((getFlags() & ff_ch_combo) == 0)) { 781 + if (isChoice() && (getFlags() & ff_ch_combo) == 0) {
781 opt = getChoices(); 782 opt = getChoices();
782 } 783 }
783 784
@@ -792,29 +793,26 @@ QPDFFormFieldObjectHelper::generateTextAppearance(QPDFAnnotationObjectHelper&amp; ao @@ -792,29 +793,26 @@ QPDFFormFieldObjectHelper::generateTextAppearance(QPDFAnnotationObjectHelper&amp; ao
792 std::string font_name = tff.getFontName(); 793 std::string font_name = tff.getFontName();
793 if (!font_name.empty()) { 794 if (!font_name.empty()) {
794 // See if the font is encoded with something we know about. 795 // See if the font is encoded with something we know about.
795 - QPDFObjectHandle resources = AS.getDict().getKey("/Resources");  
796 - QPDFObjectHandle font = getFontFromResource(resources, font_name);  
797 - bool found_font_in_dr = false; 796 + Dictionary resources = AS.getDict()["/Resources"];
  797 + Dictionary font = getFontFromResource(resources, font_name);
798 if (!font) { 798 if (!font) {
799 - QPDFObjectHandle dr = getDefaultResources();  
800 - font = getFontFromResource(dr, font_name);  
801 - found_font_in_dr = font.isDictionary();  
802 - }  
803 - if (found_font_in_dr && resources.isDictionary()) {  
804 - if (resources.isIndirect()) {  
805 - resources = resources.getQPDF().makeIndirectObject(resources.shallowCopy());  
806 - AS.getDict().replaceKey("/Resources", resources); 799 + font = getFontFromResource(getDefaultResources(), font_name);
  800 + if (resources) {
  801 + if (resources.indirect()) {
  802 + resources = resources.qpdf()->makeIndirectObject(resources.copy());
  803 + AS.getDict().replaceKey("/Resources", resources);
  804 + }
  805 + // Use mergeResources to force /Font to be local
  806 + QPDFObjectHandle res = resources;
  807 + res.mergeResources(Dictionary({{"/Font", Dictionary::empty()}}));
  808 + res.getKey("/Font").replaceKey(font_name, font);
807 } 809 }
808 - // Use mergeResources to force /Font to be local  
809 - resources.mergeResources("<< /Font << >> >>"_qpdf);  
810 - resources.getKey("/Font").replaceKey(font_name, font);  
811 } 810 }
812 811
813 - if (font.isDictionary() && font.getKey("/Encoding").isName()) {  
814 - std::string encoding = font.getKey("/Encoding").getName();  
815 - if (encoding == "/WinAnsiEncoding") { 812 + if (Name Encoding = font["/Encoding"]) {
  813 + if (Encoding == "/WinAnsiEncoding") {
816 encoder = &QUtil::utf8_to_win_ansi; 814 encoder = &QUtil::utf8_to_win_ansi;
817 - } else if (encoding == "/MacRomanEncoding") { 815 + } else if (Encoding == "/MacRomanEncoding") {
818 encoder = &QUtil::utf8_to_mac_roman; 816 encoder = &QUtil::utf8_to_mac_roman;
819 } 817 }
820 } 818 }
qpdf/qpdf.testcov
@@ -366,7 +366,6 @@ QPDFJob bytes fallback warning 0 @@ -366,7 +366,6 @@ QPDFJob bytes fallback warning 0
366 QPDFJob invalid utf-8 in auto 0 366 QPDFJob invalid utf-8 in auto 0
367 QPDFJob input password hex-bytes 0 367 QPDFJob input password hex-bytes 0
368 QPDFPageDocumentHelper ignore annotation with no appearance 0 368 QPDFPageDocumentHelper ignore annotation with no appearance 0
369 -QPDFFormFieldObjectHelper create AS from scratch 0  
370 QPDFFormFieldObjectHelper replaced BMC at EOF 0 369 QPDFFormFieldObjectHelper replaced BMC at EOF 0
371 QPDFFormFieldObjectHelper fallback Tf 0 370 QPDFFormFieldObjectHelper fallback Tf 0
372 QPDFPageObjectHelper copy shared attribute 1 371 QPDFPageObjectHelper copy shared attribute 1