1/*
2 * Copyright (C) 2010 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 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "FindIndicator.h"
28
29#include "ShareableBitmap.h"
30#include <WebCore/Gradient.h>
31#include <WebCore/GraphicsContext.h>
32#include <WebCore/IntRect.h>
33#include <WebCore/Path.h>
34
35using namespace WebCore;
36
37static const float cornerRadius = 3.0;
38
39static const float shadowOffsetX = 0.0;
40static const float shadowOffsetY = 1.0;
41static const float shadowBlurRadius = 3.0;
42
43static const int shadowRed = 0;
44static const int shadowGreen = 0;
45static const int shadowBlue = 0;
46static const int shadowAlpha = 204;
47
48static const float lightBorderThickness = 1.0;
49static const float horizontalPaddingInsideLightBorder = 3.0;
50static const float verticalPaddingInsideLightBorder = 1.0;
51
52static const float horizontalBorderInsideShadow = lightBorderThickness + horizontalPaddingInsideLightBorder;
53static const float verticalBorderInsideShadow = lightBorderThickness + verticalPaddingInsideLightBorder;
54
55static const float leftBorderThickness = horizontalBorderInsideShadow + shadowOffsetX + shadowBlurRadius / 2.0;
56static const float topBorderThickness = verticalBorderInsideShadow - shadowOffsetY + shadowBlurRadius / 2.0;
57static const float rightBorderThickness = horizontalBorderInsideShadow - shadowOffsetX + shadowBlurRadius / 2.0;
58static const float bottomBorderThickness = verticalBorderInsideShadow + shadowOffsetY + shadowBlurRadius / 2.0;
59
60static const float horizontalOutsetToCenterOfLightBorder = horizontalBorderInsideShadow - lightBorderThickness / 2.0;
61static const float verticalOutsetToCenterOfLightBorder = verticalBorderInsideShadow - lightBorderThickness / 2.0;
62
63static const int lightBorderRed = 245;
64static const int lightBorderGreen = 230;
65static const int lightBorderBlue = 0;
66static const int lightBorderAlpha = 255;
67
68static const int gradientDarkRed = 237;
69static const int gradientDarkGreen = 204;
70static const int gradientDarkBlue = 0;
71static const int gradientDarkAlpha = 255;
72
73static const int gradientLightRed = 242;
74static const int gradientLightGreen = 239;
75static const int gradientLightBlue = 0;
76static const int gradientLightAlpha = 255;
77
78namespace WebKit {
79
80PassRefPtr<FindIndicator> FindIndicator::create(const FloatRect& selectionRectInWindowCoordinates, const Vector<FloatRect>& textRectsInSelectionRectCoordinates, const ShareableBitmap::Handle& contentImageHandle)
81{
82    RefPtr<ShareableBitmap> contentImage = ShareableBitmap::create(contentImageHandle);
83    if (!contentImage)
84        return 0;
85    ASSERT(contentImage->size() == enclosingIntRect(selectionRectInWindowCoordinates).size());
86
87    return adoptRef(new FindIndicator(selectionRectInWindowCoordinates, textRectsInSelectionRectCoordinates, contentImage.release()));
88}
89
90FindIndicator::FindIndicator(const WebCore::FloatRect& selectionRectInWindowCoordinates, const Vector<WebCore::FloatRect>& textRectsInSelectionRectCoordinates, PassRefPtr<ShareableBitmap> contentImage)
91    : m_selectionRectInWindowCoordinates(selectionRectInWindowCoordinates)
92    , m_textRectsInSelectionRectCoordinates(textRectsInSelectionRectCoordinates)
93    , m_contentImage(contentImage)
94{
95}
96
97FindIndicator::~FindIndicator()
98{
99}
100
101static FloatRect inflateRect(const FloatRect& rect, float inflateX, float inflateY)
102{
103    FloatRect inflatedRect = rect;
104    inflatedRect.inflateX(inflateX);
105    inflatedRect.inflateY(inflateY);
106
107    return inflatedRect;
108}
109
110FloatRect FindIndicator::frameRect() const
111{
112    return FloatRect(m_selectionRectInWindowCoordinates.x() - leftBorderThickness, m_selectionRectInWindowCoordinates.y() - topBorderThickness,
113                     m_selectionRectInWindowCoordinates.width() + rightBorderThickness + leftBorderThickness,
114                     m_selectionRectInWindowCoordinates.height() + topBorderThickness + bottomBorderThickness);
115}
116
117static Color lightBorderColor()
118{
119    return Color(lightBorderRed, lightBorderGreen, lightBorderBlue, lightBorderAlpha);
120}
121
122static Color shadowColor()
123{
124    return Color(shadowRed, shadowGreen, shadowBlue, shadowAlpha);
125}
126
127static Color gradientLightColor()
128{
129    return Color(gradientLightRed, gradientLightGreen, gradientLightBlue, gradientLightAlpha);
130}
131
132static Color gradientDarkColor()
133{
134    return Color(gradientDarkRed, gradientDarkGreen, gradientDarkBlue, gradientDarkAlpha);
135}
136
137static Path pathWithRoundedRect(const FloatRect& pathRect, float radius)
138{
139    Path path;
140    path.addRoundedRect(pathRect, FloatSize(radius, radius));
141
142    return path;
143}
144
145void FindIndicator::draw(GraphicsContext& graphicsContext, const IntRect& dirtyRect)
146{
147    for (size_t i = 0; i < m_textRectsInSelectionRectCoordinates.size(); ++i) {
148        FloatRect textRect = m_textRectsInSelectionRectCoordinates[i];
149        textRect.move(leftBorderThickness, topBorderThickness);
150
151        graphicsContext.save();
152        FloatRect outerPathRect = inflateRect(textRect, horizontalOutsetToCenterOfLightBorder, verticalOutsetToCenterOfLightBorder);
153        graphicsContext.setShadow(FloatSize(shadowOffsetX, shadowOffsetY), shadowBlurRadius, shadowColor(), ColorSpaceSRGB);
154        graphicsContext.setFillColor(lightBorderColor(), ColorSpaceDeviceRGB);
155        graphicsContext.fillPath(pathWithRoundedRect(outerPathRect, cornerRadius));
156        graphicsContext.restore();
157
158        graphicsContext.save();
159        FloatRect innerPathRect = inflateRect(textRect, horizontalPaddingInsideLightBorder, verticalPaddingInsideLightBorder);
160        graphicsContext.clip(pathWithRoundedRect(innerPathRect, cornerRadius));
161        RefPtr<Gradient> gradient = Gradient::create(FloatPoint(innerPathRect.x(), innerPathRect.y()), FloatPoint(innerPathRect.x(), innerPathRect.maxY()));
162        gradient->addColorStop(0, gradientLightColor());
163        gradient->addColorStop(1, gradientDarkColor());
164        graphicsContext.setFillGradient(gradient);
165        graphicsContext.fillRect(outerPathRect);
166        graphicsContext.restore();
167
168        graphicsContext.save();
169        graphicsContext.translate(FloatSize(roundf(leftBorderThickness), roundf(topBorderThickness)));
170        m_contentImage->paint(graphicsContext, IntPoint(0, 0), m_contentImage->bounds());
171        graphicsContext.restore();
172    }
173}
174
175} // namespace WebKit
176