Commit 796913e9b64db041a30c2c247eca9f2e75790bce

Authored by m-holger
1 parent 306f0efa

Move QPDFObjectHandle array methods implementation to QPDF_Array.cc

libqpdf/QPDFObjectHandle.cc
@@ -766,7 +766,7 @@ QPDFObjectHandle::getValueAsInlineImage(std::string& value) const @@ -766,7 +766,7 @@ QPDFObjectHandle::getValueAsInlineImage(std::string& value) const
766 return true; 766 return true;
767 } 767 }
768 768
769 -// Array accessors 769 +// Array accessors and mutators are in QPDF_Array.cc
770 770
771 QPDFObjectHandle::QPDFArrayItems 771 QPDFObjectHandle::QPDFArrayItems
772 QPDFObjectHandle::aitems() 772 QPDFObjectHandle::aitems()
@@ -774,202 +774,6 @@ QPDFObjectHandle::aitems() @@ -774,202 +774,6 @@ QPDFObjectHandle::aitems()
774 return *this; 774 return *this;
775 } 775 }
776 776
777 -int  
778 -QPDFObjectHandle::getArrayNItems() const  
779 -{  
780 - if (auto array = as_array(strict)) {  
781 - return array.size();  
782 - }  
783 - typeWarning("array", "treating as empty");  
784 - QTC::TC("qpdf", "QPDFObjectHandle array treating as empty");  
785 - return 0;  
786 -}  
787 -  
788 -QPDFObjectHandle  
789 -QPDFObjectHandle::getArrayItem(int n) const  
790 -{  
791 - if (auto array = as_array(strict)) {  
792 - if (auto const [success, oh] = array.at(n); success) {  
793 - return oh;  
794 - } else {  
795 - objectWarning("returning null for out of bounds array access");  
796 - QTC::TC("qpdf", "QPDFObjectHandle array bounds");  
797 - }  
798 - } else {  
799 - typeWarning("array", "returning null");  
800 - QTC::TC("qpdf", "QPDFObjectHandle array null for non-array");  
801 - }  
802 - static auto constexpr msg = " -> null returned from invalid array access"sv;  
803 - return QPDF_Null::create(obj, msg, "");  
804 -}  
805 -  
806 -bool  
807 -QPDFObjectHandle::isRectangle() const  
808 -{  
809 - if (auto array = as_array(strict)) {  
810 - for (int i = 0; i < 4; ++i) {  
811 - if (auto item = array.at(i).second; !item.isNumber()) {  
812 - return false;  
813 - }  
814 - }  
815 - return array.size() == 4;  
816 - }  
817 - return false;  
818 -}  
819 -  
820 -bool  
821 -QPDFObjectHandle::isMatrix() const  
822 -{  
823 - if (auto array = as_array(strict)) {  
824 - for (int i = 0; i < 6; ++i) {  
825 - if (auto item = array.at(i).second; !item.isNumber()) {  
826 - return false;  
827 - }  
828 - }  
829 - return array.size() == 6;  
830 - }  
831 - return false;  
832 -}  
833 -  
834 -QPDFObjectHandle::Rectangle  
835 -QPDFObjectHandle::getArrayAsRectangle() const  
836 -{  
837 - if (auto array = as_array(strict)) {  
838 - if (array.size() != 4) {  
839 - return {};  
840 - }  
841 - double items[4];  
842 - for (int i = 0; i < 4; ++i) {  
843 - if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) {  
844 - return {};  
845 - }  
846 - }  
847 - return {  
848 - std::min(items[0], items[2]),  
849 - std::min(items[1], items[3]),  
850 - std::max(items[0], items[2]),  
851 - std::max(items[1], items[3])};  
852 - }  
853 - return {};  
854 -}  
855 -  
856 -QPDFObjectHandle::Matrix  
857 -QPDFObjectHandle::getArrayAsMatrix() const  
858 -{  
859 - if (auto array = as_array(strict)) {  
860 - if (array.size() != 6) {  
861 - return {};  
862 - }  
863 - double items[6];  
864 - for (int i = 0; i < 6; ++i) {  
865 - if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) {  
866 - return {};  
867 - }  
868 - }  
869 - return {items[0], items[1], items[2], items[3], items[4], items[5]};  
870 - }  
871 - return {};  
872 -}  
873 -  
874 -std::vector<QPDFObjectHandle>  
875 -QPDFObjectHandle::getArrayAsVector() const  
876 -{  
877 - if (auto array = as_array(strict)) {  
878 - return array.getAsVector();  
879 - }  
880 - typeWarning("array", "treating as empty");  
881 - QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector");  
882 - return {};  
883 -}  
884 -  
885 -// Array mutators  
886 -  
887 -void  
888 -QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item)  
889 -{  
890 - if (auto array = as_array(strict)) {  
891 - if (!array.setAt(n, item)) {  
892 - objectWarning("ignoring attempt to set out of bounds array item");  
893 - QTC::TC("qpdf", "QPDFObjectHandle set array bounds");  
894 - }  
895 - } else {  
896 - typeWarning("array", "ignoring attempt to set item");  
897 - QTC::TC("qpdf", "QPDFObjectHandle array ignoring set item");  
898 - }  
899 -}  
900 -void  
901 -QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items)  
902 -{  
903 - if (auto array = as_array(strict)) {  
904 - array.setFromVector(items);  
905 - } else {  
906 - typeWarning("array", "ignoring attempt to replace items");  
907 - QTC::TC("qpdf", "QPDFObjectHandle array ignoring replace items");  
908 - }  
909 -}  
910 -  
911 -void  
912 -QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item)  
913 -{  
914 - if (auto array = as_array(strict)) {  
915 - if (!array.insert(at, item)) {  
916 - objectWarning("ignoring attempt to insert out of bounds array item");  
917 - QTC::TC("qpdf", "QPDFObjectHandle insert array bounds");  
918 - }  
919 - } else {  
920 - typeWarning("array", "ignoring attempt to insert item");  
921 - QTC::TC("qpdf", "QPDFObjectHandle array ignoring insert item");  
922 - }  
923 -}  
924 -  
925 -QPDFObjectHandle  
926 -QPDFObjectHandle::insertItemAndGetNew(int at, QPDFObjectHandle const& item)  
927 -{  
928 - insertItem(at, item);  
929 - return item;  
930 -}  
931 -  
932 -void  
933 -QPDFObjectHandle::appendItem(QPDFObjectHandle const& item)  
934 -{  
935 - if (auto array = as_array(strict)) {  
936 - array.push_back(item);  
937 - } else {  
938 - typeWarning("array", "ignoring attempt to append item");  
939 - QTC::TC("qpdf", "QPDFObjectHandle array ignoring append item");  
940 - }  
941 -}  
942 -  
943 -QPDFObjectHandle  
944 -QPDFObjectHandle::appendItemAndGetNew(QPDFObjectHandle const& item)  
945 -{  
946 - appendItem(item);  
947 - return item;  
948 -}  
949 -  
950 -void  
951 -QPDFObjectHandle::eraseItem(int at)  
952 -{  
953 - if (auto array = as_array(strict)) {  
954 - if (!array.erase(at)) {  
955 - objectWarning("ignoring attempt to erase out of bounds array item");  
956 - QTC::TC("qpdf", "QPDFObjectHandle erase array bounds");  
957 - }  
958 - } else {  
959 - typeWarning("array", "ignoring attempt to erase item");  
960 - QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item");  
961 - }  
962 -}  
963 -  
964 -QPDFObjectHandle  
965 -QPDFObjectHandle::eraseItemAndGetOld(int at)  
966 -{  
967 - auto array = as_array(strict);  
968 - auto result = (array && at < array.size() && at >= 0) ? array.at(at).second : newNull();  
969 - eraseItem(at);  
970 - return result;  
971 -}  
972 -  
973 // Dictionary accessors are in QPDF_Dictionary.cc 777 // Dictionary accessors are in QPDF_Dictionary.cc
974 778
975 QPDFObjectHandle::QPDFDictItems 779 QPDFObjectHandle::QPDFDictItems
libqpdf/QPDF_Array.cc
@@ -3,8 +3,10 @@ @@ -3,8 +3,10 @@
3 #include <qpdf/JSON_writer.hh> 3 #include <qpdf/JSON_writer.hh>
4 #include <qpdf/QPDFObjectHandle_private.hh> 4 #include <qpdf/QPDFObjectHandle_private.hh>
5 #include <qpdf/QPDFObject_private.hh> 5 #include <qpdf/QPDFObject_private.hh>
  6 +#include <qpdf/QPDF_Null.hh>
