Bump version to 21.06.18.1
[LibreOffice.git] / cppcanvas / source / mtfrenderer / bitmapaction.cxx
blob8040c9a018c04e29df8fb88f0a7c45c0bf367d03
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 .
21 #include <com/sun/star/rendering/XBitmap.hpp>
22 #include <com/sun/star/rendering/XCanvas.hpp>
23 #include <com/sun/star/rendering/XCachedPrimitive.hpp>
24 #include <vcl/bitmapex.hxx>
25 #include <tools/gen.hxx>
26 #include <vcl/canvastools.hxx>
27 #include <canvas/canvastools.hxx>
28 #include <basegfx/matrix/b2dhommatrix.hxx>
29 #include <basegfx/point/b2dpoint.hxx>
30 #include <basegfx/range/b2drange.hxx>
31 #include <sal/log.hxx>
32 #include "cachedprimitivebase.hxx"
33 #include "bitmapaction.hxx"
34 #include <outdevstate.hxx>
35 #include "mtftools.hxx"
36 #include <basegfx/matrix/b2dhommatrixtools.hxx>
39 using namespace ::com::sun::star;
41 namespace cppcanvas::internal
43 namespace
46 class BitmapAction : public CachedPrimitiveBase
48 public:
49 BitmapAction( const ::BitmapEx&,
50 const ::basegfx::B2DPoint& rDstPoint,
51 const CanvasSharedPtr&,
52 const OutDevState& );
53 BitmapAction( const ::BitmapEx&,
54 const ::basegfx::B2DPoint& rDstPoint,
55 const ::basegfx::B2DVector& rDstSize,
56 const CanvasSharedPtr&,
57 const OutDevState& );
59 virtual bool renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
60 const Subset& rSubset ) const override;
62 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const override;
63 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
64 const Subset& rSubset ) const override;
66 virtual sal_Int32 getActionCount() const override;
68 private:
69 using Action::render;
70 virtual bool renderPrimitive( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
71 const ::basegfx::B2DHomMatrix& rTransformation ) const override;
73 uno::Reference< rendering::XBitmap > mxBitmap;
74 CanvasSharedPtr mpCanvas;
75 rendering::RenderState maState;
79 BitmapAction::BitmapAction( const ::BitmapEx& rBmpEx,
80 const ::basegfx::B2DPoint& rDstPoint,
81 const CanvasSharedPtr& rCanvas,
82 const OutDevState& rState ) :
83 CachedPrimitiveBase( rCanvas, true ),
84 mxBitmap( vcl::unotools::xBitmapFromBitmapEx( rBmpEx ) ),
85 mpCanvas( rCanvas ),
86 maState()
88 tools::initRenderState(maState,rState);
90 // Setup transformation such that the next render call is
91 // moved rPoint away.
92 const basegfx::B2DHomMatrix aLocalTransformation(basegfx::utils::createTranslateB2DHomMatrix(rDstPoint));
93 ::canvas::tools::appendToRenderState( maState,
94 aLocalTransformation );
96 // correct clip (which is relative to original transform)
97 tools::modifyClip( maState,
98 rState,
99 rCanvas,
100 rDstPoint,
101 nullptr,
102 nullptr );
105 BitmapAction::BitmapAction( const ::BitmapEx& rBmpEx,
106 const ::basegfx::B2DPoint& rDstPoint,
107 const ::basegfx::B2DVector& rDstSize,
108 const CanvasSharedPtr& rCanvas,
109 const OutDevState& rState ) :
110 CachedPrimitiveBase( rCanvas, true ),
111 mxBitmap( vcl::unotools::xBitmapFromBitmapEx( rBmpEx ) ),
112 mpCanvas( rCanvas ),
113 maState()
115 tools::initRenderState(maState,rState);
117 // Setup transformation such that the next render call is
118 // moved rPoint away, and scaled according to the ratio
119 // given by src and dst size.
120 const ::Size aBmpSize( rBmpEx.GetSizePixel() );
122 const ::basegfx::B2DVector aScale( rDstSize.getX() / aBmpSize.Width(),
123 rDstSize.getY() / aBmpSize.Height() );
124 const basegfx::B2DHomMatrix aLocalTransformation(basegfx::utils::createScaleTranslateB2DHomMatrix(
125 aScale, rDstPoint));
126 ::canvas::tools::appendToRenderState( maState, aLocalTransformation );
128 // correct clip (which is relative to original transform)
129 tools::modifyClip( maState,
130 rState,
131 rCanvas,
132 rDstPoint,
133 &aScale,
134 nullptr );
137 bool BitmapAction::renderPrimitive( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
138 const ::basegfx::B2DHomMatrix& rTransformation ) const
140 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::BitmapAction::renderPrimitive()" );
141 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::BitmapAction: 0x" << std::hex << this );
143 rendering::RenderState aLocalState( maState );
144 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
146 rCachedPrimitive = mpCanvas->getUNOCanvas()->drawBitmap( mxBitmap,
147 mpCanvas->getViewState(),
148 aLocalState );
150 return true;
153 bool BitmapAction::renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
154 const Subset& rSubset ) const
156 // bitmap only contains a single action, fail if subset
157 // requests different range
158 if( rSubset.mnSubsetBegin != 0 ||
159 rSubset.mnSubsetEnd != 1 )
160 return false;
162 return CachedPrimitiveBase::render( rTransformation );
165 ::basegfx::B2DRange BitmapAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const
167 rendering::RenderState aLocalState( maState );
168 ::canvas::tools::prependToRenderState(aLocalState, rTransformation);
170 const geometry::IntegerSize2D aSize( mxBitmap->getSize() );
172 return tools::calcDevicePixelBounds( ::basegfx::B2DRange( 0,0,
173 aSize.Width,
174 aSize.Height ),
175 mpCanvas->getViewState(),
176 aLocalState );
179 ::basegfx::B2DRange BitmapAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
180 const Subset& rSubset ) const
182 // bitmap only contains a single action, empty bounds
183 // if subset requests different range
184 if( rSubset.mnSubsetBegin != 0 ||
185 rSubset.mnSubsetEnd != 1 )
186 return ::basegfx::B2DRange();
188 return getBounds( rTransformation );
191 sal_Int32 BitmapAction::getActionCount() const
193 return 1;
197 std::shared_ptr<Action> BitmapActionFactory::createBitmapAction( const ::BitmapEx& rBmpEx,
198 const ::basegfx::B2DPoint& rDstPoint,
199 const CanvasSharedPtr& rCanvas,
200 const OutDevState& rState )
202 return std::make_shared<BitmapAction>(rBmpEx, rDstPoint, rCanvas, rState );
205 std::shared_ptr<Action> BitmapActionFactory::createBitmapAction( const ::BitmapEx& rBmpEx,
206 const ::basegfx::B2DPoint& rDstPoint,
207 const ::basegfx::B2DVector& rDstSize,
208 const CanvasSharedPtr& rCanvas,
209 const OutDevState& rState )
211 return std::make_shared<BitmapAction>(rBmpEx,
212 rDstPoint,
213 rDstSize,
214 rCanvas,
215 rState );
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */