Debian Codegraph @

src/lib/DocMkrText.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <iomanip>
35 #include <iostream>
36 #include <limits>
37 #include <map>
38 #include <sstream>
39
40 #include <librevenge/librevenge.h>
41
42 #include "MWAWTextListener.hxx"
43 #include "MWAWDebug.hxx"
44 #include "MWAWFont.hxx"
45 #include "MWAWFontConverter.hxx"
46 #include "MWAWPageSpan.hxx"
47 #include "MWAWParagraph.hxx"
48 #include "MWAWPosition.hxx"
49 #include "MWAWRSRCParser.hxx"
50 #include "MWAWSubDocument.hxx"
51
52 #include "DocMkrParser.hxx"
53
54 #include "DocMkrText.hxx"
55
56 /** Internal: the structures of a DocMkrText */
57 namespace DocMkrTextInternal
58 {
59 ////////////////////////////////////////
60 //! Internal: structure to store a table of contents of a DocMkrText
61 struct TOC {
62 //! constructor
63 TOC()
64 : m_cIdList()
65 , m_textList()
66 {
67 }
68 //! returns true if the table is empty
69 bool empty() const
70 {
71 return m_textList.empty();
72 }
73 //! the toc chapter id
74 std::vector<int> m_cIdList;
75 //! the toc texts
76 std::vector<std::string> m_textList;
77 };
78
79 ////////////////////////////////////////
80 //! Internal: structure to store a footer data of a DocMkrText
81 struct Footer {
82 //! constructor
83 Footer()
84 : m_font(3,9)
85 , m_chapterResetPage(false)
86 , m_userInfo()
87 , m_extra("")
88 {
89 for (auto &item : m_items) item=0;
90 }
91 //! returns true if the footer is empty
92 bool empty() const
93 {
94 for (auto item : m_items)
95 if (item)
96 return false;
97 return true;
98 }
99 //! operator<<
100 friend std::ostream &operator<<(std::ostream &o, Footer const &f)
101 {
102 static char const *where[]= {"TL", "TC", "TR", "BL", "BC", "BR" };
103 static char const *what[]= { "nothing", "unkn1", "unkn2", "time", "date",
104 "page", "fileName", "chapName", "userText"
105 };
106 if (f.m_chapterResetPage)
107 o << "pageReset[chapter],";
108 for (int i = 0; i < 6; i++) {
109 if (!f.m_items[i]) continue;
110 o << where[i] << "=";
111 if (f.m_items[i] > 0 && f.m_items[i] <= 8)
112 o << what[f.m_items[i]] << ",";
113 else
114 o << "#unkn" << f.m_items[i] << ",";
115 }
116 o << f.m_extra;
117 return o;
118 }
119 //! the font
120 MWAWFont m_font;
121 //! true if a chapter reset page
122 bool m_chapterResetPage;
123 //! the item values
124 int m_items[6];
125 //! the user information entry
126 std::string m_userInfo;
127 //! extra data
128 std::string m_extra;
129 };
130
131 ////////////////////////////////////////
132 //! Internal: structure to store a the data of a DocMkrText Zone
133 struct Zone {
134 //! constructor
135 Zone()
136 : m_pos()
137 , m_justify(MWAWParagraph::JustificationLeft)
138 , m_backgroundColor(MWAWColor::white())
139 , m_useFooter(true)
140 , m_name("")
141 , m_posFontMap()
142 , m_numPages(0)
143 , m_parsed(false)
144 {
145 for (auto &margin : m_margins) margin=54;
146 }
147 //! the text entry
148 MWAWEntry m_pos;
149 //! the paragraph justification
150 MWAWParagraph::Justification m_justify;
151 //! the background color
152 MWAWColor m_backgroundColor;
153 //! print or ignore the footer
154 bool m_useFooter;
155 //! the margins L,T,R,B in points
156 int m_margins[4];
157 //! the name
158 std::string m_name;
159 //! the map of id -> font
160 std::map<long, MWAWFont > m_posFontMap;
161 //! the number of page
162 mutable int m_numPages;
163 //! a flag to know if we have send the data to the listener
164 mutable bool m_parsed;
165 };
166 ////////////////////////////////////////
167 //! Internal: the state of a DocMkrText
168 struct State {
169 //! constructor
170 State()
171 : m_version(-1)
172 , m_numPages(-1)
173 , m_actualPage(0)
174 , m_pageWidth(8.5)
175 , m_idZoneMap()
176 , m_footer()
177 , m_toc()
178 {
179 }
180 //! returns the zone corresponding to an id
181 Zone &getZone(int id)
182 {
183 if (m_idZoneMap.find(id)==m_idZoneMap.end())
184 m_idZoneMap[id]=Zone();
185 return m_idZoneMap.find(id)->second;
186 }
187
188 //! the file version
189 mutable int m_version;
190
191 int m_numPages /* the number of pages */, m_actualPage /* the actual page */;
192 //! the page width ( not really define so default value 8.5)
193 double m_pageWidth;
194 //! the map of id -> text zone
195 std::map<int, Zone > m_idZoneMap;
196 //! the footer
197 Footer m_footer;
198 //! the table of content
199 TOC m_toc;
200 };
201
202 ////////////////////////////////////////
203 //! Internal: the subdocument of a DocMkrText
204 class SubDocument final : public MWAWSubDocument
205 {
206 public:
207 //! constructor for a footer zone
208 SubDocument(DocMkrText &pars, MWAWInputStreamPtr const &input, int id, libmwaw::SubDocumentType type)
209 : MWAWSubDocument(pars.m_mainParser, input, MWAWEntry())
210 , m_textParser(&pars)
211 , m_id(id)
212 , m_text("")
213 , m_type(type) {}
214
215 //! constructor for a comment zone
216 SubDocument(DocMkrText &pars, MWAWInputStreamPtr const &input, std::string const &text, libmwaw::SubDocumentType type)
217 : MWAWSubDocument(pars.m_mainParser, input, MWAWEntry())
218 , m_textParser(&pars)
219 , m_id(-1)
220 , m_text(text)
221 , m_type(type) {}
222
223 //! destructor
224 ~SubDocument() final {}
225
226 //! operator!=
227 bool operator!=(MWAWSubDocument const &doc) const final;
228 //! the parser function
229 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
230
231 protected:
232 /** the text parser */
233 DocMkrText *m_textParser;
234 //! the subdocument id
235 int m_id;
236 //! the string text
237 std::string m_text;
238 //! the subdocument type
239 libmwaw::SubDocumentType m_type;
240 private:
241 SubDocument(SubDocument const &orig) = delete;
242 SubDocument &operator=(SubDocument const &orig) = delete;
243 };
244
245 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
246 {
247 if (!listener.get()) {
248 MWAW_DEBUG_MSG(("SubDocument::parse: no listener\n"));
249 return;
250 }
251 if (!m_textParser) {
252 MWAW_DEBUG_MSG(("SubDocument::parse: no text parser\n"));
253 return;
254 }
255
256 long pos = m_input->tell();
257 if (m_type == libmwaw::DOC_HEADER_FOOTER)
258 m_textParser->sendFooter(m_id);
259 else if (m_type == libmwaw::DOC_COMMENT_ANNOTATION) {
260 listener->setFont(MWAWFont(3,10));
261 m_textParser->sendString(m_text);
262 }
263 else {
264 MWAW_DEBUG_MSG(("SubDocument::parse: oops do not know how to send this kind of document\n"));
265 }
266 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
267 }
268
269 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
270 {
271 if (MWAWSubDocument::operator!=(doc)) return true;
272 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
273 if (!sDoc) return true;
274 if (m_textParser != sDoc->m_textParser) return true;
275 if (m_id != sDoc->m_id) return true;
276 if (m_text != sDoc->m_text) return true;
277 if (m_type != sDoc->m_type) return true;
278 return false;
279 }
280 }
281
282 ////////////////////////////////////////////////////////////
283 // constructor/destructor, ...
284 ////////////////////////////////////////////////////////////
285 DocMkrText::DocMkrText(DocMkrParser &parser)
286 : m_parserState(parser.getParserState())
287 , m_state(new DocMkrTextInternal::State)
288 , m_mainParser(&parser)
289 {
290 }
291
292 DocMkrText::~DocMkrText()
293 {
294 }
295
296 int DocMkrText::version() const
297 {
298 if (m_state->m_version < 0)
299 m_state->m_version = m_parserState->m_version;
300 return m_state->m_version;
301 }
302
303 int DocMkrText::numPages() const
304 {
305 if (m_state->m_numPages >= 0)
306 return m_state->m_numPages;
307 m_state->m_actualPage = 1;
308
309 int nPages = 0;
310 // do not replace by auto it : m_state->m_idZoneMap as it->second.m_numPages is modified
311 for (auto const &it : m_state->m_idZoneMap) {
312 auto const &zone= it.second;
313 computeNumPages(zone);
314 nPages += zone.m_numPages;
315 }
316 m_state->m_numPages = nPages;
317 return m_state->m_numPages;
318 }
319
320 int DocMkrText::numChapters() const
321 {
322 return int(m_state->m_idZoneMap.size());
323 }
324
325 void DocMkrText::sendComment(std::string const &str)
326 {
327 if (!m_parserState->m_textListener) {
328 MWAW_DEBUG_MSG(("DocMkrText::sendComment: called without listener\n"));
329 return;
330 }
331 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
332 std::shared_ptr<MWAWSubDocument> comment
333 (new DocMkrTextInternal::SubDocument(*this, input, str, libmwaw::DOC_COMMENT_ANNOTATION));
334 m_parserState->m_textListener->insertComment(comment);
335 }
336
337 ////////////////////////////////////////////////////////////
338 // pages/...
339 ////////////////////////////////////////////////////////////
340 void DocMkrText::computeNumPages(DocMkrTextInternal::Zone const &zone) const
341 {
342 if (zone.m_numPages || !zone.m_pos.valid())
343 return;
344 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
345 input->seek(zone.m_pos.begin(), librevenge::RVNG_SEEK_SET);
346 long len = zone.m_pos.length();
347 int nPages=1;
348 for (long i=0; i < len; i++) {
349 if (input->isEnd())
350 break;
351 if (input->readLong(1)==0)
352 nPages++;
353 }
354 zone.m_numPages = nPages;
355 }
356
357 void DocMkrText::updatePageSpanList(std::vector<MWAWPageSpan> &spanList)
358 {
359 numPages();
360 spanList.resize(0);
361 MWAWPageSpan ps;
362 ps.setMarginTop(0.1);
363 ps.setMarginBottom(0.015);
364 ps.setMarginLeft(0.1);
365 ps.setMarginRight(0.1);
366 bool hasFooter = !m_state->m_footer.empty();
367 bool needResetPage = m_state->m_footer.m_chapterResetPage;
368 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
369 for (auto it : m_state->m_idZoneMap) {
370 int zId = it.first;
371 DocMkrTextInternal::Zone const &zone= it.second;
372 if (zone.m_numPages <= 0)
373 continue;
374 MWAWPageSpan span(ps);
375 if (needResetPage)
376 span.setPageNumber(1);
377 if (zone.m_margins[0]>=0)
378 span.setMarginLeft(double(zone.m_margins[0])/72.);
379 if (zone.m_margins[1]>=0)
380 span.setMarginTop(double(zone.m_margins[1])/72.);
381 if (zone.m_margins[2]>=0)
382 span.setMarginRight(double(zone.m_margins[2])/72.);
383 if (zone.m_margins[3]>=0)
384 span.setMarginBottom(double(zone.m_margins[3])/72.);
385 span.setBackgroundColor(zone.m_backgroundColor);
386 if (hasFooter && zone.m_useFooter) {
387 MWAWHeaderFooter footer(MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
388 footer.m_subDocument.reset
389 (new DocMkrTextInternal::SubDocument(*this, input, zId, libmwaw::DOC_HEADER_FOOTER));
390 span.setHeaderFooter(footer);
391 }
392 for (int i = 0; i < zone.m_numPages; i++) {
393 spanList.push_back(span);
394 span.setPageNumber(-1);
395 }
396 }
397 if (spanList.size()==0 || !m_state->m_toc.empty())
398 spanList.push_back(ps);
399 }
400
401 ////////////////////////////////////////////////////////////
402 // Intermediate level
403 ////////////////////////////////////////////////////////////
404
405 // find the different zones
406 bool DocMkrText::createZones()
407 {
408 if (!m_mainParser->getRSRCParser()) {
409 MWAW_DEBUG_MSG(("DocMkrText::createZones: can not find the entry map\n"));
410 return false;
411 }
412 MWAWRSRCParserPtr rsrcParser = m_mainParser->getRSRCParser();
413 auto const &entryMap = rsrcParser->getEntriesMap();
414
415 // entry 128: font name and size
416 auto it = entryMap.lower_bound("rQDF");
417 while (it != entryMap.end()) {
418 if (it->first != "rQDF")
419 break;
420 MWAWEntry const &entry = it++->second;
421 readFontNames(entry);
422 }
423 // Footer: foot:128
424 it = entryMap.lower_bound("foot");
425 while (it != entryMap.end()) {
426 if (it->first != "foot")
427 break;
428 MWAWEntry const &entry = it++->second;
429 readFooter(entry);
430 }
431 // entry 128: ?
432 it = entryMap.lower_bound("cnt#");
433 while (it != entryMap.end()) {
434 if (it->first != "cnt#")
435 break;
436 MWAWEntry const &entry = it++->second;
437 readTOC(entry);
438 }
439
440 // the chapter zone zone
441 it = entryMap.lower_bound("styl");
442 while (it != entryMap.end()) {
443 if (it->first != "styl")
444 break;
445 MWAWEntry const &entry = it++->second;
446 readStyles(entry);
447 }
448 it = entryMap.lower_bound("TEXT");
449 while (it != entryMap.end()) {
450 if (it->first != "TEXT")
451 break;
452 MWAWEntry const &entry = it++->second;
453 m_state->getZone(entry.id()).m_pos = entry;
454 }
455
456 it = entryMap.lower_bound("Wndo");
457 while (it != entryMap.end()) {
458 if (it->first != "Wndo")
459 break;
460 MWAWEntry const &entry = it++->second;
461 readWindows(entry);
462 }
463 // font color
464 it = entryMap.lower_bound("clut");
465 while (it != entryMap.end()) {
466 if (it->first != "clut")
467 break;
468 MWAWEntry const &entry = it++->second;
469 std::vector<MWAWColor> cmap;
470 rsrcParser->parseClut(entry, cmap);
471 if (entry.id() != 128)
472 continue;
473 for (size_t i = 0; i < cmap.size(); i++) {
474 if (m_state->m_idZoneMap.find(int(i)+128)==m_state->m_idZoneMap.end())
475 continue;
476 m_state->m_idZoneMap.find(int(i)+128)->second.m_backgroundColor=cmap[i];
477 }
478 }
479
480 it = entryMap.lower_bound("STR ");
481 while (it != entryMap.end()) {
482 if (it->first != "STR ")
483 break;
484 MWAWEntry const &entry = it++->second;
485 // 1000: footer (user information)
486 if (entry.id()==1000 && entry.length()>0) {
487 std::string userInfo("");
488 rsrcParser->parseSTR(entry,userInfo);
489 m_state->m_footer.m_userInfo = userInfo;
490 }
491 // 200?: chapter name
492 else if (entry.id()>2000 &&
493 m_state->m_idZoneMap.find(entry.id()-2001+128)!=m_state->m_idZoneMap.end()) {
494 std::string name("");
495 rsrcParser->parseSTR(entry,name);
496 m_state->getZone(entry.id()-2001+128).m_name=name;
497 }
498 }
499 return m_state->m_idZoneMap.size();
500 }
501
502 ////////////////////////////////////////////////////////////
503 // Text
504 ////////////////////////////////////////////////////////////
505 bool DocMkrText::sendText(DocMkrTextInternal::Zone const &zone)
506 {
507 MWAWTextListenerPtr listener=m_parserState->m_textListener;
508 if (!listener) {
509 MWAW_DEBUG_MSG(("DocMkrText::sendText: can not find the listener\n"));
510 return false;
511 }
512 if (!zone.m_pos.valid()) {
513 MWAW_DEBUG_MSG(("DocMkrText::sendText: the entry is bad\n"));
514 return false;
515 }
516 zone.m_parsed = true;
517 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
518 libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii();
519 long pos = zone.m_pos.begin(), debPos=pos-4;
520 input->seek(pos, librevenge::RVNG_SEEK_SET);
521 libmwaw::DebugStream f;
522 f << "Entries(TEXT)[" << zone.m_pos.id() << "]:";
523 listener->setFont(MWAWFont(3,12));
524 MWAWParagraph para;
525 para.m_justify=zone.m_justify;
526 listener->setParagraph(para);
527 std::map<long, MWAWFont >::const_iterator fontIt;
528 int nPict=0, zId=zone.m_pos.id()-128;
529 double w = m_state->m_pageWidth-double(zone.m_margins[0]+zone.m_margins[2])/72.;
530 for (long i = 0; i <= zone.m_pos.length(); i++) {
531 bool endPos = (i==zone.m_pos.length());
532 unsigned char c=endPos ? static_cast<unsigned char>(0) : static_cast<unsigned char>(input->readULong(1));
533 if (endPos || c==0xd || c==0) {
534 ascFile.addPos(debPos);
535 ascFile.addNote(f.str().c_str());
536 debPos = input->tell();
537 if (endPos) break;
538 f.str("");
539 f << "TEXT:";
540 }
541 fontIt=zone.m_posFontMap.find(i);
542 if (fontIt != zone.m_posFontMap.end())
543 listener->setFont(fontIt->second);
544 if (c)
545 f << c;
546 switch (c) {
547 case 0:
548 m_mainParser->newPage(++m_state->m_actualPage);
549 break;
550 case 0x9:
551 listener->insertTab();
552 break;
553 case 0xd:
554 listener->insertEOL();
555 break;
556 case 0x11: // command key
557 listener->insertUnicode(0x2318);
558 break;
559 case 0x14: // apple logo: check me
560 listener->insertUnicode(0xf8ff);
561 break;
562 case 0xca:
563 m_mainParser->sendPicture(zId, ++nPict, w);
564 break;
565 default:
566 i += listener->insertCharacter(c, input, zone.m_pos.end());
567 break;
568 }
569 }
570 return true;
571 }
572
573 ////////////////////////////////////////////////////////////
574 // Fonts
575 ////////////////////////////////////////////////////////////
576 bool DocMkrText::readFontNames(MWAWEntry const &entry)
577 {
578 if (!entry.valid() || entry.length()<2) {
579 MWAW_DEBUG_MSG(("DocMkrText::readFontNames: the entry is bad\n"));
580 return false;
581 }
582 entry.setParsed(true);
583 long pos = entry.begin();
584 long endPos = entry.end();
585 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
586 libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii();
587 input->seek(pos, librevenge::RVNG_SEEK_SET);
588
589 libmwaw::DebugStream f;
590 auto N=static_cast<int>(input->readULong(2));
591 f << "Entries(FontName)[" << entry.type() << "-" << entry.id() << "]:N="<<N;
592 ascFile.addPos(pos-4);
593 ascFile.addNote(f.str().c_str());
594
595 for (int i = 0; i < N; i++) {
596 f.str("");
597 f << "FontName-" << i << ":";
598 pos = input->tell();
599 if (pos+1 > endPos) {
600 f << "###";
601 ascFile.addPos(pos);
602 ascFile.addNote(f.str().c_str());
603
604 MWAW_DEBUG_MSG(("DocMkrText::readFontNames: can not read fontname %d\n", i));
605 return false;
606 }
607 auto sz = static_cast<int>(input->readULong(1));
608 if (pos+1+sz+2 > endPos) {
609 f.str("");
610 f << "###";
611 ascFile.addPos(pos);
612 ascFile.addNote(f.str().c_str());
613
614 MWAW_DEBUG_MSG(("DocMkrText::readFontNames: fontname size %d is bad\n", i));
615 return false;
616 }
617
618 std::string str("");
619 for (int c=0; c < sz; c++)
620 str += char(input->readULong(1));
621 f << str << ",";
622
623 auto val=static_cast<int>(input->readULong(1));
624 if (val) f << "unkn=" << val << ",";
625 auto N1=static_cast<int>(input->readULong(1));
626 if (pos+1+sz+2+N1 > endPos) {
627 f.str("");
628 f << "###";
629 ascFile.addPos(pos);
630 ascFile.addNote(f.str().c_str());
631
632 MWAW_DEBUG_MSG(("DocMkrText::readFontNames: fontname size %d is bad\n", i));
633 return false;
634 }
635 f << "fontSz=[";
636 for (int j = 0; j < N1; j++)
637 f << input->readULong(1) << ",";
638 f << "],";
639 ascFile.addPos(pos);
640 ascFile.addNote(f.str().c_str());
641 }
642 return true;
643 }
644
645 ////////////////////////////////////////////////////////////
646 // the styles
647 ////////////////////////////////////////////////////////////
648 bool DocMkrText::readStyles(MWAWEntry const &entry)
649 {
650 if (!entry.valid() || entry.length()<2) {
651 MWAW_DEBUG_MSG(("DocMkrText::readStyles: the entry is bad\n"));
652 return false;
653 }
654 entry.setParsed(true);
655 long pos = entry.begin();
656 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
657 libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii();
658 input->seek(pos, librevenge::RVNG_SEEK_SET);
659
660 libmwaw::DebugStream f;
661 auto N=static_cast<int>(input->readULong(2));
662 f << "Entries(Style)[" << entry.type() << "-" << entry.id() << "]:N="<<N;
663 if (20*N+2 != entry.length()) {
664 MWAW_DEBUG_MSG(("DocMkrText::readStyles: the number of values seems bad\n"));
665 f << "###";
666 ascFile.addPos(pos-4);
667 ascFile.addNote(f.str().c_str());
668 }
669 ascFile.addPos(pos-4);
670 ascFile.addNote(f.str().c_str());
671 DocMkrTextInternal::Zone &zone = m_state->getZone(entry.id());
672 for (int i = 0; i < N; i++) {
673 MWAWFont font;
674 f.str("");
675 pos = input->tell();
676 long cPos = input->readLong(4);
677 int dim[2];
678 for (int &j : dim)
679 j = static_cast<int>(input->readLong(2));
680 f << "height?=" << dim[0] << ":" << dim[1] << ",";
681 font.setId(static_cast<int>(input->readULong(2)));
682 auto flag=static_cast<int>(input->readULong(1));
683 uint32_t flags = 0;
684 // bit 1 = plain
685 if (flag&0x1) flags |= MWAWFont::boldBit;
686 if (flag&0x2) flags |= MWAWFont::italicBit;
687 if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
688 if (flag&0x8) flags |= MWAWFont::embossBit;
689 if (flag&0x10) flags |= MWAWFont::shadowBit;
690 if (flag&0xe0) f << "#flags=" << std::hex << (flag&0xe0) << std::dec << ",";
691 flag=static_cast<int>(input->readULong(1));
692 if (flag) f << "#flags1=" << std::hex << flag << std::dec << ",";
693 font.setSize(float(input->readULong(2)));
694 font.setFlags(flags);
695 unsigned char col[3];
696 for (auto &c : col) c = static_cast<unsigned char>(input->readULong(2)>>8);
697 font.setColor(MWAWColor(col[0],col[1],col[2]));
698 font.m_extra=f.str();
699 if (zone.m_posFontMap.find(cPos) != zone.m_posFontMap.end()) {
700 MWAW_DEBUG_MSG(("DocMkrText::readStyles: a style for pos=%lx already exist\n", static_cast<long unsigned int>(cPos)));
701 }
702 else
703 zone.m_posFontMap[cPos] = font;
704 f.str("");
705 f << "Style-" << i << ":" << "cPos=" << std::hex << cPos << std::dec << ",";
706 #ifdef DEBUG
707 f << ",font=[" << font.getDebugString(m_parserState->m_fontConverter) << "]";
708 #endif
709 ascFile.addPos(pos);
710 ascFile.addNote(f.str().c_str());
711 }
712 return true;
713 }
714
715 // Table of Content information
716 ////////////////////////////////////////////////////////////
717 bool DocMkrText::sendTOC()
718 {
719 MWAWTextListenerPtr listener=m_parserState->m_textListener;
720 if (!listener) {
721 MWAW_DEBUG_MSG(("DocMkrText::sendTOC: can not find the listener\n"));
722 return false;
723 }
724 auto const &toc=m_state->m_toc;
725 if (toc.empty())
726 return true;
727 if (toc.m_cIdList.size() != toc.m_textList.size()) {
728 MWAW_DEBUG_MSG(("DocMkrText::sendTOC: the TOC is bad\n"));
729 return false;
730 }
731
732 MWAWFont cFont(3,12);
733 cFont.setFlags(MWAWFont::boldBit);
734 MWAWFont actFont(3,10);
735 listener->setFont(actFont);
736 double w = m_state->m_pageWidth;
737 MWAWParagraph para;
738 MWAWTabStop tab;
739 tab.m_alignment = MWAWTabStop::RIGHT;
740 tab.m_leaderCharacter='.';
741 tab.m_position = w;
742 para.m_tabs->push_back(tab);
743 listener->setParagraph(para);
744
745 std::stringstream ss;
746 int prevId=-1;
747 for (size_t i = 0; i < toc.m_textList.size(); i++) {
748 int zId=toc.m_cIdList[i];
749 ss.str("");
750 ss << "C" << zId;
751
752 if (zId!=prevId) {
753 prevId=zId;
754 listener->setFont(cFont);
755
756 listener->insertUnicodeString(librevenge::RVNGString(ss.str().c_str()));
757 listener->insertChar(' ');
758 if (m_state->m_idZoneMap.find(127+zId)!=m_state->m_idZoneMap.end())
759 sendString(m_state->m_idZoneMap.find(127+zId)->second.m_name);
760 listener->insertEOL();
761 listener->setFont(actFont);
762 }
763 sendString(toc.m_textList[i]);
764 listener->insertTab();
765 listener->insertUnicodeString(librevenge::RVNGString(ss.str().c_str()));
766 listener->insertEOL();
767 }
768 return true;
769 }
770
771 bool DocMkrText::readTOC(MWAWEntry const &entry)
772 {
773 if (!entry.valid() || entry.length()<2) {
774 MWAW_DEBUG_MSG(("DocMkrText::readTOC: the entry is bad\n"));
775 return false;
776 }
777 entry.setParsed(true);
778 long pos = entry.begin();
779 long endPos = entry.end();
780 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
781 libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii();
782 input->seek(pos, librevenge::RVNG_SEEK_SET);
783
784 libmwaw::DebugStream f;
785 f << "Entries(TOC)[" << entry.type() << ":" << entry.id() << "]:";
786 auto N=static_cast<int>(input->readULong(2));
787 ascFile.addPos(pos-4);
788 ascFile.addNote(f.str().c_str());
789
790 for (int i = 0; i < N; i++) {
791 f.str("");
792 f << "TOC-" << i << ":";
793 pos = input->tell();
794 if (pos+7 > endPos) {
795 f << "###";
796 ascFile.addPos(pos);
797 ascFile.addNote(f.str().c_str());
798
799 MWAW_DEBUG_MSG(("DocMkrText::readTOC: can not read string %d\n", i));
800 return false;
801 }
802 auto zId = static_cast<int>(input->readLong(2));
803 if (zId) f << "zId=" << zId+127 << ",";
804 int cPos[2];
805 for (int &j : cPos)
806 j = static_cast<int>(input->readULong(2));
807 f << "cPos=" << std::hex << cPos[0] << "<->" << cPos[1] << std::dec << ",";
808 auto sz = static_cast<int>(input->readULong(1));
809 if (pos+7+sz > endPos) {
810 f.str("");
811 f << "###";
812 ascFile.addPos(pos);
813 ascFile.addNote(f.str().c_str());
814
815 MWAW_DEBUG_MSG(("DocMkrText::readTOC: string size %d is bad\n", i));
816 return false;
817 }
818
819 std::string str("");
820 for (int c=0; c < sz; c++)
821 str += char(input->readULong(1));
822 f << str << ",";
823 m_state->m_toc.m_cIdList.push_back(zId);
824 m_state->m_toc.m_textList.push_back(str);
825 ascFile.addPos(pos);
826 ascFile.addNote(f.str().c_str());
827 }
828 return true;
829 }
830
831 // Windows information
832 ////////////////////////////////////////////////////////////
833 bool DocMkrText::readWindows(MWAWEntry const &entry)
834 {
835 if (!entry.valid() || entry.length()<20) {
836 MWAW_DEBUG_MSG(("DocMkrText::readWindows: the entry seems very short\n"));
837 return false;
838 }
839
840 entry.setParsed(true);
841 long pos = entry.begin();
842 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
843 libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii();
844 input->seek(pos, librevenge::RVNG_SEEK_SET);
845
846 DocMkrTextInternal::Zone &zone = m_state->getZone(entry.id());
847 libmwaw::DebugStream f;
848 f << "Entries(Windows)[" << entry.type() << "-" << entry.id() << "]:";
849 auto val=static_cast<int>(input->readLong(2)); // always 0?
850 if (val) f << "unkn=" << val << ",";
851 int wDim[3];
852 for (auto &w : wDim) w=static_cast<int>(input->readLong(2));
853 f << "windows=[left=" << wDim[0] << ",right=" << wDim[2]
854 << ",bottom=" << wDim[1] << "],";
855
856 for (auto &margin : zone.m_margins) margin=static_cast<int>(input->readLong(2));
857 f << "margins=[" << zone.m_margins[1] << "x" << zone.m_margins[0]
858 << "<->" << zone.m_margins[3] << "x" << zone.m_margins[2] << ",";
859 auto flag = static_cast<int>(input->readULong(1));
860 if (flag==1) {
861 zone.m_useFooter = false;
862 f << "noFooter,";
863 }
864 else if (flag) f << "#footer=" << flag << ",";
865 flag =static_cast<int>(input->readULong(1)); //9|3e|6d|a8|
866 if (flag)
867 f << "fl=" << std::hex << flag << std::dec << ",";
868 val = static_cast<int>(input->readLong(2)); // always 0?
869 switch (val) {
870 case 0:
871 break;
872 case 1:
873 zone.m_justify = MWAWParagraph::JustificationCenter;
874 f << "just=center,";
875 break;
876 case -1:
877 zone.m_justify = MWAWParagraph::JustificationRight;
878 f << "just=right,";
879 break;
880 default:
881 f << "#justify=" << val << ",";
882 break;
883 }
884
885 if (input->tell()!=entry.end())
886 ascFile.addDelimiter(input->tell(),'|');
887 ascFile.addPos(pos-4);
888 ascFile.addNote(f.str().c_str());
889 return true;
890 }
891
892 // Footer
893 ////////////////////////////////////////////////////////////
894 bool DocMkrText::sendFooter(int zId)
895 {
896 MWAWTextListenerPtr listener=m_parserState->m_textListener;
897 if (!listener) {
898 MWAW_DEBUG_MSG(("DocMkrText::sendFooter: can not find my listener\n"));
899 return false;
900 }
901 DocMkrTextInternal::Footer const &ft=m_state->m_footer;
902 if (ft.empty()) {
903 MWAW_DEBUG_MSG(("DocMkrText::sendFooter: oops, the footer is empty\n"));
904 return false;
905 }
906 if (m_state->m_idZoneMap.find(zId)==m_state->m_idZoneMap.end()) {
907 MWAW_DEBUG_MSG(("DocMkrText::sendFooter: oops, can not find the zone\n"));
908 return false;
909 }
910 listener->setFont(ft.m_font);
911
912 auto const &zone=m_state->getZone(zId);
913 double w = m_state->m_pageWidth-double(zone.m_margins[0]+zone.m_margins[2])/72.;
914 MWAWParagraph para;
915 MWAWTabStop tab;
916 tab.m_alignment = MWAWTabStop::CENTER;
917 tab.m_position = w/2.0;
918 para.m_tabs->push_back(tab);
919 tab.m_alignment = MWAWTabStop::RIGHT;
920 tab.m_position = w;
921 para.m_tabs->push_back(tab);
922 listener->setParagraph(para);
923
924 for (int st=0; st<2; st++) {
925 int id=3*st;
926 if (ft.m_items[id]<=0&&ft.m_items[id+1]<=0&&ft.m_items[id+2]<=0)
927 continue;
928 for (int f = 0; f < 3; f++, id++) {
929 switch (ft.m_items[id]) {
930 case 3: {
931 MWAWField field(MWAWField::Time);
932 field.m_DTFormat="%H:%M";
933 listener->insertField(field);
934 break;
935 }
936 case 4: {
937 MWAWField field(MWAWField::Date);
938 field.m_DTFormat="%a, %b %d, %Y";
939 listener->insertField(field);
940 break;
941 }
942 case 5:
943 listener->insertUnicodeString(librevenge::RVNGString("Page "));
944 listener->insertField(MWAWField(MWAWField::PageNumber));
945 break;
946 case 6:
947 listener->insertField(MWAWField(MWAWField::Title));
948 break;
949 case 7:
950 sendString(zone.m_name);
951 break;
952 case 8:
953 sendString(ft.m_userInfo);
954 break;
955 default:
956 break;
957 }
958 if (f!=2)
959 listener->insertTab();
960 }
961 if (st==0)
962 listener->insertEOL();
963 }
964 return true;
965 }
966
967 bool DocMkrText::readFooter(MWAWEntry const &entry)
968 {
969 if (!entry.valid() || entry.length()<22) {
970 MWAW_DEBUG_MSG(("DocMkrText::readFooter: the entry seems bad\n"));
971 return false;
972 }
973 entry.setParsed(true);
974 long pos = entry.begin();
975 MWAWInputStreamPtr input = m_mainParser->rsrcInput();
976 libmwaw::DebugFile &ascFile = m_mainParser->rsrcAscii();
977 input->seek(pos, librevenge::RVNG_SEEK_SET);
978
979 libmwaw::DebugStream f;
980 DocMkrTextInternal::Footer &footer=m_state->m_footer;
981 for (auto &item : footer.m_items) item=static_cast<int>(input->readLong(2));
982 for (int i = 0; i < 6; i++) {
983 auto val =static_cast<int>(input->readLong(1));
984 if (!val) continue;
985 if (val!=1) {
986 f << "#fl" << i << "=" << val << ",";
987 continue;
988 }
989 switch (i) {
990 case 0:
991 footer.m_chapterResetPage = true;
992 break;
993 case 2:
994 f << "hasSep,";
995 break;
996 case 4:
997 f << "graySep,";
998 break;
999 default:
1000 f << "#fl" << i << "=" << 1 << ",";
1001 break;
1002 }
1003 }
1004 footer.m_font.setId(static_cast<int>(input->readULong(2)));
1005 footer.m_font.setSize(float(input->readULong(2)));
1006
1007 footer.m_extra=f.str();
1008
1009 f.str("");
1010 f << "Entries(Footer)[" << entry.type() << "-" << entry.id() << "]:"<<footer;
1011 #ifdef DEBUG
1012 f << ",font=[" << footer.m_font.getDebugString(m_parserState->m_fontConverter) << "]";
1013 #endif
1014
1015 if (input->tell()!=entry.end())
1016 ascFile.addDelimiter(input->tell(),'|');
1017 ascFile.addPos(pos-4);
1018 ascFile.addNote(f.str().c_str());
1019 return true;
1020 }
1021
1022 ////////////////////////////////////////////////////////////
1023 //
1024 // Low level
1025 //
1026 ////////////////////////////////////////////////////////////
1027
1028 void DocMkrText::sendString(std::string const &str) const
1029 {
1030 MWAWTextListenerPtr listener=m_parserState->m_textListener;
1031 if (!listener) return;
1032
1033 for (char s : str)
1034 listener->insertCharacter(static_cast<unsigned char>(s));
1035 }
1036
1037 bool DocMkrText::sendMainText()
1038 {
1039 if (!m_parserState->m_textListener) return true;
1040
1041 for (auto const &it : m_state->m_idZoneMap) {
1042 DocMkrTextInternal::Zone const &zone= it.second;
1043 if (zone.m_parsed)
1044 continue;
1045 if (sendText(zone))
1046 m_mainParser->newPage(++m_state->m_actualPage);
1047 }
1048 return true;
1049 }
1050
1051
1052 void DocMkrText::flushExtra()
1053 {
1054 return;
1055 }
1056
1057 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: