1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <vcl/lineinfo.hxx>
23 #include <vcl/outdev.hxx>
25 #include <gridmerg.hxx>
27 #define PAGEBREAK_LINE_DISTANCE_PIXEL 5
28 #define PAGEBREAK_LINE_DASH_LEN_PIXEL 5
29 #define PAGEBREAK_LINE_DASH_COUNT 1
31 ScGridMerger::ScGridMerger( OutputDevice
* pOutDev
, tools::Long nOnePixelX
, tools::Long nOnePixelY
)
42 // optimize (DrawGrid) only for pixel MapMode,
43 // to avoid rounding errors
45 bOptimize
= ( pDev
->GetMapMode().GetMapUnit() == MapUnit::MapPixel
);
48 ScGridMerger::~ScGridMerger()
53 void ScGridMerger::AddLine( tools::Long nStart
, tools::Long nEnd
, tools::Long nPos
)
57 // not first line - test fix position
58 // more than one previous line - test distance
60 if ( nStart
!= nFixStart
|| nEnd
!= nFixEnd
)
62 if ( nCount
== 1 && nPos
== nVarStart
&&
63 ( nStart
== nFixEnd
||
64 nStart
== nFixEnd
+ ( bVertical
? nOneY
: nOneX
) ) )
66 // additional optimization: extend connected lines
73 else if ( nCount
== 1 )
75 nVarDiff
= nPos
- nVarStart
;
78 else if ( nPos
!= nVarStart
+ nCount
* nVarDiff
) //! keep VarEnd?
86 // first line (or flushed above) - just store
96 void ScGridMerger::AddHorLine(bool bWorksInPixels
, tools::Long nX1
, tools::Long nX2
, tools::Long nY
, bool bDashed
)
100 Point
aPoint(pDev
->PixelToLogic(Point(nX1
, nY
)));
103 nX2
= pDev
->PixelToLogic(Point(nX2
, 0)).X();
108 // If there are some unflushed lines they must be flushed since
109 // new line is of different style
115 LineInfo
aLineInfo(LineStyle::Dash
, 1);
116 aLineInfo
.SetDashCount( PAGEBREAK_LINE_DASH_COUNT
);
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
)));
122 aLineInfo
.SetDistance( aDashDistanceLen
.Width() );
123 aLineInfo
.SetDashLen( aDashDistanceLen
.Height() );
125 pDev
->DrawLine( Point( nX1
, nY
), Point( nX2
, nY
), aLineInfo
);
127 else if ( bOptimize
)
134 AddLine( nX1
, nX2
, nY
);
137 pDev
->DrawLine( Point( nX1
, nY
), Point( nX2
, nY
) );
140 void ScGridMerger::AddVerLine(bool bWorksInPixels
, tools::Long nX
, tools::Long nY1
, tools::Long nY2
, bool bDashed
)
144 Point
aPoint(pDev
->PixelToLogic(Point(nX
, nY1
)));
147 nY2
= pDev
->PixelToLogic(Point(0, nY2
)).Y();
152 // If there are some unflushed lines they must be flushed since
153 // new line is of different style
159 LineInfo
aLineInfo(LineStyle::Dash
, 1);
160 aLineInfo
.SetDashCount( PAGEBREAK_LINE_DASH_COUNT
);
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
)));
166 aLineInfo
.SetDistance( aDashDistanceLen
.Width() );
167 aLineInfo
.SetDashLen( aDashDistanceLen
.Height() );
169 pDev
->DrawLine( Point( nX
, nY1
), Point( nX
, nY2
), aLineInfo
);
171 else if ( bOptimize
)
178 AddLine( nY1
, nY2
, nX
);
181 pDev
->DrawLine( Point( nX
, nY1
), Point( nX
, nY2
) );
184 void ScGridMerger::Flush()
192 pDev
->DrawLine( Point( nVarStart
, nFixStart
), Point( nVarStart
, nFixEnd
) );
195 tools::Long nVarEnd
= nVarStart
+ ( nCount
- 1 ) * nVarDiff
;
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)
202 nVarDiff
= -nVarDiff
;
203 std::swap( nVarStart
, nVarEnd
);
205 pDev
->DrawGrid( tools::Rectangle( nVarStart
, nFixStart
, nVarEnd
, nFixEnd
),
206 Size( nVarDiff
, nFixEnd
- nFixStart
),
207 DrawGridFlags::VertLines
);
213 pDev
->DrawLine( Point( nFixStart
, nVarStart
), Point( nFixEnd
, nVarStart
) );
216 tools::Long nVarEnd
= nVarStart
+ ( nCount
- 1 ) * nVarDiff
;
217 pDev
->DrawGrid( tools::Rectangle( nFixStart
, nVarStart
, nFixEnd
, nVarEnd
),
218 Size( nFixEnd
- nFixStart
, nVarDiff
),
219 DrawGridFlags::HorzLines
);
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */