diff options
-rw-r--r-- | libqpdf/QPDFAnnotationObjectHelper.cc | 21 | ||||
-rw-r--r-- | libqpdf/QPDFMatrix.cc | 46 | ||||
-rw-r--r-- | libqpdf/qpdf/QPDFMatrix.hh | 7 | ||||
-rw-r--r-- | libtests/matrix.cc | 34 |
4 files changed, 84 insertions, 24 deletions
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 <qpdf/QUtil.hh> #include <qpdf/QPDF.hh> #include <qpdf/QPDFNameTreeObjectHelper.hh> -#include <algorithm> 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<double> bx(4); - std::vector<double> 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 <qpdf/QPDFMatrix.hh> #include <qpdf/QUtil.hh> +#include <algorithm> 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<double> tx(4); + std::vector<double> 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; diff --git a/libtests/matrix.cc b/libtests/matrix.cc index f022ce4e..77a058ee 100644 --- a/libtests/matrix.cc +++ b/libtests/matrix.cc @@ -22,6 +22,25 @@ static void check_xy(double x, double y, std::string const& exp) } } +static void check_rect(QPDFObjectHandle::Rectangle const& r, + double llx, double lly, double urx, double ury) +{ + std::string actual = ( + QUtil::double_to_string(r.llx, 2) + " " + + QUtil::double_to_string(r.lly, 2) + " " + + QUtil::double_to_string(r.urx, 2) + " " + + QUtil::double_to_string(r.ury, 2)); + std::string wanted = ( + QUtil::double_to_string(llx, 2) + " " + + QUtil::double_to_string(lly, 2) + " " + + QUtil::double_to_string(urx, 2) + " " + + QUtil::double_to_string(ury, 2)); + if (actual != wanted) + { + std::cout << "got " << actual << ", wanted " << wanted << std::endl; + } +} + int main() { QPDFMatrix m; @@ -30,8 +49,14 @@ int main() check(m, "1.00000 0.00000 0.00000 1.00000 10.00000 20.00000"); m.scale(1.5, 2); check(m, "1.50000 0.00000 0.00000 2.00000 10.00000 20.00000"); + double xp = 0; + double yp = 0; + m.transform(10, 100, xp, yp); + check_xy(xp, yp, "25.00 220.00"); m.translate(30, 40); check(m, "1.50000 0.00000 0.00000 2.00000 55.00000 100.00000"); + m.transform(10, 100, xp, yp); + check_xy(xp, yp, "70.00 300.00"); m.concat(QPDFMatrix(1, 2, 3, 4, 5, 6)); check(m, "1.50000 4.00000 4.50000 8.00000 62.50000 112.00000"); m.rotatex90(90); @@ -45,8 +70,6 @@ int main() m.rotatex90(12345); check(m, "1.50000 4.00000 4.50000 8.00000 62.50000 112.00000"); - double xp = 0; - double yp = 0; m.transform(240, 480, xp, yp); check_xy(xp, yp, "2582.50 4912.00"); @@ -55,6 +78,13 @@ int main() "[3 1 4 1 5 9.26535]").getArrayAsMatrix()), "3.00000 1.00000 4.00000 1.00000 5.00000 9.26535"); + m = QPDFMatrix(); + m.rotatex90(90); + m.translate(200, -100); + check_rect( + m.transformRectangle(QPDFObjectHandle::Rectangle(10, 20, 30, 50)), + 50, 210, 80, 230); + std::cout << "matrix tests done" << std::endl; return 0; } |