Debian Codegraph @

src/lib/MWAWTable.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 /*
35 * Structure to store and construct a table from an unstructured list
36 * of cell
37 *
38 */
39
40 #include <iomanip>
41 #include <iostream>
42 #include <map>
43 #include <set>
44 #include <sstream>
45
46 #include <librevenge/librevenge.h>
47
48 #include "MWAWCell.hxx"
49 #include "MWAWGraphicShape.hxx"
50 #include "MWAWGraphicStyle.hxx"
51 #include "MWAWListener.hxx"
52 #include "MWAWPosition.hxx"
53
54 #include "MWAWTable.hxx"
55
56 /** Internal: the structures of a MWAWTable */
57 namespace MWAWTableInternal
58 {
59 //! a comparaison structure used retrieve the rows and the columns
60 struct Compare {
61 //! constructor
62 explicit Compare(int dim)
63 : m_coord(dim)
64 {
65 }
66 //! small structure to define a cell point
67 struct Point {
68 Point(int wh, MWAWCell const *cell, int cellId)
69 : m_which(wh)
70 , m_cell(cell)
71 , m_cellId(cellId)
72 {
73 }
74 float getPos(int coord) const
75 {
76 if (m_which)
77 return m_cell->bdBox().max()[coord];
78 return m_cell->bdBox().min()[coord];
79 }
80 /** returns the cells size */
81 float getSize(int coord) const
82 {
83 return m_cell->bdBox().size()[coord];
84 }
85 /** the position of the point in the cell (0: LT, 1: RB) */
86 int m_which;
87 /** the cell */
88 MWAWCell const *m_cell;
89 //! the cell id ( used by compare)
90 int m_cellId;
91 };
92
93 //! comparaison function
94 bool operator()(Point const &c1, Point const &c2) const
95 {
96 float diffF = c1.getPos(m_coord)-c2.getPos(m_coord);
97 if (diffF < 0) return true;
98 if (diffF > 0) return false;
99 int diff = c2.m_which - c1.m_which;
100 if (diff) return (diff < 0);
101 diffF = c1.m_cell->bdBox().size()[m_coord]
102 - c2.m_cell->bdBox().size()[m_coord];
103 if (diffF < 0) return true;
104 if (diffF > 0) return false;
105 return c1.m_cellId < c2.m_cellId;
106 }
107
108 //! the coord to compare
109 int m_coord;
110 };
111 }
112
113 ////////////////////////////////////////////////////////////
114 // MWAWTable
115 ////////////////////////////////////////////////////////////
116
117 // destructor, ...
118 MWAWTable::~MWAWTable()
119 {
120 }
121
122 std::shared_ptr<MWAWCell> MWAWTable::get(int id)
123 {
124 if (id < 0 || id >= int(m_cellsList.size())) {
125 MWAW_DEBUG_MSG(("MWAWTable::get: cell %d does not exists\n",id));
126 return std::shared_ptr<MWAWCell>();
127 }
128 return m_cellsList[size_t(id)];
129 }
130
131 void MWAWTable::addTablePropertiesTo(librevenge::RVNGPropertyList &propList) const
132 {
133 switch (m_alignment) {
134 case Paragraph:
135 break;
136 case Left:
137 propList.insert("table:align", "left");
138 propList.insert("fo:margin-left", double(m_leftMargin), librevenge::RVNG_POINT);
139 break;
140 case Center:
141 propList.insert("table:align", "center");
142 break;
143 case Right:
144 propList.insert("table:align", "right");
145 propList.insert("fo:margin-right", double(m_rightMargin), librevenge::RVNG_POINT);
146 break;
147 #if !defined(__clang__)
148 default:
149 break;
150 #endif
151 }
152 if (mergeBorders())
153 propList.insert("table:border-model","collapsing");
154
155 size_t nCols = m_colsSize.size();
156 float tableWidth = 0;
157 librevenge::RVNGPropertyListVector columns;
158 for (size_t c = 0; c < nCols; ++c) {
159 librevenge::RVNGPropertyList column;
160 column.insert("style:column-width", double(m_colsSize[c]), librevenge::RVNG_POINT);
161 columns.append(column);
162 tableWidth += m_colsSize[c];
163 }
164 propList.insert("style:width", double(tableWidth), librevenge::RVNG_POINT);
165 propList.insert("librevenge:table-columns", columns);
166 }
167
168 ////////////////////////////////////////////////////////////
169 // send extra line
170 void MWAWTable::sendExtraLines(MWAWListenerPtr listener) const
171 {
172 if (!listener) {
173 MWAW_DEBUG_MSG(("MWAWTable::sendExtraLines: called without listener\n"));
174 return;
175 }
176 std::vector<float> rowsPos, columnsPos;
177 size_t nRows = m_rowsSize.size();
178 rowsPos.resize(nRows+1);
179 rowsPos[0] = 0;
180 for (size_t r = 0; r < nRows; ++r)
181 rowsPos[r+1] = rowsPos[r]+
182 float(m_rowsSize[r]<0?-m_rowsSize[r]:m_rowsSize[r]);
183 size_t nColumns = m_colsSize.size();
184 columnsPos.resize(nColumns+1);
185 columnsPos[0] = 0;
186 for (size_t c = 0; c < nColumns; ++c)
187 columnsPos[c+1] = columnsPos[c]+
188 float(m_colsSize[c]<0?-m_colsSize[c]:m_colsSize[c]);
189
190 for (const auto &c : m_cellsList) {
191 if (!c) continue;
192 MWAWCell const &cell=*c;
193 if (!cell.hasExtraLine())
194 continue;
195 MWAWVec2i const &pos=c->position();
196 MWAWVec2i const &span=c->numSpannedCells();
197 if (span[0] <= 0 || span[1] <= 0 || pos[0]+span[0] > static_cast<int>(nColumns) ||
198 pos[1]+span[1] > static_cast<int>(nRows))
199 continue;
200 MWAWBox2f box;
201 box.setMin(MWAWVec2f(columnsPos[size_t(pos[0])], rowsPos[size_t(pos[1])]));
202 box.setMax(MWAWVec2f(columnsPos[size_t(pos[0]+span[0])],
203 rowsPos[size_t(pos[1]+span[1])]));
204
205 MWAWBorder const &border=cell.extraLineType();
206 MWAWGraphicStyle pStyle;
207 pStyle.m_lineWidth=float(border.m_width);
208 pStyle.m_lineColor=border.m_color;
209
210 MWAWPosition lPos(box[0], box.size(), librevenge::RVNG_POINT);
211 lPos.setRelativePosition(MWAWPosition::Frame);
212 lPos.m_wrapping=MWAWPosition::WForeground;
213 lPos.setOrder(-1);
214 if (cell.extraLine()==MWAWCell::E_Cross || cell.extraLine()==MWAWCell::E_Line1)
215 listener->insertShape(lPos, MWAWGraphicShape::line(MWAWVec2f(0,0), box.size()), pStyle);
216 if (cell.extraLine()==MWAWCell::E_Cross || cell.extraLine()==MWAWCell::E_Line2)
217 listener->insertShape(lPos, MWAWGraphicShape::line(MWAWVec2f(0,box.size()[1]), MWAWVec2f(box.size()[0], 0)), pStyle);
218 }
219 }
220
221 ////////////////////////////////////////////////////////////
222 // build the table structure
223 bool MWAWTable::buildStructures()
224 {
225 if (m_setData&CellPositionBit)
226 return true;
227 if ((m_setData&BoxBit)==0) {
228 MWAW_DEBUG_MSG(("MWAWTable::buildStructures: can not reconstruct cellule position if their boxes are not set\n"));
229 return false;
230 }
231
232 size_t nCells = m_cellsList.size();
233 std::vector<float> listPositions[2];
234 for (int dim = 0; dim < 2; dim++) {
235 MWAWTableInternal::Compare compareFunction(dim);
236 std::set<MWAWTableInternal::Compare::Point,
237 MWAWTableInternal::Compare> set(compareFunction);
238 for (size_t c = 0; c < nCells; ++c) {
239 set.insert(MWAWTableInternal::Compare::Point(0, m_cellsList[c].get(), int(c)));
240 set.insert(MWAWTableInternal::Compare::Point(1, m_cellsList[c].get(), int(c)));
241 }
242
243 std::vector<float> positions;
244 float maxPosiblePos=0;
245 int actCell = -1;
246 for (auto const &it : set) {
247 float pos = it.getPos(dim);
248 if (actCell < 0 || pos > maxPosiblePos) {
249 actCell++;
250 positions.push_back(pos);
251 maxPosiblePos = pos+2.0f; // 2 pixel ok
252 }
253 if (it.m_which == 0 && it.getPos(dim)-2.0f < maxPosiblePos)
254 maxPosiblePos = (it.getPos(dim)+pos)/2.f;
255 }
256 listPositions[dim] = positions;
257 }
258 for (auto const &cell : m_cellsList) {
259 if (!cell) continue;
260 int cellPos[2], spanCell[2];
261 for (int dim = 0; dim < 2; dim++) {
262 float const pt[2] = { cell->bdBox().min()[dim],
263 cell->bdBox().max()[dim]
264 };
265 std::vector<float> const &pos = listPositions[dim];
266 size_t numPos = pos.size();
267 size_t i = 0;
268 while (i+1 < numPos && pos[i+1] < pt[0])
269 i++;
270 if (i+1 < numPos && (pos[i]+pos[i+1])/2 < pt[0])
271 i++;
272 if (i+1 > numPos) {
273 MWAW_DEBUG_MSG(("MWAWTable::buildStructures: impossible to find cell position !!!\n"));
274 return false;
275 }
276 cellPos[dim] = int(i);
277 while (i+1 < numPos && pos[i+1] < pt[1])
278 i++;
279 if (i+1 < numPos && (pos[i]+pos[i+1])/2 < pt[1])
280 i++;
281 spanCell[dim] = int(i)-cellPos[dim];
282 if (spanCell[dim]==0 &&
283 (cell->bdBox().size()[dim] < 0 || cell->bdBox().size()[dim] > 0)) {
284 MWAW_DEBUG_MSG(("MWAWTable::buildStructures: impossible to find span number !!!\n"));
285 return false;
286 }
287 if (spanCell[dim] > 1 &&
288 pos[size_t(cellPos[dim])]+2.0f > pos[size_t(cellPos[dim]+1)]) {
289 spanCell[dim]--;
290 cellPos[dim]++;
291 }
292 }
293 cell->setPosition(MWAWVec2i(cellPos[0], cellPos[1]));
294 cell->setNumSpannedCells(MWAWVec2i(spanCell[0], spanCell[1]));
295 }
296 m_setData |= CellPositionBit;
297 // finally update the row/col size
298 for (int dim = 0; dim < 2; dim++) {
299 std::vector<float> const &pos = listPositions[dim];
300 size_t numPos = pos.size();
301 if (!numPos) continue;
302 std::vector<float> &res = (dim==0) ? m_colsSize : m_rowsSize;
303 res.resize(numPos-1);
304 for (size_t i = 0; i < numPos-1; i++)
305 res[i] = pos[i+1]-pos[i];
306 }
307 m_setData |= TableDimBit;
308 return true;
309 }
310
311
312 bool MWAWTable::buildPosToCellId()
313 {
314 if (m_setData&TablePosToCellBit)
315 return true;
316 if ((m_setData&CellPositionBit)==0) {
317 MWAW_DEBUG_MSG(("MWAWTable::buildPosToCellId: can not reconstruct cellule position if their boxes are not set\n"));
318 return false;
319 }
320 m_posToCellId.resize(0);
321
322 size_t nCells = m_cellsList.size();
323 m_numRows=(m_setData&TableDimBit) ? m_rowsSize.size() : 0;
324 m_numCols=(m_setData&TableDimBit) ? m_colsSize.size() : 0;
325 if ((m_setData&TableDimBit)==0) {
326 // m_numCols, m_numRows is not updated, we must compute it
327 m_numCols = 0;
328 m_numRows = 0;
329 for (auto const &cell : m_cellsList) {
330 if (!cell) continue;
331 MWAWVec2i const &lastPos=cell->position() + cell->numSpannedCells();
332 if (lastPos[0]>int(m_numCols)) m_numCols=size_t(lastPos[0]);
333 if (lastPos[1]>int(m_numRows)) m_numRows=size_t(lastPos[1]);
334 }
335 }
336 if (!m_numCols || !m_numRows)
337 return false;
338 m_posToCellId.resize(m_numCols*m_numRows, -1);
339 for (size_t c = 0; c < nCells; ++c) {
340 if (!m_cellsList[c]) continue;
341 if (m_cellsList[c]->hasExtraLine())
342 m_hasExtraLines=true;
343
344 MWAWVec2i const &pos=m_cellsList[c]->position();
345 MWAWVec2i lastPos=pos+m_cellsList[c]->numSpannedCells();
346 for (int x = pos[0]; x < lastPos[0]; x++) {
347 for (int y = pos[1]; y < lastPos[1]; y++) {
348 int tablePos = getCellIdPos(x,y);
349 if (tablePos<0) {
350 MWAW_DEBUG_MSG(("MWAWTable::buildPosToCellId: the position is bad!!!\n"));
351 return false;
352 }
353 if (m_posToCellId[size_t(tablePos)] != -1) {
354 MWAW_DEBUG_MSG(("MWAWTable::buildPosToCellId: cells is used!!!\n"));
355 return false;
356 }
357 if (x == pos[0] && y == pos[1])
358 m_posToCellId[size_t(tablePos)] = int(c);
359 else
360 m_posToCellId[size_t(tablePos)] = -2;
361 }
362 }
363 }
364 m_setData |= TablePosToCellBit;
365 return true;
366 }
367
368 bool MWAWTable::buildDims()
369 {
370 if (m_setData&TableDimBit)
371 return true;
372 if ((m_setData&CellPositionBit)==0)
373 return false;
374 if ((m_setData&BoxBit)==0 && (m_setData&SizeBit)==0) {
375 MWAW_DEBUG_MSG(("MWAWTable::buildDims: not enough information to reconstruct dimension\n"));
376 return false;
377 }
378
379 if (m_numRows==0 || m_numCols==0) {
380 MWAW_DEBUG_MSG(("MWAWTable::buildDims: can not compute the number of columns/row\n"));
381 return false;
382 }
383
384 std::vector<float> colLimit(m_numCols+1,0);
385 std::vector<bool> isFixed(m_numCols+1, (m_setData&BoxBit));
386 for (int c = 0; c < int(m_numCols); ++c) {
387 for (int r = 0; r < int(m_numRows); ++r) {
388 int cPos = getCellIdPos(c, r);
389 if (cPos<0 || m_posToCellId[size_t(cPos)]<0) continue;
390 auto cell=m_cellsList[size_t(m_posToCellId[size_t(cPos)])];
391 if (!cell) continue;
392 MWAWVec2i const &pos=cell->position();
393 MWAWVec2i lastPos=pos+cell->numSpannedCells();
394 if (m_setData&BoxBit) {
395 colLimit[size_t(pos[0])] = cell->bdBox()[0][0];
396 colLimit[size_t(lastPos[0])] = cell->bdBox()[1][0];
397 }
398 else if (cell->bdSize()[0]>=0) {
399 colLimit[size_t(lastPos[0])] = colLimit[size_t(pos[0])]+cell->bdSize()[0];
400 isFixed[size_t(lastPos[0])]=true;
401 }
402 else if (!isFixed[size_t(lastPos[0])])
403 colLimit[size_t(lastPos[0])] = colLimit[size_t(pos[0])]-cell->bdSize()[0];
404 }
405 if (colLimit[size_t(c)+1]<=colLimit[size_t(c)]) {
406 MWAW_DEBUG_MSG(("MWAWTable::buildDims: oops can not find the size of col %d\n", c));
407 return false;
408 }
409 }
410 m_colsSize.resize(m_numCols);
411 for (size_t c = 0; c < m_numCols; ++c) {
412 if (isFixed[c+1])
413 m_colsSize[c]=colLimit[c+1]-colLimit[c];
414 else
415 m_colsSize[c]=colLimit[c]-colLimit[c+1];
416 }
417
418 std::vector<float> rowLimit(m_numRows+1,0);
419 isFixed.resize(0);
420 isFixed.resize(m_numRows+1,(m_setData&BoxBit));
421 for (int r = 0; r < int(m_numRows); ++r) {
422 for (int c = 0; c < int(m_numCols); ++c) {
423 int cPos = getCellIdPos(c, r);
424 if (cPos<0 || m_posToCellId[size_t(cPos)]<0) continue;
425 auto cell=m_cellsList[size_t(m_posToCellId[size_t(cPos)])];
426 if (!cell) continue;
427 MWAWVec2i const &pos=cell->position();
428 MWAWVec2i lastPos=pos+cell->numSpannedCells();
429 if (m_setData&BoxBit) {
430 rowLimit[size_t(pos[1])] = cell->bdBox()[0][1];
431 rowLimit[size_t(lastPos[1])] = cell->bdBox()[1][1];
432 }
433 else if (cell->bdSize()[1]>=0) {
434 rowLimit[size_t(lastPos[1])] = rowLimit[size_t(pos[1])]+cell->bdSize()[1];
435 isFixed[size_t(lastPos[1])]=true;
436 }
437 else if (!isFixed[size_t(lastPos[1])])
438 rowLimit[size_t(lastPos[1])] = rowLimit[size_t(pos[1])]-cell->bdSize()[1];
439 }
440 if (rowLimit[size_t(r)+1]<=rowLimit[size_t(r)]) {
441 MWAW_DEBUG_MSG(("MWAWTable::buildDims: oops can not find the size of row %d\n", r));
442 return false;
443 }
444 }
445 m_rowsSize.resize(m_numRows);
446 for (size_t r = 0; r < m_numRows; ++r) {
447 if (isFixed[r+1])
448 m_rowsSize[r]=rowLimit[r+1]-rowLimit[r];
449 else
450 m_rowsSize[r]=rowLimit[r]-rowLimit[r+1];
451 }
452 m_setData |= TableDimBit;
453 return true;
454 }
455
456 ////////////////////////////////////////////////////////////
457 // try to send the table
458 bool MWAWTable::updateTable()
459 {
460 if ((m_setData&CellPositionBit)==0 && !buildStructures())
461 return false;
462 if ((m_setData&TablePosToCellBit)==0 && !buildPosToCellId())
463 return false;
464 if (!m_numCols || !m_numRows)
465 return false;
466 if ((m_givenData&TableDimBit)==0 && !buildDims())
467 return false;
468 return true;
469 }
470
471 bool MWAWTable::sendTable(MWAWListenerPtr listener, bool inFrame)
472 {
473 if (!updateTable())
474 return false;
475 if (!listener)
476 return true;
477 listener->openTable(*this);
478 for (size_t r = 0; r < m_numRows; ++r) {
479 listener->openTableRow(m_rowsSize[r], librevenge::RVNG_POINT);
480 for (size_t c = 0; c < m_numCols; ++c) {
481 int tablePos = getCellIdPos(int(c), int(r));
482 if (tablePos<0)
483 continue;
484 int id = m_posToCellId[size_t(tablePos)];
485 if (id == -1)
486 listener->addEmptyTableCell(MWAWVec2i(int(c), int(r)));
487 if (id < 0) continue;
488 m_cellsList[size_t(id)]->send(listener, *this);
489 }
490 listener->closeTableRow();
491 }
492 listener->closeTable();
493 if (inFrame && m_hasExtraLines)
494 sendExtraLines(listener);
495
496 return true;
497 }
498
499
500 ////////////////////////////////////////////////////////////
501 // try to send the table
502 bool MWAWTable::sendAsText(MWAWListenerPtr listener)
503 {
504 if (!listener) return true;
505
506 for (auto const &cell : m_cellsList) {
507 if (!cell) continue;
508 cell->sendContent(listener, *this);
509 listener->insertEOL();
510 }
511 return true;
512 }