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 = rInfo.GetDotDashArray();
236  const double fAccumulated(::std::accumulate(fDotDashArray.begin(), fDotDashArray.end(), 0.0));
237 
238  if(fAccumulated > 0.0)
239  {
240  basegfx::B2DPolyPolygon aResult;
241 
242  for(auto const& rPolygon : std::as_const(aLinePolyPolygon))
243  {
244  basegfx::B2DPolyPolygon aLineTarget;
246  rPolygon,
247  fDotDashArray,
248  &aLineTarget);
249  aResult.append(aLineTarget);
250  }
251 
252  aLinePolyPolygon = aResult;
253  }
254  }
255 
256  if(bLineWidthUsed && aLinePolyPolygon.count())
257  {
258  const double fHalfLineWidth((rInfo.GetWidth() * 0.5) + 0.5);
259 
260  if(aLinePolyPolygon.areControlPointsUsed())
261  {
262  // #i110768# When area geometry has to be created, do not
263  // use the fallback bezier decomposition inside createAreaGeometry,
264  // but one that is at least as good as ImplSubdivideBezier was.
265  // There, Polygon::AdaptiveSubdivide was used with default parameter
266  // 1.0 as quality index.
267  aLinePolyPolygon = basegfx::utils::adaptiveSubdivideByDistance(aLinePolyPolygon, 1.0);
268  }
269 
270  for(auto const& rPolygon : std::as_const(aLinePolyPolygon))
271  {
272  aFillPolyPolygon.append(basegfx::utils::createAreaGeometry(
273  rPolygon,
274  fHalfLineWidth,
275  rInfo.GetLineJoin(),
276  rInfo.GetLineCap()));
277  }
278 
279  aLinePolyPolygon.clear();
280  }
281 
282  GDIMetaFile* pOldMetaFile = mpMetaFile;
283  mpMetaFile = nullptr;
284 
285  if(aLinePolyPolygon.count())
286  {
287  for(auto const& rB2DPolygon : std::as_const(aLinePolyPolygon))
288  {
289  const bool bPixelSnapHairline(mnAntialiasing & AntialiasingFlags::PixelSnapHairline);
290  bool bDone(false);
291 
292  if(bTryB2d)
293  {
294  bDone = mpGraphics->DrawPolyLine(
296  rB2DPolygon,
297  0.0,
298  0.0, // tdf#124848 hairline
299  nullptr, // MM01
301  css::drawing::LineCap_BUTT,
302  basegfx::deg2rad(15.0), // not used with B2DLineJoin::NONE, but the correct default
303  bPixelSnapHairline,
304  *this);
305  }
306 
307  if(!bDone)
308  {
309  tools::Polygon aPolygon(rB2DPolygon);
311  aPolygon.GetSize(),
312  aPolygon.GetPointAry(),
313  *this);
314  }
315  }
316  }
317 
318  if(aFillPolyPolygon.count())
319  {
320  const Color aOldLineColor( maLineColor );
321  const Color aOldFillColor( maFillColor );
322 
323  SetLineColor();
324  InitLineColor();
325  SetFillColor( aOldLineColor );
326  InitFillColor();
327 
328  bool bDone(false);
329 
330  if(bTryB2d)
331  {
332  bDone = mpGraphics->DrawPolyPolygon(
334  aFillPolyPolygon,
335  0.0,
336  *this);
337  }
338 
339  if(!bDone)
340  {
341  for(auto const& rB2DPolygon : std::as_const(aFillPolyPolygon))
342  {
343  tools::Polygon aPolygon(rB2DPolygon);
344 
345  // need to subdivide, mpGraphics->DrawPolygon ignores curves
346  aPolygon.AdaptiveSubdivide(aPolygon);
347  mpGraphics->DrawPolygon(aPolygon.GetSize(), aPolygon.GetConstPointAry(), *this);
348  }
349  }
350 
351  SetFillColor( aOldFillColor );
352  SetLineColor( aOldLineColor );
353  }
354 
355  mpMetaFile = pOldMetaFile;
356 }
357 
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void append(const basegfx::B2DPoint &rPoint, sal_uInt32 nCount)
SAL_DLLPRIVATE bool ImplIsRecordLayout() const
Definition: outdev.cxx:724
void DrawLine(tools::Long nX1, tools::Long nY1, tools::Long nX2, tools::Long nY2, const OutputDevice &rOutDev)
bool IsTransparent() const
constexpr double deg2rad(double v)
virtual bool supportsOperation(OutDevSupportType) const =0
Color maLineColor
Definition: outdev.hxx:232
constexpr::Color COL_TRANSPARENT(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
bool mbOutputClipped
Definition: outdev.hxx:252
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:243
Color maFillColor
Definition: outdev.hxx:233
void DrawLine(const Point &rStartPt, const Point &rEndPt)
Definition: line.cxx:160
SalGraphics * mpGraphics
Graphics context to draw on.
Definition: outdev.hxx:188
SAL_DLLPRIVATE basegfx::B2DHomMatrix ImplGetDeviceTransformation() const
Get device transformation.
Definition: map.cxx:924
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)
Point * GetPointAry()
virtual void SetLineColor()=0
bool IsLineColor() const
Definition: outdev.hxx:522
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:255
void SetFillColor()
Definition: fill.cxx:29
void DrawPolyLine(sal_uInt32 nPoints, Point const *pPtAry, const OutputDevice &rOutDev)
bool mbLineColor
Definition: outdev.hxx:253
sal_uInt16 GetSize() const
RasterOp meRasterOp
Definition: outdev.hxx:238
const AllSettings & GetSettings() const
Definition: outdev.hxx:295
DrawModeFlags GetDrawMode() const
Definition: outdev.hxx:498
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:202
sal_uInt32 count() const
void AddAction(const rtl::Reference< MetaAction > &pAction)
Definition: gdimtf.cxx:581
Color GetLineColor(Color const &rColor, DrawModeFlags nDrawMode, StyleSettings const &rStyleSettings)
Definition: drawmode.cxx:30
bool mbInitClipRegion
Definition: outdev.hxx:259
B2DPolygon adaptiveSubdivideByDistance(const B2DPolygon &rCandidate, double fDistanceBound)
virtual void InitClipRegion()
const ::std::vector< Color > ImpSvNumberformatScan::StandardColor COL_BLACK
RasterOp GetRasterOp() const
Definition: outdev.hxx:507
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:488
GDIMetaFile * mpMetaFile
Definition: outdev.hxx:191