mirror of
https://invent.kde.org/graphics/okular
synced 2024-09-13 13:11:41 +00:00
epub video support
This commit is contained in:
parent
9b673d04f1
commit
e9ccc4867b
|
@ -318,9 +318,6 @@ bool TextDocumentGenerator::loadDocument( const QString & fileName, QVector<Okul
|
|||
QVector< QLinkedList<Okular::Annotation*> > annots( d->mDocument->pageCount() );
|
||||
for ( int i = 0; i < d->mAnnotationInfos.count(); ++i ) {
|
||||
const TextDocumentGeneratorPrivate::AnnotationInfo &info = d->mAnnotationInfos[ i ];
|
||||
|
||||
QRect rect( 0, info.page * size.height(), size.width(), size.height() );
|
||||
info.annotation->setBoundingRectangle( Okular::NormalizedRect( rect.left(), rect.top(), rect.right(), rect.bottom() ) );
|
||||
annots[ info.page ].append( info.annotation );
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
|
||||
add_subdirectory(data)
|
||||
|
||||
include_directories(
|
||||
${EPUB_INCLUDE_DIR}
|
||||
)
|
||||
|
|
|
@ -17,7 +17,11 @@
|
|||
|
||||
#include <kdebug.h>
|
||||
#include <klocale.h>
|
||||
#include <KStandardDirs>
|
||||
|
||||
#include <core/action.h>
|
||||
#include <core/movie.h>
|
||||
#include <core/annotations.h>
|
||||
|
||||
using namespace Epub;
|
||||
|
||||
|
@ -135,6 +139,28 @@ void Converter::_insert_local_links(const QString &key, const QPair<int, int> &v
|
|||
}
|
||||
}
|
||||
|
||||
static QPoint calculateXYPosition( QTextDocument *document, int startPosition )
|
||||
{
|
||||
const QTextBlock startBlock = document->findBlock( startPosition );
|
||||
const QRectF startBoundingRect = document->documentLayout()->blockBoundingRect( startBlock );
|
||||
|
||||
QTextLayout *startLayout = startBlock.layout();
|
||||
if (!startLayout) {
|
||||
kWarning() << "Start layout not found" << startLayout;
|
||||
return QPoint();
|
||||
}
|
||||
|
||||
int startPos = startPosition - startBlock.position();
|
||||
const QTextLine startLine = startLayout->lineForTextPosition( startPos );
|
||||
|
||||
double x = startBoundingRect.x() ;
|
||||
double y = startBoundingRect.y() + startLine.y();
|
||||
|
||||
y = (int)y % 800;
|
||||
|
||||
return QPoint(x,y);
|
||||
}
|
||||
|
||||
QTextDocument* Converter::convert( const QString &fileName )
|
||||
{
|
||||
EpubDocument *newDocument = new EpubDocument(fileName);
|
||||
|
@ -170,7 +196,10 @@ QTextDocument* Converter::convert( const QString &fileName )
|
|||
|
||||
// if the background color of the document is non-white it will be handled by QTextDocument::setHtml()
|
||||
bool firstPage = true;
|
||||
QVector<Okular::MovieAnnotation *> movieAnnots;
|
||||
const QSize videoSize(320, 240);
|
||||
do{
|
||||
movieAnnots.clear();
|
||||
if(epub_it_get_curr(it)) {
|
||||
const QString link = QString::fromUtf8(epub_it_get_curr_url(it));
|
||||
mTextDocument->setCurrentSubDocument(link);
|
||||
|
@ -208,8 +237,32 @@ QTextDocument* Converter::convert( const QString &fileName )
|
|||
svgs.at(i).parentNode().replaceChild(nd,svgs.at(i));
|
||||
}
|
||||
}
|
||||
htmlContent = dom.toString();
|
||||
}
|
||||
|
||||
// handle embedded videos
|
||||
QDomNodeList videoTags = dom.elementsByTagName("video");
|
||||
if(!videoTags.isEmpty()) {
|
||||
for (int i = 0; i < videoTags.size(); ++i) {
|
||||
QDomNodeList sourceTags = videoTags.at(i).toElement().elementsByTagName("source");
|
||||
if(!sourceTags.isEmpty()) {
|
||||
QString lnk = sourceTags.at(0).toElement().attribute("src");
|
||||
|
||||
Okular::Movie *movie = new Okular::Movie(mTextDocument->loadResource(EpubDocument::MovieResource,QUrl(lnk)).toString());
|
||||
movie->setSize(videoSize);
|
||||
movie->setShowControls(true);
|
||||
|
||||
Okular::MovieAnnotation *annot = new Okular::MovieAnnotation;
|
||||
annot->setMovie(movie);
|
||||
|
||||
movieAnnots.push_back(annot);
|
||||
QDomDocument tempDoc;
|
||||
tempDoc.setContent(QString("<pre><video></video></pre>"));
|
||||
qDebug() << "replacing";
|
||||
videoTags.at(i).parentNode().replaceChild(tempDoc.documentElement(),videoTags.at(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
htmlContent = dom.toString();
|
||||
}
|
||||
|
||||
QTextBlock before;
|
||||
|
@ -228,6 +281,22 @@ QTextDocument* Converter::convert( const QString &fileName )
|
|||
_cursor->insertHtml(htmlContent);
|
||||
}
|
||||
|
||||
QTextCursor csr(mTextDocument); // a temporary cursor
|
||||
csr.movePosition(QTextCursor::Start);
|
||||
int index = 0;
|
||||
while( !(csr = mTextDocument->find("<video></video>",csr)).isNull() ) {
|
||||
const int posStart = csr.position();
|
||||
const QPoint startPoint = calculateXYPosition(mTextDocument, posStart);
|
||||
QImage img(KStandardDirs::locate("data", "okular/pics/okular-epub-movie.png"));
|
||||
img = img.scaled(videoSize);
|
||||
csr.insertImage(img);
|
||||
const int posEnd = csr.position();
|
||||
const QRect videoRect(startPoint,videoSize);
|
||||
movieAnnots[index]->setBoundingRectangle(Okular::NormalizedRect(videoRect,mTextDocument->pageSize().width(), mTextDocument->pageSize().height()));
|
||||
emit addAnnotation(movieAnnots[index++],posStart,posEnd);
|
||||
csr.movePosition(QTextCursor::NextWord);
|
||||
}
|
||||
|
||||
mSectionMap.insert(link, before);
|
||||
|
||||
_handle_anchors(before, link);
|
||||
|
|
4
generators/epub/data/CMakeLists.txt
Normal file
4
generators/epub/data/CMakeLists.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
|
||||
install(FILES
|
||||
okular-epub-movie.png
|
||||
DESTINATION ${DATA_INSTALL_DIR}/okular/pics)
|
BIN
generators/epub/data/okular-epub-movie.png
Normal file
BIN
generators/epub/data/okular-epub-movie.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
|
@ -8,6 +8,10 @@
|
|||
***************************************************************************/
|
||||
|
||||
#include "epubdocument.h"
|
||||
#include <QTemporaryFile>
|
||||
#include <QDir>
|
||||
|
||||
#include <KDebug>
|
||||
|
||||
#include <QRegExp>
|
||||
|
||||
|
@ -84,16 +88,31 @@ QVariant EpubDocument::loadResource(int type, const QUrl &name)
|
|||
|
||||
if (data) {
|
||||
switch(type) {
|
||||
case QTextDocument::ImageResource:
|
||||
resource.setValue(QImage::fromData((unsigned char *)data, size));
|
||||
case QTextDocument::ImageResource:{
|
||||
QImage img = QImage::fromData((unsigned char *)data, size);
|
||||
const int maxHeight = maxContentHeight();
|
||||
const int maxWidth = maxContentWidth();
|
||||
if(img.height() > maxHeight)
|
||||
img = img.scaledToHeight(maxHeight);
|
||||
if(img.width() > maxWidth)
|
||||
img = img.scaledToWidth(maxWidth);
|
||||
resource.setValue(img);
|
||||
break;
|
||||
|
||||
}
|
||||
case QTextDocument::StyleSheetResource: {
|
||||
QString css = QString::fromUtf8(data);
|
||||
checkCSS(css);
|
||||
resource.setValue(css);
|
||||
break;
|
||||
}
|
||||
case EpubDocument::MovieResource: {
|
||||
QTemporaryFile *tmp = new QTemporaryFile(QString("%1/okrXXXXXX").arg(QDir::tempPath()),this);
|
||||
if(!tmp->open()) kWarning() << "EPUB : error creating temporary video file";
|
||||
if(tmp->write(data,size) == -1) kWarning() << "EPUB : error writing data" << tmp->errorString();
|
||||
tmp->flush();
|
||||
resource.setValue(tmp->fileName());
|
||||
break;
|
||||
}
|
||||
default:
|
||||
resource.setValue(QString::fromUtf8(data));
|
||||
break;
|
||||
|
|
|
@ -29,6 +29,7 @@ namespace Epub {
|
|||
void setCurrentSubDocument(const QString &doc);
|
||||
int maxContentHeight() const;
|
||||
int maxContentWidth() const;
|
||||
enum Multimedia { MovieResource = 4 };
|
||||
|
||||
protected:
|
||||
virtual QVariant loadResource(int type, const QUrl &name);
|
||||
|
|
Loading…
Reference in a new issue