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 
ScGridMerger(OutputDevice * pOutDev,tools::Long nOnePixelX,tools::Long nOnePixelY)31 ScGridMerger::ScGridMerger( OutputDevice* pOutDev, tools::Long nOnePixelX, tools::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 
~ScGridMerger()48 ScGridMerger::~ScGridMerger()
49 {
50     Flush();
51 }
52 
AddLine(tools::Long nStart,tools::Long nEnd,tools::Long nPos)53 void ScGridMerger::AddLine( tools::Long nStart, tools::Long nEnd, tools::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 )       //! keep VarEnd?
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 
AddHorLine(bool bWorksInPixels,tools::Long nX1,tools::Long nX2,tools::Long nY,bool bDashed)96 void ScGridMerger::AddHorLine(bool bWorksInPixels, tools::Long nX1, tools::Long nX2, tools::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);
116         aLineInfo.SetDashCount( PAGEBREAK_LINE_DASH_COUNT );
117 
118         // Calculating logic values of DashLen and Distance from fixed pixel values
119         Size aDashDistanceLen( pDev->PixelToLogic( Size( PAGEBREAK_LINE_DISTANCE_PIXEL,
120                                                          PAGEBREAK_LINE_DASH_LEN_PIXEL )));
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 
AddVerLine(bool bWorksInPixels,tools::Long nX,tools::Long nY1,tools::Long nY2,bool bDashed)140 void ScGridMerger::AddVerLine(bool bWorksInPixels, tools::Long nX, tools::Long nY1, tools::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);
160         aLineInfo.SetDashCount( PAGEBREAK_LINE_DASH_COUNT );
161 
162         // Calculating logic values of DashLen and Distance from fixed pixel values
163         Size aDashDistanceLen( pDev->PixelToLogic( Size( PAGEBREAK_LINE_DISTANCE_PIXEL,
164                                                          PAGEBREAK_LINE_DASH_LEN_PIXEL )));
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 
Flush()184 void ScGridMerger::Flush()
185 {
186     if (!nCount)
187         return;
188 
189     if (bVertical)
190     {
191         if ( nCount == 1 )
192             pDev->DrawLine( Point( nVarStart, nFixStart ), Point( nVarStart, nFixEnd ) );
193         else
194         {
195             tools::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                 tools::Long nTemp = nVarStart;
204                 nVarStart = nVarEnd;
205                 nVarEnd = nTemp;
206             }
207             pDev->DrawGrid( tools::Rectangle( nVarStart, nFixStart, nVarEnd, nFixEnd ),
208                             Size( nVarDiff, nFixEnd - nFixStart ),
209                             DrawGridFlags::VertLines );
210         }
211     }
212     else
213     {
214         if ( nCount == 1 )
215             pDev->DrawLine( Point( nFixStart, nVarStart ), Point( nFixEnd, nVarStart ) );
216         else
217         {
218             tools::Long nVarEnd = nVarStart + ( nCount - 1 ) * nVarDiff;
219             pDev->DrawGrid( tools::Rectangle( nFixStart, nVarStart, nFixEnd, nVarEnd ),
220                             Size( nFixEnd - nFixStart, nVarDiff ),
221                             DrawGridFlags::HorzLines );
222         }
223     }
224     nCount = 0;
225 }
226 
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
228