calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / drawinglayer / source / drawinglayeruno / xprimitive2drenderer.cxx
blobbe58c77dd3906de796fcc93c3c3b54ae8c1bd852
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::WeakAggImplHelper2<
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 const auto aViewInformation2D = geometry::createViewInformation2D(aViewInformationSequence);
120 const sal_uInt32 nDiscreteWidth(basegfx::fround(o3tl::convert(fWidth, eRangeUnit, o3tl::Length::in) * DPI_X));
121 const sal_uInt32 nDiscreteHeight(basegfx::fround(o3tl::convert(fHeight, eRangeUnit, o3tl::Length::in) * DPI_Y));
123 basegfx::B2DHomMatrix aEmbedding(
124 basegfx::utils::createTranslateB2DHomMatrix(
125 -aRange.getMinX(),
126 -aRange.getMinY()));
128 aEmbedding.scale(
129 nDiscreteWidth / fWidth,
130 nDiscreteHeight / fHeight);
132 const primitive2d::Primitive2DReference xEmbedRef(
133 new primitive2d::TransformPrimitive2D(
134 aEmbedding,
135 aPrimitive2DSequence));
136 primitive2d::Primitive2DContainer xEmbedSeq { xEmbedRef };
138 BitmapEx aBitmapEx(
139 convertToBitmapEx(
140 std::move(xEmbedSeq),
141 aViewInformation2D,
142 nDiscreteWidth,
143 nDiscreteHeight,
144 MaximumQuadraticPixels));
146 if(!aBitmapEx.IsEmpty())
148 aBitmapEx.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
149 aBitmapEx.SetPrefSize(Size(basegfx::fround(fWidth), basegfx::fround(fHeight)));
150 XBitmap = vcl::unotools::xBitmapFromBitmapEx(aBitmapEx);
155 return XBitmap;
158 OUString SAL_CALL XPrimitive2DRenderer::getImplementationName()
160 return "drawinglayer::unorenderer::XPrimitive2DRenderer";
163 sal_Bool SAL_CALL XPrimitive2DRenderer::supportsService(const OUString& rServiceName)
165 return cppu::supportsService(this, rServiceName);
168 uno::Sequence< OUString > SAL_CALL XPrimitive2DRenderer::getSupportedServiceNames()
170 return { "com.sun.star.graphic.Primitive2DTools" };
173 } // end of namespace
176 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
177 drawinglayer_XPrimitive2DRenderer(
178 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const& )
180 return cppu::acquire(new drawinglayer::unorenderer::XPrimitive2DRenderer());
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */