1 | /* |
2 | * Copyright (C) 2017 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. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "WritingMode.h" |
29 | #include <array> |
30 | |
31 | namespace WebCore { |
32 | |
33 | template<typename T> class RectEdges { |
34 | public: |
35 | RectEdges() = default; |
36 | |
37 | template<typename U> |
38 | RectEdges(U&& top, U&& right, U&& bottom, U&& left) |
39 | : m_sides({ { std::forward<T>(top), std::forward<T>(right), std::forward<T>(bottom), std::forward<T>(left) } }) |
40 | { } |
41 | |
42 | T& at(PhysicalBoxSide side) { return m_sides[static_cast<size_t>(side)]; } |
43 | T& top() { return at(PhysicalBoxSide::Top); } |
44 | T& right() { return at(PhysicalBoxSide::Right); } |
45 | T& bottom() { return at(PhysicalBoxSide::Bottom); } |
46 | T& left() { return at(PhysicalBoxSide::Left); } |
47 | |
48 | const T& at(PhysicalBoxSide side) const { return m_sides[static_cast<size_t>(side)]; } |
49 | const T& top() const { return at(PhysicalBoxSide::Top); } |
50 | const T& right() const { return at(PhysicalBoxSide::Right); } |
51 | const T& bottom() const { return at(PhysicalBoxSide::Bottom); } |
52 | const T& left() const { return at(PhysicalBoxSide::Left); } |
53 | |
54 | void setAt(PhysicalBoxSide side, const T& v) { at(side) = v; } |
55 | void setTop(const T& top) { setAt(PhysicalBoxSide::Top, top); } |
56 | void setRight(const T& right) { setAt(PhysicalBoxSide::Right, right); } |
57 | void setBottom(const T& bottom) { setAt(PhysicalBoxSide::Bottom, bottom); } |
58 | void setLeft(const T& left) { setAt(PhysicalBoxSide::Left, left); } |
59 | |
60 | T& before(WritingMode writingMode) { return at(mapLogicalSideToPhysicalSide(writingMode, LogicalBoxSide::Before)); } |
61 | T& after(WritingMode writingMode) { return at(mapLogicalSideToPhysicalSide(writingMode, LogicalBoxSide::After)); } |
62 | T& start(WritingMode writingMode, TextDirection direction = TextDirection::LTR) { return at(mapLogicalSideToPhysicalSide(makeTextFlow(writingMode, direction), LogicalBoxSide::Start)); } |
63 | T& end(WritingMode writingMode, TextDirection direction = TextDirection::LTR) { return at(mapLogicalSideToPhysicalSide(makeTextFlow(writingMode, direction), LogicalBoxSide::End)); } |
64 | |
65 | const T& before(WritingMode writingMode) const { return at(mapLogicalSideToPhysicalSide(writingMode, LogicalBoxSide::Before)); } |
66 | const T& after(WritingMode writingMode) const { return at(mapLogicalSideToPhysicalSide(writingMode, LogicalBoxSide::After)); } |
67 | const T& start(WritingMode writingMode, TextDirection direction = TextDirection::LTR) const { return at(mapLogicalSideToPhysicalSide(makeTextFlow(writingMode, direction), LogicalBoxSide::Start)); } |
68 | const T& end(WritingMode writingMode, TextDirection direction = TextDirection::LTR) const { return at(mapLogicalSideToPhysicalSide(makeTextFlow(writingMode, direction), LogicalBoxSide::End)); } |
69 | |
70 | void setBefore(const T& before, WritingMode writingMode) { this->before(writingMode) = before; } |
71 | void setAfter(const T& after, WritingMode writingMode) { this->after(writingMode) = after; } |
72 | void setStart(const T& start, WritingMode writingMode, TextDirection direction = TextDirection::LTR) { this->start(writingMode, direction) = start; } |
73 | void setEnd(const T& end, WritingMode writingMode, TextDirection direction = TextDirection::LTR) { this->end(writingMode, direction) = end; } |
74 | |
75 | bool operator==(const RectEdges& other) const { return m_sides == other.m_sides; } |
76 | bool operator!=(const RectEdges& other) const { return m_sides != other.m_sides; } |
77 | |
78 | private: |
79 | std::array<T, 4> m_sides {{0, 0, 0, 0}}; |
80 | }; |
81 | |
82 | } |
83 | |