Debian Codegraph @

src/lib/ClarisWksPresentation.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <algorithm>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <map>
39 #include <sstream>
40
41 #include <librevenge/librevenge.h>
42
43 #include "MWAWFont.hxx"
44 #include "MWAWFontConverter.hxx"
45 #include "MWAWHeader.hxx"
46 #include "MWAWParser.hxx"
47 #include "MWAWPresentationListener.hxx"
48 #include "MWAWPosition.hxx"
49
50 #include "ClarisWksDocument.hxx"
51 #include "ClarisWksStruct.hxx"
52 #include "ClarisWksStyleManager.hxx"
53
54 #include "ClarisWksPresentation.hxx"
55
56 /** Internal: the structures of a ClarisWksPresentation */
57 namespace ClarisWksPresentationInternal
58 {
59 //! Internal the presentation
60 struct Presentation final : public ClarisWksStruct::DSET {
61 // constructor
62 explicit Presentation(ClarisWksStruct::DSET const &dset = ClarisWksStruct::DSET())
63 : ClarisWksStruct::DSET(dset)
64 , m_contentIdList()
65 , m_noteIdList()
66 , m_thumbnailsIdList()
67 , m_titleList()
68 , m_masterId(0)
69 , m_masterDetached(false)
70 {
71 }
72 //! destructor
73 ~Presentation() final;
74 //! operator<<
75 friend std::ostream &operator<<(std::ostream &o, Presentation const &doc)
76 {
77 o << static_cast<ClarisWksStruct::DSET const &>(doc);
78 return o;
79 }
80 /** remove a child from a list.
81
82 Normally, this function is not called, so optimizing it is not usefull
83 */
84 void removeChild(int cId, bool normalChild) final
85 {
86 DSET::removeChild(cId, normalChild);
87 if (m_id+1==cId) m_masterDetached=true;
88 }
89
90 //! the list of main zone id
91 std::vector<int> m_contentIdList;
92 //! the list of notes zone id
93 std::vector<int> m_noteIdList;
94 //! the list of thumbnail zone id
95 std::vector<int> m_thumbnailsIdList;
96 //! the list of title
97 std::vector<librevenge::RVNGString> m_titleList;
98 //! the master zone (background + header/footer)
99 int m_masterId;
100 //! true if the auxiliary zone is detached
101 bool m_masterDetached;
102 };
103
104 Presentation::~Presentation()
105 {
106 }
107
108 //! Internal: the state of a ClarisWksPresentation
109 struct State {
110 //! constructor
111 State()
112 : m_presentation()
113 , m_presentationMap()
114 {
115 }
116 //! the main presentation
117 std::shared_ptr<Presentation> m_presentation;
118 //! map zId to presentation
119 std::map<int, std::shared_ptr<Presentation> > m_presentationMap;
120 };
121
122 ////////////////////////////////////////
123 //! Internal: the subdocument of a ClarisWksPresentation
124 class SubDocument final : public MWAWSubDocument
125 {
126 public:
127 SubDocument(ClarisWksPresentation &pars, MWAWInputStreamPtr const &input, int zoneId)
128 : MWAWSubDocument(pars.m_mainParser, input, MWAWEntry())
129 , m_presentationParser(&pars)
130 , m_id(zoneId) {}
131
132 //! destructor
133 ~SubDocument() final {}
134
135 //! operator!=
136 bool operator!=(MWAWSubDocument const &doc) const final
137 {
138 if (MWAWSubDocument::operator!=(doc)) return true;
139 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
140 if (!sDoc) return true;
141 if (m_presentationParser != sDoc->m_presentationParser) return true;
142 if (m_id != sDoc->m_id) return true;
143 return false;
144 }
145
146 //! the parser function
147 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
148 /** the presentation parser */
149 ClarisWksPresentation *m_presentationParser;
150
151 protected:
152 //! the subdocument id
153 int m_id;
154 private:
155 SubDocument(SubDocument const &orig) = delete;
156 SubDocument &operator=(SubDocument const &orig) = delete;
157 };
158
159 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type)
160 {
161 if (!listener || (type==libmwaw::DOC_TEXT_BOX&&!listener->canWriteText())) {
162 MWAW_DEBUG_MSG(("ClarisWksPresentationInternal::SubDocument::parse: no listener\n"));
163 return;
164 }
165 if (!m_presentationParser) {
166 MWAW_DEBUG_MSG(("ClarisWksPresentationInternal::SubDocument::parse: can not find parser\n"));
167 return;
168 }
169 long pos = m_input->tell();
170 m_presentationParser->askToSend(m_id);
171 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
172 }
173 }
174
175 ////////////////////////////////////////////////////////////
176 // constructor/destructor, ...
177 ////////////////////////////////////////////////////////////
178 ClarisWksPresentation::ClarisWksPresentation(ClarisWksDocument &document)
179 : m_document(document)
180 , m_parserState(document.m_parserState)
181 , m_state(new ClarisWksPresentationInternal::State)
182 , m_mainParser(&document.getMainParser())
183 {
184 }
185
186 ClarisWksPresentation::~ClarisWksPresentation()
187 { }
188
189 int ClarisWksPresentation::version() const
190 {
191 return m_parserState->m_version;
192 }
193
194 int ClarisWksPresentation::numPages() const
195 {
196 if (m_parserState->m_kind!=MWAWDocument::MWAW_K_PRESENTATION || !m_state->m_presentation)
197 return 1;
198 return int(m_state->m_presentation->m_contentIdList.size());
199 }
200
201 bool ClarisWksPresentation::updatePageSpanList(MWAWPageSpan const &page, std::vector<MWAWPageSpan> &spanList)
202 {
203 if (!m_state->m_presentation)
204 return false;
205 int numPage=numPages();
206 if (numPage<=0)
207 return false;
208 for (int i=0; i<numPage; ++i) {
209 MWAWPageSpan ps(page);
210 if (i<static_cast<int>(m_state->m_presentation->m_titleList.size()) &&
211 !m_state->m_presentation->m_titleList[size_t(i)].empty())
212 ps.setPageName(m_state->m_presentation->m_titleList[size_t(i)]);
213 ps.setPageSpan(1);
214 spanList.push_back(ps);
215 }
216 return true;
217 }
218
219 void ClarisWksPresentation::askToSend(int number)
220 {
221 m_document.sendZone(number);
222 }
223
224 ////////////////////////////////////////////////////////////
225 // Intermediate level
226 ////////////////////////////////////////////////////////////
227 void ClarisWksPresentation::updateSlideTypes() const
228 {
229 auto it = m_state->m_presentationMap.begin();
230 while (it != m_state->m_presentationMap.end()) {
231 std::shared_ptr<ClarisWksPresentationInternal::Presentation> pres = it++->second;
232 if (!pres) continue;
233 if (pres->m_id==1)
234 m_state->m_presentation=pres;
235 for (int step=0; step<3; ++step) {
236 auto const &content=step==0 ? pres->m_contentIdList :
237 step==1 ? pres->m_noteIdList : pres->m_thumbnailsIdList;
238 int c=0;
239 for (auto id : content) {
240 auto zone=m_document.getZone(id);
241 if (!zone) continue;
242 static ClarisWksStruct::DSET::Position const positions[]= {
243 ClarisWksStruct::DSET::P_Slide, ClarisWksStruct::DSET::P_SlideNote, ClarisWksStruct::DSET::P_SlideThumbnail
244 };
245 zone->m_position=positions[step];
246 zone->m_page=++c;
247 }
248 }
249 // finally update the background type
250 auto zone=m_document.getZone(pres->m_id+1);
251 if (!zone || zone->m_fileType!=0) continue;
252 zone->m_position=ClarisWksStruct::DSET::P_SlideMaster;
253 }
254 }
255
256 void ClarisWksPresentation::disconnectMasterFromContents() const
257 {
258 auto it = m_state->m_presentationMap.begin();
259 while (it != m_state->m_presentationMap.end()) {
260 auto pres = it++->second;
261 if (!pres) continue;
262 auto background=m_document.getZone(pres->m_id+1);
263 pres->m_masterId=pres->m_id+1;
264 if (!background || background->m_fathersList.size()!=1) {
265 MWAW_DEBUG_MSG(("ClarisWksPresentation::disconnectMasterFromContents: can not find the background zone\n"));
266 continue;
267 }
268 int masterId=*background->m_fathersList.begin();
269 auto master=m_document.getZone(masterId);
270 if (!master) {
271 MWAW_DEBUG_MSG(("ClarisWksPresentation::disconnectMasterFromContents: can not find the master zone\n"));
272 continue;
273 }
274 master->m_position=ClarisWksStruct::DSET::P_SlideMaster;
275 pres->m_masterId=masterId;
276 for (int step=0; step<3; ++step) {
277 std::vector<int> const &content=step==0 ? pres->m_contentIdList :
278 step==1 ? pres->m_noteIdList : pres->m_thumbnailsIdList;
279 for (auto childId : content) {
280 if (master->m_fathersList.find(childId)==master->m_fathersList.end()) {
281 MWAW_DEBUG_MSG(("ClarisWksPresentation::disconnectMasterFromContents: find a content zone with no link to master\n"));
282 continue;
283 }
284 std::shared_ptr<ClarisWksStruct::DSET> zone=m_document.getZone(childId);
285 if (!zone) continue;
286 zone->removeChild(masterId, true);
287 master->m_fathersList.erase(childId);
288 }
289 }
290 }
291 }
292
293 ////////////////////////////////////////////////////////////
294 // a document part
295 ////////////////////////////////////////////////////////////
296 std::shared_ptr<ClarisWksStruct::DSET> ClarisWksPresentation::readPresentationZone
297 (ClarisWksStruct::DSET const &zone, MWAWEntry const &entry, bool &complete)
298 {
299 complete = true;
300 if (!entry.valid() || zone.m_fileType != 5 || entry.length() < 0x40)
301 return std::shared_ptr<ClarisWksStruct::DSET>();
302 long pos = entry.begin();
303 MWAWInputStreamPtr &input= m_parserState->m_input;
304 input->seek(pos+8+16, librevenge::RVNG_SEEK_SET); // avoid header+8 generic number
305 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
306 libmwaw::DebugStream f;
307 std::shared_ptr<ClarisWksPresentationInternal::Presentation>
308 presentationZone(new ClarisWksPresentationInternal::Presentation(zone));
309
310 f << "Entries(PresentationDef):" << *presentationZone << ",";
311 ascFile.addDelimiter(input->tell(), '|');
312 ascFile.addPos(pos);
313 ascFile.addNote(f.str().c_str());
314
315 // read the last part
316 long data0Length = zone.m_dataSz;
317 long N = zone.m_numData;
318 if (entry.length() -8-12 != data0Length*N + zone.m_headerSz) {
319 if (data0Length == 0 && N) {
320 MWAW_DEBUG_MSG(("ClarisWksPresentation::readPresentationZone: can not find definition size\n"));
321 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
322 return std::shared_ptr<ClarisWksStruct::DSET>();
323 }
324
325 MWAW_DEBUG_MSG(("ClarisWksPresentation::readPresentationZone: unexpected size for zone definition, try to continue\n"));
326 }
327
328 if (m_state->m_presentationMap.find(presentationZone->m_id) != m_state->m_presentationMap.end()) {
329 MWAW_DEBUG_MSG(("ClarisWksPresentation::readPresentationZone: zone %d already exists!!!\n", presentationZone->m_id));
330 }
331 else
332 m_state->m_presentationMap[presentationZone->m_id] = presentationZone;
333
334 long dataEnd = entry.end()-N*data0Length;
335 input->seek(dataEnd, librevenge::RVNG_SEEK_SET);
336 for (long i = 0; i < N; i++) {
337 pos = input->tell();
338
339 f.str("");
340 f << "PresentationDef-" << i;
341 ascFile.addPos(pos);
342 ascFile.addNote(f.str().c_str());
343 input->seek(pos+data0Length, librevenge::RVNG_SEEK_SET);
344 }
345 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
346
347 pos = input->tell();
348 bool ok = readZone1(*presentationZone);
349 if (ok) {
350 pos = input->tell();
351 ok = readZone2(*presentationZone);
352 }
353 if (!ok)
354 input->seek(pos, librevenge::RVNG_SEEK_SET);
355
356 return presentationZone;
357 }
358
359 ////////////////////////////////////////////////////////////
360 //
361 // Intermediate level
362 //
363 ////////////////////////////////////////////////////////////
364 bool ClarisWksPresentation::readZone1(ClarisWksPresentationInternal::Presentation &pres)
365 {
366 long val;
367 MWAWInputStreamPtr &input= m_parserState->m_input;
368 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
369 libmwaw::DebugStream f;
370
371 for (int st = 0; st < 3; st++) {
372 long pos = input->tell();
373 auto N = long(input->readULong(4));
374 long endPos = pos+16*N+4;
375 if (N < 0 || !input->checkPosition(endPos)) {
376 input->seek(pos, librevenge::RVNG_SEEK_SET);
377 MWAW_DEBUG_MSG(("ClarisWksPresentation::readZone1: zone seems too short\n"));
378 return false;
379 }
380 f.str("");
381 f << "Entries(PresentationStr)[" << st << "]" << ":N=" << N << ",";
382 ascFile.addPos(pos);
383 ascFile.addNote(f.str().c_str());
384
385 for (long i = 0; i < N; i++) {
386 f.str("");
387 f << "PresentationStr" << st << "-" << i << ":";
388 pos = input->tell();
389 auto zoneId = static_cast<int>(input->readLong(4));
390 if (zoneId > 0) {
391 if (st == 1)
392 pres.m_contentIdList.push_back(zoneId);
393 else if (st == 2)
394 pres.m_noteIdList.push_back(zoneId);
395 else
396 pres.m_thumbnailsIdList.push_back(zoneId);
397 pres.m_otherChilds.push_back(zoneId);
398 }
399 else
400 f << "###";
401 f << "zId=" << zoneId << ",";
402 f << "f1=" << input->readLong(4) << ","; // always 8 ?
403 auto sSz = static_cast<int>(input->readLong(4));
404 long endStringPos=pos+12+sSz;
405 if (sSz < 0 || !input->checkPosition(endStringPos+4)) {
406 input->seek(pos, librevenge::RVNG_SEEK_SET);
407 MWAW_DEBUG_MSG(("ClarisWksPresentation::readZone1: can not read string %ld\n", i));
408 return false;
409 }
410 librevenge::RVNGString title("");
411 for (int s = 0; s < sSz; s++) {
412 auto ch=char(input->readULong(1));
413 if (!ch) continue;
414 f << ch;
415 // checkme: can we always use a defaut font
416 int unicode= m_parserState->m_fontConverter->unicode(3, static_cast<unsigned char>(ch));
417 if (unicode==-1)
418 title.append(ch);
419 else
420 libmwaw::appendUnicode(static_cast<uint32_t>(unicode), title);
421 }
422 if (st==1)
423 pres.m_titleList.push_back(title);
424 input->seek(endStringPos, librevenge::RVNG_SEEK_SET);
425 val = input->readLong(4); // always 0 ?
426 if (val)
427 f << "f2=" << val << ",";
428 ascFile.addPos(pos);
429 ascFile.addNote(f.str().c_str());
430 }
431 }
432
433 return true;
434 }
435
436 bool ClarisWksPresentation::readZone2(ClarisWksPresentationInternal::Presentation &/*pres*/)
437 {
438 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
439 libmwaw::DebugStream f;
440
441 MWAWInputStreamPtr &input= m_parserState->m_input;
442 long pos = input->tell();
443 long endPos = pos+16;
444 input->seek(endPos, librevenge::RVNG_SEEK_SET);
445 if (long(input->tell()) != endPos) {
446 input->seek(pos, librevenge::RVNG_SEEK_SET);
447 MWAW_DEBUG_MSG(("ClarisWksPresentation::readZone2: zone seems too short\n"));
448 return false;
449 }
450
451 input->seek(pos, librevenge::RVNG_SEEK_SET);
452 f << "Entries(PresentationTitle):";
453 // checkme this also be 1 times : [ 0, f2] or 1, str0, f2, or a mixt
454 for (int i = 0; i < 3; i++) { // find f0=1, f1=0, f2=[0|1|2|4]
455 long val = input->readLong(4);
456 if (val)
457 f << "f" << i << "=" << val << ",";
458 }
459 auto sSz = static_cast<int>(input->readLong(4));
460 input->seek(pos+16+sSz, librevenge::RVNG_SEEK_SET);
461 if (sSz < 0 || input->tell() != pos+16+sSz) {
462 input->seek(pos, librevenge::RVNG_SEEK_SET);
463 MWAW_DEBUG_MSG(("ClarisWksPresentation::readZone2: can not read title\n"));
464 return false;
465 }
466 input->seek(pos+16, librevenge::RVNG_SEEK_SET);
467 std::string title("");
468 for (int s = 0; s < sSz; s++)
469 title += char(input->readULong(1));
470 f << title << ",";
471
472 ascFile.addPos(pos);
473 ascFile.addNote(f.str().c_str());
474 return true;
475 }
476
477 ////////////////////////////////////////////////////////////
478 //
479 // Low level
480 //
481 ////////////////////////////////////////////////////////////
482
483 bool ClarisWksPresentation::sendMaster()
484 {
485 if (!m_state->m_presentation) {
486 MWAW_DEBUG_MSG(("ClarisWksPresentation::sendMaster: oops can not find main presentation\n"));
487 return false;
488 }
489 int masterId=m_state->m_presentation->m_masterId;
490 return m_document.sendZone(masterId > 0 ? masterId : 2);
491 }
492
493 bool ClarisWksPresentation::sendZone(int number)
494 {
495 if (number != 1) {
496 MWAW_DEBUG_MSG(("ClarisWksPresentation::sendZone: sending embeded presentation is not implemented\n"));
497 return false;
498 }
499 auto presentation = m_state->m_presentation;
500 MWAWPresentationListenerPtr listener=m_parserState->m_presentationListener;
501 if (!presentation || !listener) {
502 MWAW_DEBUG_MSG(("ClarisWksPresentation::sendZone: can not find the presentation listener\n"));
503 return false;
504 }
505 presentation->m_parsed = true;
506 if (!presentation->m_masterDetached)
507 m_document.forceParsed(number+1);
508 for (size_t p = 0; p < presentation->m_contentIdList.size(); p++) {
509 if (p)
510 listener->insertBreak(MWAWListener::PageBreak);
511
512 // first insert the content
513
514 int id = presentation->m_contentIdList[p];
515 if (id > 0)
516 m_document.sendZone(id);
517
518 // try to retrieve the notes: ie. the text zone in the notes zone
519 if (p>=presentation->m_noteIdList.size() || presentation->m_noteIdList[p] <=0)
520 continue;
521
522 auto zone=m_document.getZone(presentation->m_noteIdList[p]);
523 if (!zone) continue;
524 std::vector<int> listTextChild;
525 for (auto const &cZone : zone->m_childs) {
526 if (cZone.m_type != ClarisWksStruct::DSET::C_Zone)
527 continue;
528 int cId=cZone.m_id;
529 auto child=m_document.getZone(cId);
530 if (child && child->m_fileType==1)
531 listTextChild.push_back(cId);
532 }
533 if (listTextChild.size()!=1) {
534 MWAW_DEBUG_MSG(("ClarisWksPresentation::sendZone: the number of notes text zone is odd=%d\n",
535 static_cast<int>(listTextChild.size())));
536 }
537 if (listTextChild.empty())
538 continue;
539 std::shared_ptr<MWAWSubDocument> doc(new ClarisWksPresentationInternal::SubDocument(*this, m_parserState->m_input, listTextChild[0]));
540 // fixme
541 MWAWPosition pos(MWAWVec2f(0,400), MWAWVec2f(600,200), librevenge::RVNG_POINT);
542 pos.m_anchorTo=MWAWPosition::Page;
543 listener->insertSlideNote(pos, doc);
544 }
545 return true;
546 }
547
548 void ClarisWksPresentation::flushExtra()
549 {
550 std::shared_ptr<MWAWListener> listener=m_parserState->getMainListener();
551 if (!listener || listener->getType()!=MWAWListener::Presentation) return;
552
553 for (auto const &iter : m_state->m_presentationMap) {
554 auto presentation = iter.second;
555 if (presentation->m_parsed)
556 continue;
557 listener->insertEOL();
558 sendZone(iter.first);
559 }
560 }
561 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: