summaryrefslogtreecommitdiffstats
path: root/libtests/matrix.cc
blob: f69d270c4af653ca249f5d8343a108d88d90340f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <qpdf/assert_test.h>

#include <qpdf/QPDFMatrix.hh>
#include <qpdf/QUtil.hh>
#include <iostream>

static void
check(QPDFMatrix const& m, std::string const& exp)
{
    std::string u = m.unparse();
    if (u != exp) {
        std::cout << "got " << u << ", wanted " << exp << std::endl;
    }
}

static void
check_xy(double x, double y, std::string const& exp)
{
    std::string u =
        (QUtil::double_to_string(x, 2) + " " + QUtil::double_to_string(y, 2));
    if (u != exp) {
        std::cout << "got " << u << ", wanted " << exp << std::endl;
    }
}

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;
    check(m, "1 0 0 1 0 0");
    m.translate(10, 20);
    check(m, "1 0 0 1 10 20");
    m.scale(1.5, 2);
    check(m, "1.5 0 0 2 10 20");
    double xp = 0;
    double yp = 0;
    m.transform(10, 100, xp, yp);
    check_xy(xp, yp, "25 220");
    m.translate(30, 40);
    check(m, "1.5 0 0 2 55 100");
    m.transform(10, 100, xp, yp);
    check_xy(xp, yp, "70 300");
    m.concat(QPDFMatrix(1, 2, 3, 4, 5, 6));
    check(m, "1.5 4 4.5 8 62.5 112");
    m.rotatex90(90);
    check(m, "4.5 8 -1.5 -4 62.5 112");
    m.rotatex90(180);
    check(m, "-4.5 -8 1.5 4 62.5 112");
    m.rotatex90(270);
    check(m, "-1.5 -4 -4.5 -8 62.5 112");
    m.rotatex90(180);
    check(m, "1.5 4 4.5 8 62.5 112");
    m.rotatex90(12345);
    check(m, "1.5 4 4.5 8 62.5 112");

    m.transform(240, 480, xp, yp);
    check_xy(xp, yp, "2582.5 4912");

    check(
        QPDFMatrix(
            QPDFObjectHandle::parse("[3 1 4 1 5 9.26535]").getArrayAsMatrix()),
        "3 1 4 1 5 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;
}