Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / drawinglayer / qa / unit / border.cxx
blobf6f9feba5427c6b17dfa8bfd6ddd7c96cb69bcb7
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/.
8 */
10 #include <memory>
11 #include <cppunit/TestAssert.h>
12 #include <cppunit/plugin/TestPlugIn.h>
14 #include <drawinglayer/geometry/viewinformation2d.hxx>
15 #include <drawinglayer/primitive2d/borderlineprimitive2d.hxx>
16 #include <drawinglayer/primitive2d/PolygonStrokePrimitive2D.hxx>
17 #include <drawinglayer/processor2d/baseprocessor2d.hxx>
18 #include <drawinglayer/processor2d/processor2dtools.hxx>
19 #include <rtl/ref.hxx>
20 #include <test/bootstrapfixture.hxx>
21 #include <vcl/metaact.hxx>
22 #include <vcl/vclptr.hxx>
23 #include <vcl/virdev.hxx>
24 #include <editeng/borderline.hxx>
25 #include <svtools/borderhelper.hxx>
27 using namespace com::sun::star;
29 namespace
31 class DrawinglayerBorderTest : public test::BootstrapFixture
35 CPPUNIT_TEST_FIXTURE(DrawinglayerBorderTest, testDoubleDecompositionSolid)
37 // Create a border line primitive that's similar to the one from the bugdoc:
38 // 1.47 pixels is 0.03cm at 130% zoom and 96 DPI.
39 basegfx::B2DPoint aStart(0, 20);
40 basegfx::B2DPoint aEnd(100, 20);
41 double const fLeftWidth = 1.47;
42 double const fDistance = 1.47;
43 double const fRightWidth = 1.47;
44 double const fExtendLeftStart = 0;
45 double const fExtendLeftEnd = 0;
46 double const fExtendRightStart = 0;
47 double const fExtendRightEnd = 0;
48 basegfx::BColor aColorRight;
49 basegfx::BColor aColorLeft;
50 std::vector<double> aDashing(svtools::GetLineDashing(SvxBorderLineStyle::DOUBLE, 10.0));
51 const drawinglayer::attribute::StrokeAttribute aStrokeAttribute(std::move(aDashing));
52 std::vector<drawinglayer::primitive2d::BorderLine> aBorderlines{
54 drawinglayer::primitive2d::BorderLine(
55 drawinglayer::attribute::LineAttribute(aColorLeft, fLeftWidth), fExtendLeftStart,
56 fExtendLeftStart, fExtendLeftEnd, fExtendLeftEnd),
58 drawinglayer::primitive2d::BorderLine(fDistance),
60 drawinglayer::primitive2d::BorderLine(
61 drawinglayer::attribute::LineAttribute(aColorRight, fRightWidth), fExtendRightStart,
62 fExtendRightStart, fExtendRightEnd, fExtendRightEnd)
65 rtl::Reference<drawinglayer::primitive2d::BorderLinePrimitive2D> aBorder(
66 new drawinglayer::primitive2d::BorderLinePrimitive2D(aStart, aEnd, std::move(aBorderlines),
67 aStrokeAttribute));
69 // Decompose it into polygons.
70 drawinglayer::geometry::ViewInformation2D aView;
71 drawinglayer::primitive2d::Primitive2DContainer aContainer;
72 aBorder->get2DDecomposition(aContainer, aView);
74 // Make sure it results in two borders as it's a double one.
75 CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(2), aContainer.size());
77 // Get the inside line, now a PolygonStrokePrimitive2D
78 auto pInside = dynamic_cast<const drawinglayer::primitive2d::PolygonStrokePrimitive2D*>(
79 aContainer[0].get());
80 CPPUNIT_ASSERT(pInside);
82 // Make sure the inside line's height is fLeftWidth.
83 const double fLineWidthFromDecompose = pInside->getLineAttribute().getWidth();
85 // This was 2.47, i.e. the width of the inner line was 1 unit (in the bugdoc's case: 1 pixel) wider than expected.
86 CPPUNIT_ASSERT_DOUBLES_EQUAL(fLeftWidth, fLineWidthFromDecompose,
87 basegfx::fTools::getSmallValue());
90 CPPUNIT_TEST_FIXTURE(DrawinglayerBorderTest, testDoublePixelProcessing)
92 // Create a pixel processor.
93 ScopedVclPtrInstance<VirtualDevice> pDev;
94 drawinglayer::geometry::ViewInformation2D aView;
95 std::unique_ptr<drawinglayer::processor2d::BaseProcessor2D> pProcessor(
96 drawinglayer::processor2d::createProcessor2DFromOutputDevice(*pDev, aView));
97 CPPUNIT_ASSERT(pProcessor);
98 GDIMetaFile aMetaFile;
99 // Start recording after the processor is created, so we can test the pixel processor.
100 aMetaFile.Record(pDev);
102 // Create a border line primitive that's similar to the one from the bugdoc:
103 // 1.47 pixels is 0.03cm at 130% zoom and 96 DPI.
104 basegfx::B2DPoint aStart(0, 20);
105 basegfx::B2DPoint aEnd(100, 20);
106 double const fLeftWidth = 1.47;
107 double const fDistance = 1.47;
108 double const fRightWidth = 1.47;
109 double const fExtendLeftStart = 0;
110 double const fExtendLeftEnd = 0;
111 double const fExtendRightStart = 0;
112 double const fExtendRightEnd = 0;
113 basegfx::BColor aColorRight;
114 basegfx::BColor aColorLeft;
115 std::vector<double> aDashing(svtools::GetLineDashing(SvxBorderLineStyle::DOUBLE, 10.0));
116 const drawinglayer::attribute::StrokeAttribute aStrokeAttribute(std::move(aDashing));
117 std::vector<drawinglayer::primitive2d::BorderLine> aBorderlines{
118 drawinglayer::primitive2d::BorderLine(
119 drawinglayer::attribute::LineAttribute(aColorLeft, fLeftWidth), fExtendLeftStart,
120 fExtendLeftStart, fExtendLeftEnd, fExtendLeftEnd),
122 drawinglayer::primitive2d::BorderLine(fDistance),
124 drawinglayer::primitive2d::BorderLine(
125 drawinglayer::attribute::LineAttribute(aColorRight, fRightWidth), fExtendRightStart,
126 fExtendRightStart, fExtendRightEnd, fExtendRightEnd)
129 rtl::Reference<drawinglayer::primitive2d::BorderLinePrimitive2D> aBorder(
130 new drawinglayer::primitive2d::BorderLinePrimitive2D(aStart, aEnd, std::move(aBorderlines),
131 aStrokeAttribute));
133 drawinglayer::primitive2d::Primitive2DContainer aPrimitives;
134 aPrimitives.push_back(drawinglayer::primitive2d::Primitive2DReference(aBorder));
136 // Process the primitives.
137 pProcessor->process(aPrimitives);
139 // Double line now gets decomposed in Metafile to painting four lines
140 // with width == 0 in a cross pattern due to real line width being between
141 // 1.0 and 2.0. Count created lines
142 aMetaFile.Stop();
143 aMetaFile.WindStart();
144 sal_uInt32 nPolyLineActionCount = 0;
146 for (std::size_t nAction = 0; nAction < aMetaFile.GetActionSize(); ++nAction)
148 MetaAction* pAction = aMetaFile.GetAction(nAction);
150 if (MetaActionType::POLYLINE == pAction->GetType())
152 auto pMPLAction = static_cast<MetaPolyLineAction*>(pAction);
154 if (0 != pMPLAction->GetLineInfo().GetWidth()
155 && LineStyle::Solid == pMPLAction->GetLineInfo().GetStyle())
157 nPolyLineActionCount++;
162 // Check if all eight (2x four) simple lines with width == 0 and
163 // solid were created
165 // This has changed: Now, just the needed 'real' lines get created
166 // which have a width of 1. This are two lines. The former multiple
167 // lines were a combination of view-dependent force to a single-pixel
168 // line width (0 == lineWidth -> hairline) and vcl rendering this
169 // using a (insane) combination of single non-AAed lines. All the
170 // system-dependent part of the BorderLine stuff is now done in
171 // SdrFrameBorderPrimitive2D and svx.
172 // Adapted this test - still useful, breaking it may be a hint :-)
173 const sal_uInt32 nExpectedNumPolyLineActions = 2;
175 CPPUNIT_ASSERT_EQUAL(nExpectedNumPolyLineActions, nPolyLineActionCount);
179 CPPUNIT_PLUGIN_IMPLEMENT();
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */