1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include "WW8TableInfo.hxx"
23 #include <fmtfsize.hxx>
24 #include "attributeoutputbase.hxx"
25 #include <swtable.hxx>
28 #include <dbgoutsw.hxx>
29 #include <sal/log.hxx>
30 #include <osl/diagnose.h>
35 WW8TableNodeInfoInner::WW8TableNodeInfoInner(WW8TableNodeInfo
* pParent
)
43 , mbFinalEndOfLine(false)
45 , mbFirstInTable(false)
52 WW8TableNodeInfoInner::~WW8TableNodeInfoInner()
56 void WW8TableNodeInfoInner::setDepth(sal_uInt32 nDepth
)
61 void WW8TableNodeInfoInner::setCell(sal_uInt32 nCell
)
66 void WW8TableNodeInfoInner::setRow(sal_uInt32 nRow
)
71 void WW8TableNodeInfoInner::setShadowsBefore(sal_uInt32 nShadowsBefore
)
73 mnShadowsBefore
= nShadowsBefore
;
76 void WW8TableNodeInfoInner::setShadowsAfter(sal_uInt32 nShadowsAfter
)
78 mnShadowsAfter
= nShadowsAfter
;
81 void WW8TableNodeInfoInner::setEndOfLine(bool bEndOfLine
)
83 mbEndOfLine
= bEndOfLine
;
86 void WW8TableNodeInfoInner::setFinalEndOfLine(bool bFinalEndOfLine
)
88 mbFinalEndOfLine
= bFinalEndOfLine
;
91 void WW8TableNodeInfoInner::setEndOfCell(bool bEndOfCell
)
93 mbEndOfCell
= bEndOfCell
;
96 void WW8TableNodeInfoInner::setFirstInTable(bool bFirstInTable
)
98 mbFirstInTable
= bFirstInTable
;
101 void WW8TableNodeInfoInner::setVertMerge(bool bVertMerge
)
104 mbVertMerge
= bVertMerge
;
107 void WW8TableNodeInfoInner::setTableBox(const SwTableBox
* pTableBox
)
109 mpTableBox
= pTableBox
;
112 void WW8TableNodeInfoInner::setTable(const SwTable
* pTable
)
117 void WW8TableNodeInfoInner::setRect(const SwRect
& rRect
)
122 const SwNode
* WW8TableNodeInfoInner::getNode() const
124 const SwNode
* pResult
= nullptr;
126 if (mpParent
!= nullptr)
127 pResult
= mpParent
->getNode();
132 TableBoxVectorPtr
WW8TableNodeInfoInner::getTableBoxesOfRow() const
134 TableBoxVectorPtr pResult
= std::make_shared
<TableBoxVector
>();
136 WW8TableCellGrid::Pointer_t pCellGrid
=
137 mpParent
->getParent()->getCellGridForTable(getTable(), false);
141 const SwTableLine
* pTabLine
= getTableBox()->GetUpper();
142 const SwTableBoxes
& rTableBoxes
= pTabLine
->GetTabBoxes();
144 sal_uInt8 nBoxes
= rTableBoxes
.size();
145 if (nBoxes
> MAXTABLECELLS
)
146 nBoxes
= MAXTABLECELLS
;
147 for ( sal_uInt8 n
= 0; n
< nBoxes
; n
++ )
149 pResult
->push_back(rTableBoxes
[n
]);
153 pResult
= pCellGrid
->getTableBoxesOfRow(this);
158 GridColsPtr
WW8TableNodeInfoInner::getGridColsOfRow(AttributeOutputBase
& rBase
, bool calculateColumnsFromAllRows
)
160 GridColsPtr pResult
= std::make_shared
<GridCols
>();
163 // Check which columns should be checked - only the current row,
164 // or all the rows together
165 if (calculateColumnsFromAllRows
)
167 // Calculate the width of all the columns based on ALL the rows.
168 // The difference is that this kind of draws vertical lines,
169 // so that if the rows look like this:
171 // ------------------------
173 // ------------------------
175 // ------------------------
177 // ------------------------
179 // then the actual column widths will be broken down like this:
181 // ------------------------
183 // ------------------------
185 // See the example at
186 // http://officeopenxml.com/WPtableGrid.php
187 // Under "Word 2007 Example"
188 pWidths
= getColumnWidthsBasedOnAllRows();
192 // Calculate the width of all the columns based on the current row
193 pWidths
= getWidthsOfRow();
196 const SwFrameFormat
*pFormat
= getTable()->GetFrameFormat();
197 OSL_ENSURE(pFormat
,"Impossible");
201 const SwFormatFrameSize
&rSize
= pFormat
->GetFrameSize();
202 tools::ULong nTableSz
= static_cast<tools::ULong
>(rSize
.GetWidth());
204 tools::Long nPageSize
= 0;
205 bool bRelBoxSize
= false;
207 rBase
.GetTablePageSize( this, nPageSize
, bRelBoxSize
);
210 for (const auto& rWidth
: *pWidths
)
215 nCalc
= ( nCalc
* nPageSize
) / nTableSz
;
217 pResult
->push_back( nCalc
);
223 WidthsPtr
WW8TableNodeInfoInner::getColumnWidthsBasedOnAllRows() const
227 WW8TableCellGrid::Pointer_t pCellGrid
=
228 mpParent
->getParent()->getCellGridForTable(getTable(), false);
232 const SwTable
* pTable
= getTable();
233 const SwTableLines
& rTableLines
= pTable
->GetTabLines();
234 const size_t nNumOfLines
= rTableLines
.size();
236 // Go over all the rows - and for each row - calculate where
237 // there is a separator between columns
238 WidthsPtr pSeparators
= std::make_shared
<Widths
>();
239 for ( size_t nLineIndex
= 0; nLineIndex
< nNumOfLines
; ++nLineIndex
)
241 const SwTableLine
*pCurrentLine
= rTableLines
[nLineIndex
];
242 const SwTableBoxes
& rTabBoxes
= pCurrentLine
->GetTabBoxes();
243 size_t nBoxes
= rTabBoxes
.size();
244 if (nBoxes
> MAXTABLECELLS
)
245 nBoxes
= MAXTABLECELLS
;
247 sal_uInt32 nSeparatorPosition
= 0;
248 for (size_t nBoxIndex
= 0; nBoxIndex
< nBoxes
; ++nBoxIndex
)
250 const SwFrameFormat
* pBoxFormat
= rTabBoxes
[ nBoxIndex
]->GetFrameFormat();
251 const SwFormatFrameSize
& rLSz
= pBoxFormat
->GetFrameSize();
252 nSeparatorPosition
+= rLSz
.GetWidth();
253 pSeparators
->push_back(nSeparatorPosition
);
257 // Sort the separator positions and remove any duplicates
258 std::sort(pSeparators
->begin(), pSeparators
->end());
259 std::vector
<sal_uInt32
>::iterator it
= std::unique(pSeparators
->begin(), pSeparators
->end());
260 pSeparators
->erase(it
, pSeparators
->end());
262 // Calculate the widths based on the position of the unique & sorted
264 pWidths
= std::make_shared
<Widths
>();
265 sal_uInt32 nPreviousWidth
= 0;
266 for (const sal_uInt32 nCurrentWidth
: *pSeparators
)
268 pWidths
->push_back(nCurrentWidth
- nPreviousWidth
);
269 nPreviousWidth
= nCurrentWidth
;
274 pWidths
= pCellGrid
->getWidthsOfRow(this);
280 WidthsPtr
WW8TableNodeInfoInner::getWidthsOfRow() const
284 WW8TableCellGrid::Pointer_t pCellGrid
=
285 mpParent
->getParent()->getCellGridForTable(getTable(), false);
289 const SwTableBox
* pTabBox
= getTableBox();
290 const SwTableLine
* pTabLine
= pTabBox
->GetUpper();
291 const SwTableBoxes
& rTabBoxes
= pTabLine
->GetTabBoxes();
293 pWidths
= std::make_shared
<Widths
>();
294 // number of cell written
295 sal_uInt32 nBoxes
= rTabBoxes
.size();
296 if (nBoxes
> MAXTABLECELLS
)
297 nBoxes
= MAXTABLECELLS
;
299 for (sal_uInt32 n
= 0; n
< nBoxes
; n
++)
301 const SwFrameFormat
* pBoxFormat
= rTabBoxes
[ n
]->GetFrameFormat();
302 const SwFormatFrameSize
& rLSz
= pBoxFormat
->GetFrameSize();
304 pWidths
->push_back(rLSz
.GetWidth());
308 pWidths
= pCellGrid
->getWidthsOfRow(this);
313 RowSpansPtr
WW8TableNodeInfoInner::getRowSpansOfRow() const
315 RowSpansPtr pResult
= std::make_shared
<RowSpans
>();
317 WW8TableCellGrid::Pointer_t pCellGrid
=
318 mpParent
->getParent()->getCellGridForTable(getTable(), false);
322 const SwTableBox
* pTabBox
= getTableBox();
323 const SwTableLine
* pTabLine
= pTabBox
->GetUpper();
324 const SwTableBoxes
& rTabBoxes
= pTabLine
->GetTabBoxes();
326 sal_uInt32 nBoxes
= rTabBoxes
.size();
327 if (nBoxes
> MAXTABLECELLS
)
328 nBoxes
= MAXTABLECELLS
;
330 for (sal_uInt32 n
= 0; n
< nBoxes
; ++n
)
332 pResult
->push_back(rTabBoxes
[n
]->getRowSpan());
336 pResult
= pCellGrid
->getRowSpansOfRow(this);
343 std::string
WW8TableNodeInfoInner::toString() const
345 static char buffer
[256];
346 snprintf(buffer
, sizeof(buffer
),
347 "<tableinner depth=\"%" SAL_PRIuUINT32
"\""
348 " cell=\"%" SAL_PRIuUINT32
"\""
349 " row=\"%" SAL_PRIuUINT32
"\""
352 " shadowsBefore=\"%" SAL_PRIuUINT32
"\""
353 " shadowsAfter=\"%" SAL_PRIuUINT32
"\""
354 " vertMerge=\"%s\"/>",
355 mnDepth
, mnCell
, mnRow
,
356 mbEndOfCell
? "yes" : "no",
357 mbEndOfLine
? "yes" : "no",
360 mbVertMerge
? "yes" : "no");
362 return std::string(buffer
);
366 WW8TableNodeInfo::WW8TableNodeInfo(WW8TableInfo
* pParent
,
367 const SwNode
* pNode
)
376 WW8TableNodeInfo::~WW8TableNodeInfo()
381 std::string
WW8TableNodeInfo::toString() const
383 static char buffer
[1024];
384 snprintf(buffer
, sizeof(buffer
),
385 "<tableNodeInfo p=\"%p\" depth=\"%" SAL_PRIuUINT32
"\">"
388 std::string
sResult(buffer
);
390 for (const auto& rInner
: mInners
)
392 WW8TableNodeInfoInner::Pointer_t pInner
= rInner
.second
;
393 sResult
+= pInner
->toString();
395 sResult
+= dbg_out(*mpNode
);
396 sResult
+= "</tableNodeInfo>";
402 void WW8TableNodeInfo::setDepth(sal_uInt32 nDepth
)
406 Inners_t::iterator aIt
= mInners
.find(mnDepth
);
408 if (aIt
== mInners
.end())
409 mInners
[mnDepth
] = std::make_shared
<ww8::WW8TableNodeInfoInner
>(this);
411 mInners
[mnDepth
]->setDepth(mnDepth
);
414 void WW8TableNodeInfo::setEndOfLine(bool bEndOfLine
)
416 WW8TableNodeInfoInner::Pointer_t pInner
= getInnerForDepth(mnDepth
);
417 pInner
->setEndOfLine(bEndOfLine
);
420 SAL_INFO( "sw.ww8", "<endOfLine depth=\"" << mnDepth
<< "\">" << toString() << "</endOfLine>" );
424 void WW8TableNodeInfo::setEndOfCell(bool bEndOfCell
)
426 WW8TableNodeInfoInner::Pointer_t pInner
= getInnerForDepth(mnDepth
);
427 pInner
->setEndOfCell(bEndOfCell
);
430 SAL_INFO( "sw.ww8", "<endOfCell depth=\"" << mnDepth
<< "\">" << toString() << "</endOfCell>" );
434 void WW8TableNodeInfo::setFirstInTable(bool bFirstInTable
)
436 WW8TableNodeInfoInner::Pointer_t pInner
= getInnerForDepth(mnDepth
);
438 pInner
->setFirstInTable(bFirstInTable
);
441 SAL_INFO( "sw.ww8", "<firstInTable depth=\"" << mnDepth
<< "\">" << toString() << "</firstInTable>" );
445 void WW8TableNodeInfo::setVertMerge(bool bVertMerge
)
447 WW8TableNodeInfoInner::Pointer_t pInner
= getInnerForDepth(mnDepth
);
449 pInner
->setVertMerge(bVertMerge
);
452 SAL_INFO( "sw.ww8", "<vertMerge depth=\"" << mnDepth
<< "\">" << toString() << "</vertMerge>" );
456 void WW8TableNodeInfo::setTableBox(const SwTableBox
* pTableBox
)
458 getInnerForDepth(mnDepth
)->setTableBox(pTableBox
);
461 void WW8TableNodeInfo::setTable(const SwTable
* pTable
)
463 getInnerForDepth(mnDepth
)->setTable(pTable
);
466 void WW8TableNodeInfo::setNext(WW8TableNodeInfo
* pNext
)
471 SAL_INFO( "sw.ww8", "<setnext><from>" << toString() << "</from><to>" << pNext
->toString() << "</to></setnext>" );
475 void WW8TableNodeInfo::setNextNode(const SwNode
* pNode
)
480 void WW8TableNodeInfo::setRect(const SwRect
& rRect
)
482 getInnerForDepth(mnDepth
)->setRect(rRect
);
485 void WW8TableNodeInfo::setCell(sal_uInt32 nCell
)
487 getInnerForDepth(mnDepth
)->setCell(nCell
);
490 void WW8TableNodeInfo::setRow(sal_uInt32 nRow
)
492 getInnerForDepth(mnDepth
)->setRow(nRow
);
495 void WW8TableNodeInfo::setShadowsBefore(sal_uInt32 nShadowsBefore
)
497 getInnerForDepth(mnDepth
)->setShadowsBefore(nShadowsBefore
);
500 void WW8TableNodeInfo::setShadowsAfter(sal_uInt32 nShadowsAfter
)
502 getInnerForDepth(mnDepth
)->setShadowsAfter(nShadowsAfter
);
506 sal_uInt32
WW8TableNodeInfo::getDepth() const
508 if (!mInners
.empty())
509 return mInners
.begin()->second
->getDepth();
515 const SwTableBox
* WW8TableNodeInfo::getTableBox() const
517 return getInnerForDepth(mnDepth
)->getTableBox();
520 sal_uInt32
WW8TableNodeInfo::getCell() const
522 return getInnerForDepth(mnDepth
)->getCell();
525 sal_uInt32
WW8TableNodeInfo::getRow() const
527 return getInnerForDepth(mnDepth
)->getRow();
530 WW8TableNodeInfoInner::Pointer_t
WW8TableNodeInfo::getFirstInner() const
532 WW8TableNodeInfoInner::Pointer_t pResult
;
534 if (!mInners
.empty())
535 pResult
= mInners
.begin()->second
;
540 WW8TableNodeInfoInner::Pointer_t
WW8TableNodeInfo::getInnerForDepth(sal_uInt32 nDepth
) const
542 WW8TableNodeInfoInner::Pointer_t pResult
;
544 Inners_t::const_iterator aIt
= mInners
.find(nDepth
);
545 if (aIt
!= mInners
.end())
547 pResult
= aIt
->second
;
553 WW8TableInfo::WW8TableInfo()
557 WW8TableInfo::~WW8TableInfo()
562 WW8TableInfo::processSwTableByLayout(const SwTable
* pTable
, RowEndInners_t
&rLastRowEnds
)
564 SwTableCellInfo
aTableCellInfo(pTable
);
566 while (aTableCellInfo
.getNext())
568 SwRect aRect
= aTableCellInfo
.getRect();
570 SAL_INFO( "sw.ww8", "<CellFrame>" );
571 SAL_INFO( "sw.ww8", "<rect top=\"" << aRect
.Top() << "\" bottom=\"" << aRect
.Bottom()
572 << "\" left=\"" << aRect
.Left() << "\" right=\"" << aRect
.Right() << "\"/>" );
573 const SwTableBox
* pTableBox
= aTableCellInfo
.getTableBox();
574 const SwStartNode
* pSttNd
= pTableBox
->GetSttNd();
576 if (pSttNd
!= nullptr)
578 SwPaM
aPam(*pSttNd
, 0);
583 SwNode
& rNode
= aPam
.GetPoint()->nNode
.GetNode();
585 insertTableNodeInfo(&rNode
, pTable
, pTableBox
, 0, 0, 1, & aRect
);
587 if (rNode
.IsEndNode())
589 SwEndNode
* pEndNode
= rNode
.GetEndNode();
590 SwStartNode
* pTmpSttNd
= pEndNode
->StartOfSectionNode();
592 if (pTmpSttNd
== pSttNd
)
596 aPam
.GetPoint()->nNode
++;
601 SAL_INFO( "sw.ww8", "</CellFrame>" );
604 return reorderByLayout(pTable
, rLastRowEnds
);
607 void WW8TableInfo::processSwTable(const SwTable
* pTable
)
609 SAL_INFO( "sw.ww8", "<processSwTable>" );
611 WW8TableNodeInfo
* pPrev
= nullptr;
612 RowEndInners_t aLastRowEnds
;
614 if (pTable
->IsTableComplex() && pTable
->HasLayout())
616 pPrev
= processSwTableByLayout(pTable
, aLastRowEnds
);
618 SAL_INFO( "sw.ww8", getCellGridForTable(pTable
)->toString());
623 const SwTableLines
& rLines
= pTable
->GetTabLines();
625 for (size_t n
= 0; n
< rLines
.size(); ++n
)
627 const SwTableLine
* pLine
= rLines
[n
];
629 pPrev
= processTableLine(pTable
, pLine
, static_cast<sal_uInt32
>(n
), 1, pPrev
, aLastRowEnds
);
636 SwTableNode
* pTableNode
= pTable
->GetTableNode();
637 SwEndNode
* pEndNode
= pTableNode
->EndOfSectionNode();
638 pPrev
->setNextNode(pEndNode
);
639 assert(!aLastRowEnds
.empty());
640 for (auto &a
: aLastRowEnds
)
642 assert(a
.second
->isEndOfLine());
643 a
.second
->setFinalEndOfLine(true);
646 SAL_INFO( "sw.ww8", "</processSwTable>" );
650 WW8TableInfo::processTableLine(const SwTable
* pTable
,
651 const SwTableLine
* pTableLine
,
654 WW8TableNodeInfo
* pPrev
,
655 RowEndInners_t
&rLastRowEnds
)
657 SAL_INFO( "sw.ww8", "<processTableLine row=\"" << nRow
<< "\" depth=\"" << nDepth
<< "\">" );
659 const SwTableBoxes
& rBoxes
= pTableLine
->GetTabBoxes();
661 for (size_t n
= 0; n
< rBoxes
.size(); ++n
)
663 const SwTableBox
* pBox
= rBoxes
[n
];
665 pPrev
= processTableBox(pTable
, pBox
, nRow
, static_cast<sal_uInt32
>(n
), nDepth
, n
== rBoxes
.size() - 1, pPrev
, rLastRowEnds
);
668 SAL_INFO( "sw.ww8", "</processTableLine>" );
673 WW8TableNodeInfo::Pointer_t
674 WW8TableInfo::processTableBoxLines(const SwTableBox
* pBox
,
675 const SwTable
* pTable
,
676 const SwTableBox
* pBoxToSet
,
681 SAL_INFO( "sw.ww8", "<processTableBoxLines depth=\"" << nDepth
<< "\" row=\"" << nRow
682 << "\" cell=\"" << nCell
<< "\">" );
684 const SwTableLines
& rLines
= pBox
->GetTabLines();
685 WW8TableNodeInfo::Pointer_t pNodeInfo
;
689 for (size_t n
= 0; n
< rLines
.size(); ++n
)
691 const SwTableLine
* pLine
= rLines
[n
];
692 const SwTableBoxes
& rBoxes
= pLine
->GetTabBoxes();
694 for (size_t nBox
= 0; nBox
< rBoxes
.size(); ++nBox
)
695 pNodeInfo
= processTableBoxLines(rBoxes
[nBox
], pTable
, pBoxToSet
, nRow
, nCell
, nDepth
);
700 const SwStartNode
* pSttNd
= pBox
->GetSttNd();
701 const SwEndNode
* pEndNd
= pSttNd
->EndOfSectionNode();
702 SwPaM
aPaM(*pSttNd
, 0);
703 SwPaM
aEndPaM(*pEndNd
, 0);
708 SwNode
& rNode
= aPaM
.GetPoint()->nNode
.GetNode();
710 pNodeInfo
= insertTableNodeInfo(&rNode
, pTable
, pBoxToSet
, nRow
, nCell
, nDepth
);
712 if (aPaM
.GetPoint()->nNode
== aEndPaM
.GetPoint()->nNode
)
715 aPaM
.GetPoint()->nNode
++;
719 SAL_INFO( "sw.ww8", "</processTableBoxLines>" );
724 static void updateFinalEndOfLine(RowEndInners_t
&rLastRowEnds
, WW8TableNodeInfo
const * pEndOfCellInfo
)
726 sal_Int32 nDepth
= pEndOfCellInfo
->getDepth();
727 WW8TableNodeInfoInner::Pointer_t pInner
= pEndOfCellInfo
->getInnerForDepth(nDepth
);
729 auto aIt
= rLastRowEnds
.find(nDepth
);
730 if (aIt
== rLastRowEnds
.end() || (pInner
->getRow() > aIt
->second
->getRow()))
731 rLastRowEnds
[nDepth
] = pInner
.get();
735 WW8TableInfo::processTableBox(const SwTable
* pTable
,
736 const SwTableBox
* pBox
,
741 WW8TableNodeInfo
* pPrev
,
742 RowEndInners_t
&rLastRowEnds
)
744 SAL_INFO( "sw.ww8", "<processTableBox row=\"" << nRow
<< "\" cell=\"" << nCell
745 << "\" depth=\"" << nDepth
<< "\">" );
747 WW8TableNodeInfo::Pointer_t pNodeInfo
;
748 const SwTableLines
& rLines
= pBox
->GetTabLines();
749 const SwStartNode
* pSttNd
= pBox
->GetSttNd();
750 WW8TableNodeInfo::Pointer_t pEndOfCellInfo
;
754 pNodeInfo
= processTableBoxLines(pBox
, pTable
, pBox
, nRow
, nCell
, nDepth
);
755 pNodeInfo
->setEndOfCell(true);
758 pNodeInfo
->setEndOfLine(true);
759 updateFinalEndOfLine(rLastRowEnds
, pNodeInfo
.get());
762 for (size_t n
= 0; n
< rLines
.size(); n
++)
764 const SwTableLine
* pLine
= rLines
[n
];
766 pPrev
= processTableLine(pTable
, pLine
, n
, 1, pPrev
, rLastRowEnds
);
771 SwPaM
aPaM(*pSttNd
, 0);
774 sal_uInt32 nDepthInsideCell
= 0;
778 SwNode
& rNode
= aPaM
.GetPoint()->nNode
.GetNode();
780 if (rNode
.IsStartNode())
782 if (nDepthInsideCell
> 0)
783 pEndOfCellInfo
.reset();
788 pNodeInfo
= insertTableNodeInfo(&rNode
, pTable
, pBox
, nRow
, nCell
, nDepth
);
791 pPrev
->setNext(pNodeInfo
.get());
793 pPrev
= pNodeInfo
.get();
795 if (nDepthInsideCell
== 1 && rNode
.IsTextNode())
796 pEndOfCellInfo
= pNodeInfo
;
798 if (rNode
.IsEndNode())
802 if (nDepthInsideCell
== 0 && !pEndOfCellInfo
)
803 pEndOfCellInfo
= pNodeInfo
;
805 SwEndNode
* pEndNode
= rNode
.GetEndNode( );
806 SwStartNode
* pTmpSttNd
= pEndNode
->StartOfSectionNode();
807 if (pTmpSttNd
== pSttNd
)
811 aPaM
.GetPoint()->nNode
++;
817 pEndOfCellInfo
->setEndOfCell(true);
821 pEndOfCellInfo
->setEndOfLine(true);
822 updateFinalEndOfLine(rLastRowEnds
, pEndOfCellInfo
.get());
827 SAL_INFO( "sw.ww8", "</processTableBox>" );
832 WW8TableNodeInfo::Pointer_t
WW8TableInfo::insertTableNodeInfo
833 (const SwNode
* pNode
,
834 const SwTable
* pTable
,
835 const SwTableBox
* pTableBox
,
839 SwRect
const * pRect
)
841 WW8TableNodeInfo::Pointer_t pNodeInfo
= getTableNodeInfo(pNode
);
846 std::make_shared
<ww8::WW8TableNodeInfo
>(this, pNode
);
847 mMap
.emplace(pNode
, pNodeInfo
);
850 pNodeInfo
->setDepth(nDepth
+ pNodeInfo
->getDepth());
852 pNodeInfo
->setTable(pTable
);
853 pNodeInfo
->setTableBox(pTableBox
);
855 pNodeInfo
->setCell(nCell
);
856 pNodeInfo
->setRow(nRow
);
858 if (pNode
->IsTextNode())
860 FirstInTableMap_t::const_iterator aIt
= mFirstInTableMap
.find(pTable
);
861 if (aIt
== mFirstInTableMap
.end())
863 mFirstInTableMap
[pTable
] = pNode
;
864 pNodeInfo
->setFirstInTable(true);
870 WW8TableCellGrid::Pointer_t pCellGrid
= getCellGridForTable(pTable
);
872 pCellGrid
->insert(*pRect
, pNodeInfo
.get());
873 pNodeInfo
->setRect(*pRect
);
877 SAL_INFO( "sw.ww8", pNodeInfo
->toString());
882 WW8TableCellGrid::Pointer_t
WW8TableInfo::getCellGridForTable
883 (const SwTable
* pTable
, bool bCreate
)
885 WW8TableCellGrid::Pointer_t pResult
;
886 CellGridMap_t::iterator aIt
= mCellGridMap
.find(pTable
);
888 if (aIt
== mCellGridMap
.end())
892 pResult
= std::make_shared
<ww8::WW8TableCellGrid
>();
893 mCellGridMap
[pTable
] = pResult
;
897 pResult
= mCellGridMap
[pTable
];
902 WW8TableNodeInfo::Pointer_t
WW8TableInfo::getTableNodeInfo
903 (const SwNode
* pNode
)
905 WW8TableNodeInfo::Pointer_t pResult
;
906 Map_t::iterator aIt
= mMap
.find(pNode
);
908 if (aIt
!= mMap
.end())
909 pResult
= (*aIt
).second
;
914 const SwNode
* WW8TableInfo::getNextNode(const SwNode
* pNode
)
916 const SwNode
* pResult
= nullptr;
918 WW8TableNodeInfo::Pointer_t pNodeInfo
= getTableNodeInfo(pNode
);
922 WW8TableNodeInfo
* pNextInfo
= pNodeInfo
->getNext();
924 if (pNextInfo
!= nullptr)
925 pResult
= pNextInfo
->getNode();
928 const SwNode
* pNextNode
= pNodeInfo
->getNextNode();
930 if (pNextNode
!= nullptr)
938 bool WW8TableNodeInfo::operator < (const WW8TableNodeInfo
& rInfo
) const
942 if (rInfo
.mpNode
!= nullptr)
944 if (mpNode
== nullptr)
950 if (mpNode
->GetIndex() < rInfo
.mpNode
->GetIndex())
958 bool CellInfo::operator < (const CellInfo
& aCellInfo
) const
962 if (top() < aCellInfo
.top())
964 else if (top() == aCellInfo
.top())
966 if (left() < aCellInfo
.left())
968 else if (left() == aCellInfo
.left())
970 if (width() < aCellInfo
.width())
972 else if (width() == aCellInfo
.width())
974 if (height() < aCellInfo
.height())
976 else if (height() == aCellInfo
.height())
978 if (aCellInfo
.getTableNodeInfo())
980 if (m_pNodeInfo
== nullptr)
984 aRet
= *m_pNodeInfo
< *aCellInfo
.getTableNodeInfo();
996 std::string
CellInfo::toString() const
998 static char sBuffer
[256];
1000 snprintf(sBuffer
, sizeof(sBuffer
),
1001 "<cellinfo left=\"%" SAL_PRIdINT64
"\""
1002 " right=\"%" SAL_PRIdINT64
"\""
1003 " top=\"%" SAL_PRIdINT64
"\""
1004 " bottom=\"%" SAL_PRIdINT64
"\""
1009 sal_Int64(bottom()),
1016 WW8TableNodeInfo
* WW8TableInfo::reorderByLayout(const SwTable
* pTable
, RowEndInners_t
&rLastRowEnds
)
1018 WW8TableCellGrid::Pointer_t pCellGrid
= getCellGridForTable(pTable
);
1021 SAL_INFO( "sw.ww8", pCellGrid
->toString());
1024 pCellGrid
->addShadowCells();
1025 return pCellGrid
->connectCells(rLastRowEnds
);
1028 WW8TableCellGrid::WW8TableCellGrid()
1032 WW8TableCellGrid::~WW8TableCellGrid()
1036 WW8TableCellGridRow::Pointer_t
WW8TableCellGrid::getRow(tools::Long nTop
, bool bCreate
)
1038 WW8TableCellGridRow::Pointer_t pResult
;
1040 RowTops_t::iterator aIt
= m_aRowTops
.find(nTop
);
1042 if (aIt
== m_aRowTops
.end())
1046 pResult
= std::make_shared
<ww8::WW8TableCellGridRow
>();
1047 m_aRows
[nTop
] = pResult
;
1048 m_aRowTops
.insert(nTop
);
1052 pResult
= m_aRows
[nTop
];
1057 WW8TableCellGrid::RowTops_t::const_iterator
WW8TableCellGrid::getRowTopsBegin() const
1059 return m_aRowTops
.begin();
1062 WW8TableCellGrid::RowTops_t::const_iterator
WW8TableCellGrid::getRowTopsEnd() const
1064 return m_aRowTops
.end();
1067 CellInfoMultiSet::const_iterator
WW8TableCellGrid::getCellsBegin(tools::Long nTop
)
1069 return getRow(nTop
)->begin();
1072 CellInfoMultiSet::const_iterator
WW8TableCellGrid::getCellsEnd(tools::Long nTop
)
1074 return getRow(nTop
)->end();
1077 void WW8TableCellGrid::insert(const SwRect
& rRect
,
1078 WW8TableNodeInfo
* pNodeInfo
,
1079 const tools::ULong
* pFormatFrameWidth
)
1081 CellInfo
aCellInfo(rRect
, pNodeInfo
);
1083 if (pFormatFrameWidth
!= nullptr)
1084 aCellInfo
.setFormatFrameWidth(*pFormatFrameWidth
);
1086 WW8TableCellGridRow::Pointer_t pRow
= getRow(rRect
.Top());
1087 pRow
->insert(aCellInfo
);
1090 void WW8TableCellGrid::addShadowCells()
1092 SAL_INFO( "sw.ww8", "<addShadowCells>" );
1094 RowTops_t::const_iterator aTopsIt
= getRowTopsBegin();
1096 while (aTopsIt
!= getRowTopsEnd())
1098 CellInfoMultiSet::const_iterator aCellIt
= getCellsBegin(*aTopsIt
);
1099 CellInfoMultiSet::const_iterator aCellEndIt
= getCellsEnd(*aTopsIt
);
1101 RowSpansPtr pRowSpans
= std::make_shared
<RowSpans
>();
1103 bool bBeginningOfCell
= true;
1104 bool bVertMerge
= false;
1105 SwRect aRect
= aCellIt
->getRect();
1106 sal_Int32 nRowSpan
= 1;
1107 while (aCellIt
!= aCellEndIt
)
1109 WW8TableNodeInfo
* pNodeInfo
= aCellIt
->getTableNodeInfo();
1111 if (bBeginningOfCell
)
1113 RowTops_t::const_iterator
aRowSpanIt(aTopsIt
);
1116 if (aRowSpanIt
!= getRowTopsEnd() &&
1117 *aRowSpanIt
< aCellIt
->bottom())
1119 aRect
.Top(*aRowSpanIt
);
1120 tools::ULong nFormatFrameWidth
= aCellIt
->getFormatFrameWidth();
1121 insert(aRect
, nullptr, &nFormatFrameWidth
);
1129 while (aRowSpanIt
!= getRowTopsEnd() &&
1130 *aRowSpanIt
< aCellIt
->bottom())
1137 pRowSpans
->push_back(nRowSpan
);
1139 pRowSpans
->push_back(-nRowSpan
);
1144 pNodeInfo
->setVertMerge(bVertMerge
);
1148 if (aCellIt
!= aCellEndIt
)
1150 bBeginningOfCell
= (aRect
.Left() != aCellIt
->left());
1151 aRect
= aCellIt
->getRect();
1155 WW8TableCellGridRow::Pointer_t pRow
= getRow(*aTopsIt
);
1157 pRow
->setRowSpans(pRowSpans
);
1161 SAL_INFO( "sw.ww8", "</addShadowCells>" );
1164 WW8TableNodeInfo
* WW8TableCellGrid::connectCells(RowEndInners_t
&rLastRowEnds
)
1166 RowTops_t::const_iterator aTopsIt
= getRowTopsBegin();
1167 sal_uInt32 nRow
= 0;
1168 WW8TableNodeInfo
* pLastNodeInfo
= nullptr;
1170 while (aTopsIt
!= getRowTopsEnd())
1172 CellInfoMultiSet::const_iterator aCellIt
= getCellsBegin(*aTopsIt
);
1173 CellInfoMultiSet::const_iterator aCellEndIt
= getCellsEnd(*aTopsIt
);
1174 GridColsPtr pWidths
= std::make_shared
<Widths
>();
1175 TableBoxVectorPtr pTableBoxes
= std::make_shared
<TableBoxVector
>();
1177 sal_uInt32 nShadows
= 0;
1178 sal_uInt32 nCell
= 0;
1179 bool bBeginningOfCell
= true;
1180 WW8TableNodeInfo
* pEndOfCellInfo
= nullptr;
1181 sal_uInt32 nDepthInCell
= 0;
1182 while (aCellIt
!= aCellEndIt
)
1184 tools::Long nCellX
= aCellIt
->left();
1185 WW8TableNodeInfo
* pNodeInfo
= aCellIt
->getTableNodeInfo();
1188 const SwNode
* pNode
= pNodeInfo
->getNode();
1190 if (pNode
->IsStartNode())
1193 pEndOfCellInfo
= nullptr;
1196 if (nDepthInCell
== 1 && pNode
->IsTextNode())
1197 pEndOfCellInfo
= pNodeInfo
;
1199 pNodeInfo
->setShadowsBefore(nShadows
);
1200 pNodeInfo
->setCell(nCell
);
1201 pNodeInfo
->setRow(nRow
);
1204 pLastNodeInfo
->setNext(pNodeInfo
);
1205 pLastNodeInfo
->setNextNode(pNode
);
1207 pLastNodeInfo
= pNodeInfo
;
1210 if (pNode
->IsEndNode())
1214 if (nDepthInCell
== 0 && !pEndOfCellInfo
)
1215 pEndOfCellInfo
= pNodeInfo
;
1223 if (bBeginningOfCell
)
1225 pWidths
->push_back(aCellIt
->getFormatFrameWidth());
1228 pTableBoxes
->push_back(pNodeInfo
->getTableBox());
1230 pTableBoxes
->push_back(nullptr);
1234 bBeginningOfCell
= false;
1236 if (aCellIt
!= aCellEndIt
&& aCellIt
->left() != nCellX
)
1239 bBeginningOfCell
= true;
1243 pEndOfCellInfo
->setEndOfCell(true);
1246 pEndOfCellInfo
= nullptr;
1250 pLastNodeInfo
->setShadowsAfter(nShadows
);
1252 if (!pEndOfCellInfo
)
1254 pEndOfCellInfo
= pLastNodeInfo
;
1257 pEndOfCellInfo
->setEndOfCell(true);
1258 pLastNodeInfo
->setEndOfLine(true);
1259 updateFinalEndOfLine(rLastRowEnds
, pLastNodeInfo
);
1261 WW8TableCellGridRow::Pointer_t
pRow(getRow(*aTopsIt
));
1262 pRow
->setTableBoxVector(pTableBoxes
);
1263 pRow
->setWidths(pWidths
);
1269 return pLastNodeInfo
;
1273 std::string
WW8TableCellGrid::toString()
1275 std::string sResult
= "<WW8TableCellGrid>";
1277 RowTops_t::const_iterator aTopsIt
= getRowTopsBegin();
1278 static char sBuffer
[1024];
1279 while (aTopsIt
!= getRowTopsEnd())
1281 sprintf(sBuffer
, "<row y=\"%" SAL_PRIdINT64
"\">", sal_Int64(*aTopsIt
));
1284 CellInfoMultiSet::const_iterator aCellIt
= getCellsBegin(*aTopsIt
);
1285 CellInfoMultiSet::const_iterator aCellsEnd
= getCellsEnd(*aTopsIt
);
1287 while (aCellIt
!= aCellsEnd
)
1289 snprintf(sBuffer
, sizeof(sBuffer
), "<cellInfo top=\"%" SAL_PRIdINT64
"\" bottom=\"%" SAL_PRIdINT64
"\" left=\"%" SAL_PRIdINT64
"\" right=\"%" SAL_PRIdINT64
"\">",
1290 sal_Int64(aCellIt
->top()), sal_Int64(aCellIt
->bottom()), sal_Int64(aCellIt
->left()), sal_Int64(aCellIt
->right()));
1293 WW8TableNodeInfo
* pInfo
= aCellIt
->getTableNodeInfo();
1295 sResult
+= pInfo
->toString();
1297 sResult
+= "<shadow/>\n";
1299 sResult
+= "</cellInfo>\n";
1303 WW8TableCellGridRow::Pointer_t pRow
= getRow(*aTopsIt
);
1304 WidthsPtr pWidths
= pRow
->getWidths();
1305 if (pWidths
!= nullptr)
1307 sResult
+= "<widths>";
1309 Widths::const_iterator aItEnd
= pWidths
->end();
1310 for (Widths::const_iterator aIt
= pWidths
->begin();
1314 if (aIt
!= pWidths
->begin())
1317 snprintf(sBuffer
, sizeof(sBuffer
), "%" SAL_PRIxUINT32
"", *aIt
);
1321 sResult
+= "</widths>";
1324 RowSpansPtr pRowSpans
= pRow
->getRowSpans();
1327 sResult
+= "<rowspans>";
1329 RowSpans::const_iterator aItEnd
= pRowSpans
->end();
1330 for (RowSpans::const_iterator aIt
= pRowSpans
->begin();
1334 if (aIt
!= pRowSpans
->begin())
1337 snprintf(sBuffer
, sizeof(sBuffer
), "%" SAL_PRIxUINT32
"", *aIt
);
1341 sResult
+= "</rowspans>";
1344 sResult
+= "</row>\n";
1348 sResult
+= "</WW8TableCellGrid>\n";
1354 TableBoxVectorPtr
WW8TableCellGrid::getTableBoxesOfRow
1355 (WW8TableNodeInfoInner
const * pNodeInfoInner
)
1357 TableBoxVectorPtr pResult
;
1358 WW8TableCellGridRow::Pointer_t pRow
=
1359 getRow(pNodeInfoInner
->getRect().Top(), false);
1363 pResult
= pRow
->getTableBoxVector();
1369 WidthsPtr
WW8TableCellGrid::getWidthsOfRow
1370 (WW8TableNodeInfoInner
const * pNodeInfoInner
)
1372 GridColsPtr pResult
;
1374 WW8TableCellGridRow::Pointer_t pRow
=
1375 getRow(pNodeInfoInner
->getRect().Top(), false);
1379 pResult
= pRow
->getWidths();
1385 RowSpansPtr
WW8TableCellGrid::getRowSpansOfRow
1386 (WW8TableNodeInfoInner
const * pNodeInfoInner
)
1388 RowSpansPtr pResult
;
1390 WW8TableCellGridRow::Pointer_t pRow
=
1391 getRow(pNodeInfoInner
->getRect().Top(), false);
1395 pResult
= pRow
->getRowSpans();
1401 WW8TableCellGridRow::WW8TableCellGridRow()
1402 : m_pCellInfos(std::make_shared
<CellInfoMultiSet
>())
1406 WW8TableCellGridRow::~WW8TableCellGridRow()
1410 void WW8TableCellGridRow::insert(const CellInfo
& rCellInfo
)
1412 m_pCellInfos
->insert(rCellInfo
);
1415 SAL_INFO( "sw.ww8", "<gridRowInsert>" << rCellInfo
.toString() << "</gridRowInsert>" );
1419 CellInfoMultiSet::const_iterator
WW8TableCellGridRow::begin() const
1421 return m_pCellInfos
->begin();
1424 CellInfoMultiSet::const_iterator
WW8TableCellGridRow::end() const
1426 return m_pCellInfos
->end();
1429 void WW8TableCellGridRow::setTableBoxVector(TableBoxVectorPtr
const & pTableBoxVector
)
1431 if (pTableBoxVector
->size() > MAXTABLECELLS
)
1432 pTableBoxVector
->resize(MAXTABLECELLS
);
1433 m_pTableBoxVector
= pTableBoxVector
;
1436 void WW8TableCellGridRow::setWidths(WidthsPtr
const & pWidths
)
1438 m_pWidths
= pWidths
;
1441 void WW8TableCellGridRow::setRowSpans(RowSpansPtr
const & pRowSpans
)
1443 m_pRowSpans
= pRowSpans
;
1447 CellInfo::CellInfo(const SwRect
& aRect
, WW8TableNodeInfo
* pNodeInfo
)
1448 : m_aRect(aRect
), m_pNodeInfo(pNodeInfo
), m_nFormatFrameWidth(0)
1450 if (pNodeInfo
!= nullptr)
1452 const SwTableBox
* pBox
= pNodeInfo
->getTableBox();
1453 const SwFrameFormat
* pFrameFormat
= pBox
->GetFrameFormat();
1454 const SwFormatFrameSize
& rSize
= pFrameFormat
->GetFrameSize();
1456 m_nFormatFrameWidth
= rSize
.GetWidth();
1462 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */