LibreOffice Module canvas (master) 1
impltools.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
29#include <rtl/math.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>
37
39
40#include "canvasbitmap.hxx"
41#include "impltools.hxx"
42#include "spritecanvas.hxx"
43
44
45using namespace ::com::sun::star;
46
48{
49 ::BitmapEx bitmapExFromXBitmap( const uno::Reference< rendering::XBitmap >& xBitmap )
50 {
51 // TODO(F3): CanvasCustomSprite should also be tunnelled
52 // through (also implements XIntegerBitmap interface)
53 CanvasBitmap* pBitmapImpl = dynamic_cast< CanvasBitmap* >( xBitmap.get() );
54
55 if( pBitmapImpl )
56 {
57 return pBitmapImpl->getBitmap();
58 }
59 else
60 {
61 SpriteCanvas* pCanvasImpl = dynamic_cast< SpriteCanvas* >( xBitmap.get() );
62 if( pCanvasImpl && pCanvasImpl->getBackBuffer() )
63 {
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() );
69 }
70
71 // TODO(F2): add support for floating point bitmap formats
72 uno::Reference< rendering::XIntegerReadOnlyBitmap > xIntBmp(
73 xBitmap, uno::UNO_QUERY_THROW );
74
76 if( !aBmpEx.IsEmpty() )
77 return aBmpEx;
78
79 // TODO(F1): extract pixel from XBitmap interface
80 ENSURE_OR_THROW( false,
81 "bitmapExFromXBitmap(): could not extract bitmap" );
82 }
83
84 return ::BitmapEx();
85 }
86
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 )
92 {
94
96 rViewState,
97 rRenderState);
98
100 ::basegfx::B2DTuple aTranslate;
101 double nRotate, nShearX;
102
103 aMatrix.decompose( aScale, aTranslate, nRotate, nShearX );
104
105 // query font metric _before_ tampering with width and height
106 if( !::rtl::math::approxEqual(aScale.getX(), aScale.getY()) )
107 {
108 // retrieve true font width
109 const sal_Int32 nFontWidth( rOutDev.GetFontMetric( io_rVCLFont ).GetAverageFontWidth() );
110
111 const sal_Int32 nScaledFontWidth( ::basegfx::fround(nFontWidth * aScale.getX()) );
112
113 if( !nScaledFontWidth )
114 {
115 // scale is smaller than one pixel - disable text
116 // output altogether
117 return false;
118 }
119
120 io_rVCLFont.SetAverageFontWidth( nScaledFontWidth );
121 }
122
123 if( !::rtl::math::approxEqual(aScale.getY(), 1.0) )
124 {
125 const sal_Int32 nFontHeight( io_rVCLFont.GetFontHeight() );
126 io_rVCLFont.SetFontHeight( ::basegfx::fround(nFontHeight * aScale.getY()) );
127 }
128
129 io_rVCLFont.SetOrientation( Degree10( ::basegfx::fround(-basegfx::rad2deg<10>(fmod(nRotate, 2*M_PI))) ) );
130
131 // TODO(F2): Missing functionality in VCL: shearing
132 o_rPoint.setX( ::basegfx::fround(aTranslate.getX()) );
133 o_rPoint.setY( ::basegfx::fround(aTranslate.getY()) );
134
135 return true;
136 }
137
138 void setupFontWidth(const css::geometry::Matrix2D& rFontMatrix,
139 vcl::Font& rFont,
140 OutputDevice& rOutDev)
141 {
142 rFont.SetFontSize(Size(0, rFont.GetFontHeight()));
143
144 if (!::rtl::math::approxEqual(rFontMatrix.m00, rFontMatrix.m11))
145 {
146 const bool bOldMapState(rOutDev.IsMapModeEnabled());
147 rOutDev.EnableMapMode(false);
148
149 const Size aSize = rOutDev.GetFontMetric(rFont).GetFontSize();
150
151 const double fDividend(rFontMatrix.m10 + rFontMatrix.m11);
152 double fStretch = rFontMatrix.m00 + rFontMatrix.m01;
153
154 if (!::basegfx::fTools::equalZero(fDividend))
155 fStretch /= fDividend;
156
157 const ::tools::Long nNewWidth = ::basegfx::fround(aSize.Width() * fStretch);
158
159 rFont.SetAverageFontWidth(nNewWidth);
160
161 rOutDev.EnableMapMode(bOldMapState);
162 }
163 }
164
165 bool isRectangle( const ::tools::PolyPolygon& rPolyPoly )
166 {
167 // exclude some cheap cases first
168 if( rPolyPoly.Count() != 1 )
169 return false;
170
171 const ::tools::Polygon& rPoly( rPolyPoly[0] );
172
173 sal_uInt16 nCount( rPoly.GetSize() );
174 if( nCount < 4 )
175 return false;
176
177 // delegate to basegfx
178 return ::basegfx::utils::isRectangle( rPoly.getB2DPolygon() );
179 }
180
181
182 // VCL-Canvas related
183
184
185 ::Point mapRealPoint2D( const geometry::RealPoint2D& rPoint,
186 const rendering::ViewState& rViewState,
187 const rendering::RenderState& rRenderState )
188 {
189 ::basegfx::B2DPoint aPoint( ::basegfx::unotools::b2DPointFromRealPoint2D(rPoint) );
190
193 rViewState,
194 rRenderState);
195
196 return vcl::unotools::pointFromB2DPoint( aPoint );
197 }
198
199 ::tools::PolyPolygon mapPolyPolygon( const ::basegfx::B2DPolyPolygon& rPoly,
200 const rendering::ViewState& rViewState,
201 const rendering::RenderState& rRenderState )
202 {
205 rViewState,
206 rRenderState);
207
208 ::basegfx::B2DPolyPolygon aTemp( rPoly );
209
210 aTemp.transform( aMatrix );
211
212 return ::tools::PolyPolygon( aTemp );
213 }
214
216 const ::basegfx::B2DHomMatrix& rTransform )
217 {
218 SAL_INFO( "canvas.vcl", "::vclcanvas::tools::transformBitmap()" );
219 SAL_INFO( "canvas.vcl", "::vclcanvas::tools::transformBitmap: 0x" << std::hex << &rBitmap );
220
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() );
226 ::basegfx::B2DRectangle aDestRect;
227
228 // calc effective transformation for bitmap
229 const ::basegfx::B2DRectangle aSrcRect( 0, 0,
230 aBmpSize.Width(),
231 aBmpSize.Height() );
233 aSrcRect,
234 rTransform );
235
236 // re-center bitmap, such that it's left, top border is
237 // aligned with (0,0). The method takes the given
238 // rectangle, and calculates a transformation that maps
239 // this rectangle unscaled to the origin.
240 ::basegfx::B2DHomMatrix aLocalTransform;
242 aSrcRect,
243 rTransform );
244
245 return vcl::bitmap::CanvasTransformBitmap(rBitmap, rTransform, aDestRect, aLocalTransform);
246 }
247
249 {
250#if defined( MACOSX )
251 // use AA on VCLCanvas for Mac
252 pDevice->SetAntialiasing( AntialiasingFlags::Enable | pDevice->GetAntialiasing() );
253#else
254 // switch off AA for WIN32 and UNIX, the VCLCanvas does not look good with it and
255 // is not required to do AA. It would need to be adapted to use it correctly
256 // (especially gradient painting). This will need extra work.
257 if( SkiaHelper::isVCLSkiaEnabled()) // But Skia handles AA fine.
258 pDevice->SetAntialiasing( AntialiasingFlags::Enable | pDevice->GetAntialiasing() );
259 else
260 pDevice->SetAntialiasing(pDevice->GetAntialiasing() & ~AntialiasingFlags::Enable);
261#endif
262 }
263
264}
265
266/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsEmpty() const
const Size & GetSizePixel() const
void EnableMapMode(bool bEnable=true)
void SetAntialiasing(AntialiasingFlags nMode)
FontMetric GetFontMetric() const
AntialiasingFlags GetAntialiasing() const
bool IsMapModeEnabled() const
void setX(tools::Long nX)
void setY(tools::Long nY)
constexpr tools::Long Height() const
constexpr tools::Long Width() const
bool decompose(B2DTuple &rScale, B2DTuple &rTranslate, double &rRotate, double &rShearX) const
void transform(const basegfx::B2DHomMatrix &rMatrix)
TYPE getX() const
TYPE getY() const
tools::Long GetFontHeight() const
void SetFontSize(const Size &)
void SetOrientation(Degree10 nLineOrientation)
void SetAverageFontWidth(tools::Long nWidth)
void SetFontHeight(tools::Long nHeight)
const Size & GetFontSize() const
tools::Long GetAverageFontWidth() const
BitmapEx getBitmap() const
Not threadsafe! Returned object is shared!
Product of this component's factory.
BackBufferSharedPtr const & getBackBuffer() const
Get window for this canvas.
int nCount
#define ENSURE_OR_THROW(c, m)
#define SAL_INFO(area, stream)
VCL_DLLPUBLIC bool isVCLSkiaEnabled()
::basegfx::B2DHomMatrix & mergeViewAndRenderTransform(::basegfx::B2DHomMatrix &combinedTransform, const rendering::ViewState &viewState, const rendering::RenderState &renderState)
::basegfx::B2DHomMatrix & calcRectToOriginTransform(::basegfx::B2DHomMatrix &o_transform, const ::basegfx::B2DRange &i_srcRect, const ::basegfx::B2DHomMatrix &i_transformation)
Calc a transform that maps the upper, left corner of a rectangle to the origin.
::basegfx::B2DRange & calcTransformedRectBounds(::basegfx::B2DRange &outRect, const ::basegfx::B2DRange &inRect, const ::basegfx::B2DHomMatrix &transformation)
Calc the bounding rectangle of a transformed rectangle.
BitmapEx CanvasTransformBitmap(const BitmapEx &rBitmap, const ::basegfx::B2DHomMatrix &rTransform, ::basegfx::B2DRectangle const &rDestRect, ::basegfx::B2DHomMatrix const &rLocalTransform)
::Point pointFromB2DPoint(const basegfx::B2DPoint &rPoint)
::BitmapEx bitmapExFromXBitmap(const uno::Reference< rendering::XIntegerReadOnlyBitmap > &xInputBitmap)
void setupFontWidth(const css::geometry::Matrix2D &rFontMatrix, vcl::Font &rFont, OutputDevice &rOutDev)
Definition: impltools.cxx:138
::tools::PolyPolygon mapPolyPolygon(const ::basegfx::B2DPolyPolygon &rPoly, const rendering::ViewState &rViewState, const rendering::RenderState &rRenderState)
Definition: impltools.cxx:199
bool setupFontTransform(::Point &o_rPoint, vcl::Font &io_rVCLFont, const rendering::ViewState &rViewState, const rendering::RenderState &rRenderState, ::OutputDevice const &rOutDev)
Definition: impltools.cxx:87
void SetDefaultDeviceAntiAliasing(OutputDevice *pDevice)
Definition: impltools.cxx:248
::BitmapEx transformBitmap(const BitmapEx &rBitmap, const ::basegfx::B2DHomMatrix &rTransform)
Definition: impltools.cxx:215
bool isRectangle(const ::tools::PolyPolygon &rPolyPoly)
Predicate, to determine whether polygon is actually an axis-aligned rectangle.
Definition: impltools.cxx:165
::Point mapRealPoint2D(const geometry::RealPoint2D &rPoint, const rendering::ViewState &rViewState, const rendering::RenderState &rRenderState)
Definition: impltools.cxx:185
::BitmapEx bitmapExFromXBitmap(const uno::Reference< rendering::XBitmap > &xBitmap)
Definition: impltools.cxx:49