summaryrefslogtreecommitdiffstats
path: root/canvas/qa/cppunit/canvastest.cxx
blob: 7927f07c0723e4ed566cfd1a5ed56249aa8abdad (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#include <test/bootstrapfixture.hxx>

#include <vcl/virdev.hxx>
#include <vcl/BitmapReadAccess.hxx>
#include <vcl/canvastools.hxx>
#include <vcl/graphicfilter.hxx>
#include <tools/stream.hxx>

#include <com/sun/star/rendering/XCanvas.hpp>
#include <com/sun/star/rendering/CompositeOperation.hpp>
#include <com/sun/star/rendering/PathCapType.hpp>
#include <com/sun/star/rendering/PathJoinType.hpp>

using namespace ::com::sun::star;

class CanvasTest : public test::BootstrapFixture
{
    VclPtr<VirtualDevice> mVclDevice;
    uno::Reference<rendering::XCanvas> mCanvas;
    uno::Reference<rendering::XGraphicDevice> mDevice;
    rendering::ViewState mViewState;
    rendering::RenderState mRenderState;
    uno::Sequence<double> mColorBlack;
    uno::Sequence<double> mColorBlue;

    // if enabled - check the result images with:
    // "xdg-open ./workdir/CppunitTest/canvas_test.test.core/"
    static constexpr const bool mbExportBitmap = false;

    void exportDevice(const OUString& filename, const VclPtr<VirtualDevice>& device)
    {
        if (mbExportBitmap)
        {
            BitmapEx aBitmapEx(device->GetBitmapEx(Point(0, 0), device->GetOutputSizePixel()));
            SvFileStream aStream(filename, StreamMode::WRITE | StreamMode::TRUNC);
            GraphicFilter::GetGraphicFilter().compressAsPNG(aBitmapEx, aStream);
        }
    }

public:
    CanvasTest()
        : BootstrapFixture(true, false)
    {
    }

    virtual void setUp() override
    {
        BootstrapFixture::setUp();
        mColorBlack = vcl::unotools::colorToStdColorSpaceSequence(COL_BLACK);
        mColorBlue = vcl::unotools::colorToStdColorSpaceSequence(COL_BLUE);
        // Geometry init
        geometry::AffineMatrix2D aUnit(1, 0, 0, 0, 1, 0);
        mViewState.AffineTransform = aUnit;
        mRenderState.AffineTransform = aUnit;
        mRenderState.DeviceColor = mColorBlack;
        mRenderState.CompositeOperation = rendering::CompositeOperation::OVER;
    }

    virtual void tearDown() override
    {
        mVclDevice.disposeAndClear();
        mCanvas = uno::Reference<rendering::XCanvas>();
        mDevice = uno::Reference<rendering::XGraphicDevice>();
        BootstrapFixture::tearDown();
    }

    void setupCanvas(const Size& size, Color backgroundColor = COL_WHITE, bool alpha = false)
    {
        mVclDevice
            = alpha ? VclPtr<VirtualDevice>::Create(DeviceFormat::DEFAULT, DeviceFormat::DEFAULT)
                    : VclPtr<VirtualDevice>::Create(DeviceFormat::DEFAULT);
        mVclDevice->SetOutputSizePixel(size);
        mVclDevice->SetBackground(Wallpaper(backgroundColor));
        mVclDevice->Erase();
        mCanvas = mVclDevice->GetCanvas();
        CPPUNIT_ASSERT(mCanvas.is());
        mDevice
            = uno::Reference<rendering::XGraphicDevice>(mCanvas->getDevice(), uno::UNO_SET_THROW);
        CPPUNIT_ASSERT(mDevice.is());
    }

    void testDrawLine()
    {
        setupCanvas(Size(10, 10));
        mCanvas->drawLine(geometry::RealPoint2D(1, 1), geometry::RealPoint2D(9, 1), mViewState,
                          mRenderState);
        exportDevice("test-draw-line.png", mVclDevice);
        Bitmap bitmap = mVclDevice->GetBitmap(Point(), mVclDevice->GetOutputSizePixel());
        Bitmap::ScopedReadAccess access(bitmap);
        // Canvas uses AA, which blurs the line, and it cannot be turned off,
        // so do not check the end points.
        CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE), access->GetPixel(0, 0));
        CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_BLACK), access->GetPixel(1, 2));
        CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_BLACK), access->GetPixel(1, 8));
    }

    // Draw a dashed line scaled, make sure the dashing is scaled properly.
    void testTdf134053()
    {
        setupCanvas(Size(1000, 100));
        // Scale everything up by 10 (2 in render state, 5 in view state).
        mRenderState.AffineTransform = geometry::AffineMatrix2D(2, 0, 0, 0, 2, 0);
        mViewState.AffineTransform = geometry::AffineMatrix2D(5, 0, 0, 0, 5, 0);

        uno::Sequence<uno::Sequence<geometry::RealPoint2D>> polygonPoints{ { { 10, 5 },
                                                                             { 88, 5 } } };
        uno::Reference<rendering::XLinePolyPolygon2D> polygon
            = mDevice->createCompatibleLinePolyPolygon(polygonPoints);
        polygon->setClosed(0, false);

        mRenderState.DeviceColor = mColorBlue;
        rendering::StrokeAttributes strokeAttributes;
        strokeAttributes.StrokeWidth = 2.0;
        strokeAttributes.MiterLimit = 2.0; // ?
        strokeAttributes.StartCapType = rendering::PathCapType::ROUND;
        strokeAttributes.EndCapType = rendering::PathCapType::ROUND;
        strokeAttributes.JoinType = rendering::PathJoinType::MITER;
        strokeAttributes.DashArray = { 10, 5, 0.1, 5 };

        mCanvas->strokePolyPolygon(polygon, mViewState, mRenderState, strokeAttributes);

        exportDevice("test-tdf134053.png", mVclDevice);
        Bitmap bitmap = mVclDevice->GetBitmap(Point(), mVclDevice->GetOutputSizePixel());
        Bitmap::ScopedReadAccess access(bitmap);
        struct Check
        {
            tools::Long start;
            tools::Long end;
            Color color;
        };
        // There should be a long dash at X 100-200, a dot at 250, long one at 300-400, a dot at 450, etc.
        // until a dot at 850. Add -5/+5 to account for round caps.
        const Check checks[] = { { 0, 85, COL_WHITE }, // empty start
                                 // dash, space, dot, space
                                 { 95, 205, COL_BLUE },
                                 { 215, 235, COL_WHITE },
                                 { 245, 255, COL_BLUE },
                                 { 265, 285, COL_WHITE },
                                 { 295, 405, COL_BLUE },
                                 { 415, 435, COL_WHITE },
                                 { 445, 455, COL_BLUE },
                                 { 465, 485, COL_WHITE },
                                 { 495, 605, COL_BLUE },
                                 { 615, 635, COL_WHITE },
                                 { 645, 655, COL_BLUE },
                                 { 665, 685, COL_WHITE },
                                 { 695, 805, COL_BLUE },
                                 { 815, 835, COL_WHITE },
                                 { 845, 855, COL_BLUE },
                                 { 865, 999, COL_WHITE } }; // empty end
        for (const Check& check : checks)
        {
            for (tools::Long x = check.start; x <= check.end; ++x)
            {
                if (access->GetColor(50, x) != check.color)
                {
                    std::ostringstream str;
                    str << "X: " << x;
                    CPPUNIT_ASSERT_EQUAL_MESSAGE(str.str(), BitmapColor(check.color),
                                                 access->GetColor(50, x));
                }
            }
        }
    }

    CPPUNIT_TEST_SUITE(CanvasTest);
    CPPUNIT_TEST(testDrawLine);
    CPPUNIT_TEST(testTdf134053);
    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(CanvasTest);

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */