summaryrefslogtreecommitdiff
path: root/layout/base/DashedCornerFinder.h
blob: 1fda9b6bd49f3aa00496614b908f0a4c1713f556 (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
/* -*- 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_DashedCornerFinder_h_
#define mozilla_DashedCornerFinder_h_

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

namespace mozilla {

// Calculate {OuterT_i, InnerT_i} for each 1 < i < n, that
//   (OuterL_i + InnerL_i) / 2 == dashLength * (W_i + W_{i-1}) / 2
// where
//   OuterP_i: OuterCurve(OuterT_i)
//   InnerP_i: InnerCurve(OuterT_i)
//   OuterL_i: Elliptic arc length between OuterP_i - OuterP_{i-1}
//   InnerL_i: Elliptic arc length between InnerP_i - InnerP_{i-1}
//   W_i = |OuterP_i - InnerP_i|
//   1.0 < dashLength < 3.0
//
//                                         OuterP_1          OuterP_0
//                                         _+__-----------+ OuterCurve
//                          OuterP_2 __---- |   OuterL_1  |
//                             __+---       |             |
//                        __---  | OuterL_2 |             |
//            OuterP_3 _--       |           | W_1        | W_0
//                   _+           |          |            |
//                  /  \       W_2 |         |            |
//                 /    \          |          | InnerL_1  |
//                |      \          | InnerL_2|____-------+ InnerCurve
//                |       \          |____----+          InnerP_0
//               |     .   \    __---+       InnerP_1
//               |          \ /     InnerP_2
//              |     .     /+ InnerP_3
//              |          |
//              |    .    |
//              |         |
//              |        |
//              |        |
// OuterP_{n-1} +--------+ InnerP_{n-1}
//              |        |
//              |        |
//              |        |
//              |        |
//              |        |
//     OuterP_n +--------+ InnerP_n
//
// Returns region with [OuterCurve((OuterT_{2j} + OuterT_{2j-1}) / 2),
//                      OuterCurve((OuterT_{2j} + OuterT_{2j-1}) / 2),
//                      InnerCurve((OuterT_{2j} + OuterT_{2j+1}) / 2),
//                      InnerCurve((OuterT_{2j} + OuterT_{2j+1}) / 2)],
// to start and end with half segment.
//
//                                     _+__----+------+ OuterCurve
//                               _+---- |      |######|
//                         __+---#|     |      |######|
//                    _+---##|####|     |      |######|
//                 _-- |#####|#####|     |      |#####|
//               _+     |#####|#####|    |      |#####|
//              /  \     |#####|####|    |      |#####|
//             /    \     |####|#####|    |     |#####|
//            |      \     |####|####|    |____-+-----+ InnerCurve
//            |       \     |####|____+---+
//           |     .   \   __+---+
//           |          \ /
//          |     .     /+
//          |          |
//          |    .    |
//          |         |
//          |        |
//          |        |
//          +--------+
//          |        |
//          |        |
//          +--------+
//          |########|
//          |########|
//          +--------+

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

public:
  struct Result
  {
    // Control points for the outer curve and the inner curve.
    //
    //   outerSectionBezier
    //        |
    //        v     _+ 3
    //        ___---#|
    //  0 +---#######|
    //    |###########|
    //     |###########|
    //      |##########|
    //       |##########|
    //        |#########|
    //         |#####____+ 3
    //        0 +----
    //              ^
    //              |
    //   innerSectionBezier
    Bezier outerSectionBezier;
    Bezier innerSectionBezier;

    Result(const Bezier& aOuterSectionBezier,
           const Bezier& aInnerSectionBezier)
     : outerSectionBezier(aOuterSectionBezier),
       innerSectionBezier(aInnerSectionBezier)
    {}
  };

  //                       aCornerDim.width
  //                     |<----------------->|
  //                     |                   |
  //                   --+-------------___---+--
  //                   ^ |         __--      | ^
  //                   | |       _-          | |
  //                   | |     /             | | aBorderWidthH
  //                   | |   /               | |
  //                   | |  |                | v
  //                   | | |             __--+--
  // aCornerDim.height | ||            _-
  //                   | ||           /
  //                   | |           /
  //                   | |          |
  //                   | |          |
  //                   | |         |
  //                   | |         |
  //                   v |         |
  //                   --+---------+
  //                     |         |
  //                     |<------->|
  //                     aBorderWidthV
  DashedCornerFinder(const Bezier& aOuterBezier, const Bezier& aInnerBezier,
                     Float aBorderWidthH, Float aBorderWidthV,
                     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 and the inner curve.
  //
  //               ___---+ outer curve
  //           __--      |
  //         _-          |
  //       /             |
  //     /               |
  //    |                |
  //   |             __--+ inner curve
  //  |            _-
  //  |           /
  // |           /
  // |          |
  // |          |
  // |         |
  // |         |
  // |         |
  // +---------+
  Bezier mOuterBezier;
  Bezier mInnerBezier;

  Point mLastOuterP;
  Point mLastInnerP;
  Float mLastOuterT;
  Float mLastInnerT;

  // Length for each segment, ratio of the border width at that point.
  Float mBestDashLength;

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

  // The maximum number of segments.
  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
    //
    // Split the perfect circle's arc into 2n segments, each segment's length is
    // top-width * dashLength. Then split the inner curve and the outer curve
    // with same angles.
    //
    //                        radius.width
    //                 |<---------------------->|
    //                 |                        | v
    //               --+------------------------+--
    //               ^ |                        | | top-width / 2
    //               | | perfect                | |
    //               | | circle's         ___---+--
    //               | | arc          __-+      | ^
    //               | |  |         _-   |      |
    // radius.height | |  |       +       |     +--
    //               | |  |     /  \      |     |
    //               | |  |    |     \     |    |
    //               | |  |   |       \     |   |
    //               | |  +->|          \   |   |
    //               | |    +---__       \   |  |
    //               | |    |     --__     \  | |
    //               | |    |         ---__ \ | |
    //               v |    |              --_\||
    //               --+----+----+--------------+
    //                 |    |    |
    //                 |<-->|    |
    //               left-width / 2
    PERFECT,

    // Other cases.
    //
    // Split the outer curve and the inner curve into 2n segments, each segment
    // satisfies following:
    //   (OuterL_i + InnerL_i) / 2 == dashLength * (W_i + W_{i-1}) / 2
    OTHER
  } mType;

  size_t mI;
  size_t mCount;

  // Determine mType from parameters.
  void DetermineType(Float aBorderWidthH, Float aBorderWidthV);

  // Reset calculation.
  void Reset(void);

  // Find next segment.
  Float FindNext(Float dashLength);

  // Find mBestDashLength for parameters.
  void FindBestDashLength(Float aMinBorderWidth, Float aMaxBorderWidth,
                          Float aMinBorderRadius, Float aMaxBorderRadius);

  // Fill corner with dashes with given dash length, and return the number of
  // segments and last segment's dash length.
  bool GetCountAndLastDashLength(Float aDashLength,
                                 size_t* aCount, Float* aActualDashLength);
};

} // namespace mozilla

#endif /* mozilla_DashedCornerFinder_h_ */