Debian Codegraph @

src/lib/QuattroDos.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) 2006, 2007 Andrew Ziem
11 * Copyright (C) 2004 Marc Maurer ([email protected])
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 *
14 * For minor contributions see the git repository.
15 *
16 * Alternatively, the contents of this file may be used under the terms
17 * of the GNU Lesser General Public License Version 2.1 or later
18 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
19 * applicable instead of those above.
20 */
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <algorithm>
26 #include <cmath>
27 #include <sstream>
28
29 #include <librevenge-stream/librevenge-stream.h>
30
31 #include "libwps_internal.h"
32 #include "libwps_tools_win.h"
33
34 #include "WKSContentListener.h"
35 #include "WKSSubDocument.h"
36
37 #include "WPSCell.h"
38 #include "WPSEntry.h"
39 #include "WPSFont.h"
40 #include "WPSHeader.h"
41 #include "WPSPageSpan.h"
42
43 #include "QuattroDosChart.h"
44 #include "QuattroDosSpreadsheet.h"
45
46 #include "QuattroDos.h"
47
48 using namespace libwps;
49
50 //! Internal: namespace to define internal class of QuattroDosParser
51 namespace QuattroDosParserInternal
52 {
53 //! the font of a QuattroDosParser
54 struct Font final : public WPSFont
55 {
56 //! constructor
57 explicit Font(libwps_tools_win::Font::Type type) : WPSFont(), m_type(type)
58 {
59 }
60 Font(Font const &)=default;
61 Font(Font &&)=default;
62 Font &operator=(Font const &)=default;
63 Font &operator=(Font &&)=default;
64 //! destructor
65 ~Font() final;
66 //! font encoding type
67 libwps_tools_win::Font::Type m_type;
68 };
69
70 Font::~Font()
71 {
72 }
73
74 //! Internal: the subdocument of a WPS4Parser
75 class SubDocument final : public WKSSubDocument
76 {
77 public:
78 //! constructor for a text entry
79 SubDocument(RVNGInputStreamPtr const &input, QuattroDosParser &pars, bool header) :
80 WKSSubDocument(input, &pars), m_header(header) {}
81 //! destructor
82 ~SubDocument() final {}
83
84 //! operator==
85 bool operator==(std::shared_ptr<WPSSubDocument> const &doc) const final
86 {
87 if (!doc || !WKSSubDocument::operator==(doc))
88 return false;
89 auto const *sDoc = dynamic_cast<SubDocument const *>(doc.get());
90 if (!sDoc) return false;
91 return m_header == sDoc->m_header;
92 }
93
94 //! the parser function
95 void parse(std::shared_ptr<WKSContentListener> &listener, libwps::SubDocumentType subDocumentType) final;
96 //! a flag to known if we need to send the header or the footer
97 bool m_header;
98 };
99
100 void SubDocument::parse(std::shared_ptr<WKSContentListener> &listener, libwps::SubDocumentType)
101 {
102 if (!listener.get())
103 {
104 WPS_DEBUG_MSG(("QuattroDosParserInternal::SubDocument::parse: no listener\n"));
105 return;
106 }
107 if (!dynamic_cast<WKSContentListener *>(listener.get()))
108 {
109 WPS_DEBUG_MSG(("QuattroDosParserInternal::SubDocument::parse: bad listener\n"));
110 return;
111 }
112
113 QuattroDosParser *pser = m_parser ? dynamic_cast<QuattroDosParser *>(m_parser) : nullptr;
114 if (!pser)
115 {
116 listener->insertCharacter(' ');
117 WPS_DEBUG_MSG(("QuattroDosParserInternal::SubDocument::parse: bad parser\n"));
118 return;
119 }
120 pser->sendHeaderFooter(m_header);
121 }
122
123 //! the state of QuattroDosParser
124 struct State
125 {
126 //! constructor
127 explicit State(libwps_tools_win::Font::Type fontType)
128 : m_eof(-1)
129 , m_fontType(fontType)
130 , m_version(-1)
131 , m_hasLICSCharacters(false)
132 , m_fontsList()
133 , m_idToFileNameMap()
134 , m_pageSpan()
135 , m_actPage(0)
136 , m_numPages(0)
137 , m_headerString("")
138 , m_footerString("")
139 {
140 }
141 //! returns a color corresponding to an id
142 static bool getColor(int id, WPSColor &color);
143 //! return the default font style
144 libwps_tools_win::Font::Type getDefaultFontType() const
145 {
146 if (m_hasLICSCharacters && m_version<=2)
147 return libwps_tools_win::Font::LICS;
148 if (m_fontType != libwps_tools_win::Font::UNKNOWN)
149 return m_fontType;
150 if (m_version>2)
151 return libwps_tools_win::Font::WIN3_WEUROPE;
152 return libwps_tools_win::Font::CP_437;
153 }
154
155 //! returns a default font (Courier12) with file's version to define the default encoding */
156 WPSFont getDefaultFont() const
157 {
158 WPSFont res;
159 if (m_version <= 2)
160 res.m_name="Courier";
161 else
162 res.m_name="Times New Roman";
163 res.m_size=12;
164 return res;
165 }
166
167 //! the last file position
168 long m_eof;
169 //! the user font type
170 libwps_tools_win::Font::Type m_fontType;
171 //! the file version
172 int m_version;
173 //! flag to know if the character
174 bool m_hasLICSCharacters;
175 //! the fonts list
176 std::vector<Font> m_fontsList;
177 //! map id to filename
178 std::map<int, librevenge::RVNGString> m_idToFileNameMap;
179 //! the actual document size
180 WPSPageSpan m_pageSpan;
181 int m_actPage /** the actual page*/, m_numPages /* the number of pages */;
182 //! the header string
183 std::string m_headerString;
184 //! the footer string
185 std::string m_footerString;
186 };
187
188 bool State::getColor(int id, WPSColor &color)
189 {
190 static const uint32_t quattroColorMap[]=
191 {
192 0x000000, 0x0000FF, 0x00FF00, 0x00FFFF,
193 0xFF0000, 0xFF00FF, 0x996633/*brown*/, 0xFFFFFF,
194 0x808080 /* gray */, 0x0000C0, 0x00C000, 0x00C0C0,
195 0xC00000, 0xC000C0, 0xFFFF00, 0xC0C0C0 /* bright white */
196 };
197 if (id < 0 || id >= 16)
198 {
199 WPS_DEBUG_MSG(("QuattroDosParserInternal::State::getColor(): unknown Quattro Pro color id: %d\n",id));
200 return false;
201 }
202 color = WPSColor(quattroColorMap[id]);
203 return true;
204 }
205
206 }
207
208 // constructor, destructor
209 QuattroDosParser::QuattroDosParser(RVNGInputStreamPtr &input, WPSHeaderPtr &header,
210 libwps_tools_win::Font::Type encoding)
211 : WKSParser(input, header)
212 , m_listener()
213 , m_state(new QuattroDosParserInternal::State(encoding))
214 , m_spreadsheetParser(new QuattroDosSpreadsheet(*this))
215 , m_chartParser(new QuattroDosChart(*this))
216 {
217 }
218
219 QuattroDosParser::~QuattroDosParser()
220 {
221 }
222
223 int QuattroDosParser::version() const
224 {
225 return m_state->m_version;
226 }
227
228 bool QuattroDosParser::checkFilePosition(long pos)
229 {
230 if (m_state->m_eof < 0)
231 {
232 RVNGInputStreamPtr input = getInput();
233 long actPos = input->tell();
234 input->seek(0, librevenge::RVNG_SEEK_END);
235 m_state->m_eof=input->tell();
236 input->seek(actPos, librevenge::RVNG_SEEK_SET);
237 }
238 return pos <= m_state->m_eof;
239 }
240
241 libwps_tools_win::Font::Type QuattroDosParser::getDefaultFontType() const
242 {
243 return m_state->getDefaultFontType();
244 }
245
246 //////////////////////////////////////////////////////////////////////
247 // interface with QuattroDosChart
248 //////////////////////////////////////////////////////////////////////
249 bool QuattroDosParser::sendChart(int sheetId, Vec2i const &cell, Vec2f const &chartSize)
250 {
251 return m_chartParser->sendChart(sheetId, cell, chartSize);
252 }
253
254 //////////////////////////////////////////////////////////////////////
255 // interface with QuattroDosSpreadsheet
256 //////////////////////////////////////////////////////////////////////
257 librevenge::RVNGString QuattroDosParser::getSheetName(int id) const
258 {
259 return m_spreadsheetParser->getSheetName(id);
260 }
261
262 bool QuattroDosParser::getColor(int id, WPSColor &color) const
263 {
264 return m_state->getColor(id, color);
265 }
266
267 bool QuattroDosParser::getFont(int id, WPSFont &font, libwps_tools_win::Font::Type &type) const
268 {
269 if (id < 0 || id>=int(m_state->m_fontsList.size()))
270 {
271 WPS_DEBUG_MSG(("QuattroDosParser::getFont: can not find font %d\n", id));
272 return false;
273 }
274 auto const &ft=m_state->m_fontsList[size_t(id)];
275 font=ft;
276 type=ft.m_type;
277 return true;
278 }
279
280 librevenge::RVNGString QuattroDosParser::getFileName(int fId) const
281 {
282 auto it = m_state->m_idToFileNameMap.find(fId);
283 if (it!=m_state->m_idToFileNameMap.end())
284 return it->second;
285 WPS_DEBUG_MSG(("QuattroDosParser::parse: can not find %d name\n", fId));
286 return "";
287 }
288
289 // main function to parse the document
290 void QuattroDosParser::parse(librevenge::RVNGSpreadsheetInterface *documentInterface)
291 {
292 RVNGInputStreamPtr input=getInput();
293 if (!input)
294 {
295 WPS_DEBUG_MSG(("QuattroDosParser::parse: does not find main ole\n"));
296 throw (libwps::ParseException());
297 }
298
299 if (!checkHeader(nullptr, true)) throw(libwps::ParseException());
300
301 bool ok=false;
302 try
303 {
304 ascii().setStream(input);
305 ascii().open("MN0");
306
307 if (checkHeader(nullptr) && readZones())
308 m_listener=createListener(documentInterface);
309 if (m_listener)
310 {
311 m_chartParser->setListener(m_listener);
312 m_spreadsheetParser->setListener(m_listener);
313
314 m_listener->startDocument();
315 int numSheet=std::max(m_chartParser->getNumSpreadsheets(),
316 m_spreadsheetParser->getNumSpreadsheets());
317 if (numSheet==0) ++numSheet;
318 for (int i=0; i<numSheet; ++i)
319 {
320 std::map<Vec2i,Vec2i> cellMap;
321 m_chartParser->getChartPositionMap(i, cellMap);
322 m_spreadsheetParser->sendSpreadsheet(i, cellMap);
323 }
324 m_listener->endDocument();
325 m_listener.reset();
326 ok = true;
327 }
328 }
329 catch (...)
330 {
331 WPS_DEBUG_MSG(("QuattroDosParser::parse: exception catched when parsing MN0\n"));
332 throw (libwps::ParseException());
333 }
334
335 ascii().reset();
336 if (!ok)
337 throw(libwps::ParseException());
338 }
339
340 std::shared_ptr<WKSContentListener> QuattroDosParser::createListener(librevenge::RVNGSpreadsheetInterface *interface)
341 {
342 std::vector<WPSPageSpan> pageList;
343 WPSPageSpan ps(m_state->m_pageSpan);
344 if (!m_state->m_headerString.empty())
345 {
346 WPSSubDocumentPtr subdoc(new QuattroDosParserInternal::SubDocument
347 (getInput(), *this, true));
348 ps.setHeaderFooter(WPSPageSpan::HEADER, WPSPageSpan::ALL, subdoc);
349 }
350 if (!m_state->m_footerString.empty())
351 {
352 WPSSubDocumentPtr subdoc(new QuattroDosParserInternal::SubDocument
353 (getInput(), *this, false));
354 ps.setHeaderFooter(WPSPageSpan::FOOTER, WPSPageSpan::ALL, subdoc);
355 }
356 pageList.push_back(ps);
357 return std::shared_ptr<WKSContentListener>(new WKSContentListener(pageList, interface));
358 }
359
360 ////////////////////////////////////////////////////////////
361 // low level
362 ////////////////////////////////////////////////////////////
363 // read the header
364 ////////////////////////////////////////////////////////////
365 bool QuattroDosParser::checkHeader(WPSHeader *header, bool strict)
366 {
367 *m_state = QuattroDosParserInternal::State(m_state->m_fontType);
368 libwps::DebugStream f;
369
370 RVNGInputStreamPtr input = getInput();
371 if (!checkFilePosition(12))
372 {
373 WPS_DEBUG_MSG(("QuattroDosParser::checkHeader: file is too short\n"));
374 return false;
375 }
376
377 input->seek(0,librevenge::RVNG_SEEK_SET);
378 auto firstOffset = int(libwps::readU8(input));
379 auto type = int(libwps::read8(input));
380 f << "FileHeader:";
381 if (firstOffset == 0 && type == 0)
382 m_state->m_version=1;
383 else
384 {
385 WPS_DEBUG_MSG(("QuattroDosParser::checkHeader: find unexpected first data\n"));
386 return false;
387 }
388 auto val=int(libwps::read16(input));
389 if (val==2)
390 {
391 // version
392 val=int(libwps::readU16(input));
393 if (val==0x5120)
394 {
395 m_state->m_version=1;
396 f << "quattropro[wq1],";
397 }
398 else if (val==0x5121)
399 {
400 m_state->m_version=2;
401 f << "quattropro[wq2],";
402 }
403 else
404 {
405 WPS_DEBUG_MSG(("QuattroDosParser::checkHeader: find unknown file version\n"));
406 return false;
407 }
408 }
409 else
410 {
411 WPS_DEBUG_MSG(("QuattroDosParser::checkHeader: header contain unexpected size field data\n"));
412 return false;
413 }
414
415 input->seek(0, librevenge::RVNG_SEEK_SET);
416 if (strict)
417 {
418 for (int i=0; i < 4; ++i)
419 {
420 if (!readZone()) return false;
421 }
422 }
423 ascii().addPos(0);
424 ascii().addNote(f.str().c_str());
425
426 if (header)
427 {
428 header->setMajorVersion(uint8_t(m_state->m_version));
429 header->setCreator(libwps::WPS_QUATTRO_PRO);
430 header->setKind(libwps::WPS_SPREADSHEET);
431 header->setNeedEncoding(true);
432 }
433 return true;
434 }
435
436 bool QuattroDosParser::readZones()
437 {
438 RVNGInputStreamPtr input = getInput();
439 input->seek(0, librevenge::RVNG_SEEK_SET);
440 while (readZone()) ;
441
442 //
443 // look for ending
444 //
445 long pos = input->tell();
446 if (!checkFilePosition(pos+4))
447 {
448 WPS_DEBUG_MSG(("QuattroDosParser::readZones: cell header is too short\n"));
449 return m_spreadsheetParser->getNumSpreadsheets()>0;
450 }
451 auto type = int(libwps::readU16(input)); // 1
452 auto length = int(libwps::readU16(input));
453 if (length)
454 {
455 WPS_DEBUG_MSG(("QuattroDosParser::readZones: parse breaks before ending\n"));
456 ascii().addPos(pos);
457 ascii().addNote("Entries(BAD):###");
458 return m_spreadsheetParser->getNumSpreadsheets()>0;
459 }
460
461 ascii().addPos(pos);
462 if (type != 1)
463 {
464 WPS_DEBUG_MSG(("QuattroDosParser::readZones: odd end cell type: %d\n", type));
465 ascii().addNote("Entries(BAD):###");
466 }
467 else
468 ascii().addNote("__End");
469
470 return true;
471 }
472
473 bool QuattroDosParser::readZone()
474 {
475 libwps::DebugStream f;
476 RVNGInputStreamPtr input = getInput();
477 long pos = input->tell();
478 auto id = int(libwps::readU8(input));
479 auto type = int(libwps::read8(input));
480 auto sz = long(libwps::readU16(input));
481 if (sz<0 || !checkFilePosition(pos+4+sz))
482 {
483 WPS_DEBUG_MSG(("QuattroDosParser::readZone: size is bad\n"));
484 input->seek(pos, librevenge::RVNG_SEEK_SET);
485 return false;
486 }
487
488 if (type!=0)
489 {
490 input->seek(pos, librevenge::RVNG_SEEK_SET);
491 return false;
492 }
493
494 f << "Entries(Struct" << std::hex << id << std::dec << "E):";
495 bool ok = true, isParsed = false, needWriteInAscii = false;
496 int val;
497 input->seek(pos, librevenge::RVNG_SEEK_SET);
498 switch (id)
499 {
500 /* also
501 32: symphony windows settings(144)
502 37: password checksum(4)
503 3c: query(127)
504 3d: query name(16)
505 3e: symphony print record (679)
506 3f: printer name(16)
507 40: symphony graph record (499)
508 42: zoom(9)
509 43: number of split windows(2)
510 44: number of screen row(2)
511 45: number of screen column(2)
512 46: name ruler range(25)
513 47: name sheet range(25)
514 48: autoload comm(65)
515 49: autoexecutute macro adress(8)
516 4a: query parse information
517 */
518 case 0:
519 if (sz!=2) break;
520 f.str("");
521 f << "version=" << std::hex << libwps::readU16(input) << std::dec << ",";
522 isParsed=needWriteInAscii=true;
523 break;
524 case 0x1: // EOF
525 ok = false;
526 break;
527 // boolean
528 case 0x2: // Calculation mode 0 or FF
529 case 0x3: // Calculation order
530 case 0x4: // Split window type
531 case 0x5: // Split window syn
532 case 0x29: // label format 22|27|5e (spreadsheet)
533 case 0x30: // formatted/unformatted print 0|ff (spreadsheet)
534 case 0x31: // cursor/location 1|2
535 case 0x38: // lock
536 f.str("");
537 f << "Entries(Byte" << std::hex << id << std::dec << "Z):";
538 if (sz!=1) break;
539 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
540 val=int(libwps::readU8(input));
541 if (id==0x29)
542 f << "val=" << std::hex << val << std::dec << ",";
543 else if (id==0x31)
544 {
545 if (val!=1) f << val << ",";
546 }
547 else
548 {
549 if (val==0xFF) f << "true,";
550 else if (val) f << "#val=" << val << ",";
551 }
552 isParsed=needWriteInAscii=true;
553 break;
554 case 0x6: // active worksheet range
555 ok = m_spreadsheetParser->readSheetSize();
556 isParsed = true;
557 break;
558 case 0x7: // window 1 record
559 case 0x9: // window 2 record
560 ok = readWindowRecord();
561 isParsed=true;
562 break;
563 case 0x8: // col width
564 ok = m_spreadsheetParser->readColumnSize();
565 isParsed = true;
566 break;
567 case 0xa: // col width (window 2)
568 if (sz!=3) break;
569 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
570 // varies in this file from 0 to 5
571 f << "id=" << libwps::read16(input) << ",";
572 // small number 9-13: a dim?
573 f << "dim?=" << libwps::read8(input) << ",";
574 isParsed=needWriteInAscii=true;
575 break;
576 case 0xb: // named range
577 ok = readFieldName();
578 isParsed=true;
579 break;
580 case 0xc: // blank cell
581 case 0xd: // integer cell
582 case 0xe: // floating cell
583 case 0xf: // label cell
584 case 0x10: // formula cell
585 ok = m_spreadsheetParser->readCell();
586 isParsed = true;
587 break;
588 case 0x33: // value of string formula
589 ok = m_spreadsheetParser->readCellFormulaResult();
590 isParsed = true;
591 break;
592 // some spreadsheet zone ( mainly flags )
593 case 0x18: // data table range
594 case 0x19: // query range
595 case 0x20: // distribution range
596 case 0x27: // print setup
597 case 0x2a: // print borders
598 ok = readUnknown1();
599 isParsed=true;
600 break;
601 case 0x1a: // print range
602 case 0x1b: // sort range
603 case 0x1c: // fill range
604 case 0x1d: // primary sort key range
605 case 0x23: // secondary sort key range
606 {
607 int expectedSz=8;
608 f.str("");
609 switch (id)
610 {
611 case 0x1a: // only in spreadsheet?
612 f << "Entries(PrintRange):";
613 break;
614 case 0x1b: // a dimension or also some big selection? 31999=infinity?, related to report?
615 f << "Entries(SortRange):";
616 break;
617 case 0x1c: // a dimension or also some big selection? only in spreadsheet
618 f << "Entries(FillRange):";
619 break;
620 case 0x1d:
621 f << "Entries(PrimSort):";
622 expectedSz=9;
623 break;
624 case 0x23:
625 f << "Entries(SecSort):";
626 expectedSz=9;
627 break;
628 default:
629 break;
630 }
631 if (sz!=expectedSz) break;
632 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
633 int dim[4];
634 for (int &i : dim) i=int(libwps::read16(input));
635 // in a spreadsheet, the cell or the cells corresponding to the field
636 // in a database, col,0,col,0
637 if (dim[0]==-1 && dim[1]==dim[0] && dim[2]==dim[0] && dim[3]==dim[0])
638 {
639 }
640 else
641 {
642 f << "cell=" << dim[0] << "x" << dim[1];
643 if (dim[0]!=dim[2] || dim[1]!=dim[3])
644 f << "<->" << dim[2] << "x" << dim[3];
645 f << ",";
646 }
647 if (expectedSz==9)
648 {
649 val=int(libwps::readU8(input)); // 0|1|ff
650 if (val==0xFF) f << "true,";
651 else if (val) f << "val=" << val << ",";
652 }
653 isParsed=needWriteInAscii=true;
654 break;
655 }
656 case 0x24: // protection (global)
657 f.str("");
658 f << "Entries(Protection):global,";
659 if (sz!=1) break;
660 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
661 val=int(libwps::readU8(input));
662 if (val==0)
663 {
664 f.str("");
665 f << "_";
666 }
667 else if (val==0xFF) f << "protected,";
668 else f << "#protected=" << val << ",";
669 isParsed=needWriteInAscii=true;
670 break;
671 case 0x25: // footer
672 case 0x26: // header
673 readHeaderFooter(id==0x26);
674 isParsed = true;
675 break;
676 case 0x28: // print margin
677 if (sz!=10) break;
678 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
679 for (int i=0; i<5; ++i) // f1=4c|96|ac|f0
680 {
681 static const int expected[]= {4, 0x4c, 0x42, 2, 2};
682 val=int(libwps::read16(input));
683 if (val!=expected[i]) f << "f" << i << "=" << val << ",";
684 }
685 isParsed=needWriteInAscii=true;
686 break;
687 case 0x2d: // graph setting
688 case 0x2e: // named graph setting
689 m_chartParser->readChart();
690 isParsed = true;
691 break;
692 case 0x2f: // iteration count: only in dos file Wk1, Wks(dos), Wq[12] ?
693 if (sz!=1) break;
694 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
695 f.str("");
696 val=int(libwps::readU8(input));
697 f << "Entries(ItCount):dos";
698 if (val!=1) f << "=" << val << ",";
699 isParsed = needWriteInAscii = true;
700 break;
701 case 0x36: // find one time in a spreadsheet (not standard)
702 {
703 if (sz!=0x1e) break;
704 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
705 for (int i=0; i<3; ++i) // always 0?
706 {
707 val=int(libwps::read16(input));
708 if (val) f << "f" << i << "=" << val << ",";
709 }
710 // after find some junk, so..
711 isParsed = needWriteInAscii = true;
712 break;
713 }
714 case 0x64: // hidden column
715 isParsed = m_spreadsheetParser->readHiddenColumns();
716 break;
717
718 //
719 // specific to Quattro Pro file
720 //
721 case 0x97: // name use to refer to some external spreadsheet
722 readFileName();
723 isParsed=true;
724 break;
725 case 0x9b: // only in wq1 file
726 readUserFonts();
727 isParsed=true;
728 break;
729 case 0x9c:
730 {
731 f.str("");
732 f << "Entries(CellProperty)[position]:";
733 if ((sz%6)!=0) break;
734 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
735 auto N=int(sz/6);
736 f << "num=[";
737 for (int i=0; i<N; ++i)
738 {
739 auto row=int(libwps::readU16(input));
740 auto col=int(libwps::readU16(input));
741 f << "C" << col << "x" << row << ":";
742 auto num=int(libwps::readU16(input));
743 f << num << ",";
744 }
745 f << "],";
746 isParsed=needWriteInAscii=true;
747 break;
748 }
749 case 0x9d:
750 m_spreadsheetParser->readCellProperty();
751 isParsed = true;
752 break;
753 case 0xb8:
754 case 0xca:
755 m_chartParser->readChartSetType();
756 isParsed = true;
757 break;
758 case 0xb9:
759 m_chartParser->readChartName();
760 isParsed = true;
761 break;
762 case 0xc9:
763 m_spreadsheetParser->readUserStyle();
764 isParsed = true;
765 break;
766 case 0xd8:
767 m_spreadsheetParser->readCellStyle();
768 isParsed = true;
769 break;
770 case 0xdc:
771 m_spreadsheetParser->readSpreadsheetOpen();
772 isParsed = true;
773 break;
774 case 0xdd:
775 m_spreadsheetParser->readSpreadsheetClose();
776 isParsed = true;
777 break;
778 case 0xde:
779 m_spreadsheetParser->readSpreadsheetName();
780 isParsed = true;
781 break;
782 case 0xe0:
783 m_spreadsheetParser->readRowSize();
784 isParsed = true;
785 break;
786 case 0xe2:
787 m_spreadsheetParser->readColumnSize();
788 isParsed = true;
789 break;
790 // case 0xe6: related to the selection?
791 default:
792 break;
793 }
794
795 if (!ok)
796 {
797 input->seek(pos, librevenge::RVNG_SEEK_SET);
798 return false;
799 }
800 if (isParsed)
801 {
802 if (needWriteInAscii)
803 {
804 ascii().addPos(pos);
805 ascii().addNote(f.str().c_str());
806 }
807 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
808 return true;
809 }
810
811 if (sz && input->tell()!=pos && input->tell()!=pos+4+sz)
812 ascii().addDelimiter(input->tell(),'|');
813 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
814 ascii().addPos(pos);
815 ascii().addNote(f.str().c_str());
816 return true;
817 }
818
819 ////////////////////////////////////////////////////////////
820 // generic
821 ////////////////////////////////////////////////////////////
822 bool QuattroDosParser::readFileName()
823 {
824 libwps::DebugStream f;
825 RVNGInputStreamPtr input = getInput();
826 long pos = input->tell();
827 auto type = int(libwps::read16(input));
828
829 if (type != 0x97)
830 {
831 WPS_DEBUG_MSG(("QuattroDosParser::readFileNames: not a font zone\n"));
832 return false;
833 }
834 auto sz = long(libwps::readU16(input));
835 f << "Entries(FileName):";
836 if (sz<4)
837 {
838 WPS_DEBUG_MSG(("QuattroDosParser::readFileNames: seems very short\n"));
839 f << "###";
840 ascii().addPos(pos);
841 ascii().addNote(f.str().c_str());
842 return true;
843 }
844 auto id=int(libwps::readU16(input));
845 f << "id=" << id << ",";
846 librevenge::RVNGString name;
847 if (!readPString(name,sz-3))
848 {
849 f << "##sSz,";
850 }
851 else if (m_state->m_idToFileNameMap.find(id)!=m_state->m_idToFileNameMap.end())
852 {
853 WPS_DEBUG_MSG(("QuattroDosParser::readFileName: id=%d already found\n", id));
854 f << "##duplicated,";
855 }
856 else
857 m_state->m_idToFileNameMap[id]=name;
858 if (!name.empty())
859 f << name.cstr() << ',';
860 // seems follow by 4 bytes in wq1 files
861 if (input->tell()!=pos+4+sz) ascii().addDelimiter(input->tell(),'|');
862 ascii().addPos(pos);
863 ascii().addNote(f.str().c_str());
864 return true;
865 }
866
867 bool QuattroDosParser::readPString(librevenge::RVNGString &string, long maxSize)
868 {
869 RVNGInputStreamPtr input = getInput();
870 long pos = input->tell();
871 auto sSz=int(libwps::readU8(input));
872 string.clear();
873 if (sSz>maxSize || !checkFilePosition(pos+1+sSz))
874 {
875 WPS_DEBUG_MSG(("QuattroDosParser::readPString: string's size seems bad\n"));
876 return false;
877 }
878 std::string text;
879 for (long i=0; i<sSz; ++i)
880 {
881 auto c = char(libwps::readU8(input));
882 if (c == '\0') continue;
883 text.push_back(c);
884 }
885 if (!text.empty())
886 string=libwps_tools_win::Font::unicodeString(text, getDefaultFontType());
887 return true;
888 }
889
890 bool QuattroDosParser::readUserFonts()
891 {
892 libwps::DebugStream f;
893 RVNGInputStreamPtr input = getInput();
894 long pos = input->tell();
895 auto type = int(libwps::read16(input));
896
897 if (type != 0x9b)
898 {
899 WPS_DEBUG_MSG(("QuattroDosParser::readUserFonts: not a font zone\n"));
900 return false;
901 }
902 auto sz = long(libwps::readU16(input));
903 f << "Entries(UserFont)[qpro]:";
904 if ((sz%8)!=0)
905 {
906 WPS_DEBUG_MSG(("QuattroDosParser::readUserFonts: seems very short\n"));
907 f << "###";
908 ascii().addPos(pos);
909 ascii().addNote(f.str().c_str());
910 return true;
911 }
912 ascii().addPos(pos);
913 ascii().addNote(f.str().c_str());
914 auto N=int(sz/8);
915 for (int i=0; i<N; ++i)
916 {
917 pos=input->tell();
918
919 f.str("");
920 f << "UserFont:Fo" << i << ",";
921 QuattroDosParserInternal::Font font(getDefaultFontType());
922 if (!readFont(font, font.m_type))
923 {
924 WPS_DEBUG_MSG(("QuattroDosParser::readUserFonts: oops unexpected problem\n"));
925 font=QuattroDosParserInternal::Font(getDefaultFontType());
926 f << "###Font,";
927 }
928 m_state->m_fontsList.push_back(font);
929
930 f << font;
931 ascii().addPos(pos);
932 ascii().addNote(f.str().c_str());
933 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
934 }
935 return true;
936 }
937
938 bool QuattroDosParser::readFont(WPSFont &font, libwps_tools_win::Font::Type &type)
939 {
940 RVNGInputStreamPtr input = getInput();
941 libwps::DebugStream f;
942 long pos=input->tell();
943 if (!checkFilePosition(pos+8))
944 {
945 WPS_DEBUG_MSG(("QuattroDosParser::readFont: the zone is to short\n"));
946 return false;
947 }
948 font=WPSFont();
949 type=getDefaultFontType();
950 auto flags = int(libwps::readU16(input));
951 uint32_t attributes = 0;
952 if (flags & 1) attributes |= WPS_BOLD_BIT;
953 if (flags & 2) attributes |= WPS_ITALICS_BIT;
954 if (flags & 8) attributes |= WPS_UNDERLINE_BIT;
955
956 font.m_attributes=attributes;
957 if (flags & 0xFFF4)
958 f << "fl=" << std::hex << (flags & 0xFFF4) << std::dec << ",";
959 auto fId=int(libwps::readU16(input));
960 f << "fId=" << fId << ",";
961 auto fSize = int(libwps::readU16(input));
962 if (fSize >= 1 && fSize <= 50)
963 font.m_size=double(fSize);
964 else
965 f << "###fSize=" << fSize << ",";
966 auto color = int(libwps::readU16(input));
967 if (color && !m_state->getColor(color, font.m_color))
968 {
969 WPS_DEBUG_MSG(("QuattroDosParser::readFont: unknown color\n"));
970 f << "##color=" << color << ",";
971 }
972
973 font.m_extra=f.str();
974 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
975 return true;
976 }
977
978 // ----------------------------------------------------------------------
979 // Header/Footer
980 // ----------------------------------------------------------------------
981 void QuattroDosParser::sendHeaderFooter(bool header)
982 {
983 if (!m_listener)
984 {
985 WPS_DEBUG_MSG(("QuattroDosParser::sendHeaderFooter: can not find the listener\n"));
986 return;
987 }
988
989 m_listener->setFont(m_state->getDefaultFont());
990 auto fontType=m_state->getDefaultFontType();
991
992 std::string const &text=header ? m_state->m_headerString : m_state->m_footerString;
993 std::string sText;
994 for (size_t i= 0; i<=text.size() ; ++i)
995 {
996 auto c=i+1==text.size() ? '\0' : text[i];
997 if ((c==0 || c==0xd || c==0xa) && !sText.empty())
998 {
999 m_listener->insertUnicodeString(libwps_tools_win::Font::unicodeString(sText, fontType));
1000 sText.clear();
1001 }
1002 if (i+1==text.size())
1003 break;
1004 if (c==0xd)
1005 m_listener->insertEOL();
1006 else if (c==0xa)
1007 continue;
1008 else
1009 sText.push_back(c);
1010 }
1011
1012 }
1013
1014 bool QuattroDosParser::readHeaderFooter(bool header)
1015 {
1016 libwps::DebugStream f;
1017 RVNGInputStreamPtr input = getInput();
1018 long pos = input->tell();
1019 auto type = int(libwps::read16(input));
1020 if (type != 0x0026 && type != 0x0025)
1021 {
1022 WPS_DEBUG_MSG(("QuattroDosParser::readHeaderFooter: not a header/footer\n"));
1023 return false;
1024 }
1025 auto sz = long(libwps::readU16(input));
1026 long endPos = pos+4+sz;
1027
1028 f << "Entries(" << (header ? "HeaderText" : "FooterText") << "):";
1029 if (sz==1)
1030 {
1031 // followed with 0
1032 auto val=int(libwps::read8(input));
1033 if (val) f << "##f0=" << val << ",";
1034 ascii().addPos(pos);
1035 ascii().addNote(f.str().c_str());
1036 return true;
1037 }
1038 if (sz < 0xF2)
1039 {
1040 WPS_DEBUG_MSG(("QuattroDosParser::readHeaderFooter: the header/footer size seeem odds\n"));
1041 f << "###";
1042 ascii().addPos(pos);
1043 ascii().addNote(f.str().c_str());
1044 return false;
1045 }
1046 std::string text("");
1047 for (long i=0; i < sz; i++)
1048 {
1049 auto c=char(libwps::read8(input));
1050 if (c=='\0') break;
1051 text+=c;
1052 }
1053 if (header)
1054 m_state->m_headerString=text;
1055 else
1056 m_state->m_footerString=text;
1057 f << text;
1058 if (input->tell()!=endPos)
1059 ascii().addDelimiter(input->tell(), '|');
1060 ascii().addPos(pos);
1061 ascii().addNote(f.str().c_str());
1062
1063 return true;
1064 }
1065
1066 bool QuattroDosParser::readFieldName()
1067 {
1068 libwps::DebugStream f;
1069 RVNGInputStreamPtr input = getInput();
1070
1071 long pos = input->tell();
1072 auto type = long(libwps::readU16(input));
1073 if (type != 0xb)
1074 {
1075 WPS_DEBUG_MSG(("QuattroDosParser::readFieldName: not a zoneB type\n"));
1076 return false;
1077 }
1078 auto sz = long(libwps::readU16(input));
1079 f << "Entries(FldNames):";
1080 if (sz != 0x18 && sz != 0x1e)
1081 {
1082 // find also 0x85 a zone with 4 fldnames ?
1083 WPS_DEBUG_MSG(("QuattroDosParser::readFieldName: size seems bad\n"));
1084 f << "###";
1085 ascii().addPos(pos);
1086 ascii().addNote(f.str().c_str());
1087 return true;
1088 }
1089 librevenge::RVNGString name;
1090 if (!readPString(name,15))
1091 f << "##sSz,";
1092 else if (!name.empty())
1093 f << name.cstr() << ',';
1094
1095 input->seek(pos+20, librevenge::RVNG_SEEK_SET);
1096 // the position
1097 int dim[4];
1098 if (sz==0x18)
1099 {
1100 for (int &i : dim) i=int(libwps::read16(input));
1101 }
1102 else
1103 {
1104 for (int i=0; i<7; ++i)
1105 {
1106 auto val=int(libwps::read16(input));
1107 if (i<2) dim[i]=val;
1108 else if (i>=3 && i<5) dim[i-1]=val;
1109 else if (val) f << "f" << i << "=" << val << ",";
1110 }
1111 }
1112 f << "cell=" << dim[0] << "x" << dim[1];
1113 if (dim[0]!=dim[2] || dim[1]!=dim[3])
1114 f << "<->" << dim[2] << "x" << dim[3];
1115 f << ",";
1116 ascii().addPos(pos);
1117 ascii().addNote(f.str().c_str());
1118 return true;
1119 }
1120
1121 ////////////////////////////////////////////////////////////
1122 // Unknown
1123 ////////////////////////////////////////////////////////////
1124
1125 /* the zone 0:7 and 0:9 */
1126 bool QuattroDosParser::readWindowRecord()
1127 {
1128 libwps::DebugStream f;
1129 RVNGInputStreamPtr input = getInput();
1130
1131 long pos = input->tell();
1132 auto type = long(libwps::read16(input));
1133 if (type != 7 && type != 9)
1134 {
1135 WPS_DEBUG_MSG(("QuattroDosParser::readWindowRecord: unknown type\n"));
1136 return false;
1137 }
1138 auto sz = long(libwps::readU16(input));
1139
1140 // normally size=0x1f but one time 0x1e
1141 if (sz < 0x1e)
1142 {
1143 WPS_DEBUG_MSG(("QuattroDosParser::readWindowRecord: zone seems too short\n"));
1144 ascii().addPos(pos);
1145 ascii().addNote("Entries(WindowRecord):###");
1146 return true;
1147 }
1148
1149 f << "Entries(WindowRecord)[" << type << "]:";
1150 // f0=0-a|1f|21, f1=1-3c|1da, f2=0-6|71|7f|f1, f3=4|9|a|c(size?),
1151 // f4=0|4|6-11, f5=5-2c, f6=0|3|6|10|1f|20, f7=0-3c|1ca (related to f1?)
1152 // f8=0|1, f9=0|2, f10=f11=0
1153 for (int i=0; i<12; ++i)
1154 {
1155 auto val=int(libwps::read16(input));
1156 if (val) f << "f" << i << "=" << val << ",";
1157 }
1158 for (int i=0; i<2; ++i) // g0=4, g1=4|a|b|10
1159 {
1160 auto val=int(libwps::read16(input));
1161 if (val!=4) f << "g" << i << "=" << val << ",";
1162 }
1163 auto val=int(libwps::read16(input)); // number between -5 and ad
1164 f << "g2=" << val << ",";
1165
1166 if (sz!=0x1e)
1167 ascii().addDelimiter(input->tell(),'|');
1168 ascii().addPos(pos);
1169 ascii().addNote(f.str().c_str());
1170
1171 return true;
1172 }
1173
1174 /* some spreadsheet zones 0:18, 0:19, 0:20, 0:27, 0:2a */
1175 bool QuattroDosParser::readUnknown1()
1176 {
1177 libwps::DebugStream f;
1178 RVNGInputStreamPtr input = getInput();
1179
1180 long pos = input->tell();
1181 auto type = long(libwps::read16(input));
1182 int expectedSize=0, extraSize=0;
1183 switch (type)
1184 {
1185 case 0x18:
1186 case 0x19:
1187 expectedSize=(version()>=2) ? 0x25 : 0x19;
1188 break;
1189 case 0x20:
1190 case 0x2a:
1191 expectedSize=(version()>=2) ? 0x18 : 0x10;
1192 break;
1193 case 0x27:
1194 expectedSize=0x19;
1195 extraSize=15;
1196 break;
1197 default:
1198 WPS_DEBUG_MSG(("QuattroDosParser::readUnknown1: unexpected type ???\n"));
1199 return false;
1200 }
1201 auto sz = long(libwps::readU16(input));
1202
1203 f << "Entries(Flags" << std::hex << type << std::dec << ")]:";
1204 if (sz != expectedSize+extraSize)
1205 {
1206 // find also 270001000[01]
1207 if (type==0x27 && sz==1)
1208 {
1209 f << "f0=" << int(libwps::read8(input)) << ",";
1210 ascii().addPos(pos);
1211 ascii().addNote(f.str().c_str());
1212 return true;
1213 }
1214 WPS_DEBUG_MSG(("QuattroDosParser::readUnknown1: the zone size seems too bad\n"));
1215 f << "###";
1216 ascii().addPos(pos);
1217 ascii().addNote(f.str().c_str());
1218 return true;
1219 }
1220
1221 // find always 0xff, excepted for zone 18(f0=0), zone 19(f24=0|3), zone 27(f0=..=f23=0|ff, f24=0|3)
1222 for (int i=0; i<expectedSize; ++i)
1223 {
1224 auto val=int(libwps::read8(input));
1225 if (val!=-1) f << "f" << i << "=" << val << ",";
1226 }
1227
1228 if (type==0x27)
1229 {
1230 auto val=int(libwps::read8(input)); // always 0
1231 if (val) f << "g0=" << val << ",";
1232 for (int i=0; i<7; ++i) // g1=0|4, g2=0|72, g4=0|20, g5=0|1|4, g6=0|1, g7=0|-1|205|80d8|e9f
1233 {
1234 val=int(libwps::read16(input));
1235 if (val) f << "g" << i+1 << "=" << val << ",";
1236 }
1237 }
1238 ascii().addPos(pos);
1239 ascii().addNote(f.str().c_str());
1240
1241 return true;
1242 }
1243
1244 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */