LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / drawinglayer / qa / unit / vclmetafileprocessor2d.cxx
blob28112e61647b10307e504c81b75a5d001e4c91c0
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/canvastools.hxx>
16 #include <vcl/graphicfilter.hxx>
17 #include <vcl/metaact.hxx>
18 #include <vcl/gdimtf.hxx>
19 #include <tools/stream.hxx>
20 #include <drawinglayer/geometry/viewinformation2d.hxx>
21 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
22 #include <drawinglayer/processor2d/baseprocessor2d.hxx>
23 #include <drawinglayer/processor2d/processorfromoutputdevice.hxx>
24 #include <cppcanvas/vclfactory.hxx>
26 #include <com/sun/star/rendering/XBitmap.hpp>
27 #include <com/sun/star/rendering/XCanvas.hpp>
28 #include <com/sun/star/rendering/XBitmapCanvas.hpp>
29 #include <com/sun/star/rendering/CompositeOperation.hpp>
31 using namespace drawinglayer;
32 using namespace com::sun::star;
34 class VclMetaFileProcessor2DTest : public test::BootstrapFixture
36 VclPtr<VirtualDevice> mVclDevice;
37 uno::Reference<rendering::XCanvas> mCanvas;
39 // if enabled - check the result images with:
40 // "xdg-open ./workdir/CppunitTest/drawinglayer_processors.test.core/"
41 static constexpr const bool mbExportBitmap = false;
43 void exportDevice(const OUString& filename, const VclPtr<VirtualDevice>& device)
45 if (mbExportBitmap)
47 BitmapEx aBitmapEx(device->GetBitmapEx(Point(0, 0), device->GetOutputSizePixel()));
48 SvFileStream aStream(filename, StreamMode::WRITE | StreamMode::TRUNC);
49 GraphicFilter::GetGraphicFilter().compressAsPNG(aBitmapEx, aStream);
53 public:
54 VclMetaFileProcessor2DTest()
55 : BootstrapFixture(true, false)
59 virtual void tearDown() override
61 mVclDevice.clear();
62 mCanvas = uno::Reference<rendering::XCanvas>();
63 BootstrapFixture::tearDown();
66 void setupCanvas(const Size& size, Color backgroundColor = COL_WHITE, bool alpha = false)
68 mVclDevice
69 = alpha ? VclPtr<VirtualDevice>::Create(DeviceFormat::DEFAULT, DeviceFormat::DEFAULT)
70 : VclPtr<VirtualDevice>::Create(DeviceFormat::DEFAULT);
71 mVclDevice->SetOutputSizePixel(size);
72 mVclDevice->SetBackground(Wallpaper(backgroundColor));
73 mVclDevice->Erase();
74 mCanvas = mVclDevice->GetCanvas();
75 CPPUNIT_ASSERT(mCanvas.is());
78 // Test drawing a dotted line in Impress presentation mode.
79 void testTdf136957()
81 // Impress presentation mode first draws the slide to a metafile.
82 GDIMetaFile metafile;
83 // I got these values by adding debug output to cppcanvas::internal::ImplRenderer::ImplRenderer().
84 metafile.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
85 metafile.SetPrefSize(Size(14548, 3350));
86 ScopedVclPtrInstance<VirtualDevice> metadevice;
87 metafile.Record(metadevice);
88 drawinglayer::geometry::ViewInformation2D view;
89 std::unique_ptr<processor2d::BaseProcessor2D> processor(
90 processor2d::createBaseProcessor2DFromOutputDevice(*metadevice, view));
91 CPPUNIT_ASSERT(processor);
92 // Match the values Impress uses.
93 basegfx::B2DPolygon polygon = { { 15601, 0 }, { 15602, 5832 } };
94 attribute::LineAttribute lineAttributes(
95 basegfx::BColor(0.047058823529411764, 0.19607843137254902, 0.17254901960784313), 35,
96 basegfx::B2DLineJoin::Miter, css::drawing::LineCap_ROUND);
97 attribute::StrokeAttribute strokeAttributes({ 0.35, 69.65 });
98 rtl::Reference<primitive2d::PolygonStrokePrimitive2D> strokePrimitive(
99 new primitive2d::PolygonStrokePrimitive2D(polygon, lineAttributes, strokeAttributes));
100 primitive2d::Primitive2DContainer primitives;
101 primitives.push_back(primitive2d::Primitive2DReference(strokePrimitive));
102 processor->process(primitives);
103 metafile.Stop();
104 metafile.WindStart();
106 // Now verify that the metafile has the one PolyLine action with the right dashing.
107 int lineActionCount = 0;
108 for (std::size_t i = 0; i < metafile.GetActionSize(); ++i)
110 const MetaAction* metaAction = metafile.GetAction(i);
111 if (metaAction->GetType() == MetaActionType::POLYLINE)
113 const MetaPolyLineAction* action
114 = static_cast<const MetaPolyLineAction*>(metaAction);
116 CPPUNIT_ASSERT_EQUAL(35.0, action->GetLineInfo().GetWidth());
117 CPPUNIT_ASSERT_EQUAL(LineStyle::Dash, action->GetLineInfo().GetStyle());
118 CPPUNIT_ASSERT_EQUAL(sal_uInt16(1), action->GetLineInfo().GetDashCount());
119 CPPUNIT_ASSERT_EQUAL(0.35, action->GetLineInfo().GetDashLen());
120 CPPUNIT_ASSERT_EQUAL(sal_uInt16(0), action->GetLineInfo().GetDotCount());
121 CPPUNIT_ASSERT_EQUAL(0.0, action->GetLineInfo().GetDotLen());
122 CPPUNIT_ASSERT_EQUAL(69.65, action->GetLineInfo().GetDistance());
123 lineActionCount++;
126 CPPUNIT_ASSERT_EQUAL(1, lineActionCount);
128 // Now draw the metafile using canvas and verify that the line is drawn.
129 setupCanvas(Size(1920, 1080));
130 cppcanvas::CanvasSharedPtr cppCanvas = cppcanvas::VCLFactory::createCanvas(mCanvas);
131 // I got these matrices from a breakpoint in drawing the polyline, and walking up
132 // the stack to the canvas code.
133 cppCanvas->setTransformation(
134 basegfx::B2DHomMatrix(0.056662828121770453, 0, 0, 0, 0.056640419947506564, 0));
135 cppcanvas::RendererSharedPtr renderer = cppcanvas::VCLFactory::createRenderer(
136 cppCanvas, metafile, cppcanvas::Renderer::Parameters());
137 renderer->setTransformation(basegfx::B2DHomMatrix(14548, 0, -2, 0, 3350, 3431));
138 CPPUNIT_ASSERT(renderer->draw());
139 exportDevice("test-tdf136957", mVclDevice);
140 Bitmap bitmap = mVclDevice->GetBitmap(Point(), Size(1920, 1080));
141 Bitmap::ScopedReadAccess access(bitmap);
142 // There should be a dotted line, without the fix it wouldn't be there, so check
143 // there's a sufficient amount of non-white pixels and that's the line.
144 int nonWhiteCount = 0;
145 for (tools::Long y = 193; y <= 524; ++y)
146 for (tools::Long x = 883; x <= 885; ++x)
147 if (access->GetColor(y, x) != COL_WHITE)
148 ++nonWhiteCount;
149 CPPUNIT_ASSERT_GREATER(100, nonWhiteCount);
152 CPPUNIT_TEST_SUITE(VclMetaFileProcessor2DTest);
153 CPPUNIT_TEST(testTdf136957);
154 CPPUNIT_TEST_SUITE_END();
157 CPPUNIT_TEST_SUITE_REGISTRATION(VclMetaFileProcessor2DTest);
159 CPPUNIT_PLUGIN_IMPLEMENT();
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */