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 ::rtl::OUString SAL_CALL getServiceName( ) throw (uno::RuntimeException)
180 : {
181 0 : return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICE_NAME ) );
182 : }
183 :
184 : // Ifc XSimpleCanvas
185 0 : virtual void SAL_CALL selectFont( const ::rtl::OUString& sFontName,
186 : double size,
187 : ::sal_Bool bold,
188 : ::sal_Bool italic ) throw (uno::RuntimeException)
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)
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)
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)
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)
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)
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)
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)
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 ) throw (uno::RuntimeException)
260 : {
261 0 : ::osl::MutexGuard aGuard( m_aMutex );
262 :
263 0 : if( isFillingEnabled() )
264 0 : mxCanvas->drawPolyPolygon(xPolyPolygon,
265 : maViewState,
266 0 : createFillingRenderState());
267 0 : if( isStrokingEnabled() )
268 0 : mxCanvas->drawPolyPolygon(xPolyPolygon,
269 : maViewState,
270 0 : createStrokingRenderState());
271 0 : }
272 :
273 0 : virtual void SAL_CALL drawText( const rendering::StringContext& aText,
274 : const geometry::RealPoint2D& aOutPos,
275 : ::sal_Int8 nTextDirection ) throw (uno::RuntimeException)
276 : {
277 0 : ::osl::MutexGuard aGuard( m_aMutex );
278 0 : const basegfx::B2DHomMatrix offsetTransform(basegfx::tools::createTranslateB2DHomMatrix(aOutPos.X,aOutPos.Y));
279 0 : rendering::RenderState aRenderState( createStrokingRenderState() );
280 0 : tools::appendToRenderState(aRenderState, offsetTransform);
281 :
282 0 : mxCanvas->drawText(aText,
283 0 : maFont.getOutValue(),
284 : maViewState,
285 : aRenderState,
286 0 : nTextDirection);
287 0 : }
288 :
289 0 : virtual void SAL_CALL drawBitmap( const uno::Reference< rendering::XBitmap >& xBitmap,
290 : const geometry::RealPoint2D& aLeftTop ) throw (uno::RuntimeException)
291 : {
292 0 : ::osl::MutexGuard aGuard( m_aMutex );
293 0 : const basegfx::B2DHomMatrix offsetTransform(basegfx::tools::createTranslateB2DHomMatrix(aLeftTop.X,aLeftTop.Y));
294 0 : rendering::RenderState aRenderState( createStrokingRenderState() );
295 0 : tools::appendToRenderState(aRenderState, offsetTransform);
296 :
297 0 : mxCanvas->drawBitmap(xBitmap,maViewState,aRenderState);
298 0 : }
299 :
300 0 : virtual uno::Reference< rendering::XGraphicDevice > SAL_CALL getDevice( ) throw (uno::RuntimeException)
301 : {
302 0 : ::osl::MutexGuard aGuard( m_aMutex );
303 0 : return mxCanvas->getDevice();
304 : }
305 :
306 0 : virtual uno::Reference< rendering::XCanvas > SAL_CALL getCanvas( ) throw (uno::RuntimeException)
307 : {
308 0 : ::osl::MutexGuard aGuard( m_aMutex );
309 0 : return mxCanvas;
310 : }
311 :
312 0 : virtual rendering::FontMetrics SAL_CALL getFontMetrics( ) throw (uno::RuntimeException)
313 : {
314 0 : ::osl::MutexGuard aGuard( m_aMutex );
315 0 : return maFont.getOutValue()->getFontMetrics();
316 : }
317 :
318 0 : virtual uno::Reference< rendering::XCanvasFont > SAL_CALL getCurrentFont( ) throw (uno::RuntimeException)
319 : {
320 0 : ::osl::MutexGuard aGuard( m_aMutex );
321 0 : return maFont.getOutValue();
322 : }
323 :
324 0 : virtual ::sal_Int32 SAL_CALL getCurrentPenColor( ) throw (uno::RuntimeException)
325 : {
326 0 : ::osl::MutexGuard aGuard( m_aMutex );
327 0 : return maRenderState.m_aPenColor.getInValue();
328 : }
329 :
330 0 : virtual ::sal_Int32 SAL_CALL getCurrentFillColor( ) throw (uno::RuntimeException)
331 : {
332 0 : ::osl::MutexGuard aGuard( m_aMutex );
333 0 : return maRenderState.m_aFillColor.getInValue();
334 : }
335 :
336 0 : virtual geometry::RealRectangle2D SAL_CALL getCurrentClipRect( ) throw (uno::RuntimeException)
337 : {
338 0 : ::osl::MutexGuard aGuard( m_aMutex );
339 0 : return maRenderState.m_aRectClip.getInValue();
340 : }
341 :
342 0 : virtual geometry::AffineMatrix2D SAL_CALL getCurrentTransformation( ) throw (uno::RuntimeException)
343 : {
344 0 : ::osl::MutexGuard aGuard( m_aMutex );
345 0 : return maRenderState.m_aTransformation;
346 : }
347 :
348 0 : virtual rendering::ViewState SAL_CALL getCurrentViewState( ) throw (uno::RuntimeException)
349 : {
350 0 : ::osl::MutexGuard aGuard( m_aMutex );
351 0 : return maViewState;
352 : }
353 :
354 0 : virtual rendering::RenderState SAL_CALL getCurrentRenderState( sal_Bool bUseFillColor ) throw (uno::RuntimeException)
355 : {
356 0 : ::osl::MutexGuard aGuard( m_aMutex );
357 0 : if( bUseFillColor )
358 0 : return createFillingRenderState();
359 : else
360 0 : return createStrokingRenderState();
361 : }
362 :
363 : ///////////////////////////////////////////////////////////////////////////////////////////////
364 :
365 : typedef o3tl::LazyUpdate<
366 : rendering::FontRequest,
367 : uno::Reference< rendering::XCanvasFont >,
368 : o3tl::LAZYUPDATE_FUNCTOR_TAG > SimpleFont;
369 :
370 : uno::Reference<rendering::XCanvas> mxCanvas;
371 : SimpleFont maFont;
372 : rendering::ViewState maViewState;
373 : SimpleRenderState maRenderState;
374 : };
375 :
376 : namespace sdecl = comphelper::service_decl;
377 0 : const sdecl::ServiceDecl simpleCanvasDecl(
378 : sdecl::class_<SimpleCanvasImpl, sdecl::with_args<true> >(),
379 : "com.sun.star.comp.rendering.SimpleCanvas",
380 0 : SERVICE_NAME );
381 : }
382 :
383 : // The C shared lib entry points
384 0 : COMPHELPER_SERVICEDECL_EXPORTS1(simplecanvas, simpleCanvasDecl)
385 :
386 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|