Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / shapes / ShapeInfo.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/ShapeInfo.h"
32
33 #include "core/rendering/RenderBlock.h"
34 #include "core/rendering/RenderImage.h"
35
36 namespace WebCore {
37
38 bool checkShapeImageOrigin(Document& document, ImageResource& imageResource)
39 {
40     if (imageResource.isAccessAllowed(document.securityOrigin()))
41         return true;
42
43     const KURL& url = imageResource.url();
44     String urlString = url.isNull() ? "''" : url.elidedString();
45     document.addConsoleMessage(SecurityMessageSource, ErrorMessageLevel, "Unsafe attempt to load URL " + urlString + ".");
46
47     return false;
48 }
49
50 static LayoutRect getShapeImageRect(const StyleImage& styleImage, const RenderBox* renderBox)
51 {
52     if (renderBox->isRenderImage())
53         return toRenderImage(renderBox)->replacedContentRect();
54
55     ASSERT(styleImage.cachedImage());
56     ASSERT(styleImage.cachedImage()->hasImage());
57     return LayoutRect(LayoutPoint(), styleImage.cachedImage()->image()->size());
58 }
59
60 template<class RenderType>
61 const Shape* ShapeInfo<RenderType>::computedShape() const
62 {
63     if (Shape* shape = m_shape.get())
64         return shape;
65
66     WritingMode writingMode = m_renderer->style()->writingMode();
67     Length margin = m_renderer->style()->shapeMargin();
68     Length padding = m_renderer->style()->shapePadding();
69     float shapeImageThreshold = m_renderer->style()->shapeImageThreshold();
70     const ShapeValue* shapeValue = this->shapeValue();
71     ASSERT(shapeValue);
72
73     switch (shapeValue->type()) {
74     case ShapeValue::Shape:
75         ASSERT(shapeValue->shape());
76         m_shape = Shape::createShape(shapeValue->shape(), m_shapeLogicalSize, writingMode, margin, padding);
77         break;
78     case ShapeValue::Image: {
79         ASSERT(shapeValue->image());
80         const StyleImage& styleImage = *(shapeValue->image());
81         m_shape = Shape::createRasterShape(styleImage, shapeImageThreshold, getShapeImageRect(styleImage, m_renderer), m_shapeLogicalSize, writingMode, margin, padding);
82         break;
83     }
84     case ShapeValue::Box: {
85         const RoundedRect& shapeRect = m_renderer->style()->getRoundedBorderFor(LayoutRect(LayoutPoint(), m_shapeLogicalSize), m_renderer->view());
86         m_shape = Shape::createLayoutBoxShape(shapeRect, writingMode, margin, padding);
87         break;
88     }
89     case ShapeValue::Outside:
90         // Outside should have already resolved to a different shape value.
91         ASSERT_NOT_REACHED();
92     }
93
94     ASSERT(m_shape);
95     return m_shape.get();
96 }
97
98 template<class RenderType>
99 SegmentList ShapeInfo<RenderType>::computeSegmentsForLine(LayoutUnit lineTop, LayoutUnit lineHeight) const
100 {
101     ASSERT(lineHeight >= 0);
102     SegmentList segments;
103
104     getIntervals((lineTop - logicalTopOffset()), std::min(lineHeight, shapeLogicalBottom() - lineTop), segments);
105
106     for (size_t i = 0; i < segments.size(); i++) {
107         segments[i].logicalLeft += logicalLeftOffset();
108         segments[i].logicalRight += logicalLeftOffset();
109     }
110
111     return segments;
112 }
113
114 template class ShapeInfo<RenderBlock>;
115 template class ShapeInfo<RenderBox>;
116 }