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#pragma once
26
27#include "RenderImageResource.h"
28#include "RenderReplaced.h"
29
30namespace WebCore {
31
32class HTMLAreaElement;
33class HTMLMapElement;
34
35enum ImageSizeChangeType {
36 ImageSizeChangeNone,
37 ImageSizeChangeForAltText
38};
39
40class RenderImage : public RenderReplaced {
41 WTF_MAKE_ISO_ALLOCATED(RenderImage);
42public:
43 RenderImage(Element&, RenderStyle&&, StyleImage* = nullptr, const float = 1.0f);
44 RenderImage(Document&, RenderStyle&&, StyleImage* = nullptr);
45 virtual ~RenderImage();
46
47 RenderImageResource& imageResource() { return *m_imageResource; }
48 const RenderImageResource& imageResource() const { return *m_imageResource; }
49 CachedImage* cachedImage() const { return imageResource().cachedImage(); }
50
51 ImageSizeChangeType setImageSizeForAltText(CachedImage* newImage = nullptr);
52
53 void updateAltText();
54
55 HTMLMapElement* imageMap() const;
56 void areaElementFocusChanged(HTMLAreaElement*);
57
58#if PLATFORM(IOS_FAMILY)
59 void collectSelectionRects(Vector<SelectionRect>&, unsigned, unsigned) override;
60#endif
61
62 void setIsGeneratedContent(bool generated = true) { m_isGeneratedContent = generated; }
63
64 bool isGeneratedContent() const { return m_isGeneratedContent; }
65
66 const String& altText() const { return m_altText; }
67 void setAltText(const String& altText) { m_altText = altText; }
68
69 inline void setImageDevicePixelRatio(float factor) { m_imageDevicePixelRatio = factor; }
70 float imageDevicePixelRatio() const { return m_imageDevicePixelRatio; }
71
72 void setHasShadowControls(bool hasShadowControls) { m_hasShadowControls = hasShadowControls; }
73
74 bool isShowingMissingOrImageError() const;
75 bool isShowingAltText() const;
76
77 bool hasNonBitmapImage() const;
78
79 bool isEditableImage() const;
80
81protected:
82 void willBeDestroyed() override;
83
84 bool needsPreferredWidthsRecalculation() const final;
85 RenderBox* embeddedContentBox() const final;
86 void computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const final;
87 bool foregroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect, unsigned maxDepthToTest) const override;
88
89 void styleWillChange(StyleDifference, const RenderStyle& newStyle) override;
90 void styleDidChange(StyleDifference, const RenderStyle*) override;
91
92 void imageChanged(WrappedImagePtr, const IntRect* = nullptr) override;
93
94 ImageDrawResult paintIntoRect(PaintInfo&, const FloatRect&);
95 void paint(PaintInfo&, const LayoutPoint&) final;
96 void layout() override;
97
98 void intrinsicSizeChanged() override
99 {
100 imageChanged(imageResource().imagePtr());
101 }
102
103private:
104 const char* renderName() const override { return "RenderImage"; }
105
106 bool canHaveChildren() const override;
107
108 bool isImage() const override { return true; }
109 bool isRenderImage() const final { return true; }
110
111 bool requiresLayer() const override;
112
113 void paintReplaced(PaintInfo&, const LayoutPoint&) override;
114 void paintIncompleteImageOutline(PaintInfo&, LayoutPoint, LayoutUnit) const;
115
116 bool computeBackgroundIsKnownToBeObscured(const LayoutPoint& paintOffset) final;
117
118 LayoutUnit minimumReplacedHeight() const override;
119
120 void notifyFinished(CachedResource&) final;
121 bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) final;
122
123 bool boxShadowShouldBeAppliedToBackground(const LayoutPoint& paintOffset, BackgroundBleedAvoidance, InlineFlowBox*) const final;
124
125 virtual bool shadowControlsNeedCustomLayoutMetrics() const { return false; }
126
127 IntSize imageSizeForError(CachedImage*) const;
128 void repaintOrMarkForLayout(ImageSizeChangeType, const IntRect* = nullptr);
129 void updateIntrinsicSizeIfNeeded(const LayoutSize&);
130 // Update the size of the image to be rendered. Object-fit may cause this to be different from the CSS box's content rect.
131 void updateInnerContentRect();
132
133 void paintAreaElementFocusRing(PaintInfo&, const LayoutPoint& paintOffset);
134
135 void layoutShadowControls(const LayoutSize& oldSize);
136
137 // Text to display as long as the image isn't available.
138 String m_altText;
139 std::unique_ptr<RenderImageResource> m_imageResource;
140 bool m_needsToSetSizeForAltText { false };
141 bool m_didIncrementVisuallyNonEmptyPixelCount { false };
142 bool m_isGeneratedContent { false };
143 bool m_hasShadowControls { false };
144 float m_imageDevicePixelRatio { 1 };
145
146 friend class RenderImageScaleObserver;
147};
148
149} // namespace WebCore
150
151SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderImage, isRenderImage())
152