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