From 796913e9b64db041a30c2c247eca9f2e75790bce Mon Sep 17 00:00:00 2001 From: m-holger Date: Sat, 15 Feb 2025 13:35:55 +0000 Subject: [PATCH] Move QPDFObjectHandle array methods implementation to QPDF_Array.cc --- libqpdf/QPDFObjectHandle.cc | 198 +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- libqpdf/QPDF_Array.cc | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+), 197 deletions(-) diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 125d0e5..38bdad7 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -766,7 +766,7 @@ QPDFObjectHandle::getValueAsInlineImage(std::string& value) const return true; } -// Array accessors +// Array accessors and mutators are in QPDF_Array.cc QPDFObjectHandle::QPDFArrayItems QPDFObjectHandle::aitems() @@ -774,202 +774,6 @@ QPDFObjectHandle::aitems() return *this; } -int -QPDFObjectHandle::getArrayNItems() const -{ - if (auto array = as_array(strict)) { - return array.size(); - } - typeWarning("array", "treating as empty"); - QTC::TC("qpdf", "QPDFObjectHandle array treating as empty"); - return 0; -} - -QPDFObjectHandle -QPDFObjectHandle::getArrayItem(int n) const -{ - if (auto array = as_array(strict)) { - if (auto const [success, oh] = array.at(n); success) { - return oh; - } else { - objectWarning("returning null for out of bounds array access"); - QTC::TC("qpdf", "QPDFObjectHandle array bounds"); - } - } else { - typeWarning("array", "returning null"); - QTC::TC("qpdf", "QPDFObjectHandle array null for non-array"); - } - static auto constexpr msg = " -> null returned from invalid array access"sv; - return QPDF_Null::create(obj, msg, ""); -} - -bool -QPDFObjectHandle::isRectangle() const -{ - if (auto array = as_array(strict)) { - for (int i = 0; i < 4; ++i) { - if (auto item = array.at(i).second; !item.isNumber()) { - return false; - } - } - return array.size() == 4; - } - return false; -} - -bool -QPDFObjectHandle::isMatrix() const -{ - if (auto array = as_array(strict)) { - for (int i = 0; i < 6; ++i) { - if (auto item = array.at(i).second; !item.isNumber()) { - return false; - } - } - return array.size() == 6; - } - return false; -} - -QPDFObjectHandle::Rectangle -QPDFObjectHandle::getArrayAsRectangle() const -{ - if (auto array = as_array(strict)) { - if (array.size() != 4) { - return {}; - } - double items[4]; - for (int i = 0; i < 4; ++i) { - if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) { - return {}; - } - } - return { - std::min(items[0], items[2]), - std::min(items[1], items[3]), - std::max(items[0], items[2]), - std::max(items[1], items[3])}; - } - return {}; -} - -QPDFObjectHandle::Matrix -QPDFObjectHandle::getArrayAsMatrix() const -{ - if (auto array = as_array(strict)) { - if (array.size() != 6) { - return {}; - } - double items[6]; - for (int i = 0; i < 6; ++i) { - if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) { - return {}; - } - } - return {items[0], items[1], items[2], items[3], items[4], items[5]}; - } - return {}; -} - -std::vector -QPDFObjectHandle::getArrayAsVector() const -{ - if (auto array = as_array(strict)) { - return array.getAsVector(); - } - typeWarning("array", "treating as empty"); - QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector"); - return {}; -} - -// Array mutators - -void -QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item) -{ - if (auto array = as_array(strict)) { - if (!array.setAt(n, item)) { - objectWarning("ignoring attempt to set out of bounds array item"); - QTC::TC("qpdf", "QPDFObjectHandle set array bounds"); - } - } else { - typeWarning("array", "ignoring attempt to set item"); - QTC::TC("qpdf", "QPDFObjectHandle array ignoring set item"); - } -} -void -QPDFObjectHandle::setArrayFromVector(std::vector const& items) -{ - if (auto array = as_array(strict)) { - array.setFromVector(items); - } else { - typeWarning("array", "ignoring attempt to replace items"); - QTC::TC("qpdf", "QPDFObjectHandle array ignoring replace items"); - } -} - -void -QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item) -{ - if (auto array = as_array(strict)) { - if (!array.insert(at, item)) { - objectWarning("ignoring attempt to insert out of bounds array item"); - QTC::TC("qpdf", "QPDFObjectHandle insert array bounds"); - } - } else { - typeWarning("array", "ignoring attempt to insert item"); - QTC::TC("qpdf", "QPDFObjectHandle array ignoring insert item"); - } -} - -QPDFObjectHandle -QPDFObjectHandle::insertItemAndGetNew(int at, QPDFObjectHandle const& item) -{ - insertItem(at, item); - return item; -} - -void -QPDFObjectHandle::appendItem(QPDFObjectHandle const& item) -{ - if (auto array = as_array(strict)) { - array.push_back(item); - } else { - typeWarning("array", "ignoring attempt to append item"); - QTC::TC("qpdf", "QPDFObjectHandle array ignoring append item"); - } -} - -QPDFObjectHandle -QPDFObjectHandle::appendItemAndGetNew(QPDFObjectHandle const& item) -{ - appendItem(item); - return item; -} - -void -QPDFObjectHandle::eraseItem(int at) -{ - if (auto array = as_array(strict)) { - if (!array.erase(at)) { - objectWarning("ignoring attempt to erase out of bounds array item"); - QTC::TC("qpdf", "QPDFObjectHandle erase array bounds"); - } - } else { - typeWarning("array", "ignoring attempt to erase item"); - QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item"); - } -} - -QPDFObjectHandle -QPDFObjectHandle::eraseItemAndGetOld(int at) -{ - auto array = as_array(strict); - auto result = (array && at < array.size() && at >= 0) ? array.at(at).second : newNull(); - eraseItem(at); - return result; -} - // Dictionary accessors are in QPDF_Dictionary.cc QPDFObjectHandle::QPDFDictItems diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index c37b517..c29fd28 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -3,8 +3,10 @@ #include #include #include +#include #include +using namespace std::literals; using namespace qpdf; static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull(); @@ -365,3 +367,197 @@ Array::erase(int at) } return true; } + +int +QPDFObjectHandle::getArrayNItems() const +{ + if (auto array = as_array(strict)) { + return array.size(); + } + typeWarning("array", "treating as empty"); + QTC::TC("qpdf", "QPDFObjectHandle array treating as empty"); + return 0; +} + +QPDFObjectHandle +QPDFObjectHandle::getArrayItem(int n) const +{ + if (auto array = as_array(strict)) { + if (auto const [success, oh] = array.at(n); success) { + return oh; + } else { + objectWarning("returning null for out of bounds array access"); + QTC::TC("qpdf", "QPDFObjectHandle array bounds"); + } + } else { + typeWarning("array", "returning null"); + QTC::TC("qpdf", "QPDFObjectHandle array null for non-array"); + } + static auto constexpr msg = " -> null returned from invalid array access"sv; + return QPDF_Null::create(obj, msg, ""); +} + +bool +QPDFObjectHandle::isRectangle() const +{ + if (auto array = as_array(strict)) { + for (int i = 0; i < 4; ++i) { + if (auto item = array.at(i).second; !item.isNumber()) { + return false; + } + } + return array.size() == 4; + } + return false; +} + +bool +QPDFObjectHandle::isMatrix() const +{ + if (auto array = as_array(strict)) { + for (int i = 0; i < 6; ++i) { + if (auto item = array.at(i).second; !item.isNumber()) { + return false; + } + } + return array.size() == 6; + } + return false; +} + +QPDFObjectHandle::Rectangle +QPDFObjectHandle::getArrayAsRectangle() const +{ + if (auto array = as_array(strict)) { + if (array.size() != 4) { + return {}; + } + double items[4]; + for (int i = 0; i < 4; ++i) { + if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) { + return {}; + } + } + return { + std::min(items[0], items[2]), + std::min(items[1], items[3]), + std::max(items[0], items[2]), + std::max(items[1], items[3])}; + } + return {}; +} + +QPDFObjectHandle::Matrix +QPDFObjectHandle::getArrayAsMatrix() const +{ + if (auto array = as_array(strict)) { + if (array.size() != 6) { + return {}; + } + double items[6]; + for (int i = 0; i < 6; ++i) { + if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) { + return {}; + } + } + return {items[0], items[1], items[2], items[3], items[4], items[5]}; + } + return {}; +} + +std::vector +QPDFObjectHandle::getArrayAsVector() const +{ + if (auto array = as_array(strict)) { + return array.getAsVector(); + } + typeWarning("array", "treating as empty"); + QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector"); + return {}; +} + +void +QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item) +{ + if (auto array = as_array(strict)) { + if (!array.setAt(n, item)) { + objectWarning("ignoring attempt to set out of bounds array item"); + QTC::TC("qpdf", "QPDFObjectHandle set array bounds"); + } + } else { + typeWarning("array", "ignoring attempt to set item"); + QTC::TC("qpdf", "QPDFObjectHandle array ignoring set item"); + } +} +void +QPDFObjectHandle::setArrayFromVector(std::vector const& items) +{ + if (auto array = as_array(strict)) { + array.setFromVector(items); + } else { + typeWarning("array", "ignoring attempt to replace items"); + QTC::TC("qpdf", "QPDFObjectHandle array ignoring replace items"); + } +} + +void +QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item) +{ + if (auto array = as_array(strict)) { + if (!array.insert(at, item)) { + objectWarning("ignoring attempt to insert out of bounds array item"); + QTC::TC("qpdf", "QPDFObjectHandle insert array bounds"); + } + } else { + typeWarning("array", "ignoring attempt to insert item"); + QTC::TC("qpdf", "QPDFObjectHandle array ignoring insert item"); + } +} + +QPDFObjectHandle +QPDFObjectHandle::insertItemAndGetNew(int at, QPDFObjectHandle const& item) +{ + insertItem(at, item); + return item; +} + +void +QPDFObjectHandle::appendItem(QPDFObjectHandle const& item) +{ + if (auto array = as_array(strict)) { + array.push_back(item); + } else { + typeWarning("array", "ignoring attempt to append item"); + QTC::TC("qpdf", "QPDFObjectHandle array ignoring append item"); + } +} + +QPDFObjectHandle +QPDFObjectHandle::appendItemAndGetNew(QPDFObjectHandle const& item) +{ + appendItem(item); + return item; +} + +void +QPDFObjectHandle::eraseItem(int at) +{ + if (auto array = as_array(strict)) { + if (!array.erase(at)) { + objectWarning("ignoring attempt to erase out of bounds array item"); + QTC::TC("qpdf", "QPDFObjectHandle erase array bounds"); + } + } else { + typeWarning("array", "ignoring attempt to erase item"); + QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item"); + } +} + +QPDFObjectHandle +QPDFObjectHandle::eraseItemAndGetOld(int at) +{ + auto array = as_array(strict); + auto result = (array && at < array.size() && at >= 0) ? array.at(at).second : newNull(); + eraseItem(at); + return result; +} -- libgit2 0.21.4