Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / drawinglayer / qa / unit / vclpixelprocessor2d.cxx
blob4362d3e55f19c78dcf3b3837a3bb4ae7fe9f6364
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
11 #include <test/bootstrapfixture.hxx>
13 #include <vcl/virdev.hxx>
14 #include <vcl/BitmapReadAccess.hxx>
15 #include <vcl/graphicfilter.hxx>
16 #include <tools/stream.hxx>
17 #include <drawinglayer/geometry/viewinformation2d.hxx>
18 #include <drawinglayer/primitive2d/fillgradientprimitive2d.hxx>
19 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
20 #include <drawinglayer/processor2d/baseprocessor2d.hxx>
21 #include <drawinglayer/processor2d/processor2dtools.hxx>
22 #include <basegfx/polygon/b2dpolygontools.hxx>
23 #include <basegfx/utils/gradienttools.hxx>
25 using namespace drawinglayer;
27 class VclPixelProcessor2DTest : public test::BootstrapFixture
29 // if enabled - check the result images with:
30 // "xdg-open ./workdir/CppunitTest/drawinglayer_processors.test.core/"
31 static constexpr const bool mbExportBitmap = false;
33 void exportDevice(const OUString& filename, const VclPtr<VirtualDevice>& device)
35 if (mbExportBitmap)
37 BitmapEx aBitmapEx(device->GetBitmapEx(Point(0, 0), device->GetOutputSizePixel()));
38 SvFileStream aStream(filename, StreamMode::WRITE | StreamMode::TRUNC);
39 GraphicFilter::GetGraphicFilter().compressAsPNG(aBitmapEx, aStream);
43 public:
44 VclPixelProcessor2DTest()
45 : BootstrapFixture(true, false)
49 // Test that drawing only a part of a gradient draws the proper part of it.
50 void testTdf139000()
52 ScopedVclPtr<VirtualDevice> device
53 = VclPtr<VirtualDevice>::Create(DeviceFormat::WITHOUT_ALPHA);
54 device->SetOutputSizePixel(Size(100, 200));
55 device->SetBackground(Wallpaper(COL_RED));
56 device->Erase();
58 drawinglayer::geometry::ViewInformation2D view;
59 std::unique_ptr<processor2d::BaseProcessor2D> processor(
60 processor2d::createProcessor2DFromOutputDevice(*device, view));
61 CPPUNIT_ASSERT(processor);
63 // I stumbled over this when hunting another problem, but I have to correct
64 // this: This test does test something that is not supported. It seems to be
65 // based on the *misunderstanding* that in the version of the constructor of
66 // FillGradientPrimitive2D (and similar others) with two ranges the 2nd
67 // B2DRange parameter 'OutputRange' is a 'clipping' parameter. This is *not*
68 // the case --- it is in fact the *contrary*, it is there to *extend* the
69 // usual definition/paintRange of a gradient:
70 // It was originally needed to correctly display TextFrames (TF) in Writer: If you
71 // have a TF in SW filled with a gradient and that TF has sub-frames, it inherits
72 // the gradient fill. Since you can freely move those sub-TFs even outside the
73 // parent TF there has to be a way to not only paint gradients in their definition
74 // range (classical, all DrawObjects do that), but extended from that. This is
75 // needed e.g. for linear gradients, but - dependent of e.g. the center settings -
76 // also for all other ones, all can have geometry 'outside' the DefinitionRange.
77 // This is now also used in various other locations which is proof that this is
78 // useful and needed. It is possible to see that basic history/reason for this
79 // parameter by following the git history and why and under which circumstances
80 // that parameter was originally added. Other hints are: It is *not* named
81 // 'ClipRange'. Using a B2DRange to define a ClipRange topology would be bad due
82 // to not being transformable, a PolyPolygon would be used in that case. Using as
83 // clipping mechanism would offer a 2nd principle to add clipping for primitives
84 // besides MaskPrimitive2D - always bad style in a sub-system. A quick look
85 // on it's usages gives hints, too.
86 // This means that when defining an outputRange that resides completely *inside*
87 // the definitionRange *no change* at all is done by definition since this does
88 // not *extend* the target area of the gradient paint region at all. If an
89 // implementation does clip and limit output to 'outputRange' that should do no
90 // harm, but is not the expected/reliable way to paint primitives clipped.
91 // That's why all DrawObjects with gradient fill (and other fills do the same)
92 // embed the fill that is defined for a range (usually the BoundRange of a
93 // PolyPolygon) in a MaskPrimitive2D defined by the outline PolyPolygon of the
94 // shape. Nothing speaks against renderers detecting that combination and do
95 // something optimized if they want to, especially SDPRs, but this is not
96 // required. The standard embedded clipping of the implementations of the
97 // MaskPrimitive2D do the right thing.
98 // This test intends to paint the lower part of a gradient, so define the
99 // gradient for the full target range and embed it to a MaskPrimitive2D
100 // defining the lower part of that area to do that.
102 basegfx::B2DRange definitionRange(0, 0, 100, 200);
103 basegfx::B2DRange outputRange(0, 100, 100, 200); // Paint only lower half of the gradient.
105 const primitive2d::Primitive2DContainer primitives{
106 rtl::Reference<primitive2d::MaskPrimitive2D>(new primitive2d::MaskPrimitive2D(
107 basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(outputRange)),
108 primitive2d::Primitive2DContainer{
109 rtl::Reference<primitive2d::FillGradientPrimitive2D>(
110 new primitive2d::FillGradientPrimitive2D(
111 definitionRange, attribute::FillGradientAttribute(
112 css::awt::GradientStyle_LINEAR, 0, 0, 0, 0,
113 basegfx::BColorStops(COL_WHITE.getBColor(),
114 COL_BLACK.getBColor())))) }))
116 processor->process(primitives);
118 exportDevice("test-tdf139000.png", device);
119 Bitmap bitmap = device->GetBitmap(Point(), device->GetOutputSizePixel());
120 Bitmap::ScopedReadAccess access(bitmap);
121 // The upper half should keep its red background color.
122 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_RED), access->GetColor(Point(0, 99)));
123 // First line of the gradient should not be the start color, but something halfway.
124 CPPUNIT_ASSERT_LESS(static_cast<sal_uInt16>(16),
125 access->GetColor(Point(0, 100)).GetColorError(COL_GRAY));
126 // Last line of the gradient should be the end color, or close.
127 CPPUNIT_ASSERT_LESS(static_cast<sal_uInt16>(16),
128 access->GetColor(Point(0, 199)).GetColorError(COL_BLACK));
131 CPPUNIT_TEST_SUITE(VclPixelProcessor2DTest);
132 CPPUNIT_TEST(testTdf139000);
133 CPPUNIT_TEST_SUITE_END();
136 CPPUNIT_TEST_SUITE_REGISTRATION(VclPixelProcessor2DTest);
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */