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 <svx/framelinkarray.hxx>
21 
22 #include <math.h>
23 #include <vector>
24 #include <set>
25 #include <algorithm>
26 #include <tools/debug.hxx>
27 #include <tools/gen.hxx>
28 #include <vcl/canvastools.hxx>
29 #include <svx/sdr/primitive2d/sdrframeborderprimitive2d.hxx>
30 #include <basegfx/matrix/b2dhommatrixtools.hxx>
31 
32 namespace svx::frame {
33 
34 namespace {
35 
36 class Cell
37 {
38 private:
39     Style               maLeft;
40     Style               maRight;
41     Style               maTop;
42     Style               maBottom;
43     Style               maTLBR;
44     Style               maBLTR;
45 
46 public:
47     tools::Long                mnAddLeft;
48     tools::Long                mnAddRight;
49     tools::Long                mnAddTop;
50     tools::Long                mnAddBottom;
51 
52     SvxRotateMode       meRotMode;
53     double              mfOrientation;
54 
55     bool                mbMergeOrig;
56     bool                mbOverlapX;
57     bool                mbOverlapY;
58 
59 public:
60     explicit            Cell();
61 
SetStyleLeft(const Style & rStyle)62     void SetStyleLeft(const Style& rStyle) { maLeft = rStyle; }
SetStyleRight(const Style & rStyle)63     void SetStyleRight(const Style& rStyle) { maRight = rStyle; }
SetStyleTop(const Style & rStyle)64     void SetStyleTop(const Style& rStyle) { maTop = rStyle; }
SetStyleBottom(const Style & rStyle)65     void SetStyleBottom(const Style& rStyle) { maBottom = rStyle; }
SetStyleTLBR(const Style & rStyle)66     void SetStyleTLBR(const Style& rStyle) { maTLBR = rStyle; }
SetStyleBLTR(const Style & rStyle)67     void SetStyleBLTR(const Style& rStyle) { maBLTR = rStyle; }
68 
GetStyleLeft() const69     const Style& GetStyleLeft() const { return maLeft; }
GetStyleRight() const70     const Style& GetStyleRight() const { return maRight; }
GetStyleTop() const71     const Style& GetStyleTop() const { return maTop; }
GetStyleBottom() const72     const Style& GetStyleBottom() const { return maBottom; }
GetStyleTLBR() const73     const Style& GetStyleTLBR() const { return maTLBR; }
GetStyleBLTR() const74     const Style& GetStyleBLTR() const { return maBLTR; }
75 
IsMerged() const76     bool                IsMerged() const { return mbMergeOrig || mbOverlapX || mbOverlapY; }
IsRotated() const77     bool                IsRotated() const { return mfOrientation != 0.0; }
78 
79     void                MirrorSelfX();
80 
81     basegfx::B2DHomMatrix CreateCoordinateSystem(const Array& rArray, size_t nCol, size_t nRow, bool bExpandMerged) const;
82 };
83 
84 }
85 
86 typedef std::vector< Cell >     CellVec;
87 
CreateCoordinateSystem(const Array & rArray,size_t nCol,size_t nRow,bool bExpandMerged) const88 basegfx::B2DHomMatrix Cell::CreateCoordinateSystem(const Array& rArray, size_t nCol, size_t nRow, bool bExpandMerged) const
89 {
90     basegfx::B2DHomMatrix aRetval;
91     const basegfx::B2DRange aRange(rArray.GetCellRange(nCol, nRow, bExpandMerged));
92 
93     if(!aRange.isEmpty())
94     {
95         basegfx::B2DPoint aOrigin(aRange.getMinimum());
96         basegfx::B2DVector aX(aRange.getWidth(), 0.0);
97         basegfx::B2DVector aY(0.0, aRange.getHeight());
98 
99         if (IsRotated() && SvxRotateMode::SVX_ROTATE_MODE_STANDARD != meRotMode)
100         {
101             // when rotated, adapt values. Get Skew (cos/sin == 1/tan)
102             const double fSkew(aY.getY() * (cos(mfOrientation) / sin(mfOrientation)));
103 
104             switch (meRotMode)
105             {
106             case SvxRotateMode::SVX_ROTATE_MODE_TOP:
107                 // shear Y-Axis
108                 aY.setX(-fSkew);
109                 break;
110             case SvxRotateMode::SVX_ROTATE_MODE_CENTER:
111                 // shear origin half, Y full
112                 aOrigin.setX(aOrigin.getX() + (fSkew * 0.5));
113                 aY.setX(-fSkew);
114                 break;
115             case SvxRotateMode::SVX_ROTATE_MODE_BOTTOM:
116                 // shear origin full, Y full
117                 aOrigin.setX(aOrigin.getX() + fSkew);
118                 aY.setX(-fSkew);
119                 break;
120             default: // SvxRotateMode::SVX_ROTATE_MODE_STANDARD, already excluded above
121                 break;
122             }
123         }
124 
125         // use column vectors as coordinate axes, homogen column for translation
126         aRetval = basegfx::utils::createCoordinateSystemTransform(aOrigin, aX, aY);
127     }
128 
129     return aRetval;
130 }
131 
Cell()132 Cell::Cell() :
133     mnAddLeft( 0 ),
134     mnAddRight( 0 ),
135     mnAddTop( 0 ),
136     mnAddBottom( 0 ),
137     meRotMode(SvxRotateMode::SVX_ROTATE_MODE_STANDARD ),
138     mfOrientation( 0.0 ),
139     mbMergeOrig( false ),
140     mbOverlapX( false ),
141     mbOverlapY( false )
142 {
143 }
144 
MirrorSelfX()145 void Cell::MirrorSelfX()
146 {
147     std::swap( maLeft, maRight );
148     std::swap( mnAddLeft, mnAddRight );
149     maLeft.MirrorSelf();
150     maRight.MirrorSelf();
151     mfOrientation = -mfOrientation;
152 }
153 
154 
lclRecalcCoordVec(std::vector<tools::Long> & rCoords,const std::vector<tools::Long> & rSizes)155 static void lclRecalcCoordVec( std::vector<tools::Long>& rCoords, const std::vector<tools::Long>& rSizes )
156 {
157     DBG_ASSERT( rCoords.size() == rSizes.size() + 1, "lclRecalcCoordVec - inconsistent vectors" );
158     auto aCIt = rCoords.begin();
159     for( const auto& rSize : rSizes )
160     {
161         *(aCIt + 1) = *aCIt + rSize;
162         ++aCIt;
163     }
164 }
165 
lclSetMergedRange(CellVec & rCells,size_t nWidth,size_t nFirstCol,size_t nFirstRow,size_t nLastCol,size_t nLastRow)166 static void lclSetMergedRange( CellVec& rCells, size_t nWidth, size_t nFirstCol, size_t nFirstRow, size_t nLastCol, size_t nLastRow )
167 {
168     for( size_t nCol = nFirstCol; nCol <= nLastCol; ++nCol )
169     {
170         for( size_t nRow = nFirstRow; nRow <= nLastRow; ++nRow )
171         {
172             Cell& rCell = rCells[ nRow * nWidth + nCol ];
173             rCell.mbMergeOrig = false;
174             rCell.mbOverlapX = nCol > nFirstCol;
175             rCell.mbOverlapY = nRow > nFirstRow;
176         }
177     }
178     rCells[ nFirstRow * nWidth + nFirstCol ].mbMergeOrig = true;
179 }
180 
181 
182 const Style OBJ_STYLE_NONE;
183 const Cell OBJ_CELL_NONE;
184 
185 struct ArrayImpl
186 {
187     CellVec             maCells;
188     std::vector<tools::Long>   maWidths;
189     std::vector<tools::Long>   maHeights;
190     mutable std::vector<tools::Long>     maXCoords;
191     mutable std::vector<tools::Long>     maYCoords;
192     size_t              mnWidth;
193     size_t              mnHeight;
194     size_t              mnFirstClipCol;
195     size_t              mnFirstClipRow;
196     size_t              mnLastClipCol;
197     size_t              mnLastClipRow;
198     mutable bool        mbXCoordsDirty;
199     mutable bool        mbYCoordsDirty;
200     bool                mbMayHaveCellRotation;
201 
202     explicit            ArrayImpl( size_t nWidth, size_t nHeight );
203 
IsValidPossvx::frame::ArrayImpl204     bool         IsValidPos( size_t nCol, size_t nRow ) const
205                             { return (nCol < mnWidth) && (nRow < mnHeight); }
GetIndexsvx::frame::ArrayImpl206     size_t       GetIndex( size_t nCol, size_t nRow ) const
207                             { return nRow * mnWidth + nCol; }
208 
209     const Cell&         GetCell( size_t nCol, size_t nRow ) const;
210     Cell&               GetCellAcc( size_t nCol, size_t nRow );
211 
212     size_t              GetMergedFirstCol( size_t nCol, size_t nRow ) const;
213     size_t              GetMergedFirstRow( size_t nCol, size_t nRow ) const;
214     size_t              GetMergedLastCol( size_t nCol, size_t nRow ) const;
215     size_t              GetMergedLastRow( size_t nCol, size_t nRow ) const;
216 
217     const Cell&         GetMergedOriginCell( size_t nCol, size_t nRow ) const;
218 
219     bool                IsMergedOverlappedLeft( size_t nCol, size_t nRow ) const;
220     bool                IsMergedOverlappedRight( size_t nCol, size_t nRow ) const;
221     bool                IsMergedOverlappedTop( size_t nCol, size_t nRow ) const;
222     bool                IsMergedOverlappedBottom( size_t nCol, size_t nRow ) const;
223 
224     bool                IsInClipRange( size_t nCol, size_t nRow ) const;
225     bool                IsColInClipRange( size_t nCol ) const;
226     bool                IsRowInClipRange( size_t nRow ) const;
227 
GetMirrorColsvx::frame::ArrayImpl228     size_t       GetMirrorCol( size_t nCol ) const { return mnWidth - nCol - 1; }
229 
230     tools::Long                GetColPosition( size_t nCol ) const;
231     tools::Long                GetRowPosition( size_t nRow ) const;
232 
233     bool                HasCellRotation() const;
234 };
235 
ArrayImpl(size_t nWidth,size_t nHeight)236 ArrayImpl::ArrayImpl( size_t nWidth, size_t nHeight ) :
237     mnWidth( nWidth ),
238     mnHeight( nHeight ),
239     mnFirstClipCol( 0 ),
240     mnFirstClipRow( 0 ),
241     mnLastClipCol( nWidth - 1 ),
242     mnLastClipRow( nHeight - 1 ),
243     mbXCoordsDirty( false ),
244     mbYCoordsDirty( false ),
245     mbMayHaveCellRotation( false )
246 {
247     // default-construct all vectors
248     maCells.resize( mnWidth * mnHeight );
249     maWidths.resize( mnWidth, 0 );
250     maHeights.resize( mnHeight, 0 );
251     maXCoords.resize( mnWidth + 1, 0 );
252     maYCoords.resize( mnHeight + 1, 0 );
253 }
254 
GetCell(size_t nCol,size_t nRow) const255 const Cell& ArrayImpl::GetCell( size_t nCol, size_t nRow ) const
256 {
257     return IsValidPos( nCol, nRow ) ? maCells[ GetIndex( nCol, nRow ) ] : OBJ_CELL_NONE;
258 }
259 
GetCellAcc(size_t nCol,size_t nRow)260 Cell& ArrayImpl::GetCellAcc( size_t nCol, size_t nRow )
261 {
262     static Cell aDummy;
263     return IsValidPos( nCol, nRow ) ? maCells[ GetIndex( nCol, nRow ) ] : aDummy;
264 }
265 
GetMergedFirstCol(size_t nCol,size_t nRow) const266 size_t ArrayImpl::GetMergedFirstCol( size_t nCol, size_t nRow ) const
267 {
268     size_t nFirstCol = nCol;
269     while( (nFirstCol > 0) && GetCell( nFirstCol, nRow ).mbOverlapX ) --nFirstCol;
270     return nFirstCol;
271 }
272 
GetMergedFirstRow(size_t nCol,size_t nRow) const273 size_t ArrayImpl::GetMergedFirstRow( size_t nCol, size_t nRow ) const
274 {
275     size_t nFirstRow = nRow;
276     while( (nFirstRow > 0) && GetCell( nCol, nFirstRow ).mbOverlapY ) --nFirstRow;
277     return nFirstRow;
278 }
279 
GetMergedLastCol(size_t nCol,size_t nRow) const280 size_t ArrayImpl::GetMergedLastCol( size_t nCol, size_t nRow ) const
281 {
282     size_t nLastCol = nCol + 1;
283     while( (nLastCol < mnWidth) && GetCell( nLastCol, nRow ).mbOverlapX ) ++nLastCol;
284     return nLastCol - 1;
285 }
286 
GetMergedLastRow(size_t nCol,size_t nRow) const287 size_t ArrayImpl::GetMergedLastRow( size_t nCol, size_t nRow ) const
288 {
289     size_t nLastRow = nRow + 1;
290     while( (nLastRow < mnHeight) && GetCell( nCol, nLastRow ).mbOverlapY ) ++nLastRow;
291     return nLastRow - 1;
292 }
293 
GetMergedOriginCell(size_t nCol,size_t nRow) const294 const Cell& ArrayImpl::GetMergedOriginCell( size_t nCol, size_t nRow ) const
295 {
296     return GetCell( GetMergedFirstCol( nCol, nRow ), GetMergedFirstRow( nCol, nRow ) );
297 }
298 
IsMergedOverlappedLeft(size_t nCol,size_t nRow) const299 bool ArrayImpl::IsMergedOverlappedLeft( size_t nCol, size_t nRow ) const
300 {
301     const Cell& rCell = GetCell( nCol, nRow );
302     return rCell.mbOverlapX || (rCell.mnAddLeft > 0);
303 }
304 
IsMergedOverlappedRight(size_t nCol,size_t nRow) const305 bool ArrayImpl::IsMergedOverlappedRight( size_t nCol, size_t nRow ) const
306 {
307     return GetCell( nCol + 1, nRow ).mbOverlapX || (GetCell( nCol, nRow ).mnAddRight > 0);
308 }
309 
IsMergedOverlappedTop(size_t nCol,size_t nRow) const310 bool ArrayImpl::IsMergedOverlappedTop( size_t nCol, size_t nRow ) const
311 {
312     const Cell& rCell = GetCell( nCol, nRow );
313     return rCell.mbOverlapY || (rCell.mnAddTop > 0);
314 }
315 
IsMergedOverlappedBottom(size_t nCol,size_t nRow) const316 bool ArrayImpl::IsMergedOverlappedBottom( size_t nCol, size_t nRow ) const
317 {
318     return GetCell( nCol, nRow + 1 ).mbOverlapY || (GetCell( nCol, nRow ).mnAddBottom > 0);
319 }
320 
IsColInClipRange(size_t nCol) const321 bool ArrayImpl::IsColInClipRange( size_t nCol ) const
322 {
323     return (mnFirstClipCol <= nCol) && (nCol <= mnLastClipCol);
324 }
325 
IsRowInClipRange(size_t nRow) const326 bool ArrayImpl::IsRowInClipRange( size_t nRow ) const
327 {
328     return (mnFirstClipRow <= nRow) && (nRow <= mnLastClipRow);
329 }
330 
IsInClipRange(size_t nCol,size_t nRow) const331 bool ArrayImpl::IsInClipRange( size_t nCol, size_t nRow ) const
332 {
333     return IsColInClipRange( nCol ) && IsRowInClipRange( nRow );
334 }
335 
GetColPosition(size_t nCol) const336 tools::Long ArrayImpl::GetColPosition( size_t nCol ) const
337 {
338     if( mbXCoordsDirty )
339     {
340         lclRecalcCoordVec( maXCoords, maWidths );
341         mbXCoordsDirty = false;
342     }
343     return maXCoords[ nCol ];
344 }
345 
GetRowPosition(size_t nRow) const346 tools::Long ArrayImpl::GetRowPosition( size_t nRow ) const
347 {
348     if( mbYCoordsDirty )
349     {
350         lclRecalcCoordVec( maYCoords, maHeights );
351         mbYCoordsDirty = false;
352     }
353     return maYCoords[ nRow ];
354 }
355 
HasCellRotation() const356 bool ArrayImpl::HasCellRotation() const
357 {
358     // check cell array
359     for (const auto& aCell : maCells)
360     {
361         if (aCell.IsRotated())
362         {
363             return true;
364         }
365     }
366 
367     return false;
368 }
369 
370 namespace {
371 
372 class MergedCellIterator
373 {
374 public:
375     explicit            MergedCellIterator( const Array& rArray, size_t nCol, size_t nRow );
376 
Is() const377     bool         Is() const { return (mnCol <= mnLastCol) && (mnRow <= mnLastRow); }
Col() const378     size_t       Col() const { return mnCol; }
Row() const379     size_t       Row() const { return mnRow; }
380 
381     MergedCellIterator& operator++();
382 
383 private:
384     size_t              mnFirstCol;
385     size_t              mnFirstRow;
386     size_t              mnLastCol;
387     size_t              mnLastRow;
388     size_t              mnCol;
389     size_t              mnRow;
390 };
391 
392 }
393 
MergedCellIterator(const Array & rArray,size_t nCol,size_t nRow)394 MergedCellIterator::MergedCellIterator( const Array& rArray, size_t nCol, size_t nRow )
395 {
396     DBG_ASSERT( rArray.IsMerged( nCol, nRow ), "svx::frame::MergedCellIterator::MergedCellIterator - not in merged range" );
397     rArray.GetMergedRange( mnFirstCol, mnFirstRow, mnLastCol, mnLastRow, nCol, nRow );
398     mnCol = mnFirstCol;
399     mnRow = mnFirstRow;
400 }
401 
operator ++()402 MergedCellIterator& MergedCellIterator::operator++()
403 {
404     DBG_ASSERT( Is(), "svx::frame::MergedCellIterator::operator++() - already invalid" );
405     if( ++mnCol > mnLastCol )
406     {
407         mnCol = mnFirstCol;
408         ++mnRow;
409     }
410     return *this;
411 }
412 
413 
414 #define DBG_FRAME_CHECK( cond, funcname, error )        DBG_ASSERT( cond, "svx::frame::Array::" funcname " - " error )
415 #define DBG_FRAME_CHECK_COL( col, funcname )            DBG_FRAME_CHECK( (col) < GetColCount(), funcname, "invalid column index" )
416 #define DBG_FRAME_CHECK_ROW( row, funcname )            DBG_FRAME_CHECK( (row) < GetRowCount(), funcname, "invalid row index" )
417 #define DBG_FRAME_CHECK_COLROW( col, row, funcname )    DBG_FRAME_CHECK( ((col) < GetColCount()) && ((row) < GetRowCount()), funcname, "invalid cell index" )
418 #define DBG_FRAME_CHECK_COL_1( col, funcname )          DBG_FRAME_CHECK( (col) <= GetColCount(), funcname, "invalid column index" )
419 #define DBG_FRAME_CHECK_ROW_1( row, funcname )          DBG_FRAME_CHECK( (row) <= GetRowCount(), funcname, "invalid row index" )
420 
421 
422 #define CELL( col, row )        mxImpl->GetCell( col, row )
423 #define CELLACC( col, row )     mxImpl->GetCellAcc( col, row )
424 #define ORIGCELL( col, row )    mxImpl->GetMergedOriginCell( col, row )
425 
426 
Array()427 Array::Array()
428 {
429     Initialize( 0, 0 );
430 }
431 
~Array()432 Array::~Array()
433 {
434 }
435 
436 // array size and column/row indexes
Initialize(size_t nWidth,size_t nHeight)437 void Array::Initialize( size_t nWidth, size_t nHeight )
438 {
439     mxImpl.reset( new ArrayImpl( nWidth, nHeight ) );
440 }
441 
GetColCount() const442 size_t Array::GetColCount() const
443 {
444     return mxImpl->mnWidth;
445 }
446 
GetRowCount() const447 size_t Array::GetRowCount() const
448 {
449     return mxImpl->mnHeight;
450 }
451 
GetCellCount() const452 size_t Array::GetCellCount() const
453 {
454     return mxImpl->maCells.size();
455 }
456 
GetCellIndex(size_t nCol,size_t nRow,bool bRTL) const457 size_t Array::GetCellIndex( size_t nCol, size_t nRow, bool bRTL ) const
458 {
459     DBG_FRAME_CHECK_COLROW( nCol, nRow, "GetCellIndex" );
460     if (bRTL)
461         nCol = mxImpl->GetMirrorCol(nCol);
462     return mxImpl->GetIndex( nCol, nRow );
463 }
464 
465 // cell border styles
SetCellStyleLeft(size_t nCol,size_t nRow,const Style & rStyle)466 void Array::SetCellStyleLeft( size_t nCol, size_t nRow, const Style& rStyle )
467 {
468     DBG_FRAME_CHECK_COLROW( nCol, nRow, "SetCellStyleLeft" );
469     CELLACC( nCol, nRow ).SetStyleLeft(rStyle);
470 }
471 
SetCellStyleRight(size_t nCol,size_t nRow,const Style & rStyle)472 void Array::SetCellStyleRight( size_t nCol, size_t nRow, const Style& rStyle )
473 {
474     DBG_FRAME_CHECK_COLROW( nCol, nRow, "SetCellStyleRight" );
475     CELLACC( nCol, nRow ).SetStyleRight(rStyle);
476 }
477 
SetCellStyleTop(size_t nCol,size_t nRow,const Style & rStyle)478 void Array::SetCellStyleTop( size_t nCol, size_t nRow, const Style& rStyle )
479 {
480     DBG_FRAME_CHECK_COLROW( nCol, nRow, "SetCellStyleTop" );
481     CELLACC( nCol, nRow ).SetStyleTop(rStyle);
482 }
483 
SetCellStyleBottom(size_t nCol,size_t nRow,const Style & rStyle)484 void Array::SetCellStyleBottom( size_t nCol, size_t nRow, const Style& rStyle )
485 {
486     DBG_FRAME_CHECK_COLROW( nCol, nRow, "SetCellStyleBottom" );
487     CELLACC( nCol, nRow ).SetStyleBottom(rStyle);
488 }
489 
SetCellStyleTLBR(size_t nCol,size_t nRow,const Style & rStyle)490 void Array::SetCellStyleTLBR( size_t nCol, size_t nRow, const Style& rStyle )
491 {
492     DBG_FRAME_CHECK_COLROW( nCol, nRow, "SetCellStyleTLBR" );
493     CELLACC( nCol, nRow ).SetStyleTLBR(rStyle);
494 }
495 
SetCellStyleBLTR(size_t nCol,size_t nRow,const Style & rStyle)496 void Array::SetCellStyleBLTR( size_t nCol, size_t nRow, const Style& rStyle )
497 {
498     DBG_FRAME_CHECK_COLROW( nCol, nRow, "SetCellStyleBLTR" );
499     CELLACC( nCol, nRow ).SetStyleBLTR(rStyle);
500 }
501 
SetCellStyleDiag(size_t nCol,size_t nRow,const Style & rTLBR,const Style & rBLTR)502 void Array::SetCellStyleDiag( size_t nCol, size_t nRow, const Style& rTLBR, const Style& rBLTR )
503 {
504     DBG_FRAME_CHECK_COLROW( nCol, nRow, "SetCellStyleDiag" );
505     Cell& rCell = CELLACC( nCol, nRow );
506     rCell.SetStyleTLBR(rTLBR);
507     rCell.SetStyleBLTR(rBLTR);
508 }
509 
SetColumnStyleLeft(size_t nCol,const Style & rStyle)510 void Array::SetColumnStyleLeft( size_t nCol, const Style& rStyle )
511 {
512     DBG_FRAME_CHECK_COL( nCol, "SetColumnStyleLeft" );
513     for( size_t nRow = 0; nRow < mxImpl->mnHeight; ++nRow )
514         SetCellStyleLeft( nCol, nRow, rStyle );
515 }
516 
SetColumnStyleRight(size_t nCol,const Style & rStyle)517 void Array::SetColumnStyleRight( size_t nCol, const Style& rStyle )
518 {
519     DBG_FRAME_CHECK_COL( nCol, "SetColumnStyleRight" );
520     for( size_t nRow = 0; nRow < mxImpl->mnHeight; ++nRow )
521         SetCellStyleRight( nCol, nRow, rStyle );
522 }
523 
SetRowStyleTop(size_t nRow,const Style & rStyle)524 void Array::SetRowStyleTop( size_t nRow, const Style& rStyle )
525 {
526     DBG_FRAME_CHECK_ROW( nRow, "SetRowStyleTop" );
527     for( size_t nCol = 0; nCol < mxImpl->mnWidth; ++nCol )
528         SetCellStyleTop( nCol, nRow, rStyle );
529 }
530 
SetRowStyleBottom(size_t nRow,const Style & rStyle)531 void Array::SetRowStyleBottom( size_t nRow, const Style& rStyle )
532 {
533     DBG_FRAME_CHECK_ROW( nRow, "SetRowStyleBottom" );
534     for( size_t nCol = 0; nCol < mxImpl->mnWidth; ++nCol )
535         SetCellStyleBottom( nCol, nRow, rStyle );
536 }
537 
SetCellRotation(size_t nCol,size_t nRow,SvxRotateMode eRotMode,double fOrientation)538 void Array::SetCellRotation(size_t nCol, size_t nRow, SvxRotateMode eRotMode, double fOrientation)
539 {
540     DBG_FRAME_CHECK_COLROW(nCol, nRow, "SetCellRotation");
541     Cell& rTarget = CELLACC(nCol, nRow);
542     rTarget.meRotMode = eRotMode;
543     rTarget.mfOrientation = fOrientation;
544 
545     if (!mxImpl->mbMayHaveCellRotation)
546     {
547         // activate once when a cell gets actually rotated to allow fast
548         // answering HasCellRotation() calls
549         mxImpl->mbMayHaveCellRotation = rTarget.IsRotated();
550     }
551 }
552 
HasCellRotation() const553 bool Array::HasCellRotation() const
554 {
555     if (!mxImpl->mbMayHaveCellRotation)
556     {
557         // never set, no need to check
558         return false;
559     }
560 
561     return mxImpl->HasCellRotation();
562 }
563 
GetCellStyleLeft(size_t nCol,size_t nRow) const564 const Style& Array::GetCellStyleLeft( size_t nCol, size_t nRow ) const
565 {
566     // outside clipping rows or overlapped in merged cells: invisible
567     if( !mxImpl->IsRowInClipRange( nRow ) || mxImpl->IsMergedOverlappedLeft( nCol, nRow ) )
568         return OBJ_STYLE_NONE;
569     // left clipping border: always own left style
570     if( nCol == mxImpl->mnFirstClipCol )
571         return ORIGCELL( nCol, nRow ).GetStyleLeft();
572     // right clipping border: always right style of left neighbor cell
573     if( nCol == mxImpl->mnLastClipCol + 1 )
574         return ORIGCELL( nCol - 1, nRow ).GetStyleRight();
575     // outside clipping columns: invisible
576     if( !mxImpl->IsColInClipRange( nCol ) )
577         return OBJ_STYLE_NONE;
578     // inside clipping range: maximum of own left style and right style of left neighbor cell
579     return std::max( ORIGCELL( nCol, nRow ).GetStyleLeft(), ORIGCELL( nCol - 1, nRow ).GetStyleRight() );
580 }
581 
GetCellStyleRight(size_t nCol,size_t nRow) const582 const Style& Array::GetCellStyleRight( size_t nCol, size_t nRow ) const
583 {
584     // outside clipping rows or overlapped in merged cells: invisible
585     if( !mxImpl->IsRowInClipRange( nRow ) || mxImpl->IsMergedOverlappedRight( nCol, nRow ) )
586         return OBJ_STYLE_NONE;
587     // left clipping border: always left style of right neighbor cell
588     if( nCol + 1 == mxImpl->mnFirstClipCol )
589         return ORIGCELL( nCol + 1, nRow ).GetStyleLeft();
590     // right clipping border: always own right style
591     if( nCol == mxImpl->mnLastClipCol )
592         return ORIGCELL( nCol, nRow ).GetStyleRight();
593     // outside clipping columns: invisible
594     if( !mxImpl->IsColInClipRange( nCol ) )
595         return OBJ_STYLE_NONE;
596     // inside clipping range: maximum of own right style and left style of right neighbor cell
597     return std::max( ORIGCELL( nCol, nRow ).GetStyleRight(), ORIGCELL( nCol + 1, nRow ).GetStyleLeft() );
598 }
599 
GetCellStyleTop(size_t nCol,size_t nRow) const600 const Style& Array::GetCellStyleTop( size_t nCol, size_t nRow ) const
601 {
602     // outside clipping columns or overlapped in merged cells: invisible
603     if( !mxImpl->IsColInClipRange( nCol ) || mxImpl->IsMergedOverlappedTop( nCol, nRow ) )
604         return OBJ_STYLE_NONE;
605     // top clipping border: always own top style
606     if( nRow == mxImpl->mnFirstClipRow )
607         return ORIGCELL( nCol, nRow ).GetStyleTop();
608     // bottom clipping border: always bottom style of top neighbor cell
609     if( nRow == mxImpl->mnLastClipRow + 1 )
610         return ORIGCELL( nCol, nRow - 1 ).GetStyleBottom();
611     // outside clipping rows: invisible
612     if( !mxImpl->IsRowInClipRange( nRow ) )
613         return OBJ_STYLE_NONE;
614     // inside clipping range: maximum of own top style and bottom style of top neighbor cell
615     return std::max( ORIGCELL( nCol, nRow ).GetStyleTop(), ORIGCELL( nCol, nRow - 1 ).GetStyleBottom() );
616 }
617 
GetCellStyleBottom(size_t nCol,size_t nRow) const618 const Style& Array::GetCellStyleBottom( size_t nCol, size_t nRow ) const
619 {
620     // outside clipping columns or overlapped in merged cells: invisible
621     if( !mxImpl->IsColInClipRange( nCol ) || mxImpl->IsMergedOverlappedBottom( nCol, nRow ) )
622         return OBJ_STYLE_NONE;
623     // top clipping border: always top style of bottom neighbor cell
624     if( nRow + 1 == mxImpl->mnFirstClipRow )
625         return ORIGCELL( nCol, nRow + 1 ).GetStyleTop();
626     // bottom clipping border: always own bottom style
627     if( nRow == mxImpl->mnLastClipRow )
628         return ORIGCELL( nCol, nRow ).GetStyleBottom();
629     // outside clipping rows: invisible
630     if( !mxImpl->IsRowInClipRange( nRow ) )
631         return OBJ_STYLE_NONE;
632     // inside clipping range: maximum of own bottom style and top style of bottom neighbor cell
633     return std::max( ORIGCELL( nCol, nRow ).GetStyleBottom(), ORIGCELL( nCol, nRow + 1 ).GetStyleTop() );
634 }
635 
GetCellStyleTLBR(size_t nCol,size_t nRow) const636 const Style& Array::GetCellStyleTLBR( size_t nCol, size_t nRow ) const
637 {
638     return CELL( nCol, nRow ).GetStyleTLBR();
639 }
640 
GetCellStyleBLTR(size_t nCol,size_t nRow) const641 const Style& Array::GetCellStyleBLTR( size_t nCol, size_t nRow ) const
642 {
643     return CELL( nCol, nRow ).GetStyleBLTR();
644 }
645 
GetCellStyleTL(size_t nCol,size_t nRow) const646 const Style& Array::GetCellStyleTL( size_t nCol, size_t nRow ) const
647 {
648     // not in clipping range: always invisible
649     if( !mxImpl->IsInClipRange( nCol, nRow ) )
650         return OBJ_STYLE_NONE;
651     // return style only for top-left cell
652     size_t nFirstCol = mxImpl->GetMergedFirstCol( nCol, nRow );
653     size_t nFirstRow = mxImpl->GetMergedFirstRow( nCol, nRow );
654     return ((nCol == nFirstCol) && (nRow == nFirstRow)) ?
655         CELL( nFirstCol, nFirstRow ).GetStyleTLBR() : OBJ_STYLE_NONE;
656 }
657 
GetCellStyleBR(size_t nCol,size_t nRow) const658 const Style& Array::GetCellStyleBR( size_t nCol, size_t nRow ) const
659 {
660     // not in clipping range: always invisible
661     if( !mxImpl->IsInClipRange( nCol, nRow ) )
662         return OBJ_STYLE_NONE;
663     // return style only for bottom-right cell
664     size_t nLastCol = mxImpl->GetMergedLastCol( nCol, nRow );
665     size_t nLastRow = mxImpl->GetMergedLastRow( nCol, nRow );
666     return ((nCol == nLastCol) && (nRow == nLastRow)) ?
667         CELL( mxImpl->GetMergedFirstCol( nCol, nRow ), mxImpl->GetMergedFirstRow( nCol, nRow ) ).GetStyleTLBR() : OBJ_STYLE_NONE;
668 }
669 
GetCellStyleBL(size_t nCol,size_t nRow) const670 const Style& Array::GetCellStyleBL( size_t nCol, size_t nRow ) const
671 {
672     // not in clipping range: always invisible
673     if( !mxImpl->IsInClipRange( nCol, nRow ) )
674         return OBJ_STYLE_NONE;
675     // return style only for bottom-left cell
676     size_t nFirstCol = mxImpl->GetMergedFirstCol( nCol, nRow );
677     size_t nLastRow = mxImpl->GetMergedLastRow( nCol, nRow );
678     return ((nCol == nFirstCol) && (nRow == nLastRow)) ?
679         CELL( nFirstCol, mxImpl->GetMergedFirstRow( nCol, nRow ) ).GetStyleBLTR() : OBJ_STYLE_NONE;
680 }
681 
GetCellStyleTR(size_t nCol,size_t nRow) const682 const Style& Array::GetCellStyleTR( size_t nCol, size_t nRow ) const
683 {
684     // not in clipping range: always invisible
685     if( !mxImpl->IsInClipRange( nCol, nRow ) )
686         return OBJ_STYLE_NONE;
687     // return style only for top-right cell
688     size_t nFirstRow = mxImpl->GetMergedFirstRow( nCol, nRow );
689     size_t nLastCol = mxImpl->GetMergedLastCol( nCol, nRow );
690     return ((nCol == nLastCol) && (nRow == nFirstRow)) ?
691         CELL( mxImpl->GetMergedFirstCol( nCol, nRow ), nFirstRow ).GetStyleBLTR() : OBJ_STYLE_NONE;
692 }
693 
694 // cell merging
SetMergedRange(size_t nFirstCol,size_t nFirstRow,size_t nLastCol,size_t nLastRow)695 void Array::SetMergedRange( size_t nFirstCol, size_t nFirstRow, size_t nLastCol, size_t nLastRow )
696 {
697     DBG_FRAME_CHECK_COLROW( nFirstCol, nFirstRow, "SetMergedRange" );
698     DBG_FRAME_CHECK_COLROW( nLastCol, nLastRow, "SetMergedRange" );
699 #if OSL_DEBUG_LEVEL >= 2
700     {
701         bool bFound = false;
702         for( size_t nCurrCol = nFirstCol; !bFound && (nCurrCol <= nLastCol); ++nCurrCol )
703             for( size_t nCurrRow = nFirstRow; !bFound && (nCurrRow <= nLastRow); ++nCurrRow )
704                 bFound = CELL( nCurrCol, nCurrRow ).IsMerged();
705         DBG_FRAME_CHECK( !bFound, "SetMergedRange", "overlapping merged ranges" );
706     }
707 #endif
708     if( mxImpl->IsValidPos( nFirstCol, nFirstRow ) && mxImpl->IsValidPos( nLastCol, nLastRow ) )
709         lclSetMergedRange( mxImpl->maCells, mxImpl->mnWidth, nFirstCol, nFirstRow, nLastCol, nLastRow );
710 }
711 
SetAddMergedLeftSize(size_t nCol,size_t nRow,tools::Long nAddSize)712 void Array::SetAddMergedLeftSize( size_t nCol, size_t nRow, tools::Long nAddSize )
713 {
714     DBG_FRAME_CHECK_COLROW( nCol, nRow, "SetAddMergedLeftSize" );
715     DBG_FRAME_CHECK( mxImpl->GetMergedFirstCol( nCol, nRow ) == 0, "SetAddMergedLeftSize", "additional border inside array" );
716     for( MergedCellIterator aIt( *this, nCol, nRow ); aIt.Is(); ++aIt )
717         CELLACC( aIt.Col(), aIt.Row() ).mnAddLeft = nAddSize;
718 }
719 
SetAddMergedRightSize(size_t nCol,size_t nRow,tools::Long nAddSize)720 void Array::SetAddMergedRightSize( size_t nCol, size_t nRow, tools::Long nAddSize )
721 {
722     DBG_FRAME_CHECK_COLROW( nCol, nRow, "SetAddMergedRightSize" );
723     DBG_FRAME_CHECK( mxImpl->GetMergedLastCol( nCol, nRow ) + 1 == mxImpl->mnWidth, "SetAddMergedRightSize", "additional border inside array" );
724     for( MergedCellIterator aIt( *this, nCol, nRow ); aIt.Is(); ++aIt )
725         CELLACC( aIt.Col(), aIt.Row() ).mnAddRight = nAddSize;
726 }
727 
SetAddMergedTopSize(size_t nCol,size_t nRow,tools::Long nAddSize)728 void Array::SetAddMergedTopSize( size_t nCol, size_t nRow, tools::Long nAddSize )
729 {
730     DBG_FRAME_CHECK_COLROW( nCol, nRow, "SetAddMergedTopSize" );
731     DBG_FRAME_CHECK( mxImpl->GetMergedFirstRow( nCol, nRow ) == 0, "SetAddMergedTopSize", "additional border inside array" );
732     for( MergedCellIterator aIt( *this, nCol, nRow ); aIt.Is(); ++aIt )
733         CELLACC( aIt.Col(), aIt.Row() ).mnAddTop = nAddSize;
734 }
735 
SetAddMergedBottomSize(size_t nCol,size_t nRow,tools::Long nAddSize)736 void Array::SetAddMergedBottomSize( size_t nCol, size_t nRow, tools::Long nAddSize )
737 {
738     DBG_FRAME_CHECK_COLROW( nCol, nRow, "SetAddMergedBottomSize" );
739     DBG_FRAME_CHECK( mxImpl->GetMergedLastRow( nCol, nRow ) + 1 == mxImpl->mnHeight, "SetAddMergedBottomSize", "additional border inside array" );
740     for( MergedCellIterator aIt( *this, nCol, nRow ); aIt.Is(); ++aIt )
741         CELLACC( aIt.Col(), aIt.Row() ).mnAddBottom = nAddSize;
742 }
743 
IsMerged(size_t nCol,size_t nRow) const744 bool Array::IsMerged( size_t nCol, size_t nRow ) const
745 {
746     DBG_FRAME_CHECK_COLROW( nCol, nRow, "IsMerged" );
747     return CELL( nCol, nRow ).IsMerged();
748 }
749 
GetMergedOrigin(size_t & rnFirstCol,size_t & rnFirstRow,size_t nCol,size_t nRow) const750 void Array::GetMergedOrigin( size_t& rnFirstCol, size_t& rnFirstRow, size_t nCol, size_t nRow ) const
751 {
752     DBG_FRAME_CHECK_COLROW( nCol, nRow, "GetMergedOrigin" );
753     rnFirstCol = mxImpl->GetMergedFirstCol( nCol, nRow );
754     rnFirstRow = mxImpl->GetMergedFirstRow( nCol, nRow );
755 }
756 
GetMergedRange(size_t & rnFirstCol,size_t & rnFirstRow,size_t & rnLastCol,size_t & rnLastRow,size_t nCol,size_t nRow) const757 void Array::GetMergedRange( size_t& rnFirstCol, size_t& rnFirstRow,
758         size_t& rnLastCol, size_t& rnLastRow, size_t nCol, size_t nRow ) const
759 {
760     GetMergedOrigin( rnFirstCol, rnFirstRow, nCol, nRow );
761     rnLastCol = mxImpl->GetMergedLastCol( nCol, nRow );
762     rnLastRow = mxImpl->GetMergedLastRow( nCol, nRow );
763 }
764 
765 // clipping
SetClipRange(size_t nFirstCol,size_t nFirstRow,size_t nLastCol,size_t nLastRow)766 void Array::SetClipRange( size_t nFirstCol, size_t nFirstRow, size_t nLastCol, size_t nLastRow )
767 {
768     DBG_FRAME_CHECK_COLROW( nFirstCol, nFirstRow, "SetClipRange" );
769     DBG_FRAME_CHECK_COLROW( nLastCol, nLastRow, "SetClipRange" );
770     mxImpl->mnFirstClipCol = nFirstCol;
771     mxImpl->mnFirstClipRow = nFirstRow;
772     mxImpl->mnLastClipCol = nLastCol;
773     mxImpl->mnLastClipRow = nLastRow;
774 }
775 
776 // cell coordinates
SetXOffset(tools::Long nXOffset)777 void Array::SetXOffset( tools::Long nXOffset )
778 {
779     mxImpl->maXCoords[ 0 ] = nXOffset;
780     mxImpl->mbXCoordsDirty = true;
781 }
782 
SetYOffset(tools::Long nYOffset)783 void Array::SetYOffset( tools::Long nYOffset )
784 {
785     mxImpl->maYCoords[ 0 ] = nYOffset;
786     mxImpl->mbYCoordsDirty = true;
787 }
788 
SetColWidth(size_t nCol,tools::Long nWidth)789 void Array::SetColWidth( size_t nCol, tools::Long nWidth )
790 {
791     DBG_FRAME_CHECK_COL( nCol, "SetColWidth" );
792     mxImpl->maWidths[ nCol ] = nWidth;
793     mxImpl->mbXCoordsDirty = true;
794 }
795 
SetRowHeight(size_t nRow,tools::Long nHeight)796 void Array::SetRowHeight( size_t nRow, tools::Long nHeight )
797 {
798     DBG_FRAME_CHECK_ROW( nRow, "SetRowHeight" );
799     mxImpl->maHeights[ nRow ] = nHeight;
800     mxImpl->mbYCoordsDirty = true;
801 }
802 
SetAllColWidths(tools::Long nWidth)803 void Array::SetAllColWidths( tools::Long nWidth )
804 {
805     std::fill( mxImpl->maWidths.begin(), mxImpl->maWidths.end(), nWidth );
806     mxImpl->mbXCoordsDirty = true;
807 }
808 
SetAllRowHeights(tools::Long nHeight)809 void Array::SetAllRowHeights( tools::Long nHeight )
810 {
811     std::fill( mxImpl->maHeights.begin(), mxImpl->maHeights.end(), nHeight );
812     mxImpl->mbYCoordsDirty = true;
813 }
814 
GetColPosition(size_t nCol) const815 tools::Long Array::GetColPosition( size_t nCol ) const
816 {
817     DBG_FRAME_CHECK_COL_1( nCol, "GetColPosition" );
818     return mxImpl->GetColPosition( nCol );
819 }
820 
GetRowPosition(size_t nRow) const821 tools::Long Array::GetRowPosition( size_t nRow ) const
822 {
823     DBG_FRAME_CHECK_ROW_1( nRow, "GetRowPosition" );
824     return mxImpl->GetRowPosition( nRow );
825 }
826 
GetColWidth(size_t nFirstCol,size_t nLastCol) const827 tools::Long Array::GetColWidth( size_t nFirstCol, size_t nLastCol ) const
828 {
829     DBG_FRAME_CHECK_COL( nFirstCol, "GetColWidth" );
830     DBG_FRAME_CHECK_COL( nLastCol, "GetColWidth" );
831     return GetColPosition( nLastCol + 1 ) - GetColPosition( nFirstCol );
832 }
833 
GetRowHeight(size_t nFirstRow,size_t nLastRow) const834 tools::Long Array::GetRowHeight( size_t nFirstRow, size_t nLastRow ) const
835 {
836     DBG_FRAME_CHECK_ROW( nFirstRow, "GetRowHeight" );
837     DBG_FRAME_CHECK_ROW( nLastRow, "GetRowHeight" );
838     return GetRowPosition( nLastRow + 1 ) - GetRowPosition( nFirstRow );
839 }
840 
GetWidth() const841 tools::Long Array::GetWidth() const
842 {
843     return GetColPosition( mxImpl->mnWidth ) - GetColPosition( 0 );
844 }
845 
GetHeight() const846 tools::Long Array::GetHeight() const
847 {
848     return GetRowPosition( mxImpl->mnHeight ) - GetRowPosition( 0 );
849 }
850 
GetCellRange(size_t nCol,size_t nRow,bool bExpandMerged) const851 basegfx::B2DRange Array::GetCellRange( size_t nCol, size_t nRow, bool bExpandMerged ) const
852 {
853     if(bExpandMerged)
854     {
855         // get the Range of the fully expanded cell (if merged)
856         const size_t nFirstCol(mxImpl->GetMergedFirstCol( nCol, nRow ));
857         const size_t nFirstRow(mxImpl->GetMergedFirstRow( nCol, nRow ));
858         const size_t nLastCol(mxImpl->GetMergedLastCol( nCol, nRow ));
859         const size_t nLastRow(mxImpl->GetMergedLastRow( nCol, nRow ));
860         const Point aPoint( GetColPosition( nFirstCol ), GetRowPosition( nFirstRow ) );
861         const Size aSize( GetColWidth( nFirstCol, nLastCol ) + 1, GetRowHeight( nFirstRow, nLastRow ) + 1 );
862         tools::Rectangle aRect(aPoint, aSize);
863 
864         // adjust rectangle for partly visible merged cells
865         const Cell& rCell = CELL( nCol, nRow );
866 
867         if( rCell.IsMerged() )
868         {
869             // not *sure* what exactly this is good for,
870             // it is just a hard set extension at merged cells,
871             // probably *should* be included in the above extended
872             // GetColPosition/GetColWidth already. This might be
873             // added due to GetColPosition/GetColWidth not working
874             // correctly over PageChanges (if used), but not sure.
875             aRect.AdjustLeft( -(rCell.mnAddLeft) );
876             aRect.AdjustRight(rCell.mnAddRight );
877             aRect.AdjustTop( -(rCell.mnAddTop) );
878             aRect.AdjustBottom(rCell.mnAddBottom );
879         }
880 
881         return vcl::unotools::b2DRectangleFromRectangle(aRect);
882     }
883     else
884     {
885         const Point aPoint( GetColPosition( nCol ), GetRowPosition( nRow ) );
886         const Size aSize( GetColWidth( nCol, nCol ) + 1, GetRowHeight( nRow, nRow ) + 1 );
887         const tools::Rectangle aRect(aPoint, aSize);
888 
889         return vcl::unotools::b2DRectangleFromRectangle(aRect);
890     }
891 }
892 
893 // mirroring
MirrorSelfX()894 void Array::MirrorSelfX()
895 {
896     CellVec aNewCells;
897     aNewCells.reserve( GetCellCount() );
898 
899     size_t nCol, nRow;
900     for( nRow = 0; nRow < mxImpl->mnHeight; ++nRow )
901     {
902         for( nCol = 0; nCol < mxImpl->mnWidth; ++nCol )
903         {
904             aNewCells.push_back( CELL( mxImpl->GetMirrorCol( nCol ), nRow ) );
905             aNewCells.back().MirrorSelfX();
906         }
907     }
908     for( nRow = 0; nRow < mxImpl->mnHeight; ++nRow )
909     {
910         for( nCol = 0; nCol < mxImpl->mnWidth; ++nCol )
911         {
912             if( CELL( nCol, nRow ).mbMergeOrig )
913             {
914                 size_t nLastCol = mxImpl->GetMergedLastCol( nCol, nRow );
915                 size_t nLastRow = mxImpl->GetMergedLastRow( nCol, nRow );
916                 lclSetMergedRange( aNewCells, mxImpl->mnWidth,
917                     mxImpl->GetMirrorCol( nLastCol ), nRow,
918                     mxImpl->GetMirrorCol( nCol ), nLastRow );
919             }
920         }
921     }
922     mxImpl->maCells.swap( aNewCells );
923 
924     std::reverse( mxImpl->maWidths.begin(), mxImpl->maWidths.end() );
925     mxImpl->mbXCoordsDirty = true;
926 }
927 
928 // drawing
HelperCreateHorizontalEntry(const Array & rArray,const Style & rStyle,size_t col,size_t row,const basegfx::B2DPoint & rOrigin,const basegfx::B2DVector & rX,const basegfx::B2DVector & rY,drawinglayer::primitive2d::SdrFrameBorderDataVector & rData,bool bUpper,const Color * pForceColor)929 static void HelperCreateHorizontalEntry(
930     const Array& rArray,
931     const Style& rStyle,
932     size_t col,
933     size_t row,
934     const basegfx::B2DPoint& rOrigin,
935     const basegfx::B2DVector& rX,
936     const basegfx::B2DVector& rY,
937     drawinglayer::primitive2d::SdrFrameBorderDataVector& rData,
938     bool bUpper,
939     const Color* pForceColor)
940 {
941     // prepare SdrFrameBorderData
942     rData.emplace_back(
943         bUpper ? rOrigin : basegfx::B2DPoint(rOrigin + rY),
944         rX,
945         rStyle,
946         pForceColor);
947     drawinglayer::primitive2d::SdrFrameBorderData& rInstance(rData.back());
948 
949     // get involved styles at start
950     const Style& rStartFromTR(rArray.GetCellStyleBL( col, row - 1 ));
951     const Style& rStartLFromT(rArray.GetCellStyleLeft( col, row - 1 ));
952     const Style& rStartLFromL(rArray.GetCellStyleTop( col - 1, row ));
953     const Style& rStartLFromB(rArray.GetCellStyleLeft( col, row ));
954     const Style& rStartFromBR(rArray.GetCellStyleTL( col, row ));
955 
956     rInstance.addSdrConnectStyleData(true, rStartFromTR, rX - rY, false);
957     rInstance.addSdrConnectStyleData(true, rStartLFromT, -rY, true);
958     rInstance.addSdrConnectStyleData(true, rStartLFromL, -rX, true);
959     rInstance.addSdrConnectStyleData(true, rStartLFromB, rY, false);
960     rInstance.addSdrConnectStyleData(true, rStartFromBR, rX + rY, false);
961 
962     // get involved styles at end
963     const Style& rEndFromTL(rArray.GetCellStyleBR( col, row - 1 ));
964     const Style& rEndRFromT(rArray.GetCellStyleRight( col, row - 1 ));
965     const Style& rEndRFromR(rArray.GetCellStyleTop( col + 1, row ));
966     const Style& rEndRFromB(rArray.GetCellStyleRight( col, row ));
967     const Style& rEndFromBL(rArray.GetCellStyleTR( col, row ));
968 
969     rInstance.addSdrConnectStyleData(false, rEndFromTL, -rX - rY, true);
970     rInstance.addSdrConnectStyleData(false, rEndRFromT, -rY, true);
971     rInstance.addSdrConnectStyleData(false, rEndRFromR, rX, false);
972     rInstance.addSdrConnectStyleData(false, rEndRFromB, rY, false);
973     rInstance.addSdrConnectStyleData(false, rEndFromBL, rY - rX, true);
974 }
975 
HelperCreateVerticalEntry(const Array & rArray,const Style & rStyle,size_t col,size_t row,const basegfx::B2DPoint & rOrigin,const basegfx::B2DVector & rX,const basegfx::B2DVector & rY,drawinglayer::primitive2d::SdrFrameBorderDataVector & rData,bool bLeft,const Color * pForceColor)976 static void HelperCreateVerticalEntry(
977     const Array& rArray,
978     const Style& rStyle,
979     size_t col,
980     size_t row,
981     const basegfx::B2DPoint& rOrigin,
982     const basegfx::B2DVector& rX,
983     const basegfx::B2DVector& rY,
984     drawinglayer::primitive2d::SdrFrameBorderDataVector& rData,
985     bool bLeft,
986     const Color* pForceColor)
987 {
988     // prepare SdrFrameBorderData
989     rData.emplace_back(
990         bLeft ? rOrigin : basegfx::B2DPoint(rOrigin + rX),
991         rY,
992         rStyle,
993         pForceColor);
994     drawinglayer::primitive2d::SdrFrameBorderData& rInstance(rData.back());
995 
996     // get involved styles at start
997     const Style& rStartFromBL(rArray.GetCellStyleTR( col - 1, row ));
998     const Style& rStartTFromL(rArray.GetCellStyleTop( col - 1, row ));
999     const Style& rStartTFromT(rArray.GetCellStyleLeft( col, row - 1 ));
1000     const Style& rStartTFromR(rArray.GetCellStyleTop( col, row ));
1001     const Style& rStartFromBR(rArray.GetCellStyleTL( col, row ));
1002 
1003     rInstance.addSdrConnectStyleData(true, rStartFromBR, rX + rY, false);
1004     rInstance.addSdrConnectStyleData(true, rStartTFromR, rX, false);
1005     rInstance.addSdrConnectStyleData(true, rStartTFromT, -rY, true);
1006     rInstance.addSdrConnectStyleData(true, rStartTFromL, -rX, true);
1007     rInstance.addSdrConnectStyleData(true, rStartFromBL, rY - rX, true);
1008 
1009     // get involved styles at end
1010     const Style& rEndFromTL(rArray.GetCellStyleBR( col - 1, row ));
1011     const Style& rEndBFromL(rArray.GetCellStyleBottom( col - 1, row ));
1012     const Style& rEndBFromB(rArray.GetCellStyleLeft( col, row + 1 ));
1013     const Style& rEndBFromR(rArray.GetCellStyleBottom( col, row ));
1014     const Style& rEndFromTR(rArray.GetCellStyleBL( col, row ));
1015 
1016     rInstance.addSdrConnectStyleData(false, rEndFromTR, rX - rY, false);
1017     rInstance.addSdrConnectStyleData(false, rEndBFromR, rX, false);
1018     rInstance.addSdrConnectStyleData(false, rEndBFromB, rY, false);
1019     rInstance.addSdrConnectStyleData(false, rEndBFromL, -rX, true);
1020     rInstance.addSdrConnectStyleData(false, rEndFromTL, -rY - rX, true);
1021 }
1022 
CreateB2DPrimitiveRange(size_t nFirstCol,size_t nFirstRow,size_t nLastCol,size_t nLastRow,const Color * pForceColor) const1023 drawinglayer::primitive2d::Primitive2DContainer Array::CreateB2DPrimitiveRange(
1024     size_t nFirstCol, size_t nFirstRow, size_t nLastCol, size_t nLastRow,
1025     const Color* pForceColor ) const
1026 {
1027     DBG_FRAME_CHECK_COLROW( nFirstCol, nFirstRow, "CreateB2DPrimitiveRange" );
1028     DBG_FRAME_CHECK_COLROW( nLastCol, nLastRow, "CreateB2DPrimitiveRange" );
1029 
1030     // It may be necessary to extend the loop ranges by one cell to the outside,
1031     // when possible. This is needed e.g. when there is in Calc a Cell with an
1032     // upper CellBorder using DoubleLine and that is right/left connected upwards
1033     // to also DoubleLine. These upper DoubleLines will be extended to meet the
1034     // lower of the upper CellBorder and thus have graphical parts that are
1035     // displayed one cell below and right/left of the target cell - analog to
1036     // other examples in all other directions.
1037     // It would be possible to explicitly test this (if possible by indices at all)
1038     // looping and testing the styles in the outer cells to detect this, but since
1039     // for other usages (e.g. UI) usually nFirstRow==0 and nLastRow==GetRowCount()-1
1040     // (and analog for Col) it is okay to just expand the range when available.
1041     // Do *not* change nFirstRow/nLastRow due to these needed to the boolean tests
1042     // below (!)
1043     // Checked usages, this method is used in Calc EditView/Print/Export stuff and
1044     // in UI (Dialog), not for Writer Tables and Draw/Impress tables. All usages
1045     // seem okay with this change, so I will add it.
1046     const size_t nStartRow(nFirstRow > 0 ? nFirstRow - 1 : nFirstRow);
1047     const size_t nEndRow(nLastRow < GetRowCount() - 1 ? nLastRow + 1 : nLastRow);
1048     const size_t nStartCol(nFirstCol > 0 ? nFirstCol - 1 : nFirstCol);
1049     const size_t nEndCol(nLastCol < GetColCount() - 1 ? nLastCol + 1 : nLastCol);
1050 
1051     // prepare SdrFrameBorderDataVector
1052     std::shared_ptr<drawinglayer::primitive2d::SdrFrameBorderDataVector> aData(
1053         std::make_shared<drawinglayer::primitive2d::SdrFrameBorderDataVector>());
1054 
1055     // remember for which merged cells crossed lines were already created. To
1056     // do so, hold the size_t cell index in a set for fast check
1057     std::set< size_t > aMergedCells;
1058 
1059     for (size_t nRow(nStartRow); nRow <= nEndRow; ++nRow)
1060     {
1061         for (size_t nCol(nStartCol); nCol <= nEndCol; ++nCol)
1062         {
1063             // get Cell and CoordinateSystem (*only* for this Cell, do *not* expand for
1064             // merged cells (!)), check if used (non-empty vectors)
1065             const Cell& rCell(CELL(nCol, nRow));
1066             basegfx::B2DHomMatrix aCoordinateSystem(rCell.CreateCoordinateSystem(*this, nCol, nRow, false));
1067             basegfx::B2DVector aX(basegfx::utils::getColumn(aCoordinateSystem, 0));
1068             basegfx::B2DVector aY(basegfx::utils::getColumn(aCoordinateSystem, 1));
1069 
1070             // get needed local values
1071             basegfx::B2DPoint aOrigin(basegfx::utils::getColumn(aCoordinateSystem, 2));
1072             const bool bOverlapX(rCell.mbOverlapX);
1073             const bool bFirstCol(nCol == nFirstCol);
1074 
1075             // handle rotation: If cell is rotated, handle lower/right edge inside
1076             // this local geometry due to the created CoordinateSystem already representing
1077             // the needed transformations.
1078             const bool bRotated(rCell.IsRotated());
1079 
1080             // Additionally avoid double-handling by suppressing handling when self not rotated,
1081             // but above/left is rotated and thus already handled. Two directly connected
1082             // rotated will paint/create both edges, they might be rotated differently.
1083             const bool bSuppressLeft(!bRotated && nCol > nFirstCol && CELL(nCol - 1, nRow).IsRotated());
1084             const bool bSuppressAbove(!bRotated && nRow > nFirstRow && CELL(nCol, nRow - 1).IsRotated());
1085 
1086             if(!aX.equalZero() && !aY.equalZero())
1087             {
1088                 // additionally needed local values
1089                 const bool bOverlapY(rCell.mbOverlapY);
1090                 const bool bLastCol(nCol == nLastCol);
1091                 const bool bFirstRow(nRow == nFirstRow);
1092                 const bool bLastRow(nRow == nLastRow);
1093 
1094                 // create upper line for this Cell
1095                 if ((!bOverlapY         // true for first line in merged cells or cells
1096                     || bFirstRow)       // true for non_Calc usages of this tooling
1097                     && !bSuppressAbove) // true when above is not rotated, so edge is already handled (see bRotated)
1098                 {
1099                     // get CellStyle - method will take care to get the correct one, e.g.
1100                     // for merged cells (it uses ORIGCELL that works with topLeft's of these)
1101                     const Style& rTop(GetCellStyleTop(nCol, nRow));
1102 
1103                     if(rTop.IsUsed())
1104                     {
1105                         HelperCreateHorizontalEntry(*this, rTop, nCol, nRow, aOrigin, aX, aY, *aData, true, pForceColor);
1106                     }
1107                 }
1108 
1109                 // create lower line for this Cell
1110                 if (bLastRow       // true for non_Calc usages of this tooling
1111                     || bRotated)   // true if cell is rotated, handle lower edge in local geometry
1112                 {
1113                     const Style& rBottom(GetCellStyleBottom(nCol, nRow));
1114 
1115                     if(rBottom.IsUsed())
1116                     {
1117                         HelperCreateHorizontalEntry(*this, rBottom, nCol, nRow + 1, aOrigin, aX, aY, *aData, false, pForceColor);
1118                     }
1119                 }
1120 
1121                 // create left line for this Cell
1122                 if ((!bOverlapX         // true for first column in merged cells or cells
1123                     || bFirstCol)       // true for non_Calc usages of this tooling
1124                     && !bSuppressLeft)  // true when left is not rotated, so edge is already handled (see bRotated)
1125                 {
1126                     const Style& rLeft(GetCellStyleLeft(nCol, nRow));
1127 
1128                     if(rLeft.IsUsed())
1129                     {
1130                         HelperCreateVerticalEntry(*this, rLeft, nCol, nRow, aOrigin, aX, aY, *aData, true, pForceColor);
1131                     }
1132                 }
1133 
1134                 // create right line for this Cell
1135                 if (bLastCol        // true for non_Calc usages of this tooling
1136                     || bRotated)    // true if cell is rotated, handle right edge in local geometry
1137                 {
1138                     const Style& rRight(GetCellStyleRight(nCol, nRow));
1139 
1140                     if(rRight.IsUsed())
1141                     {
1142                         HelperCreateVerticalEntry(*this, rRight, nCol + 1, nRow, aOrigin, aX, aY, *aData, false, pForceColor);
1143                     }
1144                 }
1145 
1146                 // check for crossed lines, these need special treatment, especially
1147                 // for merged cells, see below
1148                 const Style& rTLBR(GetCellStyleTLBR(nCol, nRow));
1149                 const Style& rBLTR(GetCellStyleBLTR(nCol, nRow));
1150 
1151                 if(rTLBR.IsUsed() || rBLTR.IsUsed())
1152                 {
1153                     bool bContinue(true);
1154 
1155                     if(rCell.IsMerged())
1156                     {
1157                         // first check if this merged cell was already handled. To do so,
1158                         // calculate and use the index of the TopLeft cell
1159                         const size_t _nMergedFirstCol(mxImpl->GetMergedFirstCol(nCol, nRow));
1160                         const size_t _nMergedFirstRow(mxImpl->GetMergedFirstRow(nCol, nRow));
1161                         const size_t nIndexOfMergedCell(mxImpl->GetIndex(_nMergedFirstCol, _nMergedFirstRow));
1162                         bContinue = (aMergedCells.end() == aMergedCells.find(nIndexOfMergedCell));
1163 
1164                         if(bContinue)
1165                         {
1166                             // not found, add now to mark as handled
1167                             aMergedCells.insert(nIndexOfMergedCell);
1168 
1169                             // when merged, get extended coordinate system and derived values
1170                             // for the full range of this merged cell
1171                             aCoordinateSystem = rCell.CreateCoordinateSystem(*this, nCol, nRow, true);
1172                             aX = basegfx::utils::getColumn(aCoordinateSystem, 0);
1173                             aY = basegfx::utils::getColumn(aCoordinateSystem, 1);
1174                             aOrigin = basegfx::utils::getColumn(aCoordinateSystem, 2);
1175                         }
1176                     }
1177 
1178                     if(bContinue)
1179                     {
1180                         if(rTLBR.IsUsed())
1181                         {
1182                             /// top-left and bottom-right Style Tables
1183                             aData->emplace_back(
1184                                 aOrigin,
1185                                 aX + aY,
1186                                 rTLBR,
1187                                 pForceColor);
1188                             drawinglayer::primitive2d::SdrFrameBorderData& rInstance(aData->back());
1189 
1190                             /// Fill top-left Style Table
1191                             const Style& rTLFromRight(GetCellStyleTop(nCol, nRow));
1192                             const Style& rTLFromBottom(GetCellStyleLeft(nCol, nRow));
1193 
1194                             rInstance.addSdrConnectStyleData(true, rTLFromRight, aX, false);
1195                             rInstance.addSdrConnectStyleData(true, rTLFromBottom, aY, false);
1196 
1197                             /// Fill bottom-right Style Table
1198                             const Style& rBRFromBottom(GetCellStyleRight(nCol, nRow));
1199                             const Style& rBRFromLeft(GetCellStyleBottom(nCol, nRow));
1200 
1201                             rInstance.addSdrConnectStyleData(false, rBRFromBottom, -aY, true);
1202                             rInstance.addSdrConnectStyleData(false, rBRFromLeft, -aX, true);
1203                         }
1204 
1205                         if(rBLTR.IsUsed())
1206                         {
1207                             /// bottom-left and top-right Style Tables
1208                             aData->emplace_back(
1209                                 aOrigin + aY,
1210                                 aX - aY,
1211                                 rBLTR,
1212                                 pForceColor);
1213                             drawinglayer::primitive2d::SdrFrameBorderData& rInstance(aData->back());
1214 
1215                             /// Fill bottom-left Style Table
1216                             const Style& rBLFromTop(GetCellStyleLeft(nCol, nRow));
1217                             const Style& rBLFromBottom(GetCellStyleBottom(nCol, nRow));
1218 
1219                             rInstance.addSdrConnectStyleData(true, rBLFromTop, -aY, true);
1220                             rInstance.addSdrConnectStyleData(true, rBLFromBottom, aX, false);
1221 
1222                             /// Fill top-right Style Table
1223                             const Style& rTRFromLeft(GetCellStyleTop(nCol, nRow));
1224                             const Style& rTRFromBottom(GetCellStyleRight(nCol, nRow));
1225 
1226                             rInstance.addSdrConnectStyleData(false, rTRFromLeft, -aX, true);
1227                             rInstance.addSdrConnectStyleData(false, rTRFromBottom, aY, false);
1228                         }
1229                     }
1230                 }
1231             }
1232             else
1233             {
1234                 // create left line for this Cell
1235                 if ((!bOverlapX         // true for first column in merged cells or cells
1236                     || bFirstCol)       // true for non_Calc usages of this tooling
1237                     && !bSuppressLeft)  // true when left is not rotated, so edge is already handled (see bRotated)
1238                 {
1239                     const Style& rLeft(GetCellStyleLeft(nCol, nRow));
1240 
1241                     if (rLeft.IsUsed())
1242                     {
1243                         HelperCreateVerticalEntry(*this, rLeft, nCol, nRow, aOrigin, aX, aY, *aData, true, pForceColor);
1244                     }
1245                 }
1246             }
1247         }
1248     }
1249 
1250     // create instance of SdrFrameBorderPrimitive2D if
1251     // SdrFrameBorderDataVector is used
1252     drawinglayer::primitive2d::Primitive2DContainer aSequence;
1253 
1254     if(!aData->empty())
1255     {
1256         aSequence.append(
1257             drawinglayer::primitive2d::Primitive2DReference(
1258                 new drawinglayer::primitive2d::SdrFrameBorderPrimitive2D(
1259                     aData,
1260                     true)));    // force visualization to minimal one discrete unit (pixel)
1261     }
1262 
1263     return aSequence;
1264 }
1265 
CreateB2DPrimitiveArray() const1266 drawinglayer::primitive2d::Primitive2DContainer Array::CreateB2DPrimitiveArray() const
1267 {
1268     drawinglayer::primitive2d::Primitive2DContainer aPrimitives;
1269 
1270     if (mxImpl->mnWidth && mxImpl->mnHeight)
1271     {
1272         aPrimitives = CreateB2DPrimitiveRange(0, 0, mxImpl->mnWidth - 1, mxImpl->mnHeight - 1, nullptr);
1273     }
1274 
1275     return aPrimitives;
1276 }
1277 
1278 #undef ORIGCELL
1279 #undef CELLACC
1280 #undef CELL
1281 #undef DBG_FRAME_CHECK_ROW_1
1282 #undef DBG_FRAME_CHECK_COL_1
1283 #undef DBG_FRAME_CHECK_COLROW
1284 #undef DBG_FRAME_CHECK_ROW
1285 #undef DBG_FRAME_CHECK_COL
1286 #undef DBG_FRAME_CHECK
1287 
1288 }
1289 
1290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
1291