1 | /* |
2 | * Copyright (C) 2000 Lars Knoll (knoll@kde.org) |
3 | * (C) 2000 Antti Koivisto (koivisto@kde.org) |
4 | * (C) 2000 Dirk Mueller (mueller@kde.org) |
5 | * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
6 | * |
7 | * This library is free software; you can redistribute it and/or |
8 | * modify it under the terms of the GNU Library General Public |
9 | * License as published by the Free Software Foundation; either |
10 | * version 2 of the License, or (at your option) any later version. |
11 | * |
12 | * This library is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | * Library General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU Library General Public License |
18 | * along with this library; see the file COPYING.LIB. If not, write to |
19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | * Boston, MA 02110-1301, USA. |
21 | * |
22 | */ |
23 | |
24 | #include "config.h" |
25 | #include "StyleGeneratedImage.h" |
26 | |
27 | #include "CSSImageGeneratorValue.h" |
28 | #include "RenderElement.h" |
29 | #include "StyleResolver.h" |
30 | |
31 | namespace WebCore { |
32 | |
33 | StyleGeneratedImage::StyleGeneratedImage(Ref<CSSImageGeneratorValue>&& value) |
34 | : m_imageGeneratorValue(WTFMove(value)) |
35 | , m_fixedSize(m_imageGeneratorValue->isFixedSize()) |
36 | { |
37 | m_isGeneratedImage = true; |
38 | } |
39 | |
40 | Ref<CSSValue> StyleGeneratedImage::cssValue() const |
41 | { |
42 | return m_imageGeneratorValue.copyRef(); |
43 | } |
44 | |
45 | bool StyleGeneratedImage::isPending() const |
46 | { |
47 | return m_imageGeneratorValue->isPending(); |
48 | } |
49 | |
50 | void StyleGeneratedImage::load(CachedResourceLoader& loader, const ResourceLoaderOptions& options) |
51 | { |
52 | m_imageGeneratorValue->loadSubimages(loader, options); |
53 | } |
54 | |
55 | FloatSize StyleGeneratedImage::imageSize(const RenderElement* renderer, float multiplier) const |
56 | { |
57 | ASSERT(renderer); |
58 | if (m_fixedSize) { |
59 | FloatSize fixedSize = m_imageGeneratorValue->fixedSize(*renderer); |
60 | if (multiplier == 1.0f) |
61 | return fixedSize; |
62 | |
63 | float width = fixedSize.width() * multiplier; |
64 | float height = fixedSize.height() * multiplier; |
65 | |
66 | // Don't let images that have a width/height >= 1 shrink below 1 device pixel when zoomed. |
67 | float deviceScaleFactor = renderer ? renderer->document().deviceScaleFactor() : 1; |
68 | if (fixedSize.width() > 0) |
69 | width = std::max<float>(1 / deviceScaleFactor, width); |
70 | |
71 | if (fixedSize.height() > 0) |
72 | height = std::max<float>(1 / deviceScaleFactor, height); |
73 | |
74 | return FloatSize(width, height); |
75 | } |
76 | |
77 | return m_containerSize; |
78 | } |
79 | |
80 | void StyleGeneratedImage::computeIntrinsicDimensions(const RenderElement* renderer, Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio) |
81 | { |
82 | // At a zoom level of 1 the image is guaranteed to have a device pixel size. |
83 | FloatSize size = floorSizeToDevicePixels(LayoutSize(imageSize(renderer, 1)), renderer ? renderer->document().deviceScaleFactor() : 1); |
84 | intrinsicWidth = Length(size.width(), Fixed); |
85 | intrinsicHeight = Length(size.height(), Fixed); |
86 | intrinsicRatio = size; |
87 | } |
88 | |
89 | void StyleGeneratedImage::addClient(RenderElement* renderer) |
90 | { |
91 | ASSERT(renderer); |
92 | m_imageGeneratorValue->addClient(*renderer); |
93 | } |
94 | |
95 | void StyleGeneratedImage::removeClient(RenderElement* renderer) |
96 | { |
97 | ASSERT(renderer); |
98 | m_imageGeneratorValue->removeClient(*renderer); |
99 | } |
100 | |
101 | RefPtr<Image> StyleGeneratedImage::image(RenderElement* renderer, const FloatSize& size) const |
102 | { |
103 | ASSERT(renderer); |
104 | return m_imageGeneratorValue->image(*renderer, size); |
105 | } |
106 | |
107 | bool StyleGeneratedImage::knownToBeOpaque(const RenderElement* renderer) const |
108 | { |
109 | ASSERT(renderer); |
110 | return m_imageGeneratorValue->knownToBeOpaque(*renderer); |
111 | } |
112 | |
113 | } |
114 | |