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