bump product version to 6.4.0.3
[LibreOffice.git] / vcl / source / outdev / line.cxx
bloba9bdffb7b8e58155a808fcab5d75a5d0f03c2a3c
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 .
20 #include <cassert>
21 #include <numeric>
23 #include <vcl/gdimtf.hxx>
24 #include <vcl/lineinfo.hxx>
25 #include <vcl/metaact.hxx>
26 #include <vcl/outdev.hxx>
27 #include <vcl/virdev.hxx>
29 #include <salgdi.hxx>
31 #include <basegfx/matrix/b2dhommatrix.hxx>
32 #include <basegfx/polygon/b2dpolygontools.hxx>
33 #include <basegfx/polygon/b2dpolypolygontools.hxx>
34 #include <basegfx/polygon/b2dlinegeometry.hxx>
36 void OutputDevice::DrawLine( const Point& rStartPt, const Point& rEndPt,
37 const LineInfo& rLineInfo )
39 assert(!is_double_buffered_window());
41 if ( rLineInfo.IsDefault() )
43 DrawLine( rStartPt, rEndPt );
44 return;
47 if ( mpMetaFile )
48 mpMetaFile->AddAction( new MetaLineAction( rStartPt, rEndPt, rLineInfo ) );
50 if ( !IsDeviceOutputNecessary() || !mbLineColor || ( LineStyle::NONE == rLineInfo.GetStyle() ) || ImplIsRecordLayout() )
51 return;
53 if( !mpGraphics && !AcquireGraphics() )
54 return;
56 if ( mbInitClipRegion )
57 InitClipRegion();
59 if ( mbOutputClipped )
60 return;
62 const Point aStartPt( ImplLogicToDevicePixel( rStartPt ) );
63 const Point aEndPt( ImplLogicToDevicePixel( rEndPt ) );
64 const LineInfo aInfo( ImplLogicToDevicePixel( rLineInfo ) );
65 const bool bDashUsed(LineStyle::Dash == aInfo.GetStyle());
66 const bool bLineWidthUsed(aInfo.GetWidth() > 1);
68 if ( mbInitLineColor )
69 InitLineColor();
71 if(bDashUsed || bLineWidthUsed)
73 basegfx::B2DPolygon aLinePolygon;
74 aLinePolygon.append(basegfx::B2DPoint(aStartPt.X(), aStartPt.Y()));
75 aLinePolygon.append(basegfx::B2DPoint(aEndPt.X(), aEndPt.Y()));
77 drawLine( basegfx::B2DPolyPolygon(aLinePolygon), aInfo );
79 else
81 mpGraphics->DrawLine( aStartPt.X(), aStartPt.Y(), aEndPt.X(), aEndPt.Y(), this );
84 if( mpAlphaVDev )
85 mpAlphaVDev->DrawLine( rStartPt, rEndPt, rLineInfo );
88 void OutputDevice::DrawLine( const Point& rStartPt, const Point& rEndPt )
90 assert(!is_double_buffered_window());
92 if ( mpMetaFile )
93 mpMetaFile->AddAction( new MetaLineAction( rStartPt, rEndPt ) );
95 if ( !IsDeviceOutputNecessary() || !mbLineColor || ImplIsRecordLayout() )
96 return;
98 if ( !mpGraphics && !AcquireGraphics() )
99 return;
101 if ( mbInitClipRegion )
102 InitClipRegion();
104 if ( mbOutputClipped )
105 return;
107 if ( mbInitLineColor )
108 InitLineColor();
110 // #i101598# support AA and snap for lines, too
111 if((mnAntialiasing & AntialiasingFlags::EnableB2dDraw)
112 && mpGraphics->supportsOperation(OutDevSupportType::B2DDraw)
113 && RasterOp::OverPaint == GetRasterOp()
114 && IsLineColor())
116 // at least transform with double precision to device coordinates; this will
117 // avoid pixel snap of single, appended lines
118 const basegfx::B2DHomMatrix aTransform(ImplGetDeviceTransformation());
119 const basegfx::B2DVector aB2DLineWidth( 1.0, 1.0 );
120 basegfx::B2DPolygon aB2DPolyLine;
122 aB2DPolyLine.append(basegfx::B2DPoint(rStartPt.X(), rStartPt.Y()));
123 aB2DPolyLine.append(basegfx::B2DPoint(rEndPt.X(), rEndPt.Y()));
124 aB2DPolyLine.transform( aTransform );
126 const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
128 if( mpGraphics->DrawPolyLine(
129 basegfx::B2DHomMatrix(),
130 aB2DPolyLine,
131 0.0,
132 aB2DLineWidth,
133 basegfx::B2DLineJoin::NONE,
134 css::drawing::LineCap_BUTT,
135 basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
136 bPixelSnapHairline,
137 this))
139 return;
143 const Point aStartPt(ImplLogicToDevicePixel(rStartPt));
144 const Point aEndPt(ImplLogicToDevicePixel(rEndPt));
146 mpGraphics->DrawLine( aStartPt.X(), aStartPt.Y(), aEndPt.X(), aEndPt.Y(), this );
148 if( mpAlphaVDev )
149 mpAlphaVDev->DrawLine( rStartPt, rEndPt );
152 void OutputDevice::drawLine( basegfx::B2DPolyPolygon aLinePolyPolygon, const LineInfo& rInfo )
154 const bool bTryAA((mnAntialiasing & AntialiasingFlags::EnableB2dDraw)
155 && mpGraphics->supportsOperation(OutDevSupportType::B2DDraw)
156 && RasterOp::OverPaint == GetRasterOp()
157 && IsLineColor());
158 basegfx::B2DPolyPolygon aFillPolyPolygon;
159 const bool bDashUsed(LineStyle::Dash == rInfo.GetStyle());
160 const bool bLineWidthUsed(rInfo.GetWidth() > 1);
162 if(bDashUsed && aLinePolyPolygon.count())
164 ::std::vector< double > fDotDashArray;
165 const double fDashLen(rInfo.GetDashLen());
166 const double fDotLen(rInfo.GetDotLen());
167 const double fDistance(rInfo.GetDistance());
169 for(sal_uInt16 a(0); a < rInfo.GetDashCount(); a++)
171 fDotDashArray.push_back(fDashLen);
172 fDotDashArray.push_back(fDistance);
175 for(sal_uInt16 b(0); b < rInfo.GetDotCount(); b++)
177 fDotDashArray.push_back(fDotLen);
178 fDotDashArray.push_back(fDistance);
181 const double fAccumulated(::std::accumulate(fDotDashArray.begin(), fDotDashArray.end(), 0.0));
183 if(fAccumulated > 0.0)
185 basegfx::B2DPolyPolygon aResult;
187 for(auto const& rPolygon : aLinePolyPolygon)
189 basegfx::B2DPolyPolygon aLineTarget;
190 basegfx::utils::applyLineDashing(
191 rPolygon,
192 fDotDashArray,
193 &aLineTarget);
194 aResult.append(aLineTarget);
197 aLinePolyPolygon = aResult;
201 if(bLineWidthUsed && aLinePolyPolygon.count())
203 const double fHalfLineWidth((rInfo.GetWidth() * 0.5) + 0.5);
205 if(aLinePolyPolygon.areControlPointsUsed())
207 // #i110768# When area geometry has to be created, do not
208 // use the fallback bezier decomposition inside createAreaGeometry,
209 // but one that is at least as good as ImplSubdivideBezier was.
210 // There, Polygon::AdaptiveSubdivide was used with default parameter
211 // 1.0 as quality index.
212 aLinePolyPolygon = basegfx::utils::adaptiveSubdivideByDistance(aLinePolyPolygon, 1.0);
215 for(auto const& rPolygon : aLinePolyPolygon)
217 aFillPolyPolygon.append(basegfx::utils::createAreaGeometry(
218 rPolygon,
219 fHalfLineWidth,
220 rInfo.GetLineJoin(),
221 rInfo.GetLineCap()));
224 aLinePolyPolygon.clear();
227 GDIMetaFile* pOldMetaFile = mpMetaFile;
228 mpMetaFile = nullptr;
230 if(aLinePolyPolygon.count())
232 for(auto const& rB2DPolygon : aLinePolyPolygon)
234 const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
235 bool bDone(false);
237 if(bTryAA)
239 bDone = mpGraphics->DrawPolyLine(
240 basegfx::B2DHomMatrix(),
241 rB2DPolygon,
242 0.0,
243 basegfx::B2DVector(1.0,1.0),
244 basegfx::B2DLineJoin::NONE,
245 css::drawing::LineCap_BUTT,
246 basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
247 bPixelSnapHairline,
248 this);
251 if(!bDone)
253 tools::Polygon aPolygon(rB2DPolygon);
254 mpGraphics->DrawPolyLine(
255 aPolygon.GetSize(),
256 reinterpret_cast<SalPoint*>(aPolygon.GetPointAry()),
257 this);
262 if(aFillPolyPolygon.count())
264 const Color aOldLineColor( maLineColor );
265 const Color aOldFillColor( maFillColor );
267 SetLineColor();
268 InitLineColor();
269 SetFillColor( aOldLineColor );
270 InitFillColor();
272 bool bDone(false);
274 if(bTryAA)
276 bDone = mpGraphics->DrawPolyPolygon(
277 basegfx::B2DHomMatrix(),
278 aFillPolyPolygon,
279 0.0,
280 this);
283 if(!bDone)
285 for(auto const& rB2DPolygon : aFillPolyPolygon)
287 tools::Polygon aPolygon(rB2DPolygon);
289 // need to subdivide, mpGraphics->DrawPolygon ignores curves
290 aPolygon.AdaptiveSubdivide(aPolygon);
291 mpGraphics->DrawPolygon(aPolygon.GetSize(), reinterpret_cast<const SalPoint*>(aPolygon.GetConstPointAry()), this);
295 SetFillColor( aOldFillColor );
296 SetLineColor( aOldLineColor );
299 mpMetaFile = pOldMetaFile;
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */