diff --git a/generators/epub/converter.cpp b/generators/epub/converter.cpp
index 74df151a3..353d32586 100644
--- a/generators/epub/converter.cpp
+++ b/generators/epub/converter.cpp
@@ -111,16 +111,8 @@ QTextDocument* Converter::convert( const QString &fileName )
}
mTextDocument = newDocument;
- mTextDocument->setPageSize(QSizeF(600, 800));
-
QTextCursor *_cursor = new QTextCursor( mTextDocument );
- QTextFrameFormat frameFormat;
- frameFormat.setMargin( 20 );
-
- QTextFrame *rootFrame = mTextDocument->rootFrame();
- rootFrame->setFrameFormat( frameFormat );
-
mLocalLinks.clear();
mSectionMap.clear();
@@ -142,32 +134,46 @@ QTextDocument* Converter::convert( const QString &fileName )
// iterate over the book
it = epub_get_iterator(mTextDocument->getEpub(), EITERATOR_SPINE, 0);
- do {
- if (epub_it_get_curr(it)) {
-
- // insert block for links
- _cursor->insertBlock();
-
- QString link = QString::fromUtf8(epub_it_get_curr_url(it));
+ // if the background color of the document is non-white it will be handled by QTextDocument::setHtml()
+ bool firstPage = true;
+ do{
+ if(epub_it_get_curr(it)) {
+ const QString link = QString::fromUtf8(epub_it_get_curr_url(it));
mTextDocument->setCurrentSubDocument(link);
+ QString htmlContent = QString::fromUtf8(epub_it_get_curr(it));
+
+ QTextBlock before;
+ if(firstPage) {
+ // preHtml & postHtml make it possible to have a margin around the content of the page
+ const QString preHtml = QString("
"
+ ""
+ ""
+ "| ").arg(mTextDocument->padding);
+ const QString postHtml = " |
";
+ mTextDocument->setHtml(preHtml + htmlContent + postHtml);
+ firstPage = false;
+ before = mTextDocument->begin();
+ } else {
+ before = _cursor->block();
+ _cursor->insertHtml(htmlContent);
+ }
- // Pass on all the anchor since last block
- const QTextBlock &before = _cursor->block();
mSectionMap.insert(link, before);
- _cursor->insertHtml(QString::fromUtf8(epub_it_get_curr(it)));
- // Add anchors to hashes
_handle_anchors(before, link);
- // Start new file in a new page
- int page = mTextDocument->pageCount();
+ const int page = mTextDocument->pageCount();
+
+ // it will clear the previous format
+ // useful when the last line had a bullet
+ _cursor->insertBlock(QTextBlockFormat());
+
while(mTextDocument->pageCount() == page)
_cursor->insertText("\n");
}
} while (epub_it_get_next(it));
epub_free_iterator(it);
- mTextDocument->setCurrentSubDocument(QString());
// handle toc
struct titerator *tit;
diff --git a/generators/epub/epubdocument.cpp b/generators/epub/epubdocument.cpp
index ae19f8aee..d4a3f534b 100644
--- a/generators/epub/epubdocument.cpp
+++ b/generators/epub/epubdocument.cpp
@@ -23,9 +23,12 @@ QString resourceUrl(const KUrl &baseUrl, const QString &u)
}
-EpubDocument::EpubDocument(const QString &fileName) : QTextDocument()
+EpubDocument::EpubDocument(const QString &fileName) : QTextDocument(),
+ padding(20)
{
mEpub = epub_open(qPrintable(fileName), 3);
+
+ setPageSize(QSizeF(600, 800));
}
bool EpubDocument::isValid()
diff --git a/generators/epub/epubdocument.h b/generators/epub/epubdocument.h
index 714ede60b..1a9b328e4 100644
--- a/generators/epub/epubdocument.h
+++ b/generators/epub/epubdocument.h
@@ -34,6 +34,10 @@ namespace Epub {
private:
struct epub *mEpub;
KUrl mCurrentSubDocument;
+
+ int padding;
+
+ friend class Converter;
};
}