From 68ccd87c9e950572e859eb5147453be2a528b944 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 26 Jan 2019 16:48:41 -0500 Subject: Move rectangle transformation into QPDFMatrix --- libqpdf/QPDFAnnotationObjectHelper.cc | 21 ++++------------ libqpdf/QPDFMatrix.cc | 46 ++++++++++++++++++++++++++++++----- libqpdf/qpdf/QPDFMatrix.hh | 7 ++++++ 3 files changed, 52 insertions(+), 22 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/QPDFAnnotationObjectHelper.cc b/libqpdf/QPDFAnnotationObjectHelper.cc index 5e4be795..698f80e1 100644 --- a/libqpdf/QPDFAnnotationObjectHelper.cc +++ b/libqpdf/QPDFAnnotationObjectHelper.cc @@ -4,7 +4,6 @@ #include #include #include -#include QPDFAnnotationObjectHelper::Members::~Members() { @@ -258,18 +257,8 @@ QPDFAnnotationObjectHelper::getPageContentForAppearance( // Transform bounding box by matrix to get T QPDFObjectHandle::Rectangle bbox = bbox_obj.getArrayAsRectangle(); - std::vector bx(4); - std::vector by(4); - matrix.transform(bbox.llx, bbox.lly, bx.at(0), by.at(0)); - matrix.transform(bbox.llx, bbox.ury, bx.at(1), by.at(1)); - matrix.transform(bbox.urx, bbox.lly, bx.at(2), by.at(2)); - matrix.transform(bbox.urx, bbox.ury, bx.at(3), by.at(3)); - // Find the transformed appearance box - double t_llx = *std::min_element(bx.begin(), bx.end()); - double t_urx = *std::max_element(bx.begin(), bx.end()); - double t_lly = *std::min_element(by.begin(), by.end()); - double t_ury = *std::max_element(by.begin(), by.end()); - if ((t_urx == t_llx) || (t_ury == t_lly)) + QPDFObjectHandle::Rectangle T = matrix.transformRectangle(bbox); + if ((T.urx == T.llx) || (T.ury == T.lly)) { // avoid division by zero return ""; @@ -277,9 +266,9 @@ QPDFAnnotationObjectHelper::getPageContentForAppearance( // Compute a matrix to transform the appearance box to the rectangle QPDFMatrix AA; AA.translate(rect.llx, rect.lly); - AA.scale((rect.urx - rect.llx) / (t_urx - t_llx), - (rect.ury - rect.lly) / (t_ury - t_lly)); - AA.translate(-t_llx, -t_lly); + AA.scale((rect.urx - rect.llx) / (T.urx - T.llx), + (rect.ury - rect.lly) / (T.ury - T.lly)); + AA.translate(-T.llx, -T.lly); if (do_rotate) { AA.rotatex90(rotate); diff --git a/libqpdf/QPDFMatrix.cc b/libqpdf/QPDFMatrix.cc index 3d410435..c78154aa 100644 --- a/libqpdf/QPDFMatrix.cc +++ b/libqpdf/QPDFMatrix.cc @@ -1,5 +1,6 @@ #include #include +#include QPDFMatrix::QPDFMatrix() : a(1.0), @@ -32,16 +33,30 @@ QPDFMatrix::QPDFMatrix(QPDFObjectHandle::Matrix const& m) : { } +static double fix_rounding(double d) +{ + if ((d > -0.00001) && (d < 0.00001)) + { + d = 0.0; + } + return d; +} std::string QPDFMatrix::unparse() const { - return (QUtil::double_to_string(a, 5) + " " + - QUtil::double_to_string(b, 5) + " " + - QUtil::double_to_string(c, 5) + " " + - QUtil::double_to_string(d, 5) + " " + - QUtil::double_to_string(e, 5) + " " + - QUtil::double_to_string(f, 5)); + return (QUtil::double_to_string(fix_rounding(a), 5) + " " + + QUtil::double_to_string(fix_rounding(b), 5) + " " + + QUtil::double_to_string(fix_rounding(c), 5) + " " + + QUtil::double_to_string(fix_rounding(d), 5) + " " + + QUtil::double_to_string(fix_rounding(e), 5) + " " + + QUtil::double_to_string(fix_rounding(f), 5)); +} + +QPDFObjectHandle::Matrix +QPDFMatrix::getAsMatrix() const +{ + return QPDFObjectHandle::Matrix(a, b, c, d, e, f); } void @@ -99,3 +114,22 @@ QPDFMatrix::transform(double x, double y, double& xp, double& yp) xp = (this->a * x) + (this->c * y) + this->e; yp = (this->b * x) + (this->d * y) + this->f; } + +QPDFObjectHandle::Rectangle +QPDFMatrix::transformRectangle(QPDFObjectHandle::Rectangle r) +{ + // Transform a rectangle by creating a new rectangle the tightly + // bounds the polygon resulting from transforming the four + // corners. + std::vector tx(4); + std::vector ty(4); + transform(r.llx, r.lly, tx.at(0), ty.at(0)); + transform(r.llx, r.ury, tx.at(1), ty.at(1)); + transform(r.urx, r.lly, tx.at(2), ty.at(2)); + transform(r.urx, r.ury, tx.at(3), ty.at(3)); + return QPDFObjectHandle::Rectangle( + *std::min_element(tx.begin(), tx.end()), + *std::min_element(ty.begin(), ty.end()), + *std::max_element(tx.begin(), tx.end()), + *std::max_element(ty.begin(), ty.end())); +} diff --git a/libqpdf/qpdf/QPDFMatrix.hh b/libqpdf/qpdf/QPDFMatrix.hh index 81bc51f1..7b19a665 100644 --- a/libqpdf/qpdf/QPDFMatrix.hh +++ b/libqpdf/qpdf/QPDFMatrix.hh @@ -19,6 +19,9 @@ class QPDFMatrix QPDF_DLL std::string unparse() const; + QPDF_DLL + QPDFObjectHandle::Matrix getAsMatrix() const; + // This is not part of the public API. Just provide the methods we // need as we need them. QPDF_DLL @@ -34,6 +37,10 @@ class QPDFMatrix QPDF_DLL void transform(double x, double y, double& xp, double& yp); + QPDF_DLL + QPDFObjectHandle::Rectangle transformRectangle( + QPDFObjectHandle::Rectangle r); + private: double a; double b; -- cgit v1.2.3-54-g00ecf