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
31namespace WebCore {
32
33StyleGeneratedImage::StyleGeneratedImage(PassRef<CSSImageGeneratorValue> value)
34    : m_imageGeneratorValue(WTF::move(value))
35    , m_fixedSize(m_imageGeneratorValue->isFixedSize())
36{
37    m_isGeneratedImage = true;
38}
39
40PassRefPtr<CSSValue> StyleGeneratedImage::cssValue() const
41{
42    return &const_cast<CSSImageGeneratorValue&>(m_imageGeneratorValue.get());
43}
44
45FloatSize StyleGeneratedImage::imageSize(const RenderElement* renderer, float multiplier) const
46{
47    if (m_fixedSize) {
48        FloatSize fixedSize = const_cast<CSSImageGeneratorValue&>(m_imageGeneratorValue.get()).fixedSize(renderer);
49        if (multiplier == 1.0f)
50            return fixedSize;
51
52        float width = fixedSize.width() * multiplier;
53        float height = fixedSize.height() * multiplier;
54
55        // Don't let images that have a width/height >= 1 shrink below 1 device pixel when zoomed.
56        float deviceScaleFactor = renderer ? renderer->document().deviceScaleFactor() : 1;
57        if (fixedSize.width() > 0)
58            width = std::max<float>(1 / deviceScaleFactor, width);
59
60        if (fixedSize.height() > 0)
61            height = std::max<float>(1 / deviceScaleFactor, height);
62
63        return FloatSize(width, height);
64    }
65
66    return m_containerSize;
67}
68
69void StyleGeneratedImage::computeIntrinsicDimensions(const RenderElement* renderer, Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio)
70{
71    // At a zoom level of 1 the image is guaranteed to have a device pixel size.
72    FloatSize size = flooredForPainting(LayoutSize(imageSize(renderer, 1)), renderer ? renderer->document().deviceScaleFactor() : 1);
73    intrinsicWidth = Length(size.width(), Fixed);
74    intrinsicHeight = Length(size.height(), Fixed);
75    intrinsicRatio = size;
76}
77
78void StyleGeneratedImage::addClient(RenderElement* renderer)
79{
80    m_imageGeneratorValue->addClient(renderer);
81}
82
83void StyleGeneratedImage::removeClient(RenderElement* renderer)
84{
85    m_imageGeneratorValue->removeClient(renderer);
86}
87
88PassRefPtr<Image> StyleGeneratedImage::image(RenderElement* renderer, const FloatSize& size) const
89{
90    return const_cast<CSSImageGeneratorValue&>(m_imageGeneratorValue.get()).image(renderer, size);
91}
92
93bool StyleGeneratedImage::knownToBeOpaque(const RenderElement* renderer) const
94{
95    return m_imageGeneratorValue->knownToBeOpaque(renderer);
96}
97
98}
99