File: StyleJustifyContent.h

package info (click to toggle)
webkit2gtk 2.51.3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 477,912 kB
  • sloc: cpp: 3,898,343; javascript: 198,215; ansic: 165,229; python: 50,371; asm: 21,819; ruby: 18,095; perl: 16,953; xml: 4,623; sh: 2,398; yacc: 2,356; java: 2,019; lex: 1,358; pascal: 372; makefile: 197
file content (256 lines) | stat: -rw-r--r-- 9,827 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2025 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 <WebCore/RenderStyleConstants.h>
#include <WebCore/StyleContentAlignmentData.h>
#include <WebCore/StyleOverflowPosition.h>
#include <WebCore/StyleValueTypes.h>

namespace WebCore {
namespace Style {

// <'justify-content'> = normal | <content-distribution> | <overflow-position>? [ <content-position> | left | right ]
// https://drafts.csswg.org/css-align/#propdef-justify-content
struct JustifyContent {
     constexpr JustifyContent(CSS::Keyword::Normal);
     constexpr JustifyContent(CSS::Keyword::SpaceBetween);
     constexpr JustifyContent(CSS::Keyword::SpaceAround);
     constexpr JustifyContent(CSS::Keyword::SpaceEvenly);
     constexpr JustifyContent(CSS::Keyword::Stretch);
     constexpr JustifyContent(CSS::Keyword::Center, std::optional<OverflowPosition> = std::nullopt);
     constexpr JustifyContent(CSS::Keyword::Start, std::optional<OverflowPosition> = std::nullopt);
     constexpr JustifyContent(CSS::Keyword::End, std::optional<OverflowPosition> = std::nullopt);
     constexpr JustifyContent(CSS::Keyword::FlexStart, std::optional<OverflowPosition> = std::nullopt);
     constexpr JustifyContent(CSS::Keyword::FlexEnd, std::optional<OverflowPosition> = std::nullopt);
     constexpr JustifyContent(CSS::Keyword::Left, std::optional<OverflowPosition> = std::nullopt);
     constexpr JustifyContent(CSS::Keyword::Right, std::optional<OverflowPosition> = std::nullopt);

    constexpr bool isNormal() const { return primary() == PrimaryKind::Normal; }
    constexpr bool isSpaceBetween() const { return primary() == PrimaryKind::SpaceBetween; }
    constexpr bool isSpaceAround() const { return primary() == PrimaryKind::SpaceAround; }
    constexpr bool isSpaceEvenly() const { return primary() == PrimaryKind::SpaceEvenly; }
    constexpr bool isStretch() const { return primary() == PrimaryKind::Stretch; }
    constexpr bool isCenter() const { return primary() == PrimaryKind::Center; }
    constexpr bool isStart() const { return primary() == PrimaryKind::Start; }
    constexpr bool isEnd() const { return primary() == PrimaryKind::End; }
    constexpr bool isFlexStart() const { return primary() == PrimaryKind::FlexStart; }
    constexpr bool isFlexEnd() const { return primary() == PrimaryKind::FlexEnd; }
    constexpr bool isLeft() const { return primary() == PrimaryKind::Left; }
    constexpr bool isRight() const { return primary() == PrimaryKind::Right; }

    template<typename... F> constexpr decltype(auto) switchOn(F&&...) const;

    constexpr bool operator==(const JustifyContent&) const = default;

    StyleContentAlignmentData resolve(std::optional<StyleContentAlignmentData> = std::nullopt) const;

private:
    enum class PrimaryKind : uint8_t {
        Normal,
        SpaceBetween,
        SpaceAround,
        SpaceEvenly,
        Stretch,
        Center,
        Start,
        End,
        FlexStart,
        FlexEnd,
        Left,
        Right,
    };

    static constexpr bool isContentPosition(PrimaryKind);

    constexpr JustifyContent(PrimaryKind);
    constexpr JustifyContent(PrimaryKind, std::optional<OverflowPosition>);

    constexpr PrimaryKind primary() const;
    constexpr OverflowPositionKind overflowPosition() const;

    PREFERRED_TYPE(PrimaryKind) uint8_t m_primary : 4 { static_cast<uint8_t>(PrimaryKind::Normal) };
    uint8_t m_secondary : 2 { 0 }; // unused or OverflowPositionKind, depending on value of m_primary
};
static_assert(sizeof(JustifyContent) == 1);

constexpr JustifyContent::JustifyContent(CSS::Keyword::Normal)
    : JustifyContent { PrimaryKind::Normal }
{
}

constexpr JustifyContent::JustifyContent(CSS::Keyword::SpaceBetween)
    : JustifyContent { PrimaryKind::SpaceBetween }
{
}

constexpr JustifyContent::JustifyContent(CSS::Keyword::SpaceAround)
    : JustifyContent { PrimaryKind::SpaceAround }
{
}

constexpr JustifyContent::JustifyContent(CSS::Keyword::SpaceEvenly)
    : JustifyContent { PrimaryKind::SpaceEvenly }
{
}

constexpr JustifyContent::JustifyContent(CSS::Keyword::Stretch)
    : JustifyContent { PrimaryKind::Stretch }
{
}

constexpr JustifyContent::JustifyContent(CSS::Keyword::Center, std::optional<OverflowPosition> overflow)
    : JustifyContent { PrimaryKind::Center, overflow }
{
}

constexpr JustifyContent::JustifyContent(CSS::Keyword::Start, std::optional<OverflowPosition> overflow)
    : JustifyContent { PrimaryKind::Start, overflow }
{
}

constexpr JustifyContent::JustifyContent(CSS::Keyword::End, std::optional<OverflowPosition> overflow)
    : JustifyContent { PrimaryKind::End, overflow }
{
}

constexpr JustifyContent::JustifyContent(CSS::Keyword::FlexStart, std::optional<OverflowPosition> overflow)
    : JustifyContent { PrimaryKind::FlexStart, overflow }
{
}

constexpr JustifyContent::JustifyContent(CSS::Keyword::FlexEnd, std::optional<OverflowPosition> overflow)
    : JustifyContent { PrimaryKind::FlexEnd, overflow }
{
}

constexpr JustifyContent::JustifyContent(CSS::Keyword::Left, std::optional<OverflowPosition> overflow)
    : JustifyContent { PrimaryKind::Left, overflow }
{
}

constexpr JustifyContent::JustifyContent(CSS::Keyword::Right, std::optional<OverflowPosition> overflow)
    : JustifyContent { PrimaryKind::Right, overflow }
{
}

constexpr JustifyContent::JustifyContent(PrimaryKind primary)
    : m_primary { static_cast<uint8_t>(primary) }
{
    ASSERT(!isContentPosition(primary));
}

constexpr JustifyContent::JustifyContent(PrimaryKind primary, std::optional<OverflowPosition> overflow)
    : m_primary { static_cast<uint8_t>(primary) }
    , m_secondary { static_cast<uint8_t>(computeKind(overflow)) }
{
    ASSERT(isContentPosition(primary));
}

constexpr JustifyContent::PrimaryKind JustifyContent::primary() const
{
    return static_cast<PrimaryKind>(m_primary);
}

constexpr OverflowPositionKind JustifyContent::overflowPosition() const
{
    RELEASE_ASSERT(isContentPosition(primary()));
    return static_cast<OverflowPositionKind>(m_secondary);
}

constexpr bool JustifyContent::isContentPosition(PrimaryKind primary)
{
    switch (primary) {
    case PrimaryKind::Normal:
    case PrimaryKind::SpaceBetween:
    case PrimaryKind::SpaceAround:
    case PrimaryKind::SpaceEvenly:
    case PrimaryKind::Stretch:
        return false;
    case PrimaryKind::Center:
    case PrimaryKind::Start:
    case PrimaryKind::End:
    case PrimaryKind::FlexStart:
    case PrimaryKind::FlexEnd:
    case PrimaryKind::Left:
    case PrimaryKind::Right:
        return true;
    }
    RELEASE_ASSERT_NOT_REACHED();
}

template<typename... F> constexpr decltype(auto) JustifyContent::switchOn(F&&... f) const
{
    auto visitor = WTF::makeVisitor(std::forward<F>(f)...);

    auto visitContentPosition = [&](auto primaryKeyword) {
        switch (overflowPosition()) {
        case OverflowPositionKind::None:
            return visitor(primaryKeyword);
        case OverflowPositionKind::Unsafe:
            return visitor(SpaceSeparatedTuple { CSS::Keyword::Unsafe { }, primaryKeyword });
        case OverflowPositionKind::Safe:
            return visitor(SpaceSeparatedTuple { CSS::Keyword::Safe { }, primaryKeyword });
        }
        RELEASE_ASSERT_NOT_REACHED();
    };

    switch (primary()) {
    case PrimaryKind::Normal:
        return visitor(CSS::Keyword::Normal { });
    case PrimaryKind::SpaceBetween:
        return visitor(CSS::Keyword::SpaceBetween { });
    case PrimaryKind::SpaceAround:
        return visitor(CSS::Keyword::SpaceAround { });
    case PrimaryKind::SpaceEvenly:
        return visitor(CSS::Keyword::SpaceEvenly { });
    case PrimaryKind::Stretch:
        return visitor(CSS::Keyword::Stretch { });
    case PrimaryKind::Center:
        return visitContentPosition(CSS::Keyword::Center { });
    case PrimaryKind::Start:
        return visitContentPosition(CSS::Keyword::Start { });
    case PrimaryKind::End:
        return visitContentPosition(CSS::Keyword::End { });
    case PrimaryKind::FlexStart:
        return visitContentPosition(CSS::Keyword::FlexStart { });
    case PrimaryKind::FlexEnd:
        return visitContentPosition(CSS::Keyword::FlexEnd { });
    case PrimaryKind::Left:
        return visitContentPosition(CSS::Keyword::Left { });
    case PrimaryKind::Right:
        return visitContentPosition(CSS::Keyword::Right { });
    }
    RELEASE_ASSERT_NOT_REACHED();
}

// MARK: - Conversion

template<> struct CSSValueConversion<JustifyContent> { auto operator()(BuilderState&, const CSSValue&) -> JustifyContent; };

} // namespace Style
} // namespace WebCore

DEFINE_VARIANT_LIKE_CONFORMANCE(WebCore::Style::JustifyContent)