File: StyleAlignContent.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 (259 lines) | stat: -rw-r--r-- 10,296 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
257
258
259
/*
 * 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/StyleBaselineAlignmentPreference.h>
#include <WebCore/StyleContentAlignmentData.h>
#include <WebCore/StyleOverflowPosition.h>
#include <WebCore/StyleValueTypes.h>

namespace WebCore {
namespace Style {

// <'align-content'> = normal | <baseline-position> | <content-distribution> | <overflow-position>? <content-position>
// https://drafts.csswg.org/css-align/#propdef-align-content
struct AlignContent {
    constexpr AlignContent(CSS::Keyword::Normal);
    constexpr AlignContent(CSS::Keyword::Baseline, std::optional<BaselineAlignmentPreference> = std::nullopt);
    constexpr AlignContent(CSS::Keyword::SpaceBetween);
    constexpr AlignContent(CSS::Keyword::SpaceAround);
    constexpr AlignContent(CSS::Keyword::SpaceEvenly);
    constexpr AlignContent(CSS::Keyword::Stretch);
    constexpr AlignContent(CSS::Keyword::Center, std::optional<OverflowPosition> = std::nullopt);
    constexpr AlignContent(CSS::Keyword::Start, std::optional<OverflowPosition> = std::nullopt);
    constexpr AlignContent(CSS::Keyword::End, std::optional<OverflowPosition> = std::nullopt);
    constexpr AlignContent(CSS::Keyword::FlexStart, std::optional<OverflowPosition> = std::nullopt);
    constexpr AlignContent(CSS::Keyword::FlexEnd, std::optional<OverflowPosition> = std::nullopt);

    constexpr bool isNormal() const { return primary() == PrimaryKind::Normal; }
    constexpr bool isBaseline() const { return primary() == PrimaryKind::Baseline; }
    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 isFirstBaseline() const { return primary() == PrimaryKind::Baseline && baselineAlignmentPreference() == BaselineAlignmentPreferenceKind::First; }
    constexpr bool isLastBaseline() const { return primary() == PrimaryKind::Baseline && baselineAlignmentPreference() == BaselineAlignmentPreferenceKind::Last; }

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

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

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

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

    static constexpr bool canHaveBaselinePosition(PrimaryKind);
    static constexpr bool isContentPosition(PrimaryKind);

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

    constexpr PrimaryKind primary() const;
    constexpr BaselineAlignmentPreferenceKind baselineAlignmentPreference() 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, BaselineAlignmentPreferenceKind or OverflowPositionKind, depending on value of m_primary
};
static_assert(sizeof(AlignContent) == 1);

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

constexpr AlignContent::AlignContent(CSS::Keyword::Baseline, std::optional<BaselineAlignmentPreference> preference)
    : AlignContent { PrimaryKind::Baseline, preference }
{
}

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

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

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

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

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

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

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

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

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

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

constexpr AlignContent::AlignContent(PrimaryKind primary, std::optional<BaselineAlignmentPreference> preference)
    : m_primary { static_cast<uint8_t>(primary) }
    , m_secondary { static_cast<uint8_t>(computeKind(preference)) }
{
    ASSERT(canHaveBaselinePosition(primary));
}

constexpr AlignContent::AlignContent(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 AlignContent::PrimaryKind AlignContent::primary() const
{
    return static_cast<PrimaryKind>(m_primary);
}

constexpr BaselineAlignmentPreferenceKind AlignContent::baselineAlignmentPreference() const
{
    RELEASE_ASSERT(canHaveBaselinePosition(primary()));
    return static_cast<BaselineAlignmentPreferenceKind>(m_secondary);
}

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

constexpr bool AlignContent::canHaveBaselinePosition(PrimaryKind primary)
{
    return primary == PrimaryKind::Baseline;
}

constexpr bool AlignContent::isContentPosition(PrimaryKind primary)
{
    switch (primary) {
    case PrimaryKind::Normal:
    case PrimaryKind::Baseline:
    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:
        return true;
    }
    RELEASE_ASSERT_NOT_REACHED();
}

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

    switch (primary()) {
    case PrimaryKind::Normal:
        return visitor(CSS::Keyword::Normal { });
    case PrimaryKind::Baseline:
        return visitBaselineAlignmentPreference(CSS::Keyword::Baseline { }, baselineAlignmentPreference(), visitor);
    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 visitOverflowPosition(CSS::Keyword::Center { }, overflowPosition(), visitor);
    case PrimaryKind::Start:
        return visitOverflowPosition(CSS::Keyword::Start { }, overflowPosition(), visitor);
    case PrimaryKind::End:
        return visitOverflowPosition(CSS::Keyword::End { }, overflowPosition(), visitor);
    case PrimaryKind::FlexStart:
        return visitOverflowPosition(CSS::Keyword::FlexStart { }, overflowPosition(), visitor);
    case PrimaryKind::FlexEnd:
        return visitOverflowPosition(CSS::Keyword::FlexEnd { }, overflowPosition(), visitor);
    }
    RELEASE_ASSERT_NOT_REACHED();
}

// MARK: - Conversion

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

} // namespace Style
} // namespace WebCore

DEFINE_VARIANT_LIKE_CONFORMANCE(WebCore::Style::AlignContent)