Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / toolkit / source / awt / vclxgraphics.cxx
blob06df80a4210646a7c94e3b6c774035601d95ec90
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <memory>
22 #include <awt/vclxgraphics.hxx>
23 #include <toolkit/awt/vclxdevice.hxx>
24 #include <toolkit/helper/vclunohelper.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/outdev.hxx>
28 #include <vcl/image.hxx>
29 #include <vcl/kernarray.hxx>
30 #include <vcl/gradient.hxx>
31 #include <vcl/metric.hxx>
32 #include <tools/debug.hxx>
34 using namespace com::sun::star;
38 VCLXGraphics::VCLXGraphics()
39 : mpOutputDevice(nullptr)
40 , meRasterOp(RasterOp::OverPaint)
44 VCLXGraphics::~VCLXGraphics()
46 std::vector< VCLXGraphics* > *pLst = mpOutputDevice ? mpOutputDevice->GetUnoGraphicsList() : nullptr;
47 if ( pLst )
49 auto it = std::find(pLst->begin(), pLst->end(), this);
50 if (it != pLst->end())
51 pLst->erase( it );
54 mpClipRegion.reset();
56 SolarMutexGuard g;
57 mpOutputDevice.reset();
60 void VCLXGraphics::SetOutputDevice( OutputDevice* pOutDev )
62 mpOutputDevice = pOutDev;
63 mxDevice = nullptr;
64 initAttrs();
67 void VCLXGraphics::Init( OutputDevice* pOutDev )
69 DBG_ASSERT( !mpOutputDevice, "VCLXGraphics::Init already has pOutDev !" );
70 mpOutputDevice = pOutDev;
72 initAttrs();
73 mpClipRegion = nullptr;
75 // Register at OutputDevice
76 std::vector< VCLXGraphics* > *pLst = mpOutputDevice->GetUnoGraphicsList();
77 if ( !pLst )
78 pLst = mpOutputDevice->CreateUnoGraphicsList();
79 pLst->push_back( this );
82 void VCLXGraphics::initAttrs()
84 if ( !mpOutputDevice )
85 return;
87 maFont = mpOutputDevice->GetFont();
88 maTextColor = mpOutputDevice->GetTextColor(); /* COL_BLACK */
89 maTextFillColor = mpOutputDevice->GetTextFillColor(); /* COL_TRANSPARENT */
90 maLineColor = mpOutputDevice->GetLineColor(); /* COL_BLACK */
91 maFillColor = mpOutputDevice->GetFillColor(); /* COL_WHITE */
92 meRasterOp = mpOutputDevice->GetRasterOp(); /* RasterOp::OverPaint */
95 void VCLXGraphics::InitOutputDevice( InitOutDevFlags nFlags )
97 if(!mpOutputDevice)
98 return;
100 SolarMutexGuard aVclGuard;
102 if ( nFlags & InitOutDevFlags::FONT )
104 mpOutputDevice->SetFont( maFont );
105 mpOutputDevice->SetTextColor( maTextColor );
106 mpOutputDevice->SetTextFillColor( maTextFillColor );
109 if ( nFlags & InitOutDevFlags::COLORS )
111 mpOutputDevice->SetLineColor( maLineColor );
112 mpOutputDevice->SetFillColor( maFillColor );
115 mpOutputDevice->SetRasterOp( meRasterOp );
117 if( mpClipRegion )
118 mpOutputDevice->SetClipRegion( *mpClipRegion );
119 else
120 mpOutputDevice->SetClipRegion();
123 uno::Reference< awt::XDevice > VCLXGraphics::getDevice()
125 SolarMutexGuard aGuard;
127 if( !mxDevice.is() && mpOutputDevice )
129 rtl::Reference<VCLXDevice> pDev = new VCLXDevice;
130 pDev->SetOutputDevice( mpOutputDevice );
131 mxDevice = pDev;
133 return mxDevice;
136 awt::SimpleFontMetric VCLXGraphics::getFontMetric()
138 SolarMutexGuard aGuard;
140 awt::SimpleFontMetric aM;
141 if( mpOutputDevice )
143 mpOutputDevice->SetFont( maFont );
144 aM = VCLUnoHelper::CreateFontMetric( mpOutputDevice->GetFontMetric() );
146 return aM;
149 void VCLXGraphics::setFont( const uno::Reference< awt::XFont >& rxFont )
151 SolarMutexGuard aGuard;
153 maFont = VCLUnoHelper::CreateFont( rxFont );
156 void VCLXGraphics::selectFont( const awt::FontDescriptor& rDescription )
158 SolarMutexGuard aGuard;
160 maFont = VCLUnoHelper::CreateFont( rDescription, vcl::Font() );
163 void VCLXGraphics::setTextColor( sal_Int32 nColor )
165 SolarMutexGuard aGuard;
167 maTextColor = Color( ColorTransparency, nColor );
170 void VCLXGraphics::setTextFillColor( sal_Int32 nColor )
172 SolarMutexGuard aGuard;
174 maTextFillColor = Color( ColorTransparency, nColor );
177 void VCLXGraphics::setLineColor( sal_Int32 nColor )
179 SolarMutexGuard aGuard;
181 maLineColor = Color( ColorTransparency, nColor );
184 void VCLXGraphics::setFillColor( sal_Int32 nColor )
186 SolarMutexGuard aGuard;
188 maFillColor = Color( ColorTransparency, nColor );
191 void VCLXGraphics::setRasterOp( awt::RasterOperation eROP )
193 SolarMutexGuard aGuard;
195 meRasterOp = static_cast<RasterOp>(eROP);
198 void VCLXGraphics::setClipRegion( const uno::Reference< awt::XRegion >& rxRegion )
200 SolarMutexGuard aGuard;
202 if ( rxRegion.is() )
203 mpClipRegion.reset( new vcl::Region( VCLUnoHelper::GetRegion( rxRegion ) ) );
204 else
205 mpClipRegion.reset();
208 void VCLXGraphics::intersectClipRegion( const uno::Reference< awt::XRegion >& rxRegion )
210 SolarMutexGuard aGuard;
212 if ( rxRegion.is() )
214 vcl::Region aRegion( VCLUnoHelper::GetRegion( rxRegion ) );
215 if ( !mpClipRegion )
216 mpClipRegion.reset( new vcl::Region( aRegion ) );
217 else
218 mpClipRegion->Intersect( aRegion );
222 void VCLXGraphics::push( )
224 SolarMutexGuard aGuard;
227 if( mpOutputDevice )
228 mpOutputDevice->Push();
231 void VCLXGraphics::pop( )
233 SolarMutexGuard aGuard;
236 if( mpOutputDevice )
237 mpOutputDevice->Pop();
240 void VCLXGraphics::clear(
241 const awt::Rectangle& aRect )
243 SolarMutexGuard aGuard;
245 if( mpOutputDevice )
247 const ::tools::Rectangle aVCLRect = VCLUnoHelper::ConvertToVCLRect( aRect );
248 mpOutputDevice->Erase( aVCLRect );
252 void VCLXGraphics::copy( const uno::Reference< awt::XDevice >& rxSource, sal_Int32 nSourceX, sal_Int32 nSourceY, sal_Int32 nSourceWidth, sal_Int32 nSourceHeight, sal_Int32 nDestX, sal_Int32 nDestY, sal_Int32 nDestWidth, sal_Int32 nDestHeight )
254 SolarMutexGuard aGuard;
256 if ( mpOutputDevice )
258 VCLXDevice* pFromDev = dynamic_cast<VCLXDevice*>( rxSource.get() );
259 DBG_ASSERT( pFromDev, "VCLXGraphics::copy - invalid device" );
260 if ( pFromDev )
262 InitOutputDevice( InitOutDevFlags::NONE );
263 mpOutputDevice->DrawOutDev( Point( nDestX, nDestY ), Size( nDestWidth, nDestHeight ),
264 Point( nSourceX, nSourceY ), Size( nSourceWidth, nSourceHeight ), *pFromDev->GetOutputDevice() );
269 void VCLXGraphics::draw( const uno::Reference< awt::XDisplayBitmap >& rxBitmapHandle, sal_Int32 nSourceX, sal_Int32 nSourceY, sal_Int32 nSourceWidth, sal_Int32 nSourceHeight, sal_Int32 nDestX, sal_Int32 nDestY, sal_Int32 nDestWidth, sal_Int32 nDestHeight )
271 SolarMutexGuard aGuard;
273 if( !mpOutputDevice )
274 return;
276 InitOutputDevice( InitOutDevFlags::NONE);
277 uno::Reference< awt::XBitmap > xBitmap( rxBitmapHandle, uno::UNO_QUERY );
278 BitmapEx aBmpEx = VCLUnoHelper::GetBitmap( xBitmap );
280 Point aPos(nDestX - nSourceX, nDestY - nSourceY);
281 Size aSz = aBmpEx.GetSizePixel();
283 if(nDestWidth != nSourceWidth)
285 float zoomX = static_cast<float>(nDestWidth) / static_cast<float>(nSourceWidth);
286 aSz.setWidth( static_cast<tools::Long>(static_cast<float>(aSz.Width()) * zoomX) );
289 if(nDestHeight != nSourceHeight)
291 float zoomY = static_cast<float>(nDestHeight) / static_cast<float>(nSourceHeight);
292 aSz.setHeight( static_cast<tools::Long>(static_cast<float>(aSz.Height()) * zoomY) );
295 if(nSourceX || nSourceY || aSz.Width() != nSourceWidth || aSz.Height() != nSourceHeight)
296 mpOutputDevice->IntersectClipRegion(vcl::Region(tools::Rectangle(nDestX, nDestY, nDestX + nDestWidth - 1, nDestY + nDestHeight - 1)));
298 mpOutputDevice->DrawBitmapEx( aPos, aSz, aBmpEx );
301 void VCLXGraphics::drawPixel( sal_Int32 x, sal_Int32 y )
303 SolarMutexGuard aGuard;
305 if( mpOutputDevice )
307 InitOutputDevice( InitOutDevFlags::COLORS );
308 mpOutputDevice->DrawPixel( Point( x, y ) );
312 void VCLXGraphics::drawLine( sal_Int32 x1, sal_Int32 y1, sal_Int32 x2, sal_Int32 y2 )
314 SolarMutexGuard aGuard;
316 if( mpOutputDevice )
318 InitOutputDevice( InitOutDevFlags::COLORS );
319 mpOutputDevice->DrawLine( Point( x1, y1 ), Point( x2, y2 ) );
323 void VCLXGraphics::drawRect( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height )
325 SolarMutexGuard aGuard;
327 if( mpOutputDevice )
329 InitOutputDevice( InitOutDevFlags::COLORS );
330 mpOutputDevice->DrawRect( tools::Rectangle( Point( x, y ), Size( width, height ) ) );
334 void VCLXGraphics::drawRoundedRect( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int32 nHorzRound, sal_Int32 nVertRound )
336 SolarMutexGuard aGuard;
338 if( mpOutputDevice )
340 InitOutputDevice( InitOutDevFlags::COLORS );
341 mpOutputDevice->DrawRect( tools::Rectangle( Point( x, y ), Size( width, height ) ), nHorzRound, nVertRound );
345 void VCLXGraphics::drawPolyLine( const uno::Sequence< sal_Int32 >& DataX, const uno::Sequence< sal_Int32 >& DataY )
347 SolarMutexGuard aGuard;
349 if( mpOutputDevice )
351 InitOutputDevice( InitOutDevFlags::COLORS );
352 mpOutputDevice->DrawPolyLine( VCLUnoHelper::CreatePolygon( DataX, DataY ) );
356 void VCLXGraphics::drawPolygon( const uno::Sequence< sal_Int32 >& DataX, const uno::Sequence< sal_Int32 >& DataY )
358 SolarMutexGuard aGuard;
360 if( mpOutputDevice )
362 InitOutputDevice( InitOutDevFlags::COLORS );
363 mpOutputDevice->DrawPolygon( VCLUnoHelper::CreatePolygon( DataX, DataY ) );
367 void VCLXGraphics::drawPolyPolygon( const uno::Sequence< uno::Sequence< sal_Int32 > >& DataX, const uno::Sequence< uno::Sequence< sal_Int32 > >& DataY )
369 SolarMutexGuard aGuard;
371 if( mpOutputDevice )
373 InitOutputDevice( InitOutDevFlags::COLORS );
374 sal_uInt16 nPolys = static_cast<sal_uInt16>(DataX.getLength());
375 tools::PolyPolygon aPolyPoly( nPolys );
376 for ( sal_uInt16 n = 0; n < nPolys; n++ )
377 aPolyPoly[n] = VCLUnoHelper::CreatePolygon( DataX.getConstArray()[n], DataY.getConstArray()[n] );
379 mpOutputDevice->DrawPolyPolygon( aPolyPoly );
383 void VCLXGraphics::drawEllipse( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height )
385 SolarMutexGuard aGuard;
387 if( mpOutputDevice )
389 InitOutputDevice( InitOutDevFlags::COLORS );
390 mpOutputDevice->DrawEllipse( tools::Rectangle( Point( x, y ), Size( width, height ) ) );
394 void VCLXGraphics::drawArc( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int32 x1, sal_Int32 y1, sal_Int32 x2, sal_Int32 y2 )
396 SolarMutexGuard aGuard;
398 if( mpOutputDevice )
400 InitOutputDevice( InitOutDevFlags::COLORS );
401 mpOutputDevice->DrawArc( tools::Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
405 void VCLXGraphics::drawPie( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int32 x1, sal_Int32 y1, sal_Int32 x2, sal_Int32 y2 )
407 SolarMutexGuard aGuard;
409 if( mpOutputDevice )
411 InitOutputDevice( InitOutDevFlags::COLORS );
412 mpOutputDevice->DrawPie( tools::Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
416 void VCLXGraphics::drawChord( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int32 x1, sal_Int32 y1, sal_Int32 x2, sal_Int32 y2 )
418 SolarMutexGuard aGuard;
420 if( mpOutputDevice )
422 InitOutputDevice( InitOutDevFlags::COLORS );
423 mpOutputDevice->DrawChord( tools::Rectangle( Point( x, y ), Size( width, height ) ), Point( x1, y1 ), Point( x2, y2 ) );
427 void VCLXGraphics::drawGradient( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, const awt::Gradient& rGradient )
429 SolarMutexGuard aGuard;
431 if( !mpOutputDevice )
432 return;
434 InitOutputDevice( InitOutDevFlags::COLORS );
435 Gradient aGradient(rGradient.Style, Color(ColorTransparency, rGradient.StartColor), Color(ColorTransparency, rGradient.EndColor));
436 aGradient.SetAngle(Degree10(rGradient.Angle));
437 aGradient.SetBorder(rGradient.Border);
438 aGradient.SetOfsX(rGradient.XOffset);
439 aGradient.SetOfsY(rGradient.YOffset);
440 aGradient.SetStartIntensity(rGradient.StartIntensity);
441 aGradient.SetEndIntensity(rGradient.EndIntensity);
442 aGradient.SetSteps(rGradient.StepCount);
443 mpOutputDevice->DrawGradient( tools::Rectangle( Point( x, y ), Size( width, height ) ), aGradient );
446 void VCLXGraphics::drawText( sal_Int32 x, sal_Int32 y, const OUString& rText )
448 SolarMutexGuard aGuard;
450 if( mpOutputDevice )
452 InitOutputDevice( InitOutDevFlags::COLORS |InitOutDevFlags::FONT);
453 mpOutputDevice->DrawText( Point( x, y ), rText );
457 void VCLXGraphics::drawTextArray( sal_Int32 x, sal_Int32 y, const OUString& rText, const uno::Sequence< sal_Int32 >& rLongs )
459 SolarMutexGuard aGuard;
461 if( mpOutputDevice )
463 InitOutputDevice( InitOutDevFlags::COLORS|InitOutDevFlags::FONT );
464 KernArray aDXA;
465 aDXA.reserve(rText.getLength());
466 for(int i = 0; i < rText.getLength(); ++i)
467 aDXA.push_back(rLongs[i]);
468 mpOutputDevice->DrawTextArray( Point( x, y ), rText, aDXA, {}, 0, rText.getLength());
473 void VCLXGraphics::drawImage( sal_Int32 x, sal_Int32 y, sal_Int32 width, sal_Int32 height, sal_Int16 nStyle, const uno::Reference< graphic::XGraphic >& xGraphic )
475 SolarMutexGuard aGuard;
477 if( mpOutputDevice && xGraphic.is() )
479 Image aImage( xGraphic );
480 if ( !!aImage )
482 InitOutputDevice( InitOutDevFlags::COLORS );
483 mpOutputDevice->DrawImage( Point( x, y ), Size( width, height ), aImage, static_cast<DrawImageFlags>(nStyle) );
487 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */