Commit 53acbf15f867100b92f987f5d80e1e74ba978c75

Authored by m-holger
1 parent 1e8ce4a7

Refactor `QPDFObjectHandle::isRectangle` etc methods: replace `as_array` with `A…

…rray`, use subscript operators, simplify logic, and improve readability.
Showing 1 changed file with 31 additions and 38 deletions
libqpdf/QPDF_Array.cc
... ... @@ -2,6 +2,7 @@
2 2  
3 3 #include <qpdf/QTC.hh>
4 4  
  5 +#include <array>
5 6 #include <utility>
6 7  
7 8 using namespace std::literals;
... ... @@ -381,69 +382,61 @@ QPDFObjectHandle::getArrayItem(int n) const
381 382 bool
382 383 QPDFObjectHandle::isRectangle() const
383 384 {
384   - if (auto array = as_array(strict)) {
385   - for (int i = 0; i < 4; ++i) {
386   - if (auto item = array.at(i).second; !item.isNumber()) {
387   - return false;
388   - }
  385 + Array array(*this);
  386 + for (auto const& oh: array) {
  387 + if (!oh.isNumber()) {
  388 + return false;
389 389 }
390   - return array.size() == 4;
391 390 }
392   - return false;
  391 + return array.size() == 4;
393 392 }
394 393  
395 394 bool
396 395 QPDFObjectHandle::isMatrix() const
397 396 {
398   - if (auto array = as_array(strict)) {
399   - for (int i = 0; i < 6; ++i) {
400   - if (auto item = array.at(i).second; !item.isNumber()) {
401   - return false;
402   - }
  397 + Array array(*this);
  398 + for (auto const& oh: array) {
  399 + if (!oh.isNumber()) {
  400 + return false;
403 401 }
404   - return array.size() == 6;
405 402 }
406   - return false;
  403 + return array.size() == 6;
407 404 }
408 405  
409 406 QPDFObjectHandle::Rectangle
410 407 QPDFObjectHandle::getArrayAsRectangle() const
411 408 {
412   - if (auto array = as_array(strict)) {
413   - if (array.size() != 4) {
  409 + Array array(*this);
  410 + if (array.size() != 4) {
  411 + return {};
  412 + }
  413 + std::array<double, 4> items;
  414 + for (size_t i = 0; i < 4; ++i) {
  415 + if (!array[i].getValueAsNumber(items[i])) {
414 416 return {};
415 417 }
416   - double items[4];
417   - for (int i = 0; i < 4; ++i) {
418   - if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) {
419   - return {};
420   - }
421   - }
422   - return {
423   - std::min(items[0], items[2]),
424   - std::min(items[1], items[3]),
425   - std::max(items[0], items[2]),
426   - std::max(items[1], items[3])};
427 418 }
428   - return {};
  419 + return {
  420 + std::min(items[0], items[2]),
  421 + std::min(items[1], items[3]),
  422 + std::max(items[0], items[2]),
  423 + std::max(items[1], items[3])};
429 424 }
430 425  
431 426 QPDFObjectHandle::Matrix
432 427 QPDFObjectHandle::getArrayAsMatrix() const
433 428 {
434   - if (auto array = as_array(strict)) {
435   - if (array.size() != 6) {
  429 + Array array(*this);
  430 + if (array.size() != 6) {
  431 + return {};
  432 + }
  433 + std::array<double, 6> items;
  434 + for (size_t i = 0; i < 6; ++i) {
  435 + if (!array[i].getValueAsNumber(items[i])) {
436 436 return {};
437 437 }
438   - double items[6];
439   - for (int i = 0; i < 6; ++i) {
440   - if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) {
441   - return {};
442   - }
443   - }
444   - return {items[0], items[1], items[2], items[3], items[4], items[5]};
445 438 }
446   - return {};
  439 + return {items[0], items[1], items[2], items[3], items[4], items[5]};
447 440 }
448 441  
449 442 std::vector<QPDFObjectHandle>
... ...