1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
11 #include <test/bootstrapfixture.hxx>
13 #include <vcl/virdev.hxx>
14 #include <vcl/BitmapReadAccess.hxx>
15 #include <vcl/graphicfilter.hxx>
16 #include <vcl/metaact.hxx>
17 #include <vcl/gdimtf.hxx>
18 #include <tools/stream.hxx>
19 #include <drawinglayer/geometry/viewinformation2d.hxx>
20 #include <drawinglayer/primitive2d/PolygonStrokePrimitive2D.hxx>
21 #include <drawinglayer/processor2d/baseprocessor2d.hxx>
22 #include <drawinglayer/processor2d/processor2dtools.hxx>
23 #include <cppcanvas/vclfactory.hxx>
25 #include <com/sun/star/rendering/XCanvas.hpp>
27 using namespace drawinglayer
;
28 using namespace com::sun::star
;
30 class VclMetaFileProcessor2DTest
: public test::BootstrapFixture
32 VclPtr
<VirtualDevice
> mVclDevice
;
33 uno::Reference
<rendering::XCanvas
> mCanvas
;
35 // if enabled - check the result images with:
36 // "xdg-open ./workdir/CppunitTest/drawinglayer_processors.test.core/"
37 static constexpr const bool mbExportBitmap
= false;
39 void exportDevice(const OUString
& filename
, const VclPtr
<VirtualDevice
>& device
)
43 BitmapEx
aBitmapEx(device
->GetBitmapEx(Point(0, 0), device
->GetOutputSizePixel()));
44 SvFileStream
aStream(filename
, StreamMode::WRITE
| StreamMode::TRUNC
);
45 GraphicFilter::GetGraphicFilter().compressAsPNG(aBitmapEx
, aStream
);
50 VclMetaFileProcessor2DTest()
51 : BootstrapFixture(true, false)
55 virtual void tearDown() override
58 mCanvas
= uno::Reference
<rendering::XCanvas
>();
59 BootstrapFixture::tearDown();
62 void setupCanvas(const Size
& size
, Color backgroundColor
= COL_WHITE
, bool alpha
= false)
64 mVclDevice
= alpha
? VclPtr
<VirtualDevice
>::Create(DeviceFormat::WITH_ALPHA
)
65 : VclPtr
<VirtualDevice
>::Create(DeviceFormat::WITHOUT_ALPHA
);
66 mVclDevice
->SetOutputSizePixel(size
);
67 mVclDevice
->SetBackground(Wallpaper(backgroundColor
));
69 mCanvas
= mVclDevice
->GetCanvas();
70 CPPUNIT_ASSERT(mCanvas
.is());
73 // Test drawing a dotted line in Impress presentation mode.
76 // Impress presentation mode first draws the slide to a metafile.
78 // I got these values by adding debug output to cppcanvas::internal::ImplRenderer::ImplRenderer().
79 metafile
.SetPrefMapMode(MapMode(MapUnit::Map100thMM
));
80 metafile
.SetPrefSize(Size(14548, 3350));
81 ScopedVclPtrInstance
<VirtualDevice
> metadevice
;
82 metafile
.Record(metadevice
);
83 drawinglayer::geometry::ViewInformation2D view
;
84 std::unique_ptr
<processor2d::BaseProcessor2D
> processor(
85 processor2d::createProcessor2DFromOutputDevice(*metadevice
, view
));
86 CPPUNIT_ASSERT(processor
);
87 // Match the values Impress uses.
88 basegfx::B2DPolygon polygon
= { { 15601, 0 }, { 15602, 5832 } };
89 attribute::LineAttribute
lineAttributes(
90 basegfx::BColor(0.047058823529411764, 0.19607843137254902, 0.17254901960784313), 35,
91 basegfx::B2DLineJoin::Miter
, css::drawing::LineCap_ROUND
);
92 attribute::StrokeAttribute
strokeAttributes({ 0.35, 69.65 });
93 rtl::Reference
<primitive2d::PolygonStrokePrimitive2D
> strokePrimitive(
94 new primitive2d::PolygonStrokePrimitive2D(polygon
, lineAttributes
, strokeAttributes
));
95 primitive2d::Primitive2DContainer primitives
;
96 primitives
.push_back(primitive2d::Primitive2DReference(strokePrimitive
));
97 processor
->process(primitives
);
101 // Now verify that the metafile has the one PolyLine action with the right dashing.
102 int lineActionCount
= 0;
103 for (std::size_t i
= 0; i
< metafile
.GetActionSize(); ++i
)
105 const MetaAction
* metaAction
= metafile
.GetAction(i
);
106 if (metaAction
->GetType() == MetaActionType::POLYLINE
)
108 const MetaPolyLineAction
* action
109 = static_cast<const MetaPolyLineAction
*>(metaAction
);
111 CPPUNIT_ASSERT_EQUAL(35.0, action
->GetLineInfo().GetWidth());
112 CPPUNIT_ASSERT_EQUAL(LineStyle::Dash
, action
->GetLineInfo().GetStyle());
113 CPPUNIT_ASSERT_EQUAL(sal_uInt16(1), action
->GetLineInfo().GetDashCount());
114 CPPUNIT_ASSERT_EQUAL(0.35, action
->GetLineInfo().GetDashLen());
115 CPPUNIT_ASSERT_EQUAL(sal_uInt16(0), action
->GetLineInfo().GetDotCount());
116 CPPUNIT_ASSERT_EQUAL(0.0, action
->GetLineInfo().GetDotLen());
117 CPPUNIT_ASSERT_EQUAL(69.65, action
->GetLineInfo().GetDistance());
121 CPPUNIT_ASSERT_EQUAL(1, lineActionCount
);
123 // Now draw the metafile using canvas and verify that the line is drawn.
124 setupCanvas(Size(1920, 1080));
125 cppcanvas::CanvasSharedPtr cppCanvas
= cppcanvas::VCLFactory::createCanvas(mCanvas
);
126 // I got these matrices from a breakpoint in drawing the polyline, and walking up
127 // the stack to the canvas code.
128 cppCanvas
->setTransformation(
129 basegfx::B2DHomMatrix(0.056662828121770453, 0, 0, 0, 0.056640419947506564, 0));
130 cppcanvas::RendererSharedPtr renderer
= cppcanvas::VCLFactory::createRenderer(
131 cppCanvas
, metafile
, cppcanvas::Renderer::Parameters());
132 renderer
->setTransformation(basegfx::B2DHomMatrix(14548, 0, -2, 0, 3350, 3431));
133 CPPUNIT_ASSERT(renderer
->draw());
134 exportDevice("test-tdf136957", mVclDevice
);
135 Bitmap bitmap
= mVclDevice
->GetBitmap(Point(), Size(1920, 1080));
136 Bitmap::ScopedReadAccess
access(bitmap
);
137 // There should be a dotted line, without the fix it wouldn't be there, so check
138 // there's a sufficient amount of non-white pixels and that's the line.
139 int nonWhiteCount
= 0;
140 for (tools::Long y
= 193; y
<= 524; ++y
)
141 for (tools::Long x
= 883; x
<= 885; ++x
)
142 if (access
->GetColor(y
, x
) != COL_WHITE
)
144 CPPUNIT_ASSERT_GREATER(100, nonWhiteCount
);
147 CPPUNIT_TEST_SUITE(VclMetaFileProcessor2DTest
);
148 CPPUNIT_TEST(testTdf136957
);
149 CPPUNIT_TEST_SUITE_END();
152 CPPUNIT_TEST_SUITE_REGISTRATION(VclMetaFileProcessor2DTest
);
154 CPPUNIT_PLUGIN_IMPLEMENT();
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */