34f1ba19c270b4ea5e3bcfce51426708b5c6c892
[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     return LayoutRect(marginBoxOrigin, referenceBoxLogicalSize + marginBoxSizeDelta);
145 }
146
147 std::unique_ptr<Shape> ShapeOutsideInfo::createShapeForImage(StyleImage* styleImage, float shapeImageThreshold, WritingMode writingMode, float margin) const
148 {
149     const LayoutSize& imageSize = m_renderer.calculateImageIntrinsicDimensions(styleImage, roundedLayoutSize(m_referenceBoxLogicalSize), RenderImage::ScaleByEffectiveZoom);
150     styleImage->setContainerSizeForRenderer(&m_renderer, imageSize, m_renderer.style().effectiveZoom());
151
152     const LayoutRect& marginRect = getShapeImageMarginRect(m_renderer, m_referenceBoxLogicalSize);
153     const LayoutRect& imageRect = m_renderer.isRenderImage()
154         ? toRenderImage(&m_renderer)->replacedContentRect(m_renderer.intrinsicSize())
155         : LayoutRect(LayoutPoint(), imageSize);
156
157     ASSERT(!styleImage->isPendingImage());
158     RefPtr<Image> image = styleImage->image(const_cast<RenderBox*>(&m_renderer), imageSize);
159
160     return Shape::createRasterShape(image.get(), shapeImageThreshold, imageRect, marginRect, writingMode, margin);
161 }
162
163 const Shape& ShapeOutsideInfo::computedShape() const
164 {
165     if (Shape* shape = m_shape.get())
166         return *shape;
167
168     const RenderStyle& style = m_renderer.style();
169     ASSERT(m_renderer.containingBlock());
170     const RenderStyle& containingBlockStyle = m_renderer.containingBlock()->style();
171
172     WritingMode writingMode = containingBlockStyle.writingMode();
173     float margin = floatValueForLength(m_renderer.style().shapeMargin(), m_renderer.containingBlock() ? m_renderer.containingBlock()->contentWidth() : LayoutUnit());
174     float shapeImageThreshold = style.shapeImageThreshold();
175     const ShapeValue& shapeValue = *style.shapeOutside();
176
177     switch (shapeValue.type()) {
178     case ShapeValue::Type::Shape:
179         ASSERT(shapeValue.shape());
180         m_shape = Shape::createShape(shapeValue.shape(), m_referenceBoxLogicalSize, writingMode, margin);
181         break;
182     case ShapeValue::Type::Image:
183         ASSERT(shapeValue.isImageValid());
184         m_shape = createShapeForImage(shapeValue.image(), shapeImageThreshold, writingMode, margin);
185         break;
186     case ShapeValue::Type::Box: {
187         RoundedRect shapeRect = computeRoundedRectForBoxShape(referenceBox(shapeValue), m_renderer);
188         if (!containingBlockStyle.isHorizontalWritingMode())
189             shapeRect = shapeRect.transposedRect();
190         m_shape = Shape::createBoxShape(shapeRect, writingMode, margin);
191         break;
192     }
193     }
194
195     ASSERT(m_shape);
196     return *m_shape;
197 }
198
199 static inline LayoutUnit borderBeforeInWritingMode(const RenderBox& renderer, WritingMode writingMode)
200 {
201     switch (writingMode) {
202     case TopToBottomWritingMode: return renderer.borderTop();
203     case BottomToTopWritingMode: return renderer.borderBottom();
204     case LeftToRightWritingMode: return renderer.borderLeft();
205     case RightToLeftWritingMode: return renderer.borderRight();
206     }
207
208     ASSERT_NOT_REACHED();
209     return renderer.borderBefore();
210 }
211
212 static inline LayoutUnit borderAndPaddingBeforeInWritingMode(const RenderBox& renderer, WritingMode writingMode)
213 {
214     switch (writingMode) {
215     case TopToBottomWritingMode: return renderer.borderTop() + renderer.paddingTop();
216     case BottomToTopWritingMode: return renderer.borderBottom() + renderer.paddingBottom();
217     case LeftToRightWritingMode: return renderer.borderLeft() + renderer.paddingLeft();
218     case RightToLeftWritingMode: return renderer.borderRight() + renderer.paddingRight();
219     }
220
221     ASSERT_NOT_REACHED();
222     return renderer.borderAndPaddingBefore();
223 }
224
225 LayoutUnit ShapeOutsideInfo::logicalTopOffset() const
226 {
227     switch (referenceBox(*m_renderer.style().shapeOutside())) {
228     case MarginBox: return -m_renderer.marginBefore(&m_renderer.containingBlock()->style());
229     case BorderBox: return LayoutUnit();
230     case PaddingBox: return borderBeforeInWritingMode(m_renderer, m_renderer.containingBlock()->style().writingMode());
231     case ContentBox: return borderAndPaddingBeforeInWritingMode(m_renderer, m_renderer.containingBlock()->style().writingMode());
232     case Fill: break;
233     case Stroke: break;
234     case ViewBox: break;
235     case BoxMissing: break;
236     }
237     
238     ASSERT_NOT_REACHED();
239     return LayoutUnit();
240 }
241
242 static inline LayoutUnit borderStartWithStyleForWritingMode(const RenderBox& renderer, const RenderStyle& style)
243 {
244     if (style.isHorizontalWritingMode()) {
245         if (style.isLeftToRightDirection())
246             return renderer.borderLeft();
247         
248         return renderer.borderRight();
249     }
250     if (style.isLeftToRightDirection())
251         return renderer.borderTop();
252     
253     return renderer.borderBottom();
254 }
255
256 static inline LayoutUnit borderAndPaddingStartWithStyleForWritingMode(const RenderBox& renderer, const RenderStyle& style)
257 {
258     if (style.isHorizontalWritingMode()) {
259         if (style.isLeftToRightDirection())
260             return renderer.borderLeft() + renderer.paddingLeft();
261         
262         return renderer.borderRight() + renderer.paddingRight();
263     }
264     if (style.isLeftToRightDirection())
265         return renderer.borderTop() + renderer.paddingTop();
266     
267     return renderer.borderBottom() + renderer.paddingBottom();
268 }
269
270 LayoutUnit ShapeOutsideInfo::logicalLeftOffset() const
271 {
272     if (m_renderer.isRenderRegion())
273         return LayoutUnit();
274     
275     switch (referenceBox(*m_renderer.style().shapeOutside())) {
276     case MarginBox: return -m_renderer.marginStart(&m_renderer.containingBlock()->style());
277     case BorderBox: return LayoutUnit();
278     case PaddingBox: return borderStartWithStyleForWritingMode(m_renderer, m_renderer.containingBlock()->style());
279     case ContentBox: return borderAndPaddingStartWithStyleForWritingMode(m_renderer, m_renderer.containingBlock()->style());
280     case Fill: break;
281     case Stroke: break;
282     case ViewBox: break;
283     case BoxMissing: break;
284     }
285
286     ASSERT_NOT_REACHED();
287     return LayoutUnit();
288 }
289
290 bool ShapeOutsideInfo::isEnabledFor(const RenderBox& box)
291 {
292     ShapeValue* shapeValue = box.style().shapeOutside();
293     if (!box.isFloating() || !shapeValue)
294         return false;
295
296     switch (shapeValue->type()) {
297     case ShapeValue::Type::Shape: return shapeValue->shape();
298     case ShapeValue::Type::Image: return shapeValue->isImageValid() && checkShapeImageOrigin(box.document(), *(shapeValue->image()));
299     case ShapeValue::Type::Box: return true;
300     }
301
302     ASSERT_NOT_REACHED();
303     return false;
304 }
305
306 ShapeOutsideDeltas ShapeOutsideInfo::computeDeltasForContainingBlockLine(const RenderBlockFlow& containingBlock, const FloatingObject& floatingObject, LayoutUnit lineTop, LayoutUnit lineHeight)
307 {
308     ASSERT(lineHeight >= 0);
309     LayoutUnit borderBoxTop = containingBlock.logicalTopForFloat(&floatingObject) + containingBlock.marginBeforeForChild(m_renderer);
310     LayoutUnit borderBoxLineTop = lineTop - borderBoxTop;
311
312     if (isShapeDirty() || !m_shapeOutsideDeltas.isForLine(borderBoxLineTop, lineHeight)) {
313         LayoutUnit referenceBoxLineTop = borderBoxLineTop - logicalTopOffset();
314         LayoutUnit floatMarginBoxWidth = containingBlock.logicalWidthForFloat(&floatingObject);
315
316         if (computedShape().lineOverlapsShapeMarginBounds(referenceBoxLineTop, lineHeight)) {
317             LineSegment segment = computedShape().getExcludedInterval((borderBoxLineTop - logicalTopOffset()), std::min(lineHeight, shapeLogicalBottom() - borderBoxLineTop));
318             if (segment.isValid) {
319                 LayoutUnit logicalLeftMargin = containingBlock.style().isLeftToRightDirection() ? containingBlock.marginStartForChild(m_renderer) : containingBlock.marginEndForChild(m_renderer);
320                 LayoutUnit rawLeftMarginBoxDelta = segment.logicalLeft + logicalLeftOffset() + logicalLeftMargin;
321                 LayoutUnit leftMarginBoxDelta = clampTo<LayoutUnit>(rawLeftMarginBoxDelta, LayoutUnit(), floatMarginBoxWidth);
322
323                 LayoutUnit logicalRightMargin = containingBlock.style().isLeftToRightDirection() ? containingBlock.marginEndForChild(m_renderer) : containingBlock.marginStartForChild(m_renderer);
324                 LayoutUnit rawRightMarginBoxDelta = segment.logicalRight + logicalLeftOffset() - containingBlock.logicalWidthForChild(m_renderer) - logicalRightMargin;
325                 LayoutUnit rightMarginBoxDelta = clampTo<LayoutUnit>(rawRightMarginBoxDelta, -floatMarginBoxWidth, LayoutUnit());
326
327                 m_shapeOutsideDeltas = ShapeOutsideDeltas(leftMarginBoxDelta, rightMarginBoxDelta, true, borderBoxLineTop, lineHeight);
328                 return m_shapeOutsideDeltas;
329             }
330         }
331
332         // Lines that do not overlap the shape should act as if the float
333         // wasn't there for layout purposes. So we set the deltas to remove the
334         // entire width of the float
335         m_shapeOutsideDeltas = ShapeOutsideDeltas(floatMarginBoxWidth, -floatMarginBoxWidth, false, borderBoxLineTop, lineHeight);
336     }
337
338     return m_shapeOutsideDeltas;
339 }
340
341 }
342
343 #endif