1/*
2 * Copyright (C) 2014 Apple Inc. 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 copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "BorderEdge.h"
28
29#include "Color.h"
30#include "LayoutUnit.h"
31#include "RenderObject.h"
32#include "RenderStyle.h"
33
34namespace WebCore {
35
36BorderEdge::BorderEdge(float edgeWidth, Color edgeColor, BorderStyle edgeStyle, bool edgeIsTransparent, bool edgeIsPresent, float devicePixelRatio)
37 : m_color(edgeColor)
38 , m_width(edgeWidth)
39 , m_devicePixelRatio(devicePixelRatio)
40 , m_style(edgeStyle)
41 , m_isTransparent(edgeIsTransparent)
42 , m_isPresent(edgeIsPresent)
43{
44 if (edgeStyle == BorderStyle::Double && edgeWidth < borderWidthInDevicePixel(3))
45 m_style = BorderStyle::Solid;
46 m_flooredToDevicePixelWidth = floorf(edgeWidth * devicePixelRatio) / devicePixelRatio;
47}
48
49void BorderEdge::getBorderEdgeInfo(BorderEdge edges[], const RenderStyle& style, float deviceScaleFactor, bool includeLogicalLeftEdge, bool includeLogicalRightEdge)
50{
51 bool horizontal = style.isHorizontalWritingMode();
52
53 edges[BSTop] = BorderEdge(style.borderTopWidth(), style.visitedDependentColorWithColorFilter(CSSPropertyBorderTopColor), style.borderTopStyle(), style.borderTopIsTransparent(),
54 horizontal || includeLogicalLeftEdge, deviceScaleFactor);
55 edges[BSRight] = BorderEdge(style.borderRightWidth(), style.visitedDependentColorWithColorFilter(CSSPropertyBorderRightColor), style.borderRightStyle(), style.borderRightIsTransparent(),
56 !horizontal || includeLogicalRightEdge, deviceScaleFactor);
57 edges[BSBottom] = BorderEdge(style.borderBottomWidth(), style.visitedDependentColorWithColorFilter(CSSPropertyBorderBottomColor), style.borderBottomStyle(), style.borderBottomIsTransparent(),
58 horizontal || includeLogicalRightEdge, deviceScaleFactor);
59 edges[BSLeft] = BorderEdge(style.borderLeftWidth(), style.visitedDependentColorWithColorFilter(CSSPropertyBorderLeftColor), style.borderLeftStyle(), style.borderLeftIsTransparent(),
60 !horizontal || includeLogicalLeftEdge, deviceScaleFactor);
61 }
62
63bool BorderEdge::obscuresBackgroundEdge(float scale) const
64{
65 if (!m_isPresent || m_isTransparent || (m_width * scale) < borderWidthInDevicePixel(2) || !m_color.isOpaque() || m_style == BorderStyle::Hidden)
66 return false;
67
68 if (m_style == BorderStyle::Dotted || m_style == BorderStyle::Dashed)
69 return false;
70
71 if (m_style == BorderStyle::Double)
72 return m_width >= scale * borderWidthInDevicePixel(5); // The outer band needs to be >= 2px wide at unit scale.
73
74 return true;
75}
76
77bool BorderEdge::obscuresBackground() const
78{
79 if (!m_isPresent || m_isTransparent || !m_color.isOpaque() || m_style == BorderStyle::Hidden)
80 return false;
81
82 if (m_style == BorderStyle::Dotted || m_style == BorderStyle::Dashed || m_style == BorderStyle::Double)
83 return false;
84
85 return true;
86}
87
88void BorderEdge::getDoubleBorderStripeWidths(LayoutUnit& outerWidth, LayoutUnit& innerWidth) const
89{
90 LayoutUnit fullWidth = widthForPainting();
91 innerWidth = ceilToDevicePixel(fullWidth * 2 / 3, m_devicePixelRatio);
92 outerWidth = floorToDevicePixel(fullWidth / 3, m_devicePixelRatio);
93}
94
95} // namespace WebCore
96