summaryrefslogtreecommitdiff
path: root/layout/base/DottedCornerFinder.h
blob: 79eba5af8b7b6a9ed4416e2be400c197dc20a75f (plain)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#ifndef mozilla_DottedCornerFinder_h_
#define mozilla_DottedCornerFinder_h_

#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/BezierUtils.h"
#include "gfxRect.h"

namespace mozilla {

// Calculate C_i and r_i for each filled/unfilled circles in dotted corner.
// Returns circle with C_{2j} and r_{2j} where 0 < 2j < n.
//
//                            ____-----------+
//                      __----  *****     ###|
//                __----      *********  ####|
//           __---  #####    ***********#####|
//        _--     #########  *****+*****#####+ C_0
//      _-       ########### *** C_1****#####|
//     /         #####+#####  *********  ####|
//    /       .  ### C_2 ###    *****     ###|
//   |            #########       ____-------+
//   |     .        #####____-----
//  |              __----
//  |    .       /
// |           /
// |  *****   |
// | ******* |
// |*********|
// |****+****|
// | C_{n-1} |
// | ******* |
// |  *****  |
// |  #####  |
// | ####### |
// |#########|
// +----+----+
//     C_n

class DottedCornerFinder
{
  typedef mozilla::gfx::Bezier Bezier;
  typedef mozilla::gfx::Float Float;
  typedef mozilla::gfx::Point Point;
  typedef mozilla::gfx::Size Size;

public:
  struct Result
  {
    // Center point of dot and its radius.
    Point C;
    Float r;

    Result(const Point& aC, Float aR)
     : C(aC), r(aR)
    {
      MOZ_ASSERT(aR >= 0);
    }
  };

  //                        aBorderRadiusX
  //                       aCornerDim.width
  //                     |<----------------->|
  //                     |                   | v
  //                   --+-------------___---+--
  //                   ^ |         __--      | |
  //                   | |       _-          | | aR0
  //                   | |     /         aC0 +--
  //                   | |   /               | ^
  //                   | |  |                |
  //  aBorderRadiusY   | | |             __--+
  // aCornerDim.height | ||            _-
  //                   | ||           /
  //                   | |           /
  //                   | |          |
  //                   | |          |
  //                   | |         |
  //                   | |         |
  //                   v |    aCn  |
  //                   --+----+----+
  //                     |    |
  //                     |<-->|
  //                      aRn
  //
  // aCornerDim and (aBorderRadiusX, aBorderRadiusY) can be different when
  // aBorderRadiusX is smaller than aRn*2 or
  // aBorderRadiusY is smaller than aR0*2.
  //
  //                                        aCornerDim.width
  //                                      |<----------------->|
  //                                      |                   |
  //                                      | aBorderRadiusX    |
  //                                      |<--------->|       |
  //                                      |           |       |
  //                   -------------------+-------__--+-------+--
  //                   ^                ^ |     _-            | ^
  //                   |                | |   /               | |
  //                   |                | |  /                | |
  //                   | aBorderRadiusY | | |                 | | aR0
  //                   |                | ||                  | |
  //                   |                | |                   | |
  // aCornerDim.height |                v |                   | v
  //                   |                --+               aC0 +--
  //                   |                  |                   |
  //                   |                  |                   |
  //                   |                  |                   |
  //                   |                  |                   |
  //                   |                  |                   |
  //                   v                  |        aCn        |
  //                   -------------------+---------+---------+
  //                                      |         |
  //                                      |<------->|
  //                                          aRn
  DottedCornerFinder(const Bezier& aOuterBezier, const Bezier& aInnerBezier,
                     mozilla::css::Corner aCorner,
                     Float aBorderRadiusX, Float aBorderRadiusY,
                     const Point& aC0, Float aR0, const Point& aCn, Float aRn,
                     const Size& aCornerDim);

  bool HasMore(void) const;
  Result Next(void);

private:
  static const size_t MAX_LOOP = 32;

  // Bezier control points for the outer curve, the inner curve, and a curve
  // that center points of circles are on (center curve).
  //
  //               ___---+ outer curve
  //           __--      |
  //         _-          |
  //       /        __---+ center curve
  //     /      __--     |
  //    |     /          |
  //   |    /        __--+ inner curve
  //  |    |       _-
  //  |    |      /
  // |     |     /
  // |    |     |
  // |    |     |
  // |    |    |
  // |    |    |
  // |    |    |
  // +----+----+
  Bezier mOuterBezier;
  Bezier mInnerBezier;
  Bezier mCenterBezier;

  mozilla::css::Corner mCorner;

  // Sign of the normal vector used in radius calculation, flipped depends on
  // corner and start and end radii.
  Float mNormalSign;

  // Center points and raii for start and end circles, mR0 >= mRn.
  // mMaxR = max(mR0, mRn)
  //
  //                           v
  //               ___---+------
  //           __--     #|#    | mRn
  //         _-        ##|##   |
  //       /           ##+## ---
  //     /              mCn    ^
  //    |               #|#
  //   |             __--+
  //  |            _-
  //  |           /
  // |           /
  // |          |
  // |          |
  // |  #####  |
  // | ####### |
  // |#########|
  // +----+----+
  // |## mC0 ##|
  // | ####### |
  // |  #####  |
  // |    |
  // |<-->|
  //
  //  mR0
  //
  Point mC0;
  Point mCn;
  Float mR0;
  Float mRn;
  Float mMaxR;

  // Parameters for the center curve with perfect circle and the inner curve.
  // The center curve doesn't necessarily share the origin with others.
  //
  //               ___---+
  //           __--      |
  //         _-          |
  //       /        __-+ |
  //     /      __--     |
  //    |     /          |
  //   |    /        __--+--
  //  |    |       _-    | ^
  //  |    |      /      | |
  // |     |     /       | |
  // |    |     |        | |
  // |    |     |        | | mInnerHeight
  // |    |    |         | |
  // |    +    |         | |
  // |         |         | v
  // +---------+---------+
  //           |         | mInnerCurveOrigin
  //           |<------->|
  //           mInnerWidth
  //
  //               ___---+
  //           __--
  //         _-
  //       /        __-+
  //     /      __--   |
  //    |     /        |
  //   |    /        __--+
  //  |    |       _-  |
  //  |    |      /    |
  // |     |     /     |
  // |    |     |      |
  // |    |     |      |
  // |    |    |       |
  // |    +--- | ------+
  // |    |    |       | mCenterCurveOrigin
  // +    |    +       |
  //      |            |
  //      |            |
  //      |            |
  //      |            |
  //      |<---------->|
  //       mCenterCurveR
  //
  Point mCenterCurveOrigin;
  Float mCenterCurveR;
  Point mInnerCurveOrigin;
  Float mInnerWidth;
  Float mInnerHeight;

  Point mLastC;
  Float mLastR;
  Float mLastT;

  // Overlap between two circles.
  // It uses arc length on PERFECT, SINGLE_CURVE_AND_RADIUS, and SINGLE_CURVE,
  // and direct distance on OTHER.
  Float mBestOverlap;

  // If one of border-widths is 0, do not calculate overlap, and draw circles
  // until it reaches the other side or exceeds mMaxCount.
  bool mHasZeroBorderWidth;
  bool mHasMore;

  // The maximum number of filled/unfilled circles.
  size_t mMaxCount;

  enum {
    //                      radius.width
    //                 |<----------------->|
    //                 |                   |
    //               --+-------------___---+----
    //               ^ |         __--     #|#  ^
    //               | |       _-        ##|## |
    //               | |     /           ##+## | top-width
    //               | |   /             ##|## |
    //               | |  |               #|#  v
    //               | | |             __--+----
    // radius.height | ||            _-
    //               | ||           /
    //               | |           /
    //               | |          |
    //               | |          |
    //               | |  #####  |
    //               | | ####### |
    //               v |#########|
    //               --+----+----+
    //                 |#########|
    //                 | ####### |
    //                 |  #####  |
    //                 |         |
    //                 |<------->|
    //                  left-width

    // * top-width == left-width
    // * radius.width == radius.height
    // * top-width < radius.width * 2
    //
    // All circles has same radii and are on single perfect circle's arc.
    // Overlap is known.
    //
    // Split the perfect circle's arc into 2n segments, each segment's length is
    // top-width * (1 - overlap).  Place each circle's center point C_i on each
    // end of the segment, each circle's radius r_i is top-width / 2
    //
    //                       #####
    //                      #######
    // perfect             #########
    // circle's          ___---+####
    // arc     ##### __--  ## C_0 ##
    //   |    #####_-       ###|###
    //   |   ####+####       ##|##
    //   |   ##/C_i ##         |
    //   |    |######          |
    //   |   | #####           |
    //   +->|                  |
    //     |                   |
    //   ##|##                 |
    //  ###|###                |
    // ####|####               |
    // ####+-------------------+
    // ## C_n ##
    //  #######
    //   #####
    PERFECT,

    // * top-width == left-width
    // * 0.5 < radius.width / radius.height < 2.0
    // * top-width < min(radius.width, radius.height) * 2
    //
    // All circles has same radii and are on single elliptic arc.
    // Overlap is known.
    //
    // Split the elliptic arc into 2n segments, each segment's length is
    // top-width * (1 - overlap).  Place each circle's center point C_i on each
    // end of the segment, each circle's radius r_i is top-width / 2
    //
    //                            #####
    //                           #######
    //             #####        #########
    //            #######   ____----+####
    // elliptic  ######__---    ## C_0 ##
    // arc       ##__+-###       ###|###
    //   |      / # C_i #         ##|##
    //   +--> /    #####            |
    //       |                      |
    //   ###|#                      |
    //  ###|###                     |
    // ####|####                    |
    // ####+------------------------+
    // ## C_n ##
    //  #######
    //   #####
    SINGLE_CURVE_AND_RADIUS,

    // * top-width != left-width
    // * 0 < min(top-width, left-width)
    // * 0.5 < radius.width / radius.height < 2.0
    // * max(top-width, left-width) < min(radius.width, radius.height) * 2
    //
    // All circles are on single elliptic arc.
    // Overlap is unknown.
    //
    // Place each circle's center point C_i on elliptic arc, each circle's
    // radius r_i is the distance between the center point and the inner curve.
    // The arc segment's length between C_i and C_{i-1} is
    // (r_i + r_{i-1}) * (1 - overlap).
    //
    //  outer curve
    //           /
    //          /
    //         /         / center curve
    //        / ####### /
    //       /##       /#
    //      +#        /  #
    //     /#        /    #
    //    / #   C_i /     #
    //   /  #      +      #  /
    //  /   #     /  \    # / inner curve
    //      #    /     \  #/
    //       #  /   r_i  \+
    //        #/       ##/
    //        / ####### /
    //                 /
    SINGLE_CURVE,

    // Other cases.
    // Circles are not on single elliptic arc.
    // Overlap are unknown.
    //
    // Place tangent point innerTangent on the inner curve and find circle's
    // center point C_i and radius r_i where the circle is also tangent to the
    // outer curve.
    // Distance between C_i and C_{i-1} is (r_i + r_{i-1}) * (1 - overlap).
    //
    //  outer curve
    //           /
    //          /
    //         /
    //        / #######
    //       /##       ##
    //      +#           #
    //     /# \           #
    //    / #    \        #
    //   /  #      +      #  /
    //  /   #   C_i  \    # / inner curve
    //      #          \  #/
    //       #      r_i  \+
    //        ##       ##/ innerTangent
    //          ####### /
    //                 /
    OTHER
  } mType;

  size_t mI;
  size_t mCount;

  // Determine mType from parameters.
  void DetermineType(Float aBorderRadiusX, Float aBorderRadiusY);

  // Reset calculation.
  void Reset(void);

  // Find radius for the given tangent point on the inner curve such that the
  // circle is also tangent to the outer curve.
  void FindPointAndRadius(Point& C, Float& r, const Point& innerTangent,
                          const Point& normal, Float t);

  // Find next dot.
  Float FindNext(Float overlap);

  // Find mBestOverlap for parameters.
  void FindBestOverlap(Float aMinR,
                       Float aMinBorderRadius, Float aMaxBorderRadius);

  // Fill corner with dots with given overlap, and return the number of dots
  // and last two dots's overlap.
  bool GetCountAndLastOverlap(Float aOverlap,
                              size_t* aCount, Float* aActualOverlap);
};

} // namespace mozilla

#endif /* mozilla_DottedCornerFinder_h_ */