Debian Codegraph @

src/lib/ClarisWksText.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 "MWAWFont.hxx"
43 #include "MWAWFontConverter.hxx"
44 #include "MWAWGraphicListener.hxx"
45 #include "MWAWListener.hxx"
46 #include "MWAWParagraph.hxx"
47 #include "MWAWParser.hxx"
48 #include "MWAWPosition.hxx"
49 #include "MWAWSection.hxx"
50
51 #include "ClarisWksDocument.hxx"
52 #include "ClarisWksStruct.hxx"
53 #include "ClarisWksStyleManager.hxx"
54
55 #include "ClarisWksText.hxx"
56
57 /** Internal: the structures of a ClarisWksText */
58 namespace ClarisWksTextInternal
59 {
60 /** the different plc type */
61 enum PLCType { P_Font, P_Ruler, P_Child, P_Section, P_TextZone, P_Token, P_Unknown};
62
63 /** Internal : the different plc types: mainly for debugging */
64 struct PLC {
65 /// the constructor
66 PLC()
67 : m_type(P_Unknown)
68 , m_id(-1)
69 , m_extra("")
70 {
71 }
72 //! operator<<
73 friend std::ostream &operator<<(std::ostream &o, PLC const &plc);
74 /** the PLC types */
75 PLCType m_type;
76 /** the id */
77 int m_id;
78 /** extra data */
79 std::string m_extra;
80 };
81
82 std::ostream &operator<<(std::ostream &o, PLC const &plc)
83 {
84 switch (plc.m_type) {
85 case P_Font:
86 o << "F";
87 break;
88 case P_Ruler:
89 o << "R";
90 break;
91 case P_Child:
92 o << "C";
93 break;
94 case P_Section:
95 o << "S";
96 break;
97 case P_TextZone:
98 o << "TZ";
99 break;
100 case P_Token:
101 o << "Tok";
102 break;
103 case P_Unknown:
104 #if !defined(__clang__)
105 default:
106 #endif
107 o << "#Unkn";
108 break;
109 }
110 if (plc.m_id >= 0) o << plc.m_id;
111 else o << "_";
112 if (plc.m_extra.length()) o << ":" << plc.m_extra;
113 return o;
114 }
115
116 /** Internal: class to store the paragraph properties */
117 struct Paragraph final : public MWAWParagraph {
118 //! constructor
119 Paragraph()
120 : MWAWParagraph()
121 , m_labelType(0)
122 {
123 }
124 Paragraph(Paragraph const &)=default;
125 Paragraph &operator=(Paragraph const &)=default;
126 //! destructor
127 ~Paragraph() final;
128 //! operator<<
129 friend std::ostream &operator<<(std::ostream &o, Paragraph const &ind)
130 {
131 o << static_cast<MWAWParagraph const &>(ind) << ",";
132 if (ind.m_labelType > 0 && ind.m_labelType < 12) {
133 static char const *labelNames[] = {
134 "none", "diamond", "bullet", "checkbox", "hardvard", "leader", "legal",
135 "upperalpha", "alpha", "numeric", "upperroman", "roman"
136 };
137 o << "label=" << labelNames[ind.m_labelType] << ",";
138 }
139 else if (ind.m_labelType)
140 o << "#labelType=" << ind.m_labelType << ",";
141 return o;
142 }
143 //! update the list level
144 void updateListLevel();
145 //! the label
146 int m_labelType;
147 };
148
149 Paragraph::~Paragraph()
150 {
151 }
152
153 void Paragraph::updateListLevel()
154 {
155 int extraLevel = m_labelType!=0 ? 1 : 0;
156 if (*m_listLevelIndex+extraLevel<=0)
157 return;
158 int lev = *m_listLevelIndex+extraLevel;
159 m_listLevelIndex = lev;
160 MWAWListLevel theLevel;
161 theLevel.m_labelWidth=0.2;
162 switch (m_labelType) {
163 case 0:
164 theLevel.m_type = MWAWListLevel::NONE;
165 break;
166 case 1: // diamond
167 theLevel.m_type = MWAWListLevel::BULLET;
168 libmwaw::appendUnicode(0x25c7, theLevel.m_bullet);
169 break;
170 case 3: // checkbox
171 theLevel.m_type = MWAWListLevel::BULLET;
172 libmwaw::appendUnicode(0x2610, theLevel.m_bullet);
173 break;
174 case 4: {
175 theLevel.m_suffix = (lev <= 3) ? "." : ")";
176 if (lev == 1) theLevel.m_type = MWAWListLevel::UPPER_ROMAN;
177 else if (lev == 2) theLevel.m_type = MWAWListLevel::UPPER_ALPHA;
178 else if (lev == 3) theLevel.m_type = MWAWListLevel::DECIMAL;
179 else if (lev == 4) theLevel.m_type = MWAWListLevel::LOWER_ALPHA;
180 else if ((lev%3)==2) {
181 theLevel.m_prefix = "(";
182 theLevel.m_type = MWAWListLevel::DECIMAL;
183 }
184 else if ((lev%3)==0) {
185 theLevel.m_prefix = "(";
186 theLevel.m_type = MWAWListLevel::LOWER_ALPHA;
187 }
188 else
189 theLevel.m_type = MWAWListLevel::LOWER_ROMAN;
190 break;
191 }
192 case 5: // leader
193 theLevel.m_type = MWAWListLevel::BULLET;
194 theLevel.m_bullet = "+"; // in fact + + and -
195 break;
196 case 6: // legal
197 theLevel.m_type = MWAWListLevel::DECIMAL;
198 theLevel.m_numBeforeLabels = lev-1;
199 theLevel.m_suffix = ".";
200 theLevel.m_labelWidth = 0.2*lev;
201 break;
202 case 7:
203 theLevel.m_type = MWAWListLevel::UPPER_ALPHA;
204 theLevel.m_suffix = ".";
205 break;
206 case 8:
207 theLevel.m_type = MWAWListLevel::LOWER_ALPHA;
208 theLevel.m_suffix = ".";
209 break;
210 case 9:
211 theLevel.m_type = MWAWListLevel::DECIMAL;
212 theLevel.m_suffix = ".";
213 break;
214 case 10:
215 theLevel.m_type = MWAWListLevel::UPPER_ROMAN;
216 theLevel.m_suffix = ".";
217 break;
218 case 11:
219 theLevel.m_type = MWAWListLevel::LOWER_ROMAN;
220 theLevel.m_suffix = ".";
221 break;
222 case 2: // bullet
223 default:
224 theLevel.m_type = MWAWListLevel::BULLET;
225 libmwaw::appendUnicode(0x2022, theLevel.m_bullet);
226 break;
227 }
228 m_margins[1]=m_margins[1].get()-theLevel.m_labelWidth;
229 m_listLevel=theLevel;
230 }
231
232 struct ParagraphPLC {
233 ParagraphPLC()
234 : m_rulerId(-1)
235 , m_styleId(-1)
236 , m_flags(0)
237 , m_extra("")
238 {
239 }
240
241 friend std::ostream &operator<<(std::ostream &o, ParagraphPLC const &info)
242 {
243 if (info.m_rulerId >= 0) o << "P" << info.m_rulerId <<",";
244 if (info.m_styleId >= 0) o << "LK" << info.m_styleId <<",";
245 switch (info.m_flags&3) {
246 case 0: // normal
247 break;
248 case 1:
249 o << "hidden,";
250 break;
251 case 2:
252 o << "collapsed,";
253 break;
254 default:
255 o<< "hidden/collapsed,";
256 break;
257 }
258 if (info.m_flags&4)
259 o << "flags4,";
260
261 auto listType=int((info.m_flags>>3)&0xF);
262 if (listType>0 && listType < 12) {
263 static char const *labelNames[] = {
264 "none", "diamond", "bullet", "checkbox", "hardvard", "leader", "legal",
265 "upperalpha", "alpha", "numeric", "upperroman", "roman"
266 };
267 o << labelNames[listType] << ",";
268 }
269 else if (listType)
270 o << "#listType=" << listType << ",";
271 if (info.m_flags&0x80) o << "flags80,";
272 auto listLevel=int((info.m_flags>>8)&0xF);
273 if (listLevel) o << "level=" << listLevel+1;
274 if (info.m_flags>>12) o << "flags=" << std::hex << (info.m_flags>>12) << std::dec << ",";
275 if (info.m_extra.length()) o << info.m_extra;
276 return o;
277 }
278
279 /** the ruler id */
280 int m_rulerId;
281 /** the style id ( via the style lookup table )*/
282 int m_styleId;
283 /** some flags */
284 int m_flags;
285 /** extra data */
286 std::string m_extra;
287 };
288
289 /** internal class used to store a section */
290 struct Section {
291 //! the constructor
292 Section()
293 : m_pos(0)
294 , m_numColumns(1)
295 , m_columnsWidth()
296 , m_columnsSep()
297 , m_startOnNewPage(false)
298 , m_firstPage(0)
299 , m_hasTitlePage(false)
300 , m_continuousHF(true)
301 , m_leftRightHF(false)
302 , m_extra("")
303 {
304 for (auto &id : m_HFId) id=0;
305 }
306 //! returns a section
307 MWAWSection getSection() const
308 {
309 MWAWSection sec;
310 if (m_numColumns <= 1)
311 return sec;
312 size_t numCols = m_columnsWidth.size();
313 if (m_numColumns != int(numCols)) {
314 MWAW_DEBUG_MSG(("ClarisWksTextInternal::Section::getSection: unexpected number of columns\n"));
315 return sec;
316 }
317 bool hasSep = numCols==m_columnsSep.size();
318 if (!hasSep && m_columnsSep.size()) {
319 MWAW_DEBUG_MSG(("ClarisWksTextInternal::Section::getSection: can not used column separator\n"));
320 return sec;
321 }
322 sec.m_columns.resize(size_t(numCols));
323 for (size_t c=0; c < numCols; c++) {
324 sec.m_columns[c].m_width = double(m_columnsWidth[c]);
325 sec.m_columns[c].m_widthUnit = librevenge::RVNG_POINT;
326 if (!hasSep) continue;
327 sec.m_columns[c].m_margins[libmwaw::Left]=
328 double(m_columnsSep[c])/72.*(c==0 ? 1. : 0.5);
329 if (c+1!=numCols)
330 sec.m_columns[c].m_margins[libmwaw::Right]=double(m_columnsSep[c+1])/2.0/72.;
331 }
332 return sec;
333 }
334 //! operator <<
335 friend std::ostream &operator<<(std::ostream &o, Section const &sec)
336 {
337 o << "pos=" << sec.m_pos << ",";
338 if (sec.m_numColumns != 1) o << "numCols=" << sec.m_numColumns << ",";
339 o << "col[width]=[";
340 for (auto const &w : sec.m_columnsWidth)
341 o << w << ",";
342 o << "],";
343 if (sec.m_columnsSep.size()) {
344 o << "col[sepW]=[";
345 for (auto const &sep : sec.m_columnsSep)
346 o << sep << ",";
347 o << "],";
348 }
349 if (sec.m_firstPage) o << "first[page]=" << sec.m_firstPage << ",";
350 if (sec.m_hasTitlePage) o << "title[page],";
351 if (sec.m_continuousHF) o << "continuousHF,";
352 if (sec.m_leftRightHF) o << "leftRightHF,";
353 if (sec.m_HFId[0]) o << "id[header]=" << sec.m_HFId[0] << ",";
354 if (sec.m_HFId[1] || sec.m_HFId[0]!=sec.m_HFId[1]) o << "id[header2]=" << sec.m_HFId[1] << ",";
355 if (sec.m_HFId[2]) o << "id[footer]=" << sec.m_HFId[2] << ",";
356 if (sec.m_HFId[3] || sec.m_HFId[2]!=sec.m_HFId[3]) o << "id[footer2]=" << sec.m_HFId[3] << ",";
357 if (sec.m_extra.length()) o << sec.m_extra;
358 return o;
359 }
360 /** the character position */
361 long m_pos;
362 /** the number of column */
363 int m_numColumns;
364 /** the columns width */
365 std::vector<int> m_columnsWidth;
366 /** the columns separator */
367 std::vector<int> m_columnsSep;
368 /** a new section generates a page break */
369 bool m_startOnNewPage;
370 /** the first page */
371 int m_firstPage;
372 /** true if the first page is a title page(ie. no header/footer) */
373 bool m_hasTitlePage;
374 /** true if the header/footer are shared with previous sections */
375 bool m_continuousHF;
376 /** true if the left/right header/footer are different */
377 bool m_leftRightHF;
378 /** the header/footer id*/
379 int m_HFId[4];
380 /** a string to store unparsed data */
381 std::string m_extra;
382 };
383
384 /** internal class used to store a text zone */
385 struct TextZoneInfo {
386 //! constructor
387 TextZoneInfo()
388 : m_pos(0)
389 , m_N(0)
390 , m_extra("")
391 {
392 }
393 //! operator<<
394 friend std::ostream &operator<<(std::ostream &o, TextZoneInfo const &info)
395 {
396 o << "pos=" << info.m_pos << ",";
397 if (info.m_N >= 0) o << "size=" << info.m_N <<",";
398 if (info.m_extra.length()) o << info.m_extra;
399 return o;
400 }
401 long m_pos;
402 //! the number of character
403 int m_N;
404 //! extra data
405 std::string m_extra;
406 };
407
408 enum TokenType { TKN_UNKNOWN, TKN_FOOTNOTE, TKN_PAGENUMBER, TKN_GRAPHIC, TKN_FIELD };
409
410 /** Internal: class to store field definition: TOKN entry*/
411 struct Token {
412 //! constructor
413 Token()
414 : m_type(TKN_UNKNOWN)
415 , m_zoneId(-1)
416 , m_page(-1)
417 , m_descent(0)
418 , m_fieldEntry()
419 , m_extra("")
420 {
421 for (auto &unkn : m_unknown) unkn=0;
422 for (auto &size : m_size) size=0;
423 }
424 //! operator <<
425 friend std::ostream &operator<<(std::ostream &o, Token const &tok);
426 //! the type
427 TokenType m_type;
428 //! the zone id which correspond to this type
429 int m_zoneId;
430 //! the page
431 int m_page;
432 //! the size(?)
433 int m_size[2];
434 //! the descent
435 int m_descent;
436 //! the field name entry
437 MWAWEntry m_fieldEntry;
438 //! the unknown zone
439 int m_unknown[3];
440 //! a string used to store the parsing errors
441 std::string m_extra;
442 };
443 //! operator<< for Token
444 std::ostream &operator<<(std::ostream &o, Token const &tok)
445 {
446 switch (tok.m_type) {
447 case TKN_FOOTNOTE:
448 o << "footnoote,";
449 break;
450 case TKN_FIELD:
451 o << "field[linked],";
452 break;
453 case TKN_PAGENUMBER:
454 switch (tok.m_unknown[0]) {
455 case 0:
456 o << "field[pageNumber],";
457 break;
458 case 1:
459 o << "field[sectionNumber],";
460 break;
461 case 2:
462 o << "field[sectionInPageNumber],";
463 break;
464 case 3:
465 o << "field[pageCount],";
466 break;
467 default:
468 o << "field[pageNumber=#" << tok.m_unknown[0] << "],";
469 break;
470 }
471 break;
472 case TKN_GRAPHIC:
473 o << "graphic,";
474 break;
475 case TKN_UNKNOWN:
476 #if !defined(__clang__)
477 default:
478 #endif
479 o << "##field[unknown]" << ",";
480 break;
481 }
482 if (tok.m_zoneId != -1) o << "zoneId=" << tok.m_zoneId << ",";
483 if (tok.m_page != -1) o << "page?=" << tok.m_page << ",";
484 o << "pos?=" << tok.m_size[0] << "x" << tok.m_size[1] << ",";
485 if (tok.m_descent) o << "descent=" << tok.m_descent << ",";
486 for (int i = 0; i < 3; i++) {
487 if (tok.m_unknown[i] == 0 || (i==0 && tok.m_type==TKN_PAGENUMBER))
488 continue;
489 o << "#unkn" << i << "=" << std::hex << tok.m_unknown[i] << std::dec << ",";
490 }
491 if (!tok.m_extra.empty()) o << "err=[" << tok.m_extra << "]";
492 return o;
493 }
494
495 struct Zone final : public ClarisWksStruct::DSET {
496 //! constructor
497 explicit Zone(ClarisWksStruct::DSET const &dset = ClarisWksStruct::DSET())
498 : ClarisWksStruct::DSET(dset)
499 , m_zones()
500 , m_numChar(0)
501 , m_numTextZone(0)
502 , m_numParagInfo(0)
503 , m_numFont(0)
504 , m_fatherId(0)
505 , m_unknown(0)
506 , m_fontList()
507 , m_paragraphList()
508 , m_sectionList()
509 , m_tokenList()
510 , m_textZoneList()
511 , m_plcMap()
512 {
513 }
514 //!destructor
515 ~Zone() final;
516 //! operator<<
517 friend std::ostream &operator<<(std::ostream &o, Zone const &doc)
518 {
519 o << static_cast<ClarisWksStruct::DSET const &>(doc);
520 if (doc.m_numChar) o << "numChar=" << doc.m_numChar << ",";
521 if (doc.m_numTextZone) o << "numTextZone=" << doc.m_numTextZone << ",";
522 if (doc.m_numParagInfo) o << "numParag=" << doc.m_numParagInfo << ",";
523 if (doc.m_numFont) o << "numFont=" << doc.m_numFont << ",";
524 if (doc.m_fatherId) o << "id[father]=" << doc.m_fatherId << ",";
525 if (doc.m_unknown) o << "unkn=" << doc.m_unknown << ",";
526 return o;
527 }
528
529 /** remove a child from a list.
530
531 Normally, this function is not called, so optimizing it is not usefull
532 */
533 void removeChild(int cId, bool normalChild) final
534 {
535 DSET::removeChild(cId, normalChild);
536 for (auto &token : m_tokenList) {
537 if (token.m_zoneId!=cId) continue;
538 token.m_zoneId=0;
539 return;
540 }
541 // normally, section list point only to the text zone (ie. the
542 // child of the header/footer group), so remove child is not
543 // called on it
544 MWAW_DEBUG_MSG(("ClarisWksTextInternal::Zone can not detach %d\n", cId));
545 }
546
547 std::vector<MWAWEntry> m_zones; // the text zones
548 int m_numChar /** the number of char in text zone */;
549 int m_numTextZone /** the number of text zone ( ie. number of page ? ) */;
550 int m_numParagInfo /** the number of paragraph info */;
551 int m_numFont /** the number of font */;
552 int m_fatherId /** the father id */;
553 int m_unknown /** an unknown flags */;
554
555 std::vector<MWAWFont> m_fontList /** the list of fonts */;
556 std::vector<ParagraphPLC> m_paragraphList /** the list of paragraph */;
557 std::vector<Section> m_sectionList /** the list of section */;
558 std::vector<Token> m_tokenList /** the list of token */;
559 std::vector<TextZoneInfo> m_textZoneList /** the list of zone */;
560 std::multimap<long, PLC> m_plcMap /** the plc map */;
561 };
562
563 Zone::~Zone()
564 {
565 }
566 ////////////////////////////////////////
567 //! Internal: the state of a ClarisWksText
568 struct State {
569 //! constructor
570 State()
571 : m_version(-1)
572 , m_paragraphsList()
573 , m_zoneMap()
574 {
575 }
576
577 //! the file version
578 mutable int m_version;
579 //! the list of paragraph
580 std::vector<Paragraph> m_paragraphsList;
581 //! the list of text zone
582 std::map<int, std::shared_ptr<Zone> > m_zoneMap;
583 };
584
585 ////////////////////////////////////////
586 //! Internal: the subdocument of a ClarisWksDocument
587 class SubDocument final : public MWAWSubDocument
588 {
589 public:
590 SubDocument(ClarisWksText &parser, MWAWInputStreamPtr const &input, int zoneId)
591 : MWAWSubDocument(nullptr, input, MWAWEntry())
592 , m_textParser(parser)
593 , m_id(zoneId) {}
594
595 //! destructor
596 ~SubDocument() final {}
597
598 //! operator!=
599 bool operator!=(MWAWSubDocument const &doc) const final
600 {
601 if (MWAWSubDocument::operator!=(doc)) return true;
602 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
603 if (!sDoc) return true;
604 if (&m_textParser != &sDoc->m_textParser) return true;
605 if (m_id != sDoc->m_id) return true;
606 return false;
607 }
608
609 //! the parser function
610 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
611
612 protected:
613 //! the document manager
614 ClarisWksText &m_textParser;
615 //! the subdocument id
616 int m_id;
617 };
618
619 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType)
620 {
621 if (!listener.get()) {
622 MWAW_DEBUG_MSG(("ClarisWksTextInternal::SubDocument::parse: no listener\n"));
623 return;
624 }
625 if (m_id == -1) { // a number used to send linked frame
626 listener->insertChar(' ');
627 return;
628 }
629 if (m_id == 0) {
630 MWAW_DEBUG_MSG(("ClarisWksTextInternal::SubDocument::parse: unknown zone\n"));
631 return;
632 }
633
634 m_textParser.m_document.sendZone(m_id, listener);
635 }
636
637 }
638
639 ////////////////////////////////////////////////////////////
640 // constructor/destructor, ...
641 ////////////////////////////////////////////////////////////
642 ClarisWksText::ClarisWksText(ClarisWksDocument &document)
643 : m_document(document)
644 , m_parserState(document.m_parserState)
645 , m_state(new ClarisWksTextInternal::State)
646 , m_mainParser(&document.getMainParser())
647 {
648 }
649
650 ClarisWksText::~ClarisWksText()
651 {
652 }
653
654 int ClarisWksText::version() const
655 {
656 if (m_state->m_version < 0)
657 m_state->m_version = m_parserState->m_version;
658 return m_state->m_version;
659 }
660
661 int ClarisWksText::numPages() const
662 {
663 auto iter = m_state->m_zoneMap.find(1);
664 if (iter == m_state->m_zoneMap.end())
665 return 1;
666 int numPage = 1;
667 MWAWInputStreamPtr &input= m_parserState->m_input;
668 long pos = input->tell();
669 for (auto const &entry : iter->second->m_zones) {
670 input->seek(entry.begin()+4, librevenge::RVNG_SEEK_SET);
671 auto numC = int(entry.length()-4);
672 for (int ch = 0; ch < numC; ch++) {
673 auto c = char(input->readULong(1));
674 if (c==0xb || c==0x1)
675 numPage++;
676 }
677 }
678 input->seek(pos, librevenge::RVNG_SEEK_SET);
679 return numPage;
680 }
681
682 bool ClarisWksText::updatePageSpanList(MWAWPageSpan const &page, std::vector<MWAWPageSpan> &spanList)
683 {
684 auto it = m_state->m_zoneMap.find(1);
685 if (it==m_state->m_zoneMap.end() || !it->second || m_parserState->m_kind==MWAWDocument::MWAW_K_PRESENTATION)
686 return false;
687 auto const &zone=*it->second;
688 size_t numSection=zone.m_sectionList.size();
689 if (!numSection) return false;
690 int nPages=m_document.numPages();
691 int actPage=0;
692 spanList.resize(0);
693 for (size_t i=0; i<numSection; ++i) {
694 auto const &sec=zone.m_sectionList[i];
695 int lastPage=nPages;
696 bool ok=true;
697 while (i+1<numSection) {
698 if (zone.m_sectionList[i+1].m_continuousHF) {
699 ++i;
700 continue;
701 }
702 if (zone.m_sectionList[i+1].m_firstPage<actPage) {
703 MWAW_DEBUG_MSG(("ClarisWksText::updatePageSpanList: problem with the %d first page\n", int(i+1)));
704 ok=false;
705 break;
706 }
707 lastPage=zone.m_sectionList[i+1].m_firstPage;
708 break;
709 }
710 if (!ok)
711 break;
712 if (lastPage>nPages) {
713 MWAW_DEBUG_MSG(("ClarisWksText::updatePageSpanList: some first page seems to big\n"));
714 lastPage=nPages;
715 }
716 if (sec.m_hasTitlePage && actPage<lastPage) {
717 // title page have no header/footer
718 MWAWPageSpan ps(page);
719 ps.setPageSpan(1);
720 spanList.push_back(ps);
721 ++actPage;
722 }
723 if (actPage<lastPage) {
724 MWAWPageSpan ps(page);
725 ps.setPageSpan(lastPage-actPage);
726 for (int j=0; j<4; ++j) {
727 int zId=sec.m_HFId[j];
728 if (!zId) continue;
729 if ((j%2)==1 && zId==sec.m_HFId[j-1]) continue;
730 /* try to retrieve the father group zone */
731 auto fIt=m_state->m_zoneMap.find(zId);
732 if (fIt!=m_state->m_zoneMap.end() && fIt->second && fIt->second->m_fatherId)
733 zId=fIt->second->m_fatherId;
734 MWAWHeaderFooter hF(j<2 ? MWAWHeaderFooter::HEADER : MWAWHeaderFooter::FOOTER,
735 (j%2) ? MWAWHeaderFooter::EVEN : sec.m_HFId[j]==sec.m_HFId[j+1] ?
736 MWAWHeaderFooter::ALL : MWAWHeaderFooter::ODD);
737 hF.m_subDocument.reset(new ClarisWksTextInternal::SubDocument(*this, m_parserState->m_input, zId));
738 ps.setHeaderFooter(hF);
739 }
740 spanList.push_back(ps);
741 }
742 actPage=lastPage;
743 }
744 if (actPage<nPages) {
745 MWAWPageSpan ps(page);
746 ps.setPageSpan(nPages-actPage);
747 spanList.push_back(ps);
748 }
749 return true;
750 }
751
752 ////////////////////////////////////////////////////////////
753 // Intermediate level
754 ////////////////////////////////////////////////////////////
755
756 ////////////////////////////////////////////////////////////
757 // a document part
758 ////////////////////////////////////////////////////////////
759 std::shared_ptr<ClarisWksStruct::DSET> ClarisWksText::readDSETZone(ClarisWksStruct::DSET const &zone, MWAWEntry const &entry, bool &complete)
760 {
761 complete = false;
762 if (!entry.valid() || zone.m_fileType != 1)
763 return std::shared_ptr<ClarisWksStruct::DSET>();
764 int const vers = version();
765 long pos = entry.begin();
766 MWAWInputStreamPtr &input= m_parserState->m_input;
767 input->seek(pos+8+16, librevenge::RVNG_SEEK_SET); // avoid header+8 generic number
768 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
769 libmwaw::DebugStream f;
770 f << "Entries(DSETT):";
771
772 std::shared_ptr<ClarisWksTextInternal::Zone> textZone(new ClarisWksTextInternal::Zone(zone));
773 textZone->m_unknown = static_cast<int>(input->readULong(2)); // alway 0 ?
774 textZone->m_fatherId = static_cast<int>(input->readULong(2));
775 textZone->m_numChar = static_cast<int>(input->readULong(4));
776 textZone->m_numTextZone = static_cast<int>(input->readULong(2));
777 textZone->m_numParagInfo = static_cast<int>(input->readULong(2));
778 textZone->m_numFont = static_cast<int>(input->readULong(2));
779 switch (textZone->m_textType >> 4) {
780 case 2:
781 textZone->m_position = ClarisWksStruct::DSET::P_Header;
782 break;
783 case 4:
784 textZone->m_position = ClarisWksStruct::DSET::P_Footer;
785 break;
786 case 6:
787 textZone->m_position = ClarisWksStruct::DSET::P_Footnote;
788 break;
789 case 8:
790 textZone->m_position = ClarisWksStruct::DSET::P_Frame;
791 break;
792 case 0xe:
793 textZone->m_position = ClarisWksStruct::DSET::P_Table;
794 break;
795 case 0:
796 if (zone.m_id==1) {
797 textZone->m_position = ClarisWksStruct::DSET::P_Main;
798 break;
799 }
800 MWAW_FALLTHROUGH;
801 default:
802 MWAW_DEBUG_MSG(("ClarisWksText::readDSETZone: find unknown position %d\n", (textZone->m_textType >> 4)));
803 f << "#position="<< (textZone->m_textType >> 4) << ",";
804 break;
805 }
806 // find 2,3,6,a,b,e,f
807 if (textZone->m_textType != ClarisWksStruct::DSET::P_Unknown)
808 textZone->m_textType &= 0xF;
809 f << *textZone << ",";
810
811 if (long(input->tell())%2)
812 input->seek(1, librevenge::RVNG_SEEK_CUR);
813 ascFile.addDelimiter(input->tell(), '|');
814 ascFile.addPos(pos);
815 ascFile.addNote(f.str().c_str());
816
817 // read the last part
818 int data0Length = 0;
819 switch (vers) {
820 case 1:
821 data0Length = 24;
822 break;
823 case 2:
824 data0Length = 28;
825 break;
826 // case 3: ???
827 case 4:
828 case 5:
829 case 6:
830 data0Length = 30;
831 break;
832 default:
833 break;
834 }
835
836 auto N = int(zone.m_numData);
837 if (long(input->tell())+N*data0Length > entry.end()) {
838 MWAW_DEBUG_MSG(("ClarisWksText::readDSETZone: file is too short\n"));
839 return std::shared_ptr<ClarisWksStruct::DSET>();
840 }
841
842 input->seek(entry.end()-N*data0Length, librevenge::RVNG_SEEK_SET);
843 ClarisWksTextInternal::PLC plc;
844 plc.m_type = ClarisWksTextInternal::P_Child;
845 int numExtraHId=0;
846 if (data0Length) {
847 for (int i = 0; i < N; i++) {
848 /* definition of a list of text zone ( one by column and one by page )*/
849 pos = input->tell();
850 f.str("");
851 f << "DSETT-" << i << ":";
852 ClarisWksStruct::DSET::Child child;
853 child.m_posC = long(input->readULong(4));
854 child.m_type = ClarisWksStruct::DSET::C_SubText;
855 int dim[2];
856 for (auto &d : dim) d = static_cast<int>(input->readLong(2));
857 child.m_box = MWAWBox2f(MWAWVec2f(0,0), MWAWVec2f(float(dim[0]), float(dim[1])));
858 textZone->m_childs.push_back(child);
859 plc.m_id = i;
860 textZone->m_plcMap.insert(std::map<long, ClarisWksTextInternal::PLC>::value_type(child.m_posC, plc));
861
862 f << child;
863 f << "ptr=" << std::hex << input->readULong(4) << std::dec << ",";
864 f << "f0=" << input->readLong(2) << ","; // a small number : number of line ?
865 f << "y[real]=" << input->readLong(2) << ",";
866 for (int j = 1; j < 4; j++) {
867 auto val = static_cast<int>(input->readLong(2));
868 if (val)
869 f << "f" << j << "=" << val << ",";
870 }
871 auto order = static_cast<int>(input->readLong(2));
872 // simple id or 0: main text ?, 1 : header/footnote ?, 2: footer => id or order?
873 if (order)
874 f << "order?=" << order << ",";
875
876 if (vers>=2) {
877 long id=static_cast<int>(input->readULong(4));
878 if (id) {
879 f << "ID=" << std::hex << id << std::dec << ",";
880 ++numExtraHId;
881 }
882 }
883 long actPos = input->tell();
884 if (actPos != pos && actPos != pos+data0Length)
885 ascFile.addDelimiter(input->tell(),'|');
886 input->seek(pos+data0Length, librevenge::RVNG_SEEK_SET);
887
888 ascFile.addPos(pos);
889 ascFile.addNote(f.str().c_str());
890 }
891 }
892
893 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
894
895 // now normally three zones: paragraph, font, token
896 bool ok = true;
897 for (int z = 0; z < 4+textZone->m_numTextZone; z++) {
898 pos = input->tell();
899 auto sz = long(input->readULong(4));
900 if (!sz) {
901 f.str("");
902 f << "DSETT-Z" << z;
903 ascFile.addPos(pos);
904 ascFile.addNote(f.str().c_str());
905 continue;
906 }
907
908 MWAWEntry zEntry;
909 zEntry.setBegin(pos);
910 zEntry.setLength(sz+4);
911
912 if (!input->checkPosition(zEntry.end())) {
913 MWAW_DEBUG_MSG(("ClarisWksText::readDSETZone: entry for %d zone is too short\n", z));
914 ascFile.addPos(pos);
915 ascFile.addNote("###");
916 input->seek(pos, librevenge::RVNG_SEEK_SET);
917 if (z > 4) {
918 ok = false;
919 break;
920 }
921 return textZone;
922 }
923
924 switch (z) {
925 case 0:
926 ok = readParagraphs(zEntry, *textZone);
927 break;
928 case 1:
929 ok = readFonts(zEntry, *textZone);
930 break;
931 case 2:
932 ok = readTokens(zEntry, *textZone);
933 break;
934 case 3:
935 ok = readTextZoneSize(zEntry, *textZone);
936 break;
937 default:
938 textZone->m_zones.push_back(zEntry);
939 break;
940 }
941 if (!ok) {
942 if (z >= 4) {
943 input->seek(pos, librevenge::RVNG_SEEK_SET);
944 MWAW_DEBUG_MSG(("ClarisWksText::readDSETZone: can not find text %d zone\n", z-4));
945 if (z > 4) break;
946 return textZone;
947 }
948 f.str("");
949 f << "DSETT-Z" << z << "#";
950 ascFile.addPos(pos);
951 ascFile.addNote(f.str().c_str());
952 }
953 if (input->tell() < zEntry.end() || !ok)
954 input->seek(zEntry.end(), librevenge::RVNG_SEEK_SET);
955 }
956
957 if (ok && vers >= 2) {
958 pos = input->tell();
959 if (!readTextSection(*textZone))
960 input->seek(pos, librevenge::RVNG_SEEK_SET);
961 }
962 for (int i=0; ok && i<numExtraHId; ++i) {
963 pos=input->tell();
964 auto sz=long(input->readULong(4));
965 if (sz<10 || !input->checkPosition(pos+4+sz)) {
966 MWAW_DEBUG_MSG(("ClarisWksText::readDSETZone:: can not read an extra block\n"));
967 ascFile.addPos(pos);
968 ascFile.addNote("DSETT-extra:###");
969 input->seek(pos, librevenge::RVNG_SEEK_SET);
970 ok=false;
971 break;
972 }
973 f.str("");
974 f << "DSETT-extra:";
975 /* Checkme: no sure how to read this unfrequent structures */
976 auto val=static_cast<int>(input->readLong(2)); // 2 (with size=34 or 4c)|a(with size=a or e)|3c (with size 3c)
977 f << "type?=" << val << ",";
978 int dim[4];
979 for (auto &d : dim) d=static_cast<int>(input->readLong(2));
980 f << "dim=" << dim[1] << "x" << dim[0] << "<->" << dim[3] << "x" << dim[2] << ",";
981 if (sz!=10) ascFile.addDelimiter(input->tell(),'|');
982 ascFile.addPos(pos);
983 ascFile.addNote(f.str().c_str());
984 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
985 }
986 for (auto const &token : textZone->m_tokenList) {
987 if (token.m_zoneId > 0)
988 textZone->m_otherChilds.push_back(token.m_zoneId);
989 }
990
991 if (m_state->m_zoneMap.find(textZone->m_id) != m_state->m_zoneMap.end()) {
992 MWAW_DEBUG_MSG(("ClarisWksText::readDSETZone: zone %d already exists!!!\n", textZone->m_id));
993 }
994 else
995 m_state->m_zoneMap[textZone->m_id] = textZone;
996
997 if (ok) {
998 // look for unparsed zone
999 pos=input->tell();
1000 auto sz=long(input->readULong(4));
1001 if (input->checkPosition(pos+4+sz)) {
1002 if (sz) {
1003 MWAW_DEBUG_MSG(("ClarisWksText::readDSETZone:: find some extra block\n"));
1004 input->seek(pos+4+sz, librevenge::RVNG_SEEK_SET);
1005 ascFile.addPos(pos);
1006 ascFile.addNote("Entries(TextEnd):###");
1007 }
1008 else {
1009 // probably a problem, but...
1010 ascFile.addPos(pos);
1011 ascFile.addNote("_");
1012 }
1013 }
1014 else
1015 input->seek(pos, librevenge::RVNG_SEEK_SET);
1016 }
1017 complete = ok;
1018 return textZone;
1019 }
1020
1021 ////////////////////////////////////////////////////////////
1022 //
1023 // Low level
1024 //
1025 ////////////////////////////////////////////////////////////
1026
1027 ////////////////////////////////////////////////////////////
1028 // the fonts properties
1029 ////////////////////////////////////////////////////////////
1030 bool ClarisWksText::readFonts(MWAWEntry const &entry, ClarisWksTextInternal::Zone &zone)
1031 {
1032 long pos = entry.begin();
1033
1034 int fontSize = 0;
1035 switch (version()) {
1036 case 1:
1037 case 2:
1038 case 3:
1039 fontSize = 10;
1040 break;
1041 case 4:
1042 case 5:
1043 fontSize = 12;
1044 break;
1045 case 6:
1046 fontSize = 18;
1047 break;
1048 default:
1049 break;
1050 }
1051 if (fontSize == 0)
1052 return false;
1053 if ((entry.length()%fontSize) != 4)
1054 return false;
1055
1056 auto numElt = int((entry.length()-4)/fontSize);
1057 long actC = -1;
1058
1059 MWAWInputStreamPtr &input= m_parserState->m_input;
1060 input->seek(pos+4, librevenge::RVNG_SEEK_SET); // skip header
1061 // first check char pos is ok
1062 for (int i = 0; i < numElt; i++) {
1063 pos = input->tell();
1064 auto newC = long(input->readULong(4));
1065 if (newC < actC) return false;
1066 actC = newC;
1067 input->seek(pos+fontSize, librevenge::RVNG_SEEK_SET);
1068 }
1069
1070 pos = entry.begin();
1071 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1072 ascFile.addPos(pos);
1073 ascFile.addNote("Entries(Font)");
1074
1075 input->seek(pos+4, librevenge::RVNG_SEEK_SET); // skip header
1076 ClarisWksTextInternal::PLC plc;
1077 plc.m_type = ClarisWksTextInternal::P_Font;
1078 for (int i = 0; i < numElt; i++) {
1079 MWAWFont font;
1080 int posChar;
1081 if (!m_document.getStyleManager()->readFontAndPos(i, posChar, font)) return false;
1082 zone.m_fontList.push_back(font);
1083 plc.m_id = i;
1084 zone.m_plcMap.insert(std::map<long, ClarisWksTextInternal::PLC>::value_type(posChar, plc));
1085 }
1086
1087 return true;
1088 }
1089
1090 ////////////////////////////////////////////////////////////
1091 // the paragraphs properties
1092 ////////////////////////////////////////////////////////////
1093 bool ClarisWksText::readParagraphs(MWAWEntry const &entry, ClarisWksTextInternal::Zone &zone)
1094 {
1095 long pos = entry.begin();
1096
1097 int const vers = version();
1098 int styleSize = vers==1 ? 6 : 8;
1099 if ((entry.length()%styleSize) != 4)
1100 return false;
1101
1102 auto numElt = int((entry.length()-4)/styleSize);
1103 long actC = -1;
1104
1105 MWAWInputStreamPtr &input= m_parserState->m_input;
1106 input->seek(pos+4, librevenge::RVNG_SEEK_SET); // skip header
1107 // first check char pos is ok
1108 for (int i = 0; i < numElt; i++) {
1109 pos = input->tell();
1110 auto newC = long(input->readULong(4));
1111 if (newC < actC) return false;
1112 actC = newC;
1113 input->seek(pos+styleSize, librevenge::RVNG_SEEK_SET);
1114 }
1115
1116 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1117 pos = entry.begin();
1118 ascFile.addPos(pos);
1119 ascFile.addNote("Entries(ParaPLC)");
1120
1121 libmwaw::DebugStream f;
1122 input->seek(pos+4, librevenge::RVNG_SEEK_SET); // skip header
1123 ClarisWksTextInternal::PLC plc;
1124 plc.m_type = ClarisWksTextInternal::P_Ruler;
1125 for (int i = 0; i < numElt; i++) {
1126 pos = input->tell();
1127 ClarisWksTextInternal::ParagraphPLC info;
1128
1129 auto posC = long(input->readULong(4));
1130 f.str("");
1131 f << "ParaPLC-R" << i << ": pos=" << posC << ",";
1132 info.m_rulerId = static_cast<int>(input->readLong(2));
1133 if (styleSize >= 8)
1134 info.m_flags = static_cast<int>(input->readLong(2));
1135
1136 if (vers > 2) {
1137 info.m_styleId = info.m_rulerId;
1138 ClarisWksStyleManager::Style style;
1139 if (m_document.getStyleManager()->get(info.m_rulerId, style)) {
1140 info.m_rulerId = style.m_rulerId;
1141 #if 0
1142 f << "[style=" << style << "]";
1143 #endif
1144 }
1145 }
1146 f << info;
1147
1148 if (long(input->tell()) != pos+styleSize)
1149 ascFile.addDelimiter(input->tell(), '|');
1150 zone.m_paragraphList.push_back(info);
1151 plc.m_id = i;
1152 zone.m_plcMap.insert(std::map<long, ClarisWksTextInternal::PLC>::value_type(posC, plc));
1153 input->seek(pos+styleSize, librevenge::RVNG_SEEK_SET);
1154 ascFile.addPos(pos);
1155 ascFile.addNote(f.str().c_str());
1156 }
1157
1158 return true;
1159 }
1160
1161 ////////////////////////////////////////////////////////////
1162 // zone which corresponds to the token
1163 ////////////////////////////////////////////////////////////
1164 bool ClarisWksText::readTokens(MWAWEntry const &entry, ClarisWksTextInternal::Zone &zone)
1165 {
1166 long pos = entry.begin();
1167
1168 int dataSize = 0;
1169 int const vers=version();
1170 switch (vers) {
1171 case 1:
1172 case 2:
1173 case 3:
1174 case 4:
1175 case 5:
1176 dataSize = 32;
1177 break;
1178 case 6:
1179 dataSize = 36;
1180 break;
1181 default:
1182 break;
1183 }
1184 if (!dataSize || (entry.length()%dataSize) != 4)
1185 return false;
1186
1187 MWAWInputStreamPtr &input= m_parserState->m_input;
1188 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1189 ascFile.addPos(pos);
1190 ascFile.addNote("Entries(Token)");
1191
1192 auto numElt = int((entry.length()-4)/dataSize);
1193 input->seek(pos+4, librevenge::RVNG_SEEK_SET); // skip header
1194
1195 libmwaw::DebugStream f;
1196 ClarisWksTextInternal::PLC plc;
1197 plc.m_type = ClarisWksTextInternal::P_Token;
1198 int val;
1199 std::vector<int> fieldList;
1200 for (int i = 0; i < numElt; i++) {
1201 pos = input->tell();
1202
1203 auto posC = static_cast<int>(input->readULong(4));
1204 ClarisWksTextInternal::Token token;
1205
1206 auto type = static_cast<int>(input->readLong(2));
1207 f.str("");
1208 switch (type) {
1209 case 0:
1210 token.m_type = ClarisWksTextInternal::TKN_FOOTNOTE;
1211 break;
1212 case 1:
1213 token.m_type = ClarisWksTextInternal::TKN_GRAPHIC;
1214 break;
1215 case 2:
1216 /* find in v4-v6, does not seem to exist in v1-v2 */
1217 token.m_type = ClarisWksTextInternal::TKN_PAGENUMBER;
1218 break;
1219 case 3:
1220 token.m_type = ClarisWksTextInternal::TKN_FIELD;
1221 fieldList.push_back(i);
1222 break;
1223 default:
1224 f << "#type=" << type << ",";
1225 break;
1226 }
1227
1228 token.m_unknown[0] = static_cast<int>(input->readLong(2));
1229 token.m_zoneId = static_cast<int>(input->readLong(2));
1230 token.m_unknown[1] = static_cast<int>(input->readLong(1));
1231 token.m_page = static_cast<int>(input->readLong(1));
1232 token.m_unknown[2] = static_cast<int>(input->readLong(2));
1233 for (int j = 0; j < 2; j++)
1234 token.m_size[1-j] = static_cast<int>(input->readLong(2));
1235 for (int j = 0; j < 3; j++) {
1236 val = static_cast<int>(input->readLong(2));
1237 if (val) f << "f" << j << "=" << val << ",";
1238 }
1239 val = static_cast<int>(input->readLong(2));
1240 if (vers>=6) // checkme: ok for v6 & graphic, not for v2
1241 token.m_descent = val;
1242 else if (val)
1243 f << "f3=" << val << ",";
1244 token.m_extra = f.str();
1245 f.str("");
1246 f << "Token-" << i << ": pos=" << posC << "," << token;
1247 zone.m_tokenList.push_back(token);
1248 plc.m_id = i;
1249 zone.m_plcMap.insert(std::map<long, ClarisWksTextInternal::PLC>::value_type(posC, plc));
1250
1251 if (long(input->tell()) != pos && long(input->tell()) != pos+dataSize)
1252 ascFile.addDelimiter(input->tell(), '|');
1253 ascFile.addPos(pos);
1254 ascFile.addNote(f.str().c_str());
1255 input->seek(pos+dataSize, librevenge::RVNG_SEEK_SET);
1256 }
1257
1258 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1259 for (size_t i=0; i < fieldList.size(); ++i) {
1260 pos=input->tell();
1261 auto sz=long(input->readULong(4));
1262 f.str("");
1263 f << "Token[field-" << i << "]:";
1264 if (!input->checkPosition(pos+sz+4) || long(input->readULong(1))+1!=sz) {
1265 MWAW_DEBUG_MSG(("ClarisWksText::readTokens: can find token field name %d\n", int(i)));
1266 input->seek(pos, librevenge::RVNG_SEEK_SET);
1267 f << "###";
1268 ascFile.addPos(pos);
1269 ascFile.addNote(f.str().c_str());
1270 return false;
1271 }
1272 MWAWEntry fieldEntry;
1273 fieldEntry.setBegin(input->tell());
1274 fieldEntry.setEnd(pos+sz+4);
1275 if (size_t(fieldList[i])<zone.m_tokenList.size())
1276 zone.m_tokenList[size_t(fieldList[i])].m_fieldEntry=fieldEntry;
1277 else {
1278 MWAW_DEBUG_MSG(("ClarisWksText::readTokens: oops the token id seems bad\n"));
1279 }
1280 input->seek(fieldEntry.end(), librevenge::RVNG_SEEK_SET);
1281 f << "###id";
1282 ascFile.addPos(pos);
1283 ascFile.addNote(f.str().c_str());
1284 }
1285 return true;
1286 }
1287
1288 // read the different section definition
1289 bool ClarisWksText::readTextSection(ClarisWksTextInternal::Zone &zone)
1290 {
1291 int const vers = version();
1292 MWAWInputStreamPtr &input= m_parserState->m_input;
1293 long pos = input->tell();
1294 ClarisWksStruct::Struct header;
1295 if (!header.readHeader(input,true)) {
1296 input->seek(pos, librevenge::RVNG_SEEK_SET);
1297 MWAW_DEBUG_MSG(("ClarisWksText::readTextSection: unexpected size\n"));
1298 return false;
1299 }
1300 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1301 if (header.m_size == 0) {
1302 ascFile.addPos(pos);
1303 ascFile.addNote("Nop");
1304 return true;
1305 }
1306 long endPos =pos+4+header.m_size;
1307 libmwaw::DebugStream f;
1308 f << "Entries(TextSection):";
1309
1310 if ((vers > 3 && header.m_dataSize != 0x4e) || (vers <= 3 && header.m_dataSize < 60)) {
1311 f << "###";
1312 ascFile.addPos(pos);
1313 ascFile.addNote(f.str().c_str());
1314 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1315 MWAW_DEBUG_MSG(("ClarisWksText::readTextSection: unexpected size\n"));
1316 return true;
1317 }
1318 if (header.m_headerSize) {
1319 ascFile.addDelimiter(input->tell(), '|');
1320 input->seek(header.m_headerSize, librevenge::RVNG_SEEK_CUR);
1321 }
1322 ascFile.addPos(pos);
1323 ascFile.addNote(f.str().c_str());
1324
1325 ClarisWksTextInternal::PLC plc;
1326 plc.m_type = ClarisWksTextInternal::P_Section;
1327 for (long i= 0; i < header.m_numData; i++) {
1328 ClarisWksTextInternal::Section sec;
1329
1330 pos = input->tell();
1331 f.str("");
1332 sec.m_pos = input->readLong(4);
1333 sec.m_firstPage= static_cast<int>(input->readLong(2));
1334 for (int j = 0; j < 3; j++) {
1335 /** find f0=O| (for second section)[1|2|4]
1336 f1=0| (for second section [2e,4e,5b] , f2=0|2d|4d|5a */
1337 auto val = int(input->readLong(2));
1338 if (val) f << "f" << j << "=" << val << ",";
1339 }
1340 sec.m_numColumns = static_cast<int>(input->readULong(2));
1341 if (!sec.m_numColumns || sec.m_numColumns > 10) {
1342 MWAW_DEBUG_MSG(("ClarisWksText::readTextSection: num columns seems odd\n"));
1343 f << "#numColumns=" << sec.m_numColumns << ",";
1344 sec.m_numColumns = 1;
1345 }
1346 for (int c = 0; c < sec.m_numColumns; c++)
1347 sec.m_columnsWidth.push_back(static_cast<int>(input->readULong(2)));
1348 input->seek(pos+32, librevenge::RVNG_SEEK_SET);
1349 for (int c = 0; c < sec.m_numColumns; c++)
1350 sec.m_columnsSep.push_back(static_cast<int>(input->readLong(2)));
1351 input->seek(pos+52, librevenge::RVNG_SEEK_SET);
1352 auto val = static_cast<int>(input->readULong(2));
1353 switch ((val&3)) {
1354 case 1:
1355 f << "newPage[begin],";
1356 break;
1357 case 2: // checkme
1358 f << "leftPage[begin],";
1359 break;
1360 case 3: // checkme
1361 f << "rightPage[begin],";
1362 break;
1363 case 0: // begin on new line
1364 default:
1365 break;
1366 }
1367 sec.m_startOnNewPage=(val&3)!=0;
1368 val &=0xFFFC;
1369 if (val) f << "g0=" << std::hex << val << std::dec << ",";
1370 val = static_cast<int>(input->readULong(2)); // 0|1
1371 if (val) f << "g1=" << std::hex << val << std::dec << ",";
1372 val = static_cast<int>(input->readULong(2)); // 0 or 1
1373 sec.m_hasTitlePage=(val&1);
1374 val &= 0xFFFE;
1375 if (val) f << "g2=" << std::hex << val << std::dec << ",";
1376
1377 val = static_cast<int>(input->readULong(2)); // 0 or 100
1378 sec.m_continuousHF=(val&0x100);
1379 sec.m_leftRightHF=(val&1);
1380 val &= 0xFEFE;
1381 if (val) f << "g3=" << std::hex << val << std::dec << ",";
1382 val = static_cast<int>(input->readULong(2)); // 0 ?
1383 if (val) f << "g4=" << std::hex << val << std::dec << ",";
1384 int prevHFId=0;
1385 for (int &j : sec.m_HFId) {
1386 auto hFId=static_cast<int>(input->readLong(4));
1387 j=hFId;
1388 if (!hFId || prevHFId==hFId) continue;
1389 zone.m_otherChilds.push_back(hFId);
1390 prevHFId=hFId;
1391 }
1392 sec.m_extra = f.str();
1393 zone.m_sectionList.push_back(sec);
1394 plc.m_id = int(i);
1395 zone.m_plcMap.insert(std::map<long, ClarisWksTextInternal::PLC>::value_type(sec.m_pos, plc));
1396 f.str("");
1397 f << "TextSection-S" << i << ":" << sec;
1398 if (input->tell() != pos+header.m_dataSize)
1399 ascFile.addDelimiter(input->tell(), '|');
1400 ascFile.addPos(pos);
1401 ascFile.addNote(f.str().c_str());
1402 input->seek(pos+header.m_dataSize, librevenge::RVNG_SEEK_SET);
1403 }
1404
1405 return true;
1406 }
1407
1408 ////////////////////////////////////////////////////////////
1409 // read the different size for the text
1410 ////////////////////////////////////////////////////////////
1411 bool ClarisWksText::readTextZoneSize(MWAWEntry const &entry, ClarisWksTextInternal::Zone &zone)
1412 {
1413 long pos = entry.begin();
1414
1415 int dataSize = 10;
1416 if ((entry.length()%dataSize) != 4)
1417 return false;
1418
1419 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1420 libmwaw::DebugStream f;
1421 ascFile.addPos(pos);
1422 ascFile.addNote("Entries(TextZoneSz)");
1423
1424 auto numElt = int((entry.length()-4)/dataSize);
1425
1426 MWAWInputStreamPtr &input= m_parserState->m_input;
1427 input->seek(pos+4, librevenge::RVNG_SEEK_SET); // skip header
1428
1429 ClarisWksTextInternal::PLC plc;
1430 plc.m_type = ClarisWksTextInternal::P_TextZone;
1431 for (int i = 0; i < numElt; i++) {
1432 pos = input->tell();
1433 f.str("");
1434 f << "TextZoneSz-" << i << ":";
1435 ClarisWksTextInternal::TextZoneInfo info;
1436 info.m_pos = long(input->readULong(4));
1437 info.m_N = static_cast<int>(input->readULong(2));
1438 f << info;
1439 zone.m_textZoneList.push_back(info);
1440 plc.m_id = i;
1441 zone.m_plcMap.insert(std::map<long, ClarisWksTextInternal::PLC>::value_type(info.m_pos, plc));
1442
1443 if (long(input->tell()) != pos+dataSize)
1444 ascFile.addDelimiter(input->tell(), '|');
1445 ascFile.addPos(pos);
1446 ascFile.addNote(f.str().c_str());
1447 input->seek(pos+dataSize, librevenge::RVNG_SEEK_SET);
1448 }
1449
1450 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1451 return true;
1452 }
1453
1454 bool ClarisWksText::canSendTextAsGraphic(ClarisWksTextInternal::Zone const &zone) const
1455 {
1456 size_t numSection=zone.m_sectionList.size();
1457 if (numSection>1) return false;
1458 if (numSection==1 && zone.m_sectionList[0].m_numColumns>1)
1459 return false;
1460 for (auto const &tok : zone.m_tokenList) {
1461 if (tok.m_type!=ClarisWksTextInternal::TKN_UNKNOWN &&
1462 tok.m_type!=ClarisWksTextInternal::TKN_PAGENUMBER &&
1463 tok.m_type!=ClarisWksTextInternal::TKN_FIELD)
1464 return false;
1465 }
1466 return true;
1467 }
1468
1469 bool ClarisWksText::sendText(ClarisWksTextInternal::Zone const &zone, MWAWListenerPtr listener)
1470 {
1471 zone.m_parsed=true;
1472 bool localListener=false;
1473 if (listener)
1474 localListener=true;
1475 else
1476 listener=m_parserState->getMainListener();
1477 if (!listener || !listener->canWriteText()) {
1478 MWAW_DEBUG_MSG(("ClarisWksText::sendText: can not find a listener\n"));
1479 return false;
1480 }
1481 // Removeme when all is ok
1482 if (listener->isParagraphOpened())
1483 listener->insertEOL();
1484 long actC = 0;
1485 bool main = zone.m_id == 1;
1486 auto numParaPLC = int(zone.m_paragraphList.size());
1487 auto numParagraphs = int(m_state->m_paragraphsList.size());
1488 int actPage = 1;
1489 size_t numZones = zone.m_zones.size();
1490 if (main) {
1491 if (!localListener)
1492 m_document.newPage(actPage);
1493 else {
1494 MWAW_DEBUG_MSG(("ClarisWksText::sendText: try to send main zone as graphic\n"));
1495 main=false;
1496 }
1497 }
1498 int numCols = 1;
1499 int numSection = 0, numSectionInPage=0;
1500 size_t nextSection = 0;
1501 long nextSectionPos = main ? 0 : -1;
1502 if (zone.m_sectionList.size())
1503 nextSectionPos = zone.m_sectionList[0].m_pos;
1504 int actListId=-1;
1505 long actListCPos=-1;
1506 MWAWInputStreamPtr &input= m_parserState->m_input;
1507 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1508 std::multimap<long, ClarisWksTextInternal::PLC>::const_iterator plcIt;
1509 for (size_t z = 0; z < numZones; z++) {
1510 MWAWEntry const &entry = zone.m_zones[z];
1511 long pos = entry.begin();
1512 libmwaw::DebugStream f, f2;
1513
1514 auto numC = int(entry.length()-4);
1515 bool lastIsSectionBreak=false;
1516 input->seek(pos+4, librevenge::RVNG_SEEK_SET); // skip header
1517
1518 for (int i = 0; i < numC; i++) {
1519 if (nextSectionPos>=0 && actC >= nextSectionPos) {
1520 if (actC != nextSectionPos) {
1521 MWAW_DEBUG_MSG(("ClarisWksText::sendText: find a section inside a complex char!!!\n"));
1522 f << "###";
1523 }
1524 numSection++;
1525 numSectionInPage++;
1526 MWAWSection section;
1527 if (nextSection < zone.m_sectionList.size()) {
1528 section = zone.m_sectionList[nextSection].getSection();
1529 if (main && lastIsSectionBreak && zone.m_sectionList[nextSection].m_startOnNewPage)
1530 m_document.newPage(++actPage);
1531 if (++nextSection < zone.m_sectionList.size())
1532 nextSectionPos = zone.m_sectionList[nextSection].m_pos;
1533 else
1534 nextSectionPos = -1;
1535 }
1536 else {
1537 section=m_document.getMainSection();
1538 nextSectionPos = -1;
1539 }
1540 numCols = section.numColumns();
1541 int actCols = localListener ? 1 : listener->getSection().numColumns();
1542 if (numCols > 1 || actCols > 1) {
1543 if (listener->isSectionOpened())
1544 listener->closeSection();
1545 listener->openSection(section);
1546 }
1547 }
1548 else if (numSectionInPage==0)
1549 numSectionInPage++;
1550 plcIt = zone.m_plcMap.find(actC);
1551 bool seeToken = false;
1552 while (plcIt != zone.m_plcMap.end() && plcIt->first<=actC) {
1553 if (actC != plcIt->first) {
1554 MWAW_DEBUG_MSG(("ClarisWksText::sendText: find a plc inside a complex char!!!\n"));
1555 f << "###";
1556 }
1557 auto const &plc = plcIt++->second;
1558 f << "[" << plc << "]";
1559 switch (plc.m_type) {
1560 case ClarisWksTextInternal::P_Font:
1561 if (plc.m_id < 0 || plc.m_id >= int(zone.m_fontList.size())) {
1562 MWAW_DEBUG_MSG(("ClarisWksText::sendText: can not find font %d\n", plc.m_id));
1563 f << "###";
1564 break;
1565 }
1566 listener->setFont(zone.m_fontList[size_t(plc.m_id)]);
1567 break;
1568 case ClarisWksTextInternal::P_Ruler: {
1569 if (plc.m_id < 0 || plc.m_id >= numParaPLC)
1570 break;
1571 auto const &paraPLC = zone.m_paragraphList[size_t(plc.m_id)];
1572 f << "[" << paraPLC << "]";
1573 if (paraPLC.m_rulerId < 0 || paraPLC.m_rulerId >= numParagraphs)
1574 break;
1575 auto para = m_state->m_paragraphsList[size_t(paraPLC.m_rulerId)];
1576 if (*para.m_listLevelIndex>0 && actC >= actListCPos)
1577 actListId=findListId(zone, actListId, actC, actListCPos);
1578 #if 0
1579 // to use when the style manager is able to retrieve the correct style name
1580 if (actListId <= 0 && paraPLC.m_styleId >= 0) {
1581 std::string styleName;
1582 if (m_document.getStyleManager()->getRulerName(paraPLC.m_styleId, styleName)) {
1583 librevenge::RVNGString sfinalName("");
1584 for (size_t c=0; c < styleName.size(); ++c) {
1585 int unicode= m_parserState->m_fontConverter->unicode(3, static_cast<unsigned char>(styleName[c]));
1586 if (unicode==-1)
1587 sfinalName.append(char(styleName[c]));
1588 else
1589 libmwaw::appendUnicode(static_cast<uint32_t>(unicode), sfinalName);
1590 }
1591 para.m_styleName = librevenge::RVNGString(sfinalName,true).cstr();
1592 }
1593 }
1594 #endif
1595 setProperty(*listener, para, actListId);
1596 break;
1597 }
1598 case ClarisWksTextInternal::P_Token: {
1599 if (plc.m_id < 0 || plc.m_id >= int(zone.m_tokenList.size())) {
1600 MWAW_DEBUG_MSG(("ClarisWksText::sendText: can not find the token %d\n", plc.m_id));
1601 f << "###";
1602 break;
1603 }
1604 auto const &token = zone.m_tokenList[size_t(plc.m_id)];
1605 switch (token.m_type) {
1606 case ClarisWksTextInternal::TKN_FOOTNOTE:
1607 if (m_parserState->m_kind==MWAWDocument::MWAW_K_PAINT) {
1608 MWAW_DEBUG_MSG(("ClarisWksText::sendText: can not send footnote in a paint file\n"));
1609 f << "###";
1610 break;
1611 }
1612 if (token.m_zoneId>0)
1613 m_document.sendFootnote(token.m_zoneId);
1614 else
1615 f << "###";
1616 break;
1617 case ClarisWksTextInternal::TKN_PAGENUMBER:
1618 switch (token.m_unknown[0]) {
1619 case 1:
1620 case 2: {
1621 std::stringstream s;
1622 int num = token.m_unknown[0]==1 ? numSection : numSectionInPage;
1623 s << num;
1624 listener->insertUnicodeString(librevenge::RVNGString(s.str().c_str()));
1625 break;
1626 }
1627 case 3:
1628 listener->insertField(MWAWField(MWAWField::PageCount));
1629 break;
1630 case 0:
1631 default:
1632 listener->insertField(MWAWField(MWAWField::PageNumber));
1633 }
1634 break;
1635 case ClarisWksTextInternal::TKN_GRAPHIC:
1636 if (m_parserState->m_kind==MWAWDocument::MWAW_K_PAINT) {
1637 MWAW_DEBUG_MSG(("ClarisWksText::sendText: can not send graphic in a paint file\n"));
1638 f << "###";
1639 break;
1640 }
1641 if (m_parserState->m_kind==MWAWDocument::MWAW_K_PRESENTATION) {
1642 MWAW_DEBUG_MSG(("ClarisWksText::sendText: find a graphic in text zone, may cause some problem\n"));
1643 f << "#";
1644 }
1645 if (token.m_zoneId>0) {
1646 // fixme
1647 MWAWPosition tPos;
1648 if (token.m_descent != 0) {
1649 tPos=MWAWPosition(MWAWVec2f(0,float(token.m_descent)), MWAWVec2f(), librevenge::RVNG_POINT);
1650 tPos.setRelativePosition(MWAWPosition::Char, MWAWPosition::XLeft, MWAWPosition::YBottom);
1651 }
1652 m_document.sendZone(token.m_zoneId, MWAWListenerPtr(), tPos);
1653 }
1654 else
1655 f << "###";
1656 break;
1657 case ClarisWksTextInternal::TKN_FIELD:
1658 listener->insertUnicode(0xab);
1659 if (token.m_fieldEntry.valid() &&
1660 input->checkPosition(token.m_fieldEntry.end())) {
1661 long actPos=input->tell();
1662 input->seek(token.m_fieldEntry.begin(), librevenge::RVNG_SEEK_SET);
1663 long endFPos=token.m_fieldEntry.end();
1664 while (!input->isEnd() && input->tell() < token.m_fieldEntry.end())
1665 listener->insertCharacter(static_cast<unsigned char>(input->readULong(1)), input, endFPos);
1666 input->seek(actPos, librevenge::RVNG_SEEK_SET);
1667 }
1668 else {
1669 MWAW_DEBUG_MSG(("ClarisWksText::sendText: can not find field token data\n"));
1670 listener->insertCharacter(' ');
1671 }
1672 listener->insertUnicode(0xbb);
1673 break;
1674 case ClarisWksTextInternal::TKN_UNKNOWN:
1675 #if !defined(__clang__)
1676 default:
1677 #endif
1678 break;
1679 }
1680 seeToken = true;
1681 break;
1682 }
1683 /* checkme: normally, this corresponds to the first
1684 character following a 0xb/0x1, so we do not need to a
1685 column/page break here */
1686 case ClarisWksTextInternal::P_Child:
1687 case ClarisWksTextInternal::P_Section:
1688 case ClarisWksTextInternal::P_TextZone:
1689 case ClarisWksTextInternal::P_Unknown:
1690 #if !defined(__clang__)
1691 default:
1692 #endif
1693 break;
1694 }
1695 }
1696 auto c = char(input->readULong(1));
1697 lastIsSectionBreak=(c==0xc);
1698 actC++;
1699 if (c == '\0') {
1700 if (i == numC-1) break;
1701 MWAW_DEBUG_MSG(("ClarisWksText::sendText: OOPS, find 0 reading the text\n"));
1702 f << "###0x0";
1703 continue;
1704 }
1705 f << c;
1706 if (seeToken && static_cast<unsigned char>(c) < 32) continue;
1707 switch (c) {
1708 case 0x1: // fixme: column break
1709 if (numCols) {
1710 listener->insertBreak(MWAWListener::ColumnBreak);
1711 break;
1712 }
1713 MWAW_DEBUG_MSG(("ClarisWksText::sendText: Find unexpected char 1\n"));
1714 f << "###";
1715 MWAW_FALLTHROUGH;
1716 case 0xb: // page break
1717 numSectionInPage = 0;
1718 if (main)
1719 m_document.newPage(++actPage);
1720 break;
1721 case 0x2: // token footnote ( normally already done)
1722 break;
1723 case 0x3: // token graphic
1724 break;
1725 case 0x4:
1726 listener->insertField(MWAWField(MWAWField::Date));
1727 break;
1728 case 0x5: {
1729 MWAWField field(MWAWField::Time);
1730 field.m_DTFormat="%H:%M";
1731 listener->insertField(field);
1732 break;
1733 }
1734 case 0x6: // normally already done, but if we do not find the token, ...
1735 listener->insertField(MWAWField(MWAWField::PageNumber));
1736 break;
1737 case 0x7: // footnote index (ok to ignore : index of the footnote )
1738 break;
1739 case 0x8: // potential breaking <<hyphen>>
1740 break;
1741 case 0x9:
1742 listener->insertTab();
1743 break;
1744 case 0xa:
1745 listener->insertEOL(true);
1746 break;
1747 case 0xc: // new section: this is treated after, at the beginning of the for loop
1748 break;
1749 case 0xd:
1750 f2.str("");
1751 f2 << "Entries(TextContent):" << f.str();
1752 ascFile.addPos(pos);
1753 ascFile.addNote(f2.str().c_str());
1754 f.str("");
1755 pos = input->tell();
1756
1757 // ignore last end of line returns
1758 if (z != numZones-1 || i != numC-2)
1759 listener->insertEOL();
1760 break;
1761
1762 default: {
1763 int extraChar = listener->insertCharacter
1764 (static_cast<unsigned char>(c), input, input->tell()+(numC-1-i));
1765 if (extraChar) {
1766 i += extraChar;
1767 actC += extraChar;
1768 }
1769 }
1770 }
1771 }
1772 if (f.str().length()) {
1773 f2.str("");
1774 f2 << "Entries(TextContent):" << f.str();
1775 ascFile.addPos(pos);
1776 ascFile.addNote(f2.str().c_str());
1777 }
1778 }
1779
1780 return true;
1781 }
1782
1783 int ClarisWksText::findListId(ClarisWksTextInternal::Zone const &zone, int actListId, long actC, long &lastPos)
1784 {
1785 // retrieve the actual list
1786 std::shared_ptr<MWAWList> actList;
1787 if (actListId>0)
1788 actList = m_parserState->m_listManager->getList(actListId);
1789
1790 auto numParaPLC= int(zone.m_paragraphList.size());
1791 auto numParagraphs = int(m_state->m_paragraphsList.size());
1792 auto plcIt=zone.m_plcMap.find(actC);
1793 int listId = -1;
1794 int maxLevelSet = -1;
1795 // find the last position which can correspond to the actual list
1796 while (plcIt!=zone.m_plcMap.end()) {
1797 lastPos = plcIt->first;
1798 auto const &plc = plcIt++->second;
1799 if (plc.m_type != ClarisWksTextInternal::P_Ruler)
1800 continue;
1801 if (plc.m_id < 0 || plc.m_id >= numParaPLC)
1802 break;
1803 ClarisWksTextInternal::ParagraphPLC const &paraPLC = zone.m_paragraphList[size_t(plc.m_id)];
1804 if (paraPLC.m_rulerId < 0 || paraPLC.m_rulerId >= numParagraphs)
1805 break;
1806 ClarisWksTextInternal::Paragraph const &para=m_state->m_paragraphsList[size_t(paraPLC.m_rulerId)];
1807 int level = *para.m_listLevelIndex;
1808 if (level<=0)
1809 continue;
1810 auto newList = m_parserState->m_listManager->getNewList(actList, level, *para.m_listLevel);
1811 if (!newList)
1812 break;
1813 if (level <= maxLevelSet && newList->getId() != listId)
1814 break;
1815 if (level > maxLevelSet) maxLevelSet=level;
1816 listId = newList->getId();
1817 actList = newList;
1818 }
1819 return listId;
1820 }
1821
1822 ////////////////////////////////////////////////////////////
1823 // the style definition?
1824 ////////////////////////////////////////////////////////////
1825
1826 bool ClarisWksText::readSTYL_RULR(int N, int dataSize)
1827 {
1828 if (dataSize == 0 || N== 0) return true;
1829 if (dataSize != 108) {
1830 MWAW_DEBUG_MSG(("ClarisWksText::readSTYL_RULR: Find odd ruler size %d\n", dataSize));
1831 }
1832 MWAWInputStreamPtr &input= m_parserState->m_input;
1833 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1834 libmwaw::DebugStream f;
1835 for (int i = 0; i < N; i++) {
1836 long pos = input->tell();
1837 if (dataSize != 108 || !readParagraph(i)) {
1838 f.str("");
1839 if (!i)
1840 f << "Entries(RULR)-P0:#";
1841 else
1842 f << "RULR-P" << i << ":#";
1843 ascFile.addPos(pos);
1844 ascFile.addNote(f.str().c_str());
1845 }
1846 input->seek(pos+dataSize, librevenge::RVNG_SEEK_SET);
1847 }
1848 return true;
1849 }
1850
1851 ////////////////////////////////////////////////////////////
1852 // read a list of rulers
1853 ////////////////////////////////////////////////////////////
1854 bool ClarisWksText::readParagraphs()
1855 {
1856 MWAWInputStreamPtr &input= m_parserState->m_input;
1857 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1858 long pos = input->tell();
1859 ClarisWksStruct::Struct header;
1860 if (!header.readHeader(input,true)) {
1861 MWAW_DEBUG_MSG(("ClarisWksText::readParagraphs: can not read the header\n"));
1862 return false;
1863 }
1864 if (header.m_size==0) {
1865 ascFile.addPos(pos);
1866 ascFile.addNote("Nop");
1867 return true;
1868 }
1869
1870 libmwaw::DebugStream f;
1871 f << "Entries(RULR):" << header;
1872 if (header.m_headerSize) {
1873 ascFile.addDelimiter(input->tell(),'|');
1874 input->seek(header.m_headerSize, librevenge::RVNG_SEEK_CUR);
1875 }
1876
1877 ascFile.addPos(pos);
1878 ascFile.addNote(f.str().c_str());
1879
1880 for (long i = 0; i < header.m_numData; i++) {
1881 pos = input->tell();
1882 if (!readParagraph(int(i))) {
1883 input->seek(pos, librevenge::RVNG_SEEK_SET);
1884 return false;
1885 }
1886 }
1887 return true;
1888 }
1889
1890 ////////////////////////////////////////////////////////////
1891 // read a ruler zone
1892 ////////////////////////////////////////////////////////////
1893 bool ClarisWksText::readParagraph(int id)
1894 {
1895 int dataSize = 0;
1896 int vers = version();
1897 switch (vers) {
1898 case 1:
1899 dataSize = 92;
1900 break;
1901 case 2:
1902 case 3:
1903 dataSize = 96;
1904 break;
1905 case 4:
1906 case 5:
1907 case 6:
1908 if (id >= 0) dataSize = 108;
1909 else dataSize = 96;
1910 break;
1911 default:
1912 MWAW_DEBUG_MSG(("ClarisWksText::readParagraph: unknown size\n"));
1913 return false;
1914 }
1915
1916 ClarisWksTextInternal::Paragraph ruler;
1917 MWAWInputStreamPtr &input= m_parserState->m_input;
1918 long pos = input->tell();
1919 long endPos = pos+dataSize;
1920 if (!input->checkPosition(endPos)) {
1921 MWAW_DEBUG_MSG(("ClarisWksText::readParagraph: the zone seems too short\n"));
1922 return false;
1923 }
1924 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1925 libmwaw::DebugStream f;
1926
1927 int val;
1928 if (vers >= 4 && id >= 0) {
1929 val = static_cast<int>(input->readLong(2));
1930 if (val != -1) f << "f0=" << val << ",";
1931 val = static_cast<int>(input->readLong(4));
1932 f << "f1=" << val << ",";
1933 int dim[2];
1934 for (auto &d : dim) d = static_cast<int>(input->readLong(2));
1935 f << "dim?=" << dim[0] << "x" << dim[1] << ",";
1936 ruler.m_labelType = static_cast<int>(input->readLong(1));
1937 auto listLevel = static_cast<int>(input->readLong(1));
1938 if (listLevel < 0 || listLevel > 10) {
1939 MWAW_DEBUG_MSG(("ClarisWksText::readParagraph: can not determine list level\n"));
1940 f << "##listLevel=" << listLevel << ",";
1941 listLevel = 0;
1942 }
1943 ruler.m_listLevelIndex = listLevel;
1944 }
1945
1946 val = static_cast<int>(input->readLong(2));
1947 f << "num[used]=" << val << ",";
1948 val = static_cast<int>(input->readULong(2));
1949 int align = 0;
1950 switch (vers) {
1951 case 1:
1952 case 2:
1953 case 3:
1954 case 4:
1955 case 5:
1956 align = (val >> 14);
1957 val &= 0x3FFF;
1958 break;
1959 case 6:
1960 align = (val >> 13) & 3;
1961 val &= 0x9FFF;
1962 break;
1963 default:
1964 break;
1965 }
1966 switch (align) {
1967 case 0:
1968 break;
1969 case 1:
1970 ruler.m_justify = MWAWParagraph::JustificationCenter;
1971 break;
1972 case 2:
1973 ruler.m_justify = MWAWParagraph::JustificationRight ;
1974 break;
1975 case 3:
1976 ruler.m_justify = MWAWParagraph::JustificationFull;
1977 break;
1978 default:
1979 break;
1980 }
1981
1982
1983 bool inPoint = false;
1984 int interline = 0;
1985 switch (vers) {
1986 case 1:
1987 inPoint = (val & 0x2000);
1988 interline = val & 0xFF;
1989 val &= 0x1F00;
1990 break;
1991 case 2:
1992 case 3:
1993 case 4:
1994 case 5:
1995 case 6: {
1996 interline = (val >> 3);
1997 bool ok = true;
1998 switch (val & 7) {
1999 case 0: // PERCENT
2000 ok = interline <= 18;
2001 inPoint = false;
2002 break;
2003 case 6: // display unit pica
2004 case 5: // display unit cm
2005 case 4: // display unit mm
2006 case 3: // display unit Inch
2007 case 2: // display unit point
2008 ok = interline <= 512;
2009 inPoint = true; // data always stored in point
2010 break;
2011 default:
2012 ok = false;
2013 break;
2014 }
2015 if (ok) val = 0;
2016 else {
2017 MWAW_DEBUG_MSG(("ClarisWksText::readParagraph: can not determine interline dimension\n"));
2018 interline = 0;
2019 }
2020 break;
2021 }
2022 default:
2023 break;
2024 }
2025 if (interline) {
2026 if (inPoint)
2027 ruler.setInterline(interline, librevenge::RVNG_POINT);
2028 else
2029 ruler.setInterline(1.0+interline*0.5, librevenge::RVNG_PERCENT);
2030 }
2031 if (val) f << "#flags=" << std::hex << val << std::dec << ",";
2032 for (auto &margin : ruler.m_margins)
2033 margin = double(input->readLong(2))/72.;
2034 *(ruler.m_margins[2]) -= 28./72.;
2035 if (ruler.m_margins[2].get() < 0.0) ruler.m_margins[2] = 0.0;
2036 if (vers >= 2) {
2037 for (int i = 0; i < 2; i++) {
2038 ruler.m_spacings[i+1] = double(input->readULong(1))/72.;
2039 input->seek(1, librevenge::RVNG_SEEK_CUR); // flags to define the printing unit
2040 }
2041 }
2042 val = static_cast<int>(input->readLong(1));
2043 if (val) f << "unkn1=" << val << ",";
2044 auto numTabs = static_cast<int>(input->readULong(1));
2045 if (long(input->tell())+numTabs*4 > endPos) {
2046 if (numTabs != 255) { // 0xFF seems to be used in v1, v2
2047 MWAW_DEBUG_MSG(("ClarisWksText::readParagraph: numTabs is too big\n"));
2048 }
2049 f << "numTabs*=" << numTabs << ",";
2050 numTabs = 0;
2051 }
2052 for (int i = 0; i < numTabs; i++) {
2053 MWAWTabStop tab;
2054 tab.m_position = double(input->readLong(2))/72.;
2055 val = static_cast<int>(input->readULong(1));
2056 int leaderType = 0;
2057 switch (vers) {
2058 case 1:
2059 align = val & 3;
2060 val &= 0xFC;
2061 break;
2062 case 2:
2063 case 3:
2064 case 4:
2065 case 5:
2066 align = (val >> 6);
2067 leaderType = (val & 3);
2068 val &= 0x3C;
2069 break;
2070 case 6:
2071 align = (val >> 5);
2072 leaderType = (val & 3);
2073 val &= 0x9C;
2074 break;
2075 default:
2076 break;
2077 }
2078 switch (align&3) {
2079 case 1:
2080 tab.m_alignment = MWAWTabStop::CENTER;
2081 break;
2082 case 2:
2083 tab.m_alignment = MWAWTabStop::RIGHT;
2084 break;
2085 case 3:
2086 tab.m_alignment = MWAWTabStop::DECIMAL;
2087 break;
2088 case 0: // left
2089 default:
2090 break;
2091 }
2092 switch (leaderType) {
2093 case 1:
2094 tab.m_leaderCharacter = '.';
2095 break;
2096 case 2:
2097 tab.m_leaderCharacter = '-';
2098 break;
2099 case 3:
2100 tab.m_leaderCharacter = '_';
2101 break;
2102 case 0:
2103 default:
2104 break;
2105 }
2106 auto decimalChar = char(input->readULong(1));
2107 if (decimalChar) {
2108 int unicode= m_parserState->m_fontConverter->unicode(3, static_cast<unsigned char>(decimalChar));
2109 if (unicode==-1)
2110 tab.m_decimalCharacter = uint16_t(decimalChar);
2111 else
2112 tab.m_decimalCharacter = uint16_t(unicode);
2113 }
2114 ruler.m_tabs->push_back(tab);
2115 if (val)
2116 f << "#unkn[tab" << i << "=" << std::hex << val << std::dec << "],";
2117 }
2118 ruler.updateListLevel();
2119 ruler.m_extra = f.str();
2120 // save the style
2121 if (id >= 0) {
2122 if (int(m_state->m_paragraphsList.size()) <= id)
2123 m_state->m_paragraphsList.resize(size_t(id+1));
2124 m_state->m_paragraphsList[size_t(id)]=ruler;
2125 }
2126 f.str("");
2127 if (id == 0)
2128 f << "Entries(RULR)-P0";
2129 else if (id < 0)
2130 f << "RULR-P_";
2131 else
2132 f << "RULR-P" << id;
2133 f << ":" << ruler;
2134
2135 if (long(input->tell()) != pos+dataSize)
2136 ascFile.addDelimiter(input->tell(), '|');
2137 ascFile.addPos(pos);
2138 ascFile.addNote(f.str().c_str());
2139 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2140 if (long(input->tell()) != pos+dataSize)
2141 return false;
2142 return true;
2143 }
2144
2145 void ClarisWksText::setProperty(MWAWListener &listener, ClarisWksTextInternal::Paragraph const &ruler, int listId)
2146 {
2147 if (listId <= 0) {
2148 listener.setParagraph(ruler);
2149 return;
2150 }
2151 MWAWParagraph para=ruler;
2152 para.m_listId=listId;
2153 listener.setParagraph(para);
2154 }
2155
2156 bool ClarisWksText::canSendTextAsGraphic(int number) const
2157 {
2158 auto iter = m_state->m_zoneMap.find(number);
2159 if (iter == m_state->m_zoneMap.end() || !iter->second)
2160 return false;
2161 return canSendTextAsGraphic(*iter->second);
2162 }
2163
2164 bool ClarisWksText::sendZone(int number, MWAWListenerPtr const &listener)
2165 {
2166 auto iter = m_state->m_zoneMap.find(number);
2167 if (iter == m_state->m_zoneMap.end())
2168 return false;
2169 sendText(*iter->second, listener);
2170 return true;
2171 }
2172
2173 void ClarisWksText::flushExtra()
2174 {
2175 std::shared_ptr<MWAWListener> listener=m_parserState->getMainListener();
2176 if (!listener) return;
2177 for (auto const &iter :m_state->m_zoneMap) {
2178 std::shared_ptr<ClarisWksTextInternal::Zone> zone = iter.second;
2179 if (!zone || zone->m_parsed)
2180 continue;
2181 listener->insertEOL();
2182 if (zone->m_parsed) // can be a header/footer in draw zone
2183 continue;
2184 sendText(*zone, listener);
2185 }
2186 }
2187
2188 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: