LibreOffice Module sc (master)  1
gridmerg.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 <sal/config.h>
21 
22 #include <vcl/lineinfo.hxx>
23 #include <vcl/outdev.hxx>
24 
25 #include <gridmerg.hxx>
26 
27 #define PAGEBREAK_LINE_DISTANCE_PIXEL 5
28 #define PAGEBREAK_LINE_DASH_LEN_PIXEL 5
29 #define PAGEBREAK_LINE_DASH_COUNT 1
30 
31 ScGridMerger::ScGridMerger( OutputDevice* pOutDev, long nOnePixelX, long nOnePixelY )
32  : pDev(pOutDev)
33  , nOneX(nOnePixelX)
34  , nOneY(nOnePixelY)
35  , nFixStart(0)
36  , nFixEnd(0)
37  , nVarStart(0)
38  , nVarDiff(0)
39  , nCount(0)
40  , bVertical(false)
41 {
42  // optimize (DrawGrid) only for pixel MapMode,
43  // to avoid rounding errors
44 
45  bOptimize = ( pDev->GetMapMode().GetMapUnit() == MapUnit::MapPixel );
46 }
47 
49 {
50  Flush();
51 }
52 
53 void ScGridMerger::AddLine( long nStart, long nEnd, long nPos )
54 {
55  if ( nCount )
56  {
57  // not first line - test fix position
58  // more than one previous line - test distance
59 
60  if ( nStart != nFixStart || nEnd != nFixEnd )
61  {
62  if ( nCount == 1 && nPos == nVarStart &&
63  ( nStart == nFixEnd ||
64  nStart == nFixEnd + ( bVertical ? nOneY : nOneX ) ) )
65  {
66  // additional optimization: extend connected lines
67  // keep nCount at 1
68  nFixEnd = nEnd;
69  }
70  else
71  Flush();
72  }
73  else if ( nCount == 1 )
74  {
75  nVarDiff = nPos - nVarStart;
76  ++nCount;
77  }
78  else if ( nPos != nVarStart + nCount * nVarDiff )
79  Flush();
80  else
81  ++nCount;
82  }
83 
84  if ( !nCount )
85  {
86  // first line (or flushed above) - just store
87 
88  nFixStart = nStart;
89  nFixEnd = nEnd;
90  nVarStart = nPos;
91  nVarDiff = 0;
92  nCount = 1;
93  }
94 }
95 
96 void ScGridMerger::AddHorLine(bool bWorksInPixels, long nX1, long nX2, long nY, bool bDashed)
97 {
98  if ( bWorksInPixels )
99  {
100  Point aPoint(pDev->PixelToLogic(Point(nX1, nY)));
101  nX1 = aPoint.X();
102  nY = aPoint.Y();
103  nX2 = pDev->PixelToLogic(Point(nX2, 0)).X();
104  }
105 
106  if ( bDashed )
107  {
108  // If there are some unflushed lines they must be flushed since
109  // new line is of different style
110  if (bOptimize) {
111  Flush();
112  bVertical = false;
113  }
114 
115  LineInfo aLineInfo(LineStyle::Dash, 1);
117 
118  // Calculating logic values of DashLen and Distance from fixed pixel values
121 
122  aLineInfo.SetDistance( aDashDistanceLen.Width() );
123  aLineInfo.SetDashLen( aDashDistanceLen.Height() );
124 
125  pDev->DrawLine( Point( nX1, nY ), Point( nX2, nY ), aLineInfo );
126  }
127  else if ( bOptimize )
128  {
129  if ( bVertical )
130  {
131  Flush();
132  bVertical = false;
133  }
134  AddLine( nX1, nX2, nY );
135  }
136  else
137  pDev->DrawLine( Point( nX1, nY ), Point( nX2, nY ) );
138 }
139 
140 void ScGridMerger::AddVerLine(bool bWorksInPixels, long nX, long nY1, long nY2, bool bDashed)
141 {
142  if (bWorksInPixels)
143  {
144  Point aPoint(pDev->PixelToLogic(Point(nX, nY1)));
145  nX = aPoint.X();
146  nY1 = aPoint.Y();
147  nY2 = pDev->PixelToLogic(Point(0, nY2)).Y();
148  }
149 
150  if ( bDashed )
151  {
152  // If there are some unflushed lines they must be flushed since
153  // new line is of different style
154  if (bOptimize) {
155  Flush();
156  bVertical = false;
157  }
158 
159  LineInfo aLineInfo(LineStyle::Dash, 1);
161 
162  // Calculating logic values of DashLen and Distance from fixed pixel values
165 
166  aLineInfo.SetDistance( aDashDistanceLen.Width() );
167  aLineInfo.SetDashLen( aDashDistanceLen.Height() );
168 
169  pDev->DrawLine( Point( nX, nY1 ), Point( nX, nY2 ), aLineInfo);
170  }
171  else if ( bOptimize )
172  {
173  if ( !bVertical )
174  {
175  Flush();
176  bVertical = true;
177  }
178  AddLine( nY1, nY2, nX );
179  }
180  else
181  pDev->DrawLine( Point( nX, nY1 ), Point( nX, nY2 ) );
182 }
183 
185 {
186  if (!nCount)
187  return;
188 
189  if (bVertical)
190  {
191  if ( nCount == 1 )
193  else
194  {
195  long nVarEnd = nVarStart + ( nCount - 1 ) * nVarDiff;
196  if ( nVarDiff < 0 )
197  {
198  // nVarDiff is negative in RTL layout mode
199  // Change the positions so DrawGrid is called with a positive distance
200  // (nVarStart / nVarDiff can be modified, aren't used after Flush)
201 
202  nVarDiff = -nVarDiff;
203  long nTemp = nVarStart;
204  nVarStart = nVarEnd;
205  nVarEnd = nTemp;
206  }
209  DrawGridFlags::VertLines );
210  }
211  }
212  else
213  {
214  if ( nCount == 1 )
216  else
217  {
218  long nVarEnd = nVarStart + ( nCount - 1 ) * nVarDiff;
221  DrawGridFlags::HorzLines );
222  }
223  }
224  nCount = 0;
225 }
226 
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define PAGEBREAK_LINE_DASH_LEN_PIXEL
Definition: gridmerg.cxx:28
const MapMode & GetMapMode() const
void SetDashCount(sal_uInt16 nDashCount)
long nFixStart
Definition: gridmerg.hxx:31
void SetDashLen(sal_Int32 nDashLen)
bool bVertical
Definition: gridmerg.hxx:36
long nVarDiff
Definition: gridmerg.hxx:34
int nCount
ScGridMerger(OutputDevice *pOutDev, long nOnePixelX, long nOnePixelY)
Definition: gridmerg.cxx:31
void DrawLine(const Point &rStartPt, const Point &rEndPt)
void SetDistance(sal_Int32 nDistance)
bool bOptimize
Definition: gridmerg.hxx:37
void AddHorLine(bool bWorksInPixels, long nX1, long nX2, long nY, bool bDashed=false)
Definition: gridmerg.cxx:96
MapUnit GetMapUnit() const
Point PixelToLogic(const Point &rDevicePt) const
VclPtr< OutputDevice > pDev
Definition: gridmerg.hxx:28
void DrawGrid(const tools::Rectangle &rRect, const Size &rDist, DrawGridFlags nFlags)
void AddVerLine(bool bWorksInPixels, long nX, long nY1, long nY2, bool bDashed=false)
Definition: gridmerg.cxx:140
void AddLine(long nStart, long nEnd, long nPos)
Definition: gridmerg.cxx:53
void Flush()
Definition: gridmerg.cxx:184
#define PAGEBREAK_LINE_DASH_COUNT
Definition: gridmerg.cxx:29
#define PAGEBREAK_LINE_DISTANCE_PIXEL
Definition: gridmerg.cxx:27
long nFixEnd
Definition: gridmerg.hxx:32
long nVarStart
Definition: gridmerg.hxx:33