1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "DashedCornerFinder.h"
#include <utility>
#include "BorderCache.h"
#include "BorderConsts.h"
#include "nsTHashMap.h"
namespace mozilla {
using namespace gfx;
struct BestDashLength {
typedef mozilla::gfx::Float Float;
Float dashLength;
size_t count;
BestDashLength() : dashLength(0.0f), count(0) {}
BestDashLength(Float aDashLength, size_t aCount)
: dashLength(aDashLength), count(aCount) {}
};
static const size_t DashedCornerCacheSize = 256;
nsTHashMap<FourFloatsHashKey, BestDashLength> DashedCornerCache;
DashedCornerFinder::DashedCornerFinder(const Bezier& aOuterBezier,
const Bezier& aInnerBezier,
Float aBorderWidthH, Float aBorderWidthV,
const Size& aCornerDim)
: mOuterBezier(aOuterBezier),
mInnerBezier(aInnerBezier),
mLastOuterP(aOuterBezier.mPoints[0]),
mLastInnerP(aInnerBezier.mPoints[0]),
mLastOuterT(0.0f),
mLastInnerT(0.0f),
mBestDashLength(DOT_LENGTH * DASH_LENGTH),
mHasZeroBorderWidth(false),
mHasMore(true),
mMaxCount(aCornerDim.width + aCornerDim.height),
mType(OTHER),
mI(0),
mCount(0) {
NS_ASSERTION(aBorderWidthH > 0.0f || aBorderWidthV > 0.0f,
"At least one side should have non-zero width.");
DetermineType(aBorderWidthH, aBorderWidthV);
Reset();
}
void DashedCornerFinder::DetermineType(Float aBorderWidthH,
Float aBorderWidthV) {
if (aBorderWidthH < aBorderWidthV) {
// Always draw from wider side to thinner side.
std::swap(mInnerBezier.mPoints[0], mInnerBezier.mPoints[3]);
std::swap(mInnerBezier.mPoints[1], mInnerBezier.mPoints[2]);
std::swap(mOuterBezier.mPoints[0], mOuterBezier.mPoints[3]);
std::swap(mOuterBezier.mPoints[1], mOuterBezier.mPoints[2]);
mLastOuterP = mOuterBezier.mPoints[0];
mLastInnerP = mInnerBezier.mPoints[0];
}
// See the comment at mType declaration for each condition.
Float borderRadiusA =
fabs(mOuterBezier.mPoints[0].x - mOuterBezier.mPoints[3].x);
Float borderRadiusB =
fabs(mOuterBezier.mPoints[0].y - mOuterBezier.mPoints[3].y);
if (aBorderWidthH == aBorderWidthV && borderRadiusA == borderRadiusB &&
borderRadiusA > aBorderWidthH * 2.0f) {
Float curveHeight = borderRadiusA - aBorderWidthH / 2.0;
mType = PERFECT;
Float borderLength = M_PI * curveHeight / 2.0f;
Float dashWidth = aBorderWidthH * DOT_LENGTH * DASH_LENGTH;
size_t count = ceil(borderLength / dashWidth);
if (count % 2) {
count++;
}
mCount = count / 2 + 1;
mBestDashLength = borderLength / (aBorderWidthH * count);
}
Float minBorderWidth = std::min(aBorderWidthH, aBorderWidthV);
if (minBorderWidth == 0.0f) {
mHasZeroBorderWidth = true;
}
if (mType == OTHER && !mHasZeroBorderWidth) {
Float minBorderRadius = std::min(borderRadiusA, borderRadiusB);
Float maxBorderRadius = std::max(borderRadiusA, borderRadiusB);
Float maxBorderWidth = std::max(aBorderWidthH, aBorderWidthV);
FindBestDashLength(minBorderWidth, maxBorderWidth, minBorderRadius,
maxBorderRadius);
}
}
bool DashedCornerFinder::HasMore(void) const {
if (mHasZeroBorderWidth) {
return mI < mMaxCount && mHasMore;
}
return mI < mCount;
}
DashedCornerFinder::Result DashedCornerFinder::Next(void) {
Float lastOuterT, lastInnerT, outerT, innerT;
if (mI == 0) {
lastOuterT = 0.0f;
lastInnerT = 0.0f;
} else {
if (mType == PERFECT) {
lastOuterT = lastInnerT = (mI * 2.0f - 0.5f) / ((mCount - 1) * 2.0f);
} else {
Float last2OuterT = mLastOuterT;
Float last2InnerT = mLastInnerT;
(void)FindNext(mBestDashLength);
//
// mLastOuterT lastOuterT
// | |
// v v
// +---+---+---+---+ <- last2OuterT
// | |###|###| |
// | |###|###| |
// | |###|###| |
// +---+---+---+---+ <- last2InnerT
// ^ ^
// | |
// mLastInnerT lastInnerT
lastOuterT = (mLastOuterT + last2OuterT) / 2.0f;
lastInnerT = (mLastInnerT + last2InnerT) / 2.0f;
}
}
if ((!mHasZeroBorderWidth && mI == mCount - 1) ||
(mHasZeroBorderWidth && !mHasMore)) {
outerT = 1.0f;
innerT = 1.0f;
} else {
if (mType == PERFECT) {
outerT = innerT = (mI * 2.0f + 0.5f) / ((mCount - 1) * 2.0f);
} else {
Float last2OuterT = mLastOuterT;
Float last2InnerT = mLastInnerT;
(void)FindNext(mBestDashLength);
//
// outerT last2OuterT
// | |
// v v
// mLastOuterT -> +---+---+---+---+
// | |###|###| |
// | |###|###| |
// | |###|###| |
// mLastInnerT -> +---+---+---+---+
// ^ ^
// | |
// innerT last2InnerT
outerT = (mLastOuterT + last2OuterT) / 2.0f;
innerT = (mLastInnerT + last2InnerT) / 2.0f;
}
}
mI++;
Bezier outerSectionBezier;
Bezier innerSectionBezier;
GetSubBezier(&outerSectionBezier, mOuterBezier, lastOuterT, outerT);
GetSubBezier(&innerSectionBezier, mInnerBezier, lastInnerT, innerT);
return DashedCornerFinder::Result(outerSectionBezier, innerSectionBezier);
}
void DashedCornerFinder::Reset(void) {
mLastOuterP = mOuterBezier.mPoints[0];
mLastInnerP = mInnerBezier.mPoints[0];
mLastOuterT = 0.0f;
mLastInnerT = 0.0f;
mHasMore = true;
}
Float DashedCornerFinder::FindNext(Float dashLength) {
Float upper = 1.0f;
Float lower = mLastOuterT;
Point OuterP, InnerP;
// Start from upper bound to check if this is the last segment.
Float outerT = upper;
Float innerT;
Float W = 0.0f;
Float L = 0.0f;
const Float LENGTH_MARGIN = 0.1f;
for (size_t i = 0; i < MAX_LOOP; i++) {
OuterP = GetBezierPoint(mOuterBezier, outerT);
InnerP = FindBezierNearestPoint(mInnerBezier, OuterP, outerT, &innerT);
// Calculate approximate dash length.
//
// W = (W1 + W2) / 2
// L = (OuterL + InnerL) / 2
// dashLength = L / W
//
// ____----+----____
// OuterP ___--- | ---___ mLastOuterP
// +--- | ---+
// | | |
// | | |
// | W | W1 |
// | | |
// W2 | | |
// | | ______------+
// | ____+---- mLastInnerP
// | ___---
// | __---
// +--
// InnerP
// OuterL
// ____---------____
// OuterP ___--- ---___ mLastOuterP
// +--- ---+
// | L |
// | ___----------______ |
// | __--- -----+
// | __-- |
// +-- |
// | InnerL ______------+
// | ____----- mLastInnerP
// | ___---
// | __---
// +--
// InnerP
Float W1 = (mLastOuterP - mLastInnerP).Length();
Float W2 = (OuterP - InnerP).Length();
Float OuterL = GetBezierLength(mOuterBezier, mLastOuterT, outerT);
Float InnerL = GetBezierLength(mInnerBezier, mLastInnerT, innerT);
W = (W1 + W2) / 2.0f;
L = (OuterL + InnerL) / 2.0f;
if (L > W * dashLength + LENGTH_MARGIN) {
if (i > 0) {
upper = outerT;
}
} else if (L < W * dashLength - LENGTH_MARGIN) {
if (i == 0) {
// This is the last segment with shorter dashLength.
mHasMore = false;
break;
}
lower = outerT;
} else {
break;
}
outerT = (upper + lower) / 2.0f;
}
mLastOuterP = OuterP;
mLastInnerP = InnerP;
mLastOuterT = outerT;
mLastInnerT = innerT;
if (W == 0.0f) {
return 1.0f;
}
return L / W;
}
void DashedCornerFinder::FindBestDashLength(Float aMinBorderWidth,
Float aMaxBorderWidth,
Float aMinBorderRadius,
Float aMaxBorderRadius) {
// If dashLength is not calculateable, find it with binary search,
// such that there exists i that OuterP_i == OuterP_n and
// InnerP_i == InnerP_n with given dashLength.
FourFloats key(aMinBorderWidth, aMaxBorderWidth, aMinBorderRadius,
aMaxBorderRadius);
BestDashLength best;
if (DashedCornerCache.Get(key, &best)) {
mCount = best.count;
mBestDashLength = best.dashLength;
return;
}
Float lower = 1.0f;
Float upper = DOT_LENGTH * DASH_LENGTH;
Float dashLength = upper;
size_t targetCount = 0;
const Float LENGTH_MARGIN = 0.1f;
for (size_t j = 0; j < MAX_LOOP; j++) {
size_t count;
Float actualDashLength;
if (!GetCountAndLastDashLength(dashLength, &count, &actualDashLength)) {
if (j == 0) {
mCount = mMaxCount;
break;
}
}
if (j == 0) {
if (count == 1) {
// If only 1 segment fits, fill entire region
//
// count = 1
// mCount = 1
// | 1 |
// +---+---+
// |###|###|
// |###|###|
// |###|###|
// +---+---+
// 1
mCount = 1;
break;
}
// targetCount should be 2n.
//
// targetCount = 2
// mCount = 2
// | 1 | 2 |
// +---+---+---+---+
// |###| | |###|
// |###| | |###|
// |###| | |###|
// +---+---+---+---+
// 1 2
//
// targetCount = 6
// mCount = 4
// | 1 | 2 | 3 | 4 | 5 | 6 |
// +---+---+---+---+---+---+---+---+---+---+---+---+
// |###| | |###|###| | |###|###| | |###|
// |###| | |###|###| | |###|###| | |###|
// |###| | |###|###| | |###|###| | |###|
// +---+---+---+---+---+---+---+---+---+---+---+---+
// 1 2 3 4
if (count % 2) {
targetCount = count + 1;
} else {
targetCount = count;
}
mCount = targetCount / 2 + 1;
}
if (count == targetCount) {
mBestDashLength = dashLength;
// actualDashLength won't be greater than dashLength.
if (actualDashLength > dashLength - LENGTH_MARGIN) {
break;
}
// We started from upper bound, no need to update range when j == 0.
if (j > 0) {
upper = dashLength;
}
} else {
// |j == 0 && count != targetCount| means that |targetCount = count + 1|,
// and we started from upper bound, no need to update range when j == 0.
if (j > 0) {
if (count > targetCount) {
lower = dashLength;
} else {
upper = dashLength;
}
}
}
dashLength = (upper + lower) / 2.0f;
}
if (DashedCornerCache.Count() > DashedCornerCacheSize) {
DashedCornerCache.Clear();
}
DashedCornerCache.InsertOrUpdate(key,
BestDashLength(mBestDashLength, mCount));
}
bool DashedCornerFinder::GetCountAndLastDashLength(Float aDashLength,
size_t* aCount,
Float* aActualDashLength) {
// Return the number of segments and the last segment's dashLength for
// the given dashLength.
Reset();
for (size_t i = 0; i < mMaxCount; i++) {
Float actualDashLength = FindNext(aDashLength);
if (mLastOuterT >= 1.0f) {
*aCount = i + 1;
*aActualDashLength = actualDashLength;
return true;
}
}
return false;
}
} // namespace mozilla
|