1 | /* |
2 | * Copyright (C) 2018 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #if ENABLE(LAYOUT_FORMATTING_CONTEXT) |
29 | |
30 | #include "FloatingState.h" |
31 | #include "FormattingContext.h" |
32 | #include "LayoutBox.h" |
33 | #include "LayoutState.h" |
34 | #include "LayoutUnit.h" |
35 | #include <wtf/IsoMalloc.h> |
36 | |
37 | namespace WebCore { |
38 | |
39 | namespace Layout { |
40 | |
41 | class Box; |
42 | enum class StyleDiff; |
43 | |
44 | class FormattingState { |
45 | WTF_MAKE_ISO_ALLOCATED(FormattingState); |
46 | public: |
47 | virtual ~FormattingState(); |
48 | |
49 | FloatingState& floatingState() const { return m_floatingState; } |
50 | |
51 | void markNeedsLayout(const Box&, StyleDiff); |
52 | bool needsLayout(const Box&); |
53 | |
54 | void setIntrinsicWidthConstraints(const Box&, FormattingContext::IntrinsicWidthConstraints); |
55 | void clearIntrinsicWidthConstraints(const Box&); |
56 | Optional<FormattingContext::IntrinsicWidthConstraints> intrinsicWidthConstraints(const Box&) const; |
57 | |
58 | bool isBlockFormattingState() const { return m_type == Type::Block; } |
59 | bool isInlineFormattingState() const { return m_type == Type::Inline; } |
60 | |
61 | LayoutState& layoutState() const { return m_layoutState; } |
62 | |
63 | protected: |
64 | enum class Type { Block, Inline }; |
65 | FormattingState(Ref<FloatingState>&&, Type, LayoutState&); |
66 | |
67 | private: |
68 | LayoutState& m_layoutState; |
69 | Ref<FloatingState> m_floatingState; |
70 | HashMap<const Box*, FormattingContext::IntrinsicWidthConstraints> m_intrinsicWidthConstraints; |
71 | Type m_type; |
72 | }; |
73 | |
74 | inline void FormattingState::setIntrinsicWidthConstraints(const Box& layoutBox, FormattingContext::IntrinsicWidthConstraints intrinsicWidthConstraints) |
75 | { |
76 | ASSERT(!m_intrinsicWidthConstraints.contains(&layoutBox)); |
77 | ASSERT(&m_layoutState.formattingStateForBox(layoutBox) == this); |
78 | m_intrinsicWidthConstraints.set(&layoutBox, intrinsicWidthConstraints); |
79 | } |
80 | |
81 | inline void FormattingState::clearIntrinsicWidthConstraints(const Box& layoutBox) |
82 | { |
83 | m_intrinsicWidthConstraints.remove(&layoutBox); |
84 | } |
85 | |
86 | inline Optional<FormattingContext::IntrinsicWidthConstraints> FormattingState::intrinsicWidthConstraints(const Box& layoutBox) const |
87 | { |
88 | ASSERT(&m_layoutState.formattingStateForBox(layoutBox) == this); |
89 | auto iterator = m_intrinsicWidthConstraints.find(&layoutBox); |
90 | if (iterator == m_intrinsicWidthConstraints.end()) |
91 | return { }; |
92 | return iterator->value; |
93 | } |
94 | |
95 | } |
96 | } |
97 | |
98 | #define SPECIALIZE_TYPE_TRAITS_LAYOUT_FORMATTING_STATE(ToValueTypeName, predicate) \ |
99 | SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::Layout::ToValueTypeName) \ |
100 | static bool isType(const WebCore::Layout::FormattingState& formattingState) { return formattingState.predicate; } \ |
101 | SPECIALIZE_TYPE_TRAITS_END() |
102 | |
103 | #endif |
104 | |