1 /*
2 * Copyright (C) 2013 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "core/rendering/style/ShadowList.h"
33
34 #include "platform/geometry/FloatRect.h"
35 #include "wtf/OwnPtr.h"
36
37 namespace blink {
38
calculateShadowExtent(const ShadowList * shadowList,float & shadowLeft,float & shadowRight,float & shadowTop,float & shadowBottom)39 static inline void calculateShadowExtent(const ShadowList* shadowList, float& shadowLeft, float& shadowRight, float& shadowTop, float& shadowBottom)
40 {
41 ASSERT(shadowList);
42 size_t shadowCount = shadowList->shadows().size();
43 for (size_t i = 0; i < shadowCount; ++i) {
44 const ShadowData& shadow = shadowList->shadows()[i];
45 if (shadow.style() == Inset)
46 continue;
47 float blurAndSpread = shadow.blur() + shadow.spread();
48 shadowLeft = std::min(shadow.x() - blurAndSpread, shadowLeft);
49 shadowRight = std::max(shadow.x() + blurAndSpread, shadowRight);
50 shadowTop = std::min(shadow.y() - blurAndSpread, shadowTop);
51 shadowBottom = std::max(shadow.y() + blurAndSpread, shadowBottom);
52 }
53 }
54
adjustRectForShadow(LayoutRect & rect) const55 void ShadowList::adjustRectForShadow(LayoutRect& rect) const
56 {
57 FloatRect floatRect(rect);
58 adjustRectForShadow(floatRect);
59 rect = LayoutRect(floatRect);
60 }
61
adjustRectForShadow(FloatRect & rect) const62 void ShadowList::adjustRectForShadow(FloatRect& rect) const
63 {
64 float shadowLeft = 0;
65 float shadowRight = 0;
66 float shadowTop = 0;
67 float shadowBottom = 0;
68 calculateShadowExtent(this, shadowLeft, shadowRight, shadowTop, shadowBottom);
69
70 rect.move(shadowLeft, shadowTop);
71 rect.setWidth(rect.width() - shadowLeft + shadowRight);
72 rect.setHeight(rect.height() - shadowTop + shadowBottom);
73 }
74
blend(const ShadowList * from,const ShadowList * to,double progress)75 PassRefPtr<ShadowList> ShadowList::blend(const ShadowList* from, const ShadowList* to, double progress)
76 {
77 size_t fromLength = from ? from->shadows().size() : 0;
78 size_t toLength = to ? to->shadows().size() : 0;
79 if (!fromLength && !toLength)
80 return nullptr;
81
82 ShadowDataVector shadows;
83
84 DEFINE_STATIC_LOCAL(ShadowData, defaultShadowData, (FloatPoint(), 0, 0, Normal, Color::transparent));
85 DEFINE_STATIC_LOCAL(ShadowData, defaultInsetShadowData, (FloatPoint(), 0, 0, Inset, Color::transparent));
86
87 size_t maxLength = std::max(fromLength, toLength);
88 for (size_t i = 0; i < maxLength; ++i) {
89 const ShadowData* fromShadow = i < fromLength ? &from->shadows()[i] : 0;
90 const ShadowData* toShadow = i < toLength ? &to->shadows()[i] : 0;
91 if (!fromShadow)
92 fromShadow = toShadow->style() == Inset ? &defaultInsetShadowData : &defaultShadowData;
93 else if (!toShadow)
94 toShadow = fromShadow->style() == Inset ? &defaultInsetShadowData : &defaultShadowData;
95 shadows.append(toShadow->blend(*fromShadow, progress));
96 }
97
98 return ShadowList::adopt(shadows);
99 }
100
createDrawLooper(DrawLooperBuilder::ShadowAlphaMode alphaMode,bool isHorizontal) const101 PassOwnPtr<DrawLooperBuilder> ShadowList::createDrawLooper(DrawLooperBuilder::ShadowAlphaMode alphaMode, bool isHorizontal) const
102 {
103 OwnPtr<DrawLooperBuilder> drawLooperBuilder = DrawLooperBuilder::create();
104 for (size_t i = shadows().size(); i--; ) {
105 const ShadowData& shadow = shadows()[i];
106 float shadowX = isHorizontal ? shadow.x() : shadow.y();
107 float shadowY = isHorizontal ? shadow.y() : -shadow.x();
108 drawLooperBuilder->addShadow(FloatSize(shadowX, shadowY), shadow.blur(), shadow.color(),
109 DrawLooperBuilder::ShadowRespectsTransforms, alphaMode);
110 }
111 drawLooperBuilder->addUnmodifiedContent();
112 return drawLooperBuilder.release();
113 }
114
115 } // namespace blink
116