2007-01-13 23:15:28 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2007 by Tobias Koenig <tokoe@kde.org> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#ifndef _OKULAR_TEXTDOCUMENTGENERATOR_P_H_
|
|
|
|
#define _OKULAR_TEXTDOCUMENTGENERATOR_P_H_
|
|
|
|
|
2007-04-19 18:30:20 +00:00
|
|
|
#include <QtGui/QAbstractTextDocumentLayout>
|
2007-01-13 23:15:28 +00:00
|
|
|
#include <QtGui/QTextBlock>
|
2007-04-19 18:30:20 +00:00
|
|
|
#include <QtGui/QTextDocument>
|
2007-01-13 23:15:28 +00:00
|
|
|
|
2007-04-20 12:37:12 +00:00
|
|
|
#include "action.h"
|
2007-01-13 23:15:28 +00:00
|
|
|
#include "document.h"
|
2007-09-14 13:31:55 +00:00
|
|
|
#include "generator_p.h"
|
2007-01-13 23:15:28 +00:00
|
|
|
#include "textdocumentgenerator.h"
|
2014-09-11 19:12:27 +00:00
|
|
|
#include "debug_p.h"
|
2007-01-13 23:15:28 +00:00
|
|
|
|
|
|
|
namespace Okular {
|
|
|
|
|
2009-01-10 11:29:11 +00:00
|
|
|
namespace TextDocumentUtils {
|
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
static void calculateBoundingRect( QTextDocument *document, int startPosition, int endPosition,
|
|
|
|
QRectF &rect, int &page )
|
|
|
|
{
|
|
|
|
const QSizeF pageSize = document->pageSize();
|
|
|
|
|
|
|
|
const QTextBlock startBlock = document->findBlock( startPosition );
|
|
|
|
const QRectF startBoundingRect = document->documentLayout()->blockBoundingRect( startBlock );
|
|
|
|
|
|
|
|
const QTextBlock endBlock = document->findBlock( endPosition );
|
|
|
|
const QRectF endBoundingRect = document->documentLayout()->blockBoundingRect( endBlock );
|
|
|
|
|
|
|
|
QTextLayout *startLayout = startBlock.layout();
|
|
|
|
QTextLayout *endLayout = endBlock.layout();
|
2012-10-14 16:56:18 +00:00
|
|
|
if (!startLayout || !endLayout) {
|
2014-09-11 19:12:27 +00:00
|
|
|
qCWarning(OkularCoreDebug) << "Start or end layout not found" << startLayout << endLayout;
|
2012-10-14 16:56:18 +00:00
|
|
|
page = -1;
|
|
|
|
return;
|
|
|
|
}
|
2007-01-13 23:15:28 +00:00
|
|
|
|
|
|
|
int startPos = startPosition - startBlock.position();
|
|
|
|
int endPos = endPosition - endBlock.position();
|
|
|
|
const QTextLine startLine = startLayout->lineForTextPosition( startPos );
|
|
|
|
const QTextLine endLine = endLayout->lineForTextPosition( endPos );
|
|
|
|
|
|
|
|
double x = startBoundingRect.x() + startLine.cursorToX( startPos );
|
|
|
|
double y = startBoundingRect.y() + startLine.y();
|
|
|
|
double r = endBoundingRect.x() + endLine.cursorToX( endPos );
|
|
|
|
double b = endBoundingRect.y() + endLine.y() + endLine.height();
|
|
|
|
|
|
|
|
int offset = qRound( y ) % qRound( pageSize.height() );
|
|
|
|
|
2007-05-12 08:17:02 +00:00
|
|
|
if ( x > r ) { // line break, so return a pseudo character on the start line
|
|
|
|
rect = QRectF( x / pageSize.width(), offset / pageSize.height(),
|
|
|
|
3 / pageSize.width(), startLine.height() / pageSize.height() );
|
|
|
|
page = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
page = qRound( y ) / qRound( pageSize.height() );
|
|
|
|
rect = QRectF( x / pageSize.width(), offset / pageSize.height(),
|
|
|
|
(r - x) / pageSize.width(), (b - y) / pageSize.height() );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void calculatePositions( QTextDocument *document, int page, int &start, int &end )
|
|
|
|
{
|
|
|
|
const QAbstractTextDocumentLayout *layout = document->documentLayout();
|
|
|
|
const QSizeF pageSize = document->pageSize();
|
|
|
|
double margin = document->rootFrame()->frameFormat().margin();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take the upper left and lower left corner including the margin
|
|
|
|
*/
|
|
|
|
start = layout->hitTest( QPointF( margin, (page * pageSize.height()) + margin ), Qt::FuzzyHit );
|
|
|
|
end = layout->hitTest( QPointF( margin, ((page + 1) * pageSize.height()) - margin ), Qt::FuzzyHit );
|
|
|
|
}
|
|
|
|
|
|
|
|
static Okular::DocumentViewport calculateViewport( QTextDocument *document, const QTextBlock &block )
|
|
|
|
{
|
|
|
|
const QSizeF pageSize = document->pageSize();
|
|
|
|
const QRectF rect = document->documentLayout()->blockBoundingRect( block );
|
|
|
|
|
|
|
|
int page = qRound( rect.y() ) / qRound( pageSize.height() );
|
|
|
|
int offset = qRound( rect.y() ) % qRound( pageSize.height() );
|
|
|
|
|
|
|
|
Okular::DocumentViewport viewport( page );
|
|
|
|
viewport.rePos.normalizedX = (double)rect.x() / (double)pageSize.width();
|
|
|
|
viewport.rePos.normalizedY = (double)offset / (double)pageSize.height();
|
|
|
|
viewport.rePos.enabled = true;
|
|
|
|
viewport.rePos.pos = Okular::DocumentViewport::Center;
|
|
|
|
|
|
|
|
return viewport;
|
|
|
|
}
|
2009-01-10 11:48:04 +00:00
|
|
|
}
|
2007-01-13 23:15:28 +00:00
|
|
|
|
2007-12-08 14:45:32 +00:00
|
|
|
class TextDocumentConverterPrivate
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
2008-02-14 23:19:24 +00:00
|
|
|
public:
|
|
|
|
TextDocumentConverterPrivate()
|
|
|
|
: mParent( 0 )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TextDocumentGeneratorPrivate *mParent;
|
2014-05-10 13:31:59 +00:00
|
|
|
QTextDocument *mDocument;
|
2007-01-13 23:15:28 +00:00
|
|
|
};
|
|
|
|
|
2007-09-14 13:31:55 +00:00
|
|
|
class TextDocumentGeneratorPrivate : public GeneratorPrivate
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
friend class TextDocumentConverter;
|
|
|
|
|
|
|
|
public:
|
2007-09-14 13:31:55 +00:00
|
|
|
TextDocumentGeneratorPrivate( TextDocumentConverter *converter )
|
2013-06-06 21:50:09 +00:00
|
|
|
: mConverter( converter ), mDocument( 0 ), mGeneralSettings( 0 )
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-09-14 13:31:55 +00:00
|
|
|
virtual ~TextDocumentGeneratorPrivate()
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
delete mConverter;
|
|
|
|
delete mDocument;
|
|
|
|
}
|
|
|
|
|
2013-05-18 11:57:46 +00:00
|
|
|
void initializeGenerator();
|
|
|
|
|
2008-02-14 23:19:24 +00:00
|
|
|
Q_DECLARE_PUBLIC( TextDocumentGenerator )
|
|
|
|
|
2017-03-02 19:38:50 +00:00
|
|
|
/* reimp */ QVariant metaData( const QString &key, const QVariant &option ) const override;
|
|
|
|
/* reimp */ QImage image( PixmapRequest * ) override;
|
2008-03-15 02:15:16 +00:00
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
void calculateBoundingRect( int startPosition, int endPosition, QRectF &rect, int &page ) const;
|
|
|
|
void calculatePositions( int page, int &start, int &end ) const;
|
|
|
|
Okular::TextPage* createTextPage( int ) const;
|
|
|
|
|
2007-04-20 12:49:17 +00:00
|
|
|
void addAction( Action *action, int cursorBegin, int cursorEnd );
|
2007-01-13 23:15:28 +00:00
|
|
|
void addAnnotation( Annotation *annotation, int cursorBegin, int cursorEnd );
|
|
|
|
void addTitle( int level, const QString &title, const QTextBlock &position );
|
|
|
|
void addMetaData( const QString &key, const QString &value, const QString &title );
|
2008-02-08 14:13:55 +00:00
|
|
|
void addMetaData( DocumentInfo::Key, const QString &value );
|
2007-01-13 23:15:28 +00:00
|
|
|
|
|
|
|
void generateLinkInfos();
|
|
|
|
void generateAnnotationInfos();
|
|
|
|
void generateTitleInfos();
|
|
|
|
|
|
|
|
TextDocumentConverter *mConverter;
|
|
|
|
|
|
|
|
QTextDocument *mDocument;
|
|
|
|
Okular::DocumentInfo mDocumentInfo;
|
|
|
|
Okular::DocumentSynopsis mDocumentSynopsis;
|
|
|
|
|
|
|
|
struct TitlePosition
|
|
|
|
{
|
|
|
|
int level;
|
|
|
|
QString title;
|
|
|
|
QTextBlock block;
|
|
|
|
};
|
|
|
|
QList<TitlePosition> mTitlePositions;
|
|
|
|
|
|
|
|
struct LinkPosition
|
|
|
|
{
|
|
|
|
int startPosition;
|
|
|
|
int endPosition;
|
2007-04-20 12:37:12 +00:00
|
|
|
Action *link;
|
2007-01-13 23:15:28 +00:00
|
|
|
};
|
|
|
|
QList<LinkPosition> mLinkPositions;
|
|
|
|
|
|
|
|
struct LinkInfo
|
|
|
|
{
|
|
|
|
int page;
|
|
|
|
QRectF boundingRect;
|
2007-04-20 12:37:12 +00:00
|
|
|
Action *link;
|
2007-01-13 23:15:28 +00:00
|
|
|
};
|
|
|
|
QList<LinkInfo> mLinkInfos;
|
|
|
|
|
|
|
|
struct AnnotationPosition
|
|
|
|
{
|
|
|
|
int startPosition;
|
|
|
|
int endPosition;
|
|
|
|
Annotation *annotation;
|
|
|
|
};
|
|
|
|
QList<AnnotationPosition> mAnnotationPositions;
|
|
|
|
|
|
|
|
struct AnnotationInfo
|
|
|
|
{
|
|
|
|
int page;
|
|
|
|
QRectF boundingRect;
|
|
|
|
Annotation *annotation;
|
|
|
|
};
|
|
|
|
QList<AnnotationInfo> mAnnotationInfos;
|
2013-05-18 11:57:46 +00:00
|
|
|
|
|
|
|
TextDocumentSettings *mGeneralSettings;
|
|
|
|
|
|
|
|
QFont mFont;
|
2007-01-13 23:15:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|