Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/layout/painting/nsCSSRenderingBorders.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "nsCSSRenderingBorders.h"
8
9
#include "gfxUtils.h"
10
#include "mozilla/ArrayUtils.h"
11
#include "mozilla/gfx/2D.h"
12
#include "mozilla/gfx/Helpers.h"
13
#include "mozilla/gfx/PathHelpers.h"
14
#include "BorderConsts.h"
15
#include "DashedCornerFinder.h"
16
#include "DottedCornerFinder.h"
17
#include "nsLayoutUtils.h"
18
#include "nsStyleConsts.h"
19
#include "nsContentUtils.h"
20
#include "nsCSSColorUtils.h"
21
#include "nsCSSRendering.h"
22
#include "nsCSSRenderingGradients.h"
23
#include "nsDisplayList.h"
24
#include "GeckoProfiler.h"
25
#include "nsExpirationTracker.h"
26
#include "nsIScriptError.h"
27
#include "nsClassHashtable.h"
28
#include "nsPresContext.h"
29
#include "nsStyleStruct.h"
30
#include "mozilla/gfx/2D.h"
31
#include "gfx2DGlue.h"
32
#include "gfxGradientCache.h"
33
#include "mozilla/layers/StackingContextHelper.h"
34
#include "mozilla/layers/WebRenderLayerManager.h"
35
#include "mozilla/Range.h"
36
#include <algorithm>
37
38
using namespace mozilla;
39
using namespace mozilla::gfx;
40
using namespace mozilla::image;
41
42
#define MAX_COMPOSITE_BORDER_WIDTH LayoutDeviceIntCoord(10000)
43
44
/**
45
 * nsCSSRendering::PaintBorder
46
 * nsCSSRendering::PaintOutline
47
 *   -> DrawBorders
48
 *
49
 * DrawBorders
50
 *   -> Ability to use specialized approach?
51
 *      |- Draw using specialized function
52
 *   |- separate corners?
53
 *   |- dashed side mask
54
 *   |
55
 *   -> can border be drawn in 1 pass? (e.g., solid border same color all
56
 * around)
57
 *      |- DrawBorderSides with all 4 sides
58
 *   -> more than 1 pass?
59
 *      |- for each corner
60
 *         |- clip to DoCornerClipSubPath
61
 *         |- for each side adjacent to corner
62
 *            |- clip to GetSideClipSubPath
63
 *            |- DrawBorderSides with one side
64
 *      |- for each side
65
 *         |- GetSideClipWithoutCornersRect
66
 *         |- DrawDashedOrDottedSide || DrawBorderSides with one side
67
 */
68
69
static void
70
ComputeBorderCornerDimensions(const Float* aBorderWidths,
71
                              const RectCornerRadii& aRadii,
72
                              RectCornerRadii* aDimsResult);
73
74
// given a side index, get the previous and next side index
75
0
#define NEXT_SIDE(_s) mozilla::Side(((_s) + 1) & 3)
76
0
#define PREV_SIDE(_s) mozilla::Side(((_s) + 3) & 3)
77
78
// given a corner index, get the previous and next corner index
79
#define NEXT_CORNER(_s) Corner(((_s) + 1) & 3)
80
#define PREV_CORNER(_s) Corner(((_s) + 3) & 3)
81
82
// from the given base color and the background color, turn
83
// color into a color for the given border pattern style
84
static Color
85
MakeBorderColor(nscolor aColor,
86
                nscolor aBackgroundColor,
87
                BorderColorStyle aBorderColorStyle);
88
89
// Given a line index (an index starting from the outside of the
90
// border going inwards) and an array of line styles, calculate the
91
// color that that stripe of the border should be rendered in.
92
static Color
93
ComputeColorForLine(uint32_t aLineIndex,
94
                    const BorderColorStyle* aBorderColorStyle,
95
                    uint32_t aBorderColorStyleCount,
96
                    nscolor aBorderColor,
97
                    nscolor aBackgroundColor);
98
99
// little helper function to check if the array of 4 floats given are
100
// equal to the given value
101
static bool
102
CheckFourFloatsEqual(const Float* vals, Float k)
103
0
{
104
0
  return (vals[0] == k && vals[1] == k && vals[2] == k && vals[3] == k);
105
0
}
106
107
static bool
108
IsZeroSize(const Size& sz)
109
0
{
110
0
  return sz.width == 0.0 || sz.height == 0.0;
111
0
}
112
113
/* static */ bool
114
nsCSSBorderRenderer::AllCornersZeroSize(const RectCornerRadii& corners)
115
0
{
116
0
  return IsZeroSize(corners[eCornerTopLeft]) &&
117
0
         IsZeroSize(corners[eCornerTopRight]) &&
118
0
         IsZeroSize(corners[eCornerBottomRight]) &&
119
0
         IsZeroSize(corners[eCornerBottomLeft]);
120
0
}
121
122
static mozilla::Side
123
GetHorizontalSide(Corner aCorner)
124
0
{
125
0
  return (aCorner == C_TL || aCorner == C_TR) ? eSideTop : eSideBottom;
126
0
}
127
128
static mozilla::Side
129
GetVerticalSide(Corner aCorner)
130
0
{
131
0
  return (aCorner == C_TL || aCorner == C_BL) ? eSideLeft : eSideRight;
132
0
}
133
134
static Corner
135
GetCWCorner(mozilla::Side aSide)
136
0
{
137
0
  return Corner(NEXT_SIDE(aSide));
138
0
}
139
140
static Corner
141
GetCCWCorner(mozilla::Side aSide)
142
0
{
143
0
  return Corner(aSide);
144
0
}
145
146
static bool
147
IsSingleSide(int aSides)
148
0
{
149
0
  return aSides == eSideBitsTop || aSides == eSideBitsRight ||
150
0
         aSides == eSideBitsBottom || aSides == eSideBitsLeft;
151
0
}
152
153
static bool
154
IsHorizontalSide(mozilla::Side aSide)
155
0
{
156
0
  return aSide == eSideTop || aSide == eSideBottom;
157
0
}
158
159
typedef enum {
160
  // Normal solid square corner.  Will be rectangular, the size of the
161
  // adjacent sides.  If the corner has a border radius, the corner
162
  // will always be solid, since we don't do dotted/dashed etc.
163
  CORNER_NORMAL,
164
165
  // Paint the corner in whatever style is not dotted/dashed of the
166
  // adjacent corners.
167
  CORNER_SOLID,
168
169
  // Paint the corner as a dot, the size of the bigger of the adjacent
170
  // sides.
171
  CORNER_DOT
172
} CornerStyle;
173
174
nsCSSBorderRenderer::nsCSSBorderRenderer(nsPresContext* aPresContext,
175
                                         const nsIDocument* aDocument,
176
                                         DrawTarget* aDrawTarget,
177
                                         const Rect& aDirtyRect,
178
                                         Rect& aOuterRect,
179
                                         const uint8_t* aBorderStyles,
180
                                         const Float* aBorderWidths,
181
                                         RectCornerRadii& aBorderRadii,
182
                                         const nscolor* aBorderColors,
183
                                         nscolor aBackgroundColor,
184
                                         bool aBackfaceIsVisible,
185
                                         const Maybe<Rect>& aClipRect)
186
  : mPresContext(aPresContext)
187
  , mDocument(aDocument)
188
  , mDrawTarget(aDrawTarget)
189
  , mDirtyRect(aDirtyRect)
190
  , mOuterRect(aOuterRect)
191
  , mBorderRadii(aBorderRadii)
192
  , mBackgroundColor(aBackgroundColor)
193
  , mBackfaceIsVisible(aBackfaceIsVisible)
194
  , mLocalClip(aClipRect)
195
0
{
196
0
  PodCopy(mBorderStyles, aBorderStyles, 4);
197
0
  PodCopy(mBorderWidths, aBorderWidths, 4);
198
0
  PodCopy(mBorderColors, aBorderColors, 4);
199
0
  mInnerRect = mOuterRect;
200
0
  mInnerRect.Deflate(Margin(
201
0
    mBorderStyles[0] != NS_STYLE_BORDER_STYLE_NONE ? mBorderWidths[0] : 0,
202
0
    mBorderStyles[1] != NS_STYLE_BORDER_STYLE_NONE ? mBorderWidths[1] : 0,
203
0
    mBorderStyles[2] != NS_STYLE_BORDER_STYLE_NONE ? mBorderWidths[2] : 0,
204
0
    mBorderStyles[3] != NS_STYLE_BORDER_STYLE_NONE ? mBorderWidths[3] : 0));
205
0
206
0
  ComputeBorderCornerDimensions(
207
0
    mBorderWidths, mBorderRadii, &mBorderCornerDimensions);
208
0
209
0
  mOneUnitBorder = CheckFourFloatsEqual(mBorderWidths, 1.0);
210
0
  mNoBorderRadius = AllCornersZeroSize(mBorderRadii);
211
0
  mAllBordersSameStyle = AreBorderSideFinalStylesSame(eSideBitsAll);
212
0
  mAllBordersSameWidth = AllBordersSameWidth();
213
0
  mAvoidStroke = false;
214
0
}
215
216
/* static */ void
217
nsCSSBorderRenderer::ComputeInnerRadii(const RectCornerRadii& aRadii,
218
                                       const Float* aBorderSizes,
219
                                       RectCornerRadii* aInnerRadiiRet)
220
0
{
221
0
  RectCornerRadii& iRadii = *aInnerRadiiRet;
222
0
223
0
  iRadii[C_TL].width =
224
0
    std::max(0.f, aRadii[C_TL].width - aBorderSizes[eSideLeft]);
225
0
  iRadii[C_TL].height =
226
0
    std::max(0.f, aRadii[C_TL].height - aBorderSizes[eSideTop]);
227
0
228
0
  iRadii[C_TR].width =
229
0
    std::max(0.f, aRadii[C_TR].width - aBorderSizes[eSideRight]);
230
0
  iRadii[C_TR].height =
231
0
    std::max(0.f, aRadii[C_TR].height - aBorderSizes[eSideTop]);
232
0
233
0
  iRadii[C_BR].width =
234
0
    std::max(0.f, aRadii[C_BR].width - aBorderSizes[eSideRight]);
235
0
  iRadii[C_BR].height =
236
0
    std::max(0.f, aRadii[C_BR].height - aBorderSizes[eSideBottom]);
237
0
238
0
  iRadii[C_BL].width =
239
0
    std::max(0.f, aRadii[C_BL].width - aBorderSizes[eSideLeft]);
240
0
  iRadii[C_BL].height =
241
0
    std::max(0.f, aRadii[C_BL].height - aBorderSizes[eSideBottom]);
242
0
}
243
244
/* static */ void
245
nsCSSBorderRenderer::ComputeOuterRadii(const RectCornerRadii& aRadii,
246
                                       const Float* aBorderSizes,
247
                                       RectCornerRadii* aOuterRadiiRet)
248
0
{
249
0
  RectCornerRadii& oRadii = *aOuterRadiiRet;
250
0
251
0
  // default all corners to sharp corners
252
0
  oRadii = RectCornerRadii(0.f);
253
0
254
0
  // round the edges that have radii > 0.0 to start with
255
0
  if (aRadii[C_TL].width > 0.f && aRadii[C_TL].height > 0.f) {
256
0
    oRadii[C_TL].width =
257
0
      std::max(0.f, aRadii[C_TL].width + aBorderSizes[eSideLeft]);
258
0
    oRadii[C_TL].height =
259
0
      std::max(0.f, aRadii[C_TL].height + aBorderSizes[eSideTop]);
260
0
  }
261
0
262
0
  if (aRadii[C_TR].width > 0.f && aRadii[C_TR].height > 0.f) {
263
0
    oRadii[C_TR].width =
264
0
      std::max(0.f, aRadii[C_TR].width + aBorderSizes[eSideRight]);
265
0
    oRadii[C_TR].height =
266
0
      std::max(0.f, aRadii[C_TR].height + aBorderSizes[eSideTop]);
267
0
  }
268
0
269
0
  if (aRadii[C_BR].width > 0.f && aRadii[C_BR].height > 0.f) {
270
0
    oRadii[C_BR].width =
271
0
      std::max(0.f, aRadii[C_BR].width + aBorderSizes[eSideRight]);
272
0
    oRadii[C_BR].height =
273
0
      std::max(0.f, aRadii[C_BR].height + aBorderSizes[eSideBottom]);
274
0
  }
275
0
276
0
  if (aRadii[C_BL].width > 0.f && aRadii[C_BL].height > 0.f) {
277
0
    oRadii[C_BL].width =
278
0
      std::max(0.f, aRadii[C_BL].width + aBorderSizes[eSideLeft]);
279
0
    oRadii[C_BL].height =
280
0
      std::max(0.f, aRadii[C_BL].height + aBorderSizes[eSideBottom]);
281
0
  }
282
0
}
283
284
/*static*/ void
285
ComputeBorderCornerDimensions(const Float* aBorderWidths,
286
                              const RectCornerRadii& aRadii,
287
                              RectCornerRadii* aDimsRet)
288
0
{
289
0
  Float leftWidth = aBorderWidths[eSideLeft];
290
0
  Float topWidth = aBorderWidths[eSideTop];
291
0
  Float rightWidth = aBorderWidths[eSideRight];
292
0
  Float bottomWidth = aBorderWidths[eSideBottom];
293
0
294
0
  if (nsCSSBorderRenderer::AllCornersZeroSize(aRadii)) {
295
0
    // These will always be in pixel units from CSS
296
0
    (*aDimsRet)[C_TL] = Size(leftWidth, topWidth);
297
0
    (*aDimsRet)[C_TR] = Size(rightWidth, topWidth);
298
0
    (*aDimsRet)[C_BR] = Size(rightWidth, bottomWidth);
299
0
    (*aDimsRet)[C_BL] = Size(leftWidth, bottomWidth);
300
0
  } else {
301
0
    // Always round up to whole pixels for the corners; it's safe to
302
0
    // make the corners bigger than necessary, and this way we ensure
303
0
    // that we avoid seams.
304
0
    (*aDimsRet)[C_TL] = Size(ceil(std::max(leftWidth, aRadii[C_TL].width)),
305
0
                             ceil(std::max(topWidth, aRadii[C_TL].height)));
306
0
    (*aDimsRet)[C_TR] = Size(ceil(std::max(rightWidth, aRadii[C_TR].width)),
307
0
                             ceil(std::max(topWidth, aRadii[C_TR].height)));
308
0
    (*aDimsRet)[C_BR] = Size(ceil(std::max(rightWidth, aRadii[C_BR].width)),
309
0
                             ceil(std::max(bottomWidth, aRadii[C_BR].height)));
310
0
    (*aDimsRet)[C_BL] = Size(ceil(std::max(leftWidth, aRadii[C_BL].width)),
311
0
                             ceil(std::max(bottomWidth, aRadii[C_BL].height)));
312
0
  }
313
0
}
314
315
bool
316
nsCSSBorderRenderer::AreBorderSideFinalStylesSame(uint8_t aSides)
317
0
{
318
0
  NS_ASSERTION(aSides != 0 && (aSides & ~eSideBitsAll) == 0,
319
0
               "AreBorderSidesSame: invalid whichSides!");
320
0
321
0
  /* First check if the specified styles and colors are the same for all sides
322
0
   */
323
0
  int firstStyle = 0;
324
0
  NS_FOR_CSS_SIDES(i)
325
0
  {
326
0
    if (firstStyle == i) {
327
0
      if (((1 << i) & aSides) == 0)
328
0
        firstStyle++;
329
0
      continue;
330
0
    }
331
0
332
0
    if (((1 << i) & aSides) == 0) {
333
0
      continue;
334
0
    }
335
0
336
0
    if (mBorderStyles[firstStyle] != mBorderStyles[i] ||
337
0
        mBorderColors[firstStyle] != mBorderColors[i]) {
338
0
      return false;
339
0
    }
340
0
  }
341
0
342
0
  /* Then if it's one of the two-tone styles and we're not
343
0
   * just comparing the TL or BR sides */
344
0
  switch (mBorderStyles[firstStyle]) {
345
0
    case NS_STYLE_BORDER_STYLE_GROOVE:
346
0
    case NS_STYLE_BORDER_STYLE_RIDGE:
347
0
    case NS_STYLE_BORDER_STYLE_INSET:
348
0
    case NS_STYLE_BORDER_STYLE_OUTSET:
349
0
      return ((aSides & ~(eSideBitsTop | eSideBitsLeft)) == 0 ||
350
0
              (aSides & ~(eSideBitsBottom | eSideBitsRight)) == 0);
351
0
  }
352
0
353
0
  return true;
354
0
}
355
356
bool
357
nsCSSBorderRenderer::IsSolidCornerStyle(uint8_t aStyle, Corner aCorner)
358
0
{
359
0
  switch (aStyle) {
360
0
    case NS_STYLE_BORDER_STYLE_SOLID:
361
0
      return true;
362
0
363
0
    case NS_STYLE_BORDER_STYLE_INSET:
364
0
    case NS_STYLE_BORDER_STYLE_OUTSET:
365
0
      return (aCorner == eCornerTopLeft || aCorner == eCornerBottomRight);
366
0
367
0
    case NS_STYLE_BORDER_STYLE_GROOVE:
368
0
    case NS_STYLE_BORDER_STYLE_RIDGE:
369
0
      return mOneUnitBorder &&
370
0
             (aCorner == eCornerTopLeft || aCorner == eCornerBottomRight);
371
0
372
0
    case NS_STYLE_BORDER_STYLE_DOUBLE:
373
0
      return mOneUnitBorder;
374
0
375
0
    default:
376
0
      return false;
377
0
  }
378
0
}
379
380
bool
381
nsCSSBorderRenderer::IsCornerMergeable(Corner aCorner)
382
0
{
383
0
  // Corner between dotted borders with same width and small radii is
384
0
  // merged into single dot.
385
0
  //
386
0
  //  widthH / 2.0
387
0
  // |<---------->|
388
0
  // |            |
389
0
  // |radius.width|
390
0
  // |<--->|      |
391
0
  // |     |      |
392
0
  // |    _+------+------------+-----
393
0
  // |  /      ###|###         |
394
0
  // |/    #######|#######     |
395
0
  // +   #########|#########   |
396
0
  // |  ##########|##########  |
397
0
  // | ###########|########### |
398
0
  // | ###########|########### |
399
0
  // |############|############|
400
0
  // +------------+############|
401
0
  // |#########################|
402
0
  // | ####################### |
403
0
  // | ####################### |
404
0
  // |  #####################  |
405
0
  // |   ###################   |
406
0
  // |     ###############     |
407
0
  // |         #######         |
408
0
  // +-------------------------+----
409
0
  // |                         |
410
0
  // |                         |
411
0
  mozilla::Side sideH(GetHorizontalSide(aCorner));
412
0
  mozilla::Side sideV(GetVerticalSide(aCorner));
413
0
  uint8_t styleH = mBorderStyles[sideH];
414
0
  uint8_t styleV = mBorderStyles[sideV];
415
0
  if (styleH != styleV || styleH != NS_STYLE_BORDER_STYLE_DOTTED) {
416
0
    return false;
417
0
  }
418
0
419
0
  Float widthH = mBorderWidths[sideH];
420
0
  Float widthV = mBorderWidths[sideV];
421
0
  if (widthH != widthV) {
422
0
    return false;
423
0
  }
424
0
425
0
  Size radius = mBorderRadii[aCorner];
426
0
  return IsZeroSize(radius) ||
427
0
         (radius.width < widthH / 2.0f && radius.height < widthH / 2.0f);
428
0
}
429
430
BorderColorStyle
431
nsCSSBorderRenderer::BorderColorStyleForSolidCorner(uint8_t aStyle,
432
                                                    Corner aCorner)
433
0
{
434
0
  // note that this function assumes that the corner is already solid,
435
0
  // as per the earlier function
436
0
  switch (aStyle) {
437
0
    case NS_STYLE_BORDER_STYLE_SOLID:
438
0
    case NS_STYLE_BORDER_STYLE_DOUBLE:
439
0
      return BorderColorStyleSolid;
440
0
441
0
    case NS_STYLE_BORDER_STYLE_INSET:
442
0
    case NS_STYLE_BORDER_STYLE_GROOVE:
443
0
      if (aCorner == eCornerTopLeft)
444
0
        return BorderColorStyleDark;
445
0
      else if (aCorner == eCornerBottomRight)
446
0
        return BorderColorStyleLight;
447
0
      break;
448
0
449
0
    case NS_STYLE_BORDER_STYLE_OUTSET:
450
0
    case NS_STYLE_BORDER_STYLE_RIDGE:
451
0
      if (aCorner == eCornerTopLeft)
452
0
        return BorderColorStyleLight;
453
0
      else if (aCorner == eCornerBottomRight)
454
0
        return BorderColorStyleDark;
455
0
      break;
456
0
  }
457
0
458
0
  return BorderColorStyleNone;
459
0
}
460
461
Rect
462
nsCSSBorderRenderer::GetCornerRect(Corner aCorner)
463
0
{
464
0
  Point offset(0.f, 0.f);
465
0
466
0
  if (aCorner == C_TR || aCorner == C_BR)
467
0
    offset.x = mOuterRect.Width() - mBorderCornerDimensions[aCorner].width;
468
0
  if (aCorner == C_BR || aCorner == C_BL)
469
0
    offset.y = mOuterRect.Height() - mBorderCornerDimensions[aCorner].height;
470
0
471
0
  return Rect(mOuterRect.TopLeft() + offset, mBorderCornerDimensions[aCorner]);
472
0
}
473
474
Rect
475
nsCSSBorderRenderer::GetSideClipWithoutCornersRect(mozilla::Side aSide)
476
0
{
477
0
  Point offset(0.f, 0.f);
478
0
479
0
  // The offset from the outside rect to the start of this side's
480
0
  // box.  For the top and bottom sides, the height of the box
481
0
  // must be the border height; the x start must take into account
482
0
  // the corner size (which may be bigger than the right or left
483
0
  // side's width).  The same applies to the right and left sides.
484
0
  if (aSide == eSideTop) {
485
0
    offset.x = mBorderCornerDimensions[C_TL].width;
486
0
  } else if (aSide == eSideRight) {
487
0
    offset.x = mOuterRect.Width() - mBorderWidths[eSideRight];
488
0
    offset.y = mBorderCornerDimensions[C_TR].height;
489
0
  } else if (aSide == eSideBottom) {
490
0
    offset.x = mBorderCornerDimensions[C_BL].width;
491
0
    offset.y = mOuterRect.Height() - mBorderWidths[eSideBottom];
492
0
  } else if (aSide == eSideLeft) {
493
0
    offset.y = mBorderCornerDimensions[C_TL].height;
494
0
  }
495
0
496
0
  // The sum of the width & height of the corners adjacent to the
497
0
  // side.  This relies on the relationship between side indexing and
498
0
  // corner indexing; that is, 0 == SIDE_TOP and 0 == CORNER_TOP_LEFT,
499
0
  // with both proceeding clockwise.
500
0
  Size sideCornerSum = mBorderCornerDimensions[GetCCWCorner(aSide)] +
501
0
                       mBorderCornerDimensions[GetCWCorner(aSide)];
502
0
  Rect rect(mOuterRect.TopLeft() + offset, mOuterRect.Size() - sideCornerSum);
503
0
504
0
  if (IsHorizontalSide(aSide))
505
0
    rect.height = mBorderWidths[aSide];
506
0
  else
507
0
    rect.width = mBorderWidths[aSide];
508
0
509
0
  return rect;
510
0
}
511
512
// The side border type and the adjacent border types are
513
// examined and one of the different types of clipping (listed
514
// below) is selected.
515
516
typedef enum {
517
  // clip to the trapezoid formed by the corners of the
518
  // inner and outer rectangles for the given side
519
  //
520
  // +---------------
521
  // |\%%%%%%%%%%%%%%
522
  // |  \%%%%%%%%%%%%
523
  // |   \%%%%%%%%%%%
524
  // |     \%%%%%%%%%
525
  // |      +--------
526
  // |      |
527
  // |      |
528
  SIDE_CLIP_TRAPEZOID,
529
530
  // clip to the trapezoid formed by the outer rectangle
531
  // corners and the center of the region, making sure
532
  // that diagonal lines all go directly from the outside
533
  // corner to the inside corner, but that they then continue on
534
  // to the middle.
535
  //
536
  // This is needed for correctly clipping rounded borders,
537
  // which might extend past the SIDE_CLIP_TRAPEZOID trap.
538
  //
539
  // +-------__--+---
540
  //  \%%%%_-%%%%%%%%
541
  //    \+-%%%%%%%%%%
542
  //    / \%%%%%%%%%%
543
  //   /   \%%%%%%%%%
544
  //  |     +%%_-+---
545
  // |        +%%%%%%
546
  // |       / \%%%%%
547
  // +      +    \%%%
548
  // |      |      +-
549
  SIDE_CLIP_TRAPEZOID_FULL,
550
551
  // clip to the rectangle formed by the given side including corner.
552
  // This is used by the non-dotted side next to dotted side.
553
  //
554
  // +---------------
555
  // |%%%%%%%%%%%%%%%
556
  // |%%%%%%%%%%%%%%%
557
  // |%%%%%%%%%%%%%%%
558
  // |%%%%%%%%%%%%%%%
559
  // +------+--------
560
  // |      |
561
  // |      |
562
  SIDE_CLIP_RECTANGLE_CORNER,
563
564
  // clip to the rectangle formed by the given side excluding corner.
565
  // This is used by the dotted side next to non-dotted side.
566
  //
567
  // +------+--------
568
  // |      |%%%%%%%%
569
  // |      |%%%%%%%%
570
  // |      |%%%%%%%%
571
  // |      |%%%%%%%%
572
  // |      +--------
573
  // |      |
574
  // |      |
575
  SIDE_CLIP_RECTANGLE_NO_CORNER,
576
} SideClipType;
577
578
// Given three points, p0, p1, and midPoint, move p1 further in to the
579
// rectangle (of which aMidPoint is the center) so that it reaches the
580
// closer of the horizontal or vertical lines intersecting the midpoint,
581
// while maintaing the slope of the line.  If p0 and p1 are the same,
582
// just move p1 to midPoint (since there's no slope to maintain).
583
// FIXME: Extending only to the midpoint isn't actually sufficient for
584
// boxes with asymmetric radii.
585
static void
586
MaybeMoveToMidPoint(Point& aP0, Point& aP1, const Point& aMidPoint)
587
0
{
588
0
  Point ps = aP1 - aP0;
589
0
590
0
  if (ps.x == 0.0) {
591
0
    if (ps.y == 0.0) {
592
0
      aP1 = aMidPoint;
593
0
    } else {
594
0
      aP1.y = aMidPoint.y;
595
0
    }
596
0
  } else {
597
0
    if (ps.y == 0.0) {
598
0
      aP1.x = aMidPoint.x;
599
0
    } else {
600
0
      Float k =
601
0
        std::min((aMidPoint.x - aP0.x) / ps.x, (aMidPoint.y - aP0.y) / ps.y);
602
0
      aP1 = aP0 + ps * k;
603
0
    }
604
0
  }
605
0
}
606
607
already_AddRefed<Path>
608
nsCSSBorderRenderer::GetSideClipSubPath(mozilla::Side aSide)
609
0
{
610
0
  // the clip proceeds clockwise from the top left corner;
611
0
  // so "start" in each case is the start of the region from that side.
612
0
  //
613
0
  // the final path will be formed like:
614
0
  // s0 ------- e0
615
0
  // |         /
616
0
  // s1 ----- e1
617
0
  //
618
0
  // that is, the second point will always be on the inside
619
0
620
0
  Point start[2];
621
0
  Point end[2];
622
0
623
0
#define IS_DOTTED(_s) ((_s) == NS_STYLE_BORDER_STYLE_DOTTED)
624
0
  bool isDotted = IS_DOTTED(mBorderStyles[aSide]);
625
0
  bool startIsDotted = IS_DOTTED(mBorderStyles[PREV_SIDE(aSide)]);
626
0
  bool endIsDotted = IS_DOTTED(mBorderStyles[NEXT_SIDE(aSide)]);
627
0
#undef IS_DOTTED
628
0
629
0
  SideClipType startType = SIDE_CLIP_TRAPEZOID;
630
0
  SideClipType endType = SIDE_CLIP_TRAPEZOID;
631
0
632
0
  if (!IsZeroSize(mBorderRadii[GetCCWCorner(aSide)])) {
633
0
    startType = SIDE_CLIP_TRAPEZOID_FULL;
634
0
  } else if (startIsDotted && !isDotted) {
635
0
    startType = SIDE_CLIP_RECTANGLE_CORNER;
636
0
  } else if (!startIsDotted && isDotted) {
637
0
    startType = SIDE_CLIP_RECTANGLE_NO_CORNER;
638
0
  }
639
0
640
0
  if (!IsZeroSize(mBorderRadii[GetCWCorner(aSide)])) {
641
0
    endType = SIDE_CLIP_TRAPEZOID_FULL;
642
0
  } else if (endIsDotted && !isDotted) {
643
0
    endType = SIDE_CLIP_RECTANGLE_CORNER;
644
0
  } else if (!endIsDotted && isDotted) {
645
0
    endType = SIDE_CLIP_RECTANGLE_NO_CORNER;
646
0
  }
647
0
648
0
  Point midPoint = mInnerRect.Center();
649
0
650
0
  start[0] = mOuterRect.CCWCorner(aSide);
651
0
  start[1] = mInnerRect.CCWCorner(aSide);
652
0
653
0
  end[0] = mOuterRect.CWCorner(aSide);
654
0
  end[1] = mInnerRect.CWCorner(aSide);
655
0
656
0
  if (startType == SIDE_CLIP_TRAPEZOID_FULL) {
657
0
    MaybeMoveToMidPoint(start[0], start[1], midPoint);
658
0
  } else if (startType == SIDE_CLIP_RECTANGLE_CORNER) {
659
0
    if (IsHorizontalSide(aSide)) {
660
0
      start[1] =
661
0
        Point(mOuterRect.CCWCorner(aSide).x, mInnerRect.CCWCorner(aSide).y);
662
0
    } else {
663
0
      start[1] =
664
0
        Point(mInnerRect.CCWCorner(aSide).x, mOuterRect.CCWCorner(aSide).y);
665
0
    }
666
0
  } else if (startType == SIDE_CLIP_RECTANGLE_NO_CORNER) {
667
0
    if (IsHorizontalSide(aSide)) {
668
0
      start[0] =
669
0
        Point(mInnerRect.CCWCorner(aSide).x, mOuterRect.CCWCorner(aSide).y);
670
0
    } else {
671
0
      start[0] =
672
0
        Point(mOuterRect.CCWCorner(aSide).x, mInnerRect.CCWCorner(aSide).y);
673
0
    }
674
0
  }
675
0
676
0
  if (endType == SIDE_CLIP_TRAPEZOID_FULL) {
677
0
    MaybeMoveToMidPoint(end[0], end[1], midPoint);
678
0
  } else if (endType == SIDE_CLIP_RECTANGLE_CORNER) {
679
0
    if (IsHorizontalSide(aSide)) {
680
0
      end[1] =
681
0
        Point(mOuterRect.CWCorner(aSide).x, mInnerRect.CWCorner(aSide).y);
682
0
    } else {
683
0
      end[1] =
684
0
        Point(mInnerRect.CWCorner(aSide).x, mOuterRect.CWCorner(aSide).y);
685
0
    }
686
0
  } else if (endType == SIDE_CLIP_RECTANGLE_NO_CORNER) {
687
0
    if (IsHorizontalSide(aSide)) {
688
0
      end[0] =
689
0
        Point(mInnerRect.CWCorner(aSide).x, mOuterRect.CWCorner(aSide).y);
690
0
    } else {
691
0
      end[0] =
692
0
        Point(mOuterRect.CWCorner(aSide).x, mInnerRect.CWCorner(aSide).y);
693
0
    }
694
0
  }
695
0
696
0
  RefPtr<PathBuilder> builder = mDrawTarget->CreatePathBuilder();
697
0
  builder->MoveTo(start[0]);
698
0
  builder->LineTo(end[0]);
699
0
  builder->LineTo(end[1]);
700
0
  builder->LineTo(start[1]);
701
0
  builder->Close();
702
0
  return builder->Finish();
703
0
}
704
705
Point
706
nsCSSBorderRenderer::GetStraightBorderPoint(mozilla::Side aSide,
707
                                            Corner aCorner,
708
                                            bool* aIsUnfilled,
709
                                            Float aDotOffset)
710
0
{
711
0
  // Calculate the end point of the side for dashed/dotted border, that is also
712
0
  // the end point of the corner curve.  The point is specified by aSide and
713
0
  // aCorner. (e.g. eSideTop and C_TL means the left end of border-top)
714
0
  //
715
0
  //
716
0
  //  aCorner        aSide
717
0
  //         +--------------------
718
0
  //         |
719
0
  //         |
720
0
  //         |         +----------
721
0
  //         |    the end point
722
0
  //         |
723
0
  //         |         +----------
724
0
  //         |         |
725
0
  //         |         |
726
0
  //         |         |
727
0
  //
728
0
  // The position of the point depends on the border-style, border-width, and
729
0
  // border-radius of the side, corner, and the adjacent side beyond the corner,
730
0
  // to make those sides (and corner) interact well.
731
0
  //
732
0
  // If the style of aSide is dotted and the dot at the point should be
733
0
  // unfilled, true is stored to *aIsUnfilled, otherwise false is stored.
734
0
735
0
  const Float signsList[4][2] = {
736
0
    { +1.0f, +1.0f }, { -1.0f, +1.0f }, { -1.0f, -1.0f }, { +1.0f, -1.0f }
737
0
  };
738
0
  const Float(&signs)[2] = signsList[aCorner];
739
0
740
0
  *aIsUnfilled = false;
741
0
742
0
  Point P = mOuterRect.AtCorner(aCorner);
743
0
  uint8_t style = mBorderStyles[aSide];
744
0
  Float borderWidth = mBorderWidths[aSide];
745
0
  Size dim = mBorderCornerDimensions[aCorner];
746
0
  bool isHorizontal = IsHorizontalSide(aSide);
747
0
  //
748
0
  //    aCorner      aSide
749
0
  //           +--------------
750
0
  //           |
751
0
  //           |   +----------
752
0
  //           |   |
753
0
  // otherSide |   |
754
0
  //           |   |
755
0
  mozilla::Side otherSide =
756
0
    ((uint8_t)aSide == (uint8_t)aCorner) ? PREV_SIDE(aSide) : NEXT_SIDE(aSide);
757
0
  uint8_t otherStyle = mBorderStyles[otherSide];
758
0
  Float otherBorderWidth = mBorderWidths[otherSide];
759
0
  Size radius = mBorderRadii[aCorner];
760
0
  if (IsZeroSize(radius)) {
761
0
    radius.width = 0.0f;
762
0
    radius.height = 0.0f;
763
0
  }
764
0
  if (style == NS_STYLE_BORDER_STYLE_DOTTED) {
765
0
    // Offset the dot's location along the side toward the corner by a
766
0
    // multiple of its width.
767
0
    if (isHorizontal) {
768
0
      P.x -= signs[0] * aDotOffset * borderWidth;
769
0
    } else {
770
0
      P.y -= signs[1] * aDotOffset * borderWidth;
771
0
    }
772
0
  }
773
0
  if (style == NS_STYLE_BORDER_STYLE_DOTTED &&
774
0
      otherStyle == NS_STYLE_BORDER_STYLE_DOTTED) {
775
0
    if (borderWidth == otherBorderWidth) {
776
0
      if (radius.width < borderWidth / 2.0f &&
777
0
          radius.height < borderWidth / 2.0f) {
778
0
        // Two dots are merged into one and placed at the corner.
779
0
        //
780
0
        //  borderWidth / 2.0
781
0
        // |<---------->|
782
0
        // |            |
783
0
        // |radius.width|
784
0
        // |<--->|      |
785
0
        // |     |      |
786
0
        // |    _+------+------------+-----
787
0
        // |  /      ###|###         |
788
0
        // |/    #######|#######     |
789
0
        // +   #########|#########   |
790
0
        // |  ##########|##########  |
791
0
        // | ###########|########### |
792
0
        // | ###########|########### |
793
0
        // |############|############|
794
0
        // +------------+############|
795
0
        // |########### P ###########|
796
0
        // | ####################### |
797
0
        // | ####################### |
798
0
        // |  #####################  |
799
0
        // |   ###################   |
800
0
        // |     ###############     |
801
0
        // |         #######         |
802
0
        // +-------------------------+----
803
0
        // |                         |
804
0
        // |                         |
805
0
        P.x += signs[0] * borderWidth / 2.0f;
806
0
        P.y += signs[1] * borderWidth / 2.0f;
807
0
      } else {
808
0
        // Two dots are drawn separately.
809
0
        //
810
0
        //    borderWidth * 1.5
811
0
        //   |<------------>|
812
0
        //   |              |
813
0
        //   |radius.width  |
814
0
        //   |<----->|      |
815
0
        //   |       |      |
816
0
        //   |    _--+-+----+---
817
0
        //   |  _-     |  ##|##
818
0
        //   | /       | ###|###
819
0
        //   |/        |####|####
820
0
        //   |         |####+####
821
0
        //   |         |### P ###
822
0
        //   +         | ###|###
823
0
        //   |         |  ##|##
824
0
        //   +---------+----+---
825
0
        //   |  #####  |
826
0
        //   | ####### |
827
0
        //   |#########|
828
0
        //   +----+----+
829
0
        //   |#########|
830
0
        //   | ####### |
831
0
        //   |  #####  |
832
0
        //   |         |
833
0
        //
834
0
        // There should be enough gap between 2 dots even if radius.width is
835
0
        // small but larger than borderWidth / 2.0.  borderWidth * 1.5 is the
836
0
        // value that there's imaginally unfilled dot at the corner.  The
837
0
        // unfilled dot may overflow from the outer curve, but filled dots
838
0
        // doesn't, so this could be acceptable solution at least for now.
839
0
        // We may have to find better model/value.
840
0
        //
841
0
        //    imaginally unfilled dot at the corner
842
0
        //        |
843
0
        //        v    +----+---
844
0
        //      *****  |  ##|##
845
0
        //     ******* | ###|###
846
0
        //    *********|####|####
847
0
        //    *********|####+####
848
0
        //    *********|### P ###
849
0
        //     ******* | ###|###
850
0
        //      *****  |  ##|##
851
0
        //   +---------+----+---
852
0
        //   |  #####  |
853
0
        //   | ####### |
854
0
        //   |#########|
855
0
        //   +----+----+
856
0
        //   |#########|
857
0
        //   | ####### |
858
0
        //   |  #####  |
859
0
        //   |         |
860
0
        Float minimum = borderWidth * 1.5f;
861
0
        if (isHorizontal) {
862
0
          P.x += signs[0] * std::max(radius.width, minimum);
863
0
          P.y += signs[1] * borderWidth / 2.0f;
864
0
        } else {
865
0
          P.x += signs[0] * borderWidth / 2.0f;
866
0
          P.y += signs[1] * std::max(radius.height, minimum);
867
0
        }
868
0
      }
869
0
870
0
      return P;
871
0
    }
872
0
873
0
    if (borderWidth < otherBorderWidth) {
874
0
      // This side is smaller than other side, other side draws the corner.
875
0
      //
876
0
      //  otherBorderWidth + borderWidth / 2.0
877
0
      // |<---------->|
878
0
      // |            |
879
0
      // +---------+--+--------
880
0
      // |  #####  | *|*  ###
881
0
      // | ####### |**|**#####
882
0
      // |#########|**+**##+##
883
0
      // |####+####|* P *#####
884
0
      // |#########| ***  ###
885
0
      // | ####### +-----------
886
0
      // |  #####  |  ^
887
0
      // |         |  |
888
0
      // |         | first dot is not filled
889
0
      // |         |
890
0
      //
891
0
      //      radius.width
892
0
      // |<----------------->|
893
0
      // |                   |
894
0
      // |             ___---+-------------
895
0
      // |         __--     #|#       ###
896
0
      // |       _-        ##|##     #####
897
0
      // |     /           ##+##     ##+##
898
0
      // |   /             # P #     #####
899
0
      // |  |               #|#       ###
900
0
      // | |             __--+-------------
901
0
      // ||            _-    ^
902
0
      // ||           /      |
903
0
      // |           /      first dot is filled
904
0
      // |          |
905
0
      // |          |
906
0
      // |  #####  |
907
0
      // | ####### |
908
0
      // |#########|
909
0
      // +----+----+
910
0
      // |#########|
911
0
      // | ####### |
912
0
      // |  #####  |
913
0
      Float minimum = otherBorderWidth + borderWidth / 2.0f;
914
0
      if (isHorizontal) {
915
0
        if (radius.width < minimum) {
916
0
          *aIsUnfilled = true;
917
0
          P.x += signs[0] * minimum;
918
0
        } else {
919
0
          P.x += signs[0] * radius.width;
920
0
        }
921
0
        P.y += signs[1] * borderWidth / 2.0f;
922
0
      } else {
923
0
        P.x += signs[0] * borderWidth / 2.0f;
924
0
        if (radius.height < minimum) {
925
0
          *aIsUnfilled = true;
926
0
          P.y += signs[1] * minimum;
927
0
        } else {
928
0
          P.y += signs[1] * radius.height;
929
0
        }
930
0
      }
931
0
932
0
      return P;
933
0
    }
934
0
935
0
    // This side is larger than other side, this side draws the corner.
936
0
    //
937
0
    //  borderWidth / 2.0
938
0
    // |<-->|
939
0
    // |    |
940
0
    // +----+---------------------
941
0
    // |  ##|##           #####
942
0
    // | ###|###         #######
943
0
    // |####|####       #########
944
0
    // |####+####       ####+####
945
0
    // |### P ###       #########
946
0
    // | #######         #######
947
0
    // |  #####           #####
948
0
    // +-----+---------------------
949
0
    // | *** |
950
0
    // |*****|
951
0
    // |**+**| <-- first dot in other side is not filled
952
0
    // |*****|
953
0
    // | *** |
954
0
    // | ### |
955
0
    // |#####|
956
0
    // |##+##|
957
0
    // |#####|
958
0
    // | ### |
959
0
    // |     |
960
0
    if (isHorizontal) {
961
0
      P.x += signs[0] * std::max(radius.width, borderWidth / 2.0f);
962
0
      P.y += signs[1] * borderWidth / 2.0f;
963
0
    } else {
964
0
      P.x += signs[0] * borderWidth / 2.0f;
965
0
      P.y += signs[1] * std::max(radius.height, borderWidth / 2.0f);
966
0
    }
967
0
    return P;
968
0
  }
969
0
970
0
  if (style == NS_STYLE_BORDER_STYLE_DOTTED) {
971
0
    // If only this side is dotted, other side draws the corner.
972
0
    //
973
0
    //  otherBorderWidth + borderWidth / 2.0
974
0
    // |<------->|
975
0
    // |         |
976
0
    // +------+--+--------
977
0
    // |##  ##| *|*  ###
978
0
    // |##  ##|**|**#####
979
0
    // |##  ##|**+**##+##
980
0
    // |##  ##|* P *#####
981
0
    // |##  ##| ***  ###
982
0
    // |##  ##+-----------
983
0
    // |##  ##|  ^
984
0
    // |##  ##|  |
985
0
    // |##  ##| first dot is not filled
986
0
    // |##  ##|
987
0
    //
988
0
    //      radius.width
989
0
    // |<----------------->|
990
0
    // |                   |
991
0
    // |             ___---+-------------
992
0
    // |         __--     #|#       ###
993
0
    // |       _-        ##|##     #####
994
0
    // |     /           ##+##     ##+##
995
0
    // |   /             # P #     #####
996
0
    // |  |               #|#       ###
997
0
    // | |             __--+-------------
998
0
    // ||            _-    ^
999
0
    // ||          /       |
1000
0
    // |         /        first dot is filled
1001
0
    // |        |
1002
0
    // |       |
1003
0
    // |      |
1004
0
    // |      |
1005
0
    // |      |
1006
0
    // +------+
1007
0
    // |##  ##|
1008
0
    // |##  ##|
1009
0
    // |##  ##|
1010
0
    Float minimum = otherBorderWidth + borderWidth / 2.0f;
1011
0
    if (isHorizontal) {
1012
0
      if (radius.width < minimum) {
1013
0
        *aIsUnfilled = true;
1014
0
        P.x += signs[0] * minimum;
1015
0
      } else {
1016
0
        P.x += signs[0] * radius.width;
1017
0
      }
1018
0
      P.y += signs[1] * borderWidth / 2.0f;
1019
0
    } else {
1020
0
      P.x += signs[0] * borderWidth / 2.0f;
1021
0
      if (radius.height < minimum) {
1022
0
        *aIsUnfilled = true;
1023
0
        P.y += signs[1] * minimum;
1024
0
      } else {
1025
0
        P.y += signs[1] * radius.height;
1026
0
      }
1027
0
    }
1028
0
    return P;
1029
0
  }
1030
0
1031
0
  if (otherStyle == NS_STYLE_BORDER_STYLE_DOTTED && IsZeroSize(radius)) {
1032
0
    // If other side is dotted and radius=0, draw side to the end of corner.
1033
0
    //
1034
0
    //   +-------------------------------
1035
0
    //   |##########          ##########
1036
0
    // P +##########          ##########
1037
0
    //   |##########          ##########
1038
0
    //   +-----+-------------------------
1039
0
    //   | *** |
1040
0
    //   |*****|
1041
0
    //   |**+**| <-- first dot in other side is not filled
1042
0
    //   |*****|
1043
0
    //   | *** |
1044
0
    //   | ### |
1045
0
    //   |#####|
1046
0
    //   |##+##|
1047
0
    //   |#####|
1048
0
    //   | ### |
1049
0
    //   |     |
1050
0
    if (isHorizontal) {
1051
0
      P.y += signs[1] * borderWidth / 2.0f;
1052
0
    } else {
1053
0
      P.x += signs[0] * borderWidth / 2.0f;
1054
0
    }
1055
0
    return P;
1056
0
  }
1057
0
1058
0
  // Other cases.
1059
0
  //
1060
0
  //  dim.width
1061
0
  // |<----------------->|
1062
0
  // |                   |
1063
0
  // |             ___---+------------------
1064
0
  // |         __--      |#######        ###
1065
0
  // |       _-        P +#######        ###
1066
0
  // |     /             |#######        ###
1067
0
  // |   /          __---+------------------
1068
0
  // |  |       __--
1069
0
  // | |       /
1070
0
  // ||      /
1071
0
  // ||     |
1072
0
  // |     |
1073
0
  // |    |
1074
0
  // |   |
1075
0
  // |   |
1076
0
  // +-+-+
1077
0
  // |###|
1078
0
  // |###|
1079
0
  // |###|
1080
0
  // |###|
1081
0
  // |###|
1082
0
  // |   |
1083
0
  // |   |
1084
0
  if (isHorizontal) {
1085
0
    P.x += signs[0] * dim.width;
1086
0
    P.y += signs[1] * borderWidth / 2.0f;
1087
0
  } else {
1088
0
    P.x += signs[0] * borderWidth / 2.0f;
1089
0
    P.y += signs[1] * dim.height;
1090
0
  }
1091
0
1092
0
  return P;
1093
0
}
1094
1095
void
1096
nsCSSBorderRenderer::GetOuterAndInnerBezier(Bezier* aOuterBezier,
1097
                                            Bezier* aInnerBezier,
1098
                                            Corner aCorner)
1099
0
{
1100
0
  // Return bezier control points for outer and inner curve for given corner.
1101
0
  //
1102
0
  //               ___---+ outer curve
1103
0
  //           __--      |
1104
0
  //         _-          |
1105
0
  //       /             |
1106
0
  //     /               |
1107
0
  //    |                |
1108
0
  //   |             __--+ inner curve
1109
0
  //  |            _-
1110
0
  //  |           /
1111
0
  // |           /
1112
0
  // |          |
1113
0
  // |          |
1114
0
  // |         |
1115
0
  // |         |
1116
0
  // |         |
1117
0
  // +---------+
1118
0
1119
0
  mozilla::Side sideH(GetHorizontalSide(aCorner));
1120
0
  mozilla::Side sideV(GetVerticalSide(aCorner));
1121
0
1122
0
  Size outerCornerSize(ceil(mBorderRadii[aCorner].width),
1123
0
                       ceil(mBorderRadii[aCorner].height));
1124
0
  Size innerCornerSize(
1125
0
    ceil(std::max(0.0f, mBorderRadii[aCorner].width - mBorderWidths[sideV])),
1126
0
    ceil(std::max(0.0f, mBorderRadii[aCorner].height - mBorderWidths[sideH])));
1127
0
1128
0
  GetBezierPointsForCorner(
1129
0
    aOuterBezier, aCorner, mOuterRect.AtCorner(aCorner), outerCornerSize);
1130
0
1131
0
  GetBezierPointsForCorner(
1132
0
    aInnerBezier, aCorner, mInnerRect.AtCorner(aCorner), innerCornerSize);
1133
0
}
1134
1135
void
1136
nsCSSBorderRenderer::FillSolidBorder(const Rect& aOuterRect,
1137
                                     const Rect& aInnerRect,
1138
                                     const RectCornerRadii& aBorderRadii,
1139
                                     const Float* aBorderSizes,
1140
                                     int aSides,
1141
                                     const ColorPattern& aColor)
