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 #ifndef INCLUDED_SW_SOURCE_FILTER_WW8_WW8TABLEINFO_HXX
21 #define INCLUDED_SW_SOURCE_FILTER_WW8_WW8TABLEINFO_HXX
22 #include <string>
23 #include <map>
24 #include <memory>
25 #include <set>
26 #include <functional>
27 #include <unordered_map>
28 #include <vector>
29 #include <sal/types.h>
30 #include <swrect.hxx>
31 
32 class SwTable;
33 class SwTableLine;
34 class SwTableBox;
35 class SwNode;
36 class AttributeOutputBase;
37 
38 namespace ww8
39 {
40 const unsigned int MAXTABLECELLS = 63;
41 
42 class WW8TableNodeInfo;
43 typedef std::vector<const SwTableBox *> TableBoxVector;
44 typedef std::shared_ptr<TableBoxVector> TableBoxVectorPtr;
45 typedef std::vector<sal_uInt32> GridCols;
46 typedef std::shared_ptr<GridCols> GridColsPtr;
47 typedef std::vector<sal_Int32> RowSpans;
48 typedef std::shared_ptr<RowSpans> RowSpansPtr;
49 typedef std::vector<sal_uInt32> Widths;
50 typedef std::shared_ptr<Widths> WidthsPtr;
51 
52 class WW8TableNodeInfoInner
53 {
54     WW8TableNodeInfo * mpParent;
55     sal_uInt32 mnDepth;
56     sal_uInt32 mnCell;
57     sal_uInt32 mnRow;
58     sal_uInt32 mnShadowsBefore;
59     sal_uInt32 mnShadowsAfter;
60     bool mbEndOfLine;
61     bool mbFinalEndOfLine;
62     bool mbEndOfCell;
63     bool mbFirstInTable;
64     bool mbVertMerge;
65     const SwTableBox * mpTableBox;
66     const SwTable * mpTable;
67     SwRect maRect;
68 
69 public:
70     typedef std::shared_ptr<WW8TableNodeInfoInner> Pointer_t;
71 
72     explicit WW8TableNodeInfoInner(WW8TableNodeInfo * pParent);
73     ~WW8TableNodeInfoInner();
74 
75     void setDepth(sal_uInt32 nDepth);
76     void setCell(sal_uInt32 nCell);
77     void setRow(sal_uInt32 nRow);
78     void setShadowsBefore(sal_uInt32 nShadowsBefore);
79     void setShadowsAfter(sal_uInt32 nShadowsAfter);
80     void setEndOfLine(bool bEndOfLine);
81     void setFinalEndOfLine(bool bEndOfLine);
82     void setEndOfCell(bool bEndOfCell);
83     void setFirstInTable(bool bFirstInTable);
84     void setVertMerge(bool bVertMerge);
85     void setTableBox(const SwTableBox *pTableBox);
86     void setTable(const SwTable * pTable);
87     void setRect(const SwRect & rRect);
88 
getDepth() const89     sal_uInt32 getDepth() const { return mnDepth;}
getCell() const90     sal_uInt32 getCell() const { return mnCell;}
getRow() const91     sal_uInt32 getRow() const { return mnRow;}
getShadowsBefore() const92     sal_uInt32 getShadowsBefore() const { return mnShadowsBefore;}
getShadowsAfter() const93     sal_uInt32 getShadowsAfter() const { return mnShadowsAfter;}
isEndOfCell() const94     bool isEndOfCell() const { return mbEndOfCell;}
isEndOfLine() const95     bool isEndOfLine() const { return mbEndOfLine;}
isFinalEndOfLine() const96     bool isFinalEndOfLine() const { return mbFinalEndOfLine;}
isFirstInTable() const97     bool isFirstInTable() const { return mbFirstInTable;}
getTableBox() const98     const SwTableBox * getTableBox() const { return mpTableBox;}
getTable() const99     const SwTable * getTable() const { return mpTable;}
getRect() const100     const SwRect & getRect() const { return maRect;}
101 
102     const SwNode * getNode() const;
103 
104     TableBoxVectorPtr getTableBoxesOfRow() const;
105     WidthsPtr getWidthsOfRow() const;
106     WidthsPtr getColumnWidthsBasedOnAllRows() const;
107     GridColsPtr getGridColsOfRow(AttributeOutputBase & rBase, bool calculateColumnsFromAllRows = false);
108     RowSpansPtr getRowSpansOfRow() const;
109 
110 #ifdef DBG_UTIL
111     std::string toString() const;
112 #endif
113 };
114 
115 class CellInfo
116 {
117     SwRect const m_aRect;
118     WW8TableNodeInfo * const m_pNodeInfo;
119     unsigned long m_nFormatFrameWidth;
120 
121 public:
122     CellInfo(const SwRect & aRect, WW8TableNodeInfo * pNodeInfo);
123 
CellInfo(const CellInfo & aRectAndTableInfo)124     CellInfo(const CellInfo & aRectAndTableInfo)
125         : m_aRect(aRectAndTableInfo.m_aRect),
126           m_pNodeInfo(aRectAndTableInfo.m_pNodeInfo),
127           m_nFormatFrameWidth(aRectAndTableInfo.m_nFormatFrameWidth)
128     {
129     }
130 
131     bool operator < (const CellInfo & aCellInfo) const;
132 
top() const133     long top() const { return m_aRect.Top(); }
bottom() const134     long bottom() const { return m_aRect.Bottom(); }
left() const135     long left() const { return m_aRect.Left(); }
right() const136     long right() const { return m_aRect.Right(); }
width() const137     long width() const { return m_aRect.Width(); }
height() const138     long height() const { return m_aRect.Height(); }
getRect() const139     const SwRect& getRect() const { return m_aRect; }
getTableNodeInfo() const140     WW8TableNodeInfo * getTableNodeInfo() const
141     { return m_pNodeInfo; }
getFormatFrameWidth() const142     unsigned long getFormatFrameWidth() const
143     {
144         return m_nFormatFrameWidth;
145     }
146 
setFormatFrameWidth(unsigned long nFormatFrameWidth)147     void setFormatFrameWidth(unsigned long nFormatFrameWidth)
148     {
149         m_nFormatFrameWidth = nFormatFrameWidth;
150     }
151 
152 #ifdef DBG_UTIL
153     std::string toString() const;
154 #endif
155 };
156 
157 typedef std::multiset<CellInfo> CellInfoMultiSet;
158 typedef std::map<sal_uInt32, WW8TableNodeInfoInner*,
159             std::greater<sal_uInt32> > RowEndInners_t;
160 
161 
162 class WW8TableInfo;
163 class WW8TableNodeInfo final
164 {
165 public:
166     typedef std::map<sal_uInt32, WW8TableNodeInfoInner::Pointer_t,
167                 std::greater<sal_uInt32> > Inners_t;
168 
169 private:
170     WW8TableInfo * const mpParent;
171     sal_uInt32 mnDepth;
172     const SwNode * mpNode;
173     Inners_t mInners;
174     WW8TableNodeInfo * mpNext;
175     const SwNode * mpNextNode;
176 
177 public:
178     typedef std::shared_ptr<WW8TableNodeInfo> Pointer_t;
179 
180     WW8TableNodeInfo(WW8TableInfo * pParent, const SwNode * pTextNode);
181     ~WW8TableNodeInfo();
182 
183     void setDepth(sal_uInt32 nDepth);
184     void setEndOfLine(bool bEndOfLine);
185     void setEndOfCell(bool bEndOfCell);
186     void setFirstInTable(bool bFirstInTable);
187     void setVertMerge(bool bVertMerge);
188     void setTableBox(const SwTableBox *pTableBox);
189     void setTable(const SwTable * pTable);
190     void setCell(sal_uInt32 nCell);
191     void setRow(sal_uInt32 nRow);
192     void setShadowsBefore(sal_uInt32 nShadowsBefore);
193     void setShadowsAfter(sal_uInt32 nShadowsAfter);
194     void setNext(WW8TableNodeInfo * pNext);
195     void setNextNode(const SwNode * pNode);
196     void setRect(const SwRect & rRect);
197 
getParent() const198     WW8TableInfo * getParent() const { return mpParent;}
199     sal_uInt32 getDepth() const;
getNode() const200     const SwNode * getNode() const { return mpNode;}
201     const SwTableBox * getTableBox() const;
getNext() const202     WW8TableNodeInfo * getNext() const { return mpNext;}
getNextNode() const203     const SwNode * getNextNode() const { return mpNextNode;}
204 
getInners() const205     const Inners_t & getInners() const { return mInners;}
206     WW8TableNodeInfoInner::Pointer_t getFirstInner() const;
207     WW8TableNodeInfoInner::Pointer_t getInnerForDepth(sal_uInt32 nDepth) const;
208 
209     sal_uInt32 getCell() const;
210     sal_uInt32 getRow() const;
211 
212 #ifdef DBG_UTIL
213     std::string toString() const;
214 #endif
215 
216     bool operator < (const WW8TableNodeInfo & rInfo) const;
217 };
218 
219 struct hashNode
220 {
operator ()ww8::hashNode221     size_t operator()(const SwNode * pNode) const
222     { return reinterpret_cast<size_t>(pNode); }
223 };
224 
225 struct hashTable
226 {
operator ()ww8::hashTable227     size_t operator()(const SwTable * pTable) const
228     { return reinterpret_cast<size_t>(pTable); }
229 };
230 
231 class WW8TableCellGridRow
232 {
233     std::shared_ptr<CellInfoMultiSet> m_pCellInfos;
234     TableBoxVectorPtr m_pTableBoxVector;
235     WidthsPtr m_pWidths;
236     RowSpansPtr m_pRowSpans;
237 
238 public:
239     typedef std::shared_ptr<WW8TableCellGridRow> Pointer_t;
240     WW8TableCellGridRow();
241     ~WW8TableCellGridRow();
242 
243     void insert(const CellInfo & rCellInfo);
244     CellInfoMultiSet::const_iterator begin() const;
245     CellInfoMultiSet::const_iterator end() const;
246 
247     void setTableBoxVector(TableBoxVectorPtr const & pTableBoxVector);
248     void setWidths(WidthsPtr const & pGridCols);
249     void setRowSpans(RowSpansPtr const & pRowSpans);
250 
getTableBoxVector() const251     const TableBoxVectorPtr& getTableBoxVector() const { return m_pTableBoxVector;}
getWidths() const252     const WidthsPtr& getWidths() const { return m_pWidths;}
getRowSpans() const253     const RowSpansPtr& getRowSpans() const { return m_pRowSpans;}
254 };
255 
256 class WW8TableCellGrid
257 {
258     typedef std::set<long> RowTops_t;
259     typedef std::map<long, WW8TableCellGridRow::Pointer_t> Rows_t;
260 
261     RowTops_t m_aRowTops;
262     Rows_t m_aRows;
263 
264     WW8TableCellGridRow::Pointer_t getRow(long nTop, bool bCreate = true);
265     RowTops_t::const_iterator getRowTopsBegin() const;
266     RowTops_t::const_iterator getRowTopsEnd() const;
267     CellInfoMultiSet::const_iterator getCellsBegin(long nTop);
268     CellInfoMultiSet::const_iterator getCellsEnd(long nTop);
269 
270 public:
271     typedef std::shared_ptr<WW8TableCellGrid> Pointer_t;
272 
273     WW8TableCellGrid();
274     ~WW8TableCellGrid();
275 
276     void insert(const SwRect & rRect, WW8TableNodeInfo * pNodeInfo,
277                 unsigned long const * pFormatFrameWidth = nullptr);
278     void addShadowCells();
279     WW8TableNodeInfo *connectCells(RowEndInners_t &rLastRowEnds);
280 
281 #ifdef DBG_UTIL
282     std::string toString();
283 #endif
284 
285     TableBoxVectorPtr getTableBoxesOfRow(WW8TableNodeInfoInner const * pNodeInfo);
286     WidthsPtr getWidthsOfRow(WW8TableNodeInfoInner const * pNodeInfo);
287     RowSpansPtr getRowSpansOfRow(WW8TableNodeInfoInner const * pNodeInfo);
288 };
289 
290 class WW8TableInfo final
291 {
292     friend class WW8TableNodeInfoInner;
293     typedef std::unordered_map<const SwNode *, WW8TableNodeInfo::Pointer_t, hashNode > Map_t;
294     Map_t mMap;
295 
296     typedef std::unordered_map<const SwTable *, WW8TableCellGrid::Pointer_t, hashTable > CellGridMap_t;
297     CellGridMap_t mCellGridMap;
298 
299     typedef std::unordered_map<const SwTable *, const SwNode *, hashTable > FirstInTableMap_t;
300     FirstInTableMap_t mFirstInTableMap;
301 
302     WW8TableNodeInfo *
303     processTableLine(const SwTable * pTable,
304                      const SwTableLine * pTableLine,
305                      sal_uInt32 nRow,
306                      sal_uInt32 nDepth, WW8TableNodeInfo * pPrev, RowEndInners_t &rLastRowEnds);
307 
308     WW8TableNodeInfo *
309     processTableBox(const SwTable * pTable,
310                     const SwTableBox * pTableBox,
311                     sal_uInt32 nRow,
312                     sal_uInt32 nCell,
313                     sal_uInt32 nDepth, bool bEndOfLine,
314                     WW8TableNodeInfo * pPrev, RowEndInners_t &rLastRowEnds);
315 
316     WW8TableNodeInfo::Pointer_t
317     processTableBoxLines(const SwTableBox * pBox,
318                          const SwTable * pTable,
319                          const SwTableBox * pBoxToSet,
320                          sal_uInt32 nRow,
321                          sal_uInt32 nCell,
322                          sal_uInt32 nDepth);
323 
324     WW8TableNodeInfo::Pointer_t
325     insertTableNodeInfo(const SwNode * pNode,
326                         const SwTable * pTable,
327                         const SwTableBox * pTableBox,
328                         sal_uInt32 nRow,
329                         sal_uInt32 nCell,
330                         sal_uInt32 nDepth,
331                         SwRect const * pRect = nullptr);
332 
333     WW8TableCellGrid::Pointer_t getCellGridForTable(const SwTable * pTable,
334                                                     bool bCreate = true);
335 
336 public:
337     typedef std::shared_ptr<WW8TableInfo> Pointer_t;
338 
339     WW8TableInfo();
340     ~WW8TableInfo();
341 
342     void processSwTable(const SwTable * pTable);
343     WW8TableNodeInfo * processSwTableByLayout(const SwTable * pTable, RowEndInners_t &rLastRowEnds);
344     WW8TableNodeInfo::Pointer_t getTableNodeInfo(const SwNode * pNode);
345     const SwNode * getNextNode(const SwNode * pNode);
346 
347     WW8TableNodeInfo * reorderByLayout(const SwTable * pTable, RowEndInners_t &rLastRowEnds);
348 };
349 
350 }
351 #endif // INCLUDED_SW_SOURCE_FILTER_WW8_WW8TABLEINFO_HXX
352 
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
354