| 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 |
#ifdef HAVE_CONFIG_H |
| 11 |
#include "config.h" |
| 12 |
#endif |
| 13 |
|
| 14 |
#include "EPUBGenerator.h" |
| 15 |
|
| 16 |
#include <ctime> |
| 17 |
#include <sstream> |
| 18 |
|
| 19 |
#include <boost/uuid/uuid.hpp> |
| 20 |
#include <boost/uuid/uuid_generators.hpp> |
| 21 |
#include <boost/uuid/uuid_io.hpp> |
| 22 |
|
| 23 |
#include "EPUBCSSContent.h" |
| 24 |
#include "EPUBHTMLGenerator.h" |
| 25 |
#include "EPUBXMLContent.h" |
| 26 |
|
| 27 |
namespace libepubgen |
| 28 |
{ |
| 29 |
|
| 30 |
using librevenge::RVNGPropertyFactory; |
| 31 |
using librevenge::RVNGPropertyList; |
| 32 |
using librevenge::RVNGString; |
| 33 |
|
| 34 |
EPUBGenerator::EPUBGenerator(EPUBPackage *const package, int version) |
| 35 |
: m_package(package) |
| 36 |
, m_manifest() |
| 37 |
, m_htmlManager(m_manifest) |
| 38 |
, m_imageManager(m_manifest) |
| 39 |
, m_fontManager(m_manifest) |
| 40 |
, m_listStyleManager() |
| 41 |
, m_paragraphStyleManager() |
| 42 |
, m_spanStyleManager("span") |
| 43 |
, m_bodyStyleManager("body") |
| 44 |
, m_tableStyleManager() |
| 45 |
, m_stylesheetPath("OEBPS/styles/stylesheet.css") |
| 46 |
, m_documentProps() |
| 47 |
, m_metadata() |
| 48 |
, m_currentHtml() |
| 49 |
, m_splitGuard(EPUB_SPLIT_METHOD_PAGE_BREAK,true) |
| 50 |
, m_version(version) |
| 51 |
, m_stylesMethod(EPUB_STYLES_METHOD_CSS) |
| 52 |
, m_layoutMethod(EPUB_LAYOUT_METHOD_REFLOWABLE) |
| 53 |
{ |
| 54 |
} |
| 55 |
|
| 56 |
EPUBGenerator::~EPUBGenerator() |
| 57 |
{ |
| 58 |
} |
| 59 |
|
| 60 |
void EPUBGenerator::startDocument(const RVNGPropertyList &props) |
| 61 |
{ |
| 62 |
m_documentProps = props; |
| 63 |
|
| 64 |
startNewHtmlFile(); |
| 65 |
|
| 66 |
if (m_version >= 30) |
| 67 |
m_manifest.insert(EPUBPath("OEBPS/toc.xhtml"), "application/xhtml+xml", "toc.xhtml", "nav"); |
| 68 |
m_manifest.insert(EPUBPath("OEBPS/toc.ncx"), "application/x-dtbncx+xml", "toc.ncx", ""); |
| 69 |
m_manifest.insert(m_stylesheetPath, "text/css", "stylesheet.css", ""); |
| 70 |
} |
| 71 |
|
| 72 |
void EPUBGenerator::endDocument() |
| 73 |
{ |
| 74 |
if (bool(m_currentHtml)) |
| 75 |
{ |
| 76 |
endHtmlFile(); |
| 77 |
m_currentHtml->endDocument(); |
| 78 |
} |
| 79 |
|
| 80 |
writeContainer(); |
| 81 |
writeRoot(); |
| 82 |
writeNavigation(); |
| 83 |
writeStylesheet(); |
| 84 |
m_htmlManager.writeTo(*m_package); |
| 85 |
m_imageManager.writeTo(*m_package); |
| 86 |
m_fontManager.writeTo(*m_package); |
| 87 |
} |
| 88 |
|
| 89 |
void EPUBGenerator::setDocumentMetaData(const RVNGPropertyList &props) |
| 90 |
{ |
| 91 |
m_metadata = props; |
| 92 |
|
| 93 |
if (m_version >= 30) |
| 94 |
{ |
| 95 |
const librevenge::RVNGPropertyListVector *coverImages = props.child("librevenge:cover-images"); |
| 96 |
if (coverImages) |
| 97 |
{ |
| 98 |
for (size_t i = 0; i < coverImages->count(); ++i) |
| 99 |
{ |
| 100 |
librevenge::RVNGPropertyList const &propertyList = (*coverImages)[i]; |
| 101 |
if (propertyList["office:binary-data"] && propertyList["librevenge:mime-type"]) |
| 102 |
{ |
| 103 |
m_imageManager.insert(librevenge::RVNGBinaryData(propertyList["office:binary-data"]->getStr()), |
| 104 |
propertyList["librevenge:mime-type"]->getStr(), |
| 105 |
"cover-image"); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void EPUBGenerator::startNewHtmlFile() |
| 113 |
{ |
| 114 |
// close the current HTML file |
| 115 |
librevenge::RVNGPropertyList pageProperties; |
| 116 |
if (bool(m_currentHtml)) |
| 117 |
{ |
| 118 |
endHtmlFile(); |
| 119 |
m_currentHtml->endDocument(); |
| 120 |
m_currentHtml->getPageProperties(pageProperties); |
| 121 |
} |
| 122 |
|
| 123 |
m_splitGuard.onSplit(); |
| 124 |
|
| 125 |
m_currentHtml = m_htmlManager.create(m_imageManager, m_fontManager, m_listStyleManager, m_paragraphStyleManager, m_spanStyleManager, m_bodyStyleManager, m_tableStyleManager, m_stylesheetPath, m_stylesMethod, m_layoutMethod, m_version); |
| 126 |
|
| 127 |
// Splitted html file should keep the same page property. |
| 128 |
m_currentHtml->setPageProperties(pageProperties); |
| 129 |
|
| 130 |
// restore state in the new file |
| 131 |
m_currentHtml->startDocument(m_documentProps); |
| 132 |
m_currentHtml->setDocumentMetaData(m_metadata); |
| 133 |
|
| 134 |
startHtmlFile(); |
| 135 |
} |
| 136 |
|
| 137 |
const std::shared_ptr<EPUBHTMLGenerator> &EPUBGenerator::getHtml() const |
| 138 |
{ |
| 139 |
return m_currentHtml; |
| 140 |
} |
| 141 |
|
| 142 |
EPUBHTMLManager &EPUBGenerator::getHtmlManager() |
| 143 |
{ |
| 144 |
return m_htmlManager; |
| 145 |
} |
| 146 |
|
| 147 |
const EPUBSplitGuard &EPUBGenerator::getSplitGuard() const |
| 148 |
{ |
| 149 |
return m_splitGuard; |
| 150 |
} |
| 151 |
|
| 152 |
EPUBSplitGuard &EPUBGenerator::getSplitGuard() |
| 153 |
{ |
| 154 |
return m_splitGuard; |
| 155 |
} |
| 156 |
|
| 157 |
int EPUBGenerator::getVersion() const |
| 158 |
{ |
| 159 |
return m_version; |
| 160 |
} |
| 161 |
|
| 162 |
void EPUBGenerator::setSplitMethod(EPUBSplitMethod split) |
| 163 |
{ |
| 164 |
m_splitGuard.setSplitMethod(split); |
| 165 |
} |
| 166 |
|
| 167 |
void EPUBGenerator::setStylesMethod(EPUBStylesMethod styles) |
| 168 |
{ |
| 169 |
m_stylesMethod = styles; |
| 170 |
} |
| 171 |
|
| 172 |
void EPUBGenerator::setLayoutMethod(EPUBLayoutMethod layout) |
| 173 |
{ |
| 174 |
m_layoutMethod = layout; |
| 175 |
if (m_layoutMethod == EPUB_LAYOUT_METHOD_FIXED) |
| 176 |
// Fixed layout implies split on page break. |
| 177 |
m_splitGuard.setSplitMethod(EPUB_SPLIT_METHOD_PAGE_BREAK); |
| 178 |
|
| 179 |
m_splitGuard.setSplitOnSecondPageSpan(m_layoutMethod == EPUB_LAYOUT_METHOD_REFLOWABLE); |
| 180 |
} |
| 181 |
|
| 182 |
void EPUBGenerator::writeContainer() |
| 183 |
{ |
| 184 |
EPUBXMLContent xml; |
| 185 |
|
| 186 |
RVNGPropertyList containerAttrs; |
| 187 |
containerAttrs.insert("version", RVNGPropertyFactory::newStringProp("1.0")); |
| 188 |
containerAttrs.insert("xmlns", "urn:oasis:names:tc:opendocument:xmlns:container"); |
| 189 |
|
| 190 |
xml.openElement("container", containerAttrs); |
| 191 |
xml.openElement("rootfiles"); |
| 192 |
|
| 193 |
RVNGPropertyList rootfileAttrs; |
| 194 |
rootfileAttrs.insert("full-path", "OEBPS/content.opf"); |
| 195 |
rootfileAttrs.insert("media-type", "application/oebps-package+xml"); |
| 196 |
|
| 197 |
xml.insertEmptyElement("rootfile", rootfileAttrs); |
| 198 |
|
| 199 |
xml.closeElement("rootfiles"); |
| 200 |
xml.closeElement("container"); |
| 201 |
|
| 202 |
xml.writeTo(*m_package, "META-INF/container.xml"); |
| 203 |
} |
| 204 |
|
| 205 |
void EPUBGenerator::writeNavigation() |
| 206 |
{ |
| 207 |
if (m_version >= 30) |
| 208 |
{ |
| 209 |
EPUBXMLContent xml; |
| 210 |
|
| 211 |
const EPUBPath path("OEBPS/toc.xhtml"); |
| 212 |
RVNGPropertyList htmlAttrs; |
| 213 |
htmlAttrs.insert("xmlns", "http://www.w3.org/1999/xhtml"); |
| 214 |
htmlAttrs.insert("xmlns:epub", "http://www.idpf.org/2007/ops"); |
| 215 |
xml.openElement("html", htmlAttrs); |
| 216 |
|
| 217 |
xml.openElement("head"); |
| 218 |
xml.closeElement("head"); |
| 219 |
xml.openElement("body"); |
| 220 |
|
| 221 |
RVNGPropertyList navAttrs; |
| 222 |
navAttrs.insert("epub:type", "toc"); |
| 223 |
xml.openElement("nav", navAttrs); |
| 224 |
|
| 225 |
xml.openElement("ol"); |
| 226 |
m_htmlManager.writeTocTo(xml, path, m_version, m_layoutMethod); |
| 227 |
xml.closeElement("ol"); |
| 228 |
|
| 229 |
xml.closeElement("nav"); |
| 230 |
xml.closeElement("body"); |
| 231 |
xml.closeElement("html"); |
| 232 |
|
| 233 |
xml.writeTo(*m_package, path.str().c_str()); |
| 234 |
} |
| 235 |
|
| 236 |
EPUBXMLContent xml; |
| 237 |
|
| 238 |
const EPUBPath path("OEBPS/toc.ncx"); |
| 239 |
RVNGPropertyList ncxAttrs; |
| 240 |
ncxAttrs.insert("xmlns", "http://www.daisy.org/z3986/2005/ncx/"); |
| 241 |
ncxAttrs.insert("version", "2005-1"); |
| 242 |
|
| 243 |
xml.openElement("ncx", ncxAttrs); |
| 244 |
|
| 245 |
xml.openElement("head"); |
| 246 |
RVNGPropertyList metaAttrs; |
| 247 |
metaAttrs.insert("name", ""); |
| 248 |
metaAttrs.insert("content", ""); |
| 249 |
metaAttrs.insert("scheme", ""); |
| 250 |
xml.insertEmptyElement("meta", metaAttrs); |
| 251 |
xml.closeElement("head"); |
| 252 |
xml.openElement("docTitle"); |
| 253 |
xml.openElement("text"); |
| 254 |
xml.closeElement("text"); |
| 255 |
xml.closeElement("docTitle"); |
| 256 |
|
| 257 |
xml.openElement("navMap"); |
| 258 |
// In case of EPUB3 the (deprecated, but valid) EPUB2 markup is wanted, so |
| 259 |
// the version is unconditional here. |
| 260 |
m_htmlManager.writeTocTo(xml, path, /*version=*/20, m_layoutMethod); |
| 261 |
xml.closeElement("navMap"); |
| 262 |
|
| 263 |
xml.closeElement("ncx"); |
| 264 |
|
| 265 |
xml.writeTo(*m_package, path.str().c_str()); |
| 266 |
} |
| 267 |
|
| 268 |
void EPUBGenerator::writeStylesheet() |
| 269 |
{ |
| 270 |
EPUBCSSContent stylesheet; |
| 271 |
|
| 272 |
m_fontManager.send(stylesheet); |
| 273 |
m_listStyleManager.send(stylesheet); |
| 274 |
m_paragraphStyleManager.send(stylesheet); |
| 275 |
m_spanStyleManager.send(stylesheet); |
| 276 |
m_bodyStyleManager.send(stylesheet); |
| 277 |
m_tableStyleManager.send(stylesheet); |
| 278 |
m_imageManager.send(stylesheet); |
| 279 |
|
| 280 |
stylesheet.writeTo(*m_package, m_stylesheetPath.str().c_str()); |
| 281 |
} |
| 282 |
|
| 283 |
void EPUBGenerator::writeRoot() |
| 284 |
{ |
| 285 |
EPUBXMLContent sink; |
| 286 |
|
| 287 |
const RVNGString uniqueId("unique-identifier"); |
| 288 |
|
| 289 |
RVNGPropertyList packageAttrs; |
| 290 |
packageAttrs.insert("xmlns", "http://www.idpf.org/2007/opf"); |
| 291 |
packageAttrs.insert("xmlns:dc", "http://purl.org/dc/elements/1.1/"); |
| 292 |
packageAttrs.insert("xmlns:dcterms", "http://purl.org/dc/terms/"); |
| 293 |
packageAttrs.insert("xmlns:opf", "http://www.idpf.org/2007/opf"); |
| 294 |
if (m_version >= 30) |
| 295 |
packageAttrs.insert("version", RVNGPropertyFactory::newStringProp("3.0")); |
| 296 |
else |
| 297 |
packageAttrs.insert("version", RVNGPropertyFactory::newStringProp("2.0")); |
| 298 |
packageAttrs.insert("unique-identifier", uniqueId); |
| 299 |
|
| 300 |
sink.openElement("package", packageAttrs); |
| 301 |
|
| 302 |
sink.openElement("metadata"); |
| 303 |
|
| 304 |
RVNGPropertyList identifierAttrs; |
| 305 |
identifierAttrs.insert("id", uniqueId); |
| 306 |
sink.openElement("dc:identifier", identifierAttrs); |
| 307 |
// The identifier element is required to have a unique character content. |
| 308 |
std::stringstream identifierStream("urn:uuid:"); |
| 309 |
boost::uuids::uuid uuid = boost::uuids::random_generator()(); |
| 310 |
identifierStream << uuid; |
| 311 |
std::string identifierCharactrs = identifierStream.str(); |
| 312 |
RVNGString identifier = identifierCharactrs.c_str(); |
| 313 |
if (m_metadata["dc:identifier"] && !m_metadata["dc:identifier"]->getStr().empty()) |
| 314 |
identifier = m_metadata["dc:identifier"]->getStr(); |
| 315 |
sink.insertCharacters(identifier); |
| 316 |
sink.closeElement("dc:identifier"); |
| 317 |
|
| 318 |
RVNGString title("Unknown Title"); |
| 319 |
if (m_metadata["dc:title"] && !m_metadata["dc:title"]->getStr().empty()) |
| 320 |
title = m_metadata["dc:title"]->getStr(); |
| 321 |
sink.openElement("dc:title"); |
| 322 |
sink.insertCharacters(title); |
| 323 |
sink.closeElement("dc:title"); |
| 324 |
|
| 325 |
RVNGString creator("Unknown Author"); |
| 326 |
if (m_metadata["meta:initial-creator"] && !m_metadata["meta:initial-creator"]->getStr().empty()) |
| 327 |
creator = m_metadata["meta:initial-creator"]->getStr(); |
| 328 |
sink.openElement("dc:creator"); |
| 329 |
sink.insertCharacters(creator); |
| 330 |
sink.closeElement("dc:creator"); |
| 331 |
|
| 332 |
RVNGString language("en"); |
| 333 |
if (m_metadata["dc:language"] && !m_metadata["dc:language"]->getStr().empty()) |
| 334 |
language = m_metadata["dc:language"]->getStr(); |
| 335 |
sink.openElement("dc:language"); |
| 336 |
sink.insertCharacters(language); |
| 337 |
sink.closeElement("dc:language"); |
| 338 |
|
| 339 |
if (m_version >= 30) |
| 340 |
{ |
| 341 |
RVNGString date; |
| 342 |
time_t now = 0; |
| 343 |
time(&now); |
| 344 |
const struct tm *local = localtime(&now); |
| 345 |
if (local) |
| 346 |
{ |
| 347 |
const int MAX_BUFFER = 1024; |
| 348 |
char buffer[MAX_BUFFER]; |
| 349 |
strftime(&buffer[0], MAX_BUFFER-1, "%Y-%m-%dT%H:%M:%SZ", local); |
| 350 |
date.append(buffer); |
| 351 |
} |
| 352 |
|
| 353 |
if (m_metadata["dc:date"] && !m_metadata["dc:date"]->getStr().empty()) |
| 354 |
{ |
| 355 |
// Expecting CCYY-MM-DDThh:mm:ssZ, librevenge provides CCYY-MM-DDThh:mm:ss.sssssssss |
| 356 |
date = std::string(m_metadata["dc:date"]->getStr().cstr()).substr(0, 19).c_str(); |
| 357 |
date.append("Z"); |
| 358 |
} |
| 359 |
|
| 360 |
RVNGPropertyList metaAttrs; |
| 361 |
metaAttrs.insert("property", "dcterms:modified"); |
| 362 |
sink.openElement("meta", metaAttrs); |
| 363 |
sink.insertCharacters(date); |
| 364 |
sink.closeElement("meta"); |
| 365 |
|
| 366 |
#ifdef VERSION |
| 367 |
const std::string version(VERSION); |
| 368 |
#else |
| 369 |
const std::string version("unknown"); |
| 370 |
#endif |
| 371 |
std::string generator; |
| 372 |
if (m_metadata["meta:generator"]) |
| 373 |
generator = m_metadata["meta:generator"]->getStr().cstr(); |
| 374 |
|
| 375 |
if (generator.empty()) |
| 376 |
generator = "libepubgen/" + version; |
| 377 |
else |
| 378 |
{ |
| 379 |
generator += " ("; |
| 380 |
generator += "libepubgen/" + version; |
| 381 |
generator += ")"; |
| 382 |
} |
| 383 |
|
| 384 |
metaAttrs.clear(); |
| 385 |
metaAttrs.insert("name", "generator"); |
| 386 |
metaAttrs.insert("content", generator.c_str()); |
| 387 |
sink.openElement("meta", metaAttrs); |
| 388 |
sink.closeElement("meta"); |
| 389 |
|
| 390 |
if (m_layoutMethod == EPUB_LAYOUT_METHOD_FIXED) |
| 391 |
{ |
| 392 |
metaAttrs.clear(); |
| 393 |
metaAttrs.insert("property", "rendition:layout"); |
| 394 |
sink.openElement("meta", metaAttrs); |
| 395 |
sink.insertCharacters("pre-paginated"); |
| 396 |
sink.closeElement("meta"); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
sink.closeElement("metadata"); |
| 401 |
|
| 402 |
sink.openElement("manifest"); |
| 403 |
m_manifest.writeTo(sink); |
| 404 |
sink.closeElement("manifest"); |
| 405 |
|
| 406 |
RVNGPropertyList spineAttrs; |
| 407 |
spineAttrs.insert("toc", "toc.ncx"); |
| 408 |
|
| 409 |
sink.openElement("spine", spineAttrs); |
| 410 |
m_htmlManager.writeSpineTo(sink); |
| 411 |
sink.closeElement("spine"); |
| 412 |
|
| 413 |
sink.closeElement("package"); |
| 414 |
|
| 415 |
sink.writeTo(*m_package, "OEBPS/content.opf"); |
| 416 |
} |
| 417 |
|
| 418 |
} |
| 419 |
|
| 420 |
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |