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 36 namespace WebCore { 37 38 static inline void calculateShadowExtent(const ShadowList* shadowList, float additionalOutlineSize, float& shadowLeft, float& shadowRight, float& shadowTop, float& shadowBottom) 39 { 40 ASSERT(shadowList); 41 size_t shadowCount = shadowList->shadows().size(); 42 for (size_t i = 0; i < shadowCount; ++i) { 43 const ShadowData& shadow = shadowList->shadows()[i]; 44 if (shadow.style() == Inset) 45 continue; 46 float blurAndSpread = shadow.blur() + shadow.spread() + additionalOutlineSize; 47 shadowLeft = std::min(shadow.x() - blurAndSpread, shadowLeft); 48 shadowRight = std::max(shadow.x() + blurAndSpread, shadowRight); 49 shadowTop = std::min(shadow.y() - blurAndSpread, shadowTop); 50 shadowBottom = std::max(shadow.y() + blurAndSpread, shadowBottom); 51 } 52 } 53 54 void ShadowList::adjustRectForShadow(LayoutRect& rect, float additionalOutlineSize) const 55 { 56 FloatRect floatRect(rect); 57 adjustRectForShadow(floatRect); 58 rect = LayoutRect(floatRect); 59 } 60 61 void ShadowList::adjustRectForShadow(FloatRect& rect, float additionalOutlineSize) const 62 { 63 float shadowLeft = 0; 64 float shadowRight = 0; 65 float shadowTop = 0; 66 float shadowBottom = 0; 67 calculateShadowExtent(this, additionalOutlineSize, shadowLeft, shadowRight, shadowTop, shadowBottom); 68 69 rect.move(shadowLeft, shadowTop); 70 rect.setWidth(rect.width() - shadowLeft + shadowRight); 71 rect.setHeight(rect.height() - shadowTop + shadowBottom); 72 } 73 74 PassRefPtr<ShadowList> ShadowList::blend(const ShadowList* from, const ShadowList* to, double progress) 75 { 76 size_t fromLength = from ? from->shadows().size() : 0; 77 size_t toLength = to ? to->shadows().size() : 0; 78 if (!fromLength && !toLength) 79 return nullptr; 80 81 ShadowDataVector shadows; 82 83 DEFINE_STATIC_LOCAL(ShadowData, defaultShadowData, (FloatPoint(), 0, 0, Normal, Color::transparent)); 84 DEFINE_STATIC_LOCAL(ShadowData, defaultInsetShadowData, (FloatPoint(), 0, 0, Inset, Color::transparent)); 85 86 size_t maxLength = std::max(fromLength, toLength); 87 for (size_t i = 0; i < maxLength; ++i) { 88 const ShadowData* fromShadow = i < fromLength ? &from->shadows()[i] : 0; 89 const ShadowData* toShadow = i < toLength ? &to->shadows()[i] : 0; 90 if (!fromShadow) 91 fromShadow = toShadow->style() == Inset ? &defaultInsetShadowData : &defaultShadowData; 92 else if (!toShadow) 93 toShadow = fromShadow->style() == Inset ? &defaultInsetShadowData : &defaultShadowData; 94 shadows.append(toShadow->blend(*fromShadow, progress)); 95 } 96 97 return ShadowList::adopt(shadows); 98 } 99 100 } // namespace WebCore 101