Debian Codegraph @

src/lib/MWAWTextListener.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 /** \file MWAWTextListener.cxx
35 * Implements MWAWTextListener: the libmwaw word processor listener
36 *
37 * \note this class is the only class which does the interface with
38 * the librevenge::RVNGTextInterface
39 */
40
41 #include <cstring>
42 #include <iomanip>
43 #include <sstream>
44 #include <time.h>
45
46 #include <librevenge/librevenge.h>
47
48 #include "libmwaw_internal.hxx"
49
50 #include "MWAWCell.hxx"
51 #include "MWAWFont.hxx"
52 #include "MWAWFontConverter.hxx"
53 #include "MWAWGraphicEncoder.hxx"
54 #include "MWAWGraphicListener.hxx"
55 #include "MWAWGraphicStyle.hxx"
56 #include "MWAWGraphicShape.hxx"
57 #include "MWAWInputStream.hxx"
58 #include "MWAWList.hxx"
59 #include "MWAWPageSpan.hxx"
60 #include "MWAWParagraph.hxx"
61 #include "MWAWParser.hxx"
62 #include "MWAWPosition.hxx"
63 #include "MWAWSection.hxx"
64 #include "MWAWSubDocument.hxx"
65 #include "MWAWTable.hxx"
66
67 #include "MWAWTextListener.hxx"
68
69 //! Internal and low level namespace to define the states of MWAWTextListener
70 namespace MWAWTextListenerInternal
71 {
72 //! a enum to define basic break bit
73 enum { PageBreakBit=0x1, ColumnBreakBit=0x2 };
74 //! a class to store the document state of a MWAWTextListener
75 struct DocumentState {
76 //! constructor
77 explicit DocumentState(std::vector<MWAWPageSpan> const &pageList)
78 : m_pageList(pageList)
79 , m_pageSpan()
80 , m_metaData()
81 , m_footNoteNumber(0)
82 , m_endNoteNumber(0)
83 , m_smallPictureNumber(0)
84 , m_isDocumentStarted(false)
85 , m_isHeaderFooterStarted(false)
86 , m_sentListMarkers()
87 , m_subDocuments()
88 {
89 }
90 //! destructor
91 ~DocumentState()
92 {
93 }
94
95 //! the pages definition
96 std::vector<MWAWPageSpan> m_pageList;
97 //! the current page span
98 MWAWPageSpan m_pageSpan;
99 //! the document meta data
100 librevenge::RVNGPropertyList m_metaData;
101
102 int m_footNoteNumber /** footnote number*/, m_endNoteNumber /** endnote number*/;
103
104 int m_smallPictureNumber /** number of small picture */;
105 bool m_isDocumentStarted /** a flag to know if the document is open */, m_isHeaderFooterStarted /** a flag to know if the header footer is started */;
106 /// the list of marker corresponding to sent list
107 std::vector<int> m_sentListMarkers;
108 std::vector<MWAWSubDocumentPtr> m_subDocuments; /** list of document actually open */
109
110 private:
111 DocumentState(const DocumentState &) = delete;
112 DocumentState &operator=(const DocumentState &) = delete;
113 };
114
115 /** the state of a MWAWTextListener */
116 struct State {
117 //! constructor
118 State();
119 //! destructor
120 ~State() { }
121
122 //! a buffer to stored the text
123 librevenge::RVNGString m_textBuffer;
124 //! the number of tabs to add
125 int m_numDeferredTabs;
126
127 //! the font
128 MWAWFont m_font;
129 //! the paragraph
130 MWAWParagraph m_paragraph;
131 //! a sequence of bit used to know if we need page/column break
132 int m_paragraphNeedBreak;
133
134 std::shared_ptr<MWAWList> m_list;
135
136 bool m_isPageSpanOpened;
137 bool m_isSectionOpened;
138 bool m_isFrameOpened;
139 bool m_isPageSpanBreakDeferred;
140 bool m_isHeaderFooterWithoutParagraph;
141 //! a flag to know if openGroup was called
142 bool m_isGroupOpened;
143
144 bool m_isSpanOpened;
145 bool m_isParagraphOpened;
146 bool m_isListElementOpened;
147
148 bool m_firstParagraphInPageSpan;
149
150 bool m_isTableOpened;
151 bool m_isTableRowOpened;
152 bool m_isTableColumnOpened;
153 bool m_isTableCellOpened;
154
155 unsigned m_currentPage;
156 int m_numPagesRemainingInSpan;
157 int m_currentPageNumber;
158
159 bool m_sectionAttributesChanged;
160 //! the section
161 MWAWSection m_section;
162
163 std::vector<bool> m_listOrderedLevels; //! a stack used to know what is open
164
165 bool m_inSubDocument;
166
167 bool m_isNote;
168 bool m_inLink;
169 libmwaw::SubDocumentType m_subDocumentType;
170
171 private:
172 State(const State &) = delete;
173 State &operator=(const State &) = delete;
174 };
175
176 State::State()
177 : m_textBuffer("")
178 , m_numDeferredTabs(0)
179
180 , m_font(20,12) // default time 12
181
182 , m_paragraph()
183 , m_paragraphNeedBreak(0)
184
185 , m_list()
186
187 , m_isPageSpanOpened(false)
188 , m_isSectionOpened(false)
189 , m_isFrameOpened(false)
190 , m_isPageSpanBreakDeferred(false)
191 , m_isHeaderFooterWithoutParagraph(false)
192 , m_isGroupOpened(false)
193
194 , m_isSpanOpened(false)
195 , m_isParagraphOpened(false)
196 , m_isListElementOpened(false)
197
198 , m_firstParagraphInPageSpan(true)
199
200 , m_isTableOpened(false)
201 , m_isTableRowOpened(false)
202 , m_isTableColumnOpened(false)
203 , m_isTableCellOpened(false)
204
205 , m_currentPage(0)
206 , m_numPagesRemainingInSpan(0)
207 , m_currentPageNumber(1)
208
209 , m_sectionAttributesChanged(false)
210 , m_section()
211
212 , m_listOrderedLevels()
213
214 , m_inSubDocument(false)
215 , m_isNote(false)
216 , m_inLink(false)
217 , m_subDocumentType(libmwaw::DOC_NONE)
218 {
219 }
220 }
221
222 MWAWTextListener::MWAWTextListener(MWAWParserState &parserState, std::vector<MWAWPageSpan> const &pageList, librevenge::RVNGTextInterface *documentInterface)
223 : MWAWListener()
224 , m_ds(new MWAWTextListenerInternal::DocumentState(pageList))
225 , m_ps(new MWAWTextListenerInternal::State), m_psStack()
226 , m_parserState(parserState)
227 , m_documentInterface(documentInterface)
228 {
229 }
230
231 MWAWTextListener::~MWAWTextListener()
232 {
233 }
234
235 ///////////////////
236 // text data
237 ///////////////////
238 void MWAWTextListener::insertChar(uint8_t character)
239 {
240 if (character >= 0x80) {
241 MWAWTextListener::insertUnicode(character);
242 return;
243 }
244 _flushDeferredTabs();
245 if (!m_ps->m_isSpanOpened) _openSpan();
246 m_ps->m_textBuffer.append(char(character));
247 }
248
249 void MWAWTextListener::insertCharacter(unsigned char c)
250 {
251 int unicode = m_parserState.m_fontConverter->unicode(m_ps->m_font.id(), c);
252 if (unicode == -1) {
253 if (c < 0x20) {
254 MWAW_DEBUG_MSG(("MWAWTextListener::insertCharacter: Find odd char %x\n", static_cast<unsigned int>(c)));
255 }
256 else
257 MWAWTextListener::insertChar(static_cast<uint8_t>(c));
258 }
259 else
260 MWAWTextListener::insertUnicode(static_cast<uint32_t>(unicode));
261 }
262
263 int MWAWTextListener::insertCharacter(unsigned char c, MWAWInputStreamPtr &input, long endPos)
264 {
265 if (!input || !m_parserState.m_fontConverter) {
266 MWAW_DEBUG_MSG(("MWAWTextListener::insertCharacter: input or font converter does not exist!!!!\n"));
267 return 0;
268 }
269 long debPos=input->tell();
270 int fId = m_ps->m_font.id();
271 int unicode = endPos==debPos ?
272 m_parserState.m_fontConverter->unicode(fId, c) :
273 m_parserState.m_fontConverter->unicode(fId, c, input);
274
275 long pos=input->tell();
276 if (endPos > 0 && pos > endPos) {
277 MWAW_DEBUG_MSG(("MWAWTextListener::insertCharacter: problem reading a character\n"));
278 pos = debPos;
279 input->seek(pos, librevenge::RVNG_SEEK_SET);
280 unicode = m_parserState.m_fontConverter->unicode(fId, c);
281 }
282 if (unicode == -1) {
283 if (c < 0x20) {
284 MWAW_DEBUG_MSG(("MWAWTextListener::insertCharacter: Find odd char %x\n", static_cast<unsigned int>(c)));
285 }
286 else
287 MWAWTextListener::insertChar(static_cast<uint8_t>(c));
288 }
289 else
290 MWAWTextListener::insertUnicode(static_cast<uint32_t>(unicode));
291
292 return int(pos-debPos);
293 }
294
295 void MWAWTextListener::insertUnicode(uint32_t val)
296 {
297 // undef character, we skip it
298 if (val == 0xfffd) return;
299
300 _flushDeferredTabs();
301 if (!m_ps->m_isSpanOpened) _openSpan();
302 libmwaw::appendUnicode(val, m_ps->m_textBuffer);
303 }
304
305 void MWAWTextListener::insertUnicodeString(librevenge::RVNGString const &str)
306 {
307 _flushDeferredTabs();
308 if (!m_ps->m_isSpanOpened) _openSpan();
309 m_ps->m_textBuffer.append(str);
310 }
311
312 void MWAWTextListener::insertEOL(bool soft)
313 {
314 if (!m_ps->m_isParagraphOpened && !m_ps->m_isListElementOpened)
315 _openSpan();
316 _flushDeferredTabs();
317
318 if (soft) {
319 if (m_ps->m_isSpanOpened)
320 _flushText();
321 m_documentInterface->insertLineBreak();
322 }
323 else if (m_ps->m_isParagraphOpened)
324 _closeParagraph();
325
326 // sub/superscript must not survive a new line
327 m_ps->m_font.set(MWAWFont::Script());
328 }
329
330 void MWAWTextListener::insertTab()
331 {
332 if (!m_ps->m_isParagraphOpened) {
333 m_ps->m_numDeferredTabs++;
334 return;
335 }
336 if (m_ps->m_isSpanOpened) _flushText();
337 m_ps->m_numDeferredTabs++;
338 _flushDeferredTabs();
339 }
340
341 void MWAWTextListener::insertBreak(MWAWTextListener::BreakType breakType)
342 {
343 switch (breakType) {
344 case ColumnBreak:
345 if (!m_ps->m_isPageSpanOpened && !m_ps->m_inSubDocument)
346 _openSpan();
347 if (m_ps->m_isParagraphOpened)
348 _closeParagraph();
349 m_ps->m_paragraphNeedBreak |= MWAWTextListenerInternal::ColumnBreakBit;
350 break;
351 case PageBreak:
352 if (!m_ps->m_isPageSpanOpened && !m_ps->m_inSubDocument)
353 _openSpan();
354 if (m_ps->m_isParagraphOpened)
355 _closeParagraph();
356 m_ps->m_paragraphNeedBreak |= MWAWTextListenerInternal::PageBreakBit;
357 break;
358 case SoftPageBreak:
359 #if !defined(__clang__)
360 default:
361 #endif
362 break;
363 }
364
365 if (m_ps->m_inSubDocument)
366 return;
367
368 switch (breakType) {
369 case PageBreak:
370 case SoftPageBreak:
371 if (m_ps->m_numPagesRemainingInSpan > 0)
372 m_ps->m_numPagesRemainingInSpan--;
373 else {
374 if (!m_ps->m_isTableOpened && !m_ps->m_isParagraphOpened && !m_ps->m_isListElementOpened)
375 _closePageSpan();
376 else
377 m_ps->m_isPageSpanBreakDeferred = true;
378 }
379 m_ps->m_currentPageNumber++;
380 break;
381 case ColumnBreak:
382 #if !defined(__clang__)
383 default:
384 #endif
385 break;
386 }
387 }
388
389 void MWAWTextListener::_insertBreakIfNecessary(librevenge::RVNGPropertyList &propList)
390 {
391 if (!m_ps->m_paragraphNeedBreak)
392 return;
393
394 if ((m_ps->m_paragraphNeedBreak&MWAWTextListenerInternal::PageBreakBit) ||
395 m_ps->m_section.numColumns() <= 1) {
396 if (m_ps->m_inSubDocument) {
397 MWAW_DEBUG_MSG(("MWAWTextListener::_insertBreakIfNecessary: can not add page break in subdocument\n"));
398 }
399 else
400 propList.insert("fo:break-before", "page");
401 }
402 else if (m_ps->m_paragraphNeedBreak&MWAWTextListenerInternal::ColumnBreakBit)
403 propList.insert("fo:break-before", "column");
404 m_ps->m_paragraphNeedBreak=0;
405 }
406
407 ///////////////////
408 // font/paragraph function
409 ///////////////////
410 void MWAWTextListener::setFont(MWAWFont const &font)
411 {
412 if (font == m_ps->m_font) return;
413
414 // check if id and size are defined, if not used the previous fields
415 MWAWFont finalFont(font);
416 if (font.id() == -1)
417 finalFont.setId(m_ps->m_font.id());
418 if (font.size() <= 0)
419 finalFont.setSize(m_ps->m_font.size());
420 if (finalFont == m_ps->m_font) return;
421
422 _closeSpan();
423 m_ps->m_font = finalFont;
424 }
425
426 MWAWFont const &MWAWTextListener::getFont() const
427 {
428 return m_ps->m_font;
429 }
430
431 bool MWAWTextListener::isParagraphOpened() const
432 {
433 return m_ps->m_isParagraphOpened;
434 }
435
436 void MWAWTextListener::setParagraph(MWAWParagraph const &para)
437 {
438 if (para==m_ps->m_paragraph) return;
439
440 m_ps->m_paragraph=para;
441 }
442
443 MWAWParagraph const &MWAWTextListener::getParagraph() const
444 {
445 return m_ps->m_paragraph;
446 }
447
448 ///////////////////
449 // field/link :
450 ///////////////////
451 void MWAWTextListener::insertField(MWAWField const &field)
452 {
453 librevenge::RVNGPropertyList propList;
454 if (field.addTo(propList)) {
455 _flushDeferredTabs();
456 _flushText();
457 _openSpan();
458 m_documentInterface->insertField(propList);
459 return;
460 }
461 librevenge::RVNGString text=field.getString();
462 if (!text.empty())
463 MWAWTextListener::insertUnicodeString(text);
464 else {
465 MWAW_DEBUG_MSG(("MWAWTextListener::insertField: must not be called with type=%d\n", int(field.m_type)));
466 }
467 }
468
469 void MWAWTextListener::openLink(MWAWLink const &link)
470 {
471 if (m_ps->m_inLink) {
472 MWAW_DEBUG_MSG(("MWAWTextListener:openLink: a link is already opened\n"));
473 return;
474 }
475 if (!m_ps->m_isSpanOpened) _openSpan();
476 librevenge::RVNGPropertyList propList;
477 link.addTo(propList);
478 m_documentInterface->openLink(propList);
479 _pushParsingState();
480 m_ps->m_inLink=true;
481 // we do not want any close open paragraph in a link
482 m_ps->m_isParagraphOpened=true;
483 }
484
485 void MWAWTextListener::closeLink()
486 {
487 if (!m_ps->m_inLink) {
488 MWAW_DEBUG_MSG(("MWAWTextListener:closeLink: can not close a link\n"));
489 return;
490 }
491 if (m_ps->m_isSpanOpened) _closeSpan();
492 m_documentInterface->closeLink();
493 _popParsingState();
494 }
495
496 ///////////////////
497 // document
498 ///////////////////
499 void MWAWTextListener::setDocumentMetaData(librevenge::RVNGPropertyList const &meta)
500 {
501 librevenge::RVNGPropertyList::Iter i(meta);
502 for (i.rewind(); i.next();)
503 m_ds->m_metaData.insert(i.key(), i()->getStr());
504 }
505
506 void MWAWTextListener::setDocumentLanguage(std::string const &locale)
507 {
508 if (!locale.length()) return;
509 m_ds->m_metaData.insert("librevenge:language", locale.c_str());
510 }
511
512 bool MWAWTextListener::isDocumentStarted() const
513 {
514 return m_ds->m_isDocumentStarted;
515 }
516
517 void MWAWTextListener::startDocument()
518 {
519 if (m_ds->m_isDocumentStarted) {
520 MWAW_DEBUG_MSG(("MWAWTextListener::startDocument: the document is already started\n"));
521 return;
522 }
523
524 m_documentInterface->startDocument(librevenge::RVNGPropertyList());
525 m_ds->m_isDocumentStarted = true;
526
527 m_documentInterface->setDocumentMetaData(m_ds->m_metaData);
528 }
529
530 void MWAWTextListener::endDocument(bool sendDelayedSubDoc)
531 {
532 if (!m_ds->m_isDocumentStarted) {
533 MWAW_DEBUG_MSG(("MWAWTextListener::endDocument: the document is not started\n"));
534 return;
535 }
536
537 if (!m_ps->m_isPageSpanOpened) {
538 // we must call by hand openPageSpan to avoid sending any header/footer documents
539 if (!sendDelayedSubDoc) _openPageSpan(false);
540 _openSpan();
541 }
542
543 if (m_ps->m_isTableOpened)
544 closeTable();
545 if (m_ps->m_isParagraphOpened)
546 _closeParagraph();
547
548 m_ps->m_paragraph.m_listLevelIndex = 0;
549 _changeList(); // flush the list exterior
550
551 // close the document nice and tight
552 _closeSection();
553 _closePageSpan();
554 m_documentInterface->endDocument();
555 m_ds->m_isDocumentStarted = false;
556 }
557
558 ///////////////////
559 // page
560 ///////////////////
561 bool MWAWTextListener::isPageSpanOpened() const
562 {
563 return m_ps->m_isPageSpanOpened;
564 }
565
566 MWAWPageSpan const &MWAWTextListener::getPageSpan()
567 {
568 if (!m_ps->m_isPageSpanOpened)
569 _openPageSpan();
570 return m_ds->m_pageSpan;
571 }
572
573
574 void MWAWTextListener::_openPageSpan(bool sendHeaderFooters)
575 {
576 if (m_ps->m_isPageSpanOpened)
577 return;
578
579 if (!m_ds->m_isDocumentStarted)
580 startDocument();
581
582 if (m_ds->m_pageList.size()==0) {
583 MWAW_DEBUG_MSG(("MWAWTextListener::_openPageSpan: can not find any page\n"));
584 throw libmwaw::ParseException();
585 }
586 unsigned actPage = 0;
587 auto it = m_ds->m_pageList.begin();
588 ++m_ps->m_currentPage;
589 while (true) {
590 actPage+=static_cast<unsigned>(it->getPageSpan());
591 if (actPage>=m_ps->m_currentPage) break;
592 if (++it == m_ds->m_pageList.end()) {
593 MWAW_DEBUG_MSG(("MWAWTextListener::_openPageSpan: can not find current page, use last page\n"));
594 --it;
595 break;
596 }
597 }
598 MWAWPageSpan &currentPage = *it;
599
600 librevenge::RVNGPropertyList propList;
601 currentPage.getPageProperty(propList);
602 propList.insert("librevenge:is-last-page-span", ++it == m_ds->m_pageList.end());
603
604 if (!m_ps->m_isPageSpanOpened)
605 m_documentInterface->openPageSpan(propList);
606
607 m_ps->m_isPageSpanOpened = true;
608 m_ds->m_pageSpan = currentPage;
609
610 // we insert the header footer
611 if (sendHeaderFooters)
612 currentPage.sendHeaderFooters(this);
613
614 // first paragraph in span (necessary for resetting page number)
615 m_ps->m_firstParagraphInPageSpan = true;
616 m_ps->m_numPagesRemainingInSpan = (currentPage.getPageSpan() - 1);
617 }
618
619 void MWAWTextListener::_closePageSpan()
620 {
621 if (!m_ps->m_isPageSpanOpened)
622 return;
623
624 if (m_ps->m_isSectionOpened)
625 _closeSection();
626
627 m_documentInterface->closePageSpan();
628 m_ps->m_isPageSpanOpened = m_ps->m_isPageSpanBreakDeferred = false;
629 }
630
631 ///////////////////
632 // header/footer
633 ///////////////////
634 bool MWAWTextListener::isHeaderFooterOpened() const
635 {
636 return m_ds->m_isHeaderFooterStarted;
637 }
638
639 bool MWAWTextListener::insertHeader(MWAWSubDocumentPtr const &subDocument, librevenge::RVNGPropertyList const &extras)
640 {
641 if (m_ds->m_isHeaderFooterStarted) {
642 MWAW_DEBUG_MSG(("MWAWTextListener::insertHeader: Oops a header/footer is already opened\n"));
643 return false;
644 }
645 librevenge::RVNGPropertyList propList(extras);
646 m_documentInterface->openHeader(propList);
647 handleSubDocument(subDocument, libmwaw::DOC_HEADER_FOOTER);
648 m_documentInterface->closeHeader();
649 return true;
650 }
651
652 bool MWAWTextListener::insertFooter(MWAWSubDocumentPtr const &subDocument, librevenge::RVNGPropertyList const &extras)
653 {
654 if (m_ds->m_isHeaderFooterStarted) {
655 MWAW_DEBUG_MSG(("MWAWTextListener::insertFooter: Oops a header/footer is already opened\n"));
656 return false;
657 }
658 librevenge::RVNGPropertyList propList(extras);
659 m_documentInterface->openFooter(propList);
660 handleSubDocument(subDocument, libmwaw::DOC_HEADER_FOOTER);
661 m_documentInterface->closeFooter();
662 return true;
663 }
664
665 ///////////////////
666 // section
667 ///////////////////
668 bool MWAWTextListener::isSectionOpened() const
669 {
670 return m_ps->m_isSectionOpened;
671 }
672
673 MWAWSection const &MWAWTextListener::getSection() const
674 {
675 return m_ps->m_section;
676 }
677
678 bool MWAWTextListener::canOpenSectionAddBreak() const
679 {
680 return !m_ps->m_isTableOpened && (!m_ps->m_inSubDocument || m_ps->m_subDocumentType == libmwaw::DOC_TEXT_BOX);
681 }
682
683 bool MWAWTextListener::openSection(MWAWSection const &section)
684 {
685 if (m_ps->m_isSectionOpened) {
686 MWAW_DEBUG_MSG(("MWAWTextListener::openSection: a section is already opened\n"));
687 return false;
688 }
689
690 if (m_ps->m_isTableOpened || (m_ps->m_inSubDocument && m_ps->m_subDocumentType != libmwaw::DOC_TEXT_BOX)) {
691 MWAW_DEBUG_MSG(("MWAWTextListener::openSection: impossible to open a section\n"));
692 return false;
693 }
694
695 m_ps->m_section=section;
696 _openSection();
697 return true;
698 }
699
700 bool MWAWTextListener::closeSection()
701 {
702 if (!m_ps->m_isSectionOpened) {
703 MWAW_DEBUG_MSG(("MWAWTextListener::closeSection: no section are already opened\n"));
704 return false;
705 }
706
707 if (m_ps->m_isTableOpened || (m_ps->m_inSubDocument && m_ps->m_subDocumentType != libmwaw::DOC_TEXT_BOX)) {
708 MWAW_DEBUG_MSG(("MWAWTextListener::closeSection: impossible to close a section\n"));
709 return false;
710 }
711 _closeSection();
712 return true;
713 }
714
715 void MWAWTextListener::_openSection()
716 {
717 if (m_ps->m_isSectionOpened) {
718 MWAW_DEBUG_MSG(("MWAWTextListener::_openSection: a section is already opened\n"));
719 return;
720 }
721
722 if (!m_ps->m_isPageSpanOpened)
723 _openPageSpan();
724
725 librevenge::RVNGPropertyList propList;
726 m_ps->m_section.addTo(propList);
727
728 librevenge::RVNGPropertyListVector columns;
729 m_ps->m_section.addColumnsTo(columns);
730 if (columns.count())
731 propList.insert("style:columns", columns);
732 m_documentInterface->openSection(propList);
733
734 m_ps->m_sectionAttributesChanged = false;
735 m_ps->m_isSectionOpened = true;
736 }
737
738 void MWAWTextListener::_closeSection()
739 {
740 if (!m_ps->m_isSectionOpened ||m_ps->m_isTableOpened)
741 return;
742
743 if (m_ps->m_isParagraphOpened)
744 _closeParagraph();
745 m_ps->m_paragraph.m_listLevelIndex=0;
746 _changeList();
747
748 m_documentInterface->closeSection();
749
750 m_ps->m_section = MWAWSection();
751 m_ps->m_sectionAttributesChanged = false;
752 m_ps->m_isSectionOpened = false;
753 }
754
755 ///////////////////
756 // paragraph
757 ///////////////////
758 void MWAWTextListener::_openParagraph()
759 {
760 if (m_ps->m_isTableOpened && !m_ps->m_isTableCellOpened)
761 return;
762
763 if (m_ps->m_isParagraphOpened || m_ps->m_isListElementOpened) {
764 MWAW_DEBUG_MSG(("MWAWTextListener::_openParagraph: a paragraph (or a list) is already opened"));
765 return;
766 }
767 if (!m_ps->m_isTableOpened && (!m_ps->m_inSubDocument || m_ps->m_subDocumentType == libmwaw::DOC_TEXT_BOX)) {
768 if (m_ps->m_sectionAttributesChanged)
769 _closeSection();
770
771 if (!m_ps->m_isSectionOpened)
772 _openSection();
773 }
774
775 librevenge::RVNGPropertyList propList;
776 _appendParagraphProperties(propList);
777 if (!m_ps->m_isParagraphOpened)
778 m_documentInterface->openParagraph(propList);
779
780 _resetParagraphState();
781 m_ps->m_firstParagraphInPageSpan = false;
782 }
783
784 void MWAWTextListener::_closeParagraph()
785 {
786 // we can not close a paragraph in a link
787 if (m_ps->m_inLink)
788 return;
789 if (m_ps->m_isListElementOpened) {
790 _closeListElement();
791 return;
792 }
793
794 if (m_ps->m_isParagraphOpened) {
795 if (m_ps->m_isSpanOpened)
796 _closeSpan();
797
798 m_documentInterface->closeParagraph();
799 }
800
801 m_ps->m_isParagraphOpened = false;
802 m_ps->m_paragraph.m_listLevelIndex = 0;
803
804 if (!m_ps->m_isTableOpened && m_ps->m_isPageSpanBreakDeferred && !m_ps->m_inSubDocument)
805 _closePageSpan();
806 }
807
808 void MWAWTextListener::_resetParagraphState(const bool isListElement)
809 {
810 m_ps->m_paragraphNeedBreak = 0;
811 m_ps->m_isListElementOpened = isListElement;
812 m_ps->m_isParagraphOpened = true;
813 m_ps->m_isHeaderFooterWithoutParagraph = false;
814 }
815
816 void MWAWTextListener::_appendParagraphProperties(librevenge::RVNGPropertyList &propList, const bool /*isListElement*/)
817 {
818 m_ps->m_paragraph.addTo(propList,m_ps->m_isTableOpened);
819
820 if (!m_ps->m_inSubDocument && m_ps->m_firstParagraphInPageSpan && m_ds->m_pageSpan.getPageNumber() >= 0)
821 propList.insert("style:page-number", m_ds->m_pageSpan.getPageNumber());
822
823 _insertBreakIfNecessary(propList);
824 }
825
826 ///////////////////
827 // list
828 ///////////////////
829 void MWAWTextListener::_openListElement()
830 {
831 if (m_ps->m_isTableOpened && !m_ps->m_isTableCellOpened)
832 return;
833
834 if (m_ps->m_isParagraphOpened || m_ps->m_isListElementOpened)
835 return;
836
837 if (!m_ps->m_isTableOpened && (!m_ps->m_inSubDocument || m_ps->m_subDocumentType == libmwaw::DOC_TEXT_BOX)) {
838 if (m_ps->m_sectionAttributesChanged)
839 _closeSection();
840
841 if (!m_ps->m_isSectionOpened)
842 _openSection();
843 }
844
845 librevenge::RVNGPropertyList propList;
846 _appendParagraphProperties(propList, true);
847 // check if we must change the start value
848 int startValue=m_ps->m_paragraph.m_listStartValue.get();
849 if (startValue > 0 && m_ps->m_list && m_ps->m_list->getStartValueForNextElement() != startValue) {
850 propList.insert("text:start-value", startValue);
851 m_ps->m_list->setStartValueForNextElement(startValue);
852 }
853
854 if (m_ps->m_list) m_ps->m_list->openElement();
855 m_documentInterface->openListElement(propList);
856 _resetParagraphState(true);
857 }
858
859 void MWAWTextListener::_closeListElement()
860 {
861 if (m_ps->m_isListElementOpened) {
862 if (m_ps->m_isSpanOpened)
863 _closeSpan();
864
865 if (m_ps->m_list) m_ps->m_list->closeElement();
866 m_documentInterface->closeListElement();
867 }
868
869 m_ps->m_isListElementOpened = m_ps->m_isParagraphOpened = false;
870
871 if (!m_ps->m_isTableOpened && m_ps->m_isPageSpanBreakDeferred && !m_ps->m_inSubDocument)
872 _closePageSpan();
873 }
874
875 int MWAWTextListener::_getListId() const
876 {
877 auto newLevel= size_t(m_ps->m_paragraph.m_listLevelIndex.get());
878 if (newLevel == 0) return -1;
879 int newListId = m_ps->m_paragraph.m_listId.get();
880 if (newListId > 0) return newListId;
881 static bool first = true;
882 if (first) {
883 MWAW_DEBUG_MSG(("MWAWTextListener::_getListId: the list id is not set, try to find a new one\n"));
884 first = false;
885 }
886 auto list=m_parserState.m_listManager->getNewList(m_ps->m_list, int(newLevel), *m_ps->m_paragraph.m_listLevel);
887 if (!list) return -1;
888 return list->getId();
889 }
890
891 void MWAWTextListener::_changeList()
892 {
893 if (m_ps->m_isParagraphOpened)
894 _closeParagraph();
895
896 size_t actualLevel = m_ps->m_listOrderedLevels.size();
897 auto newLevel= size_t(m_ps->m_paragraph.m_listLevelIndex.get() > 0 ? m_ps->m_paragraph.m_listLevelIndex.get() : 0);
898 if (newLevel>100) {
899 MWAW_DEBUG_MSG(("MWAWTextListener::_changeList: find level=%d, set it to 100\n", static_cast<int>(newLevel)));
900 newLevel=100;
901 }
902 if (!m_ps->m_isSectionOpened && newLevel && !m_ps->m_isTableOpened &&
903 (!m_ps->m_inSubDocument || m_ps->m_subDocumentType == libmwaw::DOC_TEXT_BOX))
904 _openSection();
905
906 int newListId = newLevel>0 ? _getListId() : -1;
907 bool changeList = newLevel &&
908 (m_ps->m_list && m_ps->m_list->getId()!=newListId);
909 size_t minLevel = changeList ? 0 : newLevel;
910 while (actualLevel > minLevel) {
911 if (m_ps->m_listOrderedLevels[--actualLevel])
912 m_documentInterface->closeOrderedListLevel();
913 else
914 m_documentInterface->closeUnorderedListLevel();
915 }
916
917 if (newLevel) {
918 std::shared_ptr<MWAWList> theList;
919
920 theList=m_parserState.m_listManager->getList(newListId);
921 if (!theList) {
922 MWAW_DEBUG_MSG(("MWAWTextListener::_changeList: can not find any list\n"));
923 m_ps->m_listOrderedLevels.resize(actualLevel);
924 return;
925 }
926 m_parserState.m_listManager->needToSend(newListId, m_ds->m_sentListMarkers);
927 m_ps->m_list = theList;
928 m_ps->m_list->setLevel(static_cast<int>(newLevel));
929 }
930
931 m_ps->m_listOrderedLevels.resize(newLevel, false);
932 if (actualLevel == newLevel) return;
933
934 for (size_t i=actualLevel+1; i<= newLevel; i++) {
935 bool ordered = m_ps->m_list->isNumeric(int(i));
936 m_ps->m_listOrderedLevels[i-1] = ordered;
937
938 librevenge::RVNGPropertyList level;
939 m_ps->m_list->addTo(int(i), level, m_parserState.m_fontManager);
940 if (ordered)
941 m_documentInterface->openOrderedListLevel(level);
942 else
943 m_documentInterface->openUnorderedListLevel(level);
944 }
945 }
946
947 ///////////////////
948 // span
949 ///////////////////
950 void MWAWTextListener::_openSpan()
951 {
952 if (m_ps->m_isSpanOpened)
953 return;
954
955 if (m_ps->m_isTableOpened && !m_ps->m_isTableCellOpened)
956 return;
957
958 if (!m_ps->m_isParagraphOpened && !m_ps->m_isListElementOpened) {
959 _changeList();
960 if (*m_ps->m_paragraph.m_listLevelIndex == 0)
961 _openParagraph();
962 else
963 _openListElement();
964 }
965
966 librevenge::RVNGPropertyList propList;
967 m_ps->m_font.addTo(propList, m_parserState.m_fontConverter);
968
969 m_documentInterface->openSpan(propList);
970
971 m_ps->m_isSpanOpened = true;
972 }
973
974 void MWAWTextListener::_closeSpan()
975 {
976 // better not to close a link...
977 if (!m_ps->m_isSpanOpened)
978 return;
979
980 _flushText();
981 m_documentInterface->closeSpan();
982 m_ps->m_isSpanOpened = false;
983 }
984
985 ///////////////////
986 // text (send data)
987 ///////////////////
988 void MWAWTextListener::_flushDeferredTabs()
989 {
990 if (m_ps->m_numDeferredTabs == 0) return;
991 if (!m_ps->m_font.hasDecorationLines()) {
992 if (!m_ps->m_isSpanOpened) _openSpan();
993 for (; m_ps->m_numDeferredTabs > 0; m_ps->m_numDeferredTabs--)
994 m_documentInterface->insertTab();
995 return;
996 }
997
998 MWAWFont oldFont(m_ps->m_font);
999 m_ps->m_font.resetDecorationLines();
1000 _closeSpan();
1001 _openSpan();
1002 for (; m_ps->m_numDeferredTabs > 0; m_ps->m_numDeferredTabs--)
1003 m_documentInterface->insertTab();
1004 setFont(oldFont);
1005 }
1006
1007 void MWAWTextListener::_flushText()
1008 {
1009 if (m_ps->m_textBuffer.len() == 0) return;
1010
1011 // when some many ' ' follows each other, call insertSpace
1012 librevenge::RVNGString tmpText;
1013 int numConsecutiveSpaces = 0;
1014 librevenge::RVNGString::Iter i(m_ps->m_textBuffer);
1015 for (i.rewind(); i.next();) {
1016 if (*(i()) == 0x20) // this test is compatible with unicode format
1017 numConsecutiveSpaces++;
1018 else
1019 numConsecutiveSpaces = 0;
1020
1021 if (numConsecutiveSpaces > 1) {
1022 if (tmpText.len() > 0) {
1023 m_documentInterface->insertText(tmpText);
1024 tmpText.clear();
1025 }
1026 m_documentInterface->insertSpace();
1027 }
1028 else
1029 tmpText.append(i());
1030 }
1031 m_documentInterface->insertText(tmpText);
1032 m_ps->m_textBuffer.clear();
1033 }
1034
1035 ///////////////////
1036 // Note/Comment/picture/textbox
1037 ///////////////////
1038 void MWAWTextListener::insertNote(MWAWNote const &note, MWAWSubDocumentPtr &subDocument)
1039 {
1040 if (m_ps->m_isNote) {
1041 MWAW_DEBUG_MSG(("MWAWTextListener::insertNote try to insert a note recursively (ignored)\n"));
1042 return;
1043 }
1044
1045 m_ps->m_isNote = true;
1046 if (m_ds->m_isHeaderFooterStarted) {
1047 MWAW_DEBUG_MSG(("MWAWTextListener::insertNote try to insert a note in a header/footer\n"));
1048 /** Must not happen excepted in corrupted document, so we do the minimum.
1049 Note that we have no choice, either we begin by closing the paragraph,
1050 ... or we reprogram handleSubDocument.
1051 */
1052 if (m_ps->m_isParagraphOpened)
1053 _closeParagraph();
1054 int prevListLevel = *m_ps->m_paragraph.m_listLevelIndex;
1055 m_ps->m_paragraph.m_listLevelIndex = 0;
1056 _changeList(); // flush the list exterior
1057 handleSubDocument(subDocument, libmwaw::DOC_NOTE);
1058 m_ps->m_paragraph.m_listLevelIndex = prevListLevel;
1059 }
1060 else {
1061 if (!m_ps->m_isParagraphOpened)
1062 _openParagraph();
1063 else {
1064 _flushText();
1065 _closeSpan();
1066 }
1067
1068 librevenge::RVNGPropertyList propList;
1069 if (note.m_label.len())
1070 propList.insert("text:label", librevenge::RVNGPropertyFactory::newStringProp(note.m_label));
1071 if (note.m_type == MWAWNote::FootNote) {
1072 if (note.m_number >= 0)
1073 m_ds->m_footNoteNumber = note.m_number;
1074 else
1075 m_ds->m_footNoteNumber++;
1076 propList.insert("librevenge:number", m_ds->m_footNoteNumber);
1077 m_documentInterface->openFootnote(propList);
1078 handleSubDocument(subDocument, libmwaw::DOC_NOTE);
1079 m_documentInterface->closeFootnote();
1080 }
1081 else {
1082 if (note.m_number >= 0)
1083 m_ds->m_endNoteNumber = note.m_number;
1084 else
1085 m_ds->m_endNoteNumber++;
1086 propList.insert("librevenge:number", m_ds->m_endNoteNumber);
1087 m_documentInterface->openEndnote(propList);
1088 handleSubDocument(subDocument, libmwaw::DOC_NOTE);
1089 m_documentInterface->closeEndnote();
1090 }
1091 }
1092 m_ps->m_isNote = false;
1093 }
1094
1095 void MWAWTextListener::insertComment(MWAWSubDocumentPtr &subDocument)
1096 {
1097 if (m_ps->m_isNote) {
1098 MWAW_DEBUG_MSG(("MWAWTextListener::insertComment try to insert a note recursively (ignored)\n"));
1099 return;
1100 }
1101
1102 if (!m_ps->m_isParagraphOpened)
1103 _openParagraph();
1104 else {
1105 _flushText();
1106 _closeSpan();
1107 }
1108
1109 librevenge::RVNGPropertyList propList;
1110 m_documentInterface->openComment(propList);
1111
1112 m_ps->m_isNote = true;
1113 handleSubDocument(subDocument, libmwaw::DOC_COMMENT_ANNOTATION);
1114
1115 m_documentInterface->closeComment();
1116 m_ps->m_isNote = false;
1117 }
1118
1119 void MWAWTextListener::insertTextBox
1120 (MWAWPosition const &pos, MWAWSubDocumentPtr const &subDocument, MWAWGraphicStyle const &frameStyle)
1121 {
1122 if (!openFrame(pos, frameStyle)) return;
1123
1124 librevenge::RVNGPropertyList propList;
1125 if (!frameStyle.m_frameNextName.empty())
1126 propList.insert("librevenge:next-frame-name",frameStyle.m_frameNextName.c_str());
1127 // todo check style.m_rotate and style.m_flip
1128 m_documentInterface->openTextBox(propList);
1129 handleSubDocument(subDocument, libmwaw::DOC_TEXT_BOX);
1130 m_documentInterface->closeTextBox();
1131
1132 closeFrame();
1133 }
1134
1135 void MWAWTextListener::insertShape
1136 (MWAWPosition const &pos, MWAWGraphicShape const &shape, MWAWGraphicStyle const &style)
1137 {
1138 // sanity check: avoid to send to many small pict
1139 float factor=pos.getScaleFactor(pos.unit(), librevenge::RVNG_POINT);
1140 if (pos.size()[0]*factor <= 8 && pos.size()[1]*factor <= 8 && m_ds->m_smallPictureNumber++ > 200) {
1141 static bool first = true;
1142 if (first) {
1143 first = false;
1144 MWAW_DEBUG_MSG(("MWAWTextListener::insertShape: find too much small pictures, skip them from now\n"));
1145 }
1146 return;
1147 }
1148
1149 // now check that the anchor is coherent with the actual state
1150 switch (pos.m_anchorTo) {
1151 case MWAWPosition::Page:
1152 break;
1153 case MWAWPosition::Paragraph:
1154 if (m_ps->m_isParagraphOpened)
1155 _flushText();
1156 else
1157 _openParagraph();
1158 break;
1159 case MWAWPosition::Unknown:
1160 #if !defined(__clang__)
1161 default:
1162 #endif
1163 MWAW_DEBUG_MSG(("MWAWTextListener::insertShape: UNKNOWN position, insert as char position\n"));
1164 MWAW_FALLTHROUGH;
1165 case MWAWPosition::CharBaseLine:
1166 case MWAWPosition::Char:
1167 if (m_ps->m_isSpanOpened)
1168 _flushText();
1169 else
1170 _openSpan();
1171 break;
1172 case MWAWPosition::Cell:
1173 case MWAWPosition::Frame:
1174 break;
1175 }
1176
1177 librevenge::RVNGPropertyList shapePList;
1178 _handleFrameParameters(shapePList, pos);
1179 shapePList.remove("svg:x");
1180 shapePList.remove("svg:y");
1181
1182 librevenge::RVNGPropertyList list;
1183 style.addTo(list, shape.getType()==MWAWGraphicShape::Line);
1184
1185 MWAWVec2f decal = factor*pos.origin();
1186 switch (shape.addTo(decal, style.hasSurface(), shapePList)) {
1187 case MWAWGraphicShape::C_Ellipse:
1188 m_documentInterface->defineGraphicStyle(list);
1189 m_documentInterface->drawEllipse(shapePList);
1190 break;
1191 case MWAWGraphicShape::C_Path: {
1192 // odt seems to have some problem displaying path so
1193 // first create the picture, reset origin (if it is bad)
1194 MWAWBox2f bdbox = shape.getBdBox(style,true);
1195 MWAWGraphicEncoder graphicEncoder;
1196 MWAWGraphicListener graphicListener(m_parserState, MWAWBox2f(MWAWVec2f(0,0),bdbox.size()), &graphicEncoder);
1197 graphicListener.startDocument();
1198 MWAWPosition pathPos(-1.f*bdbox[0],bdbox.size(),librevenge::RVNG_POINT);
1199 pathPos.m_anchorTo=MWAWPosition::Page;
1200 graphicListener.insertShape(pathPos, shape, style);
1201 graphicListener.endDocument();
1202
1203 MWAWEmbeddedObject picture;
1204 if (!graphicEncoder.getBinaryResult(picture) || !openFrame(pos))
1205 break;
1206 librevenge::RVNGPropertyList propList;
1207 if (picture.addTo(propList))
1208 m_documentInterface->insertBinaryObject(propList);
1209 closeFrame();
1210 break;
1211 }
1212 case MWAWGraphicShape::C_Polyline:
1213 m_documentInterface->defineGraphicStyle(list);
1214 m_documentInterface->drawPolyline(shapePList);
1215 break;
1216 case MWAWGraphicShape::C_Polygon:
1217 m_documentInterface->defineGraphicStyle(list);
1218 m_documentInterface->drawPolygon(shapePList);
1219 break;
1220 case MWAWGraphicShape::C_Rectangle:
1221 m_documentInterface->defineGraphicStyle(list);
1222 m_documentInterface->drawRectangle(shapePList);
1223 break;
1224 case MWAWGraphicShape::C_Bad:
1225 break;
1226 #if !defined(__clang__)
1227 default:
1228 MWAW_DEBUG_MSG(("MWAWTextListener::insertShape: unexpected shape\n"));
1229 break;
1230 #endif
1231 }
1232 }
1233
1234 void MWAWTextListener::insertPicture(MWAWPosition const &pos, MWAWEmbeddedObject const &picture, MWAWGraphicStyle const &style)
1235 {
1236 // sanity check: avoid to send to many small pict
1237 float factor=pos.getScaleFactor(pos.unit(), librevenge::RVNG_POINT);
1238 if (pos.size()[0]*factor <= 8 && pos.size()[1]*factor <= 8 && m_ds->m_smallPictureNumber++ > 200) {
1239 static bool first = true;
1240 if (first) {
1241 first = false;
1242 MWAW_DEBUG_MSG(("MWAWTextListener::insertPicture: find too much small pictures, skip them from now\n"));
1243 }
1244 return;
1245 }
1246
1247 if (!openFrame(pos, style)) return;
1248
1249 librevenge::RVNGPropertyList propList;
1250 if (picture.addTo(propList))
1251 m_documentInterface->insertBinaryObject(propList);
1252 closeFrame();
1253 }
1254
1255 ///////////////////
1256 // frame
1257 ///////////////////
1258 bool MWAWTextListener::openFrame(MWAWPosition const &pos, MWAWGraphicStyle const &style)
1259 {
1260 if (m_ps->m_isTableOpened && !m_ps->m_isTableCellOpened) {
1261 MWAW_DEBUG_MSG(("MWAWTextListener::openFrame: called in table but cell is not opened\n"));
1262 return false;
1263 }
1264 if (m_ps->m_isFrameOpened) {
1265 MWAW_DEBUG_MSG(("MWAWTextListener::openFrame: called but a frame is already opened\n"));
1266 return false;
1267 }
1268 MWAWPosition fPos(pos);
1269 switch (pos.m_anchorTo) {
1270 case MWAWPosition::Page:
1271 break;
1272 case MWAWPosition::Paragraph:
1273 if (m_ps->m_isParagraphOpened)
1274 _flushText();
1275 else
1276 _openParagraph();
1277 break;
1278 case MWAWPosition::Unknown:
1279 MWAW_DEBUG_MSG(("MWAWTextListener::openFrame: UNKNOWN position, insert as char position\n"));
1280 MWAW_FALLTHROUGH;
1281 case MWAWPosition::CharBaseLine:
1282 case MWAWPosition::Char:
1283 if (m_ps->m_isSpanOpened)
1284 _flushText();
1285 else
1286 _openSpan();
1287 break;
1288 case MWAWPosition::Frame:
1289 if (!m_ds->m_subDocuments.size()) {
1290 MWAW_DEBUG_MSG(("MWAWTextListener::openFrame: can not determine the frame\n"));
1291 return false;
1292 }
1293 if (m_ps->m_subDocumentType==libmwaw::DOC_HEADER_FOOTER) {
1294 MWAW_DEBUG_MSG(("MWAWTextListener::openFrame: called with Frame position in header footer, switch to paragraph\n"));
1295 if (m_ps->m_isParagraphOpened)
1296 _flushText();
1297 else
1298 _openParagraph();
1299 fPos.m_anchorTo=MWAWPosition::Paragraph;
1300 }
1301 break;
1302 case MWAWPosition::Cell:
1303 if (!m_ps->m_isTableCellOpened) {
1304 MWAW_DEBUG_MSG(("MWAWTextListener::openFrame: called with Cell position not in a table cell\n"));
1305 return false;
1306 }
1307 if (pos.m_anchorCellName.empty()) {
1308 MWAW_DEBUG_MSG(("MWAWTextListener::openFrame: can not find the cell name\n"));
1309 return false;
1310 }
1311 break;
1312 #if !defined(__clang__)
1313 default:
1314 MWAW_DEBUG_MSG(("MWAWTextListener::openFrame: can not determine the anchor\n"));
1315 return false;
1316 #endif
1317 }
1318
1319 librevenge::RVNGPropertyList propList;
1320 style.addFrameTo(propList);
1321 _handleFrameParameters(propList, fPos);
1322 m_documentInterface->openFrame(propList);
1323
1324 m_ps->m_isFrameOpened = true;
1325 return true;
1326 }
1327
1328 void MWAWTextListener::closeFrame()
1329 {
1330 if (!m_ps->m_isFrameOpened) {
1331 MWAW_DEBUG_MSG(("MWAWTextListener::closeFrame: called but no frame is already opened\n"));
1332 return;
1333 }
1334 m_documentInterface->closeFrame();
1335 m_ps->m_isFrameOpened = false;
1336 }
1337
1338 bool MWAWTextListener::openGroup(MWAWPosition const &pos)
1339 {
1340 if (!m_ds->m_isDocumentStarted) {
1341 MWAW_DEBUG_MSG(("MWAWTextListener::openGroup: the document is not started\n"));
1342 return false;
1343 }
1344 if (m_ps->m_isTableOpened) {
1345 MWAW_DEBUG_MSG(("MWAWTextListener::openGroup: called in table or in a text zone\n"));
1346 return false;
1347 }
1348
1349 // now check that the anchor is coherent with the actual state
1350 switch (pos.m_anchorTo) {
1351 case MWAWPosition::Page:
1352 break;
1353 case MWAWPosition::Paragraph:
1354 if (m_ps->m_isParagraphOpened)
1355 _flushText();
1356 else
1357 _openParagraph();
1358 break;
1359 case MWAWPosition::Unknown:
1360 #if !defined(__clang__)
1361 default:
1362 #endif
1363 MWAW_DEBUG_MSG(("MWAWTextListener::openGroup: UNKNOWN position, insert as char position\n"));
1364 MWAW_FALLTHROUGH;
1365 case MWAWPosition::CharBaseLine:
1366 case MWAWPosition::Char:
1367 if (m_ps->m_isSpanOpened)
1368 _flushText();
1369 else
1370 _openSpan();
1371 break;
1372 case MWAWPosition::Frame:
1373 case MWAWPosition::Cell:
1374 break;
1375 }
1376
1377 librevenge::RVNGPropertyList propList;
1378 _handleFrameParameters(propList, pos);
1379
1380 _pushParsingState();
1381 _startSubDocument();
1382 m_ps->m_isGroupOpened = true;
1383
1384 m_documentInterface->openGroup(propList);
1385
1386 return true;
1387 }
1388
1389 void MWAWTextListener::closeGroup()
1390 {
1391 if (!m_ps->m_isGroupOpened) {
1392 MWAW_DEBUG_MSG(("MWAWTextListener::closeGroup: called but no group is already opened\n"));
1393 return;
1394 }
1395 _endSubDocument();
1396 _popParsingState();
1397 m_documentInterface->closeGroup();
1398 }
1399
1400 void MWAWTextListener::_handleFrameParameters
1401 (librevenge::RVNGPropertyList &propList, MWAWPosition const &pos)
1402 {
1403 MWAWVec2f origin = pos.origin();
1404 librevenge::RVNGUnit unit = pos.unit();
1405 float inchFactor=pos.getInvUnitScale(librevenge::RVNG_INCH);
1406 float pointFactor = pos.getInvUnitScale(librevenge::RVNG_POINT);
1407
1408 if (pos.size()[0]>0)
1409 propList.insert("svg:width", double(pos.size()[0]), unit);
1410 else if (pos.size()[0]<0)
1411 propList.insert("fo:min-width", double(-pos.size()[0]), unit);
1412 if (pos.size()[1]>0)
1413 propList.insert("svg:height", double(pos.size()[1]), unit);
1414 else if (pos.size()[1]<0)
1415 propList.insert("fo:min-height", double(-pos.size()[1]), unit);
1416 if (pos.order() > 0)
1417 propList.insert("draw:z-index", pos.order());
1418 if (pos.naturalSize().x() > 4*pointFactor && pos.naturalSize().y() > 4*pointFactor) {
1419 propList.insert("librevenge:naturalWidth", double(pos.naturalSize().x()), pos.unit());
1420 propList.insert("librevenge:naturalHeight", double(pos.naturalSize().y()), pos.unit());
1421 }
1422 MWAWVec2f TLClip = (1.f/pointFactor)*pos.leftTopClipping();
1423 MWAWVec2f RBClip = (1.f/pointFactor)*pos.rightBottomClipping();
1424 if (TLClip[0] > 0 || TLClip[1] > 0 || RBClip[0] > 0 || RBClip[1] > 0) {
1425 // in ODF1.2 we need to separate the value with ,
1426 std::stringstream s;
1427 s << "rect(" << TLClip[1] << "pt " << RBClip[0] << "pt "
1428 << RBClip[1] << "pt " << TLClip[0] << "pt)";
1429 propList.insert("fo:clip", s.str().c_str());
1430 }
1431
1432 if (pos.m_wrapping == MWAWPosition::WDynamic)
1433 propList.insert("style:wrap", "dynamic");
1434 else if (pos.m_wrapping == MWAWPosition::WBackground) {
1435 propList.insert("style:wrap", "run-through");
1436 propList.insert("style:run-through", "background");
1437 }
1438 else if (pos.m_wrapping == MWAWPosition::WForeground) {
1439 propList.insert("style:wrap", "run-through");
1440 propList.insert("style:run-through", "foreground");
1441 }
1442 else if (pos.m_wrapping == MWAWPosition::WParallel) {
1443 propList.insert("style:wrap", "parallel");
1444 propList.insert("style:run-through", "foreground");
1445 }
1446 else if (pos.m_wrapping == MWAWPosition::WRunThrough)
1447 propList.insert("style:wrap", "run-through");
1448 else
1449 propList.insert("style:wrap", "none");
1450
1451 if (pos.m_anchorTo == MWAWPosition::Paragraph ||
1452 pos.m_anchorTo == MWAWPosition::Frame) {
1453 std::string what= pos.m_anchorTo == MWAWPosition::Paragraph ?
1454 "paragraph" : "frame";
1455 propList.insert("text:anchor-type", what.c_str());
1456 propList.insert("style:vertical-rel", what.c_str());
1457 propList.insert("style:horizontal-rel", what.c_str());
1458 double w = m_ds->m_pageSpan.getPageWidth() - m_ps->m_paragraph.getMarginsWidth();
1459 w *= double(inchFactor);
1460 switch (pos.m_xPos) {
1461 case MWAWPosition::XRight:
1462 if (origin[0] < 0 || origin[0] > 0) {
1463 propList.insert("style:horizontal-pos", "from-left");
1464 propList.insert("svg:x", double(origin[0]) - double(pos.size()[0]) + w, unit);
1465 }
1466 else
1467 propList.insert("style:horizontal-pos", "right");
1468 break;
1469 case MWAWPosition::XCenter:
1470 if (origin[0] < 0 || origin[0] > 0) {
1471 propList.insert("style:horizontal-pos", "from-left");
1472 propList.insert("svg:x", double(origin[0]) - double(pos.size()[0])/2.0 + w/2.0, unit);
1473 }
1474 else
1475 propList.insert("style:horizontal-pos", "center");
1476 break;
1477 case MWAWPosition::XLeft:
1478 case MWAWPosition::XFull:
1479 #if !defined(__clang__)
1480 default:
1481 #endif
1482 if (origin[0] < 0 || origin[0] > 0) {
1483 propList.insert("style:horizontal-pos", "from-left");
1484 propList.insert("svg:x", double(origin[0]), unit);
1485 }
1486 else
1487 propList.insert("style:horizontal-pos", "left");
1488 break;
1489 }
1490
1491 if (origin[1] < 0 || origin[1] > 0) {
1492 propList.insert("style:vertical-pos", "from-top");
1493 propList.insert("svg:y", double(origin[1]), unit);
1494 }
1495 else
1496 propList.insert("style:vertical-pos", "top");
1497 return;
1498 }
1499
1500 if (pos.m_anchorTo == MWAWPosition::Page) {
1501 // Page position seems to do not use the page margin...
1502 propList.insert("text:anchor-type", "page");
1503 if (pos.page() > 0) propList.insert("text:anchor-page-number", pos.page());
1504 double w = m_ds->m_pageSpan.getFormWidth();
1505 double h = m_ds->m_pageSpan.getFormLength();
1506 w *= double(inchFactor);
1507 h *= double(inchFactor);
1508
1509 propList.insert("style:vertical-rel", "page");
1510 propList.insert("style:horizontal-rel", "page");
1511 double newPosition;
1512 switch (pos.m_yPos) {
1513 case MWAWPosition::YFull:
1514 propList.insert("svg:height", double(h), unit);
1515 MWAW_FALLTHROUGH;
1516 case MWAWPosition::YTop:
1517 if (origin[1] < 0 || origin[1] > 0) {
1518 propList.insert("style:vertical-pos", "from-top");
1519 newPosition = double(origin[1]);
1520 if (newPosition > h -double(pos.size()[1]))
1521 newPosition = h - double(pos.size()[1]);
1522 propList.insert("svg:y", double(newPosition), unit);
1523 }
1524 else
1525 propList.insert("style:vertical-pos", "top");
1526 break;
1527 case MWAWPosition::YCenter:
1528 if (origin[1] < 0 || origin[1] > 0) {
1529 propList.insert("style:vertical-pos", "from-top");
1530 newPosition = (h - double(pos.size()[1]))/2.0;
1531 if (newPosition > h -double(pos.size()[1])) newPosition = h - double(pos.size()[1]);
1532 propList.insert("svg:y", double(newPosition), unit);
1533 }
1534 else
1535 propList.insert("style:vertical-pos", "middle");
1536 break;
1537 case MWAWPosition::YBottom:
1538 if (origin[1] < 0 || origin[1] > 0) {
1539 propList.insert("style:vertical-pos", "from-top");
1540 newPosition = h - double(pos.size()[1])-double(origin[1]);
1541 if (newPosition > h -double(pos.size()[1])) newPosition = h -double(pos.size()[1]);
1542 else if (newPosition < 0) newPosition = 0;
1543 propList.insert("svg:y", double(newPosition), unit);
1544 }
1545 else
1546 propList.insert("style:vertical-pos", "bottom");
1547 break;
1548 #if !defined(__clang__)
1549 default:
1550 break;
1551 #endif
1552 }
1553
1554 switch (pos.m_xPos) {
1555 case MWAWPosition::XFull:
1556 propList.insert("svg:width", double(w), unit);
1557 MWAW_FALLTHROUGH;
1558 case MWAWPosition::XLeft:
1559 if (origin[0] < 0 || origin[0] > 0) {
1560 propList.insert("style:horizontal-pos", "from-left");
1561 propList.insert("svg:x", double(origin[0]), unit);
1562 }
1563 else
1564 propList.insert("style:horizontal-pos", "left");
1565 break;
1566 case MWAWPosition::XRight:
1567 if (origin[0] < 0 || origin[0] > 0) {
1568 propList.insert("style:horizontal-pos", "from-left");
1569 propList.insert("svg:x",w - double(pos.size()[0]) + double(origin[0]), unit);
1570 }
1571 else
1572 propList.insert("style:horizontal-pos", "right");
1573 break;
1574 case MWAWPosition::XCenter:
1575 if (origin[0] < 0 || origin[0] > 0) {
1576 propList.insert("style:horizontal-pos", "from-left");
1577 propList.insert("svg:x", (w - double(pos.size()[0]))/2. + double(origin[0]), unit);
1578 }
1579 else
1580 propList.insert("style:horizontal-pos", "center");
1581 break;
1582 #if !defined(__clang__)
1583 default:
1584 break;
1585 #endif
1586 }
1587 return;
1588 }
1589 if (pos.m_anchorTo == MWAWPosition::Cell) {
1590 if (!pos.m_anchorCellName.empty())
1591 propList.insert("table:end-cell-address", pos.m_anchorCellName);
1592 // todo: implement also different m_xPos and m_yPos
1593 if (origin[0] < 0 || origin[0] > 0)
1594 propList.insert("svg:x", double(origin[0]), unit);
1595 if (origin[1] < 0 || origin[1] > 0)
1596 propList.insert("svg:y", double(origin[1]), unit);
1597 return;
1598 }
1599 if (pos.m_anchorTo != MWAWPosition::Char &&
1600 pos.m_anchorTo != MWAWPosition::CharBaseLine &&
1601 pos.m_anchorTo != MWAWPosition::Unknown) return;
1602
1603 propList.insert("text:anchor-type", "as-char");
1604 if (pos.m_anchorTo == MWAWPosition::CharBaseLine)
1605 propList.insert("style:vertical-rel", "baseline");
1606 else
1607 propList.insert("style:vertical-rel", "line");
1608 switch (pos.m_yPos) {
1609 case MWAWPosition::YFull:
1610 case MWAWPosition::YTop:
1611 if (origin[1] < 0 || origin[1] > 0) {
1612 propList.insert("style:vertical-pos", "from-top");
1613 propList.insert("svg:y", double(origin[1]), unit);
1614 }
1615 else
1616 propList.insert("style:vertical-pos", "top");
1617 break;
1618 case MWAWPosition::YCenter:
1619 if (origin[1] < 0 || origin[1] > 0) {
1620 propList.insert("style:vertical-pos", "from-top");
1621 propList.insert("svg:y", double(origin[1] - pos.size()[1]/2.0f), unit);
1622 }
1623 else
1624 propList.insert("style:vertical-pos", "middle");
1625 break;
1626 case MWAWPosition::YBottom:
1627 #if !defined(__clang__)
1628 default:
1629 #endif
1630 if (origin[1] < 0 || origin[1] > 0) {
1631 propList.insert("style:vertical-pos", "from-top");
1632 propList.insert("svg:y", double(origin[1] - pos.size()[1]), unit);
1633 }
1634 else
1635 propList.insert("style:vertical-pos", "bottom");
1636 break;
1637 }
1638 }
1639
1640 ///////////////////
1641 // subdocument
1642 ///////////////////
1643 void MWAWTextListener::handleSubDocument(MWAWSubDocumentPtr const &subDocument, libmwaw::SubDocumentType subDocumentType)
1644 {
1645 _pushParsingState();
1646 _startSubDocument();
1647 m_ps->m_subDocumentType = subDocumentType;
1648
1649 m_ps->m_isPageSpanOpened = true;
1650 m_ps->m_list.reset();
1651
1652 switch (subDocumentType) {
1653 case libmwaw::DOC_TEXT_BOX:
1654 m_ds->m_pageSpan.setMargins(0.0);
1655 m_ps->m_sectionAttributesChanged = true;
1656 break;
1657 case libmwaw::DOC_HEADER_FOOTER:
1658 m_ps->m_isHeaderFooterWithoutParagraph = true;
1659 m_ds->m_isHeaderFooterStarted = true;
1660 break;
1661 case libmwaw::DOC_NONE:
1662 case libmwaw::DOC_CHART:
1663 case libmwaw::DOC_CHART_ZONE:
1664 case libmwaw::DOC_NOTE:
1665 case libmwaw::DOC_SHEET:
1666 case libmwaw::DOC_TABLE:
1667 case libmwaw::DOC_COMMENT_ANNOTATION:
1668 case libmwaw::DOC_GRAPHIC_GROUP:
1669 #if !defined(__clang__)
1670 default:
1671 #endif
1672 break;
1673 }
1674
1675 // Check whether the document is calling itself
1676 bool sendDoc = true;
1677 for (auto const &doc : m_ds->m_subDocuments) {
1678 if (!subDocument)
1679 break;
1680 if (!doc)
1681 continue;
1682 if (*subDocument == *doc) {
1683 MWAW_DEBUG_MSG(("MWAWTextListener::handleSubDocument: recursif call, stop...\n"));
1684 sendDoc = false;
1685 break;
1686 }
1687 }
1688 if (sendDoc) {
1689 if (subDocument) {
1690 m_ds->m_subDocuments.push_back(subDocument);
1691 std::shared_ptr<MWAWListener> listen(this, MWAW_shared_ptr_noop_deleter<MWAWTextListener>());
1692 try {
1693 subDocument->parse(listen, subDocumentType);
1694 }
1695 catch (...) {
1696 MWAW_DEBUG_MSG(("Works: MWAWTextListener::handleSubDocument exception catched \n"));
1697 }
1698 m_ds->m_subDocuments.pop_back();
1699 }
1700 if (m_ps->m_isHeaderFooterWithoutParagraph)
1701 _openSpan();
1702 }
1703
1704 switch (m_ps->m_subDocumentType) {
1705 case libmwaw::DOC_TEXT_BOX:
1706 _closeSection();
1707 break;
1708 case libmwaw::DOC_HEADER_FOOTER:
1709 m_ds->m_isHeaderFooterStarted = false;
1710 break;
1711 case libmwaw::DOC_NONE:
1712 case libmwaw::DOC_CHART:
1713 case libmwaw::DOC_CHART_ZONE:
1714 case libmwaw::DOC_NOTE:
1715 case libmwaw::DOC_SHEET:
1716 case libmwaw::DOC_TABLE:
1717 case libmwaw::DOC_COMMENT_ANNOTATION:
1718 case libmwaw::DOC_GRAPHIC_GROUP:
1719 #if !defined(__clang__)
1720 default:
1721 #endif
1722 break;
1723 }
1724 _endSubDocument();
1725 _popParsingState();
1726 }
1727
1728 bool MWAWTextListener::isSubDocumentOpened(libmwaw::SubDocumentType &subdocType) const
1729 {
1730 if (!m_ps->m_inSubDocument)
1731 return false;
1732 subdocType = m_ps->m_subDocumentType;
1733 return true;
1734 }
1735
1736 void MWAWTextListener::_startSubDocument()
1737 {
1738 m_ds->m_isDocumentStarted = true;
1739 m_ps->m_inSubDocument = true;
1740 }
1741
1742 void MWAWTextListener::_endSubDocument()
1743 {
1744 if (m_ps->m_isTableOpened)
1745 closeTable();
1746 if (m_ps->m_isParagraphOpened)
1747 _closeParagraph();
1748
1749 m_ps->m_paragraph.m_listLevelIndex=0;
1750 _changeList(); // flush the list exterior
1751 }
1752
1753 ///////////////////
1754 // table
1755 ///////////////////
1756 void MWAWTextListener::openTable(MWAWTable const &table)
1757 {
1758 if (m_ps->m_isTableOpened) {
1759 MWAW_DEBUG_MSG(("MWAWTextListener::openTable: called with m_isTableOpened=true\n"));
1760 return;
1761 }
1762
1763 if (m_ps->m_isParagraphOpened)
1764 _closeParagraph();
1765
1766 // default value: which can be redefined by table
1767 librevenge::RVNGPropertyList propList;
1768 propList.insert("table:align", "left");
1769 propList.insert("fo:margin-left", *m_ps->m_paragraph.m_margins[1], *m_ps->m_paragraph.m_marginsUnit);
1770 _pushParsingState();
1771 _startSubDocument();
1772 m_ps->m_subDocumentType = libmwaw::DOC_TABLE;
1773
1774 table.addTablePropertiesTo(propList);
1775 m_documentInterface->openTable(propList);
1776 m_ps->m_isTableOpened = true;
1777 }
1778
1779 void MWAWTextListener::closeTable()
1780 {
1781 if (!m_ps->m_isTableOpened) {
1782 MWAW_DEBUG_MSG(("MWAWTextListener::closeTable: called with m_isTableOpened=false\n"));
1783 return;
1784 }
1785
1786 m_ps->m_isTableOpened = false;
1787 _endSubDocument();
1788 m_documentInterface->closeTable();
1789
1790 _popParsingState();
1791 }
1792
1793 void MWAWTextListener::openTableRow(float h, librevenge::RVNGUnit unit, bool headerRow)
1794 {
1795 if (m_ps->m_isTableRowOpened) {
1796 MWAW_DEBUG_MSG(("MWAWTextListener::openTableRow: called with m_isTableRowOpened=true\n"));
1797 return;
1798 }
1799 if (!m_ps->m_isTableOpened) {
1800 MWAW_DEBUG_MSG(("MWAWTextListener::openTableRow: called with m_isTableOpened=false\n"));
1801 return;
1802 }
1803 librevenge::RVNGPropertyList propList;
1804 propList.insert("librevenge:is-header-row", headerRow);
1805
1806 if (h > 0)
1807 propList.insert("style:row-height", double(h), unit);
1808 else if (h < 0)
1809 propList.insert("style:min-row-height", double(-h), unit);
1810 m_documentInterface->openTableRow(propList);
1811 m_ps->m_isTableRowOpened = true;
1812 }
1813
1814 void MWAWTextListener::closeTableRow()
1815 {
1816 if (!m_ps->m_isTableRowOpened) {
1817 MWAW_DEBUG_MSG(("MWAWTextListener::openTableRow: called with m_isTableRowOpened=false\n"));
1818 return;
1819 }
1820 m_ps->m_isTableRowOpened = false;
1821 m_documentInterface->closeTableRow();
1822 }
1823
1824 void MWAWTextListener::addEmptyTableCell(MWAWVec2i const &pos, MWAWVec2i span)
1825 {
1826 if (!m_ps->m_isTableRowOpened) {
1827 MWAW_DEBUG_MSG(("MWAWTextListener::addEmptyTableCell: called with m_isTableRowOpened=false\n"));
1828 return;
1829 }
1830 if (m_ps->m_isTableCellOpened) {
1831 MWAW_DEBUG_MSG(("MWAWTextListener::addEmptyTableCell: called with m_isTableCellOpened=true\n"));
1832 closeTableCell();
1833 }
1834 librevenge::RVNGPropertyList propList;
1835 propList.insert("librevenge:column", pos[0]);
1836 propList.insert("librevenge:row", pos[1]);
1837 propList.insert("table:number-columns-spanned", span[0]);
1838 propList.insert("table:number-rows-spanned", span[1]);
1839 m_documentInterface->openTableCell(propList);
1840 m_documentInterface->closeTableCell();
1841 }
1842
1843 void MWAWTextListener::openTableCell(MWAWCell const &cell)
1844 {
1845 if (!m_ps->m_isTableRowOpened) {
1846 MWAW_DEBUG_MSG(("MWAWTextListener::openTableCell: called with m_isTableRowOpened=false\n"));
1847 return;
1848 }
1849 if (m_ps->m_isTableCellOpened) {
1850 MWAW_DEBUG_MSG(("MWAWTextListener::openTableCell: called with m_isTableCellOpened=true\n"));
1851 closeTableCell();
1852 }
1853
1854 librevenge::RVNGPropertyList propList;
1855 cell.addTo(propList, m_parserState.m_fontConverter);
1856 m_ps->m_isTableCellOpened = true;
1857 m_documentInterface->openTableCell(propList);
1858 }
1859
1860 void MWAWTextListener::closeTableCell()
1861 {
1862 if (!m_ps->m_isTableCellOpened) {
1863 MWAW_DEBUG_MSG(("MWAWTextListener::closeTableCell: called with m_isTableCellOpened=false\n"));
1864 return;
1865 }
1866
1867 _closeParagraph();
1868 m_ps->m_paragraph.m_listLevelIndex=0;
1869 _changeList(); // flush the list exterior
1870
1871 m_ps->m_isTableCellOpened = false;
1872 m_documentInterface->closeTableCell();
1873 }
1874
1875 ///////////////////
1876 // others
1877 ///////////////////
1878
1879 // ---------- state stack ------------------
1880 std::shared_ptr<MWAWTextListenerInternal::State> MWAWTextListener::_pushParsingState()
1881 {
1882 auto actual = m_ps;
1883 m_psStack.push_back(actual);
1884 m_ps.reset(new MWAWTextListenerInternal::State);
1885
1886 m_ps->m_isNote = actual->m_isNote;
1887
1888 return actual;
1889 }
1890
1891 void MWAWTextListener::_popParsingState()
1892 {
1893 if (m_psStack.size()==0) {
1894 MWAW_DEBUG_MSG(("MWAWTextListener::_popParsingState: psStack is empty()\n"));
1895 throw libmwaw::ParseException();
1896 }
1897 m_ps = m_psStack.back();
1898 m_psStack.pop_back();
1899 }
1900 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: