Debian Codegraph @

src/lib/MsWksTable.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 #include <iomanip>
35 #include <iostream>
36 #include <map>
37 #include <sstream>
38
39 #include "libmwaw_internal.hxx"
40
41 #include "MWAWCell.hxx"
42 #include "MWAWListener.hxx"
43 #include "MWAWFont.hxx"
44 #include "MWAWFontConverter.hxx"
45 #include "MWAWGraphicStyle.hxx"
46 #include "MWAWParagraph.hxx"
47 #include "MWAWPictData.hxx"
48 #include "MWAWSubDocument.hxx"
49 #include "MWAWTable.hxx"
50
51 #include "MsWksGraph.hxx"
52 #include "MsWksDocument.hxx"
53
54 #include "MsWksTable.hxx"
55
56 /** Internal: the structures of a MsWksTable */
57 namespace MsWksTableInternal
58 {
59 ////////////////////////////////////////
60 //! Internal: the chart of a MsWksTable
61 struct Chart {
62 //! constructor
63 explicit Chart(MsWksGraph::Style const &style)
64 : m_style(style)
65 , m_backgroundEntry()
66 , m_zoneId(-1)
67 {
68 for (auto &id : m_textZonesId) id=-1;
69 }
70 //! empty constructor
71 Chart()
72 : m_style()
73 , m_backgroundEntry()
74 , m_zoneId(-1)
75 {
76 for (auto &id : m_textZonesId) id=-1;
77 }
78
79 //! the graphic style
80 MWAWGraphicStyle m_style;
81 //! the three text pictures
82 int m_textZonesId[3];
83 //! the background entry
84 MWAWEntry m_backgroundEntry;
85 //! the chart zone id (in the graph parser )
86 int m_zoneId;
87 };
88
89
90 ////////////////////////////////////////
91 //! Internal: the table of a MsWksTable
92 struct Table {
93 //! the cell content
94 struct Cell {
95 Cell()
96 : m_pos(-1,-1)
97 , m_font()
98 , m_text("")
99 {
100 }
101 //! the cell position
102 MWAWVec2i m_pos;
103 //! the font
104 MWAWFont m_font;
105 //! the text
106 std::string m_text;
107 };
108 //! constructor
109 explicit Table(MsWksGraph::Style const &style)
110 : m_style(style)
111 , m_numRows(0)
112 , m_numCols(0)
113 , m_rowsDim()
114 , m_colsDim()
115 , m_font()
116 , m_cellsList()
117 {
118 m_style.m_surfaceColor = style.m_baseSurfaceColor;
119 }
120 //! empty constructor
121 Table()
122 : m_style()
123 , m_numRows(0)
124 , m_numCols(0)
125 , m_rowsDim()
126 , m_colsDim()
127 , m_font()
128 , m_cellsList()
129 {
130 }
131
132 //! try to find a cell
133 Cell const *getCell(MWAWVec2i const &pos) const
134 {
135 for (auto &cell : m_cellsList) {
136 if (cell.m_pos == pos)
137 return &cell;
138 }
139 return nullptr;
140 }
141
142 //! the graphic style
143 MWAWGraphicStyle m_style;
144 int m_numRows /** the number of rows*/, m_numCols/** the number of columns*/;
145 std::vector<int> m_rowsDim/**the rows dimensions*/, m_colsDim/*the columns dimensions*/;
146 //! the default font
147 MWAWFont m_font;
148 //! the list of cell
149 std::vector<Cell> m_cellsList;
150 };
151
152 ////////////////////////////////////////
153 //! Internal: the state of a MsWksTable
154 struct State {
155 //! constructor
156 State()
157 : m_version(-1)
158 , m_idChartMap()
159 , m_idTableMap()
160 {
161 }
162
163 //! the version
164 int m_version;
165
166 //! the map id->chart
167 std::map<int, Chart> m_idChartMap;
168 //! the map id->table
169 std::map<int, Table> m_idTableMap;
170 };
171
172 }
173
174 ////////////////////////////////////////////////////////////
175 // constructor/destructor, ...
176 ////////////////////////////////////////////////////////////
177 MsWksTable::MsWksTable(MWAWParser &parser, MsWksDocument &zone, MsWksGraph &graph)
178 : m_parserState(parser.getParserState())
179 , m_state(new MsWksTableInternal::State)
180 , m_mainParser(&parser)
181 , m_graphParser(&graph)
182 , m_zone(zone)
183 {
184 }
185
186 MsWksTable::~MsWksTable()
187 { }
188
189 int MsWksTable::version() const
190 {
191 if (m_state->m_version < 0)
192 m_state->m_version = m_parserState->m_version;
193 return m_state->m_version;
194 }
195
196 ////////////////////////////////////////////////////////////
197 // table
198 ////////////////////////////////////////////////////////////
199 bool MsWksTable::sendTable(int zoneId)
200 {
201 MWAWListenerPtr listener=m_parserState->getMainListener();
202 if (!listener) return false;
203
204 if (m_state->m_idTableMap.find(zoneId)==m_state->m_idTableMap.end()) {
205 MWAW_DEBUG_MSG(("MsWksTable::sendTable: can not find textbox %d\n", zoneId));
206 return false;
207 }
208 if (m_parserState->m_type==MWAWParserState::Spreadsheet) {
209 /* inserting a table may cause problem when there happens in some
210 ods file, so... */
211 MWAW_DEBUG_MSG(("MsWksTable::sendTable: inserting a table in a spreadsheet is not implemented\n"));
212 return false;
213 }
214
215 auto &table = m_state->m_idTableMap.find(zoneId)->second;
216
217 // open the table
218 size_t nCols = table.m_colsDim.size();
219 size_t nRows = table.m_rowsDim.size();
220 if (!nCols || !nRows) {
221 MWAW_DEBUG_MSG(("MsWksTable::sendTable: problem with dimensions\n"));
222 return false;
223 }
224 std::vector<float> colsDims(nCols);
225 for (size_t c = 0; c < nCols; c++) colsDims[c] = float(table.m_colsDim[c]);
226 MWAWTable theTable(MWAWTable::TableDimBit);
227 theTable.setColsSize(colsDims);
228 listener->openTable(theTable);
229
230 int const borderPos = libmwaw::TopBit | libmwaw::RightBit |
231 libmwaw::BottomBit | libmwaw::LeftBit;
232 MWAWBorder border, internBorder;
233 internBorder.m_width=0.5;
234 internBorder.m_color=MWAWColor(0xC0,0xC0,0xC0);
235 MWAWParagraph para;
236 para.m_justify=MWAWParagraph::JustificationCenter;
237 for (size_t row = 0; row < nRows; row++) {
238 listener->openTableRow(float(table.m_rowsDim[row]), librevenge::RVNG_POINT);
239
240 for (size_t col = 0; col < nCols; col++) {
241 MWAWCell cell;
242 MWAWVec2i cellPosition(MWAWVec2i(static_cast<int>(col),static_cast<int>(row)));
243 cell.setPosition(cellPosition);
244 cell.setBorders(borderPos, border);
245 int internWhat=0;
246 if (col!=0) internWhat|=libmwaw::LeftBit;
247 if (col+1!=nCols) internWhat|=libmwaw::RightBit;
248 if (row!=0) internWhat|=libmwaw::TopBit;
249 if (row+1!=nRows) internWhat|=libmwaw::BottomBit;
250 cell.setBorders(internWhat, internBorder);
251 if (!table.m_style.m_surfaceColor.isWhite())
252 cell.setBackgroundColor(table.m_style.m_surfaceColor);
253 listener->openTableCell(cell);
254 listener->setParagraph(para);
255
256 auto const *tCell=table.getCell(cellPosition);
257 if (tCell) {
258 listener->setFont(tCell->m_font);
259 size_t nChar = tCell->m_text.size();
260 for (size_t ch = 0; ch < nChar; ch++) {
261 auto c = static_cast<unsigned char>(tCell->m_text[ch]);
262 switch (c) {
263 case 0x9:
264 MWAW_DEBUG_MSG(("MsWksTable::sendTable: find a tab\n"));
265 listener->insertChar(' ');
266 break;
267 case 0xd:
268 listener->insertEOL();
269 break;
270 default:
271 listener->insertCharacter(c);
272 break;
273 }
274 }
275 }
276
277 listener->closeTableCell();
278 }
279 listener->closeTableRow();
280 }
281
282 // close the table
283 listener->closeTable();
284 return true;
285 }
286
287 bool MsWksTable::readTable(int numCol, int numRow, int zoneId, MsWksGraph::Style const &style)
288 {
289 int vers=version();
290 MWAWInputStreamPtr input=m_zone.getInput();
291 long actPos = input->tell();
292 libmwaw::DebugFile &ascFile = m_zone.ascii();
293 libmwaw::DebugStream f, f2;
294 f << "Entries(Table): ";
295
296 MsWksTableInternal::Table table(style);
297 table.m_numRows=numRow;
298 table.m_numCols=numCol;
299 // first we read the dim
300 for (int i = 0; i < 2; i++) {
301 auto &dim = i==0 ? table.m_rowsDim : table.m_colsDim;
302 dim.resize(0);
303 auto sz = static_cast<int>(input->readLong(4));
304 if (i == 0 && sz != 2*table.m_numRows) return false;
305 if (i == 1 && sz != 2*table.m_numCols) return false;
306
307 if (i == 0) f << "rowS=(";
308 else f << "colS=(";
309
310 for (int j = 0; j < sz/2; j++) {
311 auto val = static_cast<int>(input->readLong(2));
312
313 if (val < -10) return false;
314
315 dim.push_back(val);
316 f << val << ",";
317 }
318 f << "), ";
319 }
320
321 long sz = input->readLong(4);
322 f << "szOfCells=" << sz;
323 ascFile.addPos(actPos);
324 ascFile.addNote(f.str().c_str());
325
326 actPos = input->tell();
327 long endPos = actPos+sz;
328 // now we read the data for each size
329 while (input->tell() != endPos) {
330 f.str("");
331 actPos = input->tell();
332 MsWksTableInternal::Table::Cell cell;
333 auto y = static_cast<int>(input->readLong(2));
334 auto x = static_cast<int>(input->readLong(2));
335 cell.m_pos = MWAWVec2i(x,y);
336 if (x < 0 || y < 0 ||
337 x >= table.m_numCols || y >= table.m_numRows) return false;
338
339 f << "Table:("<< cell.m_pos << "):";
340 auto nbChar = static_cast<int>(input->readLong(1));
341 if (nbChar < 0 || actPos+5+nbChar > endPos) return false;
342
343 std::string fName("");
344 for (int c = 0; c < nbChar; c++)
345 fName +=char(input->readLong(1));
346
347 input->seek(actPos+34, librevenge::RVNG_SEEK_SET);
348 f << std::hex << "unk=" << input->readLong(2) << ", "; // 0|827
349 auto v = static_cast<int>(input->readLong(2));
350 if (v) f << "f0=" << v << ", ";
351 auto fSize = static_cast<int>(input->readLong(2));
352 v = static_cast<int>(input->readLong(2));
353 if (v) f2 << "unkn0=" << v << ", ";
354 auto fFlags = static_cast<int>(input->readLong(2));
355
356 nbChar = static_cast<int>(input->readLong(4));
357 if (nbChar <= 0 || input->tell()+nbChar > endPos) return false;
358
359 v = static_cast<int>(input->readLong(2));
360 if (v) f << "f1=" << v << ", ";
361 auto fColors = static_cast<int>(input->readLong(2));
362 v = static_cast<int>(input->readLong(2));
363 if (v) f << "f2=" << v << ", ";
364 auto bgColors = static_cast<int>(input->readLong(2));
365 if (bgColors)
366 f2 << std::dec << "bgColorId(?)=" << bgColors << ", "; // indexed
367
368 cell.m_font=MWAWFont(m_parserState->m_fontConverter->getId(fName), float(fSize));
369 uint32_t flags = 0;
370 if (fFlags & 0x1) flags |= MWAWFont::boldBit;
371 if (fFlags & 0x2) flags |= MWAWFont::italicBit;
372 if (fFlags & 0x4) cell.m_font.setUnderlineStyle(MWAWFont::Line::Simple);
373 if (fFlags & 0x8) flags |= MWAWFont::embossBit;
374 if (fFlags & 0x10) flags |= MWAWFont::shadowBit;
375 if (fFlags & 0x20) {
376 if (vers==1)
377 cell.m_font.set(MWAWFont::Script(20,librevenge::RVNG_PERCENT,80));
378 else
379 cell.m_font.set(MWAWFont::Script::super100());
380 }
381 if (fFlags & 0x40) {
382 if (vers==1)
383 cell.m_font.set(MWAWFont::Script(-20,librevenge::RVNG_PERCENT,80));
384 else
385 cell.m_font.set(MWAWFont::Script::sub100());
386 }
387 cell.m_font.setFlags(flags);
388
389 if (fColors != 0xFF) {
390 MWAWColor col;
391 if (m_zone.getColor(fColors,col,3))
392 cell.m_font.setColor(col);
393 else
394 f << "#colId=" << fColors << ",";
395 }
396 f << "[" << cell.m_font.getDebugString(m_parserState->m_fontConverter) << "," << f2.str()<< "],";
397 // check what happens, if the size of text is greater than 4
398 for (int c = 0; c < nbChar; c++)
399 cell.m_text+=char(input->readLong(1));
400 f << cell.m_text;
401
402 table.m_cellsList.push_back(cell);
403
404 ascFile.addPos(actPos);
405 ascFile.addNote(f.str().c_str());
406 }
407 if (m_state->m_idTableMap.find(zoneId)!=m_state->m_idTableMap.end()) {
408 MWAW_DEBUG_MSG(("MsWksTable::readTable: oops a table with id=%d already exists\n", zoneId));
409 }
410 else
411 m_state->m_idTableMap[zoneId]=table;
412 return true;
413 }
414
415 ////////////////////////////////////////////////////////////
416 // chart
417 ////////////////////////////////////////////////////////////
418
419 void MsWksTable::setChartZoneId(int chartId, int zoneId)
420 {
421 if (m_state->m_idChartMap.find(chartId)==m_state->m_idChartMap.end()) {
422 MWAW_DEBUG_MSG(("MsWksTable::setChartZoneId: can not find chart %d\n", chartId));
423 return;
424 }
425 auto &chart = m_state->m_idChartMap.find(chartId)->second;
426 chart.m_zoneId = zoneId;
427 }
428
429 bool MsWksTable::sendChart(int chartId)
430 {
431 MWAWListenerPtr listener=m_parserState->getMainListener();
432 if (!listener) {
433 MWAW_DEBUG_MSG(("MsWksTable::sendChart: can not find a listener\n"));
434 return false;
435 }
436 if (m_state->m_idChartMap.find(chartId)==m_state->m_idChartMap.end()) {
437 MWAW_DEBUG_MSG(("MsWksTable::sendChart: can not find chart %d\n", chartId));
438 return false;
439 }
440 auto &chart = m_state->m_idChartMap.find(chartId)->second;
441
442 MWAWInputStreamPtr input=m_zone.getInput();
443 MWAWPosition chartPos;
444 if (chart.m_zoneId < 0 || !m_graphParser->getZonePosition(chart.m_zoneId, MWAWPosition::Frame, chartPos)) {
445 MWAW_DEBUG_MSG(("MsWksTable::sendChart: oops can not find chart bdbox %d[%d]\n", chartId, chart.m_zoneId));
446 return false;
447 }
448 MWAWPosition pictPos(MWAWVec2f(0,0), chartPos.size(),librevenge::RVNG_POINT);
449 pictPos.setRelativePosition(MWAWPosition::Frame, MWAWPosition::XLeft, MWAWPosition::YTop);
450 if (chart.m_backgroundEntry.valid()) {
451 long actPos = input->tell();
452 #ifdef DEBUG_WITH_FILES
453 if (1) {
454 librevenge::RVNGBinaryData file;
455 input->seek(chart.m_backgroundEntry.begin(), librevenge::RVNG_SEEK_SET);
456 input->readDataBlock(chart.m_backgroundEntry.length(), file);
457 static int volatile pictName = 0;
458 libmwaw::DebugStream f;
459 f << "Pict-" << ++pictName << ".pct";
460 libmwaw::Debug::dumpFile(file, f.str().c_str());
461 }
462 #endif
463
464 input->seek(chart.m_backgroundEntry.begin(), librevenge::RVNG_SEEK_SET);
465 MWAWBox2f naturalBox;
466 auto res = MWAWPictData::check(input, static_cast<int>(chart.m_backgroundEntry.length()), naturalBox);
467 if (res == MWAWPict::MWAW_R_BAD) {
468 MWAW_DEBUG_MSG(("MsWksTable::sendChart: can not find the picture\n"));
469 }
470 else {
471 input->seek(chart.m_backgroundEntry.begin(), librevenge::RVNG_SEEK_SET);
472 std::shared_ptr<MWAWPict> pict(MWAWPictData::get(input, static_cast<int>(chart.m_backgroundEntry.length())));
473
474 MWAWEmbeddedObject picture;
475 if (pict && pict->getBinary(picture))
476 listener->insertPicture(pictPos, picture);
477 }
478 input->seek(actPos, librevenge::RVNG_SEEK_SET);
479 }
480 for (int i=0; i < 3; i++) {
481 int cId=chart.m_textZonesId[i];
482 MWAWPosition childPos;
483 if (!m_graphParser->getZonePosition(cId, MWAWPosition::Frame, childPos)) {
484 MWAW_DEBUG_MSG(("MsWksTable::sendChart: oops can not find chart bdbox for child %d[%d]\n", i, cId));
485 continue;
486 }
487 MWAWPosition textPos(pictPos);
488 textPos.setOrigin(childPos.origin()-chartPos.origin());
489 textPos.setSize(childPos.size());
490 m_graphParser->send(cId, textPos);
491 }
492
493 return true;
494 }
495
496 bool MsWksTable::readChart(int chartId, MsWksGraph::Style const &style)
497 {
498 // checkme: works for some chart, but not sure that it can work for all chart...
499 MWAWInputStreamPtr input=m_zone.getInput();
500 long pos = input->tell();
501 int const vers=version();
502 if (vers<=2 || (vers==3&&m_parserState->m_type!=MWAWParserState::Spreadsheet) ||
503 !input->checkPosition(pos+306))
504 return false;
505
506 libmwaw::DebugFile &ascFile = m_zone.ascii();
507 libmwaw::DebugStream f;
508 f << "Entries(Chart):";
509
510 MsWksTableInternal::Chart chart(style);
511 auto val = static_cast<int>(input->readLong(2));
512 switch (val) {
513 case 1:
514 f << "bar,";
515 break;
516 case 2:
517 f << "stacked,";
518 break;
519 case 3:
520 f << "line,";
521 break; // checkme
522 case 4:
523 f << "combo,";
524 break; // checkme
525 case 5:
526 f << "pie,";
527 break; // checkme
528 case 6:
529 f << "hi-lo-choose,";
530 break; // checkme
531 default:
532 f << "#type=val";
533 break;
534 }
535 for (int i = 0; i < 4; i++) {
536 val = static_cast<int>(input->readLong(2));
537 if (val) f << "col" << i << "=" << val << ",";
538 }
539 f << "rows=";
540 for (int i = 0; i < 2; i++) {
541 val = static_cast<int>(input->readLong(2));
542 f << val;
543 if (i==0) f << "-";
544 else f << ",";
545 }
546 val = static_cast<int>(input->readLong(2));
547 if (val) f << "colLabels=" << val << ",";
548 val = static_cast<int>(input->readLong(2));
549 if (val) f << "rowLabels=" << val << ",";
550 std::string name("");
551 auto sz = static_cast<int>(input->readULong(1));
552 if (sz > 31) {
553 MWAW_DEBUG_MSG(("MsWksTable::readChart: string size is too long\n"));
554 return false;
555 }
556 for (int i = 0; i < sz; i++) {
557 auto c = char(input->readLong(1));
558 if (!c) break;
559 name+=c;
560 }
561 f << name << ",";
562 input->seek(pos+50, librevenge::RVNG_SEEK_SET);
563 for (int i = 0; i < 128; i++) { // always 0 ?
564 val = static_cast<int>(input->readLong(2));
565 if (val) f << "g" << i << "=" << val << std::dec << ",";
566 }
567 ascFile.addPos(pos);
568 ascFile.addNote(f.str().c_str());
569
570 pos = input->tell();
571 ascFile.addPos(pos);
572 ascFile.addNote("Chart(II)");
573 input->seek(vers==3 ? 1992 : 2428, librevenge::RVNG_SEEK_CUR);
574
575 // three textbox
576 for (int i = 0; i < 3; i++) {
577 pos = input->tell();
578 MWAWEntry childZone;
579 chart.m_textZonesId[i] = m_graphParser->getEntryPicture(-9999, childZone, false, i+2);
580 if (chart.m_textZonesId[i]<0) {
581 MWAW_DEBUG_MSG(("MsWksTable::readChart: can not find textbox\n"));
582 input->seek(pos, librevenge::RVNG_SEEK_SET);
583 return false;
584 }
585 }
586 // the background picture
587 pos = input->tell();
588 auto dataSz = long(input->readULong(4));
589 auto smDataSz = long(input->readULong(2));
590 if (!dataSz || (dataSz&0xFFFF) != smDataSz || !input->checkPosition(pos+4+dataSz))
591 // background picture not always present ( at least in v3)
592 input->seek(pos, librevenge::RVNG_SEEK_SET);
593 else {
594 MWAWEntry &background=chart.m_backgroundEntry;
595 background.setBegin(pos+4);
596 background.setLength(dataSz);
597 ascFile.skipZone(background.begin(), background.end()-1);
598
599 ascFile.addPos(pos);
600 ascFile.addNote("Chart(picture)");
601 input->seek(background.end(), librevenge::RVNG_SEEK_SET);
602 }
603 // last the value ( by columns ? )
604 for (int i = 0; i < 4; i++) {
605 pos = input->tell();
606 dataSz = long(input->readULong(4));
607 if (dataSz > input->size() - pos - 4)
608 dataSz = input->size() - pos - 4;
609 if (dataSz%0x10) {
610 MWAW_DEBUG_MSG(("MsWksTable::readChart: can not read end last zone\n"));
611 input->seek(pos, librevenge::RVNG_SEEK_SET);
612 return false;
613 }
614 f.str("");
615 f << "Chart(A" << i << ")";
616 ascFile.addPos(pos);
617 ascFile.addNote(f.str().c_str());
618 auto numLine = int(dataSz/0x10);
619 for (int l = 0; l < numLine; l++) {
620 f.str("");
621 f << "Chart(A" << i << "-" << l << ")";
622 ascFile.addPos(pos+4+0x10*l);
623 ascFile.addNote(f.str().c_str());
624 }
625 if (input->seek(pos+4+dataSz, librevenge::RVNG_SEEK_SET) != 0) {
626 MWAW_DEBUG_MSG(("MsWksTable::readChart: reading past the end\n"));
627 return false;
628 }
629 }
630 if (m_state->m_idChartMap.find(chartId)!=m_state->m_idChartMap.end()) {
631 MWAW_DEBUG_MSG(("MsWksTable::readChart: oops a chart with id=%d already exists\n", chartId));
632 }
633 else
634 m_state->m_idChartMap[chartId]=chart;
635 return true;
636 }
637
638 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: