darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 1 | /* |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 2 | * 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.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 7 | * Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved. |
senorblanco@chromium.org | 97cbe00 | 2010-06-17 19:50:36 +0000 | [diff] [blame] | 8 | * Copyright (C) 2010 Google Inc. All rights reserved. |
zimmermann@webkit.org | 2454d643 | 2012-03-27 09:24:46 +0000 | [diff] [blame] | 9 | * Copyright (C) Research In Motion Limited 2011-2012. All rights reserved. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 10 | * |
| 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 |
ddkilzer | c8eccec | 2007-09-26 02:29:57 +0000 | [diff] [blame] | 23 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 24 | * Boston, MA 02110-1301, USA. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 25 | * |
| 26 | */ |
| 27 | |
| 28 | #include "config.h" |
| 29 | #include "RenderImage.h" |
| 30 | |
simon.fraser@apple.com | 620f56b | 2011-07-23 00:39:54 +0000 | [diff] [blame] | 31 | #include "BitmapImage.h" |
abarth@webkit.org | 401a379 | 2013-03-03 10:12:59 +0000 | [diff] [blame] | 32 | #include "CachedImage.h" |
commit-queue@webkit.org | d0c076d | 2012-08-22 22:25:59 +0000 | [diff] [blame] | 33 | #include "Font.h" |
msaboff@apple.com | 5dbcc69 | 2011-06-07 20:46:28 +0000 | [diff] [blame] | 34 | #include "FontCache.h" |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 35 | #include "Frame.h" |
rniwa@webkit.org | 78bbc94 | 2011-05-05 18:14:43 +0000 | [diff] [blame] | 36 | #include "FrameSelection.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 37 | #include "GraphicsContext.h" |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 38 | #include "HTMLAreaElement.h" |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 39 | #include "HTMLImageElement.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 40 | #include "HTMLInputElement.h" |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 41 | #include "HTMLMapElement.h" |
darin | 98fa8b8 | 2006-03-20 08:03:57 +0000 | [diff] [blame] | 42 | #include "HTMLNames.h" |
bdakin | c1a5a83 | 2006-10-28 18:28:00 +0000 | [diff] [blame] | 43 | #include "HitTestResult.h" |
hyatt | b21f837 | 2007-09-04 22:58:27 +0000 | [diff] [blame] | 44 | #include "Page.h" |
jchaffraix@webkit.org | 363c387 | 2012-04-17 21:51:22 +0000 | [diff] [blame] | 45 | #include "PaintInfo.h" |
hyatt | d804834 | 2006-05-31 01:48:18 +0000 | [diff] [blame] | 46 | #include "RenderView.h" |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 47 | #include "SVGImage.h" |
andersca@apple.com | a5fb7da | 2013-04-16 19:57:36 +0000 | [diff] [blame] | 48 | #include <wtf/StackStats.h> |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 49 | |
darin | 7bd7095 | 2006-04-13 07:07:34 +0000 | [diff] [blame] | 50 | using namespace std; |
| 51 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 52 | namespace WebCore { |
| 53 | |
| 54 | using namespace HTMLNames; |
| 55 | |
antti@apple.com | 6310f61 | 2013-01-19 14:34:36 +0000 | [diff] [blame] | 56 | RenderImage::RenderImage(Element* element) |
| 57 | : RenderReplaced(element, IntSize()) |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 58 | , m_needsToSetSizeForAltText(false) |
antti@apple.com | 17978ce | 2011-07-13 10:23:54 +0000 | [diff] [blame] | 59 | , m_didIncrementVisuallyNonEmptyPixelCount(false) |
leo.yang@torchmobile.com.cn | ff697755 | 2011-12-02 08:05:08 +0000 | [diff] [blame] | 60 | , m_isGeneratedContent(false) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 61 | { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 62 | updateAltText(); |
| 63 | } |
| 64 | |
antti@apple.com | 6310f61 | 2013-01-19 14:34:36 +0000 | [diff] [blame] | 65 | RenderImage* RenderImage::createAnonymous(Document* document) |
| 66 | { |
| 67 | RenderImage* image = new (document->renderArena()) RenderImage(0); |
| 68 | image->setDocumentForAnonymous(document); |
| 69 | return image; |
| 70 | } |
| 71 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 72 | RenderImage::~RenderImage() |
| 73 | { |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 74 | ASSERT(m_imageResource); |
| 75 | m_imageResource->shutdown(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 76 | } |
| 77 | |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 78 | void RenderImage::setImageResource(PassOwnPtr<RenderImageResource> imageResource) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 79 | { |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 80 | ASSERT(!m_imageResource); |
| 81 | m_imageResource = imageResource; |
| 82 | m_imageResource->initialize(this); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 83 | } |
| 84 | |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 85 | // If we'll be displaying either alt text or an image, add some padding. |
| 86 | static const unsigned short paddingWidth = 4; |
| 87 | static 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.com | 0acc935 | 2011-02-17 19:19:07 +0000 | [diff] [blame] | 91 | static const float maxAltTextWidth = 1024; |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 92 | static const int maxAltTextHeight = 256; |
| 93 | |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 94 | IntSize RenderImage::imageSizeForError(CachedImage* newImage) const |
| 95 | { |
| 96 | ASSERT_ARG(newImage, newImage); |
zimmermann@webkit.org | 8cf217f | 2011-10-14 08:08:51 +0000 | [diff] [blame] | 97 | ASSERT_ARG(newImage, newImage->imageForRenderer(this)); |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 98 | |
bdakin@apple.com | b1f426c | 2011-09-13 22:17:09 +0000 | [diff] [blame] | 99 | IntSize imageSize; |
| 100 | if (newImage->willPaintBrokenImage()) { |
bdakin@apple.com | 57d71e2 | 2011-09-22 03:29:50 +0000 | [diff] [blame] | 101 | float deviceScaleFactor = WebCore::deviceScaleFactor(frame()); |
bdakin@apple.com | e6372a7 | 2011-09-14 18:35:49 +0000 | [diff] [blame] | 102 | pair<Image*, float> brokenImageAndImageScaleFactor = newImage->brokenImage(deviceScaleFactor); |
| 103 | imageSize = brokenImageAndImageScaleFactor.first->size(); |
| 104 | imageSize.scale(1 / brokenImageAndImageScaleFactor.second); |
bdakin@apple.com | b1f426c | 2011-09-13 22:17:09 +0000 | [diff] [blame] | 105 | } else |
zimmermann@webkit.org | 8cf217f | 2011-10-14 08:08:51 +0000 | [diff] [blame] | 106 | imageSize = newImage->imageForRenderer(this)->size(); |
bdakin@apple.com | b49bc3d | 2011-09-10 06:28:00 +0000 | [diff] [blame] | 107 | |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 108 | // 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.com | b1f426c | 2011-09-13 22:17:09 +0000 | [diff] [blame] | 110 | return IntSize(paddingWidth + imageSize.width() * style()->effectiveZoom(), paddingHeight + imageSize.height() * style()->effectiveZoom()); |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 111 | } |
| 112 | |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 113 | // Sets the image height and width to fit the alt text. Returns true if the |
| 114 | // image size changed. |
| 115 | bool RenderImage::setImageSizeForAltText(CachedImage* newImage /* = 0 */) |
| 116 | { |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 117 | IntSize imageSize; |
zimmermann@webkit.org | 8cf217f | 2011-10-14 08:08:51 +0000 | [diff] [blame] | 118 | if (newImage && newImage->imageForRenderer(this)) |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 119 | 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); |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 123 | } |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 124 | |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 125 | // we have an alt and the user meant it (its not a text we invented) |
| 126 | if (!m_altText.isEmpty()) { |
msaboff@apple.com | 5dbcc69 | 2011-06-07 20:46:28 +0000 | [diff] [blame] | 127 | FontCachePurgePreventer fontCachePurgePreventer; |
| 128 | |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 129 | const Font& font = style()->font(); |
ch.dumez@sisa.samsung.com | 6050047 | 2013-04-02 20:35:33 +0000 | [diff] [blame] | 130 | 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); |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 132 | } |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 133 | |
darin | 751cf30 | 2007-05-01 22:10:41 +0000 | [diff] [blame] | 134 | if (imageSize == intrinsicSize()) |
| 135 | return false; |
| 136 | |
| 137 | setIntrinsicSize(imageSize); |
| 138 | return true; |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 139 | } |
| 140 | |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 141 | void 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.org | 2299fe0 | 2012-06-11 17:19:26 +0000 | [diff] [blame] | 149 | #if ENABLE(CSS_IMAGE_RESOLUTION) |
commit-queue@webkit.org | 017dbda | 2012-06-18 23:30:29 +0000 | [diff] [blame] | 150 | if (diff == StyleDifferenceLayout |
| 151 | && (oldStyle->imageResolution() != style()->imageResolution() |
commit-queue@webkit.org | 27e15fe | 2012-06-25 01:46:14 +0000 | [diff] [blame] | 152 | || oldStyle->imageResolutionSnap() != style()->imageResolutionSnap() |
commit-queue@webkit.org | 017dbda | 2012-06-18 23:30:29 +0000 | [diff] [blame] | 153 | || oldStyle->imageResolutionSource() != style()->imageResolutionSource())) |
commit-queue@webkit.org | 2299fe0 | 2012-06-11 17:19:26 +0000 | [diff] [blame] | 154 | imageDimensionsChanged(true /* imageSizeChanged */); |
| 155 | #endif |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 156 | } |
| 157 | |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 158 | void RenderImage::imageChanged(WrappedImagePtr newImage, const IntRect* rect) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 159 | { |
jonlee@apple.com | 37fbb62 | 2012-05-17 00:09:07 +0000 | [diff] [blame] | 160 | // 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()) |
ap | 692e06e | 2006-10-01 09:11:35 +0000 | [diff] [blame] | 164 | return; |
| 165 | |
dino@apple.com | 397229e | 2008-07-10 19:31:43 +0000 | [diff] [blame] | 166 | if (hasBoxDecorations() || hasMask()) |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 167 | RenderReplaced::imageChanged(newImage, rect); |
antti@apple.com | cae24e0 | 2013-03-20 00:17:58 +0000 | [diff] [blame] | 168 | |
aestes@apple.com | bbbea4d | 2010-10-08 21:57:29 +0000 | [diff] [blame] | 169 | if (!m_imageResource) |
| 170 | return; |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 171 | |
| 172 | if (newImage != m_imageResource->imagePtr() || !newImage) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 173 | return; |
antti@apple.com | 17978ce | 2011-07-13 10:23:54 +0000 | [diff] [blame] | 174 | |
| 175 | if (!m_didIncrementVisuallyNonEmptyPixelCount) { |
eae@chromium.org | 88c682c | 2012-11-01 15:00:26 +0000 | [diff] [blame] | 176 | // 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.com | 17978ce | 2011-07-13 10:23:54 +0000 | [diff] [blame] | 178 | m_didIncrementVisuallyNonEmptyPixelCount = true; |
| 179 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 180 | |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 181 | bool imageSizeChanged = false; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 182 | |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 183 | // Set image dimensions, taking into account the size of the alt text. |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 184 | if (m_imageResource->errorOccurred()) { |
antti@apple.com | 52bdb21 | 2012-09-28 03:51:25 +0000 | [diff] [blame] | 185 | if (!m_altText.isEmpty() && document()->hasPendingStyleRecalc()) { |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 186 | ASSERT(node()); |
| 187 | if (node()) { |
| 188 | m_needsToSetSizeForAltText = true; |
| 189 | node()->setNeedsStyleRecalc(SyntheticStyleChange); |
| 190 | } |
| 191 | return; |
| 192 | } |
mitz@apple.com | 543c300 | 2010-11-16 22:08:26 +0000 | [diff] [blame] | 193 | imageSizeChanged = setImageSizeForAltText(m_imageResource->cachedImage()); |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 194 | } |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 195 | |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 196 | imageDimensionsChanged(imageSizeChanged, rect); |
| 197 | } |
| 198 | |
eae@chromium.org | 88c682c | 2012-11-01 15:00:26 +0000 | [diff] [blame] | 199 | bool RenderImage::updateIntrinsicSizeIfNeeded(const LayoutSize& newSize, bool imageSizeChanged) |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 200 | { |
| 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.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 209 | void RenderImage::imageDimensionsChanged(bool imageSizeChanged, const IntRect* rect) |
| 210 | { |
commit-queue@webkit.org | 2299fe0 | 2012-06-11 17:19:26 +0000 | [diff] [blame] | 211 | #if ENABLE(CSS_IMAGE_RESOLUTION) |
commit-queue@webkit.org | 27e15fe | 2012-06-25 01:46:14 +0000 | [diff] [blame] | 212 | 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.org | 2299fe0 | 2012-06-11 17:19:26 +0000 | [diff] [blame] | 218 | #else |
jchaffraix@webkit.org | 64e734e | 2012-05-10 22:08:27 +0000 | [diff] [blame] | 219 | bool intrinsicSizeChanged = updateIntrinsicSizeIfNeeded(m_imageResource->imageSize(style()->effectiveZoom()), imageSizeChanged); |
commit-queue@webkit.org | 2299fe0 | 2012-06-11 17:19:26 +0000 | [diff] [blame] | 220 | #endif |
jchaffraix@webkit.org | 64e734e | 2012-05-10 22:08:27 +0000 | [diff] [blame] | 221 | |
| 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 | |
bdash | a159547 | 2007-05-02 09:17:52 +0000 | [diff] [blame] | 229 | bool shouldRepaint = true; |
jchaffraix@webkit.org | 64e734e | 2012-05-10 22:08:27 +0000 | [diff] [blame] | 230 | if (intrinsicSizeChanged) { |
jchaffraix@webkit.org | 64e734e | 2012-05-10 22:08:27 +0000 | [diff] [blame] | 231 | if (!preferredLogicalWidthsDirty()) |
| 232 | setPreferredLogicalWidthsDirty(true); |
tony@chromium.org | 513fe61 | 2013-01-04 01:21:07 +0000 | [diff] [blame] | 233 | |
| 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 | } |
bdash | a159547 | 2007-05-02 09:17:52 +0000 | [diff] [blame] | 244 | |
commit-queue@webkit.org | 7555e8c | 2012-12-18 00:59:26 +0000 | [diff] [blame] | 245 | // 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.org | 513fe61 | 2013-01-04 01:21:07 +0000 | [diff] [blame] | 249 | style()->logicalWidth().isPercent() |
| 250 | || style()->logicalMaxWidth().isPercent() |
| 251 | || style()->logicalMinWidth().isPercent(); |
commit-queue@webkit.org | 7555e8c | 2012-12-18 00:59:26 +0000 | [diff] [blame] | 252 | |
tony@chromium.org | 513fe61 | 2013-01-04 01:21:07 +0000 | [diff] [blame] | 253 | if (imageSizeChanged || hasOverrideSize || containingBlockNeedsToRecomputePreferredSize) { |
jchaffraix@webkit.org | 64e734e | 2012-05-10 22:08:27 +0000 | [diff] [blame] | 254 | shouldRepaint = false; |
| 255 | if (!selfNeedsLayout()) |
| 256 | setNeedsLayout(true); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 260 | if (shouldRepaint) { |
leviw@chromium.org | 2c32cf2 | 2011-11-04 18:27:35 +0000 | [diff] [blame] | 261 | LayoutRect repaintRect; |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 262 | 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.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 265 | repaintRect = enclosingIntRect(mapRect(*rect, FloatRect(FloatPoint(), m_imageResource->imageSize(1.0f)), contentBoxRect())); |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 266 | // Guard against too-large changed rects. |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 267 | repaintRect.intersect(contentBoxRect()); |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 268 | } else |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 269 | repaintRect = contentBoxRect(); |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 270 | |
| 271 | repaintRectangle(repaintRect); |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 272 | |
| 273 | #if USE(ACCELERATED_COMPOSITING) |
jchaffraix@webkit.org | 363c387 | 2012-04-17 21:51:22 +0000 | [diff] [blame] | 274 | // Tell any potential compositing layers that the image needs updating. |
| 275 | contentChanged(ImageChanged); |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 276 | #endif |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 277 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 278 | } |
| 279 | |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 280 | void RenderImage::notifyFinished(CachedResource* newImage) |
| 281 | { |
aestes@apple.com | bbbea4d | 2010-10-08 21:57:29 +0000 | [diff] [blame] | 282 | if (!m_imageResource) |
| 283 | return; |
| 284 | |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 285 | if (documentBeingDestroyed()) |
| 286 | return; |
| 287 | |
antti@apple.com | cae24e0 | 2013-03-20 00:17:58 +0000 | [diff] [blame] | 288 | invalidateBackgroundObscurationStatus(); |
| 289 | |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 290 | #if USE(ACCELERATED_COMPOSITING) |
jchaffraix@webkit.org | 363c387 | 2012-04-17 21:51:22 +0000 | [diff] [blame] | 291 | if (newImage == m_imageResource->cachedImage()) { |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 292 | // tell any potential compositing layers |
| 293 | // that the image is done and they can reference it directly. |
jchaffraix@webkit.org | 363c387 | 2012-04-17 21:51:22 +0000 | [diff] [blame] | 294 | contentChanged(ImageChanged); |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 295 | } |
| 296 | #else |
| 297 | UNUSED_PARAM(newImage); |
| 298 | #endif |
| 299 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 300 | |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 301 | void RenderImage::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 302 | { |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 303 | LayoutUnit cWidth = contentWidth(); |
| 304 | LayoutUnit cHeight = contentHeight(); |
| 305 | LayoutUnit leftBorder = borderLeft(); |
| 306 | LayoutUnit topBorder = borderTop(); |
| 307 | LayoutUnit leftPad = paddingLeft(); |
| 308 | LayoutUnit topPad = paddingTop(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 309 | |
antti@apple.com | 7f6c5da | 2007-12-04 19:21:09 +0000 | [diff] [blame] | 310 | GraphicsContext* context = paintInfo.context; |
| 311 | |
bdakin@apple.com | b0b3eb3 | 2012-02-29 23:10:06 +0000 | [diff] [blame] | 312 | Page* page = 0; |
| 313 | if (Frame* frame = this->frame()) |
| 314 | page = frame->page(); |
| 315 | |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 316 | if (!m_imageResource->hasImage() || m_imageResource->errorOccurred()) { |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 317 | if (paintInfo.phase == PaintPhaseSelection) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 318 | return; |
| 319 | |
bdakin@apple.com | b0b3eb3 | 2012-02-29 23:10:06 +0000 | [diff] [blame] | 320 | if (page && paintInfo.phase == PaintPhaseForeground) |
| 321 | page->addRelevantUnpaintedObject(this, visualOverflowRect()); |
| 322 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 323 | if (cWidth > 2 && cHeight > 2) { |
ch.dumez@sisa.samsung.com | 6050047 | 2013-04-02 20:35:33 +0000 | [diff] [blame] | 324 | const int borderWidth = 1; |
| 325 | |
bdash | 20fe543 | 2007-10-14 11:59:35 +0000 | [diff] [blame] | 326 | // Draw an outline rect where the image should be. |
| 327 | context->setStrokeStyle(SolidStroke); |
bdakin@apple.com | 4a0593e | 2009-11-10 21:44:56 +0000 | [diff] [blame] | 328 | context->setStrokeColor(Color::lightGray, style()->colorSpace()); |
| 329 | context->setFillColor(Color::transparent, style()->colorSpace()); |
leviw@chromium.org | 9b486cd | 2012-02-11 00:58:22 +0000 | [diff] [blame] | 330 | context->drawRect(pixelSnappedIntRect(LayoutRect(paintOffset.x() + leftBorder + leftPad, paintOffset.y() + topBorder + topPad, cWidth, cHeight))); |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 331 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 332 | bool errorPictureDrawn = false; |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 333 | LayoutSize imageOffset; |
bdash | 20fe543 | 2007-10-14 11:59:35 +0000 | [diff] [blame] | 334 | // 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.com | 6050047 | 2013-04-02 20:35:33 +0000 | [diff] [blame] | 336 | LayoutUnit usableWidth = cWidth - 2 * borderWidth; |
| 337 | LayoutUnit usableHeight = cHeight - 2 * borderWidth; |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 338 | |
simon.fraser@apple.com | 9524797 | 2011-01-25 05:52:29 +0000 | [diff] [blame] | 339 | RefPtr<Image> image = m_imageResource->image(); |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 340 | |
| 341 | if (m_imageResource->errorOccurred() && !image->isNull() && usableWidth >= image->width() && usableHeight >= image->height()) { |
bdakin@apple.com | 57d71e2 | 2011-09-22 03:29:50 +0000 | [diff] [blame] | 342 | float deviceScaleFactor = WebCore::deviceScaleFactor(frame()); |
bdakin@apple.com | b49bc3d | 2011-09-10 06:28:00 +0000 | [diff] [blame] | 343 | // Call brokenImage() explicitly to ensure we get the broken image icon at the appropriate resolution. |
bdakin@apple.com | e6372a7 | 2011-09-14 18:35:49 +0000 | [diff] [blame] | 344 | pair<Image*, float> brokenImageAndImageScaleFactor = m_imageResource->cachedImage()->brokenImage(deviceScaleFactor); |
| 345 | image = brokenImageAndImageScaleFactor.first; |
bdakin@apple.com | b49bc3d | 2011-09-10 06:28:00 +0000 | [diff] [blame] | 346 | IntSize imageSize = image->size(); |
bdakin@apple.com | e6372a7 | 2011-09-14 18:35:49 +0000 | [diff] [blame] | 347 | imageSize.scale(1 / brokenImageAndImageScaleFactor.second); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 348 | // Center the error image, accounting for border and padding. |
bdakin@apple.com | b49bc3d | 2011-09-10 06:28:00 +0000 | [diff] [blame] | 349 | LayoutUnit centerX = (usableWidth - imageSize.width()) / 2; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 350 | if (centerX < 0) |
| 351 | centerX = 0; |
bdakin@apple.com | b49bc3d | 2011-09-10 06:28:00 +0000 | [diff] [blame] | 352 | LayoutUnit centerY = (usableHeight - imageSize.height()) / 2; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 353 | if (centerY < 0) |
| 354 | centerY = 0; |
ch.dumez@sisa.samsung.com | 6050047 | 2013-04-02 20:35:33 +0000 | [diff] [blame] | 355 | imageOffset = LayoutSize(leftBorder + leftPad + centerX + borderWidth, topBorder + topPad + centerY + borderWidth); |
eae@chromium.org | 88c682c | 2012-11-01 15:00:26 +0000 | [diff] [blame] | 356 | context->drawImage(image.get(), style()->colorSpace(), pixelSnappedIntRect(LayoutRect(paintOffset + imageOffset, imageSize)), CompositeSourceOver, shouldRespectImageOrientation()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 357 | errorPictureDrawn = true; |
| 358 | } |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 359 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 360 | if (!m_altText.isEmpty()) { |
hyatt@apple.com | cbe8fce | 2009-01-17 21:38:38 +0000 | [diff] [blame] | 361 | String text = document()->displayStringModifiedByEncoding(m_altText); |
hyatt@apple.com | 36bdcfa | 2010-05-21 20:56:24 +0000 | [diff] [blame] | 362 | context->setFillColor(style()->visitedDependentColor(CSSPropertyColor), style()->colorSpace()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 363 | const Font& font = style()->font(); |
zimmermann@webkit.org | e8433cc | 2011-01-22 11:46:23 +0000 | [diff] [blame] | 364 | const FontMetrics& fontMetrics = font.fontMetrics(); |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 365 | LayoutUnit ascent = fontMetrics.ascent(); |
| 366 | LayoutPoint altTextOffset = paintOffset; |
ch.dumez@sisa.samsung.com | 6050047 | 2013-04-02 20:35:33 +0000 | [diff] [blame] | 367 | altTextOffset.move(leftBorder + leftPad + (paddingWidth / 2) - borderWidth, topBorder + topPad + ascent + (paddingHeight / 2) - borderWidth); |
weinig | 0a635f0 | 2006-11-01 21:49:13 +0000 | [diff] [blame] | 368 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 369 | // 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.org | 8a7e7ff | 2011-05-24 15:27:36 +0000 | [diff] [blame] | 371 | TextRun textRun = RenderBlock::constructTextRun(this, font, text, style()); |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 372 | LayoutUnit textWidth = font.width(textRun); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 373 | if (errorPictureDrawn) { |
leviw@chromium.org | 35e0131 | 2011-06-03 02:41:09 +0000 | [diff] [blame] | 374 | if (usableWidth >= textWidth && fontMetrics.height() <= imageOffset.height()) |
| 375 | context->drawText(font, textRun, altTextOffset); |
ch.dumez@sisa.samsung.com | 6050047 | 2013-04-02 20:35:33 +0000 | [diff] [blame] | 376 | } else if (usableWidth >= textWidth && usableHeight >= fontMetrics.height()) |
leviw@chromium.org | 35e0131 | 2011-06-03 02:41:09 +0000 | [diff] [blame] | 377 | context->drawText(font, textRun, altTextOffset); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 380 | } else if (m_imageResource->hasImage() && cWidth > 0 && cHeight > 0) { |
simon.fraser@apple.com | 9524797 | 2011-01-25 05:52:29 +0000 | [diff] [blame] | 381 | RefPtr<Image> img = m_imageResource->image(cWidth, cHeight); |
bdakin@apple.com | b0b3eb3 | 2012-02-29 23:10:06 +0000 | [diff] [blame] | 382 | if (!img || img->isNull()) { |
| 383 | if (page && paintInfo.phase == PaintPhaseForeground) |
| 384 | page->addRelevantUnpaintedObject(this, visualOverflowRect()); |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame] | 385 | return; |
bdakin@apple.com | b0b3eb3 | 2012-02-29 23:10:06 +0000 | [diff] [blame] | 386 | } |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame] | 387 | |
harrison | ddf79c1 | 2006-07-13 23:17:08 +0000 | [diff] [blame] | 388 | #if PLATFORM(MAC) |
weinig | 0a635f0 | 2006-11-01 21:49:13 +0000 | [diff] [blame] | 389 | if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled()) |
leviw@chromium.org | 35e0131 | 2011-06-03 02:41:09 +0000 | [diff] [blame] | 390 | paintCustomHighlight(toPoint(paintOffset - location()), style()->highlight(), true); |
harrison | ddf79c1 | 2006-07-13 23:17:08 +0000 | [diff] [blame] | 391 | #endif |
| 392 | |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 393 | LayoutSize contentSize(cWidth, cHeight); |
| 394 | LayoutPoint contentLocation = paintOffset; |
leviw@chromium.org | 35e0131 | 2011-06-03 02:41:09 +0000 | [diff] [blame] | 395 | contentLocation.move(leftBorder + leftPad, topBorder + topPad); |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 396 | paintIntoRect(context, LayoutRect(contentLocation, contentSize)); |
bdakin@apple.com | 83e3ffb | 2012-03-08 23:24:14 +0000 | [diff] [blame] | 397 | |
| 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 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
leviw@chromium.org | b375750 | 2011-06-29 21:33:11 +0000 | [diff] [blame] | 409 | void RenderImage::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset) |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 410 | { |
leviw@chromium.org | 61ca137 | 2011-06-07 18:56:41 +0000 | [diff] [blame] | 411 | RenderReplaced::paint(paintInfo, paintOffset); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 412 | |
| 413 | if (paintInfo.phase == PaintPhaseOutline) |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 414 | paintAreaElementFocusRing(paintInfo); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 415 | } |
| 416 | |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 417 | void RenderImage::paintAreaElementFocusRing(PaintInfo& paintInfo) |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 418 | { |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 419 | Document* document = this->document(); |
| 420 | |
| 421 | if (document->printing() || !document->frame()->selection()->isFocusedAndActive()) |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 422 | return; |
| 423 | |
| 424 | if (paintInfo.context->paintingDisabled() && !paintInfo.context->updatingControlTints()) |
| 425 | return; |
| 426 | |
akling@apple.com | 2d7fedc | 2013-05-28 12:46:31 +0000 | [diff] [blame] | 427 | Element* focusedElement = document->focusedElement(); |
kangil.han@samsung.com | ac8e2487 | 2013-06-26 17:23:33 +0000 | [diff] [blame] | 428 | if (!focusedElement || !isHTMLAreaElement(focusedElement)) |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 429 | return; |
dbates@webkit.org | d0f0396 | 2010-11-30 21:57:16 +0000 | [diff] [blame] | 430 | |
kangil.han@samsung.com | ac8e2487 | 2013-06-26 17:23:33 +0000 | [diff] [blame] | 431 | HTMLAreaElement* areaElement = toHTMLAreaElement(focusedElement); |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 432 | 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.org | 10223a7 | 2011-02-10 21:04:40 +0000 | [diff] [blame] | 438 | Path path = areaElement->computePath(this); |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 439 | 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.com | 004e16e | 2011-04-03 00:01:42 +0000 | [diff] [blame] | 445 | unsigned short outlineWidth = areaElementStyle->outlineWidth(); |
| 446 | if (!outlineWidth) |
| 447 | return; |
| 448 | |
| 449 | paintInfo.context->drawFocusRing(path, outlineWidth, |
| 450 | areaElementStyle->outlineOffset(), |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 451 | areaElementStyle->visitedDependentColor(CSSPropertyOutlineColor)); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 452 | } |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 453 | |
| 454 | void 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.org | 2c32cf2 | 2011-11-04 18:27:35 +0000 | [diff] [blame] | 464 | void RenderImage::paintIntoRect(GraphicsContext* context, const LayoutRect& rect) |
eric.carlson@apple.com | a795972 | 2010-01-12 18:23:36 +0000 | [diff] [blame] | 465 | { |
leviw@chromium.org | 4b6c231 | 2012-03-21 09:32:01 +0000 | [diff] [blame] | 466 | IntRect alignedRect = pixelSnappedIntRect(rect); |
| 467 | if (!m_imageResource->hasImage() || m_imageResource->errorOccurred() || alignedRect.width() <= 0 || alignedRect.height() <= 0) |
eric.carlson@apple.com | a795972 | 2010-01-12 18:23:36 +0000 | [diff] [blame] | 468 | return; |
| 469 | |
leviw@chromium.org | 4b6c231 | 2012-03-21 09:32:01 +0000 | [diff] [blame] | 470 | RefPtr<Image> img = m_imageResource->image(alignedRect.width(), alignedRect.height()); |
eric.carlson@apple.com | a795972 | 2010-01-12 18:23:36 +0000 | [diff] [blame] | 471 | if (!img || img->isNull()) |
| 472 | return; |
| 473 | |
kangil.han@samsung.com | 2489e83 | 2013-06-29 02:41:54 +0000 | [diff] [blame] | 474 | HTMLImageElement* imageElt = (node() && isHTMLImageElement(node())) ? toHTMLImageElement(node()) : 0; |
eric.carlson@apple.com | a795972 | 2010-01-12 18:23:36 +0000 | [diff] [blame] | 475 | CompositeOperator compositeOperator = imageElt ? imageElt->compositeOperator() : CompositeSourceOver; |
senorblanco@chromium.org | 59baba5 | 2011-01-28 00:58:02 +0000 | [diff] [blame] | 476 | Image* image = m_imageResource->image().get(); |
leviw@chromium.org | 4b6c231 | 2012-03-21 09:32:01 +0000 | [diff] [blame] | 477 | bool useLowQualityScaling = shouldPaintAtLowQuality(context, image, image, alignedRect.size()); |
timothy_horton@apple.com | 04ef44a | 2012-04-06 20:02:59 +0000 | [diff] [blame] | 478 | context->drawImage(m_imageResource->image(alignedRect.width(), alignedRect.height()).get(), style()->colorSpace(), alignedRect, compositeOperator, shouldRespectImageOrientation(), useLowQualityScaling); |
eric.carlson@apple.com | a795972 | 2010-01-12 18:23:36 +0000 | [diff] [blame] | 479 | } |
| 480 | |
commit-queue@webkit.org | 77ddca6 | 2012-06-27 03:02:48 +0000 | [diff] [blame] | 481 | bool RenderImage::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance bleedAvoidance, InlineFlowBox*) const |
| 482 | { |
| 483 | if (!RenderBoxModelObject::boxShadowShouldBeAppliedToBackground(bleedAvoidance)) |
| 484 | return false; |
| 485 | |
antti@apple.com | cae24e0 | 2013-03-20 00:17:58 +0000 | [diff] [blame] | 486 | return !const_cast<RenderImage*>(this)->backgroundIsKnownToBeObscured(); |
commit-queue@webkit.org | 77ddca6 | 2012-06-27 03:02:48 +0000 | [diff] [blame] | 487 | } |
| 488 | |
antti@apple.com | 4f141bf | 2013-03-27 01:00:57 +0000 | [diff] [blame] | 489 | bool RenderImage::foregroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect, unsigned maxDepthToTest) const |
simon.fraser@apple.com | 620f56b | 2011-07-23 00:39:54 +0000 | [diff] [blame] | 490 | { |
antti@apple.com | 4f141bf | 2013-03-27 01:00:57 +0000 | [diff] [blame] | 491 | UNUSED_PARAM(maxDepthToTest); |
simon.fraser@apple.com | 620f56b | 2011-07-23 00:39:54 +0000 | [diff] [blame] | 492 | if (!m_imageResource->hasImage() || m_imageResource->errorOccurred()) |
| 493 | return false; |
simon.fraser@apple.com | 620f56b | 2011-07-23 00:39:54 +0000 | [diff] [blame] | 494 | if (m_imageResource->cachedImage() && !m_imageResource->cachedImage()->isLoaded()) |
| 495 | return false; |
antti@apple.com | 4f141bf | 2013-03-27 01:00:57 +0000 | [diff] [blame] | 496 | if (!contentBoxRect().contains(localRect)) |
| 497 | return false; |
simon.fraser@apple.com | 620f56b | 2011-07-23 00:39:54 +0000 | [diff] [blame] | 498 | EFillBox backgroundClip = style()->backgroundClip(); |
simon.fraser@apple.com | 620f56b | 2011-07-23 00:39:54 +0000 | [diff] [blame] | 499 | // Background paints under borders. |
| 500 | if (backgroundClip == BorderFillBox && style()->hasBorder() && !borderObscuresBackground()) |
| 501 | return false; |
simon.fraser@apple.com | 620f56b | 2011-07-23 00:39:54 +0000 | [diff] [blame] | 502 | // Background shows in padding area. |
| 503 | if ((backgroundClip == BorderFillBox || backgroundClip == PaddingFillBox) && style()->hasPadding()) |
| 504 | return false; |
pdr@google.com | ebc67c6 | 2013-02-01 21:31:00 +0000 | [diff] [blame] | 505 | // Check for image with alpha. |
antti@apple.com | cae24e0 | 2013-03-20 00:17:58 +0000 | [diff] [blame] | 506 | return m_imageResource->cachedImage() && m_imageResource->cachedImage()->currentFrameKnownToBeOpaque(this); |
simon.fraser@apple.com | 620f56b | 2011-07-23 00:39:54 +0000 | [diff] [blame] | 507 | } |
| 508 | |
antti@apple.com | 4f141bf | 2013-03-27 01:00:57 +0000 | [diff] [blame] | 509 | bool RenderImage::computeBackgroundIsKnownToBeObscured() |
| 510 | { |
| 511 | if (!hasBackground()) |
| 512 | return false; |
simon.fraser@apple.com | af838eb | 2013-06-16 17:24:40 +0000 | [diff] [blame] | 513 | |
| 514 | LayoutRect paintedExtent; |
| 515 | if (!getBackgroundPaintedExtent(paintedExtent)) |
| 516 | return false; |
| 517 | return foregroundIsKnownToBeOpaqueInRect(paintedExtent, 0); |
antti@apple.com | 4f141bf | 2013-03-27 01:00:57 +0000 | [diff] [blame] | 518 | } |
| 519 | |
eae@chromium.org | 88c682c | 2012-11-01 15:00:26 +0000 | [diff] [blame] | 520 | LayoutUnit RenderImage::minimumReplacedHeight() const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 521 | { |
eae@chromium.org | 88c682c | 2012-11-01 15:00:26 +0000 | [diff] [blame] | 522 | return m_imageResource->errorOccurred() ? intrinsicSize().height() : LayoutUnit(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 523 | } |
| 524 | |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 525 | HTMLMapElement* RenderImage::imageMap() const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 526 | { |
kangil.han@samsung.com | 2489e83 | 2013-06-29 02:41:54 +0000 | [diff] [blame] | 527 | HTMLImageElement* i = node() && isHTMLImageElement(node()) ? toHTMLImageElement(node()) : 0; |
rolandsteiner@chromium.org | 0142c6a | 2011-05-03 17:20:17 +0000 | [diff] [blame] | 528 | return i ? i->treeScope()->getImageMap(i->fastGetAttribute(usemapAttr)) : 0; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 529 | } |
| 530 | |
allan.jensen@nokia.com | 9a9045b | 2012-08-28 09:41:54 +0000 | [diff] [blame] | 531 | bool RenderImage::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 532 | { |
allan.jensen@nokia.com | 4c7e352 | 2012-09-03 14:41:42 +0000 | [diff] [blame] | 533 | HitTestResult tempResult(result.hitTestLocation()); |
allan.jensen@nokia.com | 9a9045b | 2012-08-28 09:41:54 +0000 | [diff] [blame] | 534 | bool inside = RenderReplaced::nodeAtPoint(request, tempResult, locationInContainer, accumulatedOffset, hitTestAction); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 535 | |
tonikitoo@webkit.org | 81c027d | 2010-07-29 13:19:43 +0000 | [diff] [blame] | 536 | if (tempResult.innerNode() && node()) { |
mitz@apple.com | ffbc6a5 | 2009-06-25 05:01:47 +0000 | [diff] [blame] | 537 | if (HTMLMapElement* map = imageMap()) { |
leviw@chromium.org | 2c32cf2 | 2011-11-04 18:27:35 +0000 | [diff] [blame] | 538 | LayoutRect contentBox = contentBoxRect(); |
eae@chromium.org | 020f587 | 2011-08-18 03:38:05 +0000 | [diff] [blame] | 539 | float scaleFactor = 1 / style()->effectiveZoom(); |
allan.jensen@nokia.com | 9a9045b | 2012-08-28 09:41:54 +0000 | [diff] [blame] | 540 | LayoutPoint mapLocation = locationInContainer.point() - toLayoutSize(accumulatedOffset) - locationOffset() - toLayoutSize(contentBox.location()); |
eae@chromium.org | 020f587 | 2011-08-18 03:38:05 +0000 | [diff] [blame] | 541 | mapLocation.scale(scaleFactor, scaleFactor); |
commit-queue@webkit.org | 227d0ab | 2012-06-20 15:41:57 +0000 | [diff] [blame] | 542 | |
eae@chromium.org | 020f587 | 2011-08-18 03:38:05 +0000 | [diff] [blame] | 543 | if (map->mapMouseEvent(mapLocation, contentBox.size(), tempResult)) |
mitz@apple.com | ffbc6a5 | 2009-06-25 05:01:47 +0000 | [diff] [blame] | 544 | tempResult.setInnerNonSharedNode(node()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
tonikitoo@webkit.org | 81c027d | 2010-07-29 13:19:43 +0000 | [diff] [blame] | 548 | if (!inside && result.isRectBasedTest()) |
| 549 | result.append(tempResult); |
simon.fraser@apple.com | e9d4e0b | 2009-03-22 19:50:18 +0000 | [diff] [blame] | 550 | if (inside) |
| 551 | result = tempResult; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 552 | return inside; |
| 553 | } |
| 554 | |
| 555 | void RenderImage::updateAltText() |
| 556 | { |
hyatt@apple.com | 7e03253 | 2009-02-11 22:06:32 +0000 | [diff] [blame] | 557 | if (!node()) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 558 | return; |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 559 | |
kangil.han@samsung.com | c559372 | 2013-06-28 02:26:41 +0000 | [diff] [blame] | 560 | if (isHTMLInputElement(node())) |
| 561 | m_altText = toHTMLInputElement(node())->altText(); |
kangil.han@samsung.com | 2489e83 | 2013-06-29 02:41:54 +0000 | [diff] [blame] | 562 | else if (isHTMLImageElement(node())) |
| 563 | m_altText = toHTMLImageElement(node())->altText(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 564 | } |
| 565 | |
zimmermann@webkit.org | 2454d643 | 2012-03-27 09:24:46 +0000 | [diff] [blame] | 566 | void RenderImage::layout() |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 567 | { |
mark.lam@apple.com | 6df1a80 | 2012-10-19 20:09:36 +0000 | [diff] [blame] | 568 | StackStats::LayoutCheckPoint layoutCheckPoint; |
zimmermann@webkit.org | 2454d643 | 2012-03-27 09:24:46 +0000 | [diff] [blame] | 569 | RenderReplaced::layout(); |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 570 | |
zimmermann@webkit.org | 2454d643 | 2012-03-27 09:24:46 +0000 | [diff] [blame] | 571 | // Propagate container size to image resource. |
| 572 | IntSize containerSize(contentWidth(), contentHeight()); |
| 573 | if (!containerSize.isEmpty()) |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 574 | m_imageResource->setContainerSizeForRenderer(containerSize); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 575 | } |
| 576 | |
zimmermann@webkit.org | 1adc559 | 2012-01-20 15:30:17 +0000 | [diff] [blame] | 577 | void RenderImage::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 578 | { |
zimmermann@webkit.org | 2454d643 | 2012-03-27 09:24:46 +0000 | [diff] [blame] | 579 | 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.com | 2f07185 | 2012-06-25 00:11:30 +0000 | [diff] [blame] | 583 | RenderObject* containingBlock = isOutOfFlowPositioned() ? container() : this->containingBlock(); |
zimmermann@webkit.org | 2454d643 | 2012-03-27 09:24:46 +0000 | [diff] [blame] | 584 | if (containingBlock->isBox()) { |
| 585 | RenderBox* box = toRenderBox(containingBlock); |
| 586 | intrinsicSize.setWidth(box->availableLogicalWidth()); |
ojan@chromium.org | 2194326 | 2013-01-04 20:45:36 +0000 | [diff] [blame] | 587 | intrinsicSize.setHeight(box->availableLogicalHeight(IncludeMarginBorderPadding)); |
zimmermann@webkit.org | 2454d643 | 2012-03-27 09:24:46 +0000 | [diff] [blame] | 588 | } |
| 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.org | 1adc559 | 2012-01-20 15:30:17 +0000 | [diff] [blame] | 593 | return; |
zimmermann@webkit.org | 2454d643 | 2012-03-27 09:24:46 +0000 | [diff] [blame] | 594 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 595 | } |
| 596 | |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 597 | bool RenderImage::needsPreferredWidthsRecalculation() const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 598 | { |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 599 | if (RenderReplaced::needsPreferredWidthsRecalculation()) |
| 600 | return true; |
| 601 | return embeddedContentBox(); |
| 602 | } |
| 603 | |
| 604 | RenderBox* RenderImage::embeddedContentBox() const |
| 605 | { |
| 606 | if (!m_imageResource) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 607 | return 0; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 608 | |
johnnyg@google.com | f4ebad6 | 2011-10-31 23:00:42 +0000 | [diff] [blame] | 609 | #if ENABLE(SVG) |
zimmermann@webkit.org | 89d03f8 | 2011-11-08 09:32:52 +0000 | [diff] [blame] | 610 | CachedImage* cachedImage = m_imageResource->cachedImage(); |
| 611 | if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage()) |
| 612 | return static_cast<SVGImage*>(cachedImage->image())->embeddedContentBox(); |
johnnyg@google.com | f4ebad6 | 2011-10-31 23:00:42 +0000 | [diff] [blame] | 613 | #endif |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 614 | |
| 615 | return 0; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 616 | } |
| 617 | |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 618 | } // namespace WebCore |