From 4cded10821e3bd523cf96eb628d7364820a07b84 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 19 Jun 2018 11:00:15 -0400 Subject: Add QPDFObjectHandle::Rectangle type Provide a convenient way of accessing rectangles. --- libqpdf/QPDFObjectHandle.cc | 64 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) (limited to 'libqpdf/QPDFObjectHandle.cc') diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 149668eb..5c111cc8 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -554,6 +554,42 @@ QPDFObjectHandle::getArrayItem(int n) return result; } +bool +QPDFObjectHandle::isRectangle() +{ + if (! isArray()) + { + return false; + } + if (getArrayNItems() != 4) + { + return false; + } + for (size_t i = 0; i < 4; ++i) + { + if (! getArrayItem(i).isNumber()) + { + return false; + } + } + return true; +} + + +QPDFObjectHandle::Rectangle +QPDFObjectHandle::getArrayAsRectangle() +{ + Rectangle result; + if (isRectangle()) + { + result = Rectangle(getArrayItem(0).getNumericValue(), + getArrayItem(1).getNumericValue(), + getArrayItem(2).getNumericValue(), + getArrayItem(3).getNumericValue()); + } + return result; +} + std::vector QPDFObjectHandle::getArrayAsVector() { @@ -1833,6 +1869,23 @@ QPDFObjectHandle::newArray(std::vector const& items) return QPDFObjectHandle(new QPDF_Array(items)); } +QPDFObjectHandle +QPDFObjectHandle::newArray(Rectangle const& rect) +{ + std::vector items; + items.push_back(newReal(rect.llx)); + items.push_back(newReal(rect.lly)); + items.push_back(newReal(rect.urx)); + items.push_back(newReal(rect.ury)); + return newArray(items); +} + +QPDFObjectHandle +QPDFObjectHandle::newFromRectangle(Rectangle const& rect) +{ + return newArray(rect); +} + QPDFObjectHandle QPDFObjectHandle::newDictionary() { @@ -2103,7 +2156,8 @@ QPDFObjectHandle::typeWarning(char const* expected_type, } void -QPDFObjectHandle::objectWarning(std::string const& warning) +QPDFObjectHandle::warnIfPossible(std::string const& warning, + bool throw_if_no_description) { QPDF* context = 0; std::string description; @@ -2115,12 +2169,18 @@ QPDFObjectHandle::objectWarning(std::string const& warning) "", description, 0, warning)); } - else + else if (throw_if_no_description) { throw std::logic_error(warning); } } +void +QPDFObjectHandle::objectWarning(std::string const& warning) +{ + warnIfPossible(warning, true); +} + void QPDFObjectHandle::assertType(char const* type_name, bool istype) { -- cgit v1.2.3-54-g00ecf