Debian Codegraph @

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