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