blob: d025c09e06b89183cb9d0c90caf5703c5d87ff2a [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
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000267 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred()) {
weinigc24ab182006-10-30 22:41:29 +0000268 if (paintInfo.phase == PaintPhaseSelection)
darinb9481ed2006-03-20 02:57:59 +0000269 return;
270
271 if (cWidth > 2 && cHeight > 2) {
bdash20fe5432007-10-14 11:59:35 +0000272 // Draw an outline rect where the image should be.
273 context->setStrokeStyle(SolidStroke);
bdakin@apple.com4a0593e2009-11-10 21:44:56 +0000274 context->setStrokeColor(Color::lightGray, style()->colorSpace());
275 context->setFillColor(Color::transparent, style()->colorSpace());
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000276 context->drawRect(LayoutRect(paintOffset.x() + leftBorder + leftPad, paintOffset.y() + topBorder + topPad, cWidth, cHeight));
weinigc24ab182006-10-30 22:41:29 +0000277
darinb9481ed2006-03-20 02:57:59 +0000278 bool errorPictureDrawn = false;
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000279 LayoutSize imageOffset;
bdash20fe5432007-10-14 11:59:35 +0000280 // When calculating the usable dimensions, exclude the pixels of
281 // the ouline rect so the error image/alt text doesn't draw on it.
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000282 LayoutUnit usableWidth = cWidth - 2;
283 LayoutUnit usableHeight = cHeight - 2;
weinigc24ab182006-10-30 22:41:29 +0000284
simon.fraser@apple.com95247972011-01-25 05:52:29 +0000285 RefPtr<Image> image = m_imageResource->image();
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000286
287 if (m_imageResource->errorOccurred() && !image->isNull() && usableWidth >= image->width() && usableHeight >= image->height()) {
bdakin@apple.com57d71e22011-09-22 03:29:50 +0000288 float deviceScaleFactor = WebCore::deviceScaleFactor(frame());
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000289 // Call brokenImage() explicitly to ensure we get the broken image icon at the appropriate resolution.
bdakin@apple.come6372a72011-09-14 18:35:49 +0000290 pair<Image*, float> brokenImageAndImageScaleFactor = m_imageResource->cachedImage()->brokenImage(deviceScaleFactor);
291 image = brokenImageAndImageScaleFactor.first;
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000292 IntSize imageSize = image->size();
bdakin@apple.come6372a72011-09-14 18:35:49 +0000293 imageSize.scale(1 / brokenImageAndImageScaleFactor.second);
darinb9481ed2006-03-20 02:57:59 +0000294 // Center the error image, accounting for border and padding.
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000295 LayoutUnit centerX = (usableWidth - imageSize.width()) / 2;
darinb9481ed2006-03-20 02:57:59 +0000296 if (centerX < 0)
297 centerX = 0;
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000298 LayoutUnit centerY = (usableHeight - imageSize.height()) / 2;
darinb9481ed2006-03-20 02:57:59 +0000299 if (centerY < 0)
300 centerY = 0;
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000301 imageOffset = LayoutSize(leftBorder + leftPad + centerX + 1, topBorder + topPad + centerY + 1);
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +0000302 context->drawImage(image.get(), style()->colorSpace(), IntRect(paintOffset + imageOffset, imageSize));
darinb9481ed2006-03-20 02:57:59 +0000303 errorPictureDrawn = true;
304 }
weinigc24ab182006-10-30 22:41:29 +0000305
darinb9481ed2006-03-20 02:57:59 +0000306 if (!m_altText.isEmpty()) {
hyatt@apple.comcbe8fce2009-01-17 21:38:38 +0000307 String text = document()->displayStringModifiedByEncoding(m_altText);
hyatt@apple.com36bdcfa2010-05-21 20:56:24 +0000308 context->setFillColor(style()->visitedDependentColor(CSSPropertyColor), style()->colorSpace());
darinb9481ed2006-03-20 02:57:59 +0000309 const Font& font = style()->font();
zimmermann@webkit.orge8433cc2011-01-22 11:46:23 +0000310 const FontMetrics& fontMetrics = font.fontMetrics();
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000311 LayoutUnit ascent = fontMetrics.ascent();
312 LayoutPoint altTextOffset = paintOffset;
leviw@chromium.org35e01312011-06-03 02:41:09 +0000313 altTextOffset.move(leftBorder + leftPad, topBorder + topPad + ascent);
weinig0a635f02006-11-01 21:49:13 +0000314
darinb9481ed2006-03-20 02:57:59 +0000315 // Only draw the alt text if it'll fit within the content box,
316 // and only if it fits above the error image.
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +0000317 TextRun textRun = RenderBlock::constructTextRun(this, font, text, style());
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000318 LayoutUnit textWidth = font.width(textRun);
darinb9481ed2006-03-20 02:57:59 +0000319 if (errorPictureDrawn) {
leviw@chromium.org35e01312011-06-03 02:41:09 +0000320 if (usableWidth >= textWidth && fontMetrics.height() <= imageOffset.height())
321 context->drawText(font, textRun, altTextOffset);
zimmermann@webkit.orge8433cc2011-01-22 11:46:23 +0000322 } else if (usableWidth >= textWidth && cHeight >= fontMetrics.height())
leviw@chromium.org35e01312011-06-03 02:41:09 +0000323 context->drawText(font, textRun, altTextOffset);
darinb9481ed2006-03-20 02:57:59 +0000324 }
325 }
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000326 } else if (m_imageResource->hasImage() && cWidth > 0 && cHeight > 0) {
simon.fraser@apple.com95247972011-01-25 05:52:29 +0000327 RefPtr<Image> img = m_imageResource->image(cWidth, cHeight);
hyatt@apple.com82b7dbb2008-04-14 19:28:53 +0000328 if (!img || img->isNull())
329 return;
330
harrisonddf79c12006-07-13 23:17:08 +0000331#if PLATFORM(MAC)
weinig0a635f02006-11-01 21:49:13 +0000332 if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
leviw@chromium.org35e01312011-06-03 02:41:09 +0000333 paintCustomHighlight(toPoint(paintOffset - location()), style()->highlight(), true);
harrisonddf79c12006-07-13 23:17:08 +0000334#endif
335
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000336 LayoutSize contentSize(cWidth, cHeight);
337 LayoutPoint contentLocation = paintOffset;
leviw@chromium.org35e01312011-06-03 02:41:09 +0000338 contentLocation.move(leftBorder + leftPad, topBorder + topPad);
leviw@chromium.orgc4918082011-06-29 01:06:00 +0000339 paintIntoRect(context, LayoutRect(contentLocation, contentSize));
darinb9481ed2006-03-20 02:57:59 +0000340 }
341}
342
leviw@chromium.orgb3757502011-06-29 21:33:11 +0000343void RenderImage::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
cfleizach@apple.combc088902010-01-26 18:06:41 +0000344{
leviw@chromium.org61ca1372011-06-07 18:56:41 +0000345 RenderReplaced::paint(paintInfo, paintOffset);
cfleizach@apple.combc088902010-01-26 18:06:41 +0000346
347 if (paintInfo.phase == PaintPhaseOutline)
darin@apple.com88b13e62011-02-01 22:49:01 +0000348 paintAreaElementFocusRing(paintInfo);
cfleizach@apple.combc088902010-01-26 18:06:41 +0000349}
350
darin@apple.com88b13e62011-02-01 22:49:01 +0000351void RenderImage::paintAreaElementFocusRing(PaintInfo& paintInfo)
cfleizach@apple.combc088902010-01-26 18:06:41 +0000352{
darin@apple.com88b13e62011-02-01 22:49:01 +0000353 Document* document = this->document();
354
355 if (document->printing() || !document->frame()->selection()->isFocusedAndActive())
cfleizach@apple.combc088902010-01-26 18:06:41 +0000356 return;
357
358 if (paintInfo.context->paintingDisabled() && !paintInfo.context->updatingControlTints())
359 return;
360
cfleizach@apple.combc088902010-01-26 18:06:41 +0000361 Node* focusedNode = document->focusedNode();
darin@apple.com88b13e62011-02-01 22:49:01 +0000362 if (!focusedNode || !focusedNode->hasTagName(areaTag))
cfleizach@apple.combc088902010-01-26 18:06:41 +0000363 return;
dbates@webkit.orgd0f03962010-11-30 21:57:16 +0000364
darin@apple.com88b13e62011-02-01 22:49:01 +0000365 HTMLAreaElement* areaElement = static_cast<HTMLAreaElement*>(focusedNode);
366 if (areaElement->imageElement() != node())
367 return;
368
369 // Even if the theme handles focus ring drawing for entire elements, it won't do it for
370 // an area within an image, so we don't call RenderTheme::supportsFocusRing here.
371
thakis@chromium.org10223a72011-02-10 21:04:40 +0000372 Path path = areaElement->computePath(this);
darin@apple.com88b13e62011-02-01 22:49:01 +0000373 if (path.isEmpty())
374 return;
375
376 // FIXME: Do we need additional code to clip the path to the image's bounding box?
377
378 RenderStyle* areaElementStyle = areaElement->computedStyle();
aestes@apple.com004e16e2011-04-03 00:01:42 +0000379 unsigned short outlineWidth = areaElementStyle->outlineWidth();
380 if (!outlineWidth)
381 return;
382
383 paintInfo.context->drawFocusRing(path, outlineWidth,
384 areaElementStyle->outlineOffset(),
darin@apple.com88b13e62011-02-01 22:49:01 +0000385 areaElementStyle->visitedDependentColor(CSSPropertyOutlineColor));
cfleizach@apple.combc088902010-01-26 18:06:41 +0000386}
darin@apple.com88b13e62011-02-01 22:49:01 +0000387
388void RenderImage::areaElementFocusChanged(HTMLAreaElement* element)
389{
390 ASSERT_UNUSED(element, element->imageElement() == node());
391
392 // It would be more efficient to only repaint the focus ring rectangle
393 // for the passed-in area element. That would require adding functions
394 // to the area element class.
395 repaint();
396}
397
leviw@chromium.org2c32cf22011-11-04 18:27:35 +0000398void RenderImage::paintIntoRect(GraphicsContext* context, const LayoutRect& rect)
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000399{
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000400 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred() || rect.width() <= 0 || rect.height() <= 0)
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000401 return;
402
simon.fraser@apple.com95247972011-01-25 05:52:29 +0000403 RefPtr<Image> img = m_imageResource->image(rect.width(), rect.height());
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000404 if (!img || img->isNull())
405 return;
406
407 HTMLImageElement* imageElt = (node() && node()->hasTagName(imgTag)) ? static_cast<HTMLImageElement*>(node()) : 0;
408 CompositeOperator compositeOperator = imageElt ? imageElt->compositeOperator() : CompositeSourceOver;
senorblanco@chromium.org59baba52011-01-28 00:58:02 +0000409 Image* image = m_imageResource->image().get();
410 bool useLowQualityScaling = shouldPaintAtLowQuality(context, image, image, rect.size());
simon.fraser@apple.com95247972011-01-25 05:52:29 +0000411 context->drawImage(m_imageResource->image(rect.width(), rect.height()).get(), style()->colorSpace(), rect, compositeOperator, useLowQualityScaling);
eric.carlson@apple.coma7959722010-01-12 18:23:36 +0000412}
413
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000414bool RenderImage::backgroundIsObscured() const
415{
416 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred())
417 return false;
418
419 if (m_imageResource->cachedImage() && !m_imageResource->cachedImage()->isLoaded())
420 return false;
421
422 EFillBox backgroundClip = style()->backgroundClip();
423
424 // Background paints under borders.
425 if (backgroundClip == BorderFillBox && style()->hasBorder() && !borderObscuresBackground())
426 return false;
427
428 // Background shows in padding area.
429 if ((backgroundClip == BorderFillBox || backgroundClip == PaddingFillBox) && style()->hasPadding())
430 return false;
431
432 // Check for bitmap image with alpha.
433 Image* image = m_imageResource->image().get();
jamesr@google.com6820da92011-08-19 21:50:10 +0000434 if (!image || !image->isBitmapImage() || image->currentFrameHasAlpha())
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000435 return false;
436
simon.fraser@apple.com620f56b2011-07-23 00:39:54 +0000437 return true;
438}
439
antti@apple.com7f6c5da2007-12-04 19:21:09 +0000440int RenderImage::minimumReplacedHeight() const
darinb9481ed2006-03-20 02:57:59 +0000441{
zimmermann@webkit.org50d25552010-08-27 14:46:22 +0000442 return m_imageResource->errorOccurred() ? intrinsicSize().height() : 0;
darinb9481ed2006-03-20 02:57:59 +0000443}
444
cfleizach@apple.combc088902010-01-26 18:06:41 +0000445HTMLMapElement* RenderImage::imageMap() const
darinb9481ed2006-03-20 02:57:59 +0000446{
hyatt@apple.com7e032532009-02-11 22:06:32 +0000447 HTMLImageElement* i = node() && node()->hasTagName(imgTag) ? static_cast<HTMLImageElement*>(node()) : 0;
rolandsteiner@chromium.org0142c6a2011-05-03 17:20:17 +0000448 return i ? i->treeScope()->getImageMap(i->fastGetAttribute(usemapAttr)) : 0;
darinb9481ed2006-03-20 02:57:59 +0000449}
450
eae@chromium.orge14cd1a2011-07-06 23:38:32 +0000451bool RenderImage::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
darinb9481ed2006-03-20 02:57:59 +0000452{
tonikitoo@webkit.org6e7483d2010-09-28 02:28:25 +0000453 HitTestResult tempResult(result.point(), result.topPadding(), result.rightPadding(), result.bottomPadding(), result.leftPadding());
eae@chromium.orgf9ce2672011-06-08 03:12:08 +0000454 bool inside = RenderReplaced::nodeAtPoint(request, tempResult, pointInContainer, accumulatedOffset, hitTestAction);
darinb9481ed2006-03-20 02:57:59 +0000455
tonikitoo@webkit.org81c027d2010-07-29 13:19:43 +0000456 if (tempResult.innerNode() && node()) {
mitz@apple.comffbc6a52009-06-25 05:01:47 +0000457 if (HTMLMapElement* map = imageMap()) {
leviw@chromium.org2c32cf22011-11-04 18:27:35 +0000458 LayoutRect contentBox = contentBoxRect();
eae@chromium.org020f5872011-08-18 03:38:05 +0000459 float scaleFactor = 1 / style()->effectiveZoom();
460 LayoutPoint mapLocation(pointInContainer.x() - accumulatedOffset.x() - this->x() - contentBox.x(), pointInContainer.y() - accumulatedOffset.y() - this->y() - contentBox.y());
461 mapLocation.scale(scaleFactor, scaleFactor);
462
463 if (map->mapMouseEvent(mapLocation, contentBox.size(), tempResult))
mitz@apple.comffbc6a52009-06-25 05:01:47 +0000464 tempResult.setInnerNonSharedNode(node());
darinb9481ed2006-03-20 02:57:59 +0000465 }
466 }
467
tonikitoo@webkit.org81c027d2010-07-29 13:19:43 +0000468 if (!inside && result.isRectBasedTest())
469 result.append(tempResult);
simon.fraser@apple.come9d4e0b2009-03-22 19:50:18 +0000470 if (inside)
471 result = tempResult;
darinb9481ed2006-03-20 02:57:59 +0000472 return inside;
473}
474
475void RenderImage::updateAltText()
476{
hyatt@apple.com7e032532009-02-11 22:06:32 +0000477 if (!node())
darinb9481ed2006-03-20 02:57:59 +0000478 return;
weinigc24ab182006-10-30 22:41:29 +0000479
hyatt@apple.com7e032532009-02-11 22:06:32 +0000480 if (node()->hasTagName(inputTag))
481 m_altText = static_cast<HTMLInputElement*>(node())->altText();
482 else if (node()->hasTagName(imgTag))
483 m_altText = static_cast<HTMLImageElement*>(node())->altText();
darinb9481ed2006-03-20 02:57:59 +0000484}
485
leviw@chromium.org393fc7f2011-06-29 23:59:49 +0000486LayoutUnit RenderImage::computeReplacedLogicalWidth(bool includeMaxWidth) const
darinb9481ed2006-03-20 02:57:59 +0000487{
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000488 // If we've got an explicit width/height assigned, propagate it to the image resource.
489 if (style()->logicalWidth().isFixed() && style()->logicalHeight().isFixed()) {
490 LayoutUnit width = RenderReplaced::computeReplacedLogicalWidth(includeMaxWidth);
491 m_imageResource->setContainerSizeForRenderer(IntSize(width, computeReplacedLogicalHeight()));
492 return width;
493 }
494
leviw@chromium.org2c32cf22011-11-04 18:27:35 +0000495 IntSize containerSize;
zimmermann@webkit.org1adc5592012-01-20 15:30:17 +0000496 if (m_imageResource->imageHasRelativeWidth() || m_imageResource->imageHasRelativeHeight()) {
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000497 // Propagate the containing block size to the image resource, otherwhise we can't compute our own intrinsic size, if it's relative.
498 RenderObject* containingBlock = isPositioned() ? container() : this->containingBlock();
499 if (containingBlock->isBox()) {
500 RenderBox* box = toRenderBox(containingBlock);
leviw@chromium.org2c32cf22011-11-04 18:27:35 +0000501 containerSize = IntSize(box->availableWidth(), box->availableHeight()); // Already contains zooming information.
hyatt@apple.comd885df72009-01-22 02:31:52 +0000502 }
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000503 } else {
504 // Propagate the current zoomed image size to the image resource, otherwhise the image size will remain the same on-screen.
505 CachedImage* cachedImage = m_imageResource->cachedImage();
506 if (cachedImage && cachedImage->image()) {
507 containerSize = cachedImage->image()->size();
508 // FIXME: Remove unnecessary rounding when layout is off ints: webkit.org/b/63656
509 containerSize.setWidth(static_cast<LayoutUnit>(containerSize.width() * style()->effectiveZoom()));
510 containerSize.setHeight(static_cast<LayoutUnit>(containerSize.height() * style()->effectiveZoom()));
511 }
512 }
hyatt@apple.com82b7dbb2008-04-14 19:28:53 +0000513
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000514 if (!containerSize.isEmpty()) {
515 m_imageResource->setContainerSizeForRenderer(containerSize);
516 const_cast<RenderImage*>(this)->updateIntrinsicSizeIfNeeded(containerSize, false);
517 }
jchaffraix@webkit.org825bcab2011-10-26 18:58:59 +0000518
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000519 return RenderReplaced::computeReplacedLogicalWidth(includeMaxWidth);
darinb9481ed2006-03-20 02:57:59 +0000520}
521
zimmermann@webkit.org1adc5592012-01-20 15:30:17 +0000522void RenderImage::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const
darinb9481ed2006-03-20 02:57:59 +0000523{
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000524 // Assure this method is never used for SVGImages.
525 ASSERT(!embeddedContentBox());
526 isPercentageIntrinsicSize = false;
527 CachedImage* cachedImage = m_imageResource ? m_imageResource->cachedImage() : 0;
zimmermann@webkit.org1adc5592012-01-20 15:30:17 +0000528 if (!cachedImage || !cachedImage->image())
529 return;
530 intrinsicSize = cachedImage->image()->size();
531 intrinsicRatio = intrinsicSize.width() / static_cast<double>(intrinsicSize.height());
darinb9481ed2006-03-20 02:57:59 +0000532}
533
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000534bool RenderImage::needsPreferredWidthsRecalculation() const
darinb9481ed2006-03-20 02:57:59 +0000535{
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000536 if (RenderReplaced::needsPreferredWidthsRecalculation())
537 return true;
538 return embeddedContentBox();
539}
540
541RenderBox* RenderImage::embeddedContentBox() const
542{
543 if (!m_imageResource)
darinb9481ed2006-03-20 02:57:59 +0000544 return 0;
darinb9481ed2006-03-20 02:57:59 +0000545
johnnyg@google.comf4ebad62011-10-31 23:00:42 +0000546#if ENABLE(SVG)
zimmermann@webkit.org89d03f82011-11-08 09:32:52 +0000547 CachedImage* cachedImage = m_imageResource->cachedImage();
548 if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage())
549 return static_cast<SVGImage*>(cachedImage->image())->embeddedContentBox();
johnnyg@google.comf4ebad62011-10-31 23:00:42 +0000550#endif
zimmermann@webkit.org4729baa2011-10-31 14:07:49 +0000551
552 return 0;
darinb9481ed2006-03-20 02:57:59 +0000553}
554
weinigc24ab182006-10-30 22:41:29 +0000555} // namespace WebCore