Debian Codegraph @

src/lib/BeagleWksSSParser.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 <cmath>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <set>
39 #include <sstream>
40
41 #include <librevenge/librevenge.h>
42
43 #include "MWAWChart.hxx"
44 #include "MWAWCell.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWHeader.hxx"
47 #include "MWAWList.hxx"
48 #include "MWAWParagraph.hxx"
49 #include "MWAWPosition.hxx"
50 #include "MWAWPrinter.hxx"
51 #include "MWAWRSRCParser.hxx"
52 #include "MWAWSpreadsheetListener.hxx"
53 #include "MWAWSubDocument.hxx"
54
55 #include "BeagleWksStructManager.hxx"
56
57 #include "BeagleWksSSParser.hxx"
58
59 /** Internal: the structures of a BeagleWksSSParser */
60 namespace BeagleWksSSParserInternal
61 {
62 //! Internal: the cell of a BeagleWksSSParser
63 struct Cell final : public MWAWCell {
64 //! constructor
65 explicit Cell(MWAWVec2i pos=MWAWVec2i(0,0))
66 : MWAWCell()
67 , m_content()
68 , m_formula(-1)
69 , m_isEmpty(false)
70 {
71 setPosition(pos);
72 }
73 Cell(Cell const &)=default;
74 //! destructor
75 ~Cell() final;
76 //! the cell content
77 MWAWCellContent m_content;
78 //! the formula id
79 int m_formula;
80 //! flag to know if the cell is empty
81 bool m_isEmpty;
82 };
83
84 Cell::~Cell()
85 {
86 }
87
88 //! Internal: the spreadsheet of a BeagleWksSSParser
89 struct Spreadsheet {
90 //! constructor
91 Spreadsheet()
92 : m_numRows(0)
93 , m_widthCols()
94 , m_heightRows()
95 , m_cells()
96 , m_lastReadRow(-1)
97 {
98 }
99 //! try to associate a formula to a cell
100 bool addFormula(MWAWVec2i const &cellPos, std::vector<MWAWCellContent::FormulaInstruction> const &formula);
101 //! returns the height of a row in point
102 double getRowHeight(int row) const
103 {
104 if (row<0 || row>=static_cast<int>(m_heightRows.size()) || m_heightRows[size_t(row)]<0)
105 return 16.;
106 return m_heightRows[size_t(row)];
107 }
108 //! returns the height of a row in point and updated repeated row
109 double getRowHeight(int row, int &numRepeated) const
110 {
111 double res=getRowHeight(row);
112 numRepeated=1;
113 if (row<0 || row>=static_cast<int>(m_heightRows.size()))
114 numRepeated=1000;
115 else {
116 for (auto r=size_t(row+1); r<m_heightRows.size(); ++r) {
117 double nextH=getRowHeight(row+1);
118 if (nextH<res || nextH>res)
119 break;
120 ++numRepeated;
121 }
122 }
123 return res;
124 }
125 //! convert the m_widthCols, m_heightRows in a vector of of point size
126 static std::vector<float> convertInPoint(std::vector<int> const &list,
127 float defSize)
128 {
129 size_t numElt = list.size();
130 std::vector<float> res;
131 res.resize(numElt);
132 for (size_t i = 0; i < numElt; i++) {
133 if (list[i] < 0) res[i] = defSize;
134 else res[i] = float(list[i]);
135 }
136 return res;
137 }
138 //! update the number of columns and the width
139 void updateWidthCols()
140 {
141 int maxCol=-1;
142 for (auto const &cell : m_cells) {
143 if (cell.position()[0]>maxCol)
144 maxCol = cell.position()[0];
145 }
146 m_widthCols.resize(size_t(maxCol+1),-1);
147 }
148 //! the number of rows
149 int m_numRows;
150 //! the column size in points
151 std::vector<int> m_widthCols;
152 //! the row size in points
153 std::vector<int> m_heightRows;
154 //! the list of not empty cells
155 std::vector<Cell> m_cells;
156 //! the last read rows
157 int m_lastReadRow;
158 };
159
160 bool Spreadsheet::addFormula(MWAWVec2i const &cellPos, std::vector<MWAWCellContent::FormulaInstruction> const &formula)
161 {
162 for (auto &cell : m_cells) {
163 if (cell.position()!=cellPos)
164 continue;
165 cell.m_content.m_formula=formula;
166 return true;
167 }
168 MWAW_DEBUG_MSG(("Spreadsheet::addFormula: can not find cell with position %dx%d\n", cellPos[0], cellPos[1]));
169 return false;
170 }
171
172 //! Internal: the chart of a BeagleWksSSParser
173 struct Chart final : public MWAWChart {
174 //! constructor
175 Chart(std::string const &name, BeagleWksSSParser &parser)
176 : MWAWChart(name, parser.getParserState()->m_fontConverter)
177 , m_parser(&parser)
178 , m_input(parser.getInput())
179 {
180 }
181 //! send a zone content
182 void sendContent(TextZone const &zone, MWAWListenerPtr &listener) final;
183 //! the main parser
184 BeagleWksSSParser *m_parser;
185 //! the input
186 MWAWInputStreamPtr m_input;
187 private:
188 Chart(Chart const &orig) = delete;
189 Chart operator=(Chart const &orig) = delete;
190 };
191
192 void Chart::sendContent(Chart::TextZone const &zone, MWAWListenerPtr &listener)
193 {
194 if (!listener.get() || !m_parser) {
195 MWAW_DEBUG_MSG(("BeagleWksSSParserInternal::Chart::sendContent: no listener\n"));
196 return;
197 }
198 long pos = m_input->tell();
199 listener->setFont(zone.m_font);
200 m_parser->sendText(zone.m_textEntry, false);
201 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
202 }
203
204 ////////////////////////////////////////
205 //! Internal: the state of a BeagleWksSSParser
206 struct State {
207 //! constructor
208 State()
209 : m_spreadsheetBegin(-1)
210 , m_spreadsheet()
211 , m_spreadsheetName("Sheet0")
212 , m_chartList()
213 , m_typeEntryMap()
214 , m_actPage(0)
215 , m_numPages(0)
216 , m_headerHeight(0)
217 , m_footerHeight(0)
218 {
219 }
220 //! returns a color corresponding to an id
221 static bool getColor(int id, MWAWColor &color)
222 {
223 switch (id) {
224 case 0:
225 color=MWAWColor::black();
226 return true;
227 case 1:
228 color=MWAWColor::white();
229 return true;
230 case 2:
231 color=MWAWColor(0xFF,0,0);
232 return true;
233 case 3:
234 color=MWAWColor(0,0xFF,0);
235 return true;
236 case 4:
237 color=MWAWColor(0,0,0xFF);
238 return true;
239 case 5:
240 color=MWAWColor(0,0xFF,0xFF);
241 return true;
242 case 6:
243 color=MWAWColor(0xFF,0,0xFF);
244 return true;
245 case 7:
246 color=MWAWColor(0xFF,0xFF,0);
247 return true;
248 default:
249 MWAW_DEBUG_MSG(("BeagleWksSSParserInternal::State::getColor: unknown color %d\n", id));
250 return false;
251 }
252 }
253 /** the spreadsheet begin position */
254 long m_spreadsheetBegin;
255 /** the spreadsheet */
256 Spreadsheet m_spreadsheet;
257 /** the spreadsheet name */
258 std::string m_spreadsheetName;
259 /** the list of chart */
260 std::vector<std::shared_ptr<Chart> > m_chartList;
261 /** the type entry map */
262 std::multimap<std::string, MWAWEntry> m_typeEntryMap;
263 int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
264
265 int m_headerHeight /** the header height if known */,
266 m_footerHeight /** the footer height if known */;
267 };
268
269 ////////////////////////////////////////
270 //! Internal: the subdocument of a BeagleWksSSParser
271 class SubDocument final : public MWAWSubDocument
272 {
273 public:
274 SubDocument(BeagleWksSSParser &pars, MWAWInputStreamPtr &input, MWAWEntry const &entry)
275 : MWAWSubDocument(&pars, input, entry)
276 {
277 }
278
279 //! destructor
280 ~SubDocument() final {}
281
282 //! operator!=
283 bool operator!=(MWAWSubDocument const &doc) const final
284 {
285 return MWAWSubDocument::operator!=(doc);
286 }
287
288 //! the parser function
289 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
290
291 private:
292 SubDocument(SubDocument const &orig) = delete;
293 SubDocument &operator=(SubDocument const &orig) = delete;
294 };
295
296 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
297 {
298 if (!listener.get()) {
299 MWAW_DEBUG_MSG(("BeagleWksSSParserInternal::SubDocument::parse: no listener\n"));
300 return;
301 }
302 auto *parser=dynamic_cast<BeagleWksSSParser *>(m_parser);
303 if (!parser) {
304 MWAW_DEBUG_MSG(("BeagleWksSSParserInternal::SubDocument::parse: can not find the parser\n"));
305 return;
306 }
307 long pos = m_input->tell();
308 listener->setFont(MWAWFont(3,12)); // fixme
309 parser->sendText(m_zone, true);
310 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
311 }
312
313 }
314
315
316 ////////////////////////////////////////////////////////////
317 // constructor/destructor, ...
318 ////////////////////////////////////////////////////////////
319 BeagleWksSSParser::BeagleWksSSParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
320 : MWAWSpreadsheetParser(input, rsrcParser, header)
321 , m_state()
322 , m_structureManager()
323 {
324 init();
325 }
326
327 BeagleWksSSParser::~BeagleWksSSParser()
328 {
329 }
330
331 void BeagleWksSSParser::init()
332 {
333 resetSpreadsheetListener();
334 setAsciiName("main-1");
335
336 m_state.reset(new BeagleWksSSParserInternal::State);
337 m_structureManager.reset(new BeagleWksStructManager(getParserState()));
338
339 // reduce the margin (in case, the page is not defined)
340 getPageSpan().setMargins(0.1);
341 }
342
343 MWAWInputStreamPtr BeagleWksSSParser::rsrcInput()
344 {
345 return getRSRCParser()->getInput();
346 }
347
348 libmwaw::DebugFile &BeagleWksSSParser::rsrcAscii()
349 {
350 return getRSRCParser()->ascii();
351 }
352
353 ////////////////////////////////////////////////////////////
354 // position and height
355 ////////////////////////////////////////////////////////////
356 MWAWVec2f BeagleWksSSParser::getPageLeftTop() const
357 {
358 return MWAWVec2f(float(getPageSpan().getMarginLeft()),
359 float(getPageSpan().getMarginTop()+m_state->m_headerHeight/72.0));
360 }
361
362 ////////////////////////////////////////////////////////////
363 // new page
364 ////////////////////////////////////////////////////////////
365 void BeagleWksSSParser::newPage(int number)
366 {
367 if (number <= m_state->m_actPage || number > m_state->m_numPages)
368 return;
369
370 while (m_state->m_actPage < number) {
371 m_state->m_actPage++;
372 if (!getSpreadsheetListener() || m_state->m_actPage == 1)
373 continue;
374 getSpreadsheetListener()->insertBreak(MWAWSpreadsheetListener::PageBreak);
375 }
376 }
377
378 ////////////////////////////////////////////////////////////
379 // the parser
380 ////////////////////////////////////////////////////////////
381 void BeagleWksSSParser::parse(librevenge::RVNGSpreadsheetInterface *docInterface)
382 {
383 if (!getInput().get() || !checkHeader(nullptr)) throw(libmwaw::ParseException());
384 bool ok = false;
385 try {
386 // create the asciiFile
387 ascii().setStream(getInput());
388 ascii().open(asciiName());
389
390 checkHeader(nullptr);
391 ok = createZones();
392 if (ok) {
393 createDocument(docInterface);
394 sendSpreadsheet();
395 }
396 ascii().reset();
397 }
398 catch (...) {
399 MWAW_DEBUG_MSG(("BeagleWksSSParser::parse: exception catched when parsing\n"));
400 ok = false;
401 }
402
403 resetSpreadsheetListener();
404 if (!ok) throw(libmwaw::ParseException());
405 }
406
407 ////////////////////////////////////////////////////////////
408 // create the document
409 ////////////////////////////////////////////////////////////
410 void BeagleWksSSParser::createDocument(librevenge::RVNGSpreadsheetInterface *documentInterface)
411 {
412 if (!documentInterface) return;
413 if (getSpreadsheetListener()) {
414 MWAW_DEBUG_MSG(("BeagleWksSSParser::createDocument: listener already exist\n"));
415 return;
416 }
417
418 // update the page
419 m_state->m_actPage = 0;
420
421 // create the page list
422 int numPages = 1;
423 m_state->m_numPages = numPages;
424
425 MWAWEntry header, footer;
426 m_structureManager->getHeaderFooterEntries(header,footer);
427 std::vector<MWAWPageSpan> pageList;
428 MWAWPageSpan ps(getPageSpan());
429 if (header.valid()) {
430 std::shared_ptr<BeagleWksSSParserInternal::SubDocument> subDoc
431 (new BeagleWksSSParserInternal::SubDocument(*this, getInput(), header));
432 MWAWHeaderFooter hf(MWAWHeaderFooter::HEADER, MWAWHeaderFooter::ALL);
433 hf.m_subDocument=subDoc;
434 ps.setHeaderFooter(hf);
435 }
436 if (footer.valid()) {
437 std::shared_ptr<BeagleWksSSParserInternal::SubDocument> subDoc
438 (new BeagleWksSSParserInternal::SubDocument(*this, getInput(), footer));
439 MWAWHeaderFooter hf(MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
440 hf.m_subDocument=subDoc;
441 ps.setHeaderFooter(hf);
442 }
443 ps.setPageSpan(numPages);
444 pageList.push_back(ps);
445
446 MWAWSpreadsheetListenerPtr listen(new MWAWSpreadsheetListener(*getParserState(), pageList, documentInterface));
447 setSpreadsheetListener(listen);
448 listen->startDocument();
449 }
450
451
452 ////////////////////////////////////////////////////////////
453 //
454 // Intermediate level
455 //
456 ////////////////////////////////////////////////////////////
457 bool BeagleWksSSParser::createZones()
458 {
459 readRSRCZones();
460 MWAWInputStreamPtr input = getInput();
461 if (input->seek(66, librevenge::RVNG_SEEK_SET) || !readPrintInfo())
462 return false;
463 long pos = input->tell();
464 if (!input->checkPosition(pos+70)) {
465 MWAW_DEBUG_MSG(("BeagleWksSSParser::createZones: the file can not contains Zones\n"));
466 return false;
467 }
468
469 // now read the list of zones
470 libmwaw::DebugStream f;
471 input->seek(pos, librevenge::RVNG_SEEK_SET);
472 f << "Entries(Zones):";
473 for (int i=0; i<7; ++i) { // checkme: at least 2 zones, maybe 7
474 MWAWEntry entry;
475 entry.setBegin(input->readLong(4));
476 entry.setLength(input->readLong(4));
477 entry.setId(static_cast<int>(input->readLong(2)));
478 if (entry.length()==0) continue;
479 entry.setType(i==1?"Frame":"Unknown");
480 f << entry.type() << "[" << entry.id() << "]="
481 << std::hex << entry.begin() << "<->" << entry.end() << ",";
482 if (!entry.valid() || !input->checkPosition(entry.end())) {
483 f << "###";
484 if (i<2) {
485 MWAW_DEBUG_MSG(("BeagleWksSSParser::createZones: can not read the header zone, stop\n"));
486 ascii().addPos(pos);
487 ascii().addNote(f.str().c_str());
488 return false;
489 }
490 MWAW_DEBUG_MSG(("BeagleWksSSParser::createZones: can not zones entry %d\n",i));
491 continue;
492 }
493 m_state->m_typeEntryMap.insert
494 (std::multimap<std::string, MWAWEntry>::value_type(entry.type(),entry));
495 }
496 ascii().addPos(pos);
497 ascii().addNote(f.str().c_str());
498
499 // now parse the different zones
500 auto it=m_state->m_typeEntryMap.find("FontNames");
501 if (it!=m_state->m_typeEntryMap.end())
502 m_structureManager->readFontNames(it->second);
503 it=m_state->m_typeEntryMap.find("Frame");
504 if (it!=m_state->m_typeEntryMap.end())
505 m_structureManager->readFrame(it->second);
506
507 // now parse the different zones
508 for (auto const &eIt : m_state->m_typeEntryMap) {
509 MWAWEntry const &entry=eIt.second;
510 if (entry.isParsed())
511 continue;
512 f.str("");
513 f << "Entries(" << entry.type() << ")[" << entry.id() << "]:";
514 ascii().addPos(entry.begin());
515 ascii().addNote(f.str().c_str());
516 ascii().addPos(entry.end());
517 ascii().addNote("_");
518 }
519
520 input->seek(m_state->m_spreadsheetBegin, librevenge::RVNG_SEEK_SET);
521 pos = input->tell();
522 if (!m_structureManager->readDocumentPreferences())
523 input->seek(pos, librevenge::RVNG_SEEK_SET);
524 pos = input->tell();
525 if (!m_structureManager->readDocumentInfo())
526 input->seek(pos, librevenge::RVNG_SEEK_SET);
527 pos = input->tell();
528 if (!readChartZone())
529 input->seek(pos, librevenge::RVNG_SEEK_SET);
530 if (!readSpreadsheet())
531 return !m_state->m_spreadsheet.m_cells.empty();
532 /* normally ends with a zone of size 25
533 with looks like 01010000010000000000000000007cffff007d0100007c0000
534 or 01010001010000000000000000000000000001000100000000
535 some flags + selection?
536 */
537 ascii().addPos(input->tell());
538 ascii().addNote("Entries(ZoneEnd)");
539 return true;
540 }
541
542 bool BeagleWksSSParser::readRSRCZones()
543 {
544 auto rsrcParser = getRSRCParser();
545 if (!rsrcParser)
546 return true;
547
548 auto const &entryMap = rsrcParser->getEntriesMap();
549 // the 1 zone
550 char const *zNames[] = {"wPos", "DMPF" };
551 for (int z = 0; z < 2; ++z) {
552 auto it = entryMap.lower_bound(zNames[z]);
553 while (it != entryMap.end()) {
554 if (it->first != zNames[z])
555 break;
556 MWAWEntry const &entry = it++->second;
557 switch (z) {
558 case 0: // 1001
559 m_structureManager->readwPos(entry);
560 break;
561 case 1: // find in one file with id=4661 6a1f 4057
562 m_structureManager->readFontStyle(entry);
563 break;
564 /* find also
565 - edpt: see sendPicture
566 - DMPP: the paragraph style
567 - sect and alis: position?, alis=filesystem alias(dir, filename, path...)
568 */
569 default:
570 break;
571 }
572 }
573 }
574 return true;
575 }
576
577 ////////////////////////////////////////////////////////////
578 // read the print info
579 ////////////////////////////////////////////////////////////
580 bool BeagleWksSSParser::readPrintInfo()
581 {
582 MWAWInputStreamPtr input = getInput();
583 long pos = input->tell();
584 if (!input->checkPosition(pos+0x70))
585 return false;
586
587 libmwaw::DebugStream f;
588 // print info
589 libmwaw::PrinterInfo info;
590 if (!info.read(input)) return false;
591 f << "Entries(PrintInfo):"<< info;
592
593 MWAWVec2i paperSize = info.paper().size();
594 MWAWVec2i pageSize = info.page().size();
595 if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
596 paperSize.x() <= 0 || paperSize.y() <= 0) return false;
597
598 // define margin from print info
599 MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
600 MWAWVec2i rBotMargin=info.paper().pos(1) - info.page().pos(1);
601
602 // move margin left | top
603 int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
604 int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
605 lTopMargin -= MWAWVec2i(decalX, decalY);
606 rBotMargin += MWAWVec2i(decalX, decalY);
607
608 // decrease right | bottom
609 int rightMarg = rBotMargin.x() -10;
610 if (rightMarg < 0) rightMarg=0;
611 int botMarg = rBotMargin.y() -50;
612 if (botMarg < 0) botMarg=0;
613
614 getPageSpan().setMarginTop(lTopMargin.y()/72.0);
615 getPageSpan().setMarginBottom(botMarg/72.0);
616 getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
617 getPageSpan().setMarginRight(rightMarg/72.0);
618 getPageSpan().setFormLength(paperSize.y()/72.);
619 getPageSpan().setFormWidth(paperSize.x()/72.);
620
621 ascii().addPos(pos);
622 ascii().addNote(f.str().c_str());
623 input->seek(pos+0x78, librevenge::RVNG_SEEK_SET);
624 if (long(input->tell()) != pos+0x78) {
625 MWAW_DEBUG_MSG(("BeagleWksSSParser::readPrintInfo: file is too short\n"));
626 return false;
627 }
628 ascii().addPos(input->tell());
629
630 return true;
631 }
632
633 ////////////////////////////////////////////////////////////
634 // chart
635 ////////////////////////////////////////////////////////////
636 bool BeagleWksSSParser::readChartZone()
637 {
638 MWAWInputStreamPtr &input= getInput();
639 long pos=input->tell();
640 if (!input->checkPosition(pos+10)) {
641 MWAW_DEBUG_MSG(("BeagleWksSSParser::readChartZone: can not find the chart zone\n"));
642 return false;
643 }
644 libmwaw::DebugStream f;
645 f << "Entries(Chart):";
646 pos = input->tell();
647 for (int i=0; i < 4; ++i) {
648 static int const expectedValues[5]= {1, 0xF, 0x4000, 0x100 };
649 auto val= static_cast<int>(input->readULong(2));
650 if (val != expectedValues[i])
651 f << "f" << i+1 << "=" << std::hex << val << std::dec << ",";
652 }
653 ascii().addPos(pos);
654 ascii().addNote(f.str().c_str());
655 while (!input->isEnd()) {
656 pos=input->tell();
657 auto sz=long(input->readULong(2));
658 if (sz==0) {
659 ascii().addPos(pos);
660 ascii().addNote("Chart:_");
661 return true;
662 }
663 input->seek(pos, librevenge::RVNG_SEEK_SET);
664 if (!readChart())
665 return false;
666 }
667 return true;
668 }
669
670 bool BeagleWksSSParser::readChart()
671 {
672 // find only 2 charts, so this code is not sure...
673 MWAWInputStreamPtr &input= getInput();
674 long pos=input->tell();
675 if (!input->checkPosition(pos+9+51+0x5d)) {
676 MWAW_DEBUG_MSG(("BeagleWksSSParser::readChart: the zone seems to short\n"));
677 return false;
678 }
679 libmwaw::DebugStream f;
680 f << "Chart-header:";
681 auto val=static_cast<int>(input->readULong(2));
682 f << "f0=" << std::hex << val << std::dec << ",";
683 val=static_cast<int>(input->readLong(2));
684 if (val!=0x12) f << "f1=" << val << ",";
685 bool findRange=false;
686 std::shared_ptr<BeagleWksSSParserInternal::Chart> chart(new BeagleWksSSParserInternal::Chart(m_state->m_spreadsheetName, *this));
687 MWAWBox2i serieRange;
688 for (int i=0; i<2; ++i) {
689 long actPos=input->tell();
690 auto dSz=static_cast<int>(input->readULong(2));
691 auto sSz=static_cast<int>(input->readULong(1));
692 if (!input->checkPosition(actPos+2+dSz) || (sSz+1!=dSz && sSz+2!=dSz)) {
693 input->seek(pos, librevenge::RVNG_SEEK_SET);
694 MWAW_DEBUG_MSG(("BeagleWksSSParser::readChart: can not read the chart\n"));
695 f << "###";
696 ascii().addPos(pos);
697 ascii().addNote(f.str().c_str());
698 return false;
699 }
700 std::string name("");
701 for (int c=0; c<sSz; ++c)
702 name += char(input->readULong(1));
703 if (i==1 && sSz>=5 && static_cast<int>(name.length())==sSz) {
704 size_t cPos=0;
705 for (int c=0; c<2; c++) {
706 if (cPos+1>=size_t(sSz)) break;
707 char ch=name[cPos++];
708 if (ch<'A' || ch>'Z') break;
709 int col=int(ch-'A')+1;
710 ch=name[cPos++];
711 if (ch>='A' && ch<='Z') {
712 col=26*col+int(ch-'A')+1;
713 if (cPos+1>=size_t(sSz)) break;
714 ch=name[cPos++];
715 }
716 if (ch<'1' || ch>'9') break;
717 int row=ch-'0';
718 while (cPos<size_t(sSz) && name[cPos]>='0' && name[cPos]<='9') {
719 row=row*10+int(name[cPos++]-'0');
720 }
721 if (c==0)
722 serieRange.setMin(MWAWVec2i(col-1,row-1));
723 else {
724 serieRange.setMax(MWAWVec2i(col-1,row-1));
725 findRange=true;
726 break;
727 }
728 if (cPos>=size_t(sSz) || name[cPos++]!=':')
729 break;
730 }
731 }
732 if (i==1 && !findRange) {
733 MWAW_DEBUG_MSG(("BeagleWksSSParser::readChart: can not decode the range\n"));
734 f << "###";
735 }
736 f << "\"" << name << "\",";
737 input->seek(actPos+2+dSz, librevenge::RVNG_SEEK_SET);
738 }
739 ascii().addPos(pos);
740 ascii().addNote(f.str().c_str());
741
742 pos=input->tell();
743 if (!input->checkPosition(pos+31)) {
744 MWAW_DEBUG_MSG(("BeagleWksSSParser::readChart: the zone B seems too short\n"));
745 return false;
746 }
747 f.str("");
748 f << "Chart-B:";
749 val=static_cast<int>(input->readLong(1));
750 MWAWChart::Axis axis[2];
751 switch (val) {
752 case 1: // show
753 axis[0].m_showLabel=axis[1].m_showLabel=true;
754 break;
755 case 2:
756 axis[0].m_showLabel=axis[1].m_showLabel=false;
757 f << "label[no],";
758 break;
759 default:
760 f << "#show[label]=" << val << ",";
761 break;
762 }
763 val=static_cast<int>(input->readLong(1));
764 if (val) f << "f0=" << val << ",";
765 val=static_cast<int>(input->readLong(1));
766 f << "f1=" << val << ",";
767 for (int i=0; i<7; ++i) {
768 int dim[2];
769 for (int &j : dim) j=static_cast<int>(input->readLong(2));
770 if (dim[0]||dim[1])
771 f << "dim" << i << "?=" << dim[0] << "x" << dim[1] << ",";
772 }
773 ascii().addPos(pos);
774 ascii().addNote(f.str().c_str());
775
776 pos=input->tell();
777 if (!input->checkPosition(pos+46)) {
778 MWAW_DEBUG_MSG(("BeagleWksSSParser::readChart: the zone C seems too short\n"));
779 return false;
780 }
781
782 f.str("");
783 f << "Chart-C:";
784 for (int i=0; i<3; ++i) {
785 int const expectedVal[3]= {0xe, 0, 0x21};
786 val=static_cast<int>(input->readLong(2));
787 if (val!=expectedVal[i]) f << "f" << i << "=" << val << ",";
788 }
789 val=static_cast<int>(input->readLong(2));
790 f << "f3=" << val << ",";
791 val=static_cast<int>(input->readLong(1));
792
793 axis[1].m_type=MWAWChart::Axis::A_Numeric;
794 switch (val) {
795 case 1: // vertical scale numeric
796 break;
797 case 2:
798 axis[1].m_type=MWAWChart::Axis::A_Logarithmic;
799 f << "yScale=log,";
800 break;
801 default:
802 f << "#yScale="<<val << ",";
803 break;
804 }
805 val=static_cast<int>(input->readLong(1));
806
807 axis[0].m_type=MWAWChart::Axis::A_Sequence;
808 switch (val) {
809 case 1: // label value no skip
810 break;
811 case 2:
812 axis[1].m_type=MWAWChart::Axis::A_Sequence;
813 f << "labelX[skipEmpty],";
814 break;
815 case 3:
816 f << "labelX[stagger],";
817 break;
818 default:
819 f << "#labelX="<<val << ",";
820 break;
821 }
822 axis[0].m_showGrid=axis[1].m_showGrid=false;
823 for (int i=0; i<4; ++i) {
824 val=static_cast<int>(input->readLong(1));
825 if (!val) continue;
826 char const *wh[4] = {"draw[grid]", "draw[value]", "auto[scale]", "flip[RowCol]"};
827 f << wh[i];
828 if (val!=1) {
829 if (i==0)
830 axis[0].m_showGrid=axis[1].m_showGrid=true;
831 f << "=" << val << ",";
832 }
833 else f << ",";
834 }
835 for (int i=0; i<3; ++i) {
836 char const *wh[3] = {"minScale", "maxScale", "step[value]"};
837 double value=0;
838 bool isNAN=false;
839 if (!input->readDouble8(value, isNAN))
840 f << "###";
841 f << wh[i] << "=" << value << ",";
842 }
843 val=static_cast<int>(input->readULong(1)); // always 0?
844 if (val) f << "g0=" << val << ",";
845 val=static_cast<int>(input->readULong(1));
846 auto serieType=MWAWChart::Series::S_Bar;
847 switch (val) {
848 case 0x19:
849 serieType=MWAWChart::Series::S_Line;
850 f << "line,";
851 break;
852 case 0x1a:
853 f << "bar,";
854 break;
855 case 0x1b:
856 serieType=MWAWChart::Series::S_Column;
857 f << "column,";
858 break;
859 case 0x1c:
860 serieType=MWAWChart::Series::S_Area;
861 f << "area,";
862 break;
863 case 0x1d:
864 // fixme (add stack)
865 f << "stack,";
866 break;
867 case 0x1e:
868 serieType=MWAWChart::Series::S_Scatter;
869 f << "scatter,";
870 break;
871 case 0x1f:
872 serieType=MWAWChart::Series::S_Pie;
873 f << "pie,";
874 break;
875 case 0x22:
876 serieType=MWAWChart::Series::S_Stock;
877 f << "hi-lo,";
878 break;
879 default:
880 f << "#type=" << std::hex << val << std::dec << ",";
881 break;
882 }
883 ascii().addDelimiter(input->tell(),'|');
884 ascii().addPos(pos);
885 ascii().addNote(f.str().c_str());
886 input->seek(pos+46, librevenge::RVNG_SEEK_SET);
887
888 pos=input->tell();
889 long endPos = pos+67;
890 if (!input->checkPosition(endPos)) {
891 MWAW_DEBUG_MSG(("BeagleWksSSParser::readChart: the zone D seems too short\n"));
892 return false;
893 }
894
895 f.str("");
896 f << "Chart-D:";
897 auto sSz=static_cast<int>(input->readULong(1));
898 if (pos+1+sSz <= endPos) {
899 MWAWChart::TextZone title;
900 title.m_type=MWAWChart::TextZone::T_Title;
901 title.m_contentType=MWAWChart::TextZone::C_Text;
902 title.m_textEntry.setBegin(pos+1);
903 title.m_textEntry.setLength(sSz);
904 chart->add(title);
905 std::string name("");
906 for (int c=0; c<sSz; ++c)
907 name += char(input->readULong(1));
908 f << "\"" << name << "\",";
909 }
910 else
911 f << "###";
912 if (findRange) {
913 chart->add(0, axis[0]);
914 chart->add(1, axis[1]);
915 chart->setDataType(serieType, true);
916 for (int r=serieRange[0][1]; r<serieRange[1][1]; ++r) {
917 MWAWChart::Series series;
918 series.m_range=MWAWBox2i(MWAWVec2i(serieRange[0][0], r),
919 MWAWVec2i(serieRange[1][0], r));
920 series.m_type=serieType;
921 auto gray=static_cast<unsigned char>((r%4)*30);
922 series.m_style.m_lineWidth=1;
923 series.m_style.m_lineColor=MWAWColor(gray,gray,gray);
924 series.m_style.setSurfaceColor(MWAWColor(gray,gray,gray));
925 chart->add(series);
926 }
927 MWAWChart::Legend legend;
928 legend.m_show=true;
929 chart->set(legend);
930 m_state->m_chartList.push_back(chart);
931 }
932 input->seek(pos+31, librevenge::RVNG_SEEK_SET);
933 ascii().addDelimiter(input->tell(),'|');
934 ascii().addPos(pos);
935 ascii().addNote(f.str().c_str());
936 input->seek(endPos, librevenge::RVNG_SEEK_SET);
937 return true;
938 }
939
940 ////////////////////////////////////////////////////////////
941 // spreadsheet
942 ////////////////////////////////////////////////////////////
943 bool BeagleWksSSParser::readSpreadsheet()
944 {
945 MWAWInputStreamPtr &input= getInput();
946 long pos=input->tell();
947 if (!input->checkPosition(pos+9)) {
948 MWAW_DEBUG_MSG(("BeagleWksSSParser::readSpreadsheet: can not find the spreadsheet zone\n"));
949 return false;
950 }
951 libmwaw::DebugStream f;
952 f << "Entries(Spreadsheet):";
953 auto val= static_cast<int>(input->readLong(2));
954 if (val!=7)
955 f << "f1=" << val << ",";
956 auto &sheet=m_state->m_spreadsheet;
957 sheet.m_numRows=static_cast<int>(input->readLong(2))+1;
958 f << "num[row]=" << sheet.m_numRows << ",";
959 val= static_cast<int>(input->readLong(2));
960 if (val!=-1)
961 f << "f2=" << val << ",";
962 for (int i=0; i < 3; ++i) { // g0=0|73|89, other 0
963 val=static_cast<int>(input->readULong(1));
964 if (val)
965 f << "g" << i << "=" << std::hex << val << std::dec << ",";
966 }
967 ascii().addPos(pos);
968 ascii().addNote(f.str().c_str());
969
970 while (readRowSheet(sheet))
971 if (input->isEnd()) break;
972 return readZone0() && readColumnWidths(sheet) && readZone0() && readFormula(sheet);
973 }
974
975 bool BeagleWksSSParser::readRowSheet(BeagleWksSSParserInternal::Spreadsheet &sheet)
976 {
977 MWAWInputStreamPtr &input= getInput();
978 long pos=input->tell();
979 libmwaw::DebugStream f;
980 f << "Entries(Row):";
981 auto row=static_cast<int>(input->readLong(2));
982 auto fSz=long(input->readULong(4));
983 long endPos=pos+6+fSz;
984 if (fSz<18 || row <= sheet.m_lastReadRow || row >= sheet.m_numRows || !input->checkPosition(endPos)) {
985 input->seek(pos, librevenge::RVNG_SEEK_SET);
986 return false;
987 }
988 sheet.m_lastReadRow=row;
989 f << "row=" << row << ",";
990 auto val=static_cast<int>(input->readLong(2));
991 if (val && val!=int(fSz))
992 f << "#sz=" << val << ",";
993 auto h=static_cast<int>(input->readLong(2));
994 if (h!=-1)
995 f << "height=" << h << ",";
996 sheet.m_heightRows.resize(size_t(row)+1, -1);
997 sheet.m_heightRows[size_t(row)]=h;
998 input->seek(10, librevenge::RVNG_SEEK_CUR); // junk
999 int N=static_cast<int>(input->readLong(2))+1;
1000 f << "N=" << N << ",";
1001 val=static_cast<int>(input->readLong(2));
1002 if (val!=fSz && val) {
1003 if (val+1 > N) N=val+1;
1004 f << "unkn=" << val << ",";
1005 }
1006 ascii().addPos(pos);
1007 ascii().addNote(f.str().c_str());
1008
1009 input->seek(pos+6+18, librevenge::RVNG_SEEK_SET);
1010 input->pushLimit(endPos);
1011 for (int i=0; i < N; ++i) {
1012 pos=input->tell();
1013 if (pos==endPos) break;
1014 BeagleWksSSParserInternal::Cell cell(MWAWVec2i(i, row));
1015 if (!readCellSheet(cell))
1016 return false;
1017 sheet.m_cells.push_back(cell);
1018 }
1019 input->popLimit();
1020 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1021 return true;
1022 }
1023
1024 bool BeagleWksSSParser::readCellSheet(BeagleWksSSParserInternal::Cell &cell)
1025 {
1026 MWAWInputStreamPtr &input= getInput();
1027 long pos=input->tell();
1028 auto cSize=static_cast<int>(input->readULong(1));
1029 long endPos=pos+2+cSize+((cSize%2)?1:0);
1030 libmwaw::DebugStream f;
1031 f << "Entries(Cell)[" << cell.position() << "]:";
1032 if (!input->checkPosition(endPos)) {
1033 MWAW_DEBUG_MSG(("BeagleWksSSParser::readCellSheet: can not find some cell\n"));
1034 f << "###";
1035 ascii().addPos(pos);
1036 ascii().addNote(f.str().c_str());
1037 return false;
1038 }
1039 MWAWCell::Format format;
1040
1041 auto val=static_cast<int>(input->readULong(1));
1042 bool isFormula=val&0x80;
1043 if (isFormula) f << "formula,";
1044 int type=(val>>4)&7;
1045 if (val&0xf)
1046 f << "type[low]" << std::hex << (val&0xf) << std::dec << ",";
1047 if (cSize>=8) {
1048 ascii().addDelimiter(pos+2,'|');
1049 if (cSize!=8)
1050 ascii().addDelimiter(pos+10,'|');
1051 // first read the flags which indicated what is filled
1052 input->seek(pos+9, librevenge::RVNG_SEEK_SET);
1053 auto wh=static_cast<int>(input->readULong(1));
1054 if (wh) {
1055 f << "style=[";
1056 input->seek(pos+2, librevenge::RVNG_SEEK_SET);
1057 MWAWFont font(3);
1058 val=static_cast<int>(input->readLong(2));
1059 if (val>0)
1060 font.setSize(float(val));
1061 val=static_cast<int>(input->readLong(2));
1062 if (val>=0)
1063 font.setId(val);
1064 auto flag = static_cast<int>(input->readULong(1));
1065 uint32_t flags=0;
1066 MWAWColor col;
1067 if (flag&7 && m_state->getColor(flag&7, col))
1068 font.setColor(col);
1069 if (flag&0x8) flags |= MWAWFont::boldBit;
1070 if (flag&0x10) flags |= MWAWFont::italicBit;
1071 if (flag&0x20) font.setUnderlineStyle(MWAWFont::Line::Simple);
1072 if (flag&0x40) flags |= MWAWFont::embossBit;
1073 if (flag&0x80) flags |= MWAWFont::shadowBit;
1074 font.setFlags(flags);
1075 cell.setFont(font);
1076 f << font.getDebugString(getParserState()->m_fontConverter);
1077 auto form=static_cast<int>(input->readULong(1));
1078 if (form) {
1079 if (form & 0x10)
1080 format.m_thousandHasSeparator=true;
1081 switch (form>>5) {
1082 case 0: // generic
1083 break;
1084 case 1:
1085 format.m_format=MWAWCell::F_NUMBER;
1086 format.m_numberFormat=MWAWCell::F_NUMBER_CURRENCY;
1087 break;
1088 case 2:
1089 format.m_format=MWAWCell::F_NUMBER;
1090 format.m_numberFormat=MWAWCell::F_NUMBER_PERCENT;
1091 break;
1092 case 3:
1093 format.m_format=MWAWCell::F_NUMBER;
1094 format.m_numberFormat=MWAWCell::F_NUMBER_SCIENTIFIC;
1095 break;
1096 case 4:
1097 format.m_format=MWAWCell::F_NUMBER;
1098 format.m_numberFormat=MWAWCell::F_NUMBER_DECIMAL;
1099 break;
1100 case 5:
1101 format.m_format=MWAWCell::F_DATE;
1102 format.m_DTFormat="%m/%d/%y";
1103 break;
1104 case 6:
1105 switch (form & 0x7) {
1106 case 0:
1107 format.m_format=MWAWCell::F_DATE;
1108 format.m_DTFormat="%b %d, %Y";
1109 break;
1110 case 1:
1111 format.m_format=MWAWCell::F_DATE;
1112 format.m_DTFormat="%B %d, %Y";
1113 break;
1114 case 2:
1115 format.m_format=MWAWCell::F_DATE;
1116 format.m_DTFormat="%a, %b %d, %Y";
1117 break;
1118 case 3:
1119 format.m_format=MWAWCell::F_DATE;
1120 format.m_DTFormat="%A, %B %d, %Y";
1121 break;
1122 case 4:
1123 format.m_format=MWAWCell::F_TIME;
1124 format.m_DTFormat="%I:%M %p";
1125 break;
1126 case 5:
1127 format.m_format=MWAWCell::F_TIME;
1128 format.m_DTFormat="%I:%M:%S %p";
1129 break;
1130 case 6:
1131 format.m_format=MWAWCell::F_TIME;
1132 format.m_DTFormat="%H:%M";
1133 break;
1134 case 7:
1135 format.m_format=MWAWCell::F_TIME;
1136 format.m_DTFormat="%H:%M:%S";
1137 break;
1138 default:
1139 break;
1140 }
1141 form &= 0x8;
1142 break;
1143 default:
1144 f << "#form=7:";
1145 break;
1146 }
1147 if (form & 0xf)
1148 format.m_digits=(form & 0xf);
1149 f << format;
1150 }
1151 val=static_cast<int>(input->readULong(1));
1152 if (val>>4) {
1153 int cId=(val>>4);
1154 if (cId<5) { // first five gray colors, white, lightgray, ... black
1155 auto gray=static_cast<unsigned char>((4-cId)*0x40);
1156 cell.setBackgroundColor(MWAWColor(gray,gray,gray));
1157 }
1158 else if (m_state->getColor(cId-3,col))
1159 cell.setBackgroundColor(col);
1160 f << "color[back]=" << cell.backgroundColor() << ",";
1161 }
1162 if (val&0xf) {
1163 int borders=0;
1164 f << "bord=[";
1165 if (val&1) {
1166 borders|=libmwaw::LeftBit;
1167 f << "left,";
1168 }
1169 if (val&2) {
1170 borders|=libmwaw::RightBit;
1171 f << "right,";
1172 }
1173 if (val&4) {
1174 borders|=libmwaw::TopBit;
1175 f << "top,";
1176 }
1177 if (val&8) {
1178 borders|=libmwaw::BottomBit;
1179 f << "bottom,";
1180 }
1181 f << "],";
1182 cell.setBorders(borders, MWAWBorder());
1183 }
1184 switch ((wh>>5)&3) {
1185 case 0:
1186 cell.setHAlignment(MWAWCell::HALIGN_LEFT);
1187 f << "left,";
1188 break;
1189 case 1:
1190 cell.setHAlignment(MWAWCell::HALIGN_RIGHT);
1191 f << "right,";
1192 break;
1193 case 2:
1194 cell.setHAlignment(MWAWCell::HALIGN_CENTER);
1195 f << "center,";
1196 break;
1197 case 3: // default
1198 default:
1199 break;
1200 }
1201 if (wh&0x9F)
1202 f << "wh=" << std::hex << (wh&0x9F) << std::dec;
1203 f << "],";
1204 }
1205 input->seek(pos+10, librevenge::RVNG_SEEK_SET);
1206 }
1207 auto &content=cell.m_content;
1208 switch (type) {
1209 case 0:
1210 f << "_";
1211 if (cSize) {
1212 MWAW_DEBUG_MSG(("BeagleWksSSParser::readCellSheet: find some data for empty cell\n"));
1213 f << "###";
1214 break;
1215 }
1216 cell.m_isEmpty=true;
1217 break;
1218 case 1:
1219 case 3:
1220 case 4: {
1221 if (type==1)
1222 f << "number,";
1223 else if (type==3) {
1224 format.m_format=MWAWCell::F_BOOLEAN;
1225 f << "bool,";
1226 }
1227 else {
1228 content.setValue(std::numeric_limits<double>::quiet_NaN());
1229 f << "error,"; // then followed by the nan type?
1230 }
1231 if (cSize<18+(isFormula?2:0)) {
1232 MWAW_DEBUG_MSG(("BeagleWksSSParser::readCellSheet: the number size seems odd\n"));
1233 f << "###";
1234 break;
1235 }
1236 if (isFormula) {
1237 content.m_contentType=MWAWCellContent::C_FORMULA;
1238 if (format.m_format==MWAWCell::F_UNKNOWN)
1239 format.m_format=MWAWCell::F_NUMBER;
1240 cell.m_formula=static_cast<int>(input->readULong(2));
1241 f << "id[formula]=" << cell.m_formula << ",";
1242 }
1243 else
1244 content.m_contentType=MWAWCellContent::C_NUMBER;
1245 double value;
1246 bool isNan;
1247 if (!input->readDouble10(value, isNan)) {
1248 MWAW_DEBUG_MSG(("BeagleWksSSParser::readCellSheet: can not read a number\n"));
1249 f << "###";
1250 break;
1251 }
1252 else if (type!=4) {
1253 // change the reference date from 1/1/1904 to 1/1/1900
1254 if (format.m_format==MWAWCell::F_DATE)
1255 content.setValue(value+1460.);
1256 else if (format.m_format==MWAWCell::F_TIME)
1257 content.setValue(std::fmod(value,1));
1258 else
1259 content.setValue(value);
1260 }
1261 f << value << ",";
1262 break;
1263 }
1264 case 2: {
1265 f << "text,";
1266 if (cSize<8+(isFormula?2:0)) {
1267 MWAW_DEBUG_MSG(("BeagleWksSSParser::readCellSheet: text data size seems odd\n"));
1268 f << "###";
1269 break;
1270 }
1271 if (isFormula) {
1272 content.m_contentType=MWAWCellContent::C_FORMULA;
1273 if (format.m_format==MWAWCell::F_UNKNOWN)
1274 format.m_format=MWAWCell::F_TEXT;
1275 cell.m_formula=static_cast<int>(input->readULong(2));
1276 f << "id[formula]=" << cell.m_formula << ",";
1277 }
1278 else
1279 content.m_contentType=MWAWCellContent::C_TEXT;
1280 content.m_textEntry.setBegin(input->tell());
1281 content.m_textEntry.setLength(cSize-8-(isFormula?2:0));
1282 std::string text("");
1283 for (long i=0; i < content.m_textEntry.length(); ++i)
1284 text+=char(input->readULong(1));
1285 f << "\"" << text << "\",";
1286 break;
1287 }
1288 default:
1289 MWAW_DEBUG_MSG(("BeagleWksSSParser::readCellSheet: find unknown type for cell\n"));
1290 f << "#type=" << std::hex << type << std::dec << ",";
1291 break;
1292 }
1293 cell.setFormat(format);
1294 if (input->tell()!=endPos)
1295 ascii().addDelimiter(input->tell(), '|');
1296 ascii().addPos(pos);
1297 ascii().addNote(f.str().c_str());
1298 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1299 return true;
1300 }
1301
1302 ////////////////////////////////////////////////////////////
1303 // unknown zone
1304 ////////////////////////////////////////////////////////////
1305 bool BeagleWksSSParser::readZone0()
1306 {
1307 MWAWInputStreamPtr &input= getInput();
1308 long pos=input->tell();
1309 libmwaw::DebugStream f;
1310 f << "Entries(Zone0):";
1311 auto N=static_cast<int>(input->readULong(2));
1312 if (!input->checkPosition(pos+8+3*N)) {
1313 input->seek(pos, librevenge::RVNG_SEEK_SET);
1314 return false;
1315 }
1316 f << "unkn=[";
1317 for (int i=0; i < N; ++i) {
1318 f << input->readLong(2) << ":" << std::hex << input->readULong(1) << std::dec << ",";
1319 }
1320 f << "],";
1321 ascii().addPos(pos);
1322 ascii().addNote(f.str().c_str());
1323 return true;
1324 }
1325
1326 bool BeagleWksSSParser::readColumnWidths(BeagleWksSSParserInternal::Spreadsheet &sheet)
1327 {
1328 MWAWInputStreamPtr &input= getInput();
1329 long pos=input->tell();
1330 libmwaw::DebugStream f;
1331 f << "Entries(Columns):";
1332 auto N=static_cast<int>(input->readLong(2));
1333 f << "N=" << N << ",";
1334 auto val=static_cast<int>(input->readLong(2));
1335 if (val) f << "f0=" << val << ",";
1336 auto dSz=static_cast<int>(input->readULong(2));
1337 if (N<-1 || (N>=0 && dSz<=0) || !input->checkPosition(pos+6+(N+1)*dSz)) {
1338 MWAW_DEBUG_MSG(("BeagleWksSSParser::Zone&: header seems odd\n"));
1339 f << "###";
1340 ascii().addPos(pos);
1341 ascii().addNote(f.str().c_str());
1342 return false;
1343 }
1344 ascii().addPos(pos);
1345 ascii().addNote(f.str().c_str());
1346 sheet.m_widthCols.resize(0);
1347 for (int i=0; i <= N; ++i) {
1348 pos = input->tell();
1349 f.str("");
1350 f << "Columns-" << i << ":";
1351 if (dSz >= 8) {
1352 val=static_cast<int>(input->readLong(2));
1353 if (val==1) f << "set,";
1354 else if (val) f << "#set=" << val << ",";
1355 auto w=static_cast<int>(input->readLong(2));
1356 sheet.m_widthCols.push_back(w);
1357 if (w>0)
1358 f << "w=" << w << ",";
1359 for (int j=0; j<2; ++j) {
1360 val=static_cast<int>(input->readULong(2));
1361 if (val!=0xFFFF)
1362 f << "f" << j << "=" << std::hex << val << std::dec << ",";
1363 }
1364 }
1365 if (input->tell()!=pos && input->tell()!=pos+dSz)
1366 ascii().addDelimiter(input->tell(),'|');
1367 input->seek(pos+dSz, librevenge::RVNG_SEEK_SET);
1368 ascii().addPos(pos);
1369 ascii().addNote(f.str().c_str());
1370 }
1371 return true;
1372 }
1373
1374 bool BeagleWksSSParser::readFormula(BeagleWksSSParserInternal::Spreadsheet &sheet)
1375 {
1376 MWAWInputStreamPtr &input=getInput();
1377 libmwaw::DebugStream f;
1378 int id=0;
1379 while (!input->isEnd()) {
1380 long pos=input->tell();
1381 if (!input->checkPosition(pos+6))
1382 break;
1383 f.str("");
1384 if (id==0)
1385 f << "Entries(Formula):";
1386 else
1387 f << "Formula-" << id << ":";
1388 ++id;
1389 auto row=static_cast<int>(input->readULong(2));
1390 auto col=static_cast<int>(input->readULong(2));
1391 if (row==0x4000 && col==0x4000) {
1392 f << "last,";
1393 ascii().addPos(pos);
1394 ascii().addNote(f.str().c_str());
1395 return true;
1396 }
1397 f << "pos=" << col << "x" << row << ",";
1398 auto dataSz=static_cast<int>(input->readULong(2));
1399 if (!dataSz || !input->checkPosition(pos+6+dataSz)) {
1400 input->seek(pos, librevenge::RVNG_SEEK_SET);
1401 break;
1402 }
1403 std::vector<MWAWCellContent::FormulaInstruction> formula;
1404 std::string error("");
1405 if (m_structureManager->readFormula(pos+6+dataSz, MWAWVec2i(col,row), formula, error))
1406 sheet.addFormula(MWAWVec2i(col,row), formula);
1407 else
1408 f << "###";
1409 for (auto const &fo : formula)
1410 f << fo;
1411 if (input->tell()!=pos+6+dataSz)
1412 ascii().addDelimiter(input->tell(),'|');
1413 input->seek(pos+6+dataSz, librevenge::RVNG_SEEK_SET);
1414 ascii().addPos(pos);
1415 ascii().addNote(f.str().c_str());
1416 }
1417 return true;
1418 }
1419
1420 ////////////////////////////////////////////////////////////
1421 // send data
1422 ////////////////////////////////////////////////////////////
1423 bool BeagleWksSSParser::sendPageFrames()
1424 {
1425 auto const &frameMap = m_structureManager->getIdFrameMap();
1426 for (auto const &it : frameMap)
1427 sendFrame(it.second);
1428 return true;
1429 }
1430
1431 bool BeagleWksSSParser::sendFrame(BeagleWksStructManager::Frame const &frame)
1432 {
1433 MWAWPosition fPos(MWAWVec2f(0,0), frame.m_dim, librevenge::RVNG_POINT);
1434
1435 fPos.setPagePos(frame.m_page > 0 ? frame.m_page : 1, frame.m_origin);
1436 fPos.setRelativePosition(MWAWPosition::Page);
1437 fPos.m_wrapping = frame.m_wrap==0 ? MWAWPosition::WNone : MWAWPosition::WDynamic;
1438
1439 MWAWGraphicStyle style=MWAWGraphicStyle::emptyStyle();
1440 style.setBorders(frame.m_bordersSet, frame.m_border);
1441 return sendPicture(frame.m_pictId, fPos, style);
1442 }
1443
1444 // read/send picture (edtp resource)
1445 bool BeagleWksSSParser::sendPicture
1446 (int pId, MWAWPosition const &pictPos, MWAWGraphicStyle const &style)
1447 {
1448 MWAWSpreadsheetListenerPtr listener=getSpreadsheetListener();
1449 if (!listener) {
1450 MWAW_DEBUG_MSG(("BeagleWksSSParser::sendPicture: can not find the listener\n"));
1451 return false;
1452 }
1453 auto rsrcParser = getRSRCParser();
1454 if (!rsrcParser) {
1455 static bool first=true;
1456 if (first) {
1457 MWAW_DEBUG_MSG(("BeagleWksSSParser::sendPicture: need access to resource fork to retrieve picture content\n"));
1458 first=false;
1459 }
1460 return true;
1461 }
1462
1463 librevenge::RVNGBinaryData data;
1464 if (!m_structureManager->readPicture(pId, data))
1465 return false;
1466
1467 listener->insertPicture(pictPos, MWAWEmbeddedObject(data, "image/pict"), style);
1468 return true;
1469 }
1470
1471 bool BeagleWksSSParser::sendText(MWAWEntry const &entry, bool /*headerFooter*/)
1472 {
1473 MWAWSpreadsheetListenerPtr listener=getSpreadsheetListener();
1474 if (!listener) {
1475 MWAW_DEBUG_MSG(("BeagleWksSSParser::sendText: can not find the listener\n"));
1476 return false;
1477 }
1478 if (!entry.valid()) {
1479 MWAW_DEBUG_MSG(("BeagleWksSSParser::sendText: can not find the entry\n"));
1480 return false;
1481 }
1482
1483 MWAWInputStreamPtr &input= getInput();
1484 long endPos=entry.end();
1485 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1486 while (!input->isEnd()) {
1487 long pos=input->tell();
1488 if (pos>=endPos) break;
1489 auto c = static_cast<unsigned char>(input->readULong(1));
1490 switch (c) {
1491 case 0x9:
1492 listener->insertTab();
1493 break;
1494 case 0xd:
1495 listener->insertEOL();
1496 break;
1497 default:
1498 listener->insertCharacter(static_cast<unsigned char>(c));
1499 break;
1500 }
1501 }
1502 return true;
1503 }
1504
1505 ////////////////////////////////////////////////////////////
1506 ////////////////////////////////////////////////////////////
1507 bool BeagleWksSSParser::sendSpreadsheet()
1508 {
1509 MWAWSpreadsheetListenerPtr listener=getSpreadsheetListener();
1510 MWAWInputStreamPtr &input= getInput();
1511 if (!listener) {
1512 MWAW_DEBUG_MSG(("BeagleWksSSParser::sendSpreadsheet: I can not find the listener\n"));
1513 return false;
1514 }
1515 auto &sheet = m_state->m_spreadsheet;
1516 sheet.updateWidthCols();
1517
1518 int prevRow = -1;
1519 listener->openSheet(sheet.convertInPoint(sheet.m_widthCols,76), librevenge::RVNG_POINT, std::vector<int>(), m_state->m_spreadsheetName);
1520 sendPageFrames();
1521 // send the chart
1522 int n=0;
1523 for (auto &chart : m_state->m_chartList) {
1524 if (!chart) continue;
1525 // chart have no position, so we create one
1526 chart->setDimension(MWAWVec2f(200,200));
1527 MWAWPosition fPos(MWAWVec2f(0,0), MWAWVec2f(200,200), librevenge::RVNG_POINT);
1528 fPos.setPagePos(1, MWAWVec2f(float(n++)*200,200));
1529 fPos.setRelativePosition(MWAWPosition::Page);
1530 listener->insertChart(fPos, *chart);
1531 }
1532 for (auto const &cell : sheet.m_cells) {
1533 if (cell.position()[1]>prevRow+1) {
1534 while (cell.position()[1] > prevRow+1) {
1535 if (prevRow != -1) listener->closeSheetRow();
1536 int numRepeat;
1537 double h=sheet.getRowHeight(prevRow+1, numRepeat);
1538 if (cell.position()[1]<prevRow+1+numRepeat)
1539 numRepeat=cell.position()[1]-1-prevRow;
1540 listener->openSheetRow(float(h), librevenge::RVNG_POINT, numRepeat);
1541 prevRow+=numRepeat;
1542 }
1543 }
1544 if (cell.position()[1] > prevRow) {
1545 if (prevRow != -1) listener->closeSheetRow();
1546 listener->openSheetRow(float(sheet.getRowHeight(++prevRow)), librevenge::RVNG_POINT);
1547 }
1548 listener->openSheetCell(cell, cell.m_content);
1549 if (cell.m_content.m_textEntry.valid()) {
1550 listener->setFont(cell.isFontSet() ? cell.getFont() : MWAWFont());
1551 input->seek(cell.m_content.m_textEntry.begin(), librevenge::RVNG_SEEK_SET);
1552 while (!input->isEnd() && input->tell()<cell.m_content.m_textEntry.end()) {
1553 auto c=static_cast<unsigned char>(input->readULong(1));
1554 if (c==0xd)
1555 listener->insertEOL();
1556 else
1557 listener->insertCharacter(c);
1558 }
1559 }
1560 listener->closeSheetCell();
1561 }
1562 if (prevRow!=-1) listener->closeSheetRow();
1563 listener->closeSheet();
1564 return true;
1565 }
1566
1567 ////////////////////////////////////////////////////////////
1568 //
1569 // Low level
1570 //
1571 ////////////////////////////////////////////////////////////
1572
1573 ////////////////////////////////////////////////////////////
1574 // read the header
1575 ////////////////////////////////////////////////////////////
1576 bool BeagleWksSSParser::checkHeader(MWAWHeader *header, bool strict)
1577 {
1578 *m_state = BeagleWksSSParserInternal::State();
1579 MWAWInputStreamPtr input = getInput();
1580 if (!input || !input->hasDataFork() || !input->checkPosition(66))
1581 return false;
1582
1583 libmwaw::DebugStream f;
1584 f << "FileHeader:";
1585
1586 input->seek(0, librevenge::RVNG_SEEK_SET);
1587 if (input->readLong(2)!=0x4257 || input->readLong(2)!=0x6b73 ||
1588 input->readLong(2)!=0x4257 || input->readLong(2)!=0x7373 ||
1589 input->readLong(2)!=0x4257 || input->readLong(2)!=0x7373) {
1590 return false;
1591 }
1592 for (int i=0; i < 9; ++i) { // f2=f6=1 other 0
1593 auto val=static_cast<int>(input->readLong(2));
1594 if (val) f << "f" << i << "=" << val << ",";
1595 }
1596 setVersion(1);
1597
1598 if (header)
1599 header->reset(MWAWDocument::MWAW_T_BEAGLEWORKS, 1, MWAWDocument::MWAW_K_SPREADSHEET);
1600
1601 ascii().addPos(0);
1602 ascii().addNote(f.str().c_str());
1603
1604 long pos=input->tell();
1605 f.str("");
1606 f << "FileHeader-II:";
1607 m_state->m_spreadsheetBegin=input->readLong(4);
1608 if (!input->checkPosition(m_state->m_spreadsheetBegin)) {
1609 MWAW_DEBUG_MSG(("BeagleWksSSParser::checkHeader: can not read the spreadsheet position\n"));
1610 return false;
1611 }
1612 f << "spreadsheet[ptr]=" << std::hex << m_state->m_spreadsheetBegin << std::dec << ",";
1613 for (int i=0; i < 11; ++i) { // f2=0x50c|58c|5ac f3=f5=9
1614 long val=input->readLong(2);
1615 if (val) f << "f" << i << "=" << std::hex << val << std::dec << ",";
1616 }
1617 MWAWEntry entry;
1618 entry.setBegin(input->readLong(4));
1619 entry.setLength(input->readLong(4));
1620 entry.setId(static_cast<int>(input->readLong(2))); // in fact nFonts
1621 entry.setType("FontNames");
1622 f << "fontNames[ptr]=" << std::hex << entry.begin() << "<->" << entry.end()
1623 << std::dec << ",nFonts=" << entry.id() << ",";
1624 if (entry.length() && (!entry.valid() || !input->checkPosition(entry.end()))) {
1625 MWAW_DEBUG_MSG(("BeagleWksSSParser::checkHeader: can not read the font names position\n"));
1626 f << "###";
1627 ascii().addPos(pos);
1628 ascii().addNote(f.str().c_str());
1629 return false;
1630 }
1631
1632 m_state->m_typeEntryMap.insert
1633 (std::multimap<std::string, MWAWEntry>::value_type(entry.type(),entry));
1634 ascii().addPos(pos);
1635 ascii().addNote(f.str().c_str());
1636 if (strict && !readPrintInfo())
1637 return false;
1638 ascii().addPos(66);
1639 ascii().addNote("_");
1640
1641 return true;
1642 }
1643
1644 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: