2 * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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
31 #include "core/rendering/shapes/ShapeOutsideInfo.h"
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"
43 CSSBoxType referenceBox(const ShapeValue& shapeValue)
45 if (shapeValue.cssBox() == BoxMissing)
47 return shapeValue.cssBox();
50 void ShapeOutsideInfo::setReferenceBoxLogicalSize(LayoutSize newReferenceBoxLogicalSize)
52 bool isHorizontalWritingMode = m_renderer.containingBlock()->style()->isHorizontalWritingMode();
53 switch (referenceBox(*m_renderer.style()->shapeOutside())) {
55 if (isHorizontalWritingMode)
56 newReferenceBoxLogicalSize.expand(m_renderer.marginWidth(), m_renderer.marginHeight());
58 newReferenceBoxLogicalSize.expand(m_renderer.marginHeight(), m_renderer.marginWidth());
63 if (isHorizontalWritingMode)
64 newReferenceBoxLogicalSize.shrink(m_renderer.borderWidth(), m_renderer.borderHeight());
66 newReferenceBoxLogicalSize.shrink(m_renderer.borderHeight(), m_renderer.borderWidth());
69 if (isHorizontalWritingMode)
70 newReferenceBoxLogicalSize.shrink(m_renderer.borderAndPaddingWidth(), m_renderer.borderAndPaddingHeight());
72 newReferenceBoxLogicalSize.shrink(m_renderer.borderAndPaddingHeight(), m_renderer.borderAndPaddingWidth());
79 if (m_referenceBoxLogicalSize == newReferenceBoxLogicalSize)
82 m_referenceBoxLogicalSize = newReferenceBoxLogicalSize;
85 static bool checkShapeImageOrigin(Document& document, const StyleImage& styleImage)
87 if (styleImage.isGeneratedImage())
90 ASSERT(styleImage.cachedImage());
91 ImageResource& imageResource = *(styleImage.cachedImage());
92 if (imageResource.isAccessAllowed(document.securityOrigin()))
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 + "."));
102 static LayoutRect getShapeImageMarginRect(const RenderBox& renderBox, const LayoutSize& referenceBoxLogicalSize)
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);
109 static bool isValidRasterShapeRect(const LayoutRect& rect)
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());
116 return (rect.width().toFloat() * rect.height().toFloat() * 4.0) < maxImageSizeBytes;
119 PassOwnPtr<Shape> ShapeOutsideInfo::createShapeForImage(StyleImage* styleImage, float shapeImageThreshold, WritingMode writingMode, float margin) const
121 const IntSize& imageSize = m_renderer.calculateImageIntrinsicDimensions(styleImage, roundedIntSize(m_referenceBoxLogicalSize), RenderImage::ScaleByEffectiveZoom);
122 styleImage->setContainerSizeForRenderer(&m_renderer, imageSize, m_renderer.style()->effectiveZoom());
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);
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);
134 ASSERT(!styleImage->isPendingImage());
135 RefPtr<Image> image = styleImage->image(const_cast<RenderBox*>(&m_renderer), imageSize);
137 return Shape::createRasterShape(image.get(), shapeImageThreshold, imageRect, marginRect, writingMode, margin);
140 const Shape& ShapeOutsideInfo::computedShape() const
142 if (Shape* shape = m_shape.get())
145 const RenderStyle& style = *m_renderer.style();
146 ASSERT(m_renderer.containingBlock());
147 const RenderStyle& containingBlockStyle = *m_renderer.containingBlock()->style();
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());
153 float shapeImageThreshold = style.shapeImageThreshold();
154 ASSERT(style.shapeOutside());
155 const ShapeValue& shapeValue = *style.shapeOutside();
157 switch (shapeValue.type()) {
158 case ShapeValue::Shape:
159 ASSERT(shapeValue.shape());
160 m_shape = Shape::createShape(shapeValue.shape(), m_referenceBoxLogicalSize, writingMode, margin);
162 case ShapeValue::Image:
163 ASSERT(shapeValue.isImageValid());
164 m_shape = createShapeForImage(shapeValue.image(), shapeImageThreshold, writingMode, margin);
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);
177 inline LayoutUnit borderBeforeInWritingMode(const RenderBox& renderer, WritingMode writingMode)
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();
186 ASSERT_NOT_REACHED();
187 return renderer.borderBefore();
190 inline LayoutUnit borderAndPaddingBeforeInWritingMode(const RenderBox& renderer, WritingMode writingMode)
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();
199 ASSERT_NOT_REACHED();
200 return renderer.borderAndPaddingBefore();
203 LayoutUnit ShapeOutsideInfo::logicalTopOffset() const
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;
213 ASSERT_NOT_REACHED();
217 inline LayoutUnit borderStartWithStyleForWritingMode(const RenderBox& renderer, const RenderStyle* style)
219 if (style->isHorizontalWritingMode()) {
220 if (style->isLeftToRightDirection())
221 return renderer.borderLeft();
223 return renderer.borderRight();
225 if (style->isLeftToRightDirection())
226 return renderer.borderTop();
228 return renderer.borderBottom();
231 inline LayoutUnit borderAndPaddingStartWithStyleForWritingMode(const RenderBox& renderer, const RenderStyle* style)
233 if (style->isHorizontalWritingMode()) {
234 if (style->isLeftToRightDirection())
235 return renderer.borderLeft() + renderer.paddingLeft();
237 return renderer.borderRight() + renderer.paddingRight();
239 if (style->isLeftToRightDirection())
240 return renderer.borderTop() + renderer.paddingTop();
242 return renderer.borderBottom() + renderer.paddingBottom();
245 LayoutUnit ShapeOutsideInfo::logicalLeftOffset() const
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;
255 ASSERT_NOT_REACHED();
260 bool ShapeOutsideInfo::isEnabledFor(const RenderBox& box)
262 ShapeValue* shapeValue = box.style()->shapeOutside();
263 if (!box.isFloating() || !shapeValue)
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:
277 ShapeOutsideDeltas ShapeOutsideInfo::computeDeltasForContainingBlockLine(const RenderBlockFlow& containingBlock, const FloatingObject& floatingObject, LayoutUnit lineTop, LayoutUnit lineHeight)
279 ASSERT(lineHeight >= 0);
281 LayoutUnit borderBoxTop = containingBlock.logicalTopForFloat(&floatingObject) + containingBlock.marginBeforeForChild(&m_renderer);
282 LayoutUnit borderBoxLineTop = lineTop - borderBoxTop;
284 if (isShapeDirty() || !m_shapeOutsideDeltas.isForLine(borderBoxLineTop, lineHeight)) {
285 LayoutUnit referenceBoxLineTop = borderBoxLineTop - logicalTopOffset();
286 LayoutUnit floatMarginBoxWidth = containingBlock.logicalWidthForFloat(&floatingObject);
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);
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());
299 m_shapeOutsideDeltas = ShapeOutsideDeltas(leftMarginBoxDelta, rightMarginBoxDelta, true, borderBoxLineTop, lineHeight);
300 return m_shapeOutsideDeltas;
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);
310 return m_shapeOutsideDeltas;
313 LayoutRect ShapeOutsideInfo::computedShapePhysicalBoundingBox() const
315 LayoutRect physicalBoundingBox = computedShape().shapeMarginLogicalBoundingBox();
316 physicalBoundingBox.setX(physicalBoundingBox.x() + logicalLeftOffset());
318 if (m_renderer.style()->isFlippedBlocksWritingMode())
319 physicalBoundingBox.setY(m_renderer.logicalHeight() - physicalBoundingBox.maxY());
321 physicalBoundingBox.setY(physicalBoundingBox.y() + logicalTopOffset());
323 if (!m_renderer.style()->isHorizontalWritingMode())
324 physicalBoundingBox = physicalBoundingBox.transposedRect();
326 physicalBoundingBox.setY(physicalBoundingBox.y() + logicalTopOffset());
328 return physicalBoundingBox;
331 FloatPoint ShapeOutsideInfo::shapeToRendererPoint(FloatPoint point) const
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();
341 FloatSize ShapeOutsideInfo::shapeToRendererSize(FloatSize size) const
343 if (!m_renderer.style()->isHorizontalWritingMode())
344 return size.transposedSize();