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_
|
|
|
|
|
2018-08-31 09:23:45 +00:00
|
|
|
#include <QAbstractTextDocumentLayout>
|
|
|
|
#include <QTextBlock>
|
|
|
|
#include <QTextDocument>
|
2007-01-13 23:15:28 +00:00
|
|
|
|
2007-04-20 12:37:12 +00:00
|
|
|
#include "action.h"
|
2014-09-11 19:12:27 +00:00
|
|
|
#include "debug_p.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"
|
|
|
|
|
|
|
|
namespace Okular
|
2020-07-10 22:15:05 +00:00
|
|
|
{
|
2009-01-10 11:29:11 +00:00
|
|
|
namespace TextDocumentUtils
|
2020-07-10 22:15:05 +00:00
|
|
|
{
|
2007-01-13 23:15:28 +00:00
|
|
|
static void calculateBoundingRect(QTextDocument *document, int startPosition, int endPosition, QRectF &rect, int &page)
|
2020-07-10 22:15:05 +00:00
|
|
|
{
|
2007-01-13 23:15:28 +00:00
|
|
|
const QSizeF pageSize = document->pageSize();
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
const QTextBlock startBlock = document->findBlock(startPosition);
|
|
|
|
const QRectF startBoundingRect = document->documentLayout()->blockBoundingRect(startBlock);
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
const QTextBlock endBlock = document->findBlock(endPosition);
|
|
|
|
const QRectF endBoundingRect = document->documentLayout()->blockBoundingRect(endBlock);
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-01-16 18:29:52 +00:00
|
|
|
const QTextLayout *startLayout = startBlock.layout();
|
|
|
|
const 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;
|
2020-07-10 22:15:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-16 18:29:52 +00:00
|
|
|
const int startPos = startPosition - startBlock.position();
|
|
|
|
const int endPos = endPosition - endBlock.position();
|
2007-01-13 23:15:28 +00:00
|
|
|
const QTextLine startLine = startLayout->lineForTextPosition(startPos);
|
|
|
|
const QTextLine endLine = endLayout->lineForTextPosition(endPos);
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-01-16 18:29:52 +00:00
|
|
|
const double x = startBoundingRect.x() + startLine.cursorToX(startPos);
|
|
|
|
const double y = startBoundingRect.y() + startLine.y();
|
2007-01-13 23:15:28 +00:00
|
|
|
const double r = endBoundingRect.x() + endLine.cursorToX(endPos);
|
2019-01-16 18:29:52 +00:00
|
|
|
const double b = endBoundingRect.y() + endLine.y() + endLine.height();
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-01-16 18:29:52 +00:00
|
|
|
const int offset = qRound(y) % qRound(pageSize.height());
|
2020-07-10 22:15:05 +00:00
|
|
|
|
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;
|
2020-07-10 22:15:05 +00:00
|
|
|
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());
|
2020-07-10 22:15:05 +00:00
|
|
|
}
|
2007-01-13 23:15:28 +00:00
|
|
|
|
2019-01-28 21:08:24 +00:00
|
|
|
static QVector<QRectF> calculateBoundingRects(QTextDocument *document, int startPosition, int endPosition)
|
2020-07-10 22:15:05 +00:00
|
|
|
{
|
2019-01-28 21:08:24 +00:00
|
|
|
QVector<QRectF> result;
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-01-28 21:08:24 +00:00
|
|
|
const QSizeF pageSize = document->pageSize();
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-01-28 21:08:24 +00:00
|
|
|
const QTextBlock startBlock = document->findBlock(startPosition);
|
2019-01-16 18:29:52 +00:00
|
|
|
const QRectF startBoundingRect = document->documentLayout()->blockBoundingRect(startBlock);
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-01-28 21:08:24 +00:00
|
|
|
const QTextBlock endBlock = document->findBlock(endPosition);
|
|
|
|
const QRectF endBoundingRect = document->documentLayout()->blockBoundingRect(endBlock);
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-01-28 21:08:24 +00:00
|
|
|
const QTextLayout *startLayout = startBlock.layout();
|
|
|
|
const QTextLayout *endLayout = endBlock.layout();
|
|
|
|
if (!startLayout || !endLayout) {
|
|
|
|
qCWarning(OkularCoreDebug) << "Start or end layout not found" << startLayout << endLayout;
|
|
|
|
return {};
|
2020-07-10 22:15:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 18:29:52 +00:00
|
|
|
const int startPos = startPosition - startBlock.position();
|
2019-01-28 21:08:24 +00:00
|
|
|
const int endPos = endPosition - endBlock.position();
|
|
|
|
const QTextLine startLine = startLayout->lineForTextPosition(startPos);
|
|
|
|
const QTextLine endLine = endLayout->lineForTextPosition(endPos);
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-01-28 21:08:24 +00:00
|
|
|
// This only works if both start and end layout are the same
|
|
|
|
if (startLayout == endLayout) {
|
|
|
|
Q_ASSERT(startBoundingRect == endBoundingRect);
|
|
|
|
for (int i = startLine.lineNumber(); i < endLine.lineNumber(); ++i) {
|
|
|
|
const QTextLine line = startLayout->lineAt(i);
|
|
|
|
// using startPos and endPos is fine, if the pos is out of bounds for that line, it'll return beginning and end of line respectively
|
2019-01-16 18:29:52 +00:00
|
|
|
const double x = endBoundingRect.x() + line.cursorToX(startPos);
|
|
|
|
const double y = endBoundingRect.y() + line.y();
|
|
|
|
const double r = endBoundingRect.x() + line.cursorToX(endPos);
|
|
|
|
const double b = endBoundingRect.y() + line.y() + endLine.height();
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-01-16 18:29:52 +00:00
|
|
|
result.append(QRectF(x / pageSize.width(), y / pageSize.height(), (r - x) / pageSize.width(), (b - y) / pageSize.height()));
|
2020-07-10 22:15:05 +00:00
|
|
|
}
|
2007-01-13 23:15:28 +00:00
|
|
|
|
2019-01-16 18:29:52 +00:00
|
|
|
// The last line
|
2019-01-28 21:08:24 +00:00
|
|
|
const double x = endBoundingRect.x() + endLine.cursorToX(startPos);
|
2019-01-16 18:29:52 +00:00
|
|
|
const double y = endBoundingRect.y() + endLine.y();
|
2019-01-28 21:08:24 +00:00
|
|
|
const double r = endBoundingRect.x() + endLine.cursorToX(endPos);
|
2019-01-16 18:29:52 +00:00
|
|
|
const double b = endBoundingRect.y() + endLine.y() + endLine.height();
|
2007-01-13 23:15:28 +00:00
|
|
|
|
2007-05-12 08:17:02 +00:00
|
|
|
result.append(QRectF(x / pageSize.width(), y / pageSize.height(), (r - x) / pageSize.width(), (b - y) / pageSize.height()));
|
2020-07-10 22:15:05 +00:00
|
|
|
} else {
|
2007-05-12 08:17:02 +00:00
|
|
|
const double x = startBoundingRect.x() + startLine.cursorToX(startPos);
|
|
|
|
const double y = startBoundingRect.y() + startLine.y();
|
|
|
|
const double r = endBoundingRect.x() + endLine.cursorToX(endPos);
|
2019-01-28 21:08:24 +00:00
|
|
|
const double b = endBoundingRect.y() + endLine.y() + endLine.height();
|
2007-05-12 08:17:02 +00:00
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
result.append(QRectF(x / pageSize.width(), y / pageSize.height(), (r - x) / pageSize.width(), (b - y) / pageSize.height()));
|
|
|
|
}
|
|
|
|
|
2019-01-28 21:08:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
static void calculatePositions(QTextDocument *document, int page, int &start, int &end)
|
|
|
|
{
|
|
|
|
const QAbstractTextDocumentLayout *layout = document->documentLayout();
|
|
|
|
const QSizeF pageSize = document->pageSize();
|
2019-01-16 18:29:52 +00:00
|
|
|
const double margin = document->rootFrame()->frameFormat().margin();
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2019-01-16 18:29:52 +00:00
|
|
|
const int page = qRound(rect.y()) / qRound(pageSize.height());
|
|
|
|
const int offset = qRound(rect.y()) % qRound(pageSize.height());
|
2007-01-13 23:15:28 +00:00
|
|
|
|
|
|
|
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()
|
2017-09-05 21:27:18 +00:00
|
|
|
: mParent(nullptr)
|
2008-02-14 23:19:24 +00:00
|
|
|
{
|
|
|
|
}
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2008-02-14 23:19:24 +00:00
|
|
|
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:
|
2018-09-01 08:25:57 +00:00
|
|
|
explicit TextDocumentGeneratorPrivate(TextDocumentConverter *converter)
|
2017-09-05 21:27:18 +00:00
|
|
|
: mConverter(converter)
|
|
|
|
, mDocument(nullptr)
|
|
|
|
, mGeneralSettings(nullptr)
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
}
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-12-18 11:51:13 +00:00
|
|
|
~TextDocumentGeneratorPrivate() override
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
delete mConverter;
|
|
|
|
delete mDocument;
|
|
|
|
}
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2013-05-18 11:57:46 +00:00
|
|
|
void initializeGenerator();
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-01-16 18:29:52 +00:00
|
|
|
struct LinkInfo {
|
|
|
|
int page;
|
|
|
|
QRectF boundingRect;
|
|
|
|
Action *link;
|
2019-01-28 21:08:24 +00:00
|
|
|
bool ownsLink;
|
2019-01-16 18:29:52 +00:00
|
|
|
};
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-01-16 18:29:52 +00:00
|
|
|
struct AnnotationInfo {
|
|
|
|
int page;
|
|
|
|
QRectF boundingRect;
|
|
|
|
Annotation *annotation;
|
|
|
|
};
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2008-02-14 23:19:24 +00:00
|
|
|
Q_DECLARE_PUBLIC(TextDocumentGenerator)
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2017-03-02 19:38:50 +00:00
|
|
|
/* reimp */ QVariant metaData(const QString &key, const QVariant &option) const override;
|
|
|
|
/* reimp */ QImage image(PixmapRequest *) override;
|
2020-07-10 22:15:05 +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;
|
2020-07-10 22:15:05 +00:00
|
|
|
|
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);
|
2019-12-20 15:40:59 +00:00
|
|
|
void addTitle(int level, const QString &title, const QTextBlock &block);
|
2007-01-13 23:15:28 +00:00
|
|
|
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);
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2019-01-16 18:29:52 +00:00
|
|
|
QList<LinkInfo> generateLinkInfos() const;
|
|
|
|
QList<AnnotationInfo> generateAnnotationInfos() const;
|
2007-01-13 23:15:28 +00:00
|
|
|
void generateTitleInfos();
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
TextDocumentConverter *mConverter;
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
QTextDocument *mDocument;
|
|
|
|
Okular::DocumentInfo mDocumentInfo;
|
|
|
|
Okular::DocumentSynopsis mDocumentSynopsis;
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
struct TitlePosition {
|
|
|
|
int level;
|
|
|
|
QString title;
|
|
|
|
QTextBlock block;
|
|
|
|
};
|
|
|
|
QList<TitlePosition> mTitlePositions;
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
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;
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
struct AnnotationPosition {
|
|
|
|
int startPosition;
|
|
|
|
int endPosition;
|
|
|
|
Annotation *annotation;
|
|
|
|
};
|
|
|
|
QList<AnnotationPosition> mAnnotationPositions;
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2013-05-18 11:57:46 +00:00
|
|
|
TextDocumentSettings *mGeneralSettings;
|
2020-07-10 22:15:05 +00:00
|
|
|
|
2013-05-18 11:57:46 +00:00
|
|
|
QFont mFont;
|
2007-01-13 23:15:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|