Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: Source/core/paint/BoxPainter.cpp

Issue 564973002: Move a bunch more painting code out of RenderBoxModelObject and into BoxPainter. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Merged, made more things static. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "core/paint/BoxPainter.h" 6 #include "core/paint/BoxPainter.h"
7 7
8 #include "core/HTMLNames.h" 8 #include "core/HTMLNames.h"
9 #include "core/frame/Settings.h" 9 #include "core/frame/Settings.h"
10 #include "core/html/HTMLFrameOwnerElement.h" 10 #include "core/html/HTMLFrameOwnerElement.h"
11 #include "core/paint/BackgroundImageGeometry.h" 11 #include "core/paint/BackgroundImageGeometry.h"
12 #include "core/paint/BoxDecorationData.h" 12 #include "core/paint/BoxDecorationData.h"
13 #include "core/rendering/ImageQualityController.h" 13 #include "core/rendering/ImageQualityController.h"
14 #include "core/rendering/PaintInfo.h" 14 #include "core/rendering/PaintInfo.h"
15 #include "core/rendering/RenderBox.h" 15 #include "core/rendering/RenderBox.h"
16 #include "core/rendering/RenderBoxModelObject.h" 16 #include "core/rendering/RenderBoxModelObject.h"
17 #include "core/rendering/RenderLayer.h" 17 #include "core/rendering/RenderLayer.h"
18 #include "core/rendering/RenderObject.h"
18 #include "core/rendering/RenderTable.h" 19 #include "core/rendering/RenderTable.h"
19 #include "core/rendering/RenderTheme.h" 20 #include "core/rendering/RenderTheme.h"
20 #include "core/rendering/RenderView.h" 21 #include "core/rendering/RenderView.h"
21 #include "core/rendering/compositing/CompositedLayerMapping.h" 22 #include "core/rendering/compositing/CompositedLayerMapping.h"
23 #include "core/rendering/style/BorderEdge.h"
22 #include "core/rendering/style/ShadowList.h" 24 #include "core/rendering/style/ShadowList.h"
23 #include "platform/LengthFunctions.h" 25 #include "platform/LengthFunctions.h"
24 #include "platform/geometry/LayoutPoint.h" 26 #include "platform/geometry/LayoutPoint.h"
25 #include "platform/graphics/GraphicsContextStateSaver.h" 27 #include "platform/graphics/GraphicsContextStateSaver.h"
26 28
27 namespace blink { 29 namespace blink {
28 30
29 void BoxPainter::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset) 31 void BoxPainter::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
30 { 32 {
31 LayoutPoint adjustedPaintOffset = paintOffset + m_renderBox.location(); 33 LayoutPoint adjustedPaintOffset = paintOffset + m_renderBox.location();
(...skipping 15 matching lines...) Expand all
47 } 49 }
48 50
49 void BoxPainter::paintBoxDecorationBackgroundWithRect(PaintInfo& paintInfo, cons t LayoutPoint& paintOffset, const LayoutRect& paintRect) 51 void BoxPainter::paintBoxDecorationBackgroundWithRect(PaintInfo& paintInfo, cons t LayoutPoint& paintOffset, const LayoutRect& paintRect)
50 { 52 {
51 RenderStyle* style = m_renderBox.style(); 53 RenderStyle* style = m_renderBox.style();
52 BoxDecorationData boxDecorationData(*style, m_renderBox.canRenderBorderImage (), m_renderBox.backgroundHasOpaqueTopLayer(), paintInfo.context); 54 BoxDecorationData boxDecorationData(*style, m_renderBox.canRenderBorderImage (), m_renderBox.backgroundHasOpaqueTopLayer(), paintInfo.context);
53 55
54 // FIXME: Should eventually give the theme control over whether the box shad ow should paint, since controls could have 56 // FIXME: Should eventually give the theme control over whether the box shad ow should paint, since controls could have
55 // custom shadows of their own. 57 // custom shadows of their own.
56 if (!m_renderBox.boxShadowShouldBeAppliedToBackground(boxDecorationData.blee dAvoidance())) 58 if (!m_renderBox.boxShadowShouldBeAppliedToBackground(boxDecorationData.blee dAvoidance()))
57 m_renderBox.paintBoxShadow(paintInfo, paintRect, style, Normal); 59 paintBoxShadow(paintInfo, paintRect, style, Normal);
58 60
59 GraphicsContextStateSaver stateSaver(*paintInfo.context, false); 61 GraphicsContextStateSaver stateSaver(*paintInfo.context, false);
60 if (boxDecorationData.bleedAvoidance() == BackgroundBleedClipBackground) { 62 if (boxDecorationData.bleedAvoidance() == BackgroundBleedClipBackground) {
61 stateSaver.save(); 63 stateSaver.save();
62 RoundedRect border = style->getRoundedBorderFor(paintRect); 64 RoundedRect border = style->getRoundedBorderFor(paintRect);
63 paintInfo.context->clipRoundedRect(border); 65 paintInfo.context->clipRoundedRect(border);
64 } 66 }
65 67
66 // If we have a native theme appearance, paint that before painting our back ground. 68 // If we have a native theme appearance, paint that before painting our back ground.
67 // The theme will tell us whether or not we should also paint the CSS backgr ound. 69 // The theme will tell us whether or not we should also paint the CSS backgr ound.
68 IntRect snappedPaintRect(pixelSnappedIntRect(paintRect)); 70 IntRect snappedPaintRect(pixelSnappedIntRect(paintRect));
69 bool themePainted = boxDecorationData.hasAppearance && !RenderTheme::theme() .paint(&m_renderBox, paintInfo, snappedPaintRect); 71 bool themePainted = boxDecorationData.hasAppearance && !RenderTheme::theme() .paint(&m_renderBox, paintInfo, snappedPaintRect);
70 if (!themePainted) { 72 if (!themePainted) {
71 if (boxDecorationData.bleedAvoidance() == BackgroundBleedBackgroundOverB order) 73 if (boxDecorationData.bleedAvoidance() == BackgroundBleedBackgroundOverB order)
72 m_renderBox.paintBorder(paintInfo, paintRect, style, boxDecorationDa ta.bleedAvoidance()); 74 paintBorder(m_renderBox, paintInfo, paintRect, style, boxDecorationD ata.bleedAvoidance());
73 75
74 paintBackground(paintInfo, paintRect, boxDecorationData.backgroundColor, boxDecorationData.bleedAvoidance()); 76 paintBackground(paintInfo, paintRect, boxDecorationData.backgroundColor, boxDecorationData.bleedAvoidance());
75 77
76 if (boxDecorationData.hasAppearance) 78 if (boxDecorationData.hasAppearance)
77 RenderTheme::theme().paintDecorations(&m_renderBox, paintInfo, snapp edPaintRect); 79 RenderTheme::theme().paintDecorations(&m_renderBox, paintInfo, snapp edPaintRect);
78 } 80 }
79 m_renderBox.paintBoxShadow(paintInfo, paintRect, style, Inset); 81 paintBoxShadow(paintInfo, paintRect, style, Inset);
80 82
81 // The theme will tell us whether or not we should also paint the CSS border . 83 // The theme will tell us whether or not we should also paint the CSS border .
82 if (boxDecorationData.hasBorder && boxDecorationData.bleedAvoidance() != Bac kgroundBleedBackgroundOverBorder 84 if (boxDecorationData.hasBorder && boxDecorationData.bleedAvoidance() != Bac kgroundBleedBackgroundOverBorder
83 && (!boxDecorationData.hasAppearance || (!themePainted && RenderTheme::t heme().paintBorderOnly(&m_renderBox, paintInfo, snappedPaintRect))) 85 && (!boxDecorationData.hasAppearance || (!themePainted && RenderTheme::t heme().paintBorderOnly(&m_renderBox, paintInfo, snappedPaintRect)))
84 && !(m_renderBox.isTable() && toRenderTable(&m_renderBox)->collapseBorde rs())) 86 && !(m_renderBox.isTable() && toRenderTable(&m_renderBox)->collapseBorde rs()))
85 m_renderBox.paintBorder(paintInfo, paintRect, style, boxDecorationData.b leedAvoidance()); 87 paintBorder(m_renderBox, paintInfo, paintRect, style, boxDecorationData. bleedAvoidance());
86 } 88 }
87 89
88 static bool skipBodyBackground(const RenderBox* bodyElementRenderer) 90 static bool skipBodyBackground(const RenderBox* bodyElementRenderer)
89 { 91 {
90 ASSERT(bodyElementRenderer->isBody()); 92 ASSERT(bodyElementRenderer->isBody());
91 // The <body> only paints its background if the root element has defined a b ackground independent of the body, 93 // The <body> only paints its background if the root element has defined a b ackground independent of the body,
92 // or if the <body>'s parent is not the document element's renderer (e.g. in side SVG foreignObject). 94 // or if the <body>'s parent is not the document element's renderer (e.g. in side SVG foreignObject).
93 RenderObject* documentElementRenderer = bodyElementRenderer->document().docu mentElement()->renderer(); 95 RenderObject* documentElementRenderer = bodyElementRenderer->document().docu mentElement()->renderer();
94 return documentElementRenderer 96 return documentElementRenderer
95 && !documentElementRenderer->hasBackground() 97 && !documentElementRenderer->hasBackground()
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 520
519 allMaskImagesLoaded &= maskLayers.imagesAreLoaded(); 521 allMaskImagesLoaded &= maskLayers.imagesAreLoaded();
520 522
521 paintInfo.context->setCompositeOperation(CompositeDestinationIn); 523 paintInfo.context->setCompositeOperation(CompositeDestinationIn);
522 paintInfo.context->beginTransparencyLayer(1); 524 paintInfo.context->beginTransparencyLayer(1);
523 compositeOp = CompositeSourceOver; 525 compositeOp = CompositeSourceOver;
524 } 526 }
525 527
526 if (allMaskImagesLoaded) { 528 if (allMaskImagesLoaded) {
527 paintFillLayers(paintInfo, Color::transparent, m_renderBox.style()->mask Layers(), paintRect, BackgroundBleedNone, compositeOp); 529 paintFillLayers(paintInfo, Color::transparent, m_renderBox.style()->mask Layers(), paintRect, BackgroundBleedNone, compositeOp);
528 m_renderBox.paintNinePieceImage(paintInfo.context, paintRect, m_renderBo x.style(), m_renderBox.style()->maskBoxImage(), compositeOp); 530 paintNinePieceImage(m_renderBox, paintInfo.context, paintRect, m_renderB ox.style(), m_renderBox.style()->maskBoxImage(), compositeOp);
529 } 531 }
530 532
531 if (pushTransparencyLayer) 533 if (pushTransparencyLayer)
532 paintInfo.context->endLayer(); 534 paintInfo.context->endLayer();
533 } 535 }
534 536
535 void BoxPainter::paintClippingMask(PaintInfo& paintInfo, const LayoutPoint& pain tOffset) 537 void BoxPainter::paintClippingMask(PaintInfo& paintInfo, const LayoutPoint& pain tOffset)
536 { 538 {
537 if (!paintInfo.shouldPaintWithinRoot(&m_renderBox) || m_renderBox.style()->v isibility() != VISIBLE || paintInfo.phase != PaintPhaseClippingMask) 539 if (!paintInfo.shouldPaintWithinRoot(&m_renderBox) || m_renderBox.style()->v isibility() != VISIBLE || paintInfo.phase != PaintPhaseClippingMask)
538 return; 540 return;
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 ? static_cast<float>(positioningAreaSize.height()) / imageIntrinsicS ize.height() : 1; 859 ? static_cast<float>(positioningAreaSize.height()) / imageIntrinsicS ize.height() : 1;
858 float scaleFactor = type == Contain ? std::min(horizontalScaleFactor, ve rticalScaleFactor) : std::max(horizontalScaleFactor, verticalScaleFactor); 860 float scaleFactor = type == Contain ? std::min(horizontalScaleFactor, ve rticalScaleFactor) : std::max(horizontalScaleFactor, verticalScaleFactor);
859 return IntSize(std::max(1l, lround(imageIntrinsicSize.width() * scaleFac tor)), std::max(1l, lround(imageIntrinsicSize.height() * scaleFactor))); 861 return IntSize(std::max(1l, lround(imageIntrinsicSize.width() * scaleFac tor)), std::max(1l, lround(imageIntrinsicSize.height() * scaleFactor)));
860 } 862 }
861 } 863 }
862 864
863 ASSERT_NOT_REACHED(); 865 ASSERT_NOT_REACHED();
864 return IntSize(); 866 return IntSize();
865 } 867 }
866 868
869 static LayoutUnit computeBorderImageSide(const BorderImageLength& borderSlice, L ayoutUnit borderSide, LayoutUnit imageSide, LayoutUnit boxExtent)
870 {
871 if (borderSlice.isNumber())
872 return borderSlice.number() * borderSide;
873 if (borderSlice.length().isAuto())
874 return imageSide;
875 return valueForLength(borderSlice.length(), boxExtent);
876 }
877
878 bool BoxPainter::paintNinePieceImage(RenderBoxModelObject& obj, GraphicsContext* graphicsContext, const LayoutRect& rect, const RenderStyle* style, const NinePi eceImage& ninePieceImage, CompositeOperator op)
879 {
880 StyleImage* styleImage = ninePieceImage.image();
881 if (!styleImage)
882 return false;
883
884 if (!styleImage->isLoaded())
885 return true; // Never paint a nine-piece image incrementally, but don't paint the fallback borders either.
886
887 if (!styleImage->canRender(obj, style->effectiveZoom()))
888 return false;
889
890 // FIXME: border-image is broken with full page zooming when tiling has to h appen, since the tiling function
891 // doesn't have any understanding of the zoom that is in effect on the tile.
892 LayoutRect rectWithOutsets = rect;
893 rectWithOutsets.expand(style->imageOutsets(ninePieceImage));
894 IntRect borderImageRect = pixelSnappedIntRect(rectWithOutsets);
895
896 IntSize imageSize = obj.calculateImageIntrinsicDimensions(styleImage, border ImageRect.size(), RenderBoxModelObject::DoNotScaleByEffectiveZoom);
897
898 // If both values are ‘auto’ then the intrinsic width and/or height of the i mage should be used, if any.
899 styleImage->setContainerSizeForRenderer(&obj, imageSize, style->effectiveZoo m());
900
901 int imageWidth = imageSize.width();
902 int imageHeight = imageSize.height();
903
904 float imageScaleFactor = styleImage->imageScaleFactor();
905 int topSlice = std::min<int>(imageHeight, valueForLength(ninePieceImage.imag eSlices().top(), imageHeight)) * imageScaleFactor;
906 int rightSlice = std::min<int>(imageWidth, valueForLength(ninePieceImage.ima geSlices().right(), imageWidth)) * imageScaleFactor;
907 int bottomSlice = std::min<int>(imageHeight, valueForLength(ninePieceImage.i mageSlices().bottom(), imageHeight)) * imageScaleFactor;
908 int leftSlice = std::min<int>(imageWidth, valueForLength(ninePieceImage.imag eSlices().left(), imageWidth)) * imageScaleFactor;
909
910 ENinePieceImageRule hRule = ninePieceImage.horizontalRule();
911 ENinePieceImageRule vRule = ninePieceImage.verticalRule();
912
913 int topWidth = computeBorderImageSide(ninePieceImage.borderSlices().top(), s tyle->borderTopWidth(), topSlice, borderImageRect.height());
914 int rightWidth = computeBorderImageSide(ninePieceImage.borderSlices().right( ), style->borderRightWidth(), rightSlice, borderImageRect.width());
915 int bottomWidth = computeBorderImageSide(ninePieceImage.borderSlices().botto m(), style->borderBottomWidth(), bottomSlice, borderImageRect.height());
916 int leftWidth = computeBorderImageSide(ninePieceImage.borderSlices().left(), style->borderLeftWidth(), leftSlice, borderImageRect.width());
917
918 // Reduce the widths if they're too large.
919 // The spec says: Given Lwidth as the width of the border image area, Lheigh t as its height, and Wside as the border image width
920 // offset for the side, let f = min(Lwidth/(Wleft+Wright), Lheight/(Wtop+Wbo ttom)). If f < 1, then all W are reduced by
921 // multiplying them by f.
922 int borderSideWidth = std::max(1, leftWidth + rightWidth);
923 int borderSideHeight = std::max(1, topWidth + bottomWidth);
924 float borderSideScaleFactor = std::min((float)borderImageRect.width() / bord erSideWidth, (float)borderImageRect.height() / borderSideHeight);
925 if (borderSideScaleFactor < 1) {
926 topWidth *= borderSideScaleFactor;
927 rightWidth *= borderSideScaleFactor;
928 bottomWidth *= borderSideScaleFactor;
929 leftWidth *= borderSideScaleFactor;
930 }
931
932 bool drawLeft = leftSlice > 0 && leftWidth > 0;
933 bool drawTop = topSlice > 0 && topWidth > 0;
934 bool drawRight = rightSlice > 0 && rightWidth > 0;
935 bool drawBottom = bottomSlice > 0 && bottomWidth > 0;
936 bool drawMiddle = ninePieceImage.fill() && (imageWidth - leftSlice - rightSl ice) > 0 && (borderImageRect.width() - leftWidth - rightWidth) > 0
937 && (imageHeight - topSlice - bottomSlice) > 0 && (borderImageRect.height () - topWidth - bottomWidth) > 0;
938
939 RefPtr<Image> image = styleImage->image(&obj, imageSize);
940
941 float destinationWidth = borderImageRect.width() - leftWidth - rightWidth;
942 float destinationHeight = borderImageRect.height() - topWidth - bottomWidth;
943
944 float sourceWidth = imageWidth - leftSlice - rightSlice;
945 float sourceHeight = imageHeight - topSlice - bottomSlice;
946
947 float leftSideScale = drawLeft ? (float)leftWidth / leftSlice : 1;
948 float rightSideScale = drawRight ? (float)rightWidth / rightSlice : 1;
949 float topSideScale = drawTop ? (float)topWidth / topSlice : 1;
950 float bottomSideScale = drawBottom ? (float)bottomWidth / bottomSlice : 1;
951
952 if (drawLeft) {
953 // Paint the top and bottom left corners.
954
955 // The top left corner rect is (tx, ty, leftWidth, topWidth)
956 // The rect to use from within the image is obtained from our slice, and is (0, 0, leftSlice, topSlice)
957 if (drawTop) {
958 graphicsContext->drawImage(image.get(), IntRect(borderImageRect.loca tion(), IntSize(leftWidth, topWidth)),
959 LayoutRect(0, 0, leftSlice, topSlice), op);
960 }
961
962 // The bottom left corner rect is (tx, ty + h - bottomWidth, leftWidth, bottomWidth)
963 // The rect to use from within the image is (0, imageHeight - bottomSlic e, leftSlice, botomSlice)
964 if (drawBottom) {
965 graphicsContext->drawImage(image.get(), IntRect(borderImageRect.x(), borderImageRect.maxY() - bottomWidth, leftWidth, bottomWidth),
966 LayoutRect(0, imageHeight - bottomSlice, leftSlice, bottomSlice) , op);
967 }
968
969 // Paint the left edge.
970 // Have to scale and tile into the border rect.
971 if (sourceHeight > 0) {
972 graphicsContext->drawTiledImage(image.get(), IntRect(borderImageRect .x(), borderImageRect.y() + topWidth, leftWidth, destinationHeight),
973 IntRect(0, topSlice, leftSlice, sourceHeight), FloatSize(leftSid eScale, leftSideScale), Image::StretchTile, (Image::TileRule)vRule, op);
974 }
975 }
976
977 if (drawRight) {
978 // Paint the top and bottom right corners
979 // The top right corner rect is (tx + w - rightWidth, ty, rightWidth, to pWidth)
980 // The rect to use from within the image is obtained from our slice, and is (imageWidth - rightSlice, 0, rightSlice, topSlice)
981 if (drawTop) {
982 graphicsContext->drawImage(image.get(), IntRect(borderImageRect.maxX () - rightWidth, borderImageRect.y(), rightWidth, topWidth),
983 LayoutRect(imageWidth - rightSlice, 0, rightSlice, topSlice), op );
984 }
985
986 // The bottom right corner rect is (tx + w - rightWidth, ty + h - bottom Width, rightWidth, bottomWidth)
987 // The rect to use from within the image is (imageWidth - rightSlice, im ageHeight - bottomSlice, rightSlice, bottomSlice)
988 if (drawBottom) {
989 graphicsContext->drawImage(image.get(), IntRect(borderImageRect.maxX () - rightWidth, borderImageRect.maxY() - bottomWidth, rightWidth, bottomWidth),
990 LayoutRect(imageWidth - rightSlice, imageHeight - bottomSlice, r ightSlice, bottomSlice), op);
991 }
992
993 // Paint the right edge.
994 if (sourceHeight > 0) {
995 graphicsContext->drawTiledImage(image.get(), IntRect(borderImageRect .maxX() - rightWidth, borderImageRect.y() + topWidth, rightWidth,
996 destinationHeight),
997 IntRect(imageWidth - rightSlice, topSlice, rightSlice, sourceHei ght),
998 FloatSize(rightSideScale, rightSideScale),
999 Image::StretchTile, (Image::TileRule)vRule, op);
1000 }
1001 }
1002
1003 // Paint the top edge.
1004 if (drawTop && sourceWidth > 0) {
1005 graphicsContext->drawTiledImage(image.get(), IntRect(borderImageRect.x() + leftWidth, borderImageRect.y(), destinationWidth, topWidth),
1006 IntRect(leftSlice, 0, sourceWidth, topSlice),
1007 FloatSize(topSideScale, topSideScale), (Image::TileRule)hRule, Image ::StretchTile, op);
1008 }
1009
1010 // Paint the bottom edge.
1011 if (drawBottom && sourceWidth > 0) {
1012 graphicsContext->drawTiledImage(image.get(), IntRect(borderImageRect.x() + leftWidth, borderImageRect.maxY() - bottomWidth,
1013 destinationWidth, bottomWidth),
1014 IntRect(leftSlice, imageHeight - bottomSlice, sourceWidth, bottomSli ce),
1015 FloatSize(bottomSideScale, bottomSideScale),
1016 (Image::TileRule)hRule, Image::StretchTile, op);
1017 }
1018
1019 // Paint the middle.
1020 if (drawMiddle) {
1021 FloatSize middleScaleFactor(1, 1);
1022 if (drawTop)
1023 middleScaleFactor.setWidth(topSideScale);
1024 else if (drawBottom)
1025 middleScaleFactor.setWidth(bottomSideScale);
1026 if (drawLeft)
1027 middleScaleFactor.setHeight(leftSideScale);
1028 else if (drawRight)
1029 middleScaleFactor.setHeight(rightSideScale);
1030
1031 // For "stretch" rules, just override the scale factor and replace. We o nly had to do this for the
1032 // center tile, since sides don't even use the scale factor unless they have a rule other than "stretch".
1033 // The middle however can have "stretch" specified in one axis but not t he other, so we have to
1034 // correct the scale here.
1035 if (hRule == StretchImageRule)
1036 middleScaleFactor.setWidth(destinationWidth / sourceWidth);
1037
1038 if (vRule == StretchImageRule)
1039 middleScaleFactor.setHeight(destinationHeight / sourceHeight);
1040
1041 graphicsContext->drawTiledImage(image.get(),
1042 IntRect(borderImageRect.x() + leftWidth, borderImageRect.y() + topWi dth, destinationWidth, destinationHeight),
1043 IntRect(leftSlice, topSlice, sourceWidth, sourceHeight),
1044 middleScaleFactor, (Image::TileRule)hRule, (Image::TileRule)vRule, o p);
1045 }
1046
1047 return true;
1048 }
1049
1050 static IntRect calculateSideRect(const RoundedRect& outerBorder, const BorderEdg e edges[], int side)
1051 {
1052 IntRect sideRect = outerBorder.rect();
1053 int width = edges[side].width;
1054
1055 if (side == BSTop)
1056 sideRect.setHeight(width);
1057 else if (side == BSBottom)
1058 sideRect.shiftYEdgeTo(sideRect.maxY() - width);
1059 else if (side == BSLeft)
1060 sideRect.setWidth(width);
1061 else
1062 sideRect.shiftXEdgeTo(sideRect.maxX() - width);
1063
1064 return sideRect;
1065 }
1066
1067 enum BorderEdgeFlag {
1068 TopBorderEdge = 1 << BSTop,
1069 RightBorderEdge = 1 << BSRight,
1070 BottomBorderEdge = 1 << BSBottom,
1071 LeftBorderEdge = 1 << BSLeft,
1072 AllBorderEdges = TopBorderEdge | BottomBorderEdge | LeftBorderEdge | RightBo rderEdge
1073 };
1074
1075 static inline BorderEdgeFlag edgeFlagForSide(BoxSide side)
1076 {
1077 return static_cast<BorderEdgeFlag>(1 << side);
1078 }
1079
1080 static inline bool includesEdge(BorderEdgeFlags flags, BoxSide side)
1081 {
1082 return flags & edgeFlagForSide(side);
1083 }
1084
1085 inline bool styleRequiresClipPolygon(EBorderStyle style)
1086 {
1087 return style == DOTTED || style == DASHED; // These are drawn with a stroke, so we have to clip to get corner miters.
1088 }
1089
1090 static bool borderStyleFillsBorderArea(EBorderStyle style)
1091 {
1092 return !(style == DOTTED || style == DASHED || style == DOUBLE);
1093 }
1094
1095 static bool borderStyleHasInnerDetail(EBorderStyle style)
1096 {
1097 return style == GROOVE || style == RIDGE || style == DOUBLE;
1098 }
1099
1100 static bool borderStyleIsDottedOrDashed(EBorderStyle style)
1101 {
1102 return style == DOTTED || style == DASHED;
1103 }
1104
1105 // OUTSET darkens the bottom and right (and maybe lightens the top and left)
1106 // INSET darkens the top and left (and maybe lightens the bottom and right)
1107 static inline bool borderStyleHasUnmatchedColorsAtCorner(EBorderStyle style, Box Side side, BoxSide adjacentSide)
1108 {
1109 // These styles match at the top/left and bottom/right.
1110 if (style == INSET || style == GROOVE || style == RIDGE || style == OUTSET) {
1111 const BorderEdgeFlags topRightFlags = edgeFlagForSide(BSTop) | edgeFlagF orSide(BSRight);
1112 const BorderEdgeFlags bottomLeftFlags = edgeFlagForSide(BSBottom) | edge FlagForSide(BSLeft);
1113
1114 BorderEdgeFlags flags = edgeFlagForSide(side) | edgeFlagForSide(adjacent Side);
1115 return flags == topRightFlags || flags == bottomLeftFlags;
1116 }
1117 return false;
1118 }
1119
1120 static inline bool colorsMatchAtCorner(BoxSide side, BoxSide adjacentSide, const BorderEdge edges[])
1121 {
1122 if (edges[side].shouldRender() != edges[adjacentSide].shouldRender())
1123 return false;
1124
1125 if (!edges[side].sharesColorWith(edges[adjacentSide]))
1126 return false;
1127
1128 return !borderStyleHasUnmatchedColorsAtCorner(edges[side].borderStyle(), sid e, adjacentSide);
1129 }
1130
1131 static inline bool colorNeedsAntiAliasAtCorner(BoxSide side, BoxSide adjacentSid e, const BorderEdge edges[])
1132 {
1133 if (!edges[side].color.hasAlpha())
1134 return false;
1135
1136 if (edges[side].shouldRender() != edges[adjacentSide].shouldRender())
1137 return false;
1138
1139 if (!edges[side].sharesColorWith(edges[adjacentSide]))
1140 return true;
1141
1142 return borderStyleHasUnmatchedColorsAtCorner(edges[side].borderStyle(), side , adjacentSide);
1143 }
1144
1145 bool BoxPainter::shouldAntialiasLines(GraphicsContext* context)
1146 {
1147 // FIXME: We may want to not antialias when scaled by an integral value,
1148 // and we may want to antialias when translated by a non-integral value.
1149 // FIXME: See crbug.com/382491. getCTM does not include scale factors applie d at raster time, such
1150 // as device zoom.
1151 return !context->getCTM().isIdentityOrTranslationOrFlipped();
1152 }
1153
1154 static bool borderWillArcInnerEdge(const LayoutSize& firstRadius, const FloatSiz e& secondRadius)
1155 {
1156 return !firstRadius.isZero() || !secondRadius.isZero();
1157 }
1158
1159 // This assumes that we draw in order: top, bottom, left, right.
1160 static inline bool willBeOverdrawn(BoxSide side, BoxSide adjacentSide, const Bor derEdge edges[])
1161 {
1162 switch (side) {
1163 case BSTop:
1164 case BSBottom:
1165 if (edges[adjacentSide].presentButInvisible())
1166 return false;
1167
1168 if (!edges[side].sharesColorWith(edges[adjacentSide]) && edges[adjacentS ide].color.hasAlpha())
1169 return false;
1170
1171 if (!borderStyleFillsBorderArea(edges[adjacentSide].borderStyle()))
1172 return false;
1173
1174 return true;
1175
1176 case BSLeft:
1177 case BSRight:
1178 // These draw last, so are never overdrawn.
1179 return false;
1180 }
1181 return false;
1182 }
1183
1184 static inline bool borderStylesRequireMitre(BoxSide side, BoxSide adjacentSide, EBorderStyle style, EBorderStyle adjacentStyle)
1185 {
1186 if (style == DOUBLE || adjacentStyle == DOUBLE || adjacentStyle == GROOVE || adjacentStyle == RIDGE)
1187 return true;
1188
1189 if (borderStyleIsDottedOrDashed(style) != borderStyleIsDottedOrDashed(adjace ntStyle))
1190 return true;
1191
1192 if (style != adjacentStyle)
1193 return true;
1194
1195 return borderStyleHasUnmatchedColorsAtCorner(style, side, adjacentSide);
1196 }
1197
1198 static bool joinRequiresMitre(BoxSide side, BoxSide adjacentSide, const BorderEd ge edges[], bool allowOverdraw)
1199 {
1200 if ((edges[side].isTransparent && edges[adjacentSide].isTransparent) || !edg es[adjacentSide].isPresent)
1201 return false;
1202
1203 if (allowOverdraw && willBeOverdrawn(side, adjacentSide, edges))
1204 return false;
1205
1206 if (!edges[side].sharesColorWith(edges[adjacentSide]))
1207 return true;
1208
1209 if (borderStylesRequireMitre(side, adjacentSide, edges[side].borderStyle(), edges[adjacentSide].borderStyle()))
1210 return true;
1211
1212 return false;
1213 }
1214
1215 static IntRect calculateSideRectIncludingInner(const RoundedRect& outerBorder, c onst BorderEdge edges[], BoxSide side)
1216 {
1217 IntRect sideRect = outerBorder.rect();
1218 int width;
1219
1220 switch (side) {
1221 case BSTop:
1222 width = sideRect.height() - edges[BSBottom].width;
1223 sideRect.setHeight(width);
1224 break;
1225 case BSBottom:
1226 width = sideRect.height() - edges[BSTop].width;
1227 sideRect.shiftYEdgeTo(sideRect.maxY() - width);
1228 break;
1229 case BSLeft:
1230 width = sideRect.width() - edges[BSRight].width;
1231 sideRect.setWidth(width);
1232 break;
1233 case BSRight:
1234 width = sideRect.width() - edges[BSLeft].width;
1235 sideRect.shiftXEdgeTo(sideRect.maxX() - width);
1236 break;
1237 }
1238
1239 return sideRect;
1240 }
1241
1242 static RoundedRect calculateAdjustedInnerBorder(const RoundedRect&innerBorder, B oxSide side)
1243 {
1244 // Expand the inner border as necessary to make it a rounded rect (i.e. radi i contained within each edge).
1245 // This function relies on the fact we only get radii not contained within e ach edge if one of the radii
1246 // for an edge is zero, so we can shift the arc towards the zero radius corn er.
1247 RoundedRect::Radii newRadii = innerBorder.radii();
1248 IntRect newRect = innerBorder.rect();
1249
1250 float overshoot;
1251 float maxRadii;
1252
1253 switch (side) {
1254 case BSTop:
1255 overshoot = newRadii.topLeft().width() + newRadii.topRight().width() - n ewRect.width();
1256 if (overshoot > 0) {
1257 ASSERT(!(newRadii.topLeft().width() && newRadii.topRight().width())) ;
1258 newRect.setWidth(newRect.width() + overshoot);
1259 if (!newRadii.topLeft().width())
1260 newRect.move(-overshoot, 0);
1261 }
1262 newRadii.setBottomLeft(IntSize(0, 0));
1263 newRadii.setBottomRight(IntSize(0, 0));
1264 maxRadii = std::max(newRadii.topLeft().height(), newRadii.topRight().hei ght());
1265 if (maxRadii > newRect.height())
1266 newRect.setHeight(maxRadii);
1267 break;
1268
1269 case BSBottom:
1270 overshoot = newRadii.bottomLeft().width() + newRadii.bottomRight().width () - newRect.width();
1271 if (overshoot > 0) {
1272 ASSERT(!(newRadii.bottomLeft().width() && newRadii.bottomRight().wid th()));
1273 newRect.setWidth(newRect.width() + overshoot);
1274 if (!newRadii.bottomLeft().width())
1275 newRect.move(-overshoot, 0);
1276 }
1277 newRadii.setTopLeft(IntSize(0, 0));
1278 newRadii.setTopRight(IntSize(0, 0));
1279 maxRadii = std::max(newRadii.bottomLeft().height(), newRadii.bottomRight ().height());
1280 if (maxRadii > newRect.height()) {
1281 newRect.move(0, newRect.height() - maxRadii);
1282 newRect.setHeight(maxRadii);
1283 }
1284 break;
1285
1286 case BSLeft:
1287 overshoot = newRadii.topLeft().height() + newRadii.bottomLeft().height() - newRect.height();
1288 if (overshoot > 0) {
1289 ASSERT(!(newRadii.topLeft().height() && newRadii.bottomLeft().height ()));
1290 newRect.setHeight(newRect.height() + overshoot);
1291 if (!newRadii.topLeft().height())
1292 newRect.move(0, -overshoot);
1293 }
1294 newRadii.setTopRight(IntSize(0, 0));
1295 newRadii.setBottomRight(IntSize(0, 0));
1296 maxRadii = std::max(newRadii.topLeft().width(), newRadii.bottomLeft().wi dth());
1297 if (maxRadii > newRect.width())
1298 newRect.setWidth(maxRadii);
1299 break;
1300
1301 case BSRight:
1302 overshoot = newRadii.topRight().height() + newRadii.bottomRight().height () - newRect.height();
1303 if (overshoot > 0) {
1304 ASSERT(!(newRadii.topRight().height() && newRadii.bottomRight().heig ht()));
1305 newRect.setHeight(newRect.height() + overshoot);
1306 if (!newRadii.topRight().height())
1307 newRect.move(0, -overshoot);
1308 }
1309 newRadii.setTopLeft(IntSize(0, 0));
1310 newRadii.setBottomLeft(IntSize(0, 0));
1311 maxRadii = std::max(newRadii.topRight().width(), newRadii.bottomRight(). width());
1312 if (maxRadii > newRect.width()) {
1313 newRect.move(newRect.width() - maxRadii, 0);
1314 newRect.setWidth(maxRadii);
1315 }
1316 break;
1317 }
1318
1319 return RoundedRect(newRect, newRadii);
1320 }
1321
1322 void BoxPainter::clipBorderSideForComplexInnerPath(GraphicsContext* graphicsCont ext, const RoundedRect& outerBorder, const RoundedRect& innerBorder,
1323 BoxSide side, const BorderEdge edges[])
1324 {
1325 graphicsContext->clip(calculateSideRectIncludingInner(outerBorder, edges, si de));
1326 RoundedRect adjustedInnerRect = calculateAdjustedInnerBorder(innerBorder, si de);
1327 if (!adjustedInnerRect.isEmpty())
1328 graphicsContext->clipOutRoundedRect(adjustedInnerRect);
1329 }
1330
1331 static bool allCornersClippedOut(const RoundedRect& border, const LayoutRect& cl ipRect)
1332 {
1333 LayoutRect boundingRect = border.rect();
1334 if (clipRect.contains(boundingRect))
1335 return false;
1336
1337 RoundedRect::Radii radii = border.radii();
1338
1339 LayoutRect topLeftRect(boundingRect.location(), radii.topLeft());
1340 if (clipRect.intersects(topLeftRect))
1341 return false;
1342
1343 LayoutRect topRightRect(boundingRect.location(), radii.topRight());
1344 topRightRect.setX(boundingRect.maxX() - topRightRect.width());
1345 if (clipRect.intersects(topRightRect))
1346 return false;
1347
1348 LayoutRect bottomLeftRect(boundingRect.location(), radii.bottomLeft());
1349 bottomLeftRect.setY(boundingRect.maxY() - bottomLeftRect.height());
1350 if (clipRect.intersects(bottomLeftRect))
1351 return false;
1352
1353 LayoutRect bottomRightRect(boundingRect.location(), radii.bottomRight());
1354 bottomRightRect.setX(boundingRect.maxX() - bottomRightRect.width());
1355 bottomRightRect.setY(boundingRect.maxY() - bottomRightRect.height());
1356 if (clipRect.intersects(bottomRightRect))
1357 return false;
1358
1359 return true;
1360 }
1361
1362 void BoxPainter::paintBorder(RenderBoxModelObject& obj, const PaintInfo& info, c onst LayoutRect& rect, const RenderStyle* style, BackgroundBleedAvoidance bleedA voidance, bool includeLogicalLeftEdge, bool includeLogicalRightEdge)
1363 {
1364 GraphicsContext* graphicsContext = info.context;
1365 // border-image is not affected by border-radius.
1366 if (paintNinePieceImage(obj, graphicsContext, rect, style, style->borderImag e()))
1367 return;
1368
1369 BorderEdge edges[4];
1370 style->getBorderEdgeInfo(edges, includeLogicalLeftEdge, includeLogicalRightE dge);
1371 RoundedRect outerBorder = style->getRoundedBorderFor(rect, includeLogicalLef tEdge, includeLogicalRightEdge);
1372 RoundedRect innerBorder = style->getRoundedInnerBorderFor(borderInnerRectAdj ustedForBleedAvoidance(graphicsContext, rect, bleedAvoidance), includeLogicalLef tEdge, includeLogicalRightEdge);
1373
1374 if (outerBorder.rect().isEmpty())
1375 return;
1376
1377 bool haveAlphaColor = false;
1378 bool haveAllSolidEdges = true;
1379 bool haveAllDoubleEdges = true;
1380 int numEdgesVisible = 4;
1381 bool allEdgesShareColor = true;
1382 bool allEdgesShareWidth = true;
1383 int firstVisibleEdge = -1;
1384 BorderEdgeFlags edgesToDraw = 0;
1385
1386 for (int i = BSTop; i <= BSLeft; ++i) {
1387 const BorderEdge& currEdge = edges[i];
1388
1389 if (edges[i].shouldRender())
1390 edgesToDraw |= edgeFlagForSide(static_cast<BoxSide>(i));
1391
1392 if (currEdge.presentButInvisible()) {
1393 --numEdgesVisible;
1394 allEdgesShareColor = false;
1395 allEdgesShareWidth = false;
1396 continue;
1397 }
1398
1399 if (!currEdge.shouldRender()) {
1400 --numEdgesVisible;
1401 continue;
1402 }
1403
1404 if (firstVisibleEdge == -1) {
1405 firstVisibleEdge = i;
1406 } else {
1407 if (currEdge.color != edges[firstVisibleEdge].color)
1408 allEdgesShareColor = false;
1409 if (currEdge.width != edges[firstVisibleEdge].width)
1410 allEdgesShareWidth = false;
1411 }
1412
1413 if (currEdge.color.hasAlpha())
1414 haveAlphaColor = true;
1415
1416 if (currEdge.borderStyle() != SOLID)
1417 haveAllSolidEdges = false;
1418
1419 if (currEdge.borderStyle() != DOUBLE)
1420 haveAllDoubleEdges = false;
1421 }
1422
1423 // If no corner intersects the clip region, we can pretend outerBorder is
1424 // rectangular to improve performance.
1425 if (haveAllSolidEdges && outerBorder.isRounded() && allCornersClippedOut(out erBorder, info.rect))
1426 outerBorder.setRadii(RoundedRect::Radii());
1427
1428 // isRenderable() check avoids issue described in https://bugs.webkit.org/sh ow_bug.cgi?id=38787
1429 if ((haveAllSolidEdges || haveAllDoubleEdges) && allEdgesShareColor && inner Border.isRenderable()) {
1430 // Fast path for drawing all solid edges and all unrounded double edges
1431
1432 if (numEdgesVisible == 4 && (outerBorder.isRounded() || haveAlphaColor)
1433 && (haveAllSolidEdges || (!outerBorder.isRounded() && !innerBorder.i sRounded()))) {
1434 Path path;
1435
1436 if (outerBorder.isRounded() && allEdgesShareWidth) {
1437
1438 // Very fast path for single stroked round rect with circular co rners
1439
1440 graphicsContext->fillBetweenRoundedRects(outerBorder, innerBorde r, edges[firstVisibleEdge].color);
1441 return;
1442 }
1443 if (outerBorder.isRounded() && bleedAvoidance != BackgroundBleedClip Background)
1444 path.addRoundedRect(outerBorder);
1445 else
1446 path.addRect(outerBorder.rect());
1447
1448 if (haveAllDoubleEdges) {
1449 IntRect innerThirdRect = outerBorder.rect();
1450 IntRect outerThirdRect = outerBorder.rect();
1451 for (int side = BSTop; side <= BSLeft; ++side) {
1452 int outerWidth;
1453 int innerWidth;
1454 edges[side].getDoubleBorderStripeWidths(outerWidth, innerWid th);
1455
1456 if (side == BSTop) {
1457 innerThirdRect.shiftYEdgeTo(innerThirdRect.y() + innerWi dth);
1458 outerThirdRect.shiftYEdgeTo(outerThirdRect.y() + outerWi dth);
1459 } else if (side == BSBottom) {
1460 innerThirdRect.setHeight(innerThirdRect.height() - inner Width);
1461 outerThirdRect.setHeight(outerThirdRect.height() - outer Width);
1462 } else if (side == BSLeft) {
1463 innerThirdRect.shiftXEdgeTo(innerThirdRect.x() + innerWi dth);
1464 outerThirdRect.shiftXEdgeTo(outerThirdRect.x() + outerWi dth);
1465 } else {
1466 innerThirdRect.setWidth(innerThirdRect.width() - innerWi dth);
1467 outerThirdRect.setWidth(outerThirdRect.width() - outerWi dth);
1468 }
1469 }
1470
1471 RoundedRect outerThird = outerBorder;
1472 RoundedRect innerThird = innerBorder;
1473 innerThird.setRect(innerThirdRect);
1474 outerThird.setRect(outerThirdRect);
1475
1476 if (outerThird.isRounded() && bleedAvoidance != BackgroundBleedC lipBackground)
1477 path.addRoundedRect(outerThird);
1478 else
1479 path.addRect(outerThird.rect());
1480
1481 if (innerThird.isRounded() && bleedAvoidance != BackgroundBleedC lipBackground)
1482 path.addRoundedRect(innerThird);
1483 else
1484 path.addRect(innerThird.rect());
1485 }
1486
1487 if (innerBorder.isRounded())
1488 path.addRoundedRect(innerBorder);
1489 else
1490 path.addRect(innerBorder.rect());
1491
1492 graphicsContext->setFillRule(RULE_EVENODD);
1493 graphicsContext->setFillColor(edges[firstVisibleEdge].color);
1494 graphicsContext->fillPath(path);
1495 return;
1496 }
1497 // Avoid creating transparent layers
1498 if (haveAllSolidEdges && numEdgesVisible != 4 && !outerBorder.isRounded( ) && haveAlphaColor) {
1499 Path path;
1500
1501 for (int i = BSTop; i <= BSLeft; ++i) {
1502 const BorderEdge& currEdge = edges[i];
1503 if (currEdge.shouldRender()) {
1504 IntRect sideRect = calculateSideRect(outerBorder, edges, i);
1505 path.addRect(sideRect);
1506 }
1507 }
1508
1509 graphicsContext->setFillRule(RULE_NONZERO);
1510 graphicsContext->setFillColor(edges[firstVisibleEdge].color);
1511 graphicsContext->fillPath(path);
1512 return;
1513 }
1514 }
1515
1516 bool clipToOuterBorder = outerBorder.isRounded();
1517 GraphicsContextStateSaver stateSaver(*graphicsContext, clipToOuterBorder);
1518 if (clipToOuterBorder) {
1519 // Clip to the inner and outer radii rects.
1520 if (bleedAvoidance != BackgroundBleedClipBackground)
1521 graphicsContext->clipRoundedRect(outerBorder);
1522 // isRenderable() check avoids issue described in https://bugs.webkit.or g/show_bug.cgi?id=38787
1523 // The inside will be clipped out later (in clipBorderSideForComplexInne rPath)
1524 if (innerBorder.isRenderable() && !innerBorder.isEmpty())
1525 graphicsContext->clipOutRoundedRect(innerBorder);
1526 }
1527
1528 // If only one edge visible antialiasing doesn't create seams
1529 bool antialias = shouldAntialiasLines(graphicsContext) || numEdgesVisible == 1;
1530 RoundedRect unadjustedInnerBorder = (bleedAvoidance == BackgroundBleedBackgr oundOverBorder) ? style->getRoundedInnerBorderFor(rect, includeLogicalLeftEdge, includeLogicalRightEdge) : innerBorder;
1531 IntPoint innerBorderAdjustment(innerBorder.rect().x() - unadjustedInnerBorde r.rect().x(), innerBorder.rect().y() - unadjustedInnerBorder.rect().y());
1532 if (haveAlphaColor)
1533 paintTranslucentBorderSides(obj, graphicsContext, style, outerBorder, un adjustedInnerBorder, innerBorderAdjustment, edges, edgesToDraw, bleedAvoidance, includeLogicalLeftEdge, includeLogicalRightEdge, antialias);
1534 else
1535 paintBorderSides(obj, graphicsContext, style, outerBorder, unadjustedInn erBorder, innerBorderAdjustment, edges, edgesToDraw, bleedAvoidance, includeLogi calLeftEdge, includeLogicalRightEdge, antialias);
1536 }
1537
1538 static inline bool includesAdjacentEdges(BorderEdgeFlags flags)
1539 {
1540 return (flags & (TopBorderEdge | RightBorderEdge)) == (TopBorderEdge | Right BorderEdge)
1541 || (flags & (RightBorderEdge | BottomBorderEdge)) == (RightBorderEdge | BottomBorderEdge)
1542 || (flags & (BottomBorderEdge | LeftBorderEdge)) == (BottomBorderEdge | LeftBorderEdge)
1543 || (flags & (LeftBorderEdge | TopBorderEdge)) == (LeftBorderEdge | TopBo rderEdge);
1544 }
1545
1546 void BoxPainter::paintTranslucentBorderSides(RenderObject& obj, GraphicsContext* graphicsContext, const RenderStyle* style, const RoundedRect& outerBorder, cons t RoundedRect& innerBorder, const IntPoint& innerBorderAdjustment,
1547 const BorderEdge edges[], BorderEdgeFlags edgesToDraw, BackgroundBleedAvoida nce bleedAvoidance, bool includeLogicalLeftEdge, bool includeLogicalRightEdge, b ool antialias)
1548 {
1549 // willBeOverdrawn assumes that we draw in order: top, bottom, left, right.
1550 // This is different from BoxSide enum order.
1551 static const BoxSide paintOrder[] = { BSTop, BSBottom, BSLeft, BSRight };
1552
1553 while (edgesToDraw) {
1554 // Find undrawn edges sharing a color.
1555 Color commonColor;
1556
1557 BorderEdgeFlags commonColorEdgeSet = 0;
1558 for (size_t i = 0; i < sizeof(paintOrder) / sizeof(paintOrder[0]); ++i) {
1559 BoxSide currSide = paintOrder[i];
1560 if (!includesEdge(edgesToDraw, currSide))
1561 continue;
1562
1563 bool includeEdge;
1564 if (!commonColorEdgeSet) {
1565 commonColor = edges[currSide].color;
1566 includeEdge = true;
1567 } else {
1568 includeEdge = edges[currSide].color == commonColor;
1569 }
1570
1571 if (includeEdge)
1572 commonColorEdgeSet |= edgeFlagForSide(currSide);
1573 }
1574
1575 bool useTransparencyLayer = includesAdjacentEdges(commonColorEdgeSet) && commonColor.hasAlpha();
1576 if (useTransparencyLayer) {
1577 graphicsContext->beginTransparencyLayer(static_cast<float>(commonCol or.alpha()) / 255);
1578 commonColor = Color(commonColor.red(), commonColor.green(), commonCo lor.blue());
1579 }
1580
1581 paintBorderSides(obj, graphicsContext, style, outerBorder, innerBorder, innerBorderAdjustment, edges, commonColorEdgeSet, bleedAvoidance, includeLogical LeftEdge, includeLogicalRightEdge, antialias, &commonColor);
1582
1583 if (useTransparencyLayer)
1584 graphicsContext->endLayer();
1585
1586 edgesToDraw &= ~commonColorEdgeSet;
1587 }
1588 }
1589
1590 LayoutRect BoxPainter::borderInnerRectAdjustedForBleedAvoidance(GraphicsContext* context, const LayoutRect& rect, BackgroundBleedAvoidance bleedAvoidance)
1591 {
1592 // We shrink the rectangle by one pixel on each side to make it fully overla p the anti-aliased background border
1593 return (bleedAvoidance == BackgroundBleedBackgroundOverBorder) ? shrinkRectB yOnePixel(context, rect) : rect;
1594 }
1595
1596 void BoxPainter::paintOneBorderSide(RenderObject& obj, GraphicsContext* graphics Context, const RenderStyle* style, const RoundedRect& outerBorder, const Rounded Rect& innerBorder,
1597 const IntRect& sideRect, BoxSide side, BoxSide adjacentSide1, BoxSide adjace ntSide2, const BorderEdge edges[], const Path* path,
1598 BackgroundBleedAvoidance bleedAvoidance, bool includeLogicalLeftEdge, bool i ncludeLogicalRightEdge, bool antialias, const Color* overrideColor)
1599 {
1600 const BorderEdge& edgeToRender = edges[side];
1601 ASSERT(edgeToRender.width);
1602 const BorderEdge& adjacentEdge1 = edges[adjacentSide1];
1603 const BorderEdge& adjacentEdge2 = edges[adjacentSide2];
1604
1605 bool mitreAdjacentSide1 = joinRequiresMitre(side, adjacentSide1, edges, !ant ialias);
1606 bool mitreAdjacentSide2 = joinRequiresMitre(side, adjacentSide2, edges, !ant ialias);
1607
1608 bool adjacentSide1StylesMatch = colorsMatchAtCorner(side, adjacentSide1, edg es);
1609 bool adjacentSide2StylesMatch = colorsMatchAtCorner(side, adjacentSide2, edg es);
1610
1611 const Color& colorToPaint = overrideColor ? *overrideColor : edgeToRender.co lor;
1612
1613 if (path) {
1614 GraphicsContextStateSaver stateSaver(*graphicsContext);
1615 if (innerBorder.isRenderable())
1616 clipBorderSidePolygon(graphicsContext, outerBorder, innerBorder, sid e, adjacentSide1StylesMatch, adjacentSide2StylesMatch);
1617 else
1618 clipBorderSideForComplexInnerPath(graphicsContext, outerBorder, inne rBorder, side, edges);
1619 float thickness = std::max(std::max(edgeToRender.width, adjacentEdge1.wi dth), adjacentEdge2.width);
1620 drawBoxSideFromPath(graphicsContext, outerBorder.rect(), *path, edges, e dgeToRender.width, thickness, side, style,
1621 colorToPaint, edgeToRender.borderStyle(), bleedAvoidance, includeLog icalLeftEdge, includeLogicalRightEdge);
1622 } else {
1623 bool clipForStyle = styleRequiresClipPolygon(edgeToRender.borderStyle()) && (mitreAdjacentSide1 || mitreAdjacentSide2);
1624 bool clipAdjacentSide1 = colorNeedsAntiAliasAtCorner(side, adjacentSide1 , edges) && mitreAdjacentSide1;
1625 bool clipAdjacentSide2 = colorNeedsAntiAliasAtCorner(side, adjacentSide2 , edges) && mitreAdjacentSide2;
1626 bool shouldClip = clipForStyle || clipAdjacentSide1 || clipAdjacentSide2 ;
1627
1628 GraphicsContextStateSaver clipStateSaver(*graphicsContext, shouldClip);
1629 if (shouldClip) {
1630 bool aliasAdjacentSide1 = clipAdjacentSide1 || (clipForStyle && mitr eAdjacentSide1);
1631 bool aliasAdjacentSide2 = clipAdjacentSide2 || (clipForStyle && mitr eAdjacentSide2);
1632 clipBorderSidePolygon(graphicsContext, outerBorder, innerBorder, sid e, !aliasAdjacentSide1, !aliasAdjacentSide2);
1633 // Since we clipped, no need to draw with a mitre.
1634 mitreAdjacentSide1 = false;
1635 mitreAdjacentSide2 = false;
1636 }
1637
1638 obj.drawLineForBoxSide(graphicsContext, sideRect.x(), sideRect.y(), side Rect.maxX(), sideRect.maxY(), side, colorToPaint, edgeToRender.borderStyle(),
1639 mitreAdjacentSide1 ? adjacentEdge1.width : 0, mitreAdjacentSide2 ? a djacentEdge2.width : 0, antialias);
1640 }
1641 }
1642
1643 void BoxPainter::paintBorderSides(RenderObject& obj, GraphicsContext* graphicsCo ntext, const RenderStyle* style, const RoundedRect& outerBorder, const RoundedRe ct& innerBorder,
1644 const IntPoint& innerBorderAdjustment, const BorderEdge edges[], BorderEdgeF lags edgeSet, BackgroundBleedAvoidance bleedAvoidance,
1645 bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias, c onst Color* overrideColor)
1646 {
1647 bool renderRadii = outerBorder.isRounded();
1648
1649 Path roundedPath;
1650 if (renderRadii)
1651 roundedPath.addRoundedRect(outerBorder);
1652
1653 // The inner border adjustment for bleed avoidance mode BackgroundBleedBackg roundOverBorder
1654 // is only applied to sideRect, which is okay since BackgroundBleedBackgroun dOverBorder
1655 // is only to be used for solid borders and the shape of the border painted by drawBoxSideFromPath
1656 // only depends on sideRect when painting solid borders.
1657
1658 if (edges[BSTop].shouldRender() && includesEdge(edgeSet, BSTop)) {
1659 IntRect sideRect = outerBorder.rect();
1660 sideRect.setHeight(edges[BSTop].width + innerBorderAdjustment.y());
1661
1662 bool usePath = renderRadii && (borderStyleHasInnerDetail(edges[BSTop].bo rderStyle()) || borderWillArcInnerEdge(innerBorder.radii().topLeft(), innerBorde r.radii().topRight()));
1663 paintOneBorderSide(obj, graphicsContext, style, outerBorder, innerBorder , sideRect, BSTop, BSLeft, BSRight, edges, usePath ? &roundedPath : 0, bleedAvoi dance, includeLogicalLeftEdge, includeLogicalRightEdge, antialias, overrideColor );
1664 }
1665
1666 if (edges[BSBottom].shouldRender() && includesEdge(edgeSet, BSBottom)) {
1667 IntRect sideRect = outerBorder.rect();
1668 sideRect.shiftYEdgeTo(sideRect.maxY() - edges[BSBottom].width - innerBor derAdjustment.y());
1669
1670 bool usePath = renderRadii && (borderStyleHasInnerDetail(edges[BSBottom] .borderStyle()) || borderWillArcInnerEdge(innerBorder.radii().bottomLeft(), inne rBorder.radii().bottomRight()));
1671 paintOneBorderSide(obj, graphicsContext, style, outerBorder, innerBorder , sideRect, BSBottom, BSLeft, BSRight, edges, usePath ? &roundedPath : 0, bleedA voidance, includeLogicalLeftEdge, includeLogicalRightEdge, antialias, overrideCo lor);
1672 }
1673
1674 if (edges[BSLeft].shouldRender() && includesEdge(edgeSet, BSLeft)) {
1675 IntRect sideRect = outerBorder.rect();
1676 sideRect.setWidth(edges[BSLeft].width + innerBorderAdjustment.x());
1677
1678 bool usePath = renderRadii && (borderStyleHasInnerDetail(edges[BSLeft].b orderStyle()) || borderWillArcInnerEdge(innerBorder.radii().bottomLeft(), innerB order.radii().topLeft()));
1679 paintOneBorderSide(obj, graphicsContext, style, outerBorder, innerBorder , sideRect, BSLeft, BSTop, BSBottom, edges, usePath ? &roundedPath : 0, bleedAvo idance, includeLogicalLeftEdge, includeLogicalRightEdge, antialias, overrideColo r);
1680 }
1681
1682 if (edges[BSRight].shouldRender() && includesEdge(edgeSet, BSRight)) {
1683 IntRect sideRect = outerBorder.rect();
1684 sideRect.shiftXEdgeTo(sideRect.maxX() - edges[BSRight].width - innerBord erAdjustment.x());
1685
1686 bool usePath = renderRadii && (borderStyleHasInnerDetail(edges[BSRight]. borderStyle()) || borderWillArcInnerEdge(innerBorder.radii().bottomRight(), inne rBorder.radii().topRight()));
1687 paintOneBorderSide(obj, graphicsContext, style, outerBorder, innerBorder , sideRect, BSRight, BSTop, BSBottom, edges, usePath ? &roundedPath : 0, bleedAv oidance, includeLogicalLeftEdge, includeLogicalRightEdge, antialias, overrideCol or);
1688 }
1689 }
1690
1691 void BoxPainter::drawBoxSideFromPath(GraphicsContext* graphicsContext, const Lay outRect& borderRect, const Path& borderPath, const BorderEdge edges[],
1692 float thickness, float drawThickness, BoxSide side, const RenderStyle* style , Color color, EBorderStyle borderStyle, BackgroundBleedAvoidance bleedAvoidance ,
1693 bool includeLogicalLeftEdge, bool includeLogicalRightEdge)
1694 {
1695 if (thickness <= 0)
1696 return;
1697
1698 if (borderStyle == DOUBLE && thickness < 3)
1699 borderStyle = SOLID;
1700
1701 switch (borderStyle) {
1702 case BNONE:
1703 case BHIDDEN:
1704 return;
1705 case DOTTED:
1706 case DASHED: {
1707 graphicsContext->setStrokeColor(color);
1708
1709 // The stroke is doubled here because the provided path is the
1710 // outside edge of the border so half the stroke is clipped off.
1711 // The extra multiplier is so that the clipping mask can antialias
1712 // the edges to prevent jaggies.
1713 graphicsContext->setStrokeThickness(drawThickness * 2 * 1.1f);
1714 graphicsContext->setStrokeStyle(borderStyle == DASHED ? DashedStroke : D ottedStroke);
1715
1716 // If the number of dashes that fit in the path is odd and non-integral then we
1717 // will have an awkwardly-sized dash at the end of the path. To try to a void that
1718 // here, we simply make the whitespace dashes ever so slightly bigger.
1719 // FIXME: This could be even better if we tried to manipulate the dash o ffset
1720 // and possibly the gapLength to get the corners dash-symmetrical.
1721 float dashLength = thickness * ((borderStyle == DASHED) ? 3.0f : 1.0f);
1722 float gapLength = dashLength;
1723 float numberOfDashes = borderPath.length() / dashLength;
1724 // Don't try to show dashes if we have less than 2 dashes + 2 gaps.
1725 // FIXME: should do this test per side.
1726 if (numberOfDashes >= 4) {
1727 bool evenNumberOfFullDashes = !((int)numberOfDashes % 2);
1728 bool integralNumberOfDashes = !(numberOfDashes - (int)numberOfDashes );
1729 if (!evenNumberOfFullDashes && !integralNumberOfDashes) {
1730 float numberOfGaps = numberOfDashes / 2;
1731 gapLength += (dashLength / numberOfGaps);
1732 }
1733
1734 DashArray lineDash;
1735 lineDash.append(dashLength);
1736 lineDash.append(gapLength);
1737 graphicsContext->setLineDash(lineDash, dashLength);
1738 }
1739
1740 // FIXME: stroking the border path causes issues with tight corners:
1741 // https://bugs.webkit.org/show_bug.cgi?id=58711
1742 // Also, to get the best appearance we should stroke a path between the two borders.
1743 graphicsContext->strokePath(borderPath);
1744 return;
1745 }
1746 case DOUBLE: {
1747 // Get the inner border rects for both the outer border line and the inn er border line
1748 int outerBorderTopWidth;
1749 int innerBorderTopWidth;
1750 edges[BSTop].getDoubleBorderStripeWidths(outerBorderTopWidth, innerBorde rTopWidth);
1751
1752 int outerBorderRightWidth;
1753 int innerBorderRightWidth;
1754 edges[BSRight].getDoubleBorderStripeWidths(outerBorderRightWidth, innerB orderRightWidth);
1755
1756 int outerBorderBottomWidth;
1757 int innerBorderBottomWidth;
1758 edges[BSBottom].getDoubleBorderStripeWidths(outerBorderBottomWidth, inne rBorderBottomWidth);
1759
1760 int outerBorderLeftWidth;
1761 int innerBorderLeftWidth;
1762 edges[BSLeft].getDoubleBorderStripeWidths(outerBorderLeftWidth, innerBor derLeftWidth);
1763
1764 // Draw inner border line
1765 {
1766 GraphicsContextStateSaver stateSaver(*graphicsContext);
1767 RoundedRect innerClip = style->getRoundedInnerBorderFor(borderRect,
1768 innerBorderTopWidth, innerBorderBottomWidth, innerBorderLeftWidt h, innerBorderRightWidth,
1769 includeLogicalLeftEdge, includeLogicalRightEdge);
1770
1771 graphicsContext->clipRoundedRect(innerClip);
1772 drawBoxSideFromPath(graphicsContext, borderRect, borderPath, edges, thickness, drawThickness, side, style, color, SOLID, bleedAvoidance, includeLogi calLeftEdge, includeLogicalRightEdge);
1773 }
1774
1775 // Draw outer border line
1776 {
1777 GraphicsContextStateSaver stateSaver(*graphicsContext);
1778 LayoutRect outerRect = borderRect;
1779 if (bleedAvoidance == BackgroundBleedClipBackground) {
1780 outerRect.inflate(1);
1781 ++outerBorderTopWidth;
1782 ++outerBorderBottomWidth;
1783 ++outerBorderLeftWidth;
1784 ++outerBorderRightWidth;
1785 }
1786
1787 RoundedRect outerClip = style->getRoundedInnerBorderFor(outerRect,
1788 outerBorderTopWidth, outerBorderBottomWidth, outerBorderLeftWidt h, outerBorderRightWidth,
1789 includeLogicalLeftEdge, includeLogicalRightEdge);
1790 graphicsContext->clipOutRoundedRect(outerClip);
1791 drawBoxSideFromPath(graphicsContext, borderRect, borderPath, edges, thickness, drawThickness, side, style, color, SOLID, bleedAvoidance, includeLogi calLeftEdge, includeLogicalRightEdge);
1792 }
1793 return;
1794 }
1795 case RIDGE:
1796 case GROOVE:
1797 {
1798 EBorderStyle s1;
1799 EBorderStyle s2;
1800 if (borderStyle == GROOVE) {
1801 s1 = INSET;
1802 s2 = OUTSET;
1803 } else {
1804 s1 = OUTSET;
1805 s2 = INSET;
1806 }
1807
1808 // Paint full border
1809 drawBoxSideFromPath(graphicsContext, borderRect, borderPath, edges, thic kness, drawThickness, side, style, color, s1, bleedAvoidance, includeLogicalLeft Edge, includeLogicalRightEdge);
1810
1811 // Paint inner only
1812 GraphicsContextStateSaver stateSaver(*graphicsContext);
1813 LayoutUnit topWidth = edges[BSTop].usedWidth() / 2;
1814 LayoutUnit bottomWidth = edges[BSBottom].usedWidth() / 2;
1815 LayoutUnit leftWidth = edges[BSLeft].usedWidth() / 2;
1816 LayoutUnit rightWidth = edges[BSRight].usedWidth() / 2;
1817
1818 RoundedRect clipRect = style->getRoundedInnerBorderFor(borderRect,
1819 topWidth, bottomWidth, leftWidth, rightWidth,
1820 includeLogicalLeftEdge, includeLogicalRightEdge);
1821
1822 graphicsContext->clipRoundedRect(clipRect);
1823 drawBoxSideFromPath(graphicsContext, borderRect, borderPath, edges, thic kness, drawThickness, side, style, color, s2, bleedAvoidance, includeLogicalLeft Edge, includeLogicalRightEdge);
1824 return;
1825 }
1826 case INSET:
1827 if (side == BSTop || side == BSLeft)
1828 color = color.dark();
1829 break;
1830 case OUTSET:
1831 if (side == BSBottom || side == BSRight)
1832 color = color.dark();
1833 break;
1834 default:
1835 break;
1836 }
1837
1838 graphicsContext->setStrokeStyle(NoStroke);
1839 graphicsContext->setFillColor(color);
1840 graphicsContext->drawRect(pixelSnappedIntRect(borderRect));
1841 }
1842
1843 void BoxPainter::paintBoxShadow(const PaintInfo& info, const LayoutRect& paintRe ct, const RenderStyle* s, ShadowStyle shadowStyle, bool includeLogicalLeftEdge, bool includeLogicalRightEdge)
1844 {
1845 // FIXME: Deal with border-image. Would be great to use border-image as a ma sk.
1846 GraphicsContext* context = info.context;
1847 if (!s->boxShadow())
1848 return;
1849
1850 RoundedRect border = (shadowStyle == Inset) ? s->getRoundedInnerBorderFor(pa intRect, includeLogicalLeftEdge, includeLogicalRightEdge)
1851 : s->getRoundedBorderFor(paintRect, includeLogicalLeftEdge, includeLogic alRightEdge);
1852
1853 bool hasBorderRadius = s->hasBorderRadius();
1854 bool isHorizontal = s->isHorizontalWritingMode();
1855 bool hasOpaqueBackground = s->visitedDependentColor(CSSPropertyBackgroundCol or).alpha() == 255;
1856
1857 GraphicsContextStateSaver stateSaver(*context, false);
1858
1859 const ShadowList* shadowList = s->boxShadow();
1860 for (size_t i = shadowList->shadows().size(); i--; ) {
1861 const ShadowData& shadow = shadowList->shadows()[i];
1862 if (shadow.style() != shadowStyle)
1863 continue;
1864
1865 FloatSize shadowOffset(shadow.x(), shadow.y());
1866 float shadowBlur = shadow.blur();
1867 float shadowSpread = shadow.spread();
1868
1869 if (shadowOffset.isZero() && !shadowBlur && !shadowSpread)
1870 continue;
1871
1872 const Color& shadowColor = shadow.color();
1873
1874 if (shadow.style() == Normal) {
1875 FloatRect fillRect = border.rect();
1876 fillRect.inflate(shadowSpread);
1877 if (fillRect.isEmpty())
1878 continue;
1879
1880 FloatRect shadowRect(border.rect());
1881 shadowRect.inflate(shadowBlur + shadowSpread);
1882 shadowRect.move(shadowOffset);
1883
1884 // Save the state and clip, if not already done.
1885 // The clip does not depend on any shadow-specific properties.
1886 if (!stateSaver.saved()) {
1887 stateSaver.save();
1888 if (hasBorderRadius) {
1889 RoundedRect rectToClipOut = border;
1890
1891 // If the box is opaque, it is unnecessary to clip it out. H owever, doing so saves time
1892 // when painting the shadow. On the other hand, it introduce s subpixel gaps along the
1893 // corners. Those are avoided by insetting the clipping path by one pixel.
1894 if (hasOpaqueBackground)
1895 rectToClipOut.inflateWithRadii(-1);
1896
1897 if (!rectToClipOut.isEmpty()) {
1898 context->clipOutRoundedRect(rectToClipOut);
1899 }
1900 } else {
1901 // This IntRect is correct even with fractional shadows, bec ause it is used for the rectangle
1902 // of the box itself, which is always pixel-aligned.
1903 IntRect rectToClipOut = border.rect();
1904
1905 // If the box is opaque, it is unnecessary to clip it out. H owever, doing so saves time
1906 // when painting the shadow. On the other hand, it introduce s subpixel gaps along the
1907 // edges if they are not pixel-aligned. Those are avoided by insetting the clipping path
1908 // by one pixel.
1909 if (hasOpaqueBackground) {
1910 // FIXME: The function to decide on the policy based on the transform should be a named function.
1911 // FIXME: It's not clear if this check is right. What ab out integral scale factors?
1912 // FIXME: See crbug.com/382491. The use of getCTM may al so be wrong because it does not include
1913 // device zoom applied at raster time.
1914 AffineTransform transform = context->getCTM();
1915 if (transform.a() != 1 || (transform.d() != 1 && transfo rm.d() != -1) || transform.b() || transform.c())
1916 rectToClipOut.inflate(-1);
1917 }
1918
1919 if (!rectToClipOut.isEmpty()) {
1920 context->clipOut(rectToClipOut);
1921 }
1922 }
1923 }
1924
1925 // Draw only the shadow.
1926 OwnPtr<DrawLooperBuilder> drawLooperBuilder = DrawLooperBuilder::cre ate();
1927 drawLooperBuilder->addShadow(shadowOffset, shadowBlur, shadowColor,
1928 DrawLooperBuilder::ShadowRespectsTransforms, DrawLooperBuilder:: ShadowIgnoresAlpha);
1929 context->setDrawLooper(drawLooperBuilder.release());
1930
1931 if (hasBorderRadius) {
1932 RoundedRect influenceRect(pixelSnappedIntRect(LayoutRect(shadowR ect)), border.radii());
1933 influenceRect.expandRadii(2 * shadowBlur + shadowSpread);
1934 if (allCornersClippedOut(influenceRect, info.rect)) {
1935 context->fillRect(fillRect, Color::black);
1936 } else {
1937 // TODO: support non-integer shadows - crbug.com/334829
1938 RoundedRect roundedFillRect = border;
1939 roundedFillRect.inflate(shadowSpread);
1940
1941 roundedFillRect.expandRadii(shadowSpread);
1942 if (!roundedFillRect.isRenderable())
1943 roundedFillRect.adjustRadii();
1944 context->fillRoundedRect(roundedFillRect, Color::black);
1945 }
1946 } else {
1947 context->fillRect(fillRect, Color::black);
1948 }
1949 } else {
1950 // The inset shadow case.
1951 GraphicsContext::Edges clippedEdges = GraphicsContext::NoEdge;
1952 if (!includeLogicalLeftEdge) {
1953 if (isHorizontal)
1954 clippedEdges |= GraphicsContext::LeftEdge;
1955 else
1956 clippedEdges |= GraphicsContext::TopEdge;
1957 }
1958 if (!includeLogicalRightEdge) {
1959 if (isHorizontal)
1960 clippedEdges |= GraphicsContext::RightEdge;
1961 else
1962 clippedEdges |= GraphicsContext::BottomEdge;
1963 }
1964 // TODO: support non-integer shadows - crbug.com/334828
1965 context->drawInnerShadow(border, shadowColor, flooredIntSize(shadowO ffset), shadowBlur, shadowSpread, clippedEdges);
1966 }
1967 }
1968 }
1969
1970 void BoxPainter::clipBorderSidePolygon(GraphicsContext* graphicsContext, const R oundedRect& outerBorder, const RoundedRect& innerBorder, BoxSide side, bool firs tEdgeMatches, bool secondEdgeMatches)
1971 {
1972 FloatPoint quad[4];
1973
1974 const LayoutRect& outerRect = outerBorder.rect();
1975 const LayoutRect& innerRect = innerBorder.rect();
1976
1977 FloatPoint centerPoint(innerRect.location().x().toFloat() + innerRect.width( ).toFloat() / 2, innerRect.location().y().toFloat() + innerRect.height().toFloat () / 2);
1978
1979 // For each side, create a quad that encompasses all parts of that side that may draw,
1980 // including areas inside the innerBorder.
1981 //
1982 // 0----------------3
1983 // 0 \ / 0
1984 // |\ 1----------- 2 /|
1985 // | 1 1 |
1986 // | | | |
1987 // | | | |
1988 // | 2 2 |
1989 // |/ 1------------2 \|
1990 // 3 / \ 3
1991 // 0----------------3
1992 //
1993 switch (side) {
1994 case BSTop:
1995 quad[0] = outerRect.minXMinYCorner();
1996 quad[1] = innerRect.minXMinYCorner();
1997 quad[2] = innerRect.maxXMinYCorner();
1998 quad[3] = outerRect.maxXMinYCorner();
1999
2000 if (!innerBorder.radii().topLeft().isZero()) {
2001 findIntersection(quad[0], quad[1],
2002 FloatPoint(
2003 quad[1].x() + innerBorder.radii().topLeft().width(),
2004 quad[1].y()),
2005 FloatPoint(
2006 quad[1].x(),
2007 quad[1].y() + innerBorder.radii().topLeft().height()),
2008 quad[1]);
2009 }
2010
2011 if (!innerBorder.radii().topRight().isZero()) {
2012 findIntersection(quad[3], quad[2],
2013 FloatPoint(
2014 quad[2].x() - innerBorder.radii().topRight().width(),
2015 quad[2].y()),
2016 FloatPoint(
2017 quad[2].x(),
2018 quad[2].y() + innerBorder.radii().topRight().height()),
2019 quad[2]);
2020 }
2021 break;
2022
2023 case BSLeft:
2024 quad[0] = outerRect.minXMinYCorner();
2025 quad[1] = innerRect.minXMinYCorner();
2026 quad[2] = innerRect.minXMaxYCorner();
2027 quad[3] = outerRect.minXMaxYCorner();
2028
2029 if (!innerBorder.radii().topLeft().isZero()) {
2030 findIntersection(quad[0], quad[1],
2031 FloatPoint(
2032 quad[1].x() + innerBorder.radii().topLeft().width(),
2033 quad[1].y()),
2034 FloatPoint(
2035 quad[1].x(),
2036 quad[1].y() + innerBorder.radii().topLeft().height()),
2037 quad[1]);
2038 }
2039
2040 if (!innerBorder.radii().bottomLeft().isZero()) {
2041 findIntersection(quad[3], quad[2],
2042 FloatPoint(
2043 quad[2].x() + innerBorder.radii().bottomLeft().width(),
2044 quad[2].y()),
2045 FloatPoint(
2046 quad[2].x(),
2047 quad[2].y() - innerBorder.radii().bottomLeft().height()),
2048 quad[2]);
2049 }
2050 break;
2051
2052 case BSBottom:
2053 quad[0] = outerRect.minXMaxYCorner();
2054 quad[1] = innerRect.minXMaxYCorner();
2055 quad[2] = innerRect.maxXMaxYCorner();
2056 quad[3] = outerRect.maxXMaxYCorner();
2057
2058 if (!innerBorder.radii().bottomLeft().isZero()) {
2059 findIntersection(quad[0], quad[1],
2060 FloatPoint(
2061 quad[1].x() + innerBorder.radii().bottomLeft().width(),
2062 quad[1].y()),
2063 FloatPoint(
2064 quad[1].x(),
2065 quad[1].y() - innerBorder.radii().bottomLeft().height()),
2066 quad[1]);
2067 }
2068
2069 if (!innerBorder.radii().bottomRight().isZero()) {
2070 findIntersection(quad[3], quad[2],
2071 FloatPoint(
2072 quad[2].x() - innerBorder.radii().bottomRight().width(),
2073 quad[2].y()),
2074 FloatPoint(
2075 quad[2].x(),
2076 quad[2].y() - innerBorder.radii().bottomRight().height()),
2077 quad[2]);
2078 }
2079 break;
2080
2081 case BSRight:
2082 quad[0] = outerRect.maxXMinYCorner();
2083 quad[1] = innerRect.maxXMinYCorner();
2084 quad[2] = innerRect.maxXMaxYCorner();
2085 quad[3] = outerRect.maxXMaxYCorner();
2086
2087 if (!innerBorder.radii().topRight().isZero()) {
2088 findIntersection(quad[0], quad[1],
2089 FloatPoint(
2090 quad[1].x() - innerBorder.radii().topRight().width(),
2091 quad[1].y()),
2092 FloatPoint(
2093 quad[1].x(),
2094 quad[1].y() + innerBorder.radii().topRight().height()),
2095 quad[1]);
2096 }
2097
2098 if (!innerBorder.radii().bottomRight().isZero()) {
2099 findIntersection(quad[3], quad[2],
2100 FloatPoint(
2101 quad[2].x() - innerBorder.radii().bottomRight().width(),
2102 quad[2].y()),
2103 FloatPoint(
2104 quad[2].x(),
2105 quad[2].y() - innerBorder.radii().bottomRight().height()),
2106 quad[2]);
2107 }
2108 break;
2109 }
2110
2111 // If the border matches both of its adjacent sides, don't anti-alias the cl ip, and
2112 // if neither side matches, anti-alias the clip.
2113 if (firstEdgeMatches == secondEdgeMatches) {
2114 graphicsContext->clipConvexPolygon(4, quad, !firstEdgeMatches);
2115 return;
2116 }
2117
2118 // If antialiasing settings for the first edge and second edge is different,
2119 // they have to be addressed separately. We do this by breaking the quad int o
2120 // two parallelograms, made by moving quad[1] and quad[2].
2121 float ax = quad[1].x() - quad[0].x();
2122 float ay = quad[1].y() - quad[0].y();
2123 float bx = quad[2].x() - quad[1].x();
2124 float by = quad[2].y() - quad[1].y();
2125 float cx = quad[3].x() - quad[2].x();
2126 float cy = quad[3].y() - quad[2].y();
2127
2128 const static float kEpsilon = 1e-2f;
2129 float r1, r2;
2130 if (fabsf(bx) < kEpsilon && fabsf(by) < kEpsilon) {
2131 // The quad was actually a triangle.
2132 r1 = r2 = 1.0f;
2133 } else {
2134 // Extend parallelogram a bit to hide calculation error
2135 const static float kExtendFill = 1e-2f;
2136
2137 r1 = (-ax * by + ay * bx) / (cx * by - cy * bx) + kExtendFill;
2138 r2 = (-cx * by + cy * bx) / (ax * by - ay * bx) + kExtendFill;
2139 }
2140
2141 FloatPoint firstQuad[4];
2142 firstQuad[0] = quad[0];
2143 firstQuad[1] = quad[1];
2144 firstQuad[2] = FloatPoint(quad[3].x() + r2 * ax, quad[3].y() + r2 * ay);
2145 firstQuad[3] = quad[3];
2146 graphicsContext->clipConvexPolygon(4, firstQuad, !firstEdgeMatches);
2147
2148 FloatPoint secondQuad[4];
2149 secondQuad[0] = quad[0];
2150 secondQuad[1] = FloatPoint(quad[0].x() - r1 * cx, quad[0].y() - r1 * cy);
2151 secondQuad[2] = quad[2];
2152 secondQuad[3] = quad[3];
2153 graphicsContext->clipConvexPolygon(4, secondQuad, !secondEdgeMatches);
2154 }
2155
867 } // namespace blink 2156 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698