Debian Codegraph @

cppcanvas/source/mtfrenderer/bitmapaction.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21 #include <com/sun/star/rendering/XBitmap.hpp>
22 #include <com/sun/star/rendering/XCanvas.hpp>
23 #include <vcl/bitmap.hxx>
24 #include <tools/gen.hxx>
25 #include <vcl/canvastools.hxx>
26 #include <canvas/canvastools.hxx>
27 #include <basegfx/matrix/b2dhommatrix.hxx>
28 #include <basegfx/point/b2dpoint.hxx>
29 #include <basegfx/range/b2drange.hxx>
30 #include <sal/log.hxx>
31 #include "cachedprimitivebase.hxx"
32 #include "bitmapaction.hxx"
33 #include <outdevstate.hxx>
34 #include "mtftools.hxx"
35 #include <basegfx/matrix/b2dhommatrixtools.hxx>
36
37
38 using namespace ::com::sun::star;
39
40 namespace cppcanvas::internal
41 {
42 namespace
43 {
44
45 class BitmapAction : public CachedPrimitiveBase
46 {
47 public:
48 BitmapAction( const ::Bitmap&,
49 const ::basegfx::B2DPoint& rDstPoint,
50 const CanvasSharedPtr&,
51 const OutDevState& );
52 BitmapAction( const ::Bitmap&,
53 const ::basegfx::B2DPoint& rDstPoint,
54 const ::basegfx::B2DVector& rDstSize,
55 const CanvasSharedPtr&,
56 const OutDevState& );
57
58 virtual bool renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
59 const Subset& rSubset ) const override;
60
61 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const override;
62 virtual ::basegfx::B2DRange getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
63 const Subset& rSubset ) const override;
64
65 virtual sal_Int32 getActionCount() const override;
66
67 private:
68 using Action::render;
69 virtual bool renderPrimitive( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
70 const ::basegfx::B2DHomMatrix& rTransformation ) const override;
71
72 uno::Reference< rendering::XBitmap > mxBitmap;
73 CanvasSharedPtr mpCanvas;
74 rendering::RenderState maState;
75 };
76
77
78 BitmapAction::BitmapAction( const ::Bitmap& rBmpEx,
79 const ::basegfx::B2DPoint& rDstPoint,
80 const CanvasSharedPtr& rCanvas,
81 const OutDevState& rState ) :
82 CachedPrimitiveBase( rCanvas, true ),
83 mxBitmap( vcl::unotools::xBitmapFromBitmap( rBmpEx ) ),
84 mpCanvas( rCanvas )
85 {
86 cppcanvastools::initRenderState(maState,rState);
87
88 // Setup transformation such that the next render call is
89 // moved rPoint away.
90 const basegfx::B2DHomMatrix aLocalTransformation(basegfx::utils::createTranslateB2DHomMatrix(rDstPoint));
91 ::canvastools::appendToRenderState( maState,
92 aLocalTransformation );
93
94 // correct clip (which is relative to original transform)
95 cppcanvastools::modifyClip( maState,
96 rState,
97 rCanvas,
98 rDstPoint,
99 nullptr,
100 nullptr );
101 }
102
103 BitmapAction::BitmapAction( const ::Bitmap& rBmp,
104 const ::basegfx::B2DPoint& rDstPoint,
105 const ::basegfx::B2DVector& rDstSize,
106 const CanvasSharedPtr& rCanvas,
107 const OutDevState& rState ) :
108 CachedPrimitiveBase( rCanvas, true ),
109 mxBitmap( vcl::unotools::xBitmapFromBitmap( rBmp ) ),
110 mpCanvas( rCanvas )
111 {
112 cppcanvastools::initRenderState(maState,rState);
113
114 // Setup transformation such that the next render call is
115 // moved rPoint away, and scaled according to the ratio
116 // given by src and dst size.
117 const ::Size aBmpSize( rBmp.GetSizePixel() );
118
119 const ::basegfx::B2DVector aScale( rDstSize.getX() / aBmpSize.Width(),
120 rDstSize.getY() / aBmpSize.Height() );
121 const basegfx::B2DHomMatrix aLocalTransformation(basegfx::utils::createScaleTranslateB2DHomMatrix(
122 aScale, rDstPoint));
123 ::canvastools::appendToRenderState( maState, aLocalTransformation );
124
125 // correct clip (which is relative to original transform)
126 cppcanvastools::modifyClip( maState,
127 rState,
128 rCanvas,
129 rDstPoint,
130 &aScale,
131 nullptr );
132 }
133
134 bool BitmapAction::renderPrimitive( uno::Reference< rendering::XCachedPrimitive >& rCachedPrimitive,
135 const ::basegfx::B2DHomMatrix& rTransformation ) const
136 {
137 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::BitmapAction::renderPrimitive()" );
138 SAL_INFO( "cppcanvas.emf", "::cppcanvas::internal::BitmapAction: 0x" << std::hex << this );
139
140 rendering::RenderState aLocalState( maState );
141 ::canvastools::prependToRenderState(aLocalState, rTransformation);
142
143 rCachedPrimitive = mpCanvas->getUNOCanvas()->drawBitmap( mxBitmap,
144 mpCanvas->getViewState(),
145 aLocalState );
146
147 return true;
148 }
149
150 bool BitmapAction::renderSubset( const ::basegfx::B2DHomMatrix& rTransformation,
151 const Subset& rSubset ) const
152 {
153 // bitmap only contains a single action, fail if subset
154 // requests different range
155 if( rSubset.mnSubsetBegin != 0 ||
156 rSubset.mnSubsetEnd != 1 )
157 return false;
158
159 return CachedPrimitiveBase::render( rTransformation );
160 }
161
162 ::basegfx::B2DRange BitmapAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation ) const
163 {
164 rendering::RenderState aLocalState( maState );
165 ::canvastools::prependToRenderState(aLocalState, rTransformation);
166
167 const geometry::IntegerSize2D aSize( mxBitmap->getSize() );
168
169 return cppcanvastools::calcDevicePixelBounds( ::basegfx::B2DRange( 0,0,
170 aSize.Width,
171 aSize.Height ),
172 mpCanvas->getViewState(),
173 aLocalState );
174 }
175
176 ::basegfx::B2DRange BitmapAction::getBounds( const ::basegfx::B2DHomMatrix& rTransformation,
177 const Subset& rSubset ) const
178 {
179 // bitmap only contains a single action, empty bounds
180 // if subset requests different range
181 if( rSubset.mnSubsetBegin != 0 ||
182 rSubset.mnSubsetEnd != 1 )
183 return ::basegfx::B2DRange();
184
185 return getBounds( rTransformation );
186 }
187
188 sal_Int32 BitmapAction::getActionCount() const
189 {
190 return 1;
191 }
192 }
193
194 std::shared_ptr<Action> BitmapActionFactory::createBitmapAction( const ::Bitmap& rBmp,
195 const ::basegfx::B2DPoint& rDstPoint,
196 const CanvasSharedPtr& rCanvas,
197 const OutDevState& rState )
198 {
199 return std::make_shared<BitmapAction>(rBmp, rDstPoint, rCanvas, rState );
200 }
201
202 std::shared_ptr<Action> BitmapActionFactory::createBitmapAction( const ::Bitmap& rBmp,
203 const ::basegfx::B2DPoint& rDstPoint,
204 const ::basegfx::B2DVector& rDstSize,
205 const CanvasSharedPtr& rCanvas,
206 const OutDevState& rState )
207 {
208 return std::make_shared<BitmapAction>(rBmp,
209 rDstPoint,
210 rDstSize,
211 rCanvas,
212 rState );
213 }
214 }
215
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */