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
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
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 {
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;
91 nVarDiff = 0;
92 nCount = 1;
93 }
94}
95
96void 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);
117
118 // Calculating logic values of DashLen and Distance from fixed pixel values
119 Size aDashDistanceLen( pDev->PixelToLogic( Size( PAGEBREAK_LINE_DISTANCE_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
140void 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);
161
162 // Calculating logic values of DashLen and Distance from fixed pixel values
163 Size aDashDistanceLen( pDev->PixelToLogic( Size( PAGEBREAK_LINE_DISTANCE_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
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
203 std::swap( nVarStart, nVarEnd );
204 }
205 pDev->DrawGrid( tools::Rectangle( nVarStart, nFixStart, nVarEnd, nFixEnd ),
207 DrawGridFlags::VertLines );
208 }
209 }
210 else
211 {
212 if ( nCount == 1 )
213 pDev->DrawLine( Point( nFixStart, nVarStart ), Point( nFixEnd, nVarStart ) );
214 else
215 {
216 tools::Long nVarEnd = nVarStart + ( nCount - 1 ) * nVarDiff;
217 pDev->DrawGrid( tools::Rectangle( nFixStart, nVarStart, nFixEnd, nVarEnd ),
219 DrawGridFlags::HorzLines );
220 }
221 }
222 nCount = 0;
223}
224
225/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetDashCount(sal_uInt16 nDashCount)
void SetDashLen(double nDashLen)
void SetDistance(double nDistance)
constexpr tools::Long Y() const
constexpr tools::Long X() const
tools::Long nVarStart
Definition: gridmerg.hxx:35
tools::Long nVarDiff
Definition: gridmerg.hxx:36
tools::Long nOneY
Definition: gridmerg.hxx:32
ScGridMerger(OutputDevice *pOutDev, tools::Long nOnePixelX, tools::Long nOnePixelY)
Definition: gridmerg.cxx:31
tools::Long nFixEnd
Definition: gridmerg.hxx:34
tools::Long nFixStart
Definition: gridmerg.hxx:33
void AddHorLine(bool bWorksInPixels, tools::Long nX1, tools::Long nX2, tools::Long nY, bool bDashed=false)
Definition: gridmerg.cxx:96
void Flush()
Definition: gridmerg.cxx:184
tools::Long nCount
Definition: gridmerg.hxx:37
bool bVertical
Definition: gridmerg.hxx:38
bool bOptimize
Definition: gridmerg.hxx:39
void AddVerLine(bool bWorksInPixels, tools::Long nX, tools::Long nY1, tools::Long nY2, bool bDashed=false)
Definition: gridmerg.cxx:140
VclPtr< OutputDevice > pDev
Definition: gridmerg.hxx:30
void AddLine(tools::Long nStart, tools::Long nEnd, tools::Long nPos)
Definition: gridmerg.cxx:53
tools::Long nOneX
Definition: gridmerg.hxx:31
constexpr tools::Long Height() const
constexpr tools::Long Width() const
int nCount
#define PAGEBREAK_LINE_DASH_LEN_PIXEL
Definition: gridmerg.cxx:28
#define PAGEBREAK_LINE_DISTANCE_PIXEL
Definition: gridmerg.cxx:27
#define PAGEBREAK_LINE_DASH_COUNT
Definition: gridmerg.cxx:29
sal_uInt16 nPos
long Long