Commit 68ccd87c9e950572e859eb5147453be2a528b944

Authored by Jay Berkenbilt
1 parent 8cb24573

Move rectangle transformation into QPDFMatrix

libqpdf/QPDFAnnotationObjectHelper.cc
... ... @@ -4,7 +4,6 @@
4 4 #include <qpdf/QUtil.hh>
5 5 #include <qpdf/QPDF.hh>
6 6 #include <qpdf/QPDFNameTreeObjectHelper.hh>
7   -#include <algorithm>
8 7  
9 8 QPDFAnnotationObjectHelper::Members::~Members()
10 9 {
... ... @@ -258,18 +257,8 @@ QPDFAnnotationObjectHelper::getPageContentForAppearance(
258 257  
259 258 // Transform bounding box by matrix to get T
260 259 QPDFObjectHandle::Rectangle bbox = bbox_obj.getArrayAsRectangle();
261   - std::vector<double> bx(4);
262   - std::vector<double> by(4);
263   - matrix.transform(bbox.llx, bbox.lly, bx.at(0), by.at(0));
264   - matrix.transform(bbox.llx, bbox.ury, bx.at(1), by.at(1));
265   - matrix.transform(bbox.urx, bbox.lly, bx.at(2), by.at(2));
266   - matrix.transform(bbox.urx, bbox.ury, bx.at(3), by.at(3));
267   - // Find the transformed appearance box
268   - double t_llx = *std::min_element(bx.begin(), bx.end());
269   - double t_urx = *std::max_element(bx.begin(), bx.end());
270   - double t_lly = *std::min_element(by.begin(), by.end());
271   - double t_ury = *std::max_element(by.begin(), by.end());
272   - if ((t_urx == t_llx) || (t_ury == t_lly))
  260 + QPDFObjectHandle::Rectangle T = matrix.transformRectangle(bbox);
  261 + if ((T.urx == T.llx) || (T.ury == T.lly))
273 262 {
274 263 // avoid division by zero
275 264 return "";
... ... @@ -277,9 +266,9 @@ QPDFAnnotationObjectHelper::getPageContentForAppearance(
277 266 // Compute a matrix to transform the appearance box to the rectangle
278 267 QPDFMatrix AA;
279 268 AA.translate(rect.llx, rect.lly);
280   - AA.scale((rect.urx - rect.llx) / (t_urx - t_llx),
281   - (rect.ury - rect.lly) / (t_ury - t_lly));
282   - AA.translate(-t_llx, -t_lly);
  269 + AA.scale((rect.urx - rect.llx) / (T.urx - T.llx),
  270 + (rect.ury - rect.lly) / (T.ury - T.lly));
  271 + AA.translate(-T.llx, -T.lly);
283 272 if (do_rotate)
284 273 {
285 274 AA.rotatex90(rotate);
... ...
libqpdf/QPDFMatrix.cc
1 1 #include <qpdf/QPDFMatrix.hh>
2 2 #include <qpdf/QUtil.hh>
  3 +#include <algorithm>
3 4  
4 5 QPDFMatrix::QPDFMatrix() :
5 6 a(1.0),
... ... @@ -32,16 +33,30 @@ QPDFMatrix::QPDFMatrix(QPDFObjectHandle::Matrix const&amp; m) :
32 33 {
33 34 }
34 35  
  36 +static double fix_rounding(double d)
  37 +{
  38 + if ((d > -0.00001) && (d < 0.00001))
  39 + {
  40 + d = 0.0;
  41 + }
  42 + return d;
  43 +}
35 44  
36 45 std::string
37 46 QPDFMatrix::unparse() const
38 47 {
39   - return (QUtil::double_to_string(a, 5) + " " +
40   - QUtil::double_to_string(b, 5) + " " +
41   - QUtil::double_to_string(c, 5) + " " +
42   - QUtil::double_to_string(d, 5) + " " +
43   - QUtil::double_to_string(e, 5) + " " +
44   - QUtil::double_to_string(f, 5));
  48 + return (QUtil::double_to_string(fix_rounding(a), 5) + " " +
  49 + QUtil::double_to_string(fix_rounding(b), 5) + " " +
  50 + QUtil::double_to_string(fix_rounding(c), 5) + " " +
  51 + QUtil::double_to_string(fix_rounding(d), 5) + " " +
  52 + QUtil::double_to_string(fix_rounding(e), 5) + " " +
  53 + QUtil::double_to_string(fix_rounding(f), 5));
  54 +}
  55 +
  56 +QPDFObjectHandle::Matrix
  57 +QPDFMatrix::getAsMatrix() const
  58 +{
  59 + return QPDFObjectHandle::Matrix(a, b, c, d, e, f);
45 60 }
46 61  
47 62 void
... ... @@ -99,3 +114,22 @@ QPDFMatrix::transform(double x, double y, double&amp; xp, double&amp; yp)
99 114 xp = (this->a * x) + (this->c * y) + this->e;
100 115 yp = (this->b * x) + (this->d * y) + this->f;
101 116 }
  117 +
  118 +QPDFObjectHandle::Rectangle
  119 +QPDFMatrix::transformRectangle(QPDFObjectHandle::Rectangle r)
  120 +{
  121 + // Transform a rectangle by creating a new rectangle the tightly
  122 + // bounds the polygon resulting from transforming the four
  123 + // corners.
  124 + std::vector<double> tx(4);
  125 + std::vector<double> ty(4);
  126 + transform(r.llx, r.lly, tx.at(0), ty.at(0));
  127 + transform(r.llx, r.ury, tx.at(1), ty.at(1));
  128 + transform(r.urx, r.lly, tx.at(2), ty.at(2));
  129 + transform(r.urx, r.ury, tx.at(3), ty.at(3));
  130 + return QPDFObjectHandle::Rectangle(
  131 + *std::min_element(tx.begin(), tx.end()),
  132 + *std::min_element(ty.begin(), ty.end()),
  133 + *std::max_element(tx.begin(), tx.end()),
  134 + *std::max_element(ty.begin(), ty.end()));
  135 +}
... ...
libqpdf/qpdf/QPDFMatrix.hh
... ... @@ -19,6 +19,9 @@ class QPDFMatrix
19 19 QPDF_DLL
20 20 std::string unparse() const;
21 21  
  22 + QPDF_DLL
  23 + QPDFObjectHandle::Matrix getAsMatrix() const;
  24 +
22 25 // This is not part of the public API. Just provide the methods we
23 26 // need as we need them.
24 27 QPDF_DLL
... ... @@ -34,6 +37,10 @@ class QPDFMatrix
34 37 QPDF_DLL
35 38 void transform(double x, double y, double& xp, double& yp);
36 39  
  40 + QPDF_DLL
  41 + QPDFObjectHandle::Rectangle transformRectangle(
  42 + QPDFObjectHandle::Rectangle r);
  43 +
37 44 private:
38 45 double a;
39 46 double b;
... ...
libtests/matrix.cc
... ... @@ -22,6 +22,25 @@ static void check_xy(double x, double y, std::string const&amp; exp)
22 22 }
23 23 }
24 24  
  25 +static void check_rect(QPDFObjectHandle::Rectangle const& r,
  26 + double llx, double lly, double urx, double ury)
  27 +{
  28 + std::string actual = (
  29 + QUtil::double_to_string(r.llx, 2) + " " +
  30 + QUtil::double_to_string(r.lly, 2) + " " +
  31 + QUtil::double_to_string(r.urx, 2) + " " +
  32 + QUtil::double_to_string(r.ury, 2));
  33 + std::string wanted = (
  34 + QUtil::double_to_string(llx, 2) + " " +
  35 + QUtil::double_to_string(lly, 2) + " " +
  36 + QUtil::double_to_string(urx, 2) + " " +
  37 + QUtil::double_to_string(ury, 2));
  38 + if (actual != wanted)
  39 + {
  40 + std::cout << "got " << actual << ", wanted " << wanted << std::endl;
  41 + }
  42 +}
  43 +
25 44 int main()
26 45 {
27 46 QPDFMatrix m;
... ... @@ -30,8 +49,14 @@ int main()
30 49 check(m, "1.00000 0.00000 0.00000 1.00000 10.00000 20.00000");
31 50 m.scale(1.5, 2);
32 51 check(m, "1.50000 0.00000 0.00000 2.00000 10.00000 20.00000");
  52 + double xp = 0;
  53 + double yp = 0;
  54 + m.transform(10, 100, xp, yp);
  55 + check_xy(xp, yp, "25.00 220.00");
33 56 m.translate(30, 40);
34 57 check(m, "1.50000 0.00000 0.00000 2.00000 55.00000 100.00000");
  58 + m.transform(10, 100, xp, yp);
  59 + check_xy(xp, yp, "70.00 300.00");
35 60 m.concat(QPDFMatrix(1, 2, 3, 4, 5, 6));
36 61 check(m, "1.50000 4.00000 4.50000 8.00000 62.50000 112.00000");
37 62 m.rotatex90(90);
... ... @@ -45,8 +70,6 @@ int main()
45 70 m.rotatex90(12345);
46 71 check(m, "1.50000 4.00000 4.50000 8.00000 62.50000 112.00000");
47 72  
48   - double xp = 0;
49   - double yp = 0;
50 73 m.transform(240, 480, xp, yp);
51 74 check_xy(xp, yp, "2582.50 4912.00");
52 75  
... ... @@ -55,6 +78,13 @@ int main()
55 78 "[3 1 4 1 5 9.26535]").getArrayAsMatrix()),
56 79 "3.00000 1.00000 4.00000 1.00000 5.00000 9.26535");
57 80  
  81 + m = QPDFMatrix();
  82 + m.rotatex90(90);
  83 + m.translate(200, -100);
  84 + check_rect(
  85 + m.transformRectangle(QPDFObjectHandle::Rectangle(10, 20, 30, 50)),
  86 + 50, 210, 80, 230);
  87 +
58 88 std::cout << "matrix tests done" << std::endl;
59 89 return 0;
60 90 }
... ...