Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / layout / base / nsCSSRenderingBorders.cpp
blob61d15fe4d8163a82a934cf764d47fdae1dcbfc6c
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 // vim:cindent:ts=2:et:sw=2:
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Mozilla Corporation
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Vladimir Vukicevic <vladimir@pobox.com>
25 * Bas Schouten <bschouten@mozilla.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #include "nsStyleConsts.h"
42 #include "nsIFrame.h"
43 #include "nsPoint.h"
44 #include "nsRect.h"
45 #include "nsIViewManager.h"
46 #include "nsFrameManager.h"
47 #include "nsStyleContext.h"
48 #include "nsGkAtoms.h"
49 #include "nsCSSAnonBoxes.h"
50 #include "nsTransform2D.h"
51 #include "nsIDeviceContext.h"
52 #include "nsIContent.h"
53 #include "nsIDocument.h"
54 #include "nsIScrollableFrame.h"
55 #include "imgIRequest.h"
56 #include "imgIContainer.h"
57 #include "nsCSSRendering.h"
58 #include "nsCSSColorUtils.h"
59 #include "nsITheme.h"
60 #include "nsThemeConstants.h"
61 #include "nsIServiceManager.h"
62 #include "nsIHTMLDocument.h"
63 #include "nsLayoutUtils.h"
64 #include "nsINameSpaceManager.h"
65 #include "nsBlockFrame.h"
67 #include "gfxContext.h"
69 #include "nsCSSRenderingBorders.h"
71 /**
72 * nsCSSRendering::PaintBorder
73 * nsCSSRendering::PaintOutline
74 * -> DrawBorders
76 * DrawBorders
77 * -> Ability to use specialized approach?
78 * |- Draw using specialized function
79 * |- separate corners?
80 * |- dashed side mask
81 * |
82 * -> can border be drawn in 1 pass? (e.g., solid border same color all around)
83 * |- DrawBorderSides with all 4 sides
84 * -> more than 1 pass?
85 * |- for each corner
86 * |- clip to DoCornerClipSubPath
87 * |- PushGroup
88 * |- for each side adjacent to corner
89 * |- clip to DoSideClipSubPath
90 * |- DrawBorderSides with one side
91 * |- PopGroup
92 * |- for each side
93 * |- DoSideClipWithoutCornersSubPath
94 * |- DrawDashedSide || DrawBorderSides with one side
97 static void ComputeBorderCornerDimensions(const gfxRect& aOuterRect,
98 const gfxRect& aInnerRect,
99 const gfxCornerSizes& aRadii,
100 gfxCornerSizes *aDimsResult);
102 // given a side index, get the previous and next side index
103 #define NEXT_SIDE(_s) mozilla::css::Side(((_s) + 1) & 3)
104 #define PREV_SIDE(_s) mozilla::css::Side(((_s) + 3) & 3)
106 // from the given base color and the background color, turn
107 // color into a color for the given border pattern style
108 static gfxRGBA MakeBorderColor(const gfxRGBA& aColor,
109 const gfxRGBA& aBackgroundColor,
110 BorderColorStyle aBorderColorStyle);
113 // Given a line index (an index starting from the outside of the
114 // border going inwards) and an array of line styles, calculate the
115 // color that that stripe of the border should be rendered in.
116 static gfxRGBA ComputeColorForLine(PRUint32 aLineIndex,
117 const BorderColorStyle* aBorderColorStyle,
118 PRUint32 aBorderColorStyleCount,
119 nscolor aBorderColor,
120 nscolor aBackgroundColor);
122 static gfxRGBA ComputeCompositeColorForLine(PRUint32 aLineIndex,
123 const nsBorderColors* aBorderColors);
125 // little helper function to check if the array of 4 floats given are
126 // equal to the given value
127 static PRBool
128 CheckFourFloatsEqual(const gfxFloat *vals, gfxFloat k)
130 return (vals[0] == k &&
131 vals[1] == k &&
132 vals[2] == k &&
133 vals[3] == k);
136 static bool
137 IsZeroSize(const gfxSize& sz) {
138 return sz.width == 0.0 || sz.height == 0.0;
141 static bool
142 AllCornersZeroSize(const gfxCornerSizes& corners) {
143 return IsZeroSize(corners[NS_CORNER_TOP_LEFT]) &&
144 IsZeroSize(corners[NS_CORNER_TOP_RIGHT]) &&
145 IsZeroSize(corners[NS_CORNER_BOTTOM_RIGHT]) &&
146 IsZeroSize(corners[NS_CORNER_BOTTOM_LEFT]);
149 typedef enum {
150 // Normal solid square corner. Will be rectangular, the size of the
151 // adjacent sides. If the corner has a border radius, the corner
152 // will always be solid, since we don't do dotted/dashed etc.
153 CORNER_NORMAL,
155 // Paint the corner in whatever style is not dotted/dashed of the
156 // adjacent corners.
157 CORNER_SOLID,
159 // Paint the corner as a dot, the size of the bigger of the adjacent
160 // sides.
161 CORNER_DOT
162 } CornerStyle;
164 nsCSSBorderRenderer::nsCSSBorderRenderer(PRInt32 aAppUnitsPerPixel,
165 gfxContext* aDestContext,
166 gfxRect& aOuterRect,
167 const PRUint8* aBorderStyles,
168 const gfxFloat* aBorderWidths,
169 gfxCornerSizes& aBorderRadii,
170 const nscolor* aBorderColors,
171 nsBorderColors* const* aCompositeColors,
172 PRIntn aSkipSides,
173 nscolor aBackgroundColor)
174 : mContext(aDestContext),
175 mOuterRect(aOuterRect),
176 mBorderStyles(aBorderStyles),
177 mBorderWidths(aBorderWidths),
178 mBorderRadii(aBorderRadii),
179 mBorderColors(aBorderColors),
180 mCompositeColors(aCompositeColors),
181 mAUPP(aAppUnitsPerPixel),
182 mSkipSides(aSkipSides),
183 mBackgroundColor(aBackgroundColor)
185 if (!mCompositeColors) {
186 static nsBorderColors * const noColors[4] = { NULL };
187 mCompositeColors = &noColors[0];
190 mInnerRect = mOuterRect;
191 mInnerRect.Inset(mBorderStyles[0] != NS_STYLE_BORDER_STYLE_NONE ? mBorderWidths[0] : 0,
192 mBorderStyles[1] != NS_STYLE_BORDER_STYLE_NONE ? mBorderWidths[1] : 0,
193 mBorderStyles[2] != NS_STYLE_BORDER_STYLE_NONE ? mBorderWidths[2] : 0,
194 mBorderStyles[3] != NS_STYLE_BORDER_STYLE_NONE ? mBorderWidths[3] : 0);
196 ComputeBorderCornerDimensions(mOuterRect, mInnerRect, mBorderRadii, &mBorderCornerDimensions);
198 mOneUnitBorder = CheckFourFloatsEqual(mBorderWidths, 1.0);
199 mNoBorderRadius = AllCornersZeroSize(mBorderRadii);
202 /* static */ void
203 nsCSSBorderRenderer::ComputeInnerRadii(const gfxCornerSizes& aRadii,
204 const gfxFloat *aBorderSizes,
205 gfxCornerSizes *aInnerRadiiRet)
207 gfxCornerSizes& iRadii = *aInnerRadiiRet;
209 iRadii[C_TL].width = NS_MAX(0.0, aRadii[C_TL].width - aBorderSizes[NS_SIDE_LEFT]);
210 iRadii[C_TL].height = NS_MAX(0.0, aRadii[C_TL].height - aBorderSizes[NS_SIDE_TOP]);
212 iRadii[C_TR].width = NS_MAX(0.0, aRadii[C_TR].width - aBorderSizes[NS_SIDE_RIGHT]);
213 iRadii[C_TR].height = NS_MAX(0.0, aRadii[C_TR].height - aBorderSizes[NS_SIDE_TOP]);
215 iRadii[C_BR].width = NS_MAX(0.0, aRadii[C_BR].width - aBorderSizes[NS_SIDE_RIGHT]);
216 iRadii[C_BR].height = NS_MAX(0.0, aRadii[C_BR].height - aBorderSizes[NS_SIDE_BOTTOM]);
218 iRadii[C_BL].width = NS_MAX(0.0, aRadii[C_BL].width - aBorderSizes[NS_SIDE_LEFT]);
219 iRadii[C_BL].height = NS_MAX(0.0, aRadii[C_BL].height - aBorderSizes[NS_SIDE_BOTTOM]);
222 /*static*/ void
223 ComputeBorderCornerDimensions(const gfxRect& aOuterRect,
224 const gfxRect& aInnerRect,
225 const gfxCornerSizes& aRadii,
226 gfxCornerSizes *aDimsRet)
228 gfxFloat topWidth = aInnerRect.pos.y - aOuterRect.pos.y;
229 gfxFloat leftWidth = aInnerRect.pos.x - aOuterRect.pos.x;
230 gfxFloat rightWidth = aOuterRect.size.width - aInnerRect.size.width - leftWidth;
231 gfxFloat bottomWidth = aOuterRect.size.height - aInnerRect.size.height - topWidth;
233 if (AllCornersZeroSize(aRadii)) {
234 // These will always be in pixel units from CSS
235 (*aDimsRet)[C_TL] = gfxSize(leftWidth, topWidth);
236 (*aDimsRet)[C_TR] = gfxSize(rightWidth, topWidth);
237 (*aDimsRet)[C_BR] = gfxSize(rightWidth, bottomWidth);
238 (*aDimsRet)[C_BL] = gfxSize(leftWidth, bottomWidth);
239 } else {
240 // Always round up to whole pixels for the corners; it's safe to
241 // make the corners bigger than necessary, and this way we ensure
242 // that we avoid seams.
243 (*aDimsRet)[C_TL] = gfxSize(ceil(NS_MAX(leftWidth, aRadii[C_TL].width)),
244 ceil(NS_MAX(topWidth, aRadii[C_TL].height)));
245 (*aDimsRet)[C_TR] = gfxSize(ceil(NS_MAX(rightWidth, aRadii[C_TR].width)),
246 ceil(NS_MAX(topWidth, aRadii[C_TR].height)));
247 (*aDimsRet)[C_BR] = gfxSize(ceil(NS_MAX(rightWidth, aRadii[C_BR].width)),
248 ceil(NS_MAX(bottomWidth, aRadii[C_BR].height)));
249 (*aDimsRet)[C_BL] = gfxSize(ceil(NS_MAX(leftWidth, aRadii[C_BL].width)),
250 ceil(NS_MAX(bottomWidth, aRadii[C_BL].height)));
254 PRBool
255 nsCSSBorderRenderer::AreBorderSideFinalStylesSame(PRUint8 aSides)
257 NS_ASSERTION(aSides != 0 && (aSides & ~SIDE_BITS_ALL) == 0,
258 "AreBorderSidesSame: invalid whichSides!");
260 /* First check if the specified styles and colors are the same for all sides */
261 int firstStyle = 0;
262 NS_FOR_CSS_SIDES (i) {
263 if (firstStyle == i) {
264 if (((1 << i) & aSides) == 0)
265 firstStyle++;
266 continue;
269 if (((1 << i) & aSides) == 0) {
270 continue;
273 if (mBorderStyles[firstStyle] != mBorderStyles[i] ||
274 mBorderColors[firstStyle] != mBorderColors[i] ||
275 !nsBorderColors::Equal(mCompositeColors[firstStyle],
276 mCompositeColors[i]))
277 return PR_FALSE;
280 /* Then if it's one of the two-tone styles and we're not
281 * just comparing the TL or BR sides */
282 switch (mBorderStyles[firstStyle]) {
283 case NS_STYLE_BORDER_STYLE_GROOVE:
284 case NS_STYLE_BORDER_STYLE_RIDGE:
285 case NS_STYLE_BORDER_STYLE_INSET:
286 case NS_STYLE_BORDER_STYLE_OUTSET:
287 return ((aSides & ~(SIDE_BIT_TOP | SIDE_BIT_LEFT)) == 0 ||
288 (aSides & ~(SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT)) == 0);
291 return PR_TRUE;
294 PRBool
295 nsCSSBorderRenderer::IsSolidCornerStyle(PRUint8 aStyle, mozilla::css::Corner aCorner)
297 switch (aStyle) {
298 case NS_STYLE_BORDER_STYLE_DOTTED:
299 case NS_STYLE_BORDER_STYLE_DASHED:
300 case NS_STYLE_BORDER_STYLE_SOLID:
301 return PR_TRUE;
303 case NS_STYLE_BORDER_STYLE_INSET:
304 case NS_STYLE_BORDER_STYLE_OUTSET:
305 return (aCorner == NS_CORNER_TOP_LEFT || aCorner == NS_CORNER_BOTTOM_RIGHT);
307 case NS_STYLE_BORDER_STYLE_GROOVE:
308 case NS_STYLE_BORDER_STYLE_RIDGE:
309 return mOneUnitBorder && (aCorner == NS_CORNER_TOP_LEFT || aCorner == NS_CORNER_BOTTOM_RIGHT);
311 case NS_STYLE_BORDER_STYLE_DOUBLE:
312 return mOneUnitBorder;
314 default:
315 return PR_FALSE;
319 BorderColorStyle
320 nsCSSBorderRenderer::BorderColorStyleForSolidCorner(PRUint8 aStyle, mozilla::css::Corner aCorner)
322 // note that this function assumes that the corner is already solid,
323 // as per the earlier function
324 switch (aStyle) {
325 case NS_STYLE_BORDER_STYLE_DOTTED:
326 case NS_STYLE_BORDER_STYLE_DASHED:
327 case NS_STYLE_BORDER_STYLE_SOLID:
328 case NS_STYLE_BORDER_STYLE_DOUBLE:
329 return BorderColorStyleSolid;
331 case NS_STYLE_BORDER_STYLE_INSET:
332 case NS_STYLE_BORDER_STYLE_GROOVE:
333 if (aCorner == NS_CORNER_TOP_LEFT)
334 return BorderColorStyleDark;
335 else if (aCorner == NS_CORNER_BOTTOM_RIGHT)
336 return BorderColorStyleLight;
337 break;
339 case NS_STYLE_BORDER_STYLE_OUTSET:
340 case NS_STYLE_BORDER_STYLE_RIDGE:
341 if (aCorner == NS_CORNER_TOP_LEFT)
342 return BorderColorStyleLight;
343 else if (aCorner == NS_CORNER_BOTTOM_RIGHT)
344 return BorderColorStyleDark;
345 break;
348 return BorderColorStyleNone;
351 void
352 nsCSSBorderRenderer::DoCornerSubPath(mozilla::css::Corner aCorner)
354 gfxPoint offset(0.0, 0.0);
356 if (aCorner == C_TR || aCorner == C_BR)
357 offset.x = mOuterRect.size.width - mBorderCornerDimensions[aCorner].width;
358 if (aCorner == C_BR || aCorner == C_BL)
359 offset.y = mOuterRect.size.height - mBorderCornerDimensions[aCorner].height;
361 mContext->Rectangle(gfxRect(mOuterRect.pos + offset,
362 mBorderCornerDimensions[aCorner]));
365 void
366 nsCSSBorderRenderer::DoSideClipWithoutCornersSubPath(mozilla::css::Side aSide)
368 gfxPoint offset(0.0, 0.0);
370 // The offset from the outside rect to the start of this side's
371 // box. For the top and bottom sides, the height of the box
372 // must be the border height; the x start must take into account
373 // the corner size (which may be bigger than the right or left
374 // side's width). The same applies to the right and left sides.
375 if (aSide == NS_SIDE_TOP) {
376 offset.x = mBorderCornerDimensions[C_TL].width;
377 } else if (aSide == NS_SIDE_RIGHT) {
378 offset.x = mOuterRect.size.width - mBorderWidths[NS_SIDE_RIGHT];
379 offset.y = mBorderCornerDimensions[C_TR].height;
380 } else if (aSide == NS_SIDE_BOTTOM) {
381 offset.x = mBorderCornerDimensions[C_BL].width;
382 offset.y = mOuterRect.size.height - mBorderWidths[NS_SIDE_BOTTOM];
383 } else if (aSide == NS_SIDE_LEFT) {
384 offset.y = mBorderCornerDimensions[C_TL].height;
387 // The sum of the width & height of the corners adjacent to the
388 // side. This relies on the relationship between side indexing and
389 // corner indexing; that is, 0 == SIDE_TOP and 0 == CORNER_TOP_LEFT,
390 // with both proceeding clockwise.
391 gfxSize sideCornerSum = mBorderCornerDimensions[mozilla::css::Corner(aSide)]
392 + mBorderCornerDimensions[mozilla::css::Corner(NEXT_SIDE(aSide))];
393 gfxRect rect(mOuterRect.pos + offset,
394 mOuterRect.size - sideCornerSum);
396 if (aSide == NS_SIDE_TOP || aSide == NS_SIDE_BOTTOM)
397 rect.size.height = mBorderWidths[aSide];
398 else
399 rect.size.width = mBorderWidths[aSide];
401 mContext->Rectangle(rect);
404 // The side border type and the adjacent border types are
405 // examined and one of the different types of clipping (listed
406 // below) is selected.
408 typedef enum {
409 // clip to the trapezoid formed by the corners of the
410 // inner and outer rectangles for the given side
411 SIDE_CLIP_TRAPEZOID,
413 // clip to the trapezoid formed by the outer rectangle
414 // corners and the center of the region, making sure
415 // that diagonal lines all go directly from the outside
416 // corner to the inside corner, but that they then continue on
417 // to the middle.
419 // This is needed for correctly clipping rounded borders,
420 // which might extend past the SIDE_CLIP_TRAPEZOID trap.
421 SIDE_CLIP_TRAPEZOID_FULL,
423 // clip to the rectangle formed by the given side; a specific
424 // overlap algorithm is used; see the function for details.
425 // this is currently used for dashing.
426 SIDE_CLIP_RECTANGLE
427 } SideClipType;
429 // Given three points, p0, p1, and midPoint, move p1 further in to the
430 // rectangle (of which aMidPoint is the center) so that it reaches the
431 // closer of the horizontal or vertical lines intersecting the midpoint,
432 // while maintaing the slope of the line. If p0 and p1 are the same,
433 // just move p1 to midPoint (since there's no slope to maintain).
434 // FIXME: Extending only to the midpoint isn't actually sufficient for
435 // boxes with asymmetric radii.
436 static void
437 MaybeMoveToMidPoint(gfxPoint& aP0, gfxPoint& aP1, const gfxPoint& aMidPoint)
439 gfxPoint ps = aP1 - aP0;
441 if (ps.x == 0.0) {
442 if (ps.y == 0.0) {
443 aP1 = aMidPoint;
444 } else {
445 aP1.y = aMidPoint.y;
447 } else {
448 if (ps.y == 0.0) {
449 aP1.x = aMidPoint.x;
450 } else {
451 gfxFloat k = NS_MIN((aMidPoint.x - aP0.x) / ps.x,
452 (aMidPoint.y - aP0.y) / ps.y);
453 aP1 = aP0 + ps * k;
458 void
459 nsCSSBorderRenderer::DoSideClipSubPath(mozilla::css::Side aSide)
461 // the clip proceeds clockwise from the top left corner;
462 // so "start" in each case is the start of the region from that side.
464 // the final path will be formed like:
465 // s0 ------- e0
466 // | /
467 // s1 ----- e1
469 // that is, the second point will always be on the inside
471 gfxPoint start[2];
472 gfxPoint end[2];
474 #define IS_DASHED_OR_DOTTED(_s) ((_s) == NS_STYLE_BORDER_STYLE_DASHED || (_s) == NS_STYLE_BORDER_STYLE_DOTTED)
475 PRBool isDashed = IS_DASHED_OR_DOTTED(mBorderStyles[aSide]);
476 PRBool startIsDashed = IS_DASHED_OR_DOTTED(mBorderStyles[PREV_SIDE(aSide)]);
477 PRBool endIsDashed = IS_DASHED_OR_DOTTED(mBorderStyles[NEXT_SIDE(aSide)]);
478 #undef IS_DASHED_OR_DOTTED
480 SideClipType startType = SIDE_CLIP_TRAPEZOID;
481 SideClipType endType = SIDE_CLIP_TRAPEZOID;
483 if (!IsZeroSize(mBorderRadii[mozilla::css::Corner(aSide)]))
484 startType = SIDE_CLIP_TRAPEZOID_FULL;
485 else if (startIsDashed && isDashed)
486 startType = SIDE_CLIP_RECTANGLE;
488 if (!IsZeroSize(mBorderRadii[mozilla::css::Corner(NEXT_SIDE(aSide))]))
489 endType = SIDE_CLIP_TRAPEZOID_FULL;
490 else if (endIsDashed && isDashed)
491 endType = SIDE_CLIP_RECTANGLE;
493 gfxPoint midPoint = mInnerRect.pos + mInnerRect.size / 2.0;
495 start[0] = mOuterRect.CCWCorner(aSide);
496 start[1] = mInnerRect.CCWCorner(aSide);
498 end[0] = mOuterRect.CWCorner(aSide);
499 end[1] = mInnerRect.CWCorner(aSide);
501 if (startType == SIDE_CLIP_TRAPEZOID_FULL) {
502 MaybeMoveToMidPoint(start[0], start[1], midPoint);
503 } else if (startType == SIDE_CLIP_RECTANGLE) {
504 if (aSide == NS_SIDE_TOP || aSide == NS_SIDE_BOTTOM)
505 start[1] = gfxPoint(mOuterRect.CCWCorner(aSide).x, mInnerRect.CCWCorner(aSide).y);
506 else
507 start[1] = gfxPoint(mInnerRect.CCWCorner(aSide).x, mOuterRect.CCWCorner(aSide).y);
510 if (endType == SIDE_CLIP_TRAPEZOID_FULL) {
511 MaybeMoveToMidPoint(end[0], end[1], midPoint);
512 } else if (endType == SIDE_CLIP_RECTANGLE) {
513 if (aSide == NS_SIDE_TOP || aSide == NS_SIDE_BOTTOM)
514 end[0] = gfxPoint(mInnerRect.CWCorner(aSide).x, mOuterRect.CWCorner(aSide).y);
515 else
516 end[0] = gfxPoint(mOuterRect.CWCorner(aSide).x, mInnerRect.CWCorner(aSide).y);
519 mContext->MoveTo(start[0]);
520 mContext->LineTo(end[0]);
521 mContext->LineTo(end[1]);
522 mContext->LineTo(start[1]);
523 mContext->ClosePath();
526 void
527 nsCSSBorderRenderer::FillSolidBorder(const gfxRect& aOuterRect,
528 const gfxRect& aInnerRect,
529 const gfxCornerSizes& aBorderRadii,
530 const gfxFloat *aBorderSizes,
531 PRIntn aSides,
532 const gfxRGBA& aColor)
534 mContext->SetColor(aColor);
535 // Note that this function is allowed to draw more than just the
536 // requested sides.
538 // If we have a border radius, do full rounded rectangles
539 // and fill, regardless of what sides we're asked to draw.
540 if (!AllCornersZeroSize(aBorderRadii)) {
541 gfxCornerSizes innerRadii;
542 ComputeInnerRadii(aBorderRadii, aBorderSizes, &innerRadii);
544 mContext->NewPath();
546 // do the outer border
547 mContext->RoundedRectangle(aOuterRect, aBorderRadii, PR_TRUE);
549 // then do the inner border CCW
550 mContext->RoundedRectangle(aInnerRect, innerRadii, PR_FALSE);
552 mContext->Fill();
554 return;
557 // If we're asked to draw all sides of an equal-sized border,
558 // stroking is fastest. This is a fairly common path, but partial
559 // sides is probably second in the list -- there are a bunch of
560 // common border styles, such as inset and outset, that are
561 // top-left/bottom-right split.
562 if (aSides == SIDE_BITS_ALL &&
563 CheckFourFloatsEqual(aBorderSizes, aBorderSizes[0]))
565 gfxRect r(aOuterRect);
566 r.Inset(aBorderSizes[0] / 2.0);
567 mContext->SetLineWidth(aBorderSizes[0]);
569 mContext->NewPath();
570 mContext->Rectangle(r);
571 mContext->Stroke();
573 return;
576 // Otherwise, we have unequal sized borders or we're only
577 // drawing some sides; create rectangles for each side
578 // and fill them.
580 gfxRect r[4];
582 // compute base rects for each side
583 if (aSides & SIDE_BIT_TOP) {
584 r[NS_SIDE_TOP].pos = aOuterRect.TopLeft();
585 r[NS_SIDE_TOP].size.width = aOuterRect.size.width;
586 r[NS_SIDE_TOP].size.height = aBorderSizes[NS_SIDE_TOP];
589 if (aSides & SIDE_BIT_BOTTOM) {
590 r[NS_SIDE_BOTTOM].pos = aOuterRect.BottomLeft();
591 r[NS_SIDE_BOTTOM].pos.y -= aBorderSizes[NS_SIDE_BOTTOM];
592 r[NS_SIDE_BOTTOM].size.width = aOuterRect.size.width;
593 r[NS_SIDE_BOTTOM].size.height = aBorderSizes[NS_SIDE_BOTTOM];
596 if (aSides & SIDE_BIT_LEFT) {
597 r[NS_SIDE_LEFT].pos = aOuterRect.TopLeft();
598 r[NS_SIDE_LEFT].size.width = aBorderSizes[NS_SIDE_LEFT];
599 r[NS_SIDE_LEFT].size.height = aOuterRect.size.height;
602 if (aSides & SIDE_BIT_RIGHT) {
603 r[NS_SIDE_RIGHT].pos = aOuterRect.TopRight();
604 r[NS_SIDE_RIGHT].pos.x -= aBorderSizes[NS_SIDE_RIGHT];
605 r[NS_SIDE_RIGHT].size.width = aBorderSizes[NS_SIDE_RIGHT];
606 r[NS_SIDE_RIGHT].size.height = aOuterRect.size.height;
609 // If two sides meet at a corner that we're rendering, then
610 // make sure that we adjust one of the sides to avoid overlap.
611 // This is especially important in the case of colors with
612 // an alpha channel.
614 if ((aSides & (SIDE_BIT_TOP | SIDE_BIT_LEFT)) == (SIDE_BIT_TOP | SIDE_BIT_LEFT)) {
615 // adjust the left's top down a bit
616 r[NS_SIDE_LEFT].pos.y += aBorderSizes[NS_SIDE_TOP];
617 r[NS_SIDE_LEFT].size.height -= aBorderSizes[NS_SIDE_TOP];
620 if ((aSides & (SIDE_BIT_TOP | SIDE_BIT_RIGHT)) == (SIDE_BIT_TOP | SIDE_BIT_RIGHT)) {
621 // adjust the top's left a bit
622 r[NS_SIDE_TOP].size.width -= aBorderSizes[NS_SIDE_RIGHT];
625 if ((aSides & (SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT)) == (SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT)) {
626 // adjust the right's bottom a bit
627 r[NS_SIDE_RIGHT].size.height -= aBorderSizes[NS_SIDE_BOTTOM];
630 if ((aSides & (SIDE_BIT_BOTTOM | SIDE_BIT_LEFT)) == (SIDE_BIT_BOTTOM | SIDE_BIT_LEFT)) {
631 // adjust the bottom's left a bit
632 r[NS_SIDE_BOTTOM].pos.x += aBorderSizes[NS_SIDE_LEFT];
633 r[NS_SIDE_BOTTOM].size.width -= aBorderSizes[NS_SIDE_LEFT];
636 // Filling these one by one is faster than filling them all at once.
637 for (PRUint32 i = 0; i < 4; i++) {
638 if (aSides & (1 << i)) {
639 mContext->NewPath();
640 mContext->Rectangle(r[i]);
641 mContext->Fill();
646 gfxRGBA
647 MakeBorderColor(const gfxRGBA& aColor, const gfxRGBA& aBackgroundColor, BorderColorStyle aBorderColorStyle)
649 nscolor colors[2];
650 int k = 0;
652 switch (aBorderColorStyle) {
653 case BorderColorStyleNone:
654 return gfxRGBA(0.0, 0.0, 0.0, 0.0);
656 case BorderColorStyleLight:
657 k = 1;
658 /* fall through */
659 case BorderColorStyleDark:
660 NS_GetSpecial3DColors(colors, aBackgroundColor.Packed(), aColor.Packed());
661 return gfxRGBA(colors[k]);
663 case BorderColorStyleSolid:
664 default:
665 return aColor;
669 gfxRGBA
670 ComputeColorForLine(PRUint32 aLineIndex,
671 const BorderColorStyle* aBorderColorStyle,
672 PRUint32 aBorderColorStyleCount,
673 nscolor aBorderColor,
674 nscolor aBackgroundColor)
676 NS_ASSERTION(aLineIndex < aBorderColorStyleCount, "Invalid lineIndex given");
678 return MakeBorderColor(gfxRGBA(aBorderColor), gfxRGBA(aBackgroundColor), aBorderColorStyle[aLineIndex]);
681 gfxRGBA
682 ComputeCompositeColorForLine(PRUint32 aLineIndex,
683 const nsBorderColors* aBorderColors)
685 while (aLineIndex-- && aBorderColors->mNext)
686 aBorderColors = aBorderColors->mNext;
688 return gfxRGBA(aBorderColors->mColor);
691 void
692 nsCSSBorderRenderer::DrawBorderSidesCompositeColors(PRIntn aSides, const nsBorderColors *aCompositeColors)
694 gfxCornerSizes radii = mBorderRadii;
696 // the generic composite colors path; each border is 1px in size
697 gfxRect soRect = mOuterRect;
698 gfxRect siRect;
699 gfxFloat maxBorderWidth = 0;
700 NS_FOR_CSS_SIDES (i) {
701 maxBorderWidth = NS_MAX(maxBorderWidth, mBorderWidths[i]);
704 gfxFloat fakeBorderSizes[4];
706 gfxRGBA lineColor;
707 gfxPoint tl, br;
709 gfxPoint itl = mInnerRect.TopLeft();
710 gfxPoint ibr = mInnerRect.BottomRight();
712 for (PRUint32 i = 0; i < PRUint32(maxBorderWidth); i++) {
713 lineColor = ComputeCompositeColorForLine(i, aCompositeColors);
715 siRect = soRect;
716 siRect.Inset(1.0, 1.0, 1.0, 1.0);
718 // now cap the rects to the real mInnerRect
719 tl = siRect.TopLeft();
720 br = siRect.BottomRight();
722 tl.x = NS_MIN(tl.x, itl.x);
723 tl.y = NS_MIN(tl.y, itl.y);
725 br.x = NS_MAX(br.x, ibr.x);
726 br.y = NS_MAX(br.y, ibr.y);
728 siRect.pos = tl;
729 siRect.size.width = br.x - tl.x;
730 siRect.size.height = br.y - tl.y;
732 fakeBorderSizes[NS_SIDE_TOP] = siRect.TopLeft().y - soRect.TopLeft().y;
733 fakeBorderSizes[NS_SIDE_RIGHT] = soRect.TopRight().x - siRect.TopRight().x;
734 fakeBorderSizes[NS_SIDE_BOTTOM] = soRect.BottomRight().y - siRect.BottomRight().y;
735 fakeBorderSizes[NS_SIDE_LEFT] = siRect.BottomLeft().x - soRect.BottomLeft().x;
737 FillSolidBorder(soRect, siRect, radii, fakeBorderSizes, aSides, lineColor);
739 soRect = siRect;
741 ComputeInnerRadii(radii, fakeBorderSizes, &radii);
745 void
746 nsCSSBorderRenderer::DrawBorderSides(PRIntn aSides)
748 if (aSides == 0 || (aSides & ~SIDE_BITS_ALL) != 0) {
749 NS_WARNING("DrawBorderSides: invalid sides!");
750 return;
753 PRUint8 borderRenderStyle;
754 nscolor borderRenderColor;
755 const nsBorderColors *compositeColors = nsnull;
757 PRUint32 borderColorStyleCount = 0;
758 BorderColorStyle borderColorStyleTopLeft[3], borderColorStyleBottomRight[3];
759 BorderColorStyle *borderColorStyle = nsnull;
761 NS_FOR_CSS_SIDES (i) {
762 if ((aSides & (1 << i)) == 0)
763 continue;
764 borderRenderStyle = mBorderStyles[i];
765 borderRenderColor = mBorderColors[i];
766 compositeColors = mCompositeColors[i];
767 break;
770 if (borderRenderStyle == NS_STYLE_BORDER_STYLE_NONE ||
771 borderRenderStyle == NS_STYLE_BORDER_STYLE_HIDDEN)
772 return;
774 // -moz-border-colors is a hack; if we have it for a border, then
775 // it's always drawn solid, and each color is given 1px. The last
776 // color is used for the remainder of the border's size. Just
777 // hand off to another function to do all that.
778 if (compositeColors) {
779 DrawBorderSidesCompositeColors(aSides, compositeColors);
780 return;
783 // We're not doing compositeColors, so we can calculate the
784 // borderColorStyle based on the specified style. The
785 // borderColorStyle array goes from the outer to the inner style.
787 // If the border width is 1, we need to change the borderRenderStyle
788 // a bit to make sure that we get the right colors -- e.g. 'ridge'
789 // with a 1px border needs to look like solid, not like 'outset'.
790 if (mOneUnitBorder &&
791 (borderRenderStyle == NS_STYLE_BORDER_STYLE_RIDGE ||
792 borderRenderStyle == NS_STYLE_BORDER_STYLE_GROOVE ||
793 borderRenderStyle == NS_STYLE_BORDER_STYLE_DOUBLE))
794 borderRenderStyle = NS_STYLE_BORDER_STYLE_SOLID;
796 switch (borderRenderStyle) {
797 case NS_STYLE_BORDER_STYLE_SOLID:
798 case NS_STYLE_BORDER_STYLE_DASHED:
799 case NS_STYLE_BORDER_STYLE_DOTTED:
800 borderColorStyleTopLeft[0] = BorderColorStyleSolid;
802 borderColorStyleBottomRight[0] = BorderColorStyleSolid;
804 borderColorStyleCount = 1;
805 break;
807 case NS_STYLE_BORDER_STYLE_GROOVE:
808 borderColorStyleTopLeft[0] = BorderColorStyleDark;
809 borderColorStyleTopLeft[1] = BorderColorStyleLight;
811 borderColorStyleBottomRight[0] = BorderColorStyleLight;
812 borderColorStyleBottomRight[1] = BorderColorStyleDark;
814 borderColorStyleCount = 2;
815 break;
817 case NS_STYLE_BORDER_STYLE_RIDGE:
818 borderColorStyleTopLeft[0] = BorderColorStyleLight;
819 borderColorStyleTopLeft[1] = BorderColorStyleDark;
821 borderColorStyleBottomRight[0] = BorderColorStyleDark;
822 borderColorStyleBottomRight[1] = BorderColorStyleLight;
824 borderColorStyleCount = 2;
825 break;
827 case NS_STYLE_BORDER_STYLE_DOUBLE:
828 borderColorStyleTopLeft[0] = BorderColorStyleSolid;
829 borderColorStyleTopLeft[1] = BorderColorStyleNone;
830 borderColorStyleTopLeft[2] = BorderColorStyleSolid;
832 borderColorStyleBottomRight[0] = BorderColorStyleSolid;
833 borderColorStyleBottomRight[1] = BorderColorStyleNone;
834 borderColorStyleBottomRight[2] = BorderColorStyleSolid;
836 borderColorStyleCount = 3;
837 break;
839 case NS_STYLE_BORDER_STYLE_INSET:
840 borderColorStyleTopLeft[0] = BorderColorStyleDark;
841 borderColorStyleBottomRight[0] = BorderColorStyleLight;
843 borderColorStyleCount = 1;
844 break;
846 case NS_STYLE_BORDER_STYLE_OUTSET:
847 borderColorStyleTopLeft[0] = BorderColorStyleLight;
848 borderColorStyleBottomRight[0] = BorderColorStyleDark;
850 borderColorStyleCount = 1;
851 break;
853 default:
854 NS_NOTREACHED("Unhandled border style!!");
855 break;
858 // The only way to get to here is by having a
859 // borderColorStyleCount < 1 or > 3; this should never happen,
860 // since -moz-border-colors doesn't get handled here.
861 NS_ASSERTION(borderColorStyleCount > 0 && borderColorStyleCount < 4,
862 "Non-border-colors case with borderColorStyleCount < 1 or > 3; what happened?");
864 // The caller should never give us anything with a mix
865 // of TL/BR if the border style would require a
866 // TL/BR split.
867 if (aSides & (SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT))
868 borderColorStyle = borderColorStyleBottomRight;
869 else
870 borderColorStyle = borderColorStyleTopLeft;
872 // Distribute the border across the available space.
873 gfxFloat borderWidths[3][4];
875 if (borderColorStyleCount == 1) {
876 NS_FOR_CSS_SIDES (i) {
877 borderWidths[0][i] = mBorderWidths[i];
879 } else if (borderColorStyleCount == 2) {
880 // with 2 color styles, any extra pixel goes to the outside
881 NS_FOR_CSS_SIDES (i) {
882 borderWidths[0][i] = PRInt32(mBorderWidths[i]) / 2 + PRInt32(mBorderWidths[i]) % 2;
883 borderWidths[1][i] = PRInt32(mBorderWidths[i]) / 2;
885 } else if (borderColorStyleCount == 3) {
886 // with 3 color styles, any extra pixel (or lack of extra pixel)
887 // goes to the middle
888 NS_FOR_CSS_SIDES (i) {
889 if (mBorderWidths[i] == 1.0) {
890 borderWidths[0][i] = 1.0;
891 borderWidths[1][i] = borderWidths[2][i] = 0.0;
892 } else {
893 PRInt32 rest = PRInt32(mBorderWidths[i]) % 3;
894 borderWidths[0][i] = borderWidths[2][i] = borderWidths[1][i] = (PRInt32(mBorderWidths[i]) - rest) / 3;
896 if (rest == 1) {
897 borderWidths[1][i] += 1.0;
898 } else if (rest == 2) {
899 borderWidths[0][i] += 1.0;
900 borderWidths[2][i] += 1.0;
906 // make a copy that we can modify
907 gfxCornerSizes radii = mBorderRadii;
909 gfxRect soRect(mOuterRect);
910 gfxRect siRect(mOuterRect);
912 for (unsigned int i = 0; i < borderColorStyleCount; i++) {
913 // walk siRect inwards at the start of the loop to get the
914 // correct inner rect.
915 siRect.Inset(borderWidths[i]);
917 if (borderColorStyle[i] != BorderColorStyleNone) {
918 gfxRGBA color = ComputeColorForLine(i,
919 borderColorStyle, borderColorStyleCount,
920 borderRenderColor, mBackgroundColor);
922 FillSolidBorder(soRect, siRect, radii, borderWidths[i], aSides, color);
925 ComputeInnerRadii(radii, borderWidths[i], &radii);
927 // And now soRect is the same as siRect, for the next line in.
928 soRect = siRect;
932 void
933 nsCSSBorderRenderer::DrawDashedSide(mozilla::css::Side aSide)
935 gfxFloat dashWidth;
936 gfxFloat dash[2];
938 PRUint8 style = mBorderStyles[aSide];
939 gfxFloat borderWidth = mBorderWidths[aSide];
940 nscolor borderColor = mBorderColors[aSide];
942 if (borderWidth == 0.0)
943 return;
945 if (style == NS_STYLE_BORDER_STYLE_NONE ||
946 style == NS_STYLE_BORDER_STYLE_HIDDEN)
947 return;
949 if (style == NS_STYLE_BORDER_STYLE_DASHED) {
950 dashWidth = gfxFloat(borderWidth * DOT_LENGTH * DASH_LENGTH);
952 dash[0] = dashWidth;
953 dash[1] = dashWidth;
955 mContext->SetLineCap(gfxContext::LINE_CAP_BUTT);
956 } else if (style == NS_STYLE_BORDER_STYLE_DOTTED) {
957 dashWidth = gfxFloat(borderWidth * DOT_LENGTH);
959 if (borderWidth > 2.0) {
960 dash[0] = 0.0;
961 dash[1] = dashWidth * 2.0;
963 mContext->SetLineCap(gfxContext::LINE_CAP_ROUND);
964 } else {
965 dash[0] = dashWidth;
966 dash[1] = dashWidth;
968 } else {
969 SF("DrawDashedSide: style: %d!!\n", style);
970 NS_ERROR("DrawDashedSide called with style other than DASHED or DOTTED; someone's not playing nice");
971 return;
974 SF("dash: %f %f\n", dash[0], dash[1]);
976 mContext->SetDash(dash, 2, 0.0);
978 gfxPoint start = mOuterRect.CCWCorner(aSide);
979 gfxPoint end = mOuterRect.CWCorner(aSide);
981 if (aSide == NS_SIDE_TOP) {
982 start.x += mBorderCornerDimensions[C_TL].width;
983 end.x -= mBorderCornerDimensions[C_TR].width;
985 start.y += borderWidth / 2.0;
986 end.y += borderWidth / 2.0;
987 } else if (aSide == NS_SIDE_RIGHT) {
988 start.x -= borderWidth / 2.0;
989 end.x -= borderWidth / 2.0;
991 start.y += mBorderCornerDimensions[C_TR].height;
992 end.y -= mBorderCornerDimensions[C_BR].height;
993 } else if (aSide == NS_SIDE_BOTTOM) {
994 start.x -= mBorderCornerDimensions[C_BR].width;
995 end.x += mBorderCornerDimensions[C_BL].width;
997 start.y -= borderWidth / 2.0;
998 end.y -= borderWidth / 2.0;
999 } else if (aSide == NS_SIDE_LEFT) {
1000 start.x += borderWidth / 2.0;
1001 end.x += borderWidth / 2.0;
1003 start.y -= mBorderCornerDimensions[C_BL].height;
1004 end.y += mBorderCornerDimensions[C_TL].height;
1007 mContext->NewPath();
1008 mContext->MoveTo(start);
1009 mContext->LineTo(end);
1010 mContext->SetLineWidth(borderWidth);
1011 mContext->SetColor(gfxRGBA(borderColor));
1012 //mContext->SetColor(gfxRGBA(1.0, 0.0, 0.0, 1.0));
1013 mContext->Stroke();
1016 void
1017 nsCSSBorderRenderer::SetupStrokeStyle(mozilla::css::Side aSide)
1019 mContext->SetColor(gfxRGBA(mBorderColors[aSide]));
1020 mContext->SetLineWidth(mBorderWidths[aSide]);
1023 bool
1024 nsCSSBorderRenderer::AllBordersSameWidth()
1026 if (mBorderWidths[0] == mBorderWidths[1] &&
1027 mBorderWidths[0] == mBorderWidths[2] &&
1028 mBorderWidths[0] == mBorderWidths[3])
1030 return true;
1033 return false;
1036 bool
1037 nsCSSBorderRenderer::AllBordersSolid(bool *aHasCompositeColors)
1039 *aHasCompositeColors = false;
1040 NS_FOR_CSS_SIDES(i) {
1041 if (mCompositeColors[i] != nsnull) {
1042 *aHasCompositeColors = true;
1044 if (mBorderStyles[i] == NS_STYLE_BORDER_STYLE_SOLID ||
1045 mBorderStyles[i] == NS_STYLE_BORDER_STYLE_NONE ||
1046 mBorderStyles[i] == NS_STYLE_BORDER_STYLE_HIDDEN)
1048 continue;
1050 return false;
1053 return true;
1056 bool IsVisible(int aStyle)
1058 if (aStyle != NS_STYLE_BORDER_STYLE_NONE &&
1059 aStyle != NS_STYLE_BORDER_STYLE_HIDDEN) {
1060 return true;
1062 return false;
1065 already_AddRefed<gfxPattern>
1066 nsCSSBorderRenderer::CreateCornerGradient(mozilla::css::Corner aCorner,
1067 const gfxRGBA &aFirstColor,
1068 const gfxRGBA &aSecondColor)
1070 typedef struct { gfxFloat a, b; } twoFloats;
1072 const twoFloats gradientCoeff[4] = { { -1, +1 },
1073 { -1, -1 },
1074 { +1, -1 },
1075 { +1, +1 } };
1077 // Sides which form the 'width' and 'height' for the calculation of the angle
1078 // for our gradient.
1079 const int cornerWidth[4] = { 3, 1, 1, 3 };
1080 const int cornerHeight[4] = { 0, 0, 2, 2 };
1082 gfxPoint cornerOrigin = mOuterRect.AtCorner(aCorner);
1084 gfxPoint pat1, pat2;
1085 pat1.x = cornerOrigin.x +
1086 mBorderWidths[cornerHeight[aCorner]] * gradientCoeff[aCorner].a;
1087 pat1.y = cornerOrigin.y +
1088 mBorderWidths[cornerWidth[aCorner]] * gradientCoeff[aCorner].b;
1089 pat2.x = cornerOrigin.x -
1090 mBorderWidths[cornerHeight[aCorner]] * gradientCoeff[aCorner].a;
1091 pat2.y = cornerOrigin.y -
1092 mBorderWidths[cornerWidth[aCorner]] * gradientCoeff[aCorner].b;
1094 float gradientOffset;
1096 if (mContext->OriginalSurface()->GetType() == gfxASurface::SurfaceTypeD2D ||
1097 mContext->OriginalSurface()->GetType() == gfxASurface::SurfaceTypeQuartz)
1099 // On quarz this doesn't do exactly the right thing, but it does do what
1100 // most other browsers do and doing the 'right' thing seems to be
1101 // hard with the quartz cairo backend.
1102 gradientOffset = 0;
1103 } else {
1104 // When cairo does the gradient drawing this gives us pretty nice behavior!
1105 gradientOffset = 0.25 / sqrt(pow(mBorderWidths[cornerHeight[aCorner]], 2) +
1106 pow(mBorderWidths[cornerHeight[aCorner]], 2));
1109 nsRefPtr<gfxPattern> pattern = new gfxPattern(pat1.x, pat1.y, pat2.x, pat2.y);
1110 pattern->AddColorStop(0.5 - gradientOffset, gfxRGBA(aFirstColor));
1111 pattern->AddColorStop(0.5 + gradientOffset, gfxRGBA(aSecondColor));
1113 return pattern.forget();
1116 typedef struct { gfxFloat a, b; } twoFloats;
1118 void
1119 nsCSSBorderRenderer::DrawSingleWidthSolidBorder()
1121 // Easy enough to deal with.
1122 mContext->SetLineWidth(1);
1123 gfxRect rect = mOuterRect;
1124 rect.Inset(0.5);
1126 const twoFloats cornerAdjusts[4] = { { +0.5, 0 },
1127 { 0, +0.5 },
1128 { -0.5, 0 },
1129 { 0, -0.5 } };
1132 NS_FOR_CSS_SIDES(side) {
1133 gfxPoint firstCorner = rect.CCWCorner(side);
1134 firstCorner.x += cornerAdjusts[side].a;
1135 firstCorner.y += cornerAdjusts[side].b;
1136 gfxPoint secondCorner = rect.CWCorner(side);
1137 secondCorner.x += cornerAdjusts[side].a;
1138 secondCorner.y += cornerAdjusts[side].b;
1140 mContext->SetColor(gfxRGBA(mBorderColors[side]));
1141 mContext->NewPath();
1142 mContext->MoveTo(firstCorner);
1143 mContext->LineTo(secondCorner);
1144 mContext->Stroke();
1148 void
1149 nsCSSBorderRenderer::DrawNoCompositeColorSolidBorder()
1151 const gfxFloat alpha = 0.55191497064665766025;
1153 const twoFloats cornerMults[4] = { { -1, 0 },
1154 { 0, -1 },
1155 { +1, 0 },
1156 { 0, +1 } };
1158 const twoFloats centerAdjusts[4] = { { 0, +0.5 },
1159 { -0.5, 0 },
1160 { 0, -0.5 },
1161 { +0.5, 0 } };
1163 gfxPoint pc, pci, p0, p1, p2, p3, pd, p3i;
1165 gfxCornerSizes innerRadii;
1166 ComputeInnerRadii(mBorderRadii, mBorderWidths, &innerRadii);
1168 gfxRect strokeRect = mOuterRect;
1169 strokeRect.Inset(mBorderWidths[0] / 2.0, mBorderWidths[1] / 2.0,
1170 mBorderWidths[2] / 2.0, mBorderWidths[3] / 2.0);
1172 NS_FOR_CSS_CORNERS(i) {
1173 // the corner index -- either 1 2 3 0 (cw) or 0 3 2 1 (ccw)
1174 mozilla::css::Corner c = mozilla::css::Corner((i+1) % 4);
1175 mozilla::css::Corner prevCorner = mozilla::css::Corner(i);
1177 // i+2 and i+3 respectively. These are used to index into the corner
1178 // multiplier table, and were deduced by calculating out the long form
1179 // of each corner and finding a pattern in the signs and values.
1180 int i1 = (i+1) % 4;
1181 int i2 = (i+2) % 4;
1182 int i3 = (i+3) % 4;
1184 pc = mOuterRect.AtCorner(c);
1185 pci = mInnerRect.AtCorner(c);
1186 mContext->SetLineWidth(mBorderWidths[i]);
1188 nscolor firstColor, secondColor;
1189 if (IsVisible(mBorderStyles[i]) && IsVisible(mBorderStyles[i1])) {
1190 firstColor = mBorderColors[i];
1191 secondColor = mBorderColors[i1];
1192 } else if (IsVisible(mBorderStyles[i])) {
1193 firstColor = mBorderColors[i];
1194 secondColor = mBorderColors[i];
1195 } else {
1196 firstColor = mBorderColors[i1];
1197 secondColor = mBorderColors[i1];
1200 mContext->NewPath();
1202 gfxPoint strokeStart, strokeEnd;
1204 strokeStart.x = mOuterRect.AtCorner(prevCorner).x +
1205 mBorderCornerDimensions[prevCorner].width * cornerMults[i2].a;
1206 strokeStart.y = mOuterRect.AtCorner(prevCorner).y +
1207 mBorderCornerDimensions[prevCorner].height * cornerMults[i2].b;
1209 strokeEnd.x = pc.x + mBorderCornerDimensions[c].width * cornerMults[i].a;
1210 strokeEnd.y = pc.y + mBorderCornerDimensions[c].height * cornerMults[i].b;
1212 strokeStart.x += centerAdjusts[i].a * mBorderWidths[i];
1213 strokeStart.y += centerAdjusts[i].b * mBorderWidths[i];
1214 strokeEnd.x += centerAdjusts[i].a * mBorderWidths[i];
1215 strokeEnd.y += centerAdjusts[i].b * mBorderWidths[i];
1217 mContext->MoveTo(strokeStart);
1218 mContext->LineTo(strokeEnd);
1219 mContext->SetColor(gfxRGBA(mBorderColors[i]));
1220 mContext->Stroke();
1222 if (firstColor != secondColor) {
1223 nsRefPtr<gfxPattern> pattern =
1224 CreateCornerGradient(c, firstColor, secondColor);
1225 mContext->SetPattern(pattern);
1226 } else {
1227 mContext->SetColor(firstColor);
1230 if (mBorderRadii[c].width > 0 && mBorderRadii[c].height > 0) {
1231 p0.x = pc.x + cornerMults[i].a * mBorderRadii[c].width;
1232 p0.y = pc.y + cornerMults[i].b * mBorderRadii[c].height;
1234 p3.x = pc.x + cornerMults[i3].a * mBorderRadii[c].width;
1235 p3.y = pc.y + cornerMults[i3].b * mBorderRadii[c].height;
1237 p1.x = p0.x + alpha * cornerMults[i2].a * mBorderRadii[c].width;
1238 p1.y = p0.y + alpha * cornerMults[i2].b * mBorderRadii[c].height;
1240 p2.x = p3.x - alpha * cornerMults[i3].a * mBorderRadii[c].width;
1241 p2.y = p3.y - alpha * cornerMults[i3].b * mBorderRadii[c].height;
1243 mContext->NewPath();
1245 gfxPoint cornerStart;
1246 cornerStart.x = pc.x + cornerMults[i].a * mBorderCornerDimensions[c].width;
1247 cornerStart.y = pc.y + cornerMults[i].b * mBorderCornerDimensions[c].height;
1249 mContext->MoveTo(cornerStart);
1250 mContext->LineTo(p0);
1252 mContext->CurveTo(p1, p2, p3);
1254 gfxPoint outerCornerEnd;
1255 outerCornerEnd.x = pc.x + cornerMults[i3].a * mBorderCornerDimensions[c].width;
1256 outerCornerEnd.y = pc.y + cornerMults[i3].b * mBorderCornerDimensions[c].height;
1258 mContext->LineTo(outerCornerEnd);
1260 p0.x = pci.x + cornerMults[i].a * innerRadii[c].width;
1261 p0.y = pci.y + cornerMults[i].b * innerRadii[c].height;
1263 p3i.x = pci.x + cornerMults[i3].a * innerRadii[c].width;
1264 p3i.y = pci.y + cornerMults[i3].b * innerRadii[c].height;
1266 p1.x = p0.x + alpha * cornerMults[i2].a * innerRadii[c].width;
1267 p1.y = p0.y + alpha * cornerMults[i2].b * innerRadii[c].height;
1269 p2.x = p3i.x - alpha * cornerMults[i3].a * innerRadii[c].width;
1270 p2.y = p3i.y - alpha * cornerMults[i3].b * innerRadii[c].height;
1271 mContext->LineTo(p3i);
1272 mContext->CurveTo(p2, p1, p0);
1273 mContext->ClosePath();
1274 mContext->Fill();
1275 } else {
1276 gfxPoint c1, c2, c3, c4;
1278 c1.x = pc.x + cornerMults[i].a * mBorderCornerDimensions[c].width;
1279 c1.y = pc.y + cornerMults[i].b * mBorderCornerDimensions[c].height;
1280 c2 = pc;
1281 c3.x = pc.x + cornerMults[i3].a * mBorderCornerDimensions[c].width;
1282 c3.y = pc.y + cornerMults[i3].b * mBorderCornerDimensions[c].height;
1284 mContext->NewPath();
1285 mContext->MoveTo(c1);
1286 mContext->LineTo(c2);
1287 mContext->LineTo(c3);
1288 mContext->LineTo(pci);
1289 mContext->ClosePath();
1291 mContext->Fill();
1296 void
1297 nsCSSBorderRenderer::DrawRectangularCompositeColors()
1299 nsBorderColors *currentColors[4];
1300 mContext->SetLineWidth(1);
1301 memcpy(currentColors, mCompositeColors, sizeof(nsBorderColors*) * 4);
1302 gfxRect rect = mOuterRect;
1303 rect.Inset(0.5);
1305 const twoFloats cornerAdjusts[4] = { { +0.5, 0 },
1306 { 0, +0.5 },
1307 { -0.5, 0 },
1308 { 0, -0.5 } };
1310 for (int i = 0; i < mBorderWidths[0]; i++) {
1311 NS_FOR_CSS_SIDES(side) {
1312 int sideNext = (side + 1) % 4;
1314 gfxPoint firstCorner = rect.CCWCorner(side);
1315 firstCorner.x += cornerAdjusts[side].a;
1316 firstCorner.y += cornerAdjusts[side].b;
1317 gfxPoint secondCorner = rect.CWCorner(side);
1318 secondCorner.x -= cornerAdjusts[side].a;
1319 secondCorner.y -= cornerAdjusts[side].b;
1321 gfxRGBA currentColor =
1322 currentColors[side] ? gfxRGBA(currentColors[side]->mColor)
1323 : gfxRGBA(mBorderColors[side]);
1325 mContext->SetColor(currentColor);
1326 mContext->NewPath();
1327 mContext->MoveTo(firstCorner);
1328 mContext->LineTo(secondCorner);
1329 mContext->Stroke();
1331 mContext->NewPath();
1332 gfxPoint cornerTopLeft = rect.CWCorner(side);
1333 cornerTopLeft.x -= 0.5;
1334 cornerTopLeft.y -= 0.5;
1335 mContext->Rectangle(gfxRect(cornerTopLeft, gfxSize(1, 1)));
1336 gfxRGBA nextColor =
1337 currentColors[sideNext] ? gfxRGBA(currentColors[sideNext]->mColor)
1338 : gfxRGBA(mBorderColors[sideNext]);
1340 gfxRGBA cornerColor((currentColor.r + nextColor.r) / 2.0,
1341 (currentColor.g + nextColor.g) / 2.0,
1342 (currentColor.b + nextColor.b) / 2.0,
1343 (currentColor.a + nextColor.a) / 2.0);
1344 mContext->SetColor(cornerColor);
1345 mContext->Fill();
1347 if (side != 0) {
1348 // We'll have to keep side 0 for the color averaging on side 3.
1349 if (currentColors[side] && currentColors[side]->mNext) {
1350 currentColors[side] = currentColors[side]->mNext;
1354 // Now advance the color for side 0.
1355 if (currentColors[0] && currentColors[0]->mNext) {
1356 currentColors[0] = currentColors[0]->mNext;
1358 rect.Inset(1);
1362 void
1363 nsCSSBorderRenderer::DrawBorders()
1365 PRBool forceSeparateCorners = PR_FALSE;
1367 // Examine the border style to figure out if we can draw it in one
1368 // go or not.
1369 PRBool tlBordersSame = AreBorderSideFinalStylesSame(SIDE_BIT_TOP | SIDE_BIT_LEFT);
1370 PRBool brBordersSame = AreBorderSideFinalStylesSame(SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT);
1371 PRBool allBordersSame = AreBorderSideFinalStylesSame(SIDE_BITS_ALL);
1372 if (allBordersSame &&
1373 ((mCompositeColors[0] == NULL &&
1374 (mBorderStyles[0] == NS_STYLE_BORDER_STYLE_NONE ||
1375 mBorderStyles[0] == NS_STYLE_BORDER_STYLE_HIDDEN ||
1376 mBorderColors[0] == NS_RGBA(0,0,0,0))) ||
1377 (mCompositeColors[0] &&
1378 (mCompositeColors[0]->mColor == NS_RGBA(0,0,0,0) &&
1379 !mCompositeColors[0]->mNext))))
1381 // All borders are the same style, and the style is either none or hidden, or the color
1382 // is transparent.
1383 // This also checks if the first composite color is transparent, and there are
1384 // no others. It doesn't check if there are subsequent transparent ones, because
1385 // that would be very silly.
1386 return;
1389 // round mOuterRect and mInnerRect; they're already an integer
1390 // number of pixels apart and should stay that way after
1391 // rounding.
1392 mOuterRect.Round();
1393 mInnerRect.Round();
1395 gfxMatrix mat = mContext->CurrentMatrix();
1397 // Clamp the CTM to be pixel-aligned; we do this only
1398 // for translation-only matrices now, but we could do it
1399 // if the matrix has just a scale as well. We should not
1400 // do it if there's a rotation.
1401 if (!mat.HasNonTranslation()) {
1402 mat.x0 = floor(mat.x0 + 0.5);
1403 mat.y0 = floor(mat.y0 + 0.5);
1404 mContext->SetMatrix(mat);
1407 PRBool allBordersSameWidth = AllBordersSameWidth();
1409 if (allBordersSameWidth && mBorderWidths[0] == 0.0) {
1410 // Some of the allBordersSameWidth codepaths depend on the border
1411 // width being greater than zero.
1412 return;
1415 PRBool allBordersSolid;
1416 bool noCornerOutsideCenter = true;
1418 // First there's a couple of 'special cases' that have specifically optimized
1419 // drawing paths, when none of these can be used we move on to the generalized
1420 // border drawing code.
1421 if (allBordersSame &&
1422 mCompositeColors[0] == NULL &&
1423 allBordersSameWidth &&
1424 mBorderStyles[0] == NS_STYLE_BORDER_STYLE_SOLID &&
1425 mNoBorderRadius)
1427 // Very simple case.
1428 SetupStrokeStyle(NS_SIDE_TOP);
1429 gfxRect rect = mOuterRect;
1430 rect.Inset(mBorderWidths[0] / 2.0);
1431 mContext->NewPath();
1432 mContext->Rectangle(rect);
1433 mContext->Stroke();
1434 return;
1437 if (allBordersSame &&
1438 mCompositeColors[0] == NULL &&
1439 allBordersSameWidth &&
1440 mBorderStyles[0] == NS_STYLE_BORDER_STYLE_DOTTED &&
1441 mBorderWidths[0] < 3 &&
1442 mNoBorderRadius)
1444 // Very simple case. We draw this rectangular dotted borner without
1445 // antialiasing. The dots should be pixel aligned.
1446 SetupStrokeStyle(NS_SIDE_TOP);
1448 gfxFloat dash = mBorderWidths[0];
1449 mContext->SetDash(&dash, 1, 0.5);
1450 mContext->SetAntialiasMode(gfxContext::MODE_ALIASED);
1451 gfxRect rect = mOuterRect;
1452 rect.Inset(mBorderWidths[0] / 2.0);
1453 mContext->NewPath();
1454 mContext->Rectangle(rect);
1455 mContext->Stroke();
1456 return;
1460 if (allBordersSame &&
1461 allBordersSameWidth &&
1462 mCompositeColors[0] == NULL &&
1463 mBorderStyles[0] == NS_STYLE_BORDER_STYLE_SOLID)
1465 NS_FOR_CSS_CORNERS(i) {
1466 if (mBorderRadii[i].width <= mBorderWidths[0]) {
1467 noCornerOutsideCenter = false;
1469 if (mBorderRadii[i].height <= mBorderWidths[0]) {
1470 noCornerOutsideCenter = false;
1474 // We can only do a stroke here if all border radii centers are inside the
1475 // inner rect, otherwise we get rendering artifacts.
1477 if (noCornerOutsideCenter) {
1478 // Relatively simple case.
1479 SetupStrokeStyle(NS_SIDE_TOP);
1480 mOuterRect.Inset(mBorderWidths[0] / 2.0);
1481 NS_FOR_CSS_CORNERS(corner) {
1482 if (mBorderRadii.sizes[corner].height == 0 || mBorderRadii.sizes[corner].width == 0) {
1483 continue;
1485 mBorderRadii.sizes[corner].width -= mBorderWidths[0] / 2;
1486 mBorderRadii.sizes[corner].height -= mBorderWidths[0] / 2;
1489 mContext->NewPath();
1490 mContext->RoundedRectangle(mOuterRect, mBorderRadii);
1491 mContext->Stroke();
1492 return;
1496 bool hasCompositeColors;
1498 allBordersSolid = AllBordersSolid(&hasCompositeColors);
1499 // This leaves the border corners non-interpolated for single width borders.
1500 // Doing this is slightly faster and shouldn't be a problem visually.
1501 if (allBordersSolid &&
1502 allBordersSameWidth &&
1503 mCompositeColors[0] == NULL &&
1504 mBorderWidths[0] == 1 &&
1505 mNoBorderRadius)
1507 DrawSingleWidthSolidBorder();
1508 return;
1511 if (allBordersSolid && !hasCompositeColors)
1513 DrawNoCompositeColorSolidBorder();
1514 return;
1517 if (allBordersSolid &&
1518 allBordersSameWidth &&
1519 mNoBorderRadius)
1521 // Easy enough to deal with.
1522 DrawRectangularCompositeColors();
1523 return;
1526 // If we have composite colors -and- border radius,
1527 // then use separate corners so we get OPERATOR_ADD for the corners.
1528 // Otherwise, we'll get artifacts as we draw stacked 1px-wide curves.
1529 if (allBordersSame && mCompositeColors[0] != nsnull && !mNoBorderRadius)
1530 forceSeparateCorners = PR_TRUE;
1532 S(" mOuterRect: "), S(mOuterRect), SN();
1533 S(" mInnerRect: "), S(mInnerRect), SN();
1534 SF(" mBorderColors: 0x%08x 0x%08x 0x%08x 0x%08x\n", mBorderColors[0], mBorderColors[1], mBorderColors[2], mBorderColors[3]);
1536 // if conditioning the outside rect failed, then bail -- the outside
1537 // rect is supposed to enclose the entire border
1538 mOuterRect.Condition();
1539 if (mOuterRect.IsEmpty())
1540 return;
1542 mInnerRect.Condition();
1543 PRIntn dashedSides = 0;
1545 NS_FOR_CSS_SIDES(i) {
1546 PRUint8 style = mBorderStyles[i];
1547 if (style == NS_STYLE_BORDER_STYLE_DASHED ||
1548 style == NS_STYLE_BORDER_STYLE_DOTTED)
1550 // pretend that all borders aren't the same; we need to draw
1551 // things separately for dashed/dotting
1552 allBordersSame = PR_FALSE;
1553 dashedSides |= (1 << i);
1557 SF(" allBordersSame: %d dashedSides: 0x%02x\n", allBordersSame, dashedSides);
1559 if (allBordersSame && !forceSeparateCorners) {
1560 /* Draw everything in one go */
1561 DrawBorderSides(SIDE_BITS_ALL);
1562 SN("---------------- (1)");
1563 } else {
1564 /* We have more than one pass to go. Draw the corners separately from the sides. */
1567 * If we have a 1px-wide border, the corners are going to be
1568 * negligible, so don't bother doing anything fancy. Just extend
1569 * the top and bottom borders to the right 1px and the left border
1570 * to the bottom 1px. We do this by twiddling the corner dimensions,
1571 * which causes the right to happen later on. Only do this if we have
1572 * a 1.0 unit border all around and no border radius.
1575 NS_FOR_CSS_CORNERS(corner) {
1576 const mozilla::css::Side sides[2] = { mozilla::css::Side(corner), PREV_SIDE(corner) };
1578 if (!IsZeroSize(mBorderRadii[corner]))
1579 continue;
1581 if (mBorderWidths[sides[0]] == 1.0 && mBorderWidths[sides[1]] == 1.0) {
1582 if (corner == NS_CORNER_TOP_LEFT || corner == NS_CORNER_TOP_RIGHT)
1583 mBorderCornerDimensions[corner].width = 0.0;
1584 else
1585 mBorderCornerDimensions[corner].height = 0.0;
1589 // First, the corners
1590 NS_FOR_CSS_CORNERS(corner) {
1591 // if there's no corner, don't do all this work for it
1592 if (IsZeroSize(mBorderCornerDimensions[corner]))
1593 continue;
1595 const PRIntn sides[2] = { corner, PREV_SIDE(corner) };
1596 PRIntn sideBits = (1 << sides[0]) | (1 << sides[1]);
1598 PRBool simpleCornerStyle = mCompositeColors[sides[0]] == NULL &&
1599 mCompositeColors[sides[1]] == NULL &&
1600 AreBorderSideFinalStylesSame(sideBits);
1602 // If we don't have anything complex going on in this corner,
1603 // then we can just fill the corner with a solid color, and avoid
1604 // the potentially expensive clip.
1605 if (simpleCornerStyle &&
1606 IsZeroSize(mBorderRadii[corner]) &&
1607 IsSolidCornerStyle(mBorderStyles[sides[0]], corner))
1609 mContext->NewPath();
1610 DoCornerSubPath(corner);
1611 mContext->SetColor(MakeBorderColor(mBorderColors[sides[0]],
1612 mBackgroundColor,
1613 BorderColorStyleForSolidCorner(mBorderStyles[sides[0]], corner)));
1614 mContext->Fill();
1615 continue;
1618 mContext->Save();
1620 // clip to the corner
1621 mContext->NewPath();
1622 DoCornerSubPath(corner);
1623 mContext->Clip();
1625 if (simpleCornerStyle) {
1626 // we don't need a group for this corner, the sides are the same,
1627 // but we weren't able to render just a solid block for the corner.
1628 DrawBorderSides(sideBits);
1629 } else {
1630 // Sides are different. We need to draw using OPERATOR_ADD to
1631 // get correct color blending behaviour at the seam. We need
1632 // to do it in an offscreen surface to ensure that we're
1633 // always compositing on transparent black. If the colors
1634 // don't have transparency and the current destination surface
1635 // has an alpha channel, we could just clear the region and
1636 // avoid the temporary, but that situation doesn't happen all
1637 // that often in practice (we double buffer to no-alpha
1638 // surfaces).
1640 mContext->PushGroup(gfxASurface::CONTENT_COLOR_ALPHA);
1641 mContext->SetOperator(gfxContext::OPERATOR_ADD);
1643 for (int cornerSide = 0; cornerSide < 2; cornerSide++) {
1644 mozilla::css::Side side = mozilla::css::Side(sides[cornerSide]);
1645 PRUint8 style = mBorderStyles[side];
1647 SF("corner: %d cornerSide: %d side: %d style: %d\n", corner, cornerSide, side, style);
1649 mContext->Save();
1651 mContext->NewPath();
1652 DoSideClipSubPath(side);
1653 mContext->Clip();
1655 DrawBorderSides(1 << side);
1657 mContext->Restore();
1660 mContext->PopGroupToSource();
1661 mContext->SetOperator(gfxContext::OPERATOR_OVER);
1662 mContext->Paint();
1665 mContext->Restore();
1667 SN();
1670 // in the case of a single-unit border, we already munged the
1671 // corners up above; so we can just draw the top left and bottom
1672 // right sides separately, if they're the same.
1674 // We need to check for mNoBorderRadius, because when there is
1675 // one, FillSolidBorder always draws the full rounded rectangle
1676 // and expects there to be a clip in place.
1677 PRIntn alreadyDrawnSides = 0;
1678 if (mOneUnitBorder &&
1679 mNoBorderRadius &&
1680 (dashedSides & (SIDE_BIT_TOP | SIDE_BIT_LEFT)) == 0)
1682 if (tlBordersSame) {
1683 DrawBorderSides(SIDE_BIT_TOP | SIDE_BIT_LEFT);
1684 alreadyDrawnSides |= (SIDE_BIT_TOP | SIDE_BIT_LEFT);
1687 if (brBordersSame && (dashedSides & (SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT)) == 0) {
1688 DrawBorderSides(SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT);
1689 alreadyDrawnSides |= (SIDE_BIT_BOTTOM | SIDE_BIT_RIGHT);
1693 // We're done with the corners, now draw the sides.
1694 NS_FOR_CSS_SIDES (side) {
1695 // if we drew it above, skip it
1696 if (alreadyDrawnSides & (1 << side))
1697 continue;
1699 // If there's no border on this side, skip it
1700 if (mBorderWidths[side] == 0.0 ||
1701 mBorderStyles[side] == NS_STYLE_BORDER_STYLE_HIDDEN ||
1702 mBorderStyles[side] == NS_STYLE_BORDER_STYLE_NONE)
1703 continue;
1706 if (dashedSides & (1 << side)) {
1707 // Dashed sides will always draw just the part ignoring the
1708 // corners for the side, so no need to clip.
1709 DrawDashedSide (side);
1711 SN("---------------- (d)");
1712 continue;
1715 // Undashed sides will currently draw the entire side,
1716 // including parts that would normally be covered by a corner,
1717 // so we need to clip.
1719 // XXX Optimization -- it would be good to make this work like
1720 // DrawDashedSide, and have a DrawOneSide function that just
1721 // draws one side and not the corners, because then we can
1722 // avoid the potentially expensive clip.
1723 mContext->Save();
1724 mContext->NewPath();
1725 DoSideClipWithoutCornersSubPath(side);
1726 mContext->Clip();
1728 DrawBorderSides(1 << side);
1730 mContext->Restore();
1732 SN("---------------- (*)");