1 /***************************************************************************
2 * Copyright (C) 2007 by Tobias Koenig <tokoe@kde.org> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 ***************************************************************************/
10 #ifndef _OKULAR_TEXTDOCUMENTGENERATOR_P_H_
11 #define _OKULAR_TEXTDOCUMENTGENERATOR_P_H_
13 #include <QtGui/QAbstractTextDocumentLayout>
14 #include <QtGui/QTextBlock>
15 #include <QtGui/QTextDocument>
19 #include "generator_p.h"
20 #include "textdocumentgenerator.h"
27 static void calculateBoundingRect( QTextDocument
*document
, int startPosition
, int endPosition
,
28 QRectF
&rect
, int &page
)
30 const QSizeF pageSize
= document
->pageSize();
32 const QTextBlock startBlock
= document
->findBlock( startPosition
);
33 const QRectF startBoundingRect
= document
->documentLayout()->blockBoundingRect( startBlock
);
35 const QTextBlock endBlock
= document
->findBlock( endPosition
);
36 const QRectF endBoundingRect
= document
->documentLayout()->blockBoundingRect( endBlock
);
38 QTextLayout
*startLayout
= startBlock
.layout();
39 QTextLayout
*endLayout
= endBlock
.layout();
41 int startPos
= startPosition
- startBlock
.position();
42 int endPos
= endPosition
- endBlock
.position();
43 const QTextLine startLine
= startLayout
->lineForTextPosition( startPos
);
44 const QTextLine endLine
= endLayout
->lineForTextPosition( endPos
);
46 double x
= startBoundingRect
.x() + startLine
.cursorToX( startPos
);
47 double y
= startBoundingRect
.y() + startLine
.y();
48 double r
= endBoundingRect
.x() + endLine
.cursorToX( endPos
);
49 double b
= endBoundingRect
.y() + endLine
.y() + endLine
.height();
51 int offset
= qRound( y
) % qRound( pageSize
.height() );
53 if ( x
> r
) { // line break, so return a pseudo character on the start line
54 rect
= QRectF( x
/ pageSize
.width(), offset
/ pageSize
.height(),
55 3 / pageSize
.width(), startLine
.height() / pageSize
.height() );
60 page
= qRound( y
) / qRound( pageSize
.height() );
61 rect
= QRectF( x
/ pageSize
.width(), offset
/ pageSize
.height(),
62 (r
- x
) / pageSize
.width(), (b
- y
) / pageSize
.height() );
65 static void calculatePositions( QTextDocument
*document
, int page
, int &start
, int &end
)
67 const QAbstractTextDocumentLayout
*layout
= document
->documentLayout();
68 const QSizeF pageSize
= document
->pageSize();
69 double margin
= document
->rootFrame()->frameFormat().margin();
72 * Take the upper left and lower left corner including the margin
74 start
= layout
->hitTest( QPointF( margin
, (page
* pageSize
.height()) + margin
), Qt::FuzzyHit
);
75 end
= layout
->hitTest( QPointF( margin
, ((page
+ 1) * pageSize
.height()) - margin
), Qt::FuzzyHit
);
78 static Okular::DocumentViewport
calculateViewport( QTextDocument
*document
, const QTextBlock
&block
)
80 const QSizeF pageSize
= document
->pageSize();
81 const QRectF rect
= document
->documentLayout()->blockBoundingRect( block
);
83 int page
= qRound( rect
.y() ) / qRound( pageSize
.height() );
84 int offset
= qRound( rect
.y() ) % qRound( pageSize
.height() );
86 Okular::DocumentViewport
viewport( page
);
87 viewport
.rePos
.normalizedX
= (double)rect
.x() / (double)pageSize
.width();
88 viewport
.rePos
.normalizedY
= (double)offset
/ (double)pageSize
.height();
89 viewport
.rePos
.enabled
= true;
90 viewport
.rePos
.pos
= Okular::DocumentViewport::Center
;
96 class TextDocumentConverterPrivate
99 TextDocumentConverterPrivate()
104 TextDocumentGeneratorPrivate
*mParent
;
107 class TextDocumentGeneratorPrivate
: public GeneratorPrivate
109 friend class TextDocumentConverter
;
112 TextDocumentGeneratorPrivate( TextDocumentConverter
*converter
)
113 : mConverter( converter
), mDocument( 0 )
117 virtual ~TextDocumentGeneratorPrivate()
123 Q_DECLARE_PUBLIC( TextDocumentGenerator
)
125 /* reimp */ QVariant
metaData( const QString
&key
, const QVariant
&option
) const;
126 /* reimp */ QImage
image( PixmapRequest
* );
128 void calculateBoundingRect( int startPosition
, int endPosition
, QRectF
&rect
, int &page
) const;
129 void calculatePositions( int page
, int &start
, int &end
) const;
130 Okular::TextPage
* createTextPage( int ) const;
132 void addAction( Action
*action
, int cursorBegin
, int cursorEnd
);
133 void addAnnotation( Annotation
*annotation
, int cursorBegin
, int cursorEnd
);
134 void addTitle( int level
, const QString
&title
, const QTextBlock
&position
);
135 void addMetaData( const QString
&key
, const QString
&value
, const QString
&title
);
136 void addMetaData( DocumentInfo::Key
, const QString
&value
);
138 void generateLinkInfos();
139 void generateAnnotationInfos();
140 void generateTitleInfos();
142 TextDocumentConverter
*mConverter
;
144 QTextDocument
*mDocument
;
145 Okular::DocumentInfo mDocumentInfo
;
146 Okular::DocumentSynopsis mDocumentSynopsis
;
154 QList
<TitlePosition
> mTitlePositions
;
162 QList
<LinkPosition
> mLinkPositions
;
170 QList
<LinkInfo
> mLinkInfos
;
172 struct AnnotationPosition
176 Annotation
*annotation
;
178 QList
<AnnotationPosition
> mAnnotationPositions
;
180 struct AnnotationInfo
184 Annotation
*annotation
;
186 QList
<AnnotationInfo
> mAnnotationInfos
;