1142
0
{
1143
0
  // Note that this function is allowed to draw more than just the
1144
0
  // requested sides.
1145
0
1146
0
  // If we have a border radius, do full rounded rectangles
1147
0
  // and fill, regardless of what sides we're asked to draw.
1148
0
  if (!AllCornersZeroSize(aBorderRadii)) {
1149
0
    RefPtr<PathBuilder> builder = mDrawTarget->CreatePathBuilder();
1150
0
1151
0
    RectCornerRadii innerRadii;
1152
0
    ComputeInnerRadii(aBorderRadii, aBorderSizes, &innerRadii);
1153
0
1154
0
    // do the outer border
1155
0
    AppendRoundedRectToPath(builder, aOuterRect, aBorderRadii, true);
1156
0
1157
0
    // then do the inner border CCW
1158
0
    AppendRoundedRectToPath(builder, aInnerRect, innerRadii, false);
1159
0
1160
0
    RefPtr<Path> path = builder->Finish();
1161
0
1162
0
    mDrawTarget->Fill(path, aColor);
1163
0
    return;
1164
0
  }
1165
0
1166
0
  // If we're asked to draw all sides of an equal-sized border,
1167
0
  // stroking is fastest.  This is a fairly common path, but partial
1168
0
  // sides is probably second in the list -- there are a bunch of
1169
0
  // common border styles, such as inset and outset, that are
1170
0
  // top-left/bottom-right split.
1171
0
  if (aSides == eSideBitsAll &&
1172
0
      CheckFourFloatsEqual(aBorderSizes, aBorderSizes[0]) && !mAvoidStroke) {
1173
0
    Float strokeWidth = aBorderSizes[0];
1174
0
    Rect r(aOuterRect);
1175
0
    r.Deflate(strokeWidth / 2.f);
1176
0
    mDrawTarget->StrokeRect(r, aColor, StrokeOptions(strokeWidth));
1177
0
    return;
1178
0
  }
1179
0
1180
0
  // Otherwise, we have unequal sized borders or we're only
1181
0
  // drawing some sides; create rectangles for each side
1182
0
  // and fill them.
1183
0
1184
0
  Rect r[4];
1185
0
1186
0
  // compute base rects for each side
1187
0
  if (aSides & eSideBitsTop) {
1188
0
    r[eSideTop] = Rect(aOuterRect.X(),
1189
0
                       aOuterRect.Y(),
1190
0
                       aOuterRect.Width(),
1191
0
                       aBorderSizes[eSideTop]);
1192
0
  }
1193
0
1194
0
  if (aSides & eSideBitsBottom) {
1195
0
    r[eSideBottom] = Rect(aOuterRect.X(),
1196
0
                          aOuterRect.YMost() - aBorderSizes[eSideBottom],
1197
0
                          aOuterRect.Width(),
1198
0
                          aBorderSizes[eSideBottom]);
1199
0
  }
1200
0
1201
0
  if (aSides & eSideBitsLeft) {
1202
0
    r[eSideLeft] = Rect(aOuterRect.X(),
1203
0
                        aOuterRect.Y(),
1204
0
                        aBorderSizes[eSideLeft],
1205
0
                        aOuterRect.Height());
1206
0
  }
1207
0
1208
0
  if (aSides & eSideBitsRight) {
1209
0
    r[eSideRight] = Rect(aOuterRect.XMost() - aBorderSizes[eSideRight],
1210
0
                         aOuterRect.Y(),
1211
0
                         aBorderSizes[eSideRight],
1212
0
                         aOuterRect.Height());
1213
0
  }
1214
0
1215
0
  // If two sides meet at a corner that we're rendering, then
1216
0
  // make sure that we adjust one of the sides to avoid overlap.
1217
0
  // This is especially important in the case of colors with
1218
0
  // an alpha channel.
1219
0
1220
0
  if ((aSides & (eSideBitsTop | eSideBitsLeft)) ==
1221
0
      (eSideBitsTop | eSideBitsLeft)) {
1222
0
    // adjust the left's top down a bit
1223
0
    r[eSideLeft].y += aBorderSizes[eSideTop];
1224
0
    r[eSideLeft].height -= aBorderSizes[eSideTop];
1225
0
  }
1226
0
1227
0
  if ((aSides & (eSideBitsTop | eSideBitsRight)) ==
1228
0
      (eSideBitsTop | eSideBitsRight)) {
1229
0
    // adjust the top's left a bit
1230
0
    r[eSideTop].width -= aBorderSizes[eSideRight];
1231
0
  }
1232
0
1233
0
  if ((aSides & (eSideBitsBottom | eSideBitsRight)) ==
1234
0
      (eSideBitsBottom | eSideBitsRight)) {
1235
0
    // adjust the right's bottom a bit
1236
0
    r[eSideRight].height -= aBorderSizes[eSideBottom];
1237
0
  }
1238
0
1239
0
  if ((aSides & (eSideBitsBottom | eSideBitsLeft)) ==
1240
0
      (eSideBitsBottom | eSideBitsLeft)) {
1241
0
    // adjust the bottom's left a bit
1242
0
    r[eSideBottom].x += aBorderSizes[eSideLeft];
1243
0
    r[eSideBottom].width -= aBorderSizes[eSideLeft];
1244
0
  }
1245
0
1246
0
  // Filling these one by one is faster than filling them all at once.
1247
0
  for (uint32_t i = 0; i < 4; i++) {
1248
0
    if (aSides & (1 << i)) {
1249
0
      MaybeSnapToDevicePixels(r[i], *mDrawTarget, true);
1250
0
      mDrawTarget->FillRect(r[i], aColor);
1251
0
    }
1252
0
  }
1253
0
}
1254
1255
Color
1256
MakeBorderColor(nscolor aColor,
1257
                nscolor aBackgroundColor,
1258
                BorderColorStyle aBorderColorStyle)
1259
{
1260
  nscolor colors[2];
1261
  int k = 0;
1262
1263
  switch (aBorderColorStyle) {
1264
    case BorderColorStyleNone:
1265
      return Color(0.f, 0.f, 0.f, 0.f); // transparent black
1266
1267
    case BorderColorStyleLight:
1268
      k = 1;
1269
      MOZ_FALLTHROUGH;
1270
    case BorderColorStyleDark:
1271
      NS_GetSpecial3DColors(colors, aBackgroundColor, aColor);
1272
      return Color::FromABGR(colors[k]);
1273
1274
    case BorderColorStyleSolid:
1275
    default:
1276
      return Color::FromABGR(aColor);
1277
  }
1278
}
1279
1280
Color
1281
ComputeColorForLine(uint32_t aLineIndex,
1282
                    const BorderColorStyle* aBorderColorStyle,
1283
                    uint32_t aBorderColorStyleCount,
1284
                    nscolor aBorderColor,
1285
                    nscolor aBackgroundColor)
1286
0
{
1287
0
  NS_ASSERTION(aLineIndex < aBorderColorStyleCount, "Invalid lineIndex given");
1288
0
1289
0
  return MakeBorderColor(
1290
0
    aBorderColor, aBackgroundColor, aBorderColorStyle[aLineIndex]);
1291
0
}
1292
1293
void
1294
nsCSSBorderRenderer::DrawBorderSides(int aSides)
1295
0
{
1296
0
  if (aSides == 0 || (aSides & ~eSideBitsAll) != 0) {
1297
0
    NS_WARNING("DrawBorderSides: invalid sides!");
1298
0
    return;
1299
0
  }
1300
0
1301
0
  uint8_t borderRenderStyle = NS_STYLE_BORDER_STYLE_NONE;
1302
0
  nscolor borderRenderColor;
1303
0
1304
0
  uint32_t borderColorStyleCount = 0;
1305
0
  BorderColorStyle borderColorStyleTopLeft[3], borderColorStyleBottomRight[3];
1306
0
  BorderColorStyle* borderColorStyle = nullptr;
1307
0
1308
0
  NS_FOR_CSS_SIDES(i)
1309
0
  {
1310
0
    if ((aSides & (1 << i)) == 0)
1311
0
      continue;
1312
0
    borderRenderStyle = mBorderStyles[i];
1313
0
    borderRenderColor = mBorderColors[i];
1314
0
    break;
1315
0
  }
1316
0
1317
0
  if (borderRenderStyle == NS_STYLE_BORDER_STYLE_NONE ||
1318
0
      borderRenderStyle == NS_STYLE_BORDER_STYLE_HIDDEN)
1319
0
    return;
1320
0
1321
0
  if (borderRenderStyle == NS_STYLE_BORDER_STYLE_DASHED ||
1322
0
      borderRenderStyle == NS_STYLE_BORDER_STYLE_DOTTED) {
1323
0
    // Draw each corner separately, with the given side's color.
1324
0
    if (aSides & eSideBitsTop) {
1325
0
      DrawDashedOrDottedCorner(eSideTop, C_TL);
1326
0
    } else if (aSides & eSideBitsLeft) {
1327
0
      DrawDashedOrDottedCorner(eSideLeft, C_TL);
1328
0
    }
1329
0
1330
0
    if (aSides & eSideBitsTop) {
1331
0
      DrawDashedOrDottedCorner(eSideTop, C_TR);
1332
0
    } else if (aSides & eSideBitsRight) {
1333
0
      DrawDashedOrDottedCorner(eSideRight, C_TR);
1334
0
    }
1335
0
1336
0
    if (aSides & eSideBitsBottom) {
1337
0
      DrawDashedOrDottedCorner(eSideBottom, C_BL);
1338
0
    } else if (aSides & eSideBitsLeft) {
1339
0
      DrawDashedOrDottedCorner(eSideLeft, C_BL);
1340
0
    }
1341
0
1342
0
    if (aSides & eSideBitsBottom) {
1343
0
      DrawDashedOrDottedCorner(eSideBottom, C_BR);
1344
0
    } else if (aSides & eSideBitsRight) {
1345
0
      DrawDashedOrDottedCorner(eSideRight, C_BR);
1346
0
    }
1347
0
    return;
1348
0
  }
1349
0
1350
0
  // The borderColorStyle array goes from the outer to the inner style.
1351
0
  //
1352
0
  // If the border width is 1, we need to change the borderRenderStyle
1353
0
  // a bit to make sure that we get the right colors -- e.g. 'ridge'
1354
0
  // with a 1px border needs to look like solid, not like 'outset'.
1355
0
  if (mOneUnitBorder && (borderRenderStyle == NS_STYLE_BORDER_STYLE_RIDGE ||
1356
0
                         borderRenderStyle == NS_STYLE_BORDER_STYLE_GROOVE ||
1357
0
                         borderRenderStyle == NS_STYLE_BORDER_STYLE_DOUBLE))
1358
0
    borderRenderStyle = NS_STYLE_BORDER_STYLE_SOLID;
1359
0
1360
0
  switch (borderRenderStyle) {
1361
0
    case NS_STYLE_BORDER_STYLE_SOLID:
1362
0
      borderColorStyleTopLeft[0] = BorderColorStyleSolid;
1363
0
1364
0
      borderColorStyleBottomRight[0] = BorderColorStyleSolid;
1365
0
1366
0
      borderColorStyleCount = 1;
1367
0
      break;
1368
0
1369
0
    case NS_STYLE_BORDER_STYLE_GROOVE:
1370
0
      borderColorStyleTopLeft[0] = BorderColorStyleDark;
1371
0
      borderColorStyleTopLeft[1] = BorderColorStyleLight;
1372
0
1373
0
      borderColorStyleBottomRight[0] = BorderColorStyleLight;
1374
0
      borderColorStyleBottomRight[1] = BorderColorStyleDark;
1375
0
1376
0
      borderColorStyleCount = 2;
1377
0
      break;
1378
0
1379
0
    case NS_STYLE_BORDER_STYLE_RIDGE:
1380
0
      borderColorStyleTopLeft[0] = BorderColorStyleLight;
1381
0
      borderColorStyleTopLeft[1] = BorderColorStyleDark;
1382
0
1383
0
      borderColorStyleBottomRight[0] = BorderColorStyleDark;
1384
0
      borderColorStyleBottomRight[1] = BorderColorStyleLight;
1385
0
1386
0
      borderColorStyleCount = 2;
1387
0
      break;
1388
0
1389
0
    case NS_STYLE_BORDER_STYLE_DOUBLE:
1390
0
      borderColorStyleTopLeft[0] = BorderColorStyleSolid;
1391
0
      borderColorStyleTopLeft[1] = BorderColorStyleNone;
1392
0
      borderColorStyleTopLeft[2] = BorderColorStyleSolid;
1393
0
1394
0
      borderColorStyleBottomRight[0] = BorderColorStyleSolid;
1395
0
      borderColorStyleBottomRight[1] = BorderColorStyleNone;
1396
0
      borderColorStyleBottomRight[2] = BorderColorStyleSolid;
1397
0
1398
0
      borderColorStyleCount = 3;
1399
0
      break;
1400
0
1401
0
    case NS_STYLE_BORDER_STYLE_INSET:
1402
0
      borderColorStyleTopLeft[0] = BorderColorStyleDark;
1403
0
      borderColorStyleBottomRight[0] = BorderColorStyleLight;
1404
0
1405
0
      borderColorStyleCount = 1;
1406
0
      break;
1407
0
1408
0
    case NS_STYLE_BORDER_STYLE_OUTSET:
1409
0
      borderColorStyleTopLeft[0] = BorderColorStyleLight;
1410
0
      borderColorStyleBottomRight[0] = BorderColorStyleDark;
1411
0
1412
0
      borderColorStyleCount = 1;
1413
0
      break;
1414
0
1415
0
    default:
1416
0
      MOZ_ASSERT_UNREACHABLE("Unhandled border style!!");
1417
0
      break;
1418
0
  }
1419
0
1420
0
  // The only way to get to here is by having a
1421
0
  // borderColorStyleCount < 1 or > 3; this should never happen,
1422
0
  // since -moz-border-colors doesn't get handled here.
1423
0
  NS_ASSERTION(borderColorStyleCount > 0 && borderColorStyleCount < 4,
1424
0
               "Non-border-colors case with borderColorStyleCount < 1 or > 3; "
1425
0
               "what happened?");
1426
0
1427
0
  // The caller should never give us anything with a mix
1428
0
  // of TL/BR if the border style would require a
1429
0
  // TL/BR split.
1430
0
  if (aSides & (eSideBitsBottom | eSideBitsRight))
1431
0
    borderColorStyle = borderColorStyleBottomRight;
1432
0
  else
1433
0
    borderColorStyle = borderColorStyleTopLeft;
1434
0
1435
0
  // Distribute the border across the available space.
1436
0
  Float borderWidths[3][4];
1437
0
1438
0
  if (borderColorStyleCount == 1) {
1439
0
    NS_FOR_CSS_SIDES(i) { borderWidths[0][i] = mBorderWidths[i]; }
1440
0
  } else if (borderColorStyleCount == 2) {
1441
0
    // with 2 color styles, any extra pixel goes to the outside
1442
0
    NS_FOR_CSS_SIDES(i)
1443
0
    {
1444
0
      borderWidths[0][i] =
1445
0
        int32_t(mBorderWidths[i]) / 2 + int32_t(mBorderWidths[i]) % 2;
1446
0
      borderWidths[1][i] = int32_t(mBorderWidths[i]) / 2;
1447
0
    }
1448
0
  } else if (borderColorStyleCount == 3) {
1449
0
    // with 3 color styles, any extra pixel (or lack of extra pixel)
1450
0
    // goes to the middle
1451
0
    NS_FOR_CSS_SIDES(i)
1452
0
    {
1453
0
      if (mBorderWidths[i] == 1.0) {
1454
0
        borderWidths[0][i] = 1.f;
1455
0
        borderWidths[1][i] = borderWidths[2][i] = 0.f;
1456
0
      } else {
1457
0
        int32_t rest = int32_t(mBorderWidths[i]) % 3;
1458
0
        borderWidths[0][i] = borderWidths[2][i] = borderWidths[1][i] =
1459
0
          (int32_t(mBorderWidths[i]) - rest) / 3;
1460
0
1461
0
        if (rest == 1) {
1462
0
          borderWidths[1][i] += 1.f;
1463
0
        } else if (rest == 2) {
1464
0
          borderWidths[0][i] += 1.f;
1465
0
          borderWidths[2][i] += 1.f;
1466
0
        }
1467
0
      }
1468
0
    }
1469
0
  }
1470
0
1471
0
  // make a copy that we can modify
1472
0
  RectCornerRadii radii = mBorderRadii;
1473
0
1474
0
  Rect soRect(mOuterRect);
1475
0
  Rect siRect(mOuterRect);
1476
0
1477
0
  // If adjacent side is dotted and radius=0, draw side to the end of corner.
1478
0
  //
1479
0
  // +--------------------------------
1480
0
  // |################################
1481
0
  // |
1482
0
  // |################################
1483
0
  // +-----+--------------------------
1484
0
  // |     |
1485
0
  // |     |
1486
0
  // |     |
1487
0
  // |     |
1488
0
  // |     |
1489
0
  // | ### |
1490
0
  // |#####|
1491
0
  // |#####|
1492
0
  // |#####|
1493
0
  // | ### |
1494
0
  // |     |
1495
0
  bool noMarginTop = false;
1496
0
  bool noMarginRight = false;
1497
0
  bool noMarginBottom = false;
1498
0
  bool noMarginLeft = false;
1499
0
1500
0
  // If there is at least one dotted side, every side is rendered separately.
1501
0
  if (IsSingleSide(aSides)) {
1502
0
    if (aSides == eSideBitsTop) {
1503
0
      if (mBorderStyles[eSideRight] == NS_STYLE_BORDER_STYLE_DOTTED &&
1504
0
          IsZeroSize(mBorderRadii[C_TR])) {
1505
0
        noMarginRight = true;
1506
0
      }
1507
0
      if (mBorderStyles[eSideLeft] == NS_STYLE_BORDER_STYLE_DOTTED &&
1508
0
          IsZeroSize(mBorderRadii[C_TL])) {
1509
0
        noMarginLeft = true;
1510
0
      }
1511
0
    } else if (aSides == eSideBitsRight) {
1512
0
      if (mBorderStyles[eSideTop] == NS_STYLE_BORDER_STYLE_DOTTED &&
1513
0
          IsZeroSize(mBorderRadii[C_TR])) {
1514
0
        noMarginTop = true;
1515
0
      }
1516
0
      if (mBorderStyles[eSideBottom] == NS_STYLE_BORDER_STYLE_DOTTED &&
1517
0
          IsZeroSize(mBorderRadii[C_BR])) {
1518
0
        noMarginBottom = true;
1519
0
      }
1520
0
    } else if (aSides == eSideBitsBottom) {
1521
0
      if (mBorderStyles[eSideRight] == NS_STYLE_BORDER_STYLE_DOTTED &&
1522
0
          IsZeroSize(mBorderRadii[C_BR])) {
1523
0
        noMarginRight = true;
1524
0
      }
1525
0
      if (mBorderStyles[eSideLeft] == NS_STYLE_BORDER_STYLE_DOTTED &&
1526
0
          IsZeroSize(mBorderRadii[C_BL])) {
1527
0
        noMarginLeft = true;
1528
0
      }
1529
0
    } else {
1530
0
      if (mBorderStyles[eSideTop] == NS_STYLE_BORDER_STYLE_DOTTED &&
1531
0
          IsZeroSize(mBorderRadii[C_TL])) {
1532
0
        noMarginTop = true;
1533
0
      }
1534
0
      if (mBorderStyles[eSideBottom] == NS_STYLE_BORDER_STYLE_DOTTED &&
1535
0
          IsZeroSize(mBorderRadii[C_BL])) {
1536
0
        noMarginBottom = true;
1537
0
      }
1538
0
    }
1539
0
  }
1540
0
1541
0
  for (unsigned int i = 0; i < borderColorStyleCount; i++) {
1542
0
    // walk siRect inwards at the start of the loop to get the
1543
0
    // correct inner rect.
1544
0
    //
1545
0
    // If noMarginTop is false:
1546
0
    //   --------------------+
1547
0
    //                      /|
1548
0
    //                     / |
1549
0
    //                    L  |
1550
0
    //   ----------------+   |
1551
0
    //                   |   |
1552
0
    //                   |   |
1553
0
    //
1554
0
    // If noMarginTop is true:
1555
0
    //   ----------------+<--+
1556
0
    //                   |   |
1557
0
    //                   |   |
1558
0
    //                   |   |
1559
0
    //                   |   |
1560
0
    //                   |   |
1561
0
    //                   |   |
1562
0
    siRect.Deflate(Margin(noMarginTop ? 0 : borderWidths[i][0],
1563
0
                          noMarginRight ? 0 : borderWidths[i][1],
1564
0
                          noMarginBottom ? 0 : borderWidths[i][2],
1565
0
                          noMarginLeft ? 0 : borderWidths[i][3]));
1566
0
1567
0
    if (borderColorStyle[i] != BorderColorStyleNone) {
1568
0
      Color c = ComputeColorForLine(i,
1569
0
                                    borderColorStyle,
1570
0
                                    borderColorStyleCount,
1571
0
                                    borderRenderColor,
1572
0
                                    mBackgroundColor);
1573
0
      ColorPattern color(ToDeviceColor(c));
1574
0
1575
0
      FillSolidBorder(soRect, siRect, radii, borderWidths[i], aSides, color);
1576
0
    }
1577
0
1578
0
    ComputeInnerRadii(radii, borderWidths[i], &radii);
1579
0
1580
0
    // And now soRect is the same as siRect, for the next line in.
1581
0
    soRect = siRect;
1582
0
  }
1583
0
}
1584
1585
void
1586
nsCSSBorderRenderer::SetupDashedOptions(StrokeOptions* aStrokeOptions,
1587
                                        Float aDash[2],
1588
                                        mozilla::Side aSide,
1589
                                        Float aBorderLength,
1590
                                        bool isCorner)
1591
0
{
1592
0
  MOZ_ASSERT(mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DASHED ||
1593
0
               mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DOTTED,
1594
0
             "Style should be dashed or dotted.");
1595
0
1596
0
  uint8_t style = mBorderStyles[aSide];
1597
0
  Float borderWidth = mBorderWidths[aSide];
1598
0
1599
0
  // Dashed line starts and ends with half segment in most case.
1600
0
  //
1601
0
  // __--+---+---+---+---+---+---+---+---+--__
1602
0
  //     |###|   |   |###|###|   |   |###|
1603
0
  //     |###|   |   |###|###|   |   |###|
1604
0
  //     |###|   |   |###|###|   |   |###|
1605
0
  // __--+---+---+---+---+---+---+---+---+--__
1606
0
  //
1607
0
  // If radius=0 and other side is either dotted or 0-width, it starts or ends
1608
0
  // with full segment.
1609
0
  //
1610
0
  // +---+---+---+---+---+---+---+---+---+---+
1611
0
  // |###|###|   |   |###|###|   |   |###|###|
1612
0
  // |###|###|   |   |###|###|   |   |###|###|
1613
0
  // |###|###|   |   |###|###|   |   |###|###|
1614
0
  // +---++--+---+---+---+---+---+---+--++---+
1615
0
  // |    |                             |    |
1616
0
  // |    |                             |    |
1617
0
  // |    |                             |    |
1618
0
  // |    |                             |    |
1619
0
  // | ## |                             | ## |
1620
0
  // |####|                             |####|
1621
0
  // |####|                             |####|
1622
0
  // | ## |                             | ## |
1623
0
  // |    |                             |    |
1624
0
  bool fullStart = false, fullEnd = false;
1625
0
  Float halfDash;
1626
0
  if (style == NS_STYLE_BORDER_STYLE_DASHED) {
1627
0
    // If either end of the side is not connecting onto a corner then we want a
1628
0
    // full dash at that end.
1629
0
    //
1630
0
    // Note that in the case that a corner is empty, either the adjacent side
1631
0
    // has zero width, or else DrawBorders() set the corner to be empty
1632
0
    // (it does that if the adjacent side has zero length and the border widths
1633
0
    // of this and the adjacent sides are thin enough that the corner will be
1634
0
    // insignificantly small).
1635
0
1636
0
    if (mBorderRadii[GetCCWCorner(aSide)].IsEmpty() &&
1637
0
        (mBorderCornerDimensions[GetCCWCorner(aSide)].IsEmpty() ||
1638
0
         mBorderStyles[PREV_SIDE(aSide)] == NS_STYLE_BORDER_STYLE_DOTTED ||
1639
0
         // XXX why this <=1 check?
1640
0
         borderWidth <= 1.0f)) {
1641
0
      fullStart = true;
1642
0
    }
1643
0
1644
0
    if (mBorderRadii[GetCWCorner(aSide)].IsEmpty() &&
1645
0
        (mBorderCornerDimensions[GetCWCorner(aSide)].IsEmpty() ||
1646
0
         mBorderStyles[NEXT_SIDE(aSide)] == NS_STYLE_BORDER_STYLE_DOTTED)) {
1647
0
      fullEnd = true;
1648
0
    }
1649
0
1650
0
    halfDash = borderWidth * DOT_LENGTH * DASH_LENGTH / 2.0f;
1651
0
  } else {
1652
0
    halfDash = borderWidth * DOT_LENGTH / 2.0f;
1653
0
  }
1654
0
1655
0
  if (style == NS_STYLE_BORDER_STYLE_DASHED && aBorderLength > 0.0f) {
1656
0
    // The number of half segments, with maximum dash length.
1657
0
    int32_t count = floor(aBorderLength / halfDash);
1658
0
    Float minHalfDash = borderWidth * DOT_LENGTH / 2.0f;
1659
0
1660
0
    if (fullStart && fullEnd) {
1661
0
      // count should be 4n + 2
1662
0
      //
1663
0
      //   1 +       4       +        4      + 1
1664
0
      //
1665
0
      // |   |               |               |   |
1666
0
      // +---+---+---+---+---+---+---+---+---+---+
1667
0
      // |###|###|   |   |###|###|   |   |###|###|
1668
0
      // |###|###|   |   |###|###|   |   |###|###|
1669
0
      // |###|###|   |   |###|###|   |   |###|###|
1670
0
      // +---+---+---+---+---+---+---+---+---+---+
1671
0
1672
0
      // If border is too short, draw solid line.
1673
0
      if (aBorderLength < 6.0f * minHalfDash)
1674
0
        return;
1675
0
1676
0
      if (count % 4 == 0) {
1677
0
        count += 2;
1678
0
      } else if (count % 4 == 1) {
1679
0
        count += 1;
1680
0
      } else if (count % 4 == 3) {
1681
0
        count += 3;
1682
0
      }
1683
0
    } else if (fullStart || fullEnd) {
1684
0
      // count should be 4n + 1
1685
0
      //
1686
0
      //   1 +       4       +        4
1687
0
      //
1688
0
      // |   |               |               |
1689
0
      // +---+---+---+---+---+---+---+---+---+
1690
0
      // |###|###|   |   |###|###|   |   |###|
1691
0
      // |###|###|   |   |###|###|   |   |###|
1692
0
      // |###|###|   |   |###|###|   |   |###|
1693
0
      // +---+---+---+---+---+---+---+---+---+
1694
0
      //
1695
0
      //         4       +        4      + 1
1696
0
      //
1697
0
      // |               |               |   |
1698
0
      // +---+---+---+---+---+---+---+---+---+
1699
0
      // |###|   |   |###|###|   |   |###|###|
1700
0
      // |###|   |   |###|###|   |   |###|###|
1701
0
      // |###|   |   |###|###|   |   |###|###|
1702
0
      // +---+---+---+---+---+---+---+---+---+
1703
0
1704
0
      // If border is too short, draw solid line.
1705
0
      if (aBorderLength < 5.0f * minHalfDash)
1706
0
        return;
1707
0
1708
0
      if (count % 4 == 0) {
1709
0
        count += 1;
1710
0
      } else if (count % 4 == 2) {
1711
0
        count += 3;
1712
0
      } else if (count % 4 == 3) {
1713
0
        count += 2;
1714
0
      }
1715
0
    } else {
1716
0
      // count should be 4n
1717
0
      //
1718
0
      //         4       +        4
1719
0
      //
1720
0
      // |               |               |
1721
0
      // +---+---+---+---+---+---+---+---+
1722
0
      // |###|   |   |###|###|   |   |###|
1723
0
      // |###|   |   |###|###|   |   |###|
1724
0
      // |###|   |   |###|###|   |   |###|
1725
0
      // +---+---+---+---+---+---+---+---+
1726
0
1727
0
      // If border is too short, draw solid line.
1728
0
      if (aBorderLength < 4.0f * minHalfDash)
1729
0
        return;
1730
0
1731
0
      if (count % 4 == 1) {
1732
0
        count += 3;
1733
0
      } else if (count % 4 == 2) {
1734
0
        count += 2;
1735
0
      } else if (count % 4 == 3) {
1736
0
        count += 1;
1737
0
      }
1738
0
    }
1739
0
    halfDash = aBorderLength / count;
1740
0
  }
1741
0
1742
0
  Float fullDash = halfDash * 2.0f;
1743
0
1744
0
  aDash[0] = fullDash;
1745
0
  aDash[1] = fullDash;
1746
0
1747
0
  if (style == NS_STYLE_BORDER_STYLE_DASHED && fullDash > 1.0f) {
1748
0
    if (!fullStart) {
1749
0
      // Draw half segments on both ends.
1750
0
      aStrokeOptions->mDashOffset = halfDash;
1751
0
    }
1752
0
  } else if (style != NS_STYLE_BORDER_STYLE_DOTTED && isCorner) {
1753
0
    // If side ends with filled full segment, corner should start with unfilled
1754
0
    // full segment. Not needed for dotted corners, as they overlap one dot with
1755
0
    // the side's end.
1756
0
    //
1757
0
    //     corner            side
1758
0
    //   ------------>|<---------------------------
1759
0
    //                |
1760
0
    //          __+---+---+---+---+---+---+---+---+
1761
0
    //       _+-  |   |###|###|   |   |###|###|   |
1762
0
    //     /##|   |   |###|###|   |   |###|###|   |
1763
0
    //    +####|   |  |###|###|   |   |###|###|   |
1764
0
    //   /#\####| _+--+---+---+---+---+---+---+---+
1765
0
    //  |####\##+-
1766
0
    //  |#####+-
1767
0
    //  +--###/
1768
0
    //  |  --+
1769
0
    aStrokeOptions->mDashOffset = fullDash;
1770
0
  }
1771
0
1772
0
  aStrokeOptions->mDashPattern = aDash;
1773
0
  aStrokeOptions->mDashLength = 2;
1774
0
1775
0
  PrintAsFormatString("dash: %f %f\n", aDash[0], aDash[1]);
1776
0
}
1777
1778
static Float
1779
GetBorderLength(mozilla::Side aSide, const Point& aStart, const Point& aEnd)
1780
0
{
1781
0
  if (aSide == eSideTop) {
1782
0
    return aEnd.x - aStart.x;
1783
0
  }
1784
0
  if (aSide == eSideRight) {
1785
0
    return aEnd.y - aStart.y;
1786
0
  }
1787
0
  if (aSide == eSideBottom) {
1788
0
    return aStart.x - aEnd.x;
1789
0
  }
1790
0
  return aStart.y - aEnd.y;
1791
0
}
1792
1793
void
1794
nsCSSBorderRenderer::DrawDashedOrDottedSide(mozilla::Side aSide)
1795
0
{
1796
0
  // Draw dashed/dotted side with following approach.
1797
0
  //
1798
0
  // dashed side
1799
0
  //   Draw dashed line along the side, with appropriate dash length and gap
1800
0
  //   to make the side symmetric as far as possible.  Dash length equals to
1801
0
  //   the gap, and the ratio of the dash length to border-width is the maximum
1802
0
  //   value in in [1, 3] range.
1803
0
  //   In most case, line ends with half segment, to joint with corner easily.
1804
0
  //   If adjacent side is dotted or 0px and border-radius for the corner
1805
0
  //   between them is 0, the line ends with full segment.
1806
0
  //   (see comment for GetStraightBorderPoint for more detail)
1807
0
  //
1808
0
  // dotted side
1809
0
  //   If border-width <= 2.0, draw 1:1 dashed line.
1810
0
  //   Otherwise, draw circles along the side, with appropriate gap that makes
1811
0
  //   the side symmetric as far as possible.  The ratio of the gap to
1812
0
  //   border-width is the maximum value in [0.5, 1] range in most case.
1813
0
  //   if the side is too short and there's only 2 dots, it can be more smaller.
1814
0
  //   If there's no space to place 2 dots at the side, draw single dot at the
1815
0
  //   middle of the side.
1816
0
  //   In most case, line ends with filled dot, to joint with corner easily,
1817
0
  //   If adjacent side is dotted with larger border-width, or other style,
1818
0
  //   the line ends with unfilled dot.
1819
0
  //   (see comment for GetStraightBorderPoint for more detail)
1820
0
1821
0
  NS_ASSERTION(mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DASHED ||
1822
0
                 mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DOTTED,
1823
0
               "Style should be dashed or dotted.");
1824
0
1825
0
  Float borderWidth = mBorderWidths[aSide];
1826
0
  if (borderWidth == 0.0f) {
1827
0
    return;
1828
0
  }
1829
0
1830
0
  if (mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DOTTED &&
1831
0
      borderWidth > 2.0f) {
1832
0
    DrawDottedSideSlow(aSide);
1833
0
    return;
1834
0
  }
1835
0
1836
0
  nscolor borderColor = mBorderColors[aSide];
1837
0
  bool ignored;
1838
0
  // Get the start and end points of the side, ensuring that any dot origins get
1839
0
  // pushed outward to account for stroking.
1840
0
  Point start =
1841
0
    GetStraightBorderPoint(aSide, GetCCWCorner(aSide), &ignored, 0.5f);
1842
0
  Point end = GetStraightBorderPoint(aSide, GetCWCorner(aSide), &ignored, 0.5f);
1843
0
  if (borderWidth < 2.0f) {
1844
0
    // Round start to draw dot on each pixel.
1845
0
    if (IsHorizontalSide(aSide)) {
1846
0
      start.x = round(start.x);
1847
0
    } else {
1848
0
      start.y = round(start.y);
1849
0
    }
1850
0
  }
1851
0
1852
0
  Float borderLength = GetBorderLength(aSide, start, end);
1853
0
  if (borderLength < 0.0f) {
1854
0
    return;
1855
0
  }
1856
0
1857
0
  StrokeOptions strokeOptions(borderWidth);
1858
0
  Float dash[2];
1859
0
  SetupDashedOptions(&strokeOptions, dash, aSide, borderLength, false);
1860
0
1861
0
  // For dotted sides that can merge with their prior dotted sides, advance the
1862
0
  // dash offset to measure the distance around the combined path. This prevents
1863
0
  // two dots from bunching together at a corner.
1864
0
  mozilla::Side mergeSide = aSide;
1865
0
  while (IsCornerMergeable(GetCCWCorner(mergeSide))) {
1866
0
    mergeSide = PREV_SIDE(mergeSide);
1867
0
    // If we looped all the way around, measure starting at the top side, since
1868
0
    // we need to pick a fixed location to start measuring distance from still.
1869
0
    if (mergeSide == aSide) {
1870
0
      mergeSide = eSideTop;
1871
0
      break;
1872
0
    }
1873
0
  }
1874
0
  while (mergeSide != aSide) {
1875
0
    // Measure the length of the merged side starting from a possibly
1876
0
    // unmergeable corner up to the merged corner. A merged corner effectively
1877
0
    // has no border radius, so we can just use the cheaper AtCorner to find the
1878
0
    // end point.
1879
0
    Float mergeLength =
1880
0
      GetBorderLength(mergeSide,
1881
0
                      GetStraightBorderPoint(
1882
0
                        mergeSide, GetCCWCorner(mergeSide), &ignored, 0.5f),
1883
0
                      mOuterRect.AtCorner(GetCWCorner(mergeSide)));
1884
0
    // Add in the merged side length. Also offset the dash progress by an extra
1885
0
    // dot's width to avoid drawing a dot that would overdraw where the merged
1886
0
    // side would have ended in a gap, i.e. O_O_
1887
0
    //                                    O
1888
0
    strokeOptions.mDashOffset += mergeLength + borderWidth;
1889
0
    mergeSide = NEXT_SIDE(mergeSide);
1890
0
  }
1891
0
1892
0
  DrawOptions drawOptions;
1893
0
  if (mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DOTTED) {
1894
0
    drawOptions.mAntialiasMode = AntialiasMode::NONE;
1895
0
  }
1896
0
1897
0
  mDrawTarget->StrokeLine(start,
1898
0
                          end,
1899
0
                          ColorPattern(ToDeviceColor(borderColor)),
1900
0
                          strokeOptions,
1901
0
                          drawOptions);
1902
0
}
1903
1904
void
1905
nsCSSBorderRenderer::DrawDottedSideSlow(mozilla::Side aSide)
1906
0
{
1907
0
  // Draw each circles separately for dotted with borderWidth > 2.0.
1908
0
  // Dashed line with CapStyle::ROUND doesn't render perfect circles.
1909
0
1910
0
  NS_ASSERTION(mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DOTTED,
1911
0
               "Style should be dotted.");
1912
0
1913
0
  Float borderWidth = mBorderWidths[aSide];
1914
0
  if (borderWidth == 0.0f) {
1915
0
    return;
1916
0
  }
1917
0
1918
0
  nscolor borderColor = mBorderColors[aSide];
1919
0
  bool isStartUnfilled, isEndUnfilled;
1920
0
  Point start =
1921
0
    GetStraightBorderPoint(aSide, GetCCWCorner(aSide), &isStartUnfilled);
1922
0
  Point end = GetStraightBorderPoint(aSide, GetCWCorner(aSide), &isEndUnfilled);
1923
0
  enum
1924
0
  {
1925
0
    // Corner is not mergeable.
1926
0
    NO_MERGE,
1927
0
1928
0
    // Corner between different colors.
1929
0
    // Two dots are merged into one, and both side draw half dot.
1930
0
    MERGE_HALF,
1931
0
1932
0
    // Corner between same colors, CCW corner of the side.
1933
0
    // Two dots are merged into one, and this side draw entire dot.
1934
0
    //
1935
0
    // MERGE_ALL               MERGE_NONE
1936
0
    //   |                       |
1937
0
    //   v                       v
1938
0
    // +-----------------------+----+
1939
0
    // | ##      ##      ##    | ## |
1940
0
    // |####    ####    ####   |####|
1941
0
    // |####    ####    ####   |####|
1942
0
    // | ##      ##      ##    | ## |
1943
0
    // +----+------------------+    |
1944
0
    // |    |                  |    |
1945
0
    // |    |                  |    |
1946
0
    // |    |                  |    |
1947
0
    // | ## |                  | ## |
1948
0
    // |####|                  |####|
1949
0
    MERGE_ALL,
1950
0
1951
0
    // Corner between same colors, CW corner of the side.
1952
0
    // Two dots are merged into one, and this side doesn't draw dot.
1953
0
    MERGE_NONE
1954
0
  } mergeStart = NO_MERGE,
1955
0
    mergeEnd = NO_MERGE;
1956
0
1957
0
  if (IsCornerMergeable(GetCCWCorner(aSide))) {
1958
0
    if (borderColor == mBorderColors[PREV_SIDE(aSide)]) {
1959
0
      mergeStart = MERGE_ALL;
1960
0
    } else {
1961
0
      mergeStart = MERGE_HALF;
1962
0
    }
1963
0
  }
1964
0
1965
0
  if (IsCornerMergeable(GetCWCorner(aSide))) {
1966
0
    if (borderColor == mBorderColors[NEXT_SIDE(aSide)]) {
1967
0
      mergeEnd = MERGE_NONE;
1968
0
    } else {
1969
0
      mergeEnd = MERGE_HALF;
1970
0
    }
1971
0
  }
1972
0
1973
0
  Float borderLength = GetBorderLength(aSide, start, end);
1974
0
  if (borderLength < 0.0f) {
1975
0
    if (isStartUnfilled || isEndUnfilled) {
1976
0
      return;
1977
0
    }
1978
0
    borderLength = 0.0f;
1979
0
    start = end = (start + end) / 2.0f;
1980
0
  }
1981
0
1982
0
  Float dotWidth = borderWidth * DOT_LENGTH;
1983
0
  Float radius = borderWidth / 2.0f;
1984
0
  if (borderLength < dotWidth) {
1985
0
    // If dots on start and end may overlap, draw a dot at the middle of them.
1986
0
    //
1987
0
    //     ___---+-------+---___
1988
0
    // __--      | ##### |      --__
1989
0
    //          #|#######|#
1990
0
    //         ##|#######|##
1991
0
    //        ###|#######|###
1992
0
    //        ###+###+###+###
1993
0
    //         start ## end #
1994
0
    //         ##|#######|##
1995
0
    //          #|#######|#
1996
0
    //           | ##### |
1997
0
    //       __--+-------+--__
1998
0
    //     _-                 -_
1999
0
    //
2000
0
    // If that circle overflows from outer rect, do not draw it.
2001
0
    //
2002
0
    //           +-------+
2003
0
    //           | ##### |
2004
0
    //          #|#######|#
2005
0
    //         ##|#######|##
2006
0
    //        ###|#######|###
2007
0
    //        ###|###+###|###
2008
0
    //        ###|#######|###
2009
0
    //         ##|#######|##
2010
0
    //          #|#######|#
2011
0
    //           | ##### |
2012
0
    //           +--+-+--+
2013
0
    //           |  | |  |
2014
0
    //           |  | |  |
2015
0
    if (!mOuterRect.Contains(
2016
0
          Rect(start.x - radius, start.y - radius, borderWidth, borderWidth))) {
2017
0
      return;
2018
0
    }
2019
0
2020
0
    if (isStartUnfilled || isEndUnfilled) {
2021
0
      return;
2022
0
    }
2023
0
2024
0
    Point P = (start + end) / 2;
2025
0
    RefPtr<PathBuilder> builder = mDrawTarget->CreatePathBuilder();
2026
0
    builder->MoveTo(Point(P.x + radius, P.y));
2027
0
    builder->Arc(P, radius, 0.0f, Float(2.0 * M_PI));
2028
0
    RefPtr<Path> path = builder->Finish();
2029
0
    mDrawTarget->Fill(path, ColorPattern(ToDeviceColor(borderColor)));
2030
0
    return;
2031
0
  }
2032
0
2033
0
  if (mergeStart == MERGE_HALF || mergeEnd == MERGE_HALF) {
2034
0
    // MERGE_HALF
2035
0
    //               Eo
2036
0
    //   -------+----+
2037
0
    //        ##### /
2038
0
    //       ######/
2039
0
    //      ######/
2040
0
    //      ####+
2041
0
    //      ##/ end
2042
0
    //       /
2043
0
    //      /
2044
0
    //   --+
2045
0
    //     Ei
2046
0
    //
2047
0
    // other (NO_MERGE, MERGE_ALL, MERGE_NONE)
2048
0
    //               Eo
2049
0
    //   ------------+
2050
0
    //        #####  |
2051
0
    //       ####### |
2052
0
    //      #########|
2053
0
    //      ####+####|
2054
0
    //      ## end ##|
2055
0
    //       ####### |
2056
0
    //        #####  |
2057
0
    //   ------------+
2058
0
    //               Ei
2059
0
2060
0
    Point I(0.0f, 0.0f), J(0.0f, 0.0f);
2061
0
    if (aSide == eSideTop) {
2062
0
      I.x = 1.0f;
2063
0
      J.y = 1.0f;
2064
0
    } else if (aSide == eSideRight) {
2065
0
      I.y = 1.0f;
2066
0
      J.x = -1.0f;
2067
0
    } else if (aSide == eSideBottom) {
2068
0
      I.x = -1.0f;
2069
0
      J.y = -1.0f;
2070
0
    } else if (aSide == eSideLeft) {
2071
0
      I.y = -1.0f;
2072
0
      J.x = 1.0f;
2073
0
    }
2074
0
2075
0
    Point So, Si, Eo, Ei;
2076
0
2077
0
    So = (start + (-I + -J) * borderWidth / 2.0f);
2078
0
    Si = (mergeStart == MERGE_HALF) ? (start + (I + J) * borderWidth / 2.0f)
2079
0
                                    : (start + (-I + J) * borderWidth / 2.0f);
2080
0
    Eo = (end + (I - J) * borderWidth / 2.0f);
2081
0
    Ei = (mergeEnd == MERGE_HALF) ? (end + (-I + J) * borderWidth / 2.0f)
2082
0
                                  : (end + (I + J) * borderWidth / 2.0f);
2083
0
2084
0
    RefPtr<PathBuilder> builder = mDrawTarget->CreatePathBuilder();
2085
0
    builder->MoveTo(So);
2086
0
    builder->LineTo(Eo);
2087
0
    builder->LineTo(Ei);
2088
0
    builder->LineTo(Si);
2089
0
    builder->Close();
2090
0
    RefPtr<Path> path = builder->Finish();
2091
0
2092
0
    mDrawTarget->PushClip(path);
2093
0
  }
2094
0
2095
0
  size_t count = round(borderLength / dotWidth);
2096
0
  if (isStartUnfilled == isEndUnfilled) {
2097
0
    // Split into 2n segments.
2098
0
    if (count % 2) {
2099
0
      count++;
2100
0
    }
2101
0
  } else {
2102
0
    // Split into 2n+1 segments.
2103
0
    if (count % 2 == 0) {
2104
0
      count++;
2105
0
    }
2106
0
  }
2107
0
2108
0
  // A: radius == borderWidth / 2.0
2109
0
  // B: borderLength / count == borderWidth * (1 - overlap)
2110
0
  //
2111
0
  //   A      B         B        B        B     A
2112
0
  // |<-->|<------>|<------>|<------>|<------>|<-->|
2113
0
  // |    |        |        |        |        |    |
2114
0
  // +----+--------+--------+--------+--------+----+
2115
0
  // |  ##|##    **|**    ##|##    **|**    ##|##  |
2116
0
  // | ###|###  ***|***  ###|###  ***|***  ###|### |
2117
0
  // |####|####****|****####|####****|****####|####|
2118
0
  // |####+####****+****####+####****+****####+####|
2119
0
  // |# start #****|****####|####****|****## end ##|
2120
0
  // | ###|###  ***|***  ###|###  ***|***  ###|### |
2121
0
  // |  ##|##    **|**    ##|##    **|**    ##|##  |
2122
0
  // +----+----+---+--------+--------+---+----+----+
2123
0
  // |         |                         |         |
2124
0
  // |         |                         |         |
2125
0
2126
0
  // If isStartUnfilled is true, draw dots on 2j+1 points, if not, draw dots on
2127
0
  // 2j points.
2128
0
  size_t from = isStartUnfilled ? 1 : 0;
2129
0
2130
0
  // If mergeEnd == MERGE_NONE, last dot is drawn by next side.
2131
0
  size_t to = count;
2132
0
  if (mergeEnd == MERGE_NONE) {
2133
0
    if (to > 2) {
2134
0
      to -= 2;
2135
0
    } else {
2136
0
      to = 0;
2137
0
    }
2138
0
  }
2139
0
2140
0
  Point fromP = (start * (count - from) + end * from) / count;
2141
0
  Point toP = (start * (count - to) + end * to) / count;
2142
0
  // Extend dirty rect to avoid clipping pixel for anti-aliasing.
2143
0
  const Float AA_MARGIN = 2.0f;
2144
0
2145
0
  if (aSide == eSideTop) {
2146
0
    // Tweak |from| and |to| to fit into |mDirtyRect + radius margin|,
2147
0
    // to render only paths that may overlap mDirtyRect.
2148
0
    //
2149
0
    //                mDirtyRect + radius margin
2150
0
    //              +--+---------------------+--+
2151
0
    //              |                           |
2152
0
    //              |         mDirtyRect        |
2153
0
    //              +  +---------------------+  +
2154
0
    // from   ===>  |from                    to |   <===  to
2155
0
    //    +-----+-----+-----+-----+-----+-----+-----+-----+
2156
0
    //   ###        |###         ###         ###|        ###
2157
0
    //  #####       #####       #####       #####       #####
2158
0
    //  #####       #####       #####       #####       #####
2159
0
    //  #####       #####       #####       #####       #####
2160
0
    //   ###        |###         ###         ###|        ###
2161
0
    //              |  |                     |  |
2162
0
    //              +  +---------------------+  +
2163
0
    //              |                           |
2164
0
    //              |                           |
2165
0
    //              +--+---------------------+--+
2166
0
2167
0
    Float left = mDirtyRect.x - radius - AA_MARGIN;
2168
0
    if (fromP.x < left) {
2169
0
      size_t tmp = ceil(count * (left - start.x) / (end.x - start.x));
2170
0
      if (tmp > from) {
2171
0
        // We increment by 2, so odd/even should match between before/after.
2172
0
        if ((tmp & 1) != (from & 1)) {
2173
0
          from = tmp - 1;
2174
0
        } else {
2175
0
          from = tmp;
2176
0
        }
2177
0
      }
2178
0
    }
2179
0
    Float right = mDirtyRect.x + mDirtyRect.width + radius + AA_MARGIN;
2180
0
    if (toP.x > right) {
2181
0
      size_t tmp = floor(count * (right - start.x) / (end.x - start.x));
2182
0
      if (tmp < to) {
2183
0
        if ((tmp & 1) != (to & 1)) {
2184
0
          to = tmp + 1;
2185
0
        } else {
2186
0
          to = tmp;
2187
0
        }
2188
0
      }
2189
0
    }
2190
0
  } else if (aSide == eSideRight) {
2191
0
    Float top = mDirtyRect.y - radius - AA_MARGIN;
2192
0
    if (fromP.y < top) {
2193
0
      size_t tmp = ceil(count * (top - start.y) / (end.y - start.y));
2194
0
      if (tmp > from) {
2195
0
        if ((tmp & 1) != (from & 1)) {
2196
0
          from = tmp - 1;
2197
0
        } else {
2198
0
          from = tmp;
2199
0
        }
2200
0
      }
2201
0
    }
2202
0
    Float bottom = mDirtyRect.y + mDirtyRect.height + radius + AA_MARGIN;
2203
0
    if (toP.y > bottom) {
2204
0
      size_t tmp = floor(count * (bottom - start.y) / (end.y - start.y));
2205
0
      if (tmp < to) {
2206
0
        if ((tmp & 1) != (to & 1)) {
2207
0
          to = tmp + 1;
2208
0
        } else {
2209
0
          to = tmp;
2210
0
        }
2211
0
      }
2212
0
    }
2213
0
  } else if (aSide == eSideBottom) {
2214
0
    Float right = mDirtyRect.x + mDirtyRect.width + radius + AA_MARGIN;
2215
0
    if (fromP.x > right) {
2216
0
      size_t tmp = ceil(count * (right - start.x) / (end.x - start.x));
2217
0
      if (tmp > from) {
2218
0
        if ((tmp & 1) != (from & 1)) {
2219
0
          from = tmp - 1;
2220
0
        } else {
2221
0
          from = tmp;
2222
0
        }
2223
0
      }
2224
0
    }
2225
0
    Float left = mDirtyRect.x - radius - AA_MARGIN;
2226
0
    if (toP.x < left) {
2227
0
      size_t tmp = floor(count * (left - start.x) / (end.x - start.x));
2228
0
      if (tmp < to) {
2229
0
        if ((tmp & 1) != (to & 1)) {
2230
0
          to = tmp + 1;
2231
0
        } else {
2232
0
          to = tmp;
2233
0
        }
2234
0
      }
2235
0
    }
2236
0
  } else if (aSide == eSideLeft) {
2237
0
    Float bottom = mDirtyRect.y + mDirtyRect.height + radius + AA_MARGIN;
2238
0
    if (fromP.y > bottom) {
2239
0
      size_t tmp = ceil(count * (bottom - start.y) / (end.y - start.y));
2240
0
      if (tmp > from) {
2241
0
        if ((tmp & 1) != (from & 1)) {
2242
0
          from = tmp - 1;
2243
0
        } else {
2244
0
          from = tmp;
2245
0
        }
2246
0
      }
2247
0
    }
2248
0
    Float top = mDirtyRect.y - radius - AA_MARGIN;
2249
0
    if (toP.y < top) {
2250
0
      size_t tmp = floor(count * (top - start.y) / (end.y - start.y));
2251
0
      if (tmp < to) {
2252
0
        if ((tmp & 1) != (to & 1)) {
2253
0
          to = tmp + 1;
2254
0
        } else {
2255
0
          to = tmp;
2256
0
        }
2257
0
      }
2258
0
    }
2259
0
  }
2260
0
2261
0
  RefPtr<PathBuilder> builder = mDrawTarget->CreatePathBuilder();
2262
0
  size_t segmentCount = 0;
2263
0
  for (size_t i = from; i <= to; i += 2) {
2264
0
    if (segmentCount > BORDER_SEGMENT_COUNT_MAX) {
2265
0
      RefPtr<Path> path = builder->Finish();
2266
0
      mDrawTarget->Fill(path, ColorPattern(ToDeviceColor(borderColor)));
2267
0
      builder = mDrawTarget->CreatePathBuilder();
2268
0
      segmentCount = 0;
2269
0
    }
2270
0
2271
0
    Point P = (start * (count - i) + end * i) / count;
2272
0
    builder->MoveTo(Point(P.x + radius, P.y));
2273
0
    builder->Arc(P, radius, 0.0f, Float(2.0 * M_PI));
2274
0
    segmentCount++;
2275
0
  }
2276
0
  RefPtr<Path> path = builder->Finish();
2277
0
  mDrawTarget->Fill(path, ColorPattern(ToDeviceColor(borderColor)));
2278
0
2279
0
  if (mergeStart == MERGE_HALF || mergeEnd == MERGE_HALF) {
2280
0
    mDrawTarget->PopClip();
2281
0
  }
2282
0
}
2283
2284
void
2285
nsCSSBorderRenderer::DrawDashedOrDottedCorner(mozilla::Side aSide,
2286
                                              Corner aCorner)
