Changeset View
Changeset View
Standalone View
Standalone View
ui/pagepainter.cpp
Show First 20 Lines • Show All 46 Lines • ▼ Show 20 Line(s) | 45 | QPen p( | |||
---|---|---|---|---|---|
47 | width, | 47 | width, | ||
48 | ann->style().lineStyle() == Okular::Annotation::Dashed ? Qt::DashLine : Qt::SolidLine, | 48 | ann->style().lineStyle() == Okular::Annotation::Dashed ? Qt::DashLine : Qt::SolidLine, | ||
49 | Qt::SquareCap, | 49 | Qt::SquareCap, | ||
50 | Qt::MiterJoin | 50 | Qt::MiterJoin | ||
51 | ); | 51 | ); | ||
52 | return p; | 52 | return p; | ||
53 | } | 53 | } | ||
54 | 54 | | |||
55 | class LineAnnotPainter { | ||||
56 | public: | ||||
57 | LineAnnotPainter( const Okular::LineAnnotation * a, const Okular::Page * page, double pageScale, const std::vector<double> &&paintMatrix ) | ||||
58 | : la { a } | ||||
59 | , linePen { buildPen( a, a->style().width(), a->style().color() ) } | ||||
60 | , pageWidth { page->width() } | ||||
61 | , pageHeight { page->height() } | ||||
62 | , aspectRatio { page->height() / page->width() } | ||||
63 | , pageScale { pageScale } | ||||
64 | , paintMatrix { paintMatrix } | ||||
65 | { | ||||
66 | if ( ( la->lineClosed() || la->transformedLinePoints().count() == 2 ) && | ||||
67 | la->lineInnerColor().isValid() ) | ||||
68 | { | ||||
69 | fillBrush = QBrush( la->lineInnerColor() ); | ||||
70 | } | ||||
71 | } | ||||
72 | | ||||
73 | void draw( QImage &backImage ) | ||||
74 | { | ||||
75 | const Okular::NormalizedPoint delta { | ||||
76 | la->transformedLinePoints().last().x - la->transformedLinePoints().first().x, | ||||
77 | la->transformedLinePoints().first().y - la->transformedLinePoints().last().y | ||||
78 | }; | ||||
79 | const double deaspectedY = delta.y * aspectRatio; | ||||
80 | const double angle = atan2( delta.y * aspectRatio, delta.x ); | ||||
81 | const double cosA = cos( -angle ); | ||||
82 | const double sinA = sin( -angle ); | ||||
83 | | ||||
84 | /* tmpMtx allows us to construct line endings as if the main segment would be a simple horizontal line */ | ||||
85 | const std::vector<double> &tmpMtx = matrixMultiply( { | ||||
86 | cosA, sinA / aspectRatio, | ||||
87 | -sinA, cosA / aspectRatio, | ||||
88 | la->transformedLinePoints().first().x, | ||||
89 | la->transformedLinePoints().first().y | ||||
90 | }, paintMatrix ); | ||||
91 | | ||||
92 | const double mainSegmentLength = sqrt( delta.x * delta.x + deaspectedY * deaspectedY ); | ||||
93 | drawMainLine( backImage ); | ||||
94 | if ( la->transformedLinePoints().count() == 2 ) | ||||
95 | { | ||||
96 | drawLineEnds( mainSegmentLength, backImage, tmpMtx ); | ||||
97 | drawLeaderLine( mainSegmentLength, backImage, tmpMtx ); | ||||
98 | } | ||||
99 | } | ||||
100 | | ||||
101 | private: | ||||
102 | void drawMainLine( QImage &backImage ) | ||||
103 | { | ||||
104 | // draw the line as normalized path into image | ||||
105 | PagePainter::drawShapeOnImage( backImage, matrixTransformPath( | ||||
106 | la->transformedLinePoints(), paintMatrix ), la->lineClosed(), | ||||
107 | linePen, fillBrush, pageScale, PagePainter::Multiply ); | ||||
108 | } | ||||
109 | | ||||
110 | void drawLineEnds( double mainSegmentLength, QImage &backImage, const std::vector<double>& tmpMatrix ) | ||||
111 | { | ||||
112 | double lineendSize = std::min( 6. * la->style().width() / pageWidth, mainSegmentLength / 2. ); | ||||
113 | | ||||
114 | switch ( la->lineStartStyle() ) { | ||||
115 | case Okular::LineAnnotation::Square: | ||||
116 | drawLineEndRect( 0., -lineendSize, tmpMatrix, backImage ); | ||||
117 | break; | ||||
118 | case Okular::LineAnnotation::Circle: | ||||
119 | case Okular::LineAnnotation::Diamond: | ||||
120 | drawLineEndDiamond( 0., -lineendSize, tmpMatrix, backImage ); | ||||
121 | break; | ||||
122 | case Okular::LineAnnotation::OpenArrow: | ||||
123 | drawLineEndArrow( 0., -lineendSize, -1., false, tmpMatrix, backImage ); | ||||
124 | break; | ||||
125 | case Okular::LineAnnotation::ClosedArrow: | ||||
126 | drawLineEndArrow( 0., -lineendSize, -1., true, tmpMatrix, backImage ); | ||||
127 | break; | ||||
128 | case Okular::LineAnnotation::None: | ||||
129 | case Okular::LineAnnotation::Butt: | ||||
130 | case Okular::LineAnnotation::ROpenArrow: | ||||
131 | drawLineEndArrow( 0., -lineendSize, 1., false, tmpMatrix, backImage ); | ||||
132 | break; | ||||
133 | case Okular::LineAnnotation::RClosedArrow: | ||||
134 | drawLineEndArrow( 0., -lineendSize, 1., true, tmpMatrix, backImage ); | ||||
135 | break; | ||||
136 | case Okular::LineAnnotation::Slash: | ||||
137 | break; | ||||
138 | } | ||||
139 | switch ( la->lineEndStyle() ) { | ||||
140 | case Okular::LineAnnotation::Square: | ||||
141 | drawLineEndRect( mainSegmentLength, lineendSize, tmpMatrix, backImage ); | ||||
142 | break; | ||||
143 | case Okular::LineAnnotation::Circle: | ||||
144 | case Okular::LineAnnotation::Diamond: | ||||
145 | drawLineEndDiamond( mainSegmentLength, lineendSize, tmpMatrix, backImage ); | ||||
146 | break; | ||||
147 | case Okular::LineAnnotation::OpenArrow: | ||||
148 | drawLineEndArrow( mainSegmentLength, lineendSize, 1., false, tmpMatrix, backImage ); | ||||
149 | break; | ||||
150 | case Okular::LineAnnotation::ClosedArrow: | ||||
151 | drawLineEndArrow( mainSegmentLength, lineendSize, 1., true, tmpMatrix, backImage ); | ||||
152 | break; | ||||
153 | case Okular::LineAnnotation::None: | ||||
154 | case Okular::LineAnnotation::Butt: | ||||
155 | case Okular::LineAnnotation::ROpenArrow: | ||||
156 | drawLineEndArrow( mainSegmentLength, lineendSize, -1., false, tmpMatrix, backImage ); | ||||
157 | break; | ||||
158 | case Okular::LineAnnotation::RClosedArrow: | ||||
159 | drawLineEndArrow( mainSegmentLength, lineendSize, -1., true, tmpMatrix, backImage ); | ||||
160 | break; | ||||
161 | case Okular::LineAnnotation::Slash: | ||||
162 | break; | ||||
163 | } | ||||
164 | } | ||||
165 | | ||||
166 | std::vector<double> matrixMultiply(const std::vector<double> &matrixA, const std::vector<double> &matrixB) | ||||
167 | { | ||||
168 | return { | ||||
169 | matrixA[0] * matrixB[0] + matrixA[1] * matrixB[2], | ||||
170 | matrixA[0] * matrixB[1] + matrixA[1] * matrixB[3], | ||||
171 | matrixA[2] * matrixB[0] + matrixA[3] * matrixB[2], | ||||
172 | matrixA[2] * matrixB[1] + matrixA[3] * matrixB[3], | ||||
173 | matrixA[4] * matrixB[0] + matrixA[5] * matrixB[2] + matrixB[4], | ||||
174 | matrixA[4] * matrixB[1] + matrixA[5] * matrixB[3] + matrixB[5], | ||||
175 | }; | ||||
176 | } | ||||
177 | | ||||
178 | template <typename T> QList<Okular::NormalizedPoint> matrixTransformPath(const T &path, const std::vector<double> &matrix) | ||||
179 | { | ||||
180 | QList<Okular::NormalizedPoint> transformed; | ||||
181 | for( const Okular::NormalizedPoint &item : path ) | ||||
182 | { | ||||
183 | Okular::NormalizedPoint p; | ||||
184 | p.x = matrix[0] * item.x + matrix[2] * item.y + matrix[4]; | ||||
185 | p.y = matrix[1] * item.x + matrix[3] * item.y + matrix[5]; | ||||
186 | transformed.append(p); | ||||
187 | } | ||||
188 | return transformed; | ||||
189 | } | ||||
190 | | ||||
191 | void drawLineEndArrow(double xEndPos, double size, double flipX, bool close, const std::vector<double> &matrix, QImage &backImage ) | ||||
192 | { | ||||
193 | const double halfSize = size / 2.; | ||||
194 | const QList<Okular::NormalizedPoint> lineend { | ||||
195 | { xEndPos - halfSize * flipX - halfSize, size / 2. }, | ||||
196 | { xEndPos + halfSize * flipX - halfSize, 0. }, | ||||
197 | { xEndPos - halfSize * flipX - halfSize, -size / 2. }, | ||||
198 | }; | ||||
199 | PagePainter::drawShapeOnImage( backImage, matrixTransformPath(lineend, matrix), | ||||
200 | close, linePen, fillBrush, pageScale, PagePainter::Multiply); | ||||
201 | } | ||||
202 | | ||||
203 | void drawLineEndRect(double xEndPos, double size, const std::vector<double> &matrix, QImage &backImage ) | ||||
204 | { | ||||
205 | const QList<Okular::NormalizedPoint> lineend { | ||||
206 | { xEndPos, size / 2. }, | ||||
207 | { xEndPos - size, size / 2. }, | ||||
208 | { xEndPos - size, -size / 2. }, | ||||
209 | { xEndPos, -size / 2. } | ||||
210 | }; | ||||
211 | PagePainter::drawShapeOnImage( backImage, matrixTransformPath(lineend, matrix), | ||||
212 | true, linePen, fillBrush, pageScale, PagePainter::Multiply); | ||||
213 | } | ||||
214 | | ||||
215 | void drawLineEndDiamond(double xEndPos, double size, const std::vector<double> &matrix, QImage &backImage ) | ||||
216 | { | ||||
217 | const QList<Okular::NormalizedPoint> lineend { | ||||
218 | { xEndPos, 0. }, | ||||
219 | { xEndPos - size / 2., size / 2. }, | ||||
220 | { xEndPos - size, 0. }, | ||||
221 | { xEndPos - size / 2., -size / 2. } | ||||
222 | }; | ||||
223 | PagePainter::drawShapeOnImage( backImage, matrixTransformPath(lineend, matrix), | ||||
224 | true, linePen, fillBrush, pageScale, PagePainter::Multiply); | ||||
225 | } | ||||
226 | | ||||
227 | void drawLeaderLine(double xEndPos, QImage &backImage, const std::vector<double> &matrix ) | ||||
228 | { | ||||
229 | const double ll = la->lineLeadingForwardPoint() / pageHeight; | ||||
230 | const double lle = la->lineLeadingBackwardPoint() / pageHeight; | ||||
231 | QList<Okular::NormalizedPoint> lineend; | ||||
232 | | ||||
233 | int sign = ll > 0.0 ? 1 : -1; | ||||
234 | if ( fabs( ll ) > 0.1 ) { | ||||
235 | lineend.append( { xEndPos, ll } ); | ||||
236 | // do we have the extension on the "back"? | ||||
237 | if ( fabs( lle ) > 0.1 ) | ||||
238 | { | ||||
239 | lineend.append( { xEndPos, sign * lle } ); | ||||
240 | } else { | ||||
241 | lineend.append( { xEndPos, 0. } ); | ||||
242 | } | ||||
243 | } | ||||
244 | | ||||
245 | PagePainter::drawShapeOnImage( backImage, matrixTransformPath(lineend, matrix), false, | ||||
246 | linePen, fillBrush, pageScale, PagePainter::Multiply); | ||||
247 | } | ||||
248 | | ||||
249 | private: | ||||
250 | const Okular::LineAnnotation* la; | ||||
251 | const QPen linePen; | ||||
252 | double pageWidth; | ||||
253 | double pageHeight; | ||||
254 | double aspectRatio; | ||||
255 | double pageScale; | ||||
256 | std::vector<double> paintMatrix; | ||||
257 | QBrush fillBrush; | ||||
258 | }; | ||||
259 | | ||||
55 | void PagePainter::paintPageOnPainter( QPainter * destPainter, const Okular::Page * page, | 260 | void PagePainter::paintPageOnPainter( QPainter * destPainter, const Okular::Page * page, | ||
56 | Okular::DocumentObserver *observer, int flags, int scaledWidth, int scaledHeight, const QRect &limits ) | 261 | Okular::DocumentObserver *observer, int flags, int scaledWidth, int scaledHeight, const QRect &limits ) | ||
57 | { | 262 | { | ||
58 | paintCroppedPageOnPainter( destPainter, page, observer, flags, scaledWidth, scaledHeight, limits, | 263 | paintCroppedPageOnPainter( destPainter, page, observer, flags, scaledWidth, scaledHeight, limits, | ||
59 | Okular::NormalizedRect( 0, 0, 1, 1 ), nullptr ); | 264 | Okular::NormalizedRect( 0, 0, 1, 1 ), nullptr ); | ||
60 | } | 265 | } | ||
61 | 266 | | |||
62 | void PagePainter::paintCroppedPageOnPainter( QPainter * destPainter, const Okular::Page * page, | 267 | void PagePainter::paintCroppedPageOnPainter( QPainter * destPainter, const Okular::Page * page, | ||
▲ Show 20 Lines • Show All 378 Lines • ▼ Show 20 Line(s) | 645 | { | |||
441 | Okular::Annotation * a = *aIt; | 646 | Okular::Annotation * a = *aIt; | ||
442 | Okular::Annotation::SubType type = a->subType(); | 647 | Okular::Annotation::SubType type = a->subType(); | ||
443 | QColor acolor = a->style().color(); | 648 | QColor acolor = a->style().color(); | ||
444 | if ( !acolor.isValid() ) | 649 | if ( !acolor.isValid() ) | ||
445 | acolor = Qt::yellow; | 650 | acolor = Qt::yellow; | ||
446 | acolor.setAlphaF( a->style().opacity() ); | 651 | acolor.setAlphaF( a->style().opacity() ); | ||
447 | 652 | | |||
448 | // draw LineAnnotation MISSING: all | 653 | // draw LineAnnotation MISSING: all | ||
449 | if ( type == Okular::Annotation::ALine ) | 654 | if ( type == Okular::Annotation::ALine ) | ||
sander: Is that `MISSING` comment still relevant? What exactly does it mean anyway? | |||||
The comment below for HighlightAnnotation says MISSING: under/strike width, feather, capping, so it seems the comments had been intended to list annotation sub type features which are not yet implemented. In this case a consistent comment for LineAnnotation would be e.g. MISSING: caption, line endings for multi point lines. Will change it accordingly. tobiasdeiminger: The comment below for `HighlightAnnotation` says `MISSING: under/strike width, feather… | |||||
450 | { | 655 | { | ||
451 | // get the annotation | 656 | LineAnnotPainter linepainter { (Okular::LineAnnotation *) a, page, pageScale, | ||
452 | Okular::LineAnnotation * la = (Okular::LineAnnotation *) a; | 657 | { xScale, 0., 0., yScale, -xOffset * xScale, -yOffset * yScale } }; | ||
453 | 658 | linepainter.draw( backImage ); | |||
454 | NormalizedPath path; | | |||
455 | // normalize page point to image | | |||
456 | const QLinkedList<Okular::NormalizedPoint> points = la->transformedLinePoints(); | | |||
457 | QLinkedList<Okular::NormalizedPoint>::const_iterator it = points.constBegin(); | | |||
458 | QLinkedList<Okular::NormalizedPoint>::const_iterator itEnd = points.constEnd(); | | |||
459 | for ( ; it != itEnd; ++it ) | | |||
460 | { | | |||
461 | Okular::NormalizedPoint point; | | |||
462 | point.x = ( (*it).x - xOffset) * xScale; | | |||
463 | point.y = ( (*it).y - yOffset) * yScale; | | |||
464 | path.append( point ); | | |||
465 | } | | |||
466 | | ||||
467 | const QPen linePen = buildPen( a, a->style().width(), a->style().color() ); | | |||
468 | QBrush fillBrush; | | |||
469 | | ||||
470 | if ( la->lineClosed() && la->lineInnerColor().isValid() ) | | |||
471 | fillBrush = QBrush( la->lineInnerColor() ); | | |||
472 | | ||||
473 | // draw the line as normalized path into image | | |||
474 | drawShapeOnImage( backImage, path, la->lineClosed(), | | |||
475 | linePen, | | |||
476 | fillBrush, pageScale ,Multiply); | | |||
477 | | ||||
478 | if ( path.count() == 2 && fabs( la->lineLeadingForwardPoint() ) > 0.1 ) | | |||
479 | { | | |||
480 | Okular::NormalizedPoint delta( la->transformedLinePoints().last().x - la->transformedLinePoints().first().x, la->transformedLinePoints().first().y - la->transformedLinePoints().last().y ); | | |||
481 | double angle = atan2( delta.y * page->height(), delta.x * page->width() ); | | |||
482 | if ( delta.y < 0 ) | | |||
483 | angle += 2 * M_PI; | | |||
484 | | ||||
485 | int sign = la->lineLeadingForwardPoint() > 0.0 ? 1 : -1; | | |||
486 | double LLx = fabs( la->lineLeadingForwardPoint() ) * cos( angle + sign * M_PI_2 + 2 * M_PI ) / page->width(); | | |||
487 | double LLy = fabs( la->lineLeadingForwardPoint() ) * sin( angle + sign * M_PI_2 + 2 * M_PI ) / page->height(); | | |||
488 | | ||||
489 | NormalizedPath path2; | | |||
490 | NormalizedPath path3; | | |||
491 | | ||||
492 | Okular::NormalizedPoint point; | | |||
493 | point.x = ( la->transformedLinePoints().first().x + LLx - xOffset ) * xScale; | | |||
494 | point.y = ( la->transformedLinePoints().first().y - LLy - yOffset ) * yScale; | | |||
495 | path2.append( point ); | | |||
496 | point.x = ( la->transformedLinePoints().last().x + LLx - xOffset ) * xScale; | | |||
497 | point.y = ( la->transformedLinePoints().last().y - LLy - yOffset ) * yScale; | | |||
498 | path3.append( point ); | | |||
499 | // do we have the extension on the "back"? | | |||
500 | if ( fabs( la->lineLeadingBackwardPoint() ) > 0.1 ) | | |||
501 | { | | |||
502 | double LLEx = la->lineLeadingBackwardPoint() * cos( angle - sign * M_PI_2 + 2 * M_PI ) / page->width(); | | |||
503 | double LLEy = la->lineLeadingBackwardPoint() * sin( angle - sign * M_PI_2 + 2 * M_PI ) / page->height(); | | |||
504 | point.x = ( la->transformedLinePoints().first().x + LLEx - xOffset ) * xScale; | | |||
505 | point.y = ( la->transformedLinePoints().first().y - LLEy - yOffset ) * yScale; | | |||
506 | path2.append( point ); | | |||
507 | point.x = ( la->transformedLinePoints().last().x + LLEx - xOffset ) * xScale; | | |||
508 | point.y = ( la->transformedLinePoints().last().y - LLEy - yOffset ) * yScale; | | |||
509 | path3.append( point ); | | |||
510 | } | | |||
511 | else | | |||
512 | { | | |||
513 | path2.append( path[0] ); | | |||
514 | path3.append( path[1] ); | | |||
515 | } | | |||
516 | | ||||
517 | drawShapeOnImage( backImage, path2, false, linePen, QBrush(), pageScale, Multiply ); | | |||
518 | drawShapeOnImage( backImage, path3, false, linePen, QBrush(), pageScale, Multiply ); | | |||
519 | } | | |||
520 | } | 659 | } | ||
521 | // draw HighlightAnnotation MISSING: under/strike width, feather, capping | 660 | // draw HighlightAnnotation MISSING: under/strike width, feather, capping | ||
522 | else if ( type == Okular::Annotation::AHighlight ) | 661 | else if ( type == Okular::Annotation::AHighlight ) | ||
523 | { | 662 | { | ||
524 | // get the annotation | 663 | // get the annotation | ||
525 | Okular::HighlightAnnotation * ha = (Okular::HighlightAnnotation *) a; | 664 | Okular::HighlightAnnotation * ha = (Okular::HighlightAnnotation *) a; | ||
526 | Okular::HighlightAnnotation::HighlightType type = ha->highlightType(); | 665 | Okular::HighlightAnnotation::HighlightType type = ha->highlightType(); | ||
527 | 666 | | |||
▲ Show 20 Lines • Show All 468 Lines • ▼ Show 20 Line(s) | 1125 | { | |||
996 | if ( closeShape ) | 1135 | if ( closeShape ) | ||
997 | path.closeSubpath(); | 1136 | path.closeSubpath(); | ||
998 | 1137 | | |||
999 | painter.drawPath( path ); | 1138 | painter.drawPath( path ); | ||
1000 | } | 1139 | } | ||
1001 | } | 1140 | } | ||
1002 | 1141 | | |||
1003 | /* kate: replace-tabs on; indent-width 4; */ | 1142 | /* kate: replace-tabs on; indent-width 4; */ | ||
1004 | 1143 | | |||
aacid: const here and for fImageHeight and penWidth |
Is that MISSING comment still relevant? What exactly does it mean anyway?