1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef WW8_TABLE_INFO_HXX
25 #define WW8_TABLE_INFO_HXX
26 #include <hash_map>
27 #include <string>
28 #include <map>
29 #include <vector>
30 #include <functional>
31 #include <boost/shared_ptr.hpp>
32 #include <sal/types.h>
33 #include <swrect.hxx>
34 
35 class SwTable;
36 class SwTableLine;
37 class SwTableBox;
38 class SwNode;
39 class SwWW8Writer;
40 class AttributeOutputBase;
41 
42 namespace ww8
43 {
44 using namespace ::std;
45 
46 const unsigned int MAXTABLECELLS = 63;
47 
48 class WW8TableNodeInfo;
49 typedef boost::shared_ptr<SwRect> SwRectPtr;
50 typedef ::std::vector<const SwTableBox *> TableBoxVector;
51 typedef boost::shared_ptr<TableBoxVector> TableBoxVectorPtr;
52 typedef ::std::vector<sal_uInt32> GridCols;
53 typedef boost::shared_ptr<GridCols> GridColsPtr;
54 typedef ::std::vector<sal_Int32> RowSpans;
55 typedef boost::shared_ptr<RowSpans> RowSpansPtr;
56 typedef ::std::vector<sal_uInt32> Widths;
57 typedef boost::shared_ptr<Widths> WidthsPtr;
58 
59 class WW8TableNodeInfoInner
60 {
61     WW8TableNodeInfo * mpParent;
62     sal_uInt32 mnDepth;
63     sal_uInt32 mnCell;
64     sal_uInt32 mnRow;
65     sal_uInt32 mnShadowsBefore;
66     sal_uInt32 mnShadowsAfter;
67     bool mbEndOfLine;
68     bool mbEndOfCell;
69     bool mbFirstInTable;
70     bool mbVertMerge;
71     const SwTableBox * mpTableBox;
72     const SwTable * mpTable;
73     SwRect maRect;
74 
75 public:
76     typedef boost::shared_ptr<WW8TableNodeInfoInner> Pointer_t;
77 
78     WW8TableNodeInfoInner(WW8TableNodeInfo * pParent);
79     ~WW8TableNodeInfoInner();
80 
81     void setDepth(sal_uInt32 nDepth);
82     void setCell(sal_uInt32 nCell);
83     void setRow(sal_uInt32 nRow);
84     void setShadowsBefore(sal_uInt32 nShadowsBefore);
85     void setShadowsAfter(sal_uInt32 nShadowsAfter);
86     void setEndOfLine(bool bEndOfLine);
87     void setEndOfCell(bool bEndOfCell);
88     void setFirstInTable(bool bFirstInTable);
89     void setVertMerge(bool bVertMErge);
90     void setTableBox(const SwTableBox * pTableBox);
91     void setTable(const SwTable * pTable);
92     void setRect(const SwRect & rRect);
93 
94     sal_uInt32 getDepth() const;
95     sal_uInt32 getCell() const;
96     sal_uInt32 getRow() const;
97     sal_uInt32 getShadowsBefore() const;
98     sal_uInt32 getShadowsAfter() const;
99     bool isEndOfCell() const;
100     bool isEndOfLine() const;
101     bool isFirstInTable() const;
102     bool isVertMerge() const;
103     const SwTableBox * getTableBox() const;
104     const SwTable * getTable() const;
105     const SwRect & getRect() const;
106 
107     const SwNode * getNode() const;
108 
109     TableBoxVectorPtr getTableBoxesOfRow();
110     WidthsPtr getWidthsOfRow();
111     GridColsPtr getGridColsOfRow(AttributeOutputBase & rBase);
112     RowSpansPtr getRowSpansOfRow();
113 
114     string toString() const;
115 };
116 
117 class CellInfo
118 {
119     SwRect m_aRect;
120     WW8TableNodeInfo * m_pNodeInfo;
121     unsigned long m_nFmtFrmWidth;
122 
123 public:
124     CellInfo(const SwRect & aRect, WW8TableNodeInfo * pNodeInfo);
125 
CellInfo(const CellInfo & aRectAndTableInfo)126     CellInfo(const CellInfo & aRectAndTableInfo)
127         : m_aRect(aRectAndTableInfo.m_aRect),
128           m_pNodeInfo(aRectAndTableInfo.m_pNodeInfo),
129           m_nFmtFrmWidth(aRectAndTableInfo.m_nFmtFrmWidth)
130     {
131     }
132 
~CellInfo()133     ~CellInfo() {}
134 
135     bool operator < (const CellInfo & aCellInfo) const;
136 
top() const137     long top() const { return m_aRect.Top(); }
bottom() const138     long bottom() const { return m_aRect.Bottom(); }
left() const139     long left() const { return m_aRect.Left(); }
right() const140     long right() const { return m_aRect.Right(); }
width() const141     long width() const { return m_aRect.Width(); }
height() const142     long height() const { return m_aRect.Height(); }
getRect() const143     SwRect getRect() const { return m_aRect; }
getTableNodeInfo() const144     WW8TableNodeInfo * getTableNodeInfo() const
145     { return m_pNodeInfo; }
getFmtFrmWidth() const146     unsigned long getFmtFrmWidth() const
147     {
148         return m_nFmtFrmWidth;
149     }
150 
setFmtFrmWidth(unsigned long nFmtFrmWidth)151     void setFmtFrmWidth(unsigned long nFmtFrmWidth)
152     {
153         m_nFmtFrmWidth = nFmtFrmWidth;
154     }
155 
156     ::std::string toString() const;
157 };
158 
159 typedef ::std::multiset<CellInfo, less<CellInfo> > CellInfoMultiSet;
160 typedef boost::shared_ptr<CellInfoMultiSet> CellInfoMultiSetPtr;
161 
162 class WW8TableInfo;
163 class WW8TableNodeInfo
164 {
165 public:
166     typedef map<sal_uInt32, WW8TableNodeInfoInner::Pointer_t,
167                 greater<sal_uInt32> > Inners_t;
168 
169 private:
170     WW8TableInfo * 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 boost::shared_ptr<WW8TableNodeInfo> Pointer_t;
179 
180     WW8TableNodeInfo(WW8TableInfo * pParent, const SwNode * pTxtNode);
181     virtual ~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 
198     WW8TableInfo * getParent() const;
199     sal_uInt32 getDepth() const;
200     bool isEndOfLine() const;
201     bool isEndOfCell() const;
202     bool isFirstInTable() const;
203     const SwNode * getNode() const;
204     const SwTableBox * getTableBox() const;
205     const SwTable * getTable() const;
206     WW8TableNodeInfo * getNext() const;
207     const SwNode * getNextNode() const;
208     const SwRect & getRect() const;
209 
210     const Inners_t & getInners() const;
211     const WW8TableNodeInfoInner::Pointer_t getFirstInner() const;
212     const WW8TableNodeInfoInner::Pointer_t getInnerForDepth(sal_uInt32 nDepth) const;
213 
214     sal_uInt32 getCell() const;
215     sal_uInt32 getRow() const;
216 
217     ::std::string toString() const;
218 
219     bool operator < (const WW8TableNodeInfo & rInfo) const;
220 };
221 
222 struct hashNode
223 {
operator ()ww8::hashNode224     size_t operator()(const SwNode * pNode) const
225     { return reinterpret_cast<size_t>(pNode); }
226 };
227 
228 struct hashTable
229 {
operator ()ww8::hashTable230     size_t operator()(const SwTable * pTable) const
231     { return reinterpret_cast<size_t>(pTable); }
232 };
233 
234 class WW8TableCellGridRow
235 {
236     CellInfoMultiSetPtr m_pCellInfos;
237     TableBoxVectorPtr m_pTableBoxVector;
238     WidthsPtr m_pWidths;
239     RowSpansPtr m_pRowSpans;
240 
241 public:
242     typedef boost::shared_ptr<WW8TableCellGridRow> Pointer_t;
243     WW8TableCellGridRow();
244     ~WW8TableCellGridRow();
245 
246     void insert(const CellInfo & rCellInfo);
247     CellInfoMultiSet::const_iterator begin() const;
248     CellInfoMultiSet::const_iterator end() const;
249 
250     void setTableBoxVector(TableBoxVectorPtr pTableBoxVector);
251     void setWidths(WidthsPtr pGridCols);
252     void setRowSpans(RowSpansPtr pRowSpans);
253 
254     TableBoxVectorPtr getTableBoxVector() const;
255     WidthsPtr getWidths() const;
256     RowSpansPtr getRowSpans() const;
257 };
258 
259 class WW8TableCellGrid
260 {
261     typedef ::std::set<long> RowTops_t;
262     typedef ::std::map<long, WW8TableCellGridRow::Pointer_t> Rows_t;
263 
264     RowTops_t m_aRowTops;
265     Rows_t m_aRows;
266 
267     WW8TableCellGridRow::Pointer_t getRow(long nTop, bool bCreate = true);
268     RowTops_t::const_iterator getRowTopsBegin() const;
269     RowTops_t::const_iterator getRowTopsEnd() const;
270     CellInfoMultiSet::const_iterator getCellsBegin(long nTop);
271     CellInfoMultiSet::const_iterator getCellsEnd(long nTop);
272 
273 public:
274     typedef ::boost::shared_ptr<WW8TableCellGrid> Pointer_t;
275 
276     WW8TableCellGrid();
277     ~WW8TableCellGrid();
278 
279     void insert(const SwRect & rRect, WW8TableNodeInfo * pNodeInfo,
280                 unsigned long * pFmtFrmWidth = NULL);
281     void addShadowCells();
282     WW8TableNodeInfo * connectCells();
283 
284     string toString();
285 
286     TableBoxVectorPtr getTableBoxesOfRow(WW8TableNodeInfoInner * pNodeInfo);
287     WidthsPtr getWidthsOfRow(WW8TableNodeInfoInner * pNodeInfo);
288     RowSpansPtr getRowSpansOfRow(WW8TableNodeInfoInner * pNodeInfo);
289 };
290 
291 class WW8TableInfo
292 {
293     friend class WW8TableNodeInfoInner;
294     typedef hash_map<const SwNode *, WW8TableNodeInfo::Pointer_t, hashNode > Map_t;
295     Map_t mMap;
296 
297     typedef hash_map<const SwTable *, WW8TableCellGrid::Pointer_t, hashTable > CellGridMap_t;
298     CellGridMap_t mCellGridMap;
299 
300     typedef hash_map<const SwTable *, const SwNode *, hashTable > FirstInTableMap_t;
301     FirstInTableMap_t mFirstInTableMap;
302 
303     WW8TableNodeInfo *
304     processTableLine(const SwTable * pTable,
305                      const SwTableLine * pTableLine,
306                      sal_uInt32 nRow,
307                      sal_uInt32 nDepth, WW8TableNodeInfo * pPrev);
308 
309     WW8TableNodeInfo *
310     processTableBox(const SwTable * pTable,
311                     const SwTableBox * pTableBox,
312                     sal_uInt32 nRow,
313                     sal_uInt32 nCell,
314                     sal_uInt32 nDepth, bool bEndOfLine, WW8TableNodeInfo * pPrev);
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 * pRect = NULL);
332 
333     WW8TableCellGrid::Pointer_t getCellGridForTable(const SwTable * pTable,
334                                                     bool bCreate = true);
335 
336 public:
337     typedef boost::shared_ptr<WW8TableInfo> Pointer_t;
338 
339     WW8TableInfo();
340     virtual ~WW8TableInfo();
341 
342     void processSwTable(const SwTable * pTable);
343     WW8TableNodeInfo * processSwTableByLayout(const SwTable * pTable);
344     WW8TableNodeInfo::Pointer_t getTableNodeInfo(const SwNode * pNode);
345     const SwNode * getNextNode(const SwNode * pNode);
346     const WW8TableNodeInfo * getFirstTableNodeInfo() const;
347 
348     WW8TableNodeInfo * reorderByLayout(const SwTable * pTable);
349 };
350 
351 }
352 #endif // WW8_TABLE_INFO_HXX
353 
354