add SVG support

This commit is contained in:
Jaydeep Solanki 2013-10-22 20:19:41 +05:30 committed by Albert Astals Cid
parent 6b3ac2e1b0
commit ed0d146457
4 changed files with 45 additions and 1 deletions

View file

@ -14,7 +14,7 @@ set(okularGenerator_epub_PART_SRCS
kde4_add_plugin(okularGenerator_epub ${okularGenerator_epub_PART_SRCS})
target_link_libraries(okularGenerator_epub okularcore ${EPUB_LIBRARIES} ${KDE4_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} ${QT_QTGUI_LIBRARY})
target_link_libraries(okularGenerator_epub okularcore ${EPUB_LIBRARIES} ${KDE4_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} ${QT_QTGUI_LIBRARY} ${QT_QTXML_LIBRARY})
install(TARGETS okularGenerator_epub DESTINATION ${PLUGIN_INSTALL_DIR})

View file

@ -180,6 +180,38 @@ QTextDocument* Converter::convert( const QString &fileName )
htmlContent.replace(QRegExp("< *section"),"<p");
htmlContent.replace(QRegExp("< */ *section"),"</p");
// convert svg tags to img
const int maxHeight = mTextDocument->maxContentHeight();
const int maxWidth = mTextDocument->maxContentWidth();
QDomDocument dom;
if(dom.setContent(htmlContent)) {
QDomNodeList svgs = dom.elementsByTagName("svg");
if(!svgs.isEmpty()) {
QList< QDomNode > imgNodes;
for (uint i = 0; i < svgs.length(); ++i) {
QDomNodeList images = svgs.at(i).toElement().elementsByTagName("image");
for (uint j = 0; j < images.length(); ++j) {
QString lnk = images.at(i).toElement().attribute("xlink:href");
int ht = images.at(i).toElement().attribute("height").toInt();
int wd = images.at(i).toElement().attribute("width").toInt();
QImage img = mTextDocument->loadResource(QTextDocument::ImageResource,QUrl(lnk)).value<QImage>();
if(ht == 0) ht = img.height();
if(wd == 0) wd = img.width();
if(ht > maxHeight) ht = maxHeight;
if(wd > maxWidth) wd = maxWidth;
mTextDocument->addResource(QTextDocument::ImageResource,QUrl(lnk),img);
QDomDocument newDoc;
newDoc.setContent(QString("<img src=\"%1\" height=\"%2\" width=\"%3\" />").arg(lnk).arg(ht).arg(wd));
imgNodes.append(newDoc.documentElement());
}
foreach (QDomNode nd, imgNodes) {
svgs.at(i).parentNode().replaceChild(nd,svgs.at(i));
}
}
htmlContent = dom.toString();
}
}
QTextBlock before;
if(firstPage) {
// preHtml & postHtml make it possible to have a margin around the content of the page

View file

@ -54,6 +54,16 @@ void EpubDocument::setCurrentSubDocument(const QString &doc)
mCurrentSubDocument = KUrl::fromPath("/" + doc);
}
int EpubDocument::maxContentHeight() const
{
return pageSize().height() - (2 * padding);
}
int EpubDocument::maxContentWidth() const
{
return pageSize().width() - (2 * padding);
}
QVariant EpubDocument::loadResource(int type, const QUrl &name)
{
int size;

View file

@ -27,6 +27,8 @@ namespace Epub {
~EpubDocument();
struct epub *getEpub();
void setCurrentSubDocument(const QString &doc);
int maxContentHeight() const;
int maxContentWidth() const;
protected:
virtual QVariant loadResource(int type, const QUrl &name);