Debian Codegraph @

src/lib/EPUBHTMLGenerator.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libepubgen project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include "EPUBHTMLGenerator.h"
11
12 #include <algorithm>
13 #include <cmath>
14 #include <sstream>
15 #include <stack>
16 #include <string>
17 #include <vector>
18
19 #include <boost/algorithm/string/replace.hpp>
20
21 #include "EPUBFontManager.h"
22 #include "EPUBImageManager.h"
23 #include "EPUBListStyleManager.h"
24 #include "EPUBParagraphStyleManager.h"
25 #include "EPUBPath.h"
26 #include "EPUBSpanStyleManager.h"
27 #include "EPUBTableStyleManager.h"
28 #include "EPUBXMLContent.h"
29
30 #include "libepubgen_utils.h"
31
32 namespace libepubgen
33 {
34
35 using librevenge::RVNGBinaryData;
36 using librevenge::RVNGPropertyList;
37 using librevenge::RVNGString;
38
39 using std::string;
40
41 namespace
42 {
43
44 class ZoneSinkImpl
45 {
46 public:
47 ZoneSinkImpl();
48
49 void openElement(const char *name, const librevenge::RVNGPropertyList &attributes);
50 void closeElement(const char *name);
51
52 void insertCharacters(const librevenge::RVNGString &characters);
53
54 void append(const ZoneSinkImpl &other);
55
56 const EPUBXMLContent &get() const;
57 EPUBXMLContent &get();
58
59 bool endsInLineBreak() const;
60
61 private:
62 EPUBXMLContent m_content;
63 std::string m_lastCloseElement;
64 };
65
66 ZoneSinkImpl::ZoneSinkImpl()
67 : m_content()
68 , m_lastCloseElement()
69 {
70 }
71
72 void ZoneSinkImpl::openElement(const char *const name, const librevenge::RVNGPropertyList &attributes)
73 {
74 m_content.openElement(name, attributes);
75 m_lastCloseElement.clear();
76 }
77
78 void ZoneSinkImpl::closeElement(const char *const name)
79 {
80 m_content.closeElement(name);
81 m_lastCloseElement = name;
82 }
83
84 void ZoneSinkImpl::insertCharacters(const librevenge::RVNGString &characters)
85 {
86 m_content.insertCharacters(characters);
87 m_lastCloseElement.clear();
88 }
89
90 void ZoneSinkImpl::append(const ZoneSinkImpl &other)
91 {
92 m_content.append(other.m_content);
93 m_lastCloseElement = other.m_lastCloseElement;
94 }
95
96 const EPUBXMLContent &ZoneSinkImpl::get() const
97 {
98 return m_content;
99 }
100
101 EPUBXMLContent &ZoneSinkImpl::get()
102 {
103 return m_content;
104 }
105
106 bool ZoneSinkImpl::endsInLineBreak() const
107 {
108 return m_lastCloseElement == "p"
109 || m_lastCloseElement == "ul"
110 || m_lastCloseElement == "ol"
111 || m_lastCloseElement == "br"
112 ;
113 }
114
115 /// Convers inches to CSS pixels.
116 int inchToCSSPixel(const librevenge::RVNGProperty *property)
117 {
118 return round(property->getDouble() * 96);
119 }
120 }
121
122 namespace
123 {
124
125 struct TextZoneSink;
126
127 //! a zone to regroup footnote/endnote,... data
128 struct EPUBHTMLTextZone
129 {
130 friend struct TextZoneSink;
131
132 //! the different zone
133 enum Type { Z_Comment=0, Z_EndNote, Z_FootNote, Z_Main, Z_MetaData, Z_TextBox, Z_Unknown, Z_NumZones= Z_Unknown+1};
134 //! constructor for basic stream
135 EPUBHTMLTextZone(Type tp=Z_Unknown) : m_type(tp), m_actualId(0), m_zoneSinks(), m_version(20)
136 {
137 }
138 //! the type
139 Type type() const
140 {
141 return m_type;
142 }
143 //! the type
144 void setType(Type tp)
145 {
146 m_type=tp;
147 }
148 void setVersion(int version)
149 {
150 m_version = version;
151 }
152 int getVersion() const
153 {
154 return m_version;
155 }
156 //! returns a new sink corresponding to this zone
157 std::unique_ptr<TextZoneSink> getNewSink();
158 //! returns true if there is no data
159 bool isEmpty() const
160 {
161 for (const auto &zoneSink : m_zoneSinks)
162 if (!zoneSink.get().empty())
163 return false;
164 return true;
165 }
166 //! send the zone data
167 void send(EPUBXMLContent &out) const
168 {
169 if (isEmpty() || m_type==Z_Unknown || m_type==Z_Main)
170 return;
171 if (m_type!=Z_MetaData && m_version < 30)
172 {
173 out.openElement("hr", RVNGPropertyList());
174 out.closeElement("hr");
175 }
176 if (m_type==Z_MetaData)
177 {
178 for (const auto &zoneSink : m_zoneSinks)
179 out.append(zoneSink.get());
180 return;
181 }
182 if (m_type==Z_TextBox)
183 {
184 out.openElement("p", RVNGPropertyList());
185 out.openElement("b", RVNGPropertyList());
186 out.insertCharacters("TEXT BOXES");
187 out.closeElement("b");
188 out.closeElement("p");
189 for (const auto &zoneSink : m_zoneSinks)
190 {
191 out.append(zoneSink.get());
192 out.openElement("hr", RVNGPropertyList());
193 out.closeElement("hr");
194 }
195 return;
196 }
197 for (const auto &zoneSink : m_zoneSinks)
198 {
199 out.append(zoneSink.get());
200 // check if we need to add a return line
201 if (!zoneSink.endsInLineBreak())
202 {
203 out.openElement("br", RVNGPropertyList());
204 out.closeElement("br");
205 }
206 }
207 }
208
209 protected:
210 //! return a label corresponding to the zone
211 std::string label(int id) const;
212 //! the zone type
213 Type m_type;
214 //! the actual id
215 mutable int m_actualId;
216 //! the list of data string
217 std::vector<ZoneSinkImpl> m_zoneSinks;
218 int m_version;
219 private:
220 EPUBHTMLTextZone(EPUBHTMLTextZone const &orig);
221 EPUBHTMLTextZone operator=(EPUBHTMLTextZone const &orig);
222 };
223
224 struct TextZoneSink
225 {
226 //! constructor
227 TextZoneSink(EPUBHTMLTextZone *zone): m_zone(zone), m_zoneId(0), m_sink(), m_delayedLabel()
228 {
229 if (m_zone)
230 m_zoneId=m_zone->m_actualId++;
231 }
232 //! destructor
233 ~TextZoneSink() { }
234 //! add a label called on main and a label in this ( delayed to allow openParagraph to be called )
235 //! @param closeAnchor determintes if the anchor on the main sink should be closed or not.
236 void addLabel(EPUBXMLContent &output, const librevenge::RVNGString &number, bool closeAnchor)
237 {
238 // Unique label, e.g. 'F1' for the first footnote.
239 std::string lbl=label();
240 // User-visible label, e.g. '1'.
241 std::string uiLabel = lbl;
242 if (!number.empty())
243 uiLabel = number.cstr();
244 if (!lbl.length())
245 return;
246 int version = 30;
247 if (m_zone)
248 version = m_zone->getVersion();
249 {
250 RVNGPropertyList supAttrs;
251 supAttrs.insert("id", ("called" + lbl).c_str());
252 if (closeAnchor)
253 output.openElement("sup", supAttrs);
254 RVNGPropertyList aAttrs;
255 if (version >= 30)
256 aAttrs.insert("epub:type", "noteref");
257 aAttrs.insert("href", ("#data" + lbl).c_str());
258 output.openElement("a", aAttrs);
259 if (closeAnchor)
260 {
261 output.insertCharacters(uiLabel.c_str());
262 output.closeElement("a");
263 output.closeElement("sup");
264 }
265 }
266 flush();
267 if (version >= 30)
268 {
269 RVNGPropertyList asideAttrs;
270 asideAttrs.insert("epub:type", "footnote");
271 asideAttrs.insert("id", ("data" + lbl).c_str());
272 m_sink.openElement("aside", asideAttrs);
273 }
274 RVNGPropertyList supAttrs;
275 if (version < 30)
276 supAttrs.insert("id", ("data" + lbl).c_str());
277 if (closeAnchor)
278 {
279 m_delayedLabel.openElement("sup", supAttrs);
280 RVNGPropertyList aAttrs;
281 aAttrs.insert("href", ("#called" + lbl).c_str());
282 m_delayedLabel.openElement("a", aAttrs);
283 m_delayedLabel.insertCharacters(uiLabel.c_str());
284 m_delayedLabel.closeElement("a");
285 m_delayedLabel.closeElement("sup");
286 }
287 }
288 //! flush delayed label, ...
289 void flush()
290 {
291 m_sink.append(m_delayedLabel);
292 m_delayedLabel = ZoneSinkImpl();
293 }
294 //! return the sink
295 EPUBXMLContent &getContent()
296 {
297 return m_sink.get();
298 }
299 //! send the data to the zone
300 void send()
301 {
302 if (!m_zone || m_zone->m_type==EPUBHTMLTextZone::Z_Main)
303 {
304 EPUBGEN_DEBUG_MSG(("TextZoneSink::send: must not be called\n"));
305 return;
306 }
307 flush();
308 if (m_zone->m_zoneSinks.size() <= size_t(m_zoneId))
309 m_zone->m_zoneSinks.resize(size_t(m_zoneId)+1);
310 m_zone->m_zoneSinks[size_t(m_zoneId)] = m_sink;
311 }
312 //! send the data to the zone
313 void sendMain(EPUBXMLContent &output)
314 {
315 flush();
316 output.append(m_sink.get());
317 }
318 protected:
319 //! return the zone label
320 std::string label() const
321 {
322 if (!m_zone || m_zone->m_type==EPUBHTMLTextZone::Z_Main)
323 {
324 EPUBGEN_DEBUG_MSG(("TextZoneSink::label: must not be called\n"));
325 return "";
326 }
327 return m_zone->label(m_zoneId);
328 }
329 //! a zone
330 EPUBHTMLTextZone *m_zone;
331 //! the zone id
332 int m_zoneId;
333 //! the sink
334 ZoneSinkImpl m_sink;
335 //! the label
336 ZoneSinkImpl m_delayedLabel;
337 private:
338 TextZoneSink(TextZoneSink const &orig);
339 TextZoneSink operator=(TextZoneSink const &orig);
340 };
341
342 std::unique_ptr<TextZoneSink> EPUBHTMLTextZone::getNewSink()
343 {
344 return make_unique<TextZoneSink>(this);
345 }
346
347 std::string EPUBHTMLTextZone::label(int id) const
348 {
349 char c=0;
350 switch (m_type)
351 {
352 case Z_Comment:
353 c='C';
354 break;
355 case Z_EndNote:
356 c='E';
357 break;
358 case Z_FootNote:
359 c='F';
360 break;
361 case Z_TextBox:
362 c='T';
363 break;
364 case Z_Main:
365 case Z_MetaData:
366 case Z_Unknown:
367 case Z_NumZones:
368 default:
369 break;
370 }
371 if (c==0)
372 return "";
373 std::stringstream s;
374 s << c << id+1;
375 return s.str();
376 }
377
378 }
379
380 //! the internal state of a html document generator
381 struct EPUBHTMLGeneratorImpl
382 {
383 //! constructor
384 EPUBHTMLGeneratorImpl(EPUBXMLContent &document, EPUBImageManager &imageManager, EPUBFontManager &fontManager, EPUBListStyleManager &listStyleManager, EPUBParagraphStyleManager &paragraphStyleManager, EPUBSpanStyleManager &spanStyleManager, EPUBSpanStyleManager &bodyStyleManager, EPUBTableStyleManager &tableStyleManager, const EPUBPath &path, const EPUBPath &stylesheetPath, EPUBStylesMethod stylesMethod, EPUBLayoutMethod layoutMethod, int version)
385 : m_document(document)
386 , m_imageManager(imageManager)
387 , m_fontManager(fontManager)
388 , m_listManager(listStyleManager)
389 , m_paragraphManager(paragraphStyleManager)
390 , m_spanManager(spanStyleManager)
391 , m_bodyManager(bodyStyleManager)
392 , m_tableManager(tableStyleManager)
393 , m_path(path)
394 , m_stylesheetPath(stylesheetPath)
395 , m_actualPage(0)
396 , m_actualPageProperties()
397 , m_ignore(false)
398 , m_hasText(false)
399 , m_version(version)
400 , m_frameAnchorTypes()
401 , m_framePropertiesStack()
402 , m_linkPropertiesStack()
403 , m_paragraphAttributesStack()
404 , m_spanAttributesStack()
405 , m_rubyText()
406 , m_stylesMethod(stylesMethod)
407 , m_layoutMethod(layoutMethod)
408 , m_actualSink()
409 , m_sinkStack()
410 {
411 for (int i = 0; i < EPUBHTMLTextZone::Z_NumZones; ++i)
412 {
413 m_zones[i].setType(EPUBHTMLTextZone::Type(i));
414 m_zones[i].setVersion(version);
415 }
416 m_actualSink=m_zones[EPUBHTMLTextZone::Z_Main].getNewSink();
417 }
418 //! destructor
419 ~EPUBHTMLGeneratorImpl()
420 {
421 }
422
423 //! returns the actual output ( sending delayed data if needed)
424 EPUBXMLContent &output(bool sendDelayed=true)
425 {
426 if (sendDelayed)
427 m_actualSink->flush();
428 return m_actualSink->getContent();
429 }
430 //! returns the actual sink
431 TextZoneSink &getSink()
432 {
433 return *m_actualSink;
434 }
435 void push(EPUBHTMLTextZone::Type type)
436 {
437 m_sinkStack.push(std::move(m_actualSink));
438 if (type==EPUBHTMLTextZone::Z_Main || type==EPUBHTMLTextZone::Z_NumZones)
439 {
440 EPUBGEN_DEBUG_MSG(("EPUBHTMLGeneratorImpl::push: can not push the main zone\n"));
441 type=EPUBHTMLTextZone::Z_Unknown;
442 }
443 m_actualSink=m_zones[type].getNewSink();
444 }
445 void pop()
446 {
447 if (m_sinkStack.empty())
448 {
449 EPUBGEN_DEBUG_MSG(("EPUBHTMLGenerator::pop: can not pop sink\n"));
450 return;
451 }
452 if (m_actualSink)
453 {
454 m_actualSink->send();
455 m_actualSink.reset();
456 }
457 m_actualSink = std::move(m_sinkStack.top());
458 m_sinkStack.pop();
459 }
460 void sendMetaData(EPUBXMLContent &out)
461 {
462 m_zones[EPUBHTMLTextZone::Z_MetaData].send(out);
463 }
464 void flushUnsent(EPUBXMLContent &out)
465 {
466 if (!m_sinkStack.empty())
467 {
468 EPUBGEN_DEBUG_MSG(("EPUBHTMLGenerator::flushUnsent: the sink stack is not empty\n"));
469 while (!m_sinkStack.empty())
470 pop();
471 }
472 // first send the main data
473 if (!m_actualSink)
474 {
475 EPUBGEN_DEBUG_MSG(("EPUBHTMLGenerator::flushUnsent: can not find the main block\n"));
476 }
477 else
478 m_actualSink->sendMain(out);
479 m_zones[EPUBHTMLTextZone::Z_Comment].send(out);
480 m_zones[EPUBHTMLTextZone::Z_FootNote].send(out);
481 m_zones[EPUBHTMLTextZone::Z_EndNote].send(out);
482 m_zones[EPUBHTMLTextZone::Z_TextBox].send(out);
483 }
484
485 EPUBXMLContent &m_document;
486 EPUBImageManager &m_imageManager;
487 EPUBFontManager &m_fontManager;
488 EPUBListStyleManager &m_listManager;
489 EPUBParagraphStyleManager &m_paragraphManager;
490 EPUBSpanStyleManager &m_spanManager;
491 EPUBSpanStyleManager &m_bodyManager;
492 EPUBTableStyleManager &m_tableManager;
493 const EPUBPath m_path;
494 const EPUBPath m_stylesheetPath;
495
496 int m_actualPage;
497 RVNGPropertyList m_actualPageProperties;
498 bool m_ignore;
499 /// Does the currently opened paragraph have some text?
500 bool m_hasText;
501 int m_version;
502
503 std::stack<std::string> m_frameAnchorTypes;
504 std::stack<RVNGPropertyList> m_framePropertiesStack;
505 /// This is used for links which don't have a href.
506 std::stack<RVNGPropertyList> m_linkPropertiesStack;
507 std::stack<RVNGPropertyList> m_paragraphAttributesStack;
508 std::stack<RVNGPropertyList> m_spanAttributesStack;
509
510 /// This is set when the span has ruby text and should be wrapped in <ruby></ruby>.
511 std::string m_rubyText;
512
513 EPUBStylesMethod m_stylesMethod;
514 EPUBLayoutMethod m_layoutMethod;
515
516 protected:
517 std::unique_ptr<TextZoneSink> m_actualSink;
518 std::stack<std::unique_ptr<TextZoneSink>> m_sinkStack;
519
520 EPUBHTMLTextZone m_zones[EPUBHTMLTextZone::Z_NumZones];
521 private:
522 EPUBHTMLGeneratorImpl(EPUBHTMLGeneratorImpl const &orig);
523 EPUBHTMLGeneratorImpl operator=(EPUBHTMLGeneratorImpl const &orig);
524 };
525
526 EPUBHTMLGenerator::EPUBHTMLGenerator(EPUBXMLContent &document, EPUBImageManager &imageManager, EPUBFontManager &fontManager, EPUBListStyleManager &listStyleManager, EPUBParagraphStyleManager &paragraphStyleManager, EPUBSpanStyleManager &spanStyleManager, EPUBSpanStyleManager &bodyStyleManager, EPUBTableStyleManager &tableStyleManager, const EPUBPath &path, const EPUBPath &stylesheetPath, EPUBStylesMethod stylesMethod, EPUBLayoutMethod layoutMethod, int version)
527 : m_impl(new EPUBHTMLGeneratorImpl(document, imageManager, fontManager, listStyleManager, paragraphStyleManager, spanStyleManager, bodyStyleManager, tableStyleManager, path, stylesheetPath, stylesMethod, layoutMethod, version))
528 {
529 }
530
531 EPUBHTMLGenerator::~EPUBHTMLGenerator()
532 {
533 }
534
535 void EPUBHTMLGenerator::setDocumentMetaData(const RVNGPropertyList &propList)
536 {
537 m_impl->push(EPUBHTMLTextZone::Z_MetaData);
538 EPUBXMLContent &meta=m_impl->output();
539 static char const *wpdMetaFields[9]=
540 {
541 "meta:initial-creator", "dc:creator", "dc:subject", "dc:publisher", "meta:keywords",
542 "dc:language", "dc:description", "librevenge:descriptive-name", "librevenge:descriptive-type"
543 };
544 static char const *metaFields[9]=
545 {
546 "author", "typist", "subject", "publisher", "keywords",
547 "language", "abstract", "descriptive-name", "descriptive-type"
548 };
549 for (int i = 0; i < 9; i++)
550 {
551 if (!propList[wpdMetaFields[i]])
552 continue;
553 RVNGPropertyList attrs;
554 attrs.insert("name", metaFields[i]);
555 attrs.insert("content", propList[wpdMetaFields[i]]->getStr());
556 meta.openElement("meta", attrs);
557 meta.closeElement("meta");
558 }
559 meta.openElement("title", RVNGPropertyList());
560 if (propList["librevenge:descriptive-name"])
561 meta.insertCharacters(propList["librevenge:descriptive-name"]->getStr());
562 meta.closeElement("title");
563 m_impl->pop();
564 }
565
566 void EPUBHTMLGenerator::startDocument(const RVNGPropertyList &)
567 {
568 }
569
570 void EPUBHTMLGenerator::endDocument()
571 {
572 RVNGPropertyList htmlAttrs;
573 // TODO: set lang and xml:lang from metadata
574 htmlAttrs.insert("xmlns", "http://www.w3.org/1999/xhtml");
575 m_impl->m_document.openElement("html", htmlAttrs);
576 m_impl->m_document.openElement("head", RVNGPropertyList());
577 if (m_impl->m_version < 30)
578 {
579 m_impl->m_document.openElement("title", RVNGPropertyList());
580 m_impl->m_document.closeElement("title");
581 }
582 RVNGPropertyList metaAttrs;
583 metaAttrs.insert("http-equiv", "content-type");
584 metaAttrs.insert("content", "text/html; charset=UTF-8");
585 m_impl->m_document.openElement("meta", metaAttrs);
586 m_impl->m_document.closeElement("meta");
587 if (m_impl->m_version >= 30 && m_impl->m_layoutMethod == EPUB_LAYOUT_METHOD_FIXED)
588 {
589 metaAttrs.clear();
590 metaAttrs.insert("name", "viewport");
591 std::stringstream content;
592 if (const librevenge::RVNGProperty *pageWidth = m_impl->m_actualPageProperties["fo:page-width"])
593 {
594 content << "width=";
595 content << inchToCSSPixel(pageWidth);
596 }
597 if (const librevenge::RVNGProperty *pageHeight = m_impl->m_actualPageProperties["fo:page-height"])
598 {
599 content << ", height=";
600 content << inchToCSSPixel(pageHeight);
601 }
602 metaAttrs.insert("content", content.str().c_str());
603 m_impl->m_document.openElement("meta", metaAttrs);
604 m_impl->m_document.closeElement("meta");
605 }
606 if (m_impl->m_version < 30)
607 m_impl->sendMetaData(m_impl->m_document);
608 RVNGPropertyList linkAttrs;
609 linkAttrs.insert("href", m_impl->m_stylesheetPath.relativeTo(m_impl->m_path).str().c_str());
610 linkAttrs.insert("type", "text/css");
611 linkAttrs.insert("rel", "stylesheet");
612 m_impl->m_document.insertEmptyElement("link", linkAttrs);
613 m_impl->m_document.closeElement("head");
614 RVNGPropertyList bodyAttrs;
615 if (m_impl->m_version >= 30)
616 bodyAttrs.insert("xmlns:epub", "http://www.idpf.org/2007/ops");
617
618 if (m_impl->m_actualPageProperties["style:writing-mode"])
619 {
620 switch (m_impl->m_stylesMethod)
621 {
622 case EPUB_STYLES_METHOD_CSS:
623 bodyAttrs.insert("class", m_impl->m_bodyManager.getClass(m_impl->m_actualPageProperties).c_str());
624 break;
625 case EPUB_STYLES_METHOD_INLINE:
626 bodyAttrs.insert("style", m_impl->m_bodyManager.getStyle(m_impl->m_actualPageProperties).c_str());
627 break;
628 }
629 }
630
631 m_impl->m_document.openElement("body", bodyAttrs);
632 m_impl->flushUnsent(m_impl->m_document);
633 m_impl->m_document.closeElement("body");
634 m_impl->m_document.closeElement("html");
635 }
636
637 void EPUBHTMLGenerator::defineEmbeddedFont(const RVNGPropertyList &propList)
638 {
639 m_impl->m_fontManager.insert(propList, m_impl->m_path);
640 }
641
642 void EPUBHTMLGenerator::openPageSpan(const RVNGPropertyList &propList)
643 {
644 m_impl->m_actualPage++;
645
646 librevenge::RVNGPropertyList::Iter i(propList);
647 for (i.rewind(); i.next();)
648 m_impl->m_actualPageProperties.insert(i.key(), i()->clone());
649 }
650
651 void EPUBHTMLGenerator::closePageSpan()
652 {
653 }
654
655 void EPUBHTMLGenerator::definePageStyle(const RVNGPropertyList &) {}
656
657 void EPUBHTMLGenerator::openHeader(const RVNGPropertyList & /* propList */)
658 {
659 m_impl->m_ignore = true;
660 }
661
662 void EPUBHTMLGenerator::closeHeader()
663 {
664 m_impl->m_ignore = false;
665 }
666
667
668 void EPUBHTMLGenerator::openFooter(const RVNGPropertyList & /* propList */)
669 {
670 m_impl->m_ignore = true;
671 }
672
673 void EPUBHTMLGenerator::closeFooter()
674 {
675 m_impl->m_ignore = false;
676 }
677
678 void EPUBHTMLGenerator::defineSectionStyle(const RVNGPropertyList &) {}
679
680 void EPUBHTMLGenerator::openSection(const RVNGPropertyList & /* propList */)
681 {
682 // Once output is produced here, EPUBTextGenerator::openSection() will need
683 // to call EPUBSplitGuard::openLevel().
684 }
685
686 void EPUBHTMLGenerator::closeSection()
687 {
688 // Once output is produced here, EPUBTextGenerator::closeSection() will need
689 // to call EPUBSplitGuard::closeLevel().
690 }
691
692 void EPUBHTMLGenerator::defineParagraphStyle(const RVNGPropertyList &propList)
693 {
694 m_impl->m_paragraphManager.defineParagraph(propList);
695 }
696
697 void EPUBHTMLGenerator::openParagraph(const RVNGPropertyList &propList)
698 {
699 if (m_impl->m_ignore)
700 return;
701
702 RVNGPropertyList attrs;
703 switch (m_impl->m_stylesMethod)
704 {
705 case EPUB_STYLES_METHOD_CSS:
706 attrs.insert("class", m_impl->m_paragraphManager.getClass(propList).c_str());
707 break;
708 case EPUB_STYLES_METHOD_INLINE:
709 attrs.insert("style", m_impl->m_paragraphManager.getStyle(propList).c_str());
710 break;
711 }
712 m_impl->output(false).openElement("p", attrs);
713 m_impl->m_hasText = false;
714
715 librevenge::RVNGPropertyList::Iter i(attrs);
716 RVNGPropertyList paragraphAttributes;
717 for (i.rewind(); i.next();)
718 paragraphAttributes.insert(i.key(), i()->clone());
719 m_impl->m_paragraphAttributesStack.push(paragraphAttributes);
720 }
721
722 void EPUBHTMLGenerator::closeParagraph()
723 {
724 if (m_impl->m_ignore)
725 return;
726
727 if (!m_impl->m_paragraphAttributesStack.empty())
728 m_impl->m_paragraphAttributesStack.pop();
729
730 if (!m_impl->m_hasText)
731 insertSpace();
732
733 m_impl->output().closeElement("p");
734 }
735
736 void EPUBHTMLGenerator::defineCharacterStyle(const RVNGPropertyList &propList)
737 {
738 m_impl->m_spanManager.defineSpan(propList);
739 }
740
741 void EPUBHTMLGenerator::openSpan(const RVNGPropertyList &propList)
742 {
743 if (m_impl->m_ignore)
744 return;
745
746 RVNGPropertyList attrs;
747 switch (m_impl->m_stylesMethod)
748 {
749 case EPUB_STYLES_METHOD_CSS:
750 attrs.insert("class", m_impl->m_spanManager.getClass(propList).c_str());
751 break;
752 case EPUB_STYLES_METHOD_INLINE:
753 attrs.insert("style", m_impl->m_spanManager.getStyle(propList).c_str());
754 break;
755 }
756
757 const librevenge::RVNGProperty *rubyText = propList["text:ruby-text"];
758 if (rubyText)
759 {
760 m_impl->m_rubyText = rubyText->getStr().cstr();
761 m_impl->output(false).openElement("ruby", attrs);
762 }
763
764 m_impl->output(false).openElement("span", attrs);
765
766 librevenge::RVNGPropertyList::Iter i(attrs);
767 RVNGPropertyList spanAttributes;
768 for (i.rewind(); i.next();)
769 spanAttributes.insert(i.key(), i()->clone());
770 m_impl->m_spanAttributesStack.push(spanAttributes);
771 }
772
773 void EPUBHTMLGenerator::closeSpan()
774 {
775 if (m_impl->m_ignore)
776 return;
777
778 if (!m_impl->m_spanAttributesStack.empty())
779 m_impl->m_spanAttributesStack.pop();
780
781 m_impl->output().closeElement("span");
782
783 if (m_impl->m_rubyText.length())
784 {
785 m_impl->output().openElement("rt");
786 m_impl->output().insertCharacters(m_impl->m_rubyText.c_str());
787 m_impl->output().closeElement("rt");
788 m_impl->output().closeElement("ruby");
789 m_impl->m_hasText = true;
790 m_impl->m_rubyText.clear();
791 }
792 }
793
794 void EPUBHTMLGenerator::openLink(const RVNGPropertyList &propList)
795 {
796 if (m_impl->m_ignore)
797 return;
798
799 if (!propList["xlink:type"])
800 {
801 EPUBGEN_DEBUG_MSG(("EPUBHTMLGenerator::openLink: xlink:type: not filled, suppose link\n"));
802 }
803 RVNGPropertyList attrs;
804 if (propList["xlink:href"])
805 {
806 std::string href(propList["xlink:href"]->getStr().cstr());
807
808 // Basic URL sanitization.
809 boost::replace_all(href, "http:///", "http://");
810 boost::replace_all(href, "https:///", "https://");
811
812 attrs.insert("href", href.c_str());
813 }
814 const librevenge::RVNGProperty *binaryDataProp = propList["office:binary-data"];
815 const librevenge::RVNGProperty *mimeTypeProp = propList["librevenge:mime-type"];
816 if (binaryDataProp && mimeTypeProp)
817 {
818 // This is not a real link, but more an additional image on top of an
819 // existing one, map it to footnotes instead.
820 RVNGPropertyList linkProperties;
821 linkProperties.insert("office:binary-data", binaryDataProp->clone());
822 linkProperties.insert("librevenge:mime-type", mimeTypeProp->clone());
823 m_impl->m_linkPropertiesStack.push(linkProperties);
824 }
825 else
826 // Implicit sendDelayed=true, so that in case the link is at the start of a
827 // footnote, links are not nested.
828 m_impl->output().openElement("a", attrs);
829 }
830
831 void EPUBHTMLGenerator::closeLink()
832 {
833 if (m_impl->m_ignore)
834 return;
835 if (!m_impl->m_linkPropertiesStack.empty())
836 m_impl->m_linkPropertiesStack.pop();
837 else
838 m_impl->output().closeElement("a");
839 }
840
841 void EPUBHTMLGenerator::insertTab()
842 {
843 if (m_impl->m_ignore)
844 return;
845
846 // \t would not have a lot of effect since tabs in html are ignorable
847 // white-space. Write NBSPs and a breakable space instead.
848 for (int i = 0; i < 15; ++i)
849 m_impl->output().insertCharacters("\xc2\xa0");
850 m_impl->output().insertCharacters(" ");
851 m_impl->m_hasText = true;
852 }
853
854 void EPUBHTMLGenerator::insertLineBreak()
855 {
856 if (m_impl->m_ignore)
857 return;
858 m_impl->output().openElement("br", RVNGPropertyList());
859 m_impl->output().closeElement("br");
860 }
861
862 void EPUBHTMLGenerator::insertField(const RVNGPropertyList & /* propList */)
863 {
864 if (m_impl->m_ignore)
865 return;
866 m_impl->output().insertCharacters("#");
867 }
868
869 void EPUBHTMLGenerator::insertText(const RVNGString &text)
870 {
871 if (m_impl->m_ignore)
872 return;
873 EPUBXMLContent &output = openPopup();
874 output.insertCharacters(text);
875 closePopup(output);
876 m_impl->m_hasText = true;
877 }
878
879 void EPUBHTMLGenerator::insertSpace()
880 {
881 if (m_impl->m_ignore)
882 return;
883 // NBSP.
884 m_impl->output().insertCharacters("\xc2\xa0");
885 }
886
887 void EPUBHTMLGenerator::openOrderedListLevel(const RVNGPropertyList &propList)
888 {
889 if (m_impl->m_ignore)
890 return;
891 m_impl->m_listManager.defineLevel(propList, true);
892 RVNGPropertyList attrs;
893 attrs.insert("class", m_impl->m_listManager.openLevel(propList, true).c_str());
894 // fixme: if level is > 1, we must first insert a div here
895 m_impl->output(false).openElement("ol", attrs);
896 }
897
898 void EPUBHTMLGenerator::closeOrderedListLevel()
899 {
900 if (m_impl->m_ignore)
901 return;
902 m_impl->m_listManager.closeLevel();
903 m_impl->output().closeElement("ol");
904 }
905
906 void EPUBHTMLGenerator::openUnorderedListLevel(const RVNGPropertyList &propList)
907 {
908 if (m_impl->m_ignore)
909 return;
910 m_impl->m_listManager.defineLevel(propList, false);
911 RVNGPropertyList attrs;
912 attrs.insert("class", m_impl->m_listManager.openLevel(propList, false).c_str());
913 // fixme: if level is > 1, we must first insert a div here
914 m_impl->output(false).openElement("ul", attrs);
915 }
916
917 void EPUBHTMLGenerator::closeUnorderedListLevel()
918 {
919 if (m_impl->m_ignore)
920 return;
921 m_impl->m_listManager.closeLevel();
922 m_impl->output().closeElement("ul");
923 }
924
925
926 void EPUBHTMLGenerator::openListElement(const RVNGPropertyList &propList)
927 {
928 if (m_impl->m_ignore)
929 return;
930 RVNGPropertyList attrs;
931 attrs.insert("class", m_impl->m_listManager.getClass(propList).c_str());
932 m_impl->output(false).openElement("li", attrs);
933 }
934
935 void EPUBHTMLGenerator::closeListElement()
936 {
937 if (m_impl->m_ignore)
938 return;
939 m_impl->output().closeElement("li");
940 }
941
942 void EPUBHTMLGenerator::openFootnote(const RVNGPropertyList &propList)
943 {
944 if (m_impl->m_ignore)
945 return;
946 EPUBXMLContent &output = m_impl->output();
947 m_impl->push(EPUBHTMLTextZone::Z_FootNote);
948 librevenge::RVNGString number;
949 if (const librevenge::RVNGProperty *numProp = propList["librevenge:number"])
950 number = numProp->getStr();
951 bool closeAnchor = m_impl->m_linkPropertiesStack.empty();
952 m_impl->getSink().addLabel(output, number, closeAnchor);
953 }
954
955 void EPUBHTMLGenerator::closeFootnote()
956 {
957 if (m_impl->m_ignore)
958 return;
959 if (m_impl->m_version >= 30)
960 m_impl->output().closeElement("aside");
961 m_impl->pop();
962 }
963
964 void EPUBHTMLGenerator::openEndnote(const RVNGPropertyList &)
965 {
966 if (m_impl->m_ignore)
967 return;
968 EPUBXMLContent &output = m_impl->output();
969 m_impl->push(EPUBHTMLTextZone::Z_EndNote);
970 m_impl->getSink().addLabel(output, librevenge::RVNGString(), true);
971 }
972
973 void EPUBHTMLGenerator::closeEndnote()
974 {
975 if (m_impl->m_ignore)
976 return;
977 m_impl->pop();
978 }
979
980 void EPUBHTMLGenerator::openComment(const RVNGPropertyList & /*propList*/)
981 {
982 if (m_impl->m_ignore)
983 return;
984 EPUBXMLContent &output = m_impl->output();
985 m_impl->push(EPUBHTMLTextZone::Z_Comment);
986 m_impl->getSink().addLabel(output, librevenge::RVNGString(), true);
987 }
988
989 void EPUBHTMLGenerator::closeComment()
990 {
991 if (m_impl->m_ignore)
992 return;
993 m_impl->pop();
994 }
995
996 void EPUBHTMLGenerator::openTextBox(const RVNGPropertyList & /*propList*/)
997 {
998 if (m_impl->m_ignore)
999 return;
1000
1001 if (!m_impl->m_spanAttributesStack.empty())
1002 m_impl->output().closeElement("span");
1003 if (!m_impl->m_paragraphAttributesStack.empty())
1004 m_impl->output().closeElement("p");
1005
1006 RVNGPropertyList attrs;
1007
1008 if (!m_impl->m_framePropertiesStack.empty())
1009 {
1010 RVNGPropertyList &frameProperties = m_impl->m_framePropertiesStack.top();
1011 switch (m_impl->m_stylesMethod)
1012 {
1013 case EPUB_STYLES_METHOD_CSS:
1014 attrs.insert("class", m_impl->m_imageManager.getFrameClass(frameProperties).c_str());
1015 break;
1016 case EPUB_STYLES_METHOD_INLINE:
1017 attrs.insert("style", m_impl->m_imageManager.getFrameStyle(frameProperties).c_str());
1018 break;
1019 }
1020 }
1021
1022 m_impl->output().openElement("div", attrs);
1023 }
1024
1025 void EPUBHTMLGenerator::closeTextBox()
1026 {
1027 if (m_impl->m_ignore)
1028 return;
1029
1030 m_impl->output().closeElement("div");
1031
1032 if (!m_impl->m_framePropertiesStack.empty())
1033 {
1034 RVNGPropertyList &frameProperties = m_impl->m_framePropertiesStack.top();
1035 RVNGString wrapStyle = m_impl->m_imageManager.getWrapStyle(frameProperties).c_str();
1036 if (!wrapStyle.empty())
1037 {
1038 RVNGPropertyList attrs;
1039 attrs.insert("style", wrapStyle);
1040 m_impl->output().insertEmptyElement("br", attrs);
1041 }
1042 }
1043
1044 if (!m_impl->m_paragraphAttributesStack.empty())
1045 m_impl->output(false).openElement("p", m_impl->m_paragraphAttributesStack.top());
1046 if (!m_impl->m_spanAttributesStack.empty())
1047 m_impl->output(false).openElement("span", m_impl->m_spanAttributesStack.top());
1048 }
1049
1050 void EPUBHTMLGenerator::openTable(const RVNGPropertyList &propList)
1051 {
1052 if (m_impl->m_ignore)
1053 return;
1054
1055 m_impl->m_tableManager.openTable(propList);
1056 RVNGPropertyList attrs;
1057 switch (m_impl->m_stylesMethod)
1058 {
1059 case EPUB_STYLES_METHOD_CSS:
1060 attrs.insert("class", m_impl->m_tableManager.getTableClass(propList).c_str());
1061 break;
1062 case EPUB_STYLES_METHOD_INLINE:
1063 attrs.insert("style", m_impl->m_tableManager.getTableStyle(propList).c_str());
1064 break;
1065 }
1066 m_impl->output().openElement("table", attrs);
1067 m_impl->output().openElement("tbody", RVNGPropertyList());
1068 }
1069
1070 void EPUBHTMLGenerator::openTableRow(const RVNGPropertyList &propList)
1071 {
1072 if (m_impl->m_ignore)
1073 return;
1074 RVNGPropertyList attrs;
1075 switch (m_impl->m_stylesMethod)
1076 {
1077 case EPUB_STYLES_METHOD_CSS:
1078 attrs.insert("class", m_impl->m_tableManager.getRowClass(propList).c_str());
1079 break;
1080 case EPUB_STYLES_METHOD_INLINE:
1081 attrs.insert("style", m_impl->m_tableManager.getRowStyle(propList).c_str());
1082 break;
1083 }
1084 m_impl->output().openElement("tr", attrs);
1085 }
1086
1087 void EPUBHTMLGenerator::closeTableRow()
1088 {
1089 if (m_impl->m_ignore)
1090 return;
1091 m_impl->output().closeElement("tr");
1092 }
1093
1094 void EPUBHTMLGenerator::openTableCell(const RVNGPropertyList &propList)
1095 {
1096 if (m_impl->m_ignore)
1097 return;
1098 RVNGPropertyList attrs;
1099 switch (m_impl->m_stylesMethod)
1100 {
1101 case EPUB_STYLES_METHOD_CSS:
1102 attrs.insert("class", m_impl->m_tableManager.getCellClass(propList).c_str());
1103 break;
1104 case EPUB_STYLES_METHOD_INLINE:
1105 attrs.insert("style", m_impl->m_tableManager.getCellStyle(propList).c_str());
1106 break;
1107 }
1108 if (propList["table:number-columns-spanned"])
1109 attrs.insert("colspan", propList["table:number-columns-spanned"]->getInt());
1110 if (propList["table:number-rows-spanned"])
1111 attrs.insert("rowspan", propList["table:number-rows-spanned"]->getInt());
1112 m_impl->output().openElement("td", attrs);
1113
1114 }
1115
1116 void EPUBHTMLGenerator::closeTableCell()
1117 {
1118 if (m_impl->m_ignore)
1119 return;
1120 m_impl->output().closeElement("td");
1121 }
1122
1123 void EPUBHTMLGenerator::insertCoveredTableCell(const RVNGPropertyList & /* propList */) {}
1124
1125 void EPUBHTMLGenerator::closeTable()
1126 {
1127 if (m_impl->m_ignore)
1128 return;
1129 m_impl->output().closeElement("tbody");
1130 m_impl->output().closeElement("table");
1131 m_impl->m_tableManager.closeTable();
1132 }
1133
1134 void EPUBHTMLGenerator::openFrame(const RVNGPropertyList &propList)
1135 {
1136 librevenge::RVNGPropertyList::Iter i(propList);
1137 std::string anchorType;
1138 RVNGPropertyList frameProperties;
1139 for (i.rewind(); i.next();)
1140 {
1141 if (std::string("text:anchor-type") == i.key())
1142 anchorType = i()->getStr().cstr();
1143
1144 // Remember the property for binary object purposes.
1145 frameProperties.insert(i.key(), i()->clone());
1146 }
1147
1148 if (anchorType == "page")
1149 // Other anchor types are already inside a paragraph.
1150 m_impl->output().openElement("p", RVNGPropertyList());
1151 m_impl->m_frameAnchorTypes.push(anchorType);
1152
1153 m_impl->m_framePropertiesStack.push(frameProperties);
1154 }
1155
1156 void EPUBHTMLGenerator::closeFrame()
1157 {
1158 if (!m_impl->m_framePropertiesStack.empty())
1159 m_impl->m_framePropertiesStack.pop();
1160
1161 if (m_impl->m_frameAnchorTypes.empty())
1162 return;
1163
1164 if (m_impl->m_frameAnchorTypes.top() == "page")
1165 m_impl->output().closeElement("p");
1166
1167 m_impl->m_frameAnchorTypes.pop();
1168 }
1169
1170 void EPUBHTMLGenerator::openGroup(const librevenge::RVNGPropertyList & /* propList */) {}
1171 void EPUBHTMLGenerator::closeGroup() {}
1172
1173 void EPUBHTMLGenerator::defineGraphicStyle(const librevenge::RVNGPropertyList & /* propList */) {}
1174 void EPUBHTMLGenerator::drawRectangle(const librevenge::RVNGPropertyList & /* propList */) {}
1175 void EPUBHTMLGenerator::drawEllipse(const librevenge::RVNGPropertyList & /* propList */) {}
1176 void EPUBHTMLGenerator::drawPolygon(const librevenge::RVNGPropertyList & /* propList */) {}
1177 void EPUBHTMLGenerator::drawPolyline(const librevenge::RVNGPropertyList & /* propList */) {}
1178 void EPUBHTMLGenerator::drawPath(const librevenge::RVNGPropertyList & /* propList */) {}
1179 void EPUBHTMLGenerator::drawConnector(const librevenge::RVNGPropertyList & /* propList */) {}
1180
1181 void EPUBHTMLGenerator::insertBinaryObject(const RVNGPropertyList &propList)
1182 {
1183 const EPUBPath &path = m_impl->m_imageManager.insert(
1184 RVNGBinaryData(propList["office:binary-data"]->getStr()),
1185 propList["librevenge:mime-type"]->getStr());
1186
1187 RVNGPropertyList attrs;
1188 RVNGString wrapStyle;
1189
1190 if (!m_impl->m_framePropertiesStack.empty())
1191 {
1192 RVNGPropertyList &frameProperties = m_impl->m_framePropertiesStack.top();
1193 switch (m_impl->m_stylesMethod)
1194 {
1195 case EPUB_STYLES_METHOD_CSS:
1196 attrs.insert("class", m_impl->m_imageManager.getFrameClass(frameProperties).c_str());
1197 break;
1198 case EPUB_STYLES_METHOD_INLINE:
1199 attrs.insert("style", m_impl->m_imageManager.getFrameStyle(frameProperties).c_str());
1200 break;
1201 }
1202
1203 wrapStyle = m_impl->m_imageManager.getWrapStyle(frameProperties).c_str();
1204 }
1205
1206 attrs.insert("src", path.relativeTo(m_impl->m_path).str().c_str());
1207 // FIXME: use alternative repr. if available
1208 attrs.insert("alt", path.str().c_str());
1209 EPUBXMLContent &popup = openPopup();
1210 popup.insertEmptyElement("img", attrs);
1211 closePopup(popup);
1212
1213 if (!wrapStyle.empty())
1214 {
1215 attrs.clear();
1216 attrs.insert("style", wrapStyle);
1217 m_impl->output().insertEmptyElement("br", attrs);
1218 }
1219 }
1220
1221 EPUBXMLContent &EPUBHTMLGenerator::openPopup()
1222 {
1223 if (!m_impl->m_linkPropertiesStack.empty())
1224 {
1225 // Save the main sink, as m_impl->output() will point to the footnote sink.
1226 libepubgen::EPUBXMLContent &main = m_impl->output();
1227 openFootnote(RVNGPropertyList());
1228 return main;
1229 }
1230 else
1231 return m_impl->output();
1232 }
1233
1234 void EPUBHTMLGenerator::closePopup(EPUBXMLContent &main)
1235 {
1236 if (!m_impl->m_linkPropertiesStack.empty())
1237 {
1238 RVNGPropertyList &linkProperties = m_impl->m_linkPropertiesStack.top();
1239 main.closeElement("a");
1240 const EPUBPath &linkPath = m_impl->m_imageManager.insert(
1241 RVNGBinaryData(linkProperties["office:binary-data"]->getStr()),
1242 linkProperties["librevenge:mime-type"]->getStr());
1243 RVNGPropertyList linkAttrs;
1244 linkAttrs.insert("src", linkPath.relativeTo(m_impl->m_path).str().c_str());
1245 linkAttrs.insert("alt", linkPath.str().c_str());
1246 m_impl->output().insertEmptyElement("img", linkAttrs);
1247 closeFootnote();
1248 }
1249 }
1250
1251 void EPUBHTMLGenerator::insertEquation(const RVNGPropertyList & /* propList */) {}
1252
1253 void EPUBHTMLGenerator::getPageProperties(librevenge::RVNGPropertyList &propList) const
1254 {
1255 propList.clear();
1256 librevenge::RVNGPropertyList::Iter i(m_impl->m_actualPageProperties);
1257 for (i.rewind(); i.next();)
1258 propList.insert(i.key(), i()->clone());
1259 }
1260
1261 void EPUBHTMLGenerator::setPageProperties(const librevenge::RVNGPropertyList &propList)
1262 {
1263 m_impl->m_actualPageProperties.clear();
1264 librevenge::RVNGPropertyList::Iter i(propList);
1265 for (i.rewind(); i.next();)
1266 m_impl->m_actualPageProperties.insert(i.key(), i()->clone());
1267 }
1268
1269 }
1270
1271 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */