Debian Codegraph @

src/lib/GreatWksText.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 <cstring>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <map>
39 #include <sstream>
40
41 #include <librevenge/librevenge.h>
42
43 #include "MWAWDebug.hxx"
44 #include "MWAWFont.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWGraphicListener.hxx"
47 #include "MWAWListener.hxx"
48 #include "MWAWParagraph.hxx"
49 #include "MWAWParser.hxx"
50 #include "MWAWPosition.hxx"
51 #include "MWAWSection.hxx"
52 #include "MWAWRSRCParser.hxx"
53
54 #include "GreatWksDocument.hxx"
55
56 #include "GreatWksText.hxx"
57
58 /** Internal: the structures of a GreatWksText */
59 namespace GreatWksTextInternal
60 {
61 /** the different plc type */
62 enum PLCType { P_Font, P_Page, P_Ruler, P_Token, P_Unknown};
63
64 /** Internal : a PLC: used to store change of properties in GreatWksTextInternal::Zone */
65 struct PLC {
66 /// the constructor
67 PLC()
68 : m_type(P_Unknown)
69 , m_id(-1)
70 , m_extra("")
71 {
72 }
73 //! operator<<
74 friend std::ostream &operator<<(std::ostream &o, PLC const &plc);
75 /** the PLC types */
76 PLCType m_type;
77 /** the id */
78 int m_id;
79 /** extra data */
80 std::string m_extra;
81 };
82 std::ostream &operator<<(std::ostream &o, PLC const &plc)
83 {
84 switch (plc.m_type) {
85 case P_Font:
86 o << "F";
87 break;
88 case P_Page:
89 o << "Pg";
90 break;
91 case P_Ruler:
92 o << "R";
93 break;
94 case P_Token:
95 o << "Tn";
96 break;
97 case P_Unknown:
98 #if !defined(__clang__)
99 default:
100 #endif
101 o << "#Unkn";
102 break;
103 }
104 if (plc.m_id >= 0) o << plc.m_id;
105 else o << "_";
106 if (plc.m_extra.length()) o << ":" << plc.m_extra;
107 return o;
108 }
109
110 ////////////////////////////////////////
111 //! Internal and low level: structure which stores a token for GreatWksText
112 struct Token {
113 //! constructor
114 Token()
115 : m_type(-1)
116 , m_format(0)
117 , m_pictEntry()
118 , m_dataSize(0)
119 , m_dim(0,0)
120 , m_date(0xFFFFFFFF)
121 , m_extra("")
122 {
123 }
124 //! returns a field format
125 std::string getDTFormat() const;
126 //! try to send the token to the listener
127 bool sendTo(MWAWListener &listener) const;
128 //! operator<<
129 friend std::ostream &operator<<(std::ostream &o, Token const &tkn);
130 //! the token type
131 int m_type;
132 //! the token format
133 int m_format;
134 //! the picture entry
135 MWAWEntry m_pictEntry;
136 //! the picture data size
137 long m_dataSize;
138 //! the picture dimension
139 MWAWVec2f m_dim;
140 //! the token date (0xFFFFFFFF means actual date)
141 uint32_t m_date;
142 //! extra data
143 std::string m_extra;
144 };
145
146 bool Token::sendTo(MWAWListener &listener) const
147 {
148 switch (m_type) {
149 case 2:
150 switch (m_format) {
151 case 1:
152 case 3: // must be roman
153 listener.insertField(MWAWField(MWAWField::PageNumber));
154 break;
155 case 2:
156 case 4: // must be roman
157 listener.insertField(MWAWField(MWAWField::PageNumber));
158 listener.insertUnicodeString(librevenge::RVNGString(" of "));
159 listener.insertField(MWAWField(MWAWField::PageCount));
160 break;
161 default:
162 listener.insertField(MWAWField(MWAWField::PageNumber));
163 break;
164 }
165 return true;
166 case 0x15:
167 case 0x16: {
168 MWAWField field(m_type==0x15 ? MWAWField::Date : MWAWField::Time);
169 field.m_DTFormat=getDTFormat();
170 listener.insertField(field);
171 return true;
172 }
173 default:
174 break;
175 }
176 return false;
177 }
178
179 std::string Token::getDTFormat() const
180 {
181 switch (m_type) {
182 case 0x15:
183 switch (m_format) {
184 case 0xa:
185 return "%m/%d/%y";
186 case 0xb:
187 return "%b %d, %Y";
188 case 0xc:
189 return "%b %Y";
190 case 0xd:
191 return "%b %d";
192 case 0xe:
193 return "%B %d, %Y";
194 case 0xf:
195 return "%B %Y";
196 case 0x10:
197 return "%B %d";
198 case 0x11:
199 return "%a, %b %d, %Y";
200 case 0x12:
201 return "%A, %B %d, %Y";
202 default:
203 break;
204 }
205 break;
206 case 0x16:
207 switch (m_format) {
208 case 0x14:
209 return "%I:%M %p";
210 case 0x15:
211 return "%I:%M:%S %p";
212 case 0x16:
213 return "%I:%M";
214 case 0x17:
215 return "%I:%M:%S";
216 case 0x18:
217 return "%H:%M";
218 case 0x19:
219 return "%H:%M:%S";
220 default:
221 break;
222 }
223 break;
224 default:
225 break;
226 }
227 return "";
228 }
229
230 std::ostream &operator<<(std::ostream &o, Token const &token)
231 {
232 switch (token.m_type) {
233 case 0: // none
234 break;
235 case 2:
236 switch (token.m_format) {
237 case 0:
238 o << "page,";
239 break;
240 case 1:
241 o << "page/pagecount,";
242 break;
243 case 2:
244 o << "page[roman],";
245 break;
246 case 3:
247 o << "page/pagecount[roman],";
248 break;
249 default:
250 o << "page[#m_format=" << token.m_format << "],";
251 break;
252 }
253 break;
254 case 4:
255 o << "pict,dim="<< token.m_dim << ",sz=" << std::hex << token.m_dataSize << std::dec << ",";
256 break;
257 case 0x15:
258 case 0x16: {
259 o << (token.m_type==0x15 ? "date" : "time");
260 std::string format=token.getDTFormat();
261 if (!format.empty())
262 o << "[" << format << "]";
263 else
264 o << "[#format=" << token.m_format << "]";
265 if (token.m_date != 0xFFFFFFFF)
266 o << ":val=" << std::hex << token.m_date << std::dec;
267 o << ",";
268 break;
269 }
270 default:
271 MWAW_DEBUG_MSG(("GreatWksTextInternal::Token: unknown type=%d\n", token.m_type));
272 o << "#type=" << token.m_type << ",";
273 if (token.m_format)
274 o << "#format=" << token.m_format << ",";
275 }
276 o << token.m_extra;
277 return o;
278 }
279 ////////////////////////////////////////
280 //! Internal and low level: structure which stores a text position for GreatWksText
281 struct Frame {
282 //! constructor
283 Frame()
284 : m_pos()
285 , m_page(0)
286 , m_extra("")
287 {
288 }
289 //! operator<<
290 friend std::ostream &operator<<(std::ostream &o, Frame const &frm)
291 {
292 o << "dim=" << frm.m_pos << ",";
293 if (frm.m_page) o << "page=" << frm.m_page << ",";
294 o << frm.m_extra;
295 return o;
296 }
297
298 //! the frame position
299 MWAWBox2f m_pos;
300 //! the page
301 int m_page;
302 //! extra data
303 std::string m_extra;
304 };
305 ////////////////////////////////////////
306 //! Internal and low level: structure which stores a text zone header for GreatWksText
307 struct Zone {
308 //! constructor
309 Zone()
310 : m_type(-1)
311 , m_numFonts(0)
312 , m_numRulers(0)
313 , m_numLines(0)
314 , m_numTokens(0)
315 , m_numChar(0)
316 , m_numCharPLC(0)
317 , m_numFrames(0)
318 , m_fontList()
319 , m_rulerList()
320 , m_tokenList()
321 , m_frameList()
322 , m_textEntry()
323 , m_posPLCMap()
324 , m_parsed(false)
325 , m_extra("")
326 {
327 }
328 //! returns true if this is the main zone
329 bool isMain() const
330 {
331 return m_type==3;
332 }
333 //! check if the data read are or not ok
334 bool ok() const
335 {
336 if (m_type<0 || m_type > 5)
337 return false;
338 if (m_numFonts<=0 || m_numRulers<=0 || m_numLines<0 || m_numTokens<=0 || m_numChar<0 ||
339 m_numCharPLC<=0 || m_numFrames<0)
340 return false;
341 return true;
342 }
343 //! returns the data size
344 long size() const
345 {
346 return 22*m_numFonts+192*m_numRulers+6*m_numCharPLC
347 +18*m_numTokens+14*m_numLines+22*m_numFrames+m_numChar;
348 }
349 //! returns true if the data has graphic
350 bool hasGraphics() const
351 {
352 for (auto const &token : m_tokenList) {
353 if (token.m_type==4)
354 return true;
355 }
356 return false;
357 }
358 //! operator<<
359 friend std::ostream &operator<<(std::ostream &o, Zone const &fr)
360 {
361 switch (fr.m_type) {
362 case 1: // in draw=textbox
363 o << "header/footer,";
364 break;
365 case 2:
366 o << "textbox,";
367 break;
368 case 3:
369 o << "main,";
370 break;
371 default:
372 o << "#type=" << fr.m_type << ",";
373 break;
374 }
375 if (fr.m_numFonts)
376 o << "nFonts=" << fr.m_numFonts << ",";
377 if (fr.m_numRulers)
378 o << "nRulers=" << fr.m_numRulers << ",";
379 if (fr.m_numChar)
380 o << "length[text]=" << fr.m_numChar << ",";
381 if (fr.m_numCharPLC)
382 o << "char[plc]=" << fr.m_numCharPLC << ",";
383 if (fr.m_numLines)
384 o << "nLines=" << fr.m_numLines << ",";
385 if (fr.m_numTokens)
386 o << "nTokens=" << fr.m_numTokens << ",";
387 if (fr.m_numFrames)
388 o << "nFrames=" << fr.m_numFrames << ",";
389 o << fr.m_extra;
390 return o;
391 }
392 //! the main type: 1=auxi, 3=main
393 int m_type;
394 //! the number of fonts
395 int m_numFonts;
396 //! the number of rulers
397 int m_numRulers;
398 //! the number of lines
399 int m_numLines;
400 //! the number of token
401 int m_numTokens;
402 //! the number of character
403 long m_numChar;
404 //! the number of char plc
405 int m_numCharPLC;
406 //! the number of frames (ie. one by column and one by pages )
407 int m_numFrames;
408 //! the list of font
409 std::vector<MWAWFont> m_fontList;
410 //! the list of ruler
411 std::vector<MWAWParagraph> m_rulerList;
412 //! the list of token
413 std::vector<Token> m_tokenList;
414 //! the list of frame token
415 std::vector<Frame> m_frameList;
416 //! the text entry list
417 MWAWEntry m_textEntry;
418 //! a map text pos -> PLC
419 std::multimap<long,PLC> m_posPLCMap;
420 //! a bool to know if the data are send to the listener
421 mutable bool m_parsed;
422 //! extra data
423 std::string m_extra;
424 };
425
426 ////////////////////////////////////////
427 //! Internal: the state of a GreatWksText
428 struct State {
429 //! constructor
430 State()
431 : m_fileIdFontIdMap()
432 , m_zonesList()
433 , m_version(-1)
434 , m_numPages(-1)
435 {
436 }
437 //! returns the font id corresponding to a fId
438 int getFId(int fileId) const
439 {
440 if (m_fileIdFontIdMap.find(fileId)==m_fileIdFontIdMap.end())
441 return fileId;
442 return m_fileIdFontIdMap.find(fileId)->second;
443 }
444 //! a global map file font id-> fontconverter id
445 std::map<int,int> m_fileIdFontIdMap;
446 //! a list of text zone
447 std::vector<Zone> m_zonesList;
448 //! the file version
449 mutable int m_version;
450 int m_numPages /* the number of pages */;
451 };
452 }
453
454 ////////////////////////////////////////////////////////////
455 // constructor/destructor, ...
456 ////////////////////////////////////////////////////////////
457 GreatWksText::GreatWksText(GreatWksDocument &document)
458 : m_document(document)
459 , m_parserState(document.m_parserState)
460 , m_state(new GreatWksTextInternal::State)
461 , m_mainParser(&document.getMainParser())
462 {
463 }
464
465 GreatWksText::~GreatWksText()
466 { }
467
468 int GreatWksText::version() const
469 {
470 if (m_state->m_version < 0)
471 m_state->m_version = m_parserState->m_version;
472 return m_state->m_version;
473 }
474
475 int GreatWksText::numPages() const
476 {
477 if (m_state->m_numPages >= 0)
478 return m_state->m_numPages;
479
480 int nPages=1;
481 for (auto const &zone : m_state->m_zonesList) {
482 if (!zone.isMain() || zone.m_frameList.empty())
483 continue;
484 if (zone.m_frameList.back().m_page>1)
485 nPages = zone.m_frameList.back().m_page;
486 break;
487 }
488
489 return m_state->m_numPages = nPages;
490 }
491
492 int GreatWksText::getFontId(int fileId) const
493 {
494 return m_state->getFId(fileId);
495 }
496
497 int GreatWksText::numHFZones() const
498 {
499 int nHF=0;
500 for (auto const &zone : m_state->m_zonesList) {
501 if (zone.isMain())
502 break;
503 nHF++;
504 }
505 return nHF;
506 }
507
508 bool GreatWksText::canSendTextBoxAsGraphic(MWAWEntry const &entry)
509 {
510 if (!entry.valid())
511 return false;
512 MWAWInputStreamPtr &input= m_parserState->m_input;
513 long pos=input->tell();
514 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
515 GreatWksTextInternal::Zone zone;
516 bool ok=!readZone(zone) ||!zone.hasGraphics();
517 input->seek(pos, librevenge::RVNG_SEEK_SET);
518 return ok;
519 }
520
521 bool GreatWksText::sendTextbox(MWAWEntry const &entry, MWAWListenerPtr listener)
522 {
523 if (!listener && !m_parserState->getMainListener()) {
524 MWAW_DEBUG_MSG(("GreatWksText::sendTextbox: can not find a listener\n"));
525 return false;
526 }
527 if (!entry.valid())
528 return false;
529 MWAWInputStreamPtr &input= m_parserState->m_input;
530 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
531 GreatWksTextInternal::Zone zone;
532 if (readZone(zone)) {
533 sendZone(zone, listener);
534 return true;
535 }
536
537 return sendSimpleTextbox(entry, listener);
538 }
539
540 ////////////////////////////////////////////////////////////
541 // Intermediate level
542 ////////////////////////////////////////////////////////////
543 bool GreatWksText::createZones(int expectedHF)
544 {
545 MWAWInputStreamPtr &input= m_parserState->m_input;
546 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
547 libmwaw::DebugStream f;
548
549 long pos = input->tell();
550 f << "Entries(TZoneHeader):";
551 auto val=static_cast<int>(input->readULong(2));
552 if (val)
553 f << "numPages=" << val << ",";
554 val=static_cast<int>(input->readULong(2));
555 if (val) // related to number of header/footer?
556 f << "f0=" << val << ",";
557 f << "height[total]=" << input->readLong(4) << ","; // checkme
558 ascFile.addDelimiter(input->tell(),'|');
559 ascFile.addPos(pos);
560 ascFile.addNote(f.str().c_str());
561
562 pos += 68;
563 input->seek(pos, librevenge::RVNG_SEEK_SET);
564
565 if (!readFontNames()) {
566 MWAW_DEBUG_MSG(("GreatWksText::createZones: can not find the font names\n"));
567 input->seek(pos, librevenge::RVNG_SEEK_SET);
568 }
569
570 bool findMainZone=false;
571 int nAuxi=0;
572 while (!input->isEnd()) {
573 pos=input->tell();
574 GreatWksTextInternal::Zone zone;
575 if (!readZone(zone)) {
576 input->seek(pos,librevenge::RVNG_SEEK_SET);
577 if (findMainZone)
578 break;
579
580 if (!findNextZone() || !readZone(zone)) {
581 input->seek(pos,librevenge::RVNG_SEEK_SET);
582 break;
583 }
584 }
585 m_state->m_zonesList.push_back(zone);
586 if (zone.isMain())
587 findMainZone=true;
588 else
589 nAuxi++;
590 }
591 if (nAuxi!=expectedHF) {
592 MWAW_DEBUG_MSG(("GreatWksText::createZones: unexpected HF zones: %d/%d\n", nAuxi, expectedHF));
593 }
594 return findMainZone;
595 }
596
597 bool GreatWksText::findNextZone()
598 {
599 MWAWInputStreamPtr &input= m_parserState->m_input;
600
601 long searchPos=input->tell(), pos=searchPos;
602 int const headerSize=24+22+184;
603 if (!input->checkPosition(pos+headerSize))
604 return false;
605
606 // first look for ruler
607 input->seek(pos+headerSize, librevenge::RVNG_SEEK_SET);
608 while (true) {
609 if (input->isEnd())
610 return false;
611 pos = input->tell();
612 unsigned long val=input->readULong(4);
613 if (val==0x20FFFF)
614 input->seek(pos, librevenge::RVNG_SEEK_SET);
615 else if (val==0x20FFFFFF)
616 input->seek(pos-1, librevenge::RVNG_SEEK_SET);
617 else if (val==0xFFFFFFFF)
618 input->seek(pos-2, librevenge::RVNG_SEEK_SET);
619 else if (val==0xFFFFFF2E)
620 input->seek(pos-3, librevenge::RVNG_SEEK_SET);
621 else
622 continue;
623 if (input->readULong(4)!=0x20FFFF || input->readULong(4)!=0xFFFF2E00) {
624 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
625 continue;
626 }
627 // ok a empty tabs stop
628 while (!input->isEnd()) {
629 pos = input->tell();
630 if (input->readULong(4)!=0x20FFFF || input->readULong(4)!=0xFFFF2E00) {
631 input->seek(pos, librevenge::RVNG_SEEK_SET);
632 break;
633 }
634 }
635 break;
636 }
637
638 pos=input->tell();
639 int nFonts=0;
640 GreatWksTextInternal::Zone zone;
641 while (true) {
642 long hSize=headerSize+22*nFonts++;
643 if (pos-hSize < searchPos)
644 break;
645 input->seek(pos-hSize, librevenge::RVNG_SEEK_SET);
646 if (input->readLong(4))
647 continue;
648 auto val=static_cast<int>(input->readULong(2));
649 if (val & 0xFEFE)
650 continue;
651 input->seek(2,librevenge::RVNG_SEEK_CUR);
652 if (input->readLong(2)!=nFonts)
653 continue;
654 input->seek(pos-hSize, librevenge::RVNG_SEEK_SET);
655 if (readZone(zone)) {
656 input->seek(pos-hSize, librevenge::RVNG_SEEK_SET);
657 return true;
658 }
659 }
660
661 MWAW_DEBUG_MSG(("GreatWksText::findNextZone: can not find begin of zone for pos=%lx\n", static_cast<long unsigned int>(pos)));
662 input->seek(searchPos, librevenge::RVNG_SEEK_SET);
663 return false;
664 }
665
666 bool GreatWksText::readZone(GreatWksTextInternal::Zone &zone)
667 {
668 zone=GreatWksTextInternal::Zone();
669 MWAWInputStreamPtr &input= m_parserState->m_input;
670 long pos=input->tell();
671 long endPos=pos+24;
672 if (!input->checkPosition(endPos))
673 return false;
674
675 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
676 libmwaw::DebugStream f;
677 input->seek(pos, librevenge::RVNG_SEEK_SET);
678 if (input->readLong(4)) {
679 input->seek(pos, librevenge::RVNG_SEEK_SET);
680 return false;
681 }
682 for (int i=0; i < 2; ++i) {
683 auto val=static_cast<int>(input->readLong(1));
684 if (val==0) continue;
685 if (val!=1)
686 return false;
687 if (i==0)
688 f << "smartquote,";
689 else
690 f << "hidepict,";
691 }
692 zone.m_type=static_cast<int>(input->readLong(1));
693 if (input->readLong(1)) { // simple|complex field
694 input->seek(pos, librevenge::RVNG_SEEK_SET);
695 return false;
696 }
697 zone.m_numFonts=static_cast<int>(input->readULong(2));
698 zone.m_numRulers=static_cast<int>(input->readULong(2));
699 zone.m_numCharPLC=static_cast<int>(input->readULong(2));
700 zone.m_numTokens=static_cast<int>(input->readULong(2));
701 zone.m_numLines=static_cast<int>(input->readULong(2));
702 zone.m_numFrames=static_cast<int>(input->readULong(2));
703 zone.m_numChar=long(input->readULong(4));
704 if (!zone.ok() || !input->checkPosition(endPos+zone.size())) {
705 input->seek(pos, librevenge::RVNG_SEEK_SET);
706 return false;
707 }
708 zone.m_extra=f.str();
709 f.str("");
710 f << "Entries(HeaderText):" << zone;
711 if (input->tell()!=endPos) {
712 ascFile.addDelimiter(input->tell(),'|');
713 input->seek(endPos, librevenge::RVNG_SEEK_SET);
714 }
715 ascFile.addPos(pos);
716 ascFile.addNote(f.str().c_str());
717
718 for (int i=0; i<zone.m_numFonts; ++i) {
719 pos = input->tell();
720 f.str("");
721 f << "Entries(FontDef)-F" << i << ":";
722
723 MWAWFont font;
724 if (!readFont(font)) {
725 f << "###";
726 font = MWAWFont();
727 input->seek(pos+22, librevenge::RVNG_SEEK_SET);
728 }
729 zone.m_fontList.push_back(font);
730 f << font.getDebugString(m_parserState->m_fontConverter) << ",";
731 ascFile.addPos(pos);
732 ascFile.addNote(f.str().c_str());
733 }
734 MWAWParagraph para;
735 for (int i=0; i<zone.m_numRulers; ++i) {
736 pos = input->tell();
737 f.str("");
738 f << "Entries(RulerDef)-R" << i << ":";
739 if (!readRuler(para)) {
740 f << "###";
741 para = MWAWParagraph();
742 input->seek(pos+192, librevenge::RVNG_SEEK_SET);
743 }
744 zone.m_rulerList.push_back(para);
745 f << para;
746 ascFile.addPos(pos);
747 ascFile.addNote(f.str().c_str());
748 }
749
750 pos=input->tell();
751 f.str("");
752 f << "Entries(CharPLC):";
753 GreatWksTextInternal::PLC plc;
754 plc.m_type = GreatWksTextInternal::P_Font;
755 long textPos = 0;
756 for (int i=0; i < zone.m_numCharPLC; ++i) {
757 plc.m_id=static_cast<int>(input->readULong(2));
758 zone.m_posPLCMap.insert
759 (std::multimap<long,GreatWksTextInternal::PLC>::value_type(textPos, plc));
760 f << "F" << plc.m_id << ":" << std::hex << textPos << std::dec << ",";
761 textPos+= static_cast<int>(input->readULong(4));
762 }
763 ascFile.addPos(pos);
764 ascFile.addNote(f.str().c_str());
765 // the token
766 plc.m_type = GreatWksTextInternal::P_Token;
767 textPos = 0;
768 for (int i=0; i < zone.m_numTokens; ++i) {
769 pos = input->tell();
770 f.str("");
771 plc.m_id=i;
772 f << "Entries(Token)-Tn" << i << ":";
773 GreatWksTextInternal::Token tkn;
774 long numC;
775 if (!readToken(tkn,numC)) {
776 f << "###";
777 tkn = GreatWksTextInternal::Token();
778 input->seek(pos+2, librevenge::RVNG_SEEK_SET);
779 numC=input->readLong(2);
780 }
781 zone.m_posPLCMap.insert
782 (std::multimap<long,GreatWksTextInternal::PLC>::value_type(textPos, plc));
783 zone.m_tokenList.push_back(tkn);
784 f << tkn << ",pos=" << std::hex << textPos << std::dec;
785 textPos+= numC;
786 input->seek(pos+18, librevenge::RVNG_SEEK_SET);
787 ascFile.addPos(pos);
788 ascFile.addNote(f.str().c_str());
789 }
790 if (!readZonePositions(zone))
791 return false;
792 // fill the text entry
793 pos=input->tell();
794 zone.m_textEntry.setBegin(pos);
795 zone.m_textEntry.setLength(zone.m_numChar);
796 ascFile.addPos(pos);
797 ascFile.addNote("_");
798 pos += zone.m_numChar;
799 ascFile.addPos(pos);
800 ascFile.addNote("_");
801
802 for (auto &tkn : zone.m_tokenList) {
803 if (tkn.m_type != 4)
804 continue;
805 if (tkn.m_dataSize <= 0 || !input->checkPosition(pos+tkn.m_dataSize)) {
806 MWAW_DEBUG_MSG(("GreatWksText::readZone: can not determine the picture size\n"));
807 break;
808 }
809 tkn.m_pictEntry.setBegin(pos);
810 tkn.m_pictEntry.setLength(tkn.m_dataSize);
811 pos+=tkn.m_dataSize;
812 }
813 input->seek(pos, librevenge::RVNG_SEEK_SET);
814 return true;
815 }
816
817 bool GreatWksText::readZonePositions(GreatWksTextInternal::Zone &zone)
818 {
819 MWAWInputStreamPtr &input= m_parserState->m_input;
820 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
821 libmwaw::DebugStream f;
822
823 // the line
824 GreatWksTextInternal::PLC plc;
825 plc.m_type = GreatWksTextInternal::P_Ruler;
826 long textPos = 0;
827 std::vector<long> linesPos;
828 linesPos.push_back(0);
829 for (int i=0; i < zone.m_numLines; ++i) {
830 long pos=input->tell();
831 f.str("");
832 plc.m_id=static_cast<int>(input->readULong(2));
833 auto numC = long(input->readULong(4));
834 f << "y=" << double(input->readLong(4))/65536.;
835 f << "->" << double(input->readLong(4))/65536.;
836 plc.m_extra=f.str();
837 zone.m_posPLCMap.insert
838 (std::multimap<long,GreatWksTextInternal::PLC>::value_type(textPos, plc));
839 f.str("");
840 f << "Entries(Line)-L" << i << ":" << plc << ":"
841 << std::hex << textPos << std::dec;
842 textPos+=numC;
843 linesPos.push_back(textPos);
844 input->seek(pos+14, librevenge::RVNG_SEEK_SET);
845 ascFile.addPos(pos);
846 ascFile.addNote(f.str().c_str());
847 }
848 // the page dimension
849 plc.m_type = GreatWksTextInternal::P_Page;
850 for (int i=0; i < zone.m_numFrames; ++i) {
851 GreatWksTextInternal::Frame frame;
852 long pos=input->tell();
853 plc.m_id=i;
854 f.str("");
855 float dim[4];
856 for (auto &d : dim) d=float(input->readLong(4))/65536.f;
857 frame.m_pos=MWAWBox2f(MWAWVec2f(dim[1],dim[0]),MWAWVec2f(dim[3],dim[2]));
858 auto val=static_cast<int>(input->readLong(2)); // always 0
859 if (val) f << "#unkn=" << val << ",";
860 frame.m_page=static_cast<int>(input->readLong(2));
861 auto line=static_cast<int>(input->readLong(2));
862 plc.m_extra=f.str();
863 if (line >= 0 && line < int(linesPos.size())) {
864 textPos=linesPos[size_t(line)];
865 zone.m_posPLCMap.insert
866 (std::multimap<long,GreatWksTextInternal::PLC>::value_type(textPos, plc));
867 if (textPos)
868 f << "pos=" << std::hex << textPos << std::dec;
869 }
870 else {
871 MWAW_DEBUG_MSG(("GreatWksText::readZone: can not find begin pos for page %d\n",line));
872 f << "##pos[line]=" << line << ",";
873 }
874 frame.m_extra=f.str();
875 zone.m_frameList.push_back(frame);
876 f.str("");
877 f << "Entries(TFrames)-" << i << ":" << frame;
878 input->seek(pos+22, librevenge::RVNG_SEEK_SET);
879 ascFile.addPos(pos);
880 ascFile.addNote(f.str().c_str());
881 }
882 return true;
883 }
884
885 //////////////////////////////////////////////
886 // Fonts
887 //////////////////////////////////////////////
888 bool GreatWksText::readFontNames()
889 {
890 MWAWInputStreamPtr &input= m_parserState->m_input;
891 long pos=input->tell();
892 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
893 libmwaw::DebugStream f;
894 f << "Entries(FontNames):";
895 auto sz= long(input->readULong(4));
896 long endPos = input->tell()+sz;
897 if (sz < 2 || !input->checkPosition(endPos)) {
898 MWAW_DEBUG_MSG(("GreatWksText::readFontNames: can not read field size\n"));
899 f << "###";
900 ascFile.addPos(pos);
901 ascFile.addNote(f.str().c_str());
902 return false;
903 }
904 auto N=static_cast<int>(input->readLong(2));
905 f << "N=" << N << ",";
906 if (N*5+2 > sz) {
907 MWAW_DEBUG_MSG(("GreatWksText::readFontNames: can not read N\n"));
908 f << "###";
909 ascFile.addPos(pos);
910 ascFile.addNote(f.str().c_str());
911 return false;
912 }
913 ascFile.addPos(pos);
914 ascFile.addNote(f.str().c_str());
915 for (int i=0; i < N; ++i) {
916 pos = input->tell();
917 f.str("");
918 f << "FontNames-" << i << ":";
919 auto fId=static_cast<int>(input->readULong(2));
920 f << "fId=" << fId << ",";
921 auto val=static_cast<int>(input->readLong(2)); // always 0 ?
922 if (val)
923 f << "unkn=" << val << ",";
924 auto fSz=static_cast<int>(input->readULong(1));
925 if (pos+5+fSz>endPos) {
926 MWAW_DEBUG_MSG(("GreatWksText::readFontNames: can not read font %d\n", i));
927 f << "###";
928 ascFile.addPos(pos);
929 ascFile.addNote(f.str().c_str());
930 input->seek(endPos, librevenge::RVNG_SEEK_SET);
931 return i>0;
932 }
933 std::string name("");
934 for (int c=0; c < fSz; ++c)
935 name+=char(input->readULong(1));
936 if (!name.empty())
937 m_state->m_fileIdFontIdMap[fId]=m_parserState->m_fontConverter->getId(name);
938 if ((fSz%2)==0)
939 input->seek(1, librevenge::RVNG_SEEK_CUR);
940
941 f << "\"" << name << "\",";
942 ascFile.addPos(pos);
943 ascFile.addNote(f.str().c_str());
944 }
945 pos = input->tell();
946 if (pos!=endPos) {
947 MWAW_DEBUG_MSG(("GreatWksText::readFontNames: find extra data\n"));
948 ascFile.addPos(pos);
949 ascFile.addNote("FontNames:###");
950 input->seek(endPos, librevenge::RVNG_SEEK_SET);
951 }
952 return true;
953 }
954
955 bool GreatWksText::readFont(MWAWFont &font)
956 {
957 font=MWAWFont();
958 MWAWInputStreamPtr &input= m_parserState->m_input;
959 long pos=input->tell();
960 long endPos=pos+22;
961 if (!input->checkPosition(endPos))
962 return false;
963
964 libmwaw::DebugStream f;
965 input->seek(pos, librevenge::RVNG_SEEK_SET);
966 auto val=static_cast<int>(input->readLong(2)); // small number between 0 and 5
967 if (val==0) f << "unused,";
968 else if (val!=1) f << "nbUsed=" << val << ",";
969 // some dim dim[0]>dim[1]: minH, maxH ?
970 int dim[2];
971 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
972 f << "dim?=" << dim[1] << "x" << dim[0] << ",";
973
974 for (int i=0; i < 2; ++i) { // f0=0|4(with italic), f1=0
975 val=static_cast<int>(input->readLong(2));
976 if (val) f << "f" << i << "=" << val << ",";
977 }
978 font.setId(m_state->getFId(static_cast<int>(input->readULong(2))));
979 auto flag =static_cast<int>(input->readULong(2));
980 uint32_t flags=0;
981 if (flag&0x1) flags |= MWAWFont::boldBit;
982 if (flag&0x2) flags |= MWAWFont::italicBit;
983 if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
984 if (flag&0x8) flags |= MWAWFont::embossBit;
985 if (flag&0x10) flags |= MWAWFont::shadowBit;
986 if (flag&0x20) font.setDeltaLetterSpacing(-1);
987 if (flag&0x40) font.setDeltaLetterSpacing(1);
988 if (flag&0x100) font.set(MWAWFont::Script::super100());
989 if (flag&0x200) font.set(MWAWFont::Script::sub100());
990 if (flag&0x800) font.setStrikeOutStyle(MWAWFont::Line::Simple);
991 if (flag&0x2000) {
992 font.setUnderlineStyle(MWAWFont::Line::Simple);
993 font.setUnderlineType(MWAWFont::Line::Double);
994 }
995 flag &=0xD480;
996 if (flag) f << "#fl=" << std::hex << flag << std::dec << ",";
997 font.setFlags(flags);
998 font.setSize(float(input->readULong(2)));
999 unsigned char color[3];
1000 for (auto &c : color) c = static_cast<unsigned char>(input->readULong(2)>>8);
1001 font.setColor(MWAWColor(color[0],color[1],color[2]));
1002 font.m_extra=f.str();
1003
1004 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1005 return true;
1006 }
1007 //////////////////////////////////////////////
1008 // Ruler
1009 //////////////////////////////////////////////
1010 bool GreatWksText::readRuler(MWAWParagraph &para)
1011 {
1012 para = MWAWParagraph();
1013 MWAWInputStreamPtr &input= m_parserState->m_input;
1014 long pos=input->tell();
1015 long endPos=pos+192;
1016 if (!input->checkPosition(endPos))
1017 return false;
1018
1019 libmwaw::DebugStream f;
1020 input->seek(pos, librevenge::RVNG_SEEK_SET);
1021 auto val=static_cast<int>(input->readLong(2));
1022 if (val==0) f << "unused,";
1023 else if (val!=1) f << "nbUsed=" << val << ",";
1024 val=static_cast<int>(input->readLong(2));
1025 switch (val) {
1026 case 0:
1027 break;
1028 case 1:
1029 para.m_justify = MWAWParagraph::JustificationCenter;
1030 break;
1031 case 2:
1032 para.m_justify = MWAWParagraph::JustificationRight ;
1033 break;
1034 case 3:
1035 para.m_justify = MWAWParagraph::JustificationFull;
1036 break;
1037 default:
1038 f << "#align" << val << ",";
1039 break;
1040 }
1041 para.m_marginsUnit=librevenge::RVNG_POINT;
1042 for (auto &margin : para.m_margins)
1043 margin = double(input->readLong(4))/65536.;
1044 para.m_margins[0]=*para.m_margins[0]-*para.m_margins[1];
1045 double spacing[3];
1046 for (double &i : spacing)
1047 i = double(input->readLong(4))/65536.;
1048 int unit[3];
1049 for (auto &u : unit) u = static_cast<int>(input->readLong(1));
1050 switch (unit[0]) {
1051 case 1:
1052 case 2:
1053 case 3:
1054 case 4:
1055 case 5:
1056 para.setInterline(spacing[0], librevenge::RVNG_POINT);
1057 break;
1058 case 6:
1059 para.setInterline(spacing[0], librevenge::RVNG_PERCENT);
1060 break;
1061 default:
1062 f << "#interline=" << spacing[0] << "[unit=" << unit[0] << "],";
1063 }
1064 for (int w=1; w < 3; ++w) {
1065 if (unit[w]==6)
1066 para.m_spacings[w]=spacing[w]*12./72.;
1067 else if (unit[w]>0 && unit[w]<6)
1068 para.m_spacings[w]=spacing[w]/72.;
1069 else
1070 f << "#spac" << w << "=" << spacing[w] << "[unit=" << unit[w] << "],";
1071 }
1072 val = static_cast<int>(input->readLong(1));
1073 if (val) f << "#f0=" << val << ",";
1074
1075 for (int i=0; i < 20; ++i) {
1076 MWAWTabStop tab;
1077 val = static_cast<int>(input->readLong(1));
1078 switch (val) {
1079 case 0: // left
1080 break;
1081 case 1:
1082 tab.m_alignment = MWAWTabStop::CENTER;
1083 break;
1084 case 2:
1085 tab.m_alignment = MWAWTabStop::RIGHT;
1086 break;
1087 case 3:
1088 tab.m_alignment = MWAWTabStop::DECIMAL;
1089 break;
1090 default:
1091 f << "#tab" << i << "[align]=" << val << ",";
1092 break;
1093 }
1094 auto leaderChar = char(input->readULong(1));
1095 if (leaderChar) {
1096 int unicode= m_parserState->m_fontConverter->unicode(3, static_cast<unsigned char>(leaderChar));
1097 if (unicode==-1)
1098 tab.m_leaderCharacter = uint16_t(leaderChar);
1099 else
1100 tab.m_leaderCharacter = uint16_t(unicode);
1101 }
1102 long tPos=input->readLong(4);
1103 if (tPos==-1) {
1104 input->seek(2, librevenge::RVNG_SEEK_CUR);
1105 continue;
1106 }
1107 tab.m_position=double(tPos)/72./65536.;
1108 auto decimalChar = char(input->readULong(1));
1109 if (decimalChar) {
1110 int unicode= m_parserState->m_fontConverter->unicode(3, static_cast<unsigned char>(decimalChar));
1111 if (unicode==-1)
1112 tab.m_decimalCharacter = uint16_t(decimalChar);
1113 else
1114 tab.m_decimalCharacter = uint16_t(unicode);
1115 }
1116 val = static_cast<int>(input->readLong(1));
1117 if (val) f << "#tab" << i << "[f0=" << val << ",";
1118 para.m_tabs->push_back(tab);
1119 }
1120 para.m_extra=f.str();
1121
1122 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1123 return true;
1124 }
1125
1126 //////////////////////////////////////////////
1127 // Token
1128 //////////////////////////////////////////////
1129 bool GreatWksText::readToken(GreatWksTextInternal::Token &token, long &nChar)
1130 {
1131 token = GreatWksTextInternal::Token();
1132 MWAWInputStreamPtr &input= m_parserState->m_input;
1133 long pos=input->tell();
1134 long endPos=pos+18;
1135 if (!input->checkPosition(endPos))
1136 return false;
1137
1138 libmwaw::DebugStream f;
1139 token.m_type=static_cast<int>(input->readULong(1));
1140 token.m_format=static_cast<int>(input->readULong(1));
1141 nChar=input->readLong(4);
1142 if (token.m_type==0x15||token.m_type==0x16)
1143 token.m_date=static_cast<uint32_t>(input->readULong(4));
1144 else if (token.m_type==4) {
1145 token.m_dataSize = input->readLong(4);
1146 float dim[2];
1147 for (auto &d : dim) d = float(input->readLong(4))/65536.f;
1148 token.m_dim=MWAWVec2f(dim[0],dim[1]); // checkme
1149 }
1150 int nUnread=int(endPos-input->tell())/2;
1151 for (int i=0; i < nUnread; i++) {
1152 auto val=static_cast<int>(input->readLong(2));
1153 if (val) f << "f" << i << "=" << val << ",";
1154 }
1155 token.m_extra=f.str();
1156
1157 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1158 return true;
1159 }
1160
1161 //////////////////////////////////////////////
1162 // send data
1163 //////////////////////////////////////////////
1164 bool GreatWksText::sendMainText()
1165 {
1166 for (auto const &zone : m_state->m_zonesList) {
1167 if (!zone.isMain())
1168 continue;
1169 return sendZone(zone);
1170 }
1171 return false;
1172 }
1173
1174 bool GreatWksText::sendHF(int id)
1175 {
1176 for (auto const &zone : m_state->m_zonesList) {
1177 if (zone.isMain())
1178 continue;
1179 if (id--==0)
1180 return sendZone(zone);
1181 }
1182 MWAW_DEBUG_MSG(("GreatWksText::sendHF: can not find a header/footer\n"));
1183 return false;
1184 }
1185
1186 void GreatWksText::flushExtra()
1187 {
1188 MWAWListenerPtr listener=m_parserState->getMainListener();
1189 if (!listener) {
1190 MWAW_DEBUG_MSG(("GreatWksText::flushExtra: can not find a listener\n"));
1191 return;
1192 }
1193 for (auto const &zone : m_state->m_zonesList) {
1194 if (zone.m_parsed)
1195 continue;
1196 sendZone(zone);
1197 }
1198 }
1199
1200 bool GreatWksText::sendSimpleTextbox(MWAWEntry const &entry, MWAWListenerPtr listener)
1201 {
1202 if (!listener)
1203 listener=m_parserState->getMainListener();
1204 if (!listener || !listener->canWriteText()) {
1205 MWAW_DEBUG_MSG(("GreatWksText::sendSimpleTextbox: can not find a listener\n"));
1206 return false;
1207 }
1208 if (entry.length()<51) {
1209 MWAW_DEBUG_MSG(("GreatWksText::sendSimpleTextbox: the entry seems to short\n"));
1210 return false;
1211 }
1212 MWAWInputStreamPtr &input= m_parserState->m_input;
1213 long pos=entry.begin();
1214 input->seek(pos, librevenge::RVNG_SEEK_SET);
1215
1216 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1217 libmwaw::DebugStream f;
1218 f << "Entries(Texbox):";
1219 bool ok = input->readLong(4)==0;
1220 for (int i=0; ok && i < 2; ++i) {
1221 auto val=static_cast<int>(input->readLong(1));
1222 if (val==0) continue;
1223 if (val!=1) {
1224 f << "#fl" << i << "=" << val << ",";
1225 ok = false;
1226 break;
1227 }
1228 if (i==0)
1229 f << "smartquote,";
1230 else
1231 f << "hidepict,";
1232 }
1233 auto type=static_cast<int>(input->readLong(1));
1234 if (ok) {
1235 if (type<0 || type>5) {
1236 f << "#type=" << type << ",";
1237 ok = false;
1238 }
1239 else if (type==1)
1240 f << "textbox[draw],";
1241 else if (type!=2)
1242 f << "type=" << type << ",";
1243 }
1244 if (ok) ok=input->readLong(1)==1;
1245
1246 if (!ok) {
1247 MWAW_DEBUG_MSG(("GreatWksText::sendSimpleTextbox: the header seems bad\n"));
1248 f << "###";
1249 ascFile.addPos(pos);
1250 ascFile.addNote(f.str().c_str());
1251 input->seek(pos, librevenge::RVNG_SEEK_SET);
1252 return false;
1253 }
1254 MWAWFont font;
1255 font.setId(m_state->getFId(static_cast<int>(input->readULong(2))));
1256 auto flag =static_cast<int>(input->readULong(2));
1257 uint32_t flags=0;
1258 if (flag&0x1) flags |= MWAWFont::boldBit;
1259 if (flag&0x2) flags |= MWAWFont::italicBit;
1260 if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
1261 if (flag&0x8) flags |= MWAWFont::embossBit;
1262 if (flag&0x10) flags |= MWAWFont::shadowBit;
1263 if (flag&0x20) font.setDeltaLetterSpacing(-1);
1264 if (flag&0x40) font.setDeltaLetterSpacing(1);
1265 if (flag&0x100) font.set(MWAWFont::Script::super100());
1266 if (flag&0x200) font.set(MWAWFont::Script::sub100());
1267 if (flag&0x800) font.setStrikeOutStyle(MWAWFont::Line::Simple);
1268 if (flag&0x2000) {
1269 font.setUnderlineStyle(MWAWFont::Line::Simple);
1270 font.setUnderlineType(MWAWFont::Line::Double);
1271 }
1272 flag &=0xD480;
1273 if (flag) f << "font[#fl]=" << std::hex << flag << std::dec << ",";
1274 font.setFlags(flags);
1275 font.setSize(float(input->readULong(2)));
1276 unsigned char color[3];
1277 for (auto &c : color) c = static_cast<unsigned char>(input->readULong(2)>>8);
1278 font.setColor(MWAWColor(color[0],color[1],color[2]));
1279 f << "font=[" << font.getDebugString(m_parserState->m_fontConverter) << "],";
1280 listener->setFont(font);
1281
1282 int val;
1283 f << "unkn=[" << std::hex;
1284 for (int i=0; i<6; i++) { // junk ?
1285 val=static_cast<int>(input->readULong(2));
1286 if (val)
1287 f << val << ",";
1288 else
1289 f << "_,";
1290 }
1291 f << std::dec << "],";
1292 MWAWParagraph para;
1293 val=static_cast<int>(input->readLong(2));
1294 switch (val) {
1295 case 0:
1296 break;
1297 case 1:
1298 para.m_justify = MWAWParagraph::JustificationCenter;
1299 break;
1300 case 2:
1301 para.m_justify = MWAWParagraph::JustificationRight ;
1302 break;
1303 case 3:
1304 para.m_justify = MWAWParagraph::JustificationFull;
1305 break;
1306 default:
1307 f << "#align" << val << ",";
1308 break;
1309 }
1310 f << para <<",";
1311 listener->setParagraph(para);
1312 double dim[4];
1313 for (auto &d : dim) d=double(input->readLong(4))/65536.;
1314 f << "dim=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << "," ;
1315 auto fSz=static_cast<int>(input->readULong(1));
1316 if (50+fSz > entry.length()) {
1317 MWAW_DEBUG_MSG(("GreatWksText::sendSimpleTextbox: the text size seems too big\n"));
1318 f << "###";
1319 ascFile.addPos(pos);
1320 ascFile.addNote(f.str().c_str());
1321 input->seek(pos, librevenge::RVNG_SEEK_SET);
1322 return false;
1323 }
1324
1325 ascFile.addPos(pos);
1326 ascFile.addNote(f.str().c_str());
1327
1328 pos=input->tell();
1329 f.str("");
1330 f << "Entries(Text):";
1331 for (int i=0; i < fSz; ++i) {
1332 auto c = char(input->readULong(1));
1333 f << c;
1334 switch (c) {
1335 case 0x9:
1336 listener->insertTab();
1337 break;
1338 case 0xd:
1339 listener->insertEOL();
1340 break;
1341 default:
1342 listener->insertCharacter(static_cast<unsigned char>(c));
1343 break;
1344 }
1345 }
1346 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1347 ascFile.addPos(pos);
1348 ascFile.addNote(f.str().c_str());
1349 return true;
1350 }
1351
1352 bool GreatWksText::sendZone(GreatWksTextInternal::Zone const &zone, MWAWListenerPtr listener)
1353 {
1354 bool inLocalZone=false;
1355 if (listener)
1356 inLocalZone=true;
1357 else
1358 listener=m_parserState->getMainListener();
1359 if (!listener || !listener->canWriteText()) {
1360 MWAW_DEBUG_MSG(("GreatWksText::sendZone: can not find a listener\n"));
1361 return false;
1362 }
1363 bool isMain = zone.isMain();
1364 int actPage = 1, actCol = 0, numCol=1;
1365 if (isMain && !listener->canOpenSectionAddBreak()) {
1366 MWAW_DEBUG_MSG(("GreatWksText::sendZone: in a main zone, but can not open section\n"));
1367 isMain = false;
1368 }
1369 else if (isMain) {
1370 m_document.newPage(1);
1371 MWAWSection sec=m_document.getMainSection();
1372 numCol = sec.numColumns();
1373 if (numCol>1) {
1374 if (listener->isSectionOpened())
1375 listener->closeSection();
1376 listener->openSection(sec);
1377 }
1378 }
1379 if (!zone.m_textEntry.valid())
1380 return true;
1381 MWAWInputStreamPtr &input= m_parserState->m_input;
1382 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1383 libmwaw::DebugStream f;
1384 f << "Entries(Text):";
1385 zone.m_parsed=true;
1386 long pos = zone.m_textEntry.begin(), endPos = zone.m_textEntry.end();
1387 input->seek(pos, librevenge::RVNG_SEEK_SET);
1388
1389 long cPos=0;
1390 while (1) {
1391 long actPos = input->tell();
1392 bool done = input->isEnd() || actPos==endPos;
1393
1394 char c = done ? char(0) : char(input->readULong(1));
1395 if (pos!=actPos && (c==0xd || done)) {
1396 ascFile.addPos(pos);
1397 ascFile.addNote(f.str().c_str());
1398 f.str("");
1399 f << "Text:";
1400 pos = actPos+1;
1401 }
1402 if (done) break;
1403
1404 auto pIt=zone.m_posPLCMap.find(cPos);
1405 GreatWksTextInternal::Token token;
1406 while (pIt!=zone.m_posPLCMap.end() && pIt->first==cPos) {
1407 GreatWksTextInternal::PLC const &plc=(pIt++)->second;
1408 f << "[" << plc << "]";
1409 switch (plc.m_type) {
1410 case GreatWksTextInternal::P_Font:
1411 if (plc.m_id < 0 || plc.m_id >= static_cast<int>(zone.m_fontList.size())) {
1412 MWAW_DEBUG_MSG(("GreatWksText::sendZone: can not find font %d\n", plc.m_id));
1413 break;
1414 }
1415 listener->setFont(zone.m_fontList[size_t(plc.m_id)]);
1416 break;
1417 case GreatWksTextInternal::P_Ruler:
1418 if (plc.m_id < 0 || plc.m_id >= static_cast<int>(zone.m_rulerList.size())) {
1419 MWAW_DEBUG_MSG(("GreatWksText::sendZone: can not find ruler %d\n", plc.m_id));
1420 break;
1421 }
1422 listener->setParagraph(zone.m_rulerList[size_t(plc.m_id)]);
1423 break;
1424 case GreatWksTextInternal::P_Token:
1425 if (plc.m_id < 0 || plc.m_id >= static_cast<int>(zone.m_tokenList.size())) {
1426 MWAW_DEBUG_MSG(("GreatWksText::sendZone: can not find token %d\n", plc.m_id));
1427 break;
1428 }
1429 token=zone.m_tokenList[size_t(plc.m_id)];
1430 break;
1431 case GreatWksTextInternal::P_Page:
1432 case GreatWksTextInternal::P_Unknown:
1433 #if !defined(__clang__)
1434 default:
1435 #endif
1436 break;
1437 }
1438 }
1439 if (c!=0xd) f << c;
1440 switch (c) {
1441 case 0x4: {
1442 f << "[pict]";
1443 if (token.m_type!=4 || !token.m_pictEntry.valid()) {
1444 MWAW_DEBUG_MSG(("GreatWksText::sendZone: can not find a picture\n"));
1445 f << "###";
1446 break;
1447 }
1448 if (inLocalZone) {
1449 MWAW_DEBUG_MSG(("GreatWksText::sendZone: oops, can not send a picture in a graphic zone\n"));
1450 break;
1451 }
1452 MWAWPosition pictPos(MWAWVec2f(0,0), token.m_dim, librevenge::RVNG_POINT);
1453 pictPos.setRelativePosition(MWAWPosition::Char, MWAWPosition::XLeft, MWAWPosition::YBottom);
1454 m_document.sendPicture(token.m_pictEntry, pictPos);
1455 break;
1456 }
1457 case 0x9:
1458 listener->insertTab();
1459 break;
1460 case 0xb:
1461 f << "[colbreak]";
1462 if (!isMain) {
1463 MWAW_DEBUG_MSG(("GreatWksText::sendZone: find column break in auxilliary block\n"));
1464 break;
1465 }
1466 if (actCol < numCol-1 && numCol > 1) {
1467 listener->insertBreak(MWAWListener::ColumnBreak);
1468 actCol++;
1469 }
1470 else {
1471 actCol = 0;
1472 m_document.newPage(++actPage);
1473 }
1474 break;
1475 case 0xc:
1476 f << "[pagebreak]";
1477 if (!isMain) {
1478 f << "###";
1479 MWAW_DEBUG_MSG(("GreatWksText::sendZone: find page break in auxilliary zone\n"));
1480 break;
1481 }
1482 m_document.newPage(++actPage);
1483 actCol = 0;
1484 break;
1485 case 0xd:
1486 listener->insertEOL();
1487 break;
1488 case 2: // page number
1489 case 0x15: // date
1490 case 0x16: // time
1491 if (token.sendTo(*listener))
1492 break;
1493 f << "###";
1494 MWAW_DEBUG_MSG(("GreatWksText::sendZone: can not send token\n"));
1495 break;
1496 default:
1497 listener->insertCharacter(static_cast<unsigned char>(c));
1498 break;
1499 }
1500 cPos++;
1501 }
1502 return true;
1503 }
1504 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: