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