From 76cd7ea67aee7edd1004a68a28e63a00a38239dd Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 6 Sep 2022 19:00:40 -0400 Subject: Clarify and improve QPDFPageObjectHelper::get*Box methods Add copy_if_fallback and explain how it differs from copy_if_shared. --- libqpdf/QPDFPageObjectHelper.cc | 77 ++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 16 deletions(-) (limited to 'libqpdf/QPDFPageObjectHelper.cc') diff --git a/libqpdf/QPDFPageObjectHelper.cc b/libqpdf/QPDFPageObjectHelper.cc index 23a54231..1c810318 100644 --- a/libqpdf/QPDFPageObjectHelper.cc +++ b/libqpdf/QPDFPageObjectHelper.cc @@ -237,6 +237,16 @@ QPDFPageObjectHelper::QPDFPageObjectHelper(QPDFObjectHandle oh) : QPDFObjectHandle QPDFPageObjectHelper::getAttribute(std::string const& name, bool copy_if_shared) +{ + return getAttribute(name, copy_if_shared, nullptr, false); +} + +QPDFObjectHandle +QPDFPageObjectHelper::getAttribute( + std::string const& name, + bool copy_if_shared, + std::function get_fallback, + bool copy_if_fallback) { QPDFObjectHandle result; QPDFObjectHandle dict; @@ -272,36 +282,71 @@ QPDFPageObjectHelper::getAttribute(std::string const& name, bool copy_if_shared) "qpdf", "QPDFPageObjectHelper copy shared attribute", is_form_xobject ? 0 : 1); - result = result.shallowCopy(); - dict.replaceKey(name, result); + result = dict.replaceKeyAndGetNew(name, result.shallowCopy()); + } + if (result.isNull() && get_fallback) { + result = get_fallback(); + if (copy_if_fallback && !result.isNull()) { + QTC::TC("qpdf", "QPDFPageObjectHelper copied fallback"); + result = dict.replaceKeyAndGetNew(name, result.shallowCopy()); + } else { + QTC::TC( + "qpdf", "QPDFPageObjectHelper used fallback without copying"); + } } return result; } QPDFObjectHandle -QPDFPageObjectHelper::getTrimBox(bool copy_if_shared) +QPDFPageObjectHelper::getMediaBox(bool copy_if_shared) { - QPDFObjectHandle result = getAttribute("/TrimBox", copy_if_shared); - if (result.isNull()) { - result = getCropBox(copy_if_shared); - } - return result; + return getAttribute("/MediaBox", copy_if_shared); } QPDFObjectHandle -QPDFPageObjectHelper::getCropBox(bool copy_if_shared) +QPDFPageObjectHelper::getCropBox(bool copy_if_shared, bool copy_if_fallback) { - QPDFObjectHandle result = getAttribute("/CropBox", copy_if_shared); - if (result.isNull()) { - result = getMediaBox(); - } - return result; + return getAttribute( + "/CropBox", + copy_if_shared, + [this, copy_if_shared]() { return this->getMediaBox(copy_if_shared); }, + copy_if_fallback); } QPDFObjectHandle -QPDFPageObjectHelper::getMediaBox(bool copy_if_shared) +QPDFPageObjectHelper::getTrimBox(bool copy_if_shared, bool copy_if_fallback) { - return getAttribute("/MediaBox", copy_if_shared); + return getAttribute( + "/TrimBox", + copy_if_shared, + [this, copy_if_shared, copy_if_fallback]() { + return this->getCropBox(copy_if_shared, copy_if_fallback); + }, + copy_if_fallback); +} + +QPDFObjectHandle +QPDFPageObjectHelper::getArtBox(bool copy_if_shared, bool copy_if_fallback) +{ + return getAttribute( + "/ArtBox", + copy_if_shared, + [this, copy_if_shared, copy_if_fallback]() { + return this->getCropBox(copy_if_shared, copy_if_fallback); + }, + copy_if_fallback); +} + +QPDFObjectHandle +QPDFPageObjectHelper::getBleedBox(bool copy_if_shared, bool copy_if_fallback) +{ + return getAttribute( + "/BleedBox", + copy_if_shared, + [this, copy_if_shared, copy_if_fallback]() { + return this->getCropBox(copy_if_shared, copy_if_fallback); + }, + copy_if_fallback); } void -- cgit v1.2.3-70-g09d2