1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
|
/*
* Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
* Copyright (C) 2007 Alp Toker <alp@atoker.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "GraphicsContext.h"
#if PLATFORM(CAIRO)
#include "AffineTransform.h"
#include "CairoPath.h"
#include "FloatRect.h"
#include "Font.h"
#include "ImageBuffer.h"
#include "IntRect.h"
#include "NotImplemented.h"
#include "Path.h"
#include "SimpleFontData.h"
#include <cairo.h>
#include <math.h>
#include <stdio.h>
#include <wtf/MathExtras.h>
#if PLATFORM(GTK)
#include <gdk/gdk.h>
#include <pango/pango.h>
#elif PLATFORM(WIN)
#include <cairo-win32.h>
#endif
#include "GraphicsContextPlatformPrivateCairo.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
namespace WebCore {
static inline void setColor(cairo_t* cr, const Color& col)
{
float red, green, blue, alpha;
col.getRGBA(red, green, blue, alpha);
cairo_set_source_rgba(cr, red, green, blue, alpha);
}
// A fillRect helper
static inline void fillRectSourceOver(cairo_t* cr, const FloatRect& rect, const Color& col)
{
setColor(cr, col);
cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_fill(cr);
}
GraphicsContext::GraphicsContext(PlatformGraphicsContext* cr)
: m_common(createGraphicsContextPrivate())
, m_data(new GraphicsContextPlatformPrivate)
{
m_data->cr = cairo_reference(cr);
setPaintingDisabled(!cr);
}
GraphicsContext::~GraphicsContext()
{
destroyGraphicsContextPrivate(m_common);
delete m_data;
}
AffineTransform GraphicsContext::getCTM() const
{
cairo_t* cr = platformContext();
cairo_matrix_t m;
cairo_get_matrix(cr, &m);
return m;
}
cairo_t* GraphicsContext::platformContext() const
{
return m_data->cr;
}
void GraphicsContext::savePlatformState()
{
cairo_save(m_data->cr);
m_data->save();
}
void GraphicsContext::restorePlatformState()
{
cairo_restore(m_data->cr);
m_data->restore();
}
// Draws a filled rectangle with a stroked border.
void GraphicsContext::drawRect(const IntRect& rect)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
if (fillColor().alpha())
fillRectSourceOver(cr, rect, fillColor());
if (strokeStyle() != NoStroke) {
setColor(cr, strokeColor());
FloatRect r(rect);
r.inflate(-.5f);
cairo_rectangle(cr, r.x(), r.y(), r.width(), r.height());
cairo_set_line_width(cr, 1.0);
cairo_stroke(cr);
}
cairo_restore(cr);
}
// FIXME: Now that this is refactored, it should be shared by all contexts.
static void adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, StrokeStyle style)
{
// For odd widths, we add in 0.5 to the appropriate x/y so that the float arithmetic
// works out. For example, with a border width of 3, KHTML will pass us (y1+y2)/2, e.g.,
// (50+53)/2 = 103/2 = 51 when we want 51.5. It is always true that an even width gave
// us a perfect position, but an odd width gave us a position that is off by exactly 0.5.
if (style == DottedStroke || style == DashedStroke) {
if (p1.x() == p2.x()) {
p1.setY(p1.y() + strokeWidth);
p2.setY(p2.y() - strokeWidth);
}
else {
p1.setX(p1.x() + strokeWidth);
p2.setX(p2.x() - strokeWidth);
}
}
if (static_cast<int>(strokeWidth) % 2) {
if (p1.x() == p2.x()) {
// We're a vertical line. Adjust our x.
p1.setX(p1.x() + 0.5);
p2.setX(p2.x() + 0.5);
}
else {
// We're a horizontal line. Adjust our y.
p1.setY(p1.y() + 0.5);
p2.setY(p2.y() + 0.5);
}
}
}
// This is only used to draw borders.
void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2)
{
if (paintingDisabled())
return;
StrokeStyle style = strokeStyle();
if (style == NoStroke)
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
float width = strokeThickness();
if (width < 1)
width = 1;
FloatPoint p1 = point1;
FloatPoint p2 = point2;
bool isVerticalLine = (p1.x() == p2.x());
adjustLineToPixelBoundaries(p1, p2, width, style);
cairo_set_line_width(cr, width);
int patWidth = 0;
switch (style) {
case NoStroke:
case SolidStroke:
break;
case DottedStroke:
patWidth = static_cast<int>(width);
break;
case DashedStroke:
patWidth = 3*static_cast<int>(width);
break;
}
setColor(cr, strokeColor());
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
if (patWidth) {
// Do a rect fill of our endpoints. This ensures we always have the
// appearance of being a border. We then draw the actual dotted/dashed line.
if (isVerticalLine) {
fillRectSourceOver(cr, FloatRect(p1.x() - width/2, p1.y() - width, width, width), strokeColor());
fillRectSourceOver(cr, FloatRect(p2.x() - width/2, p2.y(), width, width), strokeColor());
} else {
fillRectSourceOver(cr, FloatRect(p1.x() - width, p1.y() - width/2, width, width), strokeColor());
fillRectSourceOver(cr, FloatRect(p2.x(), p2.y() - width/2, width, width), strokeColor());
}
// Example: 80 pixels with a width of 30 pixels.
// Remainder is 20. The maximum pixels of line we could paint
// will be 50 pixels.
int distance = (isVerticalLine ? (point2.y() - point1.y()) : (point2.x() - point1.x())) - 2*static_cast<int>(width);
int remainder = distance%patWidth;
int coverage = distance-remainder;
int numSegments = coverage/patWidth;
float patternOffset = 0;
// Special case 1px dotted borders for speed.
if (patWidth == 1)
patternOffset = 1.0;
else {
bool evenNumberOfSegments = numSegments%2 == 0;
if (remainder)
evenNumberOfSegments = !evenNumberOfSegments;
if (evenNumberOfSegments) {
if (remainder) {
patternOffset += patWidth - remainder;
patternOffset += remainder/2;
}
else
patternOffset = patWidth/2;
}
else if (!evenNumberOfSegments) {
if (remainder)
patternOffset = (patWidth - remainder)/2;
}
}
double dash = patWidth;
cairo_set_dash(cr, &dash, 1, patternOffset);
}
cairo_move_to(cr, p1.x(), p1.y());
cairo_line_to(cr, p2.x(), p2.y());
cairo_stroke(cr);
cairo_restore(cr);
}
// This method is only used to draw the little circles used in lists.
void GraphicsContext::drawEllipse(const IntRect& rect)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
float yRadius = .5 * rect.height();
float xRadius = .5 * rect.width();
cairo_translate(cr, rect.x() + xRadius, rect.y() + yRadius);
cairo_scale(cr, xRadius, yRadius);
cairo_arc(cr, 0., 0., 1., 0., 2 * M_PI);
cairo_restore(cr);
if (fillColor().alpha()) {
setColor(cr, fillColor());
cairo_fill_preserve(cr);
}
if (strokeStyle() != NoStroke) {
setColor(cr, strokeColor());
cairo_set_line_width(cr, strokeThickness());
cairo_stroke(cr);
}
cairo_new_path(cr);
}
// FIXME: This function needs to be adjusted to match the functionality on the Mac side.
void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan)
{
if (paintingDisabled())
return;
if (strokeStyle() == NoStroke)
return;
int x = rect.x();
int y = rect.y();
float w = rect.width();
#if 0 // FIXME: unused so far
float h = rect.height();
float scaleFactor = h / w;
float reverseScaleFactor = w / h;
#endif
float r = w / 2;
float fa = startAngle;
float falen = fa + angleSpan;
cairo_t* cr = m_data->cr;
cairo_save(cr);
cairo_arc_negative(cr, x + r, y + r, r, -fa * M_PI/180, -falen * M_PI/180);
setColor(cr, strokeColor());
cairo_set_line_width(cr, strokeThickness());
cairo_stroke(cr);
cairo_restore(cr);
}
void GraphicsContext::drawConvexPolygon(size_t npoints, const FloatPoint* points, bool shouldAntialias)
{
if (paintingDisabled())
return;
if (npoints <= 1)
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
cairo_set_antialias(cr, shouldAntialias ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE);
cairo_move_to(cr, points[0].x(), points[0].y());
for (size_t i = 1; i < npoints; i++)
cairo_line_to(cr, points[i].x(), points[i].y());
cairo_close_path(cr);
if (fillColor().alpha()) {
setColor(cr, fillColor());
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
cairo_fill_preserve(cr);
}
if (strokeStyle() != NoStroke) {
setColor(cr, strokeColor());
cairo_set_line_width(cr, strokeThickness());
cairo_stroke(cr);
}
cairo_new_path(cr);
cairo_restore(cr);
}
void GraphicsContext::fillRect(const IntRect& rect, const Color& color)
{
if (paintingDisabled())
return;
if (color.alpha())
fillRectSourceOver(m_data->cr, rect, color);
}
void GraphicsContext::fillRect(const FloatRect& rect, const Color& color)
{
if (paintingDisabled())
return;
if (color.alpha())
fillRectSourceOver(m_data->cr, rect, color);
}
void GraphicsContext::clip(const FloatRect& rect)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING);
cairo_clip(cr);
cairo_set_fill_rule(cr, savedFillRule);
m_data->clip(rect);
}
void GraphicsContext::drawFocusRing(const Color& color)
{
if (paintingDisabled())
return;
int radius = (focusRingWidth() - 1) / 2;
int offset = radius + focusRingOffset();
const Vector<IntRect>& rects = focusRingRects();
unsigned rectCount = rects.size();
IntRect finalFocusRect;
for (unsigned i = 0; i < rectCount; i++) {
IntRect focusRect = rects[i];
focusRect.inflate(offset);
finalFocusRect.unite(focusRect);
}
cairo_t* cr = m_data->cr;
cairo_save(cr);
// FIXME: These rects should be rounded
cairo_rectangle(cr, finalFocusRect.x(), finalFocusRect.y(), finalFocusRect.width(), finalFocusRect.height());
// Force the alpha to 50%. This matches what the Mac does with outline rings.
Color ringColor(color.red(), color.green(), color.blue(), 127);
setColor(cr, ringColor);
cairo_stroke(cr);
cairo_restore(cr);
}
void GraphicsContext::drawLineForText(const IntPoint& origin, int width, bool printing)
{
if (paintingDisabled())
return;
// This is a workaround for http://bugs.webkit.org/show_bug.cgi?id=15659
StrokeStyle savedStrokeStyle = strokeStyle();
setStrokeStyle(SolidStroke);
IntPoint endPoint = origin + IntSize(width, 0);
drawLine(origin, endPoint);
setStrokeStyle(savedStrokeStyle);
}
void GraphicsContext::drawLineForMisspellingOrBadGrammar(const IntPoint& origin, int width, bool grammar)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
// Convention is green for grammar, red for spelling
// These need to become configurable
if (grammar)
cairo_set_source_rgb(cr, 0, 1, 0);
else
cairo_set_source_rgb(cr, 1, 0, 0);
#if PLATFORM(GTK)
// We ignore most of the provided constants in favour of the platform style
pango_cairo_show_error_underline(cr, origin.x(), origin.y(), width, cMisspellingLineThickness);
#else
notImplemented();
#endif
cairo_restore(cr);
}
FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& frect)
{
FloatRect result;
double x = frect.x();
double y = frect.y();
cairo_t* cr = m_data->cr;
cairo_user_to_device(cr, &x, &y);
x = round(x);
y = round(y);
cairo_device_to_user(cr, &x, &y);
result.setX(static_cast<float>(x));
result.setY(static_cast<float>(y));
x = frect.width();
y = frect.height();
cairo_user_to_device_distance(cr, &x, &y);
x = round(x);
y = round(y);
cairo_device_to_user_distance(cr, &x, &y);
result.setWidth(static_cast<float>(x));
result.setHeight(static_cast<float>(y));
return result;
}
void GraphicsContext::translate(float x, float y)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_translate(cr, x, y);
m_data->translate(x, y);
}
IntPoint GraphicsContext::origin()
{
cairo_matrix_t matrix;
cairo_t* cr = m_data->cr;
cairo_get_matrix(cr, &matrix);
return IntPoint(static_cast<int>(matrix.x0), static_cast<int>(matrix.y0));
}
void GraphicsContext::setPlatformFillColor(const Color& col)
{
// FIXME: this is probably a no-op but I'm not sure
// notImplemented(); // commented-out because it's chatty and clutters output
}
void GraphicsContext::setPlatformStrokeColor(const Color& col)
{
// FIXME: this is probably a no-op but I'm not sure
//notImplemented(); // commented-out because it's chatty and clutters output
}
void GraphicsContext::setPlatformStrokeThickness(float strokeThickness)
{
if (paintingDisabled())
return;
cairo_set_line_width(m_data->cr, strokeThickness);
}
void GraphicsContext::setPlatformStrokeStyle(const StrokeStyle& strokeStyle)
{
static double dashPattern[] = {5.0, 5.0};
static double dotPattern[] = {1.0, 1.0};
if (paintingDisabled())
return;
switch (strokeStyle) {
case NoStroke:
// FIXME: is it the right way to emulate NoStroke?
cairo_set_line_width(m_data->cr, 0);
break;
case SolidStroke:
cairo_set_dash(m_data->cr, 0, 0, 0);
break;
case DottedStroke:
cairo_set_dash(m_data->cr, dotPattern, 2, 0);
break;
case DashedStroke:
cairo_set_dash(m_data->cr, dashPattern, 2, 0);
break;
default:
notImplemented();
break;
}
}
void GraphicsContext::setURLForRect(const KURL& link, const IntRect& destRect)
{
notImplemented();
}
void GraphicsContext::concatCTM(const AffineTransform& transform)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
const cairo_matrix_t* matrix = reinterpret_cast<const cairo_matrix_t*>(&transform);
cairo_transform(cr, matrix);
m_data->concatCTM(transform);
}
void GraphicsContext::addInnerRoundedRectClip(const IntRect& rect, int thickness)
{
if (paintingDisabled())
return;
clip(rect);
Path p;
FloatRect r(rect);
// Add outer ellipse
p.addEllipse(r);
// Add inner ellipse
r.inflate(-thickness);
p.addEllipse(r);
addPath(p);
cairo_t* cr = m_data->cr;
cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
cairo_clip(cr);
cairo_set_fill_rule(cr, savedFillRule);
}
void GraphicsContext::clipToImageBuffer(const FloatRect& rect, const ImageBuffer* imageBuffer)
{
if (paintingDisabled())
return;
notImplemented();
}
void GraphicsContext::setPlatformShadow(IntSize const&, int, Color const&)
{
notImplemented();
}
void GraphicsContext::clearPlatformShadow()
{
notImplemented();
}
void GraphicsContext::beginTransparencyLayer(float opacity)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_push_group(cr);
m_data->layers.append(opacity);
m_data->beginTransparencyLayer();
}
void GraphicsContext::endTransparencyLayer()
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_pop_group_to_source(cr);
cairo_paint_with_alpha(cr, m_data->layers.last());
m_data->layers.removeLast();
m_data->endTransparencyLayer();
}
void GraphicsContext::clearRect(const FloatRect& rect)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_fill(cr);
cairo_restore(cr);
}
void GraphicsContext::strokeRect(const FloatRect& rect, float width)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
setColor(cr, strokeColor());
cairo_set_line_width(cr, width);
cairo_stroke(cr);
cairo_restore(cr);
}
void GraphicsContext::setLineCap(LineCap lineCap)
{
if (paintingDisabled())
return;
cairo_line_cap_t cairoCap = CAIRO_LINE_CAP_BUTT;
switch (lineCap) {
case ButtCap:
// no-op
break;
case RoundCap:
cairoCap = CAIRO_LINE_CAP_ROUND;
break;
case SquareCap:
cairoCap = CAIRO_LINE_CAP_SQUARE;
break;
}
cairo_set_line_cap(m_data->cr, cairoCap);
}
void GraphicsContext::setLineJoin(LineJoin lineJoin)
{
if (paintingDisabled())
return;
cairo_line_join_t cairoJoin = CAIRO_LINE_JOIN_MITER;
switch (lineJoin) {
case MiterJoin:
// no-op
break;
case RoundJoin:
cairoJoin = CAIRO_LINE_JOIN_ROUND;
break;
case BevelJoin:
cairoJoin = CAIRO_LINE_JOIN_BEVEL;
break;
}
cairo_set_line_join(m_data->cr, cairoJoin);
}
void GraphicsContext::setMiterLimit(float miter)
{
if (paintingDisabled())
return;
cairo_set_miter_limit(m_data->cr, miter);
}
void GraphicsContext::setAlpha(float)
{
notImplemented();
}
static inline cairo_operator_t toCairoOperator(CompositeOperator op)
{
switch (op) {
case CompositeClear:
return CAIRO_OPERATOR_CLEAR;
case CompositeCopy:
return CAIRO_OPERATOR_SOURCE;
case CompositeSourceOver:
return CAIRO_OPERATOR_OVER;
case CompositeSourceIn:
return CAIRO_OPERATOR_IN;
case CompositeSourceOut:
return CAIRO_OPERATOR_OUT;
case CompositeSourceAtop:
return CAIRO_OPERATOR_ATOP;
case CompositeDestinationOver:
return CAIRO_OPERATOR_DEST_OVER;
case CompositeDestinationIn:
return CAIRO_OPERATOR_DEST_IN;
case CompositeDestinationOut:
return CAIRO_OPERATOR_DEST_OUT;
case CompositeDestinationAtop:
return CAIRO_OPERATOR_DEST_ATOP;
case CompositeXOR:
return CAIRO_OPERATOR_XOR;
case CompositePlusDarker:
return CAIRO_OPERATOR_SATURATE;
case CompositeHighlight:
// There is no Cairo equivalent for CompositeHighlight.
return CAIRO_OPERATOR_OVER;
case CompositePlusLighter:
return CAIRO_OPERATOR_ADD;
default:
return CAIRO_OPERATOR_SOURCE;
}
}
void GraphicsContext::setCompositeOperation(CompositeOperator op)
{
if (paintingDisabled())
return;
cairo_set_operator(m_data->cr, toCairoOperator(op));
}
void GraphicsContext::beginPath()
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_new_path(cr);
}
void GraphicsContext::addPath(const Path& path)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_path_t* p = cairo_copy_path(path.platformPath()->m_cr);
cairo_append_path(cr, p);
cairo_path_destroy(p);
}
void GraphicsContext::clip(const Path& path)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_path_t* p = cairo_copy_path(path.platformPath()->m_cr);
cairo_append_path(cr, p);
cairo_path_destroy(p);
cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING);
cairo_clip(cr);
cairo_set_fill_rule(cr, savedFillRule);
m_data->clip(path);
}
void GraphicsContext::clipOut(const Path& path)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
double x1, y1, x2, y2;
cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
cairo_rectangle(cr, x1, y1, x2 - x1, y2 - y1);
addPath(path);
cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
cairo_clip(cr);
cairo_set_fill_rule(cr, savedFillRule);
}
void GraphicsContext::rotate(float radians)
{
if (paintingDisabled())
return;
cairo_rotate(m_data->cr, radians);
m_data->rotate(radians);
}
void GraphicsContext::scale(const FloatSize& size)
{
if (paintingDisabled())
return;
cairo_scale(m_data->cr, size.width(), size.height());
m_data->scale(size);
}
void GraphicsContext::clipOut(const IntRect& r)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
double x1, y1, x2, y2;
cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
cairo_rectangle(cr, x1, x2, x2 - x1, y2 - y1);
cairo_rectangle(cr, r.x(), r.y(), r.width(), r.height());
cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
cairo_clip(cr);
cairo_set_fill_rule(cr, savedFillRule);
}
void GraphicsContext::clipOutEllipseInRect(const IntRect& r)
{
if (paintingDisabled())
return;
Path p;
p.addEllipse(r);
clipOut(p);
}
void GraphicsContext::fillRoundedRect(const IntRect& r, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color& color)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
beginPath();
addPath(Path::createRoundedRectangle(r, topLeft, topRight, bottomLeft, bottomRight));
setColor(cr, color);
cairo_fill(cr);
cairo_restore(cr);
}
#if PLATFORM(GTK)
void GraphicsContext::setGdkExposeEvent(GdkEventExpose* expose)
{
m_data->expose = expose;
}
GdkEventExpose* GraphicsContext::gdkExposeEvent() const
{
return m_data->expose;
}
GdkDrawable* GraphicsContext::gdkDrawable() const
{
if (!m_data->expose)
return 0;
return GDK_DRAWABLE(m_data->expose->window);
}
IntPoint GraphicsContext::translatePoint(const IntPoint& point) const
{
cairo_matrix_t tm;
cairo_get_matrix(m_data->cr, &tm);
double x = point.x();
double y = point.y();
cairo_matrix_transform_point(&tm, &x, &y);
return IntPoint(x, y);
}
#endif
void GraphicsContext::setUseAntialiasing(bool enable)
{
if (paintingDisabled())
return;
// When true, use the default Cairo backend antialias mode (usually this
// enables standard 'grayscale' antialiasing); false to explicitly disable
// antialiasing. This is the same strategy as used in drawConvexPolygon().
cairo_set_antialias(m_data->cr, enable ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE);
}
} // namespace WebCore
#endif // PLATFORM(CAIRO)
|