2287
0
{
2288
0
  // Draw dashed/dotted corner with following approach.
2289
0
  //
2290
0
  // dashed corner
2291
0
  //   If both side has same border-width and border-width <= 2.0, draw dashed
2292
0
  //   line along the corner, with appropriate dash length and gap to make the
2293
0
  //   corner symmetric as far as possible.  Dash length equals to the gap, and
2294
0
  //   the ratio of the dash length to border-width is the maximum value in in
2295
0
  //   [1, 3] range.
2296
0
  //   Otherwise, draw dashed segments along the corner, keeping same dash
2297
0
  //   length ratio to border-width at that point.
2298
0
  //   (see DashedCornerFinder.h for more detail)
2299
0
  //   Line ends with half segments, to joint with both side easily.
2300
0
  //
2301
0
  // dotted corner
2302
0
  //   If both side has same border-width and border-width <= 2.0, draw 1:1
2303
0
  //   dashed line along the corner.
2304
0
  //   Otherwise Draw circles along the corner, with appropriate gap that makes
2305
0
  //   the corner symmetric as far as possible.  The size of the circle may
2306
0
  //   change along the corner, that is tangent to the outer curver and the
2307
0
  //   inner curve.  The ratio of the gap to circle diameter is the maximum
2308
0
  //   value in [0.5, 1] range.
2309
0
  //   (see DottedCornerFinder.h for more detail)
2310
0
  //   Corner ends with filled dots but those dots are drawn by
2311
0
  //   DrawDashedOrDottedSide.  So this may draw no circles if there's no space
2312
0
  //   between 2 dots at both ends.
2313
0
2314
0
  NS_ASSERTION(mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DASHED ||
2315
0
                 mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DOTTED,
2316
0
               "Style should be dashed or dotted.");
2317
0
2318
0
  if (IsCornerMergeable(aCorner)) {
2319
0
    // DrawDashedOrDottedSide will draw corner.
2320
0
    return;
2321
0
  }
2322
0
2323
0
  mozilla::Side sideH(GetHorizontalSide(aCorner));
2324
0
  mozilla::Side sideV(GetVerticalSide(aCorner));
2325
0
  Float borderWidthH = mBorderWidths[sideH];
2326
0
  Float borderWidthV = mBorderWidths[sideV];
2327
0
  if (borderWidthH == 0.0f && borderWidthV == 0.0f) {
2328
0
    return;
2329
0
  }
2330
0
2331
0
  Float styleH = mBorderStyles[sideH];
2332
0
  Float styleV = mBorderStyles[sideV];
2333
0
2334
0
  // Corner between dotted and others with radius=0 is drawn by side.
2335
0
  if (IsZeroSize(mBorderRadii[aCorner]) &&
2336
0
      (styleV == NS_STYLE_BORDER_STYLE_DOTTED ||
2337
0
       styleH == NS_STYLE_BORDER_STYLE_DOTTED)) {
2338
0
    return;
2339
0
  }
2340
0
2341
0
  Float maxRadius =
2342
0
    std::max(mBorderRadii[aCorner].width, mBorderRadii[aCorner].height);
2343
0
  if (maxRadius > BORDER_DOTTED_CORNER_MAX_RADIUS) {
2344
0
    DrawFallbackSolidCorner(aSide, aCorner);
2345
0
    return;
2346
0
  }
2347
0
2348
0
  if (borderWidthH != borderWidthV || borderWidthH > 2.0f) {
2349
0
    uint8_t style = mBorderStyles[aSide];
2350
0
    if (style == NS_STYLE_BORDER_STYLE_DOTTED) {
2351
0
      DrawDottedCornerSlow(aSide, aCorner);
2352
0
    } else {
2353
0
      DrawDashedCornerSlow(aSide, aCorner);
2354
0
    }
2355
0
    return;
2356
0
  }
2357
0
2358
0
  nscolor borderColor = mBorderColors[aSide];
2359
0
  Point points[4];
2360
0
  bool ignored;
2361
0
  // Get the start and end points of the corner arc, ensuring that any dot
2362
0
  // origins get pushed backwards towards the edges of the corner rect to
2363
0
  // account for stroking.
2364
0
  points[0] = GetStraightBorderPoint(sideH, aCorner, &ignored, -0.5f);
2365
0
  points[3] = GetStraightBorderPoint(sideV, aCorner, &ignored, -0.5f);
2366
0
  // Round points to draw dot on each pixel.
2367
0
  if (borderWidthH < 2.0f) {
2368
0
    points[0].x = round(points[0].x);
2369
0
  }
2370
0
  if (borderWidthV < 2.0f) {
2371
0
    points[3].y = round(points[3].y);
2372
0
  }
2373
0
  points[1] = points[0];
2374
0
  points[1].x += kKappaFactor * (points[3].x - points[0].x);
2375
0
  points[2] = points[3];
2376
0
  points[2].y += kKappaFactor * (points[0].y - points[3].y);
2377
0
2378
0
  Float len = GetQuarterEllipticArcLength(fabs(points[0].x - points[3].x),
2379
0
                                          fabs(points[0].y - points[3].y));
2380
0
2381
0
  Float dash[2];
2382
0
  StrokeOptions strokeOptions(borderWidthH);
2383
0
  SetupDashedOptions(&strokeOptions, dash, aSide, len, true);
2384
0
2385
0
  RefPtr<PathBuilder> builder = mDrawTarget->CreatePathBuilder();
2386
0
  builder->MoveTo(points[0]);
2387
0
  builder->BezierTo(points[1], points[2], points[3]);
2388
0
  RefPtr<Path> path = builder->Finish();
2389
0
  mDrawTarget->Stroke(
2390
0
    path, ColorPattern(ToDeviceColor(borderColor)), strokeOptions);
2391
0
}
2392
2393
void
2394
nsCSSBorderRenderer::DrawDottedCornerSlow(mozilla::Side aSide, Corner aCorner)
2395
0
{
2396
0
  NS_ASSERTION(mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DOTTED,
2397
0
               "Style should be dotted.");
2398
0
2399
0
  mozilla::Side sideH(GetHorizontalSide(aCorner));
2400
0
  mozilla::Side sideV(GetVerticalSide(aCorner));
2401
0
  Float R0 = mBorderWidths[sideH] / 2.0f;
2402
0
  Float Rn = mBorderWidths[sideV] / 2.0f;
2403
0
  if (R0 == 0.0f && Rn == 0.0f) {
2404
0
    return;
2405
0
  }
2406
0
2407
0
  nscolor borderColor = mBorderColors[aSide];
2408
0
  Bezier outerBezier;
2409
0
  Bezier innerBezier;
2410
0
  GetOuterAndInnerBezier(&outerBezier, &innerBezier, aCorner);
2411
0
2412
0
  bool ignored;
2413
0
  Point C0 = GetStraightBorderPoint(sideH, aCorner, &ignored);
2414
0
  Point Cn = GetStraightBorderPoint(sideV, aCorner, &ignored);
2415
0
  DottedCornerFinder finder(outerBezier,
2416
0
                            innerBezier,
2417
0
                            aCorner,
2418
0
                            mBorderRadii[aCorner].width,
2419
0
                            mBorderRadii[aCorner].height,
2420
0
                            C0,
2421
0
                            R0,
2422
0
                            Cn,
2423
0
                            Rn,
2424
0
                            mBorderCornerDimensions[aCorner]);
2425
0
2426
0
  RefPtr<PathBuilder> builder = mDrawTarget->CreatePathBuilder();
2427
0
  size_t segmentCount = 0;
2428
0
  const Float AA_MARGIN = 2.0f;
2429
0
  Rect marginedDirtyRect = mDirtyRect;
2430
0
  marginedDirtyRect.Inflate(std::max(R0, Rn) + AA_MARGIN);
2431
0
  bool entered = false;
2432
0
  while (finder.HasMore()) {
2433
0
    if (segmentCount > BORDER_SEGMENT_COUNT_MAX) {
2434
0
      RefPtr<Path> path = builder->Finish();
2435
0
      mDrawTarget->Fill(path, ColorPattern(ToDeviceColor(borderColor)));
2436
0
      builder = mDrawTarget->CreatePathBuilder();
2437
0
      segmentCount = 0;
2438
0
    }
2439
0
2440
0
    DottedCornerFinder::Result result = finder.Next();
2441
0
2442
0
    if (marginedDirtyRect.Contains(result.C) && result.r > 0) {
2443
0
      entered = true;
2444
0
      builder->MoveTo(Point(result.C.x + result.r, result.C.y));
2445
0
      builder->Arc(result.C, result.r, 0, Float(2.0 * M_PI));
2446
0
      segmentCount++;
2447
0
    } else if (entered) {
2448
0
      break;
2449
0
    }
2450
0
  }
2451
0
  RefPtr<Path> path = builder->Finish();
2452
0
  mDrawTarget->Fill(path, ColorPattern(ToDeviceColor(borderColor)));
2453
0
}
2454
2455
static inline bool
2456
DashedPathOverlapsRect(Rect& pathRect,
2457
                       const Rect& marginedDirtyRect,
2458
                       DashedCornerFinder::Result& result)
2459
0
{
2460
0
  // Calculate a rect that contains all control points of the |result| path,
2461
0
  // and check if it intersects with |marginedDirtyRect|.
2462
0
  pathRect.SetRect(result.outerSectionBezier.mPoints[0].x,
2463
0
                   result.outerSectionBezier.mPoints[0].y,
2464
0
                   0,
2465
0
                   0);
2466
0
  pathRect.ExpandToEnclose(result.outerSectionBezier.mPoints[1]);
2467
0
  pathRect.ExpandToEnclose(result.outerSectionBezier.mPoints[2]);
2468
0
  pathRect.ExpandToEnclose(result.outerSectionBezier.mPoints[3]);
2469
0
  pathRect.ExpandToEnclose(result.innerSectionBezier.mPoints[0]);
2470
0
  pathRect.ExpandToEnclose(result.innerSectionBezier.mPoints[1]);
2471
0
  pathRect.ExpandToEnclose(result.innerSectionBezier.mPoints[2]);
2472
0
  pathRect.ExpandToEnclose(result.innerSectionBezier.mPoints[3]);
2473
0
2474
0
  return pathRect.Intersects(marginedDirtyRect);
2475
0
}
2476
2477
void
2478
nsCSSBorderRenderer::DrawDashedCornerSlow(mozilla::Side aSide, Corner aCorner)
2479
0
{
2480
0
  NS_ASSERTION(mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DASHED,
2481
0
               "Style should be dashed.");
2482
0
2483
0
  mozilla::Side sideH(GetHorizontalSide(aCorner));
2484
0
  mozilla::Side sideV(GetVerticalSide(aCorner));
2485
0
  Float borderWidthH = mBorderWidths[sideH];
2486
0
  Float borderWidthV = mBorderWidths[sideV];
2487
0
  if (borderWidthH == 0.0f && borderWidthV == 0.0f) {
2488
0
    return;
2489
0
  }
2490
0
2491
0
  nscolor borderColor = mBorderColors[aSide];
2492
0
  Bezier outerBezier;
2493
0
  Bezier innerBezier;
2494
0
  GetOuterAndInnerBezier(&outerBezier, &innerBezier, aCorner);
2495
0
2496
0
  DashedCornerFinder finder(outerBezier,
2497
0
                            innerBezier,
2498
0
                            borderWidthH,
2499
0
                            borderWidthV,
2500
0
                            mBorderCornerDimensions[aCorner]);
2501
0
2502
0
  RefPtr<PathBuilder> builder = mDrawTarget->CreatePathBuilder();
2503
0
  size_t segmentCount = 0;
2504
0
  const Float AA_MARGIN = 2.0f;
2505
0
  Rect marginedDirtyRect = mDirtyRect;
2506
0
  marginedDirtyRect.Inflate(AA_MARGIN);
2507
0
  Rect pathRect;
2508
0
  bool entered = false;
2509
0
  while (finder.HasMore()) {
2510
0
    if (segmentCount > BORDER_SEGMENT_COUNT_MAX) {
2511
0
      RefPtr<Path> path = builder->Finish();
2512
0
      mDrawTarget->Fill(path, ColorPattern(ToDeviceColor(borderColor)));
2513
0
      builder = mDrawTarget->CreatePathBuilder();
2514
0
      segmentCount = 0;
2515
0
    }
2516
0
2517
0
    DashedCornerFinder::Result result = finder.Next();
2518
0
2519
0
    if (DashedPathOverlapsRect(pathRect, marginedDirtyRect, result)) {
2520
0
      entered = true;
2521
0
      builder->MoveTo(result.outerSectionBezier.mPoints[0]);
2522
0
      builder->BezierTo(result.outerSectionBezier.mPoints[1],
2523
0
                        result.outerSectionBezier.mPoints[2],
2524
0
                        result.outerSectionBezier.mPoints[3]);
2525
0
      builder->LineTo(result.innerSectionBezier.mPoints[3]);
2526
0
      builder->BezierTo(result.innerSectionBezier.mPoints[2],
2527
0
                        result.innerSectionBezier.mPoints[1],
2528
0
                        result.innerSectionBezier.mPoints[0]);
2529
0
      builder->LineTo(result.outerSectionBezier.mPoints[0]);
2530
0
      segmentCount++;
2531
0
    } else if (entered) {
2532
0
      break;
2533
0
    }
2534
0
  }
2535
0
2536
0
  if (outerBezier.mPoints[0].x != innerBezier.mPoints[0].x) {
2537
0
    // Fill gap before the first section.
2538
0
    //
2539
0
    //     outnerPoint[0]
2540
0
    //         |
2541
0
    //         v
2542
0
    //        _+-----------+--
2543
0
    //      /   \##########|
2544
0
    //    /      \#########|
2545
0
    //   +        \########|
2546
0
    //   |\         \######|
2547
0
    //   |  \        \#####|
2548
0
    //   |    \       \####|
2549
0
    //   |      \       \##|
2550
0
    //   |        \      \#|
2551
0
    //   |          \     \|
2552
0
    //   |            \  _-+--
2553
0
    //   +--------------+  ^
2554
0
    //   |              |  |
2555
0
    //   |              |  innerPoint[0]
2556
0
    //   |              |
2557
0
    builder->MoveTo(outerBezier.mPoints[0]);
2558
0
    builder->LineTo(innerBezier.mPoints[0]);
2559
0
    builder->LineTo(Point(innerBezier.mPoints[0].x, outerBezier.mPoints[0].y));
2560
0
    builder->LineTo(outerBezier.mPoints[0]);
2561
0
  }
2562
0
2563
0
  if (outerBezier.mPoints[3].y != innerBezier.mPoints[3].y) {
2564
0
    // Fill gap after the last section.
2565
0
    //
2566
0
    // outnerPoint[3]
2567
0
    //   |
2568
0
    //   |
2569
0
    //   |    _+-----------+--
2570
0
    //   |  /   \          |
2571
0
    //   v/      \         |
2572
0
    //   +        \        |
2573
0
    //   |\         \      |
2574
0
    //   |##\        \     |
2575
0
    //   |####\       \    |
2576
0
    //   |######\       \  |
2577
0
    //   |########\      \ |
2578
0
    //   |##########\     \|
2579
0
    //   |############\  _-+--
2580
0
    //   +--------------+<-- innerPoint[3]
2581
0
    //   |              |
2582
0
    //   |              |
2583
0
    //   |              |
2584
0
    builder->MoveTo(outerBezier.mPoints[3]);
2585
0
    builder->LineTo(innerBezier.mPoints[3]);
2586
0
    builder->LineTo(Point(outerBezier.mPoints[3].x, innerBezier.mPoints[3].y));
2587
0
    builder->LineTo(outerBezier.mPoints[3]);
2588
0
  }
2589
0
2590
0
  RefPtr<Path> path = builder->Finish();
2591
0
  mDrawTarget->Fill(path, ColorPattern(ToDeviceColor(borderColor)));
2592
0
}
2593
2594
void
2595
nsCSSBorderRenderer::DrawFallbackSolidCorner(mozilla::Side aSide,
2596
                                             Corner aCorner)
2597
0
{
2598
0
  // Render too large dashed or dotted corner with solid style, to avoid hangup
2599
0
  // inside DashedCornerFinder and DottedCornerFinder.
2600
0
2601
0
  NS_ASSERTION(mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DASHED ||
2602
0
                 mBorderStyles[aSide] == NS_STYLE_BORDER_STYLE_DOTTED,
2603
0
               "Style should be dashed or dotted.");
2604
0
2605
0
  nscolor borderColor = mBorderColors[aSide];
2606
0
  Bezier outerBezier;
2607
0
  Bezier innerBezier;
2608
0
  GetOuterAndInnerBezier(&outerBezier, &innerBezier, aCorner);
2609
0
2610
0
  RefPtr<PathBuilder> builder = mDrawTarget->CreatePathBuilder();
2611
0
2612
0
  builder->MoveTo(outerBezier.mPoints[0]);
2613
0
  builder->BezierTo(
2614
0
    outerBezier.mPoints[1], outerBezier.mPoints[2], outerBezier.mPoints[3]);
2615
0
  builder->LineTo(innerBezier.mPoints[3]);
2616
0
  builder->BezierTo(
2617
0
    innerBezier.mPoints[2], innerBezier.mPoints[1], innerBezier.mPoints[0]);
2618
0
  builder->LineTo(outerBezier.mPoints[0]);
2619
0
2620
0
  RefPtr<Path> path = builder->Finish();
2621
0
  mDrawTarget->Fill(path, ColorPattern(ToDeviceColor(borderColor)));
2622
0
2623
0
  if (mDocument) {
2624
0
    if (!mPresContext->HasWarnedAboutTooLargeDashedOrDottedRadius()) {
2625
0
      mPresContext->SetHasWarnedAboutTooLargeDashedOrDottedRadius();
2626
0
      nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
2627
0
                                      NS_LITERAL_CSTRING("CSS"),
2628
0
                                      mDocument,
2629
0
                                      nsContentUtils::eCSS_PROPERTIES,
2630
0
                                      mBorderStyles[aSide] ==
2631
0
                                          NS_STYLE_BORDER_STYLE_DASHED
2632
0
                                        ? "TooLargeDashedRadius"
2633
0
                                        : "TooLargeDottedRadius");
2634
0
    }
2635
0
  }
