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