2d84666763b19a963942fea8498efe56faacfd3c
[WebKit-https.git] / Source / WebCore / rendering / shapes / ShapeOutsideInfo.cpp
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 #include "config.h"
31
32 #if ENABLE(CSS_SHAPES)
33
34 #include "ShapeOutsideInfo.h"
35
36 #include "BoxShape.h"
37 #include "FloatingObjects.h"
38 #include "LengthFunctions.h"
39 #include "RenderBlockFlow.h"
40 #include "RenderBox.h"
41 #include "RenderImage.h"
42 #include "RenderRegion.h"
43
44 namespace WebCore {
45
46 LayoutRect ShapeOutsideInfo::computedShapePhysicalBoundingBox() const
47 {
48     LayoutRect physicalBoundingBox = computedShape().shapeMarginLogicalBoundingBox();
49     physicalBoundingBox.setX(physicalBoundingBox.x() + logicalLeftOffset());
50     physicalBoundingBox.setY(physicalBoundingBox.y() + logicalTopOffset());
51     if (m_renderer.style().isFlippedBlocksWritingMode())
52         physicalBoundingBox.setY(m_renderer.logicalHeight() - physicalBoundingBox.maxY());
53     if (!m_renderer.style().isHorizontalWritingMode())
54         physicalBoundingBox = physicalBoundingBox.transposedRect();
55     return physicalBoundingBox;
56 }
57
58 FloatPoint ShapeOutsideInfo::shapeToRendererPoint(const FloatPoint& point) const
59 {
60     FloatPoint result = FloatPoint(point.x() + logicalLeftOffset(), point.y() + logicalTopOffset());
61     if (m_renderer.style().isFlippedBlocksWritingMode())
62         result.setY(m_renderer.logicalHeight() - result.y());
63     if (!m_renderer.style().isHorizontalWritingMode())
64         result = result.transposedPoint();
65     return result;
66 }
67
68 FloatSize ShapeOutsideInfo::shapeToRendererSize(const FloatSize& size) const
69 {
70     if (!m_renderer.style().isHorizontalWritingMode())
71         return size.transposedSize();
72     return size;
73 }
74
75 static inline CSSBoxType referenceBox(const ShapeValue& shapeValue)
76 {
77     if (shapeValue.cssBox() == BoxMissing) {
78         if (shapeValue.type() == ShapeValue::Type::Image)
79             return ContentBox;
80         return MarginBox;
81     }
82     return shapeValue.cssBox();
83 }
84
85 void ShapeOutsideInfo::setReferenceBoxLogicalSize(LayoutSize newReferenceBoxLogicalSize)
86 {
87     bool isHorizontalWritingMode = m_renderer.containingBlock()->style().isHorizontalWritingMode();
88     switch (referenceBox(*m_renderer.style().shapeOutside())) {
89     case MarginBox:
90         if (isHorizontalWritingMode)
91             newReferenceBoxLogicalSize.expand(m_renderer.horizontalMarginExtent(), m_renderer.verticalMarginExtent());
92         else
93             newReferenceBoxLogicalSize.expand(m_renderer.verticalMarginExtent(), m_renderer.horizontalMarginExtent());
94         break;
95     case BorderBox:
96         break;
97     case PaddingBox:
98         if (isHorizontalWritingMode)
99             newReferenceBoxLogicalSize.shrink(m_renderer.horizontalBorderExtent(), m_renderer.verticalBorderExtent());
100         else
101             newReferenceBoxLogicalSize.shrink(m_renderer.verticalBorderExtent(), m_renderer.horizontalBorderExtent());
102         break;
103     case ContentBox:
104         if (isHorizontalWritingMode)
105             newReferenceBoxLogicalSize.shrink(m_renderer.horizontalBorderAndPaddingExtent(), m_renderer.verticalBorderAndPaddingExtent());
106         else
107             newReferenceBoxLogicalSize.shrink(m_renderer.verticalBorderAndPaddingExtent(), m_renderer.horizontalBorderAndPaddingExtent());
108         break;
109     case Fill:
110     case Stroke:
111     case ViewBox:
112     case BoxMissing:
113         ASSERT_NOT_REACHED();
114         break;
115     }
116
117     if (m_referenceBoxLogicalSize == newReferenceBoxLogicalSize)
118         return;
119     markShapeAsDirty();
120     m_referenceBoxLogicalSize = newReferenceBoxLogicalSize;
121 }
122
123 static inline bool checkShapeImageOrigin(Document& document, const StyleImage& styleImage)
124 {
125     if (styleImage.isGeneratedImage())
126         return true;
127
128     ASSERT(styleImage.cachedImage());
129     CachedImage& cachedImage = *(styleImage.cachedImage());
130     if (cachedImage.isOriginClean(document.securityOrigin()))
131         return true;
132
133     const URL& url = cachedImage.url();
134     String urlString = url.isNull() ? "''" : url.stringCenterEllipsizedToLength();
135     document.addConsoleMessage(MessageSource::Security, MessageLevel::Error, "Unsafe attempt to load URL " + urlString + ".");
136
137     return false;
138 }
139
140 static LayoutRect getShapeImageMarginRect(const RenderBox& renderBox, const LayoutSize& referenceBoxLogicalSize)
141 {
142     LayoutPoint marginBoxOrigin(-renderBox.marginLogicalLeft() - renderBox.borderAndPaddingLogicalLeft(), -renderBox.marginBefore() - renderBox.borderBefore() - renderBox.paddingBefore());
143     LayoutSize marginBoxSizeDelta(renderBox.marginLogicalWidth() + renderBox.borderAndPaddingLogicalWidth(), renderBox.marginLogicalHeight() + renderBox.borderAndPaddingLogicalHeight());
144     LayoutSize marginRectSize(referenceBoxLogicalSize + marginBoxSizeDelta);
145     marginRectSize.clampNegativeToZero();
146     return LayoutRect(marginBoxOrigin, marginRectSize);
147 }
148
149 std::unique_ptr<Shape> ShapeOutsideInfo::createShapeForImage(StyleImage* styleImage, float shapeImageThreshold, WritingMode writingMode, float margin) const
150 {
151     LayoutSize imageSize = m_renderer.calculateImageIntrinsicDimensions(styleImage, m_referenceBoxLogicalSize, RenderImage::ScaleByEffectiveZoom);
152     styleImage->setContainerSizeForRenderer(&m_renderer, imageSize, m_renderer.style().effectiveZoom());
153
154     const LayoutRect& marginRect = getShapeImageMarginRect(m_renderer, m_referenceBoxLogicalSize);
155     const LayoutRect& imageRect = is<RenderImage>(m_renderer)
156         ? downcast<RenderImage>(m_renderer).replacedContentRect(m_renderer.intrinsicSize())
157         : LayoutRect(LayoutPoint(), imageSize);
158
159     ASSERT(!styleImage->isPending());
160     RefPtr<Image> image = styleImage->image(const_cast<RenderBox*>(&m_renderer), imageSize);
161     return Shape::createRasterShape(image.get(), shapeImageThreshold, imageRect, marginRect, writingMode, margin);
162 }
163
164 const Shape& ShapeOutsideInfo::computedShape() const
165 {
166     if (Shape* shape = m_shape.get())
167         return *shape;
168
169     const RenderStyle& style = m_renderer.style();
170     ASSERT(m_renderer.containingBlock());
171     const RenderStyle& containingBlockStyle = m_renderer.containingBlock()->style();
172
173     WritingMode writingMode = containingBlockStyle.writingMode();
174     float margin = floatValueForLength(m_renderer.style().shapeMargin(), m_renderer.containingBlock() ? m_renderer.containingBlock()->contentWidth() : LayoutUnit());
175     float shapeImageThreshold = style.shapeImageThreshold();
176     const ShapeValue& shapeValue = *style.shapeOutside();
177
178     switch (shapeValue.type()) {
179     case ShapeValue::Type::Shape:
180         ASSERT(shapeValue.shape());
181         m_shape = Shape::createShape(*shapeValue.shape(), m_referenceBoxLogicalSize, writingMode, margin);
182         break;
183     case ShapeValue::Type::Image:
184         ASSERT(shapeValue.isImageValid());
185         m_shape = createShapeForImage(shapeValue.image(), shapeImageThreshold, writingMode, margin);
186         break;
187     case ShapeValue::Type::Box: {
188         RoundedRect shapeRect = computeRoundedRectForBoxShape(referenceBox(shapeValue), m_renderer);
189         if (!containingBlockStyle.isHorizontalWritingMode())
190             shapeRect = shapeRect.transposedRect();
191         m_shape = Shape::createBoxShape(shapeRect, writingMode, margin);
192         break;
193     }
194     }
195
196     ASSERT(m_shape);
197     return *m_shape;
198 }
199
200 static inline LayoutUnit borderBeforeInWritingMode(const RenderBox& renderer, WritingMode writingMode)
201 {
202     switch (writingMode) {
203     case TopToBottomWritingMode: return renderer.borderTop();
204     case BottomToTopWritingMode: return renderer.borderBottom();
205     case LeftToRightWritingMode: return renderer.borderLeft();
206     case RightToLeftWritingMode: return renderer.borderRight();
207     }
208
209     ASSERT_NOT_REACHED();
210     return renderer.borderBefore();
211 }
212
213 static inline LayoutUnit borderAndPaddingBeforeInWritingMode(const RenderBox& renderer, WritingMode writingMode)
214 {
215     switch (writingMode) {
216     case TopToBottomWritingMode: return renderer.borderTop() + renderer.paddingTop();
217     case BottomToTopWritingMode: return renderer.borderBottom() + renderer.paddingBottom();
218     case LeftToRightWritingMode: return renderer.borderLeft() + renderer.paddingLeft();
219     case RightToLeftWritingMode: return renderer.borderRight() + renderer.paddingRight();
220     }
221
222     ASSERT_NOT_REACHED();
223     return renderer.borderAndPaddingBefore();
224 }
225
226 LayoutUnit ShapeOutsideInfo::logicalTopOffset() const
227 {
228     switch (referenceBox(*m_renderer.style().shapeOutside())) {
229     case MarginBox: return -m_renderer.marginBefore(&m_renderer.containingBlock()->style());
230     case BorderBox: return LayoutUnit();
231     case PaddingBox: return borderBeforeInWritingMode(m_renderer, m_renderer.containingBlock()->style().writingMode());
232     case ContentBox: return borderAndPaddingBeforeInWritingMode(m_renderer, m_renderer.containingBlock()->style().writingMode());
233     case Fill: break;
234     case Stroke: break;
235     case ViewBox: break;
236     case BoxMissing: break;
237     }
238     
239     ASSERT_NOT_REACHED();
240     return LayoutUnit();
241 }
242
243 static inline LayoutUnit borderStartWithStyleForWritingMode(const RenderBox& renderer, const RenderStyle& style)
244 {
245     if (style.isHorizontalWritingMode()) {
246         if (style.isLeftToRightDirection())
247             return renderer.borderLeft();
248         
249         return renderer.borderRight();
250     }
251     if (style.isLeftToRightDirection())
252         return renderer.borderTop();
253     
254     return renderer.borderBottom();
255 }
256
257 static inline LayoutUnit borderAndPaddingStartWithStyleForWritingMode(const RenderBox& renderer, const RenderStyle& style)
258 {
259     if (style.isHorizontalWritingMode()) {
260         if (style.isLeftToRightDirection())
261             return renderer.borderLeft() + renderer.paddingLeft();
262         
263         return renderer.borderRight() + renderer.paddingRight();
264     }
265     if (style.isLeftToRightDirection())
266         return renderer.borderTop() + renderer.paddingTop();
267     
268     return renderer.borderBottom() + renderer.paddingBottom();
269 }
270
271 LayoutUnit ShapeOutsideInfo::logicalLeftOffset() const
272 {
273     if (m_renderer.isRenderRegion())
274         return LayoutUnit();
275     
276     switch (referenceBox(*m_renderer.style().shapeOutside())) {
277     case MarginBox: return -m_renderer.marginStart(&m_renderer.containingBlock()->style());
278     case BorderBox: return LayoutUnit();
279     case PaddingBox: return borderStartWithStyleForWritingMode(m_renderer, m_renderer.containingBlock()->style());
280     case ContentBox: return borderAndPaddingStartWithStyleForWritingMode(m_renderer, m_renderer.containingBlock()->style());
281     case Fill: break;
282     case Stroke: break;
283     case ViewBox: break;
284     case BoxMissing: break;
285     }
286
287     ASSERT_NOT_REACHED();
288     return LayoutUnit();
289 }
290
291 bool ShapeOutsideInfo::isEnabledFor(const RenderBox& box)
292 {
293     ShapeValue* shapeValue = box.style().shapeOutside();
294     if (!box.isFloating() || !shapeValue)
295         return false;
296
297     switch (shapeValue->type()) {
298     case ShapeValue::Type::Shape: return shapeValue->shape();
299     case ShapeValue::Type::Image: return shapeValue->isImageValid() && checkShapeImageOrigin(box.document(), *(shapeValue->image()));
300     case ShapeValue::Type::Box: return true;
301     }
302
303     ASSERT_NOT_REACHED();
304     return false;
305 }
306
307 ShapeOutsideDeltas ShapeOutsideInfo::computeDeltasForContainingBlockLine(const RenderBlockFlow& containingBlock, const FloatingObject& floatingObject, LayoutUnit lineTop, LayoutUnit lineHeight)
308 {
309     ASSERT(lineHeight >= 0);
310     LayoutUnit borderBoxTop = containingBlock.logicalTopForFloat(floatingObject) + containingBlock.marginBeforeForChild(m_renderer);
311     LayoutUnit borderBoxLineTop = lineTop - borderBoxTop;
312
313     if (isShapeDirty() || !m_shapeOutsideDeltas.isForLine(borderBoxLineTop, lineHeight)) {
314         LayoutUnit referenceBoxLineTop = borderBoxLineTop - logicalTopOffset();
315         LayoutUnit floatMarginBoxWidth = std::max<LayoutUnit>(LayoutUnit(), containingBlock.logicalWidthForFloat(floatingObject));
316
317         if (computedShape().lineOverlapsShapeMarginBounds(referenceBoxLineTop, lineHeight)) {
318             LineSegment segment = computedShape().getExcludedInterval((borderBoxLineTop - logicalTopOffset()), std::min(lineHeight, shapeLogicalBottom() - borderBoxLineTop));
319             if (segment.isValid) {
320                 LayoutUnit logicalLeftMargin = containingBlock.style().isLeftToRightDirection() ? containingBlock.marginStartForChild(m_renderer) : containingBlock.marginEndForChild(m_renderer);
321                 LayoutUnit rawLeftMarginBoxDelta = segment.logicalLeft + logicalLeftOffset() + logicalLeftMargin;
322                 LayoutUnit leftMarginBoxDelta = clampTo<LayoutUnit>(rawLeftMarginBoxDelta, LayoutUnit(), floatMarginBoxWidth);
323
324                 LayoutUnit logicalRightMargin = containingBlock.style().isLeftToRightDirection() ? containingBlock.marginEndForChild(m_renderer) : containingBlock.marginStartForChild(m_renderer);
325                 LayoutUnit rawRightMarginBoxDelta = segment.logicalRight + logicalLeftOffset() - containingBlock.logicalWidthForChild(m_renderer) - logicalRightMargin;
326                 LayoutUnit rightMarginBoxDelta = clampTo<LayoutUnit>(rawRightMarginBoxDelta, -floatMarginBoxWidth, LayoutUnit());
327
328                 m_shapeOutsideDeltas = ShapeOutsideDeltas(leftMarginBoxDelta, rightMarginBoxDelta, true, borderBoxLineTop, lineHeight);
329                 return m_shapeOutsideDeltas;
330             }
331         }
332
333         // Lines that do not overlap the shape should act as if the float
334         // wasn't there for layout purposes. So we set the deltas to remove the
335         // entire width of the float
336         m_shapeOutsideDeltas = ShapeOutsideDeltas(floatMarginBoxWidth, -floatMarginBoxWidth, false, borderBoxLineTop, lineHeight);
337     }
338
339     return m_shapeOutsideDeltas;
340 }
341
342 }
343
344 #endif