Line data Source code
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/XSimpleCanvas.hpp>
22 : #include <com/sun/star/rendering/CompositeOperation.hpp>
23 : #include <com/sun/star/rendering/PanoseLetterForm.hpp>
24 : #include <com/sun/star/rendering/PanoseWeight.hpp>
25 : #include <com/sun/star/lang/XServiceName.hpp>
26 :
27 : #include <o3tl/lazy_update.hxx>
28 : #include <cppuhelper/factory.hxx>
29 : #include <cppuhelper/implementationentry.hxx>
30 : #include <cppuhelper/compbase2.hxx>
31 : #include <cppuhelper/basemutex.hxx>
32 :
33 : #include <comphelper/servicedecl.hxx>
34 :
35 : #include <basegfx/matrix/b2dhommatrix.hxx>
36 : #include <basegfx/matrix/b2dhommatrixtools.hxx>
37 :
38 : #include <canvas/canvastools.hxx>
39 :
40 : #include <boost/bind.hpp>
41 :
42 : #define SERVICE_NAME "com.sun.star.rendering.SimpleCanvas"
43 :
44 : using namespace ::com::sun::star;
45 : using namespace canvas;
46 :
47 : namespace
48 : {
49 0 : inline uno::Sequence< double > color2Sequence( sal_Int32 const& nColor )
50 : {
51 : // TODO(F3): Color management
52 0 : uno::Sequence< double > aRes( 4 );
53 :
54 0 : aRes[0] = static_cast<sal_uInt8>( (nColor&0xFF000000U) >> 24U ) / 255.0;
55 0 : aRes[1] = static_cast<sal_uInt8>( (nColor&0x00FF0000U) >> 16U ) / 255.0;
56 0 : aRes[2] = static_cast<sal_uInt8>( (nColor&0x0000FF00U) >> 8U ) / 255.0;
57 0 : aRes[3] = static_cast<sal_uInt8>( (nColor&0x000000FFU) ) / 255.0;
58 :
59 0 : return aRes;
60 : }
61 :
62 0 : inline uno::Reference< rendering::XPolyPolygon2D > rect2Poly( uno::Reference<rendering::XGraphicDevice> const& xDevice,
63 : geometry::RealRectangle2D const& rRect )
64 : {
65 0 : uno::Sequence< geometry::RealPoint2D > rectSequence( 4 );
66 0 : geometry::RealPoint2D* pOutput = rectSequence.getArray();
67 0 : pOutput[0] = geometry::RealPoint2D( rRect.X1, rRect.Y1 );
68 0 : pOutput[1] = geometry::RealPoint2D( rRect.X2, rRect.Y1 );
69 0 : pOutput[2] = geometry::RealPoint2D( rRect.X2, rRect.Y2 );
70 0 : pOutput[3] = geometry::RealPoint2D( rRect.X1, rRect.Y2 );
71 :
72 0 : uno::Sequence< uno::Sequence< geometry::RealPoint2D > > sequenceSequence( 1 );
73 0 : sequenceSequence[0] = rectSequence;
74 :
75 : uno::Reference< rendering::XPolyPolygon2D > xRes(
76 0 : xDevice->createCompatibleLinePolyPolygon( sequenceSequence ),
77 0 : uno::UNO_QUERY );
78 0 : if( xRes.is() )
79 0 : xRes->setClosed( 0, sal_True );
80 0 : return xRes;
81 : }
82 :
83 0 : struct SimpleRenderState
84 : {
85 : o3tl::LazyUpdate<sal_Int32,
86 : uno::Sequence<double>,
87 : o3tl::LAZYUPDATE_FUNCTION_TAG > m_aPenColor;
88 : o3tl::LazyUpdate<sal_Int32,
89 : uno::Sequence<double>,
90 : o3tl::LAZYUPDATE_FUNCTION_TAG > m_aFillColor;
91 : o3tl::LazyUpdate<geometry::RealRectangle2D,
92 : uno::Reference< rendering::XPolyPolygon2D >,
93 : o3tl::LAZYUPDATE_FUNCTOR_TAG > m_aRectClip;
94 : geometry::AffineMatrix2D m_aTransformation;
95 :
96 0 : explicit SimpleRenderState( uno::Reference<rendering::XGraphicDevice> const& xDevice ) :
97 : m_aPenColor( &color2Sequence),
98 : m_aFillColor( &color2Sequence ),
99 : m_aRectClip( boost::bind( &rect2Poly,
100 : xDevice,
101 : _1 )),
102 0 : m_aTransformation()
103 : {
104 0 : tools::setIdentityAffineMatrix2D( m_aTransformation );
105 0 : }
106 : };
107 :
108 :
109 : typedef ::cppu::WeakComponentImplHelper2< ::com::sun::star::rendering::XSimpleCanvas,
110 : ::com::sun::star::lang::XServiceName > SimpleCanvasBase;
111 :
112 0 : class SimpleCanvasImpl : private cppu::BaseMutex,
113 : public SimpleCanvasBase
114 : {
115 : private:
116 0 : bool isStrokingEnabled() const
117 : {
118 0 : return maRenderState.m_aPenColor.getInValue() && sal_Int32(0xFF) != 0;
119 : }
120 :
121 0 : rendering::RenderState createStrokingRenderState() const
122 : {
123 : return rendering::RenderState(maRenderState.m_aTransformation,
124 0 : *maRenderState.m_aRectClip,
125 0 : *maRenderState.m_aPenColor,
126 0 : rendering::CompositeOperation::OVER);
127 : }
128 :
129 0 : bool isFillingEnabled() const
130 : {
131 0 : return maRenderState.m_aFillColor.getInValue() && sal_Int32(0xFF) != 0;
132 : }
133 :
134 0 : rendering::RenderState createFillingRenderState() const
135 : {
136 : return rendering::RenderState(maRenderState.m_aTransformation,
137 0 : *maRenderState.m_aRectClip,
138 0 : *maRenderState.m_aFillColor,
139 0 : rendering::CompositeOperation::OVER);
140 : }
141 :
142 0 : static uno::Reference<rendering::XCanvas> grabCanvas( uno::Sequence<uno::Any> const& rArgs )
143 : {
144 0 : uno::Reference<rendering::XCanvas> xRet;
145 :
146 : // can't do much without an XCanvas, can't we?
147 0 : if( rArgs.getLength() < 1 )
148 0 : throw lang::IllegalArgumentException();
149 :
150 0 : xRet.set( rArgs[0], uno::UNO_QUERY );
151 :
152 : // can't do much without an XCanvas, can't we?
153 0 : if( !xRet.is() )
154 0 : throw lang::IllegalArgumentException();
155 :
156 0 : return xRet;
157 : }
158 :
159 : public:
160 0 : SimpleCanvasImpl( const uno::Sequence< uno::Any >& aArguments,
161 : const uno::Reference< uno::XComponentContext >& ) :
162 : SimpleCanvasBase( m_aMutex ),
163 : mxCanvas( grabCanvas(aArguments) ),
164 : maFont(boost::bind( &rendering::XCanvas::createFont,
165 : boost::cref(mxCanvas),
166 : _1,
167 : uno::Sequence< beans::PropertyValue >(),
168 : geometry::Matrix2D() )),
169 : maViewState(),
170 0 : maRenderState( mxCanvas->getDevice() )
171 : {
172 0 : tools::initViewState(maViewState);
173 0 : }
174 :
175 :
176 :
177 : private:
178 : // Ifc XServiceName
179 0 : virtual OUString SAL_CALL getServiceName( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
180 : {
181 0 : return OUString( SERVICE_NAME );
182 : }
183 :
184 : // Ifc XSimpleCanvas
185 0 : virtual void SAL_CALL selectFont( const OUString& sFontName,
186 : double size,
187 : sal_Bool bold,
188 : sal_Bool italic ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
189 : {
190 0 : ::osl::MutexGuard aGuard( m_aMutex );
191 :
192 0 : maFont->FontDescription.FamilyName = sFontName;
193 0 : maFont->CellSize = size;
194 0 : maFont->FontDescription.FontDescription.Weight =
195 0 : bold ? rendering::PanoseWeight::BOLD : rendering::PanoseWeight::MEDIUM;
196 0 : maFont->FontDescription.FontDescription.Letterform =
197 0 : italic ? rendering::PanoseLetterForm::OBLIQUE_CONTACT : rendering::PanoseLetterForm::ANYTHING;
198 0 : }
199 :
200 0 : virtual void SAL_CALL setPenColor( ::sal_Int32 nsRgbaColor ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
201 : {
202 0 : ::osl::MutexGuard aGuard( m_aMutex );
203 0 : *(maRenderState.m_aPenColor) = nsRgbaColor;
204 0 : }
205 :
206 0 : virtual void SAL_CALL setFillColor( ::sal_Int32 nsRgbaColor ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
207 : {
208 0 : ::osl::MutexGuard aGuard( m_aMutex );
209 0 : *(maRenderState.m_aFillColor) = nsRgbaColor;
210 0 : }
211 :
212 0 : virtual void SAL_CALL setRectClip( const geometry::RealRectangle2D& aRect ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
213 : {
214 0 : ::osl::MutexGuard aGuard( m_aMutex );
215 0 : *(maRenderState.m_aRectClip) = aRect;
216 0 : }
217 :
218 0 : virtual void SAL_CALL setTransformation( const geometry::AffineMatrix2D& aTransform ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
219 : {
220 0 : ::osl::MutexGuard aGuard( m_aMutex );
221 0 : maRenderState.m_aTransformation = aTransform;
222 0 : }
223 :
224 0 : virtual void SAL_CALL drawPixel( const geometry::RealPoint2D& aPoint ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
225 : {
226 0 : ::osl::MutexGuard aGuard( m_aMutex );
227 0 : mxCanvas->drawPoint(aPoint,
228 : maViewState,
229 0 : createFillingRenderState());
230 0 : }
231 :
232 0 : virtual void SAL_CALL drawLine( const geometry::RealPoint2D& aStartPoint,
233 : const geometry::RealPoint2D& aEndPoint ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
234 : {
235 0 : ::osl::MutexGuard aGuard( m_aMutex );
236 0 : mxCanvas->drawLine(aStartPoint,
237 : aEndPoint,
238 : maViewState,
239 0 : createStrokingRenderState());
240 0 : }
241 :
242 0 : virtual void SAL_CALL drawRect( const geometry::RealRectangle2D& aRect ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
243 : {
244 0 : ::osl::MutexGuard aGuard( m_aMutex );
245 : uno::Reference< rendering::XPolyPolygon2D > xPoly(
246 0 : rect2Poly( mxCanvas->getDevice(),
247 0 : aRect));
248 :
249 0 : if( isFillingEnabled() )
250 0 : mxCanvas->drawPolyPolygon(xPoly,
251 : maViewState,
252 0 : createFillingRenderState());
253 0 : if( isStrokingEnabled() )
254 0 : mxCanvas->drawPolyPolygon(xPoly,
255 : maViewState,
256 0 : createStrokingRenderState());
257 0 : }
258 :
259 0 : virtual void SAL_CALL drawPolyPolygon( const uno::Reference< rendering::XPolyPolygon2D >& xPolyPolygon )
260 : throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
261 : {
262 0 : ::osl::MutexGuard aGuard( m_aMutex );
263 :
264 0 : if( isFillingEnabled() )
265 0 : mxCanvas->drawPolyPolygon(xPolyPolygon,
266 : maViewState,
267 0 : createFillingRenderState());
268 0 : if( isStrokingEnabled() )
269 0 : mxCanvas->drawPolyPolygon(xPolyPolygon,
270 : maViewState,
271 0 : createStrokingRenderState());
272 0 : }
273 :
274 0 : virtual void SAL_CALL drawText( const rendering::StringContext& aText,
275 : const geometry::RealPoint2D& aOutPos,
276 : ::sal_Int8 nTextDirection ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
277 : {
278 0 : ::osl::MutexGuard aGuard( m_aMutex );
279 0 : const basegfx::B2DHomMatrix offsetTransform(basegfx::tools::createTranslateB2DHomMatrix(aOutPos.X,aOutPos.Y));
280 0 : rendering::RenderState aRenderState( createStrokingRenderState() );
281 0 : tools::appendToRenderState(aRenderState, offsetTransform);
282 :
283 0 : mxCanvas->drawText(aText,
284 0 : maFont.getOutValue(),
285 : maViewState,
286 : aRenderState,
287 0 : nTextDirection);
288 0 : }
289 :
290 0 : virtual void SAL_CALL drawBitmap( const uno::Reference< rendering::XBitmap >& xBitmap,
291 : const geometry::RealPoint2D& aLeftTop )
292 : throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
293 : {
294 0 : ::osl::MutexGuard aGuard( m_aMutex );
295 0 : const basegfx::B2DHomMatrix offsetTransform(basegfx::tools::createTranslateB2DHomMatrix(aLeftTop.X,aLeftTop.Y));
296 0 : rendering::RenderState aRenderState( createStrokingRenderState() );
297 0 : tools::appendToRenderState(aRenderState, offsetTransform);
298 :
299 0 : mxCanvas->drawBitmap(xBitmap,maViewState,aRenderState);
300 0 : }
301 :
302 0 : virtual uno::Reference< rendering::XGraphicDevice > SAL_CALL getDevice( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
303 : {
304 0 : ::osl::MutexGuard aGuard( m_aMutex );
305 0 : return mxCanvas->getDevice();
306 : }
307 :
308 0 : virtual uno::Reference< rendering::XCanvas > SAL_CALL getCanvas( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
309 : {
310 0 : ::osl::MutexGuard aGuard( m_aMutex );
311 0 : return mxCanvas;
312 : }
313 :
314 0 : virtual rendering::FontMetrics SAL_CALL getFontMetrics( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
315 : {
316 0 : ::osl::MutexGuard aGuard( m_aMutex );
317 0 : return maFont.getOutValue()->getFontMetrics();
318 : }
319 :
320 0 : virtual uno::Reference< rendering::XCanvasFont > SAL_CALL getCurrentFont( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
321 : {
322 0 : ::osl::MutexGuard aGuard( m_aMutex );
323 0 : return maFont.getOutValue();
324 : }
325 :
326 0 : virtual ::sal_Int32 SAL_CALL getCurrentPenColor( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
327 : {
328 0 : ::osl::MutexGuard aGuard( m_aMutex );
329 0 : return maRenderState.m_aPenColor.getInValue();
330 : }
331 :
332 0 : virtual ::sal_Int32 SAL_CALL getCurrentFillColor( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
333 : {
334 0 : ::osl::MutexGuard aGuard( m_aMutex );
335 0 : return maRenderState.m_aFillColor.getInValue();
336 : }
337 :
338 0 : virtual geometry::RealRectangle2D SAL_CALL getCurrentClipRect( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
339 : {
340 0 : ::osl::MutexGuard aGuard( m_aMutex );
341 0 : return maRenderState.m_aRectClip.getInValue();
342 : }
343 :
344 0 : virtual geometry::AffineMatrix2D SAL_CALL getCurrentTransformation( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
345 : {
346 0 : ::osl::MutexGuard aGuard( m_aMutex );
347 0 : return maRenderState.m_aTransformation;
348 : }
349 :
350 0 : virtual rendering::ViewState SAL_CALL getCurrentViewState( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
351 : {
352 0 : ::osl::MutexGuard aGuard( m_aMutex );
353 0 : return maViewState;
354 : }
355 :
356 0 : virtual rendering::RenderState SAL_CALL getCurrentRenderState( sal_Bool bUseFillColor ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
357 : {
358 0 : ::osl::MutexGuard aGuard( m_aMutex );
359 0 : if( bUseFillColor )
360 0 : return createFillingRenderState();
361 : else
362 0 : return createStrokingRenderState();
363 : }
364 :
365 :
366 :
367 : typedef o3tl::LazyUpdate<
368 : rendering::FontRequest,
369 : uno::Reference< rendering::XCanvasFont >,
370 : o3tl::LAZYUPDATE_FUNCTOR_TAG > SimpleFont;
371 :
372 : uno::Reference<rendering::XCanvas> mxCanvas;
373 : SimpleFont maFont;
374 : rendering::ViewState maViewState;
375 : SimpleRenderState maRenderState;
376 : };
377 :
378 : namespace sdecl = comphelper::service_decl;
379 0 : const sdecl::ServiceDecl simpleCanvasDecl(
380 : sdecl::class_<SimpleCanvasImpl, sdecl::with_args<true> >(),
381 : "com.sun.star.comp.rendering.SimpleCanvas",
382 0 : SERVICE_NAME );
383 : }
384 :
385 : // The C shared lib entry points
386 0 : COMPHELPER_SERVICEDECL_EXPORTS1(simplecanvas, simpleCanvasDecl)
387 :
388 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|