From cfa2eb97fb9b38d62fd2cd8d54ab59bac503967f Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 12 Aug 2017 13:22:46 -0400 Subject: Add page rotation (fixes #132) --- libqpdf/QPDFObjectHandle.cc | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'libqpdf') diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index e263a3ec..a8a7e5a7 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -673,6 +673,62 @@ QPDFObjectHandle::addPageContents(QPDFObjectHandle new_contents, bool first) this->replaceKey("/Contents", contents); } +void +QPDFObjectHandle::rotatePage(int angle, bool relative) +{ + assertPageObject(); + if ((angle % 90) != 0) + { + throw std::runtime_error( + "QPDF::rotatePage called with an" + " angle that is not a multiple of 90"); + } + int new_angle = angle; + if (relative) + { + int old_angle = 0; + bool found_rotate = false; + QPDFObjectHandle cur_obj = *this; + bool searched_parent = false; + std::set visited; + while (! found_rotate) + { + if (visited.count(cur_obj.getObjGen())) + { + // Don't get stuck in an infinite loop + break; + } + if (! visited.empty()) + { + searched_parent = true; + } + visited.insert(cur_obj.getObjGen()); + if (cur_obj.getKey("/Rotate").isInteger()) + { + found_rotate = true; + old_angle = cur_obj.getKey("/Rotate").getIntValue(); + } + else if (cur_obj.getKey("/Parent").isDictionary()) + { + cur_obj = cur_obj.getKey("/Parent"); + } + else + { + break; + } + } + QTC::TC("qpdf", "QPDFObjectHandle found old angle", + searched_parent ? 0 : 1); + if ((old_angle % 90) != 0) + { + old_angle = 0; + } + new_angle += old_angle; + } + new_angle = (new_angle + 360) % 360; + replaceKey("/Rotate", QPDFObjectHandle::newInteger(new_angle)); +} + std::string QPDFObjectHandle::unparse() { -- cgit v1.2.3-54-g00ecf