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 | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 7 | * Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 8 | * |
| 9 | * This library is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU Library General Public |
| 11 | * License as published by the Free Software Foundation; either |
| 12 | * version 2 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * Library General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Library General Public License |
| 20 | * along with this library; see the file COPYING.LIB. If not, write to |
ddkilzer | c8eccec | 2007-09-26 02:29:57 +0000 | [diff] [blame] | 21 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 22 | * Boston, MA 02110-1301, USA. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "RenderImage.h" |
| 28 | |
eseidel | a844579 | 2006-12-28 00:48:37 +0000 | [diff] [blame] | 29 | #include "BitmapImage.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 30 | #include "Document.h" |
| 31 | #include "GraphicsContext.h" |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 32 | #include "HTMLImageElement.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 33 | #include "HTMLInputElement.h" |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 34 | #include "HTMLMapElement.h" |
darin | 98fa8b8 | 2006-03-20 08:03:57 +0000 | [diff] [blame] | 35 | #include "HTMLNames.h" |
bdakin | c1a5a83 | 2006-10-28 18:28:00 +0000 | [diff] [blame] | 36 | #include "HitTestResult.h" |
hyatt | b21f837 | 2007-09-04 22:58:27 +0000 | [diff] [blame] | 37 | #include "Page.h" |
hyatt | d804834 | 2006-05-31 01:48:18 +0000 | [diff] [blame] | 38 | #include "RenderView.h" |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 39 | |
darin | 7bd7095 | 2006-04-13 07:07:34 +0000 | [diff] [blame] | 40 | using namespace std; |
| 41 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 42 | namespace WebCore { |
| 43 | |
| 44 | using namespace HTMLNames; |
| 45 | |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 46 | RenderImage::RenderImage(Node* node) |
darin | 751cf30 | 2007-05-01 22:10:41 +0000 | [diff] [blame] | 47 | : RenderReplaced(node, IntSize(0, 0)) |
eseidel | 8494362 | 2006-05-15 23:23:42 +0000 | [diff] [blame] | 48 | , m_cachedImage(0) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 49 | { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 50 | updateAltText(); |
| 51 | } |
| 52 | |
| 53 | RenderImage::~RenderImage() |
| 54 | { |
| 55 | if (m_cachedImage) |
hyatt@apple.com | 2c814c4 | 2008-04-12 04:09:22 +0000 | [diff] [blame] | 56 | m_cachedImage->removeClient(this); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 57 | } |
| 58 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 59 | void RenderImage::setCachedImage(CachedImage* newImage) |
| 60 | { |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame^] | 61 | if (m_cachedImage == newImage) |
bdakin | eb2c204 | 2006-07-10 11:39:35 +0000 | [diff] [blame] | 62 | return; |
darin | ec37548 | 2007-01-06 01:36:24 +0000 | [diff] [blame] | 63 | if (m_cachedImage) |
hyatt@apple.com | 2c814c4 | 2008-04-12 04:09:22 +0000 | [diff] [blame] | 64 | m_cachedImage->removeClient(this); |
darin | ec37548 | 2007-01-06 01:36:24 +0000 | [diff] [blame] | 65 | m_cachedImage = newImage; |
| 66 | if (m_cachedImage) { |
hyatt@apple.com | 2c814c4 | 2008-04-12 04:09:22 +0000 | [diff] [blame] | 67 | m_cachedImage->addClient(this); |
kmccullo | adfd67d | 2007-03-03 02:18:43 +0000 | [diff] [blame] | 68 | if (m_cachedImage->errorOccurred()) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 69 | imageChanged(m_cachedImage); |
| 70 | } |
| 71 | } |
| 72 | |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 73 | // If we'll be displaying either alt text or an image, add some padding. |
| 74 | static const unsigned short paddingWidth = 4; |
| 75 | static const unsigned short paddingHeight = 4; |
| 76 | |
| 77 | // Alt text is restricted to this maximum size, in pixels. These are |
| 78 | // signed integers because they are compared with other signed values. |
| 79 | static const int maxAltTextWidth = 1024; |
| 80 | static const int maxAltTextHeight = 256; |
| 81 | |
| 82 | // Sets the image height and width to fit the alt text. Returns true if the |
| 83 | // image size changed. |
| 84 | bool RenderImage::setImageSizeForAltText(CachedImage* newImage /* = 0 */) |
| 85 | { |
| 86 | int imageWidth = 0; |
| 87 | int imageHeight = 0; |
| 88 | |
| 89 | // If we'll be displaying either text or an image, add a little padding. |
| 90 | if (!m_altText.isEmpty() || newImage) { |
| 91 | imageWidth = paddingWidth; |
| 92 | imageHeight = paddingHeight; |
| 93 | } |
| 94 | |
| 95 | if (newImage) { |
hyatt@apple.com | a803148 | 2008-03-19 17:58:22 +0000 | [diff] [blame] | 96 | // imageSize() returns 0 for the error image. We need the true size of the |
| 97 | // error image, so we have to get it by grabbing image() directly. |
| 98 | imageWidth += newImage->image()->width() * style()->effectiveZoom(); |
| 99 | imageHeight += newImage->image()->height() * style()->effectiveZoom(); |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // we have an alt and the user meant it (its not a text we invented) |
| 103 | if (!m_altText.isEmpty()) { |
| 104 | const Font& font = style()->font(); |
| 105 | imageWidth = max(imageWidth, min(font.width(TextRun(m_altText.characters(), m_altText.length())), maxAltTextWidth)); |
| 106 | imageHeight = max(imageHeight, min(font.height(), maxAltTextHeight)); |
| 107 | } |
| 108 | |
darin | 751cf30 | 2007-05-01 22:10:41 +0000 | [diff] [blame] | 109 | IntSize imageSize = IntSize(imageWidth, imageHeight); |
| 110 | if (imageSize == intrinsicSize()) |
| 111 | return false; |
| 112 | |
| 113 | setIntrinsicSize(imageSize); |
| 114 | return true; |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 115 | } |
| 116 | |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 117 | void RenderImage::imageChanged(CachedImage* newImage) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 118 | { |
ap | 692e06e | 2006-10-01 09:11:35 +0000 | [diff] [blame] | 119 | if (documentBeingDestroyed()) |
| 120 | return; |
| 121 | |
hyatt | 0fd84c8 | 2007-03-17 23:39:50 +0000 | [diff] [blame] | 122 | if (hasBoxDecorations()) |
| 123 | RenderReplaced::imageChanged(newImage); |
| 124 | |
hyatt@apple.com | a803148 | 2008-03-19 17:58:22 +0000 | [diff] [blame] | 125 | if (newImage != m_cachedImage || !newImage) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 126 | return; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 127 | |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 128 | bool imageSizeChanged = false; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 129 | |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 130 | // Set image dimensions, taking into account the size of the alt text. |
kmccullo | adfd67d | 2007-03-03 02:18:43 +0000 | [diff] [blame] | 131 | if (newImage->errorOccurred()) |
bdash | 58feef6 | 2007-01-11 02:24:30 +0000 | [diff] [blame] | 132 | imageSizeChanged = setImageSizeForAltText(newImage); |
| 133 | |
bdash | a159547 | 2007-05-02 09:17:52 +0000 | [diff] [blame] | 134 | bool shouldRepaint = true; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 135 | |
| 136 | // Image dimensions have been changed, see what needs to be done |
hyatt@apple.com | a803148 | 2008-03-19 17:58:22 +0000 | [diff] [blame] | 137 | if (newImage->imageSize(style()->effectiveZoom()) != intrinsicSize() || imageSizeChanged) { |
darin | 751cf30 | 2007-05-01 22:10:41 +0000 | [diff] [blame] | 138 | if (!newImage->errorOccurred()) |
hyatt@apple.com | a803148 | 2008-03-19 17:58:22 +0000 | [diff] [blame] | 139 | setIntrinsicSize(newImage->imageSize(style()->effectiveZoom())); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 140 | |
| 141 | // In the case of generated image content using :before/:after, we might not be in the |
| 142 | // render tree yet. In that case, we don't need to worry about check for layout, since we'll get a |
| 143 | // layout when we get added in to the render tree hierarchy later. |
| 144 | if (containingBlock()) { |
| 145 | // lets see if we need to relayout at all.. |
| 146 | int oldwidth = m_width; |
| 147 | int oldheight = m_height; |
bdash | a159547 | 2007-05-02 09:17:52 +0000 | [diff] [blame] | 148 | if (!prefWidthsDirty()) |
| 149 | setPrefWidthsDirty(true); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 150 | calcWidth(); |
| 151 | calcHeight(); |
bdash | a159547 | 2007-05-02 09:17:52 +0000 | [diff] [blame] | 152 | |
| 153 | if (imageSizeChanged || m_width != oldwidth || m_height != oldheight) { |
| 154 | shouldRepaint = false; |
| 155 | if (!selfNeedsLayout()) |
| 156 | setNeedsLayout(true); |
| 157 | } |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 158 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 159 | m_width = oldwidth; |
| 160 | m_height = oldheight; |
| 161 | } |
| 162 | } |
| 163 | |
bdash | a159547 | 2007-05-02 09:17:52 +0000 | [diff] [blame] | 164 | if (shouldRepaint) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 165 | // FIXME: We always just do a complete repaint, since we always pass in the full image |
| 166 | // rect at the moment anyway. |
bdash | a159547 | 2007-05-02 09:17:52 +0000 | [diff] [blame] | 167 | repaintRectangle(contentBox()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void RenderImage::resetAnimation() |
| 171 | { |
| 172 | if (m_cachedImage) { |
| 173 | image()->resetAnimation(); |
| 174 | if (!needsLayout()) |
| 175 | repaint(); |
| 176 | } |
| 177 | } |
| 178 | |
antti@apple.com | 7f6c5da | 2007-12-04 19:21:09 +0000 | [diff] [blame] | 179 | void RenderImage::paintReplaced(PaintInfo& paintInfo, int tx, int ty) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 180 | { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 181 | int cWidth = contentWidth(); |
| 182 | int cHeight = contentHeight(); |
| 183 | int leftBorder = borderLeft(); |
| 184 | int topBorder = borderTop(); |
| 185 | int leftPad = paddingLeft(); |
| 186 | int topPad = paddingTop(); |
| 187 | |
antti@apple.com | 7f6c5da | 2007-12-04 19:21:09 +0000 | [diff] [blame] | 188 | if (document()->printing() && !view()->printImages()) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 189 | return; |
| 190 | |
antti@apple.com | 7f6c5da | 2007-12-04 19:21:09 +0000 | [diff] [blame] | 191 | GraphicsContext* context = paintInfo.context; |
| 192 | |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame^] | 193 | if (!hasImage() || errorOccurred()) { |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 194 | if (paintInfo.phase == PaintPhaseSelection) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 195 | return; |
| 196 | |
| 197 | if (cWidth > 2 && cHeight > 2) { |
bdash | 20fe543 | 2007-10-14 11:59:35 +0000 | [diff] [blame] | 198 | // Draw an outline rect where the image should be. |
| 199 | context->setStrokeStyle(SolidStroke); |
| 200 | context->setStrokeColor(Color::lightGray); |
| 201 | context->setFillColor(Color::transparent); |
| 202 | context->drawRect(IntRect(tx + leftBorder + leftPad, ty + topBorder + topPad, cWidth, cHeight)); |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 203 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 204 | bool errorPictureDrawn = false; |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 205 | int imageX = 0; |
| 206 | int imageY = 0; |
bdash | 20fe543 | 2007-10-14 11:59:35 +0000 | [diff] [blame] | 207 | // When calculating the usable dimensions, exclude the pixels of |
| 208 | // the ouline rect so the error image/alt text doesn't draw on it. |
| 209 | int usableWidth = cWidth - 2; |
| 210 | int usableHeight = cHeight - 2; |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 211 | |
kmccullo | adfd67d | 2007-03-03 02:18:43 +0000 | [diff] [blame] | 212 | if (errorOccurred() && !image()->isNull() && (usableWidth >= image()->width()) && (usableHeight >= image()->height())) { |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 213 | // Center the error image, accounting for border and padding. |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 214 | int centerX = (usableWidth - image()->width()) / 2; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 215 | if (centerX < 0) |
| 216 | centerX = 0; |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 217 | int centerY = (usableHeight - image()->height()) / 2; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 218 | if (centerY < 0) |
| 219 | centerY = 0; |
mitz@apple.com | c547c0f | 2007-11-26 01:06:27 +0000 | [diff] [blame] | 220 | imageX = leftBorder + leftPad + centerX + 1; |
| 221 | imageY = topBorder + topPad + centerY + 1; |
weinig | 0a635f0 | 2006-11-01 21:49:13 +0000 | [diff] [blame] | 222 | context->drawImage(image(), IntPoint(tx + imageX, ty + imageY)); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 223 | errorPictureDrawn = true; |
| 224 | } |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 225 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 226 | if (!m_altText.isEmpty()) { |
darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 227 | String text = m_altText; |
darin | 7ab3109 | 2006-05-10 04:59:57 +0000 | [diff] [blame] | 228 | text.replace('\\', backslashAsCurrencySymbol()); |
weinig | 0a635f0 | 2006-11-01 21:49:13 +0000 | [diff] [blame] | 229 | context->setFont(style()->font()); |
hyatt | 6ce9862 | 2006-12-19 10:20:38 +0000 | [diff] [blame] | 230 | context->setFillColor(style()->color()); |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 231 | int ax = tx + leftBorder + leftPad; |
| 232 | int ay = ty + topBorder + topPad; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 233 | const Font& font = style()->font(); |
| 234 | int ascent = font.ascent(); |
weinig | 0a635f0 | 2006-11-01 21:49:13 +0000 | [diff] [blame] | 235 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 236 | // Only draw the alt text if it'll fit within the content box, |
| 237 | // and only if it fits above the error image. |
darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 238 | TextRun textRun(text.characters(), text.length()); |
hyatt | 43d6c79 | 2006-05-11 10:19:34 +0000 | [diff] [blame] | 239 | int textWidth = font.width(textRun); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 240 | if (errorPictureDrawn) { |
| 241 | if (usableWidth >= textWidth && font.height() <= imageY) |
weinig | 0a635f0 | 2006-11-01 21:49:13 +0000 | [diff] [blame] | 242 | context->drawText(textRun, IntPoint(ax, ay + ascent)); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 243 | } else if (usableWidth >= textWidth && cHeight >= font.height()) |
weinig | 0a635f0 | 2006-11-01 21:49:13 +0000 | [diff] [blame] | 244 | context->drawText(textRun, IntPoint(ax, ay + ascent)); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame^] | 247 | } else if (hasImage() && cWidth > 0 && cHeight > 0) { |
| 248 | Image* img = image(cWidth, cHeight); |
| 249 | if (!img || img->isNull()) |
| 250 | return; |
| 251 | |
harrison | ddf79c1 | 2006-07-13 23:17:08 +0000 | [diff] [blame] | 252 | #if PLATFORM(MAC) |
weinig | 0a635f0 | 2006-11-01 21:49:13 +0000 | [diff] [blame] | 253 | if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled()) |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 254 | paintCustomHighlight(tx - m_x, ty - m_y, style()->highlight(), true); |
harrison | ddf79c1 | 2006-07-13 23:17:08 +0000 | [diff] [blame] | 255 | #endif |
| 256 | |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 257 | IntRect rect(IntPoint(tx + leftBorder + leftPad, ty + topBorder + topPad), IntSize(cWidth, cHeight)); |
| 258 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 259 | HTMLImageElement* imageElt = (element() && element()->hasTagName(imgTag)) ? static_cast<HTMLImageElement*>(element()) : 0; |
darin | 33137f7 | 2006-04-21 06:52:11 +0000 | [diff] [blame] | 260 | CompositeOperator compositeOperator = imageElt ? imageElt->compositeOperator() : CompositeSourceOver; |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame^] | 261 | context->drawImage(image(cWidth, cHeight), rect, compositeOperator, document()->page()->inLowQualityImageInterpolationMode()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
antti@apple.com | 7f6c5da | 2007-12-04 19:21:09 +0000 | [diff] [blame] | 265 | int RenderImage::minimumReplacedHeight() const |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 266 | { |
antti@apple.com | 7f6c5da | 2007-12-04 19:21:09 +0000 | [diff] [blame] | 267 | return errorOccurred() ? intrinsicSize().height() : 0; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | HTMLMapElement* RenderImage::imageMap() |
| 271 | { |
| 272 | HTMLImageElement* i = element() && element()->hasTagName(imgTag) ? static_cast<HTMLImageElement*>(element()) : 0; |
darin@apple.com | 640fa30 | 2008-02-15 05:03:55 +0000 | [diff] [blame] | 273 | return i ? i->document()->getImageMap(i->useMap()) : 0; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 274 | } |
| 275 | |
bdakin | c4d8acc | 2006-10-31 21:48:20 +0000 | [diff] [blame] | 276 | bool RenderImage::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int _x, int _y, int _tx, int _ty, HitTestAction hitTestAction) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 277 | { |
bdakin | c4d8acc | 2006-10-31 21:48:20 +0000 | [diff] [blame] | 278 | bool inside = RenderReplaced::nodeAtPoint(request, result, _x, _y, _tx, _ty, hitTestAction); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 279 | |
| 280 | if (inside && element()) { |
| 281 | int tx = _tx + m_x; |
| 282 | int ty = _ty + m_y; |
| 283 | |
| 284 | HTMLMapElement* map = imageMap(); |
| 285 | if (map) { |
| 286 | // we're a client side image map |
bdakin | 1c628c0 | 2006-10-29 22:07:47 +0000 | [diff] [blame] | 287 | inside = map->mapMouseEvent(_x - tx, _y - ty, IntSize(contentWidth(), contentHeight()), result); |
| 288 | result.setInnerNonSharedNode(element()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | return inside; |
| 293 | } |
| 294 | |
| 295 | void RenderImage::updateAltText() |
| 296 | { |
| 297 | if (!element()) |
| 298 | return; |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 299 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 300 | if (element()->hasTagName(inputTag)) |
| 301 | m_altText = static_cast<HTMLInputElement*>(element())->altText(); |
| 302 | else if (element()->hasTagName(imgTag)) |
| 303 | m_altText = static_cast<HTMLImageElement*>(element())->altText(); |
| 304 | } |
| 305 | |
| 306 | bool RenderImage::isWidthSpecified() const |
| 307 | { |
| 308 | switch (style()->width().type()) { |
| 309 | case Fixed: |
| 310 | case Percent: |
| 311 | return true; |
antti@apple.com | 8fd3ecc | 2008-01-18 19:51:25 +0000 | [diff] [blame] | 312 | case Auto: |
| 313 | case Relative: // FIXME: Shouldn't this case return true? |
| 314 | case Static: |
| 315 | case Intrinsic: |
| 316 | case MinIntrinsic: |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 317 | return false; |
| 318 | } |
antti@apple.com | 8fd3ecc | 2008-01-18 19:51:25 +0000 | [diff] [blame] | 319 | ASSERT(false); |
| 320 | return false; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | bool RenderImage::isHeightSpecified() const |
| 324 | { |
| 325 | switch (style()->height().type()) { |
| 326 | case Fixed: |
| 327 | case Percent: |
| 328 | return true; |
antti@apple.com | 8fd3ecc | 2008-01-18 19:51:25 +0000 | [diff] [blame] | 329 | case Auto: |
| 330 | case Relative: // FIXME: Shouldn't this case return true? |
| 331 | case Static: |
| 332 | case Intrinsic: |
| 333 | case MinIntrinsic: |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 334 | return false; |
| 335 | } |
antti@apple.com | 8fd3ecc | 2008-01-18 19:51:25 +0000 | [diff] [blame] | 336 | ASSERT(false); |
| 337 | return false; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | int RenderImage::calcReplacedWidth() const |
| 341 | { |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame^] | 342 | if (imageHasRelativeWidth()) |
bdakin@apple.com | 05d7e8e | 2007-12-19 07:53:52 +0000 | [diff] [blame] | 343 | if (RenderObject* cb = isPositioned() ? container() : containingBlock()) |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame^] | 344 | setImageContainerSize(IntSize(cb->availableWidth(), cb->availableHeight())); |
| 345 | |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 346 | int width; |
| 347 | if (isWidthSpecified()) |
adele | c1116ca | 2006-06-01 05:28:13 +0000 | [diff] [blame] | 348 | width = calcReplacedWidthUsing(style()->width()); |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame^] | 349 | else if (usesImageContainerSize()) |
| 350 | width = imageSize(style()->effectiveZoom()).width(); |
| 351 | else if (imageHasRelativeWidth()) |
bdakin@apple.com | 05d7e8e | 2007-12-19 07:53:52 +0000 | [diff] [blame] | 352 | width = 0; // If the image is relatively-sized, set the width to 0 until there is a set container size. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 353 | else |
| 354 | width = calcAspectRatioWidth(); |
| 355 | |
adele | c1116ca | 2006-06-01 05:28:13 +0000 | [diff] [blame] | 356 | int minW = calcReplacedWidthUsing(style()->minWidth()); |
ddkilzer | d1687ca | 2007-01-04 05:28:35 +0000 | [diff] [blame] | 357 | int maxW = style()->maxWidth().isUndefined() ? width : calcReplacedWidthUsing(style()->maxWidth()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 358 | |
darin | 7bd7095 | 2006-04-13 07:07:34 +0000 | [diff] [blame] | 359 | return max(minW, min(width, maxW)); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | int RenderImage::calcReplacedHeight() const |
| 363 | { |
| 364 | int height; |
| 365 | if (isHeightSpecified()) |
adele | c1116ca | 2006-06-01 05:28:13 +0000 | [diff] [blame] | 366 | height = calcReplacedHeightUsing(style()->height()); |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame^] | 367 | else if (usesImageContainerSize()) |
| 368 | height = imageSize(style()->effectiveZoom()).height(); |
| 369 | else if (imageHasRelativeHeight()) |
bdakin@apple.com | 05d7e8e | 2007-12-19 07:53:52 +0000 | [diff] [blame] | 370 | height = 0; // If the image is relatively-sized, set the height to 0 until there is a set container size. |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 371 | else |
| 372 | height = calcAspectRatioHeight(); |
| 373 | |
adele | c1116ca | 2006-06-01 05:28:13 +0000 | [diff] [blame] | 374 | int minH = calcReplacedHeightUsing(style()->minHeight()); |
ddkilzer | d1687ca | 2007-01-04 05:28:35 +0000 | [diff] [blame] | 375 | int maxH = style()->maxHeight().isUndefined() ? height : calcReplacedHeightUsing(style()->maxHeight()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 376 | |
darin | 7bd7095 | 2006-04-13 07:07:34 +0000 | [diff] [blame] | 377 | return max(minH, min(height, maxH)); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | int RenderImage::calcAspectRatioWidth() const |
| 381 | { |
darin | 751cf30 | 2007-05-01 22:10:41 +0000 | [diff] [blame] | 382 | IntSize size = intrinsicSize(); |
| 383 | if (!size.height()) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 384 | return 0; |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame^] | 385 | if (!hasImage() || errorOccurred()) |
darin | 751cf30 | 2007-05-01 22:10:41 +0000 | [diff] [blame] | 386 | return size.width(); // Don't bother scaling. |
| 387 | return RenderReplaced::calcReplacedHeight() * size.width() / size.height(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | int RenderImage::calcAspectRatioHeight() const |
| 391 | { |
darin | 751cf30 | 2007-05-01 22:10:41 +0000 | [diff] [blame] | 392 | IntSize size = intrinsicSize(); |
| 393 | if (!size.width()) |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 394 | return 0; |
hyatt@apple.com | 82b7dbb | 2008-04-14 19:28:53 +0000 | [diff] [blame^] | 395 | if (!hasImage() || errorOccurred()) |
darin | 751cf30 | 2007-05-01 22:10:41 +0000 | [diff] [blame] | 396 | return size.height(); // Don't bother scaling. |
| 397 | return RenderReplaced::calcReplacedWidth() * size.height() / size.width(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 398 | } |
| 399 | |
darin | 7e7c8e4 | 2007-04-25 01:14:03 +0000 | [diff] [blame] | 400 | void RenderImage::calcPrefWidths() |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 401 | { |
darin | 7e7c8e4 | 2007-04-25 01:14:03 +0000 | [diff] [blame] | 402 | ASSERT(prefWidthsDirty()); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 403 | |
darin | 7e7c8e4 | 2007-04-25 01:14:03 +0000 | [diff] [blame] | 404 | m_maxPrefWidth = calcReplacedWidth() + paddingLeft() + paddingRight() + borderLeft() + borderRight(); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 405 | |
| 406 | if (style()->width().isPercent() || style()->height().isPercent() || |
| 407 | style()->maxWidth().isPercent() || style()->maxHeight().isPercent() || |
| 408 | style()->minWidth().isPercent() || style()->minHeight().isPercent()) |
darin | 7e7c8e4 | 2007-04-25 01:14:03 +0000 | [diff] [blame] | 409 | m_minPrefWidth = 0; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 410 | else |
darin | 7e7c8e4 | 2007-04-25 01:14:03 +0000 | [diff] [blame] | 411 | m_minPrefWidth = m_maxPrefWidth; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 412 | |
darin | 7e7c8e4 | 2007-04-25 01:14:03 +0000 | [diff] [blame] | 413 | setPrefWidthsDirty(false); |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | Image* RenderImage::nullImage() |
| 417 | { |
eseidel | a844579 | 2006-12-28 00:48:37 +0000 | [diff] [blame] | 418 | static BitmapImage sharedNullImage; |
darin | b9481ed | 2006-03-20 02:57:59 +0000 | [diff] [blame] | 419 | return &sharedNullImage; |
| 420 | } |
| 421 | |
weinig | c24ab18 | 2006-10-30 22:41:29 +0000 | [diff] [blame] | 422 | } // namespace WebCore |