Debian Codegraph @

src/lib/HanMacWrdJText.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 "MWAWGraphicListener.hxx"
47 #include "MWAWParagraph.hxx"
48 #include "MWAWPosition.hxx"
49 #include "MWAWRSRCParser.hxx"
50 #include "MWAWSection.hxx"
51 #include "MWAWSubDocument.hxx"
52
53 #include "HanMacWrdJParser.hxx"
54
55 #include "HanMacWrdJText.hxx"
56
57 /** Internal: the structures of a HanMacWrdJText */
58 namespace HanMacWrdJTextInternal
59 {
60 /** different PLC types */
61 enum PLCType { CHAR=0, RULER, LINE, TOKEN, Unknown};
62 /** Internal and low level: the PLC different types and their structures of a HanMacWrdJText */
63 struct PLC {
64 //! constructor
65 explicit PLC(PLCType w= Unknown, int id=0)
66 : m_type(w)
67 , m_id(id)
68 , m_extra("") {}
69 //! operator<<
70 friend std::ostream &operator<<(std::ostream &o, PLC const &plc)
71 {
72 switch (plc.m_type) {
73 case CHAR:
74 o << "F" << plc.m_id << ",";
75 break;
76 case LINE:
77 o << "L" << plc.m_id << ",";
78 break;
79 case RULER:
80 o << "R" << plc.m_id << ",";
81 break;
82 case TOKEN:
83 o << "T" << plc.m_id << ",";
84 break;
85 case Unknown:
86 #if !defined(__clang__)
87 default:
88 #endif
89 o << "#unknown" << plc.m_id << ",";
90 }
91 o << plc.m_extra;
92 return o;
93 }
94 //! PLC type
95 PLCType m_type;
96 //! the indentificator
97 int m_id;
98 //! extra data
99 std::string m_extra;
100 };
101
102 /** Internal: class to store a token of a HanMacWrdJText */
103 struct Token {
104 //! constructor
105 Token()
106 : m_type(0)
107 , m_id(0)
108 , m_localId(0)
109 , m_bookmark("")
110 , m_length(0)
111 , m_extra("")
112 {
113 }
114 //! operator<<
115 friend std::ostream &operator<<(std::ostream &o, Token const &tk)
116 {
117 switch (tk.m_type) { // checkme
118 case 0:
119 break;
120 case 1:
121 o << "field,";
122 break;
123 case 2:
124 o << "footnote,";
125 break;
126 case 8:
127 o << "toc,";
128 break;
129 case 0x20:
130 o << "bookmark,";
131 break;
132 default:
133 o << "#type=" << tk.m_type << ",";
134 break;
135 }
136 if (tk.m_bookmark.length())
137 o << "text[bookmark]=" << tk.m_bookmark << ",";
138 if (tk.m_id)
139 o << "zId=" << std::hex << tk.m_id << std::dec << ",";
140 if (tk.m_localId)
141 o << "id=" << tk.m_localId << ",";
142 if (tk.m_length)
143 o << "length=" << tk.m_length << ",";
144 o << tk.m_extra;
145 return o;
146 }
147 //! the token type
148 int m_type;
149 //! the id ( to be send)
150 long m_id;
151 //! the local id
152 int m_localId;
153 //! the bookmark string
154 std::string m_bookmark;
155 //! the token length in caller text
156 int m_length;
157 //! extra string string
158 std::string m_extra;
159 };
160
161 /** Internal: class to store a section of a HanMacWrdJText */
162 struct Section {
163 //! constructor
164 Section()
165 : m_numCols(1)
166 , m_colWidth()
167 , m_colSep()
168 , m_id(0)
169 , m_extra("")
170 {
171 }
172 //! returns a MWAWSection
173 MWAWSection getSection() const
174 {
175 MWAWSection sec;
176 if (m_colWidth.size()==0) {
177 MWAW_DEBUG_MSG(("HanMacWrdJTextInternal::Section:getSection can not find any width\n"));
178 return sec;
179 }
180 if (m_numCols <= 1)
181 return sec;
182 bool hasSep = m_colWidth.size()==m_colSep.size();
183 sec.m_columns.resize(size_t(m_numCols));
184 if (m_colWidth.size()==size_t(m_numCols)) {
185 for (size_t c=0; c < size_t(m_numCols); c++) {
186 sec.m_columns[c].m_width = double(m_colWidth[c]);
187 sec.m_columns[c].m_widthUnit = librevenge::RVNG_POINT;
188 if (!hasSep) continue;
189 sec.m_columns[c].m_margins[libmwaw::Left]=
190 sec.m_columns[c].m_margins[libmwaw::Right]=double(m_colSep[c])/2.0/72.;
191 }
192 }
193 else {
194 if (m_colWidth.size()>1) {
195 MWAW_DEBUG_MSG(("HanMacWrdJTextInternal::Section:getSection colWidth is not coherent with numCols\n"));
196 }
197 sec.setColumns(m_numCols, double(m_colWidth[0]), librevenge::RVNG_POINT,
198 hasSep ? double(m_colSep[0])/72. : 0);
199 }
200 return sec;
201 }
202 //! operator<<
203 friend std::ostream &operator<<(std::ostream &o, Section const &sec)
204 {
205 if (sec.m_numCols!=1)
206 o << "numCols=" << sec.m_numCols << ",";
207 if (sec.m_colWidth.size()) {
208 o << "colWidth=[";
209 for (size_t i = 0; i < sec.m_colWidth.size(); i++)
210 o << sec.m_colWidth[i] << ":" << sec.m_colSep[i] << ",";
211 o << "],";
212 }
213 if (sec.m_id)
214 o << "id=" << std::hex << sec.m_id << std::dec << ",";
215 o << sec.m_extra;
216 return o;
217 }
218 //! the number of column
219 int m_numCols;
220 //! the columns width
221 std::vector<double> m_colWidth;
222 //! the columns separator width
223 std::vector<double> m_colSep;
224 //! the id
225 long m_id;
226 //! extra string string
227 std::string m_extra;
228 };
229
230 //! Internal: a struct used to store a text zone
231 struct TextZone {
232 //! enum used to define the zone type
233 enum Type { T_Main=0, T_Header=1, T_Footer=2, T_Footnote=3, T_Textbox=4,
234 T_Table=9, T_Comment=10, T_Unknown
235 };
236 //! constructor
237 TextZone()
238 : m_type(T_Unknown)
239 , m_entry()
240 , m_id(0)
241 , m_PLCMap()
242 , m_tokenList()
243 , m_parsed(false)
244 {
245 }
246
247 //! the zone type
248 Type m_type;
249 //! the main entry
250 MWAWEntry m_entry;
251 //! the file zone id
252 long m_id;
253 //! the plc map
254 std::multimap<long, PLC> m_PLCMap;
255 //! the tokens list
256 std::vector<Token> m_tokenList;
257
258 //! true if the zone is sended
259 mutable bool m_parsed;
260 };
261
262
263 /** Internal: class to store the paragraph properties of a HanMacWrdJText */
264 struct Paragraph final : public MWAWParagraph {
265 //! Constructor
266 Paragraph()
267 : MWAWParagraph()
268 , m_type(0)
269 , m_addPageBreak(false)
270 {
271 }
272 Paragraph(Paragraph const &)=default;
273 Paragraph &operator=(Paragraph const &)=default;
274 Paragraph &operator=(Paragraph &&)=default;
275 //! destructor
276 ~Paragraph() final;
277 //! operator<<
278 friend std::ostream &operator<<(std::ostream &o, Paragraph const &ind)
279 {
280 switch (ind.m_type) {
281 case 0:
282 break;
283 case 1:
284 o << "header,";
285 break;
286 case 2:
287 o << "footer,";
288 break;
289 case 5:
290 o << "footnote,";
291 break;
292 default:
293 o << "#type=" << ind.m_type << ",";
294 break;
295 }
296 o << static_cast<MWAWParagraph const &>(ind) << ",";
297 if (ind.m_addPageBreak) o << "pageBreakBef,";
298 return o;
299 }
300 //! the type
301 int m_type;
302 //! flag to store a force page break
303 bool m_addPageBreak;
304 };
305
306 Paragraph::~Paragraph()
307 {
308 }
309
310 ////////////////////////////////////////
311 //! Internal: the state of a HanMacWrdJText
312 struct State {
313 //! constructor
314 State()
315 : m_version(-1)
316 , m_fontList()
317 , m_paragraphList()
318 , m_sectionList()
319 , m_ftnTextId(0)
320 , m_ftnFirstPosList()
321 , m_textZoneList()
322 , m_idTextZoneMap()
323 , m_numPages(-1)
324 , m_actualPage(0)
325 {
326 }
327 //! the file version
328 mutable int m_version;
329 /** the font list */
330 std::vector<MWAWFont> m_fontList;
331 //! the list of paragraph
332 std::vector<Paragraph> m_paragraphList;
333 //! the list of section
334 std::vector<Section> m_sectionList;
335 //! the footnote zone id;
336 long m_ftnTextId;
337 //! the footnote begin positions
338 std::vector<long> m_ftnFirstPosList;
339 //! the list of text zone
340 std::vector<TextZone> m_textZoneList;
341 //! a map textId -> id in m_textZoneList
342 std::map<long, int> m_idTextZoneMap;
343 int m_numPages /* the number of pages */, m_actualPage /* the actual page */;
344 };
345
346 ////////////////////////////////////////
347 //! Internal: the subdocument of a HanMacWrdJText
348 class SubDocument final : public MWAWSubDocument
349 {
350 protected:
351 //! the subdocument type
352 enum Type { S_TextZone, S_String };
353 public:
354 //! constructor to call a textzone
355 SubDocument(HanMacWrdJText &pars, MWAWInputStreamPtr const &input, long id, long cPos=0)
356 : MWAWSubDocument(pars.m_mainParser, input, MWAWEntry())
357 , m_type(S_TextZone)
358 , m_textParser(&pars)
359 , m_id(id)
360 , m_cPos(cPos)
361 , m_bookmark("") {}
362 //! constructor to send a string
363 SubDocument(HanMacWrdJText &pars, MWAWInputStreamPtr const &input, std::string const &text)
364 : MWAWSubDocument(pars.m_mainParser, input, MWAWEntry())
365 , m_type(S_String)
366 , m_textParser(&pars)
367 , m_id(0)
368 , m_cPos(0)
369 , m_bookmark(text) {}
370
371
372 //! destructor
373 ~SubDocument() final {}
374
375 //! operator!=
376 bool operator!=(MWAWSubDocument const &doc) const final;
377
378 //! the parser function
379 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
380
381 protected:
382 /** the subdocument type */
383 Type m_type;
384 /** the text parser */
385 HanMacWrdJText *m_textParser;
386 //! the subdocument id
387 long m_id;
388 //! the first charecter position
389 long m_cPos;
390 //! the bookmark string
391 std::string m_bookmark;
392 private:
393 SubDocument(SubDocument const &orig) = delete;
394 SubDocument &operator=(SubDocument const &orig) = delete;
395 };
396
397 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
398 {
399 if (!listener.get()) {
400 MWAW_DEBUG_MSG(("HanMacWrdJTextInternal::SubDocument::parse: no listener\n"));
401 return;
402 }
403
404 if (m_type==S_String) {
405 for (auto &c : m_bookmark)
406 listener->insertCharacter(static_cast<unsigned char>(c));
407 return;
408 }
409 if (!m_textParser) {
410 MWAW_DEBUG_MSG(("HanMacWrdJTextInternal::SubDocument::parse: no parser\n"));
411 return;
412 }
413 long pos = m_input->tell();
414 m_textParser->sendText(m_id, m_cPos);
415 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
416 }
417
418 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
419 {
420 if (MWAWSubDocument::operator!=(doc)) return true;
421 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
422 if (!sDoc) return true;
423 if (m_textParser != sDoc->m_textParser) return true;
424 if (m_type != sDoc->m_type) return true;
425 if (m_id != sDoc->m_id) return true;
426 if (m_cPos != sDoc->m_cPos) return true;
427 if (m_bookmark != sDoc->m_bookmark) return true;
428 return false;
429 }
430 }
431
432 ////////////////////////////////////////////////////////////
433 // constructor/destructor, ...
434 ////////////////////////////////////////////////////////////
435 HanMacWrdJText::HanMacWrdJText(HanMacWrdJParser &parser)
436 : m_parserState(parser.getParserState())
437 , m_state(new HanMacWrdJTextInternal::State)
438 , m_mainParser(&parser)
439 {
440 }
441
442 HanMacWrdJText::~HanMacWrdJText()
443 {
444 }
445
446 int HanMacWrdJText::version() const
447 {
448 if (m_state->m_version < 0)
449 m_state->m_version = m_parserState->m_version;
450 return m_state->m_version;
451 }
452
453 int HanMacWrdJText::numPages() const
454 {
455 int nPages = 1;
456 for (auto const &zone : m_state->m_textZoneList) {
457 if (zone.m_type != HanMacWrdJTextInternal::TextZone::T_Main)
458 continue;
459 nPages = const_cast<HanMacWrdJText *>(this)->computeNumPages(zone);
460 break;
461 }
462 // fixme: compute the number of page
463 m_state->m_numPages = nPages;
464 return nPages;
465 }
466
467 std::vector<long> HanMacWrdJText::getTokenIdList() const
468 {
469 std::vector<long> res;
470 for (auto const &zone : m_state->m_textZoneList) {
471 for (auto const &token : zone.m_tokenList) {
472 if (token.m_type==1)
473 res.push_back(token.m_id);
474 }
475 }
476 return res;
477 }
478
479 ////////////////////////////////////////////////////////////
480 // Intermediate level
481 ////////////////////////////////////////////////////////////
482
483 ////////////////////////////////////////////////////////////
484 // Text
485 ////////////////////////////////////////////////////////////
486 bool HanMacWrdJText::canSendTextAsGraphic(long id, long cPos)
487 {
488 if (m_state->m_idTextZoneMap.find(id)==m_state->m_idTextZoneMap.end()) {
489 MWAW_DEBUG_MSG(("HanMacWrdJText::canSendTextAsGraphic: can not find text zone with id %lx\n", static_cast<long unsigned int>(id)));
490 return false;
491 }
492 int zId = m_state->m_idTextZoneMap.find(id)->second;
493 if (zId < 0 || zId >= static_cast<int>(m_state->m_textZoneList.size()))
494 return false;
495 return canSendTextAsGraphic(m_state->m_textZoneList[size_t(zId)], cPos);
496 }
497
498 bool HanMacWrdJText::canSendTextAsGraphic(HanMacWrdJTextInternal::TextZone const &zone, long cPos)
499 {
500 if (!zone.m_entry.valid())
501 return false;
502
503 auto plcIt=zone.m_PLCMap.find(cPos);
504 while (plcIt != zone.m_PLCMap.end() && plcIt->first < cPos)
505 ++plcIt;
506 while (plcIt != zone.m_PLCMap.end()) {
507 auto const &plc = plcIt++->second;
508 if (plc.m_type!=HanMacWrdJTextInternal::TOKEN) continue;
509 if (plc.m_id < 0 || plc.m_id >= static_cast<int>(zone.m_tokenList.size()))
510 continue;
511 auto const &tkn=zone.m_tokenList[size_t(plc.m_id)];
512 switch (tkn.m_type) {
513 case 1:
514 case 2:
515 case 0x20:
516 return false;
517 default:
518 break;
519 }
520 }
521 return true;
522 }
523
524 bool HanMacWrdJText::sendText(long id, long cPos, MWAWListenerPtr listener)
525 {
526 if (m_state->m_idTextZoneMap.find(id)==m_state->m_idTextZoneMap.end()) {
527 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not find text zone with id %lx\n", static_cast<long unsigned int>(id)));
528 return false;
529 }
530 int zId = m_state->m_idTextZoneMap.find(id)->second;
531 if (zId < 0 || zId >= static_cast<int>(m_state->m_textZoneList.size()))
532 return false;
533 return sendText(m_state->m_textZoneList[size_t(zId)], cPos, listener);
534 }
535
536 bool HanMacWrdJText::sendMainText()
537 {
538 for (auto &zone : m_state->m_textZoneList) {
539 if (zone.m_type != HanMacWrdJTextInternal::TextZone::T_Main)
540 continue;
541 sendText(zone,0);
542 return true;
543 }
544 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not find the main zone\n"));
545 return false;
546 }
547
548
549 bool HanMacWrdJText::sendText(HanMacWrdJTextInternal::TextZone const &zone, long fPos, MWAWListenerPtr listener)
550 {
551 if (!zone.m_entry.valid()) {
552 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: call without entry\n"));
553 return false;
554 }
555 bool withLocalListener=false;
556 if (listener)
557 withLocalListener=true;
558 else
559 listener=m_parserState->m_textListener;
560 if (!listener) {
561 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not find the listener\n"));
562 return false;
563 }
564
565 zone.m_parsed=true;
566 librevenge::RVNGBinaryData data;
567 if (!m_mainParser->decodeZone(zone.m_entry, data)) {
568 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not decode a zone\n"));
569 m_parserState->m_asciiFile.addPos(zone.m_entry.begin());
570 m_parserState->m_asciiFile.addNote("###");
571 return false;
572 }
573 if (!data.size())
574 return true;
575 if (fPos < 0 || 2*fPos > long(data.size())) {
576 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: first pos %ld is too big zone\n", fPos));
577 return false;
578 }
579
580 MWAWInputStreamPtr input=MWAWInputStream::get(data, false);
581 if (!input) {
582 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not find my input\n"));
583 return false;
584 }
585 libmwaw::DebugFile asciiFile(input);
586
587 #ifdef DEBUG_WITH_FILES
588 if (fPos==0) {
589 static int tId=0;
590 std::stringstream s;
591 s << "Text" << tId++;
592 asciiFile.open(s.str());
593 }
594 #endif
595
596 bool isMain = zone.m_type==HanMacWrdJTextInternal::TextZone::T_Main;
597 bool charOneIsEnd = zone.m_type==HanMacWrdJTextInternal::TextZone::T_Footnote ||
598 zone.m_type==HanMacWrdJTextInternal::TextZone::T_Table;
599
600 long cPos=fPos, endCPos=-1;
601 int actPage = 1, actCol = 0, numCol=1, actSection = 1;
602 if (isMain)
603 m_mainParser->newPage(1);
604 if (isMain && !m_state->m_sectionList.size()) {
605 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not find section 0\n"));
606 }
607 else if (isMain) {
608 auto sec = m_state->m_sectionList[0];
609 if (sec.m_numCols >= 1 && sec.m_colWidth.size() > 0) {
610 if (listener->isSectionOpened())
611 listener->closeSection();
612 listener->openSection(sec.getSection());
613 numCol = listener->getSection().numColumns();
614 }
615 }
616 long pos = 2*fPos;
617 input->seek(pos, librevenge::RVNG_SEEK_SET);
618 libmwaw::DebugStream f;
619 f << "Entries(TextData):";
620
621 while (true) {
622 if (cPos!=fPos) {
623 asciiFile.addPos(pos);
624 asciiFile.addNote(f.str().c_str());
625 f.str("");
626 f << "TextData:";
627 pos = input->tell();
628 }
629 auto plcIt=zone.m_PLCMap.find(cPos);
630 int expectedChar=0;
631 if (plcIt != zone.m_PLCMap.end()) {
632 while (plcIt != zone.m_PLCMap.end() && plcIt->first == cPos) {
633 auto const &plc = plcIt++->second;
634 f << "[" << plc << "]";
635 switch (plc.m_type) {
636 case HanMacWrdJTextInternal::CHAR: {
637 if (plc.m_id < 0 || plc.m_id >= static_cast<int>(m_state->m_fontList.size())) {
638 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not find font\n"));
639 f << "[###font]";
640 break;
641 }
642 listener->setFont(m_state->m_fontList[size_t(plc.m_id)]);
643 break;
644 }
645 case HanMacWrdJTextInternal::RULER: {
646 if (plc.m_id < 0 || plc.m_id >= static_cast<int>(m_state->m_paragraphList.size())) {
647 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not find paragraph\n"));
648 f << "[###paragraph]";
649 break;
650 }
651 auto const &para=m_state->m_paragraphList[size_t(plc.m_id)];
652 if (isMain && para.m_addPageBreak)
653 m_mainParser->newPage(++actPage);
654 listener->setParagraph(para);
655 break;
656 }
657 case HanMacWrdJTextInternal::TOKEN: {
658 if (plc.m_id < 0 || plc.m_id >= static_cast<int>(zone.m_tokenList.size())) {
659 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not find token\n"));
660 f << "[###token]";
661 break;
662 }
663 auto const &tkn=zone.m_tokenList[size_t(plc.m_id)];
664 switch (tkn.m_type) {
665 case 1:
666 expectedChar=0x1;
667 if (withLocalListener) {
668 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: unexpected token type=1 in graphic\n"));
669 break;
670 }
671 m_mainParser->sendZone(tkn.m_id);
672 break;
673 case 2: {
674 expectedChar=0x11;
675 if (withLocalListener) {
676 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not insert footnote in graphic\n"));
677 break;
678 }
679 if (tkn.m_localId < 0 || tkn.m_localId >= int(m_state->m_ftnFirstPosList.size())) {
680 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not find footnote\n"));
681 f << "[###ftnote]";
682 break;
683 }
684 MWAWSubDocumentPtr subdoc
685 (new HanMacWrdJTextInternal::SubDocument
686 (*this, input, m_state->m_ftnTextId,
687 m_state->m_ftnFirstPosList[size_t(tkn.m_localId)]));
688 m_parserState->m_textListener->insertNote(MWAWNote(MWAWNote::FootNote),subdoc);
689 break;
690 }
691 case 8: // TOC, ok to ignore
692 break;
693 case 0x20:
694 if (withLocalListener) {
695 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not insert bookmark in graphic\n"));
696 break;
697 }
698 else {
699 MWAWSubDocumentPtr subdoc(new HanMacWrdJTextInternal::SubDocument(*this, input, tkn.m_bookmark));
700 m_parserState->m_textListener->insertComment(subdoc);
701 break;
702 }
703 default:
704 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not send token with type %d\n", tkn.m_type));
705 break;
706 }
707
708 break;
709 }
710 case HanMacWrdJTextInternal::LINE:
711 case HanMacWrdJTextInternal::Unknown:
712 #if !defined(__clang__)
713 default:
714 #endif
715 break;
716 }
717 }
718 endCPos = (plcIt != zone.m_PLCMap.end()) ? plcIt->first : -1;
719 }
720 if (input->isEnd())
721 break;
722 if (expectedChar) {
723 if (static_cast<int>(input->readULong(1))==expectedChar) {
724 cPos++;
725 input->seek(1, librevenge::RVNG_SEEK_CUR);
726 }
727 else {
728 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not find expected char token\n"));
729 input->seek(-1, librevenge::RVNG_SEEK_CUR);
730 f << "###";
731 }
732 }
733 while (endCPos<0 || cPos < endCPos) {
734 cPos++;
735 if (input->isEnd())
736 break;
737 auto c = static_cast<int>(input->readULong(2));
738 if (c==0) {
739 if (input->isEnd())
740 break;
741 f << "#[0]";
742 continue;
743 }
744 if (c==1 && charOneIsEnd)
745 return true;
746 switch (c) {
747 case 0x1000:
748 f << "[pgNum]";
749 listener->insertField(MWAWField(MWAWField::PageNumber));
750 break;
751 case 0x1001:
752 f << "[pgCount]";
753 listener->insertField(MWAWField(MWAWField::PageCount));
754 break;
755 case 0x1002: {
756 f << "[date]";
757 MWAWField field(MWAWField::Date);
758 field.m_DTFormat="%A, %b %d, %Y";
759 listener->insertField(field);
760 break;
761 }
762 case 0x1003: {
763 f << "[time]";
764 MWAWField field(MWAWField::Time);
765 field.m_DTFormat="%I:%M %p";
766 listener->insertField(field);
767 break;
768 }
769 case 0x1004:
770 f << "[title]";
771 listener->insertField(MWAWField(MWAWField::Title));
772 break;
773 case 0x1005: {
774 std::stringstream s;
775 f << "[section]";
776 s << actSection;
777 listener->insertUnicodeString(librevenge::RVNGString(s.str().c_str()));
778 break;
779 }
780 case 2:
781 f << "[colBreak]";
782 if (!isMain) {
783 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: find column break in auxilliary block\n"));
784 break;
785 }
786 if (actCol < numCol-1 && numCol > 1) {
787 listener->insertBreak(MWAWTextListener::ColumnBreak);
788 actCol++;
789 }
790 else {
791 actCol = 0;
792 m_mainParser->newPage(++actPage);
793 }
794 break;
795 case 3:
796 f << "[pageBreak]";
797 if (isMain) {
798 m_mainParser->newPage(++actPage);
799 actCol = 0;
800 }
801 break;
802 case 4:
803 f << "[sectionBreak]";
804 if (!isMain) {
805 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: find section in auxilliary block\n"));
806 break;
807 }
808 if (size_t(actSection) >= m_state->m_sectionList.size()) {
809 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: can not find section %d\n", actSection));
810 break;
811 }
812 else {
813 auto const &sec = m_state->m_sectionList[size_t(actSection++)];
814 actCol = 0;
815 if (listener->isSectionOpened())
816 listener->closeSection();
817 m_mainParser->newPage(++actPage);
818 listener->openSection(sec.getSection());
819 numCol = listener->getSection().numColumns();
820 }
821 break;
822 case 9:
823 f << char(c);
824 listener->insertTab();
825 break;
826 case 0xd:
827 f << char(c);
828 listener->insertEOL();
829 break;
830 default: {
831 if (c >= 0x1100 && c <= 0x11ff) // ok a footnote
832 break;
833 if (c <= 0x1f || c >= 0x100) {
834 f << "#[" << std::hex << c << std::dec << "]";
835 MWAW_DEBUG_MSG(("HanMacWrdJText::sendText: find a odd char %x\n", static_cast<unsigned int>(c)));
836 break;
837 }
838 f << char(c);
839 listener->insertCharacter(static_cast<unsigned char>(c), input);
840 break;
841 }
842 }
843 }
844 }
845 return true;
846 }
847
848 int HanMacWrdJText::computeNumPages(HanMacWrdJTextInternal::TextZone const &zone)
849 {
850 if (zone.m_type!=HanMacWrdJTextInternal::TextZone::T_Main)
851 return 1;
852 if (!zone.m_entry.valid())
853 return 0;
854 librevenge::RVNGBinaryData data;
855 if (!m_mainParser->decodeZone(zone.m_entry, data) || !data.size())
856 return 0;
857
858 MWAWInputStreamPtr input=MWAWInputStream::get(data, false);
859 if (!input)
860 return 0;
861 int nPages = 1, actCol = 0, numCol=1, actSection = 1;
862
863 if (m_state->m_sectionList.size()) {
864 auto const &sec = m_state->m_sectionList[0];
865 if (sec.m_numCols >= 1)
866 numCol = sec.m_numCols;
867 }
868 input->seek(0, librevenge::RVNG_SEEK_SET);
869 while (!input->isEnd()) {
870 auto c = static_cast<int>(input->readULong(2));
871 switch (c) {
872 case 2:
873 if (actCol < numCol-1 && numCol > 1)
874 actCol++;
875 else {
876 actCol = 0;
877 nPages++;
878 }
879 break;
880 case 3:
881 actCol = 0;
882 nPages++;
883 break;
884 case 4: {
885 if (size_t(actSection) >= m_state->m_sectionList.size())
886 break;
887 actCol = 0;
888 nPages++;
889 auto const &sec = m_state->m_sectionList[size_t(actSection++)];
890 numCol = sec.m_numCols >= 1 ? sec.m_numCols : 1;
891 break;
892 }
893 default:
894 break;
895 }
896 }
897 return nPages;
898 }
899
900 void HanMacWrdJText::updateTextZoneTypes(std::map<long,int> const &idTypeMap)
901 {
902 auto numZones = static_cast<int>(m_state->m_textZoneList.size());
903 for (auto const &it : idTypeMap) {
904 if (m_state->m_idTextZoneMap.find(it.first)==m_state->m_idTextZoneMap.end()) {
905 MWAW_DEBUG_MSG(("HanMacWrdJText::updateTextZoneTypes: can not find text zone with id %lx\n", static_cast<long unsigned int>(it.first)));
906 continue;
907 }
908 int zId = m_state->m_idTextZoneMap.find(it.first)->second;
909 if (zId < 0 || zId >= numZones)
910 continue;
911 m_state->m_textZoneList[size_t(zId)].m_type=
912 HanMacWrdJTextInternal::TextZone::Type(it.second);
913 }
914 }
915
916 void HanMacWrdJText::updateFootnoteInformations(long const &textZId, std::vector<long> const &fPosList)
917 {
918 m_state->m_ftnTextId = textZId;
919 m_state->m_ftnFirstPosList = fPosList;
920 }
921
922 bool HanMacWrdJText::readTextZonesList(MWAWEntry const &entry)
923 {
924 if (!entry.valid()) {
925 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZonesList: called without any entry\n"));
926 return false;
927 }
928 if (entry.length() == 8) {
929 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZonesList: find an empty zone\n"));
930 entry.setParsed(true);
931 return true;
932 }
933 if (entry.length() < 12) {
934 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZonesList: the entry seems too short\n"));
935 return false;
936 }
937 if (m_state->m_textZoneList.size()) {
938 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZonesList: oops the text zone list os ,not empty\n"));
939 }
940 long pos = entry.begin()+8; // skip header
941 long endPos = entry.end();
942
943 MWAWInputStreamPtr input = m_parserState->m_input;
944 libmwaw::DebugFile &asciiFile = m_parserState->m_asciiFile;
945 libmwaw::DebugStream f;
946 entry.setParsed(true);
947 input->seek(pos, librevenge::RVNG_SEEK_SET);
948 // first read the header
949 f << entry.name() << "[header]:";
950 HanMacWrdJZoneHeader mainHeader(true);
951 if (!m_mainParser->readClassicHeader(mainHeader,endPos) || mainHeader.m_fieldSize!=4) {
952 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZonesList: can not read an entry\n"));
953 f << "###sz=" << mainHeader.m_length;
954 asciiFile.addPos(pos);
955 asciiFile.addNote(f.str().c_str());
956 return false;
957 }
958 long headerEnd=pos+4+mainHeader.m_length;
959 f << mainHeader;
960 long val;
961 f << "listId=[" << std::hex;
962 std::vector<long> listIds;
963 for (int i = 0; i < mainHeader.m_n; i++) {
964 val = long(input->readULong(4));
965 m_state->m_idTextZoneMap[val]=i;
966 listIds.push_back(val);
967 f << val << ",";
968 }
969 f << std::dec << "],";
970 asciiFile.addPos(pos);
971 asciiFile.addNote(f.str().c_str());
972 if (input->tell()!=headerEnd) {
973 asciiFile.addDelimiter(input->tell(),'|');
974 input->seek(headerEnd, librevenge::RVNG_SEEK_SET);
975 }
976 m_state->m_textZoneList.resize(size_t(mainHeader.m_n));
977 if (mainHeader.m_n)
978 m_state->m_textZoneList[0].m_type=HanMacWrdJTextInternal::TextZone::T_Main;
979 for (int i = 0; i < mainHeader.m_n; i++) {
980 auto &zone=m_state->m_textZoneList[size_t(i)];
981 zone.m_id = listIds[size_t(i)];
982 // first a field of size 0x26
983 pos = input->tell();
984 f.str("");
985 f << entry.name() << "-A" << i << ":";
986 f << "id=" << std::hex << listIds[size_t(i)] << std::dec << ",";
987
988 long dataSz = (pos+4>endPos) ? 0: long(input->readULong(4));
989 long zoneEnd=pos+4+dataSz;
990 if (zoneEnd > endPos) {
991 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZonesList: can not read first zone size for id=%d\n",i));
992 f << "###sz=" << dataSz;
993 asciiFile.addPos(pos);
994 asciiFile.addNote(f.str().c_str());
995 return false;
996 }
997 if (dataSz < 38) {
998 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZonesList: first zone size for id=%d seems very short\n",i));
999 f << "###";
1000 }
1001 else {
1002 int sel[3];
1003 for (auto &s : sel) s = static_cast<int>(input->readLong(4));
1004 if (sel[0] || sel[1] || sel[2]) {
1005 f << "select=" << sel[1] << "x" << sel[0];
1006 if (sel[1] != sel[2])
1007 f << "[" << sel[2] << "]";
1008 f << ",";
1009 }
1010 f << "listIds=[" << std::hex; // 5 or 6 id
1011 for (int j = 0; j < 6; j++) {
1012 val = long(input->readULong(4));
1013 if (val)
1014 f << val << ",";
1015 else
1016 f << "_,";
1017 }
1018 f << std::dec << "],";
1019 auto N1 = static_cast<int>(input->readULong(2)); // correspond to next N1
1020 f << "N=" << N1 << ",";
1021 }
1022 asciiFile.addPos(pos);
1023 asciiFile.addNote(f.str().c_str());
1024
1025 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
1026 f.str("");
1027 f << entry.name() << "-B" << i << ":";
1028 f << "id=" << std::hex << listIds[size_t(i)] << std::dec << ",";
1029
1030 pos = input->tell();
1031 HanMacWrdJZoneHeader header(false);
1032 if (!m_mainParser->readClassicHeader(header,endPos) || header.m_fieldSize!=4) {
1033 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZonesList: can not read second zone %d\n", i));
1034 f << "###" << header;
1035 asciiFile.addPos(pos);
1036 asciiFile.addNote(f.str().c_str());
1037 if (header.m_length<16 || pos+4+header.m_length>endPos)
1038 return false;
1039 input->seek(pos+4+header.m_length, librevenge::RVNG_SEEK_SET);
1040 continue;
1041 }
1042 zoneEnd=pos+4+header.m_length;
1043 f << header;
1044 /* h0,h1,h2 seems to correspond to freeBlock, 0, 8[totalblock=8*..], h3=1*/
1045 f << "listId?=[" << std::hex;
1046 for (int j = 0; j < header.m_n; j++) {
1047 val = long(input->readULong(4));
1048 f << val << ",";
1049 }
1050 f << std::dec << "],";
1051 asciiFile.addPos(pos);
1052 asciiFile.addNote(f.str().c_str());
1053 if (input->tell()!=zoneEnd)
1054 asciiFile.addDelimiter(input->tell(),'|');
1055 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
1056 }
1057
1058 pos = input->tell();
1059 if (pos!=endPos) {
1060 f.str("");
1061 f << entry.name() << "[last]:###";
1062 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZonesList: find unexpected end of data\n"));
1063 asciiFile.addPos(pos);
1064 asciiFile.addNote(f.str().c_str());
1065 }
1066
1067 return true;
1068 }
1069
1070 bool HanMacWrdJText::readTextZone(MWAWEntry const &entry, int actZone)
1071 {
1072 if (!entry.valid()) {
1073 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: called without any entry\n"));
1074 return false;
1075 }
1076 if (entry.length() < 8+20*3) {
1077 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: the entry seems too short\n"));
1078 return false;
1079 }
1080
1081 MWAWInputStreamPtr input = m_parserState->m_input;
1082 libmwaw::DebugFile &asciiFile = m_parserState->m_asciiFile;
1083 libmwaw::DebugStream f;
1084 entry.setParsed(true);
1085
1086 long pos = entry.begin()+8; // skip header
1087 long endPos = entry.end();
1088 input->seek(pos, librevenge::RVNG_SEEK_SET);
1089
1090 if (actZone >= static_cast<int>(m_state->m_textZoneList.size()) || actZone < 0) {
1091 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: find an unexpected zone\n"));
1092 if (actZone < 0)
1093 actZone = static_cast<int>(m_state->m_textZoneList.size());
1094 m_state->m_textZoneList.resize(size_t(actZone)+1);
1095 }
1096 auto &zone = m_state->m_textZoneList[size_t(actZone)];
1097 long val;
1098
1099 // first read the char properties list
1100 std::vector<HanMacWrdJTextInternal::PLC> cPLCList;
1101 std::vector<MWAWVec2i> cPLCPosList; // 0:line, 1:char
1102
1103 f.str("");
1104 f << entry.name() << "-char:";
1105
1106 pos = input->tell();
1107 HanMacWrdJZoneHeader header(false);
1108 bool ok = true;
1109 if (!m_mainParser->readClassicHeader(header,endPos) || header.m_fieldSize!=8) {
1110 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: can not read zone the char plc list\n"));
1111 f << "###";
1112 asciiFile.addPos(pos);
1113 asciiFile.addNote(f.str().c_str());
1114 input->seek(pos, librevenge::RVNG_SEEK_SET);
1115 ok = false;
1116 }
1117 else {
1118 f << header;
1119 long zoneEnd=pos+4+header.m_length;
1120 asciiFile.addPos(pos);
1121 asciiFile.addNote(f.str().c_str());
1122
1123 for (int j = 0; j < header.m_n; j++) {
1124 pos = input->tell();
1125 f.str("");
1126 MWAWVec2i cPos;
1127 cPos[0] = static_cast<int>(input->readLong(2));
1128 cPos[1] = static_cast<int>(input->readLong(2));
1129 HanMacWrdJTextInternal::PLC plc;
1130 plc.m_type = HanMacWrdJTextInternal::CHAR;
1131 plc.m_id = static_cast<int>(input->readLong(2));
1132 val = long(input->readULong(2)); // 0|1|4|5|f|26|2d|42|4e|4f|..|6a|6e|7a|7d
1133 if (val) f << "#f0=" << std::hex << val << std::dec << ",";
1134 plc.m_extra = f.str();
1135 cPLCPosList.push_back(cPos);
1136 cPLCList.push_back(plc);
1137
1138 f.str("");
1139 f << entry.name() << "-Char" << j << "]:lcPos=" << cPos << "," << plc;
1140 asciiFile.addPos(pos);
1141 asciiFile.addNote(f.str().c_str());
1142 input->seek(pos+header.m_fieldSize, librevenge::RVNG_SEEK_SET);
1143 }
1144
1145 if (input->tell() != zoneEnd) { // junk ?
1146 asciiFile.addDelimiter(input->tell(),'|');
1147 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
1148 }
1149 }
1150
1151 // second read the ruler list
1152 std::map<int,HanMacWrdJTextInternal::PLC> rLineRulerMap;
1153
1154 f.str("");
1155 f << entry.name() << "-ruler:";
1156
1157 pos = input->tell();
1158 header=HanMacWrdJZoneHeader(false);
1159 if (!ok) {
1160 }
1161 else if (!m_mainParser->readClassicHeader(header,endPos) || header.m_fieldSize!=8) {
1162 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: can not read zone the ruler plc list\n"));
1163 f << "###";
1164 asciiFile.addPos(pos);
1165 asciiFile.addNote(f.str().c_str());
1166 input->seek(pos, librevenge::RVNG_SEEK_SET);
1167 ok = false;
1168 }
1169 else {
1170 f << header;
1171 long zoneEnd=pos+4+header.m_length;
1172 asciiFile.addPos(pos);
1173 asciiFile.addNote(f.str().c_str());
1174
1175 for (int j = 0; j < header.m_n; j++) {
1176 pos = input->tell();
1177 f.str("");
1178 auto line = static_cast<int>(input->readLong(2));
1179 HanMacWrdJTextInternal::PLC plc;
1180 plc.m_type = HanMacWrdJTextInternal::RULER;
1181 plc.m_id = static_cast<int>(input->readLong(2));
1182
1183 val = long(input->readULong(4)); // find 0,000008a0, 023ddd20, 0046ddbb, 5808001d, 5808004e
1184 if (val) f << "#f0=" << std::hex << val << std::dec << ",";
1185 plc.m_extra = f.str();
1186 if (rLineRulerMap.find(line) != rLineRulerMap.end()) {
1187 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: already find a ruler for line=%d\n",line));
1188 f << "###";
1189 }
1190 else
1191 rLineRulerMap[line]=plc;
1192
1193 f.str("");
1194 f << entry.name() << "-Ruler" << j << "]:line=" << line << "," << plc;
1195 asciiFile.addPos(pos);
1196 asciiFile.addNote(f.str().c_str());
1197 input->seek(pos+header.m_fieldSize, librevenge::RVNG_SEEK_SET);
1198 }
1199
1200 if (input->tell() != zoneEnd) { // junk ?
1201 asciiFile.addDelimiter(input->tell(),'|');
1202 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
1203 }
1204 }
1205
1206 // now we can read the line position
1207 std::vector<long> linePosList;
1208
1209 f.str("");
1210 f << entry.name() << "-line:";
1211 pos = input->tell();
1212 header=HanMacWrdJZoneHeader(false);
1213 if (!ok) {
1214 }
1215 else if (!m_mainParser->readClassicHeader(header,endPos) || header.m_fieldSize!=4) {
1216 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: can not read zone the line plc list\n"));
1217 f << "###";
1218 asciiFile.addPos(pos);
1219 asciiFile.addNote(f.str().c_str());
1220 input->seek(pos, librevenge::RVNG_SEEK_SET);
1221 ok = false;
1222 }
1223 else {
1224 f << header;
1225 long zoneEnd=pos+4+header.m_length;
1226 asciiFile.addPos(pos);
1227 asciiFile.addNote(f.str().c_str());
1228 HanMacWrdJTextInternal::PLC plc;
1229 plc.m_type = HanMacWrdJTextInternal::LINE;
1230
1231 f << "linePos=[" << std::hex;
1232 for (int j = 0; j < header.m_n; j++) {
1233 long linePos = input->readLong(4);
1234 linePosList.push_back(linePos);
1235 plc.m_id = j;
1236 zone.m_PLCMap.insert
1237 (std::multimap<long, HanMacWrdJTextInternal::PLC>::value_type(linePos, plc));
1238 f << linePos << ",";
1239 }
1240 f << std::dec << "],";
1241 asciiFile.addPos(pos);
1242 asciiFile.addNote(f.str().c_str());
1243
1244 if (input->tell() != zoneEnd) { // junk ?
1245 asciiFile.addDelimiter(input->tell(),'|');
1246 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
1247 }
1248 }
1249
1250 // ok, we can now update the plc
1251 if (ok) {
1252 auto nLines = int(linePosList.size());
1253 for (auto const &rIt : rLineRulerMap) {
1254 int line = rIt.first;
1255 if (line < 0 || line >= nLines) {
1256 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: build rule plc, can not find line %d\n", line));
1257 continue;
1258
1259 }
1260 zone.m_PLCMap.insert
1261 (std::multimap<long, HanMacWrdJTextInternal::PLC>::value_type(linePosList[size_t(line)], rIt.second));
1262 }
1263 size_t numCProp = cPLCPosList.size();
1264 if (numCProp != cPLCList.size()) {
1265 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: cPLCPosList and cPLCList have not the same size\n"));
1266 if (numCProp > cPLCList.size())
1267 numCProp = cPLCList.size();
1268 }
1269 for (size_t i = 0; i < numCProp; i++) {
1270 int line = cPLCPosList[i][0];
1271 if (line < 0 || line >= nLines) {
1272 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: build char plc, can not find line %d\n", line));
1273 continue;
1274 }
1275 long cPos = linePosList[size_t(line)]+long(cPLCPosList[i][1]);
1276 zone.m_PLCMap.insert
1277 (std::multimap<long, HanMacWrdJTextInternal::PLC>::value_type(cPos, cPLCList[i]));
1278 }
1279 }
1280 asciiFile.addPos(endPos);
1281 asciiFile.addNote("_");
1282
1283 // now potentially a token zone, called with endPos-1 to avoid reading the last text zone
1284 readTextToken(endPos-1, zone);
1285
1286 pos = input->tell();
1287 if (pos==endPos) {
1288 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: can not read find the last zone\n"));
1289
1290 return true;
1291 }
1292
1293 // normally now the text data but check if there remains some intermediar unparsed zone
1294 auto dataSz = long(input->readULong(4));
1295 while (dataSz>0 && pos+4+dataSz < endPos) {
1296 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: find some unparsed zone\n"));
1297 f.str("");
1298 f << entry.name() << "-###:";
1299 asciiFile.addPos(pos);
1300 asciiFile.addNote(f.str().c_str());
1301
1302 pos = pos+4+dataSz;
1303 input->seek(pos, librevenge::RVNG_SEEK_SET);
1304 dataSz = long(input->readULong(4));
1305 }
1306 input->seek(pos, librevenge::RVNG_SEEK_SET);
1307
1308 // the text data
1309 f.str("");
1310 f << entry.name() << "-text:";
1311 dataSz = long(input->readULong(4));
1312 if (pos+4+dataSz>endPos) {
1313 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextZone: can not read last zone size\n"));
1314 f << "###sz=" << dataSz;
1315 asciiFile.addPos(pos);
1316 asciiFile.addNote(f.str().c_str());
1317 return false;
1318 }
1319
1320 asciiFile.addPos(pos);
1321 asciiFile.addNote(f.str().c_str());
1322
1323 zone.m_entry.setBegin(pos);
1324 zone.m_entry.setEnd(endPos);
1325 zone.m_entry.setName(entry.name());
1326
1327 return true;
1328 }
1329
1330 bool HanMacWrdJText::readTextToken(long endPos, HanMacWrdJTextInternal::TextZone &zone)
1331 {
1332 MWAWInputStreamPtr input = m_parserState->m_input;
1333 libmwaw::DebugFile &asciiFile = m_parserState->m_asciiFile;
1334 libmwaw::DebugStream f;
1335 long pos=input->tell();
1336 if (pos+4>=endPos)
1337 return true;
1338
1339 f << "Entries(TextToken):";
1340 HanMacWrdJZoneHeader header(false);
1341 if (!m_mainParser->readClassicHeader(header,endPos) || header.m_fieldSize!=16 ||
1342 16+16*header.m_n+4 > header.m_length) {
1343 input->seek(pos, librevenge::RVNG_SEEK_SET);
1344 return true;
1345 }
1346 int val;
1347 for (int i = 0; i < 2; i++) { // always 0?
1348 val = static_cast<int>(input->readLong(2));
1349 if (val) f << "f" << i << "=" << val << ",";
1350 }
1351 f << header;
1352 long zoneEnd=pos+4+header.m_length;
1353 asciiFile.addPos(pos);
1354 asciiFile.addNote(f.str().c_str());
1355
1356 HanMacWrdJTextInternal::PLC plc(HanMacWrdJTextInternal::TOKEN);
1357 std::vector<int> bkmIdList;
1358 for (int i = 0; i < header.m_n; i++) {
1359 pos = input->tell();
1360 f.str("");
1361 HanMacWrdJTextInternal::Token tkn;
1362 long fPos = input->readLong(4);
1363
1364 val = static_cast<int>(input->readLong(2)); // always 0 ?
1365 if (val) f << "f0=" << val << ",";
1366 tkn.m_length=static_cast<int>(input->readLong(2));
1367 tkn.m_id = long(input->readULong(4));
1368
1369 tkn.m_type=static_cast<int>(input->readLong(1));
1370 for (int j=0; j < 2; j++) { // f1=0, f2=0|1[field]|11[footnote]
1371 val = static_cast<int>(input->readLong(1));
1372 if (val) f << "f" << j+1 << "=" << val << ",";
1373 }
1374 tkn.m_localId = static_cast<int>(input->readLong(1));
1375 tkn.m_extra = f.str();
1376 zone.m_tokenList.push_back(tkn);
1377 if (tkn.m_type==0x20)
1378 bkmIdList.push_back(i);
1379 plc.m_id = i;
1380 zone.m_PLCMap.insert(std::multimap<long, HanMacWrdJTextInternal::PLC>::value_type(fPos, plc));
1381
1382 f.str("");
1383 f << "TextToken-" << i << ":";
1384 if (fPos) f << "fPos=" << std::hex << fPos << std::dec << ",";
1385 f << tkn;
1386 asciiFile.addPos(pos);
1387 asciiFile.addNote(f.str().c_str());
1388 input->seek(pos+16, librevenge::RVNG_SEEK_SET);
1389 }
1390
1391 if (input->tell() != zoneEnd) {
1392 asciiFile.addDelimiter(input->tell(),'|');
1393 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
1394 }
1395
1396 pos = input->tell();
1397 // next: we can find potential bookmark zone
1398 for (size_t i=0; i < bkmIdList.size(); i++) {
1399 pos = input->tell();
1400 auto dataSz = long(input->readULong(4));
1401 zoneEnd = pos+4+dataSz;
1402 if (input->isEnd() || dataSz<0 || zoneEnd >= endPos) {
1403 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextToken: can not find bookmark text %d\n", int(i)));
1404 break;
1405 }
1406
1407 f.str("");
1408 f << "TextToken-data" << i << ":";
1409 asciiFile.addPos(pos);
1410 asciiFile.addNote(f.str().c_str());
1411 auto fSz=static_cast<int>(input->readULong(1));
1412 if (fSz == dataSz-2 || fSz==dataSz-1) {
1413 std::string bkmark("");
1414 for (int c=0; c < fSz; c++)
1415 bkmark+=char(input->readULong(1));
1416 f << bkmark;
1417
1418 zone.m_tokenList[size_t(bkmIdList[i])].m_bookmark=bkmark;
1419 }
1420 else {
1421 MWAW_DEBUG_MSG(("HanMacWrdJText::readTextToken: can not read bookmark text %d\n", int(i)));
1422 f << "###";
1423 }
1424 asciiFile.addPos(pos);
1425 asciiFile.addNote(f.str().c_str());
1426
1427 pos = zoneEnd;
1428 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
1429 }
1430 input->seek(pos, librevenge::RVNG_SEEK_SET);
1431 return true;
1432 }
1433
1434 ////////////////////////////////////////////////////////////
1435 // Fonts
1436 ////////////////////////////////////////////////////////////
1437
1438 // a single font
1439 bool HanMacWrdJText::readFont(MWAWFont &font, long endPos)
1440 {
1441 font = MWAWFont(-1,-1);
1442
1443 MWAWInputStreamPtr input = m_parserState->m_input;
1444 long pos = input->tell(), debPos=pos;
1445 if (endPos <= 0) {
1446 auto dataSz=long(input->readULong(4));
1447 pos+=4;
1448 endPos=pos+dataSz;
1449 if (!input->checkPosition(endPos)) {
1450 MWAW_DEBUG_MSG(("HanMacWrdJText::readFont: pb reading font size\n"));
1451 input->seek(debPos, librevenge::RVNG_SEEK_SET);
1452 return false;
1453 }
1454 }
1455 long len=endPos-pos;
1456 if (len < 24) {
1457 MWAW_DEBUG_MSG(("HanMacWrdJText::readFont: the zone is too short\n"));
1458 input->seek(debPos, librevenge::RVNG_SEEK_SET);
1459 return false;
1460 }
1461
1462 libmwaw::DebugStream f;
1463 font.setId(static_cast<int>(input->readLong(2)));
1464 auto val = static_cast<int>(input->readLong(2));
1465 if (val) f << "#f1=" << val << ",";
1466 font.setSize(float(input->readLong(4))/65536.f);
1467 float expand = float(input->readLong(4))/65536.f;
1468 if (expand < 0 || expand > 0)
1469 font.setDeltaLetterSpacing(expand*font.size());
1470 float xScale = float(input->readLong(4))/65536.f;
1471 if (xScale < 1 || xScale > 1)
1472 font.setWidthStreching(xScale);
1473
1474 auto flag =static_cast<int>(input->readULong(2));
1475 uint32_t flags=0;
1476 if (flag&1) {
1477 font.setUnderlineStyle(MWAWFont::Line::Simple);
1478 font.setUnderlineType(MWAWFont::Line::Double);
1479 }
1480 if (flag&2)
1481 font.setUnderlineStyle(MWAWFont::Line::Dot);
1482 if (flag&4) {
1483 font.setUnderlineStyle(MWAWFont::Line::Dot);
1484 font.setUnderlineWidth(2.0);
1485 }
1486 if (flag&8)
1487 font.setUnderlineStyle(MWAWFont::Line::Dash);
1488 if (flag&0x10)
1489 font.setStrikeOutStyle(MWAWFont::Line::Simple);
1490 if (flag&0x20) {
1491 font.setStrikeOutStyle(MWAWFont::Line::Simple);
1492 font.setStrikeOutType(MWAWFont::Line::Double);
1493 }
1494 if (flag&0xFFC0)
1495 f << "#flag0=" << std::hex << (flag&0xFFF2) << std::dec << ",";
1496 flag =static_cast<int>(input->readULong(2));
1497 if (flag&1) flags |= MWAWFont::boldBit;
1498 if (flag&0x2) flags |= MWAWFont::italicBit;
1499 if (flag&0x4) flags |= MWAWFont::outlineBit;
1500 if (flag&0x8) flags |= MWAWFont::shadowBit;
1501 if (flag&0x10) flags |= MWAWFont::reverseVideoBit;
1502 if (flag&0x20) font.set(MWAWFont::Script::super100());
1503 if (flag&0x40) font.set(MWAWFont::Script::sub100());
1504 if (flag&0x80) {
1505 if (flag&0x20)
1506 font.set(MWAWFont::Script(48,librevenge::RVNG_PERCENT,58));
1507 else if (flag&0x40)
1508 font.set(MWAWFont::Script(16,librevenge::RVNG_PERCENT,58));
1509 else
1510 font.set(MWAWFont::Script::super());
1511 }
1512 if (flag&0x100) {
1513 font.setOverlineStyle(MWAWFont::Line::Dot);
1514 font.setOverlineWidth(2.0);
1515 }
1516 if (flag&0x200) flags |= MWAWFont::boxedBit;
1517 if (flag&0x400) flags |= MWAWFont::boxedRoundedBit;
1518 if (flag&0x800) {
1519 font.setUnderlineStyle(MWAWFont::Line::Simple);
1520 font.setUnderlineWidth(0.5);
1521 }
1522 if (flag&0x1000) font.setUnderlineStyle(MWAWFont::Line::Simple);
1523 if (flag&0x2000) {
1524 font.setUnderlineStyle(MWAWFont::Line::Simple);
1525 font.setUnderlineWidth(2.0);
1526 }
1527 if (flag&0x4000) {
1528 font.setUnderlineStyle(MWAWFont::Line::Simple);
1529 font.setUnderlineWidth(3.0);
1530 }
1531
1532 if (flag&0x8000) {
1533 font.setUnderlineStyle(MWAWFont::Line::Simple);
1534 font.setUnderlineType(MWAWFont::Line::Double);
1535 font.setUnderlineWidth(0.5);
1536 }
1537 auto color = static_cast<int>(input->readLong(2));
1538 MWAWColor col;
1539 if (color && m_mainParser->getColor(color, 1, col))
1540 font.setColor(col);
1541 else if (color)
1542 f << "##fColor=" << color << ",";
1543 val = static_cast<int>(input->readLong(2));
1544 if (val) f << "#unk=" << val << ",";
1545 if (len >= 28) {
1546 for (int i = 0; i < 2; i++) {
1547 val = static_cast<int>(input->readLong(2));
1548 if (val) f << "#g" << i << "=" << val << ",";
1549 }
1550 }
1551 if (len >= 36) {
1552 color = static_cast<int>(input->readLong(2));
1553 auto pattern = static_cast<int>(input->readLong(2));
1554 if ((color || pattern) && m_mainParser->getColor(color, pattern, col))
1555 font.setBackgroundColor(col);
1556 else if (color || pattern)
1557 f << "#backColor=" << color << ", #pattern=" << pattern << ",";
1558 }
1559 if (input->tell() != endPos)
1560 m_parserState->m_asciiFile.addDelimiter(input->tell(),'|');
1561 font.setFlags(flags);
1562 font.m_extra = f.str();
1563
1564 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1565 return true;
1566 }
1567
1568 bool HanMacWrdJText::readFonts(MWAWEntry const &entry)
1569 {
1570 if (!entry.valid()) {
1571 MWAW_DEBUG_MSG(("HanMacWrdJText::readFonts: called without any entry\n"));
1572 return false;
1573 }
1574 if (entry.length() <= 8) {
1575 MWAW_DEBUG_MSG(("HanMacWrdJText::readFonts: the entry seems too short\n"));
1576 return false;
1577 }
1578 if (m_state->m_fontList.size()) {
1579 MWAW_DEBUG_MSG(("HanMacWrdJText::readFonts: oops the font list is not empty\n"));
1580 m_state->m_fontList.resize(0);
1581 }
1582 MWAWInputStreamPtr input = m_parserState->m_input;
1583 libmwaw::DebugFile &asciiFile = m_parserState->m_asciiFile;
1584 libmwaw::DebugStream f;
1585 entry.setParsed(true);
1586
1587 long pos = entry.begin()+8; // skip header
1588 long endPos = entry.end();
1589 input->seek(pos, librevenge::RVNG_SEEK_SET);
1590
1591 // first read the header
1592 f << entry.name() << "[header]:";
1593 HanMacWrdJZoneHeader mainHeader(false);
1594 if (!m_mainParser->readClassicHeader(mainHeader,endPos) || mainHeader.m_fieldSize!=8) {
1595 MWAW_DEBUG_MSG(("HanMacWrdJText::readFonts: can not read the header\n"));
1596 f << "###sz=" << mainHeader.m_length;
1597 asciiFile.addPos(pos);
1598 asciiFile.addNote(f.str().c_str());
1599 return false;
1600 }
1601 long headerEnd=pos+4+mainHeader.m_length;
1602 f << mainHeader;
1603 f << "unk=[";
1604 for (int i = 0; i < mainHeader.m_n; i++) {
1605 f << "[";
1606 long val = input->readLong(2); // always -2 ?
1607 if (val!=-2)
1608 f << val << ",";
1609 else
1610 f << "_,";
1611 val = long(input->readULong(2)); // 0 or 5020 : junk?
1612 if (val)
1613 f << std::hex << val << std::dec << ",";
1614 else
1615 f << "_,";
1616 val = long(input->readULong(4)); // id
1617 f << std::hex << val << std::dec;
1618 f << "]";
1619 }
1620 f << "],";
1621 if (input->tell()!=headerEnd) {
1622 asciiFile.addDelimiter(input->tell(),'|');
1623 input->seek(headerEnd, librevenge::RVNG_SEEK_SET);
1624 }
1625 asciiFile.addPos(pos);
1626 asciiFile.addNote(f.str().c_str());
1627
1628 for (int i = 0; i < mainHeader.m_n; i++) {
1629 pos = input->tell();
1630 f.str("");
1631 f << entry.name() << "-" << i << ":";
1632 MWAWFont font(-1,-1);
1633 if (!readFont(font) || input->tell() > endPos) {
1634 MWAW_DEBUG_MSG(("HanMacWrdJText::readFonts: can not read font %d\n", i));
1635 f << "###";
1636 asciiFile.addPos(pos);
1637 asciiFile.addNote(f.str().c_str());
1638 return false;
1639 }
1640 f << font.getDebugString(m_parserState->m_fontConverter) << ",";
1641 m_state->m_fontList.push_back(font);
1642 asciiFile.addPos(pos);
1643 asciiFile.addNote(f.str().c_str());
1644 }
1645 asciiFile.addPos(endPos);
1646 asciiFile.addNote("_");
1647 return true;
1648 }
1649
1650 // the list of fonts
1651 bool HanMacWrdJText::readFontNames(MWAWEntry const &entry)
1652 {
1653 if (!entry.valid()) {
1654 MWAW_DEBUG_MSG(("HanMacWrdJText::readFontNames: called without any entry\n"));
1655 return false;
1656 }
1657 if (entry.length() < 28) {
1658 MWAW_DEBUG_MSG(("HanMacWrdJText::readFontNames: the entry seems too short\n"));
1659 return false;
1660 }
1661 MWAWInputStreamPtr input = m_parserState->m_input;
1662 libmwaw::DebugFile &asciiFile = m_parserState->m_asciiFile;
1663 libmwaw::DebugStream f;
1664 entry.setParsed(true);
1665
1666 f << entry.name() << "[data]:";
1667
1668 long pos = entry.begin()+8; // skip header
1669 input->seek(pos, librevenge::RVNG_SEEK_SET);
1670 int N, val;
1671 auto readDataSz = long(input->readULong(4));
1672 if (readDataSz+12 != entry.length()) {
1673 MWAW_DEBUG_MSG(("HanMacWrdJText::readFontNames: the data size seems odd\n"));
1674 f << "##dataSz=" << readDataSz << ",";
1675 }
1676 N = static_cast<int>(input->readLong(2));
1677 f << "N=" << N << ",";
1678 auto fieldSz = long(input->readULong(4));
1679 if (fieldSz != 68) {
1680 MWAW_DEBUG_MSG(("HanMacWrdJText::readFontNames: the field size seems odd\n"));
1681 f << "##fieldSz=" << fieldSz << ",";
1682 }
1683 for (int i = 0; i < 3; i++) { //f1=f2=1
1684 val = static_cast<int>(input->readLong(2));
1685 if (val) f << "f" << i << "=" << val << ",";
1686 }
1687 auto id = long(input->readULong(4));
1688 if (id) f << "id=" << std::hex << id << std::dec << ",";
1689
1690 long expectedSz = N*68+28;
1691 if (expectedSz != entry.length() && expectedSz+1 != entry.length()) {
1692 MWAW_DEBUG_MSG(("HanMacWrdJText::readFontNames: the entry size seems odd\n"));
1693 return false;
1694 }
1695 asciiFile.addPos(pos);
1696 asciiFile.addNote(f.str().c_str());
1697
1698 for (int i = 0; i < N; i++) {
1699 pos = input->tell();
1700 f.str("");
1701 f << entry.name() << "-" << i << ":";
1702 auto fId = static_cast<int>(input->readLong(2));
1703 f << "fId=" << fId << ",";
1704 val = static_cast<int>(input->readLong(2));
1705 if (val != fId)
1706 f << "#fId2=" << val << ",";
1707 auto fSz = static_cast<int>(input->readULong(1));
1708 if (fSz+5 > 68) {
1709 f << "###fSz";
1710 MWAW_DEBUG_MSG(("HanMacWrdJText::readFontNames: can not read a font\n"));
1711 }
1712 else {
1713 std::string name("");
1714 for (int c = 0; c < fSz; c++)
1715 name += char(input->readULong(1));
1716 f << name;
1717 m_parserState->m_fontConverter->setCorrespondance(fId, name);
1718 }
1719 asciiFile.addPos(pos);
1720 asciiFile.addNote(f.str().c_str());
1721 input->seek(pos+68, librevenge::RVNG_SEEK_SET);
1722 }
1723 asciiFile.addPos(entry.end());
1724 asciiFile.addNote("_");
1725 return true;
1726 }
1727
1728 ////////////////////////////////////////////////////////////
1729 // Style
1730 ////////////////////////////////////////////////////////////
1731 bool HanMacWrdJText::readStyles(MWAWEntry const &entry)
1732 {
1733 if (!entry.valid()) {
1734 MWAW_DEBUG_MSG(("HanMacWrdJText::readStyles: called without any zone\n"));
1735 return false;
1736 }
1737
1738 long dataSz = entry.length();
1739 if (dataSz < 4) {
1740 MWAW_DEBUG_MSG(("HanMacWrdJText::readStyles: the zone seems too short\n"));
1741 return false;
1742 }
1743 MWAWInputStreamPtr input = m_parserState->m_input;
1744 libmwaw::DebugFile &asciiFile = m_parserState->m_asciiFile;
1745 libmwaw::DebugStream f;
1746 entry.setParsed(true);
1747
1748 f << entry.name() << "[header]:";
1749
1750 long pos = entry.begin()+8; // skip header
1751 long endPos = entry.end();
1752 input->seek(pos, librevenge::RVNG_SEEK_SET);
1753
1754 HanMacWrdJZoneHeader mainHeader(false);
1755 if (!m_mainParser->readClassicHeader(mainHeader,endPos) || mainHeader.m_fieldSize!=4) {
1756 MWAW_DEBUG_MSG(("HanMacWrdJText::readStyles: can not read the header\n"));
1757 f << "###sz=" << mainHeader.m_length;
1758 asciiFile.addPos(pos);
1759 asciiFile.addNote(f.str().c_str());
1760 return false;
1761 }
1762 long headerEnd=pos+4+mainHeader.m_length;
1763 f << mainHeader;
1764
1765 f << "listIds=[" << std::hex;
1766 for (int i = 0; i < mainHeader.m_n; i++)
1767 f << input->readULong(4) << ",";
1768 f << std::dec << "],";
1769 input->seek(headerEnd, librevenge::RVNG_SEEK_SET);
1770 asciiFile.addPos(pos);
1771 asciiFile.addNote(f.str().c_str());
1772
1773 for (int i = 0; i < mainHeader.m_n; i++) {
1774 f.str("");
1775 f << entry.name() << "-" << i << ":";
1776 pos = input->tell();
1777 long fieldSz = long(input->readULong(4))+4;
1778 if (fieldSz < 0x1bc || pos+fieldSz > endPos) {
1779 f << "###";
1780 asciiFile.addPos(pos);
1781 asciiFile.addNote(f.str().c_str());
1782
1783 MWAW_DEBUG_MSG(("HanMacWrdJText::readStyles: can not read field %d\n", i));
1784 return true;
1785 }
1786 auto val = static_cast<int>(input->readULong(1));
1787 if (val != i) f << "#id=" << val << ",";
1788
1789 // f0=c2|c6, f2=0|44, f3=1|14|15|16: fontId?
1790 for (int j=0; j < 5; j++) {
1791 val = static_cast<int>(input->readULong(1));
1792 if (val)
1793 f << "f" << j << "=" << std::hex << val << std::dec << ",";
1794 }
1795 /* g1=9|a|c|e|12|18: size ?, g5=1, g8=0|1, g25=0|18|30|48, g31=1, g35=0|1 */
1796 for (int j=0; j < 33; j++) {
1797 val = static_cast<int>(input->readULong(2));
1798 if (val)
1799 f << "g" << j << "=" << val << ",";
1800 }
1801 for (int j=0; j < 4; j++) { // b,b,b,0
1802 val = static_cast<int>(input->readULong(1));
1803 if ((j < 3 && val != 0xb) || (j==3 && val))
1804 f << "h" << j << "=" << val << ",";
1805 }
1806
1807 for (int j=0; j < 17; j++) { // always 0
1808 val = static_cast<int>(input->readULong(2));
1809 if (val)
1810 f << "l" << j << "=" << val << ",";
1811 }
1812 asciiFile.addPos(pos);
1813 asciiFile.addNote(f.str().c_str());
1814 long pos2 = input->tell();
1815 f.str("");
1816 f << entry.name() << "-" << i << "[B]:";
1817 // checkme probably f15=numTabs ..
1818 for (int j = 0; j < 50; j++) {
1819 val = static_cast<int>(input->readULong(2));
1820 if ((j < 5 && val != 1) || (j >= 5 && val))
1821 f << "f" << j << "=" << val << ",";
1822 }
1823 for (int j = 0; j < 50; j++) {
1824 val = static_cast<int>(input->readULong(2));
1825 if (val)
1826 f << "g" << j << "=" << val << ",";
1827 }
1828 for (int j = 0; j < 43; j++) {
1829 val = static_cast<int>(input->readULong(2));
1830 if (val)
1831 f << "h" << j << "=" << val << ",";
1832 }
1833 asciiFile.addPos(pos2);
1834 asciiFile.addNote(f.str().c_str());
1835
1836 pos2 = input->tell();
1837 f.str("");
1838 f << entry.name() << "-" << i << "[C]:";
1839 val = static_cast<int>(input->readLong(2));
1840 if (val != -1) f << "unkn=" << val << ",";
1841 val = static_cast<int>(input->readLong(2));
1842 if (val != i) f << "#id" << val << ",";
1843 for (int j = 0; j < 4; j++) {
1844 val = static_cast<int>(input->readLong(2));
1845 if (val) f << "f" << j << "=" << val << ",";
1846 }
1847 auto fSz = static_cast<int>(input->readULong(1));
1848 if (input->tell()+fSz > pos+fieldSz) {
1849 MWAW_DEBUG_MSG(("HanMacWrdJText::readStyles: can not read styleName\n"));
1850 f << "###";
1851 }
1852 else {
1853 std::string name("");
1854 for (int j = 0; j < fSz; j++)
1855 name +=char(input->readULong(1));
1856 f << name;
1857 }
1858 asciiFile.addPos(pos2);
1859 asciiFile.addNote(f.str().c_str());
1860 if (input->tell() != pos+fieldSz)
1861 asciiFile.addDelimiter(input->tell(),'|');
1862
1863 input->seek(pos+fieldSz, librevenge::RVNG_SEEK_SET);
1864 }
1865
1866 if (!input->isEnd()) {
1867 asciiFile.addPos(input->tell());
1868 asciiFile.addNote("_");
1869 }
1870 return true;
1871 }
1872
1873 ////////////////////////////////////////////////////////////
1874 // Paragraph
1875 ////////////////////////////////////////////////////////////
1876 bool HanMacWrdJText::readParagraph(HanMacWrdJTextInternal::Paragraph &para, long endPos)
1877 {
1878 para = HanMacWrdJTextInternal::Paragraph();
1879
1880 MWAWInputStreamPtr input = m_parserState->m_input;
1881 long pos = input->tell(), debPos=pos;
1882 if (endPos <= 0) {
1883 auto dataSz=long(input->readULong(4));
1884 pos+=4;
1885 endPos=pos+dataSz;
1886 if (!input->checkPosition(endPos)) {
1887 MWAW_DEBUG_MSG(("HanMacWrdJText::readParagraph: pb reading para size\n"));
1888 input->seek(debPos, librevenge::RVNG_SEEK_SET);
1889 return false;
1890 }
1891 }
1892 long len=endPos-pos;
1893 if (len < 102) {
1894 MWAW_DEBUG_MSG(("HanMacWrdJText::readParagraph: the zone is too short\n"));
1895 input->seek(debPos, librevenge::RVNG_SEEK_SET);
1896 return false;
1897 }
1898
1899 libmwaw::DebugStream f;
1900 libmwaw::DebugFile &asciiFile = m_parserState->m_asciiFile;
1901 auto flags = static_cast<int>(input->readULong(1));
1902 if (flags&0x80)
1903 para.m_breakStatus = para.m_breakStatus.get()|MWAWParagraph::NoBreakWithNextBit;
1904 if (flags&0x40)
1905 para.m_breakStatus = para.m_breakStatus.get()|MWAWParagraph::NoBreakBit;
1906 if (flags&0x2)
1907 para.m_addPageBreak = true;
1908 if (flags&0x4)
1909 f << "linebreakByWord,";
1910
1911 if (flags & 0x39) f << "#fl=" << std::hex << (flags&0x39) << std::dec << ",";
1912
1913 auto val = static_cast<int>(input->readLong(2));
1914 if (val) f << "#f0=" << val << ",";
1915 val = static_cast<int>(input->readULong(2));
1916 switch (val&3) {
1917 case 0:
1918 para.m_justify = MWAWParagraph::JustificationLeft;
1919 break;
1920 case 1:
1921 para.m_justify = MWAWParagraph::JustificationRight;
1922 break;
1923 case 2:
1924 para.m_justify = MWAWParagraph::JustificationCenter;
1925 break;
1926 case 3:
1927 para.m_justify = MWAWParagraph::JustificationFull;
1928 break;
1929 default:
1930 break;
1931 }
1932 if (val&0xFFFC) f << "#f1=" << val << ",";
1933 val = static_cast<int>(input->readLong(1));
1934 if (val) f << "#f2=" << val << ",";
1935 para.m_type = static_cast<int>(input->readLong(2));
1936
1937 float dim[3];
1938 for (auto &d : dim) d = float(input->readLong(4))/65536.0f;
1939 para.m_marginsUnit = librevenge::RVNG_POINT;
1940 para.m_margins[0]=double(dim[1]);
1941 para.m_margins[1]=double(dim[0]);
1942 para.m_margins[2]=double(dim[2]); // ie. distance to right border - ...
1943
1944 for (auto &spacing : para.m_spacings)
1945 spacing = double(input->readLong(4))/65536.0;
1946 int spacingsUnit[3]; // 1=mm, ..., 4=pt, b=line
1947 for (auto &s : spacingsUnit) s= static_cast<int>(input->readULong(1));
1948 if (spacingsUnit[0]==0xb)
1949 para.m_spacingsInterlineUnit = librevenge::RVNG_PERCENT;
1950 else
1951 para.m_spacingsInterlineUnit = librevenge::RVNG_POINT;
1952 for (int i = 1; i < 3; i++) // convert point|line -> inches
1953 para.m_spacings[i]= ((spacingsUnit[i]==0xb) ? 12.0 : 1.0)*(para.m_spacings[i].get())/72.0;
1954
1955 val = static_cast<int>(input->readLong(1));
1956 if (val) f << "#f3=" << val << ",";
1957 for (int i = 0; i< 2; i++) { // one time f4=8000
1958 val = static_cast<int>(input->readULong(2));
1959 if (val) f << "#f" << i+4 << "=" << std::hex << val << std::dec << ",";
1960 }
1961 // the borders
1962 char const *wh[5] = { "T", "L", "B", "R", "VSep" };
1963 MWAWBorder borders[5];
1964 for (auto &border : borders) border.m_width = double(input->readLong(4))/65536.;
1965 for (int d=0; d < 5; d++) {
1966 val = static_cast<int>(input->readULong(1));
1967 switch (val) {
1968 case 0: // normal
1969 break;
1970 case 1:
1971 borders[d].m_type = MWAWBorder::Double;
1972 break;
1973 case 2:
1974 borders[d].m_type = MWAWBorder::Double;
1975 f << "bord" << wh[d] << "[ext=2],";
1976 break;
1977 case 3:
1978 borders[d].m_type = MWAWBorder::Double;
1979 f << "bord" << wh[d] << "[int=2],";
1980 break;
1981 default:
1982 f << "#bord" << wh[d] << "[style=" << val << "],";
1983 break;
1984 }
1985 }
1986 int color[5], pattern[5];
1987 for (auto &c : color) c = static_cast<int>(input->readULong(1));
1988 for (auto &p : pattern) p= static_cast<int>(input->readULong(2));
1989 for (int d=0; d < 5; d++) {
1990 if (!color[d] && !pattern[d])
1991 continue;
1992 MWAWColor col;
1993 if (m_mainParser->getColor(color[d], pattern[d], col))
1994 borders[d].m_color = col;
1995 else
1996 f << "#bord" << wh[d] << "[col=" << color[d] << ",pat=" << pattern[d] << "],";
1997 }
1998 // update the paragraph
1999 para.resizeBorders(6);
2000 libmwaw::Position const which[5] = {
2001 libmwaw::Top, libmwaw::Left, libmwaw::Bottom, libmwaw::Right,
2002 libmwaw::VMiddle
2003 };
2004 for (int d=0; d < 5; d++) {
2005 if (borders[d].m_width <= 0)
2006 continue;
2007 para.m_borders[which[d]]=borders[d];
2008 }
2009 val = static_cast<int>(input->readLong(1));
2010 if (val) f << "#f6=" << val << ",";
2011 double bMargins[5]= {0,0,0,0,0};
2012 for (int d = 0; d < 5; d++) {
2013 bMargins[d] = double(input->readLong(4))/256./65536./72.;
2014 if (bMargins[d] > 0 || bMargins[d] < 0)
2015 f << "bordMarg" << wh[d] << "=" << bMargins[d] << ",";
2016 }
2017 auto nTabs = static_cast<int>(input->readULong(1));
2018 if (input->tell()+2+nTabs*12 > endPos) {
2019 MWAW_DEBUG_MSG(("HanMacWrdJText::readParagraph: can not read numbers of tab\n"));
2020 input->seek(debPos, librevenge::RVNG_SEEK_SET);
2021 return false;
2022 }
2023 val = static_cast<int>(input->readULong(2));
2024 if (val) f << "#h3=" << val << ",";
2025 para.m_extra=f.str();
2026 f.str("");
2027 f << "Ruler:" << para;
2028
2029 asciiFile.addPos(debPos);
2030 asciiFile.addNote(f.str().c_str());
2031 for (int i = 0; i < nTabs; i++) {
2032 pos = input->tell();
2033 f.str("");
2034 f << "Ruler[Tabs-" << i << "]:";
2035
2036 MWAWTabStop tab;
2037 val = static_cast<int>(input->readULong(1));
2038 switch (val) {
2039 case 0:
2040 break;
2041 case 1:
2042 tab.m_alignment = MWAWTabStop::CENTER;
2043 break;
2044 case 2:
2045 tab.m_alignment = MWAWTabStop::RIGHT;
2046 break;
2047 case 3:
2048 tab.m_alignment = MWAWTabStop::DECIMAL;
2049 break;
2050 case 4:
2051 tab.m_alignment = MWAWTabStop::BAR;
2052 break;
2053 default:
2054 f << "#type=" << val << ",";
2055 break;
2056 }
2057 val = static_cast<int>(input->readULong(1));
2058 if (val) f << "barType=" << val << ",";
2059 val = static_cast<int>(input->readULong(2));
2060 if (val) {
2061 int unicode= m_parserState->m_fontConverter->unicode(3, static_cast<unsigned char>(val));
2062 if (unicode==-1)
2063 tab.m_decimalCharacter = uint16_t(val);
2064 else
2065 tab.m_decimalCharacter = uint16_t(unicode);
2066 }
2067 val = static_cast<int>(input->readULong(2));
2068 if (val) {
2069 int unicode= m_parserState->m_fontConverter->unicode(3, static_cast<unsigned char>(val));
2070 if (unicode==-1)
2071 tab.m_leaderCharacter = uint16_t(val);
2072 else
2073 tab.m_leaderCharacter = uint16_t(unicode);
2074 }
2075 val = static_cast<int>(input->readULong(2)); // 0|73|74|a044|f170|f1e0|f590
2076 if (val) f << "f0=" << std::hex << val << std::dec << ",";
2077
2078 tab.m_position = double(input->readLong(4))/65536./72.;
2079 para.m_tabs->push_back(tab);
2080 f << tab;
2081
2082 asciiFile.addPos(pos);
2083 asciiFile.addNote(f.str().c_str());
2084 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
2085 }
2086 if (input->tell()!=endPos) {
2087 asciiFile.addDelimiter(input->tell(),'|');
2088 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2089 }
2090 return true;
2091 }
2092
2093 bool HanMacWrdJText::readParagraphs(MWAWEntry const &entry)
2094 {
2095 if (!entry.valid()) {
2096 MWAW_DEBUG_MSG(("HanMacWrdJText::readParagraphs: called without any entry\n"));
2097 return false;
2098 }
2099 if (entry.length() <= 8) {
2100 MWAW_DEBUG_MSG(("HanMacWrdJText::readParagraphs: the entry seems too short\n"));
2101 return false;
2102 }
2103 if (m_state->m_paragraphList.size()) {
2104 MWAW_DEBUG_MSG(("HanMacWrdJText::readParagraphs: oops the paragraph list is not empty\n"));
2105 m_state->m_paragraphList.resize(0);
2106 }
2107
2108 MWAWInputStreamPtr input = m_parserState->m_input;
2109 libmwaw::DebugFile &asciiFile = m_parserState->m_asciiFile;
2110 libmwaw::DebugStream f;
2111 entry.setParsed(true);
2112
2113 long pos = entry.begin()+8; // skip header
2114 long endPos = entry.end();
2115 input->seek(pos, librevenge::RVNG_SEEK_SET);
2116
2117 // first read the header
2118 f << entry.name() << "[header]:";
2119 HanMacWrdJZoneHeader mainHeader(false);
2120 if (!m_mainParser->readClassicHeader(mainHeader,endPos) || mainHeader.m_fieldSize!=12) {
2121 MWAW_DEBUG_MSG(("HanMacWrdJText::readParagraphs: can not read the header\n"));
2122 f << "###sz=" << mainHeader.m_length;
2123 asciiFile.addPos(pos);
2124 asciiFile.addNote(f.str().c_str());
2125 return false;
2126 }
2127 long headerEnd=pos+4+mainHeader.m_length;
2128 f << mainHeader;
2129
2130 f << "unk=[";
2131 for (int i = 0; i < mainHeader.m_n; i++) {
2132 f << "[";
2133 long val = input->readLong(2); // always -2 ?
2134 if (val!=-2)
2135 f << "unkn0=" << val << ",";
2136 val = long(input->readULong(2)); // 0|1|2|5
2137 if (val)
2138 f << "type=" << val << ",";
2139 val = long(input->readULong(4)); // a id
2140 if (val)
2141 f << "id1=" << std::hex << val << std::dec << ",";
2142 val = long(input->readULong(4));
2143 if (val)
2144 f << "id2=" << std::hex << val << std::dec << ",";
2145 f << "]";
2146 }
2147 f << "],";
2148 if (input->tell()!=headerEnd) {
2149 asciiFile.addDelimiter(input->tell(),'|');
2150 input->seek(headerEnd, librevenge::RVNG_SEEK_SET);
2151 }
2152 asciiFile.addPos(pos);
2153 asciiFile.addNote(f.str().c_str());
2154
2155 for (int i = 0; i < mainHeader.m_n; i++) {
2156 pos = input->tell();
2157 HanMacWrdJTextInternal::Paragraph paragraph;
2158 if (!readParagraph(paragraph) || input->tell() > endPos) {
2159 MWAW_DEBUG_MSG(("HanMacWrdJText::readParagraphs: can not read paragraph %d\n", i));
2160 asciiFile.addPos(pos);
2161 asciiFile.addNote("Ruler###");
2162 return false;
2163 }
2164 m_state->m_paragraphList.push_back(paragraph);
2165 }
2166 asciiFile.addPos(endPos);
2167 asciiFile.addNote("_");
2168 return true;
2169 }
2170
2171 ////////////////////////////////////////////////////////////
2172 // the sections/footnotes
2173 ////////////////////////////////////////////////////////////
2174 bool HanMacWrdJText::readSections(MWAWEntry const &entry)
2175 {
2176 if (!entry.valid()) {
2177 MWAW_DEBUG_MSG(("HanMacWrdJText::readSections: called without any entry\n"));
2178 return false;
2179 }
2180 if (entry.length() < 20) {
2181 MWAW_DEBUG_MSG(("HanMacWrdJText::readSections: the entry seems too short\n"));
2182 return false;
2183 }
2184 if (m_state->m_sectionList.size()) {
2185 MWAW_DEBUG_MSG(("HanMacWrdJText::readSections: the list of section is not empty\n"));
2186 m_state->m_sectionList.resize(0);
2187 }
2188 long pos = entry.begin()+8; // skip header
2189 long endPos = entry.end();
2190
2191 MWAWInputStreamPtr input = m_parserState->m_input;
2192 libmwaw::DebugFile &asciiFile = m_parserState->m_asciiFile;
2193 libmwaw::DebugStream f;
2194 entry.setParsed(true);
2195 input->seek(pos, librevenge::RVNG_SEEK_SET);
2196 f << entry.name() << "[header]:";
2197 long val;
2198 for (int i = 0; i < 2; i++) { // fl0=0|0
2199 val = long(input->readULong(1));
2200 if (val) f << "fl" << i << "=" << std::hex << val << std::hex << ",";
2201 }
2202 for (int i = 0; i < 3; i++) { // f0=1, f1=N, f2=0
2203 val = input->readLong(2);
2204 if (val) f << "f" << i << "=" << val << ",";
2205 }
2206 asciiFile.addPos(pos);
2207 asciiFile.addNote(f.str().c_str());
2208
2209 auto dataSz = long(input->readULong(4));
2210 if (!dataSz) return true;
2211
2212 input->seek(-4, librevenge::RVNG_SEEK_CUR);
2213 pos = input->tell();
2214 f.str("");
2215 f << entry.name() << ":";
2216 HanMacWrdJZoneHeader header(false);
2217 if (!m_mainParser->readClassicHeader(header,endPos) || header.m_fieldSize!=0x5c) {
2218 MWAW_DEBUG_MSG(("HanMacWrdJText::readSections: can not read second zone\n"));
2219 f << "###" << header;
2220 asciiFile.addPos(pos);
2221 asciiFile.addNote(f.str().c_str());
2222 return false;
2223 }
2224 f << header;
2225 long zoneEnd=pos+4+header.m_length;
2226 asciiFile.addPos(pos);
2227 asciiFile.addNote(f.str().c_str());
2228
2229 for (int i = 0; i < header.m_n; i++) {
2230 pos = input->tell();
2231 f.str("");
2232 HanMacWrdJTextInternal::Section sec;
2233 sec.m_id= input->readLong(2);
2234 val = input->readLong(2); // almost alway=id, but not always
2235 if (val != sec.m_id)
2236 f << "#id2=" << val << ",";
2237 val = input->readLong(2); // 0|1
2238 if (val) f << "f0=" << val << ",";
2239 val = long(input->readULong(2));
2240 auto numCol = int(val >> 12);
2241 if (numCol<=0 || numCol > 8) {
2242 MWAW_DEBUG_MSG(("HanMacWrdJText::readSections: can not determine the num of columns\n"));
2243 f << "#numCols=" << numCol << ",";
2244 numCol = 1;
2245 }
2246 else
2247 sec.m_numCols=numCol;
2248 bool differentWidth=(val & 0xFFF) == 0;
2249 if (val & 0x7FF)
2250 f << "#fl=" << std::hex << (val & 0xFFF) << ",";
2251 if (differentWidth) {
2252 for (int j = 0; j < numCol; j++) {
2253 sec.m_colWidth.push_back(double(input->readLong(4))/65536);
2254 sec.m_colSep.push_back(double(input->readLong(4))/65536);
2255 }
2256 }
2257 else {
2258 sec.m_colWidth.push_back(double(input->readLong(4))/65536);
2259 sec.m_colSep.push_back(double(input->readLong(4))/65536);
2260 }
2261 sec.m_extra = f.str();
2262 m_state->m_sectionList.push_back(sec);
2263
2264 f.str("");
2265 f << entry.name() << "-" << i << ":" << sec;
2266
2267 asciiFile.addDelimiter(input->tell(), '|');
2268 asciiFile.addPos(pos);
2269 asciiFile.addNote(f.str().c_str());
2270 input->seek(pos+0x5c, librevenge::RVNG_SEEK_SET);
2271 }
2272 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
2273
2274 pos = input->tell();
2275 if (pos!=endPos) {
2276 MWAW_DEBUG_MSG(("HanMacWrdJText::readSections: find unexpected end data\n"));
2277 f.str("");
2278 f << entry.name() << "###:";
2279 asciiFile.addPos(pos);
2280 asciiFile.addNote(f.str().c_str());
2281 }
2282 return true;
2283 }
2284
2285 bool HanMacWrdJText::readFtnPos(MWAWEntry const &entry)
2286 {
2287 if (!entry.valid()) {
2288 MWAW_DEBUG_MSG(("HanMacWrdJText::readFtnPos: called without any entry\n"));
2289 return false;
2290 }
2291 if (entry.length() < 16) {
2292 MWAW_DEBUG_MSG(("HanMacWrdJText::readFtnPos: the entry seems too short\n"));
2293 return false;
2294 }
2295 long pos = entry.begin()+8; // skip header
2296 long endPos = entry.end();
2297
2298 MWAWInputStreamPtr input = m_parserState->m_input;
2299 libmwaw::DebugFile &asciiFile = m_parserState->m_asciiFile;
2300 libmwaw::DebugStream f;
2301 entry.setParsed(true);
2302 input->seek(pos, librevenge::RVNG_SEEK_SET);
2303 f << entry.name() << "[header]:";
2304 auto val = long(input->readULong(2)); // always 0x200
2305 if (val!=0x2000)
2306 f << "f0=" << std::hex << val << std::dec << ",";
2307 val = input->readLong(2); // always 1 ?
2308 if (val != 1)
2309 f << "f1=" << val << ",";
2310 asciiFile.addPos(pos);
2311 asciiFile.addNote(f.str().c_str());
2312
2313 auto dataSz = long(input->readULong(4));
2314 if (!dataSz) return true;
2315
2316 input->seek(-4, librevenge::RVNG_SEEK_CUR);
2317 pos = input->tell();
2318 f.str("");
2319 f << entry.name() << ":";
2320 HanMacWrdJZoneHeader header(false);
2321 if (!m_mainParser->readClassicHeader(header,endPos) || header.m_fieldSize!=20||
2322 16+20*header.m_n+28 > header.m_length) {
2323 MWAW_DEBUG_MSG(("HanMacWrdJText::readFtnPos: can not read second zone\n"));
2324 f << "###" << header;
2325 asciiFile.addPos(pos);
2326 asciiFile.addNote(f.str().c_str());
2327 return false;
2328 }
2329 f << header;
2330 long zoneEnd=pos+4+header.m_length;
2331 asciiFile.addDelimiter(input->tell(),'|');
2332 asciiFile.addPos(pos);
2333 asciiFile.addNote(f.str().c_str());
2334
2335 input->seek(28, librevenge::RVNG_SEEK_CUR);
2336 for (int i = 0; i < header.m_n; i++) {
2337 pos = input->tell();
2338 f.str("");
2339 f << entry.name() << "-" << i << ":";
2340 val = long(input->readULong(1));
2341 if (val!=0x11) {
2342 MWAW_DEBUG_MSG(("HanMacWrdJText::readFtnPos: find unexpected type\n"));
2343 f << "#type=" << std::hex << val << std::dec << ",";
2344 }
2345 f << "id=" << input->readLong(1) << ",";
2346 for (int j = 0; j < 5; j++) { // always 0?
2347 val = input->readLong(2);
2348 if (val) f << "f" << j << "=" << val << ",";
2349 }
2350 // the text id
2351 f << "zId[text]=" << std::hex << input->readULong(4) << std::dec << ",";
2352 // the footnote id
2353 f << "zId[footnote]=" << std::hex << input->readULong(4) << std::dec << ",";
2354 asciiFile.addPos(pos);
2355 asciiFile.addNote(f.str().c_str());
2356 input->seek(pos+20, librevenge::RVNG_SEEK_SET);
2357 }
2358 input->seek(zoneEnd, librevenge::RVNG_SEEK_SET);
2359
2360 pos = input->tell();
2361 if (pos!=endPos) {
2362 MWAW_DEBUG_MSG(("HanMacWrdJText::readFtnPos: find unexpected end data\n"));
2363 f.str("");
2364 f << entry.name() << "###:";
2365 asciiFile.addPos(pos);
2366 asciiFile.addNote(f.str().c_str());
2367 }
2368 return true;
2369 }
2370 ////////////////////////////////////////////////////////////
2371 //
2372 // Low level
2373 //
2374 ////////////////////////////////////////////////////////////
2375
2376 //! send data to the listener
2377
2378 void HanMacWrdJText::flushExtra()
2379 {
2380 if (!m_parserState->m_textListener) return;
2381
2382 #ifdef DEBUG
2383 for (auto &zone : m_state->m_textZoneList) {
2384 if (zone.m_parsed)
2385 continue;
2386
2387 static bool first = true;
2388 if (first) {
2389 MWAW_DEBUG_MSG(("HanMacWrdJText::flushExtra: find some unsent zone\n"));
2390 first = false;
2391 }
2392 sendText(zone,0);
2393 m_parserState->m_textListener->insertEOL();
2394 }
2395 #endif
2396 }
2397
2398 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: