File size: 3,482 Bytes
5cee033
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <memory>

#include <QtTest/QTest>
#include <QtCore/QDebug>
#include <QImage>

#include <poppler-qt6.h>

// Unit tests for rendering axial shadings without full opacity
class TestStrokeOpacity : public QObject
{
    Q_OBJECT
public:
    explicit TestStrokeOpacity(QObject *parent = nullptr) : QObject(parent) { }
private slots:
    void checkStrokeOpacity_data();
    void checkStrokeOpacity();
};

void TestStrokeOpacity::checkStrokeOpacity_data()
{
    QTest::addColumn<int>("backendType");

    QTest::newRow("splash") << (int)Poppler::Document::SplashBackend;
    QTest::newRow("qpainter") << (int)Poppler::Document::QPainterBackend;
}

void TestStrokeOpacity::checkStrokeOpacity()
{
    QFETCH(int, backendType);

    std::unique_ptr<Poppler::Document> doc = Poppler::Document::load(TESTDATADIR "/unittestcases/stroke-alpha-pattern.pdf");
    QVERIFY(doc != nullptr);

    doc->setRenderBackend((Poppler::Document::RenderBackend)backendType);

    // BUG: For some reason splash gets the opacity wrong when antialiasing is switched off
    if (backendType == (int)Poppler::Document::SplashBackend) {
        doc->setRenderHint(Poppler::Document::Antialiasing, true);
    }

    const auto page = std::unique_ptr<Poppler::Page>(doc->page(0));
    QVERIFY(page != nullptr);

    // Render (at low resolution and with cropped marging)
    QImage image = page->renderToImage(36, 36, 40, 50, 200, 230);

    // The actual tests start here

    // Allow a tolerance.
    int tolerance;
    auto approximatelyEqual = [&tolerance](QRgb c0, const QColor &c1) {
        return std::abs(qAlpha(c0) - c1.alpha()) <= tolerance && std::abs(qRed(c0) - c1.red()) <= tolerance && std::abs(qGreen(c0) - c1.green()) <= tolerance && std::abs(qBlue(c0) - c1.blue()) <= tolerance;
    };

    // At the lower left of the test document is a square with an axial shading,
    // which should be rendered with opacity 0.25.
    // Check that with a sample pixel
    auto pixel = image.pixel(70, 160);

    // Splash and QPainter backends implement shadings slightly differently,
    // hence we cannot expect to get precisely the same colors.
    tolerance = 2;
    QVERIFY(approximatelyEqual(pixel, QColor(253, 233, 196, 255)));

    // At the upper left of the test document is a stroked square with an axial shading.
    // This is implemented by filling a clip region defined by a stroke outline.
    // Check whether the backend really only renders the stroke, not the region
    // surrounded by the stroke.
    auto pixelUpperLeftInterior = image.pixel(70, 70);

    tolerance = 0;
    QVERIFY(approximatelyEqual(pixelUpperLeftInterior, Qt::white));

    // Now check whether that stroke is semi-transparent.
    // Bug https://gitlab.freedesktop.org/poppler/poppler/-/issues/178
    auto pixelUpperLeftOnStroke = image.pixel(70, 20);

    tolerance = 2;
    QVERIFY(approximatelyEqual(pixelUpperLeftOnStroke, QColor(253, 233, 196, 255)));

    // At the upper right there is a semi-transparent stroked red square
    // a) Make sure that the color is correct.
    auto pixelUpperRightOnStroke = image.pixel(130, 20);

    tolerance = 0;
    QVERIFY(approximatelyEqual(pixelUpperRightOnStroke, QColor(246, 196, 206, 255)));

    // b) Make sure that it is really stroked, not filled
    auto pixelUpperRightInterior = image.pixel(130, 50);
    QVERIFY(approximatelyEqual(pixelUpperRightInterior, Qt::white));
}

QTEST_GUILESS_MAIN(TestStrokeOpacity)

#include "check_stroke_opacity.moc"