1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <sal/config.h>
22 #include <basegfx/matrix/b2dhommatrix.hxx>
23 #include <basegfx/numeric/ftools.hxx>
24 #include <basegfx/point/b2dpoint.hxx>
25 #include <basegfx/polygon/b2dpolygontools.hxx>
26 #include <basegfx/range/b2drectangle.hxx>
27 #include <basegfx/utils/canvastools.hxx>
28 #include <basegfx/tuple/b2dtuple.hxx>
29 #include <rtl/math.hxx>
30 #include <comphelper/diagnose_ex.hxx>
31 #include <sal/log.hxx>
32 #include <vcl/bitmapex.hxx>
33 #include <vcl/canvastools.hxx>
34 #include <vcl/BitmapTools.hxx>
35 #include <vcl/metric.hxx>
36 #include <vcl/skia/SkiaHelper.hxx>
38 #include <canvas/canvastools.hxx>
40 #include "canvasbitmap.hxx"
41 #include "impltools.hxx"
42 #include "spritecanvas.hxx"
45 using namespace ::com::sun::star
;
47 namespace vclcanvas::tools
49 ::BitmapEx
bitmapExFromXBitmap( const uno::Reference
< rendering::XBitmap
>& xBitmap
)
51 // TODO(F3): CanvasCustomSprite should also be tunnelled
52 // through (also implements XIntegerBitmap interface)
53 CanvasBitmap
* pBitmapImpl
= dynamic_cast< CanvasBitmap
* >( xBitmap
.get() );
57 return pBitmapImpl
->getBitmap();
61 SpriteCanvas
* pCanvasImpl
= dynamic_cast< SpriteCanvas
* >( xBitmap
.get() );
62 if( pCanvasImpl
&& pCanvasImpl
->getBackBuffer() )
64 // TODO(F3): mind the plain Canvas impl. Consolidate with CWS canvas05
65 const ::OutputDevice
& rDev( pCanvasImpl
->getBackBuffer()->getOutDev() );
66 const ::Point aEmptyPoint
;
67 return rDev
.GetBitmapEx( aEmptyPoint
,
68 rDev
.GetOutputSizePixel() );
71 // TODO(F2): add support for floating point bitmap formats
72 uno::Reference
< rendering::XIntegerReadOnlyBitmap
> xIntBmp(
73 xBitmap
, uno::UNO_QUERY_THROW
);
75 ::BitmapEx aBmpEx
= vcl::unotools::bitmapExFromXBitmap( xIntBmp
);
76 if( !aBmpEx
.IsEmpty() )
79 // TODO(F1): extract pixel from XBitmap interface
80 ENSURE_OR_THROW( false,
81 "bitmapExFromXBitmap(): could not extract bitmap" );
87 bool setupFontTransform( ::Point
& o_rPoint
,
88 vcl::Font
& io_rVCLFont
,
89 const rendering::ViewState
& rViewState
,
90 const rendering::RenderState
& rRenderState
,
91 ::OutputDevice
const & rOutDev
)
93 ::basegfx::B2DHomMatrix aMatrix
;
95 ::canvas::tools::mergeViewAndRenderTransform(aMatrix
,
99 ::basegfx::B2DTuple aScale
;
100 ::basegfx::B2DTuple aTranslate
;
101 double nRotate
, nShearX
;
103 aMatrix
.decompose( aScale
, aTranslate
, nRotate
, nShearX
);
105 // query font metric _before_ tampering with width and height
106 if( !::rtl::math::approxEqual(aScale
.getX(), aScale
.getY()) )
108 // retrieve true font width
109 const sal_Int32
nFontWidth( rOutDev
.GetFontMetric( io_rVCLFont
).GetAverageFontWidth() );
111 const sal_Int32
nScaledFontWidth( ::basegfx::fround(nFontWidth
* aScale
.getX()) );
113 if( !nScaledFontWidth
)
115 // scale is smaller than one pixel - disable text
120 io_rVCLFont
.SetAverageFontWidth( nScaledFontWidth
);
123 if( !::rtl::math::approxEqual(aScale
.getY(), 1.0) )
125 const sal_Int32
nFontHeight( io_rVCLFont
.GetFontHeight() );
126 io_rVCLFont
.SetFontHeight( ::basegfx::fround
<::tools::Long
>(nFontHeight
* aScale
.getY()) );
129 io_rVCLFont
.SetOrientation( Degree10( ::basegfx::fround(-basegfx::rad2deg
<10>(fmod(nRotate
, 2*M_PI
))) ) );
131 // TODO(F2): Missing functionality in VCL: shearing
132 o_rPoint
.setX( ::basegfx::fround
<::tools::Long
>(aTranslate
.getX()) );
133 o_rPoint
.setY( ::basegfx::fround
<::tools::Long
>(aTranslate
.getY()) );
138 void setupFontWidth(const css::geometry::Matrix2D
& rFontMatrix
,
140 OutputDevice
& rOutDev
)
142 rFont
.SetFontSize(Size(0, rFont
.GetFontHeight()));
144 if (!::rtl::math::approxEqual(rFontMatrix
.m00
, rFontMatrix
.m11
))
146 const bool bOldMapState(rOutDev
.IsMapModeEnabled());
147 rOutDev
.EnableMapMode(false);
149 const Size aSize
= rOutDev
.GetFontMetric(rFont
).GetFontSize();
151 const double fDividend(rFontMatrix
.m10
+ rFontMatrix
.m11
);
152 double fStretch
= rFontMatrix
.m00
+ rFontMatrix
.m01
;
154 if (!::basegfx::fTools::equalZero(fDividend
))
155 fStretch
/= fDividend
;
157 const ::tools::Long nNewWidth
= ::basegfx::fround
<::tools::Long
>(aSize
.Width() * fStretch
);
159 rFont
.SetAverageFontWidth(nNewWidth
);
161 rOutDev
.EnableMapMode(bOldMapState
);
165 bool isRectangle( const ::tools::PolyPolygon
& rPolyPoly
)
167 // exclude some cheap cases first
168 if( rPolyPoly
.Count() != 1 )
171 const ::tools::Polygon
& rPoly( rPolyPoly
[0] );
173 sal_uInt16
nCount( rPoly
.GetSize() );
177 // delegate to basegfx
178 return ::basegfx::utils::isRectangle( rPoly
.getB2DPolygon() );
182 // VCL-Canvas related
185 ::Point
mapRealPoint2D( const geometry::RealPoint2D
& rPoint
,
186 const rendering::ViewState
& rViewState
,
187 const rendering::RenderState
& rRenderState
)
189 ::basegfx::B2DPoint
aPoint( ::basegfx::unotools::b2DPointFromRealPoint2D(rPoint
) );
191 ::basegfx::B2DHomMatrix aMatrix
;
192 aPoint
*= ::canvas::tools::mergeViewAndRenderTransform(aMatrix
,
196 return vcl::unotools::pointFromB2DPoint( aPoint
);
199 ::tools::PolyPolygon
mapPolyPolygon( const ::basegfx::B2DPolyPolygon
& rPoly
,
200 const rendering::ViewState
& rViewState
,
201 const rendering::RenderState
& rRenderState
)
203 ::basegfx::B2DHomMatrix aMatrix
;
204 ::canvas::tools::mergeViewAndRenderTransform(aMatrix
,
208 ::basegfx::B2DPolyPolygon
aTemp( rPoly
);
210 aTemp
.transform( aMatrix
);
212 return ::tools::PolyPolygon( aTemp
);
215 ::BitmapEx
transformBitmap( const BitmapEx
& rBitmap
,
216 const ::basegfx::B2DHomMatrix
& rTransform
)
218 SAL_INFO( "canvas.vcl", "::vclcanvas::tools::transformBitmap()" );
219 SAL_INFO( "canvas.vcl", "::vclcanvas::tools::transformBitmap: 0x" << std::hex
<< &rBitmap
);
221 // calc transformation and size of bitmap to be
222 // generated. Note, that the translational components are
223 // deleted from the transformation; this can be handled by
224 // an offset when painting the bitmap
225 const Size
aBmpSize( rBitmap
.GetSizePixel() );
227 // calc effective transformation for bitmap
228 const ::basegfx::B2DRectangle
aSrcRect( 0, 0,
231 ::basegfx::B2DRectangle aDestRect
= ::canvas::tools::calcTransformedRectBounds(
235 // re-center bitmap, such that it's left, top border is
236 // aligned with (0,0). The method takes the given
237 // rectangle, and calculates a transformation that maps
238 // this rectangle unscaled to the origin.
239 ::basegfx::B2DHomMatrix aLocalTransform
= ::canvas::tools::calcRectToOriginTransform(
243 return vcl::bitmap::CanvasTransformBitmap(rBitmap
, rTransform
, aDestRect
, aLocalTransform
);
246 void SetDefaultDeviceAntiAliasing( OutputDevice
* pDevice
)
248 #if defined( MACOSX )
249 // use AA on VCLCanvas for Mac
250 pDevice
->SetAntialiasing( AntialiasingFlags::Enable
| pDevice
->GetAntialiasing() );
252 // switch off AA for WIN32 and UNIX, the VCLCanvas does not look good with it and
253 // is not required to do AA. It would need to be adapted to use it correctly
254 // (especially gradient painting). This will need extra work.
255 if( SkiaHelper::isVCLSkiaEnabled()) // But Skia handles AA fine.
256 pDevice
->SetAntialiasing( AntialiasingFlags::Enable
| pDevice
->GetAntialiasing() );
258 pDevice
->SetAntialiasing(pDevice
->GetAntialiasing() & ~AntialiasingFlags::Enable
);
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */