1/*
2 * Copyright (C) 2013 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 HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "config.h"
31#include "core/rendering/shapes/BoxShape.h"
32
33#include "wtf/MathExtras.h"
34
35namespace blink {
36
37LayoutRect BoxShape::shapeMarginLogicalBoundingBox() const
38{
39    FloatRect marginBounds(m_bounds.rect());
40    if (shapeMargin() > 0)
41        marginBounds.inflate(shapeMargin());
42    return static_cast<LayoutRect>(marginBounds);
43}
44
45FloatRoundedRect BoxShape::shapeMarginBounds() const
46{
47    FloatRoundedRect marginBounds(m_bounds);
48    if (shapeMargin() > 0) {
49        marginBounds.inflate(shapeMargin());
50        marginBounds.expandRadii(shapeMargin());
51    }
52    return marginBounds;
53}
54
55LineSegment BoxShape::getExcludedInterval(LayoutUnit logicalTop, LayoutUnit logicalHeight) const
56{
57    const FloatRoundedRect& marginBounds = shapeMarginBounds();
58    if (marginBounds.isEmpty() || !lineOverlapsShapeMarginBounds(logicalTop, logicalHeight))
59        return LineSegment();
60
61    float y1 = logicalTop.toFloat();
62    float y2 = (logicalTop + logicalHeight).toFloat();
63    const FloatRect& rect = marginBounds.rect();
64
65    if (!marginBounds.isRounded())
66        return LineSegment(marginBounds.rect().x(), marginBounds.rect().maxX());
67
68    float topCornerMaxY = std::max<float>(marginBounds.topLeftCorner().maxY(), marginBounds.topRightCorner().maxY());
69    float bottomCornerMinY = std::min<float>(marginBounds.bottomLeftCorner().y(), marginBounds.bottomRightCorner().y());
70
71    if (topCornerMaxY <= bottomCornerMinY && y1 <= topCornerMaxY && y2 >= bottomCornerMinY)
72        return LineSegment(rect.x(), rect.maxX());
73
74    float x1 = rect.maxX();
75    float x2 = rect.x();
76    float minXIntercept;
77    float maxXIntercept;
78
79    if (y1 <= marginBounds.topLeftCorner().maxY() && y2 >= marginBounds.bottomLeftCorner().y())
80        x1 = rect.x();
81
82    if (y1 <= marginBounds.topRightCorner().maxY() && y2 >= marginBounds.bottomRightCorner().y())
83        x2 = rect.maxX();
84
85    if (marginBounds.xInterceptsAtY(y1, minXIntercept, maxXIntercept)) {
86        x1 = std::min<float>(x1, minXIntercept);
87        x2 = std::max<float>(x2, maxXIntercept);
88    }
89
90    if (marginBounds.xInterceptsAtY(y2, minXIntercept, maxXIntercept)) {
91        x1 = std::min<float>(x1, minXIntercept);
92        x2 = std::max<float>(x2, maxXIntercept);
93    }
94
95    ASSERT(x2 >= x1);
96    return LineSegment(x1, x2);
97}
98
99void BoxShape::buildDisplayPaths(DisplayPaths& paths) const
100{
101    paths.shape.addRoundedRect(m_bounds.rect(), m_bounds.radii().topLeft(), m_bounds.radii().topRight(), m_bounds.radii().bottomLeft(), m_bounds.radii().bottomRight());
102    if (shapeMargin())
103        paths.marginShape.addRoundedRect(shapeMarginBounds().rect(), shapeMarginBounds().radii().topLeft(), shapeMarginBounds().radii().topRight(), shapeMarginBounds().radii().bottomLeft(), shapeMarginBounds().radii().bottomRight());
104}
105
106} // namespace blink
107