Debian Codegraph @

src/lib/WPSTable.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2.1+
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 * Major Contributor(s):
10 * Copyright (C) 2009, 2011 Alonso Laurent ([email protected])
11 * Copyright (C) 2006, 2007 Andrew Ziem
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 * Copyright (C) 2004 Marc Maurer ([email protected])
14 * Copyright (C) 2003-2005 William Lachance ([email protected])
15 *
16 * For minor contributions see the git repository.
17 *
18 * Alternatively, the contents of this file may be used under the terms
19 * of the GNU Lesser General Public License Version 2.1 or later
20 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
21 * applicable instead of those above.
22 *
23 * For further information visit http://libwps.sourceforge.net
24 */
25
26 /*
27 * Structure to store and construct a table from an unstructured list
28 * of cell
29 *
30 */
31
32 #include <iostream>
33 #include <set>
34
35 #include "WPSContentListener.h"
36 #include "WPSCell.h"
37
38 #include "WPSTable.h"
39
40 ////////////////////////////////////////////////////////////
41 // WPSColumnFormat
42 std::ostream &operator<<(std::ostream &o, WPSColumnFormat const &column)
43 {
44 if (column.m_width>=0)
45 {
46 if (column.m_isPercentWidth)
47 o<<"w=" << column.m_width << "%,";
48 else
49 o<<"w=" << column.m_width << ",";
50 }
51 if (column.m_useOptimalWidth) o << "optimal[h],";
52 if (column.m_isHeader) o << "table[header],";
53 if (column.m_numRepeat>1) o << "repeat=" << column.m_numRepeat << ",";
54 return o;
55 }
56
57 void WPSColumnFormat::addTo(librevenge::RVNGPropertyList &propList) const
58 {
59 if (m_width>=0)
60 propList.insert("style:column-width", double(m_width), m_isPercentWidth ? librevenge::RVNG_PERCENT : librevenge::RVNG_POINT);
61 if (m_useOptimalWidth)
62 propList.insert("style:use-optimal-column-width", true);
63 if (m_isHeader)
64 propList.insert("librevenge:is-header-column", true); // checkme
65 if (m_numRepeat>1)
66 propList.insert("table:number-columns-repeated", m_numRepeat);
67 }
68
69 ////////////////////////////////////////////////////////////
70 // WPSRowFormat
71 std::ostream &operator<<(std::ostream &o, WPSRowFormat const &row)
72 {
73 if (row.m_height>=0)
74 {
75 if (row.m_isMinimalHeight)
76 o<<"h[min]=" << row.m_height << ",";
77 else
78 o<<"h=" << row.m_height << ",";
79 }
80 if (row.m_useOptimalHeight) o << "optimal[h],";
81 if (row.m_isHeader) o << "table[header],";
82 return o;
83 }
84
85 void WPSRowFormat::addTo(librevenge::RVNGPropertyList &propList) const
86 {
87 if (m_height>=0)
88 {
89 if (m_isMinimalHeight)
90 propList.insert("style:min-row-height", double(m_height), librevenge::RVNG_POINT);
91 else
92 propList.insert("style:row-height", double(m_height), librevenge::RVNG_POINT);
93 }
94 if (m_useOptimalHeight)
95 propList.insert("style:use-optimal-row-height", true);
96 propList.insert("librevenge:is-header-row", m_isHeader);
97 }
98
99 ////////////////////////////////////////////////////////////
100 // destructor, ...
101 WPSTable::~WPSTable()
102 {
103 }
104
105 void WPSTable::add(WPSCellPtr &cell)
106 {
107 m_cellsList.push_back(cell);
108 }
109
110 WPSCellPtr WPSTable::getCell(int id)
111 {
112 if (id < 0 || id >= int(m_cellsList.size()))
113 {
114 WPS_DEBUG_MSG(("WPSTable::get: cell %d does not exists\n",id));
115 return std::shared_ptr<WPSCell>();
116 }
117 return m_cellsList[size_t(id)];
118 }
119
120 ////////////////////////////////////////////////////////////
121 // build the table structure
122 bool WPSTable::buildStructures()
123 {
124 if (m_colsSize.size())
125 return true;
126
127 size_t nCells = m_cellsList.size();
128 std::vector<float> listPositions[2];
129 for (int dim = 0; dim < 2; dim++)
130 {
131 WPSCell::Compare compareFunction(dim);
132 std::set<WPSCell::Compare::Point,
133 WPSCell::Compare> set(compareFunction);
134 for (size_t c = 0; c < nCells; c++)
135 {
136 set.insert(WPSCell::Compare::Point(0, m_cellsList[c].get()));
137 set.insert(WPSCell::Compare::Point(1, m_cellsList[c].get()));
138 }
139
140 std::vector<float> positions;
141 float maxPosiblePos=0;
142 int actCell = -1;
143 for (auto const &it : set)
144 {
145 float pos = it.getPos(dim);
146 if (actCell < 0 || pos > maxPosiblePos)
147 {
148 actCell++;
149 positions.push_back(pos);
150 maxPosiblePos = pos+2.0f; // 2 pixel ok
151 }
152 if (it.m_which == 0 && it.getPos(dim)-2.0f < maxPosiblePos)
153 maxPosiblePos = pos;
154 }
155 listPositions[dim] = positions;
156 }
157 std::vector<int> numYSet(listPositions[1].size(), 0);
158 std::vector<int> numYUnset(listPositions[1].size(), 0);
159 for (auto const &cell : m_cellsList)
160 {
161 int cellPos[2], spanCell[2];
162 for (int dim = 0; dim < 2; dim++)
163 {
164 float const pt[2] = { cell->box().min()[dim],
165 cell->box().max()[dim]
166 };
167 std::vector<float> const &pos = listPositions[dim];
168 size_t numPos = pos.size();
169 size_t i = 0;
170 while (i+1 < numPos && pos[i+1] < pt[0])
171 i++;
172 while (i+1 < numPos && (pos[i]+pos[i+1])/2 < pt[0])
173 i++;
174 if (i+1 > numPos)
175 {
176 WPS_DEBUG_MSG(("WPSTable::buildStructures: impossible to find cell position !!!\n"));
177 return false;
178 }
179 cellPos[dim] = int(i);
180 while (i+1 < numPos && pos[i+1] < pt[1])
181 i++;
182 if (i+1 < numPos && (pos[i]+pos[i+1])/2 < pt[1])
183 i++;
184 spanCell[dim] = int(i)-cellPos[dim];
185 if (spanCell[dim]==0 &&
186 (cell->box().size()[dim]<0||cell->box().size()[dim]>0))
187 {
188 WPS_DEBUG_MSG(("WPSTable::buildStructures: impossible to find span number !!!\n"));
189 return false;
190 }
191 if (spanCell[dim] > 1 &&
192 pos[size_t(cellPos[dim])]+2.0f > pos[size_t(cellPos[dim]+1)])
193 {
194 spanCell[dim]--;
195 cellPos[dim]++;
196 }
197 if (spanCell[dim] > 1 &&
198 pos[size_t(cellPos[dim])]+2.0f > pos[size_t(cellPos[dim]+1)])
199 {
200 spanCell[dim]--;
201 cellPos[dim]++;
202 }
203 }
204 cell->m_position = Vec2i(cellPos[0], cellPos[1]);
205 cell->m_numberCellSpanned = Vec2i(spanCell[0], spanCell[1]);
206 if (spanCell[1] > 0)
207 {
208 for (int x = cellPos[0]; x < cellPos[0]+spanCell[0]; x++)
209 {
210 if (cell->isVerticalSet())
211 numYSet[size_t(cellPos[1]+spanCell[1]-1)]++;
212 else
213 numYUnset[size_t(cellPos[1]+spanCell[1]-1)]++;
214 }
215 }
216 }
217 // finally update the row/col size
218 for (int dim = 0; dim < 2; dim++)
219 {
220 std::vector<float> const &pos = listPositions[dim];
221 size_t numPos = pos.size();
222 if (!numPos) continue;
223 std::vector<float> &res = (dim==0) ? m_colsSize : m_rowsSize;
224 res.resize(numPos-1);
225 for (size_t i = 0; i < numPos-1; i++)
226 {
227 if (dim==0 || numYUnset[i]==0)
228 res[i] = pos[i+1]-pos[i];
229 else if (numYSet[i])
230 res[i] = -(pos[i+1]-pos[i]);
231 else
232 res[i] = 0;
233 }
234 }
235
236 return true;
237 }
238
239 ////////////////////////////////////////////////////////////
240 // try to send the table
241 bool WPSTable::sendTable(WPSContentListenerPtr listener)
242 {
243 if (!buildStructures())
244 return false;
245 if (!listener)
246 return true;
247
248 size_t numCols = m_colsSize.size();
249 size_t numRows = m_rowsSize.size();
250 if (!numCols || !numRows)
251 return false;
252 std::vector<int> cellsId(numCols*numRows, -1);
253 size_t nCells = m_cellsList.size();
254 for (size_t c = 0; c < nCells; c++)
255 {
256 if (!m_cellsList[c]) continue;
257 Vec2i const &pos=m_cellsList[c]->m_position;
258 Vec2i const &span=m_cellsList[c]->m_numberCellSpanned;
259
260 for (int x = pos[0]; x < pos[0]+span[0]; x++)
261 {
262 if (x >= int(numCols))
263 {
264 WPS_DEBUG_MSG(("WPSTable::sendTable: x is too big !!!\n"));
265 return false;
266 }
267 for (int y = pos[1]; y < pos[1]+span[1]; y++)
268 {
269 if (y >= int(numRows))
270 {
271 WPS_DEBUG_MSG(("WPSTable::sendTable: y is too big !!!\n"));
272 return false;
273 }
274 auto tablePos = size_t(y*int(numCols)+x);
275 if (cellsId[tablePos] != -1)
276 {
277 WPS_DEBUG_MSG(("WPSTable::sendTable: cells is used!!!\n"));
278 return false;
279 }
280 if (x == pos[0] && y == pos[1])
281 cellsId[tablePos] = int(c);
282 else
283 cellsId[tablePos] = -2;
284 }
285 }
286 }
287
288 listener->openTable(m_colsSize, librevenge::RVNG_POINT);
289 WPSListenerPtr listen=listener;
290 for (size_t r = 0; r < numRows; r++)
291 {
292 listener->openTableRow(m_rowsSize[r], librevenge::RVNG_POINT);
293 for (size_t c = 0; c < numCols; c++)
294 {
295 size_t tablePos = r*numCols+c;
296 int id = cellsId[tablePos];
297 if (id == -1)
298 listener->addEmptyTableCell(Vec2i(int(c), int(r)));
299 if (id < 0)
300 continue;
301
302 m_cellsList[size_t(id)]->send(listen);
303 }
304 listener->closeTableRow();
305 }
306 listener->closeTable();
307
308 return true;
309 }
310
311
312 ////////////////////////////////////////////////////////////
313 // try to send the table
314 bool WPSTable::sendAsText(WPSContentListenerPtr listener)
315 {
316 if (!listener) return true;
317
318 WPSListenerPtr listen=listener;
319 for (auto &cell : m_cellsList)
320 {
321 if (!cell) continue;
322 cell->sendContent(listen);
323 listener->insertEOL();
324 }
325 return true;
326 }
327 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */