Bump version to 24.04.3.4
[LibreOffice.git] / drawinglayer / source / drawinglayeruno / xprimitive2drenderer.cxx
blob58df9f4a7b2858be94349fe6e460eb9326c0c960
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 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <com/sun/star/graphic/XPrimitive2DRenderer.hpp>
23 #include <com/sun/star/lang/XServiceInfo.hpp>
24 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <cppuhelper/implbase2.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <drawinglayer/geometry/viewinformation2d.hxx>
28 #include <basegfx/numeric/ftools.hxx>
29 #include <vcl/bitmapex.hxx>
30 #include <vcl/canvastools.hxx>
31 #include <com/sun/star/geometry/RealRectangle2D.hpp>
32 #include <basegfx/matrix/b2dhommatrixtools.hxx>
33 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
35 #include <drawinglayer/converters.hxx>
36 #include <comphelper/sequenceashashmap.hxx>
38 using namespace ::com::sun::star;
41 namespace drawinglayer::unorenderer
43 namespace {
45 class XPrimitive2DRenderer:
46 public cppu::WeakImplHelper<
47 css::graphic::XPrimitive2DRenderer, css::lang::XServiceInfo>
49 public:
50 XPrimitive2DRenderer();
52 XPrimitive2DRenderer(const XPrimitive2DRenderer&) = delete;
53 const XPrimitive2DRenderer& operator=(const XPrimitive2DRenderer&) = delete;
55 // XPrimitive2DRenderer
56 virtual uno::Reference< rendering::XBitmap > SAL_CALL rasterize(
57 const uno::Sequence< uno::Reference< graphic::XPrimitive2D > >& Primitive2DSequence,
58 const uno::Sequence< beans::PropertyValue >& aViewInformationSequence,
59 ::sal_uInt32 DPI_X,
60 ::sal_uInt32 DPI_Y,
61 const css::geometry::RealRectangle2D& Range,
62 ::sal_uInt32 MaximumQuadraticPixels) override;
64 // XServiceInfo
65 virtual OUString SAL_CALL getImplementationName() override;
66 virtual sal_Bool SAL_CALL supportsService(const OUString&) override;
67 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
72 XPrimitive2DRenderer::XPrimitive2DRenderer()
76 uno::Reference< rendering::XBitmap > XPrimitive2DRenderer::rasterize(
77 const uno::Sequence< uno::Reference< graphic::XPrimitive2D > >& aPrimitive2DSequence,
78 const uno::Sequence< beans::PropertyValue >& aViewInformationSequence,
79 ::sal_uInt32 DPI_X,
80 ::sal_uInt32 DPI_Y,
81 const css::geometry::RealRectangle2D& Range,
82 ::sal_uInt32 MaximumQuadraticPixels)
84 o3tl::Length eRangeUnit = o3tl::Length::mm100;
85 comphelper::SequenceAsHashMap aViewInformationMap(aViewInformationSequence);
86 auto it = aViewInformationMap.find("RangeUnit");
87 if (it != aViewInformationMap.end())
89 sal_Int32 nVal{};
90 it->second >>= nVal;
91 eRangeUnit = static_cast<o3tl::Length>(nVal);
94 uno::Reference< rendering::XBitmap > XBitmap;
96 if(aPrimitive2DSequence.hasElements())
98 const basegfx::B2DRange aRange(Range.X1, Range.Y1, Range.X2, Range.Y2);
99 const double fWidth(aRange.getWidth());
100 const double fHeight(aRange.getHeight());
102 if(basegfx::fTools::more(fWidth, 0.0) && basegfx::fTools::more(fHeight, 0.0))
104 if(0 == DPI_X)
106 DPI_X = 75;
109 if(0 == DPI_Y)
111 DPI_Y = 75;
114 if(0 == MaximumQuadraticPixels)
116 MaximumQuadraticPixels = 500000;
119 auto aViewInformation2D = geometry::createViewInformation2D(aViewInformationSequence);
121 if(aViewInformation2D.getViewport().isEmpty())
123 // we have a Viewport since we create a discrete pixel device, use it
124 // if none is given
125 aViewInformation2D.setViewport(aRange);
128 const sal_uInt32 nDiscreteWidth(basegfx::fround(o3tl::convert(fWidth, eRangeUnit, o3tl::Length::in) * DPI_X));
129 const sal_uInt32 nDiscreteHeight(basegfx::fround(o3tl::convert(fHeight, eRangeUnit, o3tl::Length::in) * DPI_Y));
131 basegfx::B2DHomMatrix aEmbedding(
132 basegfx::utils::createTranslateB2DHomMatrix(
133 -aRange.getMinX(),
134 -aRange.getMinY()));
136 aEmbedding.scale(
137 nDiscreteWidth / fWidth,
138 nDiscreteHeight / fHeight);
140 const primitive2d::Primitive2DReference xEmbedRef(
141 new primitive2d::TransformPrimitive2D(
142 aEmbedding,
143 aPrimitive2DSequence));
144 primitive2d::Primitive2DContainer xEmbedSeq { xEmbedRef };
146 BitmapEx aBitmapEx(
147 convertToBitmapEx(
148 std::move(xEmbedSeq),
149 aViewInformation2D,
150 nDiscreteWidth,
151 nDiscreteHeight,
152 MaximumQuadraticPixels));
154 if(!aBitmapEx.IsEmpty())
156 aBitmapEx.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
157 aBitmapEx.SetPrefSize(Size(basegfx::fround(fWidth), basegfx::fround(fHeight)));
158 XBitmap = vcl::unotools::xBitmapFromBitmapEx(aBitmapEx);
163 return XBitmap;
166 OUString SAL_CALL XPrimitive2DRenderer::getImplementationName()
168 return "drawinglayer::unorenderer::XPrimitive2DRenderer";
171 sal_Bool SAL_CALL XPrimitive2DRenderer::supportsService(const OUString& rServiceName)
173 return cppu::supportsService(this, rServiceName);
176 uno::Sequence< OUString > SAL_CALL XPrimitive2DRenderer::getSupportedServiceNames()
178 return { "com.sun.star.graphic.Primitive2DTools" };
181 } // end of namespace
184 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
185 drawinglayer_XPrimitive2DRenderer(
186 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const& )
188 return cppu::acquire(new drawinglayer::unorenderer::XPrimitive2DRenderer());
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */