blob: 698894bb98c5cf855af94a78d4ebb18748eec03a [file] [log] [blame]
darin@apple.com640fa302008-02-15 05:03:55 +00001/*
darinb9481ed2006-03-20 02:57:59 +00002 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2000 Dirk Mueller (mueller@kde.org)
5 * (C) 2006 Allan Sandfeld Jensen (kde@carewolf.com)
6 * (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
darin@apple.com88b13e62011-02-01 22:49:01 +00007 * Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
senorblanco@chromium.org97cbe002010-06-17 19:50:36 +00008 * Copyright (C) 2010 Google Inc. All rights reserved.
zimmermann@webkit.org2454d6432012-03-27 09:24:46 +00009 * Copyright (C) Research In Motion Limited 2011-2012. All rights reserved.
darinb9481ed2006-03-20 02:57:59 +000010 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
20 *
21 * You should have received a copy of the GNU Library General Public License
22 * along with this library; see the file COPYING.LIB. If not, write to
ddkilzerc8eccec2007-09-26 02:29:57 +000023 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
darinb9481ed2006-03-20 02:57:59 +000025 *
26 */
27
28#include "config.h"
29#include "RenderImage.h"
30
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +000031#include "BitmapImage.h"
abarth@webkit.org401a3792013-03-03 10:12:59 +000032#include "CachedImage.h"
commit-queue@webkit.orgd0c076d2012-08-22 22:25:59 +000033#include "Font.h"
msaboff@apple.com5dbcc692011-06-07 20:46:28 +000034#include "FontCache.h"
cfleizach@apple.combc088902010-01-26 18:06:41 +000035#include "Frame.h"
rniwa@webkit.org78bbc942011-05-05 18:14:43 +000036#include "FrameSelection.h"
darinb9481ed2006-03-20 02:57:59 +000037#include "GraphicsContext.h"
cfleizach@apple.combc088902010-01-26 18:06:41 +000038#include "HTMLAreaElement.h"
eseidel84943622006-05-15 23:23:42 +000039#include "HTMLImageElement.h"
darinb9481ed2006-03-20 02:57:59 +000040#include "HTMLInputElement.h"
eseidel84943622006-05-15 23:23:42 +000041#include "HTMLMapElement.h"
darin98fa8b82006-03-20 08:03:57 +000042#include "HTMLNames.h"
bdakinc1a5a832006-10-28 18:28:00 +000043#include "HitTestResult.h"
hyattb21f8372007-09-04 22:58:27 +000044#include "Page.h"
jchaffraix@webkit.org363c3872012-04-17 21:51:22 +000045#include "PaintInfo.h"
hyattd8048342006-05-31 01:48:18 +000046#include "RenderView.h"
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +000047#include "SVGImage.h"
andersca@apple.coma5fb7da2013-04-16 19:57:36 +000048#include <wtf/StackStats.h>
darinb9481ed2006-03-20 02:57:59 +000049
darin7bd70952006-04-13 07:07:34 +000050using namespace std;
51
darinb9481ed2006-03-20 02:57:59 +000052namespace WebCore {
53
54using namespace HTMLNames;
55
antti@apple.com6310f612013-01-19 14:34:36 +000056RenderImage::RenderImage(Element* element)
57 : RenderReplaced(element, IntSize())
mitz@apple.comc3d51cd2010-11-15 22:27:06 +000058 , m_needsToSetSizeForAltText(false)
antti@apple.com17978ce2011-07-13 10:23:54 +000059 , m_didIncrementVisuallyNonEmptyPixelCount(false)
leo.yang@torchmobile.com.cnff6977552011-12-02 08:05:08 +000060 , m_isGeneratedContent(false)
darinb9481ed2006-03-20 02:57:59 +000061{
darinb9481ed2006-03-20 02:57:59 +000062 updateAltText();
63}
64
antti@apple.com6310f612013-01-19 14:34:36 +000065RenderImage* RenderImage::createAnonymous(Document* document)
66{
67 RenderImage* image = new (document->renderArena()) RenderImage(0);
68 image->setDocumentForAnonymous(document);
69 return image;
70}
71
darinb9481ed2006-03-20 02:57:59 +000072RenderImage::~RenderImage()
73{
zimmermann@webkit.org50d25552010-08-27 14:46:22 +000074 ASSERT(m_imageResource);
75 m_imageResource->shutdown();
darinb9481ed2006-03-20 02:57:59 +000076}
77
zimmermann@webkit.org50d25552010-08-27 14:46:22 +000078void RenderImage::setImageResource(PassOwnPtr<RenderImageResource> imageResource)
darinb9481ed2006-03-20 02:57:59 +000079{
zimmermann@webkit.org50d25552010-08-27 14:46:22 +000080 ASSERT(!m_imageResource);
81 m_imageResource = imageResource;
82 m_imageResource->initialize(this);
darinb9481ed2006-03-20 02:57:59 +000083}
84
bdash58feef62007-01-11 02:24:30 +000085// If we'll be displaying either alt text or an image, add some padding.
86static const unsigned short paddingWidth = 4;
87static const unsigned short paddingHeight = 4;
88
89// Alt text is restricted to this maximum size, in pixels. These are
90// signed integers because they are compared with other signed values.
hyatt@apple.com0acc9352011-02-17 19:19:07 +000091static const float maxAltTextWidth = 1024;
bdash58feef62007-01-11 02:24:30 +000092static const int maxAltTextHeight = 256;
93
mitz@apple.comc3d51cd2010-11-15 22:27:06 +000094IntSize RenderImage::imageSizeForError(CachedImage* newImage) const
95{
96 ASSERT_ARG(newImage, newImage);
zimmermann@webkit.org8cf217f2011-10-14 08:08:51 +000097 ASSERT_ARG(newImage, newImage->imageForRenderer(this));
mitz@apple.comc3d51cd2010-11-15 22:27:06 +000098
bdakin@apple.comb1f426c2011-09-13 22:17:09 +000099 IntSize imageSize;
100 if (newImage->willPaintBrokenImage()) {
bdakin@apple.com57d71e22011-09-22 03:29:50 +0000101 float deviceScaleFactor = WebCore::deviceScaleFactor(frame());
bdakin@apple.come6372a72011-09-14 18:35:49 +0000102 pair<Image*, float> brokenImageAndImageScaleFactor = newImage->brokenImage(deviceScaleFactor);
103 imageSize = brokenImageAndImageScaleFactor.first->size();
104 imageSize.scale(1 / brokenImageAndImageScaleFactor.second);
bdakin@apple.comb1f426c2011-09-13 22:17:09 +0000105 } else
zimmermann@webkit.org8cf217f2011-10-14 08:08:51 +0000106 imageSize = newImage->imageForRenderer(this)->size();
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000107
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000108 // imageSize() returns 0 for the error image. We need the true size of the
109 // error image, so we have to get it by grabbing image() directly.
bdakin@apple.comb1f426c2011-09-13 22:17:09 +0000110 return IntSize(paddingWidth + imageSize.width() * style()->effectiveZoom(), paddingHeight + imageSize.height() * style()->effectiveZoom());
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000111}
112
bdash58feef62007-01-11 02:24:30 +0000113// Sets the image height and width to fit the alt text. Returns true if the
114// image size changed.
115bool RenderImage::setImageSizeForAltText(CachedImage* newImage /* = 0 */)
116{
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000117 IntSize imageSize;
zimmermann@webkit.org8cf217f2011-10-14 08:08:51 +0000118 if (newImage && newImage->imageForRenderer(this))
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000119 imageSize = imageSizeForError(newImage);
120 else if (!m_altText.isEmpty() || newImage) {
121 // If we'll be displaying either text or an image, add a little padding.
122 imageSize = IntSize(paddingWidth, paddingHeight);
bdash58feef62007-01-11 02:24:30 +0000123 }
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000124
bdash58feef62007-01-11 02:24:30 +0000125 // we have an alt and the user meant it (its not a text we invented)
126 if (!m_altText.isEmpty()) {
msaboff@apple.com5dbcc692011-06-07 20:46:28 +0000127 FontCachePurgePreventer fontCachePurgePreventer;
128
bdash58feef62007-01-11 02:24:30 +0000129 const Font& font = style()->font();
ch.dumez@sisa.samsung.com60500472013-04-02 20:35:33 +0000130 IntSize paddedTextSize(paddingWidth + min(ceilf(font.width(RenderBlock::constructTextRun(this, font, m_altText, style()))), maxAltTextWidth), paddingHeight + min(font.fontMetrics().height(), maxAltTextHeight));
131 imageSize = imageSize.expandedTo(paddedTextSize);
bdash58feef62007-01-11 02:24:30 +0000132 }
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000133
darin751cf302007-05-01 22:10:41 +0000134 if (imageSize == intrinsicSize())
135 return false;
136
137 setIntrinsicSize(imageSize);
138 return true;
bdash58feef62007-01-11 02:24:30 +0000139}
140
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000141void RenderImage::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
142{
143 RenderReplaced::styleDidChange(diff, oldStyle);
144 if (m_needsToSetSizeForAltText) {
145 if (!m_altText.isEmpty() && setImageSizeForAltText(m_imageResource->cachedImage()))
146 imageDimensionsChanged(true /* imageSizeChanged */);
147 m_needsToSetSizeForAltText = false;
148 }
commit-queue@webkit.org2299fe02012-06-11 17:19:26 +0000149#if ENABLE(CSS_IMAGE_RESOLUTION)
commit-queue@webkit.org017dbda2012-06-18 23:30:29 +0000150 if (diff == StyleDifferenceLayout
151 && (oldStyle->imageResolution() != style()->imageResolution()
commit-queue@webkit.org27e15fe2012-06-25 01:46:14 +0000152 || oldStyle->imageResolutionSnap() != style()->imageResolutionSnap()
commit-queue@webkit.org017dbda2012-06-18 23:30:29 +0000153 || oldStyle->imageResolutionSource() != style()->imageResolutionSource()))
commit-queue@webkit.org2299fe02012-06-11 17:19:26 +0000154 imageDimensionsChanged(true /* imageSizeChanged */);
155#endif
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000156}
157
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000158void RenderImage::imageChanged(WrappedImagePtr newImage, const IntRect* rect)
darinb9481ed2006-03-20 02:57:59 +0000159{
jonlee@apple.com37fbb622012-05-17 00:09:07 +0000160 // FIXME (86669): Instead of the RenderImage determining whether its document is in the page
161 // cache, the RenderImage should remove itself as a client when its document is put into the
162 // page cache.
163 if (documentBeingDestroyed() || document()->inPageCache())
ap692e06e2006-10-01 09:11:35 +0000164 return;
165
dino@apple.com397229e2008-07-10 19:31:43 +0000166 if (hasBoxDecorations() || hasMask())
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000167 RenderReplaced::imageChanged(newImage, rect);
antti@apple.comcae24e02013-03-20 00:17:58 +0000168
aestes@apple.combbbea4d2010-10-08 21:57:29 +0000169 if (!m_imageResource)
170 return;
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000171
172 if (newImage != m_imageResource->imagePtr() || !newImage)
darinb9481ed2006-03-20 02:57:59 +0000173 return;
antti@apple.com17978ce2011-07-13 10:23:54 +0000174
175 if (!m_didIncrementVisuallyNonEmptyPixelCount) {
eae@chromium.org88c682c2012-11-01 15:00:26 +0000176 // At a zoom level of 1 the image is guaranteed to have an integer size.
177 view()->frameView()->incrementVisuallyNonEmptyPixelCount(flooredIntSize(m_imageResource->imageSize(1.0f)));
antti@apple.com17978ce2011-07-13 10:23:54 +0000178 m_didIncrementVisuallyNonEmptyPixelCount = true;
179 }
darinb9481ed2006-03-20 02:57:59 +0000180
weinigc24ab182006-10-30 22:41:29 +0000181 bool imageSizeChanged = false;
darinb9481ed2006-03-20 02:57:59 +0000182
bdash58feef62007-01-11 02:24:30 +0000183 // Set image dimensions, taking into account the size of the alt text.
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000184 if (m_imageResource->errorOccurred()) {
antti@apple.com52bdb212012-09-28 03:51:25 +0000185 if (!m_altText.isEmpty() && document()->hasPendingStyleRecalc()) {
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000186 ASSERT(node());
187 if (node()) {
188 m_needsToSetSizeForAltText = true;
189 node()->setNeedsStyleRecalc(SyntheticStyleChange);
190 }
191 return;
192 }
mitz@apple.com543c3002010-11-16 22:08:26 +0000193 imageSizeChanged = setImageSizeForAltText(m_imageResource->cachedImage());
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000194 }
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000195
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000196 imageDimensionsChanged(imageSizeChanged, rect);
197}
198
eae@chromium.org88c682c2012-11-01 15:00:26 +0000199bool RenderImage::updateIntrinsicSizeIfNeeded(const LayoutSize& newSize, bool imageSizeChanged)
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000200{
201 if (newSize == intrinsicSize() && !imageSizeChanged)
202 return false;
203 if (m_imageResource->errorOccurred())
204 return imageSizeChanged;
205 setIntrinsicSize(newSize);
206 return true;
207}
208
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000209void RenderImage::imageDimensionsChanged(bool imageSizeChanged, const IntRect* rect)
210{
commit-queue@webkit.org2299fe02012-06-11 17:19:26 +0000211#if ENABLE(CSS_IMAGE_RESOLUTION)
commit-queue@webkit.org27e15fe2012-06-25 01:46:14 +0000212 double scale = style()->imageResolution();
213 if (style()->imageResolutionSnap() == ImageResolutionSnapPixels)
214 scale = roundForImpreciseConversion<int>(scale);
215 if (scale <= 0)
216 scale = 1;
217 bool intrinsicSizeChanged = updateIntrinsicSizeIfNeeded(m_imageResource->imageSize(style()->effectiveZoom() / scale), imageSizeChanged);
commit-queue@webkit.org2299fe02012-06-11 17:19:26 +0000218#else
jchaffraix@webkit.org64e734e2012-05-10 22:08:27 +0000219 bool intrinsicSizeChanged = updateIntrinsicSizeIfNeeded(m_imageResource->imageSize(style()->effectiveZoom()), imageSizeChanged);
commit-queue@webkit.org2299fe02012-06-11 17:19:26 +0000220#endif
jchaffraix@webkit.org64e734e2012-05-10 22:08:27 +0000221
222 // In the case of generated image content using :before/:after/content, we might not be
223 // in the render tree yet. In that case, we just need to update our intrinsic size.
224 // layout() will be called after we are inserted in the tree which will take care of
225 // what we are doing here.
226 if (!containingBlock())
227 return;
228
bdasha1595472007-05-02 09:17:52 +0000229 bool shouldRepaint = true;
jchaffraix@webkit.org64e734e2012-05-10 22:08:27 +0000230 if (intrinsicSizeChanged) {
jchaffraix@webkit.org64e734e2012-05-10 22:08:27 +0000231 if (!preferredLogicalWidthsDirty())
232 setPreferredLogicalWidthsDirty(true);
tony@chromium.org513fe612013-01-04 01:21:07 +0000233
234 bool hasOverrideSize = hasOverrideHeight() || hasOverrideWidth();
235 if (!hasOverrideSize && !imageSizeChanged) {
236 LogicalExtentComputedValues computedValues;
237 computeLogicalWidthInRegion(computedValues);
238 LayoutUnit newWidth = computedValues.m_extent;
239 computeLogicalHeight(height(), 0, computedValues);
240 LayoutUnit newHeight = computedValues.m_extent;
241
242 imageSizeChanged = width() != newWidth || height() != newHeight;
243 }
bdasha1595472007-05-02 09:17:52 +0000244
commit-queue@webkit.org7555e8c2012-12-18 00:59:26 +0000245 // FIXME: We only need to recompute the containing block's preferred size
246 // if the containing block's size depends on the image's size (i.e., the container uses shrink-to-fit sizing).
247 // There's no easy way to detect that shrink-to-fit is needed, always force a layout.
248 bool containingBlockNeedsToRecomputePreferredSize =
tony@chromium.org513fe612013-01-04 01:21:07 +0000249 style()->logicalWidth().isPercent()
250 || style()->logicalMaxWidth().isPercent()
251 || style()->logicalMinWidth().isPercent();
commit-queue@webkit.org7555e8c2012-12-18 00:59:26 +0000252
tony@chromium.org513fe612013-01-04 01:21:07 +0000253 if (imageSizeChanged || hasOverrideSize || containingBlockNeedsToRecomputePreferredSize) {
jchaffraix@webkit.org64e734e2012-05-10 22:08:27 +0000254 shouldRepaint = false;
255 if (!selfNeedsLayout())
256 setNeedsLayout(true);
darinb9481ed2006-03-20 02:57:59 +0000257 }
258 }
259
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000260 if (shouldRepaint) {
leviw@chromium.org2c32cf22011-11-04 18:27:35 +0000261 LayoutRect repaintRect;
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000262 if (rect) {
263 // The image changed rect is in source image coordinates (pre-zooming),
264 // so map from the bounds of the image to the contentsBox.
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000265 repaintRect = enclosingIntRect(mapRect(*rect, FloatRect(FloatPoint(), m_imageResource->imageSize(1.0f)), contentBoxRect()));
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000266 // Guard against too-large changed rects.
hyatt@apple.comd885df72009-01-22 02:31:52 +0000267 repaintRect.intersect(contentBoxRect());
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000268 } else
hyatt@apple.comd885df72009-01-22 02:31:52 +0000269 repaintRect = contentBoxRect();
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000270
271 repaintRectangle(repaintRect);
dino@apple.comce3a4e02009-03-26 01:38:52 +0000272
273#if USE(ACCELERATED_COMPOSITING)
jchaffraix@webkit.org363c3872012-04-17 21:51:22 +0000274 // Tell any potential compositing layers that the image needs updating.
275 contentChanged(ImageChanged);
dino@apple.comce3a4e02009-03-26 01:38:52 +0000276#endif
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000277 }
darinb9481ed2006-03-20 02:57:59 +0000278}
279
dino@apple.comce3a4e02009-03-26 01:38:52 +0000280void RenderImage::notifyFinished(CachedResource* newImage)
281{
aestes@apple.combbbea4d2010-10-08 21:57:29 +0000282 if (!m_imageResource)
283 return;
284
dino@apple.comce3a4e02009-03-26 01:38:52 +0000285 if (documentBeingDestroyed())
286 return;
287
antti@apple.comcae24e02013-03-20 00:17:58 +0000288 invalidateBackgroundObscurationStatus();
289
dino@apple.comce3a4e02009-03-26 01:38:52 +0000290#if USE(ACCELERATED_COMPOSITING)
jchaffraix@webkit.org363c3872012-04-17 21:51:22 +0000291 if (newImage == m_imageResource->cachedImage()) {
dino@apple.comce3a4e02009-03-26 01:38:52 +0000292 // tell any potential compositing layers
293 // that the image is done and they can reference it directly.
jchaffraix@webkit.org363c3872012-04-17 21:51:22 +0000294 contentChanged(ImageChanged);
dino@apple.comce3a4e02009-03-26 01:38:52 +0000295 }
296#else
297 UNUSED_PARAM(newImage);
298#endif
299}
darinb9481ed2006-03-20 02:57:59 +0000300
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000301void RenderImage::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
darinb9481ed2006-03-20 02:57:59 +0000302{
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000303 LayoutUnit cWidth = contentWidth();
304 LayoutUnit cHeight = contentHeight();
305 LayoutUnit leftBorder = borderLeft();
306 LayoutUnit topBorder = borderTop();
307 LayoutUnit leftPad = paddingLeft();
308 LayoutUnit topPad = paddingTop();
darinb9481ed2006-03-20 02:57:59 +0000309
antti@apple.com7f6c5da2007-12-04 19:21:09 +0000310 GraphicsContext* context = paintInfo.context;
311
bdakin@apple.comb0b3eb32012-02-29 23:10:06 +0000312 Page* page = 0;
313 if (Frame* frame = this->frame())
314 page = frame->page();
315
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000316 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred()) {
weinigc24ab182006-10-30 22:41:29 +0000317 if (paintInfo.phase == PaintPhaseSelection)
darinb9481ed2006-03-20 02:57:59 +0000318 return;
319
bdakin@apple.comb0b3eb32012-02-29 23:10:06 +0000320 if (page && paintInfo.phase == PaintPhaseForeground)
321 page->addRelevantUnpaintedObject(this, visualOverflowRect());
322
darinb9481ed2006-03-20 02:57:59 +0000323 if (cWidth > 2 && cHeight > 2) {
ch.dumez@sisa.samsung.com60500472013-04-02 20:35:33 +0000324 const int borderWidth = 1;
325
bdash20fe5432007-10-14 11:59:35 +0000326 // Draw an outline rect where the image should be.
327 context->setStrokeStyle(SolidStroke);
bdakin@apple.com4a0593e2009-11-10 21:44:56 +0000328 context->setStrokeColor(Color::lightGray, style()->colorSpace());
329 context->setFillColor(Color::transparent, style()->colorSpace());
leviw@chromium.org9b486cd2012-02-11 00:58:22 +0000330 context->drawRect(pixelSnappedIntRect(LayoutRect(paintOffset.x() + leftBorder + leftPad, paintOffset.y() + topBorder + topPad, cWidth, cHeight)));
weinigc24ab182006-10-30 22:41:29 +0000331
darinb9481ed2006-03-20 02:57:59 +0000332 bool errorPictureDrawn = false;
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000333 LayoutSize imageOffset;
bdash20fe5432007-10-14 11:59:35 +0000334 // When calculating the usable dimensions, exclude the pixels of
335 // the ouline rect so the error image/alt text doesn't draw on it.
ch.dumez@sisa.samsung.com60500472013-04-02 20:35:33 +0000336 LayoutUnit usableWidth = cWidth - 2 * borderWidth;
337 LayoutUnit usableHeight = cHeight - 2 * borderWidth;
weinigc24ab182006-10-30 22:41:29 +0000338
simon.fraser@apple.com95247972011-01-25 05:52:29 +0000339 RefPtr<Image> image = m_imageResource->image();
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000340
341 if (m_imageResource->errorOccurred() && !image->isNull() && usableWidth >= image->width() && usableHeight >= image->height()) {
bdakin@apple.com57d71e22011-09-22 03:29:50 +0000342 float deviceScaleFactor = WebCore::deviceScaleFactor(frame());
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000343 // Call brokenImage() explicitly to ensure we get the broken image icon at the appropriate resolution.
bdakin@apple.come6372a72011-09-14 18:35:49 +0000344 pair<Image*, float> brokenImageAndImageScaleFactor = m_imageResource->cachedImage()->brokenImage(deviceScaleFactor);
345 image = brokenImageAndImageScaleFactor.first;
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000346 IntSize imageSize = image->size();
bdakin@apple.come6372a72011-09-14 18:35:49 +0000347 imageSize.scale(1 / brokenImageAndImageScaleFactor.second);
darinb9481ed2006-03-20 02:57:59 +0000348 // Center the error image, accounting for border and padding.
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000349 LayoutUnit centerX = (usableWidth - imageSize.width()) / 2;
darinb9481ed2006-03-20 02:57:59 +0000350 if (centerX < 0)
351 centerX = 0;
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000352 LayoutUnit centerY = (usableHeight - imageSize.height()) / 2;
darinb9481ed2006-03-20 02:57:59 +0000353 if (centerY < 0)
354 centerY = 0;
ch.dumez@sisa.samsung.com60500472013-04-02 20:35:33 +0000355 imageOffset = LayoutSize(leftBorder + leftPad + centerX + borderWidth, topBorder + topPad + centerY + borderWidth);
eae@chromium.org88c682c2012-11-01 15:00:26 +0000356 context->drawImage(image.get(), style()->colorSpace(), pixelSnappedIntRect(LayoutRect(paintOffset + imageOffset, imageSize)), CompositeSourceOver, shouldRespectImageOrientation());
darinb9481ed2006-03-20 02:57:59 +0000357 errorPictureDrawn = true;
358 }
weinigc24ab182006-10-30 22:41:29 +0000359
darinb9481ed2006-03-20 02:57:59 +0000360 if (!m_altText.isEmpty()) {
hyatt@apple.comcbe8fce2009-01-17 21:38:38 +0000361 String text = document()->displayStringModifiedByEncoding(m_altText);
hyatt@apple.com36bdcfa2010-05-21 20:56:24 +0000362 context->setFillColor(style()->visitedDependentColor(CSSPropertyColor), style()->colorSpace());
darinb9481ed2006-03-20 02:57:59 +0000363 const Font& font = style()->font();
zimmermann@webkit.orge8433cc2011-01-22 11:46:23 +0000364 const FontMetrics& fontMetrics = font.fontMetrics();
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000365 LayoutUnit ascent = fontMetrics.ascent();
366 LayoutPoint altTextOffset = paintOffset;
ch.dumez@sisa.samsung.com60500472013-04-02 20:35:33 +0000367 altTextOffset.move(leftBorder + leftPad + (paddingWidth / 2) - borderWidth, topBorder + topPad + ascent + (paddingHeight / 2) - borderWidth);
weinig0a635f02006-11-01 21:49:13 +0000368
darinb9481ed2006-03-20 02:57:59 +0000369 // Only draw the alt text if it'll fit within the content box,
370 // and only if it fits above the error image.
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +0000371 TextRun textRun = RenderBlock::constructTextRun(this, font, text, style());
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000372 LayoutUnit textWidth = font.width(textRun);
darinb9481ed2006-03-20 02:57:59 +0000373 if (errorPictureDrawn) {
leviw@chromium.org35e01312011-06-03 02:41:09 +0000374 if (usableWidth >= textWidth && fontMetrics.height() <= imageOffset.height())
375 context->drawText(font, textRun, altTextOffset);
ch.dumez@sisa.samsung.com60500472013-04-02 20:35:33 +0000376 } else if (usableWidth >= textWidth && usableHeight >= fontMetrics.height())
leviw@chromium.org35e01312011-06-03 02:41:09 +0000377 context->drawText(font, textRun, altTextOffset);
darinb9481ed2006-03-20 02:57:59 +0000378 }
379 }
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000380 } else if (m_imageResource->hasImage() && cWidth > 0 && cHeight > 0) {
simon.fraser@apple.com95247972011-01-25 05:52:29 +0000381 RefPtr<Image> img = m_imageResource->image(cWidth, cHeight);
bdakin@apple.comb0b3eb32012-02-29 23:10:06 +0000382 if (!img || img->isNull()) {
383 if (page && paintInfo.phase == PaintPhaseForeground)
384 page->addRelevantUnpaintedObject(this, visualOverflowRect());
hyatt@apple.com82b7dbb2008-04-14 19:28:53 +0000385 return;
bdakin@apple.comb0b3eb32012-02-29 23:10:06 +0000386 }
hyatt@apple.com82b7dbb2008-04-14 19:28:53 +0000387
harrisonddf79c12006-07-13 23:17:08 +0000388#if PLATFORM(MAC)
weinig0a635f02006-11-01 21:49:13 +0000389 if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
leviw@chromium.org35e01312011-06-03 02:41:09 +0000390 paintCustomHighlight(toPoint(paintOffset - location()), style()->highlight(), true);
harrisonddf79c12006-07-13 23:17:08 +0000391#endif
392
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000393 LayoutSize contentSize(cWidth, cHeight);
394 LayoutPoint contentLocation = paintOffset;
leviw@chromium.org35e01312011-06-03 02:41:09 +0000395 contentLocation.move(leftBorder + leftPad, topBorder + topPad);
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000396 paintIntoRect(context, LayoutRect(contentLocation, contentSize));
bdakin@apple.com83e3ffb2012-03-08 23:24:14 +0000397
398 if (cachedImage() && page && paintInfo.phase == PaintPhaseForeground) {
399 // For now, count images as unpainted if they are still progressively loading. We may want
400 // to refine this in the future to account for the portion of the image that has painted.
401 if (cachedImage()->isLoading())
402 page->addRelevantUnpaintedObject(this, LayoutRect(contentLocation, contentSize));
403 else
404 page->addRelevantRepaintedObject(this, LayoutRect(contentLocation, contentSize));
405 }
darinb9481ed2006-03-20 02:57:59 +0000406 }
407}
408
leviw@chromium.orgb3757502011-06-29 21:33:11 +0000409void RenderImage::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
cfleizach@apple.combc088902010-01-26 18:06:41 +0000410{
leviw@chromium.org61ca1372011-06-07 18:56:41 +0000411 RenderReplaced::paint(paintInfo, paintOffset);
cfleizach@apple.combc088902010-01-26 18:06:41 +0000412
413 if (paintInfo.phase == PaintPhaseOutline)
darin@apple.com88b13e62011-02-01 22:49:01 +0000414 paintAreaElementFocusRing(paintInfo);
cfleizach@apple.combc088902010-01-26 18:06:41 +0000415}
416
darin@apple.com88b13e62011-02-01 22:49:01 +0000417void RenderImage::paintAreaElementFocusRing(PaintInfo& paintInfo)
cfleizach@apple.combc088902010-01-26 18:06:41 +0000418{
darin@apple.com88b13e62011-02-01 22:49:01 +0000419 Document* document = this->document();
420
421 if (document->printing() || !document->frame()->selection()->isFocusedAndActive())
cfleizach@apple.combc088902010-01-26 18:06:41 +0000422 return;
423
424 if (paintInfo.context->paintingDisabled() && !paintInfo.context->updatingControlTints())
425 return;
426
akling@apple.com2d7fedc2013-05-28 12:46:31 +0000427 Element* focusedElement = document->focusedElement();
kangil.han@samsung.comac8e24872013-06-26 17:23:33 +0000428 if (!focusedElement || !isHTMLAreaElement(focusedElement))
cfleizach@apple.combc088902010-01-26 18:06:41 +0000429 return;
dbates@webkit.orgd0f03962010-11-30 21:57:16 +0000430
kangil.han@samsung.comac8e24872013-06-26 17:23:33 +0000431 HTMLAreaElement* areaElement = toHTMLAreaElement(focusedElement);
darin@apple.com88b13e62011-02-01 22:49:01 +0000432 if (areaElement->imageElement() != node())
433 return;
434
435 // Even if the theme handles focus ring drawing for entire elements, it won't do it for
436 // an area within an image, so we don't call RenderTheme::supportsFocusRing here.
437
thakis@chromium.org10223a72011-02-10 21:04:40 +0000438 Path path = areaElement->computePath(this);
darin@apple.com88b13e62011-02-01 22:49:01 +0000439 if (path.isEmpty())
440 return;
441
442 // FIXME: Do we need additional code to clip the path to the image's bounding box?
443
444 RenderStyle* areaElementStyle = areaElement->computedStyle();
aestes@apple.com004e16e2011-04-03 00:01:42 +0000445 unsigned short outlineWidth = areaElementStyle->outlineWidth();
446 if (!outlineWidth)
447 return;
448
449 paintInfo.context->drawFocusRing(path, outlineWidth,
450 areaElementStyle->outlineOffset(),
darin@apple.com88b13e62011-02-01 22:49:01 +0000451 areaElementStyle->visitedDependentColor(CSSPropertyOutlineColor));
cfleizach@apple.combc088902010-01-26 18:06:41 +0000452}
darin@apple.com88b13e62011-02-01 22:49:01 +0000453
454void RenderImage::areaElementFocusChanged(HTMLAreaElement* element)
455{
456 ASSERT_UNUSED(element, element->imageElement() == node());
457
458 // It would be more efficient to only repaint the focus ring rectangle
459 // for the passed-in area element. That would require adding functions
460 // to the area element class.
461 repaint();
462}
463
leviw@chromium.org2c32cf22011-11-04 18:27:35 +0000464void RenderImage::paintIntoRect(GraphicsContext* context, const LayoutRect& rect)
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000465{
leviw@chromium.org4b6c2312012-03-21 09:32:01 +0000466 IntRect alignedRect = pixelSnappedIntRect(rect);
467 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred() || alignedRect.width() <= 0 || alignedRect.height() <= 0)
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000468 return;
469
leviw@chromium.org4b6c2312012-03-21 09:32:01 +0000470 RefPtr<Image> img = m_imageResource->image(alignedRect.width(), alignedRect.height());
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000471 if (!img || img->isNull())
472 return;
473
kangil.han@samsung.com2489e832013-06-29 02:41:54 +0000474 HTMLImageElement* imageElt = (node() && isHTMLImageElement(node())) ? toHTMLImageElement(node()) : 0;
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000475 CompositeOperator compositeOperator = imageElt ? imageElt->compositeOperator() : CompositeSourceOver;
senorblanco@chromium.org59baba52011-01-28 00:58:02 +0000476 Image* image = m_imageResource->image().get();
leviw@chromium.org4b6c2312012-03-21 09:32:01 +0000477 bool useLowQualityScaling = shouldPaintAtLowQuality(context, image, image, alignedRect.size());
timothy_horton@apple.com04ef44a2012-04-06 20:02:59 +0000478 context->drawImage(m_imageResource->image(alignedRect.width(), alignedRect.height()).get(), style()->colorSpace(), alignedRect, compositeOperator, shouldRespectImageOrientation(), useLowQualityScaling);
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000479}
480
commit-queue@webkit.org77ddca62012-06-27 03:02:48 +0000481bool RenderImage::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance bleedAvoidance, InlineFlowBox*) const
482{
483 if (!RenderBoxModelObject::boxShadowShouldBeAppliedToBackground(bleedAvoidance))
484 return false;
485
antti@apple.comcae24e02013-03-20 00:17:58 +0000486 return !const_cast<RenderImage*>(this)->backgroundIsKnownToBeObscured();
commit-queue@webkit.org77ddca62012-06-27 03:02:48 +0000487}
488
antti@apple.com4f141bf2013-03-27 01:00:57 +0000489bool RenderImage::foregroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect, unsigned maxDepthToTest) const
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000490{
antti@apple.com4f141bf2013-03-27 01:00:57 +0000491 UNUSED_PARAM(maxDepthToTest);
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000492 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred())
493 return false;
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000494 if (m_imageResource->cachedImage() && !m_imageResource->cachedImage()->isLoaded())
495 return false;
antti@apple.com4f141bf2013-03-27 01:00:57 +0000496 if (!contentBoxRect().contains(localRect))
497 return false;
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000498 EFillBox backgroundClip = style()->backgroundClip();
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000499 // Background paints under borders.
500 if (backgroundClip == BorderFillBox && style()->hasBorder() && !borderObscuresBackground())
501 return false;
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000502 // Background shows in padding area.
503 if ((backgroundClip == BorderFillBox || backgroundClip == PaddingFillBox) && style()->hasPadding())
504 return false;
pdr@google.comebc67c62013-02-01 21:31:00 +0000505 // Check for image with alpha.
antti@apple.comcae24e02013-03-20 00:17:58 +0000506 return m_imageResource->cachedImage() && m_imageResource->cachedImage()->currentFrameKnownToBeOpaque(this);
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000507}
508
antti@apple.com4f141bf2013-03-27 01:00:57 +0000509bool RenderImage::computeBackgroundIsKnownToBeObscured()
510{
511 if (!hasBackground())
512 return false;
simon.fraser@apple.comaf838eb2013-06-16 17:24:40 +0000513
514 LayoutRect paintedExtent;
515 if (!getBackgroundPaintedExtent(paintedExtent))
516 return false;
517 return foregroundIsKnownToBeOpaqueInRect(paintedExtent, 0);
antti@apple.com4f141bf2013-03-27 01:00:57 +0000518}
519
eae@chromium.org88c682c2012-11-01 15:00:26 +0000520LayoutUnit RenderImage::minimumReplacedHeight() const
darinb9481ed2006-03-20 02:57:59 +0000521{
eae@chromium.org88c682c2012-11-01 15:00:26 +0000522 return m_imageResource->errorOccurred() ? intrinsicSize().height() : LayoutUnit();
darinb9481ed2006-03-20 02:57:59 +0000523}
524
cfleizach@apple.combc088902010-01-26 18:06:41 +0000525HTMLMapElement* RenderImage::imageMap() const
darinb9481ed2006-03-20 02:57:59 +0000526{
kangil.han@samsung.com2489e832013-06-29 02:41:54 +0000527 HTMLImageElement* i = node() && isHTMLImageElement(node()) ? toHTMLImageElement(node()) : 0;
rolandsteiner@chromium.org0142c6a2011-05-03 17:20:17 +0000528 return i ? i->treeScope()->getImageMap(i->fastGetAttribute(usemapAttr)) : 0;
darinb9481ed2006-03-20 02:57:59 +0000529}
530
allan.jensen@nokia.com9a9045b2012-08-28 09:41:54 +0000531bool RenderImage::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
darinb9481ed2006-03-20 02:57:59 +0000532{
allan.jensen@nokia.com4c7e3522012-09-03 14:41:42 +0000533 HitTestResult tempResult(result.hitTestLocation());
allan.jensen@nokia.com9a9045b2012-08-28 09:41:54 +0000534 bool inside = RenderReplaced::nodeAtPoint(request, tempResult, locationInContainer, accumulatedOffset, hitTestAction);
darinb9481ed2006-03-20 02:57:59 +0000535
tonikitoo@webkit.org81c027d2010-07-29 13:19:43 +0000536 if (tempResult.innerNode() && node()) {
mitz@apple.comffbc6a52009-06-25 05:01:47 +0000537 if (HTMLMapElement* map = imageMap()) {
leviw@chromium.org2c32cf22011-11-04 18:27:35 +0000538 LayoutRect contentBox = contentBoxRect();
eae@chromium.org020f5872011-08-18 03:38:05 +0000539 float scaleFactor = 1 / style()->effectiveZoom();
allan.jensen@nokia.com9a9045b2012-08-28 09:41:54 +0000540 LayoutPoint mapLocation = locationInContainer.point() - toLayoutSize(accumulatedOffset) - locationOffset() - toLayoutSize(contentBox.location());
eae@chromium.org020f5872011-08-18 03:38:05 +0000541 mapLocation.scale(scaleFactor, scaleFactor);
commit-queue@webkit.org227d0ab2012-06-20 15:41:57 +0000542
eae@chromium.org020f5872011-08-18 03:38:05 +0000543 if (map->mapMouseEvent(mapLocation, contentBox.size(), tempResult))
mitz@apple.comffbc6a52009-06-25 05:01:47 +0000544 tempResult.setInnerNonSharedNode(node());
darinb9481ed2006-03-20 02:57:59 +0000545 }
546 }
547
tonikitoo@webkit.org81c027d2010-07-29 13:19:43 +0000548 if (!inside && result.isRectBasedTest())
549 result.append(tempResult);
simon.fraser@apple.come9d4e0b2009-03-22 19:50:18 +0000550 if (inside)
551 result = tempResult;
darinb9481ed2006-03-20 02:57:59 +0000552 return inside;
553}
554
555void RenderImage::updateAltText()
556{
hyatt@apple.com7e032532009-02-11 22:06:32 +0000557 if (!node())
darinb9481ed2006-03-20 02:57:59 +0000558 return;
weinigc24ab182006-10-30 22:41:29 +0000559
kangil.han@samsung.comc5593722013-06-28 02:26:41 +0000560 if (isHTMLInputElement(node()))
561 m_altText = toHTMLInputElement(node())->altText();
kangil.han@samsung.com2489e832013-06-29 02:41:54 +0000562 else if (isHTMLImageElement(node()))
563 m_altText = toHTMLImageElement(node())->altText();
darinb9481ed2006-03-20 02:57:59 +0000564}
565
zimmermann@webkit.org2454d6432012-03-27 09:24:46 +0000566void RenderImage::layout()
darinb9481ed2006-03-20 02:57:59 +0000567{
mark.lam@apple.com6df1a802012-10-19 20:09:36 +0000568 StackStats::LayoutCheckPoint layoutCheckPoint;
zimmermann@webkit.org2454d6432012-03-27 09:24:46 +0000569 RenderReplaced::layout();
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000570
zimmermann@webkit.org2454d6432012-03-27 09:24:46 +0000571 // Propagate container size to image resource.
572 IntSize containerSize(contentWidth(), contentHeight());
573 if (!containerSize.isEmpty())
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000574 m_imageResource->setContainerSizeForRenderer(containerSize);
darinb9481ed2006-03-20 02:57:59 +0000575}
576
zimmermann@webkit.org1adc5592012-01-20 15:30:17 +0000577void RenderImage::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const
darinb9481ed2006-03-20 02:57:59 +0000578{
zimmermann@webkit.org2454d6432012-03-27 09:24:46 +0000579 RenderReplaced::computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio, isPercentageIntrinsicSize);
580
581 // Our intrinsicSize is empty if we're rendering generated images with relative width/height. Figure out the right intrinsic size to use.
582 if (intrinsicSize.isEmpty() && (m_imageResource->imageHasRelativeWidth() || m_imageResource->imageHasRelativeHeight())) {
simon.fraser@apple.com2f071852012-06-25 00:11:30 +0000583 RenderObject* containingBlock = isOutOfFlowPositioned() ? container() : this->containingBlock();
zimmermann@webkit.org2454d6432012-03-27 09:24:46 +0000584 if (containingBlock->isBox()) {
585 RenderBox* box = toRenderBox(containingBlock);
586 intrinsicSize.setWidth(box->availableLogicalWidth());
ojan@chromium.org21943262013-01-04 20:45:36 +0000587 intrinsicSize.setHeight(box->availableLogicalHeight(IncludeMarginBorderPadding));
zimmermann@webkit.org2454d6432012-03-27 09:24:46 +0000588 }
589 }
590 // Don't compute an intrinsic ratio to preserve historical WebKit behavior if we're painting alt text and/or a broken image.
591 if (m_imageResource && m_imageResource->errorOccurred()) {
592 intrinsicRatio = 1;
zimmermann@webkit.org1adc5592012-01-20 15:30:17 +0000593 return;
zimmermann@webkit.org2454d6432012-03-27 09:24:46 +0000594 }
darinb9481ed2006-03-20 02:57:59 +0000595}
596
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000597bool RenderImage::needsPreferredWidthsRecalculation() const
darinb9481ed2006-03-20 02:57:59 +0000598{
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000599 if (RenderReplaced::needsPreferredWidthsRecalculation())
600 return true;
601 return embeddedContentBox();
602}
603
604RenderBox* RenderImage::embeddedContentBox() const
605{
606 if (!m_imageResource)
darinb9481ed2006-03-20 02:57:59 +0000607 return 0;
darinb9481ed2006-03-20 02:57:59 +0000608
johnnyg@google.comf4ebad62011-10-31 23:00:42 +0000609#if ENABLE(SVG)
zimmermann@webkit.org89d03f82011-11-08 09:32:52 +0000610 CachedImage* cachedImage = m_imageResource->cachedImage();
611 if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage())
612 return static_cast<SVGImage*>(cachedImage->image())->embeddedContentBox();
johnnyg@google.comf4ebad62011-10-31 23:00:42 +0000613#endif
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000614
615 return 0;
darinb9481ed2006-03-20 02:57:59 +0000616}
617
weinigc24ab182006-10-30 22:41:29 +0000618} // namespace WebCore