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. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 9 | * |
| 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 |
ddkilzer | c8eccec | 2007-09-26 02:29:57 +0000 | [diff] [blame] | 22 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 23 | * Boston, MA 02110-1301, USA. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "RenderImage.h" |
| 29 | |
simon.fraser@apple.com | 620f56b | 2011-07-23 00:39:54 +0000 | [diff] [blame] | 30 | #include "BitmapImage.h" |
msaboff@apple.com | 5dbcc69 | 2011-06-07 20:46:28 +0000 | [diff] [blame] | 31 | #include "FontCache.h" |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 32 | #include "Frame.h" |
rniwa@webkit.org | 78bbc94 | 2011-05-05 18:14:43 +0000 | [diff] [blame] | 33 | #include "FrameSelection.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 34 | #include "GraphicsContext.h" |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 35 | #include "HTMLAreaElement.h" |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 36 | #include "HTMLImageElement.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 37 | #include "HTMLInputElement.h" |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 38 | #include "HTMLMapElement.h" |
darin | 98fa8b8 | 2006-03-20 08:03:57 +0000 | [diff] [blame] | 39 | #include "HTMLNames.h" |
bdakin | c1a5a83 | 2006-10-28 18:28:00 +0000 | [diff] [blame] | 40 | #include "HitTestResult.h" |
hyatt | b21f837 | 2007-09-04 22:58:27 +0000 | [diff] [blame] | 41 | #include "Page.h" |
eric@webkit.org | b35848a | 2010-06-10 08:33:22 +0000 | [diff] [blame] | 42 | #include "RenderLayer.h" |
hyatt | d804834 | 2006-05-31 01:48:18 +0000 | [diff] [blame] | 43 | #include "RenderView.h" |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 44 | #include "SVGImage.h" |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 45 | #include <wtf/UnusedParam.h> |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 46 | |
darin | 7bd7095 | 2006-04-13 07:07:34 +0000 | [diff] [blame] | 47 | using namespace std; |
| 48 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 49 | namespace WebCore { |
| 50 | |
| 51 | using namespace HTMLNames; |
| 52 | |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 53 | RenderImage::RenderImage(Node* node) |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 54 | : RenderReplaced(node, IntSize()) |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 55 | , m_needsToSetSizeForAltText(false) |
antti@apple.com | 17978ce | 2011-07-13 10:23:54 +0000 | [diff] [blame] | 56 | , m_didIncrementVisuallyNonEmptyPixelCount(false) |
leo.yang@torchmobile.com.cn | ff697755 | 2011-12-02 08:05:08 +0000 | [diff] [blame] | 57 | , m_isGeneratedContent(false) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 58 | { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 59 | updateAltText(); |
| 60 | } |
| 61 | |
| 62 | RenderImage::~RenderImage() |
| 63 | { |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 64 | ASSERT(m_imageResource); |
| 65 | m_imageResource->shutdown(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 66 | } |
| 67 | |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 68 | void RenderImage::setImageResource(PassOwnPtr<RenderImageResource> imageResource) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 69 | { |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 70 | ASSERT(!m_imageResource); |
| 71 | m_imageResource = imageResource; |
| 72 | m_imageResource->initialize(this); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 73 | } |
| 74 | |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 75 | // If we'll be displaying either alt text or an image, add some padding. |
| 76 | static const unsigned short paddingWidth = 4; |
| 77 | static 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.com | 0acc935 | 2011-02-17 19:19:07 +0000 | [diff] [blame] | 81 | static const float maxAltTextWidth = 1024; |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 82 | static const int maxAltTextHeight = 256; |
| 83 | |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 84 | IntSize RenderImage::imageSizeForError(CachedImage* newImage) const |
| 85 | { |
| 86 | ASSERT_ARG(newImage, newImage); |
zimmermann@webkit.org | 8cf217f | 2011-10-14 08:08:51 +0000 | [diff] [blame] | 87 | ASSERT_ARG(newImage, newImage->imageForRenderer(this)); |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 88 | |
bdakin@apple.com | b1f426c | 2011-09-13 22:17:09 +0000 | [diff] [blame] | 89 | IntSize imageSize; |
| 90 | if (newImage->willPaintBrokenImage()) { |
bdakin@apple.com | 57d71e2 | 2011-09-22 03:29:50 +0000 | [diff] [blame] | 91 | float deviceScaleFactor = WebCore::deviceScaleFactor(frame()); |
bdakin@apple.com | e6372a7 | 2011-09-14 18:35:49 +0000 | [diff] [blame] | 92 | pair<Image*, float> brokenImageAndImageScaleFactor = newImage->brokenImage(deviceScaleFactor); |
| 93 | imageSize = brokenImageAndImageScaleFactor.first->size(); |
| 94 | imageSize.scale(1 / brokenImageAndImageScaleFactor.second); |
bdakin@apple.com | b1f426c | 2011-09-13 22:17:09 +0000 | [diff] [blame] | 95 | } else |
zimmermann@webkit.org | 8cf217f | 2011-10-14 08:08:51 +0000 | [diff] [blame] | 96 | imageSize = newImage->imageForRenderer(this)->size(); |
bdakin@apple.com | b49bc3d | 2011-09-10 06:28:00 +0000 | [diff] [blame] | 97 | |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 98 | // 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.com | b1f426c | 2011-09-13 22:17:09 +0000 | [diff] [blame] | 100 | 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] | 101 | } |
| 102 | |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 103 | // Sets the image height and width to fit the alt text. Returns true if the |
| 104 | // image size changed. |
| 105 | bool RenderImage::setImageSizeForAltText(CachedImage* newImage /* = 0 */) |
| 106 | { |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 107 | IntSize imageSize; |
zimmermann@webkit.org | 8cf217f | 2011-10-14 08:08:51 +0000 | [diff] [blame] | 108 | if (newImage && newImage->imageForRenderer(this)) |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 109 | 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); |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 113 | } |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 114 | |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 115 | // we have an alt and the user meant it (its not a text we invented) |
| 116 | if (!m_altText.isEmpty()) { |
msaboff@apple.com | 5dbcc69 | 2011-06-07 20:46:28 +0000 | [diff] [blame] | 117 | FontCachePurgePreventer fontCachePurgePreventer; |
| 118 | |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 119 | const Font& font = style()->font(); |
zimmermann@webkit.org | 8a7e7ff | 2011-05-24 15:27:36 +0000 | [diff] [blame] | 120 | IntSize textSize(min(font.width(RenderBlock::constructTextRun(this, font, m_altText, style())), maxAltTextWidth), min(font.fontMetrics().height(), maxAltTextHeight)); |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 121 | imageSize = imageSize.expandedTo(textSize); |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 122 | } |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 123 | |
darin | 751cf30 | 2007-05-01 22:10:41 +0000 | [diff] [blame] | 124 | if (imageSize == intrinsicSize()) |
| 125 | return false; |
| 126 | |
| 127 | setIntrinsicSize(imageSize); |
| 128 | return true; |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 129 | } |
| 130 | |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 131 | void 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.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 141 | void RenderImage::imageChanged(WrappedImagePtr newImage, const IntRect* rect) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 142 | { |
ap | 692e06e | 2006-10-01 09:11:35 +0000 | [diff] [blame] | 143 | if (documentBeingDestroyed()) |
| 144 | return; |
| 145 | |
dino@apple.com | 397229e | 2008-07-10 19:31:43 +0000 | [diff] [blame] | 146 | if (hasBoxDecorations() || hasMask()) |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 147 | RenderReplaced::imageChanged(newImage, rect); |
aestes@apple.com | bbbea4d | 2010-10-08 21:57:29 +0000 | [diff] [blame] | 148 | |
| 149 | if (!m_imageResource) |
| 150 | return; |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 151 | |
| 152 | if (newImage != m_imageResource->imagePtr() || !newImage) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 153 | return; |
antti@apple.com | 17978ce | 2011-07-13 10:23:54 +0000 | [diff] [blame] | 154 | |
| 155 | if (!m_didIncrementVisuallyNonEmptyPixelCount) { |
| 156 | view()->frameView()->incrementVisuallyNonEmptyPixelCount(m_imageResource->imageSize(1.0f)); |
| 157 | m_didIncrementVisuallyNonEmptyPixelCount = true; |
| 158 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 159 | |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 160 | bool imageSizeChanged = false; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 161 | |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 162 | // 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] | 163 | if (m_imageResource->errorOccurred()) { |
mitz@apple.com | 543c300 | 2010-11-16 22:08:26 +0000 | [diff] [blame] | 164 | if (!m_altText.isEmpty() && document()->isPendingStyleRecalc()) { |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 165 | ASSERT(node()); |
| 166 | if (node()) { |
| 167 | m_needsToSetSizeForAltText = true; |
| 168 | node()->setNeedsStyleRecalc(SyntheticStyleChange); |
| 169 | } |
| 170 | return; |
| 171 | } |
mitz@apple.com | 543c300 | 2010-11-16 22:08:26 +0000 | [diff] [blame] | 172 | imageSizeChanged = setImageSizeForAltText(m_imageResource->cachedImage()); |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 173 | } |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 174 | |
mitz@apple.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 175 | imageDimensionsChanged(imageSizeChanged, rect); |
| 176 | } |
| 177 | |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 178 | bool 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.com | c3d51cd | 2010-11-15 22:27:06 +0000 | [diff] [blame] | 188 | void RenderImage::imageDimensionsChanged(bool imageSizeChanged, const IntRect* rect) |
| 189 | { |
bdash | a159547 | 2007-05-02 09:17:52 +0000 | [diff] [blame] | 190 | bool shouldRepaint = true; |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 191 | if (updateIntrinsicSizeIfNeeded(m_imageResource->imageSize(style()->effectiveZoom()), imageSizeChanged)) { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 192 | // 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.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 197 | int oldwidth = width(); |
| 198 | int oldheight = height(); |
hyatt@apple.com | 75dad74 | 2010-09-24 18:07:44 +0000 | [diff] [blame] | 199 | if (!preferredLogicalWidthsDirty()) |
| 200 | setPreferredLogicalWidthsDirty(true); |
hyatt@apple.com | 5f4fae0 | 2010-09-24 06:45:16 +0000 | [diff] [blame] | 201 | computeLogicalWidth(); |
| 202 | computeLogicalHeight(); |
bdash | a159547 | 2007-05-02 09:17:52 +0000 | [diff] [blame] | 203 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 204 | if (imageSizeChanged || width() != oldwidth || height() != oldheight) { |
bdash | a159547 | 2007-05-02 09:17:52 +0000 | [diff] [blame] | 205 | shouldRepaint = false; |
| 206 | if (!selfNeedsLayout()) |
| 207 | setNeedsLayout(true); |
| 208 | } |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 209 | |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 210 | setWidth(oldwidth); |
| 211 | setHeight(oldheight); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 215 | if (shouldRepaint) { |
leviw@chromium.org | 2c32cf2 | 2011-11-04 18:27:35 +0000 | [diff] [blame] | 216 | LayoutRect repaintRect; |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 217 | 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.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 220 | 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] | 221 | // Guard against too-large changed rects. |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 222 | repaintRect.intersect(contentBoxRect()); |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 223 | } else |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 224 | repaintRect = contentBoxRect(); |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 225 | |
| 226 | repaintRectangle(repaintRect); |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 227 | |
| 228 | #if USE(ACCELERATED_COMPOSITING) |
| 229 | if (hasLayer()) { |
| 230 | // Tell any potential compositing layers that the image needs updating. |
simon.fraser@apple.com | 2f5f5c8 | 2010-12-15 00:09:58 +0000 | [diff] [blame] | 231 | layer()->contentChanged(RenderLayer::ImageChanged); |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 232 | } |
| 233 | #endif |
simon.fraser@apple.com | f7d140d | 2009-01-02 21:04:27 +0000 | [diff] [blame] | 234 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 235 | } |
| 236 | |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 237 | void RenderImage::notifyFinished(CachedResource* newImage) |
| 238 | { |
aestes@apple.com | bbbea4d | 2010-10-08 21:57:29 +0000 | [diff] [blame] | 239 | if (!m_imageResource) |
| 240 | return; |
| 241 | |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 242 | if (documentBeingDestroyed()) |
| 243 | return; |
| 244 | |
| 245 | #if USE(ACCELERATED_COMPOSITING) |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 246 | if (newImage == m_imageResource->cachedImage() && hasLayer()) { |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 247 | // tell any potential compositing layers |
| 248 | // that the image is done and they can reference it directly. |
simon.fraser@apple.com | 2f5f5c8 | 2010-12-15 00:09:58 +0000 | [diff] [blame] | 249 | layer()->contentChanged(RenderLayer::ImageChanged); |
dino@apple.com | ce3a4e0 | 2009-03-26 01:38:52 +0000 | [diff] [blame] | 250 | } |
| 251 | #else |
| 252 | UNUSED_PARAM(newImage); |
| 253 | #endif |
| 254 | } |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 255 | |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 256 | void RenderImage::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 257 | { |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 258 | LayoutUnit cWidth = contentWidth(); |
| 259 | LayoutUnit cHeight = contentHeight(); |
| 260 | LayoutUnit leftBorder = borderLeft(); |
| 261 | LayoutUnit topBorder = borderTop(); |
| 262 | LayoutUnit leftPad = paddingLeft(); |
| 263 | LayoutUnit topPad = paddingTop(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 264 | |
antti@apple.com | 7f6c5da | 2007-12-04 19:21:09 +0000 | [diff] [blame] | 265 | GraphicsContext* context = paintInfo.context; |
| 266 | |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 267 | if (!m_imageResource->hasImage() || m_imageResource->errorOccurred()) { |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 268 | if (paintInfo.phase == PaintPhaseSelection) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 269 | return; |
| 270 | |
| 271 | if (cWidth > 2 && cHeight > 2) { |
bdash | 20fe543 | 2007-10-14 11:59:35 +0000 | [diff] [blame] | 272 | // Draw an outline rect where the image should be. |
| 273 | context->setStrokeStyle(SolidStroke); |
bdakin@apple.com | 4a0593e | 2009-11-10 21:44:56 +0000 | [diff] [blame] | 274 | context->setStrokeColor(Color::lightGray, style()->colorSpace()); |
| 275 | context->setFillColor(Color::transparent, style()->colorSpace()); |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 276 | context->drawRect(LayoutRect(paintOffset.x() + leftBorder + leftPad, paintOffset.y() + topBorder + topPad, cWidth, cHeight)); |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 277 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 278 | bool errorPictureDrawn = false; |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 279 | LayoutSize imageOffset; |
bdash | 20fe543 | 2007-10-14 11:59:35 +0000 | [diff] [blame] | 280 | // 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.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 282 | LayoutUnit usableWidth = cWidth - 2; |
| 283 | LayoutUnit usableHeight = cHeight - 2; |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 284 | |
simon.fraser@apple.com | 9524797 | 2011-01-25 05:52:29 +0000 | [diff] [blame] | 285 | RefPtr<Image> image = m_imageResource->image(); |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 286 | |
| 287 | 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] | 288 | float deviceScaleFactor = WebCore::deviceScaleFactor(frame()); |
bdakin@apple.com | b49bc3d | 2011-09-10 06:28:00 +0000 | [diff] [blame] | 289 | // 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] | 290 | pair<Image*, float> brokenImageAndImageScaleFactor = m_imageResource->cachedImage()->brokenImage(deviceScaleFactor); |
| 291 | image = brokenImageAndImageScaleFactor.first; |
bdakin@apple.com | b49bc3d | 2011-09-10 06:28:00 +0000 | [diff] [blame] | 292 | IntSize imageSize = image->size(); |
bdakin@apple.com | e6372a7 | 2011-09-14 18:35:49 +0000 | [diff] [blame] | 293 | imageSize.scale(1 / brokenImageAndImageScaleFactor.second); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 294 | // Center the error image, accounting for border and padding. |
bdakin@apple.com | b49bc3d | 2011-09-10 06:28:00 +0000 | [diff] [blame] | 295 | LayoutUnit centerX = (usableWidth - imageSize.width()) / 2; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 296 | if (centerX < 0) |
| 297 | centerX = 0; |
bdakin@apple.com | b49bc3d | 2011-09-10 06:28:00 +0000 | [diff] [blame] | 298 | LayoutUnit centerY = (usableHeight - imageSize.height()) / 2; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 299 | if (centerY < 0) |
| 300 | centerY = 0; |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 301 | imageOffset = LayoutSize(leftBorder + leftPad + centerX + 1, topBorder + topPad + centerY + 1); |
bdakin@apple.com | b49bc3d | 2011-09-10 06:28:00 +0000 | [diff] [blame] | 302 | context->drawImage(image.get(), style()->colorSpace(), IntRect(paintOffset + imageOffset, imageSize)); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 303 | errorPictureDrawn = true; |
| 304 | } |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 305 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 306 | if (!m_altText.isEmpty()) { |
hyatt@apple.com | cbe8fce | 2009-01-17 21:38:38 +0000 | [diff] [blame] | 307 | String text = document()->displayStringModifiedByEncoding(m_altText); |
hyatt@apple.com | 36bdcfa | 2010-05-21 20:56:24 +0000 | [diff] [blame] | 308 | context->setFillColor(style()->visitedDependentColor(CSSPropertyColor), style()->colorSpace()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 309 | const Font& font = style()->font(); |
zimmermann@webkit.org | e8433cc | 2011-01-22 11:46:23 +0000 | [diff] [blame] | 310 | const FontMetrics& fontMetrics = font.fontMetrics(); |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 311 | LayoutUnit ascent = fontMetrics.ascent(); |
| 312 | LayoutPoint altTextOffset = paintOffset; |
leviw@chromium.org | 35e0131 | 2011-06-03 02:41:09 +0000 | [diff] [blame] | 313 | altTextOffset.move(leftBorder + leftPad, topBorder + topPad + ascent); |
weinig | 0a635f0 | 2006-11-01 21:49:13 +0000 | [diff] [blame] | 314 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 315 | // 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.org | 8a7e7ff | 2011-05-24 15:27:36 +0000 | [diff] [blame] | 317 | TextRun textRun = RenderBlock::constructTextRun(this, font, text, style()); |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 318 | LayoutUnit textWidth = font.width(textRun); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 319 | if (errorPictureDrawn) { |
leviw@chromium.org | 35e0131 | 2011-06-03 02:41:09 +0000 | [diff] [blame] | 320 | if (usableWidth >= textWidth && fontMetrics.height() <= imageOffset.height()) |
| 321 | context->drawText(font, textRun, altTextOffset); |
zimmermann@webkit.org | e8433cc | 2011-01-22 11:46:23 +0000 | [diff] [blame] | 322 | } else if (usableWidth >= textWidth && cHeight >= fontMetrics.height()) |
leviw@chromium.org | 35e0131 | 2011-06-03 02:41:09 +0000 | [diff] [blame] | 323 | context->drawText(font, textRun, altTextOffset); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 326 | } else if (m_imageResource->hasImage() && cWidth > 0 && cHeight > 0) { |
simon.fraser@apple.com | 9524797 | 2011-01-25 05:52:29 +0000 | [diff] [blame] | 327 | RefPtr<Image> img = m_imageResource->image(cWidth, cHeight); |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame] | 328 | if (!img || img->isNull()) |
| 329 | return; |
| 330 | |
harrison | ddf79c1 | 2006-07-13 23:17:08 +0000 | [diff] [blame] | 331 | #if PLATFORM(MAC) |
weinig | 0a635f0 | 2006-11-01 21:49:13 +0000 | [diff] [blame] | 332 | if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled()) |
leviw@chromium.org | 35e0131 | 2011-06-03 02:41:09 +0000 | [diff] [blame] | 333 | paintCustomHighlight(toPoint(paintOffset - location()), style()->highlight(), true); |
harrison | ddf79c1 | 2006-07-13 23:17:08 +0000 | [diff] [blame] | 334 | #endif |
| 335 | |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 336 | LayoutSize contentSize(cWidth, cHeight); |
| 337 | LayoutPoint contentLocation = paintOffset; |
leviw@chromium.org | 35e0131 | 2011-06-03 02:41:09 +0000 | [diff] [blame] | 338 | contentLocation.move(leftBorder + leftPad, topBorder + topPad); |
leviw@chromium.org | c491808 | 2011-06-29 01:06:00 +0000 | [diff] [blame] | 339 | paintIntoRect(context, LayoutRect(contentLocation, contentSize)); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
leviw@chromium.org | b375750 | 2011-06-29 21:33:11 +0000 | [diff] [blame] | 343 | void RenderImage::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset) |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 344 | { |
leviw@chromium.org | 61ca137 | 2011-06-07 18:56:41 +0000 | [diff] [blame] | 345 | RenderReplaced::paint(paintInfo, paintOffset); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 346 | |
| 347 | if (paintInfo.phase == PaintPhaseOutline) |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 348 | paintAreaElementFocusRing(paintInfo); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 349 | } |
| 350 | |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 351 | void RenderImage::paintAreaElementFocusRing(PaintInfo& paintInfo) |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 352 | { |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 353 | Document* document = this->document(); |
| 354 | |
| 355 | if (document->printing() || !document->frame()->selection()->isFocusedAndActive()) |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 356 | return; |
| 357 | |
| 358 | if (paintInfo.context->paintingDisabled() && !paintInfo.context->updatingControlTints()) |
| 359 | return; |
| 360 | |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 361 | Node* focusedNode = document->focusedNode(); |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 362 | if (!focusedNode || !focusedNode->hasTagName(areaTag)) |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 363 | return; |
dbates@webkit.org | d0f0396 | 2010-11-30 21:57:16 +0000 | [diff] [blame] | 364 | |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 365 | 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.org | 10223a7 | 2011-02-10 21:04:40 +0000 | [diff] [blame] | 372 | Path path = areaElement->computePath(this); |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 373 | 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.com | 004e16e | 2011-04-03 00:01:42 +0000 | [diff] [blame] | 379 | unsigned short outlineWidth = areaElementStyle->outlineWidth(); |
| 380 | if (!outlineWidth) |
| 381 | return; |
| 382 | |
| 383 | paintInfo.context->drawFocusRing(path, outlineWidth, |
| 384 | areaElementStyle->outlineOffset(), |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 385 | areaElementStyle->visitedDependentColor(CSSPropertyOutlineColor)); |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 386 | } |
darin@apple.com | 88b13e6 | 2011-02-01 22:49:01 +0000 | [diff] [blame] | 387 | |
| 388 | void 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.org | 2c32cf2 | 2011-11-04 18:27:35 +0000 | [diff] [blame] | 398 | void RenderImage::paintIntoRect(GraphicsContext* context, const LayoutRect& rect) |
eric.carlson@apple.com | a795972 | 2010-01-12 18:23:36 +0000 | [diff] [blame] | 399 | { |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 400 | if (!m_imageResource->hasImage() || m_imageResource->errorOccurred() || rect.width() <= 0 || rect.height() <= 0) |
eric.carlson@apple.com | a795972 | 2010-01-12 18:23:36 +0000 | [diff] [blame] | 401 | return; |
| 402 | |
simon.fraser@apple.com | 9524797 | 2011-01-25 05:52:29 +0000 | [diff] [blame] | 403 | RefPtr<Image> img = m_imageResource->image(rect.width(), rect.height()); |
eric.carlson@apple.com | a795972 | 2010-01-12 18:23:36 +0000 | [diff] [blame] | 404 | 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.org | 59baba5 | 2011-01-28 00:58:02 +0000 | [diff] [blame] | 409 | Image* image = m_imageResource->image().get(); |
| 410 | bool useLowQualityScaling = shouldPaintAtLowQuality(context, image, image, rect.size()); |
simon.fraser@apple.com | 9524797 | 2011-01-25 05:52:29 +0000 | [diff] [blame] | 411 | context->drawImage(m_imageResource->image(rect.width(), rect.height()).get(), style()->colorSpace(), rect, compositeOperator, useLowQualityScaling); |
eric.carlson@apple.com | a795972 | 2010-01-12 18:23:36 +0000 | [diff] [blame] | 412 | } |
| 413 | |
simon.fraser@apple.com | 620f56b | 2011-07-23 00:39:54 +0000 | [diff] [blame] | 414 | bool 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.com | 6820da9 | 2011-08-19 21:50:10 +0000 | [diff] [blame] | 434 | if (!image || !image->isBitmapImage() || image->currentFrameHasAlpha()) |
simon.fraser@apple.com | 620f56b | 2011-07-23 00:39:54 +0000 | [diff] [blame] | 435 | return false; |
| 436 | |
simon.fraser@apple.com | 620f56b | 2011-07-23 00:39:54 +0000 | [diff] [blame] | 437 | return true; |
| 438 | } |
| 439 | |
antti@apple.com | 7f6c5da | 2007-12-04 19:21:09 +0000 | [diff] [blame] | 440 | int RenderImage::minimumReplacedHeight() const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 441 | { |
zimmermann@webkit.org | 50d2555 | 2010-08-27 14:46:22 +0000 | [diff] [blame] | 442 | return m_imageResource->errorOccurred() ? intrinsicSize().height() : 0; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 443 | } |
| 444 | |
cfleizach@apple.com | bc08890 | 2010-01-26 18:06:41 +0000 | [diff] [blame] | 445 | HTMLMapElement* RenderImage::imageMap() const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 446 | { |
hyatt@apple.com | 7e03253 | 2009-02-11 22:06:32 +0000 | [diff] [blame] | 447 | HTMLImageElement* i = node() && node()->hasTagName(imgTag) ? static_cast<HTMLImageElement*>(node()) : 0; |
rolandsteiner@chromium.org | 0142c6a | 2011-05-03 17:20:17 +0000 | [diff] [blame] | 448 | return i ? i->treeScope()->getImageMap(i->fastGetAttribute(usemapAttr)) : 0; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 449 | } |
| 450 | |
eae@chromium.org | e14cd1a | 2011-07-06 23:38:32 +0000 | [diff] [blame] | 451 | bool RenderImage::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 452 | { |
tonikitoo@webkit.org | 6e7483d | 2010-09-28 02:28:25 +0000 | [diff] [blame] | 453 | HitTestResult tempResult(result.point(), result.topPadding(), result.rightPadding(), result.bottomPadding(), result.leftPadding()); |
eae@chromium.org | f9ce267 | 2011-06-08 03:12:08 +0000 | [diff] [blame] | 454 | bool inside = RenderReplaced::nodeAtPoint(request, tempResult, pointInContainer, accumulatedOffset, hitTestAction); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 455 | |
tonikitoo@webkit.org | 81c027d | 2010-07-29 13:19:43 +0000 | [diff] [blame] | 456 | if (tempResult.innerNode() && node()) { |
mitz@apple.com | ffbc6a5 | 2009-06-25 05:01:47 +0000 | [diff] [blame] | 457 | if (HTMLMapElement* map = imageMap()) { |
leviw@chromium.org | 2c32cf2 | 2011-11-04 18:27:35 +0000 | [diff] [blame] | 458 | LayoutRect contentBox = contentBoxRect(); |
eae@chromium.org | 020f587 | 2011-08-18 03:38:05 +0000 | [diff] [blame] | 459 | 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.com | ffbc6a5 | 2009-06-25 05:01:47 +0000 | [diff] [blame] | 464 | tempResult.setInnerNonSharedNode(node()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
tonikitoo@webkit.org | 81c027d | 2010-07-29 13:19:43 +0000 | [diff] [blame] | 468 | if (!inside && result.isRectBasedTest()) |
| 469 | result.append(tempResult); |
simon.fraser@apple.com | e9d4e0b | 2009-03-22 19:50:18 +0000 | [diff] [blame] | 470 | if (inside) |
| 471 | result = tempResult; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 472 | return inside; |
| 473 | } |
| 474 | |
| 475 | void RenderImage::updateAltText() |
| 476 | { |
hyatt@apple.com | 7e03253 | 2009-02-11 22:06:32 +0000 | [diff] [blame] | 477 | if (!node()) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 478 | return; |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 479 | |
hyatt@apple.com | 7e03253 | 2009-02-11 22:06:32 +0000 | [diff] [blame] | 480 | 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(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 484 | } |
| 485 | |
leviw@chromium.org | 393fc7f | 2011-06-29 23:59:49 +0000 | [diff] [blame] | 486 | LayoutUnit RenderImage::computeReplacedLogicalWidth(bool includeMaxWidth) const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 487 | { |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 488 | // 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.org | 2c32cf2 | 2011-11-04 18:27:35 +0000 | [diff] [blame] | 495 | IntSize containerSize; |
zimmermann@webkit.org | 1adc559 | 2012-01-20 15:30:17 +0000 | [diff] [blame^] | 496 | if (m_imageResource->imageHasRelativeWidth() || m_imageResource->imageHasRelativeHeight()) { |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 497 | // 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.org | 2c32cf2 | 2011-11-04 18:27:35 +0000 | [diff] [blame] | 501 | containerSize = IntSize(box->availableWidth(), box->availableHeight()); // Already contains zooming information. |
hyatt@apple.com | d885df7 | 2009-01-22 02:31:52 +0000 | [diff] [blame] | 502 | } |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 503 | } 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.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame] | 513 | |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 514 | if (!containerSize.isEmpty()) { |
| 515 | m_imageResource->setContainerSizeForRenderer(containerSize); |
| 516 | const_cast<RenderImage*>(this)->updateIntrinsicSizeIfNeeded(containerSize, false); |
| 517 | } |
jchaffraix@webkit.org | 825bcab | 2011-10-26 18:58:59 +0000 | [diff] [blame] | 518 | |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 519 | return RenderReplaced::computeReplacedLogicalWidth(includeMaxWidth); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 520 | } |
| 521 | |
zimmermann@webkit.org | 1adc559 | 2012-01-20 15:30:17 +0000 | [diff] [blame^] | 522 | void RenderImage::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 523 | { |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 524 | // 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.org | 1adc559 | 2012-01-20 15:30:17 +0000 | [diff] [blame^] | 528 | if (!cachedImage || !cachedImage->image()) |
| 529 | return; |
| 530 | intrinsicSize = cachedImage->image()->size(); |
| 531 | intrinsicRatio = intrinsicSize.width() / static_cast<double>(intrinsicSize.height()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 532 | } |
| 533 | |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 534 | bool RenderImage::needsPreferredWidthsRecalculation() const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 535 | { |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 536 | if (RenderReplaced::needsPreferredWidthsRecalculation()) |
| 537 | return true; |
| 538 | return embeddedContentBox(); |
| 539 | } |
| 540 | |
| 541 | RenderBox* RenderImage::embeddedContentBox() const |
| 542 | { |
| 543 | if (!m_imageResource) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 544 | return 0; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 545 | |
johnnyg@google.com | f4ebad6 | 2011-10-31 23:00:42 +0000 | [diff] [blame] | 546 | #if ENABLE(SVG) |
zimmermann@webkit.org | 89d03f8 | 2011-11-08 09:32:52 +0000 | [diff] [blame] | 547 | CachedImage* cachedImage = m_imageResource->cachedImage(); |
| 548 | if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage()) |
| 549 | return static_cast<SVGImage*>(cachedImage->image())->embeddedContentBox(); |
johnnyg@google.com | f4ebad6 | 2011-10-31 23:00:42 +0000 | [diff] [blame] | 550 | #endif |
zimmermann@webkit.org | 4729baa | 2011-10-31 14:07:49 +0000 | [diff] [blame] | 551 | |
| 552 | return 0; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 553 | } |
| 554 | |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 555 | } // namespace WebCore |