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/window.hxx>
21 :
22 : #include "invmerge.hxx"
23 :
24 : //------------------------------------------------------------------
25 :
26 214 : ScInvertMerger::ScInvertMerger( ::std::vector< Rectangle >* pRectangles ) :
27 214 : pRects( pRectangles )
28 : {
29 : // collect rectangles instead of inverting
30 214 : }
31 :
32 214 : ScInvertMerger::~ScInvertMerger()
33 : {
34 214 : Flush();
35 214 : }
36 :
37 214 : void ScInvertMerger::Flush()
38 : {
39 214 : FlushLine();
40 214 : FlushTotal();
41 :
42 : OSL_ENSURE( aLineRect.IsEmpty() && aTotalRect.IsEmpty(), "Flush: not empty" );
43 :
44 214 : if ( pRects )
45 : {
46 : //
47 : // also join vertically if there are non-adjacent columns involved
48 : //
49 :
50 214 : size_t nComparePos = 0;
51 667 : while ( nComparePos < pRects->size() )
52 : {
53 239 : Rectangle aCompRect = (*pRects)[nComparePos];
54 239 : sal_Int32 nBottom = aCompRect.Bottom();
55 239 : size_t nOtherPos = nComparePos + 1;
56 :
57 575 : while ( nOtherPos < pRects->size() )
58 : {
59 108 : Rectangle aOtherRect = (*pRects)[nOtherPos];
60 108 : if ( aOtherRect.Top() > nBottom + 1 )
61 : {
62 : // rectangles are sorted, so we can stop searching
63 11 : break;
64 : }
65 266 : if ( aOtherRect.Top() == nBottom + 1 &&
66 157 : aOtherRect.Left() == aCompRect.Left() &&
67 60 : aOtherRect.Right() == aCompRect.Right() )
68 : {
69 : // extend first rectangle
70 48 : nBottom = aOtherRect.Bottom();
71 48 : aCompRect.Bottom() = nBottom;
72 48 : (*pRects)[nComparePos].Bottom() = nBottom;
73 :
74 : // remove second rectangle
75 48 : pRects->erase( pRects->begin() + nOtherPos );
76 :
77 : // continue at unmodified nOtherPos
78 : }
79 : else
80 49 : ++nOtherPos;
81 : }
82 :
83 239 : ++nComparePos;
84 : }
85 : }
86 214 : }
87 :
88 287 : void ScInvertMerger::FlushTotal()
89 : {
90 287 : if( aTotalRect.IsEmpty() )
91 287 : return; // nothing to do
92 :
93 287 : if ( pRects )
94 287 : pRects->push_back( aTotalRect );
95 :
96 287 : aTotalRect.SetEmpty();
97 : }
98 :
99 594 : void ScInvertMerger::FlushLine()
100 : {
101 594 : if( aLineRect.IsEmpty() )
102 594 : return; // nothing to do
103 :
104 594 : if ( aTotalRect.IsEmpty() )
105 : {
106 214 : aTotalRect = aLineRect; // start new total rect
107 : }
108 : else
109 : {
110 1079 : if ( aLineRect.Left() == aTotalRect.Left() &&
111 687 : aLineRect.Right() == aTotalRect.Right() &&
112 307 : aLineRect.Top() == aTotalRect.Bottom() + 1 )
113 : {
114 : // extend total rect
115 307 : aTotalRect.Bottom() = aLineRect.Bottom();
116 : }
117 : else
118 : {
119 73 : FlushTotal(); // draw old total rect
120 73 : aTotalRect = aLineRect; // and start new one
121 : }
122 : }
123 :
124 594 : aLineRect.SetEmpty();
125 : }
126 :
127 1743 : void ScInvertMerger::AddRect( const Rectangle& rRect )
128 : {
129 1743 : Rectangle aJustified = rRect;
130 1743 : if ( rRect.Left() > rRect.Right() ) // switch for RTL layout
131 : {
132 0 : aJustified.Left() = rRect.Right();
133 0 : aJustified.Right() = rRect.Left();
134 : }
135 :
136 1743 : if ( aLineRect.IsEmpty() )
137 : {
138 214 : aLineRect = aJustified; // start new line rect
139 : }
140 : else
141 : {
142 1529 : sal_Bool bDone = false;
143 2703 : if ( aJustified.Top() == aLineRect.Top() &&
144 1174 : aJustified.Bottom() == aLineRect.Bottom() )
145 : {
146 : // try to extend line rect
147 1174 : if ( aJustified.Left() == aLineRect.Right() + 1 )
148 : {
149 1149 : aLineRect.Right() = aJustified.Right();
150 1149 : bDone = sal_True;
151 : }
152 25 : else if ( aJustified.Right() + 1 == aLineRect.Left() ) // for RTL layout
153 : {
154 0 : aLineRect.Left() = aJustified.Left();
155 0 : bDone = sal_True;
156 : }
157 : }
158 1529 : if (!bDone)
159 : {
160 380 : FlushLine(); // use old line rect for total rect
161 380 : aLineRect = aJustified; // and start new one
162 : }
163 : }
164 1836 : }
165 :
166 :
167 :
168 :
169 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|