Changeset View
Changeset View
Standalone View
Standalone View
ui/pagepainter.cpp
Show All 13 Lines | |||||
14 | #include <qpainter.h> | 14 | #include <qpainter.h> | ||
15 | #include <qpalette.h> | 15 | #include <qpalette.h> | ||
16 | #include <qpixmap.h> | 16 | #include <qpixmap.h> | ||
17 | #include <qvarlengtharray.h> | 17 | #include <qvarlengtharray.h> | ||
18 | #include <kiconloader.h> | 18 | #include <kiconloader.h> | ||
19 | #include <QDebug> | 19 | #include <QDebug> | ||
20 | #include <QApplication> | 20 | #include <QApplication> | ||
21 | #include <QIcon> | 21 | #include <QIcon> | ||
22 | #include <QTransform> | ||||
22 | 23 | | |||
23 | // system includes | 24 | // system includes | ||
24 | #include <math.h> | 25 | #include <math.h> | ||
25 | 26 | | |||
26 | // local includes | 27 | // local includes | ||
27 | #include "core/area.h" | 28 | #include "core/area.h" | ||
28 | #include "core/page.h" | 29 | #include "core/page.h" | ||
29 | #include "core/page_p.h" | 30 | #include "core/page_p.h" | ||
Show All 17 Lines | 46 | QPen p( | |||
47 | width, | 48 | width, | ||
48 | ann->style().lineStyle() == Okular::Annotation::Dashed ? Qt::DashLine : Qt::SolidLine, | 49 | ann->style().lineStyle() == Okular::Annotation::Dashed ? Qt::DashLine : Qt::SolidLine, | ||
49 | Qt::SquareCap, | 50 | Qt::SquareCap, | ||
50 | Qt::MiterJoin | 51 | Qt::MiterJoin | ||
51 | ); | 52 | ); | ||
52 | return p; | 53 | return p; | ||
53 | } | 54 | } | ||
54 | 55 | | |||
56 | class LineAnnotPainter { | ||||
57 | public: | ||||
58 | LineAnnotPainter( const Okular::LineAnnotation * a, const Okular::Page * page, double pageScale, QTransform &&paintMatrix ) | ||||
59 | : la { a } | ||||
60 | , linePen { buildPen( a, a->style().width(), a->style().color() ) } | ||||
61 | , pageWidth { page->width() } | ||||
62 | , pageHeight { page->height() } | ||||
63 | , aspectRatio { page->height() / page->width() } | ||||
64 | , pageScale { pageScale } | ||||
65 | , paintMatrix { paintMatrix } | ||||
66 | { | ||||
67 | if ( ( la->lineClosed() || la->transformedLinePoints().count() == 2 ) && | ||||
68 | la->lineInnerColor().isValid() ) | ||||
69 | { | ||||
70 | fillBrush = QBrush( la->lineInnerColor() ); | ||||
71 | } | ||||
72 | } | ||||
73 | | ||||
74 | void draw( QImage &image ) | ||||
75 | { | ||||
76 | const Okular::NormalizedPoint delta { | ||||
77 | la->transformedLinePoints().last().x - la->transformedLinePoints().first().x, | ||||
78 | la->transformedLinePoints().first().y - la->transformedLinePoints().last().y | ||||
79 | }; | ||||
80 | const double deaspectedY = delta.y * aspectRatio; | ||||
81 | const double angle = atan2( delta.y * aspectRatio, delta.x ); | ||||
82 | const double cosA = cos( -angle ); | ||||
83 | const double sinA = sin( -angle ); | ||||
84 | const QTransform tmpMatrix = QTransform { | ||||
85 | cosA, sinA / aspectRatio, | ||||
86 | -sinA, cosA / aspectRatio, | ||||
87 | la->transformedLinePoints().first().x, | ||||
88 | la->transformedLinePoints().first().y } * paintMatrix; | ||||
89 | const double mainSegmentLength = sqrt( delta.x * delta.x + deaspectedY * deaspectedY ); | ||||
90 | const double lineendSize = std::min( 6. * la->style().width() / pageWidth, mainSegmentLength / 2. ); | ||||
91 | | ||||
92 | if ( la->transformedLinePoints().count() == 2 ) | ||||
93 | { | ||||
94 | drawShortenedLine( mainSegmentLength, lineendSize, image, tmpMatrix ); | ||||
95 | drawLineEnds( mainSegmentLength, lineendSize, image, tmpMatrix ); | ||||
96 | drawLeaderLine( mainSegmentLength, image, tmpMatrix ); | ||||
97 | } | ||||
98 | else if ( la->transformedLinePoints().count() > 2 ) | ||||
99 | { | ||||
100 | drawMainLine( image ); | ||||
101 | } | ||||
102 | } | ||||
103 | | ||||
104 | private: | ||||
105 | void drawMainLine( QImage &image ) | ||||
106 | { | ||||
107 | // draw the line as normalized path into image | ||||
108 | PagePainter::drawShapeOnImage( image, transformPath( | ||||
109 | la->transformedLinePoints(), paintMatrix ), la->lineClosed(), | ||||
110 | linePen, fillBrush, pageScale, PagePainter::Multiply ); | ||||
111 | } | ||||
112 | | ||||
113 | static double shortenForArrow( double size, Okular::LineAnnotation::TermStyle endStyle ) | ||||
114 | { | ||||
115 | double shortenBy = 0.; | ||||
116 | switch ( endStyle ) { | ||||
117 | case Okular::LineAnnotation::Square: | ||||
118 | case Okular::LineAnnotation::Circle: | ||||
119 | case Okular::LineAnnotation::Diamond: | ||||
120 | case Okular::LineAnnotation::ClosedArrow: | ||||
121 | case Okular::LineAnnotation::RClosedArrow: | ||||
122 | case Okular::LineAnnotation::ROpenArrow: | ||||
123 | case Okular::LineAnnotation::OpenArrow: | ||||
124 | shortenBy = size; | ||||
125 | break; | ||||
126 | case Okular::LineAnnotation::Slash: | ||||
127 | shortenBy = cos( M_PI/3. ) * size / 2.; | ||||
128 | break; | ||||
129 | default: | ||||
130 | break; | ||||
131 | } | ||||
132 | return shortenBy; | ||||
133 | } | ||||
134 | | ||||
135 | void drawShortenedLine( double mainSegmentLength, double size, QImage &image, const QTransform& transform ) | ||||
136 | { | ||||
137 | const QList<Okular::NormalizedPoint> path { | ||||
138 | { shortenForArrow(size, la->lineStartStyle()), 0. }, | ||||
139 | { mainSegmentLength - shortenForArrow(size, la->lineEndStyle()), 0. } | ||||
140 | }; | ||||
141 | PagePainter::drawShapeOnImage( image, transformPath( | ||||
142 | path, transform ), la->lineClosed(), | ||||
143 | linePen, fillBrush, pageScale, PagePainter::Multiply ); | ||||
144 | } | ||||
145 | | ||||
146 | void drawLineEnds( double mainSegmentLength, double size, QImage &image, const QTransform& transform ) | ||||
147 | { | ||||
148 | switch ( la->lineStartStyle() ) { | ||||
149 | case Okular::LineAnnotation::Square: | ||||
150 | drawLineEndRect( 0., -size, transform, image ); | ||||
151 | break; | ||||
152 | case Okular::LineAnnotation::Circle: | ||||
153 | case Okular::LineAnnotation::Diamond: | ||||
154 | drawLineEndDiamond( 0., -size, transform, image ); | ||||
155 | break; | ||||
156 | case Okular::LineAnnotation::OpenArrow: | ||||
157 | drawLineEndArrow( 0., -size, -1., false, transform, image ); | ||||
158 | break; | ||||
159 | case Okular::LineAnnotation::ClosedArrow: | ||||
160 | drawLineEndArrow( 0., -size, -1., true, transform, image ); | ||||
161 | break; | ||||
162 | case Okular::LineAnnotation::None: | ||||
163 | case Okular::LineAnnotation::Butt: | ||||
164 | drawLineEndButt( 0., size, transform, image ); | ||||
165 | break; | ||||
166 | case Okular::LineAnnotation::ROpenArrow: | ||||
167 | drawLineEndArrow( 0., -size, 1., false, transform, image ); | ||||
168 | break; | ||||
169 | case Okular::LineAnnotation::RClosedArrow: | ||||
170 | drawLineEndArrow( 0., -size, 1., true, transform, image ); | ||||
171 | break; | ||||
172 | case Okular::LineAnnotation::Slash: | ||||
173 | drawLineEndSlash( 0., -size, transform, image ); | ||||
174 | break; | ||||
175 | } | ||||
176 | switch ( la->lineEndStyle() ) { | ||||
177 | case Okular::LineAnnotation::Square: | ||||
178 | drawLineEndRect( mainSegmentLength, size, transform, image ); | ||||
179 | break; | ||||
180 | case Okular::LineAnnotation::Circle: | ||||
181 | case Okular::LineAnnotation::Diamond: | ||||
182 | drawLineEndDiamond( mainSegmentLength, size, transform, image ); | ||||
183 | break; | ||||
184 | case Okular::LineAnnotation::OpenArrow: | ||||
185 | drawLineEndArrow( mainSegmentLength, size, 1., false, transform, image ); | ||||
186 | break; | ||||
187 | case Okular::LineAnnotation::ClosedArrow: | ||||
188 | drawLineEndArrow( mainSegmentLength, size, 1., true, transform, image ); | ||||
189 | break; | ||||
190 | case Okular::LineAnnotation::None: | ||||
191 | case Okular::LineAnnotation::Butt: | ||||
192 | drawLineEndButt( mainSegmentLength, size, transform, image ); | ||||
193 | break; | ||||
194 | case Okular::LineAnnotation::ROpenArrow: | ||||
195 | drawLineEndArrow( mainSegmentLength, size, -1., false, transform, image ); | ||||
196 | break; | ||||
197 | case Okular::LineAnnotation::RClosedArrow: | ||||
198 | drawLineEndArrow( mainSegmentLength, size, -1., true, transform, image ); | ||||
199 | break; | ||||
200 | case Okular::LineAnnotation::Slash: | ||||
201 | drawLineEndSlash( mainSegmentLength, size, transform, image ); | ||||
202 | break; | ||||
203 | } | ||||
204 | } | ||||
205 | | ||||
206 | template <typename T> QList<Okular::NormalizedPoint> transformPath( const T& path, const QTransform& transform ) | ||||
207 | { | ||||
208 | QList<Okular::NormalizedPoint> transformedPath; | ||||
209 | for( const Okular::NormalizedPoint &item : path ) | ||||
210 | { | ||||
211 | Okular::NormalizedPoint p; | ||||
212 | transform.map( item.x, item.y, &p.x, &p.y ); | ||||
213 | transformedPath.append(p); | ||||
214 | } | ||||
215 | return transformedPath; | ||||
216 | } | ||||
217 | | ||||
218 | void drawLineEndArrow( double xEndPos, double size, double flipX, bool close, const QTransform& transform, QImage &image ) | ||||
219 | { | ||||
220 | const double halfSize = size / 2.; | ||||
221 | const QList<Okular::NormalizedPoint> path { | ||||
222 | { xEndPos - halfSize * flipX - halfSize, size / 2. }, | ||||
223 | { xEndPos + halfSize * flipX - halfSize, 0. }, | ||||
224 | { xEndPos - halfSize * flipX - halfSize, -size / 2. }, | ||||
225 | }; | ||||
226 | PagePainter::drawShapeOnImage( image, transformPath(path, transform), | ||||
227 | close, linePen, fillBrush, pageScale, PagePainter::Multiply); | ||||
228 | } | ||||
229 | | ||||
230 | void drawLineEndButt( double xEndPos, double size, const QTransform& transform, QImage &image ) | ||||
231 | { | ||||
232 | const double halfSize = size / 2.; | ||||
233 | const QList<Okular::NormalizedPoint> path { | ||||
234 | { xEndPos, halfSize }, | ||||
235 | { xEndPos, -halfSize }, | ||||
236 | }; | ||||
237 | PagePainter::drawShapeOnImage( image, transformPath(path, transform), | ||||
238 | true, linePen, fillBrush, pageScale, PagePainter::Multiply); | ||||
239 | } | ||||
240 | | ||||
241 | void drawLineEndRect( double xEndPos, double size, const QTransform& transform, QImage &image ) | ||||
242 | { | ||||
243 | const QList<Okular::NormalizedPoint> path { | ||||
244 | { xEndPos, size / 2. }, | ||||
245 | { xEndPos - size, size / 2. }, | ||||
246 | { xEndPos - size, -size / 2. }, | ||||
247 | { xEndPos, -size / 2. } | ||||
248 | }; | ||||
249 | PagePainter::drawShapeOnImage( image, transformPath(path, transform), | ||||
250 | true, linePen, fillBrush, pageScale, PagePainter::Multiply); | ||||
251 | } | ||||
252 | | ||||
253 | void drawLineEndDiamond( double xEndPos, double size, const QTransform& transform, QImage &image ) | ||||
254 | { | ||||
255 | const QList<Okular::NormalizedPoint> path { | ||||
256 | { xEndPos, 0. }, | ||||
257 | { xEndPos - size / 2., size / 2. }, | ||||
258 | { xEndPos - size, 0. }, | ||||
259 | { xEndPos - size / 2., -size / 2. } | ||||
260 | }; | ||||
261 | PagePainter::drawShapeOnImage( image, transformPath(path, transform), | ||||
262 | true, linePen, fillBrush, pageScale, PagePainter::Multiply); | ||||
263 | } | ||||
264 | | ||||
265 | void drawLineEndSlash( double xEndPos, double size, const QTransform& transform, QImage &image ) | ||||
266 | { | ||||
267 | const double halfSize = size / 2.; | ||||
268 | const QList<Okular::NormalizedPoint> path { | ||||
269 | { xEndPos - cos(M_PI/3.) * size, halfSize }, | ||||
270 | { xEndPos, -halfSize }, | ||||
271 | }; | ||||
272 | PagePainter::drawShapeOnImage( image, transformPath(path, transform), | ||||
273 | true, linePen, fillBrush, pageScale, PagePainter::Multiply); | ||||
274 | } | ||||
275 | | ||||
276 | void drawLeaderLine( double xEndPos, QImage &image, const QTransform& transform ) | ||||
277 | { | ||||
278 | const double ll = la->lineLeadingForwardPoint() / pageHeight; | ||||
279 | const double lle = la->lineLeadingBackwardPoint() / pageHeight; | ||||
280 | QList<Okular::NormalizedPoint> path; | ||||
281 | | ||||
282 | int sign = ll > 0.0 ? 1 : -1; | ||||
283 | if ( fabs( ll ) > 0.1 ) { | ||||
284 | path.append( { xEndPos, ll } ); | ||||
285 | // do we have the extension on the "back"? | ||||
286 | if ( fabs( lle ) > 0.1 ) | ||||
287 | { | ||||
288 | path.append( { xEndPos, sign * lle } ); | ||||
289 | } else { | ||||
290 | path.append( { xEndPos, 0. } ); | ||||
291 | } | ||||
292 | } | ||||
293 | | ||||
294 | PagePainter::drawShapeOnImage( image, transformPath(path, transform), false, | ||||
295 | linePen, fillBrush, pageScale, PagePainter::Multiply); | ||||
296 | } | ||||
297 | | ||||
298 | private: | ||||
299 | const Okular::LineAnnotation* la; | ||||
300 | const QPen linePen; | ||||
301 | double pageWidth; | ||||
302 | double pageHeight; | ||||
303 | double aspectRatio; | ||||
304 | double pageScale; | ||||
305 | QTransform paintMatrix; | ||||
306 | QBrush fillBrush; | ||||
307 | }; | ||||
308 | | ||||
55 | void PagePainter::paintPageOnPainter( QPainter * destPainter, const Okular::Page * page, | 309 | void PagePainter::paintPageOnPainter( QPainter * destPainter, const Okular::Page * page, | ||
56 | Okular::DocumentObserver *observer, int flags, int scaledWidth, int scaledHeight, const QRect &limits ) | 310 | Okular::DocumentObserver *observer, int flags, int scaledWidth, int scaledHeight, const QRect &limits ) | ||
57 | { | 311 | { | ||
58 | paintCroppedPageOnPainter( destPainter, page, observer, flags, scaledWidth, scaledHeight, limits, | 312 | paintCroppedPageOnPainter( destPainter, page, observer, flags, scaledWidth, scaledHeight, limits, | ||
59 | Okular::NormalizedRect( 0, 0, 1, 1 ), nullptr ); | 313 | Okular::NormalizedRect( 0, 0, 1, 1 ), nullptr ); | ||
60 | } | 314 | } | ||
61 | 315 | | |||
62 | void PagePainter::paintCroppedPageOnPainter( QPainter * destPainter, const Okular::Page * page, | 316 | void PagePainter::paintCroppedPageOnPainter( QPainter * destPainter, const Okular::Page * page, | ||
▲ Show 20 Lines • Show All 378 Lines • ▼ Show 20 Line(s) | 694 | { | |||
441 | Okular::Annotation * a = *aIt; | 695 | Okular::Annotation * a = *aIt; | ||
442 | Okular::Annotation::SubType type = a->subType(); | 696 | Okular::Annotation::SubType type = a->subType(); | ||
443 | QColor acolor = a->style().color(); | 697 | QColor acolor = a->style().color(); | ||
444 | if ( !acolor.isValid() ) | 698 | if ( !acolor.isValid() ) | ||
445 | acolor = Qt::yellow; | 699 | acolor = Qt::yellow; | ||
446 | acolor.setAlphaF( a->style().opacity() ); | 700 | acolor.setAlphaF( a->style().opacity() ); | ||
447 | 701 | | |||
448 | // draw LineAnnotation MISSING: all | 702 | // draw LineAnnotation MISSING: all | ||
449 | if ( type == Okular::Annotation::ALine ) | 703 | 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 | { | 704 | { | ||
451 | // get the annotation | 705 | LineAnnotPainter linepainter { (Okular::LineAnnotation *) a, page, pageScale, | ||
452 | Okular::LineAnnotation * la = (Okular::LineAnnotation *) a; | 706 | { xScale, 0., 0., yScale, -xOffset * xScale, -yOffset * yScale } }; | ||
453 | 707 | 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 | } | 708 | } | ||
521 | // draw HighlightAnnotation MISSING: under/strike width, feather, capping | 709 | // draw HighlightAnnotation MISSING: under/strike width, feather, capping | ||
522 | else if ( type == Okular::Annotation::AHighlight ) | 710 | else if ( type == Okular::Annotation::AHighlight ) | ||
523 | { | 711 | { | ||
524 | // get the annotation | 712 | // get the annotation | ||
525 | Okular::HighlightAnnotation * ha = (Okular::HighlightAnnotation *) a; | 713 | Okular::HighlightAnnotation * ha = (Okular::HighlightAnnotation *) a; | ||
526 | Okular::HighlightAnnotation::HighlightType type = ha->highlightType(); | 714 | Okular::HighlightAnnotation::HighlightType type = ha->highlightType(); | ||
527 | 715 | | |||
▲ Show 20 Lines • Show All 468 Lines • ▼ Show 20 Line(s) | 1174 | { | |||
996 | if ( closeShape ) | 1184 | if ( closeShape ) | ||
997 | path.closeSubpath(); | 1185 | path.closeSubpath(); | ||
998 | 1186 | | |||
999 | painter.drawPath( path ); | 1187 | painter.drawPath( path ); | ||
1000 | } | 1188 | } | ||
1001 | } | 1189 | } | ||
1002 | 1190 | | |||
1003 | /* kate: replace-tabs on; indent-width 4; */ | 1191 | /* kate: replace-tabs on; indent-width 4; */ | ||
1004 | 1192 | | |||
aacid: const here and for fImageHeight and penWidth |
Is that MISSING comment still relevant? What exactly does it mean anyway?