Debian Codegraph @

src/lib/HanMacWrdKText.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
52 #include "HanMacWrdKParser.hxx"
53
54 #include "HanMacWrdKText.hxx"
55
56 /** Internal: the structures of a HanMacWrdKText */
57 namespace HanMacWrdKTextInternal
58 {
59 /** Internal: class to store the paragraph properties of a HanMacWrdKText */
60 struct Paragraph final : public MWAWParagraph {
61 //! Constructor
62 Paragraph()
63 : MWAWParagraph()
64 , m_type(0)
65 , m_addPageBreak(false)
66 {
67 }
68 Paragraph &operator=(Paragraph const &)=default;
69 Paragraph &operator=(Paragraph &&)=default;
70 //! destructor
71 ~Paragraph() final;
72 //! operator<<
73 friend std::ostream &operator<<(std::ostream &o, Paragraph const &ind)
74 {
75 switch (ind.m_type) {
76 case 0:
77 break;
78 case 1:
79 o << "header,";
80 break;
81 case 2:
82 o << "footer,";
83 break;
84 case 5:
85 o << "footnote,";
86 break;
87 default:
88 o << "#type=" << ind.m_type << ",";
89 break;
90 }
91 o << static_cast<MWAWParagraph const &>(ind) << ",";
92 if (ind.m_addPageBreak) o << "pageBreakBef,";
93 return o;
94 }
95 //! the type
96 int m_type;
97 //! flag to store a force page break
98 bool m_addPageBreak;
99 };
100
101 Paragraph::~Paragraph()
102 {
103 }
104
105 /** Internal: class to store a section of a HanMacWrdKText */
106 struct Section {
107 //! constructor
108 Section()
109 : m_numCols(1)
110 , m_colWidth()
111 , m_colSep()
112 , m_id(0)
113 , m_extra("")
114 {
115 }
116 //! returns a MWAWSection
117 MWAWSection getSection() const
118 {
119 MWAWSection sec;
120 if (m_colWidth.size()==0) {
121 MWAW_DEBUG_MSG(("HanMacWrdKTextInternal::Section:getSection can not find any width\n"));
122 return sec;
123 }
124 if (m_numCols <= 1)
125 return sec;
126 bool hasSep = m_colWidth.size()==m_colSep.size();
127 sec.m_columns.resize(size_t(m_numCols));
128 if (m_colWidth.size()==size_t(m_numCols)) {
129 for (size_t c=0; c < size_t(m_numCols); c++) {
130 sec.m_columns[c].m_width = double(m_colWidth[c]);
131 sec.m_columns[c].m_widthUnit = librevenge::RVNG_POINT;
132 if (!hasSep) continue;
133 sec.m_columns[c].m_margins[libmwaw::Left]=
134 sec.m_columns[c].m_margins[libmwaw::Right]=double(m_colSep[c])/2.0/72.;
135 }
136 }
137 else {
138 if (m_colWidth.size()>1) {
139 MWAW_DEBUG_MSG(("HanMacWrdKTextInternal::Section:getSection colWidth is not coherent with numCols\n"));
140 }
141 sec.setColumns(m_numCols, double(m_colWidth[0]), librevenge::RVNG_POINT,
142 hasSep ? double(m_colSep[0])/72.0 : 0);
143 }
144 return sec;
145 }
146 //! operator<<
147 friend std::ostream &operator<<(std::ostream &o, Section const &sec)
148 {
149 if (sec.m_numCols!=1)
150 o << "numCols=" << sec.m_numCols << ",";
151 if (!sec.m_colWidth.empty()) {
152 o << "colWidth=[";
153 for (size_t i = 0; i < sec.m_colWidth.size(); i++)
154 o << sec.m_colWidth[i] << ":" << sec.m_colSep[i] << ",";
155 o << "],";
156 }
157 if (sec.m_id)
158 o << "id=" << std::hex << sec.m_id << std::dec << ",";
159 o << sec.m_extra;
160 return o;
161 }
162 //! the number of column
163 int m_numCols;
164 //! the columns width
165 std::vector<double> m_colWidth;
166 //! the columns separator width
167 std::vector<double> m_colSep;
168 //! the id
169 long m_id;
170 //! extra string string
171 std::string m_extra;
172 };
173
174 /** Internal: class to store the token properties of a HanMacWrdKText */
175 struct Token {
176 //! Constructor
177 Token()
178 : m_type(0)
179 , m_id(-1)
180 , m_extra("")
181 {
182 }
183 Token &operator=(Token const &)=default;
184 Token &operator=(Token &&)=default;
185 //! destructor
186 ~Token()
187 {
188 }
189 //! operator<<
190 friend std::ostream &operator<<(std::ostream &o, Token const &tkn)
191 {
192 switch (tkn.m_type) {
193 case 0: // main text
194 break;
195 case 1:
196 o << "header,";
197 break;
198 case 2:
199 o << "footer,";
200 break;
201 case 3:
202 o << "footnote[frame],";
203 break;
204 case 4:
205 o << "textbox,";
206 break;
207 case 6:
208 o << "picture,";
209 break;
210 case 8:
211 o << "basicGraphic,";
212 break;
213 case 9:
214 o << "table,";
215 break;
216 case 10:
217 o << "comments,"; // memo
218 break;
219 case 11:
220 o << "group";
221 break;
222 default:
223 o << "#type=" << tkn.m_type << ",";
224 break;
225 }
226 o << "id=" << std::hex << tkn.m_id << std::dec << ",";
227 o << tkn.m_extra;
228 return o;
229 }
230 //! the type
231 int m_type;
232 //! the identificator
233 long m_id;
234 //! extra data, mainly for debugging
235 std::string m_extra;
236 };
237
238 ////////////////////////////////////////
239 //! Internal: the state of a HanMacWrdKText
240 struct State {
241 //! constructor
242 State()
243 : m_version(-1)
244 , m_IdTextMaps()
245 , m_IdTypeMaps()
246 , m_tokenIdList()
247 , m_sectionList()
248 , m_numPages(-1)
249 , m_actualPage(0)
250 , m_headerId(0)
251 , m_footerId(0)
252 {
253 }
254
255 //! the file version
256 mutable int m_version;
257 //! the map of id -> text zone
258 std::multimap<long, std::shared_ptr<HanMacWrdKZone> > m_IdTextMaps;
259 //! the zone frame type if known
260 std::map<long,int> m_IdTypeMaps;
261 //! the token id list
262 std::vector<long> m_tokenIdList;
263 //! the list of section
264 std::map<long, Section> m_sectionList;
265 int m_numPages /* the number of pages */, m_actualPage /* the actual page */;
266 /** the header text zone id or 0*/
267 long m_headerId;
268 /** the footer text zone id or 0*/
269 long m_footerId;
270 };
271
272 static Section getSection(const State &state, const long id)
273 {
274 auto it = state.m_sectionList.find(id);
275 if (it != state.m_sectionList.end())
276 return it->second;
277 MWAW_DEBUG_MSG(("HanMacWrdKTextInternal::getSection: can not find section %ld\n", id));
278 return Section();
279 }
280
281 }
282
283 ////////////////////////////////////////////////////////////
284 // constructor/destructor, ...
285 ////////////////////////////////////////////////////////////
286 HanMacWrdKText::HanMacWrdKText(HanMacWrdKParser &parser)
287 : m_parserState(parser.getParserState())
288 , m_state(new HanMacWrdKTextInternal::State)
289 , m_mainParser(&parser)
290 {
291 }
292
293 HanMacWrdKText::~HanMacWrdKText()
294 {
295 }
296
297 int HanMacWrdKText::version() const
298 {
299 if (m_state->m_version < 0)
300 m_state->m_version = m_parserState->m_version;
301 return m_state->m_version;
302 }
303
304 int HanMacWrdKText::numPages() const
305 {
306 int nPages = 1;
307 for (auto const &tIt : m_state->m_IdTextMaps) {
308 if (!tIt.second) continue;
309 int pages = const_cast<HanMacWrdKText *>(this)->computeNumPages(*tIt.second);
310 if (pages > nPages) nPages=pages;
311 }
312 m_state->m_numPages = nPages;
313 return nPages;
314 }
315
316 void HanMacWrdKText::getHeaderFooterId(long &headerId, long &footerId) const
317 {
318 headerId=m_state->m_headerId;
319 footerId=m_state->m_footerId;
320 }
321
322 std::vector<long> const &HanMacWrdKText::getTokenIdList() const
323 {
324 return m_state->m_tokenIdList;
325 }
326
327 void HanMacWrdKText::updateTextZoneTypes(std::map<long,int> const &idTypeMap)
328 {
329 m_state->m_IdTypeMaps=idTypeMap;
330 if (m_state->m_headerId)
331 m_state->m_IdTypeMaps[m_state->m_headerId]=1;
332 if (m_state->m_footerId)
333 m_state->m_IdTypeMaps[m_state->m_footerId]=1;
334 int numUnkns=0;
335 for (auto const &tIt : m_state->m_IdTextMaps) {
336 if (m_state->m_IdTypeMaps.find(tIt.first)!=m_state->m_IdTypeMaps.end())
337 continue;
338 m_state->m_IdTypeMaps[tIt.first]=0;
339 numUnkns++;
340 }
341 if (numUnkns!=1) {
342 MWAW_DEBUG_MSG(("HanMacWrdKText::updateTextZoneTypes: find unexpected number of unknown zone %d\n", numUnkns));
343 }
344 }
345
346 ////////////////////////////////////////////////////////////
347 // Intermediate level
348 ////////////////////////////////////////////////////////////
349
350 ////////////////////////////////////////////////////////////
351 // Text
352 ////////////////////////////////////////////////////////////
353 int HanMacWrdKText::computeNumPages(HanMacWrdKZone const &zone) const
354 {
355 if (!zone.valid())
356 return 0;
357 // first check if this is the main zone
358 if (m_state->m_IdTypeMaps.find(zone.m_id)== m_state->m_IdTypeMaps.end() ||
359 m_state->m_IdTypeMaps.find(zone.m_id)->second != 0)
360 return 1;
361
362 long endPos = zone.end();
363 MWAWInputStreamPtr input = zone.m_input;
364 long pos = zone.begin();
365 input->seek(pos, librevenge::RVNG_SEEK_SET);
366
367 int actPage = 1, actCol = 0, numCol=1, actSection = 1;
368 if (!m_state->m_sectionList.empty()) {
369 HanMacWrdKTextInternal::Section sec = getSection(*m_state, 0);
370 if (sec.m_numCols >= 1 && sec.m_colWidth.size() > 0)
371 numCol = sec.m_numCols;
372 }
373
374 input->seek(0, librevenge::RVNG_SEEK_SET);
375 while (!input->isEnd()) {
376 pos = input->tell();
377 auto val = long(input->readULong(1));
378 if (val != 1 || input->readLong(1) != 0) break;
379
380 auto type = static_cast<int>(input->readLong(2));
381 bool done=false;
382 switch (type) {
383 case 1: {
384 MWAWFont font(-1,-1);
385 done=readFont(zone,font);
386 break;
387 }
388 case 2: { // ruler
389 HanMacWrdKTextInternal::Paragraph para;
390 if (!readParagraph(zone,para))
391 break;
392 if (para.m_addPageBreak) {
393 ++actPage;
394 actCol = 0;
395 }
396 done=true;
397 break;
398 }
399 case 3: { // footnote, attachment
400 HanMacWrdKTextInternal::Token token;
401 done=readToken(zone,token);
402 break;
403 }
404 case 4: {
405 actCol = 0;
406 actPage++;
407 auto const &sec = getSection(*m_state, actSection++);
408 numCol = sec.m_numCols >= 1 ? sec.m_numCols : 1;
409 break;
410 }
411 default:
412 break;
413 }
414
415 if (!done) {
416 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
417 auto sz = long(input->readULong(2));
418 if (pos+6+sz>endPos)
419 break;
420 input->seek(pos+6+sz, librevenge::RVNG_SEEK_SET);
421 }
422 bool ok=true;
423 while (!input->isEnd()) {
424 auto c=static_cast<int>(input->readLong(2));
425 if (c==0x100) {
426 input->seek(-2, librevenge::RVNG_SEEK_CUR);
427 break;
428 }
429 if (c==0 && input->isEnd())
430 break;
431 if (c==0) {
432 ok = false;
433 break;
434 }
435 if (c==2) {
436 if (actCol < numCol-1 && numCol > 1)
437 actCol++;
438 else {
439 actCol = 0;
440 actPage++;
441 }
442 }
443 else if (c==3)
444 actPage++;
445 }
446 if (!ok)
447 break;
448 }
449 return actPage;
450 }
451
452 bool HanMacWrdKText::readTextZone(std::shared_ptr<HanMacWrdKZone> zone)
453 {
454 if (!zone || !zone->valid()) {
455 MWAW_DEBUG_MSG(("HanMacWrdKText::readTextZone: called without any zone\n"));
456 return false;
457 }
458 m_state->m_IdTextMaps.insert
459 (std::multimap<long, std::shared_ptr<HanMacWrdKZone> >::value_type(zone->m_id, zone));
460 long endPos = zone->end();
461 MWAWInputStreamPtr input = zone->m_input;
462 input->seek(zone->begin(), librevenge::RVNG_SEEK_SET);
463
464 while (!input->isEnd()) {
465 long pos = input->tell();
466 if (pos >= endPos)
467 break;
468 auto val = long(input->readULong(1));
469 if (val == 0 && input->isEnd()) break;
470 if (val != 1 || input->readLong(1) != 0)
471 break;
472 auto type = static_cast<int>(input->readLong(2));
473 bool done=false;
474 switch (type) {
475 case 2: { // ruler
476 HanMacWrdKTextInternal::Paragraph para;
477 done=readParagraph(*zone,para);
478 break;
479 }
480 case 3: { // token
481 HanMacWrdKTextInternal::Token token;
482 done=readToken(*zone,token);
483 if (done)
484 m_state->m_tokenIdList.push_back(token.m_id);
485 break;
486 }
487 default:
488 break;
489 }
490
491 if (!done) {
492 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
493 auto sz = long(input->readULong(2));
494 if (pos+6+sz > endPos)
495 break;
496 input->seek(pos+6+sz, librevenge::RVNG_SEEK_SET);
497 }
498
499 bool ok=true;
500 while (!input->isEnd()) {
501 auto c=static_cast<int>(input->readLong(2));
502 if (c==0x100) {
503 input->seek(-2, librevenge::RVNG_SEEK_CUR);
504 break;
505 }
506 if (c==0 && input->isEnd())
507 break;
508 if (c==0) {
509 ok = false;
510 break;
511 }
512 }
513 if (!ok)
514 break;
515 }
516 return true;
517 }
518
519 bool HanMacWrdKText::canSendTextAsGraphic(long id, long subId)
520 {
521 auto tIt=m_state->m_IdTextMaps.lower_bound(id);
522 if (tIt == m_state->m_IdTextMaps.end() || tIt->first != id)
523 return false;
524 while (tIt != m_state->m_IdTextMaps.end() && tIt->first == id) {
525 auto zone = (tIt++)->second;
526 if (!zone || zone->m_subId != subId) continue;
527 return canSendTextAsGraphic(*zone);
528 }
529 return false;
530 }
531
532 bool HanMacWrdKText::sendText(long id, long subId, MWAWListenerPtr listener)
533 {
534 auto tIt=m_state->m_IdTextMaps.lower_bound(id);
535 if (tIt == m_state->m_IdTextMaps.end() || tIt->first != id) {
536 MWAW_DEBUG_MSG(("HanMacWrdKText::sendText: can not find the text zone\n"));
537 return false;
538 }
539 while (tIt != m_state->m_IdTextMaps.end() && tIt->first == id) {
540 auto zone = (tIt++)->second;
541 if (!zone || zone->m_subId != subId) continue;
542 sendText(*zone, listener);
543 return true;
544 }
545 MWAW_DEBUG_MSG(("HanMacWrdKText::sendText: can not find the text zone\n"));
546 return false;
547 }
548
549 bool HanMacWrdKText::sendMainText()
550 {
551 for (auto const &tIt : m_state->m_IdTypeMaps) {
552 if (tIt.second != 0)
553 continue;
554 sendText(tIt.first, 0);
555 return true;
556 }
557 MWAW_DEBUG_MSG(("HanMacWrdKText::sendMainText: can not find the main zone\n"));
558 return false;
559 }
560
561 bool HanMacWrdKText::canSendTextAsGraphic(HanMacWrdKZone &zone)
562 {
563 if (!zone.valid()) return false;
564
565 long endPos = zone.end();
566 MWAWInputStreamPtr input = zone.m_input;
567 long pos = zone.begin();
568 input->seek(pos, librevenge::RVNG_SEEK_SET);
569 if (m_state->m_IdTypeMaps.find(zone.m_id)!= m_state->m_IdTypeMaps.end()
570 && m_state->m_IdTypeMaps.find(zone.m_id)->second == 0) // isMain
571 return false;
572 MWAWFont font;
573 while (!input->isEnd()) {
574 pos = input->tell();
575 if (pos>=endPos)
576 break;
577 auto val = long(input->readULong(1));
578 if (val == 0 && input->isEnd()) break;
579 if (val != 1 || input->readLong(1) != 0)
580 return false;
581 auto type = static_cast<int>(input->readLong(2));
582 bool done=false;
583 switch (type) {
584 case 1:
585 if (!readFont(zone,font))
586 return false;
587 done = true;
588 break;
589 case 2: { // ruler
590 HanMacWrdKTextInternal::Paragraph para;
591 if (!readParagraph(zone,para))
592 return false;
593 done = true;
594 break;
595 }
596 case 3: // footnote, attachment
597 case 4: // section
598 return false;
599 default:
600 break;
601 }
602
603 if (!done) {
604 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
605 auto sz = long(input->readULong(2));
606 if (pos+6+sz > endPos) return false;
607 }
608
609 while (!input->isEnd()) {
610 auto c=static_cast<int>(input->readULong(2));
611 if (c==0x100) {
612 input->seek(-2, librevenge::RVNG_SEEK_CUR);
613 break;
614 }
615 if (c==0 && input->isEnd())
616 break;
617 if (c==0) return false;
618 }
619 }
620 return true;
621 }
622
623 bool HanMacWrdKText::sendText(HanMacWrdKZone &zone, MWAWListenerPtr listener)
624 {
625 if (!zone.valid()) {
626 MWAW_DEBUG_MSG(("HanMacWrdKText::sendText: called without any zone\n"));
627 return false;
628 }
629 if (!listener)
630 listener=m_parserState->m_textListener;
631 if (!listener) {
632 MWAW_DEBUG_MSG(("HanMacWrdKText::sendText: can not find a listener\n"));
633 return false;
634 }
635
636 long endPos = zone.end();
637 MWAWInputStreamPtr input = zone.m_input;
638 libmwaw::DebugFile &asciiFile = zone.ascii();
639 libmwaw::DebugStream f;
640 zone.m_parsed = true;
641
642 long pos = zone.begin();
643 input->seek(pos, librevenge::RVNG_SEEK_SET);
644 f << "PTR=" << std::hex << zone.fileBeginPos() << std::dec << ",";
645 asciiFile.addPos(pos);
646 asciiFile.addNote(f.str().c_str());
647
648 bool isMain = false;
649 if (m_state->m_IdTypeMaps.find(zone.m_id)!= m_state->m_IdTypeMaps.end())
650 isMain = m_state->m_IdTypeMaps.find(zone.m_id)->second == 0;
651 int actPage = 1, actCol = 0, numCol=1, actSection = 1;
652 auto width = float(72.0*m_mainParser->getPageWidth());
653
654 if (isMain)
655 m_mainParser->newPage(1);
656 if (isMain && !m_state->m_sectionList.size()) {
657 MWAW_DEBUG_MSG(("HanMacWrdKText::sendText: can not find section 0\n"));
658 }
659 else if (isMain) {
660 auto sec = getSection(*m_state, 0);
661 if (sec.m_numCols >= 1 && sec.m_colWidth.size() > 0) {
662 if (listener->isSectionOpened())
663 listener->closeSection();
664 listener->openSection(sec.getSection());
665 numCol = listener->getSection().numColumns();
666 }
667 }
668
669 while (!input->isEnd()) {
670 pos = input->tell();
671 if (pos>=endPos)
672 break;
673 f.str("");
674 f << zone.name()<< ":";
675 auto val = long(input->readULong(1));
676 if (val == 0 && input->isEnd()) break;
677 if (val != 1 || input->readLong(1) != 0) {
678 f << "###";
679 asciiFile.addPos(pos);
680 asciiFile.addNote(f.str().c_str());
681 break;
682 }
683 auto type = static_cast<int>(input->readLong(2));
684 bool done=false;
685 switch (type) {
686 case 1: {
687 f << "font,";
688 MWAWFont font(-1,-1);
689 if (!readFont(zone,font))
690 break;
691 done = true;
692 listener->setFont(font);
693 break;
694 }
695 case 2: { // ruler
696 f << "ruler,";
697 HanMacWrdKTextInternal::Paragraph para;
698 if (!readParagraph(zone,para))
699 break;
700 if (para.m_addPageBreak) {
701 m_mainParser->newPage(++actPage);
702 actCol = 0;
703 }
704 setProperty(para, width);
705 done=true;
706 break;
707 }
708 case 3: { // footnote, attachment
709 f << "token,";
710 HanMacWrdKTextInternal::Token token;
711 done=readToken(zone,token);
712 if (!done) break;
713 switch (token.m_type) {
714 case 3:
715 case 4:
716 case 6:
717 case 8:
718 case 9:
719 case 10:
720 case 11:
721 m_mainParser->sendZone(token.m_id);
722 break;
723 default:
724 MWAW_DEBUG_MSG(("HanMacWrdKText::sendText: do not send how to send token with type: %d\n", token.m_type));
725 break;
726 }
727 break;
728 }
729 case 4:
730 f << "section[new],";
731 if (!isMain) {
732 MWAW_DEBUG_MSG(("HanMacWrdKText::sendText: find section in auxilliary block\n"));
733 break;
734 }
735 {
736 auto sec = getSection(*m_state, actSection++);
737 actCol = 0;
738 if (listener->isSectionOpened())
739 listener->closeSection();
740 m_mainParser->newPage(++actPage);
741 listener->openSection(sec.getSection());
742 numCol = listener->getSection().numColumns();
743 }
744 break;
745 case 6: // follow by id?
746 f << "toc,";
747 break;
748 case 7:
749 f << "bookmark,";
750 break;
751 case 9: // follow by id?
752 f << "rubi,";
753 break;
754 case 11: // follow by id?
755 f << "endToc,";
756 break;
757 case 12: // follow by id?
758 f << "endBookmark,";
759 break;
760 case 14:
761 f << "endRubi,";
762 break;
763 default:
764 break;
765 }
766
767 bool ok=true;
768 if (!done) {
769 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
770 auto sz = long(input->readULong(2));
771 ok = pos+6+sz <= endPos;
772 if (!ok)
773 f << "###";
774 else
775 input->seek(pos+6+sz, librevenge::RVNG_SEEK_SET);
776
777 asciiFile.addPos(pos);
778 asciiFile.addNote(f.str().c_str());
779 }
780 if (!ok) {
781 MWAW_DEBUG_MSG(("HanMacWrdKText::sendText: can not read a zone\n"));
782 break;
783 }
784
785 pos = input->tell();
786 f.str("");
787 f << zone.name()<< ":text,";
788 while (!input->isEnd()) {
789 auto c=static_cast<int>(input->readULong(2));
790 if (c==0x100) {
791 input->seek(-2, librevenge::RVNG_SEEK_CUR);
792 break;
793 }
794 if (c==0 && input->isEnd())
795 break;
796 if (c==0) {
797 ok = false;
798 f << "###";
799 break;
800 }
801 switch (c) {
802 case 0x1000:
803 f << "[pgNum]";
804 listener->insertField(MWAWField(MWAWField::PageNumber));
805 break;
806 case 0x1001:
807 f << "[pgCount]";
808 listener->insertField(MWAWField(MWAWField::PageCount));
809 break;
810 case 0x1002: {
811 f << "[date]";
812 MWAWField field(MWAWField::Date);
813 field.m_DTFormat="%A, %b %d, %Y";
814 listener->insertField(field);
815 break;
816 }
817 case 0x1003: {
818 f << "[time]";
819 MWAWField field(MWAWField::Time);
820 field.m_DTFormat="%I:%M %p";
821 listener->insertField(field);
822 break;
823 }
824 case 0x1004:
825 f << "[title]";
826 listener->insertField(MWAWField(MWAWField::Title));
827 break;
828 case 0x1005: {
829 std::stringstream s;
830 f << "[section]";
831 s << actSection;
832 listener->insertUnicodeString(librevenge::RVNGString(s.str().c_str()));
833 break;
834 }
835 case 2:
836 f << "[colBreak]";
837 if (!isMain) {
838 MWAW_DEBUG_MSG(("HanMacWrdKText::sendText: find column break in auxilliary block\n"));
839 break;
840 }
841 if (actCol < numCol-1 && numCol > 1) {
842 listener->insertBreak(MWAWTextListener::ColumnBreak);
843 actCol++;
844 }
845 else {
846 actCol = 0;
847 m_mainParser->newPage(++actPage);
848 }
849 break;
850 case 3:
851 f << "[pageBreak]";
852 if (isMain) {
853 m_mainParser->newPage(++actPage);
854 actCol = 0;
855 }
856 break;
857 case 9:
858 f << char(c);
859 listener->insertTab();
860 break;
861 case 0xd:
862 f << char(c);
863 listener->insertEOL();
864 break;
865 default: {
866 if (c <= 0x1f || c >= 0x100) {
867 f << "###" << std::hex << c << std::dec;
868 MWAW_DEBUG_MSG(("HanMacWrdKText::sendText: find a odd char %x\n", static_cast<unsigned int>(c)));
869 break;
870 }
871 f << char(c);
872 listener->insertCharacter(static_cast<unsigned char>(c), input);
873 break;
874 }
875 }
876 }
877 if (input->tell() != pos) {
878 asciiFile.addPos(pos);
879 asciiFile.addNote(f.str().c_str());
880 }
881 if (!ok) {
882 MWAW_DEBUG_MSG(("HanMacWrdKText::sendText: can not read a text zone\n"));
883 break;
884 }
885 }
886 // FIXME: remove this when the frame/textbox are sent normally
887 listener->insertEOL();
888 return true;
889 }
890
891 ////////////////////////////////////////////////////////////
892 // Fonts
893 ////////////////////////////////////////////////////////////
894
895 // a font in the text zone
896 bool HanMacWrdKText::readFont(HanMacWrdKZone const &zone, MWAWFont &font) const
897 {
898 font = MWAWFont(-1,-1);
899
900 MWAWInputStreamPtr input = zone.m_input;
901 long pos = input->tell();
902 if (pos+30 > zone.length()) {
903 MWAW_DEBUG_MSG(("HanMacWrdKText::readFont: the zone is too short\n"));
904 return false;
905 }
906
907 libmwaw::DebugStream f;
908 auto val = static_cast<int>(input->readLong(2));
909 if (val!=28) {
910 MWAW_DEBUG_MSG(("HanMacWrdKText::readFont: the data size seems bad\n"));
911 f << "##dataSz=" << val << ",";
912 }
913 font.setId(static_cast<int>(input->readLong(2)));
914 val = static_cast<int>(input->readLong(2));
915 if (val) f << "#f1=" << val << ",";
916 font.setSize(float(input->readLong(4))/65536.f);
917 float expand = float(input->readLong(4))/65536.f;
918 if (expand < 0 || expand > 0)
919 font.setDeltaLetterSpacing(expand*font.size());
920 float xScale = float(input->readLong(4))/65536.f;
921 if (xScale < 1 || xScale > 1)
922 font.setWidthStreching(xScale);
923
924 auto flag =static_cast<int>(input->readULong(2));
925 uint32_t flags=0;
926 if (flag&1) {
927 font.setUnderlineStyle(MWAWFont::Line::Simple);
928 font.setUnderlineType(MWAWFont::Line::Double);
929 }
930 if (flag&2)
931 font.setUnderlineStyle(MWAWFont::Line::Dot);
932 if (flag&4) {
933 font.setUnderlineStyle(MWAWFont::Line::Dot);
934 font.setUnderlineWidth(2.0);
935 }
936 if (flag&8)
937 font.setUnderlineStyle(MWAWFont::Line::Dash);
938 if (flag&0x10)
939 font.setStrikeOutStyle(MWAWFont::Line::Simple);
940 if (flag&0x20) {
941 font.setStrikeOutStyle(MWAWFont::Line::Simple);
942 font.setStrikeOutType(MWAWFont::Line::Double);
943 }
944 if (flag&0xFFC0)
945 f << "#flag0=" << std::hex << (flag&0xFFF2) << std::dec << ",";
946 flag =static_cast<int>(input->readULong(2));
947 if (flag&1) flags |= MWAWFont::boldBit;
948 if (flag&0x2) flags |= MWAWFont::italicBit;
949 if (flag&0x4) flags |= MWAWFont::outlineBit;
950 if (flag&0x8) flags |= MWAWFont::shadowBit;
951 if (flag&0x10) flags |= MWAWFont::reverseVideoBit;
952 if (flag&0x20) font.set(MWAWFont::Script::super100());
953 if (flag&0x40) font.set(MWAWFont::Script::sub100());
954 if (flag&0x80) {
955 if (flag&0x20)
956 font.set(MWAWFont::Script(48,librevenge::RVNG_PERCENT,58));
957 else if (flag&0x40)
958 font.set(MWAWFont::Script(16,librevenge::RVNG_PERCENT,58));
959 else
960 font.set(MWAWFont::Script::super());
961 }
962 if (flag&0x100) {
963 font.setOverlineStyle(MWAWFont::Line::Dot);
964 font.setOverlineWidth(2.0);
965 }
966 if (flag&0x200) flags |= MWAWFont::boxedBit;
967 if (flag&0x400) flags |= MWAWFont::boxedRoundedBit;
968 if (flag&0x800) {
969 font.setUnderlineStyle(MWAWFont::Line::Simple);
970 font.setUnderlineWidth(0.5);
971 }
972 if (flag&0x1000) font.setUnderlineStyle(MWAWFont::Line::Simple);
973 if (flag&0x2000) {
974 font.setUnderlineStyle(MWAWFont::Line::Simple);
975 font.setUnderlineWidth(2.0);
976 }
977 if (flag&0x4000) {
978 font.setUnderlineStyle(MWAWFont::Line::Simple);
979 font.setUnderlineWidth(3.0);
980 }
981 if (flag&0x8000) {
982 font.setUnderlineStyle(MWAWFont::Line::Simple);
983 font.setUnderlineType(MWAWFont::Line::Double);
984 font.setUnderlineWidth(0.5);
985 }
986 auto color = static_cast<int>(input->readLong(2));
987 MWAWColor col;
988 if (color && m_mainParser->getColor(color, 1, col))
989 font.setColor(col);
990 else if (color)
991 f << "##fColor=" << color << ",";
992 val = static_cast<int>(input->readLong(2));
993 if (val) f << "#unk=" << val << ",";
994 color = static_cast<int>(input->readLong(2));
995 auto pattern = static_cast<int>(input->readLong(2));
996 if ((color || pattern) && m_mainParser->getColor(color, pattern, col))
997 font.setBackgroundColor(col);
998 else if (color || pattern)
999 f << "#backColor=" << color << ", #pattern=" << pattern << ",";
1000 font.setFlags(flags);
1001 font.m_extra = f.str();
1002
1003 static bool first=true;
1004 f.str("");
1005 if (first) {
1006 f << "Entries(FontDef):";
1007 first = false;
1008 }
1009 else
1010 f << "FontDef:";
1011 f << font.getDebugString(m_parserState->m_fontConverter) << ",";
1012
1013 zone.ascii().addPos(pos-4);
1014 zone.ascii().addNote(f.str().c_str());
1015
1016 input->seek(pos+30, librevenge::RVNG_SEEK_SET);
1017 return true;
1018 }
1019
1020 // the list of fonts
1021 bool HanMacWrdKText::readFontNames(std::shared_ptr<HanMacWrdKZone> zone)
1022 {
1023 if (!zone) {
1024 MWAW_DEBUG_MSG(("HanMacWrdKText::readFontNames: called without any zone\n"));
1025 return false;
1026 }
1027 long dataSz = zone->length();
1028 if (dataSz < 2) {
1029 MWAW_DEBUG_MSG(("HanMacWrdKText::readFontNames: the zone seems too short\n"));
1030 return false;
1031 }
1032 MWAWInputStreamPtr input = zone->m_input;
1033 libmwaw::DebugFile &asciiFile = zone->ascii();
1034 libmwaw::DebugStream f;
1035 zone->m_parsed = true;
1036
1037 f << zone->name() << ":PTR=" << std::hex << zone->fileBeginPos() << std::dec << ",";
1038
1039 long pos = zone->begin();
1040 input->seek(pos, librevenge::RVNG_SEEK_SET);
1041 auto N = static_cast<int>(input->readLong(2));
1042 f << "N=" << N << ",";
1043 long expectedSz = N*68+2;
1044 if (expectedSz != dataSz && expectedSz+1 != dataSz) {
1045 MWAW_DEBUG_MSG(("HanMacWrdKText::readFontNames: the zone size seems odd\n"));
1046 return false;
1047 }
1048 asciiFile.addPos(pos);
1049 asciiFile.addNote(f.str().c_str());
1050
1051 for (int i = 0; i < N; i++) {
1052 pos = input->tell();
1053 f.str("");
1054 f << zone->name() << "-" << i << ":";
1055 auto fId = static_cast<int>(input->readLong(2));
1056 f << "fId=" << fId << ",";
1057 auto val = static_cast<int>(input->readLong(2));
1058 if (val != fId)
1059 f << "#fId2=" << val << ",";
1060 auto fSz = static_cast<int>(input->readULong(1));
1061 if (fSz+5 > 68) {
1062 f << "###fSz";
1063 MWAW_DEBUG_MSG(("HanMacWrdKText::readFontNames: can not read a font\n"));
1064 }
1065 else {
1066 std::string name("");
1067 for (int c = 0; c < fSz; c++)
1068 name += char(input->readULong(1));
1069 f << name;
1070 m_parserState->m_fontConverter->setCorrespondance(fId, name);
1071 }
1072 asciiFile.addPos(pos);
1073 asciiFile.addNote(f.str().c_str());
1074 input->seek(pos+68, librevenge::RVNG_SEEK_SET);
1075 }
1076 return true;
1077 }
1078
1079 ////////////////////////////////////////////////////////////
1080 // Style
1081 ////////////////////////////////////////////////////////////
1082 bool HanMacWrdKText::readStyles(std::shared_ptr<HanMacWrdKZone> zone)
1083 {
1084 if (!zone) {
1085 MWAW_DEBUG_MSG(("HanMacWrdKText::readStyles: called without any zone\n"));
1086 return false;
1087 }
1088
1089 long dataSz = zone->length();
1090 if (dataSz < 24) {
1091 MWAW_DEBUG_MSG(("HanMacWrdKText::readStyles: the zone seems too short\n"));
1092 return false;
1093 }
1094 MWAWInputStreamPtr input = zone->m_input;
1095 libmwaw::DebugFile &asciiFile = zone->ascii();
1096 libmwaw::DebugStream f;
1097 zone->m_parsed = true;
1098
1099 f << zone->name() << ":PTR=" << std::hex << zone->fileBeginPos() << std::dec << ",";
1100
1101 long pos = zone->begin();
1102 input->seek(pos, librevenge::RVNG_SEEK_SET);
1103 long fieldSz = 636;
1104 auto N = static_cast<int>(input->readULong(2));
1105 f << "N=" << N << ",";
1106 long expectedSz = N*fieldSz+2;
1107 if (expectedSz != dataSz && expectedSz+1 != dataSz) {
1108 MWAW_DEBUG_MSG(("HanMacWrdKText::readStyles: the zone size seems odd\n"));
1109 return false;
1110 }
1111 asciiFile.addPos(pos);
1112 asciiFile.addNote(f.str().c_str());
1113
1114 for (int i = 0; i < N; i++) {
1115 f.str("");
1116 f << zone->name() << "-" << i << ":";
1117 pos = input->tell();
1118 auto val = static_cast<int>(input->readULong(2));
1119 if (val != i) f << "#id=" << val << ",";
1120
1121 // f0=c2|c6, f2=0|44, f3=1|14|15|16: fontId?
1122 for (int j=0; j < 4; j++) {
1123 val = static_cast<int>(input->readULong(1));
1124 if (val)
1125 f << "f" << j << "=" << std::hex << val << std::dec << ",";
1126 }
1127 /* g1=9|a|c|e|12|18: size ?, g5=1, g8=0|1, g25=0|18|30|48, g31=1, g35=0|1 */
1128 for (int j=0; j < 37; j++) {
1129 val = static_cast<int>(input->readULong(2));
1130 if (val)
1131 f << "g" << j << "=" << val << ",";
1132 }
1133 for (int j=0; j < 4; j++) { // b,b,b,0
1134 val = static_cast<int>(input->readULong(1));
1135 if ((j < 3 && val != 0xb) || (j==3 && val))
1136 f << "h" << j << "=" << val << ",";
1137 }
1138
1139 for (int j=0; j < 17; j++) { // always 0
1140 val = static_cast<int>(input->readULong(2));
1141 if (val)
1142 f << "l" << j << "=" << val << ",";
1143 }
1144 asciiFile.addPos(pos);
1145 asciiFile.addNote(f.str().c_str());
1146 long pos2 = input->tell();
1147 f.str("");
1148 f << "Style-" << i << "[B]:";
1149 // checkme probably f15=numTabs ..
1150 for (int j = 0; j < 50; j++) {
1151 val = static_cast<int>(input->readULong(2));
1152 if ((j < 5 && val != 1) || (j >= 5 && val))
1153 f << "f" << j << "=" << val << ",";
1154 }
1155 for (int j = 0; j < 50; j++) {
1156 val = static_cast<int>(input->readULong(2));
1157 if (val)
1158 f << "g" << j << "=" << val << ",";
1159 }
1160 for (int j = 0; j < 100; j++) {
1161 val = static_cast<int>(input->readULong(2));
1162 if (val)
1163 f << "h" << j << "=" << val << ",";
1164 }
1165 for (int j = 0; j < 41; j++) {
1166 val = static_cast<int>(input->readULong(2));
1167 if (val)
1168 f << "l" << j << "=" << val << ",";
1169 }
1170 asciiFile.addPos(pos2);
1171 asciiFile.addNote(f.str().c_str());
1172
1173 pos2 = input->tell();
1174 f.str("");
1175 f << "Style-" << i << "[C]:";
1176 val = static_cast<int>(input->readLong(2));
1177 if (val != -1) f << "unkn=" << val << ",";
1178 val = static_cast<int>(input->readLong(2));
1179 if (val != i) f << "#id" << val << ",";
1180 auto fSz = static_cast<int>(input->readULong(1));
1181 if (input->tell()+fSz > pos+fieldSz) {
1182 MWAW_DEBUG_MSG(("HanMacWrdKText::readStyles: can not read styleName\n"));
1183 f << "###";
1184 }
1185 else {
1186 std::string name("");
1187 for (int j = 0; j < fSz; j++)
1188 name +=char(input->readULong(1));
1189 f << name;
1190 }
1191 asciiFile.addPos(pos2);
1192 asciiFile.addNote(f.str().c_str());
1193 if (input->tell() != pos+fieldSz)
1194 asciiFile.addDelimiter(input->tell(),'|');
1195
1196 input->seek(pos+fieldSz, librevenge::RVNG_SEEK_SET);
1197 }
1198
1199 if (!input->isEnd()) {
1200 asciiFile.addPos(input->tell());
1201 asciiFile.addNote("_");
1202 }
1203 return true;
1204 }
1205
1206 ////////////////////////////////////////////////////////////
1207 // Paragraph
1208 ////////////////////////////////////////////////////////////
1209 void HanMacWrdKText::setProperty(HanMacWrdKTextInternal::Paragraph const &para, float)
1210 {
1211 if (!m_parserState->m_textListener) return;
1212 m_parserState->m_textListener->setParagraph(para);
1213 }
1214
1215 bool HanMacWrdKText::readParagraph(HanMacWrdKZone const &zone, HanMacWrdKTextInternal::Paragraph &para) const
1216 {
1217 para = HanMacWrdKTextInternal::Paragraph();
1218
1219 MWAWInputStreamPtr input = zone.m_input;
1220 long pos = input->tell();
1221 long dataSz = zone.length();
1222 if (pos+102 > dataSz) {
1223 MWAW_DEBUG_MSG(("HanMacWrdKText::readParagraph: the zone is too short\n"));
1224 return false;
1225 }
1226
1227 libmwaw::DebugStream f;
1228 libmwaw::DebugFile &asciiFile = zone.ascii();
1229 auto val = static_cast<int>(input->readLong(2));
1230 if (val!=100) {
1231 MWAW_DEBUG_MSG(("HanMacWrdKText::readParagraph: the data size seems bad\n"));
1232 f << "##dataSz=" << val << ",";
1233 }
1234 auto flags = static_cast<int>(input->readULong(1));
1235 if (flags&0x80)
1236 para.m_breakStatus = para.m_breakStatus.get()|MWAWParagraph::NoBreakWithNextBit;
1237 if (flags&0x40)
1238 para.m_breakStatus = para.m_breakStatus.get()|MWAWParagraph::NoBreakBit;
1239 if (flags&0x2)
1240 para.m_addPageBreak = true;
1241 if (flags&0x4)
1242 f << "linebreakByWord,";
1243
1244 if (flags & 0x39) f << "#fl=" << std::hex << (flags&0x39) << std::dec << ",";
1245
1246 val = static_cast<int>(input->readLong(2));
1247 if (val) f << "#f0=" << val << ",";
1248 val = static_cast<int>(input->readULong(2));
1249 switch (val&3) {
1250 case 0:
1251 para.m_justify = MWAWParagraph::JustificationLeft;
1252 break;
1253 case 1:
1254 para.m_justify = MWAWParagraph::JustificationRight;
1255 break;
1256 case 2:
1257 para.m_justify = MWAWParagraph::JustificationCenter;
1258 break;
1259 case 3:
1260 para.m_justify = MWAWParagraph::JustificationFull;
1261 break;
1262 default:
1263 break;
1264 }
1265 if (val&0xFFFC) f << "#f1=" << val << ",";
1266 val = static_cast<int>(input->readLong(1));
1267 if (val) f << "#f2=" << val << ",";
1268 para.m_type = static_cast<int>(input->readLong(2));
1269
1270 float dim[3];
1271 for (auto &d : dim) d = float(input->readLong(4))/65536.0f;
1272 para.m_marginsUnit = librevenge::RVNG_POINT;
1273 para.m_margins[0]=double(dim[1]);
1274 para.m_margins[1]=double(dim[0]);
1275 para.m_margins[2]=double(dim[2]); // ie. distance to right border - ...
1276
1277 for (auto &spacing : para.m_spacings)
1278 spacing = double(input->readLong(4))/65536.0;
1279 int spacingsUnit[3]; // 1=mm, ..., 4=pt, b=line
1280 for (auto &unit : spacingsUnit) unit = static_cast<int>(input->readULong(1));
1281 if (spacingsUnit[0]==0xb)
1282 para.m_spacingsInterlineUnit = librevenge::RVNG_PERCENT;
1283 else
1284 para.m_spacingsInterlineUnit = librevenge::RVNG_POINT;
1285 for (int i = 1; i < 3; i++) // convert point|line -> inches
1286 para.m_spacings[i]= ((spacingsUnit[i]==0xb) ? 12.0 : 1.0)*(para.m_spacings[i].get())/72.0;
1287
1288 val = static_cast<int>(input->readLong(1));
1289 if (val) f << "#f3=" << val << ",";
1290 for (int i = 0; i< 2; i++) { // one time f4=8000
1291 val = static_cast<int>(input->readULong(2));
1292 if (val) f << "#f" << i+4 << "=" << std::hex << val << std::dec << ",";
1293 }
1294 // the borders
1295 char const *wh[5] = { "T", "L", "B", "R", "VSep" };
1296 MWAWBorder borders[5];
1297 for (auto &border : borders)
1298 border.m_width = double(input->readLong(4))/65536.;
1299 for (int d=0; d < 5; d++) {
1300 val = static_cast<int>(input->readULong(1));
1301 switch (val) {
1302 case 0: // normal
1303 break;
1304 case 1:
1305 borders[d].m_type = MWAWBorder::Double;
1306 break;
1307 case 2:
1308 borders[d].m_type = MWAWBorder::Double;
1309 f << "bord" << wh[d] << "[ext=2],";
1310 break;
1311 case 3:
1312 borders[d].m_type = MWAWBorder::Double;
1313 f << "bord" << wh[d] << "[int=2],";
1314 break;
1315 default:
1316 f << "#bord" << wh[d] << "[style=" << val << "],";
1317 break;
1318 }
1319 }
1320 int color[5], pattern[5];
1321 for (auto &c : color) c = static_cast<int>(input->readULong(1));
1322 for (auto &p : pattern) p = static_cast<int>(input->readULong(2));
1323 for (int d=0; d < 5; d++) {
1324 if (!pattern[d]) {
1325 borders[d].m_style=MWAWBorder::None;
1326 continue;
1327 }
1328 if (!color[d] && !pattern[d])
1329 continue;
1330 MWAWColor col;
1331 if (m_mainParser->getColor(color[d], pattern[d], col))
1332 borders[d].m_color = col;
1333 else
1334 f << "#bord" << wh[d] << "[col=" << color[d] << ",pat=" << pattern[d] << "],";
1335 }
1336 // update the paragraph
1337 para.resizeBorders(6);
1338 libmwaw::Position const which[5] = {
1339 libmwaw::Top, libmwaw::Left, libmwaw::Bottom, libmwaw::Right,
1340 libmwaw::VMiddle
1341 };
1342 for (int d=0; d < 5; d++) {
1343 if (borders[d].m_width <= 0)
1344 continue;
1345 para.m_borders[which[d]]=borders[d];
1346 }
1347 val = static_cast<int>(input->readLong(1));
1348 if (val) f << "#f6=" << val << ",";
1349 double bMargins[4]= {0,0,0,0};
1350 for (int d = 0; d < 4; d++) {
1351 bMargins[d] = double(input->readLong(4))/256./65536./72.;
1352 if (bMargins[d] > 0 || bMargins[d] < 0)
1353 f << "bordMarg" << wh[d] << "=" << bMargins[d] << ",";
1354 }
1355 auto nTabs = static_cast<int>(input->readULong(1));
1356 if (pos+102+nTabs*12 > dataSz) {
1357 MWAW_DEBUG_MSG(("HanMacWrdKText::readParagraph: can not read numbers of tab\n"));
1358 return false;
1359 }
1360 val = static_cast<int>(input->readULong(2)); // always 0
1361 if (val) f << "#h0=" << val << ",";
1362 para.m_extra=f.str();
1363 static bool first=true;
1364 f.str("");
1365 if (first) {
1366 f << "Entries(Ruler):";
1367 first = false;
1368 }
1369 else
1370 f << "Ruler:";
1371 f << para;
1372
1373 asciiFile.addPos(pos-4);
1374 asciiFile.addNote(f.str().c_str());
1375 for (int i = 0; i < nTabs; i++) {
1376 pos = input->tell();
1377 f.str("");
1378 f << "Ruler[Tabs-" << i << "]:";
1379
1380 MWAWTabStop tab;
1381 val = static_cast<int>(input->readULong(1));
1382 switch (val) {
1383 case 0:
1384 break;
1385 case 1:
1386 tab.m_alignment = MWAWTabStop::CENTER;
1387 break;
1388 case 2:
1389 tab.m_alignment = MWAWTabStop::RIGHT;
1390 break;
1391 case 3:
1392 tab.m_alignment = MWAWTabStop::DECIMAL;
1393 break;
1394 case 4:
1395 tab.m_alignment = MWAWTabStop::BAR;
1396 break;
1397 default:
1398 f << "#type=" << val << ",";
1399 break;
1400 }
1401 val = static_cast<int>(input->readULong(1));
1402 if (val) f << "barType=" << val << ",";
1403 val = static_cast<int>(input->readULong(2));
1404 if (val) {
1405 int unicode= m_parserState->m_fontConverter->unicode(3, static_cast<unsigned char>(val));
1406 if (unicode==-1)
1407 tab.m_decimalCharacter = uint16_t(val);
1408 else
1409 tab.m_decimalCharacter = uint16_t(unicode);
1410 }
1411 val = static_cast<int>(input->readULong(2));
1412 if (val) {
1413 int unicode= m_parserState->m_fontConverter->unicode(3, static_cast<unsigned char>(val));
1414 if (unicode==-1)
1415 tab.m_leaderCharacter = uint16_t(val);
1416 else
1417 tab.m_leaderCharacter = uint16_t(unicode);
1418 }
1419 val = static_cast<int>(input->readULong(2)); // 0|73|74|a044|f170|f1e0|f590
1420 if (val) f << "f0=" << std::hex << val << std::dec << ",";
1421
1422 tab.m_position = double(input->readLong(4))/65536./72.;
1423 para.m_tabs->push_back(tab);
1424 f << tab;
1425
1426 asciiFile.addPos(pos);
1427 asciiFile.addNote(f.str().c_str());
1428 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
1429 }
1430 return true;
1431 }
1432
1433 ////////////////////////////////////////////////////////////
1434 // the token
1435 ////////////////////////////////////////////////////////////
1436 bool HanMacWrdKText::readToken(HanMacWrdKZone const &zone, HanMacWrdKTextInternal::Token &token) const
1437 {
1438 token = HanMacWrdKTextInternal::Token();
1439
1440 MWAWInputStreamPtr input = zone.m_input;
1441 long pos = input->tell();
1442 long dataSz = zone.length();
1443 if (pos+10 > dataSz) {
1444 MWAW_DEBUG_MSG(("HanMacWrdKText::readToken: the zone is too short\n"));
1445 return false;
1446 }
1447
1448 libmwaw::DebugStream f;
1449 libmwaw::DebugFile &asciiFile = zone.ascii();
1450 auto val = static_cast<int>(input->readLong(2));
1451 if (val!=8) {
1452 MWAW_DEBUG_MSG(("HanMacWrdKText::readToken: the data size seems bad\n"));
1453 f << "##dataSz=" << val << ",";
1454 }
1455
1456 token.m_type = static_cast<int>(input->readLong(1));
1457 val = static_cast<int>(input->readLong(1));
1458 if (val) f << "f0=" << val << ",";
1459 val = static_cast<int>(input->readLong(2));
1460 if (val) f << "f1=" << val << ",";
1461 token.m_id = long(input->readULong(4));
1462 token.m_extra = f.str();
1463 f.str("");
1464 static bool first=true;
1465 f.str("");
1466 if (first) {
1467 f << "Entries(Token):";
1468 first = false;
1469 }
1470 else
1471 f << "Token:";
1472 f << token;
1473 asciiFile.addPos(pos-4);
1474 asciiFile.addNote(f.str().c_str());
1475 return true;
1476 }
1477
1478 ////////////////////////////////////////////////////////////
1479 // the sections
1480 ////////////////////////////////////////////////////////////
1481 bool HanMacWrdKText::readSections(std::shared_ptr<HanMacWrdKZone> zone)
1482 {
1483 if (!zone) {
1484 MWAW_DEBUG_MSG(("HanMacWrdKText::readSections: called without any zone\n"));
1485 return false;
1486 }
1487 long dataSz = zone->length();
1488 if (dataSz < 160) {
1489 MWAW_DEBUG_MSG(("HanMacWrdKText::readSections: the zone seems too short\n"));
1490 return false;
1491 }
1492
1493 MWAWInputStreamPtr input = zone->m_input;
1494 libmwaw::DebugFile &asciiFile = zone->ascii();
1495 libmwaw::DebugStream f;
1496 zone->m_parsed = true;
1497
1498 HanMacWrdKTextInternal::Section sec;
1499 long pos=0;
1500 input->seek(pos, librevenge::RVNG_SEEK_SET);
1501 long val = input->readLong(2);
1502 if (val != 1) // always 1
1503 f << "f0=" << val << ",";
1504 auto numColumns = static_cast<int>(input->readLong(2));
1505 if (numColumns <= 0 || numColumns > 8) {
1506 MWAW_DEBUG_MSG(("HanMacWrdKText::readSections: numColumns seems bad\n"));
1507 f << "###nCols=" << numColumns << ",";
1508 numColumns = 1;
1509 }
1510 else
1511 sec.m_numCols=numColumns;
1512 val = long(input->readLong(1));
1513 bool diffWidth=val==0;
1514 if (val==1)
1515 f << "sameWidth,";
1516 else if (val)
1517 f << "#width=" << val << ",";
1518 val = long(input->readLong(1)); // always 0
1519 if (val) f << "f1=" << val << ",";
1520 for (int i = 0; i < 19; i++) { // always 0
1521 val = long(input->readLong(2));
1522 if (val) f << "g" << i << "=" << val << ",";
1523 }
1524
1525 if (diffWidth) {
1526 for (int i = 0; i < numColumns; i++) {
1527 sec.m_colWidth.push_back(double(input->readLong(4))/65536);
1528 sec.m_colSep.push_back(double(input->readLong(4))/65536);
1529 }
1530 }
1531 else {
1532 sec.m_colWidth.push_back(double(input->readLong(4))/65536);
1533 sec.m_colSep.push_back(double(input->readLong(4))/65536);
1534 }
1535 sec.m_extra = f.str();
1536
1537 f.str("");
1538 f << zone->name() << "(A):PTR=" << std::hex << zone->fileBeginPos() << std::dec << "," << sec;
1539
1540 asciiFile.addDelimiter(input->tell(),'|');
1541 asciiFile.addPos(pos);
1542 asciiFile.addNote(f.str().c_str());
1543 input->seek(pos+0x6c, librevenge::RVNG_SEEK_SET);
1544
1545 pos = input->tell();
1546 f.str("");
1547 f << zone->name() << "(B):";
1548 for (int i = 0; i < 4; i++) {
1549 long id = input->readLong(4);
1550 if (!id) continue;
1551 if (i < 2) {
1552 if (!m_state->m_headerId)
1553 m_state->m_headerId = id;
1554 else if (m_state->m_headerId != id) {
1555 MWAW_DEBUG_MSG(("HanMacWrdKText::readSections: headerid is already defined\n"));
1556 f << "###";
1557 }
1558 f << "headerId=" << std::hex << id << std::dec << ",";
1559 }
1560 else {
1561 if (!m_state->m_footerId)
1562 m_state->m_footerId = id;
1563 else if (m_state->m_footerId != id) {
1564 MWAW_DEBUG_MSG(("HanMacWrdKText::readSections: footerid is already defined\n"));
1565 f << "###";
1566 }
1567 f << "footerId=" << std::hex << id << std::dec << ",";
1568 }
1569 }
1570 for (int i = 0; i < 8; i++) {
1571 val = input->readLong(2);
1572 if (val) f << "f" << i << "=" << val << ",";
1573 }
1574 asciiFile.addDelimiter(input->tell(),'|');
1575 asciiFile.addPos(pos);
1576 asciiFile.addNote(f.str().c_str());
1577 if (zone->m_id >= 0) {
1578 m_state->m_sectionList[long(zone->m_id)]=sec;
1579 }
1580 return true;
1581 }
1582
1583 //! send data to the listener
1584 void HanMacWrdKText::flushExtra()
1585 {
1586 if (!m_parserState->m_textListener) return;
1587 for (auto const &tIt : m_state->m_IdTextMaps) {
1588 if (!tIt.second) continue;
1589 HanMacWrdKZone &zone = *tIt.second;
1590 if (zone.m_parsed) continue;
1591 sendText(zone);
1592 }
1593 }
1594
1595 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: