michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: // vim:cindent:ts=2:et:sw=2: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsStyleConsts.h" michael@0: #include "nsCSSColorUtils.h" michael@0: #include "GeckoProfiler.h" michael@0: #include "nsExpirationTracker.h" michael@0: #include "RoundedRect.h" michael@0: #include "nsClassHashtable.h" michael@0: #include "nsStyleStruct.h" michael@0: #include "gfxContext.h" michael@0: #include "nsCSSRenderingBorders.h" michael@0: #include "mozilla/gfx/2D.h" michael@0: #include "gfx2DGlue.h" michael@0: #include "gfxGradientCache.h" michael@0: #include michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::gfx; michael@0: michael@0: /** michael@0: * nsCSSRendering::PaintBorder michael@0: * nsCSSRendering::PaintOutline michael@0: * -> DrawBorders michael@0: * michael@0: * DrawBorders michael@0: * -> Ability to use specialized approach? michael@0: * |- Draw using specialized function michael@0: * |- separate corners? michael@0: * |- dashed side mask michael@0: * | michael@0: * -> can border be drawn in 1 pass? (e.g., solid border same color all around) michael@0: * |- DrawBorderSides with all 4 sides michael@0: * -> more than 1 pass? michael@0: * |- for each corner michael@0: * |- clip to DoCornerClipSubPath michael@0: * |- for each side adjacent to corner michael@0: * |- clip to DoSideClipSubPath michael@0: * |- DrawBorderSides with one side michael@0: * |- for each side michael@0: * |- DoSideClipWithoutCornersSubPath michael@0: * |- DrawDashedSide || DrawBorderSides with one side michael@0: */ michael@0: michael@0: static void ComputeBorderCornerDimensions(const gfxRect& aOuterRect, michael@0: const gfxRect& aInnerRect, michael@0: const gfxCornerSizes& aRadii, michael@0: gfxCornerSizes *aDimsResult); michael@0: michael@0: // given a side index, get the previous and next side index michael@0: #define NEXT_SIDE(_s) mozilla::css::Side(((_s) + 1) & 3) michael@0: #define PREV_SIDE(_s) mozilla::css::Side(((_s) + 3) & 3) michael@0: michael@0: // from the given base color and the background color, turn michael@0: // color into a color for the given border pattern style michael@0: static gfxRGBA MakeBorderColor(const gfxRGBA& aColor, michael@0: const gfxRGBA& aBackgroundColor, michael@0: BorderColorStyle aBorderColorStyle); michael@0: michael@0: michael@0: // Given a line index (an index starting from the outside of the michael@0: // border going inwards) and an array of line styles, calculate the michael@0: // color that that stripe of the border should be rendered in. michael@0: static gfxRGBA ComputeColorForLine(uint32_t aLineIndex, michael@0: const BorderColorStyle* aBorderColorStyle, michael@0: uint32_t aBorderColorStyleCount, michael@0: nscolor aBorderColor, michael@0: nscolor aBackgroundColor); michael@0: michael@0: static gfxRGBA ComputeCompositeColorForLine(uint32_t aLineIndex, michael@0: const nsBorderColors* aBorderColors); michael@0: michael@0: // little helper function to check if the array of 4 floats given are michael@0: // equal to the given value michael@0: static bool michael@0: CheckFourFloatsEqual(const gfxFloat *vals, gfxFloat k) michael@0: { michael@0: return (vals[0] == k && michael@0: vals[1] == k && michael@0: vals[2] == k && michael@0: vals[3] == k); michael@0: } michael@0: michael@0: static bool michael@0: IsZeroSize(const gfxSize& sz) { michael@0: return sz.width == 0.0 || sz.height == 0.0; michael@0: } michael@0: michael@0: static bool michael@0: AllCornersZeroSize(const gfxCornerSizes& corners) { michael@0: return IsZeroSize(corners[NS_CORNER_TOP_LEFT]) && michael@0: IsZeroSize(corners[NS_CORNER_TOP_RIGHT]) && michael@0: IsZeroSize(corners[NS_CORNER_BOTTOM_RIGHT]) && michael@0: IsZeroSize(corners[NS_CORNER_BOTTOM_LEFT]); michael@0: } michael@0: michael@0: typedef enum { michael@0: // Normal solid square corner. Will be rectangular, the size of the michael@0: // adjacent sides. If the corner has a border radius, the corner michael@0: // will always be solid, since we don't do dotted/dashed etc. michael@0: CORNER_NORMAL, michael@0: michael@0: // Paint the corner in whatever style is not dotted/dashed of the michael@0: // adjacent corners. michael@0: CORNER_SOLID, michael@0: michael@0: // Paint the corner as a dot, the size of the bigger of the adjacent michael@0: // sides. michael@0: CORNER_DOT michael@0: } CornerStyle; michael@0: michael@0: nsCSSBorderRenderer::nsCSSBorderRenderer(int32_t aAppUnitsPerPixel, michael@0: gfxContext* aDestContext, michael@0: gfxRect& aOuterRect, michael@0: const uint8_t* aBorderStyles, michael@0: const gfxFloat* aBorderWidths, michael@0: gfxCornerSizes& aBorderRadii, michael@0: const nscolor* aBorderColors, michael@0: nsBorderColors* const* aCompositeColors, michael@0: int aSkipSides, michael@0: nscolor aBackgroundColor) michael@0: : mContext(aDestContext), michael@0: mOuterRect(aOuterRect), michael@0: mBorderStyles(aBorderStyles), michael@0: mBorderWidths(aBorderWidths), michael@0: mBorderRadii(aBorderRadii), michael@0: mBorderColors(aBorderColors), michael@0: mCompositeColors(aCompositeColors), michael@0: mAUPP(aAppUnitsPerPixel), michael@0: mSkipSides(aSkipSides), michael@0: mBackgroundColor(aBackgroundColor) michael@0: { michael@0: if (!mCompositeColors) { michael@0: static nsBorderColors * const noColors[4] = { nullptr }; michael@0: mCompositeColors = &noColors[0]; michael@0: } michael@0: michael@0: mInnerRect = mOuterRect; michael@0: mInnerRect.Deflate( michael@0: gfxMargin(mBorderStyles[0] != NS_STYLE_BORDER_STYLE_NONE ? mBorderWidths[0] : 0, michael@0: mBorderStyles[1] != NS_STYLE_BORDER_STYLE_NONE ? mBorderWidths[1] : 0, michael@0: mBorderStyles[2] != NS_STYLE_BORDER_STYLE_NONE ? mBorderWidths[2] : 0, michael@0: mBorderStyles[3] != NS_STYLE_BORDER_STYLE_NONE ? mBorderWidths[3] : 0)); michael@0: michael@0: ComputeBorderCornerDimensions(mOuterRect, mInnerRect, mBorderRadii, &mBorderCornerDimensions); michael@0: michael@0: mOneUnitBorder = CheckFourFloatsEqual(mBorderWidths, 1.0); michael@0: mNoBorderRadius = AllCornersZeroSize(mBorderRadii); michael@0: mAvoidStroke = false; michael@0: } michael@0: michael@0: /* static */ void michael@0: nsCSSBorderRenderer::ComputeInnerRadii(const gfxCornerSizes& aRadii, michael@0: const gfxFloat *aBorderSizes, michael@0: gfxCornerSizes *aInnerRadiiRet) michael@0: { michael@0: gfxCornerSizes& iRadii = *aInnerRadiiRet; michael@0: michael@0: iRadii[C_TL].width = std::max(0.0, aRadii[C_TL].width - aBorderSizes[NS_SIDE_LEFT]); michael@0: iRadii[C_TL].height = std::max(0.0, aRadii[C_TL].height - aBorderSizes[NS_SIDE_TOP]); michael@0: michael@0: iRadii[C_TR].width = std::max(0.0, aRadii[C_TR].width - aBorderSizes[NS_SIDE_RIGHT]); michael@0: iRadii[C_TR].height = std::max(0.0, aRadii[C_TR].height - aBorderSizes[NS_SIDE_TOP]); michael@0: michael@0: iRadii[C_BR].width = std::max(0.0, aRadii[C_BR].width - aBorderSizes[NS_SIDE_RIGHT]); michael@0: iRadii[C_BR].height = std::max(0.0, aRadii[C_BR].height - aBorderSizes[NS_SIDE_BOTTOM]); michael@0: michael@0: iRadii[C_BL].width = std::max(0.0, aRadii[C_BL].width - aBorderSizes[NS_SIDE_LEFT]); michael@0: iRadii[C_BL].height = std::max(0.0, aRadii[C_BL].height - aBorderSizes[NS_SIDE_BOTTOM]); michael@0: } michael@0: michael@0: /* static */ void michael@0: nsCSSBorderRenderer::ComputeOuterRadii(const gfxCornerSizes& aRadii, michael@0: const gfxFloat *aBorderSizes, michael@0: gfxCornerSizes *aOuterRadiiRet) michael@0: { michael@0: gfxCornerSizes& oRadii = *aOuterRadiiRet; michael@0: michael@0: // default all corners to sharp corners michael@0: oRadii = gfxCornerSizes(0.0); michael@0: michael@0: // round the edges that have radii > 0.0 to start with michael@0: if (aRadii[C_TL].width > 0.0 && aRadii[C_TL].height > 0.0) { michael@0: oRadii[C_TL].width = std::max(0.0, aRadii[C_TL].width + aBorderSizes[NS_SIDE_LEFT]); michael@0: oRadii[C_TL].height = std::max(0.0, aRadii[C_TL].height + aBorderSizes[NS_SIDE_TOP]); michael@0: } michael@0: michael@0: if (aRadii[C_TR].width > 0.0 && aRadii[C_TR].height > 0.0) { michael@0: oRadii[C_TR].width = std::max(0.0, aRadii[C_TR].width + aBorderSizes[NS_SIDE_RIGHT]); michael@0: oRadii[C_TR].height = std::max(0.0, aRadii[C_TR].height + aBorderSizes[NS_SIDE_TOP]); michael@0: } michael@0: michael@0: if (aRadii[C_BR].width > 0.0 && aRadii[C_BR].height > 0.0) { michael@0: oRadii[C_BR].width = std::max(0.0, aRadii[C_BR].width + aBorderSizes[NS_SIDE_RIGHT]); michael@0: oRadii[C_BR].height = std::max(0.0, aRadii[C_BR].height + aBorderSizes[NS_SIDE_BOTTOM]); michael@0: } michael@0: michael@0: if (aRadii[C_BL].width > 0.0 && aRadii[C_BL].height > 0.0) { michael@0: oRadii[C_BL].width = std::max(0.0, aRadii[C_BL].width + aBorderSizes[NS_SIDE_LEFT]); michael@0: oRadii[C_BL].height = std::max(0.0, aRadii[C_BL].height + aBorderSizes[NS_SIDE_BOTTOM]); michael@0: } michael@0: } michael@0: michael@0: /*static*/ void michael@0: ComputeBorderCornerDimensions(const gfxRect& aOuterRect, michael@0: const gfxRect& aInnerRect, michael@0: const gfxCornerSizes& aRadii, michael@0: gfxCornerSizes *aDimsRet) michael@0: { michael@0: gfxFloat leftWidth = aInnerRect.X() - aOuterRect.X(); michael@0: gfxFloat topWidth = aInnerRect.Y() - aOuterRect.Y(); michael@0: gfxFloat rightWidth = aOuterRect.Width() - aInnerRect.Width() - leftWidth; michael@0: gfxFloat bottomWidth = aOuterRect.Height() - aInnerRect.Height() - topWidth; michael@0: michael@0: if (AllCornersZeroSize(aRadii)) { michael@0: // These will always be in pixel units from CSS michael@0: (*aDimsRet)[C_TL] = gfxSize(leftWidth, topWidth); michael@0: (*aDimsRet)[C_TR] = gfxSize(rightWidth, topWidth); michael@0: (*aDimsRet)[C_BR] = gfxSize(rightWidth, bottomWidth); michael@0: (*aDimsRet)[C_BL] = gfxSize(leftWidth, bottomWidth); michael@0: } else { michael@0: // Always round up to whole pixels for the corners; it's safe to michael@0: // make the corners bigger than necessary, and this way we ensure michael@0: // that we avoid seams. michael@0: (*aDimsRet)[C_TL] = gfxSize(ceil(std::max(leftWidth, aRadii[C_TL].width)), michael@0: ceil(std::max(topWidth, aRadii[C_TL].height))); michael@0: (*aDimsRet)[C_TR] = gfxSize(ceil(std::max(rightWidth, aRadii[C_TR].width)), michael@0: ceil(std::max(topWidth, aRadii[C_TR].height))); michael@0: (*aDimsRet)[C_BR] = gfxSize(ceil(std::max(rightWidth, aRadii[C_BR].width)), michael@0: ceil(std::max(bottomWidth, aRadii[C_BR].height))); michael@0: (*aDimsRet)[C_BL] = gfxSize(ceil(std::max(leftWidth, aRadii[C_BL].width)), michael@0: ceil(std::max(bottomWidth, aRadii[C_BL].height))); michael@0: } michael@0: } michael@0: michael@0: bool michael@0: nsCSSBorderRenderer::AreBorderSideFinalStylesSame(uint8_t aSides) michael@0: { michael@0: NS_ASSERTION(aSides != 0 && (aSides & ~SIDE_BITS_ALL) == 0, michael@0: "AreBorderSidesSame: invalid whichSides!"); michael@0: michael@0: /* First check if the specified styles and colors are the same for all sides */ michael@0: int firstStyle = 0; michael@0: NS_FOR_CSS_SIDES (i) { michael@0: if (firstStyle == i) { michael@0: if (((1 << i) & aSides) == 0) michael@0: firstStyle++; michael@0: continue; michael@0: } michael@0: michael@0: if (((1 << i) & aSides) == 0) { michael@0: continue; michael@0: } michael@0: michael@0: if (mBorderStyles[firstStyle] != mBorderStyles[i] || michael@0: mBorderColors[firstStyle] != mBorderColors[i] || michael@0: !nsBorderColors::Equal(mCompositeColors[firstStyle], michael@0: mCompositeColors[i])) michael@0: return false; michael@0: } michael@0: michael@0: /* Then if it's one of the two-tone styles and we're not michael@0: * just comparing the TL or BR sides */ michael@0: switch (mBorderStyles[firstStyle]) { michael@0: case NS_STYLE_BORDER_STYLE_GROOVE: michael@0: case NS_STYLE_BORDER_STYLE_RIDGE: michael@0: case NS_STYLE_BORDER_STYLE_INSET: michael@0: case NS_STYLE_BORDER_STYLE_OUTSET: michael@0: return ((aSides & ~(SIDE_BIT_TOP | SIDE_BIT_LEFT)) == 0 || michael@0: (aSides & ~(SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT)) == 0); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: nsCSSBorderRenderer::IsSolidCornerStyle(uint8_t aStyle, mozilla::css::Corner aCorner) michael@0: { michael@0: switch (aStyle) { michael@0: case NS_STYLE_BORDER_STYLE_DOTTED: michael@0: case NS_STYLE_BORDER_STYLE_DASHED: michael@0: case NS_STYLE_BORDER_STYLE_SOLID: michael@0: return true; michael@0: michael@0: case NS_STYLE_BORDER_STYLE_INSET: michael@0: case NS_STYLE_BORDER_STYLE_OUTSET: michael@0: return (aCorner == NS_CORNER_TOP_LEFT || aCorner == NS_CORNER_BOTTOM_RIGHT); michael@0: michael@0: case NS_STYLE_BORDER_STYLE_GROOVE: michael@0: case NS_STYLE_BORDER_STYLE_RIDGE: michael@0: return mOneUnitBorder && (aCorner == NS_CORNER_TOP_LEFT || aCorner == NS_CORNER_BOTTOM_RIGHT); michael@0: michael@0: case NS_STYLE_BORDER_STYLE_DOUBLE: michael@0: return mOneUnitBorder; michael@0: michael@0: default: michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: BorderColorStyle michael@0: nsCSSBorderRenderer::BorderColorStyleForSolidCorner(uint8_t aStyle, mozilla::css::Corner aCorner) michael@0: { michael@0: // note that this function assumes that the corner is already solid, michael@0: // as per the earlier function michael@0: switch (aStyle) { michael@0: case NS_STYLE_BORDER_STYLE_DOTTED: michael@0: case NS_STYLE_BORDER_STYLE_DASHED: michael@0: case NS_STYLE_BORDER_STYLE_SOLID: michael@0: case NS_STYLE_BORDER_STYLE_DOUBLE: michael@0: return BorderColorStyleSolid; michael@0: michael@0: case NS_STYLE_BORDER_STYLE_INSET: michael@0: case NS_STYLE_BORDER_STYLE_GROOVE: michael@0: if (aCorner == NS_CORNER_TOP_LEFT) michael@0: return BorderColorStyleDark; michael@0: else if (aCorner == NS_CORNER_BOTTOM_RIGHT) michael@0: return BorderColorStyleLight; michael@0: break; michael@0: michael@0: case NS_STYLE_BORDER_STYLE_OUTSET: michael@0: case NS_STYLE_BORDER_STYLE_RIDGE: michael@0: if (aCorner == NS_CORNER_TOP_LEFT) michael@0: return BorderColorStyleLight; michael@0: else if (aCorner == NS_CORNER_BOTTOM_RIGHT) michael@0: return BorderColorStyleDark; michael@0: break; michael@0: } michael@0: michael@0: return BorderColorStyleNone; michael@0: } michael@0: michael@0: void michael@0: nsCSSBorderRenderer::DoCornerSubPath(mozilla::css::Corner aCorner) michael@0: { michael@0: gfxPoint offset(0.0, 0.0); michael@0: michael@0: if (aCorner == C_TR || aCorner == C_BR) michael@0: offset.x = mOuterRect.Width() - mBorderCornerDimensions[aCorner].width; michael@0: if (aCorner == C_BR || aCorner == C_BL) michael@0: offset.y = mOuterRect.Height() - mBorderCornerDimensions[aCorner].height; michael@0: michael@0: mContext->Rectangle(gfxRect(mOuterRect.TopLeft() + offset, michael@0: mBorderCornerDimensions[aCorner])); michael@0: } michael@0: michael@0: void michael@0: nsCSSBorderRenderer::DoSideClipWithoutCornersSubPath(mozilla::css::Side aSide) michael@0: { michael@0: gfxPoint offset(0.0, 0.0); michael@0: michael@0: // The offset from the outside rect to the start of this side's michael@0: // box. For the top and bottom sides, the height of the box michael@0: // must be the border height; the x start must take into account michael@0: // the corner size (which may be bigger than the right or left michael@0: // side's width). The same applies to the right and left sides. michael@0: if (aSide == NS_SIDE_TOP) { michael@0: offset.x = mBorderCornerDimensions[C_TL].width; michael@0: } else if (aSide == NS_SIDE_RIGHT) { michael@0: offset.x = mOuterRect.Width() - mBorderWidths[NS_SIDE_RIGHT]; michael@0: offset.y = mBorderCornerDimensions[C_TR].height; michael@0: } else if (aSide == NS_SIDE_BOTTOM) { michael@0: offset.x = mBorderCornerDimensions[C_BL].width; michael@0: offset.y = mOuterRect.Height() - mBorderWidths[NS_SIDE_BOTTOM]; michael@0: } else if (aSide == NS_SIDE_LEFT) { michael@0: offset.y = mBorderCornerDimensions[C_TL].height; michael@0: } michael@0: michael@0: // The sum of the width & height of the corners adjacent to the michael@0: // side. This relies on the relationship between side indexing and michael@0: // corner indexing; that is, 0 == SIDE_TOP and 0 == CORNER_TOP_LEFT, michael@0: // with both proceeding clockwise. michael@0: gfxSize sideCornerSum = mBorderCornerDimensions[mozilla::css::Corner(aSide)] michael@0: + mBorderCornerDimensions[mozilla::css::Corner(NEXT_SIDE(aSide))]; michael@0: gfxRect rect(mOuterRect.TopLeft() + offset, michael@0: mOuterRect.Size() - sideCornerSum); michael@0: michael@0: if (aSide == NS_SIDE_TOP || aSide == NS_SIDE_BOTTOM) michael@0: rect.height = mBorderWidths[aSide]; michael@0: else michael@0: rect.width = mBorderWidths[aSide]; michael@0: michael@0: mContext->Rectangle(rect); michael@0: } michael@0: michael@0: // The side border type and the adjacent border types are michael@0: // examined and one of the different types of clipping (listed michael@0: // below) is selected. michael@0: michael@0: typedef enum { michael@0: // clip to the trapezoid formed by the corners of the michael@0: // inner and outer rectangles for the given side michael@0: SIDE_CLIP_TRAPEZOID, michael@0: michael@0: // clip to the trapezoid formed by the outer rectangle michael@0: // corners and the center of the region, making sure michael@0: // that diagonal lines all go directly from the outside michael@0: // corner to the inside corner, but that they then continue on michael@0: // to the middle. michael@0: // michael@0: // This is needed for correctly clipping rounded borders, michael@0: // which might extend past the SIDE_CLIP_TRAPEZOID trap. michael@0: SIDE_CLIP_TRAPEZOID_FULL, michael@0: michael@0: // clip to the rectangle formed by the given side; a specific michael@0: // overlap algorithm is used; see the function for details. michael@0: // this is currently used for dashing. michael@0: SIDE_CLIP_RECTANGLE michael@0: } SideClipType; michael@0: michael@0: // Given three points, p0, p1, and midPoint, move p1 further in to the michael@0: // rectangle (of which aMidPoint is the center) so that it reaches the michael@0: // closer of the horizontal or vertical lines intersecting the midpoint, michael@0: // while maintaing the slope of the line. If p0 and p1 are the same, michael@0: // just move p1 to midPoint (since there's no slope to maintain). michael@0: // FIXME: Extending only to the midpoint isn't actually sufficient for michael@0: // boxes with asymmetric radii. michael@0: static void michael@0: MaybeMoveToMidPoint(gfxPoint& aP0, gfxPoint& aP1, const gfxPoint& aMidPoint) michael@0: { michael@0: gfxPoint ps = aP1 - aP0; michael@0: michael@0: if (ps.x == 0.0) { michael@0: if (ps.y == 0.0) { michael@0: aP1 = aMidPoint; michael@0: } else { michael@0: aP1.y = aMidPoint.y; michael@0: } michael@0: } else { michael@0: if (ps.y == 0.0) { michael@0: aP1.x = aMidPoint.x; michael@0: } else { michael@0: gfxFloat k = std::min((aMidPoint.x - aP0.x) / ps.x, michael@0: (aMidPoint.y - aP0.y) / ps.y); michael@0: aP1 = aP0 + ps * k; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsCSSBorderRenderer::DoSideClipSubPath(mozilla::css::Side aSide) michael@0: { michael@0: // the clip proceeds clockwise from the top left corner; michael@0: // so "start" in each case is the start of the region from that side. michael@0: // michael@0: // the final path will be formed like: michael@0: // s0 ------- e0 michael@0: // | / michael@0: // s1 ----- e1 michael@0: // michael@0: // that is, the second point will always be on the inside michael@0: michael@0: gfxPoint start[2]; michael@0: gfxPoint end[2]; michael@0: michael@0: #define IS_DASHED_OR_DOTTED(_s) ((_s) == NS_STYLE_BORDER_STYLE_DASHED || (_s) == NS_STYLE_BORDER_STYLE_DOTTED) michael@0: bool isDashed = IS_DASHED_OR_DOTTED(mBorderStyles[aSide]); michael@0: bool startIsDashed = IS_DASHED_OR_DOTTED(mBorderStyles[PREV_SIDE(aSide)]); michael@0: bool endIsDashed = IS_DASHED_OR_DOTTED(mBorderStyles[NEXT_SIDE(aSide)]); michael@0: #undef IS_DASHED_OR_DOTTED michael@0: michael@0: SideClipType startType = SIDE_CLIP_TRAPEZOID; michael@0: SideClipType endType = SIDE_CLIP_TRAPEZOID; michael@0: michael@0: if (!IsZeroSize(mBorderRadii[mozilla::css::Corner(aSide)])) michael@0: startType = SIDE_CLIP_TRAPEZOID_FULL; michael@0: else if (startIsDashed && isDashed) michael@0: startType = SIDE_CLIP_RECTANGLE; michael@0: michael@0: if (!IsZeroSize(mBorderRadii[mozilla::css::Corner(NEXT_SIDE(aSide))])) michael@0: endType = SIDE_CLIP_TRAPEZOID_FULL; michael@0: else if (endIsDashed && isDashed) michael@0: endType = SIDE_CLIP_RECTANGLE; michael@0: michael@0: gfxPoint midPoint = mInnerRect.Center(); michael@0: michael@0: start[0] = mOuterRect.CCWCorner(aSide); michael@0: start[1] = mInnerRect.CCWCorner(aSide); michael@0: michael@0: end[0] = mOuterRect.CWCorner(aSide); michael@0: end[1] = mInnerRect.CWCorner(aSide); michael@0: michael@0: if (startType == SIDE_CLIP_TRAPEZOID_FULL) { michael@0: MaybeMoveToMidPoint(start[0], start[1], midPoint); michael@0: } else if (startType == SIDE_CLIP_RECTANGLE) { michael@0: if (aSide == NS_SIDE_TOP || aSide == NS_SIDE_BOTTOM) michael@0: start[1] = gfxPoint(mOuterRect.CCWCorner(aSide).x, mInnerRect.CCWCorner(aSide).y); michael@0: else michael@0: start[1] = gfxPoint(mInnerRect.CCWCorner(aSide).x, mOuterRect.CCWCorner(aSide).y); michael@0: } michael@0: michael@0: if (endType == SIDE_CLIP_TRAPEZOID_FULL) { michael@0: MaybeMoveToMidPoint(end[0], end[1], midPoint); michael@0: } else if (endType == SIDE_CLIP_RECTANGLE) { michael@0: if (aSide == NS_SIDE_TOP || aSide == NS_SIDE_BOTTOM) michael@0: end[0] = gfxPoint(mInnerRect.CWCorner(aSide).x, mOuterRect.CWCorner(aSide).y); michael@0: else michael@0: end[0] = gfxPoint(mOuterRect.CWCorner(aSide).x, mInnerRect.CWCorner(aSide).y); michael@0: } michael@0: michael@0: mContext->MoveTo(start[0]); michael@0: mContext->LineTo(end[0]); michael@0: mContext->LineTo(end[1]); michael@0: mContext->LineTo(start[1]); michael@0: mContext->ClosePath(); michael@0: } michael@0: michael@0: void michael@0: nsCSSBorderRenderer::FillSolidBorder(const gfxRect& aOuterRect, michael@0: const gfxRect& aInnerRect, michael@0: const gfxCornerSizes& aBorderRadii, michael@0: const gfxFloat *aBorderSizes, michael@0: int aSides, michael@0: const gfxRGBA& aColor) michael@0: { michael@0: mContext->SetColor(aColor); michael@0: // Note that this function is allowed to draw more than just the michael@0: // requested sides. michael@0: michael@0: // If we have a border radius, do full rounded rectangles michael@0: // and fill, regardless of what sides we're asked to draw. michael@0: if (!AllCornersZeroSize(aBorderRadii)) { michael@0: gfxCornerSizes innerRadii; michael@0: ComputeInnerRadii(aBorderRadii, aBorderSizes, &innerRadii); michael@0: michael@0: mContext->NewPath(); michael@0: michael@0: // do the outer border michael@0: mContext->RoundedRectangle(aOuterRect, aBorderRadii, true); michael@0: michael@0: // then do the inner border CCW michael@0: mContext->RoundedRectangle(aInnerRect, innerRadii, false); michael@0: michael@0: mContext->Fill(); michael@0: michael@0: return; michael@0: } michael@0: michael@0: // If we're asked to draw all sides of an equal-sized border, michael@0: // stroking is fastest. This is a fairly common path, but partial michael@0: // sides is probably second in the list -- there are a bunch of michael@0: // common border styles, such as inset and outset, that are michael@0: // top-left/bottom-right split. michael@0: if (aSides == SIDE_BITS_ALL && michael@0: CheckFourFloatsEqual(aBorderSizes, aBorderSizes[0]) && michael@0: !mAvoidStroke) michael@0: { michael@0: gfxRect r(aOuterRect); michael@0: r.Deflate(aBorderSizes[0] / 2.0); michael@0: mContext->SetLineWidth(aBorderSizes[0]); michael@0: michael@0: mContext->NewPath(); michael@0: mContext->Rectangle(r); michael@0: mContext->Stroke(); michael@0: michael@0: return; michael@0: } michael@0: michael@0: // Otherwise, we have unequal sized borders or we're only michael@0: // drawing some sides; create rectangles for each side michael@0: // and fill them. michael@0: michael@0: gfxRect r[4]; michael@0: michael@0: // compute base rects for each side michael@0: if (aSides & SIDE_BIT_TOP) { michael@0: r[NS_SIDE_TOP] = michael@0: gfxRect(aOuterRect.X(), aOuterRect.Y(), michael@0: aOuterRect.Width(), aBorderSizes[NS_SIDE_TOP]); michael@0: } michael@0: michael@0: if (aSides & SIDE_BIT_BOTTOM) { michael@0: r[NS_SIDE_BOTTOM] = michael@0: gfxRect(aOuterRect.X(), aOuterRect.YMost() - aBorderSizes[NS_SIDE_BOTTOM], michael@0: aOuterRect.Width(), aBorderSizes[NS_SIDE_BOTTOM]); michael@0: } michael@0: michael@0: if (aSides & SIDE_BIT_LEFT) { michael@0: r[NS_SIDE_LEFT] = michael@0: gfxRect(aOuterRect.X(), aOuterRect.Y(), michael@0: aBorderSizes[NS_SIDE_LEFT], aOuterRect.Height()); michael@0: } michael@0: michael@0: if (aSides & SIDE_BIT_RIGHT) { michael@0: r[NS_SIDE_RIGHT] = michael@0: gfxRect(aOuterRect.XMost() - aBorderSizes[NS_SIDE_RIGHT], aOuterRect.Y(), michael@0: aBorderSizes[NS_SIDE_RIGHT], aOuterRect.Height()); michael@0: } michael@0: michael@0: // If two sides meet at a corner that we're rendering, then michael@0: // make sure that we adjust one of the sides to avoid overlap. michael@0: // This is especially important in the case of colors with michael@0: // an alpha channel. michael@0: michael@0: if ((aSides & (SIDE_BIT_TOP | SIDE_BIT_LEFT)) == (SIDE_BIT_TOP | SIDE_BIT_LEFT)) { michael@0: // adjust the left's top down a bit michael@0: r[NS_SIDE_LEFT].y += aBorderSizes[NS_SIDE_TOP]; michael@0: r[NS_SIDE_LEFT].height -= aBorderSizes[NS_SIDE_TOP]; michael@0: } michael@0: michael@0: if ((aSides & (SIDE_BIT_TOP | SIDE_BIT_RIGHT)) == (SIDE_BIT_TOP | SIDE_BIT_RIGHT)) { michael@0: // adjust the top's left a bit michael@0: r[NS_SIDE_TOP].width -= aBorderSizes[NS_SIDE_RIGHT]; michael@0: } michael@0: michael@0: if ((aSides & (SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT)) == (SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT)) { michael@0: // adjust the right's bottom a bit michael@0: r[NS_SIDE_RIGHT].height -= aBorderSizes[NS_SIDE_BOTTOM]; michael@0: } michael@0: michael@0: if ((aSides & (SIDE_BIT_BOTTOM | SIDE_BIT_LEFT)) == (SIDE_BIT_BOTTOM | SIDE_BIT_LEFT)) { michael@0: // adjust the bottom's left a bit michael@0: r[NS_SIDE_BOTTOM].x += aBorderSizes[NS_SIDE_LEFT]; michael@0: r[NS_SIDE_BOTTOM].width -= aBorderSizes[NS_SIDE_LEFT]; michael@0: } michael@0: michael@0: // Filling these one by one is faster than filling them all at once. michael@0: for (uint32_t i = 0; i < 4; i++) { michael@0: if (aSides & (1 << i)) { michael@0: mContext->NewPath(); michael@0: mContext->Rectangle(r[i], true); michael@0: mContext->Fill(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: gfxRGBA michael@0: MakeBorderColor(const gfxRGBA& aColor, const gfxRGBA& aBackgroundColor, BorderColorStyle aBorderColorStyle) michael@0: { michael@0: nscolor colors[2]; michael@0: int k = 0; michael@0: michael@0: switch (aBorderColorStyle) { michael@0: case BorderColorStyleNone: michael@0: return gfxRGBA(0.0, 0.0, 0.0, 0.0); michael@0: michael@0: case BorderColorStyleLight: michael@0: k = 1; michael@0: /* fall through */ michael@0: case BorderColorStyleDark: michael@0: NS_GetSpecial3DColors(colors, aBackgroundColor.Packed(), aColor.Packed()); michael@0: return gfxRGBA(colors[k]); michael@0: michael@0: case BorderColorStyleSolid: michael@0: default: michael@0: return aColor; michael@0: } michael@0: } michael@0: michael@0: gfxRGBA michael@0: ComputeColorForLine(uint32_t aLineIndex, michael@0: const BorderColorStyle* aBorderColorStyle, michael@0: uint32_t aBorderColorStyleCount, michael@0: nscolor aBorderColor, michael@0: nscolor aBackgroundColor) michael@0: { michael@0: NS_ASSERTION(aLineIndex < aBorderColorStyleCount, "Invalid lineIndex given"); michael@0: michael@0: return MakeBorderColor(gfxRGBA(aBorderColor), gfxRGBA(aBackgroundColor), aBorderColorStyle[aLineIndex]); michael@0: } michael@0: michael@0: gfxRGBA michael@0: ComputeCompositeColorForLine(uint32_t aLineIndex, michael@0: const nsBorderColors* aBorderColors) michael@0: { michael@0: while (aLineIndex-- && aBorderColors->mNext) michael@0: aBorderColors = aBorderColors->mNext; michael@0: michael@0: return gfxRGBA(aBorderColors->mColor); michael@0: } michael@0: michael@0: void michael@0: nsCSSBorderRenderer::DrawBorderSidesCompositeColors(int aSides, const nsBorderColors *aCompositeColors) michael@0: { michael@0: gfxCornerSizes radii = mBorderRadii; michael@0: michael@0: // the generic composite colors path; each border is 1px in size michael@0: gfxRect soRect = mOuterRect; michael@0: gfxFloat maxBorderWidth = 0; michael@0: NS_FOR_CSS_SIDES (i) { michael@0: maxBorderWidth = std::max(maxBorderWidth, mBorderWidths[i]); michael@0: } michael@0: michael@0: gfxFloat fakeBorderSizes[4]; michael@0: michael@0: gfxPoint itl = mInnerRect.TopLeft(); michael@0: gfxPoint ibr = mInnerRect.BottomRight(); michael@0: michael@0: for (uint32_t i = 0; i < uint32_t(maxBorderWidth); i++) { michael@0: gfxRGBA lineColor = ComputeCompositeColorForLine(i, aCompositeColors); michael@0: michael@0: gfxRect siRect = soRect; michael@0: siRect.Deflate(1.0); michael@0: michael@0: // now cap the rects to the real mInnerRect michael@0: gfxPoint tl = siRect.TopLeft(); michael@0: gfxPoint br = siRect.BottomRight(); michael@0: michael@0: tl.x = std::min(tl.x, itl.x); michael@0: tl.y = std::min(tl.y, itl.y); michael@0: michael@0: br.x = std::max(br.x, ibr.x); michael@0: br.y = std::max(br.y, ibr.y); michael@0: michael@0: siRect = gfxRect(tl.x, tl.y, br.x - tl.x , br.y - tl.y); michael@0: michael@0: fakeBorderSizes[NS_SIDE_TOP] = siRect.TopLeft().y - soRect.TopLeft().y; michael@0: fakeBorderSizes[NS_SIDE_RIGHT] = soRect.TopRight().x - siRect.TopRight().x; michael@0: fakeBorderSizes[NS_SIDE_BOTTOM] = soRect.BottomRight().y - siRect.BottomRight().y; michael@0: fakeBorderSizes[NS_SIDE_LEFT] = siRect.BottomLeft().x - soRect.BottomLeft().x; michael@0: michael@0: FillSolidBorder(soRect, siRect, radii, fakeBorderSizes, aSides, lineColor); michael@0: michael@0: soRect = siRect; michael@0: michael@0: ComputeInnerRadii(radii, fakeBorderSizes, &radii); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsCSSBorderRenderer::DrawBorderSides(int aSides) michael@0: { michael@0: if (aSides == 0 || (aSides & ~SIDE_BITS_ALL) != 0) { michael@0: NS_WARNING("DrawBorderSides: invalid sides!"); michael@0: return; michael@0: } michael@0: michael@0: uint8_t borderRenderStyle = NS_STYLE_BORDER_STYLE_NONE; michael@0: nscolor borderRenderColor; michael@0: const nsBorderColors *compositeColors = nullptr; michael@0: michael@0: uint32_t borderColorStyleCount = 0; michael@0: BorderColorStyle borderColorStyleTopLeft[3], borderColorStyleBottomRight[3]; michael@0: BorderColorStyle *borderColorStyle = nullptr; michael@0: michael@0: NS_FOR_CSS_SIDES (i) { michael@0: if ((aSides & (1 << i)) == 0) michael@0: continue; michael@0: borderRenderStyle = mBorderStyles[i]; michael@0: borderRenderColor = mBorderColors[i]; michael@0: compositeColors = mCompositeColors[i]; michael@0: break; michael@0: } michael@0: michael@0: if (borderRenderStyle == NS_STYLE_BORDER_STYLE_NONE || michael@0: borderRenderStyle == NS_STYLE_BORDER_STYLE_HIDDEN) michael@0: return; michael@0: michael@0: // -moz-border-colors is a hack; if we have it for a border, then michael@0: // it's always drawn solid, and each color is given 1px. The last michael@0: // color is used for the remainder of the border's size. Just michael@0: // hand off to another function to do all that. michael@0: if (compositeColors) { michael@0: DrawBorderSidesCompositeColors(aSides, compositeColors); michael@0: return; michael@0: } michael@0: michael@0: // We're not doing compositeColors, so we can calculate the michael@0: // borderColorStyle based on the specified style. The michael@0: // borderColorStyle array goes from the outer to the inner style. michael@0: // michael@0: // If the border width is 1, we need to change the borderRenderStyle michael@0: // a bit to make sure that we get the right colors -- e.g. 'ridge' michael@0: // with a 1px border needs to look like solid, not like 'outset'. michael@0: if (mOneUnitBorder && michael@0: (borderRenderStyle == NS_STYLE_BORDER_STYLE_RIDGE || michael@0: borderRenderStyle == NS_STYLE_BORDER_STYLE_GROOVE || michael@0: borderRenderStyle == NS_STYLE_BORDER_STYLE_DOUBLE)) michael@0: borderRenderStyle = NS_STYLE_BORDER_STYLE_SOLID; michael@0: michael@0: switch (borderRenderStyle) { michael@0: case NS_STYLE_BORDER_STYLE_SOLID: michael@0: case NS_STYLE_BORDER_STYLE_DASHED: michael@0: case NS_STYLE_BORDER_STYLE_DOTTED: michael@0: borderColorStyleTopLeft[0] = BorderColorStyleSolid; michael@0: michael@0: borderColorStyleBottomRight[0] = BorderColorStyleSolid; michael@0: michael@0: borderColorStyleCount = 1; michael@0: break; michael@0: michael@0: case NS_STYLE_BORDER_STYLE_GROOVE: michael@0: borderColorStyleTopLeft[0] = BorderColorStyleDark; michael@0: borderColorStyleTopLeft[1] = BorderColorStyleLight; michael@0: michael@0: borderColorStyleBottomRight[0] = BorderColorStyleLight; michael@0: borderColorStyleBottomRight[1] = BorderColorStyleDark; michael@0: michael@0: borderColorStyleCount = 2; michael@0: break; michael@0: michael@0: case NS_STYLE_BORDER_STYLE_RIDGE: michael@0: borderColorStyleTopLeft[0] = BorderColorStyleLight; michael@0: borderColorStyleTopLeft[1] = BorderColorStyleDark; michael@0: michael@0: borderColorStyleBottomRight[0] = BorderColorStyleDark; michael@0: borderColorStyleBottomRight[1] = BorderColorStyleLight; michael@0: michael@0: borderColorStyleCount = 2; michael@0: break; michael@0: michael@0: case NS_STYLE_BORDER_STYLE_DOUBLE: michael@0: borderColorStyleTopLeft[0] = BorderColorStyleSolid; michael@0: borderColorStyleTopLeft[1] = BorderColorStyleNone; michael@0: borderColorStyleTopLeft[2] = BorderColorStyleSolid; michael@0: michael@0: borderColorStyleBottomRight[0] = BorderColorStyleSolid; michael@0: borderColorStyleBottomRight[1] = BorderColorStyleNone; michael@0: borderColorStyleBottomRight[2] = BorderColorStyleSolid; michael@0: michael@0: borderColorStyleCount = 3; michael@0: break; michael@0: michael@0: case NS_STYLE_BORDER_STYLE_INSET: michael@0: borderColorStyleTopLeft[0] = BorderColorStyleDark; michael@0: borderColorStyleBottomRight[0] = BorderColorStyleLight; michael@0: michael@0: borderColorStyleCount = 1; michael@0: break; michael@0: michael@0: case NS_STYLE_BORDER_STYLE_OUTSET: michael@0: borderColorStyleTopLeft[0] = BorderColorStyleLight; michael@0: borderColorStyleBottomRight[0] = BorderColorStyleDark; michael@0: michael@0: borderColorStyleCount = 1; michael@0: break; michael@0: michael@0: default: michael@0: NS_NOTREACHED("Unhandled border style!!"); michael@0: break; michael@0: } michael@0: michael@0: // The only way to get to here is by having a michael@0: // borderColorStyleCount < 1 or > 3; this should never happen, michael@0: // since -moz-border-colors doesn't get handled here. michael@0: NS_ASSERTION(borderColorStyleCount > 0 && borderColorStyleCount < 4, michael@0: "Non-border-colors case with borderColorStyleCount < 1 or > 3; what happened?"); michael@0: michael@0: // The caller should never give us anything with a mix michael@0: // of TL/BR if the border style would require a michael@0: // TL/BR split. michael@0: if (aSides & (SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT)) michael@0: borderColorStyle = borderColorStyleBottomRight; michael@0: else michael@0: borderColorStyle = borderColorStyleTopLeft; michael@0: michael@0: // Distribute the border across the available space. michael@0: gfxFloat borderWidths[3][4]; michael@0: michael@0: if (borderColorStyleCount == 1) { michael@0: NS_FOR_CSS_SIDES (i) { michael@0: borderWidths[0][i] = mBorderWidths[i]; michael@0: } michael@0: } else if (borderColorStyleCount == 2) { michael@0: // with 2 color styles, any extra pixel goes to the outside michael@0: NS_FOR_CSS_SIDES (i) { michael@0: borderWidths[0][i] = int32_t(mBorderWidths[i]) / 2 + int32_t(mBorderWidths[i]) % 2; michael@0: borderWidths[1][i] = int32_t(mBorderWidths[i]) / 2; michael@0: } michael@0: } else if (borderColorStyleCount == 3) { michael@0: // with 3 color styles, any extra pixel (or lack of extra pixel) michael@0: // goes to the middle michael@0: NS_FOR_CSS_SIDES (i) { michael@0: if (mBorderWidths[i] == 1.0) { michael@0: borderWidths[0][i] = 1.0; michael@0: borderWidths[1][i] = borderWidths[2][i] = 0.0; michael@0: } else { michael@0: int32_t rest = int32_t(mBorderWidths[i]) % 3; michael@0: borderWidths[0][i] = borderWidths[2][i] = borderWidths[1][i] = (int32_t(mBorderWidths[i]) - rest) / 3; michael@0: michael@0: if (rest == 1) { michael@0: borderWidths[1][i] += 1.0; michael@0: } else if (rest == 2) { michael@0: borderWidths[0][i] += 1.0; michael@0: borderWidths[2][i] += 1.0; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // make a copy that we can modify michael@0: gfxCornerSizes radii = mBorderRadii; michael@0: michael@0: gfxRect soRect(mOuterRect); michael@0: gfxRect siRect(mOuterRect); michael@0: michael@0: for (unsigned int i = 0; i < borderColorStyleCount; i++) { michael@0: // walk siRect inwards at the start of the loop to get the michael@0: // correct inner rect. michael@0: siRect.Deflate(gfxMargin(borderWidths[i][0], borderWidths[i][1], michael@0: borderWidths[i][2], borderWidths[i][3])); michael@0: michael@0: if (borderColorStyle[i] != BorderColorStyleNone) { michael@0: gfxRGBA color = ComputeColorForLine(i, michael@0: borderColorStyle, borderColorStyleCount, michael@0: borderRenderColor, mBackgroundColor); michael@0: michael@0: FillSolidBorder(soRect, siRect, radii, borderWidths[i], aSides, color); michael@0: } michael@0: michael@0: ComputeInnerRadii(radii, borderWidths[i], &radii); michael@0: michael@0: // And now soRect is the same as siRect, for the next line in. michael@0: soRect = siRect; michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsCSSBorderRenderer::DrawDashedSide(mozilla::css::Side aSide) michael@0: { michael@0: gfxFloat dashWidth; michael@0: gfxFloat dash[2]; michael@0: michael@0: uint8_t style = mBorderStyles[aSide]; michael@0: gfxFloat borderWidth = mBorderWidths[aSide]; michael@0: nscolor borderColor = mBorderColors[aSide]; michael@0: michael@0: if (borderWidth == 0.0) michael@0: return; michael@0: michael@0: if (style == NS_STYLE_BORDER_STYLE_NONE || michael@0: style == NS_STYLE_BORDER_STYLE_HIDDEN) michael@0: return; michael@0: michael@0: if (style == NS_STYLE_BORDER_STYLE_DASHED) { michael@0: dashWidth = gfxFloat(borderWidth * DOT_LENGTH * DASH_LENGTH); michael@0: michael@0: dash[0] = dashWidth; michael@0: dash[1] = dashWidth; michael@0: michael@0: mContext->SetLineCap(gfxContext::LINE_CAP_BUTT); michael@0: } else if (style == NS_STYLE_BORDER_STYLE_DOTTED) { michael@0: dashWidth = gfxFloat(borderWidth * DOT_LENGTH); michael@0: michael@0: if (borderWidth > 2.0) { michael@0: dash[0] = 0.0; michael@0: dash[1] = dashWidth * 2.0; michael@0: michael@0: mContext->SetLineCap(gfxContext::LINE_CAP_ROUND); michael@0: } else { michael@0: dash[0] = dashWidth; michael@0: dash[1] = dashWidth; michael@0: } michael@0: } else { michael@0: SF("DrawDashedSide: style: %d!!\n", style); michael@0: NS_ERROR("DrawDashedSide called with style other than DASHED or DOTTED; someone's not playing nice"); michael@0: return; michael@0: } michael@0: michael@0: SF("dash: %f %f\n", dash[0], dash[1]); michael@0: michael@0: mContext->SetDash(dash, 2, 0.0); michael@0: michael@0: gfxPoint start = mOuterRect.CCWCorner(aSide); michael@0: gfxPoint end = mOuterRect.CWCorner(aSide); michael@0: michael@0: if (aSide == NS_SIDE_TOP) { michael@0: start.x += mBorderCornerDimensions[C_TL].width; michael@0: end.x -= mBorderCornerDimensions[C_TR].width; michael@0: michael@0: start.y += borderWidth / 2.0; michael@0: end.y += borderWidth / 2.0; michael@0: } else if (aSide == NS_SIDE_RIGHT) { michael@0: start.x -= borderWidth / 2.0; michael@0: end.x -= borderWidth / 2.0; michael@0: michael@0: start.y += mBorderCornerDimensions[C_TR].height; michael@0: end.y -= mBorderCornerDimensions[C_BR].height; michael@0: } else if (aSide == NS_SIDE_BOTTOM) { michael@0: start.x -= mBorderCornerDimensions[C_BR].width; michael@0: end.x += mBorderCornerDimensions[C_BL].width; michael@0: michael@0: start.y -= borderWidth / 2.0; michael@0: end.y -= borderWidth / 2.0; michael@0: } else if (aSide == NS_SIDE_LEFT) { michael@0: start.x += borderWidth / 2.0; michael@0: end.x += borderWidth / 2.0; michael@0: michael@0: start.y -= mBorderCornerDimensions[C_BL].height; michael@0: end.y += mBorderCornerDimensions[C_TL].height; michael@0: } michael@0: michael@0: mContext->NewPath(); michael@0: mContext->MoveTo(start); michael@0: mContext->LineTo(end); michael@0: mContext->SetLineWidth(borderWidth); michael@0: mContext->SetColor(gfxRGBA(borderColor)); michael@0: //mContext->SetColor(gfxRGBA(1.0, 0.0, 0.0, 1.0)); michael@0: mContext->Stroke(); michael@0: } michael@0: michael@0: void michael@0: nsCSSBorderRenderer::SetupStrokeStyle(mozilla::css::Side aSide) michael@0: { michael@0: mContext->SetColor(gfxRGBA(mBorderColors[aSide])); michael@0: mContext->SetLineWidth(mBorderWidths[aSide]); michael@0: } michael@0: michael@0: bool michael@0: nsCSSBorderRenderer::AllBordersSameWidth() michael@0: { michael@0: if (mBorderWidths[0] == mBorderWidths[1] && michael@0: mBorderWidths[0] == mBorderWidths[2] && michael@0: mBorderWidths[0] == mBorderWidths[3]) michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: nsCSSBorderRenderer::AllBordersSolid(bool *aHasCompositeColors) michael@0: { michael@0: *aHasCompositeColors = false; michael@0: NS_FOR_CSS_SIDES(i) { michael@0: if (mCompositeColors[i] != nullptr) { michael@0: *aHasCompositeColors = true; michael@0: } michael@0: if (mBorderStyles[i] == NS_STYLE_BORDER_STYLE_SOLID || michael@0: mBorderStyles[i] == NS_STYLE_BORDER_STYLE_NONE || michael@0: mBorderStyles[i] == NS_STYLE_BORDER_STYLE_HIDDEN) michael@0: { michael@0: continue; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool IsVisible(int aStyle) michael@0: { michael@0: if (aStyle != NS_STYLE_BORDER_STYLE_NONE && michael@0: aStyle != NS_STYLE_BORDER_STYLE_HIDDEN) { michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsCSSBorderRenderer::CreateCornerGradient(mozilla::css::Corner aCorner, michael@0: const gfxRGBA &aFirstColor, michael@0: const gfxRGBA &aSecondColor) michael@0: { michael@0: typedef struct { gfxFloat a, b; } twoFloats; michael@0: michael@0: const twoFloats gradientCoeff[4] = { { -1, +1 }, michael@0: { -1, -1 }, michael@0: { +1, -1 }, michael@0: { +1, +1 } }; michael@0: michael@0: // Sides which form the 'width' and 'height' for the calculation of the angle michael@0: // for our gradient. michael@0: const int cornerWidth[4] = { 3, 1, 1, 3 }; michael@0: const int cornerHeight[4] = { 0, 0, 2, 2 }; michael@0: michael@0: gfxPoint cornerOrigin = mOuterRect.AtCorner(aCorner); michael@0: michael@0: gfxPoint pat1, pat2; michael@0: pat1.x = cornerOrigin.x + michael@0: mBorderWidths[cornerHeight[aCorner]] * gradientCoeff[aCorner].a; michael@0: pat1.y = cornerOrigin.y + michael@0: mBorderWidths[cornerWidth[aCorner]] * gradientCoeff[aCorner].b; michael@0: pat2.x = cornerOrigin.x - michael@0: mBorderWidths[cornerHeight[aCorner]] * gradientCoeff[aCorner].a; michael@0: pat2.y = cornerOrigin.y - michael@0: mBorderWidths[cornerWidth[aCorner]] * gradientCoeff[aCorner].b; michael@0: michael@0: float gradientOffset; michael@0: michael@0: if (mContext->IsCairo() && michael@0: (mContext->OriginalSurface()->GetType() == gfxSurfaceType::D2D || michael@0: mContext->OriginalSurface()->GetType() == gfxSurfaceType::Quartz)) michael@0: { michael@0: // On quarz this doesn't do exactly the right thing, but it does do what michael@0: // most other browsers do and doing the 'right' thing seems to be michael@0: // hard with the quartz cairo backend. michael@0: gradientOffset = 0; michael@0: } else { michael@0: // When cairo/Azure does the gradient drawing this gives us pretty nice behavior! michael@0: gradientOffset = 0.25 / sqrt(pow(mBorderWidths[cornerHeight[aCorner]], 2) + michael@0: pow(mBorderWidths[cornerHeight[aCorner]], 2)); michael@0: } michael@0: michael@0: nsRefPtr pattern = new gfxPattern(pat1.x, pat1.y, pat2.x, pat2.y); michael@0: pattern->AddColorStop(0.5 - gradientOffset, gfxRGBA(aFirstColor)); michael@0: pattern->AddColorStop(0.5 + gradientOffset, gfxRGBA(aSecondColor)); michael@0: michael@0: return pattern.forget(); michael@0: } michael@0: michael@0: TemporaryRef michael@0: nsCSSBorderRenderer::CreateCornerGradient(mozilla::css::Corner aCorner, michael@0: const gfxRGBA &aFirstColor, michael@0: const gfxRGBA &aSecondColor, michael@0: DrawTarget *aDT, michael@0: Point &aPoint1, michael@0: Point &aPoint2) michael@0: { michael@0: typedef struct { gfxFloat a, b; } twoFloats; michael@0: michael@0: const twoFloats gradientCoeff[4] = { { -1, +1 }, michael@0: { -1, -1 }, michael@0: { +1, -1 }, michael@0: { +1, +1 } }; michael@0: michael@0: // Sides which form the 'width' and 'height' for the calculation of the angle michael@0: // for our gradient. michael@0: const int cornerWidth[4] = { 3, 1, 1, 3 }; michael@0: const int cornerHeight[4] = { 0, 0, 2, 2 }; michael@0: michael@0: gfxPoint cornerOrigin = mOuterRect.AtCorner(aCorner); michael@0: michael@0: gfxPoint pat1, pat2; michael@0: pat1.x = cornerOrigin.x + michael@0: mBorderWidths[cornerHeight[aCorner]] * gradientCoeff[aCorner].a; michael@0: pat1.y = cornerOrigin.y + michael@0: mBorderWidths[cornerWidth[aCorner]] * gradientCoeff[aCorner].b; michael@0: pat2.x = cornerOrigin.x - michael@0: mBorderWidths[cornerHeight[aCorner]] * gradientCoeff[aCorner].a; michael@0: pat2.y = cornerOrigin.y - michael@0: mBorderWidths[cornerWidth[aCorner]] * gradientCoeff[aCorner].b; michael@0: michael@0: aPoint1 = Point(pat1.x, pat1.y); michael@0: aPoint2 = Point(pat2.x, pat2.y); michael@0: michael@0: Color firstColor = ToColor(aFirstColor); michael@0: Color secondColor = ToColor(aSecondColor); michael@0: michael@0: nsTArray rawStops(2); michael@0: rawStops.SetLength(2); michael@0: // This is only guaranteed to give correct (and in some cases more correct) michael@0: // rendering with the Direct2D Azure and Quartz Cairo backends. For other michael@0: // cairo backends it could create un-antialiased border corner transitions michael@0: // since that at least used to be pixman's behaviour for hard stops. michael@0: rawStops[0].color = firstColor; michael@0: rawStops[0].offset = 0.5; michael@0: rawStops[1].color = secondColor; michael@0: rawStops[1].offset = 0.5; michael@0: RefPtr gs = michael@0: gfxGradientCache::GetGradientStops(aDT, rawStops, ExtendMode::CLAMP); michael@0: if (!gs) { michael@0: // Having two corners, both with reversed color stops is pretty common michael@0: // for certain border types. Let's optimize it! michael@0: rawStops[0].color = secondColor; michael@0: rawStops[1].color = firstColor; michael@0: Point tmp = aPoint1; michael@0: aPoint1 = aPoint2; michael@0: aPoint2 = tmp; michael@0: gs = gfxGradientCache::GetOrCreateGradientStops(aDT, rawStops, ExtendMode::CLAMP); michael@0: } michael@0: return gs; michael@0: } michael@0: michael@0: typedef struct { gfxFloat a, b; } twoFloats; michael@0: michael@0: void michael@0: nsCSSBorderRenderer::DrawSingleWidthSolidBorder() michael@0: { michael@0: // Easy enough to deal with. michael@0: mContext->SetLineWidth(1); michael@0: gfxRect rect = mOuterRect; michael@0: rect.Deflate(0.5); michael@0: michael@0: const twoFloats cornerAdjusts[4] = { { +0.5, 0 }, michael@0: { 0, +0.5 }, michael@0: { -0.5, 0 }, michael@0: { 0, -0.5 } }; michael@0: michael@0: michael@0: NS_FOR_CSS_SIDES(side) { michael@0: gfxPoint firstCorner = rect.CCWCorner(side); michael@0: firstCorner.x += cornerAdjusts[side].a; michael@0: firstCorner.y += cornerAdjusts[side].b; michael@0: gfxPoint secondCorner = rect.CWCorner(side); michael@0: secondCorner.x += cornerAdjusts[side].a; michael@0: secondCorner.y += cornerAdjusts[side].b; michael@0: michael@0: mContext->SetColor(gfxRGBA(mBorderColors[side])); michael@0: mContext->NewPath(); michael@0: mContext->MoveTo(firstCorner); michael@0: mContext->LineTo(secondCorner); michael@0: mContext->Stroke(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsCSSBorderRenderer::DrawNoCompositeColorSolidBorder() michael@0: { michael@0: const gfxFloat alpha = 0.55191497064665766025; michael@0: michael@0: const twoFloats cornerMults[4] = { { -1, 0 }, michael@0: { 0, -1 }, michael@0: { +1, 0 }, michael@0: { 0, +1 } }; michael@0: michael@0: const twoFloats centerAdjusts[4] = { { 0, +0.5 }, michael@0: { -0.5, 0 }, michael@0: { 0, -0.5 }, michael@0: { +0.5, 0 } }; michael@0: michael@0: gfxPoint pc, pci, p0, p1, p2, p3, pd, p3i; michael@0: michael@0: gfxCornerSizes innerRadii; michael@0: ComputeInnerRadii(mBorderRadii, mBorderWidths, &innerRadii); michael@0: michael@0: gfxRect strokeRect = mOuterRect; michael@0: strokeRect.Deflate(gfxMargin(mBorderWidths[0] / 2.0, mBorderWidths[1] / 2.0, michael@0: mBorderWidths[2] / 2.0, mBorderWidths[3] / 2.0)); michael@0: michael@0: NS_FOR_CSS_CORNERS(i) { michael@0: // the corner index -- either 1 2 3 0 (cw) or 0 3 2 1 (ccw) michael@0: mozilla::css::Corner c = mozilla::css::Corner((i+1) % 4); michael@0: mozilla::css::Corner prevCorner = mozilla::css::Corner(i); michael@0: michael@0: // i+2 and i+3 respectively. These are used to index into the corner michael@0: // multiplier table, and were deduced by calculating out the long form michael@0: // of each corner and finding a pattern in the signs and values. michael@0: int i1 = (i+1) % 4; michael@0: int i2 = (i+2) % 4; michael@0: int i3 = (i+3) % 4; michael@0: michael@0: pc = mOuterRect.AtCorner(c); michael@0: pci = mInnerRect.AtCorner(c); michael@0: mContext->SetLineWidth(mBorderWidths[i]); michael@0: michael@0: nscolor firstColor, secondColor; michael@0: if (IsVisible(mBorderStyles[i]) && IsVisible(mBorderStyles[i1])) { michael@0: firstColor = mBorderColors[i]; michael@0: secondColor = mBorderColors[i1]; michael@0: } else if (IsVisible(mBorderStyles[i])) { michael@0: firstColor = mBorderColors[i]; michael@0: secondColor = mBorderColors[i]; michael@0: } else { michael@0: firstColor = mBorderColors[i1]; michael@0: secondColor = mBorderColors[i1]; michael@0: } michael@0: michael@0: mContext->NewPath(); michael@0: michael@0: gfxPoint strokeStart, strokeEnd; michael@0: michael@0: strokeStart.x = mOuterRect.AtCorner(prevCorner).x + michael@0: mBorderCornerDimensions[prevCorner].width * cornerMults[i2].a; michael@0: strokeStart.y = mOuterRect.AtCorner(prevCorner).y + michael@0: mBorderCornerDimensions[prevCorner].height * cornerMults[i2].b; michael@0: michael@0: strokeEnd.x = pc.x + mBorderCornerDimensions[c].width * cornerMults[i].a; michael@0: strokeEnd.y = pc.y + mBorderCornerDimensions[c].height * cornerMults[i].b; michael@0: michael@0: strokeStart.x += centerAdjusts[i].a * mBorderWidths[i]; michael@0: strokeStart.y += centerAdjusts[i].b * mBorderWidths[i]; michael@0: strokeEnd.x += centerAdjusts[i].a * mBorderWidths[i]; michael@0: strokeEnd.y += centerAdjusts[i].b * mBorderWidths[i]; michael@0: michael@0: mContext->MoveTo(strokeStart); michael@0: mContext->LineTo(strokeEnd); michael@0: mContext->SetColor(gfxRGBA(mBorderColors[i])); michael@0: mContext->Stroke(); michael@0: michael@0: if (firstColor != secondColor) { michael@0: nsRefPtr pattern = michael@0: CreateCornerGradient(c, firstColor, secondColor); michael@0: mContext->SetPattern(pattern); michael@0: } else { michael@0: mContext->SetColor(firstColor); michael@0: } michael@0: michael@0: if (mBorderRadii[c].width > 0 && mBorderRadii[c].height > 0) { michael@0: p0.x = pc.x + cornerMults[i].a * mBorderRadii[c].width; michael@0: p0.y = pc.y + cornerMults[i].b * mBorderRadii[c].height; michael@0: michael@0: p3.x = pc.x + cornerMults[i3].a * mBorderRadii[c].width; michael@0: p3.y = pc.y + cornerMults[i3].b * mBorderRadii[c].height; michael@0: michael@0: p1.x = p0.x + alpha * cornerMults[i2].a * mBorderRadii[c].width; michael@0: p1.y = p0.y + alpha * cornerMults[i2].b * mBorderRadii[c].height; michael@0: michael@0: p2.x = p3.x - alpha * cornerMults[i3].a * mBorderRadii[c].width; michael@0: p2.y = p3.y - alpha * cornerMults[i3].b * mBorderRadii[c].height; michael@0: michael@0: mContext->NewPath(); michael@0: michael@0: gfxPoint cornerStart; michael@0: cornerStart.x = pc.x + cornerMults[i].a * mBorderCornerDimensions[c].width; michael@0: cornerStart.y = pc.y + cornerMults[i].b * mBorderCornerDimensions[c].height; michael@0: michael@0: mContext->MoveTo(cornerStart); michael@0: mContext->LineTo(p0); michael@0: michael@0: mContext->CurveTo(p1, p2, p3); michael@0: michael@0: gfxPoint outerCornerEnd; michael@0: outerCornerEnd.x = pc.x + cornerMults[i3].a * mBorderCornerDimensions[c].width; michael@0: outerCornerEnd.y = pc.y + cornerMults[i3].b * mBorderCornerDimensions[c].height; michael@0: michael@0: mContext->LineTo(outerCornerEnd); michael@0: michael@0: p0.x = pci.x + cornerMults[i].a * innerRadii[c].width; michael@0: p0.y = pci.y + cornerMults[i].b * innerRadii[c].height; michael@0: michael@0: p3i.x = pci.x + cornerMults[i3].a * innerRadii[c].width; michael@0: p3i.y = pci.y + cornerMults[i3].b * innerRadii[c].height; michael@0: michael@0: p1.x = p0.x + alpha * cornerMults[i2].a * innerRadii[c].width; michael@0: p1.y = p0.y + alpha * cornerMults[i2].b * innerRadii[c].height; michael@0: michael@0: p2.x = p3i.x - alpha * cornerMults[i3].a * innerRadii[c].width; michael@0: p2.y = p3i.y - alpha * cornerMults[i3].b * innerRadii[c].height; michael@0: mContext->LineTo(p3i); michael@0: mContext->CurveTo(p2, p1, p0); michael@0: mContext->ClosePath(); michael@0: mContext->Fill(); michael@0: } else { michael@0: gfxPoint c1, c2, c3, c4; michael@0: michael@0: c1.x = pc.x + cornerMults[i].a * mBorderCornerDimensions[c].width; michael@0: c1.y = pc.y + cornerMults[i].b * mBorderCornerDimensions[c].height; michael@0: c2 = pc; michael@0: c3.x = pc.x + cornerMults[i3].a * mBorderCornerDimensions[c].width; michael@0: c3.y = pc.y + cornerMults[i3].b * mBorderCornerDimensions[c].height; michael@0: michael@0: mContext->NewPath(); michael@0: mContext->MoveTo(c1); michael@0: mContext->LineTo(c2); michael@0: mContext->LineTo(c3); michael@0: mContext->LineTo(pci); michael@0: mContext->ClosePath(); michael@0: michael@0: mContext->Fill(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsCSSBorderRenderer::DrawNoCompositeColorSolidBorderAzure() michael@0: { michael@0: DrawTarget *dt = mContext->GetDrawTarget(); michael@0: michael@0: const gfxFloat alpha = 0.55191497064665766025; michael@0: michael@0: const twoFloats cornerMults[4] = { { -1, 0 }, michael@0: { 0, -1 }, michael@0: { +1, 0 }, michael@0: { 0, +1 } }; michael@0: michael@0: const twoFloats centerAdjusts[4] = { { 0, +0.5 }, michael@0: { -0.5, 0 }, michael@0: { 0, -0.5 }, michael@0: { +0.5, 0 } }; michael@0: michael@0: Point pc, pci, p0, p1, p2, p3, pd, p3i; michael@0: michael@0: gfxCornerSizes innerRadii; michael@0: ComputeInnerRadii(mBorderRadii, mBorderWidths, &innerRadii); michael@0: michael@0: gfxRect strokeRect = mOuterRect; michael@0: strokeRect.Deflate(gfxMargin(mBorderWidths[0] / 2.0, mBorderWidths[1] / 2.0, michael@0: mBorderWidths[2] / 2.0, mBorderWidths[3] / 2.0)); michael@0: michael@0: ColorPattern colorPat(Color(0, 0, 0, 0)); michael@0: LinearGradientPattern gradPat(Point(), Point(), nullptr); michael@0: michael@0: NS_FOR_CSS_CORNERS(i) { michael@0: // the corner index -- either 1 2 3 0 (cw) or 0 3 2 1 (ccw) michael@0: mozilla::css::Corner c = mozilla::css::Corner((i+1) % 4); michael@0: mozilla::css::Corner prevCorner = mozilla::css::Corner(i); michael@0: michael@0: // i+2 and i+3 respectively. These are used to index into the corner michael@0: // multiplier table, and were deduced by calculating out the long form michael@0: // of each corner and finding a pattern in the signs and values. michael@0: int i1 = (i+1) % 4; michael@0: int i2 = (i+2) % 4; michael@0: int i3 = (i+3) % 4; michael@0: michael@0: pc = ToPoint(mOuterRect.AtCorner(c)); michael@0: pci = ToPoint(mInnerRect.AtCorner(c)); michael@0: michael@0: nscolor firstColor, secondColor; michael@0: if (IsVisible(mBorderStyles[i]) && IsVisible(mBorderStyles[i1])) { michael@0: firstColor = mBorderColors[i]; michael@0: secondColor = mBorderColors[i1]; michael@0: } else if (IsVisible(mBorderStyles[i])) { michael@0: firstColor = mBorderColors[i]; michael@0: secondColor = mBorderColors[i]; michael@0: } else { michael@0: firstColor = mBorderColors[i1]; michael@0: secondColor = mBorderColors[i1]; michael@0: } michael@0: michael@0: RefPtr builder = dt->CreatePathBuilder(); michael@0: michael@0: Point strokeStart, strokeEnd; michael@0: michael@0: strokeStart.x = mOuterRect.AtCorner(prevCorner).x + michael@0: mBorderCornerDimensions[prevCorner].width * cornerMults[i2].a; michael@0: strokeStart.y = mOuterRect.AtCorner(prevCorner).y + michael@0: mBorderCornerDimensions[prevCorner].height * cornerMults[i2].b; michael@0: michael@0: strokeEnd.x = pc.x + mBorderCornerDimensions[c].width * cornerMults[i].a; michael@0: strokeEnd.y = pc.y + mBorderCornerDimensions[c].height * cornerMults[i].b; michael@0: michael@0: strokeStart.x += centerAdjusts[i].a * mBorderWidths[i]; michael@0: strokeStart.y += centerAdjusts[i].b * mBorderWidths[i]; michael@0: strokeEnd.x += centerAdjusts[i].a * mBorderWidths[i]; michael@0: strokeEnd.y += centerAdjusts[i].b * mBorderWidths[i]; michael@0: michael@0: builder->MoveTo(strokeStart); michael@0: builder->LineTo(strokeEnd); michael@0: RefPtr path = builder->Finish(); michael@0: dt->Stroke(path, ColorPattern(Color::FromABGR(mBorderColors[i])), StrokeOptions(mBorderWidths[i])); michael@0: builder = nullptr; michael@0: path = nullptr; michael@0: michael@0: Pattern *pattern; michael@0: michael@0: if (firstColor != secondColor) { michael@0: gradPat.mStops = CreateCornerGradient(c, firstColor, secondColor, dt, gradPat.mBegin, gradPat.mEnd); michael@0: pattern = &gradPat; michael@0: } else { michael@0: colorPat.mColor = Color::FromABGR(firstColor); michael@0: pattern = &colorPat; michael@0: } michael@0: michael@0: builder = dt->CreatePathBuilder(); michael@0: michael@0: if (mBorderRadii[c].width > 0 && mBorderRadii[c].height > 0) { michael@0: p0.x = pc.x + cornerMults[i].a * mBorderRadii[c].width; michael@0: p0.y = pc.y + cornerMults[i].b * mBorderRadii[c].height; michael@0: michael@0: p3.x = pc.x + cornerMults[i3].a * mBorderRadii[c].width; michael@0: p3.y = pc.y + cornerMults[i3].b * mBorderRadii[c].height; michael@0: michael@0: p1.x = p0.x + alpha * cornerMults[i2].a * mBorderRadii[c].width; michael@0: p1.y = p0.y + alpha * cornerMults[i2].b * mBorderRadii[c].height; michael@0: michael@0: p2.x = p3.x - alpha * cornerMults[i3].a * mBorderRadii[c].width; michael@0: p2.y = p3.y - alpha * cornerMults[i3].b * mBorderRadii[c].height; michael@0: michael@0: Point cornerStart; michael@0: cornerStart.x = pc.x + cornerMults[i].a * mBorderCornerDimensions[c].width; michael@0: cornerStart.y = pc.y + cornerMults[i].b * mBorderCornerDimensions[c].height; michael@0: michael@0: builder->MoveTo(cornerStart); michael@0: builder->LineTo(p0); michael@0: michael@0: builder->BezierTo(p1, p2, p3); michael@0: michael@0: Point outerCornerEnd; michael@0: outerCornerEnd.x = pc.x + cornerMults[i3].a * mBorderCornerDimensions[c].width; michael@0: outerCornerEnd.y = pc.y + cornerMults[i3].b * mBorderCornerDimensions[c].height; michael@0: michael@0: builder->LineTo(outerCornerEnd); michael@0: michael@0: p0.x = pci.x + cornerMults[i].a * innerRadii[c].width; michael@0: p0.y = pci.y + cornerMults[i].b * innerRadii[c].height; michael@0: michael@0: p3i.x = pci.x + cornerMults[i3].a * innerRadii[c].width; michael@0: p3i.y = pci.y + cornerMults[i3].b * innerRadii[c].height; michael@0: michael@0: p1.x = p0.x + alpha * cornerMults[i2].a * innerRadii[c].width; michael@0: p1.y = p0.y + alpha * cornerMults[i2].b * innerRadii[c].height; michael@0: michael@0: p2.x = p3i.x - alpha * cornerMults[i3].a * innerRadii[c].width; michael@0: p2.y = p3i.y - alpha * cornerMults[i3].b * innerRadii[c].height; michael@0: builder->LineTo(p3i); michael@0: builder->BezierTo(p2, p1, p0); michael@0: builder->Close(); michael@0: path = builder->Finish(); michael@0: dt->Fill(path, *pattern); michael@0: } else { michael@0: Point c1, c2, c3, c4; michael@0: michael@0: c1.x = pc.x + cornerMults[i].a * mBorderCornerDimensions[c].width; michael@0: c1.y = pc.y + cornerMults[i].b * mBorderCornerDimensions[c].height; michael@0: c2 = pc; michael@0: c3.x = pc.x + cornerMults[i3].a * mBorderCornerDimensions[c].width; michael@0: c3.y = pc.y + cornerMults[i3].b * mBorderCornerDimensions[c].height; michael@0: michael@0: builder->MoveTo(c1); michael@0: builder->LineTo(c2); michael@0: builder->LineTo(c3); michael@0: builder->LineTo(pci); michael@0: builder->Close(); michael@0: michael@0: path = builder->Finish(); michael@0: michael@0: dt->Fill(path, *pattern); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsCSSBorderRenderer::DrawRectangularCompositeColors() michael@0: { michael@0: nsBorderColors *currentColors[4]; michael@0: mContext->SetLineWidth(1); michael@0: memcpy(currentColors, mCompositeColors, sizeof(nsBorderColors*) * 4); michael@0: gfxRect rect = mOuterRect; michael@0: rect.Deflate(0.5); michael@0: michael@0: const twoFloats cornerAdjusts[4] = { { +0.5, 0 }, michael@0: { 0, +0.5 }, michael@0: { -0.5, 0 }, michael@0: { 0, -0.5 } }; michael@0: michael@0: for (int i = 0; i < mBorderWidths[0]; i++) { michael@0: NS_FOR_CSS_SIDES(side) { michael@0: int sideNext = (side + 1) % 4; michael@0: michael@0: gfxPoint firstCorner = rect.CCWCorner(side); michael@0: firstCorner.x += cornerAdjusts[side].a; michael@0: firstCorner.y += cornerAdjusts[side].b; michael@0: gfxPoint secondCorner = rect.CWCorner(side); michael@0: secondCorner.x -= cornerAdjusts[side].a; michael@0: secondCorner.y -= cornerAdjusts[side].b; michael@0: michael@0: gfxRGBA currentColor = michael@0: currentColors[side] ? gfxRGBA(currentColors[side]->mColor) michael@0: : gfxRGBA(mBorderColors[side]); michael@0: michael@0: mContext->SetColor(currentColor); michael@0: mContext->NewPath(); michael@0: mContext->MoveTo(firstCorner); michael@0: mContext->LineTo(secondCorner); michael@0: mContext->Stroke(); michael@0: michael@0: mContext->NewPath(); michael@0: gfxPoint cornerTopLeft = rect.CWCorner(side); michael@0: cornerTopLeft.x -= 0.5; michael@0: cornerTopLeft.y -= 0.5; michael@0: mContext->Rectangle(gfxRect(cornerTopLeft, gfxSize(1, 1))); michael@0: gfxRGBA nextColor = michael@0: currentColors[sideNext] ? gfxRGBA(currentColors[sideNext]->mColor) michael@0: : gfxRGBA(mBorderColors[sideNext]); michael@0: michael@0: gfxRGBA cornerColor((currentColor.r + nextColor.r) / 2.0, michael@0: (currentColor.g + nextColor.g) / 2.0, michael@0: (currentColor.b + nextColor.b) / 2.0, michael@0: (currentColor.a + nextColor.a) / 2.0); michael@0: mContext->SetColor(cornerColor); michael@0: mContext->Fill(); michael@0: michael@0: if (side != 0) { michael@0: // We'll have to keep side 0 for the color averaging on side 3. michael@0: if (currentColors[side] && currentColors[side]->mNext) { michael@0: currentColors[side] = currentColors[side]->mNext; michael@0: } michael@0: } michael@0: } michael@0: // Now advance the color for side 0. michael@0: if (currentColors[0] && currentColors[0]->mNext) { michael@0: currentColors[0] = currentColors[0]->mNext; michael@0: } michael@0: rect.Deflate(1); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsCSSBorderRenderer::DrawBorders() michael@0: { michael@0: bool forceSeparateCorners = false; michael@0: michael@0: // Examine the border style to figure out if we can draw it in one michael@0: // go or not. michael@0: bool tlBordersSame = AreBorderSideFinalStylesSame(SIDE_BIT_TOP | SIDE_BIT_LEFT); michael@0: bool brBordersSame = AreBorderSideFinalStylesSame(SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT); michael@0: bool allBordersSame = AreBorderSideFinalStylesSame(SIDE_BITS_ALL); michael@0: if (allBordersSame && michael@0: ((mCompositeColors[0] == nullptr && michael@0: (mBorderStyles[0] == NS_STYLE_BORDER_STYLE_NONE || michael@0: mBorderStyles[0] == NS_STYLE_BORDER_STYLE_HIDDEN || michael@0: mBorderColors[0] == NS_RGBA(0,0,0,0))) || michael@0: (mCompositeColors[0] && michael@0: (mCompositeColors[0]->mColor == NS_RGBA(0,0,0,0) && michael@0: !mCompositeColors[0]->mNext)))) michael@0: { michael@0: // All borders are the same style, and the style is either none or hidden, or the color michael@0: // is transparent. michael@0: // This also checks if the first composite color is transparent, and there are michael@0: // no others. It doesn't check if there are subsequent transparent ones, because michael@0: // that would be very silly. michael@0: return; michael@0: } michael@0: michael@0: gfxMatrix mat = mContext->CurrentMatrix(); michael@0: michael@0: // Clamp the CTM to be pixel-aligned; we do this only michael@0: // for translation-only matrices now, but we could do it michael@0: // if the matrix has just a scale as well. We should not michael@0: // do it if there's a rotation. michael@0: if (mat.HasNonTranslation()) { michael@0: if (!mat.HasNonAxisAlignedTransform()) { michael@0: // Scale + transform. Avoid stroke fast-paths so that we have a chance michael@0: // of snapping to pixel boundaries. michael@0: mAvoidStroke = true; michael@0: } michael@0: } else { michael@0: mat.x0 = floor(mat.x0 + 0.5); michael@0: mat.y0 = floor(mat.y0 + 0.5); michael@0: mContext->SetMatrix(mat); michael@0: michael@0: // round mOuterRect and mInnerRect; they're already an integer michael@0: // number of pixels apart and should stay that way after michael@0: // rounding. We don't do this if there's a scale in the current transform michael@0: // since this loses information that might be relevant when we're scaling. michael@0: mOuterRect.Round(); michael@0: mInnerRect.Round(); michael@0: } michael@0: michael@0: bool allBordersSameWidth = AllBordersSameWidth(); michael@0: michael@0: if (allBordersSameWidth && mBorderWidths[0] == 0.0) { michael@0: // Some of the allBordersSameWidth codepaths depend on the border michael@0: // width being greater than zero. michael@0: return; michael@0: } michael@0: michael@0: bool allBordersSolid; michael@0: michael@0: // First there's a couple of 'special cases' that have specifically optimized michael@0: // drawing paths, when none of these can be used we move on to the generalized michael@0: // border drawing code. michael@0: if (allBordersSame && michael@0: mCompositeColors[0] == nullptr && michael@0: allBordersSameWidth && michael@0: mBorderStyles[0] == NS_STYLE_BORDER_STYLE_SOLID && michael@0: mNoBorderRadius && michael@0: !mAvoidStroke) michael@0: { michael@0: // Very simple case. michael@0: SetupStrokeStyle(NS_SIDE_TOP); michael@0: gfxRect rect = mOuterRect; michael@0: rect.Deflate(mBorderWidths[0] / 2.0); michael@0: mContext->NewPath(); michael@0: mContext->Rectangle(rect); michael@0: mContext->Stroke(); michael@0: return; michael@0: } michael@0: michael@0: if (allBordersSame && michael@0: mCompositeColors[0] == nullptr && michael@0: allBordersSameWidth && michael@0: mBorderStyles[0] == NS_STYLE_BORDER_STYLE_DOTTED && michael@0: mBorderWidths[0] < 3 && michael@0: mNoBorderRadius && michael@0: !mAvoidStroke) michael@0: { michael@0: // Very simple case. We draw this rectangular dotted borner without michael@0: // antialiasing. The dots should be pixel aligned. michael@0: SetupStrokeStyle(NS_SIDE_TOP); michael@0: michael@0: gfxFloat dash = mBorderWidths[0]; michael@0: mContext->SetDash(&dash, 1, 0.5); michael@0: mContext->SetAntialiasMode(gfxContext::MODE_ALIASED); michael@0: gfxRect rect = mOuterRect; michael@0: rect.Deflate(mBorderWidths[0] / 2.0); michael@0: mContext->NewPath(); michael@0: mContext->Rectangle(rect); michael@0: mContext->Stroke(); michael@0: return; michael@0: } michael@0: michael@0: michael@0: if (allBordersSame && michael@0: mCompositeColors[0] == nullptr && michael@0: mBorderStyles[0] == NS_STYLE_BORDER_STYLE_SOLID && michael@0: !mAvoidStroke && michael@0: !mNoBorderRadius) michael@0: { michael@0: // Relatively simple case. michael@0: SetupStrokeStyle(NS_SIDE_TOP); michael@0: michael@0: RoundedRect borderInnerRect(mOuterRect, mBorderRadii); michael@0: borderInnerRect.Deflate(mBorderWidths[NS_SIDE_TOP], michael@0: mBorderWidths[NS_SIDE_BOTTOM], michael@0: mBorderWidths[NS_SIDE_LEFT], michael@0: mBorderWidths[NS_SIDE_RIGHT]); michael@0: michael@0: // Instead of stroking we just use two paths: an inner and an outer. michael@0: // This allows us to draw borders that we couldn't when stroking. For example, michael@0: // borders with a border width >= the border radius. (i.e. when there are michael@0: // square corners on the inside) michael@0: // michael@0: // Further, this approach can be more efficient because the backend michael@0: // doesn't need to compute an offset curve to stroke the path. We know that michael@0: // the rounded parts are elipses we can offset exactly and can just compute michael@0: // a new cubic approximation. michael@0: mContext->NewPath(); michael@0: mContext->RoundedRectangle(mOuterRect, mBorderRadii, true); michael@0: mContext->RoundedRectangle(borderInnerRect.rect, borderInnerRect.corners, false); michael@0: mContext->Fill(); michael@0: return; michael@0: } michael@0: michael@0: bool hasCompositeColors; michael@0: michael@0: allBordersSolid = AllBordersSolid(&hasCompositeColors); michael@0: // This leaves the border corners non-interpolated for single width borders. michael@0: // Doing this is slightly faster and shouldn't be a problem visually. michael@0: if (allBordersSolid && michael@0: allBordersSameWidth && michael@0: mCompositeColors[0] == nullptr && michael@0: mBorderWidths[0] == 1 && michael@0: mNoBorderRadius && michael@0: !mAvoidStroke) michael@0: { michael@0: DrawSingleWidthSolidBorder(); michael@0: return; michael@0: } michael@0: michael@0: if (allBordersSolid && !hasCompositeColors && michael@0: !mAvoidStroke) michael@0: { michael@0: if (mContext->IsCairo()) { michael@0: DrawNoCompositeColorSolidBorder(); michael@0: } else { michael@0: DrawNoCompositeColorSolidBorderAzure(); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: if (allBordersSolid && michael@0: allBordersSameWidth && michael@0: mNoBorderRadius && michael@0: !mAvoidStroke) michael@0: { michael@0: // Easy enough to deal with. michael@0: DrawRectangularCompositeColors(); michael@0: return; michael@0: } michael@0: michael@0: // If we have composite colors -and- border radius, michael@0: // then use separate corners so we get OPERATOR_ADD for the corners. michael@0: // Otherwise, we'll get artifacts as we draw stacked 1px-wide curves. michael@0: if (allBordersSame && mCompositeColors[0] != nullptr && !mNoBorderRadius) michael@0: forceSeparateCorners = true; michael@0: michael@0: S(" mOuterRect: "), S(mOuterRect), SN(); michael@0: S(" mInnerRect: "), S(mInnerRect), SN(); michael@0: SF(" mBorderColors: 0x%08x 0x%08x 0x%08x 0x%08x\n", mBorderColors[0], mBorderColors[1], mBorderColors[2], mBorderColors[3]); michael@0: michael@0: // if conditioning the outside rect failed, then bail -- the outside michael@0: // rect is supposed to enclose the entire border michael@0: mOuterRect.Condition(); michael@0: if (mOuterRect.IsEmpty()) michael@0: return; michael@0: michael@0: mInnerRect.Condition(); michael@0: int dashedSides = 0; michael@0: michael@0: NS_FOR_CSS_SIDES(i) { michael@0: uint8_t style = mBorderStyles[i]; michael@0: if (style == NS_STYLE_BORDER_STYLE_DASHED || michael@0: style == NS_STYLE_BORDER_STYLE_DOTTED) michael@0: { michael@0: // pretend that all borders aren't the same; we need to draw michael@0: // things separately for dashed/dotting michael@0: allBordersSame = false; michael@0: dashedSides |= (1 << i); michael@0: } michael@0: } michael@0: michael@0: SF(" allBordersSame: %d dashedSides: 0x%02x\n", allBordersSame, dashedSides); michael@0: michael@0: if (allBordersSame && !forceSeparateCorners) { michael@0: /* Draw everything in one go */ michael@0: DrawBorderSides(SIDE_BITS_ALL); michael@0: SN("---------------- (1)"); michael@0: } else { michael@0: PROFILER_LABEL("nsCSSBorderRenderer", "DrawBorders::multipass"); michael@0: /* We have more than one pass to go. Draw the corners separately from the sides. */ michael@0: michael@0: /* michael@0: * If we have a 1px-wide border, the corners are going to be michael@0: * negligible, so don't bother doing anything fancy. Just extend michael@0: * the top and bottom borders to the right 1px and the left border michael@0: * to the bottom 1px. We do this by twiddling the corner dimensions, michael@0: * which causes the right to happen later on. Only do this if we have michael@0: * a 1.0 unit border all around and no border radius. michael@0: */ michael@0: michael@0: NS_FOR_CSS_CORNERS(corner) { michael@0: const mozilla::css::Side sides[2] = { mozilla::css::Side(corner), PREV_SIDE(corner) }; michael@0: michael@0: if (!IsZeroSize(mBorderRadii[corner])) michael@0: continue; michael@0: michael@0: if (mBorderWidths[sides[0]] == 1.0 && mBorderWidths[sides[1]] == 1.0) { michael@0: if (corner == NS_CORNER_TOP_LEFT || corner == NS_CORNER_TOP_RIGHT) michael@0: mBorderCornerDimensions[corner].width = 0.0; michael@0: else michael@0: mBorderCornerDimensions[corner].height = 0.0; michael@0: } michael@0: } michael@0: michael@0: // First, the corners michael@0: NS_FOR_CSS_CORNERS(corner) { michael@0: // if there's no corner, don't do all this work for it michael@0: if (IsZeroSize(mBorderCornerDimensions[corner])) michael@0: continue; michael@0: michael@0: const int sides[2] = { corner, PREV_SIDE(corner) }; michael@0: int sideBits = (1 << sides[0]) | (1 << sides[1]); michael@0: michael@0: bool simpleCornerStyle = mCompositeColors[sides[0]] == nullptr && michael@0: mCompositeColors[sides[1]] == nullptr && michael@0: AreBorderSideFinalStylesSame(sideBits); michael@0: michael@0: // If we don't have anything complex going on in this corner, michael@0: // then we can just fill the corner with a solid color, and avoid michael@0: // the potentially expensive clip. michael@0: if (simpleCornerStyle && michael@0: IsZeroSize(mBorderRadii[corner]) && michael@0: IsSolidCornerStyle(mBorderStyles[sides[0]], corner)) michael@0: { michael@0: mContext->NewPath(); michael@0: DoCornerSubPath(corner); michael@0: mContext->SetColor(MakeBorderColor(mBorderColors[sides[0]], michael@0: mBackgroundColor, michael@0: BorderColorStyleForSolidCorner(mBorderStyles[sides[0]], corner))); michael@0: mContext->Fill(); michael@0: continue; michael@0: } michael@0: michael@0: mContext->Save(); michael@0: michael@0: // clip to the corner michael@0: mContext->NewPath(); michael@0: DoCornerSubPath(corner); michael@0: mContext->Clip(); michael@0: michael@0: if (simpleCornerStyle) { michael@0: // we don't need a group for this corner, the sides are the same, michael@0: // but we weren't able to render just a solid block for the corner. michael@0: DrawBorderSides(sideBits); michael@0: } else { michael@0: // Sides are different. We could draw using OPERATOR_ADD to michael@0: // get correct color blending behaviour at the seam. We'd need michael@0: // to do it in an offscreen surface to ensure that we're michael@0: // always compositing on transparent black. If the colors michael@0: // don't have transparency and the current destination surface michael@0: // has an alpha channel, we could just clear the region and michael@0: // avoid the temporary, but that situation doesn't happen all michael@0: // that often in practice (we double buffer to no-alpha michael@0: // surfaces). We choose just to seam though, as the performance michael@0: // advantages outway the modest easthetic improvement. michael@0: michael@0: for (int cornerSide = 0; cornerSide < 2; cornerSide++) { michael@0: mozilla::css::Side side = mozilla::css::Side(sides[cornerSide]); michael@0: uint8_t style = mBorderStyles[side]; michael@0: michael@0: SF("corner: %d cornerSide: %d side: %d style: %d\n", corner, cornerSide, side, style); michael@0: michael@0: mContext->Save(); michael@0: michael@0: mContext->NewPath(); michael@0: DoSideClipSubPath(side); michael@0: mContext->Clip(); michael@0: michael@0: DrawBorderSides(1 << side); michael@0: michael@0: mContext->Restore(); michael@0: } michael@0: } michael@0: michael@0: mContext->Restore(); michael@0: michael@0: SN(); michael@0: } michael@0: michael@0: // in the case of a single-unit border, we already munged the michael@0: // corners up above; so we can just draw the top left and bottom michael@0: // right sides separately, if they're the same. michael@0: // michael@0: // We need to check for mNoBorderRadius, because when there is michael@0: // one, FillSolidBorder always draws the full rounded rectangle michael@0: // and expects there to be a clip in place. michael@0: int alreadyDrawnSides = 0; michael@0: if (mOneUnitBorder && michael@0: mNoBorderRadius && michael@0: (dashedSides & (SIDE_BIT_TOP | SIDE_BIT_LEFT)) == 0) michael@0: { michael@0: if (tlBordersSame) { michael@0: DrawBorderSides(SIDE_BIT_TOP | SIDE_BIT_LEFT); michael@0: alreadyDrawnSides |= (SIDE_BIT_TOP | SIDE_BIT_LEFT); michael@0: } michael@0: michael@0: if (brBordersSame && (dashedSides & (SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT)) == 0) { michael@0: DrawBorderSides(SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT); michael@0: alreadyDrawnSides |= (SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT); michael@0: } michael@0: } michael@0: michael@0: // We're done with the corners, now draw the sides. michael@0: NS_FOR_CSS_SIDES (side) { michael@0: // if we drew it above, skip it michael@0: if (alreadyDrawnSides & (1 << side)) michael@0: continue; michael@0: michael@0: // If there's no border on this side, skip it michael@0: if (mBorderWidths[side] == 0.0 || michael@0: mBorderStyles[side] == NS_STYLE_BORDER_STYLE_HIDDEN || michael@0: mBorderStyles[side] == NS_STYLE_BORDER_STYLE_NONE) michael@0: continue; michael@0: michael@0: michael@0: if (dashedSides & (1 << side)) { michael@0: // Dashed sides will always draw just the part ignoring the michael@0: // corners for the side, so no need to clip. michael@0: DrawDashedSide (side); michael@0: michael@0: SN("---------------- (d)"); michael@0: continue; michael@0: } michael@0: michael@0: // Undashed sides will currently draw the entire side, michael@0: // including parts that would normally be covered by a corner, michael@0: // so we need to clip. michael@0: // michael@0: // XXX Optimization -- it would be good to make this work like michael@0: // DrawDashedSide, and have a DrawOneSide function that just michael@0: // draws one side and not the corners, because then we can michael@0: // avoid the potentially expensive clip. michael@0: mContext->Save(); michael@0: mContext->NewPath(); michael@0: DoSideClipWithoutCornersSubPath(side); michael@0: mContext->Clip(); michael@0: michael@0: DrawBorderSides(1 << side); michael@0: michael@0: mContext->Restore(); michael@0: michael@0: SN("---------------- (*)"); michael@0: } michael@0: } michael@0: }