1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _CAIROCANVAS_CANVASHELPER_HXX_
25 #define _CAIROCANVAS_CANVASHELPER_HXX_
26 
27 #include <com/sun/star/rendering/XCanvas.hpp>
28 #include <com/sun/star/rendering/XIntegerBitmap.hpp>
29 
30 #include <basegfx/vector/b2isize.hxx>
31 #include <basegfx/vector/b2dsize.hxx>
32 #include <com/sun/star/rendering/XLinePolyPolygon2D.hpp>
33 
34 #include <boost/utility.hpp>
35 
36 #include "cairo_cairo.hxx"
37 #include "cairo_surfaceprovider.hxx"
38 
39 class VirtualDevice;
40 
41 namespace basegfx {
42     class B2DPolyPolygon;
43 }
44 
45 namespace cairocanvas
46 {
47     class SpriteCanvas;
48 
49 	enum Operation {
50 	    Stroke,
51 	    Fill,
52 	    Clip
53 	};
54 
55     class CanvasHelper : private ::boost::noncopyable
56     {
57     public:
58         CanvasHelper();
59 
60         /// Release all references
61         void disposing();
62 
63 		/** Initialize canvas helper
64 
65         	This method late-initializes the canvas helper, providing
66         	it with the necessary device and size. Note that the
67         	CanvasHelper does <em>not</em> take ownership of the
68         	passed rDevice reference, nor does it perform any
69         	reference counting. Thus, to prevent the reference counted
70         	SpriteCanvas object from deletion, the user of this class
71         	is responsible for holding ref-counted references itself!
72 
73             @param rSizePixel
74             Size of the output surface in pixel.
75 
76         	@param rDevice
77             Reference device this canvas is associated with
78 
79          */
80         void init( const ::basegfx::B2ISize& rSizePixel,
81                    SurfaceProvider&          rSurfaceProvider,
82                    ::com::sun::star::rendering::XGraphicDevice* pDevice );
83 
84         void setSize( const ::basegfx::B2ISize& rSize );
85         void setSurface( const ::cairo::SurfaceSharedPtr& pSurface, bool bHasAlpha );
86 
87         // CanvasHelper functionality
88         // ==========================
89 
90         // XCanvas (only providing, not implementing the
91         // interface. Also note subtle method parameter differences)
92         void clear();
93         void drawPoint( const ::com::sun::star::rendering::XCanvas* 	pCanvas,
94                         const ::com::sun::star::geometry::RealPoint2D& 	aPoint,
95                         const ::com::sun::star::rendering::ViewState& 	viewState,
96                         const ::com::sun::star::rendering::RenderState& renderState );
97         void drawLine( const ::com::sun::star::rendering::XCanvas* 		pCanvas,
98                        const ::com::sun::star::geometry::RealPoint2D& 	aStartPoint,
99                        const ::com::sun::star::geometry::RealPoint2D& 	aEndPoint,
100                        const ::com::sun::star::rendering::ViewState& 	viewState,
101                        const ::com::sun::star::rendering::RenderState& 	renderState	);
102         void drawBezier( const ::com::sun::star::rendering::XCanvas* 			pCanvas,
103                          const ::com::sun::star::geometry::RealBezierSegment2D&	aBezierSegment,
104                          const ::com::sun::star::geometry::RealPoint2D& 		aEndPoint,
105                          const ::com::sun::star::rendering::ViewState& 			viewState,
106                          const ::com::sun::star::rendering::RenderState& 		renderState );
107         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive >
108         	drawPolyPolygon( const ::com::sun::star::rendering::XCanvas* 			pCanvas,
109                              const ::com::sun::star::uno::Reference<
110                              		::com::sun::star::rendering::XPolyPolygon2D >&	xPolyPolygon,
111                              const ::com::sun::star::rendering::ViewState& 			viewState,
112                              const ::com::sun::star::rendering::RenderState& 		renderState );
113         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive >
114         	strokePolyPolygon( const ::com::sun::star::rendering::XCanvas* 			pCanvas,
115                                const ::com::sun::star::uno::Reference<
116                                		::com::sun::star::rendering::XPolyPolygon2D >& 	xPolyPolygon,
117                                const ::com::sun::star::rendering::ViewState& 		viewState,
118                                const ::com::sun::star::rendering::RenderState& 		renderState,
119                                const ::com::sun::star::rendering::StrokeAttributes& strokeAttributes );
120         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive >
121         	strokeTexturedPolyPolygon( const ::com::sun::star::rendering::XCanvas* 			pCanvas,
122                                        const ::com::sun::star::uno::Reference<
123                                        		::com::sun::star::rendering::XPolyPolygon2D >& 	xPolyPolygon,
124                                        const ::com::sun::star::rendering::ViewState& 		viewState,
125                                        const ::com::sun::star::rendering::RenderState& 		renderState,
126                                        const ::com::sun::star::uno::Sequence<
127                                        		::com::sun::star::rendering::Texture >& 		textures,
128                                        const ::com::sun::star::rendering::StrokeAttributes& strokeAttributes );
129         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive >
130         	strokeTextureMappedPolyPolygon( const ::com::sun::star::rendering::XCanvas* 			pCanvas,
131                                             const ::com::sun::star::uno::Reference<
132                                             		::com::sun::star::rendering::XPolyPolygon2D >& 	xPolyPolygon,
133                                             const ::com::sun::star::rendering::ViewState& 			viewState,
134                                             const ::com::sun::star::rendering::RenderState& 		renderState,
135                                             const ::com::sun::star::uno::Sequence<
136                                             		::com::sun::star::rendering::Texture >& 		textures,
137                                             const ::com::sun::star::uno::Reference<
138                                             		::com::sun::star::geometry::XMapping2D >& 		xMapping,
139                                             const ::com::sun::star::rendering::StrokeAttributes& 	strokeAttributes );
140         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XPolyPolygon2D >
141         	queryStrokeShapes( const ::com::sun::star::rendering::XCanvas* 			pCanvas,
142                                const ::com::sun::star::uno::Reference<
143                                		::com::sun::star::rendering::XPolyPolygon2D >& 	xPolyPolygon,
144                                const ::com::sun::star::rendering::ViewState& 		viewState,
145                                const ::com::sun::star::rendering::RenderState& 		renderState,
146                                const ::com::sun::star::rendering::StrokeAttributes& strokeAttributes );
147         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive >
148         	fillPolyPolygon( const ::com::sun::star::rendering::XCanvas* 			pCanvas,
149                              const ::com::sun::star::uno::Reference<
150                              		::com::sun::star::rendering::XPolyPolygon2D >& 	xPolyPolygon,
151                              const ::com::sun::star::rendering::ViewState& 			viewState,
152                              const ::com::sun::star::rendering::RenderState& 		renderState );
153         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive >
154         	fillTexturedPolyPolygon( const ::com::sun::star::rendering::XCanvas* 			pCanvas,
155                                      const ::com::sun::star::uno::Reference<
156                                      		::com::sun::star::rendering::XPolyPolygon2D >& 	xPolyPolygon,
157                                      const ::com::sun::star::rendering::ViewState& 			viewState,
158                                      const ::com::sun::star::rendering::RenderState& 		renderState,
159                                      const ::com::sun::star::uno::Sequence<
160                                      		::com::sun::star::rendering::Texture >& 		textures );
161         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive >
162         	fillTextureMappedPolyPolygon( const ::com::sun::star::rendering::XCanvas* 			pCanvas,
163                                           const ::com::sun::star::uno::Reference<
164                                           		::com::sun::star::rendering::XPolyPolygon2D >& 	xPolyPolygon,
165                                           const ::com::sun::star::rendering::ViewState& 		viewState,
166                                           const ::com::sun::star::rendering::RenderState& 		renderState,
167                                           const ::com::sun::star::uno::Sequence<
168                                           		::com::sun::star::rendering::Texture >& 		textures,
169                                           const ::com::sun::star::uno::Reference<
170                                           		::com::sun::star::geometry::XMapping2D >& 		xMapping );
171 
172         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCanvasFont > SAL_CALL
173         	createFont( const ::com::sun::star::rendering::XCanvas* 			pCanvas,
174                         const ::com::sun::star::rendering::FontRequest& 		fontRequest,
175                         const ::com::sun::star::uno::Sequence<
176                         		::com::sun::star::beans::PropertyValue >& 		extraFontProperties,
177                         const ::com::sun::star::geometry::Matrix2D& 			fontMatrix );
178 
179         ::com::sun::star::uno::Sequence< ::com::sun::star::rendering::FontInfo >
180         	queryAvailableFonts( const ::com::sun::star::rendering::XCanvas* 		pCanvas,
181                                  const ::com::sun::star::rendering::FontInfo& 		aFilter,
182                                  const ::com::sun::star::uno::Sequence<
183                                  		::com::sun::star::beans::PropertyValue >& 	aFontProperties );
184 
185         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive >
186         	drawText( const ::com::sun::star::rendering::XCanvas* 			pCanvas,
187                       const ::com::sun::star::rendering::StringContext& 	text,
188                       const ::com::sun::star::uno::Reference<
189                       		::com::sun::star::rendering::XCanvasFont >& 	xFont,
190                       const ::com::sun::star::rendering::ViewState& 		viewState,
191                       const ::com::sun::star::rendering::RenderState& 		renderState,
192                       sal_Int8 												textDirection );
193 
194         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive >
195         	drawTextLayout( const ::com::sun::star::rendering::XCanvas* 	pCanvas,
196                             const ::com::sun::star::uno::Reference<
197                             	::com::sun::star::rendering::XTextLayout >& layoutetText,
198                             const ::com::sun::star::rendering::ViewState& 	viewState,
199                             const ::com::sun::star::rendering::RenderState& renderState );
200 
201         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive >
202         	drawBitmap( const ::com::sun::star::rendering::XCanvas* 	pCanvas,
203                         const ::com::sun::star::uno::Reference<
204                         		::com::sun::star::rendering::XBitmap >& xBitmap,
205                         const ::com::sun::star::rendering::ViewState& 	viewState,
206                         const ::com::sun::star::rendering::RenderState& renderState );
207         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive >
208         	drawBitmapModulated( const ::com::sun::star::rendering::XCanvas* 		pCanvas,
209                                  const ::com::sun::star::uno::Reference<
210                                  		::com::sun::star::rendering::XBitmap >&		xBitmap,
211                                  const ::com::sun::star::rendering::ViewState& 		viewState,
212                                  const ::com::sun::star::rendering::RenderState& 	renderState );
213         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XGraphicDevice >
214         	getDevice();
215 
216         // BitmapCanvasHelper functionality
217         // ================================
218 
219         void copyRect( const ::com::sun::star::rendering::XCanvas* 			pCanvas,
220                        const ::com::sun::star::uno::Reference<
221                        		::com::sun::star::rendering::XBitmapCanvas >& 	sourceCanvas,
222                        const ::com::sun::star::geometry::RealRectangle2D& 	sourceRect,
223                        const ::com::sun::star::rendering::ViewState& 		sourceViewState,
224                        const ::com::sun::star::rendering::RenderState& 		sourceRenderState,
225                        const ::com::sun::star::geometry::RealRectangle2D& 	destRect,
226                        const ::com::sun::star::rendering::ViewState& 		destViewState,
227                        const ::com::sun::star::rendering::RenderState& 		destRenderState );
228 
229         ::com::sun::star::geometry::IntegerSize2D getSize();
230 
231         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XBitmap >
232         	getScaledBitmap( const ::com::sun::star::geometry::RealSize2D& 	newSize,
233                              sal_Bool 										beFast );
234 
235         ::com::sun::star::uno::Sequence< sal_Int8 >
236 	        getData( ::com::sun::star::rendering::IntegerBitmapLayout& 		bitmapLayout,
237                      const ::com::sun::star::geometry::IntegerRectangle2D&  rect );
238 
239         void setData( const ::com::sun::star::uno::Sequence< sal_Int8 >&         data,
240                       const ::com::sun::star::rendering::IntegerBitmapLayout&    bitmapLayout,
241                       const ::com::sun::star::geometry::IntegerRectangle2D&      rect );
242 
243         void setPixel( const ::com::sun::star::uno::Sequence< sal_Int8 >&        color,
244                        const ::com::sun::star::rendering::IntegerBitmapLayout&   bitmapLayout,
245                        const ::com::sun::star::geometry::IntegerPoint2D&         pos );
246 
247         ::com::sun::star::uno::Sequence< sal_Int8 >
248 	        getPixel( ::com::sun::star::rendering::IntegerBitmapLayout& bitmapLayout,
249                       const ::com::sun::star::geometry::IntegerPoint2D& pos );
250 
251         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XBitmapPalette > getPalette();
252 
253         ::com::sun::star::rendering::IntegerBitmapLayout getMemoryLayout();
254 
255         // Flush drawing queue to screen
256         void					flush() const;
257 
258         /** Called from XCanvas base classes, to notify that content
259             is _about_ to change
260         */
modifying()261         void modifying() {}
262 
263         bool hasAlpha() const;
264 
265         enum ColorType
266         {
267             LINE_COLOR, FILL_COLOR, TEXT_COLOR, IGNORE_COLOR
268         };
269 
270 	void doPolyPolygonPath( const ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XPolyPolygon2D >& xPolyPolygon,
271 				  Operation aOperation,
272                   bool bNoLineJoin = false,
273 				  const ::com::sun::star::uno::Sequence< ::com::sun::star::rendering::Texture >* pTextures=NULL,
274 				  ::cairo::Cairo* pCairo=NULL ) const;
275 
276         ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XCachedPrimitive >
277 	implDrawBitmapSurface(
278 			       const ::com::sun::star::rendering::XCanvas* 	    pCanvas,
279                    const ::cairo::SurfaceSharedPtr&                 pSurface,
280 			       const ::com::sun::star::rendering::ViewState& 	viewState,
281 			       const ::com::sun::star::rendering::RenderState&  renderState,
282 			       const ::com::sun::star::geometry::IntegerSize2D& rSize,
283 			       bool bModulateColors,
284 			       bool bHasAlpha );
285 
286         bool repaint( const ::cairo::SurfaceSharedPtr& pSurface,
287 		      const ::com::sun::star::rendering::ViewState&	viewState,
288 		      const ::com::sun::star::rendering::RenderState&	renderState );
289 
290     protected:
291         /** Surface provider
292 
293             Deliberately not a refcounted reference, because of
294             potential circular references for canvas. Provides us with
295             our output surface and associated functionality.
296          */
297         SurfaceProvider* mpSurfaceProvider;
298 
299         /** Phyical output device
300 
301             Deliberately not a refcounted reference, because of
302             potential circular references for spritecanvas.
303          */
304         ::com::sun::star::rendering::XGraphicDevice* mpDevice;
305 
306     private:
307 
308 		boost::shared_ptr<VirtualDevice> mpVirtualDevice;
309 
310 	void useStates( const ::com::sun::star::rendering::ViewState& viewState,
311 			const ::com::sun::star::rendering::RenderState& renderState,
312 			bool setColor );
313 
314         /// When true, content is able to represent alpha
315         bool mbHaveAlpha;
316 
317 		CairoSharedPtr     mpCairo;
318 		SurfaceSharedPtr   mpSurface;
319         ::basegfx::B2ISize maSize;
320     };
321 
322     /// also needed from SpriteHelper
323     void doPolyPolygonImplementation( ::basegfx::B2DPolyPolygon aPolyPolygon,
324                                       Operation aOperation,
325                                       ::cairo::Cairo* pCairo,
326                                       const ::com::sun::star::uno::Sequence< ::com::sun::star::rendering::Texture >* pTextures,
327                                       const SurfaceProviderRef& pDevice,
328                                       ::com::sun::star::rendering::FillRule eFillrule );
329 }
330 
331 #endif /* _CAIROCANVAS_CANVASHELPER_HXX_ */
332