File: CSSShapeFunction.h

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (393 lines) | stat: -rw-r--r-- 15,700 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright (C) 2024 Samuel Weinig <sam@webkit.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include "CSSFillRule.h"
#include "CSSPosition.h"
#include "CSSPrimitiveNumericTypes.h"

namespace WebCore {
namespace CSS {

// <coordinate-pair> = <length-percentage>{2}
using CoordinatePair = SpaceSeparatedPoint<LengthPercentage<>>;

// <by-to> = by | to
// https://drafts.csswg.org/css-shapes-2/#typedef-shape-by-to
// Indicates if a command is relative or absolute.
using CommandAffinity = std::variant<Keyword::By, Keyword::To>;

// <arc-sweep> = cw | ccw
using ArcSweep        = std::variant<Keyword::Cw, Keyword::Ccw>;

// <arc-size> = large | small
using ArcSize         = std::variant<Keyword::Large, Keyword::Small>;

// <control-point-anchor> = start | end | origin
using ControlPointAnchor = std::variant<Keyword::Start, Keyword::End, Keyword::Origin>;

// <to-position> = to <position>
struct ToPosition {
    static constexpr auto affinity = Keyword::To { };

    Position offset;

    bool operator==(const ToPosition&) const = default;
};
DEFINE_TYPE_WRAPPER_GET(ToPosition, offset);

template<> struct Serialize<ToPosition> { void operator()(StringBuilder&, const SerializationContext&, const ToPosition&); };

// <by-coordinate-pair> = by <coordinate-pair>
struct ByCoordinatePair {
    static constexpr auto affinity = Keyword::By { };

    CoordinatePair offset;

    bool operator==(const ByCoordinatePair&) const = default;
};
DEFINE_TYPE_WRAPPER_GET(ByCoordinatePair, offset);

template<> struct Serialize<ByCoordinatePair> { void operator()(StringBuilder&, const SerializationContext&, const ByCoordinatePair&); };

// <relative-control-point> = [<coordinate-pair> [from [start | end | origin]]?]
// Specified https://github.com/w3c/csswg-drafts/issues/10649#issuecomment-2412816773
struct RelativeControlPoint {
    using Anchor = ControlPointAnchor;

    CoordinatePair offset;
    std::optional<Anchor> anchor;

    bool operator==(const RelativeControlPoint&) const = default;
};
template<size_t I> const auto& get(const RelativeControlPoint& value)
{
    if constexpr (!I)
        return value.offset;
    if constexpr (I == 1)
        return value.anchor;
}
template<> struct Serialize<RelativeControlPoint> { void operator()(StringBuilder&, const SerializationContext&, const RelativeControlPoint&); };

// <to-control-point> = [<position> | <relative-control-point>]
// Specified https://github.com/w3c/csswg-drafts/issues/10649#issuecomment-2412816773
struct AbsoluteControlPoint {
    using Anchor = ControlPointAnchor;

    Position offset;
    std::optional<Anchor> anchor;

    bool operator==(const AbsoluteControlPoint&) const = default;
};
template<size_t I> const auto& get(const AbsoluteControlPoint& value)
{
    if constexpr (!I)
        return value.offset;
    if constexpr (I == 1)
        return value.anchor;
}
template<> struct Serialize<AbsoluteControlPoint> { void operator()(StringBuilder&, const SerializationContext&, const AbsoluteControlPoint&); };

// <move-command> = move [to <position>] | [by <coordinate-pair>]
// https://drafts.csswg.org/css-shapes-2/#typedef-shape-move-command
// Modified by https://github.com/w3c/csswg-drafts/issues/10649#issuecomment-2412816773
struct MoveCommand {
    static constexpr auto name = CSSValueMove;
    using To = ToPosition;
    using By = ByCoordinatePair;
    std::variant<To, By> toBy;

    bool operator==(const MoveCommand&) const = default;
};
DEFINE_TYPE_WRAPPER_GET(MoveCommand, toBy);

template<> struct Serialize<MoveCommand> { void operator()(StringBuilder&, const SerializationContext&, const MoveCommand&); };

// <line-command> = line [to <position>] | [by <coordinate-pair>]
// https://drafts.csswg.org/css-shapes-2/#typedef-shape-line-command
// Modified by https://github.com/w3c/csswg-drafts/issues/10649#issuecomment-2412816773
struct LineCommand {
    static constexpr auto name = CSSValueLine;
    using To = ToPosition;
    using By = ByCoordinatePair;
    std::variant<To, By> toBy;

    bool operator==(const LineCommand&) const = default;
};
DEFINE_TYPE_WRAPPER_GET(LineCommand, toBy);

template<> struct Serialize<LineCommand> { void operator()(StringBuilder&, const SerializationContext&, const LineCommand&); };

// <horizontal-line-command> = hline [ to [ <length-percentage> | left | center | right | x-start | x-end ] | by <length-percentage> ]
// https://drafts.csswg.org/css-shapes-2/#typedef-shape-hv-line-command
// Modified by https://github.com/w3c/csswg-drafts/issues/10649#issuecomment-2426552611
struct HLineCommand {
    static constexpr auto name = CSSValueHline;

    struct To {
        static constexpr auto affinity = Keyword::To { };

        TwoComponentPositionHorizontal offset;

        bool operator==(const To&) const = default;
    };
    struct By {
        static constexpr auto affinity = Keyword::By { };

        LengthPercentage<> offset;

        bool operator==(const By&) const = default;
    };
    std::variant<To, By> toBy;

    bool operator==(const HLineCommand&) const = default;
};
DEFINE_TYPE_WRAPPER_GET(HLineCommand::By, offset);
DEFINE_TYPE_WRAPPER_GET(HLineCommand::To, offset);
DEFINE_TYPE_WRAPPER_GET(HLineCommand, toBy);

template<> struct Serialize<HLineCommand::To> { void operator()(StringBuilder&, const SerializationContext&, const HLineCommand::To&); };
template<> struct Serialize<HLineCommand::By> { void operator()(StringBuilder&, const SerializationContext&, const HLineCommand::By&); };
template<> struct Serialize<HLineCommand> { void operator()(StringBuilder&, const SerializationContext&, const HLineCommand&); };

// <vertical-line-command> = vline [ to [ <length-percentage> | top | center | bottom | y-start | y-end ] | by <length-percentage> ]
// https://drafts.csswg.org/css-shapes-2/#typedef-shape-hv-line-command
// Modified by https://github.com/w3c/csswg-drafts/issues/10649#issuecomment-2426552611
struct VLineCommand {
    static constexpr auto name = CSSValueVline;
    struct To {
        static constexpr auto affinity = Keyword::To { };

        TwoComponentPositionVertical offset;

        bool operator==(const To&) const = default;
    };
    struct By {
        static constexpr auto affinity = Keyword::By { };

        LengthPercentage<> offset;

        bool operator==(const By&) const = default;
    };
    std::variant<To, By> toBy;

    bool operator==(const VLineCommand&) const = default;
};
DEFINE_TYPE_WRAPPER_GET(VLineCommand::By, offset);
DEFINE_TYPE_WRAPPER_GET(VLineCommand::To, offset);
DEFINE_TYPE_WRAPPER_GET(VLineCommand, toBy);

template<> struct Serialize<VLineCommand::To> { void operator()(StringBuilder&, const SerializationContext&, const VLineCommand::To&); };
template<> struct Serialize<VLineCommand::By> { void operator()(StringBuilder&, const SerializationContext&, const VLineCommand::By&); };
template<> struct Serialize<VLineCommand> { void operator()(StringBuilder&, const SerializationContext&, const VLineCommand&); };

// <curve-command> = curve [to <position> with <to-control-point> [/ <to-control-point>]?]
//                       | [by <coordinate-pair> with <relative-control-point> [/ <relative-control-point>]?]
// https://drafts.csswg.org/css-shapes-2/#typedef-shape-curve-command
// Modified by https://github.com/w3c/csswg-drafts/issues/10649#issuecomment-2412816773
struct CurveCommand {
    static constexpr auto name = CSSValueCurve;
    struct To {
        static constexpr auto affinity = Keyword::To { };

        Position offset;
        AbsoluteControlPoint controlPoint1;
        std::optional<AbsoluteControlPoint> controlPoint2;

        bool operator==(const To&) const = default;
    };
    struct By {
        static constexpr auto affinity = Keyword::By { };

        CoordinatePair offset;
        RelativeControlPoint controlPoint1;
        std::optional<RelativeControlPoint> controlPoint2;

        bool operator==(const By&) const = default;
    };
    std::variant<To, By> toBy;

    bool operator==(const CurveCommand&) const = default;
};
template<size_t I> const auto& get(const CurveCommand::To& value)
{
    if constexpr (!I)
        return value.offset;
    if constexpr (I == 1)
        return value.controlPoint1;
    if constexpr (I == 2)
        return value.controlPoint2;
}
template<size_t I> const auto& get(const CurveCommand::By& value)
{
    if constexpr (!I)
        return value.offset;
    if constexpr (I == 1)
        return value.controlPoint1;
    if constexpr (I == 2)
        return value.controlPoint2;
}
DEFINE_TYPE_WRAPPER_GET(CurveCommand, toBy);

template<> struct Serialize<CurveCommand::To> { void operator()(StringBuilder&, const SerializationContext&, const CurveCommand::To&); };
template<> struct Serialize<CurveCommand::By> { void operator()(StringBuilder&, const SerializationContext&, const CurveCommand::By&); };
template<> struct Serialize<CurveCommand> { void operator()(StringBuilder&, const SerializationContext&, const CurveCommand&); };

// <smooth-command> = smooth [to <position> [with <to-control-point>]?]
//                         | [by <coordinate-pair> [with <relative-control-point>]?]
// https://drafts.csswg.org/css-shapes-2/#typedef-shape-smooth-command
// Modified by https://github.com/w3c/csswg-drafts/issues/10649#issuecomment-2412816773
struct SmoothCommand {
    static constexpr auto name = CSSValueSmooth;
    struct To {
        static constexpr auto affinity = Keyword::To { };

        Position offset;
        std::optional<AbsoluteControlPoint> controlPoint;

        bool operator==(const To&) const = default;
    };
    struct By {
        static constexpr auto affinity = Keyword::By { };

        CoordinatePair offset;
        std::optional<RelativeControlPoint> controlPoint;

        bool operator==(const By&) const = default;
    };
    std::variant<To, By> toBy;

    bool operator==(const SmoothCommand&) const = default;
};
template<size_t I> const auto& get(const SmoothCommand::To& value)
{
    if constexpr (!I)
        return value.offset;
    if constexpr (I == 1)
        return value.controlPoint;
}
template<size_t I> const auto& get(const SmoothCommand::By& value)
{
    if constexpr (!I)
        return value.offset;
    if constexpr (I == 1)
        return value.controlPoint;
}
DEFINE_TYPE_WRAPPER_GET(SmoothCommand, toBy);

template<> struct Serialize<SmoothCommand::To> { void operator()(StringBuilder&, const SerializationContext&, const SmoothCommand::To&); };
template<> struct Serialize<SmoothCommand::By> { void operator()(StringBuilder&, const SerializationContext&, const SmoothCommand::By&); };
template<> struct Serialize<SmoothCommand> { void operator()(StringBuilder&, const SerializationContext&, const SmoothCommand&); };

// <arc-command> = arc [to <position>] | [by <coordinate-pair>] of <length-percentage>{1,2} [<arc-sweep>? || <arc-size>? || [rotate <angle>]?]
// https://drafts.csswg.org/css-shapes-2/#typedef-shape-arc-command
// Modified by https://github.com/w3c/csswg-drafts/issues/10649#issuecomment-2412816773
struct ArcCommand {
    static constexpr auto name = CSSValueArc;
    using To = ToPosition;
    using By = ByCoordinatePair;
    std::variant<To, By> toBy;

    using SizeOfEllipse = SpaceSeparatedSize<LengthPercentage<>>;
    SizeOfEllipse size;

    ArcSweep arcSweep;
    ArcSize arcSize;
    Angle<> rotation;

    bool operator==(const ArcCommand&) const = default;
};
template<size_t I> const auto& get(const ArcCommand& value)
{
    if constexpr (!I)
        return value.toBy;
    if constexpr (I == 1)
        return value.size;
    if constexpr (I == 2)
        return value.arcSweep;
    if constexpr (I == 3)
        return value.arcSize;
    if constexpr (I == 4)
        return value.rotation;
}
template<> struct Serialize<ArcCommand> { void operator()(StringBuilder&, const SerializationContext&, const ArcCommand&); };

// <close> = close
// https://drafts.csswg.org/css-shapes-2/#valdef-shape-close
using CloseCommand = Keyword::Close;

// <shape-command> = <move-command> | <line-command> | <hv-line-command> | <curve-command> | <smooth-command> | <arc-command> | close
// https://drafts.csswg.org/css-shapes-2/#typedef-shape-command
using ShapeCommand = std::variant<MoveCommand, LineCommand, HLineCommand, VLineCommand, CurveCommand, SmoothCommand, ArcCommand, CloseCommand>;

// shape() = shape( <'fill-rule'>? from <coordinate-pair>, <shape-command>#)
// https://drafts.csswg.org/css-shapes-2/#shape-function
struct Shape {
    using Commands = CommaSeparatedVector<ShapeCommand>;

    std::optional<FillRule> fillRule;
    // FIXME: The spec says this should be a <coordinate-pair>, but the tests and some comments indicate it has changed to position.
    Position startingPoint;
    Commands commands;

    bool operator==(const Shape&) const = default;
};
using ShapeFunction = FunctionNotation<CSSValueShape, Shape>;

template<size_t I> const auto& get(const Shape& value)
{
    if constexpr (!I)
        return value.fillRule;
    else if constexpr (I == 1)
        return value.startingPoint;
    else if constexpr (I == 2)
        return value.commands;
}

template<> struct Serialize<Shape> { void operator()(StringBuilder&, const SerializationContext&, const Shape&); };

} // namespace CSS
} // namespace WebCore

DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::ToPosition, 1)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::ByCoordinatePair, 1)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::RelativeControlPoint, 2)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::AbsoluteControlPoint, 2)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::MoveCommand, 1)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::LineCommand, 1)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::HLineCommand::To, 1)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::HLineCommand::By, 1)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::HLineCommand, 1)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::VLineCommand::To, 1)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::VLineCommand::By, 1)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::VLineCommand, 1)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::CurveCommand::To, 3)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::CurveCommand::By, 3)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::CurveCommand, 1)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::SmoothCommand::To, 2)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::SmoothCommand::By, 2)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::SmoothCommand, 1)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::ArcCommand, 5)
DEFINE_TUPLE_LIKE_CONFORMANCE(WebCore::CSS::Shape, 3)