LibreOffice Module vcl (master)  1
line.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 
24 #include <tools/debug.hxx>
25 
26 #include <vcl/lineinfo.hxx>
27 #include <vcl/metaact.hxx>
28 #include <vcl/virdev.hxx>
29 
30 #include <drawmode.hxx>
31 #include <salgdi.hxx>
32 
33 #include <cassert>
34 #include <numeric>
35 
37 {
38 
39  if ( mpMetaFile )
40  mpMetaFile->AddAction( new MetaLineColorAction( Color(), false ) );
41 
42  if ( mbLineColor )
43  {
44  mbInitLineColor = true;
45  mbLineColor = false;
47  }
48 
49  if( mpAlphaVDev )
51 }
52 
53 void OutputDevice::SetLineColor( const Color& rColor )
54 {
55 
56  Color aColor = vcl::drawmode::GetLineColor(rColor, GetDrawMode(), GetSettings().GetStyleSettings());
57 
58  if( mpMetaFile )
59  mpMetaFile->AddAction( new MetaLineColorAction( aColor, true ) );
60 
61  if( aColor.IsTransparent() )
62  {
63  if ( mbLineColor )
64  {
65  mbInitLineColor = true;
66  mbLineColor = false;
68  }
69  }
70  else
71  {
72  if( maLineColor != aColor )
73  {
74  mbInitLineColor = true;
75  mbLineColor = true;
76  maLineColor = aColor;
77  }
78  }
79 
80  if( mpAlphaVDev )
82 }
83 
85 {
87 
88  if( mbLineColor )
89  {
90  if( RasterOp::N0 == meRasterOp )
92  else if( RasterOp::N1 == meRasterOp )
94  else if( RasterOp::Invert == meRasterOp )
96  else
98  }
99  else
100  {
102  }
103 
104  mbInitLineColor = false;
105 }
106 
107 void OutputDevice::DrawLine( const Point& rStartPt, const Point& rEndPt,
108  const LineInfo& rLineInfo )
109 {
110  assert(!is_double_buffered_window());
111 
112  if ( rLineInfo.IsDefault() )
113  {
114  DrawLine( rStartPt, rEndPt );
115  return;
116  }
117 
118  if ( mpMetaFile )
119  mpMetaFile->AddAction( new MetaLineAction( rStartPt, rEndPt, rLineInfo ) );
120 
121  if ( !IsDeviceOutputNecessary() || !mbLineColor || ( LineStyle::NONE == rLineInfo.GetStyle() ) || ImplIsRecordLayout() )
122  return;
123 
124  if( !mpGraphics && !AcquireGraphics() )
125  return;
126  assert(mpGraphics);
127 
128  if ( mbInitClipRegion )
129  InitClipRegion();
130 
131  if ( mbOutputClipped )
132  return;
133 
134  const Point aStartPt( ImplLogicToDevicePixel( rStartPt ) );
135  const Point aEndPt( ImplLogicToDevicePixel( rEndPt ) );
136  const LineInfo aInfo( ImplLogicToDevicePixel( rLineInfo ) );
137  const bool bDashUsed(LineStyle::Dash == aInfo.GetStyle());
138  const bool bLineWidthUsed(aInfo.GetWidth() > 1);
139 
140  if ( mbInitLineColor )
141  InitLineColor();
142 
143  if(bDashUsed || bLineWidthUsed)
144  {
145  basegfx::B2DPolygon aLinePolygon;
146  aLinePolygon.append(basegfx::B2DPoint(aStartPt.X(), aStartPt.Y()));
147  aLinePolygon.append(basegfx::B2DPoint(aEndPt.X(), aEndPt.Y()));
148 
149  drawLine( basegfx::B2DPolyPolygon(aLinePolygon), aInfo );
150  }
151  else
152  {
153  mpGraphics->DrawLine( aStartPt.X(), aStartPt.Y(), aEndPt.X(), aEndPt.Y(), *this );
154  }
155 
156  if( mpAlphaVDev )
157  mpAlphaVDev->DrawLine( rStartPt, rEndPt, rLineInfo );
158 }
159 
160 void OutputDevice::DrawLine( const Point& rStartPt, const Point& rEndPt )
161 {
162  assert(!is_double_buffered_window());
163 
164  if ( mpMetaFile )
165  mpMetaFile->AddAction( new MetaLineAction( rStartPt, rEndPt ) );
166 
168  return;
169 
170  if ( !mpGraphics && !AcquireGraphics() )
171  return;
172  assert(mpGraphics);
173 
174  if ( mbInitClipRegion )
175  InitClipRegion();
176 
177  if ( mbOutputClipped )
178  return;
179 
180  if ( mbInitLineColor )
181  InitLineColor();
182 
183  bool bDrawn = false;
184 
185  // #i101598# support AA and snap for lines, too
188  && IsLineColor())
189  {
190  // at least transform with double precision to device coordinates; this will
191  // avoid pixel snap of single, appended lines
193  basegfx::B2DPolygon aB2DPolyLine;
194 
195  aB2DPolyLine.append(basegfx::B2DPoint(rStartPt.X(), rStartPt.Y()));
196  aB2DPolyLine.append(basegfx::B2DPoint(rEndPt.X(), rEndPt.Y()));
197  aB2DPolyLine.transform( aTransform );
198 
199  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
200 
201  bDrawn = mpGraphics->DrawPolyLine(
203  aB2DPolyLine,
204  0.0,
205  0.0, // tdf#124848 hairline
206  nullptr, // MM01
208  css::drawing::LineCap_BUTT,
209  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
210  bPixelSnapHairline,
211  *this);
212  }
213  if(!bDrawn)
214  {
215  const Point aStartPt(ImplLogicToDevicePixel(rStartPt));
216  const Point aEndPt(ImplLogicToDevicePixel(rEndPt));
217  mpGraphics->DrawLine( aStartPt.X(), aStartPt.Y(), aEndPt.X(), aEndPt.Y(), *this );
218  }
219 
220  if( mpAlphaVDev )
221  mpAlphaVDev->DrawLine( rStartPt, rEndPt );
222 }
223 
224 void OutputDevice::drawLine( basegfx::B2DPolyPolygon aLinePolyPolygon, const LineInfo& rInfo )
225 {
228  && IsLineColor());
229  basegfx::B2DPolyPolygon aFillPolyPolygon;
230  const bool bDashUsed(LineStyle::Dash == rInfo.GetStyle());
231  const bool bLineWidthUsed(rInfo.GetWidth() > 1);
232 
233  if(bDashUsed && aLinePolyPolygon.count())
234  {
235  ::std::vector< double > fDotDashArray;
236  const double fDashLen(rInfo.GetDashLen());
237  const double fDotLen(rInfo.GetDotLen());
238  const double fDistance(rInfo.GetDistance());
239 
240  for(sal_uInt16 a(0); a < rInfo.GetDashCount(); a++)
241  {
242  fDotDashArray.push_back(fDashLen);
243  fDotDashArray.push_back(fDistance);
244  }
245 
246  for(sal_uInt16 b(0); b < rInfo.GetDotCount(); b++)
247  {
248  fDotDashArray.push_back(fDotLen);
249  fDotDashArray.push_back(fDistance);
250  }
251 
252  const double fAccumulated(::std::accumulate(fDotDashArray.begin(), fDotDashArray.end(), 0.0));
253 
254  if(fAccumulated > 0.0)
255  {
256  basegfx::B2DPolyPolygon aResult;
257 
258  for(auto const& rPolygon : std::as_const(aLinePolyPolygon))
259  {
260  basegfx::B2DPolyPolygon aLineTarget;
262  rPolygon,
263  fDotDashArray,
264  &aLineTarget);
265  aResult.append(aLineTarget);
266  }
267 
268  aLinePolyPolygon = aResult;
269  }
270  }
271 
272  if(bLineWidthUsed && aLinePolyPolygon.count())
273  {
274  const double fHalfLineWidth((rInfo.GetWidth() * 0.5) + 0.5);
275 
276  if(aLinePolyPolygon.areControlPointsUsed())
277  {
278  // #i110768# When area geometry has to be created, do not
279  // use the fallback bezier decomposition inside createAreaGeometry,
280  // but one that is at least as good as ImplSubdivideBezier was.
281  // There, Polygon::AdaptiveSubdivide was used with default parameter
282  // 1.0 as quality index.
283  aLinePolyPolygon = basegfx::utils::adaptiveSubdivideByDistance(aLinePolyPolygon, 1.0);
284  }
285 
286  for(auto const& rPolygon : std::as_const(aLinePolyPolygon))
287  {
288  aFillPolyPolygon.append(basegfx::utils::createAreaGeometry(
289  rPolygon,
290  fHalfLineWidth,
291  rInfo.GetLineJoin(),
292  rInfo.GetLineCap()));
293  }
294 
295  aLinePolyPolygon.clear();
296  }
297 
298  GDIMetaFile* pOldMetaFile = mpMetaFile;
299  mpMetaFile = nullptr;
300 
301  if(aLinePolyPolygon.count())
302  {
303  for(auto const& rB2DPolygon : std::as_const(aLinePolyPolygon))
304  {
305  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
306  bool bDone(false);
307 
308  if(bTryB2d)
309  {
310  bDone = mpGraphics->DrawPolyLine(
312  rB2DPolygon,
313  0.0,
314  0.0, // tdf#124848 hairline
315  nullptr, // MM01
317  css::drawing::LineCap_BUTT,
318  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
319  bPixelSnapHairline,
320  *this);
321  }
322 
323  if(!bDone)
324  {
325  tools::Polygon aPolygon(rB2DPolygon);
327  aPolygon.GetSize(),
328  aPolygon.GetPointAry(),
329  *this);
330  }
331  }
332  }
333 
334  if(aFillPolyPolygon.count())
335  {
336  const Color aOldLineColor( maLineColor );
337  const Color aOldFillColor( maFillColor );
338 
339  SetLineColor();
340  InitLineColor();
341  SetFillColor( aOldLineColor );
342  InitFillColor();
343 
344  bool bDone(false);
345 
346  if(bTryB2d)
347  {
348  bDone = mpGraphics->DrawPolyPolygon(
350  aFillPolyPolygon,
351  0.0,
352  *this);
353  }
354 
355  if(!bDone)
356  {
357  for(auto const& rB2DPolygon : std::as_const(aFillPolyPolygon))
358  {
359  tools::Polygon aPolygon(rB2DPolygon);
360 
361  // need to subdivide, mpGraphics->DrawPolygon ignores curves
362  aPolygon.AdaptiveSubdivide(aPolygon);
363  mpGraphics->DrawPolygon(aPolygon.GetSize(), aPolygon.GetConstPointAry(), *this);
364  }
365  }
366 
367  SetFillColor( aOldFillColor );
368  SetLineColor( aOldLineColor );
369  }
370 
371  mpMetaFile = pOldMetaFile;
372 }
373 
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void append(const basegfx::B2DPoint &rPoint, sal_uInt32 nCount)
SAL_DLLPRIVATE bool ImplIsRecordLayout() const
Definition: outdev.cxx:702
void DrawLine(tools::Long nX1, tools::Long nY1, tools::Long nX2, tools::Long nY2, const OutputDevice &rOutDev)
bool IsTransparent() const
virtual bool supportsOperation(OutDevSupportType) const =0
Color maLineColor
Definition: outdev.hxx:225
constexpr::Color COL_TRANSPARENT(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
bool mbOutputClipped
Definition: outdev.hxx:244
void DrawPolyPolygon(sal_uInt32 nPoly, const sal_uInt32 *pPoints, const Point **pPtAry, const OutputDevice &rOutDev)
SAL_DLLPRIVATE bool is_double_buffered_window() const
B2DPolyPolygon createAreaGeometry(const B2DPolygon &rCandidate, double fHalfLineWidth, B2DLineJoin eJoin, css::drawing::LineCap eCap, double fMaxAllowedAngle=basegfx::deg2rad(12.5), double fMaxPartOfEdge=0.4, double fMiterMinimumAngle=basegfx::deg2rad(15.0), basegfx::triangulator::B2DTriangleVector *pTriangles=nullptr)
SAL_DLLPRIVATE void InitLineColor()
Definition: line.cxx:84
virtual void SetROPLineColor(SalROPColor nROPColor)=0
AntialiasingFlags mnAntialiasing
Definition: outdev.hxx:236
Color maFillColor
Definition: outdev.hxx:226
void DrawLine(const Point &rStartPt, const Point &rEndPt)
Definition: line.cxx:160
SalGraphics * mpGraphics
Graphics context to draw on.
Definition: outdev.hxx:181
SAL_DLLPRIVATE basegfx::B2DHomMatrix ImplGetDeviceTransformation() const
Get device transformation.
Definition: map.cxx:869
void applyLineDashing(const B2DPolygon &rCandidate, const std::vector< double > &rDotDashArray, B2DPolyPolygon *pLineTarget, B2DPolyPolygon *pGapTarget, double fDotDashLength)
void SetLineColor()
Definition: line.cxx:36
SAL_DLLPRIVATE void InitFillColor()
Definition: fill.cxx:76
void DrawPolygon(sal_uInt32 nPoints, const Point *pPtAry, const OutputDevice &rOutDev)
uno_Any a
Point * GetPointAry()
virtual void SetLineColor()=0
bool IsLineColor() const
Definition: outdev.hxx:507
const Point * GetConstPointAry() const
virtual bool AcquireGraphics() const =0
Acquire a graphics device that the output device uses to draw on.
bool mbInitLineColor
Definition: outdev.hxx:247
void SetFillColor()
Definition: fill.cxx:29
void DrawPolyLine(sal_uInt32 nPoints, Point const *pPtAry, const OutputDevice &rOutDev)
bool mbLineColor
Definition: outdev.hxx:245
sal_uInt16 GetSize() const
constexpr double deg2rad(double v)
RasterOp meRasterOp
Definition: outdev.hxx:231
const AllSettings & GetSettings() const
Definition: outdev.hxx:287
DrawModeFlags GetDrawMode() const
Definition: outdev.hxx:483
SAL_DLLPRIVATE void drawLine(basegfx::B2DPolyPolygon aLinePolyPolygon, const LineInfo &rInfo)
Helper for line geometry paint with support for graphic expansion (pattern and fat_to_area) ...
Definition: line.cxx:224
void transform(const basegfx::B2DHomMatrix &rMatrix)
void append(const B2DPolygon &rPolygon, sal_uInt32 nCount=1)
VclPtr< VirtualDevice > mpAlphaVDev
Definition: outdev.hxx:195
sal_uInt32 count() const
void AddAction(const rtl::Reference< MetaAction > &pAction)
Definition: gdimtf.cxx:562
Color GetLineColor(Color const &rColor, DrawModeFlags nDrawMode, StyleSettings const &rStyleSettings)
Definition: drawmode.cxx:30
bool mbInitClipRegion
Definition: outdev.hxx:251
B2DPolygon adaptiveSubdivideByDistance(const B2DPolygon &rCandidate, double fDistanceBound)
virtual void InitClipRegion()
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_BLACK
RasterOp GetRasterOp() const
Definition: outdev.hxx:492
void AdaptiveSubdivide(tools::Polygon &rResult, const double d=1.0) const
SAL_DLLPRIVATE tools::Rectangle ImplLogicToDevicePixel(const tools::Rectangle &rLogicRect) const
Convert a logical rectangle to a rectangle in physical device pixel units.
Definition: map.cxx:386
#define DBG_TESTSOLARMUTEX()
bool areControlPointsUsed() const
bool IsDeviceOutputNecessary() const
Definition: outdev.hxx:477
GDIMetaFile * mpMetaFile
Definition: outdev.hxx:184