Debian Codegraph @

src/lib/MacWrtProStructures.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 <algorithm>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <map>
39 #include <set>
40 #include <sstream>
41
42 #include <librevenge/librevenge.h>
43
44 #include "MWAWCell.hxx"
45 #include "MWAWTextListener.hxx"
46 #include "MWAWFont.hxx"
47 #include "MWAWFontConverter.hxx"
48 #include "MWAWHeader.hxx"
49 #include "MWAWParagraph.hxx"
50 #include "MWAWPosition.hxx"
51 #include "MWAWSection.hxx"
52 #include "MWAWTable.hxx"
53
54 #include "MacWrtProStructures.hxx"
55 #include "MacWrtProParser.hxx"
56
57 /** Internal: the structures of a MacWrtProStructures */
58 namespace MacWrtProStructuresInternal
59 {
60 //! Internal: the graphic structure
61 struct Graphic {
62 enum Type { UNKNOWN, GRAPHIC, TEXT, NOTE };
63 //! the constructor
64 explicit Graphic(int vers)
65 : m_version(vers)
66 , m_type(-1)
67 , m_contentType(UNKNOWN)
68 , m_fileBlock(0)
69 , m_id(-1)
70 , m_attachment(false)
71 , m_page(-1)
72 , m_box()
73 , m_textPos(0)
74 // II
75 , m_textboxType(0)
76 , m_headerFooterFlag(0)
77 , m_column(1)
78 , m_colSeparator(0)
79 , m_lastFlag(0)
80 // 1 or 1.5
81 , m_baseline(0.)
82 , m_surfaceColor(MWAWColor::white())
83 , m_lineBorder()
84 , m_isHeader(false)
85 , m_row(0)
86 , m_col(0)
87 , m_textboxCellType(0)
88 // all
89 , m_extra("")
90 , m_send(false)
91 {
92 for (auto &w : m_borderWList) w= 0;
93 }
94
95 //! operator<<
96 friend std::ostream &operator<<(std::ostream &o, Graphic const &bl)
97 {
98 if (bl.m_version==0) {
99 switch (std::abs(bl.m_type)) {
100 case 3:
101 o << "textbox,";
102 break;
103 default:
104 o << "type=" << bl.m_type << ",";
105 break;
106 }
107 switch (bl.m_textboxType) {
108 case 1: // a header zone
109 o << "header,";
110 break;
111 case 2: // a footer zone
112 o << "footer,";
113 break;
114 case 3: // a footnote zone
115 o << "footnote,";
116 break;
117 case 0:
118 break;
119 default:
120 MWAW_DEBUG_MSG(("MacWrtProStructures::Graphic::operator<<: find unknown textbox type\n"));
121 o << "##fram[type]=" << bl.m_textboxType << ",";
122 break;
123 }
124 switch (bl.m_headerFooterFlag) {
125 case 1:
126 o << "left[page],";
127 break;
128 case 2:
129 o << "right[page],";
130 break;
131 case 3:
132 o << "###page=" << bl.m_headerFooterFlag << ",";
133 break;
134 case 0:
135 default: // all
136 break;
137 }
138 if (bl.m_textPos) o << "textPos=" << bl.m_textPos << ",";
139 if (bl.m_type<0) o << "background,";
140 }
141 else {
142 switch (bl.m_contentType) {
143 case GRAPHIC:
144 o << "graphic,";
145 if (bl.m_type != 8) {
146 MWAW_DEBUG_MSG(("MacWrtProStructuresInternal::Graphic::operator<< unknown type\n"));
147 o << "#type=" << bl.m_type << ",";
148 }
149 break;
150 case NOTE:
151 o << "note";
152 break;
153 case TEXT:
154 o << "text";
155 switch (bl.m_type) {
156 case 3:
157 o << "[table]";
158 break;
159 case 4:
160 o << "[textbox/cell/note]";
161 break;
162 case 5:
163 if (bl.m_textPos) o << "[pageBreak:" << bl.m_textPos << "]";
164 break;
165 case 6:
166 if (bl.m_isHeader)
167 o << "[header]";
168 else
169 o << "[footer]";
170 break;
171 case 7:
172 o << "[footnote]";
173 break;
174 case 8:
175 o << "[empty frame]";
176 break;
177 default:
178 // v2: -1: can be the document zone, but not always
179 MWAW_DEBUG_MSG(("MacWrtProStructuresInternal::Graphic::operator<< unknown type\n"));
180 o << "[#" << bl.m_type << "]";
181 break;
182 }
183 o << ",";
184 break;
185 case UNKNOWN:
186 #if !defined(__clang__)
187 default:
188 #endif
189 break;
190 }
191 }
192 if (bl.m_column>1) {
193 o << "col[num]=" << bl.m_column << ",";
194 o << "col[sep]=" << bl.m_colSeparator << ",";
195 }
196 if (bl.m_id >= 0) o << "id=" << bl.m_id << ",";
197 o << "box=" << bl.m_box << ",";
198 static char const *wh[] = { "L", "R", "T", "B" };
199 if (bl.hasSameBorders()) {
200 if (bl.m_borderWList[0] > 0)
201 o << "bord[width]=" << bl.m_borderWList[0] << ",";
202 }
203 else {
204 for (int i = 0; i < 4; ++i) {
205 if (bl.m_borderWList[i] <= 0)
206 continue;
207 o << "bord" << wh[i] << "[width]=" << bl.m_borderWList[i] << ",";
208 }
209 }
210 if (bl.m_contentType==TEXT && bl.m_type==4) {
211 for (int i = 0; i < 4; ++i)
212 o << "bord" << wh[i] << "[cell]=[" << bl.m_borderCellList[i] << "],";
213 }
214 if (bl.m_baseline < 0 || bl.m_baseline >0) o << "baseline=" << bl.m_baseline << ",";
215 if (!bl.m_surfaceColor.isWhite())
216 o << "col=" << bl.m_surfaceColor << ",";
217 if (!bl.m_lineBorder.isEmpty())
218 o << "line=" << bl.m_lineBorder << ",";
219 if (bl.m_fileBlock > 0) o << "block=" << std::hex << bl.m_fileBlock << std::dec << ",";
220 if (bl.m_extra.length())
221 o << bl.m_extra << ",";
222 return o;
223 }
224 //! update the style to include frame style
225 void fillFrame(MWAWGraphicStyle &style) const
226 {
227 if (!m_surfaceColor.isWhite())
228 style.setBackgroundColor(m_surfaceColor);
229 if (!hasBorders())
230 return;
231 for (int w=0; w < 4; ++w) {
232 MWAWBorder border(m_lineBorder);
233 border.m_width=m_borderWList[w]; // ok also for setAll
234 if (border.isEmpty())
235 continue;
236 static int const wh[] = { libmwaw::LeftBit, libmwaw::RightBit, libmwaw::TopBit, libmwaw::BottomBit};
237 style.setBorders(wh[w], border);
238 }
239 }
240
241 //! returns true is this is a graphic zone
242 bool isGraphic() const
243 {
244 return m_fileBlock > 0 && m_contentType == GRAPHIC;
245 }
246 //! returns true is this is a text zone (or a not)
247 bool isText() const
248 {
249 return m_fileBlock > 0 && (m_contentType == TEXT || m_contentType == NOTE);
250 }
251 //! returns true is this is a table zone
252 bool isTable() const
253 {
254 return m_fileBlock <= 0 && m_type == 3;
255 }
256 bool hasSameBorders() const
257 {
258 for (int i=1; i < 4; ++i) {
259 if (m_borderWList[i] > m_borderWList[0] ||
260 m_borderWList[i] < m_borderWList[0])
261 return false;
262 }
263 return true;
264 }
265 bool hasBorders() const
266 {
267 if (m_lineBorder.m_color.isWhite() || m_lineBorder.isEmpty())
268 return false;
269 for (auto w : m_borderWList) {
270 if (w > 0)
271 return true;
272 }
273 return false;
274 }
275
276 MWAWPosition getPosition() const
277 {
278 MWAWPosition res;
279 if (m_attachment) {
280 res = MWAWPosition(MWAWVec2f(0,0), m_box.size(), librevenge::RVNG_POINT);
281 res.setRelativePosition(MWAWPosition::Char, MWAWPosition::XLeft, getRelativeYPos());
282 }
283 else {
284 res = MWAWPosition(m_box.min(), m_box.size(), librevenge::RVNG_POINT);
285 res.setRelativePosition(MWAWPosition::Page);
286 res.setPage(m_page);
287 res.m_wrapping = m_contentType==NOTE ? MWAWPosition::WRunThrough :
288 MWAWPosition::WDynamic;
289 }
290 return res;
291 }
292
293 MWAWPosition::YPos getRelativeYPos() const
294 {
295 float height = m_box.size()[1];
296 if (m_baseline < 0.25f*height) return MWAWPosition::YBottom;
297 if (m_baseline < 0.75f*height) return MWAWPosition::YCenter;
298 return MWAWPosition::YTop;
299 }
300 bool contains(MWAWBox2f const &box) const
301 {
302 return box[0][0] >= m_box[0][0] && box[0][1] >= m_box[0][1] &&
303 box[1][0] <= m_box[1][0] && box[1][1] <= m_box[1][1];
304 }
305 bool intersects(MWAWBox2f const &box) const
306 {
307 if (box[0][0] >= m_box[1][0] || box[0][1] >= m_box[1][1] ||
308 box[1][0] <= m_box[0][0] || box[1][1] <= m_box[0][1])
309 return false;
310 return true;
311 }
312
313 //! the version
314 int m_version;
315
316 //! the type
317 int m_type;
318
319 //! the type 1.0 or 1.5
320 Type m_contentType;
321
322 //! the file block id
323 int m_fileBlock;
324
325 //! the graphic id
326 int m_id;
327
328 //! true if this is an attachment 1.0 or 1.5
329 bool m_attachment;
330
331 //! the page (if absolute)
332 int m_page;
333
334 //! the bdbox
335 MWAWBox2f m_box;
336
337 /** filled for pagebreak pos */
338 int m_textPos;
339
340 // vII
341
342 //! the header footer type
343 int m_textboxType;
344 //! the header footer flag
345 int m_headerFooterFlag;
346
347 //! the number of columns
348 int m_column;
349 //! the columns separator
350 float m_colSeparator;
351
352
353 /** the last flag */
354 int m_lastFlag;
355
356 //
357 // v1.0 or v.15
358 //
359
360 //! the borders width
361 double m_borderWList[4];
362
363 //! the cell borders
364 MWAWBorder m_borderCellList[4];
365
366 //! the baseline ( in point 0=bottom aligned)
367 float m_baseline;
368
369 //! the background color
370 MWAWColor m_surfaceColor;
371
372 //! the line border
373 MWAWBorder m_lineBorder;
374
375 /** filled for header/footer */
376 bool m_isHeader;
377
378 /** number of row, filled for table */
379 int m_row;
380
381 /** number of columns, filled for table */
382 int m_col;
383
384 /** filled for textbox : 0: unknown/textbox, 1: cell, 2: textbox(opened)*/
385 int m_textboxCellType;
386
387 //! extra data
388 std::string m_extra;
389
390 //! true if we have send the data
391 bool m_send;
392 };
393
394 //! Internal: a page
395 struct Page {
396 //! the constructor
397 Page()
398 : m_page(-1)
399 , m_graphicsList()
400 , m_extra("")
401 , m_send(false)
402 {
403 }
404 //! operator<<
405 friend std::ostream &operator<<(std::ostream &o, Page const &bl)
406 {
407 if (bl.m_page > 0) o << "page=" << bl.m_page << ",";
408 if (bl.m_extra.length())
409 o << bl.m_extra << ",";
410 return o;
411 }
412 //! the page (if absolute)
413 int m_page;
414 //! the graphic list
415 std::vector<std::shared_ptr<Graphic> > m_graphicsList;
416 //! extra data
417 std::string m_extra;
418
419 //! true if we have send the data
420 bool m_send;
421 };
422
423 ////////////////////////////////////////
424 //! Internal: the fonts
425 struct Font {
426 //! the constructor
427 Font()
428 : m_font()
429 , m_flags(0)
430 , m_token(-1)
431 {
432 for (auto &val : m_values) val = 0;
433 }
434 //! operator<<
435 friend std::ostream &operator<<(std::ostream &o, Font const &font)
436 {
437 if (font.m_flags) o << "flags=" << std::hex << font.m_flags << std::dec << ",";
438 for (int i = 0; i < 5; ++i) {
439 if (!font.m_values[i]) continue;
440 o << "f" << i << "=" << font.m_values[i] << ",";
441 }
442 switch (font.m_token) {
443 case -1:
444 break;
445 default:
446 o << "token=" << font.m_token << ",";
447 break;
448 }
449 return o;
450 }
451
452 //! the font
453 MWAWFont m_font;
454 //! some unknown flag
455 int m_flags;
456 //! the token type(checkme)
457 int m_token;
458 //! unknown values
459 int m_values[5];
460 };
461
462 ////////////////////////////////////////
463 /** Internal: class to store the paragraph properties */
464 struct Paragraph final : public MWAWParagraph {
465 //! Constructor
466 Paragraph()
467 : m_value(0)
468 {
469 }
470 Paragraph(Paragraph const &)=default;
471 Paragraph &operator=(Paragraph const &)=default;
472 Paragraph &operator=(Paragraph &&)=default;
473 //! destructor
474 ~Paragraph() final;
475 //! operator<<
476 friend std::ostream &operator<<(std::ostream &o, Paragraph const &ind)
477 {
478 o << static_cast<MWAWParagraph const &>(ind);
479 if (ind.m_value) o << "unkn=" << ind.m_value << ",";
480 return o;
481 }
482 //! a unknown value
483 int m_value;
484 };
485
486 Paragraph::~Paragraph()
487 {
488 }
489
490 ////////////////////////////////////////
491 //! Internal: the cell of a MacWrtProStructure
492 struct Cell final : public MWAWCell {
493 //! constructor
494 Cell(MacWrtProStructures &parser, Graphic *graphic)
495 : MWAWCell()
496 , m_parser(parser)
497 , m_graphicId(0)
498 {
499 if (!graphic) return;
500 setBdBox(MWAWBox2f(graphic->m_box.min(), graphic->m_box.max()-MWAWVec2f(1,1)));
501 setBackgroundColor(graphic->m_surfaceColor);
502 m_graphicId = graphic->m_id;
503 for (int b=0; b<4; ++b) {
504 int const wh[4] = { libmwaw::LeftBit, libmwaw::RightBit,
505 libmwaw::TopBit, libmwaw::BottomBit
506 };
507 setBorders(wh[b], graphic->m_borderCellList[b]);
508 }
509 }
510 //! destructor
511 ~Cell() final;
512 //! send the content
513 bool sendContent(MWAWListenerPtr listener, MWAWTable &) final
514 {
515 if (m_graphicId > 0)
516 m_parser.send(m_graphicId);
517 else if (listener) // try to avoid empty cell
518 listener->insertChar(' ');
519 return true;
520 }
521
522 //! the text parser
523 MacWrtProStructures &m_parser;
524 //! the graphic id
525 int m_graphicId;
526 };
527
528 Cell::~Cell()
529 {
530 }
531
532 ////////////////////////////////////////
533 ////////////////////////////////////////
534 struct Table final : public MWAWTable {
535 //! constructor
536 Table() : MWAWTable()
537 {
538 }
539 //! destructor
540 ~Table() final;
541 //! return a cell corresponding to id
542 Cell *get(int id)
543 {
544 if (id < 0 || id >= numCells()) {
545 MWAW_DEBUG_MSG(("MacWrtProStructuresInternal::Table::get: cell %d does not exists\n",id));
546 return nullptr;
547 }
548 return static_cast<Cell *>(MWAWTable::get(id).get());
549 }
550 };
551
552 Table::~Table()
553 {
554 }
555
556 ////////////////////////////////////////
557 //! Internal: the section of a MacWrtProStructures
558 struct Section {
559 enum StartType { S_Line, S_Page, S_PageLeft, S_PageRight };
560
561 //! constructor
562 Section()
563 : m_start(S_Page)
564 , m_colsPos()
565 , m_textLength(0)
566 , m_extra("")
567 {
568 for (auto &id : m_headerIds) id=0;
569 for (auto &id : m_footerIds) id=0;
570 }
571 //! returns a MWAWSection
572 MWAWSection getSection() const
573 {
574 MWAWSection sec;
575 size_t numCols=m_colsPos.size()/2;
576 if (numCols <= 1)
577 return sec;
578 sec.m_columns.resize(numCols);
579 float prevPos=0;
580 for (size_t c=0; c < numCols; ++c) {
581 sec.m_columns[c].m_width = double(m_colsPos[2*c+1]-prevPos);
582 prevPos = m_colsPos[2*c+1];
583 sec.m_columns[c].m_widthUnit = librevenge::RVNG_POINT;
584 sec.m_columns[c].m_margins[libmwaw::Right] =
585 double(m_colsPos[2*c+1]-m_colsPos[2*c])/72.;
586 }
587 return sec;
588 }
589 //! return the number of columns
590 int numColumns() const
591 {
592 auto numCols=int(m_colsPos.size()/2);
593 return numCols ? numCols : 1;
594 }
595 //! operator<<
596 friend std::ostream &operator<<(std::ostream &o, Section const &sec)
597 {
598 switch (sec.m_start) {
599 case S_Line:
600 o << "newLine,";
601 break;
602 case S_Page:
603 break;
604 case S_PageLeft:
605 o << "newPage[left],";
606 break;
607 case S_PageRight:
608 o << "newPage[right],";
609 break;
610 #if !defined(__clang__)
611 default:
612 break;
613 #endif
614 }
615 auto nColumns = size_t(sec.numColumns());
616 if (nColumns != 1) {
617 o << "nCols=" << nColumns << ",";
618 o << "colsPos=[";
619 for (size_t i = 0; i < 2*nColumns; i+=2)
620 o << sec.m_colsPos[i] << ":" << sec.m_colsPos[i+1] << ",";
621 o << "],";
622 }
623 if (sec.m_headerIds[0]) o << "sec.headerId=" << sec.m_headerIds[0] << ",";
624 if (sec.m_headerIds[0]!=sec.m_headerIds[1]) o << "sec.headerId1=" << sec.m_headerIds[0] << ",";
625 if (sec.m_footerIds[0]) o << "sec.footerId=" << sec.m_footerIds[0] << ",";
626 if (sec.m_footerIds[0]!=sec.m_footerIds[1]) o << "sec.footerId1=" << sec.m_footerIds[0] << ",";
627 if (sec.m_textLength) o << "nChar=" << sec.m_textLength << ",";
628 if (sec.m_extra.length()) o << sec.m_extra;
629 return o;
630 }
631 //! the way to start the new section
632 StartType m_start;
633 //! the columns position ( series of end columns <-> new column begin)
634 std::vector<float> m_colsPos;
635 //! the header graphic ids
636 int m_headerIds[2];
637 //! the footerer graphic ids
638 int m_footerIds[2];
639 //! the number of character in the sections
640 long m_textLength;
641 //! extra data
642 std::string m_extra;
643 };
644
645 ////////////////////////////////////////
646 //! Internal: the state of a MacWrtProStructures
647 struct State {
648 //! constructor
649 State()
650 : m_version(-1)
651 , m_numPages(1)
652 , m_inputData()
653 , m_fontsList()
654 , m_paragraphsList()
655 , m_pagesList()
656 , m_sectionsList()
657 , m_graphicsList()
658 , m_tablesMap()
659 , m_idGraphicMap()
660 , m_headersMap()
661 , m_footersMap()
662 , m_graphicsSendSet()
663 {
664 }
665
666 //! try to set the line properties of a border
667 static bool updateLineType(int lineType, MWAWBorder &border)
668 {
669 switch (lineType) {
670 case 2:
671 border.m_type=MWAWBorder::Double;
672 border.m_widthsList.resize(3,2.);
673 border.m_widthsList[1]=1.0;
674 break;
675 case 3:
676 border.m_type=MWAWBorder::Double;
677 border.m_widthsList.resize(3,1.);
678 border.m_widthsList[2]=2.0;
679 break;
680 case 4:
681 border.m_type=MWAWBorder::Double;
682 border.m_widthsList.resize(3,1.);
683 border.m_widthsList[0]=2.0;
684 break;
685 case 1: // solid
686 break;
687 default:
688 return false;
689 }
690 return true;
691 }
692
693 //! the file version
694 int m_version;
695
696 //! the number of pages
697 int m_numPages;
698
699 //! the input data
700 librevenge::RVNGBinaryData m_inputData;
701
702 //! the list of fonts
703 std::vector<Font> m_fontsList;
704 //! the list of paragraph
705 std::vector<Paragraph> m_paragraphsList;
706 //! the pages list (for MWII)
707 std::vector<Page> m_pagesList;
708 //! the list of section
709 std::vector<Section> m_sectionsList;
710 //! the list of graphic
711 std::vector<std::shared_ptr<Graphic> > m_graphicsList;
712 //! a map graphic id -> table
713 std::map<int, std::shared_ptr<Table> > m_tablesMap;
714 //! a map graphic id -> graphic
715 std::map<int, std::shared_ptr<Graphic> > m_idGraphicMap;
716 //! a map page -> header id
717 std::map<int, int> m_headersMap;
718 //! a map page -> footer id
719 std::map<int, int> m_footersMap;
720 //! a list of graphic use to avoid potential loop in bad file
721 std::set<MWAWVec2i> m_graphicsSendSet;
722 };
723
724 }
725
726
727 ////////////////////////////////////////////////////////////
728 // constructor/destructor, ...
729 ////////////////////////////////////////////////////////////
730 MacWrtProStructures::MacWrtProStructures(MacWrtProParser &parser)
731 : m_parserState(parser.getParserState())
732 , m_mainParser(parser)
733 , m_state()
734 , m_asciiName()
735 {
736 init();
737 }
738
739 MacWrtProStructures::~MacWrtProStructures()
740 {
741 }
742
743 void MacWrtProStructures::init()
744 {
745 m_state.reset(new MacWrtProStructuresInternal::State);
746 }
747
748 int MacWrtProStructures::version() const
749 {
750 if (m_state->m_version < 0)
751 m_state->m_version = m_parserState->m_version;
752 return m_state->m_version;
753 }
754
755 MWAWTextListenerPtr &MacWrtProStructures::getTextListener()
756 {
757 return m_parserState->m_textListener;
758 }
759
760 int MacWrtProStructures::numPages() const
761 {
762 return m_state->m_numPages;
763 }
764
765 ////////////////////////////////////////////////////////////
766 // update a page span
767 void MacWrtProStructures::updatePageSpan(int page, bool hasTitlePage, MWAWPageSpan &ps)
768 {
769 if (version()==0) {
770 // title page has no header/footer
771 if (hasTitlePage && page==0) {
772 ps.setPageSpan(1);
773 return;
774 }
775 // hf is defined for all pages excepted the title page
776 int index=0;
777 for (size_t i = 0; i < m_state->m_pagesList.size(); ++i) {
778 if (i>=2) break;
779 auto const &pge = m_state->m_pagesList[i];
780 for (auto const &graphic : pge.m_graphicsList) {
781 if (graphic->m_textboxType<1 || graphic->m_textboxType>2) continue;
782 m_state->m_idGraphicMap[++index]=graphic;
783 MWAWHeaderFooter hf(graphic->m_textboxType==1 ? MWAWHeaderFooter::HEADER : MWAWHeaderFooter::FOOTER,
784 graphic->m_headerFooterFlag==1 ? MWAWHeaderFooter::EVEN : graphic->m_headerFooterFlag==2 ? MWAWHeaderFooter::ODD : MWAWHeaderFooter::ALL);
785 hf.m_subDocument=m_mainParser.getSubDocument(index);
786 ps.setHeaderFooter(hf);
787 }
788 }
789 ps.setPageSpan(m_state->m_numPages>page ? m_state->m_numPages-page : 100);
790 return;
791 }
792 ++page;
793 int numSimilar[2]= {1,1};
794 for (int st=0; st<2; ++st) {
795 auto const &map=st==0 ? m_state->m_headersMap : m_state->m_footersMap;
796 auto it=map.lower_bound(page);
797 if (it==map.end()) {
798 if (m_state->m_numPages>page)
799 numSimilar[st]=m_state->m_numPages-page+1;
800 continue;
801 }
802 if (it->first!=page) {
803 numSimilar[st]=it->first-page;
804 continue;
805 }
806 int id=it->second;
807 while (++it!=map.end() && it->second==id)
808 numSimilar[st]++;
809 if (!id) continue;
810 MWAWHeaderFooter hf(st==0 ? MWAWHeaderFooter::HEADER : MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
811 hf.m_subDocument=m_mainParser.getSubDocument(id);
812 ps.setHeaderFooter(hf);
813 }
814 ps.setPageSpan(std::min<int>(numSimilar[0],numSimilar[1]));
815 }
816
817 ////////////////////////////////////////////////////////////
818 // try to return the color/pattern, set the line properties
819 bool MacWrtProStructures::getColor(int colId, MWAWColor &color) const
820 {
821 if (version()==0) {
822 // MWII: 2:red 4: blue, ..
823 switch (colId) {
824 case 0:
825 color = 0xFFFFFF;
826 break;
827 case 1:
828 color = 0;
829 break;
830 case 2:
831 color = 0xFF0000;
832 break;
833 case 3:
834 color = 0x00FF00;
835 break;
836 case 4:
837 color = 0x0000FF;
838 break;
839 case 5:
840 color = 0x00FFFF;
841 break; // cyan
842 case 6:
843 color = 0xFF00FF;
844 break; // magenta
845 case 7:
846 color = 0xFFFF00;
847 break; // yellow
848 default:
849 MWAW_DEBUG_MSG(("MacWrtProStructures::getColor: unknown color %d\n", colId));
850 return false;
851 }
852 }
853 else {
854 /* 0: white, 38: yellow, 44: magenta, 36: red, 41: cyan, 39: green, 42: blue
855 checkme: this probably corresponds to the following 81 gray/color palette...
856 */
857 uint32_t const colorMap[] = {
858 0xFFFFFF, 0x0, 0x222222, 0x444444, 0x666666, 0x888888, 0xaaaaaa, 0xcccccc, 0xeeeeee,
859 0x440000, 0x663300, 0x996600, 0x002200, 0x003333, 0x003399, 0x000055, 0x330066, 0x660066,
860 0x770000, 0x993300, 0xcc9900, 0x004400, 0x336666, 0x0033ff, 0x000077, 0x660099, 0x990066,
861 0xaa0000, 0xcc3300, 0xffcc00, 0x006600, 0x006666, 0x0066ff, 0x0000aa, 0x663399, 0xcc0099,
862 0xdd0000, 0xff3300, 0xffff00, 0x008800, 0x009999, 0x0099ff, 0x0000dd, 0x9900cc, 0xff0099,
863 0xff3333, 0xff6600, 0xffff33, 0x00ee00, 0x00cccc, 0x00ccff, 0x3366ff, 0x9933ff, 0xff33cc,
864 0xff6666, 0xff6633, 0xffff66, 0x66ff66, 0x66cccc, 0x66ffff, 0x3399ff, 0x9966ff, 0xff66ff,
865 0xff9999, 0xff9966, 0xffff99, 0x99ff99, 0x66ffcc, 0x99ffff, 0x66ccff, 0x9999ff, 0xff99ff,
866 0xffcccc, 0xffcc99, 0xffffcc, 0xccffcc, 0x99ffcc, 0xccffff, 0x99ccff, 0xccccff, 0xffccff
867 };
868 if (colId < 0 || colId >= 81) {
869 MWAW_DEBUG_MSG(("MacWrtProStructures::getColor: unknown color %d\n", colId));
870 return false;
871 }
872 color = colorMap[colId];
873 }
874 return true;
875 }
876
877 bool MacWrtProStructures::getPattern(int patId, float &patternPercent) const
878 {
879 patternPercent=1.0f;
880 if (version()==0) // not implemented
881 return false;
882 static float const defPercentPattern[64] = {
883 0.0f, 1.0f, 0.968750f, 0.93750f, 0.8750f, 0.750f, 0.50f, 0.250f,
884 0.250f, 0.18750f, 0.18750f, 0.1250f, 0.06250f, 0.06250f, 0.031250f, 0.015625f,
885 0.750f, 0.50f, 0.250f, 0.3750f, 0.250f, 0.1250f, 0.250f, 0.1250f,
886 0.750f, 0.50f, 0.250f, 0.3750f, 0.250f, 0.1250f, 0.250f, 0.1250f,
887 0.750f, 0.50f, 0.50f, 0.50f, 0.50f, 0.250f, 0.250f, 0.234375f,
888 0.6250f, 0.3750f, 0.1250f, 0.250f, 0.218750f, 0.218750f, 0.1250f, 0.093750f,
889 0.50f, 0.56250f, 0.43750f, 0.3750f, 0.218750f, 0.281250f, 0.18750f, 0.093750f,
890 0.593750f, 0.56250f, 0.515625f, 0.343750f, 0.31250f, 0.250f, 0.250f, 0.234375f,
891 };
892 if (patId <= 0 || patId>64) {
893 MWAW_DEBUG_MSG(("MacWrtProStructures::getPattern: unknown pattern %d\n", patId));
894 return false;
895 }
896 patternPercent=defPercentPattern[patId-1];
897 return true;
898 }
899
900 bool MacWrtProStructures::getColor(int colId, int patId, MWAWColor &color) const
901 {
902 if (!getColor(colId, color))
903 return false;
904 if (patId==0)
905 return true;
906 float percent;
907 if (!getPattern(patId,percent))
908 return false;
909 color=MWAWColor::barycenter(percent,color,1.f-percent,MWAWColor::white());
910 return true;
911 }
912
913 ////////////////////////////////////////////////////////////
914 //
915 // Intermediate level
916 //
917 ////////////////////////////////////////////////////////////
918 bool MacWrtProStructures::createZones(std::shared_ptr<MWAWStream> &stream, int numPages)
919 {
920 if (!stream)
921 return false;
922 if (version() == 0)
923 return createZonesII(stream, numPages);
924
925 auto &input=stream->m_input;
926 auto &ascFile=stream->m_ascii;
927 long pos;
928
929 bool ok = readStyles(stream) && readCharStyles(stream);
930 if (ok) {
931 pos = input->tell();
932 if (!readSelection(stream)) {
933 ascFile.addPos(pos);
934 ascFile.addNote("Entries(Selection):#");
935 input->seek(pos+16, librevenge::RVNG_SEEK_SET);
936 }
937
938 pos = input->tell();
939 ok = readFontsName(stream);
940 if (!ok) {
941 ascFile.addPos(pos);
942 ascFile.addNote("Entries(FontsName):#");
943 }
944 }
945 if (ok) {
946 pos = input->tell();
947 ok = readStructB(stream);
948 if (!ok) {
949 ascFile.addPos(pos);
950 ascFile.addNote("Entries(StructB):#");
951 }
952 }
953 if (ok) {
954 pos = input->tell();
955 ok = readFontsDef(stream);
956 if (!ok) {
957 ascFile.addPos(pos);
958 ascFile.addNote("Entries(FontsDef):#");
959 }
960 }
961 if (ok) {
962 pos = input->tell();
963 ok = readParagraphs(stream);
964 if (!ok) {
965 ascFile.addPos(pos);
966 ascFile.addNote("Entries(ParaZone):#");
967 }
968 }
969 for (int st = 0; st < 2; ++st) {
970 if (!ok) break;
971 pos = input->tell();
972 std::vector<MacWrtProStructuresInternal::Section> sections;
973 ok = readSections(stream, sections);
974 if (!ok) {
975 ascFile.addPos(pos);
976 ascFile.addNote("Entries(Sections):#");
977 break;
978 }
979 if (st == 0) continue;
980 m_state->m_sectionsList = sections;
981 }
982 if (ok) {
983 pos = input->tell();
984 libmwaw::DebugStream f;
985 f << "Entries(UserName):";
986 // username,
987 std::string res;
988 for (int i = 0; i < 2; ++i) {
989 ok = readString(input, res);
990 if (!ok) {
991 f << "#" ;
992 break;
993 }
994 f << "'" << res << "',";
995 }
996 ascFile.addPos(pos);
997 ascFile.addNote(f.str().c_str());
998 }
999 if (ok) {
1000 pos = input->tell();
1001 ok = readGraphicsList(stream, numPages);
1002 if (!ok) {
1003 ascFile.addPos(pos);
1004 ascFile.addNote("Entries(Graphic):#");
1005 }
1006 }
1007
1008 pos = input->tell();
1009 ascFile.addPos(pos);
1010 ascFile.addNote("Entries(End)");
1011
1012 // ok, now we can build the structures
1013 buildPageStructures();
1014 buildTableStructures();
1015
1016 return true;
1017 }
1018
1019 bool MacWrtProStructures::createZonesII(std::shared_ptr<MWAWStream> &stream, int numPages)
1020 {
1021 if (!stream) return false;
1022 if (version()) {
1023 MWAW_DEBUG_MSG(("MacWrtProStructures::createZonesII: must be called for a MacWriteII file\n"));
1024 return false;
1025 }
1026 libmwaw::DebugStream f;
1027 auto &input=stream->m_input;
1028 auto &ascFile=stream->m_ascii;
1029 long pos;
1030 bool ok = readFontsName(stream);
1031 if (ok) {
1032 pos = input->tell();
1033 long val = long(input->readULong(4));
1034 if (val) {
1035 MWAW_DEBUG_MSG(("MacWrtProStructures::createZonesII: argh!!! find data after the fonts name zone. Trying to continue.\n"));
1036 f.str("");
1037 // in QuarkXPress color
1038 f << "Entries(Color):#" << std::hex << val << std::dec;
1039
1040 ascFile.addPos(pos);
1041 ascFile.addNote(f.str().c_str());
1042 }
1043 else {
1044 ascFile.addPos(pos);
1045 ascFile.addNote("_");
1046 }
1047 ok = readCharStyles(stream);
1048 }
1049 if (ok)
1050 ok = readFontsDef(stream);
1051 if (ok)
1052 ok = readParagraphs(stream);
1053 // FIXME: this code is bad, look for XPressGraph::readPagesListII which is very simillar
1054 if (ok)
1055 readPagesListII(stream, numPages);
1056
1057 pos = input->tell();
1058 if (input->checkPosition(pos+256)) {
1059 MWAW_DEBUG_MSG(("MacWrtProStructures::createZonesII: oops, probable problem when reading the pages...\n"));
1060 }
1061 ascFile.addPos(pos);
1062 ascFile.addNote("Entries(Page)[End]:");
1063
1064 int nPages = numPages;
1065 int nFootnotes=0;
1066 for (auto const &pages : m_state->m_pagesList) {
1067 bool mainBlock=true;
1068 for (auto const &graphic : pages.m_graphicsList) {
1069 if (graphic->m_fileBlock>0) {
1070 m_mainParser.parseDataZone(graphic->m_fileBlock, 0);
1071 if (mainBlock)
1072 nPages += m_mainParser.findNumHardBreaks(graphic->m_fileBlock);
1073 }
1074 if (graphic->m_textboxType==3) // footnote
1075 m_state->m_idGraphicMap[--nFootnotes]=graphic;
1076 mainBlock=false;
1077 }
1078 }
1079 m_state->m_numPages = nPages;
1080
1081 return true;
1082 }
1083
1084 ////////////////////////////////////////////////////////////
1085 // try to find the main text zone and sent it
1086 bool MacWrtProStructures::sendMainZone()
1087 {
1088 int vers = version();
1089 if (vers==0) {
1090 if (m_state->m_pagesList.size()>=3) {
1091 auto const &page=m_state->m_pagesList[2];
1092 if (!page.m_graphicsList.empty()) {
1093 m_state->m_idGraphicMap[0]=page.m_graphicsList[0];
1094 return send(0, true);
1095 }
1096 }
1097 }
1098 else {
1099 for (size_t i = 0; i < m_state->m_graphicsList.size(); ++i) {
1100 auto graphic = m_state->m_graphicsList[i];
1101 if (!graphic->isText() || graphic->m_send) continue;
1102 if (vers == 1 && graphic->m_type != 5)
1103 continue;
1104 return send(graphic->m_id, true);
1105 }
1106 }
1107 //ok the main zone can be empty
1108 std::shared_ptr<MacWrtProStructures> THIS
1109 (this, MWAW_shared_ptr_noop_deleter<MacWrtProStructures>());
1110 MacWrtProStructuresListenerState listenerState(THIS, true, vers);
1111 return true;
1112 }
1113
1114 ////////////////////////////////////////////////////////////
1115 // try to find the header and the pages break
1116 void MacWrtProStructures::buildPageStructures()
1117 {
1118 // first find the pages break
1119 std::set<long> set;
1120 int actPage = 0;
1121 for (auto const &graphic : m_state->m_graphicsList) {
1122 graphic->m_page = actPage ? actPage : 1; // mainly ok
1123 if (graphic->m_type != 5)
1124 continue;
1125 actPage++;
1126 set.insert(graphic->m_textPos);
1127 }
1128 long actSectPos = 0;
1129 for (auto const &sec : m_state->m_sectionsList) {
1130 if (sec.m_start != sec.S_Line) set.insert(actSectPos);
1131 actSectPos += sec.m_textLength;
1132 }
1133 std::vector<int> pagesBreak;
1134 pagesBreak.assign(set.begin(), set.end());
1135
1136 // now associates the header/footer to each pages
1137 int nPages = m_state->m_numPages = int(pagesBreak.size());
1138 int actPagePos = 0;
1139 actPage = 0;
1140 actSectPos = 0;
1141 for (auto const &sec : m_state->m_sectionsList) {
1142 std::vector<int> listPages;
1143 actSectPos += sec.m_textLength;
1144 while (actPagePos < actSectPos) {
1145 listPages.push_back(actPage);
1146 if (actPage >= nPages-1 || pagesBreak[size_t(actPage+1)] > actSectPos)
1147 break;
1148 actPagePos=pagesBreak[size_t(++actPage)];
1149 }
1150 int headerId = 0, footerId = 0;
1151 for (int k = 0; k < 2; ++k) {
1152 if (sec.m_headerIds[k])
1153 headerId = sec.m_headerIds[k];
1154 if (sec.m_footerIds[k])
1155 footerId = sec.m_footerIds[k];
1156 }
1157 if (!headerId && !footerId) continue;
1158 for (auto p : listPages) {
1159 ++p;
1160 if (headerId && m_state->m_headersMap.find(p) == m_state->m_headersMap.end())
1161 m_state->m_headersMap[p] = headerId;
1162 if (footerId)
1163 m_state->m_footersMap[p] = footerId;
1164 }
1165 }
1166 // finally mark the attachment
1167 auto const &listCalled = m_mainParser.getGraphicIdCalledByToken();
1168 for (auto id : listCalled) {
1169 if (m_state->m_idGraphicMap.find(id) == m_state->m_idGraphicMap.end()) {
1170 MWAW_DEBUG_MSG(("MacWrtProStructures::buildPageStructures: can not find attachment block %d...\n",
1171 id));
1172 continue;
1173 }
1174 auto graphic = m_state->m_idGraphicMap.find(id)->second;
1175 graphic->m_attachment = true;
1176 }
1177 }
1178
1179 ////////////////////////////////////////////////////////////
1180 // try to find the main text zone and sent it
1181 void MacWrtProStructures::buildTableStructures()
1182 {
1183 size_t numGraphics = m_state->m_graphicsList.size();
1184 for (size_t i = 0; i < numGraphics; ++i) {
1185 if (m_state->m_graphicsList[i]->m_type != 3)
1186 continue;
1187 auto table = m_state->m_graphicsList[i];
1188 std::vector<std::shared_ptr<MacWrtProStructuresInternal::Graphic> > graphicList;
1189 size_t j = i+1;
1190 for (; j < numGraphics; ++j) {
1191 auto cell = m_state->m_graphicsList[j];
1192 if (cell->m_type != 4)
1193 break;
1194 if (!table->contains(cell->m_box))
1195 break;
1196 bool ok = true;
1197 for (auto const &graphic : graphicList) {
1198 if (cell->intersects(graphic->m_box)) {
1199 ok = false;
1200 break;
1201 }
1202 }
1203 if (!ok)
1204 break;
1205 graphicList.push_back(cell);
1206 }
1207 if (j-1 >= i) i = j-1;
1208
1209 size_t numCells = graphicList.size();
1210 bool ok = numCells > 1;
1211 if (!ok && numCells == 1)
1212 ok = table->m_col == 1 && table->m_row == 1;
1213 if (!ok) {
1214 MWAW_DEBUG_MSG(("MacWrtProStructures::buildTableStructures: find a table with %ld cells : ignored...\n", long(numCells)));
1215 continue;
1216 }
1217
1218 std::shared_ptr<MacWrtProStructuresInternal::Table> newTable(new MacWrtProStructuresInternal::Table);
1219 for (auto const &graphic : graphicList) {
1220 graphic->m_send = true;
1221 graphic->m_attachment = true;
1222 graphic->m_textboxCellType=1;
1223 newTable->add(std::shared_ptr<MacWrtProStructuresInternal::Cell>
1224 (new MacWrtProStructuresInternal::Cell(*this, graphic.get())));
1225 }
1226 m_state->m_tablesMap[table->m_id]=newTable;
1227 }
1228 }
1229
1230 ////////////////////////////////////////////////////////////
1231 //
1232 // Low level
1233 //
1234 ////////////////////////////////////////////////////////////
1235
1236 ////////////////////////////////////////////////////////////
1237 // read the font names
1238 bool MacWrtProStructures::readFontsName(std::shared_ptr<MWAWStream> &stream)
1239 {
1240 auto &input = stream->m_input;
1241 auto &ascFile = stream->m_ascii;
1242 long pos = input->tell();
1243 libmwaw::DebugStream f;
1244
1245 auto sz = long(input->readULong(4));
1246 if (sz == 0) {
1247 ascFile.addPos(pos);
1248 ascFile.addNote("_");
1249 return true;
1250 }
1251 int vers = version();
1252 long endPos = pos+4+sz;
1253 if (!stream->checkPosition(endPos)) {
1254 MWAW_DEBUG_MSG(("MacWrtProStructures::readFontsName: file is too short\n"));
1255 input->seek(pos, librevenge::RVNG_SEEK_SET);
1256 return false;
1257 }
1258 f << "Entries(FontsName):";
1259 auto N=static_cast<int>(input->readULong(2));
1260 if (3*N+2 > sz) {
1261 MWAW_DEBUG_MSG(("MacWrtProStructures::readFontsName: can not read the number of fonts\n"));
1262 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1263 f << "#";
1264 ascFile.addPos(pos);
1265 ascFile.addNote(f.str().c_str());
1266 return true;
1267 }
1268
1269 for (int ft = 0; ft < N; ++ft) {
1270 auto fId = static_cast<int>(input->readLong(2));
1271 f << "[id=" << fId << ",";
1272 for (int st = 0; st < 2; ++st) {
1273 auto sSz = static_cast<int>(input->readULong(1));
1274 if (long(input->tell())+sSz > endPos) {
1275 MWAW_DEBUG_MSG(("MacWrtProStructures::readFontsName: can not read the %d font\n", ft));
1276 f << "#";
1277 break;
1278 }
1279 std::string name("");
1280 for (int i = 0; i < sSz; ++i)
1281 name += char(input->readULong(1));
1282 if (name.length()) {
1283 if (st == 0)
1284 m_parserState->m_fontConverter->setCorrespondance(fId, name);
1285 f << name << ",";
1286 }
1287 if (vers)
1288 break;
1289 }
1290 f << "],";
1291 }
1292
1293 if (long(input->tell()) != endPos)
1294 ascFile.addDelimiter(input->tell(),'|');
1295 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1296
1297 ascFile.addPos(pos);
1298 ascFile.addNote(f.str().c_str());
1299 return true;
1300 }
1301
1302 ////////////////////////////////////////////////////////////
1303 // read the character properties
1304 bool MacWrtProStructures::readFontsDef(std::shared_ptr<MWAWStream> &stream)
1305 {
1306 auto &input = stream->m_input;
1307 auto &ascFile = stream->m_ascii;
1308 long pos = input->tell();
1309 libmwaw::DebugStream f;
1310
1311 auto sz = long(input->readULong(4));
1312 if (sz == 0) {
1313 ascFile.addPos(pos);
1314 ascFile.addNote("_");
1315 return true;
1316 }
1317 long endPos = pos+4+sz;
1318 int expectedSize = version()==0 ? 10 : 20;
1319 if ((sz%expectedSize) != 0 || !stream->checkPosition(endPos)) {
1320 MWAW_DEBUG_MSG(("MacWrtProStructures::readFontsDef: find an odd value for sz\n"));
1321 input->seek(pos, librevenge::RVNG_SEEK_SET);
1322 return false;
1323 }
1324
1325 f << "Entries(FontsDef):";
1326 auto N = int(sz/expectedSize);
1327 f << "N=" << N;
1328 ascFile.addPos(pos);
1329 ascFile.addNote(f.str().c_str());
1330
1331 m_state->m_fontsList.resize(0);
1332 for (int n = 0; n < N; ++n) {
1333 pos = input->tell();
1334 MacWrtProStructuresInternal::Font font;
1335 if (!readFont(stream, font)) {
1336 ascFile.addPos(pos);
1337 ascFile.addNote("FontsDef-#");
1338 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1339 return true;
1340 }
1341 m_state->m_fontsList.push_back(font);
1342 f.str("");
1343 f << "FontsDef-C" << n << ":";
1344 f << font.m_font.getDebugString(m_parserState->m_fontConverter) << font << ",";
1345 ascFile.addPos(pos);
1346 ascFile.addNote(f.str().c_str());
1347 }
1348 return true;
1349 }
1350
1351 bool MacWrtProStructures::readFont(std::shared_ptr<MWAWStream> &stream, MacWrtProStructuresInternal::Font &font)
1352 {
1353 auto &input = stream->m_input;
1354 long pos = input->tell();
1355 int vers = version();
1356 libmwaw::DebugStream f;
1357 font = MacWrtProStructuresInternal::Font();
1358 font.m_values[0] = static_cast<int>(input->readLong(2)); // 1, 3 or 6
1359 auto val = static_cast<int>(input->readULong(2));
1360 if (val != 0xFFFF)
1361 font.m_font.setId(val);
1362 val = static_cast<int>(input->readULong(2));
1363 if (val != 0xFFFF)
1364 font.m_font.setSize(float(val)/4.f);
1365 if (vers >= 1)
1366 font.m_values[1] = static_cast<int>(input->readLong(2));
1367 auto flag = long(input->readULong(2));
1368 uint32_t flags=0;
1369 if (flag&0x1) flags |= MWAWFont::boldBit;
1370 if (flag&0x2) flags |= MWAWFont::italicBit;
1371 if (flag&0x4) font.m_font.setUnderlineStyle(MWAWFont::Line::Simple);
1372 if (flag&0x8) flags |= MWAWFont::embossBit;
1373 if (flag&0x10) flags |= MWAWFont::shadowBit;
1374 if (flag&0x20) font.m_font.set(MWAWFont::Script(40,librevenge::RVNG_PERCENT));
1375 if (flag&0x40) font.m_font.set(MWAWFont::Script(-40,librevenge::RVNG_PERCENT));
1376 if (flag&0x100) font.m_font.set(MWAWFont::Script::super());
1377 if (flag&0x200) font.m_font.setStrikeOutStyle(MWAWFont::Line::Simple);
1378 if (flag&0x400) flags |= MWAWFont::uppercaseBit;
1379 if (flag&0x800) flags |= MWAWFont::smallCapsBit;
1380 if (flag&0x1000) font.m_font.setUnderlineStyle(MWAWFont::Line::Simple);
1381 if (flag&0x2000) {
1382 font.m_font.setUnderlineStyle(MWAWFont::Line::Simple);
1383 font.m_font.setUnderlineType(MWAWFont::Line::Double);
1384 }
1385 if (flag&0x4000) flags |= MWAWFont::lowercaseBit;
1386 font.m_flags = (flag&0x8080L);
1387
1388 auto color = static_cast<int>(input->readULong(1));
1389 MWAWColor col;
1390 if (color != 1 && getColor(color, col))
1391 font.m_font.setColor(col);
1392 else if (color != 1)
1393 f << "#colId=" << color << ",";
1394 val = static_cast<int>(input->readULong(1)); // always 0x64 (unused?)
1395 if (val != 0x64) font.m_values[2] = val;
1396 if (vers == 1) {
1397 auto lang = static_cast<int>(input->readLong(2));
1398 switch (lang) {
1399 case 0:
1400 font.m_font.setLanguage("en_US");
1401 break;
1402 case 2:
1403 font.m_font.setLanguage("en_GB");
1404 break;
1405 case 3:
1406 font.m_font.setLanguage("de");
1407 break;
1408 default:
1409 f << "#lang=" << lang << ",";
1410 break;
1411 }
1412 font.m_token = static_cast<int>(input->readLong(2));
1413 auto spacings = static_cast<int>(input->readLong(2));
1414 if (spacings) {
1415 if (spacings < -50 || spacings > 100) {
1416 MWAW_DEBUG_MSG(("MacWrtProStructures::readFont: character spacings seems odd\n"));
1417 f << "#spacings=" << spacings << "%,";
1418 spacings = spacings < 0 ? -50 : 100;
1419 }
1420 float fSz = font.m_font.size();
1421 if (fSz <= 0) {
1422 MWAW_DEBUG_MSG(("MacWrtProStructures::readFont: expand called without fSize, assume 12pt\n"));
1423 fSz = 12;
1424 }
1425 font.m_font.setDeltaLetterSpacing(fSz*float(spacings)/100.f);
1426 }
1427 for (int i = 4; i < 5; ++i)
1428 font.m_values[i] = static_cast<int>(input->readLong(2));
1429 input->seek(pos+20, librevenge::RVNG_SEEK_SET);
1430 }
1431 font.m_font.setFlags(flags);
1432 font.m_font.m_extra = f.str();
1433
1434 return true;
1435 }
1436
1437 ////////////////////////////////////////////////////////////
1438 // read a paragraph and a list of paragraph
1439 bool MacWrtProStructures::readParagraphs(std::shared_ptr<MWAWStream> &stream)
1440 {
1441 auto &input = stream->m_input;
1442 auto &ascFile = stream->m_ascii;
1443 long pos = input->tell();
1444 libmwaw::DebugStream f;
1445 int dataSz = version()==0 ? 202 : 192;
1446
1447 auto sz = long(input->readULong(4));
1448 if (sz == 0) {
1449 ascFile.addPos(pos);
1450 ascFile.addNote("_");
1451 return true;
1452 }
1453 long endPos = pos+sz;
1454 if ((sz%dataSz) != 0 || !stream->checkPosition(endPos)) {
1455 MWAW_DEBUG_MSG(("MacWrtProStructures::readParagraphs: find an odd value for sz\n"));
1456 input->seek(pos, librevenge::RVNG_SEEK_SET);
1457 return false;
1458 }
1459
1460 f << "Entries(ParaZone):";
1461 auto N = int(sz/dataSz);
1462 f << "N=" << N;
1463 ascFile.addPos(pos);
1464 ascFile.addNote(f.str().c_str());
1465
1466 m_state->m_paragraphsList.resize(0);
1467 for (int n = 0; n < N; ++n) {
1468 pos = input->tell();
1469 auto val = static_cast<int>(input->readLong(2));
1470 f.str("");
1471 f << "Entries(Paragraph)[" << n << "]:";
1472 if (val) f << "used?="<<val <<",";
1473 MacWrtProStructuresInternal::Paragraph para;
1474 if (!readParagraph(stream, para)) {
1475 f << "#";
1476 m_state->m_paragraphsList.push_back(MacWrtProStructuresInternal::Paragraph());
1477 input->seek(pos+dataSz, librevenge::RVNG_SEEK_SET);
1478 }
1479 else {
1480 f << para;
1481 m_state->m_paragraphsList.push_back(para);
1482 }
1483 ascFile.addPos(pos);
1484 ascFile.addNote(f.str().c_str());
1485 }
1486 return true;
1487 }
1488
1489 bool MacWrtProStructures::readParagraph(std::shared_ptr<MWAWStream> &stream, MacWrtProStructuresInternal::Paragraph &para)
1490 {
1491 auto &input = stream->m_input;
1492 auto &ascFile = stream->m_ascii;
1493 libmwaw::DebugStream f;
1494 int vers = version();
1495 long pos = input->tell(), endPos = pos+(vers == 0 ? 200: 190);
1496 para = MacWrtProStructuresInternal::Paragraph();
1497
1498 if (!stream->checkPosition(endPos)) {
1499 MWAW_DEBUG_MSG(("MacWrtProStructures::readParagraph: file is too short\n"));
1500 return false;
1501 }
1502 int val, just = 0;
1503 if (vers == 0) {
1504 just = static_cast<int>(input->readULong(2));
1505 val = static_cast<int>(input->readLong(2));
1506 if (val) f << "unkn=" << val << ",";
1507 }
1508 para.m_margins[1] = double(input->readLong(4))/72.0/65536.;
1509 para.m_margins[0] = double(input->readLong(4))/72.0/65536.;
1510 para.m_margins[2] = double(input->readLong(4))/72.0/65536.;
1511
1512
1513 float spacings[3];
1514 for (auto &spacing : spacings) spacing = float(input->readLong(4))/65536.f;
1515 for (int i = 0; i < 3; ++i) {
1516 int dim = vers==0 ? static_cast<int>(input->readLong(4)) : static_cast<int>(input->readULong(1));
1517 bool inPoint = true;
1518 bool ok = true;
1519 switch (dim) {
1520 case 0: // point
1521 ok = spacings[i] < 721 && (i || spacings[0] > 0);
1522 spacings[i]/=72.f;
1523 break;
1524 case -1:
1525 case 0xFF: // percent
1526 ok = (spacings[i] >= 0 && spacings[i]<46);
1527 if (i==0) spacings[i]+=1.0f;
1528 inPoint=false;
1529 break;
1530 default:
1531 f << "#inter[dim]=" << std::hex << dim << std::dec << ",";
1532 ok = spacings[i] < 721 && (i || spacings[0] > 0);
1533 spacings[i]/=72.f;
1534 break;
1535 }
1536 if (ok) {
1537 if (i == 0 && inPoint) {
1538 if (spacings[0] > 0)
1539 para.setInterline(double(spacings[0]), librevenge::RVNG_INCH, MWAWParagraph::AtLeast);
1540 else if (spacings[0] < 0) f << "interline=" << spacings[0] << ",";
1541 continue;
1542 }
1543 para.m_spacings[i] = double(spacings[i]);
1544 if (inPoint && spacings[i] > 1) {
1545 MWAW_DEBUG_MSG(("MacWrtProStructures::readParagraph: spacings looks big decreasing it\n"));
1546 f << "#prevSpacings" << i << "=" << spacings[i] << ",";
1547 para.m_spacings[i] = 1.0;
1548 }
1549 else if (!inPoint && i && (spacings[i]<0 || spacings[i]>0)) {
1550 if (i==1) f << "spaceBef";
1551 else f << "spaceAft";
1552 f << "=" << spacings[i] << "%,";
1553 /** seems difficult to set bottom a percentage of the line unit,
1554 so do the strict minimum... */
1555 *(para.m_spacings[i]) *= 10./72.;
1556 }
1557 }
1558 else
1559 f << "#spacings" << i << ",";
1560 }
1561
1562 if (vers==1) {
1563 just = static_cast<int>(input->readULong(1));
1564 input->seek(pos+28, librevenge::RVNG_SEEK_SET);
1565 }
1566 else {
1567 ascFile.addDelimiter(input->tell(),'|');
1568 }
1569 /* Note: when no extra tab the justification,
1570 if there is a extra tab, this corresponds to the extra tab alignment :-~ */
1571 switch (just & 0x3) {
1572 case 0:
1573 break;
1574 case 1:
1575 para.m_justify = MWAWParagraph::JustificationCenter;
1576 break;
1577 case 2:
1578 para.m_justify = MWAWParagraph::JustificationRight;
1579 break;
1580 case 3:
1581 para.m_justify = MWAWParagraph::JustificationFull;
1582 break;
1583 default:
1584 break;
1585 }
1586 if (just & 0x40)
1587 para.m_breakStatus = MWAWParagraph::NoBreakWithNextBit;
1588 if (just & 0x80)
1589 para.m_breakStatus = para.m_breakStatus.get()|MWAWParagraph::NoBreakBit;
1590 if (just&0x3C) f << "#justify=" << std::hex << (just&0x3C) << std::dec << ",";
1591 for (int i = 0; i < 20; ++i) {
1592 pos = input->tell();
1593 MWAWTabStop newTab;
1594 auto type = static_cast<int>(input->readULong(1));
1595 switch (type & 3) {
1596 case 0:
1597 break;
1598 case 1:
1599 newTab.m_alignment = MWAWTabStop::CENTER;
1600 break;
1601 case 2:
1602 newTab.m_alignment = MWAWTabStop::RIGHT;
1603 break;
1604 case 3:
1605 newTab.m_alignment = MWAWTabStop::DECIMAL;
1606 break;
1607 default:
1608 break;
1609 }
1610 if (type & 0xfc) {
1611 MWAW_DEBUG_MSG(("MacWrtProStructures::readParagraph: tab type is odd\n"));
1612 f << "tabs" << i << "[#type]=" << std::hex << (type & 0xFc) << std::dec << ",";
1613 }
1614 auto leader = static_cast<int>(input->readULong(1));
1615 if (leader != 0x20)
1616 newTab.m_leaderCharacter = static_cast<uint16_t>(leader);
1617 unsigned long tabPos = input->readULong(4);
1618 if (tabPos == 0xFFFFFFFFL) { // no more tab
1619 ascFile.addDelimiter(pos,'|');
1620 break;
1621 }
1622 newTab.m_position = double(tabPos)/72./65536.;
1623 auto decimalChar = static_cast<int>(input->readULong(1));
1624 if (decimalChar && decimalChar != '.')
1625 newTab.m_decimalCharacter=static_cast<uint16_t>(decimalChar);
1626 val = static_cast<int>(input->readLong(1)); // always 0?
1627 if (val)
1628 f << "tab" << i << "[#unkn=" << std::hex << val << std::dec << "],";
1629 para.m_tabs->push_back(newTab);
1630 input->seek(pos+8, librevenge::RVNG_SEEK_SET);
1631 }
1632
1633 if (vers==1) {
1634 input->seek(endPos-2, librevenge::RVNG_SEEK_SET);
1635 para.m_value = static_cast<int>(input->readLong(2));
1636 }
1637 para.m_extra=f.str();
1638
1639 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1640 return true;
1641 }
1642
1643 ////////////////////////////////////////////////////////////
1644 // read the styles ( character )
1645 bool MacWrtProStructures::readCharStyles(std::shared_ptr<MWAWStream> &stream)
1646 {
1647 auto &input = stream->m_input;
1648 auto &ascFile = stream->m_ascii;
1649 long pos = input->tell();
1650 libmwaw::DebugStream f;
1651 int vers = version();
1652
1653 int N;
1654 int expectedSz = 0x42;
1655 if (version() == 1) {
1656 auto sz = long(input->readULong(4));
1657 if ((sz%0x42) != 0) {
1658 MWAW_DEBUG_MSG(("MacWrtProStructures::readCharStyles: find an odd value for sz=%ld\n",sz));
1659 input->seek(pos, librevenge::RVNG_SEEK_SET);
1660 return false;
1661 }
1662 N = int(sz/0x42);
1663 }
1664 else {
1665 N = static_cast<int>(input->readULong(2));
1666 expectedSz = 0x2a;
1667 }
1668
1669 if (N == 0) {
1670 ascFile.addPos(pos);
1671 ascFile.addNote("_");
1672 return true;
1673 }
1674 long actPos = input->tell();
1675 long endPos = actPos+long(N)*expectedSz;
1676
1677 if (!stream->checkPosition(endPos)) {
1678 MWAW_DEBUG_MSG(("MacWrtProStructures::readCharStyles: file is too short\n"));
1679 input->seek(pos, librevenge::RVNG_SEEK_SET);
1680 return false;
1681 }
1682 f << "Entries(CharStyles):N=" << N;
1683 ascFile.addPos(pos);
1684 ascFile.addNote(f.str().c_str());
1685
1686 for (int i = 0; i < N; ++i) {
1687 pos = input->tell();
1688 f.str("");
1689 f << "CharStyles-" << i << ":";
1690 auto sSz = static_cast<int>(input->readULong(1));
1691 if (sSz > 31) {
1692 MWAW_DEBUG_MSG(("MacWrtProStructures::readCharStyles: string size seems odd\n"));
1693 sSz = 31;
1694 f << "#";
1695 }
1696 std::string name("");
1697 for (int c = 0; c < sSz; ++c)
1698 name += char(input->readULong(1));
1699 f << name << ",";
1700 input->seek(pos+32, librevenge::RVNG_SEEK_SET);
1701
1702 if (vers == 1) {
1703 auto val = static_cast<int>(input->readLong(2));
1704 if (val) f << "unkn0=" << val << ",";
1705 val = static_cast<int>(input->readLong(2));
1706 if (val != -1) f << "unkn1=" << val << ",";
1707 f << "date=" << MacWrtProParser::convertDateToDebugString(unsigned(input->readULong(4))) << ","; // unsure
1708 val = static_cast<int>(input->readLong(2)); // small number between 0 and 2 (nextId?)
1709 if (val) f << "f0=" << val << ",";
1710 for (int j = 1; j < 5; ++j) { // [-1,0,1], [0,1 or ee], 0, 0
1711 val = static_cast<int>(input->readLong(1));
1712 if (val) f << "f" << j <<"=" << val << ",";
1713 }
1714 }
1715 MacWrtProStructuresInternal::Font font;
1716 if (!readFont(stream, font)) {
1717 MWAW_DEBUG_MSG(("MacWrtProStructures::readCharStyles: can not read the font\n"));
1718 f << "###";
1719 }
1720 else
1721 f << font.m_font.getDebugString(m_parserState->m_fontConverter) << font << ",";
1722
1723 ascFile.addPos(pos);
1724 ascFile.addNote(f.str().c_str());
1725
1726 input->seek(pos+expectedSz, librevenge::RVNG_SEEK_SET);
1727 }
1728 return true;
1729 }
1730
1731 ////////////////////////////////////////////////////////////
1732 // read the styles ( paragraph + font)
1733 bool MacWrtProStructures::readStyles(std::shared_ptr<MWAWStream> &stream)
1734 {
1735 auto &input = stream->m_input;
1736 auto &ascFile = stream->m_ascii;
1737 long pos = input->tell();
1738 libmwaw::DebugStream f;
1739 auto sz = long(input->readULong(4));
1740 if ((sz%0x106) != 0) {
1741 MWAW_DEBUG_MSG(("MacWrtProStructures::readStyles: find an odd value for sz=%ld\n",sz));
1742 input->seek(pos, librevenge::RVNG_SEEK_SET);
1743 return false;
1744 }
1745 auto N = int(sz/0x106);
1746
1747 if (N==0) {
1748 ascFile.addPos(pos);
1749 ascFile.addNote("_");
1750 return true;
1751 }
1752
1753 f << "Entries(Style):";
1754 f << "N=" << N;
1755 ascFile.addPos(pos);
1756 ascFile.addNote(f.str().c_str());
1757
1758 for (int i = 0; i < N; ++i) {
1759 pos = input->tell();
1760 if (!readStyle(stream, i)) {
1761 f.str("");
1762 f << "#Style-" << i << ":";
1763 input->seek(pos, librevenge::RVNG_SEEK_SET);
1764 ascFile.addPos(pos);
1765 ascFile.addNote(f.str().c_str());
1766 return false;
1767 }
1768 }
1769 ascFile.addPos(input->tell());
1770 ascFile.addNote("_");
1771
1772 return true;
1773 }
1774
1775 bool MacWrtProStructures::readStyle(std::shared_ptr<MWAWStream> &stream, int styleId)
1776 {
1777 auto &input = stream->m_input;
1778 auto &ascFile = stream->m_ascii;
1779 long debPos = input->tell(), pos = debPos;
1780 libmwaw::DebugStream f;
1781 // checkme something is odd here
1782 long dataSz = 0x106;
1783 long endPos = pos+dataSz;
1784 if (!stream->checkPosition(endPos)) {
1785 MWAW_DEBUG_MSG(("MacWrtProStructures::readStyle: file is too short\n"));
1786 return false;
1787 }
1788 f << "Style-" << styleId << ":";
1789 auto strlen = static_cast<int>(input->readULong(1));
1790 if (!strlen || strlen > 31) {
1791 MWAW_DEBUG_MSG(("MacWrtProStructures::readStyle: style name length seems bad!!\n"));
1792 input->seek(pos, librevenge::RVNG_SEEK_SET);
1793 return false;
1794 }
1795 std::string name("");
1796 for (int i = 0; i < strlen; ++i) // default
1797 name+=char(input->readULong(1));
1798 f << name << ",";
1799 input->seek(pos+32, librevenge::RVNG_SEEK_SET); // probably end of name
1800
1801 int val;
1802 for (int i = 0; i < 3; ++i) { // 0 | [0,1,-1] | numTabs or idStyle?
1803 val = static_cast<int>(input->readLong(2));
1804 if (val) f << "f" << i << "=" << val << ",";
1805 }
1806 f << "date=" << MacWrtProParser::convertDateToDebugString(unsigned(input->readULong(4))) << ",";
1807 ascFile.addPos(pos);
1808 ascFile.addNote(f.str().c_str());
1809
1810 pos = input->tell();
1811
1812 f.str("");
1813 f << "Entries(Paragraph)[" << styleId << "]:";
1814 MacWrtProStructuresInternal::Paragraph para;
1815 if (!readParagraph(stream, para)) {
1816 f << "#";
1817 input->seek(pos+190, librevenge::RVNG_SEEK_SET);
1818 }
1819 else
1820 f << para;
1821 ascFile.addPos(pos);
1822 ascFile.addNote(f.str().c_str());
1823
1824 pos = input->tell();
1825 f.str("");
1826 f << "Style-" << styleId << "(II):";
1827 val = static_cast<int>(input->readLong(2));
1828 if (val != -1) f << "nextId?=" << val << ",";
1829 val = static_cast<int>(input->readLong(1)); // -1 0 or 1
1830 if (val) f << "f0=" << val << ",";
1831 for (int i = 1; i < 4; ++i) { // 0, then 0|1
1832 val = static_cast<int>(input->readLong(i==3?1:2));
1833 if (val) f << "f" << i << "=" << val << ",";
1834 }
1835 ascFile.addPos(pos);
1836 ascFile.addNote(f.str().c_str());
1837
1838 pos = input->tell();
1839 MacWrtProStructuresInternal::Font font;
1840 if (!readFont(stream, font)) {
1841 MWAW_DEBUG_MSG(("MacWrtProStructures::readStyle: end of style seems bad\n"));
1842 ascFile.addPos(pos);
1843 ascFile.addNote("Style:end###");
1844 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1845 return true;
1846 }
1847
1848 f.str("");
1849 f << "FontsDef:";
1850 f << font.m_font.getDebugString(m_parserState->m_fontConverter) << font << ",";
1851 ascFile.addPos(pos);
1852 ascFile.addNote(f.str().c_str());
1853
1854 pos = input->tell();
1855
1856 f.str("");
1857 f << "Style-" << styleId << "(end):";
1858 val = static_cast<int>(input->readLong(2));
1859 if (val!=-1) f << "unkn=" << val << ",";
1860
1861 ascFile.addPos(pos);
1862 ascFile.addNote(f.str().c_str());
1863 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1864 return true;
1865 }
1866
1867 ////////////////////////////////////////////////////////////
1868 // read the pages
1869 bool MacWrtProStructures::readPagesListII(std::shared_ptr<MWAWStream> const &stream, int numPages)
1870 {
1871 auto input=stream->m_input;
1872 long pos = input->tell();
1873 if (!stream->checkPosition(pos+50)) {
1874 MWAW_DEBUG_MSG(("MacWrtProStructures::readPagesListII: can not find the page zone\n"));
1875 return false;
1876 }
1877 m_state->m_pagesList.resize(2+size_t(numPages));
1878 for (size_t p=0; p<2+size_t(numPages); ++p) {
1879 pos=input->tell();
1880 if (!readPageII(stream, int(p), m_state->m_pagesList[p])) {
1881 m_state->m_pagesList.resize(p);
1882 input->seek(pos,librevenge::RVNG_SEEK_SET);
1883 break;
1884 }
1885 }
1886 return true;
1887 }
1888
1889 bool MacWrtProStructures::readPageII(std::shared_ptr<MWAWStream> const &stream, int wh, MacWrtProStructuresInternal::Page &page)
1890 {
1891 auto input=stream->m_input;
1892 auto &ascFile=stream->m_ascii;
1893 long pos = input->tell();
1894 if (!stream->checkPosition(pos+12+66)) {
1895 MWAW_DEBUG_MSG(("MacWrtProStructures::readPageII: the zone is too short\n"));
1896 return false;
1897 }
1898 libmwaw::DebugStream f;
1899 std::string name;
1900 for (int i=0; i<4; ++i) {
1901 auto c=char(input->readULong(1));
1902 if (!c) break;
1903 name+=c;
1904 }
1905 if (!name.empty()) f << "name=" << name << ",";
1906 input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1907
1908 page=MacWrtProStructuresInternal::Page();
1909 page.m_page = static_cast<int>(input->readLong(2));
1910 long val = input->readLong(1); // always -1 ?
1911 if (val>=1 && val<=5) {
1912 char const *what[]= {nullptr, "num", "Roman", "roman", "Alpha", "alpha"};
1913 f << "format=" << what[val] << ",";
1914 }
1915 else if (val != -1) f << "##format=" << val << ",";
1916 val = long(input->readULong(1)); // 0|80
1917 if (val) f << "fl=" << std::hex << val << std::dec << ",";
1918 for (int i = 0; i < 2; ++i) {
1919 int const expected[]= {0,1};
1920 val = input->readLong(2);
1921 if (val != expected[i]) f << "f" << i << "=" << val << ",";
1922 }
1923 page.m_extra=f.str();
1924 f.str("");
1925 f << "Entries(Page)[" << wh << "]:" << page;
1926 ascFile.addPos(pos);
1927 ascFile.addNote(f.str().c_str());
1928 int k=100*wh;
1929 while (!input->isEnd()) {
1930 pos=input->tell();
1931 auto graphic=std::make_shared<MacWrtProStructuresInternal::Graphic>(0);
1932 if (!readGraphicII(stream, ++k, page.m_graphicsList.empty(), *graphic)) {
1933 input->seek(pos, librevenge::RVNG_SEEK_SET);
1934 break;
1935 }
1936 page.m_graphicsList.push_back(graphic);
1937 if (graphic->m_lastFlag<0 || graphic->m_lastFlag>=2)
1938 break;
1939 }
1940
1941 return true;
1942 }
1943
1944 bool MacWrtProStructures::readGraphicII(std::shared_ptr<MWAWStream> const &stream, int wh, bool mainBlock, MacWrtProStructuresInternal::Graphic &graphic)
1945 {
1946 auto input=stream->m_input;
1947 auto &ascFile=stream->m_ascii;
1948 long pos = input->tell();
1949 libmwaw::DebugStream f;
1950 if (!stream->checkPosition(pos+76))
1951 return false;
1952 auto type = static_cast<int>(input->readLong(1)); // fd or 3
1953 long val;
1954 if (type<=-0x10 || type>=0x10) {
1955 input->seek(pos, librevenge::RVNG_SEEK_SET);
1956 return false;
1957 }
1958 long const expectedWidth[]= {
1959 -1, -1, -1, 76,
1960 -1, -1, -1, -1,
1961 -1, -1, -1, -1,
1962 -1, -1, -1, -1
1963 };
1964 long const len = expectedWidth[std::abs(type)];
1965 if (len==-1) {
1966 MWAW_DEBUG_MSG(("MacWrtProStructures::readGraphicII: unknown block %d\n", type));
1967 ascFile.addPos(pos);
1968 ascFile.addNote("Entries(Graphic):###");
1969 return false;
1970 }
1971 long endPos = pos+len;
1972 if (!stream->checkPosition(endPos)) {
1973 input->seek(pos, librevenge::RVNG_SEEK_SET);
1974 return false;
1975 }
1976 graphic=MacWrtProStructuresInternal::Graphic(0);
1977 graphic.m_type=type;
1978 val = long(input->readULong(1)); // 0, 6a, 78, f2, fa : type ?
1979 if (val) f << "fl=" << std::hex << val << std::dec << ",";
1980 val = long(input->readULong(1));
1981 if (!mainBlock) {
1982 graphic.m_textboxType=int(val>>6);
1983 graphic.m_headerFooterFlag=int((val>>4)&3);
1984 val &= 0xf;
1985 }
1986 if (val) f << "f0=" << std::hex << val << std::dec << ",";
1987 f << "unkn0=[";
1988 for (int i = 0; i < 6; ++i) {
1989 val = long(input->readULong(i==0 ? 1 : 2));
1990 if (val==0) f << "_,";
1991 else f << std::hex << val << std::dec << ",";
1992 }
1993 f << "],";
1994 f << "unkn1=[";
1995 for (int i = 0; i < 3; ++i) { // big number ptr?, junk
1996 auto lVal = long(input->readULong(4));
1997 if (lVal==0) f << "_,";
1998 else f << std::hex << lVal << std::dec << ",";
1999 }
2000 f << "],";
2001 graphic.m_fileBlock = static_cast<int>(input->readULong(2));
2002 float dim[4];
2003 for (auto &d : dim) d = float(input->readLong(2));
2004 graphic.m_box = MWAWBox2f(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2]));
2005 if (dim[0]>dim[2] || dim[1]>dim[3] || (dim[2]<=0 && dim[3]<=0)) {
2006 // bad box or (0,0,0,0) is clearly an indice of a problem
2007 input->seek(pos, librevenge::RVNG_SEEK_SET);
2008 return false;
2009 }
2010 for (int i = 0; i < 4; ++i) {
2011 // 8000*4, probably the box decimal position ; unsure if 0x8000 means decal 0.5 pt or 0 pt, ...
2012 val = long(input->readULong(2));
2013 if (val != 0x8000) f << "g" << i+2 << "=" << float(val)/float(0x8000) << ",";
2014 }
2015 graphic.m_textPos = int(input->readULong(1))<<16;
2016 graphic.m_textPos += int(input->readULong(2));
2017 if (graphic.m_textPos) {
2018 // ok this is a soft page break block
2019 graphic.m_page = graphic.m_fileBlock;
2020 graphic.m_fileBlock = 0;
2021 }
2022 val = long(input->readULong(1));
2023 if (val) f << "g6=" << std::hex << val << std::dec << ",";
2024 f << "unkn=[";
2025 for (int i = 0; i < 3; ++i) { // often unkn[0]=unkn1[0] or unkn1[1], excepted when unkn1[0]==unkn1[1]==unkn1[2]
2026 auto lVal = long(input->readULong(4));
2027 if (lVal==0) f << "_,";
2028 else f << std::hex << lVal << std::dec << ",";
2029 }
2030 f << "],";
2031 graphic.m_column=int(input->readULong(1));
2032 input->seek(1, librevenge::RVNG_SEEK_CUR);
2033 graphic.m_colSeparator=float(input->readULong(4))/float(0x10000);
2034 val=int(input->readLong(2)); // 0
2035 if (val) f << "h0=" << val << ",";
2036 val=int(input->readLong(2));
2037 if (val) f << "nextPage=" << val+1 << ",";
2038 auto ID=input->readULong(4); // 0 or big number if pageBreak
2039 if (ID) f << "ID=" << std::hex << ID << std::dec << ",";
2040 val=int(input->readLong(1)); // 0|ff
2041 if (val!=-1) f << "k1=" << val << ",";
2042 if (graphic.m_type==-3) endPos+=12;
2043 if (val!=0)
2044 endPos+=3;
2045 if (!stream->checkPosition(endPos)) {
2046 input->seek(pos, librevenge::RVNG_SEEK_SET);
2047 return false;
2048 }
2049
2050 if (input->tell()!=endPos-1)
2051 ascFile.addDelimiter(input->tell(),'|');
2052 input->seek(endPos-1, librevenge::RVNG_SEEK_SET);
2053 ascFile.addDelimiter(input->tell(),'|');
2054 graphic.m_lastFlag=int(input->readLong(1));
2055 if (graphic.m_lastFlag<0 || graphic.m_lastFlag>2) {
2056 MWAW_DEBUG_MSG(("MacWrtProStructures::readGraphicII: unknown last value\n"));
2057 f << "###isLast=" << graphic.m_lastFlag << ",";
2058 }
2059
2060 graphic.m_extra=f.str();
2061 f.str("");
2062 f << "Entries(Graphic)[" << wh << "]:" << graphic;
2063 ascFile.addPos(pos);
2064 ascFile.addNote(f.str().c_str());
2065 return true;
2066 }
2067
2068 ////////////////////////////////////////////////////////////
2069 // read the list of blocks
2070 bool MacWrtProStructures::readGraphicsList(std::shared_ptr<MWAWStream> &stream, int numPages)
2071 {
2072 auto &input = stream->m_input;
2073 auto &ascFile = stream->m_ascii;
2074 long pos = input->tell();
2075 libmwaw::DebugStream f;
2076
2077 long endPos = pos+45;
2078 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2079 if (long(input->tell()) != endPos) {
2080 MWAW_DEBUG_MSG(("MacWrtProStructures::readGraphicsList: file is too short\n"));
2081 input->seek(pos, librevenge::RVNG_SEEK_SET);
2082 return false;
2083 }
2084 input->seek(pos, librevenge::RVNG_SEEK_SET);
2085 f << "Entries(Graphic):";
2086 auto rev= static_cast<int>(input->readLong(4)); // 1 or 3
2087 f << "revision=" << rev << ",";
2088 auto uVal = input->readULong(4);
2089 if (uVal) f << "revision[min]=" << double(uVal)/60. << "',";
2090 long val;
2091 for (int i = 0; i < 4; ++i) { // [0|81|ff][0|03|33|63|ff][0|ff][0|ff]
2092 val = long(input->readULong(1));
2093 if (val) f << "flA" << i << "=" << std::hex << val << std::dec << ",";
2094 }
2095 val = input->readLong(4); // 0, 2, 46, 1479
2096 if (val) f << "f1=" << val << ",";
2097 for (int i = 0; i < 4; ++i) { // [0|1][0|74][0][0|4]
2098 val = long(input->readULong(1));
2099 if (val) f << "flB" << i << "=" << std::hex << val << std::dec << ",";
2100 }
2101 for (int i = 2; i < 4; ++i) { // [0|72] [0|a]
2102 val = input->readLong(2);
2103 if (val) f << "f" << i << "=" << val << ",";
2104 }
2105 val = long(input->readULong(4));
2106 if (val) f << "date=" << MacWrtProParser::convertDateToDebugString(unsigned(val)) << ",";
2107
2108 std::string str;
2109 if (!readString(input, str))
2110 return false;
2111 if (str.length()) f << "dir='" << str << "',";
2112 val = input->readLong(2);
2113 if (val) f << "f4=" << val << ",";
2114 ascFile.addPos(pos);
2115 ascFile.addNote(f.str().c_str());
2116
2117 pos = input->tell();
2118 if (!stream->checkPosition(pos+6)) {
2119 MWAW_DEBUG_MSG(("MacWrtProStructures::readGraphicsList: can not find the block zone\n"));
2120 return false;
2121 }
2122 f.str("");
2123 f << "Graphic-end:";
2124 ascFile.addPos(pos);
2125 ascFile.addNote(f.str().c_str());
2126 input->seek(6, librevenge::RVNG_SEEK_CUR);
2127
2128 int actPage=0;
2129 while (actPage<=numPages) {
2130 pos=input->tell();
2131 auto graphic = readGraphic(stream);
2132 if (!graphic) {
2133 input->seek(pos, librevenge::RVNG_SEEK_SET);
2134 if (stream->checkPosition(pos+2) && input->readULong(2)==0x7fff) {
2135 f.str("");
2136 f << "Graphic-Pg" << actPage++ << ",";
2137 ascFile.addPos(pos);
2138 ascFile.addNote(f.str().c_str());
2139 continue;
2140 }
2141 input->seek(pos, librevenge::RVNG_SEEK_SET);
2142 break;
2143 }
2144 m_state->m_graphicsList.push_back(graphic);
2145 if (m_state->m_idGraphicMap.find(graphic->m_id) != m_state->m_idGraphicMap.end()) {
2146 MWAW_DEBUG_MSG(("MacWrtProStructures::readGraphicsList: graphic %d already exists\n", graphic->m_id));
2147 }
2148 else
2149 m_state->m_idGraphicMap[graphic->m_id] = graphic;
2150 if (graphic->isGraphic() || graphic->isText())
2151 m_mainParser.parseDataZone(graphic->m_fileBlock, graphic->isGraphic() ? 1 : 0);
2152 }
2153 return true;
2154 }
2155
2156 std::shared_ptr<MacWrtProStructuresInternal::Graphic> MacWrtProStructures::readGraphic(std::shared_ptr<MWAWStream> &stream)
2157 {
2158 auto &input = stream->m_input;
2159 auto &ascFile = stream->m_ascii;
2160 long pos = input->tell();
2161 libmwaw::DebugStream f;
2162 int type=int(input->readLong(2));
2163 if (type<-3 || type>2) { // normally -2..1
2164 input->seek(pos, librevenge::RVNG_SEEK_SET);
2165 return std::shared_ptr<MacWrtProStructuresInternal::Graphic>();
2166 }
2167 f << "type=" << type << ",";
2168 auto sz = long(input->readULong(4));
2169 // pat2*3?, dim[pt*65536], border[pt*65536], ?, [0|10|1c], 0, graphic?
2170 if (sz < 0x40) {
2171 input->seek(pos, librevenge::RVNG_SEEK_SET);
2172 return std::shared_ptr<MacWrtProStructuresInternal::Graphic>();
2173 }
2174
2175 long endPos = pos+sz+6;
2176 if (!stream->checkPosition(endPos)) {
2177 input->seek(pos, librevenge::RVNG_SEEK_SET);
2178 return std::shared_ptr<MacWrtProStructuresInternal::Graphic>();
2179 }
2180
2181 auto graphic=std::make_shared<MacWrtProStructuresInternal::Graphic>(1);
2182 long val;
2183 f << "pat?=[" << std::hex;
2184 for (int i = 0; i < 2; ++i)
2185 f << input->readULong(2) << ",";
2186 f << std::dec << "],";
2187 graphic->m_type = static_cast<int>(input->readULong(2));
2188 float dim[4];
2189 for (auto &d : dim) d = float(input->readLong(4))/65536.f;
2190 graphic->m_box = MWAWBox2f(MWAWVec2f(dim[1],dim[0]), MWAWVec2f(dim[3],dim[2]));
2191
2192 static int const wh[4] = { libmwaw::Top, libmwaw::Left, libmwaw::Bottom, libmwaw::Right };
2193 for (auto what : wh)
2194 graphic->m_borderWList[what]=double(input->readLong(4))/65536.;
2195
2196 /* 4: pagebreak,
2197 5: text
2198 1: floating, 7: none(wrapping/attachment), b: attachment
2199 0/a: table ?
2200 */
2201 for (int i = 0; i < 2; ++i) {
2202 val = long(input->readULong(2));
2203 if (val)
2204 f << "fl" << i << "=" << std::hex << val << std::dec << ",";
2205 }
2206 val = input->readLong(2);
2207 if (val) f << "f0=" << val << ",";
2208 graphic->m_fileBlock = static_cast<int>(input->readLong(2));
2209 graphic->m_id = static_cast<int>(input->readLong(2));
2210 val = static_cast<int>(input->readLong(2)); // almost always 4 ( one time 0)
2211 if (val!=4)
2212 f << "bordOffset=" << val << ",";
2213 for (int i = 2; i < 7; ++i) {
2214 /* always 0, except f3=-1 (in one file),
2215 and in other file f4=1,f5=1,f6=1, */
2216 val = static_cast<int>(input->readLong(2));
2217 if (val) f << "f" << i << "=" << val << ",";
2218 }
2219 graphic->m_baseline = float(input->readLong(4))/65536.f;
2220 auto colorId = static_cast<int>(input->readLong(2));
2221 auto patId=static_cast<int>(input->readLong(2));
2222 MWAWColor color(MWAWColor::white());
2223 if (getColor(colorId, patId, color))
2224 graphic->m_surfaceColor = color;
2225 else
2226 f << "#colorId=" << colorId << ", #patId=" << patId << ",";
2227
2228 colorId = static_cast<int>(input->readLong(2));
2229 patId=static_cast<int>(input->readLong(2));
2230 if (getColor(colorId, patId, color))
2231 graphic->m_lineBorder.m_color = color;
2232 else
2233 f << "line[#colorId=" << colorId << ", #patId[line]=" << patId << "],";
2234 val = input->readLong(2);
2235 static double const w[9]= {0,0.5,1,2,4,6,8,10,12};
2236 if (val>0&&val<10)
2237 graphic->m_lineBorder.m_width = w[val-1];
2238 else
2239 f << "#lineWidth=" << val << ",";
2240 val = input->readLong(2);
2241 if (!m_state->updateLineType(static_cast<int>(val), graphic->m_lineBorder))
2242 f << "#line[type]=" << val << ",";
2243 auto contentType = static_cast<int>(input->readULong(1));
2244 switch (contentType) {
2245 case 0:
2246 graphic->m_contentType = MacWrtProStructuresInternal::Graphic::TEXT;
2247 break;
2248 case 1:
2249 graphic->m_contentType = MacWrtProStructuresInternal::Graphic::GRAPHIC;
2250 break;
2251 default:
2252 MWAW_DEBUG_MSG(("MacWrtProStructures::readGraphic: find unknown block content type\n"));
2253 f << "#contentType=" << contentType << ",";
2254 break;
2255 }
2256
2257 bool isNote = false;
2258 if (graphic->m_type==4 && sz == 0xa0) {
2259 // this can be a note, let check
2260 isNote=true;
2261 static double const expectedWidth[4] = {5,5,19,5};
2262 for (int i=0; i < 4; ++i) {
2263 if (graphic->m_borderWList[i]<expectedWidth[i] ||
2264 graphic->m_borderWList[i]>expectedWidth[i]) {
2265 isNote = false;
2266 break;
2267 }
2268 }
2269 }
2270 if (isNote) {
2271 long actPos = input->tell();
2272 ascFile.addDelimiter(pos+118,'|');
2273 input->seek(pos+118, librevenge::RVNG_SEEK_SET);
2274 val = input->readLong(2);
2275 isNote = val==0 || val==0x100;
2276 if (isNote) {
2277 float dim2[4];
2278 for (auto &d : dim2) {
2279 d = float(input->readLong(4))/65536.f;
2280 if (!val && (d<0||d>0)) {
2281 isNote = false;
2282 break;
2283 }
2284 }
2285 if (isNote && val) {
2286 // ok, reset the box only if it is bigger
2287 if (dim2[3]-dim2[1]>dim[3]-dim[1] && dim2[2]-dim2[0]>dim[2]-dim[0])
2288 graphic->m_box=MWAWBox2f(MWAWVec2f(dim2[1],dim2[0]),MWAWVec2f(dim2[3],dim2[2]));
2289 }
2290 }
2291 if (isNote) {
2292 graphic->m_contentType = MacWrtProStructuresInternal::Graphic::NOTE;
2293 // ok reset the border and the line color to gray
2294 for (int i = 0; i < 4; ++i) {
2295 if (i!=libmwaw::Top)
2296 graphic->m_borderWList[i]=1;
2297 }
2298 graphic->m_lineBorder=MWAWBorder();
2299 graphic->m_lineBorder.m_color=MWAWColor(128,128,128);
2300
2301 if (val)
2302 f << "note[closed],";
2303 else
2304 f << "note,";
2305 }
2306 input->seek(actPos, librevenge::RVNG_SEEK_SET);
2307 }
2308 else if (graphic->m_type==4 && sz == 0x9a) {
2309 long actPos = input->tell();
2310 ascFile.addDelimiter(pos+110,'|');
2311 input->seek(pos+110, librevenge::RVNG_SEEK_SET);
2312 libmwaw::DebugStream f2;
2313 for (int i=0; i<4; ++i) {
2314 MWAWBorder border;
2315 colorId = static_cast<int>(input->readLong(2));
2316 patId=static_cast<int>(input->readLong(2));
2317 f2.str("");
2318 if (getColor(colorId, patId, color))
2319 border.m_color=color;
2320 else
2321 f2 << "#colorId=" << colorId << ", #patId=" << patId << ",";
2322 val= input->readLong(2);
2323 if (val > 0 && val < 10)
2324 border.m_width=w[val-1];
2325 else
2326 f2 << "#w[line]=" << val << ",";
2327 val= input->readLong(2);
2328 if (!m_state->updateLineType(static_cast<int>(val), border))
2329 f2 << "#border[type]=" << val << ",";
2330 val=input->readLong(2);
2331 if (int(val)!=i)
2332 f2 << "#id=" << val << ",";
2333 border.m_extra = f2.str();
2334 graphic->m_borderCellList[wh[i]]=border;
2335 }
2336 input->seek(actPos, librevenge::RVNG_SEEK_SET);
2337 }
2338
2339 graphic->m_extra = f.str();
2340
2341 f.str("");
2342 f << "Graphic-B" << m_state->m_graphicsList.size() << ":" << *graphic;
2343 ascFile.addPos(pos);
2344 ascFile.addNote(f.str().c_str());
2345
2346 if (long(input->tell()) != endPos)
2347 ascFile.addDelimiter(input->tell(), '|');
2348
2349 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2350
2351 // ok now read the end of the header
2352 pos = input->tell();
2353 if (stream->checkPosition(pos+6)) {
2354 f.str("");
2355 f << "Graphic-data-B" << m_state->m_graphicsList.size()<< "[" << graphic->m_type << "]:";
2356 switch (graphic->m_type) {
2357 case 3: { // table
2358 graphic->m_row = static_cast<int>(input->readLong(2));
2359 graphic->m_col = static_cast<int>(input->readLong(2));
2360 f << "numRow=" << graphic->m_row << ",";
2361 f << "numCol=" << graphic->m_col << ",";
2362 break;
2363 }
2364 case 4: { // cell/textbox : no sure it contain data?
2365 val = input->readLong(2); // always 0 ?
2366 if (val) f << "f0=" << val << ",";
2367 val = long(input->readULong(2)); // [0|10|1e|10c0|1cc0|a78a|a7a6|d0c0|dcc0]
2368 if (val) f << "fl?=" << std::hex << val << std::dec << ",";
2369 break;
2370 }
2371 case 5: { // text or ?
2372 bool emptyBlock = graphic->m_fileBlock <= 0;
2373 val = long(input->readULong(2)); // always 0 ?
2374 if (emptyBlock) {
2375 if (val & 0xFF00)
2376 f << "#f0=" << val << ",";
2377 graphic->m_textPos=int(((val&0xFF)<<16) | static_cast<int>(input->readULong(2)));
2378 f << "posC=" << graphic->m_textPos << ",";
2379 }
2380 else if (val) f << "f0=" << val << ",";
2381 val = long(input->readULong(2)); // 30c0[normal], 20c0|0[empty]
2382 f << "fl?=" << std::hex << val << ",";
2383 break;
2384 }
2385 case 6: {
2386 for (int i = 0; i < 4; ++i) { // [10|d0],40, 0, 0
2387 val = long(input->readULong(1));
2388 f << "f" << i << "=" << val << ",";
2389 }
2390 val = input->readLong(1);
2391 switch (val) {
2392 case 1:
2393 f << "header,";
2394 graphic->m_isHeader = true;
2395 break;
2396 case 2:
2397 f << "footer,";
2398 graphic->m_isHeader = false;
2399 break;
2400 default:
2401 MWAW_DEBUG_MSG(("MacWrtProStructures::readGraphic: find unknown header/footer type\n"));
2402 f << "#type=" << val << ",";
2403 break;
2404 }
2405 val = input->readLong(1); // alway 1 ?
2406 if (val != 1) f << "f4=" << val << ",";
2407 break;
2408 }
2409 case 7: { // footnote: something here ?
2410 for (int i = 0; i < 3; ++i) { // 0, 0, [0|4000]
2411 val = long(input->readULong(2));
2412 f << "f" << i << "=" << std::hex << val << std::dec << ",";
2413 }
2414 break;
2415 }
2416 case 8:
2417 break; // graphic: clearly nothing
2418 default:
2419 break;
2420 }
2421 ascFile.addPos(pos);
2422 ascFile.addNote(f.str().c_str());
2423 input->seek(pos+6, librevenge::RVNG_SEEK_SET);
2424 }
2425
2426 return graphic;
2427 }
2428
2429 ////////////////////////////////////////////////////////////
2430 // read the column information zone : checkme
2431 bool MacWrtProStructures::readSections(std::shared_ptr<MWAWStream> &stream, std::vector<MacWrtProStructuresInternal::Section> &sections)
2432 {
2433 auto &input = stream->m_input;
2434 auto &ascFile = stream->m_ascii;
2435 long pos = input->tell();
2436 libmwaw::DebugStream f;
2437
2438 auto sz = long(input->readULong(4));
2439 if (sz == 0) {
2440 ascFile.addPos(pos);
2441 ascFile.addNote("_");
2442 return true;
2443 }
2444 long endPos = pos+4+sz;
2445 if ((sz%0xd8)) {
2446 MWAW_DEBUG_MSG(("MacWrtProStructures::readSections: find an odd value for sz\n"));
2447 ascFile.addPos(pos);
2448 ascFile.addNote("Entries(Sections)#");
2449 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2450 return true;
2451 }
2452 if (!stream->checkPosition(endPos)) {
2453 MWAW_DEBUG_MSG(("MacWrtProStructures::readSections: section is outside of the input\n"));
2454 return true;
2455 }
2456
2457 auto N = int(sz/0xd8);
2458 f << "Entries(Section):";
2459 f << "N=" << N;
2460 ascFile.addPos(pos);
2461 ascFile.addNote(f.str().c_str());
2462
2463 for (int n = 0; n < N; ++n) {
2464 MacWrtProStructuresInternal::Section sec;
2465 pos = input->tell();
2466 f.str("");
2467 sec.m_textLength = long(input->readULong(4));
2468 long val = input->readLong(4); // almost always 0 or a dim?
2469 if (val) f << "dim?=" << float(val)/65536.f << ",";
2470 auto startWay = static_cast<int>(input->readLong(2));
2471 switch (startWay) {
2472 case 1:
2473 sec.m_start = sec.S_Line;
2474 break;
2475 case 2:
2476 sec.m_start = sec.S_Page;
2477 break;
2478 case 3:
2479 sec.m_start = sec.S_PageLeft;
2480 break;
2481 case 4:
2482 sec.m_start = sec.S_PageRight;
2483 break;
2484 default:
2485 MWAW_DEBUG_MSG(("MacWrtProStructures::readSections: find an odd value for start\n"));
2486 f << "#start=" << startWay << ",";
2487 }
2488 val = input->readLong(2);
2489 if (val)
2490 f << "f0=" << val << ",";
2491 // a flag ? and noused ?
2492 for (int i = 0; i < 2; ++i) {
2493 val = long(input->readULong(1));
2494 if (val == 0xFF) f << "fl" << i<< "=true,";
2495 else if (val) f << "fl" << i << "=" << std::hex << val << std::dec << ",";
2496 }
2497
2498 for (int st = 0; st < 2; ++st) {
2499 val = input->readLong(2); // alway 1 ?
2500 if (val != 1) f << "f" << 1+st << "=" << val << ",";
2501 // another flag ?
2502 val = long(input->readULong(1));
2503 if (val) f << "fl" << st+2 << "=" << std::hex << val << std::dec << ",";
2504 }
2505 auto numColumns = static_cast<int>(input->readLong(2));
2506 if (numColumns < 1 || numColumns > 20) {
2507 MWAW_DEBUG_MSG(("MacWrtProStructures::readSections: bad number of columns\n"));
2508 f << "#nCol=" << numColumns << ",";
2509 numColumns = 1;
2510 }
2511 val = input->readLong(2); // find: 3, c, 24
2512 if (val) f << "f3=" << val << ",";
2513 for (int i = 4; i < 7; ++i) { // always 0 ?
2514 val = input->readLong(2);
2515 if (val)
2516 f << "f" << i << "=" << val << ",";
2517 }
2518 long actPos = input->tell();
2519 for (int c = 0; c < 2*numColumns; ++c)
2520 sec.m_colsPos.push_back(float(input->readLong(4))/65536.f);
2521 input->seek(actPos+20*8+4, librevenge::RVNG_SEEK_SET);
2522 // 5 flags ( 1+unused?)
2523 for (int i = 0; i < 6; ++i) {
2524 val = long(input->readULong(1));
2525 if ((i!=5 && val!=1) || (i==5 && val))
2526 f << "g" << i << "=" << val << ",";
2527 }
2528 for (int st = 0; st < 2; ++st) { // pair, unpair?
2529 for (int i = 0; i < 2; ++i) { // header/footer
2530 val = input->readLong(2);
2531 if (val)
2532 f << "#h" << 2*st+i << "=" << val << ",";
2533
2534 val = input->readLong(2);
2535 if (i==0) sec.m_headerIds[st] = static_cast<int>(val);
2536 else sec.m_footerIds[st] = static_cast<int>(val);
2537 }
2538 }
2539 sec.m_extra=f.str();
2540 sections.push_back(sec);
2541
2542 f.str("");
2543 f << "Section" << "-" << n << ":" << sec;
2544 ascFile.addPos(pos);
2545 ascFile.addNote(f.str().c_str());
2546 input->seek(pos+0xd8, librevenge::RVNG_SEEK_SET);
2547 }
2548
2549 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2550 return true;
2551 }
2552
2553 ////////////////////////////////////////////////////////////
2554 // read the selection zone
2555 bool MacWrtProStructures::readSelection(std::shared_ptr<MWAWStream> &stream)
2556 {
2557 auto &input = stream->m_input;
2558 auto &ascFile = stream->m_ascii;
2559 long pos = input->tell();
2560 libmwaw::DebugStream f;
2561
2562 long endPos = pos+14;
2563 if (!stream->checkPosition(endPos)) {
2564 MWAW_DEBUG_MSG(("MacWrtProStructures::readSelection: file is too short\n"));
2565 return false;
2566 }
2567 f << "Entries(Selection):";
2568 auto val = static_cast<int>(input->readLong(2));
2569 f << "f0=" << val << ","; // zone?
2570 val = static_cast<int>(input->readLong(4)); // -1, 0 or 8 : zone type?
2571 if (val == -1 || val == 0) { // checkme: none ?
2572 f << "*";
2573 ascFile.addPos(pos);
2574 ascFile.addNote(f.str().c_str());
2575 input->seek(pos+6, librevenge::RVNG_SEEK_SET);
2576 return true;
2577 }
2578 if (val!=8) f << "f1=" << val << ",";
2579 f << "char=";
2580 for (int i = 0; i < 2; ++i) {
2581 f << input->readULong(4);
2582 if (i==0) f << "x";
2583 else f << ",";
2584 }
2585 ascFile.addPos(pos);
2586 ascFile.addNote(f.str().c_str());
2587
2588 input->seek(endPos, librevenge::RVNG_SEEK_SET);
2589 return true;
2590 }
2591
2592 ////////////////////////////////////////////////////////////
2593 // read a string
2594 bool MacWrtProStructures::readString(MWAWInputStreamPtr input, std::string &res)
2595 {
2596 res="";
2597 long pos = input->tell();
2598 auto sz = static_cast<int>(input->readLong(2));
2599 if (sz == 0) return true;
2600 if (sz < 0) {
2601 input->seek(pos, librevenge::RVNG_SEEK_SET);
2602 MWAW_DEBUG_MSG(("MacWrtProStructures::readString: odd value for size\n"));
2603 return false;
2604 }
2605 if (!input->checkPosition(pos+sz+2)) {
2606 input->seek(pos, librevenge::RVNG_SEEK_SET);
2607 MWAW_DEBUG_MSG(("MacWrtProStructures::readString: file is too short\n"));
2608 return false;
2609 }
2610 for (int i= 0; i < sz; ++i) {
2611 auto c = char(input->readULong(1));
2612 if (c) {
2613 res+=c;
2614 continue;
2615 }
2616 if (i==sz-1) break;
2617
2618 input->seek(pos, librevenge::RVNG_SEEK_SET);
2619 MWAW_DEBUG_MSG(("MacWrtProStructures::readString: find odd character in string\n"));
2620 return false;
2621 }
2622 return true;
2623 }
2624
2625 ////////////////////////////////////////////////////////////
2626 // read an unknown zone
2627 bool MacWrtProStructures::readStructB(std::shared_ptr<MWAWStream> &stream)
2628 {
2629 auto &input = stream->m_input;
2630 auto &ascFile = stream->m_ascii;
2631 long pos = input->tell();
2632 libmwaw::DebugStream f;
2633
2634 auto N = static_cast<int>(input->readULong(2));
2635 if (N==0) {
2636 ascFile.addPos(pos);
2637 ascFile.addNote("_");
2638 return true;
2639 }
2640 f << "Entries(StructB):N=" << N << ",";
2641
2642 // CHECKME: find N=2 only one time ( and across a checksum zone ...)
2643 long endPos = pos+N*10+6;
2644 if (!stream->checkPosition(endPos)) {
2645 MWAW_DEBUG_MSG(("MacWrtProStructures::readZonB: file is too short\n"));
2646 input->seek(pos, librevenge::RVNG_SEEK_SET);
2647 return false;
2648 }
2649 auto val = static_cast<int>(input->readULong(2));
2650 if (val != 0x2af8)
2651 f << "f0=" << std::hex << val << std::dec << ",";
2652 val = static_cast<int>(input->readULong(2));
2653 if (val) f << "f1=" << val << ",";
2654
2655 ascFile.addPos(pos);
2656 ascFile.addNote(f.str().c_str());
2657
2658 for (int n = 0; n < N; ++n) {
2659 pos = input->tell();
2660 f.str("");
2661 f << "StructB" << "-" << n;
2662 ascFile.addPos(pos);
2663 ascFile.addNote(f.str().c_str());
2664 input->seek(pos+10, librevenge::RVNG_SEEK_SET);
2665 }
2666 return true;
2667 }
2668
2669 ////////////////////////////////////////////////////////////
2670 // check if a block is sent
2671 bool MacWrtProStructures::isSent(int graphicId)
2672 {
2673 if (m_state->m_idGraphicMap.find(graphicId) == m_state->m_idGraphicMap.end()) {
2674 MWAW_DEBUG_MSG(("MacWrtProStructures::isSent: can not find the block %d\n", graphicId));
2675 return true;
2676 }
2677 return m_state->m_idGraphicMap.find(graphicId)->second->m_send;
2678 }
2679
2680 ////////////////////////////////////////////////////////////
2681 // send a block
2682 bool MacWrtProStructures::send(int graphicId, bool mainZone)
2683 {
2684 MWAWTextListenerPtr listener=m_parserState->m_textListener;
2685 if (version()==0) {
2686 if (m_state->m_idGraphicMap.find(graphicId)==m_state->m_idGraphicMap.end()) {
2687 MWAW_DEBUG_MSG(("MacWrtProStructures::send: can not find the graphic %d\n", graphicId));
2688 return false;
2689 }
2690 MWAWVec2i const gId(graphicId, 0);
2691 if (m_state->m_graphicsSendSet.find(gId)!=m_state->m_graphicsSendSet.end()) {
2692 MWAW_DEBUG_MSG(("MacWrtProStructures::send: oops find a loop for %d\n", graphicId));
2693 return false;
2694 }
2695
2696 m_state->m_graphicsSendSet.insert(gId);
2697 auto graphic=m_state->m_idGraphicMap.find(graphicId)->second;
2698 graphic->m_send = true;
2699 if (graphic->m_fileBlock>0)
2700 m_mainParser.sendTextZone(graphic->m_fileBlock, mainZone);
2701 m_state->m_graphicsSendSet.insert(gId);
2702
2703 return true;
2704 }
2705 if (m_state->m_idGraphicMap.find(graphicId) == m_state->m_idGraphicMap.end()) {
2706 MWAW_DEBUG_MSG(("MacWrtProStructures::send: can not find the block %d\n", graphicId));
2707 return false;
2708 }
2709 auto graphic = m_state->m_idGraphicMap.find(graphicId)->second;
2710 MWAWVec2i graphicIdMain(graphicId, (mainZone ? 1 : 0) + (graphic->m_textboxCellType ? 2 : 0)
2711 +(!graphic->m_attachment ? 4 : 0));
2712 if (m_state->m_graphicsSendSet.find(graphicIdMain)!=m_state->m_graphicsSendSet.end()) {
2713 MWAW_DEBUG_MSG(("MacWrtProStructures::send: oops find a loop for %d\n", graphicId));
2714 return false;
2715 }
2716 m_state->m_graphicsSendSet.insert(graphicIdMain);
2717 graphic->m_send = true;
2718 if (graphic->m_type == 4 && graphic->m_textboxCellType == 0) {
2719 graphic->m_textboxCellType = 2;
2720 MWAWGraphicStyle style;
2721 graphic->fillFrame(style);
2722 m_mainParser.sendTextBoxZone(graphicId, graphic->getPosition(), style);
2723 graphic->m_textboxCellType = 0;
2724 }
2725 else if (graphic->isText())
2726 m_mainParser.sendTextZone(graphic->m_fileBlock, mainZone);
2727 else if (graphic->isGraphic()) {
2728 MWAWGraphicStyle style;
2729 graphic->fillFrame(style);
2730 m_mainParser.sendPictureZone(graphic->m_fileBlock, graphic->getPosition(), style);
2731 }
2732 else if (graphic->m_type == 3) {
2733 if (m_state->m_tablesMap.find(graphicId) == m_state->m_tablesMap.end()) {
2734 MWAW_DEBUG_MSG(("MacWrtProStructures::send: can not find table with id=%d\n", graphicId));
2735 }
2736 else {
2737 bool needTextBox = listener && !graphic->m_attachment && graphic->m_textboxCellType == 0;
2738 if (needTextBox) {
2739 graphic->m_textboxCellType = 2;
2740 m_mainParser.sendTextBoxZone(graphicId, graphic->getPosition());
2741 }
2742 else {
2743 auto table = m_state->m_tablesMap.find(graphicId)->second;
2744 if (!table->sendTable(listener))
2745 table->sendAsText(listener);
2746 graphic->m_textboxCellType = 0;
2747 }
2748 }
2749 }
2750 else if (graphic->m_type == 4 || graphic->m_type == 6) {
2751 // probably ok, can be an empty cell, textbox, header/footer ..
2752 if (listener) listener->insertChar(' ');
2753 }
2754 else if (graphic->m_type == 8) { // empty frame
2755 MWAWGraphicStyle style;
2756 graphic->fillFrame(style);
2757 m_mainParser.sendEmptyFrameZone(graphic->getPosition(), style);
2758 }
2759 else {
2760 MWAW_DEBUG_MSG(("MacWrtProStructures::send: can not send block with type=%d\n", graphic->m_type));
2761 }
2762 m_state->m_graphicsSendSet.erase(graphicIdMain);
2763 return true;
2764 }
2765
2766 ////////////////////////////////////////////////////////////
2767 // send the not sent data
2768 void MacWrtProStructures::flushExtra()
2769 {
2770 int vers = version();
2771 MWAWTextListenerPtr listener=m_parserState->m_textListener;
2772 if (listener && listener->isSectionOpened()) {
2773 listener->closeSection();
2774 listener->openSection(MWAWSection());
2775 }
2776 if (version()==0) {
2777 #ifdef DEBUG
2778 bool const checkHFs=true;
2779 #else
2780 bool const checkHFs=false;
2781 #endif
2782 for (size_t i=checkHFs ? 0 : 2; i<m_state->m_pagesList.size(); ++i) {
2783 auto const &page = m_state->m_pagesList[i];
2784 for (auto const &graphic : page.m_graphicsList) {
2785 if (graphic->m_send || graphic->m_fileBlock<=0 || graphic->m_textPos>0) continue;
2786 int const id=1000;
2787 m_state->m_idGraphicMap[id]=graphic;
2788 send(id);
2789 }
2790 }
2791 }
2792 else {
2793 // first send the text
2794 for (size_t i = 0; i < m_state->m_graphicsList.size(); ++i) {
2795 auto graphic = m_state->m_graphicsList[i];
2796 if (graphic->m_send)
2797 continue;
2798 if (graphic->m_type == 6) {
2799 /* Fixme: macwritepro can have one header/footer by page and one by default.
2800 For the moment, we only print the first one :-~ */
2801 MWAW_DEBUG_MSG(("MacWrtProStructures::flushExtra: find some header/footer\n"));
2802 continue;
2803 }
2804 int id = vers == 0 ? int(i) : graphic->m_id;
2805 if (graphic->isText()) {
2806 // force to non floating position
2807 graphic->m_attachment = true;
2808 send(id);
2809 if (listener) listener->insertEOL();
2810 }
2811 else if (graphic->m_type == 3) {
2812 // force to non floating position
2813 graphic->m_attachment = true;
2814 send(id);
2815 }
2816 }
2817 // then send graphic
2818 for (auto const &graphic : m_state->m_graphicsList) {
2819 if (graphic->m_send)
2820 continue;
2821 if (graphic->isGraphic()) {
2822 // force to non floating position
2823 graphic->m_attachment = true;
2824 send(graphic->m_id);
2825 }
2826 }
2827 }
2828 }
2829
2830 ////////////////////////////////////////////////////////////
2831 // interface with the listener
2832 MacWrtProStructuresListenerState::MacWrtProStructuresListenerState(std::shared_ptr<MacWrtProStructures> const &structures, bool mainZone, int version)
2833 : m_isMainZone(mainZone)
2834 , m_version(version)
2835 , m_actPage(0)
2836 , m_actTab(0)
2837 , m_numTab(0)
2838 , m_section(0)
2839 , m_numCols(1)
2840 , m_newPageDone(false)
2841 , m_structures(structures)
2842 {
2843 if (!m_structures) {
2844 MWAW_DEBUG_MSG(("MacWrtProStructuresListenerState::MacWrtProStructuresListenerState can not find structures parser\n"));
2845 return;
2846 }
2847 if (mainZone) {
2848 newPage();
2849 sendSection(0);
2850 }
2851 }
2852
2853 MacWrtProStructuresListenerState::~MacWrtProStructuresListenerState()
2854 {
2855 }
2856
2857 bool MacWrtProStructuresListenerState::isSent(int graphicId)
2858 {
2859 if (!m_structures) return false;
2860 return m_structures->isSent(graphicId);
2861 }
2862
2863 bool MacWrtProStructuresListenerState::send(int graphicId)
2864 {
2865 m_newPageDone = false;
2866 if (!m_structures) return false;
2867 int oldNumTab=m_numTab;
2868 bool ok=m_structures->send(graphicId);
2869 m_numTab=oldNumTab;
2870 return ok;
2871 }
2872
2873 void MacWrtProStructuresListenerState::insertSoftPageBreak()
2874 {
2875 if (m_newPageDone) return;
2876 newPage(true);
2877 }
2878
2879 bool MacWrtProStructuresListenerState::newPage(bool softBreak)
2880 {
2881 if (!m_structures || !m_isMainZone) {
2882 MWAW_DEBUG_MSG(("MacWrtProStructuresListenerState::newPage: can not create a new page\n"));
2883 return false;
2884 }
2885
2886 // first send all the floating data
2887 if (m_actPage == 0) {
2888 for (auto const &graphic : m_structures->m_state->m_graphicsList) {
2889 if (graphic->m_send || graphic->m_attachment) continue;
2890 if (graphic->m_type != 3 && graphic->m_type != 4 && graphic->m_type != 8) continue;
2891 m_structures->send(graphic->m_id);
2892 }
2893 }
2894
2895 m_structures->m_mainParser.newPage(++m_actPage, softBreak);
2896 m_actTab=0;
2897 m_newPageDone = true;
2898 double colSep;
2899 if (m_version==0 && m_structures->m_mainParser.numColumns(colSep)>1 && m_actPage>1) {
2900 if (!softBreak || (m_actPage==2&&m_structures->m_mainParser.hasTitlePage())) {
2901 MWAWTextListenerPtr listener=m_structures->getTextListener();
2902 if (listener->isSectionOpened())
2903 listener->closeSection();
2904 sendSection(++m_section);
2905 }
2906 }
2907 return true;
2908 }
2909
2910 std::vector<int> MacWrtProStructuresListenerState::getPageBreaksPos() const
2911 {
2912 std::vector<int> res;
2913 if (!m_structures || !m_isMainZone) return res;
2914 if (m_version==0) {
2915 for (size_t i=2 ; i<m_structures->m_state->m_pagesList.size(); ++i) {
2916 for (auto const &graphic : m_structures->m_state->m_pagesList[i].m_graphicsList) {
2917 if (graphic->m_textboxType!=0)
2918 continue;
2919 if (graphic->m_textPos) res.push_back(graphic->m_textPos);
2920 }
2921 }
2922 }
2923 else {
2924 for (auto const &graphic : m_structures->m_state->m_graphicsList) {
2925 if (graphic->m_type != 5) continue;
2926 if (graphic->m_textPos) res.push_back(graphic->m_textPos);
2927 }
2928 }
2929 return res;
2930 }
2931
2932 // ----------- character function ---------------------
2933 void MacWrtProStructuresListenerState::sendChar(char c)
2934 {
2935 if (!m_structures) return;
2936 bool newPageDone = m_newPageDone;
2937 m_newPageDone = false;
2938 MWAWTextListenerPtr listener=m_structures->getTextListener();
2939 if (!listener) return;
2940 switch (c) {
2941 case 0:
2942 break; // ignore
2943 case 3: // footnote ok
2944 case 4: // figure ok
2945 case 5: // hyphen ok
2946 break;
2947 case 7:
2948 if (m_version==0) {
2949 m_actTab = 0;
2950 listener->insertEOL(true);
2951 }
2952 else {
2953 MWAW_DEBUG_MSG(("MacWrtProStructuresListenerState::sendChar: Find odd char 0x7\n"));
2954 }
2955 break;
2956 case 0x9:
2957 if ((m_actTab++ < m_numTab) || m_actTab!=1)
2958 listener->insertTab();
2959 else // this case appears in list, 2.\tItem..., we do not always want a tab here
2960 listener->insertChar(' ');
2961 break;
2962 case 0xa:
2963 m_actTab = 0;
2964 if (newPageDone) break;
2965 listener->insertEOL();
2966 break; // soft break
2967 case 0xd:
2968 m_actTab = 0;
2969 if (newPageDone) break;
2970 listener->insertEOL();
2971 break;
2972 case 0xc:
2973 m_actTab = 0;
2974 if (m_isMainZone) newPage();
2975 break;
2976 case 0xb: // add a columnbreak
2977 m_actTab = 0;
2978 if (m_isMainZone) {
2979 if (m_numCols <= 1) newPage();
2980 else if (listener)
2981 listener->insertBreak(MWAWTextListener::ColumnBreak);
2982 }
2983 break;
2984 case 0xe:
2985 m_actTab = 0;
2986 if (!m_isMainZone) break;
2987
2988 // create a new section here
2989 if (listener->isSectionOpened())
2990 listener->closeSection();
2991 sendSection(++m_section);
2992 break;
2993 case 2: // for MWII
2994 case 0x15:
2995 case 0x17:
2996 case 0x1a:
2997 break;
2998 case 0x1f: // some hyphen
2999 break;
3000 /* 0x10 and 0x13 : bad character which can happen in conversion */
3001 default:
3002 listener->insertCharacter(static_cast<unsigned char>(c));
3003 break;
3004 }
3005 }
3006
3007 // ----------- font function ---------------------
3008 bool MacWrtProStructuresListenerState::sendFont(int id)
3009 {
3010 if (!m_structures) return false;
3011 if (!m_structures->getTextListener()) return true;
3012 if (id < 0 || id >= int(m_structures->m_state->m_fontsList.size())) {
3013 MWAW_DEBUG_MSG(("MacWrtProStructuresListenerState::sendFont: can not find font %d\n", id));
3014 return false;
3015 }
3016 m_structures->getTextListener()->setFont(m_structures->m_state->m_fontsList[size_t(id)].m_font);
3017
3018 return true;
3019 }
3020
3021 // ----------- paragraph function ---------------------
3022 bool MacWrtProStructuresListenerState::sendParagraph(int id)
3023 {
3024 if (!m_structures) return false;
3025 if (!m_structures->getTextListener()) return true;
3026 if (id < 0 || id >= int(m_structures->m_state->m_paragraphsList.size())) {
3027 MWAW_DEBUG_MSG(("MacWrtProStructuresListenerState::sendParagraph: can not find paragraph %d\n", id));
3028 return false;
3029 }
3030 auto const &para=m_structures->m_state->m_paragraphsList[size_t(id)];
3031 m_structures->getTextListener()->setParagraph(para);
3032 m_numTab = int(para.m_tabs->size());
3033 return true;
3034 }
3035
3036 // ----------- section function ---------------------
3037 void MacWrtProStructuresListenerState::sendSection(int nSection)
3038 {
3039 if (!m_structures) return;
3040 MWAWTextListenerPtr listener=m_structures->getTextListener();
3041 if (!listener) return;
3042 if (listener->isSectionOpened()) {
3043 MWAW_DEBUG_MSG(("MacWrtProStructuresListenerState::sendSection: a section is already opened\n"));
3044 listener->closeSection();
3045 }
3046 if (m_version==0) {
3047 double colSep=0.16666;
3048 m_numCols = (nSection==0 && m_structures->m_mainParser.hasTitlePage()) ? 1 : m_structures->m_mainParser.numColumns(colSep);
3049 if (m_numCols > 10) {
3050 MWAW_DEBUG_MSG(("MacWrtProStructuresListenerState::sendSection: num columns is to big, reset to 1\n"));
3051 m_numCols = 1;
3052 }
3053 MWAWSection sec;
3054 if (m_numCols>1)
3055 sec.setColumns(m_numCols, (m_structures->m_mainParser.getPageWidth()-colSep*double(m_numCols-1))/double(m_numCols), librevenge::RVNG_INCH, colSep);
3056 listener->openSection(sec);
3057 return;
3058 }
3059
3060 if (nSection >= int(m_structures->m_state->m_sectionsList.size())) {
3061 MWAW_DEBUG_MSG(("MacWrtProStructuresListenerState::sendSection: can not find section %d\n", nSection));
3062 return;
3063 }
3064 auto const &section = m_structures->m_state->m_sectionsList[size_t(nSection)];
3065 if (nSection && section.m_start != section.S_Line) newPage();
3066
3067 listener->openSection(section.getSection());
3068 m_numCols = listener->getSection().numColumns();
3069 }
3070 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: