| 1 | /* |
| 2 | * Copyright (C) 2018 Igalia S.L. 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 | #include "GridLayoutFunctions.h" |
| 29 | #include "RenderStyleConstants.h" |
| 30 | #include "WritingMode.h" |
| 31 | #include <wtf/HashMap.h> |
| 32 | #include <wtf/HashSet.h> |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | // These classes are used to implement the Baseline Alignment logic, as described in the CSS Box Alignment |
| 37 | // specification. |
| 38 | // https://drafts.csswg.org/css-align/#baseline-terms |
| 39 | // |
| 40 | // A baseline-sharing group is composed of boxes that participate in baseline alignment together. This is |
| 41 | // possible only if they: |
| 42 | // |
| 43 | // * Share an alignment context along an axis perpendicular to their baseline alignment axis. |
| 44 | // * Have compatible baseline alignment preferences (i.e., the baselines that want to align are on the same |
| 45 | // side of the alignment context). |
| 46 | // |
| 47 | // Once the BaselineGroup is instantiated, defined by a 'block-direction' (WritingMode) and a 'baseline-preference' |
| 48 | // (first/last baseline), it's ready to collect the items that will participate in the Baseline Alignment logic. |
| 49 | // |
| 50 | class BaselineGroup { |
| 51 | public: |
| 52 | // It stores an item (if not already present) and update the max_ascent and max_descent associated to this |
| 53 | // baseline-sharing group. |
| 54 | void update(const RenderBox&, LayoutUnit ascent, LayoutUnit descent); |
| 55 | LayoutUnit maxAscent() const { return m_maxAscent; } |
| 56 | LayoutUnit maxDescent() const { return m_maxDescent; } |
| 57 | int size() const { return m_items.size(); } |
| 58 | |
| 59 | private: |
| 60 | friend class BaselineContext; |
| 61 | BaselineGroup(WritingMode blockFlow, ItemPosition childPreference); |
| 62 | |
| 63 | // Determines whether a baseline-sharing group is compatible with an item, based on its 'block-flow' and |
| 64 | // 'baseline-preference' |
| 65 | bool isCompatible(WritingMode, ItemPosition) const; |
| 66 | |
| 67 | // Determines whether the baseline-sharing group's associated block-flow is opposite (LR vs RL) to particular |
| 68 | // item's writing-mode. |
| 69 | bool isOppositeBlockFlow(WritingMode blockFlow) const; |
| 70 | |
| 71 | // Determines whether the baseline-sharing group's associated block-flow is orthogonal (vertical vs horizontal) |
| 72 | // to particular item's writing-mode. |
| 73 | bool isOrthogonalBlockFlow(WritingMode blockFlow) const; |
| 74 | |
| 75 | WritingMode m_blockFlow; |
| 76 | ItemPosition m_preference; |
| 77 | LayoutUnit m_maxAscent; |
| 78 | LayoutUnit m_maxDescent; |
| 79 | HashSet<const RenderBox*> m_items; |
| 80 | }; |
| 81 | |
| 82 | // https://drafts.csswg.org/css-align-3/#shared-alignment-context |
| 83 | // Boxes share an alignment context along a particular axis when they are: |
| 84 | // |
| 85 | // * table cells in the same row, along the table's row (inline) axis |
| 86 | // * table cells in the same column, along the table's column (block) axis |
| 87 | // * grid items in the same row, along the grid's row (inline) axis |
| 88 | // * grid items in the same column, along the grid's colum (block) axis |
| 89 | // * flex items in the same flex line, along the flex container's main axis |
| 90 | // |
| 91 | // https://drafts.csswg.org/css-align-3/#baseline-sharing-group |
| 92 | // A Baseline alignment-context may handle several baseline-sharing groups. In order to create an instance, we |
| 93 | // need to pass the required data to define the first baseline-sharing group; a Baseline Context must have at |
| 94 | // least one baseline-sharing group. |
| 95 | // |
| 96 | // By adding new items to a Baseline Context, the baseline-sharing groups it handles are automatically updated, |
| 97 | // if there is one that is compatible with such item. Otherwise, a new baseline-sharing group is created, |
| 98 | // compatible with the new item. |
| 99 | class BaselineContext { |
| 100 | WTF_MAKE_FAST_ALLOCATED; |
| 101 | public: |
| 102 | BaselineContext(const RenderBox& child, ItemPosition preference, LayoutUnit ascent, LayoutUnit descent); |
| 103 | const BaselineGroup& sharedGroup(const RenderBox& child, ItemPosition preference) const; |
| 104 | |
| 105 | // Updates the baseline-sharing group compatible with the item. |
| 106 | // We pass the item's baseline-preference to avoid dependencies with the LayoutGrid class, which is the one |
| 107 | // managing the alignment behavior of the Grid Items. |
| 108 | void updateSharedGroup(const RenderBox& child, ItemPosition preference, LayoutUnit ascent, LayoutUnit descent); |
| 109 | |
| 110 | private: |
| 111 | // Returns the baseline-sharing group compatible with an item. |
| 112 | // We pass the item's baseline-preference to avoid dependencies with the LayoutGrid class, which is the one |
| 113 | // managing the alignment behavior of the Grid Items. |
| 114 | // FIXME: Properly implement baseline-group compatibility. |
| 115 | // See https://github.com/w3c/csswg-drafts/issues/721 |
| 116 | BaselineGroup& findCompatibleSharedGroup(const RenderBox& child, ItemPosition preference); |
| 117 | |
| 118 | Vector<BaselineGroup> m_sharedGroups; |
| 119 | }; |
| 120 | |
| 121 | static inline bool isBaselinePosition(ItemPosition position) |
| 122 | { |
| 123 | return position == ItemPosition::Baseline || position == ItemPosition::LastBaseline; |
| 124 | } |
| 125 | |
| 126 | // This is the class that implements the Baseline Alignment logic, using internally the BaselineContext and |
| 127 | // BaselineGroupd classes (described above). |
| 128 | // |
| 129 | // The first phase is to collect the items that will participate in baseline alignment together. During this |
| 130 | // phase the required baseline-sharing groups will be created for each Baseline alignment-context shared by |
| 131 | // the items participating in the baseline alignment. |
| 132 | // |
| 133 | // Additionally, the baseline-sharing groups' offsets, max-ascend and max-descent will be computed and stored. |
| 134 | // This class also computes the baseline offset for a particular item, based on the max-ascent for its associated |
| 135 | // baseline-sharing group. |
| 136 | class GridBaselineAlignment { |
| 137 | public: |
| 138 | // Collects the items participating in baseline alignment and updates the corresponding baseline-sharing |
| 139 | // group of the Baseline Context the items belongs to. |
| 140 | // All the baseline offsets are updated accordingly based on the added item. |
| 141 | void updateBaselineAlignmentContext(ItemPosition, unsigned sharedContext, const RenderBox&, GridAxis); |
| 142 | |
| 143 | // Returns the baseline offset of a particular item, based on the max-ascent for its associated |
| 144 | // baseline-sharing group |
| 145 | LayoutUnit baselineOffsetForChild(ItemPosition, unsigned sharedContext, const RenderBox&, GridAxis) const; |
| 146 | |
| 147 | // Sets the Grid Container's writing-mode so that we can avoid the dependecy of the LayoutGrid class for |
| 148 | // determining whether a grid item is orthogonal or not. |
| 149 | void setBlockFlow(WritingMode blockFlow) { m_blockFlow = blockFlow; }; |
| 150 | |
| 151 | // Clearing the Baseline Alignment context and their internal classes and data structures. |
| 152 | void clear(GridAxis); |
| 153 | |
| 154 | private: |
| 155 | const BaselineGroup& baselineGroupForChild(ItemPosition, unsigned sharedContext, const RenderBox&, GridAxis) const; |
| 156 | LayoutUnit marginOverForChild(const RenderBox&, GridAxis) const; |
| 157 | LayoutUnit marginUnderForChild(const RenderBox&, GridAxis) const; |
| 158 | LayoutUnit logicalAscentForChild(const RenderBox&, GridAxis) const; |
| 159 | LayoutUnit ascentForChild(const RenderBox&, GridAxis) const; |
| 160 | LayoutUnit descentForChild(const RenderBox&, LayoutUnit, GridAxis) const; |
| 161 | bool isDescentBaselineForChild(const RenderBox&, GridAxis) const; |
| 162 | bool isHorizontalBaselineAxis(GridAxis) const; |
| 163 | bool isOrthogonalChildForBaseline(const RenderBox&) const; |
| 164 | bool isParallelToBaselineAxisForChild(const RenderBox&, GridAxis) const; |
| 165 | |
| 166 | typedef HashMap<unsigned, std::unique_ptr<BaselineContext>, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> BaselineContextsMap; |
| 167 | |
| 168 | // Grid Container's WritingMode, used to determine grid item's orthogonality. |
| 169 | WritingMode m_blockFlow; |
| 170 | BaselineContextsMap m_rowAxisAlignmentContext; |
| 171 | BaselineContextsMap m_colAxisAlignmentContext; |
| 172 | }; |
| 173 | |
| 174 | } // namespace WebCore |
| 175 | |