LibreOffice Module vcl (master)  1
outdev/hatch.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 <cassert>
21 #include <cstdlib>
22 
23 #include <osl/diagnose.h>
24 #include <tools/line.hxx>
25 #include <tools/helpers.hxx>
26 
27 #include <vcl/hatch.hxx>
28 #include <vcl/metaact.hxx>
29 #include <vcl/settings.hxx>
30 #include <vcl/outdev.hxx>
31 #include <vcl/virdev.hxx>
32 
33 #include <drawmode.hxx>
34 #include <salgdi.hxx>
35 
36 #include <memory>
37 
38 #define HATCH_MAXPOINTS 1024
39 
40 extern "C" {
41 
42 static int HatchCmpFnc( const void* p1, const void* p2 )
43 {
44  const tools::Long nX1 = static_cast<Point const *>(p1)->X();
45  const tools::Long nX2 = static_cast<Point const *>(p2)->X();
46  const tools::Long nY1 = static_cast<Point const *>(p1)->Y();
47  const tools::Long nY2 = static_cast<Point const *>(p2)->Y();
48 
49  return ( nX1 > nX2 ? 1 : nX1 == nX2 ? nY1 > nY2 ? 1: nY1 == nY2 ? 0 : -1 : -1 );
50 }
51 
52 }
53 
54 void OutputDevice::DrawHatch( const tools::PolyPolygon& rPolyPoly, const Hatch& rHatch )
55 {
56  assert(!is_double_buffered_window());
57 
58  Hatch aHatch( rHatch );
60 
61  if( mpMetaFile )
62  mpMetaFile->AddAction( new MetaHatchAction( rPolyPoly, aHatch ) );
63 
65  return;
66 
67  if( !mpGraphics && !AcquireGraphics() )
68  return;
69  assert(mpGraphics);
70 
71  if( mbInitClipRegion )
73 
74  if( mbOutputClipped )
75  return;
76 
77  if( rPolyPoly.Count() )
78  {
79  tools::PolyPolygon aPolyPoly( LogicToPixel( rPolyPoly ) );
80  GDIMetaFile* pOldMetaFile = mpMetaFile;
81  bool bOldMap = mbMap;
82 
83  aPolyPoly.Optimize( PolyOptimizeFlags::NO_SAME );
85 
86  mpMetaFile = nullptr;
87  EnableMapMode( false );
89  SetLineColor( aHatch.GetColor() );
90  InitLineColor();
91  DrawHatch( aPolyPoly, aHatch, false );
92  Pop();
93  EnableMapMode( bOldMap );
94  mpMetaFile = pOldMetaFile;
95  }
96 
97  if( mpAlphaVDev )
98  mpAlphaVDev->DrawHatch( rPolyPoly, rHatch );
99 }
100 
101 void OutputDevice::AddHatchActions( const tools::PolyPolygon& rPolyPoly, const Hatch& rHatch,
102  GDIMetaFile& rMtf )
103 {
104 
105  tools::PolyPolygon aPolyPoly( rPolyPoly );
106  aPolyPoly.Optimize( PolyOptimizeFlags::NO_SAME | PolyOptimizeFlags::CLOSE );
107 
108  if( aPolyPoly.Count() )
109  {
110  GDIMetaFile* pOldMtf = mpMetaFile;
111 
112  mpMetaFile = &rMtf;
114  mpMetaFile->AddAction( new MetaLineColorAction( rHatch.GetColor(), true ) );
115  DrawHatch( aPolyPoly, rHatch, true );
117  mpMetaFile = pOldMtf;
118  }
119 }
120 
121 void OutputDevice::DrawHatch( const tools::PolyPolygon& rPolyPoly, const Hatch& rHatch, bool bMtf )
122 {
123  assert(!is_double_buffered_window());
124 
125  if(!rPolyPoly.Count())
126  return;
127 
128  // #i115630# DrawHatch does not work with beziers included in the polypolygon, take care of that
129  bool bIsCurve(false);
130 
131  for(sal_uInt16 a(0); !bIsCurve && a < rPolyPoly.Count(); a++)
132  {
133  if(rPolyPoly[a].HasFlags())
134  {
135  bIsCurve = true;
136  }
137  }
138 
139  if(bIsCurve)
140  {
141  OSL_ENSURE(false, "DrawHatch does *not* support curves, falling back to AdaptiveSubdivide()...");
142  tools::PolyPolygon aPolyPoly;
143 
144  rPolyPoly.AdaptiveSubdivide(aPolyPoly);
145  DrawHatch(aPolyPoly, rHatch, bMtf);
146  }
147  else
148  {
149  tools::Rectangle aRect( rPolyPoly.GetBoundRect() );
150  const tools::Long nLogPixelWidth = ImplDevicePixelToLogicWidth( 1 );
151  const tools::Long nWidth = ImplDevicePixelToLogicWidth( std::max( ImplLogicWidthToDevicePixel( rHatch.GetDistance() ), tools::Long(3) ) );
152  std::unique_ptr<Point[]> pPtBuffer(new Point[ HATCH_MAXPOINTS ]);
153  Point aPt1, aPt2, aEndPt1;
154  Size aInc;
155 
156  // Single hatch
157  aRect.AdjustLeft( -nLogPixelWidth ); aRect.AdjustTop( -nLogPixelWidth ); aRect.AdjustRight(nLogPixelWidth ); aRect.AdjustBottom(nLogPixelWidth );
158  CalcHatchValues( aRect, nWidth, rHatch.GetAngle(), aPt1, aPt2, aInc, aEndPt1 );
159  do
160  {
161  DrawHatchLine( tools::Line( aPt1, aPt2 ), rPolyPoly, pPtBuffer.get(), bMtf );
162  aPt1.AdjustX(aInc.Width() ); aPt1.AdjustY(aInc.Height() );
163  aPt2.AdjustX(aInc.Width() ); aPt2.AdjustY(aInc.Height() );
164  }
165  while( ( aPt1.X() <= aEndPt1.X() ) && ( aPt1.Y() <= aEndPt1.Y() ) );
166 
167  if( ( rHatch.GetStyle() == HatchStyle::Double ) || ( rHatch.GetStyle() == HatchStyle::Triple ) )
168  {
169  // Double hatch
170  CalcHatchValues( aRect, nWidth, rHatch.GetAngle() + 900_deg10, aPt1, aPt2, aInc, aEndPt1 );
171  do
172  {
173  DrawHatchLine( tools::Line( aPt1, aPt2 ), rPolyPoly, pPtBuffer.get(), bMtf );
174  aPt1.AdjustX(aInc.Width() ); aPt1.AdjustY(aInc.Height() );
175  aPt2.AdjustX(aInc.Width() ); aPt2.AdjustY(aInc.Height() );
176  }
177  while( ( aPt1.X() <= aEndPt1.X() ) && ( aPt1.Y() <= aEndPt1.Y() ) );
178 
179  if( rHatch.GetStyle() == HatchStyle::Triple )
180  {
181  // Triple hatch
182  CalcHatchValues( aRect, nWidth, rHatch.GetAngle() + 450_deg10, aPt1, aPt2, aInc, aEndPt1 );
183  do
184  {
185  DrawHatchLine( tools::Line( aPt1, aPt2 ), rPolyPoly, pPtBuffer.get(), bMtf );
186  aPt1.AdjustX(aInc.Width() ); aPt1.AdjustY(aInc.Height() );
187  aPt2.AdjustX(aInc.Width() ); aPt2.AdjustY(aInc.Height() );
188  }
189  while( ( aPt1.X() <= aEndPt1.X() ) && ( aPt1.Y() <= aEndPt1.Y() ) );
190  }
191  }
192  }
193 }
194 
196  Point& rPt1, Point& rPt2, Size& rInc, Point& rEndPt1 )
197 {
198  Point aRef;
199  Degree10 nAngle = nAngle10 % 1800_deg10;
200  tools::Long nOffset = 0;
201 
202  if( nAngle > 900_deg10 )
203  nAngle -= 1800_deg10;
204 
205  aRef = ( !IsRefPoint() ? rRect.TopLeft() : GetRefPoint() );
206 
207  if( 0_deg10 == nAngle )
208  {
209  rInc = Size( 0, nDist );
210  rPt1 = rRect.TopLeft();
211  rPt2 = rRect.TopRight();
212  rEndPt1 = rRect.BottomLeft();
213 
214  if( aRef.Y() <= rRect.Top() )
215  nOffset = ( ( rRect.Top() - aRef.Y() ) % nDist );
216  else
217  nOffset = ( nDist - ( ( aRef.Y() - rRect.Top() ) % nDist ) );
218 
219  rPt1.AdjustY( -nOffset );
220  rPt2.AdjustY( -nOffset );
221  }
222  else if( 900_deg10 == nAngle )
223  {
224  rInc = Size( nDist, 0 );
225  rPt1 = rRect.TopLeft();
226  rPt2 = rRect.BottomLeft();
227  rEndPt1 = rRect.TopRight();
228 
229  if( aRef.X() <= rRect.Left() )
230  nOffset = ( rRect.Left() - aRef.X() ) % nDist;
231  else
232  nOffset = nDist - ( ( aRef.X() - rRect.Left() ) % nDist );
233 
234  rPt1.AdjustX( -nOffset );
235  rPt2.AdjustX( -nOffset );
236  }
237  else if( nAngle >= Degree10(-450) && nAngle <= 450_deg10 )
238  {
239  const double fAngle = F_PI1800 * std::abs( nAngle.get() );
240  const double fTan = tan( fAngle );
241  const tools::Long nYOff = FRound( ( rRect.Right() - rRect.Left() ) * fTan );
242  tools::Long nPY;
243 
244  nDist = FRound( nDist / cos( fAngle ) );
245  rInc = Size( 0, nDist );
246 
247  if( nAngle > 0_deg10 )
248  {
249  rPt1 = rRect.TopLeft();
250  rPt2 = Point( rRect.Right(), rRect.Top() - nYOff );
251  rEndPt1 = Point( rRect.Left(), rRect.Bottom() + nYOff );
252  nPY = FRound( aRef.Y() - ( ( rPt1.X() - aRef.X() ) * fTan ) );
253  }
254  else
255  {
256  rPt1 = rRect.TopRight();
257  rPt2 = Point( rRect.Left(), rRect.Top() - nYOff );
258  rEndPt1 = Point( rRect.Right(), rRect.Bottom() + nYOff );
259  nPY = FRound( aRef.Y() + ( ( rPt1.X() - aRef.X() ) * fTan ) );
260  }
261 
262  if( nPY <= rPt1.Y() )
263  nOffset = ( rPt1.Y() - nPY ) % nDist;
264  else
265  nOffset = nDist - ( ( nPY - rPt1.Y() ) % nDist );
266 
267  rPt1.AdjustY( -nOffset );
268  rPt2.AdjustY( -nOffset );
269  }
270  else
271  {
272  const double fAngle = F_PI1800 * std::abs( nAngle.get() );
273  const double fTan = tan( fAngle );
274  const tools::Long nXOff = FRound( ( rRect.Bottom() - rRect.Top() ) / fTan );
275  tools::Long nPX;
276 
277  nDist = FRound( nDist / sin( fAngle ) );
278  rInc = Size( nDist, 0 );
279 
280  if( nAngle > 0_deg10 )
281  {
282  rPt1 = rRect.TopLeft();
283  rPt2 = Point( rRect.Left() - nXOff, rRect.Bottom() );
284  rEndPt1 = Point( rRect.Right() + nXOff, rRect.Top() );
285  nPX = FRound( aRef.X() - ( ( rPt1.Y() - aRef.Y() ) / fTan ) );
286  }
287  else
288  {
289  rPt1 = rRect.BottomLeft();
290  rPt2 = Point( rRect.Left() - nXOff, rRect.Top() );
291  rEndPt1 = Point( rRect.Right() + nXOff, rRect.Bottom() );
292  nPX = FRound( aRef.X() + ( ( rPt1.Y() - aRef.Y() ) / fTan ) );
293  }
294 
295  if( nPX <= rPt1.X() )
296  nOffset = ( rPt1.X() - nPX ) % nDist;
297  else
298  nOffset = nDist - ( ( nPX - rPt1.X() ) % nDist );
299 
300  rPt1.AdjustX( -nOffset );
301  rPt2.AdjustX( -nOffset );
302  }
303 }
304 
305 void OutputDevice::DrawHatchLine( const tools::Line& rLine, const tools::PolyPolygon& rPolyPoly,
306  Point* pPtBuffer, bool bMtf )
307 {
308  assert(!is_double_buffered_window());
309 
310  double fX, fY;
311  tools::Long nAdd, nPCounter = 0;
312 
313  for( tools::Long nPoly = 0, nPolyCount = rPolyPoly.Count(); nPoly < nPolyCount; nPoly++ )
314  {
315  const tools::Polygon& rPoly = rPolyPoly[ static_cast<sal_uInt16>(nPoly) ];
316 
317  if( rPoly.GetSize() > 1 )
318  {
319  tools::Line aCurSegment( rPoly[ 0 ], Point() );
320 
321  for( tools::Long i = 1, nCount = rPoly.GetSize(); i <= nCount; i++ )
322  {
323  aCurSegment.SetEnd( rPoly[ static_cast<sal_uInt16>( i % nCount ) ] );
324  nAdd = 0;
325 
326  if( rLine.Intersection( aCurSegment, fX, fY ) )
327  {
328  if( ( fabs( fX - aCurSegment.GetStart().X() ) <= 0.0000001 ) &&
329  ( fabs( fY - aCurSegment.GetStart().Y() ) <= 0.0000001 ) )
330  {
331  const tools::Line aPrevSegment( rPoly[ static_cast<sal_uInt16>( ( i > 1 ) ? ( i - 2 ) : ( nCount - 1 ) ) ], aCurSegment.GetStart() );
332  const double fPrevDistance = rLine.GetDistance( aPrevSegment.GetStart() );
333  const double fCurDistance = rLine.GetDistance( aCurSegment.GetEnd() );
334 
335  if( ( fPrevDistance <= 0.0 && fCurDistance > 0.0 ) ||
336  ( fPrevDistance > 0.0 && fCurDistance < 0.0 ) )
337  {
338  nAdd = 1;
339  }
340  }
341  else if( ( fabs( fX - aCurSegment.GetEnd().X() ) <= 0.0000001 ) &&
342  ( fabs( fY - aCurSegment.GetEnd().Y() ) <= 0.0000001 ) )
343  {
344  const tools::Line aNextSegment( aCurSegment.GetEnd(), rPoly[ static_cast<sal_uInt16>( ( i + 1 ) % nCount ) ] );
345 
346  if( ( fabs( rLine.GetDistance( aNextSegment.GetEnd() ) ) <= 0.0000001 ) &&
347  ( rLine.GetDistance( aCurSegment.GetStart() ) > 0.0 ) )
348  {
349  nAdd = 1;
350  }
351  }
352  else
353  nAdd = 1;
354 
355  if( nAdd )
356  pPtBuffer[ nPCounter++ ] = Point( FRound( fX ), FRound( fY ) );
357  }
358 
359  aCurSegment.SetStart( aCurSegment.GetEnd() );
360  }
361  }
362  }
363 
364  if( nPCounter <= 1 )
365  return;
366 
367  qsort( pPtBuffer, nPCounter, sizeof( Point ), HatchCmpFnc );
368 
369  if( nPCounter & 1 )
370  nPCounter--;
371 
372  if( bMtf )
373  {
374  for( tools::Long i = 0; i < nPCounter; i += 2 )
375  mpMetaFile->AddAction( new MetaLineAction( pPtBuffer[ i ], pPtBuffer[ i + 1 ] ) );
376  }
377  else
378  {
379  for( tools::Long i = 0; i < nPCounter; i += 2 )
380  DrawHatchLine_DrawLine(pPtBuffer[i], pPtBuffer[i+1]);
381  }
382 }
383 
384 void OutputDevice::DrawHatchLine_DrawLine(const Point& rStartPoint, const Point& rEndPoint)
385 {
386  Point aPt1{ImplLogicToDevicePixel(rStartPoint)}, aPt2{ImplLogicToDevicePixel(rEndPoint)};
387  mpGraphics->DrawLine(aPt1.X(), aPt1.Y(), aPt2.X(), aPt2.Y(), *this);
388 }
389 
390 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 Count() const
bool Intersection(const tools::Line &rLine, double &rIntersectionX, double &rIntersectionY) const
void SetDistance(tools::Long nDistance)
Definition: gdi/hatch.cxx:69
Definition: hatch.hxx:46
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)
const Point & GetRefPoint() const
Definition: outdev.hxx:303
constexpr tools::Long Left() const
SAL_DLLPRIVATE void CalcHatchValues(const tools::Rectangle &rRect, tools::Long nDist, Degree10 nAngle10, Point &rPt1, Point &rPt2, Size &rInc, Point &rEndPt1)
long Long
const StyleSettings & GetStyleSettings() const
#define F_PI1800
void Push(vcl::PushFlags nFlags=vcl::PushFlags::ALL)
Definition: stack.cxx:33
bool mbOutputClipped
Definition: outdev.hxx:244
SAL_DLLPRIVATE bool is_double_buffered_window() const
void EnableMapMode(bool bEnable=true)
Definition: map.cxx:586
bool mbMap
Definition: outdev.hxx:239
SAL_DLLPRIVATE void InitLineColor()
Definition: line.cxx:84
constexpr Point BottomLeft() const
int nCount
void Pop()
Definition: stack.cxx:92
const Point & GetEnd() const
HatchStyle GetStyle() const
Definition: hatch.hxx:59
static int HatchCmpFnc(const void *p1, const void *p2)
void AdaptiveSubdivide(tools::PolyPolygon &rResult) const
#define HATCH_MAXPOINTS
SalGraphics * mpGraphics
Graphics context to draw on.
Definition: outdev.hxx:181
void Optimize(PolyOptimizeFlags nOptimizeFlags)
void SetLineColor()
Definition: line.cxx:36
UNDERLYING_TYPE get() const
int i
uno_Any a
tools::Long FRound(double fVal)
SAL_DLLPRIVATE void DrawHatchLine(const tools::Line &rLine, const tools::PolyPolygon &rPolyPoly, Point *pPtBuffer, bool bMtf)
virtual bool AcquireGraphics() const =0
Acquire a graphics device that the output device uses to draw on.
const Color & GetColor() const
Definition: hatch.hxx:62
void SetEnd(const Point &rEndPt)
constexpr tools::Long Right() const
SAL_DLLPRIVATE tools::Long ImplLogicWidthToDevicePixel(tools::Long nWidth) const
Convert a logical width to a width in units of device pixels.
Definition: map.cxx:324
constexpr tools::Long Top() const
sal_uInt16 GetSize() const
const AllSettings & GetSettings() const
Definition: outdev.hxx:287
DrawModeFlags GetDrawMode() const
Definition: outdev.hxx:483
void DrawHatch(const tools::PolyPolygon &rPolyPoly, const Hatch &rHatch)
#define Y
virtual void DrawHatchLine_DrawLine(const Point &rStartPoint, const Point &rEndPoint)
constexpr Point TopLeft() const
constexpr tools::Long Bottom() const
SAL_WARN_UNUSED_RESULT Point LogicToPixel(const Point &rLogicPt) const
Definition: map.cxx:878
VclPtr< VirtualDevice > mpAlphaVDev
Definition: outdev.hxx:195
void SetStart(const Point &rStartPt)
bool IsRefPoint() const
Definition: outdev.hxx:304
void AddAction(const rtl::Reference< MetaAction > &pAction)
Definition: gdimtf.cxx:562
bool mbInitClipRegion
Definition: outdev.hxx:251
constexpr Point TopRight() const
virtual void InitClipRegion()
void AddHatchActions(const tools::PolyPolygon &rPolyPoly, const Hatch &rHatch, GDIMetaFile &rMtf)
tools::Long GetDistance() const
Definition: hatch.hxx:65
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
Color GetHatchColor(Color const &rColor, DrawModeFlags nDrawMode, StyleSettings const &rStyleSettings)
Definition: drawmode.cxx:102
tools::Rectangle GetBoundRect() const
double GetDistance(const double &rPtX, const double &rPtY) const
Degree10 GetAngle() const
Definition: hatch.hxx:68
const Point & GetStart() const
SAL_DLLPRIVATE tools::Long ImplDevicePixelToLogicWidth(tools::Long nWidth) const
Convert device pixels to a width in logical units.
Definition: map.cxx:348
bool IsDeviceOutputNecessary() const
Definition: outdev.hxx:477
void SetColor(const Color &rColor)
Definition: gdi/hatch.cxx:64
GDIMetaFile * mpMetaFile
Definition: outdev.hxx:184