2636
0
}
2637
2638
bool
2639
nsCSSBorderRenderer::AllBordersSameWidth()
2640
0
{
2641
0
  if (mBorderWidths[0] == mBorderWidths[1] &&
2642
0
      mBorderWidths[0] == mBorderWidths[2] &&
2643
0
      mBorderWidths[0] == mBorderWidths[3]) {
2644
0
    return true;
2645
0
  }
2646
0
2647
0
  return false;
2648
0
}
2649
2650
bool
2651
nsCSSBorderRenderer::AllBordersSolid()
2652
0
{
2653
0
  NS_FOR_CSS_SIDES(i)
2654
0
  {
2655
0
    if (mBorderStyles[i] == NS_STYLE_BORDER_STYLE_SOLID ||
2656
0
        mBorderStyles[i] == NS_STYLE_BORDER_STYLE_NONE ||
2657
0
        mBorderStyles[i] == NS_STYLE_BORDER_STYLE_HIDDEN) {
2658
0
      continue;
2659
0
    }
2660
0
    return false;
2661
0
  }
2662
0
2663
0
  return true;
2664
0
}
2665
2666
static bool
2667
IsVisible(int aStyle)
2668
0
{
2669
0
  if (aStyle != NS_STYLE_BORDER_STYLE_NONE &&
2670
0
      aStyle != NS_STYLE_BORDER_STYLE_HIDDEN) {
2671
0
    return true;
2672
0
  }
2673
0
  return false;
2674
0
}
2675
2676
struct twoFloats
2677
{
2678
  Float a, b;
2679
2680
  twoFloats operator*(const Size& aSize) const
2681
0
  {
2682
0
    return { a * aSize.width, b * aSize.height };
2683
0
  }
2684
2685
0
  twoFloats operator*(Float aScale) const { return { a * aScale, b * aScale }; }
2686
2687
  twoFloats operator+(const Point& aPoint) const
2688
0
  {
2689
0
    return { a + aPoint.x, b + aPoint.y };
2690
0
  }
2691
2692
0
  operator Point() const { return Point(a, b); }
2693
};
2694
2695
void
2696
nsCSSBorderRenderer::DrawSingleWidthSolidBorder()
2697
0
{
2698
0
  // Easy enough to deal with.
2699
0
  Rect rect = mOuterRect;
2700
0
  rect.Deflate(0.5);
2701
0
2702
0
  const twoFloats cornerAdjusts[4] = {
2703
0
    { +0.5, 0 }, { 0, +0.5 }, { -0.5, 0 }, { 0, -0.5 }
2704
0
  };
2705
0
  NS_FOR_CSS_SIDES(side)
2706
0
  {
2707
0
    Point firstCorner = rect.CCWCorner(side) + cornerAdjusts[side];
2708
0
    Point secondCorner = rect.CWCorner(side) + cornerAdjusts[side];
2709
0
2710
0
    ColorPattern color(ToDeviceColor(mBorderColors[side]));
2711
0
2712
0
    mDrawTarget->StrokeLine(firstCorner, secondCorner, color);
2713
0
  }
2714
0
}
2715
2716
// Intersect a ray from the inner corner to the outer corner
2717
// with the border radius, yielding the intersection point.
2718
static Point
2719
IntersectBorderRadius(const Point& aCenter,
2720
                      const Size& aRadius,
2721
                      const Point& aInnerCorner,
2722
                      const Point& aCornerDirection)
2723
0
{
2724
0
  Point toCorner = aCornerDirection;
2725
0
  // transform to-corner ray to unit-circle space
2726
0
  toCorner.x /= aRadius.width;
2727
0
  toCorner.y /= aRadius.height;
2728
0
  // normalize to-corner ray
2729
0
  Float cornerDist = toCorner.Length();
2730
0
  if (cornerDist < 1.0e-6f) {
2731
0
    return aInnerCorner;
2732
0
  }
2733
0
  toCorner = toCorner / cornerDist;
2734
0
  // ray from inner corner to border radius center
2735
0
  Point toCenter = aCenter - aInnerCorner;
2736
0
  // transform to-center ray to unit-circle space
2737
0
  toCenter.x /= aRadius.width;
2738
0
  toCenter.y /= aRadius.height;
2739
0
  // compute offset of intersection with border radius unit circle
2740
0
  Float offset = toCenter.DotProduct(toCorner);
2741
0
  // compute discriminant to check for intersections
2742
0
  Float discrim = 1.0f - toCenter.DotProduct(toCenter) + offset * offset;
2743
0
  // choose farthest intersection
2744
0
  offset += sqrtf(std::max(discrim, 0.0f));
2745
0
  // transform to-corner ray back out of unit-circle space
2746
0
  toCorner.x *= aRadius.width;
2747
0
  toCorner.y *= aRadius.height;
2748
0
  return aInnerCorner + toCorner * offset;
2749
0
}
2750
2751
// Calculate the split point and split angle for a border radius with
2752
// differing sides.
2753
static inline void
2754
SplitBorderRadius(const Point& aCenter,
2755
                  const Size& aRadius,
2756
                  const Point& aOuterCorner,
2757
                  const Point& aInnerCorner,
2758
                  const twoFloats& aCornerMults,
2759
                  Float aStartAngle,
2760
                  Point& aSplit,
2761
                  Float& aSplitAngle)
2762
0
{
2763
0
  Point cornerDir = aOuterCorner - aInnerCorner;
2764
0
  if (cornerDir.x == cornerDir.y && aRadius.IsSquare()) {
2765
0
    // optimize 45-degree intersection with circle since we can assume
2766
0
    // the circle center lies along the intersection edge
2767
0
    aSplit = aCenter - aCornerMults * (aRadius * Float(1.0f / M_SQRT2));
2768
0
    aSplitAngle = aStartAngle + 0.5f * M_PI / 2.0f;
2769
0
  } else {
2770
0
    aSplit = IntersectBorderRadius(aCenter, aRadius, aInnerCorner, cornerDir);
2771
0
    aSplitAngle = atan2f((aSplit.y - aCenter.y) / aRadius.height,
2772
0
                         (aSplit.x - aCenter.x) / aRadius.width);
2773
0
  }
2774
0
}
2775
2776
// Compute the size of the skirt needed, given the color alphas
2777
// of each corner side and the slope between them.
2778
static void
2779
ComputeCornerSkirtSize(Float aAlpha1,
2780
                       Float aAlpha2,
2781
                       Float aSlopeY,
2782
                       Float aSlopeX,
2783
                       Float& aSizeResult,
2784
                       Float& aSlopeResult)
2785
0
{
2786
0
  // If either side is (almost) invisible or there is no diagonal edge,
2787
0
  // then don't try to render a skirt.
2788
0
  if (aAlpha1 < 0.01f || aAlpha2 < 0.01f) {
2789
0
    return;
2790
0
  }
2791
0
  aSlopeX = fabs(aSlopeX);
2792
0
  aSlopeY = fabs(aSlopeY);
2793
0
  if (aSlopeX < 1.0e-6f || aSlopeY < 1.0e-6f) {
2794
0
    return;
2795
0
  }
2796
0
2797
0
  // If first and second color don't match, we need to split the corner in
2798
0
  // half. The diagonal edges created may not have full pixel coverage given
2799
0
  // anti-aliasing, so we need to compute a small subpixel skirt edge. This
2800
0
  // assumes each half has half coverage to start with, and that coverage
2801
0
  // increases as the skirt is pushed over, with the end result that we want
2802
0
  // to roughly preserve the alpha value along this edge.
2803
0
  // Given slope m, alphas a and A, use quadratic formula to solve for S in:
2804
0
  //   a*(1 - 0.5*(1-S)*(1-mS))*(1 - 0.5*A) + 0.5*A = A
2805
0
  // yielding:
2806
0
  //   S = ((1+m) - sqrt((1+m)*(1+m) + 4*m*(1 - A/(a*(1-0.5*A))))) / (2*m)
2807
0
  // and substitute k = (1+m)/(2*m):
2808
0
  //   S = k - sqrt(k*k + (1 - A/(a*(1-0.5*A)))/m)
2809
0
  Float slope = aSlopeY / aSlopeX;
2810
0
  Float slopeScale = (1.0f + slope) / (2.0f * slope);
2811
0
  Float discrim = slopeScale * slopeScale +
2812
0
                  (1 - aAlpha2 / (aAlpha1 * (1.0f - 0.49f * aAlpha2))) / slope;
2813
0
  if (discrim >= 0) {
2814
0
    aSizeResult = slopeScale - sqrtf(discrim);
2815
0
    aSlopeResult = slope;
2816
0
  }
2817
0
}
2818
2819
// Draws a border radius with possibly different sides.
2820
// A skirt is drawn underneath the corner intersection to hide possible
2821
// seams when anti-aliased drawing is used.
2822
static void
2823
DrawBorderRadius(DrawTarget* aDrawTarget,
2824
                 Corner c,
2825
                 const Point& aOuterCorner,
2826
                 const Point& aInnerCorner,
2827
                 const twoFloats& aCornerMultPrev,
2828
                 const twoFloats& aCornerMultNext,
2829
                 const Size& aCornerDims,
2830
                 const Size& aOuterRadius,
2831
                 const Size& aInnerRadius,
2832
                 const Color& aFirstColor,
2833
                 const Color& aSecondColor,
2834
                 Float aSkirtSize,
2835
                 Float aSkirtSlope)
2836
0
{
2837
0
  // Connect edge to outer arc start point
2838
0
  Point outerCornerStart = aOuterCorner + aCornerMultPrev * aCornerDims;
2839
0
  // Connect edge to outer arc end point
2840
0
  Point outerCornerEnd = aOuterCorner + aCornerMultNext * aCornerDims;
2841
0
  // Connect edge to inner arc start point
2842
0
  Point innerCornerStart =
2843
0
    outerCornerStart + aCornerMultNext * (aCornerDims - aInnerRadius);
2844
0
  // Connect edge to inner arc end point
2845
0
  Point innerCornerEnd =
2846
0
    outerCornerEnd + aCornerMultPrev * (aCornerDims - aInnerRadius);
2847
0
2848
0
  // Outer arc start point
2849
0
  Point outerArcStart = aOuterCorner + aCornerMultPrev * aOuterRadius;
2850
0
  // Outer arc end point
2851
0
  Point outerArcEnd = aOuterCorner + aCornerMultNext * aOuterRadius;
2852
0
  // Inner arc start point
2853
0
  Point innerArcStart = aInnerCorner + aCornerMultPrev * aInnerRadius;
2854
0
  // Inner arc end point
2855
0
  Point innerArcEnd = aInnerCorner + aCornerMultNext * aInnerRadius;
2856
0
2857
0
  // Outer radius center
2858
0
  Point outerCenter =
2859
0
    aOuterCorner + (aCornerMultPrev + aCornerMultNext) * aOuterRadius;
2860
0
  // Inner radius center
2861
0
  Point innerCenter =
2862
0
    aInnerCorner + (aCornerMultPrev + aCornerMultNext) * aInnerRadius;
2863
0
2864
0
  RefPtr<PathBuilder> builder;
2865
0
  RefPtr<Path> path;
2866
0
2867
0
  if (aFirstColor.a > 0) {
2868
0
    builder = aDrawTarget->CreatePathBuilder();
2869
0
    builder->MoveTo(outerCornerStart);
2870
0
  }
2871
0
2872
0
  if (aFirstColor != aSecondColor) {
2873
0
    // Start and end angles of corner quadrant
2874
0
    Float startAngle = (c * M_PI) / 2.0f - M_PI,
2875
0
          endAngle = startAngle + M_PI / 2.0f, outerSplitAngle, innerSplitAngle;
2876
0
    Point outerSplit, innerSplit;
2877
0
2878
0
    // Outer half-way point
2879
0
    SplitBorderRadius(outerCenter,
2880
0
                      aOuterRadius,
2881
0
                      aOuterCorner,
2882
0
                      aInnerCorner,
2883
0
                      aCornerMultPrev + aCornerMultNext,
2884
0
                      startAngle,
2885
0
                      outerSplit,
2886
0
                      outerSplitAngle);
2887
0
    // Inner half-way point
2888
0
    if (aInnerRadius.IsEmpty()) {
2889
0
      innerSplit = aInnerCorner;
2890
0
      innerSplitAngle = endAngle;
2891
0
    } else {
2892
0
      SplitBorderRadius(innerCenter,
2893
0
                        aInnerRadius,
2894
0
                        aOuterCorner,
2895
0
                        aInnerCorner,
2896
0
                        aCornerMultPrev + aCornerMultNext,
2897
0
                        startAngle,
2898
0
                        innerSplit,
2899
0
                        innerSplitAngle);
2900
0
    }
2901
0
2902
0
    // Draw first half with first color
2903
0
    if (aFirstColor.a > 0) {
2904
0
      AcuteArcToBezier(builder.get(),
2905
0
                       outerCenter,
2906
0
                       aOuterRadius,
2907
0
                       outerArcStart,
2908
0
                       outerSplit,
2909
0
                       startAngle,
2910
0
                       outerSplitAngle);
2911
0
      // Draw skirt as part of first half
2912
0
      if (aSkirtSize > 0) {
2913
0
        builder->LineTo(outerSplit + aCornerMultNext * aSkirtSize);
2914
0
        builder->LineTo(innerSplit -
2915
0
                        aCornerMultPrev * (aSkirtSize * aSkirtSlope));
2916
0
      }
2917
0
      AcuteArcToBezier(builder.get(),
2918
0
                       innerCenter,
2919
0
                       aInnerRadius,
2920
0
                       innerSplit,
2921
0
                       innerArcStart,
2922
0
                       innerSplitAngle,
2923
0
                       startAngle);
2924
0
      if ((innerCornerStart - innerArcStart).DotProduct(aCornerMultPrev) > 0) {
2925
0
        builder->LineTo(innerCornerStart);
2926
0
      }
2927
0
      builder->Close();
2928
0
      path = builder->Finish();
2929
0
      aDrawTarget->Fill(path, ColorPattern(aFirstColor));
2930
0
    }
2931
0
2932
0
    // Draw second half with second color
2933
0
    if (aSecondColor.a > 0) {
2934
0
      builder = aDrawTarget->CreatePathBuilder();
2935
0
      builder->MoveTo(outerCornerEnd);
2936
0
      if ((innerArcEnd - innerCornerEnd).DotProduct(aCornerMultNext) < 0) {
2937
0
        builder->LineTo(innerCornerEnd);
2938
0
      }
2939
0
      AcuteArcToBezier(builder.get(),
2940
0
                       innerCenter,
2941
0
                       aInnerRadius,
2942
0
                       innerArcEnd,
2943
0
                       innerSplit,
2944
0
                       endAngle,
2945
0
                       innerSplitAngle);
2946
0
      AcuteArcToBezier(builder.get(),
2947
0
                       outerCenter,
2948
0
                       aOuterRadius,
2949
0
                       outerSplit,
2950
0
                       outerArcEnd,
2951
0
                       outerSplitAngle,
2952
0
                       endAngle);
2953
0
      builder->Close();
2954
0
      path = builder->Finish();
2955
0
      aDrawTarget->Fill(path, ColorPattern(aSecondColor));
2956
0
    }
2957
0
  } else if (aFirstColor.a > 0) {
2958
0
    // Draw corner with single color
2959
0
    AcuteArcToBezier(
2960
0
      builder.get(), outerCenter, aOuterRadius, outerArcStart, outerArcEnd);
2961
0
    builder->LineTo(outerCornerEnd);
2962
0
    if ((innerArcEnd - innerCornerEnd).DotProduct(aCornerMultNext) < 0) {
2963
0
      builder->LineTo(innerCornerEnd);
2964
0
    }
2965
0
    AcuteArcToBezier(builder.get(),
2966
0
                     innerCenter,
2967
0
                     aInnerRadius,
2968
0
                     innerArcEnd,
2969
0
                     innerArcStart,
2970
0
                     -kKappaFactor);
2971
0
    if ((innerCornerStart - innerArcStart).DotProduct(aCornerMultPrev) > 0) {
2972
0
      builder->LineTo(innerCornerStart);
2973
0
    }
2974
0
    builder->Close();
2975
0
    path = builder->Finish();
2976
0
    aDrawTarget->Fill(path, ColorPattern(aFirstColor));
2977
0
  }
2978
0
}
2979
2980
// Draw a corner with possibly different sides.
2981
// A skirt is drawn underneath the corner intersection to hide possible
2982
// seams when anti-aliased drawing is used.
2983
static void
2984
DrawCorner(DrawTarget* aDrawTarget,
2985
           const Point& aOuterCorner,
2986
           const Point& aInnerCorner,
2987
           const twoFloats& aCornerMultPrev,
2988
           const twoFloats& aCornerMultNext,
2989
           const Size& aCornerDims,
2990
           const Color& aFirstColor,
2991
           const Color& aSecondColor,
2992
           Float aSkirtSize,
2993
           Float aSkirtSlope)
