aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDFObjectHandle.cc
diff options
context:
space:
mode:
authorm-holger <m-holger@kubitscheck.org>2023-03-24 16:01:40 +0100
committerm-holger <m-holger@kubitscheck.org>2023-04-01 14:54:17 +0200
commita1a8f35b63bcad7e0cac31a205e109d6a0d70622 (patch)
treee9a889ed70d94ea277111056d874a73dbc24a4e2 /libqpdf/QPDFObjectHandle.cc
parent51d350c98c549ff59dd6423e98d993385f57fa9c (diff)
downloadqpdf-a1a8f35b63bcad7e0cac31a205e109d6a0d70622.tar.zst
Refactor QPDF_Array::getItem and rename to at
Diffstat (limited to 'libqpdf/QPDFObjectHandle.cc')
-rw-r--r--libqpdf/QPDFObjectHandle.cc114
1 files changed, 56 insertions, 58 deletions
diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc
index 9fd8684b..e08c675d 100644
--- a/libqpdf/QPDFObjectHandle.cc
+++ b/libqpdf/QPDFObjectHandle.cc
@@ -801,90 +801,88 @@ QPDFObjectHandle::getArrayNItems()
QPDFObjectHandle
QPDFObjectHandle::getArrayItem(int n)
{
- auto array = asArray();
- if (array && n < array->size() && n >= 0) {
- return array->getItem(n);
- } else {
- if (array) {
+ if (auto array = asArray()) {
+ if (auto result = array->at(n); result.obj != nullptr) {
+ return result;
+ } 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, "");
+ } 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()
{
- auto array = asArray();
- if (array == nullptr || array->size() != 4) {
- return false;
- }
- for (int i = 0; i < 4; ++i) {
- if (!array->getItem(i).isNumber()) {
- return false;
+ if (auto array = asArray()) {
+ for (int i = 0; i < 4; ++i) {
+ if (auto item = array->at(i); !(item.obj && item.isNumber())) {
+ return false;
+ }
}
+ return array->size() == 4;
}
- return true;
+ return false;
}
bool
QPDFObjectHandle::isMatrix()
{
- auto array = asArray();
- if (array == nullptr || array->size() != 6) {
- return false;
- }
- for (int i = 0; i < 6; ++i) {
- if (!array->getItem(i).isNumber()) {
- return false;
+ if (auto array = asArray()) {
+ for (int i = 0; i < 6; ++i) {
+ if (auto item = array->at(i); !(item.obj && item.isNumber())) {
+ return false;
+ }
}
+ return array->size() == 6;
}
- return true;
+ return false;
}
QPDFObjectHandle::Rectangle
QPDFObjectHandle::getArrayAsRectangle()
{
- Rectangle result;
- if (isRectangle()) {
- auto array = asArray();
- // Rectangle coordinates are always supposed to be llx, lly,
- // urx, ury, but files have been found in the wild where
- // llx > urx or lly > ury.
- double i0 = array->getItem(0).getNumericValue();
- double i1 = array->getItem(1).getNumericValue();
- double i2 = array->getItem(2).getNumericValue();
- double i3 = array->getItem(3).getNumericValue();
- result = Rectangle(
- std::min(i0, i2),
- std::min(i1, i3),
- std::max(i0, i2),
- std::max(i1, i3));
+ if (auto array = asArray()) {
+ if (array->size() != 4) {
+ return {};
+ }
+ double items[4];
+ for (int i = 0; i < 4; ++i) {
+ if (!array->at(i).getValueAsNumber(items[i])) {
+ return {};
+ }
+ }
+ return Rectangle(
+ 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 result;
+ return {};
}
QPDFObjectHandle::Matrix
QPDFObjectHandle::getArrayAsMatrix()
{
- Matrix result;
- if (isMatrix()) {
- auto array = asArray();
- result = Matrix(
- array->getItem(0).getNumericValue(),
- array->getItem(1).getNumericValue(),
- array->getItem(2).getNumericValue(),
- array->getItem(3).getNumericValue(),
- array->getItem(4).getNumericValue(),
- array->getItem(5).getNumericValue());
+ if (auto array = asArray()) {
+ if (array->size() != 6) {
+ return {};
+ }
+ double items[6];
+ for (int i = 0; i < 6; ++i) {
+ if (!array->at(i).getValueAsNumber(items[i])) {
+ return {};
+ }
+ }
+ return Matrix(
+ items[0], items[1], items[2], items[3], items[4], items[5]);
}
- return result;
+ return {};
}
std::vector<QPDFObjectHandle>
@@ -991,8 +989,8 @@ QPDFObjectHandle
QPDFObjectHandle::eraseItemAndGetOld(int at)
{
auto array = asArray();
- auto result = (array && at < array->size() && at >= 0) ? array->getItem(at)
- : newNull();
+ auto result =
+ (array && at < array->size() && at >= 0) ? array->at(at) : newNull();
eraseItem(at);
return result;
}
@@ -1515,7 +1513,7 @@ QPDFObjectHandle::arrayOrStreamToStreamArray(
if (auto array = asArray()) {
int n_items = array->size();
for (int i = 0; i < n_items; ++i) {
- QPDFObjectHandle item = array->getItem(i);
+ QPDFObjectHandle item = array->at(i);
if (item.isStream()) {
result.push_back(item);
} else {
@@ -2215,7 +2213,7 @@ QPDFObjectHandle::makeDirect(
auto array = asArray();
int n = array->size();
for (int i = 0; i < n; ++i) {
- items.push_back(array->getItem(i));
+ items.push_back(array->at(i));
items.back().makeDirect(visited, stop_at_streams);
}
this->obj = QPDF_Array::create(items);