6 #include <qpdf/QTC.hh> 7 #include <qpdf/QTC.hh>
7 8
  9 +using namespace std::literals;
8 using namespace qpdf; 10 using namespace qpdf;
9 11
10 static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull(); 12 static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull();
@@ -365,3 +367,197 @@ Array::erase(int at) @@ -365,3 +367,197 @@ Array::erase(int at)
365 } 367 }
366 return true; 368 return true;
367 } 369 }
  370 +
  371 +int
  372 +QPDFObjectHandle::getArrayNItems() const
  373 +{
  374 + if (auto array = as_array(strict)) {
  375 + return array.size();
  376 + }
  377 + typeWarning("array", "treating as empty");
  378 + QTC::TC("qpdf", "QPDFObjectHandle array treating as empty");
  379 + return 0;
  380 +}
  381 +
  382 +QPDFObjectHandle
  383 +QPDFObjectHandle::getArrayItem(int n) const
  384 +{
  385 + if (auto array = as_array(strict)) {
  386 + if (auto const [success, oh] = array.at(n); success) {
  387 + return oh;
  388 + } else {
  389 + objectWarning("returning null for out of bounds array access");
  390 + QTC::TC("qpdf", "QPDFObjectHandle array bounds");
  391 + }
  392 + } else {
  393 + typeWarning("array", "returning null");
  394 + QTC::TC("qpdf", "QPDFObjectHandle array null for non-array");
  395 + }
  396 + static auto constexpr msg = " -> null returned from invalid array access"sv;
  397 + return QPDF_Null::create(obj, msg, "");
  398 +}
  399 +
  400 +bool
  401 +QPDFObjectHandle::isRectangle() const
  402 +{
  403 + if (auto array = as_array(strict)) {
  404 + for (int i = 0; i < 4; ++i) {
  405 + if (auto item = array.at(i).second; !item.isNumber()) {
  406 + return false;
  407 + }
  408 + }
  409 + return array.size() == 4;
  410 + }
  411 + return false;
  412 +}
  413 +
  414 +bool
  415 +QPDFObjectHandle::isMatrix() const
  416 +{
  417 + if (auto array = as_array(strict)) {
  418 + for (int i = 0; i < 6; ++i) {
  419 + if (auto item = array.at(i).second; !item.isNumber()) {
  420 + return false;
  421 + }
  422 + }
  423 + return array.size() == 6;
  424 + }
  425 + return false;
  426 +}
  427 +
  428 +QPDFObjectHandle::Rectangle
  429 +QPDFObjectHandle::getArrayAsRectangle() const
  430 +{
  431 + if (auto array = as_array(strict)) {
  432 + if (array.size() != 4) {
  433 + return {};
  434 + }
  435 + double items[4];
  436 + for (int i = 0; i < 4; ++i) {
  437 + if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) {
  438 + return {};
  439 + }
  440 + }
  441 + return {
  442 + std::min(items[0], items[2]),
  443 + std::min(items[1], items[3]),
  444 + std::max(items[0], items[2]),
  445 + std::max(items[1], items[3])};
  446 + }
  447 + return {};
  448 +}
  449 +
  450 +QPDFObjectHandle::Matrix
  451 +QPDFObjectHandle::getArrayAsMatrix() const
  452 +{
  453 + if (auto array = as_array(strict)) {
  454 + if (array.size() != 6) {
  455 + return {};
  456 + }
  457 + double items[6];
  458 + for (int i = 0; i < 6; ++i) {
  459 + if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) {
  460 + return {};
  461 + }
  462 + }
  463 + return {items[0], items[1], items[2], items[3], items[4], items[5]};
  464 + }
  465 + return {};
  466 +}
  467 +
  468 +std::vector<QPDFObjectHandle>
  469 +QPDFObjectHandle::getArrayAsVector() const
  470 +{
  471 + if (auto array = as_array(strict)) {
  472 + return array.getAsVector();
  473 + }
  474 + typeWarning("array", "treating as empty");
  475 + QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector");
  476 + return {};
  477 +}
  478 +
  479 +void
  480 +QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item)
  481 +{
  482 + if (auto array = as_array(strict)) {
  483 + if (!array.setAt(n, item)) {
  484 + objectWarning("ignoring attempt to set out of bounds array item");
  485 + QTC::TC("qpdf", "QPDFObjectHandle set array bounds");
  486 + }
  487 + } else {
  488 + typeWarning("array", "ignoring attempt to set item");
  489 + QTC::TC("qpdf", "QPDFObjectHandle array ignoring set item");
  490 + }
  491 +}
  492 +void
  493 +QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items)
  494 +{
  495 + if (auto array = as_array(strict)) {
  496 + array.setFromVector(items);
  497 + } else {
  498 + typeWarning("array", "ignoring attempt to replace items");
  499 + QTC::TC("qpdf", "QPDFObjectHandle array ignoring replace items");
  500 + }
  501 +}
  502 +
  503 +void
  504 +QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item)
  505 +{
  506 + if (auto array = as_array(strict)) {
  507 + if (!array.insert(at, item)) {
  508 + objectWarning("ignoring attempt to insert out of bounds array item");
  509 + QTC::TC("qpdf", "QPDFObjectHandle insert array bounds");
  510 + }
  511 + } else {
  512 + typeWarning("array", "ignoring attempt to insert item");
  513 + QTC::TC("qpdf", "QPDFObjectHandle array ignoring insert item");
  514 + }
  515 +}
  516 +
  517 +QPDFObjectHandle
  518 +QPDFObjectHandle::insertItemAndGetNew(int at, QPDFObjectHandle const& item)
  519 +{
  520 + insertItem(at, item);
  521 + return item;
  522 +}
  523 +
  524 +void
  525 +QPDFObjectHandle::appendItem(QPDFObjectHandle const& item)
  526 +{
  527 + if (auto array = as_array(strict)) {
  528 + array.push_back(item);
  529 + } else {
  530 + typeWarning("array", "ignoring attempt to append item");
  531 + QTC::TC("qpdf", "QPDFObjectHandle array ignoring append item");
  532 + }
  533 +}
  534 +
  535 +QPDFObjectHandle
  536 +QPDFObjectHandle::appendItemAndGetNew(QPDFObjectHandle const& item)
  537 +{
  538 + appendItem(item);
  539 + return item;
  540 +}
  541 +
  542 +void
  543 +QPDFObjectHandle::eraseItem(int at)
  544 +{
  545 + if (auto array = as_array(strict)) {
  546 + if (!array.erase(at)) {
  547 + objectWarning("ignoring attempt to erase out of bounds array item");
  548 + QTC::TC("qpdf", "QPDFObjectHandle erase array bounds");
  549 + }
  550 + } else {
  551 + typeWarning("array", "ignoring attempt to erase item");
  552 + QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item");
  553 + }
  554 +}
  555 +
  556 +QPDFObjectHandle
  557 +QPDFObjectHandle::eraseItemAndGetOld(int at)
  558 +{
  559 + auto array = as_array(strict);
  560 + auto result = (array && at < array.size() && at >= 0) ? array.at(at).second : newNull();
  561 + eraseItem(at);
  562 + return result;
  563 +}