1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2006 Allan Sandfeld Jensen (kde@carewolf.com)
5 *           (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 Apple Inc. All rights reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB.  If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 *
23 */
24
25#ifndef RenderImage_h
26#define RenderImage_h
27
28#include "RenderImageResource.h"
29#include "RenderReplaced.h"
30
31namespace WebCore {
32
33class HTMLAreaElement;
34class HTMLMapElement;
35
36class RenderImage : public RenderReplaced {
37public:
38    RenderImage(Element&, PassRef<RenderStyle>, StyleImage* = nullptr, const float = 1.0f);
39    RenderImage(Document&, PassRef<RenderStyle>, StyleImage* = nullptr);
40    virtual ~RenderImage();
41
42    RenderImageResource& imageResource() { return *m_imageResource; }
43    const RenderImageResource& imageResource() const { return *m_imageResource; }
44    CachedImage* cachedImage() const { return imageResource().cachedImage(); }
45
46    bool setImageSizeForAltText(CachedImage* newImage = 0);
47
48    void updateAltText();
49
50    HTMLMapElement* imageMap() const;
51    void areaElementFocusChanged(HTMLAreaElement*);
52
53#if PLATFORM(IOS)
54    virtual void collectSelectionRects(Vector<SelectionRect>&, unsigned, unsigned) override;
55#endif
56
57    void setIsGeneratedContent(bool generated = true) { m_isGeneratedContent = generated; }
58
59    bool isGeneratedContent() const { return m_isGeneratedContent; }
60
61    const String& altText() const { return m_altText; }
62    void setAltText(const String& altText) { m_altText = altText; }
63
64    inline void setImageDevicePixelRatio(float factor) { m_imageDevicePixelRatio = factor; }
65    float imageDevicePixelRatio() const { return m_imageDevicePixelRatio; }
66
67    void setHasShadowControls(bool hasShadowControls) { m_hasShadowControls = hasShadowControls; }
68
69protected:
70    virtual bool needsPreferredWidthsRecalculation() const override final;
71    virtual RenderBox* embeddedContentBox() const override final;
72    virtual void computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const override final;
73    virtual bool foregroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect, unsigned maxDepthToTest) const override;
74
75    virtual void styleDidChange(StyleDifference, const RenderStyle*) override final;
76
77    virtual void imageChanged(WrappedImagePtr, const IntRect* = 0) override;
78
79    void paintIntoRect(GraphicsContext*, const FloatRect&);
80    virtual void paint(PaintInfo&, const LayoutPoint&) override final;
81    virtual void layout() override;
82
83    virtual void intrinsicSizeChanged() override
84    {
85        imageChanged(imageResource().imagePtr());
86    }
87
88private:
89    virtual const char* renderName() const override { return "RenderImage"; }
90
91    virtual bool canHaveChildren() const override;
92
93    virtual bool isImage() const override { return true; }
94    virtual bool isRenderImage() const override final { return true; }
95
96    virtual void paintReplaced(PaintInfo&, const LayoutPoint&) override;
97
98    virtual bool computeBackgroundIsKnownToBeObscured() override final;
99
100    virtual LayoutUnit minimumReplacedHeight() const override;
101
102    virtual void notifyFinished(CachedResource*) override final;
103    virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) override final;
104
105    virtual bool boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance, InlineFlowBox*) const override final;
106
107    virtual bool shadowControlsNeedCustomLayoutMetrics() const { return false; }
108
109    IntSize imageSizeForError(CachedImage*) const;
110    void imageDimensionsChanged(bool imageSizeChanged, const IntRect* = 0);
111    bool updateIntrinsicSizeIfNeeded(const LayoutSize&, bool imageSizeChanged);
112    // Update the size of the image to be rendered. Object-fit may cause this to be different from the CSS box's content rect.
113    void updateInnerContentRect();
114
115    void paintAreaElementFocusRing(PaintInfo&);
116
117    void layoutShadowControls(const LayoutSize& oldSize);
118
119    // Text to display as long as the image isn't available.
120    String m_altText;
121    std::unique_ptr<RenderImageResource> m_imageResource;
122    bool m_needsToSetSizeForAltText;
123    bool m_didIncrementVisuallyNonEmptyPixelCount;
124    bool m_isGeneratedContent;
125    bool m_hasShadowControls;
126    float m_imageDevicePixelRatio;
127
128    friend class RenderImageScaleObserver;
129};
130
131RENDER_OBJECT_TYPE_CASTS(RenderImage, isRenderImage())
132
133} // namespace WebCore
134
135#endif // RenderImage_h
136