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