2994
0
{
2995
0
  // Corner box start point
2996
0
  Point cornerStart = aOuterCorner + aCornerMultPrev * aCornerDims;
2997
0
  // Corner box end point
2998
0
  Point cornerEnd = aOuterCorner + aCornerMultNext * aCornerDims;
2999
0
3000
0
  RefPtr<PathBuilder> builder;
3001
0
  RefPtr<Path> path;
3002
0
3003
0
  if (aFirstColor.a > 0) {
3004
0
    builder = aDrawTarget->CreatePathBuilder();
3005
0
    builder->MoveTo(cornerStart);
3006
0
  }
3007
0
3008
0
  if (aFirstColor != aSecondColor) {
3009
0
    // Draw first half with first color
3010
0
    if (aFirstColor.a > 0) {
3011
0
      builder->LineTo(aOuterCorner);
3012
0
      // Draw skirt as part of first half
3013
0
      if (aSkirtSize > 0) {
3014
0
        builder->LineTo(aOuterCorner + aCornerMultNext * aSkirtSize);
3015
0
        builder->LineTo(aInnerCorner -
3016
0
                        aCornerMultPrev * (aSkirtSize * aSkirtSlope));
3017
0
      }
3018
0
      builder->LineTo(aInnerCorner);
3019
0
      builder->Close();
3020
0
      path = builder->Finish();
3021
0
      aDrawTarget->Fill(path, ColorPattern(aFirstColor));
3022
0
    }
3023
0
3024
0
    // Draw second half with second color
3025
0
    if (aSecondColor.a > 0) {
3026
0
      builder = aDrawTarget->CreatePathBuilder();
3027
0
      builder->MoveTo(cornerEnd);
3028
0
      builder->LineTo(aInnerCorner);
3029
0
      builder->LineTo(aOuterCorner);
3030
0
      builder->Close();
3031
0
      path = builder->Finish();
3032
0
      aDrawTarget->Fill(path, ColorPattern(aSecondColor));
3033
0
    }
3034
0
  } else if (aFirstColor.a > 0) {
3035
0
    // Draw corner with single color
3036
0
    builder->LineTo(aOuterCorner);
3037
0
    builder->LineTo(cornerEnd);
3038
0
    builder->LineTo(aInnerCorner);
3039
0
    builder->Close();
3040
0
    path = builder->Finish();
3041
0
    aDrawTarget->Fill(path, ColorPattern(aFirstColor));
3042
0
  }
3043
0
}
3044
3045
void
3046
nsCSSBorderRenderer::DrawSolidBorder()
3047
0
{
3048
0
  const twoFloats cornerMults[4] = {
3049
0
    { -1, 0 }, { 0, -1 }, { +1, 0 }, { 0, +1 }
3050
0
  };
3051
0
3052
0
  const twoFloats centerAdjusts[4] = {
3053
0
    { 0, +0.5 }, { -0.5, 0 }, { 0, -0.5 }, { +0.5, 0 }
3054
0
  };
3055
0
3056
0
  RectCornerRadii innerRadii;
3057
0
  ComputeInnerRadii(mBorderRadii, mBorderWidths, &innerRadii);
3058
0
3059
0
  Rect strokeRect = mOuterRect;
3060
0
  strokeRect.Deflate(Margin(mBorderWidths[0] / 2.0,
3061
0
                            mBorderWidths[1] / 2.0,
3062
0
                            mBorderWidths[2] / 2.0,
3063
0
                            mBorderWidths[3] / 2.0));
3064
0
3065
0
  NS_FOR_CSS_SIDES(i)
3066
0
  {
3067
0
    // We now draw the current side and the CW corner following it.
3068
0
    // The CCW corner of this side was already drawn in the previous iteration.
3069
0
    // The side will be drawn as an explicit stroke, and the CW corner will be
3070
0
    // filled separately.
3071
0
    // If the next side does not have a matching color, then we split the
3072
0
    // corner into two halves, one of each side's color and draw both.
3073
0
    // Thus, the CCW corner of the next side will end up drawn here.
3074
0
3075
0
    // the corner index -- either 1 2 3 0 (cw) or 0 3 2 1 (ccw)
3076
0
    Corner c = Corner((i + 1) % 4);
3077
0
    Corner prevCorner = Corner(i);
3078
0
3079
0
    // i+2 and i+3 respectively.  These are used to index into the corner
3080
0
    // multiplier table, and were deduced by calculating out the long form
3081
0
    // of each corner and finding a pattern in the signs and values.
3082
0
    int i1 = (i + 1) % 4;
3083
0
    int i2 = (i + 2) % 4;
3084
0
    int i3 = (i + 3) % 4;
3085
0
3086
0
    Float sideWidth = 0.0f;
3087
0
    Color firstColor, secondColor;
3088
0
    if (IsVisible(mBorderStyles[i]) && mBorderWidths[i]) {
3089
0
      // draw the side since it is visible
3090
0
      sideWidth = mBorderWidths[i];
3091
0
      firstColor = ToDeviceColor(mBorderColors[i]);
3092
0
      // if the next side is visible, use its color for corner
3093
0
      secondColor = IsVisible(mBorderStyles[i1]) && mBorderWidths[i1]
3094
0
                      ? ToDeviceColor(mBorderColors[i1])
3095
0
                      : firstColor;
3096
0
    } else if (IsVisible(mBorderStyles[i1]) && mBorderWidths[i1]) {
3097
0
      // assign next side's color to both corner sides
3098
0
      firstColor = ToDeviceColor(mBorderColors[i1]);
3099
0
      secondColor = firstColor;
3100
0
    } else {
3101
0
      // neither side is visible, so nothing to do
3102
0
      continue;
3103
0
    }
3104
0
3105
0
    Point outerCorner = mOuterRect.AtCorner(c);
3106
0
    Point innerCorner = mInnerRect.AtCorner(c);
3107
0
3108
0
    // start and end points of border side stroke between corners
3109
0
    Point sideStart = mOuterRect.AtCorner(prevCorner) +
3110
0
                      cornerMults[i2] * mBorderCornerDimensions[prevCorner];
3111
0
    Point sideEnd = outerCorner + cornerMults[i] * mBorderCornerDimensions[c];
3112
0
    // check if the side is visible and not inverted
3113
0
    if (sideWidth > 0 && firstColor.a > 0 &&
3114
0
        -(sideEnd - sideStart).DotProduct(cornerMults[i]) > 0) {
3115
0
      mDrawTarget->StrokeLine(sideStart + centerAdjusts[i] * sideWidth,
3116
0
                              sideEnd + centerAdjusts[i] * sideWidth,
3117
0
                              ColorPattern(firstColor),
3118
0
                              StrokeOptions(sideWidth));
3119
0
    }
3120
0
3121
0
    Float skirtSize = 0.0f, skirtSlope = 0.0f;
3122
0
    // the sides don't match, so compute a skirt
3123
0
    if (firstColor != secondColor &&
3124
0
        mPresContext->Type() != nsPresContext::eContext_Print) {
3125
0
      Point cornerDir = outerCorner - innerCorner;
3126
0
      ComputeCornerSkirtSize(firstColor.a,
3127
0
                             secondColor.a,
3128
0
                             cornerDir.DotProduct(cornerMults[i]),
3129
0
                             cornerDir.DotProduct(cornerMults[i3]),
3130
0
                             skirtSize,
3131
0
                             skirtSlope);
3132
0
    }
3133
0
3134
0
    if (!mBorderRadii[c].IsEmpty()) {
3135
0
      // the corner has a border radius
3136
0
      DrawBorderRadius(mDrawTarget,
3137
0
                       c,
3138
0
                       outerCorner,
3139
0
                       innerCorner,
3140
0
                       cornerMults[i],
3141
0
                       cornerMults[i3],
3142
0
                       mBorderCornerDimensions[c],
3143
0
                       mBorderRadii[c],
3144
0
                       innerRadii[c],
3145
0
                       firstColor,
3146
0
                       secondColor,
3147
0
                       skirtSize,
3148
0
                       skirtSlope);
3149
0
    } else if (!mBorderCornerDimensions[c].IsEmpty()) {
3150
0
      // a corner with no border radius
3151
0
      DrawCorner(mDrawTarget,
3152
0
                 outerCorner,
3153
0
                 innerCorner,
3154
0
                 cornerMults[i],
3155
0
                 cornerMults[i3],
3156
0
                 mBorderCornerDimensions[c],
3157
0
                 firstColor,
3158
0
                 secondColor,
3159
0
                 skirtSize,
3160
0
                 skirtSlope);
3161
0
    }
3162
0
  }
3163
0
}
3164
3165
void
3166
nsCSSBorderRenderer::DrawBorders()
3167
0
{
3168
0
  if (mAllBordersSameStyle &&
3169
0
      (mBorderStyles[0] == NS_STYLE_BORDER_STYLE_NONE ||
3170
0
       mBorderStyles[0] == NS_STYLE_BORDER_STYLE_HIDDEN ||
3171
0
       mBorderColors[0] == NS_RGBA(0, 0, 0, 0))) {
3172
0
    // All borders are the same style, and the style is either none or hidden,
3173
0
    // or the color is transparent.
3174
0
    return;
3175
0
  }
3176
0
3177
0
  if (mAllBordersSameWidth && mBorderWidths[0] == 0.0) {
3178
0
    // Some of the mAllBordersSameWidth codepaths depend on the border
3179
0
    // width being greater than zero.
3180
0
    return;
3181
0
  }
3182
0
3183
0
  AutoRestoreTransform autoRestoreTransform;
3184
0
  Matrix mat = mDrawTarget->GetTransform();
3185
0
3186
0
  // Clamp the CTM to be pixel-aligned; we do this only
3187
0
  // for translation-only matrices now, but we could do it
3188
0
  // if the matrix has just a scale as well.  We should not
3189
0
  // do it if there's a rotation.
3190
0
  if (mat.HasNonTranslation()) {
3191
0
    if (!mat.HasNonAxisAlignedTransform()) {
3192
0
      // Scale + transform. Avoid stroke fast-paths so that we have a chance
3193
0
      // of snapping to pixel boundaries.
3194
0
      mAvoidStroke = true;
3195
0
    }
3196
0
  } else {
3197
0
    mat._31 = floor(mat._31 + 0.5);
3198
0
    mat._32 = floor(mat._32 + 0.5);
3199
0
    autoRestoreTransform.Init(mDrawTarget);
3200
0
    mDrawTarget->SetTransform(mat);
3201
0
3202
0
    // round mOuterRect and mInnerRect; they're already an integer
3203
0
    // number of pixels apart and should stay that way after
3204
0
    // rounding. We don't do this if there's a scale in the current transform
3205
0
    // since this loses information that might be relevant when we're scaling.
3206
0
    mOuterRect.Round();
3207
0
    mInnerRect.Round();
3208
0
  }
3209
0
3210
0
  // Initial values only used when the border colors/widths are all the same:
3211
0
  ColorPattern color(ToDeviceColor(mBorderColors[eSideTop]));
3212
0
  StrokeOptions strokeOptions(mBorderWidths[eSideTop]); // stroke width
3213
0
3214
0
  // First there's a couple of 'special cases' that have specifically optimized
3215
0
  // drawing paths, when none of these can be used we move on to the generalized
3216
0
  // border drawing code.
3217
0
  if (mAllBordersSameStyle && mAllBordersSameWidth &&
3218
0
      mBorderStyles[0] == NS_STYLE_BORDER_STYLE_SOLID && mNoBorderRadius &&
3219
0
      !mAvoidStroke) {
3220
0
    // Very simple case.
3221
0
    Rect rect = mOuterRect;
3222
0
    rect.Deflate(mBorderWidths[0] / 2.0);
3223
0
    mDrawTarget->StrokeRect(rect, color, strokeOptions);
3224
0
    return;
3225
0
  }
3226
0
3227
0
  if (mAllBordersSameStyle && mBorderStyles[0] == NS_STYLE_BORDER_STYLE_SOLID &&
3228
0
      !mAvoidStroke && !mNoBorderRadius) {
3229
0
    // Relatively simple case.
3230
0
    RoundedRect borderInnerRect(mOuterRect, mBorderRadii);
3231
0
    borderInnerRect.Deflate(mBorderWidths[eSideTop],
3232
0
                            mBorderWidths[eSideBottom],
3233
0
                            mBorderWidths[eSideLeft],
3234
0
                            mBorderWidths[eSideRight]);
3235
0
3236
0
    // Instead of stroking we just use two paths: an inner and an outer.
3237
0
    // This allows us to draw borders that we couldn't when stroking. For
3238
0
    // example, borders with a border width >= the border radius. (i.e. when
3239
0
    // there are square corners on the inside)
3240
0
    //
3241
0
    // Further, this approach can be more efficient because the backend
3242
0
    // doesn't need to compute an offset curve to stroke the path. We know that
3243
0
    // the rounded parts are elipses we can offset exactly and can just compute
3244
0
    // a new cubic approximation.
3245
0
    RefPtr<PathBuilder> builder = mDrawTarget->CreatePathBuilder();
3246
0
    AppendRoundedRectToPath(builder, mOuterRect, mBorderRadii, true);
3247
0
    AppendRoundedRectToPath(
3248
0
      builder, borderInnerRect.rect, borderInnerRect.corners, false);
3249
0
    RefPtr<Path> path = builder->Finish();
3250
0
    mDrawTarget->Fill(path, color);
3251
0
    return;
3252
0
  }
3253
0
3254
0
  const bool allBordersSolid = AllBordersSolid();
3255
0
3256
0
  // This leaves the border corners non-interpolated for single width borders.
3257
0
  // Doing this is slightly faster and shouldn't be a problem visually.
3258
0
  if (allBordersSolid && mAllBordersSameWidth && mBorderWidths[0] == 1 &&
3259
0
      mNoBorderRadius && !mAvoidStroke) {
3260
0
    DrawSingleWidthSolidBorder();
3261
0
    return;
3262
0
  }
3263
0
3264
0
  if (allBordersSolid && !mAvoidStroke) {
3265
0
    DrawSolidBorder();
3266
0
    return;
3267
0
  }
3268
0
3269
0
  PrintAsString(" mOuterRect: ");
3270
0
  PrintAsString(mOuterRect);
3271
0
  PrintAsStringNewline();
3272
0
  PrintAsString(" mInnerRect: ");
3273
0
  PrintAsString(mInnerRect);
3274
0
  PrintAsStringNewline();
3275
0
  PrintAsFormatString(" mBorderColors: 0x%08x 0x%08x 0x%08x 0x%08x\n",
3276
0
                      mBorderColors[0],
3277
0
                      mBorderColors[1],
3278
0
                      mBorderColors[2],
3279
0
                      mBorderColors[3]);
3280
0
3281
0
  // if conditioning the outside rect failed, then bail -- the outside
3282
0
  // rect is supposed to enclose the entire border
3283
0
  {
3284
0
    gfxRect outerRect = ThebesRect(mOuterRect);
3285
0
    gfxUtils::ConditionRect(outerRect);
3286
0
    if (outerRect.IsEmpty())
3287
0
      return;
3288
0
    mOuterRect = ToRect(outerRect);
3289
0
3290
0
    gfxRect innerRect = ThebesRect(mInnerRect);
3291
0
    gfxUtils::ConditionRect(innerRect);
3292
0
    mInnerRect = ToRect(innerRect);
3293
0
  }
3294
0
3295
0
  int dashedSides = 0;
3296
0
  bool forceSeparateCorners = false;
3297
0
3298
0
  NS_FOR_CSS_SIDES(i)
3299
0
  {
3300
0
    uint8_t style = mBorderStyles[i];
3301
0
    if (style == NS_STYLE_BORDER_STYLE_DASHED ||
3302
0
        style == NS_STYLE_BORDER_STYLE_DOTTED) {
3303
0
      // we need to draw things separately for dashed/dotting
3304
0
      forceSeparateCorners = true;
3305
0
      dashedSides |= (1 << i);
3306
0
    }
3307
0
  }
3308
0
3309
0
  PrintAsFormatString(" mAllBordersSameStyle: %d dashedSides: 0x%02x\n",
3310
0
                      mAllBordersSameStyle,
3311
0
                      dashedSides);
3312
0
3313
0
  if (mAllBordersSameStyle && !forceSeparateCorners) {
3314
0
    /* Draw everything in one go */
3315
0
    DrawBorderSides(eSideBitsAll);
3316
0
    PrintAsStringNewline("---------------- (1)");
3317
0
  } else {
3318
0
    AUTO_PROFILER_LABEL("nsCSSBorderRenderer::DrawBorders:multipass", GRAPHICS);
3319
0
3320
0
    /* We have more than one pass to go.  Draw the corners separately from the
3321
0
     * sides. */
3322
0
3323
0
    // The corner is going to have negligible size if its two adjacent border
3324
0
    // sides are only 1px wide and there is no border radius.  In that case we
3325
0
    // skip the overhead of painting the corner by setting the width or height
3326
0
    // of the corner to zero, which effectively extends one of the corner's
3327
0
    // adjacent border sides.  We extend the longer adjacent side so that
3328
0
    // opposite sides will be the same length, which is necessary for opposite
3329
0
    // dashed/dotted sides to be symmetrical.
3330
0
    //
3331
0
    //   if width > height
3332
0
    //     +--+--------------+--+    +--------------------+
3333
0
    //     |  |              |  |    |                    |
3334
0
    //     +--+--------------+--+    +--+--------------+--+
3335
0
    //     |  |              |  |    |  |              |  |
3336
0
    //     |  |              |  | => |  |              |  |
3337
0
    //     |  |              |  |    |  |              |  |
3338
0
    //     +--+--------------+--+    +--+--------------+--+
3339
0
    //     |  |              |  |    |                    |
3340
0
    //     +--+--------------+--+    +--------------------+
3341
0
    //
3342
0
    //   if width <= height
3343
0
    //     +--+--------+--+    +--+--------+--+
3344
0
    //     |  |        |  |    |  |        |  |
3345
0
    //     +--+--------+--+    |  +--------+  |
3346
0
    //     |  |        |  |    |  |        |  |
3347
0
    //     |  |        |  |    |  |        |  |
3348
0
    //     |  |        |  |    |  |        |  |
3349
0
    //     |  |        |  | => |  |        |  |
3350
0
    //     |  |        |  |    |  |        |  |
3351
0
    //     |  |        |  |    |  |        |  |
3352
0
    //     |  |        |  |    |  |        |  |
3353
0
    //     +--+--------+--+    |  +--------+  |
3354
0
    //     |  |        |  |    |  |        |  |
3355
0
    //     +--+--------+--+    +--+--------+--+
3356
0
    //
3357
0
    // Note that if we have different border widths we could end up with
3358
0
    // opposite sides of different length.  For example, if the left and
3359
0
    // bottom borders are 2px wide instead of 1px, we will end up doing
3360
0
    // something like:
3361
0
    //
3362
0
    //     +----+------------+--+    +----+---------------+
3363
0
    //     |    |            |  |    |    |               |
3364
0
    //     +----+------------+--+    +----+------------+--+
3365
0
    //     |    |            |  |    |    |            |  |
3366
0
    //     |    |            |  | => |    |            |  |
3367
0
    //     |    |            |  |    |    |            |  |
3368
0
    //     +----+------------+--+    +----+------------+--+
3369
0
    //     |    |            |  |    |    |            |  |
3370
0
    //     |    |            |  |    |    |            |  |
3371
0
    //     +----+------------+--+    +----+------------+--+
3372
0
    //
3373
0
    // XXX Should we only do this optimization if |mAllBordersSameWidth| is
3374
0
    // true?
3375
0
    //
3376
0
    // XXX In fact is this optimization even worth the complexity it adds to
3377
0
    // the code?  1px wide dashed borders are not overly common, and drawing
3378
0
    // corners for them is not that expensive.
3379
0
    NS_FOR_CSS_FULL_CORNERS(corner)
3380
0
    {
3381
0
      const mozilla::Side sides[2] = { mozilla::Side(corner),
3382
0
                                       PREV_SIDE(corner) };
3383
0
3384
0
      if (!IsZeroSize(mBorderRadii[corner]))
3385
0
        continue;
3386
0
3387
0
      if (mBorderWidths[sides[0]] == 1.0 && mBorderWidths[sides[1]] == 1.0) {
3388
0
        if (mOuterRect.Width() > mOuterRect.Height()) {
3389
0
          mBorderCornerDimensions[corner].width = 0.0;
3390
0
        } else {
3391
0
          mBorderCornerDimensions[corner].height = 0.0;
3392
0
        }
3393
0
      }
3394
0
    }
3395
0
3396
0
    // First, the corners
3397
0
    NS_FOR_CSS_FULL_CORNERS(corner)
3398
0
    {
3399
0
      // if there's no corner, don't do all this work for it
3400
0
      if (IsZeroSize(mBorderCornerDimensions[corner]))
3401
0
        continue;
3402
0
3403
0
      const int sides[2] = { corner, PREV_SIDE(corner) };
3404
0
      int sideBits = (1 << sides[0]) | (1 << sides[1]);
3405
0
3406
0
      bool simpleCornerStyle = AreBorderSideFinalStylesSame(sideBits);
3407
0
3408
0
      // If we don't have anything complex going on in this corner,
3409
0
      // then we can just fill the corner with a solid color, and avoid
3410
0
      // the potentially expensive clip.
3411
0
      if (simpleCornerStyle && IsZeroSize(mBorderRadii[corner]) &&
3412
0
          IsSolidCornerStyle(mBorderStyles[sides[0]], corner)) {
3413
0
        Color color = MakeBorderColor(
3414
0
          mBorderColors[sides[0]],
3415
0
          mBackgroundColor,
3416
0
          BorderColorStyleForSolidCorner(mBorderStyles[sides[0]], corner));
3417
0
        mDrawTarget->FillRect(GetCornerRect(corner),
3418
0
                              ColorPattern(ToDeviceColor(color)));
3419
0
        continue;
3420
0
      }
3421
0
3422
0
      // clip to the corner
3423
0
      mDrawTarget->PushClipRect(GetCornerRect(corner));
3424
0
3425
0
      if (simpleCornerStyle) {
3426
0
        // we don't need a group for this corner, the sides are the same,
3427
0
        // but we weren't able to render just a solid block for the corner.
3428
0
        DrawBorderSides(sideBits);
3429
0
      } else {
3430
0
        // Sides are different.  We could draw using OP_ADD to
3431
0
        // get correct color blending behaviour at the seam.  We'd need
3432
0
        // to do it in an offscreen surface to ensure that we're
3433
0
        // always compositing on transparent black.  If the colors
3434
0
        // don't have transparency and the current destination surface
3435
0
        // has an alpha channel, we could just clear the region and
3436
0
        // avoid the temporary, but that situation doesn't happen all
3437
0
        // that often in practice (we double buffer to no-alpha
3438
0
        // surfaces). We choose just to seam though, as the performance
3439
0
        // advantages outway the modest easthetic improvement.
3440
0
3441
0
        for (int cornerSide = 0; cornerSide < 2; cornerSide++) {
3442
0
          mozilla::Side side = mozilla::Side(sides[cornerSide]);
3443
0
          uint8_t style = mBorderStyles[side];
3444
0
3445
0
          PrintAsFormatString("corner: %d cornerSide: %d side: %d style: %d\n",
3446
0
                              corner,
3447
0
                              cornerSide,
3448
0
                              side,
3449
0
                              style);
3450
0
3451
0
          RefPtr<Path> path = GetSideClipSubPath(side);
3452
0
          mDrawTarget->PushClip(path);
3453
0
3454
0
          DrawBorderSides(1 << side);
3455
0
3456
0
          mDrawTarget->PopClip();
3457
0
        }
3458
0
      }
3459
0
3460
0
      mDrawTarget->PopClip();
3461
0
3462
0
      PrintAsStringNewline();
3463
0
    }
3464
0
3465
0
    // in the case of a single-unit border, we already munged the
3466
0
    // corners up above; so we can just draw the top left and bottom
3467
0
    // right sides separately, if they're the same.
3468
0
    //
3469
0
    // We need to check for mNoBorderRadius, because when there is
3470
0
    // one, FillSolidBorder always draws the full rounded rectangle
3471
0
    // and expects there to be a clip in place.
3472
0
    int alreadyDrawnSides = 0;
3473
0
    if (mOneUnitBorder && mNoBorderRadius &&
3474
0
        (dashedSides & (eSideBitsTop | eSideBitsLeft)) == 0) {
3475
0
      bool tlBordersSameStyle =
3476
0
        AreBorderSideFinalStylesSame(eSideBitsTop | eSideBitsLeft);
3477
0
      bool brBordersSameStyle =
3478
0
        AreBorderSideFinalStylesSame(eSideBitsBottom | eSideBitsRight);
3479
0
3480
0
      if (tlBordersSameStyle) {
3481
0
        DrawBorderSides(eSideBitsTop | eSideBitsLeft);
3482
0
        alreadyDrawnSides |= (eSideBitsTop | eSideBitsLeft);
3483
0
      }
3484
0
3485
0
      if (brBordersSameStyle &&
3486
0
          (dashedSides & (eSideBitsBottom | eSideBitsRight)) == 0) {
3487
0
        DrawBorderSides(eSideBitsBottom | eSideBitsRight);
3488
0
        alreadyDrawnSides |= (eSideBitsBottom | eSideBitsRight);
3489
0
      }
3490
0
    }
3491
0
3492
0
    // We're done with the corners, now draw the sides.
3493
0
    NS_FOR_CSS_SIDES(side)
3494
0
    {
3495
0
      // if we drew it above, skip it
3496
0
      if (alreadyDrawnSides & (1 << side))
3497
0
        continue;
3498
0
3499
0
      // If there's no border on this side, skip it
3500
0
      if (mBorderWidths[side] == 0.0 ||
3501
0
          mBorderStyles[side] == NS_STYLE_BORDER_STYLE_HIDDEN ||
3502
0
          mBorderStyles[side] == NS_STYLE_BORDER_STYLE_NONE)
3503
0
        continue;
3504
0
3505
0
      if (dashedSides & (1 << side)) {
3506
0
        // Dashed sides will always draw just the part ignoring the
3507
0
        // corners for the side, so no need to clip.
3508
0
        DrawDashedOrDottedSide(side);
3509
0
3510
0
        PrintAsStringNewline("---------------- (d)");
3511
0
        continue;
3512
0
      }
3513
0
3514
0
      // Undashed sides will currently draw the entire side,
3515
0
      // including parts that would normally be covered by a corner,
3516
0
      // so we need to clip.
3517
0
      //
3518
0
      // XXX Optimization -- it would be good to make this work like
3519
0
      // DrawDashedOrDottedSide, and have a DrawOneSide function that just
3520
0
      // draws one side and not the corners, because then we can
3521
0
      // avoid the potentially expensive clip.
3522
0
      mDrawTarget->PushClipRect(GetSideClipWithoutCornersRect(side));
3523
0
3524
0
      DrawBorderSides(1 << side);
3525
0
3526
0
      mDrawTarget->PopClip();
3527
0
3528
0
      PrintAsStringNewline("---------------- (*)");
3529
0
    }
3530
0
  }
3531
0
}
3532
3533
void
3534
nsCSSBorderRenderer::CreateWebRenderCommands(
3535
  nsDisplayItem* aItem,
3536
  wr::DisplayListBuilder& aBuilder,
3537
  wr::IpcResourceUpdateQueue& aResources,
3538
  const layers::StackingContextHelper& aSc)
3539
0
{
3540
0
  LayoutDeviceRect outerRect = LayoutDeviceRect::FromUnknownRect(mOuterRect);
3541
0
  wr::LayoutRect roundedRect = wr::ToRoundedLayoutRect(outerRect);
3542
0
  wr::BorderSide side[4];
3543
0
  NS_FOR_CSS_SIDES(i)
3544
0
  {
3545
0
    side[i] =
3546
0
      wr::ToBorderSide(ToDeviceColor(mBorderColors[i]), mBorderStyles[i]);
3547
0
  }
3548
0
3549
0
  wr::BorderRadius borderRadius =
3550
0
    wr::ToBorderRadius(LayoutDeviceSize::FromUnknownSize(mBorderRadii[0]),
3551
0
                       LayoutDeviceSize::FromUnknownSize(mBorderRadii[1]),
3552
0
                       LayoutDeviceSize::FromUnknownSize(mBorderRadii[3]),
3553
0
                       LayoutDeviceSize::FromUnknownSize(mBorderRadii[2]));
3554
0
3555
0
  if (mLocalClip) {
3556
0
    LayoutDeviceRect clip =
3557
0
      LayoutDeviceRect::FromUnknownRect(mLocalClip.value());
3558
0
    wr::LayoutRect clipRect = wr::ToRoundedLayoutRect(clip);
3559
0
    wr::WrClipId clipId = aBuilder.DefineClip(Nothing(), clipRect);
3560
0
    aBuilder.PushClip(clipId);
3561
0
  }
3562
0
3563
0
  Range<const wr::BorderSide> wrsides(side, 4);
3564
0
  aBuilder.PushBorder(
3565
0
    roundedRect,
3566
0
    roundedRect,
3567
0
    mBackfaceIsVisible,
3568
0
    wr::ToBorderWidths(
3569
0
      mBorderWidths[0], mBorderWidths[1], mBorderWidths[2], mBorderWidths[3]),
3570
0
    wrsides,
3571
0
    borderRadius);
3572
0
3573
0
  if (mLocalClip) {
3574
0
    aBuilder.PopClip();
3575
0
  }
3576
0
}
3577
3578
/* static */ Maybe<nsCSSBorderImageRenderer>
3579
nsCSSBorderImageRenderer::CreateBorderImageRenderer(
3580
  nsPresContext* aPresContext,
3581
  nsIFrame* aForFrame,
3582
  const nsRect& aBorderArea,
3583
  const nsStyleBorder& aStyleBorder,
3584
  const nsRect& aDirtyRect,
3585
  Sides aSkipSides,
3586
  uint32_t aFlags,
3587
  ImgDrawResult* aDrawResult)
3588
0
{
3589
0
  MOZ_ASSERT(aDrawResult);
3590
0
3591
0
  if (aDirtyRect.IsEmpty()) {
3592
0
    *aDrawResult = ImgDrawResult::SUCCESS;
3593
0
    return Nothing();
3594
0
  }
3595
0
3596
0
  nsImageRenderer imgRenderer(
3597
0
    aForFrame, &aStyleBorder.mBorderImageSource, aFlags);
3598
0
  if (!imgRenderer.PrepareImage()) {
3599
0
    *aDrawResult = imgRenderer.PrepareResult();
3600
0
    return Nothing();
3601
0
  }
3602
0
3603
0
  // Ensure we get invalidated for loads and animations of the image.
3604
0
  // We need to do this here because this might be the only code that
3605
0
  // knows about the association of the style data with the frame.
3606
0
  // XXX We shouldn't really... since if anybody is passing in a
3607
0
  // different style, they'll potentially have the wrong size for the
3608
0
  // border too.
3609
0
  aForFrame->AssociateImage(aStyleBorder.mBorderImageSource, aPresContext, 0);
3610
0
3611
0
  nsCSSBorderImageRenderer renderer(
3612
0
    aForFrame, aBorderArea, aStyleBorder, aSkipSides, imgRenderer);
3613
0
  *aDrawResult = ImgDrawResult::SUCCESS;
3614
0
  return Some(renderer);
3615
0
}
3616
3617
ImgDrawResult
3618
nsCSSBorderImageRenderer::DrawBorderImage(nsPresContext* aPresContext,
3619
                                          gfxContext& aRenderingContext,
3620
                                          nsIFrame* aForFrame,
3621
                                          const nsRect& aDirtyRect)
