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