Line data Source code
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 <vcl/outdev.hxx>
21 :
22 : #include "gridmerg.hxx"
23 :
24 0 : ScGridMerger::ScGridMerger( OutputDevice* pOutDev, long nOnePixelX, long nOnePixelY )
25 : : pDev(pOutDev)
26 : , nOneX(nOnePixelX)
27 : , nOneY(nOnePixelY)
28 : , nFixStart(0)
29 : , nFixEnd(0)
30 : , nVarStart(0)
31 : , nVarDiff(0)
32 : , nCount(0)
33 0 : , bVertical(false)
34 : {
35 : // optimize (DrawGrid) only for pixel MapMode,
36 : // to avoid rounding errors
37 :
38 0 : bOptimize = ( pDev->GetMapMode().GetMapUnit() == MAP_PIXEL );
39 0 : }
40 :
41 0 : ScGridMerger::~ScGridMerger()
42 : {
43 0 : Flush();
44 0 : }
45 :
46 0 : void ScGridMerger::AddLine( long nStart, long nEnd, long nPos )
47 : {
48 0 : if ( nCount )
49 : {
50 : // not first line - test fix position
51 : // more than one previous line - test distance
52 :
53 0 : if ( nStart != nFixStart || nEnd != nFixEnd )
54 : {
55 0 : if ( nCount == 1 && nPos == nVarStart &&
56 0 : ( nStart == nFixEnd ||
57 0 : nStart == nFixEnd + ( bVertical ? nOneY : nOneX ) ) )
58 : {
59 : // additional optimization: extend connected lines
60 : // keep nCount at 1
61 0 : nFixEnd = nEnd;
62 : }
63 : else
64 0 : Flush();
65 : }
66 0 : else if ( nCount == 1 )
67 : {
68 0 : nVarDiff = nPos - nVarStart;
69 0 : ++nCount;
70 : }
71 0 : else if ( nPos != nVarStart + nCount * nVarDiff ) //! keep VarEnd?
72 0 : Flush();
73 : else
74 0 : ++nCount;
75 : }
76 :
77 0 : if ( !nCount )
78 : {
79 : // first line (or flushed above) - just store
80 :
81 0 : nFixStart = nStart;
82 0 : nFixEnd = nEnd;
83 0 : nVarStart = nPos;
84 0 : nVarDiff = 0;
85 0 : nCount = 1;
86 : }
87 0 : }
88 :
89 0 : void ScGridMerger::AddHorLine( long nX1, long nX2, long nY )
90 : {
91 0 : if ( bOptimize )
92 : {
93 0 : if ( bVertical )
94 : {
95 0 : Flush();
96 0 : bVertical = false;
97 : }
98 0 : AddLine( nX1, nX2, nY );
99 : }
100 : else
101 0 : pDev->DrawLine( Point( nX1, nY ), Point( nX2, nY ) );
102 0 : }
103 :
104 0 : void ScGridMerger::AddVerLine( long nX, long nY1, long nY2 )
105 : {
106 0 : if ( bOptimize )
107 : {
108 0 : if ( !bVertical )
109 : {
110 0 : Flush();
111 0 : bVertical = true;
112 : }
113 0 : AddLine( nY1, nY2, nX );
114 : }
115 : else
116 0 : pDev->DrawLine( Point( nX, nY1 ), Point( nX, nY2 ) );
117 0 : }
118 :
119 0 : void ScGridMerger::Flush()
120 : {
121 0 : if (nCount)
122 : {
123 0 : if (bVertical)
124 : {
125 0 : if ( nCount == 1 )
126 0 : pDev->DrawLine( Point( nVarStart, nFixStart ), Point( nVarStart, nFixEnd ) );
127 : else
128 : {
129 0 : long nVarEnd = nVarStart + ( nCount - 1 ) * nVarDiff;
130 0 : if ( nVarDiff < 0 )
131 : {
132 : // nVarDiff is negative in RTL layout mode
133 : // Change the positions so DrawGrid is called with a positive distance
134 : // (nVarStart / nVarDiff can be modified, aren't used after Flush)
135 :
136 0 : nVarDiff = -nVarDiff;
137 0 : long nTemp = nVarStart;
138 0 : nVarStart = nVarEnd;
139 0 : nVarEnd = nTemp;
140 : }
141 : pDev->DrawGrid( Rectangle( nVarStart, nFixStart, nVarEnd, nFixEnd ),
142 : Size( nVarDiff, nFixEnd - nFixStart ),
143 0 : GRID_VERTLINES );
144 : }
145 : }
146 : else
147 : {
148 0 : if ( nCount == 1 )
149 0 : pDev->DrawLine( Point( nFixStart, nVarStart ), Point( nFixEnd, nVarStart ) );
150 : else
151 : {
152 0 : long nVarEnd = nVarStart + ( nCount - 1 ) * nVarDiff;
153 : pDev->DrawGrid( Rectangle( nFixStart, nVarStart, nFixEnd, nVarEnd ),
154 : Size( nFixEnd - nFixStart, nVarDiff ),
155 0 : GRID_HORZLINES );
156 : }
157 : }
158 0 : nCount = 0;
159 : }
160 0 : }
161 :
162 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|