nss: upgrade to release 3.73
[LibreOffice.git] / sw / source / filter / ww8 / WW8TableInfo.cxx
blob907e4131cec0bdf50a9b428c7acacc5bcff413fb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <iostream>
21 #include <stdio.h>
22 #include "WW8TableInfo.hxx"
23 #include <fmtfsize.hxx>
24 #include "attributeoutputbase.hxx"
25 #include <swtable.hxx>
26 #include <frmfmt.hxx>
27 #include <pam.hxx>
28 #include <dbgoutsw.hxx>
29 #include <sal/log.hxx>
30 #include <osl/diagnose.h>
32 namespace ww8
35 WW8TableNodeInfoInner::WW8TableNodeInfoInner(WW8TableNodeInfo * pParent)
36 : mpParent(pParent)
37 , mnDepth(0)
38 , mnCell(0)
39 , mnRow(0)
40 , mnShadowsBefore(0)
41 , mnShadowsAfter(0)
42 , mbEndOfLine(false)
43 , mbFinalEndOfLine(false)
44 , mbEndOfCell(false)
45 , mbFirstInTable(false)
46 , mbVertMerge(false)
47 , mpTableBox(nullptr)
48 , mpTable(nullptr)
52 WW8TableNodeInfoInner::~WW8TableNodeInfoInner()
56 void WW8TableNodeInfoInner::setDepth(sal_uInt32 nDepth)
58 mnDepth = nDepth;
61 void WW8TableNodeInfoInner::setCell(sal_uInt32 nCell)
63 mnCell = nCell;
66 void WW8TableNodeInfoInner::setRow(sal_uInt32 nRow)
68 mnRow = 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)
114 mpTable = pTable;
117 void WW8TableNodeInfoInner::setRect(const SwRect & rRect)
119 maRect = rRect;
122 const SwNode * WW8TableNodeInfoInner::getNode() const
124 const SwNode * pResult = nullptr;
126 if (mpParent != nullptr)
127 pResult = mpParent->getNode();
129 return pResult;
132 TableBoxVectorPtr WW8TableNodeInfoInner::getTableBoxesOfRow() const
134 TableBoxVectorPtr pResult = std::make_shared<TableBoxVector>();
136 WW8TableCellGrid::Pointer_t pCellGrid =
137 mpParent->getParent()->getCellGridForTable(getTable(), false);
139 if (!pCellGrid)
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]);
152 else
153 pResult = pCellGrid->getTableBoxesOfRow(this);
155 return pResult;
158 GridColsPtr WW8TableNodeInfoInner::getGridColsOfRow(AttributeOutputBase & rBase, bool calculateColumnsFromAllRows)
160 GridColsPtr pResult = std::make_shared<GridCols>();
161 WidthsPtr pWidths;
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 // ------------------------
172 // | | |
173 // ------------------------
174 // | | |
175 // ------------------------
176 // | | |
177 // ------------------------
179 // then the actual column widths will be broken down like this:
181 // ------------------------
182 // | | | | |
183 // ------------------------
185 // See the example at
186 // http://officeopenxml.com/WPtableGrid.php
187 // Under "Word 2007 Example"
188 pWidths = getColumnWidthsBasedOnAllRows();
190 else
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");
198 if (!pFormat)
199 return pResult;
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 );
209 SwTwips nSz = 0;
210 for (const auto& rWidth : *pWidths)
212 nSz += rWidth;
213 SwTwips nCalc = nSz;
214 if ( bRelBoxSize )
215 nCalc = ( nCalc * nPageSize ) / nTableSz;
217 pResult->push_back( nCalc );
220 return pResult;
223 WidthsPtr WW8TableNodeInfoInner::getColumnWidthsBasedOnAllRows() const
225 WidthsPtr pWidths;
227 WW8TableCellGrid::Pointer_t pCellGrid =
228 mpParent->getParent()->getCellGridForTable(getTable(), false);
230 if (!pCellGrid)
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
263 // column separators
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;
272 else
274 pWidths = pCellGrid->getWidthsOfRow(this);
277 return pWidths;
280 WidthsPtr WW8TableNodeInfoInner::getWidthsOfRow() const
282 WidthsPtr pWidths;
284 WW8TableCellGrid::Pointer_t pCellGrid =
285 mpParent->getParent()->getCellGridForTable(getTable(), false);
287 if (!pCellGrid)
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());
307 else
308 pWidths = pCellGrid->getWidthsOfRow(this);
310 return pWidths;
313 RowSpansPtr WW8TableNodeInfoInner::getRowSpansOfRow() const
315 RowSpansPtr pResult = std::make_shared<RowSpans>();
317 WW8TableCellGrid::Pointer_t pCellGrid =
318 mpParent->getParent()->getCellGridForTable(getTable(), false);
320 if (!pCellGrid)
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());
335 else
336 pResult = pCellGrid->getRowSpansOfRow(this);
338 return pResult;
342 #ifdef DBG_UTIL
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 "\""
350 " endOfCell=\"%s\""
351 " endOfLine=\"%s\""
352 " shadowsBefore=\"%" SAL_PRIuUINT32 "\""
353 " shadowsAfter=\"%" SAL_PRIuUINT32 "\""
354 " vertMerge=\"%s\"/>",
355 mnDepth, mnCell, mnRow,
356 mbEndOfCell ? "yes" : "no",
357 mbEndOfLine ? "yes" : "no",
358 mnShadowsBefore,
359 mnShadowsAfter,
360 mbVertMerge ? "yes" : "no");
362 return std::string(buffer);
364 #endif
366 WW8TableNodeInfo::WW8TableNodeInfo(WW8TableInfo * pParent,
367 const SwNode * pNode)
368 : mpParent(pParent),
369 mnDepth(0),
370 mpNode(pNode),
371 mpNext(nullptr),
372 mpNextNode(nullptr)
376 WW8TableNodeInfo::~WW8TableNodeInfo()
380 #ifdef DBG_UTIL
381 std::string WW8TableNodeInfo::toString() const
383 static char buffer[1024];
384 snprintf(buffer, sizeof(buffer),
385 "<tableNodeInfo p=\"%p\" depth=\"%" SAL_PRIuUINT32 "\">"
386 ,this, getDepth());
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>";
398 return sResult;
400 #endif
402 void WW8TableNodeInfo::setDepth(sal_uInt32 nDepth)
404 mnDepth = 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);
419 #ifdef DBG_UTIL
420 SAL_INFO( "sw.ww8", "<endOfLine depth=\"" << mnDepth << "\">" << toString() << "</endOfLine>" );
421 #endif
424 void WW8TableNodeInfo::setEndOfCell(bool bEndOfCell)
426 WW8TableNodeInfoInner::Pointer_t pInner = getInnerForDepth(mnDepth);
427 pInner->setEndOfCell(bEndOfCell);
429 #ifdef DBG_UTIL
430 SAL_INFO( "sw.ww8", "<endOfCell depth=\"" << mnDepth << "\">" << toString() << "</endOfCell>" );
431 #endif
434 void WW8TableNodeInfo::setFirstInTable(bool bFirstInTable)
436 WW8TableNodeInfoInner::Pointer_t pInner = getInnerForDepth(mnDepth);
438 pInner->setFirstInTable(bFirstInTable);
440 #ifdef DBG_UTIL
441 SAL_INFO( "sw.ww8", "<firstInTable depth=\"" << mnDepth << "\">" << toString() << "</firstInTable>" );
442 #endif
445 void WW8TableNodeInfo::setVertMerge(bool bVertMerge)
447 WW8TableNodeInfoInner::Pointer_t pInner = getInnerForDepth(mnDepth);
449 pInner->setVertMerge(bVertMerge);
451 #ifdef DBG_UTIL
452 SAL_INFO( "sw.ww8", "<vertMerge depth=\"" << mnDepth << "\">" << toString() << "</vertMerge>" );
453 #endif
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)
468 mpNext = pNext;
470 #ifdef DBG_UTIL
471 SAL_INFO( "sw.ww8", "<setnext><from>" << toString() << "</from><to>" << pNext->toString() << "</to></setnext>" );
472 #endif
475 void WW8TableNodeInfo::setNextNode(const SwNode * pNode)
477 mpNextNode = 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();
511 return mnDepth;
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;
537 return pResult;
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;
550 return pResult;
553 WW8TableInfo::WW8TableInfo()
557 WW8TableInfo::~WW8TableInfo()
561 WW8TableNodeInfo *
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);
580 bool bDone = false;
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)
593 bDone = true;
596 aPam.GetPoint()->nNode++;
598 while (!bDone);
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);
617 #ifdef DBG_UTIL
618 SAL_INFO( "sw.ww8", getCellGridForTable(pTable)->toString());
619 #endif
621 else
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);
634 if (pPrev)
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>" );
649 WW8TableNodeInfo *
650 WW8TableInfo::processTableLine(const SwTable * pTable,
651 const SwTableLine * pTableLine,
652 sal_uInt32 nRow,
653 sal_uInt32 nDepth,
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>" );
670 return pPrev;
673 WW8TableNodeInfo::Pointer_t
674 WW8TableInfo::processTableBoxLines(const SwTableBox * pBox,
675 const SwTable * pTable,
676 const SwTableBox * pBoxToSet,
677 sal_uInt32 nRow,
678 sal_uInt32 nCell,
679 sal_uInt32 nDepth)
681 SAL_INFO( "sw.ww8", "<processTableBoxLines depth=\"" << nDepth << "\" row=\"" << nRow
682 << "\" cell=\"" << nCell << "\">" );
684 const SwTableLines & rLines = pBox->GetTabLines();
685 WW8TableNodeInfo::Pointer_t pNodeInfo;
687 if (!rLines.empty())
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);
698 else
700 const SwStartNode * pSttNd = pBox->GetSttNd();
701 const SwEndNode * pEndNd = pSttNd->EndOfSectionNode();
702 SwPaM aPaM(*pSttNd, 0);
703 SwPaM aEndPaM(*pEndNd, 0);
705 bool bDone = false;
706 while (!bDone)
708 SwNode & rNode = aPaM.GetPoint()->nNode.GetNode();
710 pNodeInfo = insertTableNodeInfo(&rNode, pTable, pBoxToSet, nRow, nCell, nDepth);
712 if (aPaM.GetPoint()->nNode == aEndPaM.GetPoint()->nNode)
713 bDone = true;
714 else
715 aPaM.GetPoint()->nNode++;
719 SAL_INFO( "sw.ww8", "</processTableBoxLines>" );
721 return pNodeInfo;
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();
734 WW8TableNodeInfo *
735 WW8TableInfo::processTableBox(const SwTable * pTable,
736 const SwTableBox * pBox,
737 sal_uInt32 nRow,
738 sal_uInt32 nCell,
739 sal_uInt32 nDepth,
740 bool bEndOfLine,
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;
752 if (!rLines.empty())
754 pNodeInfo = processTableBoxLines(pBox, pTable, pBox, nRow, nCell, nDepth);
755 pNodeInfo->setEndOfCell(true);
756 if (bEndOfLine)
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);
769 else
771 SwPaM aPaM(*pSttNd, 0);
773 bool bDone = false;
774 sal_uInt32 nDepthInsideCell = 0;
778 SwNode & rNode = aPaM.GetPoint()->nNode.GetNode();
780 if (rNode.IsStartNode())
782 if (nDepthInsideCell > 0)
783 pEndOfCellInfo.reset();
785 nDepthInsideCell++;
788 pNodeInfo = insertTableNodeInfo(&rNode, pTable, pBox, nRow, nCell, nDepth);
790 if (pPrev)
791 pPrev->setNext(pNodeInfo.get());
793 pPrev = pNodeInfo.get();
795 if (nDepthInsideCell == 1 && rNode.IsTextNode())
796 pEndOfCellInfo = pNodeInfo;
798 if (rNode.IsEndNode())
800 nDepthInsideCell--;
802 if (nDepthInsideCell == 0 && !pEndOfCellInfo)
803 pEndOfCellInfo = pNodeInfo;
805 SwEndNode * pEndNode = rNode.GetEndNode( );
806 SwStartNode * pTmpSttNd = pEndNode->StartOfSectionNode();
807 if (pTmpSttNd == pSttNd)
808 bDone = true;
811 aPaM.GetPoint()->nNode++;
813 while (!bDone);
815 if (pEndOfCellInfo)
817 pEndOfCellInfo->setEndOfCell(true);
819 if (bEndOfLine)
821 pEndOfCellInfo->setEndOfLine(true);
822 updateFinalEndOfLine(rLastRowEnds, pEndOfCellInfo.get());
827 SAL_INFO( "sw.ww8", "</processTableBox>" );
829 return pPrev;
832 WW8TableNodeInfo::Pointer_t WW8TableInfo::insertTableNodeInfo
833 (const SwNode * pNode,
834 const SwTable * pTable,
835 const SwTableBox * pTableBox,
836 sal_uInt32 nRow,
837 sal_uInt32 nCell,
838 sal_uInt32 nDepth,
839 SwRect const * pRect)
841 WW8TableNodeInfo::Pointer_t pNodeInfo = getTableNodeInfo(pNode);
843 if (!pNodeInfo)
845 pNodeInfo =
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);
868 if (pRect)
870 WW8TableCellGrid::Pointer_t pCellGrid = getCellGridForTable(pTable);
872 pCellGrid->insert(*pRect, pNodeInfo.get());
873 pNodeInfo->setRect(*pRect);
876 #ifdef DBG_UTIL
877 SAL_INFO( "sw.ww8", pNodeInfo->toString());
878 #endif
879 return pNodeInfo;
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())
890 if (bCreate)
892 pResult = std::make_shared<ww8::WW8TableCellGrid>();
893 mCellGridMap[pTable] = pResult;
896 else
897 pResult = mCellGridMap[pTable];
899 return pResult;
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;
911 return pResult;
914 const SwNode * WW8TableInfo::getNextNode(const SwNode * pNode)
916 const SwNode * pResult = nullptr;
918 WW8TableNodeInfo::Pointer_t pNodeInfo = getTableNodeInfo(pNode);
920 if (pNodeInfo)
922 WW8TableNodeInfo * pNextInfo = pNodeInfo->getNext();
924 if (pNextInfo != nullptr)
925 pResult = pNextInfo->getNode();
926 else
928 const SwNode * pNextNode = pNodeInfo->getNextNode();
930 if (pNextNode != nullptr)
931 pResult = pNextNode;
935 return pResult;
938 bool WW8TableNodeInfo::operator < (const WW8TableNodeInfo & rInfo) const
940 bool bRet = false;
942 if (rInfo.mpNode != nullptr)
944 if (mpNode == nullptr)
946 bRet = true;
948 else
950 if (mpNode->GetIndex() < rInfo.mpNode->GetIndex())
951 bRet = true;
955 return bRet;
958 bool CellInfo::operator < (const CellInfo & aCellInfo) const
960 bool aRet = false;
962 if (top() < aCellInfo.top())
963 aRet = true;
964 else if (top() == aCellInfo.top())
966 if (left() < aCellInfo.left())
967 aRet = true;
968 else if (left() == aCellInfo.left())
970 if (width() < aCellInfo.width())
971 aRet = true;
972 else if (width() == aCellInfo.width())
974 if (height() < aCellInfo.height())
975 aRet = true;
976 else if (height() == aCellInfo.height())
978 if (aCellInfo.getTableNodeInfo())
980 if (m_pNodeInfo == nullptr)
981 aRet = true;
982 else
984 aRet = *m_pNodeInfo < *aCellInfo.getTableNodeInfo();
992 return aRet;
995 #ifdef DBG_UTIL
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 "\""
1005 " node=\"%p\"/>",
1006 sal_Int64(left()),
1007 sal_Int64(right()),
1008 sal_Int64(top()),
1009 sal_Int64(bottom()),
1010 m_pNodeInfo);
1012 return sBuffer;
1014 #endif
1016 WW8TableNodeInfo * WW8TableInfo::reorderByLayout(const SwTable * pTable, RowEndInners_t &rLastRowEnds)
1018 WW8TableCellGrid::Pointer_t pCellGrid = getCellGridForTable(pTable);
1020 #ifdef DBG_UTIL
1021 SAL_INFO( "sw.ww8", pCellGrid->toString());
1022 #endif
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())
1044 if (bCreate)
1046 pResult = std::make_shared<ww8::WW8TableCellGridRow>();
1047 m_aRows[nTop] = pResult;
1048 m_aRowTops.insert(nTop);
1051 else
1052 pResult = m_aRows[nTop];
1054 return pResult;
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);
1114 ++aRowSpanIt;
1116 if (aRowSpanIt != getRowTopsEnd() &&
1117 *aRowSpanIt < aCellIt->bottom())
1119 aRect.Top(*aRowSpanIt);
1120 tools::ULong nFormatFrameWidth = aCellIt->getFormatFrameWidth();
1121 insert(aRect, nullptr, &nFormatFrameWidth);
1123 bVertMerge = true;
1125 else
1126 bVertMerge = false;
1128 nRowSpan = 1;
1129 while (aRowSpanIt != getRowTopsEnd() &&
1130 *aRowSpanIt < aCellIt->bottom())
1132 ++aRowSpanIt;
1133 nRowSpan++;
1136 if (pNodeInfo)
1137 pRowSpans->push_back(nRowSpan);
1138 else
1139 pRowSpans->push_back(-nRowSpan);
1142 if (pNodeInfo)
1144 pNodeInfo->setVertMerge(bVertMerge);
1147 ++aCellIt;
1148 if (aCellIt != aCellEndIt)
1150 bBeginningOfCell = (aRect.Left() != aCellIt->left());
1151 aRect = aCellIt->getRect();
1155 WW8TableCellGridRow::Pointer_t pRow = getRow(*aTopsIt);
1156 if (pRow)
1157 pRow->setRowSpans(pRowSpans);
1159 ++aTopsIt;
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();
1186 if (pNodeInfo)
1188 const SwNode * pNode = pNodeInfo->getNode();
1190 if (pNode->IsStartNode())
1192 nDepthInCell++;
1193 pEndOfCellInfo = nullptr;
1196 if (nDepthInCell == 1 && pNode->IsTextNode())
1197 pEndOfCellInfo = pNodeInfo;
1199 pNodeInfo->setShadowsBefore(nShadows);
1200 pNodeInfo->setCell(nCell);
1201 pNodeInfo->setRow(nRow);
1202 if (pLastNodeInfo)
1204 pLastNodeInfo->setNext(pNodeInfo);
1205 pLastNodeInfo->setNextNode(pNode);
1207 pLastNodeInfo = pNodeInfo;
1208 nShadows = 0;
1210 if (pNode->IsEndNode())
1212 nDepthInCell--;
1214 if (nDepthInCell == 0 && !pEndOfCellInfo)
1215 pEndOfCellInfo = pNodeInfo;
1218 else
1220 nShadows++;
1223 if (bBeginningOfCell)
1225 pWidths->push_back(aCellIt->getFormatFrameWidth());
1227 if (pNodeInfo)
1228 pTableBoxes->push_back(pNodeInfo->getTableBox());
1229 else
1230 pTableBoxes->push_back(nullptr);
1233 ++aCellIt;
1234 bBeginningOfCell = false;
1236 if (aCellIt != aCellEndIt && aCellIt->left() != nCellX)
1238 nCell++;
1239 bBeginningOfCell = true;
1241 if (pEndOfCellInfo)
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);
1265 ++aTopsIt;
1266 nRow++;
1269 return pLastNodeInfo;
1272 #ifdef DBG_UTIL
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));
1282 sResult += sBuffer;
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()));
1291 sResult += sBuffer;
1293 WW8TableNodeInfo * pInfo = aCellIt->getTableNodeInfo();
1294 if (pInfo)
1295 sResult += pInfo->toString();
1296 else
1297 sResult += "<shadow/>\n";
1299 sResult += "</cellInfo>\n";
1300 ++aCellIt;
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();
1311 aIt != aItEnd;
1312 ++aIt)
1314 if (aIt != pWidths->begin())
1315 sResult += ", ";
1317 snprintf(sBuffer, sizeof(sBuffer), "%" SAL_PRIxUINT32 "", *aIt);
1318 sResult += sBuffer;
1321 sResult += "</widths>";
1324 RowSpansPtr pRowSpans = pRow->getRowSpans();
1325 if (pRowSpans)
1327 sResult += "<rowspans>";
1329 RowSpans::const_iterator aItEnd = pRowSpans->end();
1330 for (RowSpans::const_iterator aIt = pRowSpans->begin();
1331 aIt != aItEnd;
1332 ++aIt)
1334 if (aIt != pRowSpans->begin())
1335 sResult += ", ";
1337 snprintf(sBuffer, sizeof(sBuffer), "%" SAL_PRIxUINT32 "", *aIt);
1338 sResult += sBuffer;
1341 sResult += "</rowspans>";
1344 sResult += "</row>\n";
1345 ++aTopsIt;
1348 sResult += "</WW8TableCellGrid>\n";
1350 return sResult;
1352 #endif
1354 TableBoxVectorPtr WW8TableCellGrid::getTableBoxesOfRow
1355 (WW8TableNodeInfoInner const * pNodeInfoInner)
1357 TableBoxVectorPtr pResult;
1358 WW8TableCellGridRow::Pointer_t pRow =
1359 getRow(pNodeInfoInner->getRect().Top(), false);
1361 if (pRow)
1363 pResult = pRow->getTableBoxVector();
1366 return pResult;
1369 WidthsPtr WW8TableCellGrid::getWidthsOfRow
1370 (WW8TableNodeInfoInner const * pNodeInfoInner)
1372 GridColsPtr pResult;
1374 WW8TableCellGridRow::Pointer_t pRow =
1375 getRow(pNodeInfoInner->getRect().Top(), false);
1377 if (pRow)
1379 pResult = pRow->getWidths();
1382 return pResult;
1385 RowSpansPtr WW8TableCellGrid::getRowSpansOfRow
1386 (WW8TableNodeInfoInner const * pNodeInfoInner)
1388 RowSpansPtr pResult;
1390 WW8TableCellGridRow::Pointer_t pRow =
1391 getRow(pNodeInfoInner->getRect().Top(), false);
1393 if (pRow)
1395 pResult = pRow->getRowSpans();
1398 return pResult;
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);
1414 #ifdef DBG_UTIL
1415 SAL_INFO( "sw.ww8", "<gridRowInsert>" << rCellInfo.toString() << "</gridRowInsert>" );
1416 #endif
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: */