3622
0
{
3623
0
  // NOTE: no Save() yet, we do that later by calling autoSR.EnsureSaved()
3624
0
  // in case we need it.
3625
0
  gfxContextAutoSaveRestore autoSR;
3626
0
3627
0
  if (!mClip.IsEmpty()) {
3628
0
    autoSR.EnsureSaved(&aRenderingContext);
3629
0
    aRenderingContext.Clip(
3630
0
      NSRectToSnappedRect(mClip,
3631
0
                          aForFrame->PresContext()->AppUnitsPerDevPixel(),
3632
0
                          *aRenderingContext.GetDrawTarget()));
3633
0
  }
3634
0
3635
0
  // intrinsicSize.CanComputeConcreteSize() return false means we can not
3636
0
  // read intrinsic size from aStyleBorder.mBorderImageSource.
3637
0
  // In this condition, we pass imageSize(a resolved size comes from
3638
0
  // default sizing algorithm) to renderer as the viewport size.
3639
0
  CSSSizeOrRatio intrinsicSize = mImageRenderer.ComputeIntrinsicSize();
3640
0
  Maybe<nsSize> svgViewportSize =
3641
0
    intrinsicSize.CanComputeConcreteSize() ? Nothing() : Some(mImageSize);
3642
0
  bool hasIntrinsicRatio = intrinsicSize.HasRatio();
3643
0
  mImageRenderer.PurgeCacheForViewportChange(svgViewportSize,
3644
0
                                             hasIntrinsicRatio);
3645
0
3646
0
  // These helper tables recharacterize the 'slice' and 'width' margins
3647
0
  // in a more convenient form: they are the x/y/width/height coords
3648
0
  // required for various bands of the border, and they have been transformed
3649
0
  // to be relative to the innerRect (for 'slice') or the page (for 'border').
3650
0
  enum
3651
0
  {
3652
0
    LEFT,
3653
0
    MIDDLE,
3654
0
    RIGHT,
3655
0
    TOP = LEFT,
3656
0
    BOTTOM = RIGHT
3657
0
  };
3658
0
  const nscoord borderX[3] = {
3659
0
    mArea.x + 0,
3660
0
    mArea.x + mWidths.left,
3661
0
    mArea.x + mArea.width - mWidths.right,
3662
0
  };
3663
0
  const nscoord borderY[3] = {
3664
0
    mArea.y + 0,
3665
0
    mArea.y + mWidths.top,
3666
0
    mArea.y + mArea.height - mWidths.bottom,
3667
0
  };
3668
0
  const nscoord borderWidth[3] = {
3669
0
    mWidths.left,
3670
0
    mArea.width - mWidths.left - mWidths.right,
3671
0
    mWidths.right,
3672
0
  };
3673
0
  const nscoord borderHeight[3] = {
3674
0
    mWidths.top,
3675
0
    mArea.height - mWidths.top - mWidths.bottom,
3676
0
    mWidths.bottom,
3677
0
  };
3678
0
  const int32_t sliceX[3] = {
3679
0
    0,
3680
0
    mSlice.left,
3681
0
    mImageSize.width - mSlice.right,
3682
0
  };
3683
0
  const int32_t sliceY[3] = {
3684
0
    0,
3685
0
    mSlice.top,
3686
0
    mImageSize.height - mSlice.bottom,
3687
0
  };
3688
0
  const int32_t sliceWidth[3] = {
3689
0
    mSlice.left,
3690
0
    std::max(mImageSize.width - mSlice.left - mSlice.right, 0),
3691
0
    mSlice.right,
3692
0
  };
3693
0
  const int32_t sliceHeight[3] = {
3694
0
    mSlice.top,
3695
0
    std::max(mImageSize.height - mSlice.top - mSlice.bottom, 0),
3696
0
    mSlice.bottom,
3697
0
  };
3698
0
3699
0
  ImgDrawResult result = ImgDrawResult::SUCCESS;
3700
0
3701
0
  for (int i = LEFT; i <= RIGHT; i++) {
3702
0
    for (int j = TOP; j <= BOTTOM; j++) {
3703
0
      StyleBorderImageRepeat fillStyleH, fillStyleV;
3704
0
      nsSize unitSize;
3705
0
3706
0
      if (i == MIDDLE && j == MIDDLE) {
3707
0
        // Discard the middle portion unless set to fill.
3708
0
        if (NS_STYLE_BORDER_IMAGE_SLICE_NOFILL == mFill) {
3709
0
          continue;
3710
0
        }
3711
0
3712
0
        NS_ASSERTION(NS_STYLE_BORDER_IMAGE_SLICE_FILL == mFill,
3713
0
                     "Unexpected border image fill");
3714
0
3715
0
        // css-background:
3716
0
        //     The middle image's width is scaled by the same factor as the
3717
0
        //     top image unless that factor is zero or infinity, in which
3718
0
        //     case the scaling factor of the bottom is substituted, and
3719
0
        //     failing that, the width is not scaled. The height of the
3720
0
        //     middle image is scaled by the same factor as the left image
3721
0
        //     unless that factor is zero or infinity, in which case the
3722
0
        //     scaling factor of the right image is substituted, and failing
3723
0
        //     that, the height is not scaled.
3724
0
        gfxFloat hFactor, vFactor;
3725
0
3726
0
        if (0 < mWidths.left && 0 < mSlice.left)
3727
0
          vFactor = gfxFloat(mWidths.left) / mSlice.left;
3728
0
        else if (0 < mWidths.right && 0 < mSlice.right)
3729
0
          vFactor = gfxFloat(mWidths.right) / mSlice.right;
3730
0
        else
3731
0
          vFactor = 1;
3732
0
3733
0
        if (0 < mWidths.top && 0 < mSlice.top)
3734
0
          hFactor = gfxFloat(mWidths.top) / mSlice.top;
3735
0
        else if (0 < mWidths.bottom && 0 < mSlice.bottom)
3736
0
          hFactor = gfxFloat(mWidths.bottom) / mSlice.bottom;
3737
0
        else
3738
0
          hFactor = 1;
3739
0
3740
0
        unitSize.width = sliceWidth[i] * hFactor;
3741
0
        unitSize.height = sliceHeight[j] * vFactor;
3742
0
        fillStyleH = mRepeatModeHorizontal;
3743
0
        fillStyleV = mRepeatModeVertical;
3744
0
3745
0
      } else if (i == MIDDLE) { // top, bottom
3746
0
        // Sides are always stretched to the thickness of their border,
3747
0
        // and stretched proportionately on the other axis.
3748
0
        gfxFloat factor;
3749
0
        if (0 < borderHeight[j] && 0 < sliceHeight[j])
3750
0
          factor = gfxFloat(borderHeight[j]) / sliceHeight[j];
3751
0
        else
3752
0
          factor = 1;
3753
0
3754
0
        unitSize.width = sliceWidth[i] * factor;
3755
0
        unitSize.height = borderHeight[j];
3756
0
        fillStyleH = mRepeatModeHorizontal;
3757
0
        fillStyleV = StyleBorderImageRepeat::Stretch;
3758
0
3759
0
      } else if (j == MIDDLE) { // left, right
3760
0
        gfxFloat factor;
3761
0
        if (0 < borderWidth[i] && 0 < sliceWidth[i])
3762
0
          factor = gfxFloat(borderWidth[i]) / sliceWidth[i];
3763
0
        else
3764
0
          factor = 1;
3765
0
3766
0
        unitSize.width = borderWidth[i];
3767
0
        unitSize.height = sliceHeight[j] * factor;
3768
0
        fillStyleH = StyleBorderImageRepeat::Stretch;
3769
0
        fillStyleV = mRepeatModeVertical;
3770
0
3771
0
      } else {
3772
0
        // Corners are always stretched to fit the corner.
3773
0
        unitSize.width = borderWidth[i];
3774
0
        unitSize.height = borderHeight[j];
3775
0
        fillStyleH = StyleBorderImageRepeat::Stretch;
3776
0
        fillStyleV = StyleBorderImageRepeat::Stretch;
3777
0
      }
3778
0
3779
0
      nsRect destArea(borderX[i], borderY[j], borderWidth[i], borderHeight[j]);
3780
0
      nsRect subArea(sliceX[i], sliceY[j], sliceWidth[i], sliceHeight[j]);
3781
0
      if (subArea.IsEmpty())
3782
0
        continue;
3783
0
3784
0
      nsIntRect intSubArea = subArea.ToOutsidePixels(AppUnitsPerCSSPixel());
3785
0
      result &= mImageRenderer.DrawBorderImageComponent(
3786
0
        aPresContext,
3787
0
        aRenderingContext,
3788
0
        aDirtyRect,
3789
0
        destArea,
3790
0
        CSSIntRect(
3791
0
          intSubArea.x, intSubArea.y, intSubArea.width, intSubArea.height),
3792
0
        fillStyleH,
3793
0
        fillStyleV,
3794
0
        unitSize,
3795
0
        j * (RIGHT + 1) + i,
3796
0
        svgViewportSize,
3797
0
        hasIntrinsicRatio);
3798
0
    }
3799
0
  }
3800
0
3801
0
  return result;
3802
0
}
3803
3804
ImgDrawResult
3805
nsCSSBorderImageRenderer::CreateWebRenderCommands(
3806
  nsDisplayItem* aItem,
3807
  nsIFrame* aForFrame,
3808
  mozilla::wr::DisplayListBuilder& aBuilder,
3809
  mozilla::wr::IpcResourceUpdateQueue& aResources,
3810
  const mozilla::layers::StackingContextHelper& aSc,
3811
  mozilla::layers::WebRenderLayerManager* aManager,
3812
  nsDisplayListBuilder* aDisplayListBuilder)
3813
0
{
3814
0
  if (!mImageRenderer.IsReady()) {
3815
0
    return ImgDrawResult::NOT_READY;
3816
0
  }
3817
0
3818
0
  float widths[4];
3819
0
  float slice[4];
3820
0
  float outset[4];
3821
0
  const int32_t appUnitsPerDevPixel =
3822
0
    aForFrame->PresContext()->AppUnitsPerDevPixel();
3823
0
  NS_FOR_CSS_SIDES(i)
3824
0
  {
3825
0
    slice[i] = (float)(mSlice.Side(i)) / appUnitsPerDevPixel;
3826
0
    widths[i] = (float)(mWidths.Side(i)) / appUnitsPerDevPixel;
3827
0
    outset[i] = (float)(mImageOutset.Side(i)) / appUnitsPerDevPixel;
3828
0
  }
3829
0
3830
0
  LayoutDeviceRect destRect =
3831
0
    LayoutDeviceRect::FromAppUnits(mArea, appUnitsPerDevPixel);
3832
0
  wr::LayoutRect dest = wr::ToRoundedLayoutRect(destRect);
3833
0
3834
0
  wr::LayoutRect clip = dest;
3835
0
  if (!mClip.IsEmpty()) {
3836
0
    LayoutDeviceRect clipRect =
3837
0
      LayoutDeviceRect::FromAppUnits(mClip, appUnitsPerDevPixel);
3838
0
    clip = wr::ToRoundedLayoutRect(clipRect);
3839
0
  }
3840
0
3841
0
  ImgDrawResult drawResult = ImgDrawResult::SUCCESS;
3842
0
  switch (mImageRenderer.GetType()) {
3843
0
    case eStyleImageType_Image: {
3844
0
      uint32_t flags = imgIContainer::FLAG_ASYNC_NOTIFY;
3845
0
      if (aDisplayListBuilder->IsPaintingToWindow()) {
3846
0
        flags |= imgIContainer::FLAG_HIGH_QUALITY_SCALING;
3847
0
      }
3848
0
      if (aDisplayListBuilder->ShouldSyncDecodeImages()) {
3849
0
        flags |= imgIContainer::FLAG_SYNC_DECODE;
3850
0
      }
3851
0
3852
0
      RefPtr<imgIContainer> img = mImageRenderer.GetImage();
3853
0
      Maybe<SVGImageContext> svgContext;
3854
0
      gfx::IntSize decodeSize =
3855
0
        nsLayoutUtils::ComputeImageContainerDrawingParameters(
3856
0
          img, aForFrame, destRect, aSc, flags, svgContext);
3857
0
3858
0
      RefPtr<layers::ImageContainer> container;
3859
0
      drawResult = img->GetImageContainerAtSize(aManager, decodeSize, svgContext,
3860
0
                                                flags, getter_AddRefs(container));
3861
0
      if (!container) {
3862
0
        break;
3863
0
      }
3864
0
3865
0
      mozilla::wr::ImageRendering rendering = wr::ToImageRendering(
3866
0
        nsLayoutUtils::GetSamplingFilterForFrame(aItem->Frame()));
3867
0
      gfx::IntSize size;
3868
0
      Maybe<wr::ImageKey> key =
3869
0
        aManager->CommandBuilder().CreateImageKey(aItem,
3870
0
                                                  container,
3871
0
                                                  aBuilder,
3872
0
                                                  aResources,
3873
0
                                                  rendering,
3874
0
                                                  aSc,
3875
0
                                                  size,
3876
0
                                                  Nothing());
3877
0
      if (key.isNothing()) {
3878
0
        break;
3879
0
      }
3880
0
3881
0
      aBuilder.PushBorderImage(
3882
0
        dest,
3883
0
        clip,
3884
0
        !aItem->BackfaceIsHidden(),
3885
0
        wr::ToBorderWidths(widths[0], widths[1], widths[2], widths[3]),
3886
0
        key.value(),
3887
0
        (float)(mImageSize.width) / appUnitsPerDevPixel,
3888
0
        (float)(mImageSize.height) / appUnitsPerDevPixel,
3889
0
        wr::ToSideOffsets2D_u32(slice[0], slice[1], slice[2], slice[3]),
3890
0
        wr::ToSideOffsets2D_f32(outset[0], outset[1], outset[2], outset[3]),
3891
0
        wr::ToRepeatMode(mRepeatModeHorizontal),
3892
0
        wr::ToRepeatMode(mRepeatModeVertical));
3893
0
      break;
3894
0
    }
3895
0
    case eStyleImageType_Gradient: {
3896
0
      RefPtr<nsStyleGradient> gradientData = mImageRenderer.GetGradientData();
3897
0
      nsCSSGradientRenderer renderer = nsCSSGradientRenderer::Create(
3898
0
        aForFrame->PresContext(), aForFrame->Style(), gradientData, mImageSize);
3899
0
3900
0
      wr::ExtendMode extendMode;
3901
0
      nsTArray<wr::GradientStop> stops;
3902
0
      LayoutDevicePoint lineStart;
3903
0
      LayoutDevicePoint lineEnd;
3904
0
      LayoutDeviceSize gradientRadius;
3905
0
      renderer.BuildWebRenderParameters(
3906
0
        1.0, extendMode, stops, lineStart, lineEnd, gradientRadius);
3907
0
3908
0
      if (gradientData->mShape == NS_STYLE_GRADIENT_SHAPE_LINEAR) {
3909
0
        LayoutDevicePoint startPoint =
3910
0
          LayoutDevicePoint(dest.origin.x, dest.origin.y) + lineStart;
3911
0
        LayoutDevicePoint endPoint =
3912
0
          LayoutDevicePoint(dest.origin.x, dest.origin.y) + lineEnd;
3913
0
3914
0
        aBuilder.PushBorderGradient(
3915
0
          dest,
3916
0
          clip,
3917
0
          !aItem->BackfaceIsHidden(),
3918
0
          wr::ToBorderWidths(widths[0], widths[1], widths[2], widths[3]),
3919
0
          (float)(mImageSize.width) / appUnitsPerDevPixel,
3920
0
          (float)(mImageSize.height) / appUnitsPerDevPixel,
3921
0
          wr::ToSideOffsets2D_u32(slice[0], slice[1], slice[2], slice[3]),
3922
0
          wr::ToLayoutPoint(startPoint),
3923
0
          wr::ToLayoutPoint(endPoint),
3924
0
          stops,
3925
0
          extendMode,
3926
0
          wr::ToSideOffsets2D_f32(outset[0], outset[1], outset[2], outset[3]));
3927
0
      } else {
3928
0
        aBuilder.PushBorderRadialGradient(
3929
0
          dest,
3930
0
          clip,
3931
0
          !aItem->BackfaceIsHidden(),
3932
0
          wr::ToBorderWidths(widths[0], widths[1], widths[2], widths[3]),
3933
0
          wr::ToLayoutPoint(lineStart),
3934
0
          wr::ToLayoutSize(gradientRadius),
3935
0
          stops,
3936
0
          extendMode,
3937
0
          wr::ToSideOffsets2D_f32(outset[0], outset[1], outset[2], outset[3]));
3938
0
      }
3939
0
      break;
3940
0
    }
3941
0
    default:
3942
0
      MOZ_ASSERT_UNREACHABLE("Unsupport border image type");
3943
0
      drawResult = ImgDrawResult::NOT_SUPPORTED;
3944
0
  }
3945
0
3946
0
  return drawResult;
3947
0
}
3948
3949
nsCSSBorderImageRenderer::nsCSSBorderImageRenderer(
3950
  const nsCSSBorderImageRenderer& aRhs)
3951
  : mImageRenderer(aRhs.mImageRenderer)
3952
  , mImageSize(aRhs.mImageSize)
3953
  , mSlice(aRhs.mSlice)
3954
  , mWidths(aRhs.mWidths)
3955
  , mImageOutset(aRhs.mImageOutset)
3956
  , mArea(aRhs.mArea)
3957
  , mClip(aRhs.mClip)
3958
  , mRepeatModeHorizontal(aRhs.mRepeatModeHorizontal)
3959
  , mRepeatModeVertical(aRhs.mRepeatModeVertical)
3960
  , mFill(aRhs.mFill)
3961
0
{
3962
0
  Unused << mImageRenderer.PrepareResult();
3963
0
}
3964
3965
nsCSSBorderImageRenderer&
3966
nsCSSBorderImageRenderer::operator=(const nsCSSBorderImageRenderer& aRhs)
3967
0
{
3968
0
  mImageRenderer = aRhs.mImageRenderer;
3969
0
  mImageSize = aRhs.mImageSize;
3970
0
  mSlice = aRhs.mSlice;
3971
0
  mWidths = aRhs.mWidths;
3972
0
  mImageOutset = aRhs.mImageOutset;
3973
0
  mArea = aRhs.mArea;
3974
0
  mClip = aRhs.mClip;
3975
0
  mRepeatModeHorizontal = aRhs.mRepeatModeHorizontal;
3976
0
  mRepeatModeVertical = aRhs.mRepeatModeVertical;
3977
0
  mFill = aRhs.mFill;
3978
0
  Unused << mImageRenderer.PrepareResult();
3979
0
3980
0
  return *this;
3981
0
}
3982
3983
nsCSSBorderImageRenderer::nsCSSBorderImageRenderer(
3984
  nsIFrame* aForFrame,
3985
  const nsRect& aBorderArea,
3986
  const nsStyleBorder& aStyleBorder,
3987
  Sides aSkipSides,
3988
  const nsImageRenderer& aImageRenderer)
3989
  : mImageRenderer(aImageRenderer)
3990
0
{
3991
0
  // Determine the border image area, which by default corresponds to the
3992
0
  // border box but can be modified by 'border-image-outset'.
3993
0
  // Note that 'border-radius' do not apply to 'border-image' borders per
3994
0
  // <http://dev.w3.org/csswg/css-backgrounds/#corner-clipping>.
3995
0
  nsMargin borderWidths(aStyleBorder.GetComputedBorder());
3996
0
  mImageOutset = aStyleBorder.GetImageOutset();
3997
0
  if (nsCSSRendering::IsBoxDecorationSlice(aStyleBorder) &&
3998
0
      !aSkipSides.IsEmpty()) {
3999
0
    mArea = nsCSSRendering::BoxDecorationRectForBorder(
4000
0
      aForFrame, aBorderArea, aSkipSides, &aStyleBorder);
4001
0
    if (mArea.IsEqualEdges(aBorderArea)) {
4002
0
      // No need for a clip, just skip the sides we don't want.
4003
0
      borderWidths.ApplySkipSides(aSkipSides);
4004
0
      mImageOutset.ApplySkipSides(aSkipSides);
4005
0
      mArea.Inflate(mImageOutset);
4006
0
    } else {
4007
0
      // We're drawing borders around the joined continuation boxes so we need
4008
0
      // to clip that to the slice that we want for this frame.
4009
0
      mArea.Inflate(mImageOutset);
4010
0
      mImageOutset.ApplySkipSides(aSkipSides);
4011
0
      mClip = aBorderArea;
4012
0
      mClip.Inflate(mImageOutset);
4013
0
    }
4014
0
  } else {
4015
0
    mArea = aBorderArea;
4016
0
    mArea.Inflate(mImageOutset);
4017
0
  }
4018
0
4019
0
  // Calculate the image size used to compute slice points.
4020
0
  CSSSizeOrRatio intrinsicSize = mImageRenderer.ComputeIntrinsicSize();
4021
0
  mImageSize = nsImageRenderer::ComputeConcreteSize(
4022
0
    CSSSizeOrRatio(), intrinsicSize, mArea.Size());
4023
0
  mImageRenderer.SetPreferredSize(intrinsicSize, mImageSize);
4024
0
4025
0
  // Compute the used values of 'border-image-slice' and 'border-image-width';
4026
0
  // we do them together because the latter can depend on the former.
4027
0
  nsMargin slice;
4028
0
  nsMargin border;
4029
0
  NS_FOR_CSS_SIDES(s)
4030
0
  {
4031
0
    nsStyleCoord coord = aStyleBorder.mBorderImageSlice.Get(s);
4032
0
    int32_t imgDimension =
4033
0
      SideIsVertical(s) ? mImageSize.width : mImageSize.height;
4034
0
    nscoord borderDimension = SideIsVertical(s) ? mArea.width : mArea.height;
4035
0
    double value;
4036
0
    switch (coord.GetUnit()) {
4037
0
      case eStyleUnit_Percent:
4038
0
        value = coord.GetPercentValue() * imgDimension;
4039
0
        break;
4040
0
      case eStyleUnit_Factor:
4041
0
        value =
4042
0
          nsPresContext::CSSPixelsToAppUnits(NS_lround(coord.GetFactorValue()));
4043
0
        break;
4044
0
      default:
4045
0
        MOZ_ASSERT_UNREACHABLE("unexpected CSS unit for image slice");
4046
0
        value = 0;
4047
0
        break;
4048
0
    }
4049
0
    if (value < 0)
4050
0
      value = 0;
4051
0
    if (value > imgDimension)
4052
0
      value = imgDimension;
4053
0
    mSlice.Side(s) = value;
4054
0
4055
0
    coord = aStyleBorder.mBorderImageWidth.Get(s);
4056
0
    switch (coord.GetUnit()) {
4057
0
      case eStyleUnit_Coord: // absolute dimension
4058
0
        value = coord.GetCoordValue();
4059
0
        break;
4060
0
      case eStyleUnit_Percent:
4061
0
        value = coord.GetPercentValue() * borderDimension;
4062
0
        break;
4063
0
      case eStyleUnit_Factor:
4064
0
        value = coord.GetFactorValue() * borderWidths.Side(s);
4065
0
        break;
4066
0
      case eStyleUnit_Auto: // same as the slice value, in CSS pixels
4067
0
        value = mSlice.Side(s);
4068
0
        break;
4069
0
      default:
4070
0
        MOZ_ASSERT_UNREACHABLE("unexpected CSS unit for border image area "
4071
0
                               "division");
4072
0
        value = 0;
4073
0
        break;
4074
0
    }
4075
0
    // NSToCoordRoundWithClamp rounds towards infinity, but that's OK
4076
0
    // because we expect value to be non-negative.
4077
0
    MOZ_ASSERT(value >= 0);
4078
0
    mWidths.Side(s) = NSToCoordRoundWithClamp(value);
4079
0
    MOZ_ASSERT(mWidths.Side(s) >= 0);
4080
0
  }
4081
0
4082
0
  // "If two opposite border-image-width offsets are large enough that they
4083
0
  // overlap, their used values are proportionately reduced until they no
4084
0
  // longer overlap."
4085
0
  uint32_t combinedBorderWidth =
4086
0
    uint32_t(mWidths.left) + uint32_t(mWidths.right);
4087
0
  double scaleX = combinedBorderWidth > uint32_t(mArea.width)
4088
0
                    ? mArea.width / double(combinedBorderWidth)
4089
0
                    : 1.0;
4090
0
  uint32_t combinedBorderHeight =
4091
0
    uint32_t(mWidths.top) + uint32_t(mWidths.bottom);
4092
0
  double scaleY = combinedBorderHeight > uint32_t(mArea.height)
4093
0
                    ? mArea.height / double(combinedBorderHeight)
4094
0
                    : 1.0;
4095
0
  double scale = std::min(scaleX, scaleY);
4096
0
  if (scale < 1.0) {
4097
0
    mWidths.left *= scale;
4098
0
    mWidths.right *= scale;
4099
0
    mWidths.top *= scale;
4100
0
    mWidths.bottom *= scale;
4101
0
    NS_ASSERTION(mWidths.left + mWidths.right <= mArea.width &&
4102
0
                   mWidths.top + mWidths.bottom <= mArea.height,
4103
0
                 "rounding error in width reduction???");
4104
0
  }
4105
0
4106
0
  mRepeatModeHorizontal = aStyleBorder.mBorderImageRepeatH;
4107
0
  mRepeatModeVertical = aStyleBorder.mBorderImageRepeatV;
4108
0
  mFill = aStyleBorder.mBorderImageFill;
4109
0
}