00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "config.h"
00034 #include "wtf/Platform.h"
00035
00036 #include "AffineTransform.h"
00037 #include "Path.h"
00038
00039 #include "GraphicsContext.h"
00040
00041
00042
00043
00044
00045 #include <QStack>
00046 #include <QPainter>
00047 #include <QPolygonF>
00048 #include <QPainterPath>
00049 #include <QPaintDevice>
00050 #include <QPixmap>
00051 #include <QDebug>
00052
00053 #ifndef M_PI
00054 #define M_PI 3.14159265358979323846
00055 #endif
00056
00057 namespace WebCore {
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 struct TransparencyLayer
00144 {
00145 TransparencyLayer(const QPainter* p, const QRect &rect)
00146 : pixmap(rect.width(), rect.height())
00147 {
00148 offset = rect.topLeft();
00149 pixmap.fill(Qt::transparent);
00150 painter.begin(&pixmap);
00151 painter.translate(-offset);
00152 painter.setPen(p->pen());
00153 painter.setBrush(p->brush());
00154 painter.setTransform(p->transform(), true);
00155 painter.setOpacity(p->opacity());
00156 painter.setFont(p->font());
00157 painter.setCompositionMode(p->compositionMode());
00158 painter.setClipPath(p->clipPath());
00159 }
00160
00161 TransparencyLayer()
00162 {
00163 }
00164
00165 QPixmap pixmap;
00166 QPoint offset;
00167 QPainter painter;
00168 qreal opacity;
00169 private:
00170 TransparencyLayer(const TransparencyLayer &) {}
00171 TransparencyLayer & operator=(const TransparencyLayer &) { return *this; }
00172 };
00173
00174 struct TextShadow
00175 {
00176 TextShadow()
00177 : x(0)
00178 , y(0)
00179 , blur(0)
00180 {
00181 }
00182
00183 bool isNull() { return !x && !y && !blur; }
00184
00185 int x;
00186 int y;
00187 int blur;
00188
00189
00190 };
00191
00192 class GraphicsContextPlatformPrivate
00193 {
00194 public:
00195 GraphicsContextPlatformPrivate(QPainter* painter);
00196 ~GraphicsContextPlatformPrivate();
00197
00198 inline QPainter* p()
00199 {
00200 if (layers.isEmpty()) {
00201 if (redirect)
00202 return redirect;
00203
00204 return painter;
00205 } else
00206 return &layers.top()->painter;
00207 }
00208
00209 QPaintDevice* device;
00210
00211 QStack<TransparencyLayer *> layers;
00212 QPainter* redirect;
00213
00214 TextShadow shadow;
00215
00216
00217 QPainterPath currentPath;
00218
00219 private:
00220 QPainter* painter;
00221 };
00222
00223
00224 GraphicsContextPlatformPrivate::GraphicsContextPlatformPrivate(QPainter* p)
00225 {
00226 painter = p;
00227 device = painter ? painter->device() : 0;
00228 redirect = 0;
00229
00230
00231 if (painter)
00232 painter->setRenderHint(QPainter::Antialiasing);
00233 }
00234
00235 GraphicsContextPlatformPrivate::~GraphicsContextPlatformPrivate()
00236 {
00237 }
00238
00239 GraphicsContext::GraphicsContext(PlatformGraphicsContext* context)
00240 : m_common(createGraphicsContextPrivate())
00241 , m_data(new GraphicsContextPlatformPrivate(context))
00242 {
00243 setPaintingDisabled(!context);
00244 if (context) {
00245
00246
00247
00248 }
00249 }
00250
00251 GraphicsContext::~GraphicsContext()
00252 {
00253 while(!m_data->layers.isEmpty())
00254 endTransparencyLayer();
00255
00256 destroyGraphicsContextPrivate(m_common);
00257 delete m_data;
00258 }
00259
00260 PlatformGraphicsContext* GraphicsContext::platformContext() const
00261 {
00262 return m_data->p();
00263 }
00264
00265 AffineTransform GraphicsContext::getCTM() const
00266 {
00267 return platformContext()->combinedMatrix();
00268 }
00269
00270 void GraphicsContext::savePlatformState()
00271 {
00272 m_data->p()->save();
00273 }
00274
00275 void GraphicsContext::restorePlatformState()
00276 {
00277 m_data->p()->restore();
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387 void GraphicsContext::drawRect(const IntRect& rect)
00388 {
00389 if (paintingDisabled())
00390 return;
00391
00392 m_data->p()->drawRect(rect);
00393 }
00394
00395
00396 static void adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth,
00397 const StrokeStyle& penStyle)
00398 {
00399
00400
00401
00402
00403 if (penStyle == DottedStroke || penStyle == DashedStroke) {
00404 if (p1.x() == p2.x()) {
00405 p1.setY(p1.y() + strokeWidth);
00406 p2.setY(p2.y() - strokeWidth);
00407 } else {
00408 p1.setX(p1.x() + strokeWidth);
00409 p2.setX(p2.x() - strokeWidth);
00410 }
00411 }
00412
00413 if (((int) strokeWidth) % 2) {
00414 if (p1.x() == p2.x()) {
00415
00416 p1.setX(p1.x() + 0.5);
00417 p2.setX(p2.x() + 0.5);
00418 } else {
00419
00420 p1.setY(p1.y() + 0.5);
00421 p2.setY(p2.y() + 0.5);
00422 }
00423 }
00424 }
00425
00426
00427 void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2)
00428 {
00429 if (paintingDisabled())
00430 return;
00431
00432 FloatPoint p1 = point1;
00433 FloatPoint p2 = point2;
00434
00435
00436 m_data->p()->drawLine(p1, p2);
00437 }
00438
00439
00440 void GraphicsContext::drawEllipse(const IntRect& rect)
00441 {
00442 if (paintingDisabled())
00443 return;
00444
00445 m_data->p()->drawEllipse(rect);
00446 }
00447
00448 void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan)
00449 {
00450
00451
00452
00453 m_data->p()->drawArc(rect, startAngle * 16, angleSpan * 16);
00454 }
00455
00456 void GraphicsContext::drawConvexPolygon(size_t npoints, const FloatPoint* points, bool shouldAntialias)
00457 {
00458 if (paintingDisabled())
00459 return;
00460
00461 if (npoints <= 1)
00462 return;
00463
00464 QPolygonF polygon(npoints);
00465
00466 for (size_t i = 0; i < npoints; i++)
00467 polygon[i] = points[i];
00468
00469 QPainter *p = m_data->p();
00470 p->save();
00471 p->setRenderHint(QPainter::Antialiasing, shouldAntialias);
00472 p->drawConvexPolygon(polygon);
00473 p->restore();
00474 }
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 void GraphicsContext::beginPath()
00502 {
00503 m_data->currentPath = QPainterPath();
00504 }
00505
00506 void GraphicsContext::addPath(const Path& path)
00507 {
00508 m_data->currentPath = *(path.platformPath());
00509 }
00510
00511 void GraphicsContext::setFillRule(WindRule rule)
00512 {
00513 m_data->currentPath.setFillRule(rule == RULE_EVENODD ? Qt::OddEvenFill : Qt::WindingFill);
00514 }
00515
00516 PlatformPath* GraphicsContext::currentPath()
00517 {
00518 return &m_data->currentPath;
00519 }
00520
00521 void GraphicsContext::clip(const FloatRect& rect)
00522 {
00523 if (paintingDisabled())
00524 return;
00525
00526 QPainter *p = m_data->p();
00527 if (p->clipRegion().isEmpty())
00528 p->setClipRect(rect);
00529 else p->setClipRect(rect, Qt::IntersectClip);
00530 }
00531
00537 void setFocusRingColorChangeFunction(void (*)()) { }
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576 void GraphicsContext::drawLineForText(const IntPoint& origin, int width, bool printing)
00577 {
00578 if (paintingDisabled())
00579 return;
00580
00581 IntPoint endPoint = origin + IntSize(width, 0);
00582 drawLine(origin, endPoint);
00583 }
00584
00585 void GraphicsContext::drawLineForMisspellingOrBadGrammar(const IntPoint&,
00586 int width, bool grammar)
00587 {
00588 if (paintingDisabled())
00589 return;
00590
00591
00592 }
00593
00594 FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& frect)
00595 {
00596 QRectF rect(frect);
00597 rect = m_data->p()->deviceMatrix().mapRect(rect);
00598
00599 QRect result = rect.toRect();
00600 return FloatRect(QRectF(result));
00601 }
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614 void GraphicsContext::clearPlatformShadow()
00615 {
00616 if (paintingDisabled())
00617 return;
00618
00619 m_data->shadow = TextShadow();
00620 }
00621
00622 void GraphicsContext::beginTransparencyLayer(float opacity)
00623 {
00624 if (paintingDisabled())
00625 return;
00626
00627 int x, y, w, h;
00628 x = y = 0;
00629 w = m_data->device->width();
00630 h = m_data->device->height();
00631
00632 QPainter *p = m_data->p();
00633 QRectF clip = p->clipPath().boundingRect();
00634 bool ok;
00635 QTransform transform = p->transform().inverted(&ok);
00636 if (ok) {
00637 QRectF deviceClip = transform.mapRect(clip);
00638 x = int(qBound(qreal(0), deviceClip.x(), (qreal)w));
00639 y = int(qBound(qreal(0), deviceClip.y(), (qreal)h));
00640 w = int(qBound(qreal(0), deviceClip.width(), (qreal)w) + 2);
00641 h = int(qBound(qreal(0), deviceClip.height(), (qreal)h) + 2);
00642 }
00643 TransparencyLayer * layer = new TransparencyLayer(m_data->p(), QRect(x, y, w, h));
00644
00645 layer->opacity = opacity;
00646 m_data->layers.push(layer);
00647 }
00648
00649 void GraphicsContext::endTransparencyLayer()
00650 {
00651 if (paintingDisabled())
00652 return;
00653
00654 TransparencyLayer *layer = m_data->layers.pop();
00655 layer->painter.end();
00656
00657 QPainter *p = m_data->p();
00658 p->save();
00659 p->resetTransform();
00660 p->setOpacity(layer->opacity);
00661 p->drawPixmap(layer->offset, layer->pixmap);
00662 p->restore();
00663
00664 delete layer;
00665 }
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714 void GraphicsContext::setMiterLimit(float limit)
00715 {
00716 if (paintingDisabled())
00717 return;
00718
00719 QPainter *p = m_data->p();
00720 QPen nPen = p->pen();
00721 nPen.setMiterLimit(limit);
00722 p->setPen(nPen);
00723 }
00724
00725 void GraphicsContext::setAlpha(float opacity)
00726 {
00727 if (paintingDisabled())
00728 return;
00729 QPainter *p = m_data->p();
00730 p->setOpacity(opacity);
00731 }
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741 void GraphicsContext::clip(const Path& path)
00742 {
00743 if (paintingDisabled())
00744 return;
00745
00746 m_data->p()->setClipPath(*path.platformPath(), Qt::IntersectClip);
00747 }
00748
00749 void GraphicsContext::clipOut(const Path& path)
00750 {
00751 if (paintingDisabled())
00752 return;
00753
00754 QPainter *p = m_data->p();
00755 QRectF clipBounds = p->clipPath().boundingRect();
00756 QPainterPath clippedOut = *path.platformPath();
00757 QPainterPath newClip;
00758 newClip.setFillRule(Qt::OddEvenFill);
00759 newClip.addRect(clipBounds);
00760 newClip.addPath(clippedOut);
00761
00762 p->setClipPath(newClip, Qt::IntersectClip);
00763 }
00764
00765 void GraphicsContext::translate(float x, float y)
00766 {
00767 if (paintingDisabled())
00768 return;
00769
00770 m_data->p()->translate(x, y);
00771 }
00772
00773 IntPoint GraphicsContext::origin()
00774 {
00775 if (paintingDisabled())
00776 return IntPoint();
00777 const QTransform &transform = m_data->p()->transform();
00778 return IntPoint(qRound(transform.dx()), qRound(transform.dy()));
00779 }
00780
00781 void GraphicsContext::rotate(float radians)
00782 {
00783 if (paintingDisabled())
00784 return;
00785
00786 m_data->p()->rotate(radians);
00787 }
00788
00789 void GraphicsContext::scale(const FloatSize& s)
00790 {
00791 if (paintingDisabled())
00792 return;
00793
00794 m_data->p()->scale(s.width(), s.height());
00795 }
00796
00797 void GraphicsContext::clipOut(const IntRect& rect)
00798 {
00799 if (paintingDisabled())
00800 return;
00801
00802 QPainter *p = m_data->p();
00803 QRectF clipBounds = p->clipPath().boundingRect();
00804 QPainterPath newClip;
00805 newClip.setFillRule(Qt::OddEvenFill);
00806 newClip.addRect(clipBounds);
00807 newClip.addRect(QRect(rect));
00808
00809 p->setClipPath(newClip, Qt::IntersectClip);
00810 }
00811
00812 void GraphicsContext::clipOutEllipseInRect(const IntRect& rect)
00813 {
00814 if (paintingDisabled())
00815 return;
00816
00817 QPainter *p = m_data->p();
00818 QRectF clipBounds = p->clipPath().boundingRect();
00819 QPainterPath newClip;
00820 newClip.setFillRule(Qt::OddEvenFill);
00821 newClip.addRect(clipBounds);
00822 newClip.addEllipse(QRect(rect));
00823
00824 p->setClipPath(newClip, Qt::IntersectClip);
00825 }
00826
00827 void GraphicsContext::addInnerRoundedRectClip(const IntRect& rect,
00828 int thickness)
00829 {
00830 if (paintingDisabled())
00831 return;
00832
00833 clip(rect);
00834 QPainterPath path;
00835
00836
00837 path.addEllipse(QRectF(rect.x(), rect.y(), rect.width(), rect.height()));
00838
00839
00840 path.addEllipse(QRectF(rect.x() + thickness, rect.y() + thickness,
00841 rect.width() - (thickness * 2), rect.height() - (thickness * 2)));
00842
00843 path.setFillRule(Qt::OddEvenFill);
00844 m_data->p()->setClipPath(path, Qt::IntersectClip);
00845 }
00846
00847 void GraphicsContext::concatCTM(const AffineTransform& transform)
00848 {
00849 if (paintingDisabled())
00850 return;
00851
00852 m_data->p()->setMatrix(transform, true);
00853 }
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887 void GraphicsContext::setPlatformStrokeThickness(float thickness)
00888 {
00889 if (paintingDisabled())
00890 return;
00891 QPainter *p = m_data->p();
00892 QPen newPen(p->pen());
00893 newPen.setWidthF(thickness);
00894 p->setPen(newPen);
00895 }
00896
00897
00898
00899
00900
00901
00902
00903
00904 void GraphicsContext::setUseAntialiasing(bool enable)
00905 {
00906 if (paintingDisabled())
00907 return;
00908 m_data->p()->setRenderHint(QPainter::Antialiasing, enable);
00909 }
00910
00911 }
00912
00913