blob: 047012ce24caf518183846086bed5e62f210bf1e [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.
darinb9481ed2006-03-20 02:57:59 +00009 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public License
21 * along with this library; see the file COPYING.LIB. If not, write to
ddkilzerc8eccec2007-09-26 02:29:57 +000022 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
darinb9481ed2006-03-20 02:57:59 +000024 *
25 */
26
27#include "config.h"
28#include "RenderImage.h"
29
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +000030#include "BitmapImage.h"
msaboff@apple.com5dbcc692011-06-07 20:46:28 +000031#include "FontCache.h"
cfleizach@apple.combc088902010-01-26 18:06:41 +000032#include "Frame.h"
rniwa@webkit.org78bbc942011-05-05 18:14:43 +000033#include "FrameSelection.h"
darinb9481ed2006-03-20 02:57:59 +000034#include "GraphicsContext.h"
cfleizach@apple.combc088902010-01-26 18:06:41 +000035#include "HTMLAreaElement.h"
eseidel84943622006-05-15 23:23:42 +000036#include "HTMLImageElement.h"
darinb9481ed2006-03-20 02:57:59 +000037#include "HTMLInputElement.h"
eseidel84943622006-05-15 23:23:42 +000038#include "HTMLMapElement.h"
darin98fa8b82006-03-20 08:03:57 +000039#include "HTMLNames.h"
bdakinc1a5a832006-10-28 18:28:00 +000040#include "HitTestResult.h"
hyattb21f8372007-09-04 22:58:27 +000041#include "Page.h"
eric@webkit.orgb35848a2010-06-10 08:33:22 +000042#include "RenderLayer.h"
hyattd8048342006-05-31 01:48:18 +000043#include "RenderView.h"
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +000044#include "SVGImage.h"
dino@apple.comce3a4e02009-03-26 01:38:52 +000045#include <wtf/UnusedParam.h>
darinb9481ed2006-03-20 02:57:59 +000046
darin7bd70952006-04-13 07:07:34 +000047using namespace std;
48
darinb9481ed2006-03-20 02:57:59 +000049namespace WebCore {
50
51using namespace HTMLNames;
52
weinigc24ab182006-10-30 22:41:29 +000053RenderImage::RenderImage(Node* node)
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +000054 : RenderReplaced(node, IntSize())
mitz@apple.comc3d51cd2010-11-15 22:27:06 +000055 , m_needsToSetSizeForAltText(false)
antti@apple.com17978ce2011-07-13 10:23:54 +000056 , m_didIncrementVisuallyNonEmptyPixelCount(false)
leo.yang@torchmobile.com.cnff6977552011-12-02 08:05:08 +000057 , m_isGeneratedContent(false)
darinb9481ed2006-03-20 02:57:59 +000058{
darinb9481ed2006-03-20 02:57:59 +000059 updateAltText();
60}
61
62RenderImage::~RenderImage()
63{
zimmermann@webkit.org50d25552010-08-27 14:46:22 +000064 ASSERT(m_imageResource);
65 m_imageResource->shutdown();
darinb9481ed2006-03-20 02:57:59 +000066}
67
zimmermann@webkit.org50d25552010-08-27 14:46:22 +000068void RenderImage::setImageResource(PassOwnPtr<RenderImageResource> imageResource)
darinb9481ed2006-03-20 02:57:59 +000069{
zimmermann@webkit.org50d25552010-08-27 14:46:22 +000070 ASSERT(!m_imageResource);
71 m_imageResource = imageResource;
72 m_imageResource->initialize(this);
darinb9481ed2006-03-20 02:57:59 +000073}
74
bdash58feef62007-01-11 02:24:30 +000075// If we'll be displaying either alt text or an image, add some padding.
76static const unsigned short paddingWidth = 4;
77static const unsigned short paddingHeight = 4;
78
79// Alt text is restricted to this maximum size, in pixels. These are
80// signed integers because they are compared with other signed values.
hyatt@apple.com0acc9352011-02-17 19:19:07 +000081static const float maxAltTextWidth = 1024;
bdash58feef62007-01-11 02:24:30 +000082static const int maxAltTextHeight = 256;
83
mitz@apple.comc3d51cd2010-11-15 22:27:06 +000084IntSize RenderImage::imageSizeForError(CachedImage* newImage) const
85{
86 ASSERT_ARG(newImage, newImage);
zimmermann@webkit.org8cf217f2011-10-14 08:08:51 +000087 ASSERT_ARG(newImage, newImage->imageForRenderer(this));
mitz@apple.comc3d51cd2010-11-15 22:27:06 +000088
bdakin@apple.comb1f426c2011-09-13 22:17:09 +000089 IntSize imageSize;
90 if (newImage->willPaintBrokenImage()) {
bdakin@apple.com57d71e22011-09-22 03:29:50 +000091 float deviceScaleFactor = WebCore::deviceScaleFactor(frame());
bdakin@apple.come6372a72011-09-14 18:35:49 +000092 pair<Image*, float> brokenImageAndImageScaleFactor = newImage->brokenImage(deviceScaleFactor);
93 imageSize = brokenImageAndImageScaleFactor.first->size();
94 imageSize.scale(1 / brokenImageAndImageScaleFactor.second);
bdakin@apple.comb1f426c2011-09-13 22:17:09 +000095 } else
zimmermann@webkit.org8cf217f2011-10-14 08:08:51 +000096 imageSize = newImage->imageForRenderer(this)->size();
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +000097
mitz@apple.comc3d51cd2010-11-15 22:27:06 +000098 // imageSize() returns 0 for the error image. We need the true size of the
99 // error image, so we have to get it by grabbing image() directly.
bdakin@apple.comb1f426c2011-09-13 22:17:09 +0000100 return IntSize(paddingWidth + imageSize.width() * style()->effectiveZoom(), paddingHeight + imageSize.height() * style()->effectiveZoom());
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000101}
102
bdash58feef62007-01-11 02:24:30 +0000103// Sets the image height and width to fit the alt text. Returns true if the
104// image size changed.
105bool RenderImage::setImageSizeForAltText(CachedImage* newImage /* = 0 */)
106{
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000107 IntSize imageSize;
zimmermann@webkit.org8cf217f2011-10-14 08:08:51 +0000108 if (newImage && newImage->imageForRenderer(this))
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000109 imageSize = imageSizeForError(newImage);
110 else if (!m_altText.isEmpty() || newImage) {
111 // If we'll be displaying either text or an image, add a little padding.
112 imageSize = IntSize(paddingWidth, paddingHeight);
bdash58feef62007-01-11 02:24:30 +0000113 }
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000114
bdash58feef62007-01-11 02:24:30 +0000115 // we have an alt and the user meant it (its not a text we invented)
116 if (!m_altText.isEmpty()) {
msaboff@apple.com5dbcc692011-06-07 20:46:28 +0000117 FontCachePurgePreventer fontCachePurgePreventer;
118
bdash58feef62007-01-11 02:24:30 +0000119 const Font& font = style()->font();
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +0000120 IntSize textSize(min(font.width(RenderBlock::constructTextRun(this, font, m_altText, style())), maxAltTextWidth), min(font.fontMetrics().height(), maxAltTextHeight));
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000121 imageSize = imageSize.expandedTo(textSize);
bdash58feef62007-01-11 02:24:30 +0000122 }
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000123
darin751cf302007-05-01 22:10:41 +0000124 if (imageSize == intrinsicSize())
125 return false;
126
127 setIntrinsicSize(imageSize);
128 return true;
bdash58feef62007-01-11 02:24:30 +0000129}
130
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000131void RenderImage::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
132{
133 RenderReplaced::styleDidChange(diff, oldStyle);
134 if (m_needsToSetSizeForAltText) {
135 if (!m_altText.isEmpty() && setImageSizeForAltText(m_imageResource->cachedImage()))
136 imageDimensionsChanged(true /* imageSizeChanged */);
137 m_needsToSetSizeForAltText = false;
138 }
139}
140
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000141void RenderImage::imageChanged(WrappedImagePtr newImage, const IntRect* rect)
darinb9481ed2006-03-20 02:57:59 +0000142{
ap692e06e2006-10-01 09:11:35 +0000143 if (documentBeingDestroyed())
144 return;
145
dino@apple.com397229e2008-07-10 19:31:43 +0000146 if (hasBoxDecorations() || hasMask())
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000147 RenderReplaced::imageChanged(newImage, rect);
aestes@apple.combbbea4d2010-10-08 21:57:29 +0000148
149 if (!m_imageResource)
150 return;
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000151
152 if (newImage != m_imageResource->imagePtr() || !newImage)
darinb9481ed2006-03-20 02:57:59 +0000153 return;
antti@apple.com17978ce2011-07-13 10:23:54 +0000154
155 if (!m_didIncrementVisuallyNonEmptyPixelCount) {
156 view()->frameView()->incrementVisuallyNonEmptyPixelCount(m_imageResource->imageSize(1.0f));
157 m_didIncrementVisuallyNonEmptyPixelCount = true;
158 }
darinb9481ed2006-03-20 02:57:59 +0000159
weinigc24ab182006-10-30 22:41:29 +0000160 bool imageSizeChanged = false;
darinb9481ed2006-03-20 02:57:59 +0000161
bdash58feef62007-01-11 02:24:30 +0000162 // Set image dimensions, taking into account the size of the alt text.
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000163 if (m_imageResource->errorOccurred()) {
mitz@apple.com543c3002010-11-16 22:08:26 +0000164 if (!m_altText.isEmpty() && document()->isPendingStyleRecalc()) {
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000165 ASSERT(node());
166 if (node()) {
167 m_needsToSetSizeForAltText = true;
168 node()->setNeedsStyleRecalc(SyntheticStyleChange);
169 }
170 return;
171 }
mitz@apple.com543c3002010-11-16 22:08:26 +0000172 imageSizeChanged = setImageSizeForAltText(m_imageResource->cachedImage());
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000173 }
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000174
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000175 imageDimensionsChanged(imageSizeChanged, rect);
176}
177
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000178bool RenderImage::updateIntrinsicSizeIfNeeded(const LayoutSize& newSize, bool imageSizeChanged)
179{
180 if (newSize == intrinsicSize() && !imageSizeChanged)
181 return false;
182 if (m_imageResource->errorOccurred())
183 return imageSizeChanged;
184 setIntrinsicSize(newSize);
185 return true;
186}
187
mitz@apple.comc3d51cd2010-11-15 22:27:06 +0000188void RenderImage::imageDimensionsChanged(bool imageSizeChanged, const IntRect* rect)
189{
bdasha1595472007-05-02 09:17:52 +0000190 bool shouldRepaint = true;
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000191 if (updateIntrinsicSizeIfNeeded(m_imageResource->imageSize(style()->effectiveZoom()), imageSizeChanged)) {
darinb9481ed2006-03-20 02:57:59 +0000192 // In the case of generated image content using :before/:after, we might not be in the
193 // render tree yet. In that case, we don't need to worry about check for layout, since we'll get a
194 // layout when we get added in to the render tree hierarchy later.
195 if (containingBlock()) {
196 // lets see if we need to relayout at all..
hyatt@apple.comd885df72009-01-22 02:31:52 +0000197 int oldwidth = width();
198 int oldheight = height();
hyatt@apple.com75dad742010-09-24 18:07:44 +0000199 if (!preferredLogicalWidthsDirty())
200 setPreferredLogicalWidthsDirty(true);
hyatt@apple.com5f4fae02010-09-24 06:45:16 +0000201 computeLogicalWidth();
202 computeLogicalHeight();
bdasha1595472007-05-02 09:17:52 +0000203
hyatt@apple.comd885df72009-01-22 02:31:52 +0000204 if (imageSizeChanged || width() != oldwidth || height() != oldheight) {
bdasha1595472007-05-02 09:17:52 +0000205 shouldRepaint = false;
206 if (!selfNeedsLayout())
207 setNeedsLayout(true);
208 }
weinigc24ab182006-10-30 22:41:29 +0000209
hyatt@apple.comd885df72009-01-22 02:31:52 +0000210 setWidth(oldwidth);
211 setHeight(oldheight);
darinb9481ed2006-03-20 02:57:59 +0000212 }
213 }
214
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000215 if (shouldRepaint) {
leviw@chromium.org2c32cf22011-11-04 18:27:35 +0000216 LayoutRect repaintRect;
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000217 if (rect) {
218 // The image changed rect is in source image coordinates (pre-zooming),
219 // so map from the bounds of the image to the contentsBox.
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000220 repaintRect = enclosingIntRect(mapRect(*rect, FloatRect(FloatPoint(), m_imageResource->imageSize(1.0f)), contentBoxRect()));
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000221 // Guard against too-large changed rects.
hyatt@apple.comd885df72009-01-22 02:31:52 +0000222 repaintRect.intersect(contentBoxRect());
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000223 } else
hyatt@apple.comd885df72009-01-22 02:31:52 +0000224 repaintRect = contentBoxRect();
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000225
226 repaintRectangle(repaintRect);
dino@apple.comce3a4e02009-03-26 01:38:52 +0000227
228#if USE(ACCELERATED_COMPOSITING)
229 if (hasLayer()) {
230 // Tell any potential compositing layers that the image needs updating.
simon.fraser@apple.com2f5f5c82010-12-15 00:09:58 +0000231 layer()->contentChanged(RenderLayer::ImageChanged);
dino@apple.comce3a4e02009-03-26 01:38:52 +0000232 }
233#endif
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +0000234 }
darinb9481ed2006-03-20 02:57:59 +0000235}
236
dino@apple.comce3a4e02009-03-26 01:38:52 +0000237void RenderImage::notifyFinished(CachedResource* newImage)
238{
aestes@apple.combbbea4d2010-10-08 21:57:29 +0000239 if (!m_imageResource)
240 return;
241
dino@apple.comce3a4e02009-03-26 01:38:52 +0000242 if (documentBeingDestroyed())
243 return;
244
245#if USE(ACCELERATED_COMPOSITING)
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000246 if (newImage == m_imageResource->cachedImage() && hasLayer()) {
dino@apple.comce3a4e02009-03-26 01:38:52 +0000247 // tell any potential compositing layers
248 // that the image is done and they can reference it directly.
simon.fraser@apple.com2f5f5c82010-12-15 00:09:58 +0000249 layer()->contentChanged(RenderLayer::ImageChanged);
dino@apple.comce3a4e02009-03-26 01:38:52 +0000250 }
251#else
252 UNUSED_PARAM(newImage);
253#endif
254}
darinb9481ed2006-03-20 02:57:59 +0000255
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000256void RenderImage::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
darinb9481ed2006-03-20 02:57:59 +0000257{
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000258 LayoutUnit cWidth = contentWidth();
259 LayoutUnit cHeight = contentHeight();
260 LayoutUnit leftBorder = borderLeft();
261 LayoutUnit topBorder = borderTop();
262 LayoutUnit leftPad = paddingLeft();
263 LayoutUnit topPad = paddingTop();
darinb9481ed2006-03-20 02:57:59 +0000264
antti@apple.com7f6c5da2007-12-04 19:21:09 +0000265 GraphicsContext* context = paintInfo.context;
266
bdakin@apple.comb0b3eb32012-02-29 23:10:06 +0000267 Page* page = 0;
268 if (Frame* frame = this->frame())
269 page = frame->page();
270
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000271 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred()) {
weinigc24ab182006-10-30 22:41:29 +0000272 if (paintInfo.phase == PaintPhaseSelection)
darinb9481ed2006-03-20 02:57:59 +0000273 return;
274
bdakin@apple.comb0b3eb32012-02-29 23:10:06 +0000275 if (page && paintInfo.phase == PaintPhaseForeground)
276 page->addRelevantUnpaintedObject(this, visualOverflowRect());
277
darinb9481ed2006-03-20 02:57:59 +0000278 if (cWidth > 2 && cHeight > 2) {
bdash20fe5432007-10-14 11:59:35 +0000279 // Draw an outline rect where the image should be.
280 context->setStrokeStyle(SolidStroke);
bdakin@apple.com4a0593e2009-11-10 21:44:56 +0000281 context->setStrokeColor(Color::lightGray, style()->colorSpace());
282 context->setFillColor(Color::transparent, style()->colorSpace());
leviw@chromium.org9b486cd2012-02-11 00:58:22 +0000283 context->drawRect(pixelSnappedIntRect(LayoutRect(paintOffset.x() + leftBorder + leftPad, paintOffset.y() + topBorder + topPad, cWidth, cHeight)));
weinigc24ab182006-10-30 22:41:29 +0000284
darinb9481ed2006-03-20 02:57:59 +0000285 bool errorPictureDrawn = false;
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000286 LayoutSize imageOffset;
bdash20fe5432007-10-14 11:59:35 +0000287 // When calculating the usable dimensions, exclude the pixels of
288 // the ouline rect so the error image/alt text doesn't draw on it.
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000289 LayoutUnit usableWidth = cWidth - 2;
290 LayoutUnit usableHeight = cHeight - 2;
weinigc24ab182006-10-30 22:41:29 +0000291
simon.fraser@apple.com95247972011-01-25 05:52:29 +0000292 RefPtr<Image> image = m_imageResource->image();
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000293
294 if (m_imageResource->errorOccurred() && !image->isNull() && usableWidth >= image->width() && usableHeight >= image->height()) {
bdakin@apple.com57d71e22011-09-22 03:29:50 +0000295 float deviceScaleFactor = WebCore::deviceScaleFactor(frame());
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000296 // Call brokenImage() explicitly to ensure we get the broken image icon at the appropriate resolution.
bdakin@apple.come6372a72011-09-14 18:35:49 +0000297 pair<Image*, float> brokenImageAndImageScaleFactor = m_imageResource->cachedImage()->brokenImage(deviceScaleFactor);
298 image = brokenImageAndImageScaleFactor.first;
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000299 IntSize imageSize = image->size();
bdakin@apple.come6372a72011-09-14 18:35:49 +0000300 imageSize.scale(1 / brokenImageAndImageScaleFactor.second);
darinb9481ed2006-03-20 02:57:59 +0000301 // Center the error image, accounting for border and padding.
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000302 LayoutUnit centerX = (usableWidth - imageSize.width()) / 2;
darinb9481ed2006-03-20 02:57:59 +0000303 if (centerX < 0)
304 centerX = 0;
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000305 LayoutUnit centerY = (usableHeight - imageSize.height()) / 2;
darinb9481ed2006-03-20 02:57:59 +0000306 if (centerY < 0)
307 centerY = 0;
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000308 imageOffset = LayoutSize(leftBorder + leftPad + centerX + 1, topBorder + topPad + centerY + 1);
leviw@chromium.org770847e2012-02-09 23:09:38 +0000309 context->drawImage(image.get(), style()->colorSpace(), IntRect(roundedIntPoint(paintOffset + imageOffset), imageSize));
darinb9481ed2006-03-20 02:57:59 +0000310 errorPictureDrawn = true;
311 }
weinigc24ab182006-10-30 22:41:29 +0000312
darinb9481ed2006-03-20 02:57:59 +0000313 if (!m_altText.isEmpty()) {
hyatt@apple.comcbe8fce2009-01-17 21:38:38 +0000314 String text = document()->displayStringModifiedByEncoding(m_altText);
hyatt@apple.com36bdcfa2010-05-21 20:56:24 +0000315 context->setFillColor(style()->visitedDependentColor(CSSPropertyColor), style()->colorSpace());
darinb9481ed2006-03-20 02:57:59 +0000316 const Font& font = style()->font();
zimmermann@webkit.orge8433cc2011-01-22 11:46:23 +0000317 const FontMetrics& fontMetrics = font.fontMetrics();
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000318 LayoutUnit ascent = fontMetrics.ascent();
319 LayoutPoint altTextOffset = paintOffset;
leviw@chromium.org35e01312011-06-03 02:41:09 +0000320 altTextOffset.move(leftBorder + leftPad, topBorder + topPad + ascent);
weinig0a635f02006-11-01 21:49:13 +0000321
darinb9481ed2006-03-20 02:57:59 +0000322 // Only draw the alt text if it'll fit within the content box,
323 // and only if it fits above the error image.
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +0000324 TextRun textRun = RenderBlock::constructTextRun(this, font, text, style());
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000325 LayoutUnit textWidth = font.width(textRun);
darinb9481ed2006-03-20 02:57:59 +0000326 if (errorPictureDrawn) {
leviw@chromium.org35e01312011-06-03 02:41:09 +0000327 if (usableWidth >= textWidth && fontMetrics.height() <= imageOffset.height())
328 context->drawText(font, textRun, altTextOffset);
zimmermann@webkit.orge8433cc2011-01-22 11:46:23 +0000329 } else if (usableWidth >= textWidth && cHeight >= fontMetrics.height())
leviw@chromium.org35e01312011-06-03 02:41:09 +0000330 context->drawText(font, textRun, altTextOffset);
darinb9481ed2006-03-20 02:57:59 +0000331 }
332 }
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000333 } else if (m_imageResource->hasImage() && cWidth > 0 && cHeight > 0) {
simon.fraser@apple.com95247972011-01-25 05:52:29 +0000334 RefPtr<Image> img = m_imageResource->image(cWidth, cHeight);
bdakin@apple.comb0b3eb32012-02-29 23:10:06 +0000335 if (!img || img->isNull()) {
336 if (page && paintInfo.phase == PaintPhaseForeground)
337 page->addRelevantUnpaintedObject(this, visualOverflowRect());
hyatt@apple.com82b7dbb2008-04-14 19:28:53 +0000338 return;
bdakin@apple.comb0b3eb32012-02-29 23:10:06 +0000339 }
hyatt@apple.com82b7dbb2008-04-14 19:28:53 +0000340
bdakin@apple.com740c3552012-02-29 23:59:59 +0000341 if (cachedImage() && page && paintInfo.phase == PaintPhaseForeground) {
bdakin@apple.comb0b3eb32012-02-29 23:10:06 +0000342 // For now, count images as unpainted if they are still progressively loading. We may want
343 // to refine this in the future to account for the portion of the image that has painted.
344 if (cachedImage()->isLoading())
345 page->addRelevantUnpaintedObject(this, visualOverflowRect());
346 else
347 page->addRelevantRepaintedObject(this, visualOverflowRect());
bdakin@apple.com3ab37372012-02-01 21:52:41 +0000348 }
349
harrisonddf79c12006-07-13 23:17:08 +0000350#if PLATFORM(MAC)
weinig0a635f02006-11-01 21:49:13 +0000351 if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
leviw@chromium.org35e01312011-06-03 02:41:09 +0000352 paintCustomHighlight(toPoint(paintOffset - location()), style()->highlight(), true);
harrisonddf79c12006-07-13 23:17:08 +0000353#endif
354
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000355 LayoutSize contentSize(cWidth, cHeight);
356 LayoutPoint contentLocation = paintOffset;
leviw@chromium.org35e01312011-06-03 02:41:09 +0000357 contentLocation.move(leftBorder + leftPad, topBorder + topPad);
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000358 paintIntoRect(context, LayoutRect(contentLocation, contentSize));
darinb9481ed2006-03-20 02:57:59 +0000359 }
360}
361
leviw@chromium.orgb3757502011-06-29 21:33:11 +0000362void RenderImage::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
cfleizach@apple.combc088902010-01-26 18:06:41 +0000363{
leviw@chromium.org61ca1372011-06-07 18:56:41 +0000364 RenderReplaced::paint(paintInfo, paintOffset);
cfleizach@apple.combc088902010-01-26 18:06:41 +0000365
366 if (paintInfo.phase == PaintPhaseOutline)
darin@apple.com88b13e62011-02-01 22:49:01 +0000367 paintAreaElementFocusRing(paintInfo);
cfleizach@apple.combc088902010-01-26 18:06:41 +0000368}
369
darin@apple.com88b13e62011-02-01 22:49:01 +0000370void RenderImage::paintAreaElementFocusRing(PaintInfo& paintInfo)
cfleizach@apple.combc088902010-01-26 18:06:41 +0000371{
darin@apple.com88b13e62011-02-01 22:49:01 +0000372 Document* document = this->document();
373
374 if (document->printing() || !document->frame()->selection()->isFocusedAndActive())
cfleizach@apple.combc088902010-01-26 18:06:41 +0000375 return;
376
377 if (paintInfo.context->paintingDisabled() && !paintInfo.context->updatingControlTints())
378 return;
379
cfleizach@apple.combc088902010-01-26 18:06:41 +0000380 Node* focusedNode = document->focusedNode();
darin@apple.com88b13e62011-02-01 22:49:01 +0000381 if (!focusedNode || !focusedNode->hasTagName(areaTag))
cfleizach@apple.combc088902010-01-26 18:06:41 +0000382 return;
dbates@webkit.orgd0f03962010-11-30 21:57:16 +0000383
darin@apple.com88b13e62011-02-01 22:49:01 +0000384 HTMLAreaElement* areaElement = static_cast<HTMLAreaElement*>(focusedNode);
385 if (areaElement->imageElement() != node())
386 return;
387
388 // Even if the theme handles focus ring drawing for entire elements, it won't do it for
389 // an area within an image, so we don't call RenderTheme::supportsFocusRing here.
390
thakis@chromium.org10223a72011-02-10 21:04:40 +0000391 Path path = areaElement->computePath(this);
darin@apple.com88b13e62011-02-01 22:49:01 +0000392 if (path.isEmpty())
393 return;
394
395 // FIXME: Do we need additional code to clip the path to the image's bounding box?
396
397 RenderStyle* areaElementStyle = areaElement->computedStyle();
aestes@apple.com004e16e2011-04-03 00:01:42 +0000398 unsigned short outlineWidth = areaElementStyle->outlineWidth();
399 if (!outlineWidth)
400 return;
401
402 paintInfo.context->drawFocusRing(path, outlineWidth,
403 areaElementStyle->outlineOffset(),
darin@apple.com88b13e62011-02-01 22:49:01 +0000404 areaElementStyle->visitedDependentColor(CSSPropertyOutlineColor));
cfleizach@apple.combc088902010-01-26 18:06:41 +0000405}
darin@apple.com88b13e62011-02-01 22:49:01 +0000406
407void RenderImage::areaElementFocusChanged(HTMLAreaElement* element)
408{
409 ASSERT_UNUSED(element, element->imageElement() == node());
410
411 // It would be more efficient to only repaint the focus ring rectangle
412 // for the passed-in area element. That would require adding functions
413 // to the area element class.
414 repaint();
415}
416
leviw@chromium.org2c32cf22011-11-04 18:27:35 +0000417void RenderImage::paintIntoRect(GraphicsContext* context, const LayoutRect& rect)
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000418{
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000419 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred() || rect.width() <= 0 || rect.height() <= 0)
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000420 return;
421
simon.fraser@apple.com95247972011-01-25 05:52:29 +0000422 RefPtr<Image> img = m_imageResource->image(rect.width(), rect.height());
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000423 if (!img || img->isNull())
424 return;
425
426 HTMLImageElement* imageElt = (node() && node()->hasTagName(imgTag)) ? static_cast<HTMLImageElement*>(node()) : 0;
427 CompositeOperator compositeOperator = imageElt ? imageElt->compositeOperator() : CompositeSourceOver;
senorblanco@chromium.org59baba52011-01-28 00:58:02 +0000428 Image* image = m_imageResource->image().get();
429 bool useLowQualityScaling = shouldPaintAtLowQuality(context, image, image, rect.size());
simon.fraser@apple.com95247972011-01-25 05:52:29 +0000430 context->drawImage(m_imageResource->image(rect.width(), rect.height()).get(), style()->colorSpace(), rect, compositeOperator, useLowQualityScaling);
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000431}
432
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000433bool RenderImage::backgroundIsObscured() const
434{
435 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred())
436 return false;
437
438 if (m_imageResource->cachedImage() && !m_imageResource->cachedImage()->isLoaded())
439 return false;
440
441 EFillBox backgroundClip = style()->backgroundClip();
442
443 // Background paints under borders.
444 if (backgroundClip == BorderFillBox && style()->hasBorder() && !borderObscuresBackground())
445 return false;
446
447 // Background shows in padding area.
448 if ((backgroundClip == BorderFillBox || backgroundClip == PaddingFillBox) && style()->hasPadding())
449 return false;
450
451 // Check for bitmap image with alpha.
452 Image* image = m_imageResource->image().get();
jamesr@google.com6820da92011-08-19 21:50:10 +0000453 if (!image || !image->isBitmapImage() || image->currentFrameHasAlpha())
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000454 return false;
455
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000456 return true;
457}
458
antti@apple.com7f6c5da2007-12-04 19:21:09 +0000459int RenderImage::minimumReplacedHeight() const
darinb9481ed2006-03-20 02:57:59 +0000460{
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000461 return m_imageResource->errorOccurred() ? intrinsicSize().height() : 0;
darinb9481ed2006-03-20 02:57:59 +0000462}
463
cfleizach@apple.combc088902010-01-26 18:06:41 +0000464HTMLMapElement* RenderImage::imageMap() const
darinb9481ed2006-03-20 02:57:59 +0000465{
hyatt@apple.com7e032532009-02-11 22:06:32 +0000466 HTMLImageElement* i = node() && node()->hasTagName(imgTag) ? static_cast<HTMLImageElement*>(node()) : 0;
rolandsteiner@chromium.org0142c6a2011-05-03 17:20:17 +0000467 return i ? i->treeScope()->getImageMap(i->fastGetAttribute(usemapAttr)) : 0;
darinb9481ed2006-03-20 02:57:59 +0000468}
469
eae@chromium.orge14cd1a2011-07-06 23:38:32 +0000470bool RenderImage::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
darinb9481ed2006-03-20 02:57:59 +0000471{
tonikitoo@webkit.org6e7483d2010-09-28 02:28:25 +0000472 HitTestResult tempResult(result.point(), result.topPadding(), result.rightPadding(), result.bottomPadding(), result.leftPadding());
eae@chromium.orgf9ce2672011-06-08 03:12:08 +0000473 bool inside = RenderReplaced::nodeAtPoint(request, tempResult, pointInContainer, accumulatedOffset, hitTestAction);
darinb9481ed2006-03-20 02:57:59 +0000474
tonikitoo@webkit.org81c027d2010-07-29 13:19:43 +0000475 if (tempResult.innerNode() && node()) {
mitz@apple.comffbc6a52009-06-25 05:01:47 +0000476 if (HTMLMapElement* map = imageMap()) {
leviw@chromium.org2c32cf22011-11-04 18:27:35 +0000477 LayoutRect contentBox = contentBoxRect();
eae@chromium.org020f5872011-08-18 03:38:05 +0000478 float scaleFactor = 1 / style()->effectiveZoom();
479 LayoutPoint mapLocation(pointInContainer.x() - accumulatedOffset.x() - this->x() - contentBox.x(), pointInContainer.y() - accumulatedOffset.y() - this->y() - contentBox.y());
480 mapLocation.scale(scaleFactor, scaleFactor);
481
482 if (map->mapMouseEvent(mapLocation, contentBox.size(), tempResult))
mitz@apple.comffbc6a52009-06-25 05:01:47 +0000483 tempResult.setInnerNonSharedNode(node());
darinb9481ed2006-03-20 02:57:59 +0000484 }
485 }
486
tonikitoo@webkit.org81c027d2010-07-29 13:19:43 +0000487 if (!inside && result.isRectBasedTest())
488 result.append(tempResult);
simon.fraser@apple.come9d4e0b2009-03-22 19:50:18 +0000489 if (inside)
490 result = tempResult;
darinb9481ed2006-03-20 02:57:59 +0000491 return inside;
492}
493
494void RenderImage::updateAltText()
495{
hyatt@apple.com7e032532009-02-11 22:06:32 +0000496 if (!node())
darinb9481ed2006-03-20 02:57:59 +0000497 return;
weinigc24ab182006-10-30 22:41:29 +0000498
hyatt@apple.com7e032532009-02-11 22:06:32 +0000499 if (node()->hasTagName(inputTag))
500 m_altText = static_cast<HTMLInputElement*>(node())->altText();
501 else if (node()->hasTagName(imgTag))
502 m_altText = static_cast<HTMLImageElement*>(node())->altText();
darinb9481ed2006-03-20 02:57:59 +0000503}
504
leviw@chromium.org393fc7f2011-06-29 23:59:49 +0000505LayoutUnit RenderImage::computeReplacedLogicalWidth(bool includeMaxWidth) const
darinb9481ed2006-03-20 02:57:59 +0000506{
commit-queue@webkit.org0ab7e682012-03-07 21:02:44 +0000507 // If we've got an explicit width/height assigned, propagate it to the image resource.
508 if (style()->logicalWidth().isSpecified() && style()->logicalHeight().isSpecified()) {
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000509 LayoutUnit width = RenderReplaced::computeReplacedLogicalWidth(includeMaxWidth);
510 m_imageResource->setContainerSizeForRenderer(IntSize(width, computeReplacedLogicalHeight()));
511 return width;
512 }
513
leviw@chromium.org2c32cf22011-11-04 18:27:35 +0000514 IntSize containerSize;
zimmermann@webkit.org1adc5592012-01-20 15:30:17 +0000515 if (m_imageResource->imageHasRelativeWidth() || m_imageResource->imageHasRelativeHeight()) {
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000516 // Propagate the containing block size to the image resource, otherwhise we can't compute our own intrinsic size, if it's relative.
517 RenderObject* containingBlock = isPositioned() ? container() : this->containingBlock();
518 if (containingBlock->isBox()) {
519 RenderBox* box = toRenderBox(containingBlock);
leviw@chromium.org2c32cf22011-11-04 18:27:35 +0000520 containerSize = IntSize(box->availableWidth(), box->availableHeight()); // Already contains zooming information.
hyatt@apple.comd885df72009-01-22 02:31:52 +0000521 }
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000522 } else {
523 // Propagate the current zoomed image size to the image resource, otherwhise the image size will remain the same on-screen.
524 CachedImage* cachedImage = m_imageResource->cachedImage();
525 if (cachedImage && cachedImage->image()) {
526 containerSize = cachedImage->image()->size();
527 // FIXME: Remove unnecessary rounding when layout is off ints: webkit.org/b/63656
528 containerSize.setWidth(static_cast<LayoutUnit>(containerSize.width() * style()->effectiveZoom()));
529 containerSize.setHeight(static_cast<LayoutUnit>(containerSize.height() * style()->effectiveZoom()));
530 }
531 }
hyatt@apple.com82b7dbb2008-04-14 19:28:53 +0000532
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000533 if (!containerSize.isEmpty()) {
534 m_imageResource->setContainerSizeForRenderer(containerSize);
535 const_cast<RenderImage*>(this)->updateIntrinsicSizeIfNeeded(containerSize, false);
536 }
jchaffraix@webkit.org825bcab2011-10-26 18:58:59 +0000537
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000538 return RenderReplaced::computeReplacedLogicalWidth(includeMaxWidth);
darinb9481ed2006-03-20 02:57:59 +0000539}
540
zimmermann@webkit.org1adc5592012-01-20 15:30:17 +0000541void RenderImage::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const
darinb9481ed2006-03-20 02:57:59 +0000542{
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000543 // Assure this method is never used for SVGImages.
544 ASSERT(!embeddedContentBox());
545 isPercentageIntrinsicSize = false;
546 CachedImage* cachedImage = m_imageResource ? m_imageResource->cachedImage() : 0;
zimmermann@webkit.org1adc5592012-01-20 15:30:17 +0000547 if (!cachedImage || !cachedImage->image())
548 return;
549 intrinsicSize = cachedImage->image()->size();
550 intrinsicRatio = intrinsicSize.width() / static_cast<double>(intrinsicSize.height());
darinb9481ed2006-03-20 02:57:59 +0000551}
552
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000553bool RenderImage::needsPreferredWidthsRecalculation() const
darinb9481ed2006-03-20 02:57:59 +0000554{
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000555 if (RenderReplaced::needsPreferredWidthsRecalculation())
556 return true;
557 return embeddedContentBox();
558}
559
560RenderBox* RenderImage::embeddedContentBox() const
561{
562 if (!m_imageResource)
darinb9481ed2006-03-20 02:57:59 +0000563 return 0;
darinb9481ed2006-03-20 02:57:59 +0000564
johnnyg@google.comf4ebad62011-10-31 23:00:42 +0000565#if ENABLE(SVG)
zimmermann@webkit.org89d03f82011-11-08 09:32:52 +0000566 CachedImage* cachedImage = m_imageResource->cachedImage();
567 if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage())
568 return static_cast<SVGImage*>(cachedImage->image())->embeddedContentBox();
johnnyg@google.comf4ebad62011-10-31 23:00:42 +0000569#endif
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000570
571 return 0;
darinb9481ed2006-03-20 02:57:59 +0000572}
573
weinigc24ab182006-10-30 22:41:29 +0000574} // namespace WebCore