Debian Codegraph @

src/lib/WPS8Table.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 #include <iomanip>
27 #include <iostream>
28
29 #include <librevenge/librevenge.h>
30
31 #include "WPSCell.h"
32 #include "WPSContentListener.h"
33 #include "WPSEntry.h"
34 #include "WPSPosition.h"
35 #include "WPSTable.h"
36
37 #include "WPS8.h"
38 #include "WPS8Struct.h"
39
40 #include "WPS8Table.h"
41
42
43 /** Internal: the structures of a WPS8Table */
44 namespace WPS8TableInternal
45 {
46 /** Internal: class to store a basic cell with borders */
47 struct Cell final : public WPSCell
48 {
49 //! constructor
50 explicit Cell(WPS8Table &parser)
51 : WPSCell()
52 , m_tableParser(parser)
53 , m_id(-1)
54 , m_strsId(-1)
55 , m_size()
56 {
57 WPSBorder emptyBorder;
58 emptyBorder.m_style = WPSBorder::None;
59 m_bordersList.resize(4, emptyBorder);
60 for (float &i : m_bordersSep) i = 0.0;
61 }
62 //! virtual destructor
63 ~Cell() final;
64
65 //! operator<<
66 friend std::ostream &operator<<(std::ostream &o, Cell const &cell);
67 //! call when a cell must be send
68 bool send(WPSListenerPtr &listener) final
69 {
70 if (!listener) return true;
71 auto *listen=dynamic_cast<WPSContentListener *>(listener.get());
72 if (!listen)
73 {
74 WPS_DEBUG_MSG(("WPS8TableInternal::Cell::send: unexpected listener\n"));
75 return true;
76 }
77 listen->openTableCell(*this);
78 sendContent(listener);
79 listen->closeTableCell();
80 return true;
81 }
82
83 //! call when the content of a cell must be send
84 bool sendContent(WPSListenerPtr &) final
85 {
86 m_tableParser.sendTextInCell(m_strsId, m_id);
87 return true;
88 }
89
90 //! the actual parser
91 WPS8Table &m_tableParser;
92 //! the cell id
93 int m_id;
94 //! the strsId
95 mutable int m_strsId;
96 //! frame size in inches
97 Vec2f m_size;
98 //! border text separator T,L,R,B ( checkme, not sure )
99 float m_bordersSep[4];
100 };
101 Cell::~Cell()
102 {
103 }
104 //! operator<< for a Cell
105 std::ostream &operator<<(std::ostream &o, Cell const &cell)
106 {
107 o << reinterpret_cast<WPSCell const &>(cell);
108 if (cell.m_size.x() > 0 || cell.m_size.y() > 0) o << "size=" << cell.m_size << ",";
109 bool hasBSize = false;
110 for (float i : cell.m_bordersSep)
111 {
112 if (i <= 0)
113 continue;
114 hasBSize = true;
115 break;
116 }
117 if (hasBSize)
118 {
119 o << "borderSep?=[";
120 for (float i : cell.m_bordersSep)
121 if (i > 0) o << i << ",";
122 else o << "_,";
123 o << "],";
124 }
125 return o;
126 }
127
128 /** Internal: class to store a table: list of cells */
129 struct Table final : public WPSTable
130 {
131 //! constructor
132 Table()
133 : m_id(-1)
134 , m_parsed(false) {}
135 Table &operator=(Table const &)=default;
136 //! destructor
137 ~Table() final;
138 //! operator<<
139 friend std::ostream &operator<<(std::ostream &o, Table const &table);
140 //! the table id
141 int m_id;
142
143 //! a bool to know if the table has been parsed
144 mutable bool m_parsed;
145 };
146
147 Table::~Table()
148 {
149 }
150
151 //! operator<< for a Table
152 std::ostream &operator<<(std::ostream &o, Table const &table)
153 {
154 o << "id=" << table.m_id << ",";
155 for (int i = 0; i < table.numCells(); i++)
156 {
157 WPSCellPtr cell = const_cast<Table &>(table).getCell(i);
158 if (!cell) continue;
159 o << "cell" << i << "=[" << *reinterpret_cast<Cell const *>(cell.get()) << "],";
160 }
161 return o;
162 }
163
164 /** Internal: the state of a WPS8Table */
165 struct State
166 {
167 //! constructor
168 State()
169 : m_version(-1)
170 , m_numPages(0)
171 , m_tableMap()
172 , m_MCLDTypes()
173 {
174 initTypeMaps();
175 }
176
177 //! initialize the type map
178 void initTypeMaps();
179 //! the version
180 int m_version;
181 //! the number page
182 int m_numPages;
183 //! a map id -> table
184 std::map<int, Table> m_tableMap;
185 //! the MCLD type
186 std::map<int,int> m_MCLDTypes;
187 };
188
189 void State::initTypeMaps()
190 {
191 static int const MCLDTypes[] =
192 {
193 0, 0x22, 1, 0x22, 2, 0x22, 3, 0x22, 4, 0x22, 5, 0x22, 6, 0x22, 7, 0x22,
194 8, 0x22, 9, 0x22, 0xa, 0x22, 0xb, 0x1a, 0xc, 0x2, 0xd, 0x22, 0xe, 0x22,
195 0x11, 0x22, 0x12, 0x22, 0x13, 0x12, 0x14, 0x2, 0x15, 0x22, 0x16, 0x22, 0x17, 0x22,
196 0x18, 0x22, 0x19, 0x2, 0x1a, 0x2, 0x1d, 0x22, 0x1e, 0x22, 0x1f, 0x12,
197 0x20, 0x22, 0x21, 0x12, 0x22, 0x22, 0x23, 0x22, 0x24, 0x12, 0x25, 0x22, 0x26, 0x22, 0x27, 0x12,
198 0x28, 0x22, 0x29, 0x22, 0x2a, 0x12, 0x2b, 0x22, 0x2c, 0x12,
199 0x31, 0x18
200 };
201 for (int i = 0; i+1 < int(WPS_N_ELEMENTS(MCLDTypes)); i+=2)
202 m_MCLDTypes[MCLDTypes[i]] = MCLDTypes[i+1];
203
204 }
205 }
206
207 ////////////////////////////////////////////////////////////
208 // constructor/destructor
209 ////////////////////////////////////////////////////////////
210 WPS8Table::WPS8Table(WPS8Parser &parser)
211 : m_listener()
212 , m_mainParser(parser)
213 , m_state(new WPS8TableInternal::State)
214 , m_asciiFile(parser.ascii())
215 {
216 }
217
218 WPS8Table::~WPS8Table()
219 {
220 }
221
222 ////////////////////////////////////////////////////////////
223 // update the positions and send data to the listener
224 ////////////////////////////////////////////////////////////
225 int WPS8Table::version() const
226 {
227 if (m_state->m_version <= 0)
228 m_state->m_version = m_mainParser.version();
229 return m_state->m_version;
230 }
231
232 int WPS8Table::numPages() const
233 {
234 return m_state->m_numPages;
235 }
236
237 void WPS8Table::sendTextInCell(int strsId, int cellId)
238 {
239 m_mainParser.sendTextInCell(strsId, cellId);
240 }
241
242 ////////////////////////////////////////////////////////////
243 // update the positions and send data to the listener
244 ////////////////////////////////////////////////////////////
245 void WPS8Table::computePositions() const
246 {
247 m_state->m_numPages = 0;
248 }
249
250 void WPS8Table::flushExtra()
251 {
252 if (m_listener.get() == nullptr) return;
253
254 for (auto const &table : m_state->m_tableMap)
255 {
256 if (table.second.m_parsed) continue;
257 int strsid = m_mainParser.getTableSTRSId(table.second.m_id);
258 if (strsid < 0) continue;
259 sendTable(Vec2f(100.,100.), table.second.m_id, strsid);
260 }
261 }
262
263 ////////////////////////////////////////////////////////////
264 // send a table id
265 ////////////////////////////////////////////////////////////
266 bool WPS8Table::sendTable(Vec2f const &siz, int tableId, int strsid, bool inTextBox)
267 {
268 if (!m_listener)
269 {
270 WPS_DEBUG_MSG(("WPS8Table::sendTable: listener is not set\n"));
271 return true;
272 }
273 if (strsid <= 0)
274 {
275 WPS_DEBUG_MSG(("WPS8Table::sendTable: strsid is not set\n"));
276 return false;
277 }
278
279 auto pos = m_state->m_tableMap.find(tableId);
280 if (pos == m_state->m_tableMap.end())
281 {
282 WPS_DEBUG_MSG(("WPS8Table::sendTable: can not find table with id=%d\n", tableId));
283 if (inTextBox)
284 m_mainParser.send(strsid);
285 else
286 {
287 // OK, we revert to a textbox inserted as a character
288 WPSPosition tablePos(Vec2f(), siz);
289 tablePos.m_anchorTo = WPSPosition::CharBaseLine;
290 tablePos.m_wrapping = WPSPosition::WDynamic;
291
292 m_mainParser.sendTextBox(tablePos, strsid);
293 }
294 return true;
295 }
296
297 auto &table = pos->second;
298 if (table.m_parsed)
299 WPS_DEBUG_MSG(("WPS8Table::sendTable: table with id=%d is already parsed\n", tableId));
300 else
301 table.m_parsed = true;
302 // first we need to update the strsid
303 for (int c = 0; c < table.numCells(); c++)
304 {
305 WPSCellPtr cell = table.getCell(c);
306 if (!cell) continue;
307 reinterpret_cast<WPS8TableInternal::Cell *>(cell.get())->m_strsId = strsid;
308 }
309 if (!table.sendTable(m_listener))
310 table.sendAsText(m_listener);
311 return true;
312 }
313
314 ////////////////////////////////////////////////////////////
315 // find all structures which correspond to the table
316 ////////////////////////////////////////////////////////////
317 bool WPS8Table::readStructures(RVNGInputStreamPtr const &input)
318 {
319 m_state->m_tableMap.clear();
320
321 auto const &nameTable = m_mainParser.getNameEntryMap();
322 auto pos = nameTable.lower_bound("MCLD");
323 while (pos != nameTable.end())
324 {
325 WPSEntry const &entry = pos++->second;
326 if (!entry.hasName("MCLD")) break;
327 if (!entry.hasType("MCLD")) continue;
328
329 readMCLD(input, entry);
330 }
331
332 return true;
333 }
334
335 ////////////////////////////////////////////////////////////
336 // low level
337 ////////////////////////////////////////////////////////////
338 bool WPS8Table::readMCLD(RVNGInputStreamPtr input, WPSEntry const &entry)
339 {
340 if (!entry.hasType(entry.name()))
341 {
342 WPS_DEBUG_MSG(("WPS8Table::readMCLD: warning: MCLD name=%s, type=%s\n",
343 entry.name().c_str(), entry.type().c_str()));
344 return false;
345 }
346
347 long page_offset = entry.begin();
348 long length = entry.length();
349 long endPage = entry.end();
350
351 if (length < 24)
352 {
353 WPS_DEBUG_MSG(("WPS8Table::readMCLD: warning: MCLD length=0x%lx\n", static_cast<unsigned long>(length)));
354
355 return false;
356 }
357
358 entry.setParsed();
359 input->seek(page_offset, librevenge::RVNG_SEEK_SET);
360
361 libwps::DebugStream f;
362 auto mZone = int(libwps::read32(input));
363 auto nTables = int(libwps::read32(input));
364
365 f << "maxUnknown=" << mZone << ", nTables= " << nTables;
366 if ((6+long(nTables)) * 4 > length) return false;
367
368 f << ", ids=(";
369 std::vector<int> listIds;
370 for (int i = 0; i < nTables; i++)
371 {
372 auto val = int(libwps::read32(input));
373 listIds.push_back(val);
374 f << val << ",";
375 }
376 f << ")";
377
378 ascii().addPos(page_offset);
379 ascii().addNote(f.str().c_str());
380
381 int mCounter = -1;
382 bool ok = true;
383
384 static char const *borderNames[] = { "T", "L", "R", "B" };
385 static int const borderPos[] =
386 { WPSBorder::Top, WPSBorder::Left, WPSBorder::Right, WPSBorder::Bottom};
387 static int const borderBit[] =
388 {
389 WPSBorder::TopBit, WPSBorder::LeftBit,
390 WPSBorder::RightBit, WPSBorder::BottomBit
391 };
392 long lastPosOk = input->tell();
393 while (input->tell() != endPage)
394 {
395 if (input->tell()+16 > endPage)
396 {
397 ok = false;
398 break;
399 }
400
401 mCounter++;
402 lastPosOk = input->tell();
403
404 WPS8Struct::FileData tableData;
405 auto sz = int(libwps::read16(input));
406 if (sz < 2 || lastPosOk+sz > endPage)
407 {
408 ok = false;
409 break;
410 }
411
412 std::string error;
413 if (!readBlockData(input, lastPosOk+sz,tableData, error))
414 {
415 f.str("");
416 f << tableData;
417 error = f.str();
418 }
419
420 f.str("");
421 int tableId = (mCounter < int(listIds.size())) ? listIds[size_t(mCounter)] : -1;
422 if (tableId < 0) f << "MCLD/Table[###unknownId]:";
423 else f << "MCLD/Table" << listIds[size_t(mCounter)]<<":";
424
425 auto N = int(libwps::readU32(input));
426 f << " nCells=" << N;
427 if (N < 0 || N > 100)
428 {
429 ok = false;
430 break;
431 }
432
433 WPS8TableInternal::Table table;
434 table.m_id = tableId;
435
436 // point dim seems odd, we will need to update them
437 Vec2f totalRealDim(0,0), totalDataDim(0,0);
438
439 if (!tableData.m_recursData.empty())
440 {
441 f << ",(";
442 for (auto const &dt : tableData.m_recursData)
443 {
444 if (dt.isBad()) continue;
445 int const expectedTypes[] = { 2, 0x22 };
446 if (dt.id() < 0 || dt.id() > 1)
447 {
448 f << "##" << dt << ",";
449 continue;
450 }
451 if (expectedTypes[dt.id()]!=dt.type())
452 {
453 WPS_DEBUG_MSG(("WPS8Table::readMCLD: unexpected type for %d=%d\n", dt.id(), dt.type()));
454 f << "###" << dt << ",";
455 continue;
456 }
457 bool done = true;
458 switch (dt.id())
459 {
460 case 0: // always set
461 f << "f" << dt.id() << ", ";
462 break;
463 case 1: // always 0
464 f << "f" << dt.id() << "=" << dt.m_value << ", ";
465 break;
466 default:
467 done = false;
468 break;
469 }
470
471 if (!done) f << "###" << dt << ",";
472 }
473 f << ")";
474 }
475 if (!error.empty()) f << ", ###err=" << error;
476
477 ascii().addPos(lastPosOk);
478 ascii().addNote(f.str().c_str());
479
480 for (size_t actualCell = 0; actualCell < size_t(N); actualCell++)
481 {
482 lastPosOk = input->tell();
483 sz = int(libwps::read16(input));
484 if (sz < 2 || lastPosOk+sz > endPage)
485 {
486 ok = false;
487 break;
488 }
489
490 WPS8Struct::FileData mainData;
491 error = "";
492
493 if (!readBlockData(input, lastPosOk+sz,mainData, error))
494 {
495 f.str("");
496 f << mainData;
497 error = f.str();
498 }
499
500 // original position in point ( checkme)
501 float dim[4] = { 0, 0, 0, 0 };
502
503 f.str("");
504 f << "MCLD/Table";
505 if (tableId >= 0) f << tableId;
506 f << "(Cell" << actualCell <<"):";
507
508 auto cell = std::make_shared<WPS8TableInternal::Cell>(*this);
509 WPSCellPtr cellPtr(cell);
510 cell->m_id = int(actualCell);
511
512 libwps::DebugStream f2;
513 WPSColor cellColor[] = { WPSColor::black(), WPSColor::white() };
514
515 for (auto &dt : mainData.m_recursData)
516 {
517 if (dt.isBad()) continue;
518 if (m_state->m_MCLDTypes.find(dt.id())==m_state->m_MCLDTypes.end())
519 {
520 f << "##" << dt << ",";
521 continue;
522 }
523 if (m_state->m_MCLDTypes.find(dt.id())->second != dt.type())
524 {
525 WPS_DEBUG_MSG(("WPS8Table::readMCLD: unexpected type for %d=%d\n", dt.id(), dt.type()));
526 f << "###" << dt << ",";
527 continue;
528 }
529
530 bool done = true;
531 switch (dt.id())
532 {
533 case 0:
534 case 1:
535 case 2:
536 case 3: // min/max pos in point (96 dpi ?)
537 dim[dt.id()] = float(dt.m_value);
538 break;
539 case 4:
540 case 5: // look coherent with EOBJ info
541 if (dt.id() == 4) cell->m_size.setX(float(dt.m_value)/914400.f);
542 else cell->m_size.setY(float(dt.m_value)/914400.f);
543 break;
544 // border size : unknown dim
545 // [ 1, 3, 1, 3, ]
546 // [ 0.666667, 1.33333, 0.666667, 1.33333, ]/30
547 case 6:
548 case 7:
549 case 8:
550 case 9:
551 cell->m_bordersSep[dt.id()-6] = float(dt.m_value)/914400.f;
552 break;
553 case 0xe: // always 0x20000000
554 if (dt.m_value != 0x20000000)
555 f2 << "f" << dt.id() << "=" << std::hex << dt.m_value << std::dec << ",";
556 break;
557 case 0x12: // 0x0 or 0x997aa0 or 0x3fffe238 ?
558 f2 << "f" << dt.id() << "=" << std::hex << dt.m_value << std::dec << ",";
559 break;
560 case 0x13: //find -1|6 here
561 f2 << "f" << dt.id() << "=" << int(int8_t(dt.m_value)) << ",";
562 break;
563 case 0x1d: // first color
564 case 0x1e: // second color
565 cellColor[dt.id()-0x1d] = dt.getRGBColor();
566 break;
567 case 0x1f:
568 {
569 float percent=0.5;
570 if (dt.m_value == 0) // no motif
571 break;
572 if (dt.m_value >= 3 && dt.m_value <= 9)
573 percent = float(dt.m_value)*0.1f; // gray motif
574 else
575 f2 << "backMotif=" << dt.m_value << ",";
576 cell->setBackgroundColor(WPSColor::barycenter(percent,cellColor[0],1.f-percent,cellColor[1]));
577 break;
578 }
579 // the border color
580 case 0x20:
581 case 0x23:
582 case 0x26:
583 case 0x29:
584 {
585 int wh = (dt.id()-0x20)/3;
586 WPSBorder border = cell->borders()[size_t(borderPos[wh])];
587 border.m_color = dt.getRGBColor();
588 cell->setBorders(borderBit[wh], border);
589 break;
590 }
591 case 0x21:
592 case 0x24:
593 case 0x27:
594 case 0x2a:
595 {
596 std::string mess("");
597 int wh = (dt.id()-0x21)/3;
598 WPSBorder border = cell->borders()[size_t(borderPos[wh])];
599 dt.getBorderStyles(border.m_style, border.m_type, mess);
600 cell->setBorders(borderBit[wh], border);
601 if (mess.length())
602 f2 << "bordStyle" << borderNames[wh] << "=[" << mess << "],";
603 break;
604 }
605 case 0x22:
606 case 0x25:
607 case 0x28:
608 case 0x2b: // often 20
609 f2 << "unknBord" << borderNames[(dt.id()-0x22)/3] << "=" << dt.m_value << ",";
610 break;
611 case 0x2c: // 1, 0, -1
612 switch (dt.m_value)
613 {
614 case 0:
615 break; // normal
616 case 0xFF: // also unset, diff with value = 1 ?
617 f2 << "#f" << dt.id() << "=" << std::hex << dt.m_value << std::dec << ",";
618 WPS_FALLTHROUGH;
619 case 1:
620 cell->setVerticalSet(false);
621 break;
622 default:
623 f2 << "f" << dt.id() << "=" << std::hex << dt.m_value << std::dec << ",";
624 break;
625 }
626 break;
627 // always 0,excepted 0x15 hich can be 0 or 1 ?
628 case 0xa:
629 case 0xb:
630 case 0xd:
631 case 0x11:
632 case 0x15:
633 case 0x16:
634 case 0x17:
635 if (dt.m_value == 0);
636 else if (dt.m_value > -10 && dt.m_value < 10)
637 f2 << "f" << dt.id() << "=" << dt.m_value << ",";
638 else done = false;
639 break;
640 case 0x18:
641 switch (dt.m_value)
642 {
643 case 0:
644 cell->setVAlignment(WPSCellFormat::VALIGN_TOP);
645 break;
646 case 1:
647 cell->setVAlignment(WPSCellFormat::VALIGN_CENTER);
648 break;
649 case 2:
650 cell->setVAlignment(WPSCellFormat::VALIGN_BOTTOM);
651 break;
652 default:
653 if (dt.m_value > -10 && dt.m_value < 10)
654 f2 << "f" << dt.id() << "=" << dt.m_value << ",";
655 else
656 done = false;
657 break;
658 }
659 break;
660 case 0xc:
661 case 0x14:
662 case 0x19:
663 case 0x1a: // flag ?
664 if (dt.isTrue())
665 f2 << "f" << dt.id() << ", ";
666 else
667 f2 << "f" << dt.id() << "=false, ";
668 break;
669 default:
670 done = false;
671 break;
672 }
673
674 if (!done)
675 f2 << dt << ",";
676 }
677
678 cell->setBox(WPSBox2f(Vec2f(dim[1],dim[0]),Vec2f(dim[3],dim[2])));
679 totalRealDim += cell->m_size;
680 totalDataDim += cell->box().size();
681 table.add(cellPtr);
682
683 f << *cell;
684 if (!f2.str().empty())
685 f << ", unk=(" << f2.str() << ")";
686
687 if (!error.empty()) f << ",###err=" << error;
688 input->seek(lastPosOk+sz, librevenge::RVNG_SEEK_SET);
689
690 ascii().addPos(lastPosOk);
691 ascii().addNote(f.str().c_str());
692 }
693 if (!ok) break;
694
695 float factor[2] = { 1, 1};
696 if (totalDataDim[0] > 0) factor[0] = 72.f*totalRealDim[0]/totalDataDim[0];
697 if (totalDataDim[1] > 0) factor[1] = 72.f*totalRealDim[1]/totalDataDim[1];
698 for (int c = 0; c < table.numCells(); c++)
699 {
700 auto cell = table.getCell(c);
701 if (!cell) continue;
702 WPSBox2f box=cell->box();
703 cell->setBox(WPSBox2f(Vec2f(box[0][0]*factor[0], box[0][1]*factor[1]),
704 Vec2f(box[1][0]*factor[0], box[1][1]*factor[1])));
705 }
706
707 if (tableId >=0) m_state->m_tableMap[tableId] = table;
708 else
709 {
710 WPS_DEBUG_MSG(("WPS8Table::readMCLD: find a table with negative id\n"));
711 }
712 }
713
714 if (!ok)
715 {
716 WPS_DEBUG_MSG(("WPS8Table::readMCLD: stopped prematively"));
717 ascii().addPos(lastPosOk);
718 ascii().addNote("###MCLD");
719 }
720 return ok;
721 }
722 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: