1/*
2 * Copyright (C) 2012 Adobe Systems Incorporated. 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 *
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#pragma once
31
32#include "LayoutSize.h"
33#include "Shape.h"
34#include <wtf/HashMap.h>
35#include <wtf/NeverDestroyed.h>
36
37namespace WebCore {
38
39class RenderBlockFlow;
40class RenderBox;
41class StyleImage;
42class FloatingObject;
43
44class ShapeOutsideDeltas final {
45public:
46 ShapeOutsideDeltas()
47 : m_lineOverlapsShape(false)
48 , m_isValid(false)
49 {
50 }
51
52 ShapeOutsideDeltas(LayoutUnit leftMarginBoxDelta, LayoutUnit rightMarginBoxDelta, bool lineOverlapsShape, LayoutUnit borderBoxLineTop, LayoutUnit lineHeight)
53 : m_leftMarginBoxDelta(leftMarginBoxDelta)
54 , m_rightMarginBoxDelta(rightMarginBoxDelta)
55 , m_borderBoxLineTop(borderBoxLineTop)
56 , m_lineHeight(lineHeight)
57 , m_lineOverlapsShape(lineOverlapsShape)
58 , m_isValid(true)
59 {
60 }
61
62 bool isForLine(LayoutUnit borderBoxLineTop, LayoutUnit lineHeight)
63 {
64 return m_isValid && m_borderBoxLineTop == borderBoxLineTop && m_lineHeight == lineHeight;
65 }
66
67 bool isValid() { return m_isValid; }
68 LayoutUnit leftMarginBoxDelta() { ASSERT(m_isValid); return m_leftMarginBoxDelta; }
69 LayoutUnit rightMarginBoxDelta() { ASSERT(m_isValid); return m_rightMarginBoxDelta; }
70 bool lineOverlapsShape() { ASSERT(m_isValid); return m_lineOverlapsShape; }
71
72private:
73 LayoutUnit m_leftMarginBoxDelta;
74 LayoutUnit m_rightMarginBoxDelta;
75 LayoutUnit m_borderBoxLineTop;
76 LayoutUnit m_lineHeight;
77 unsigned m_lineOverlapsShape : 1;
78 unsigned m_isValid : 1;
79};
80
81class ShapeOutsideInfo final {
82 WTF_MAKE_FAST_ALLOCATED;
83public:
84 ShapeOutsideInfo(const RenderBox& renderer)
85 : m_renderer(renderer)
86 {
87 }
88
89 static bool isEnabledFor(const RenderBox&);
90
91 ShapeOutsideDeltas computeDeltasForContainingBlockLine(const RenderBlockFlow&, const FloatingObject&, LayoutUnit lineTop, LayoutUnit lineHeight);
92
93 void setReferenceBoxLogicalSize(LayoutSize);
94
95 LayoutUnit shapeLogicalTop() const { return computedShape().shapeMarginLogicalBoundingBox().y() + logicalTopOffset(); }
96 LayoutUnit shapeLogicalBottom() const { return computedShape().shapeMarginLogicalBoundingBox().maxY() + logicalTopOffset(); }
97 LayoutUnit shapeLogicalLeft() const { return computedShape().shapeMarginLogicalBoundingBox().x() + logicalLeftOffset(); }
98 LayoutUnit shapeLogicalRight() const { return computedShape().shapeMarginLogicalBoundingBox().maxX() + logicalLeftOffset(); }
99 LayoutUnit shapeLogicalWidth() const { return computedShape().shapeMarginLogicalBoundingBox().width(); }
100 LayoutUnit shapeLogicalHeight() const { return computedShape().shapeMarginLogicalBoundingBox().height(); }
101
102 void markShapeAsDirty() { m_shape = nullptr; }
103 bool isShapeDirty() { return !m_shape; }
104
105 LayoutRect computedShapePhysicalBoundingBox() const;
106 FloatPoint shapeToRendererPoint(const FloatPoint&) const;
107 FloatSize shapeToRendererSize(const FloatSize&) const;
108
109 const Shape& computedShape() const;
110
111 static ShapeOutsideInfo& ensureInfo(const RenderBox& key)
112 {
113 InfoMap& infoMap = ShapeOutsideInfo::infoMap();
114 if (ShapeOutsideInfo* info = infoMap.get(&key))
115 return *info;
116 auto result = infoMap.add(&key, std::make_unique<ShapeOutsideInfo>(key));
117 return *result.iterator->value;
118 }
119 static void removeInfo(const RenderBox& key) { infoMap().remove(&key); }
120 static ShapeOutsideInfo* info(const RenderBox& key) { return infoMap().get(&key); }
121
122private:
123 std::unique_ptr<Shape> createShapeForImage(StyleImage*, float shapeImageThreshold, WritingMode, float margin) const;
124
125 LayoutUnit logicalTopOffset() const;
126 LayoutUnit logicalLeftOffset() const;
127
128 typedef HashMap<const RenderBox*, std::unique_ptr<ShapeOutsideInfo>> InfoMap;
129 static InfoMap& infoMap()
130 {
131 static NeverDestroyed<InfoMap> staticInfoMap;
132 return staticInfoMap;
133 }
134
135 const RenderBox& m_renderer;
136
137 mutable std::unique_ptr<Shape> m_shape;
138 LayoutSize m_referenceBoxLogicalSize;
139
140 ShapeOutsideDeltas m_shapeOutsideDeltas;
141};
142
143}
144