1/* 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) 7 * Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved. 8 * Copyright (C) 2010 Google Inc. All rights reserved. 9 * Copyright (C) Research In Motion Limited 2011-2012. All rights reserved. 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 23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 24 * Boston, MA 02110-1301, USA. 25 * 26 */ 27 28#include "config.h" 29#include "RenderImage.h" 30 31#include "BitmapImage.h" 32#include "CachedImage.h" 33#include "Font.h" 34#include "FontCache.h" 35#include "Frame.h" 36#include "FrameSelection.h" 37#include "GraphicsContext.h" 38#include "HTMLAreaElement.h" 39#include "HTMLImageElement.h" 40#include "HTMLInputElement.h" 41#include "HTMLMapElement.h" 42#include "HTMLNames.h" 43#include "HitTestResult.h" 44#include "Page.h" 45#include "PaintInfo.h" 46#include "RenderView.h" 47#include "SVGImage.h" 48#include <wtf/StackStats.h> 49 50using namespace std; 51 52namespace WebCore { 53 54using namespace HTMLNames; 55 56RenderImage::RenderImage(Element* element) 57 : RenderReplaced(element, IntSize()) 58 , m_needsToSetSizeForAltText(false) 59 , m_didIncrementVisuallyNonEmptyPixelCount(false) 60 , m_isGeneratedContent(false) 61{ 62 updateAltText(); 63} 64 65RenderImage* RenderImage::createAnonymous(Document* document) 66{ 67 RenderImage* image = new (document->renderArena()) RenderImage(0); 68 image->setDocumentForAnonymous(document); 69 return image; 70} 71 72RenderImage::~RenderImage() 73{ 74 ASSERT(m_imageResource); 75 m_imageResource->shutdown(); 76} 77 78void RenderImage::setImageResource(PassOwnPtr<RenderImageResource> imageResource) 79{ 80 ASSERT(!m_imageResource); 81 m_imageResource = imageResource; 82 m_imageResource->initialize(this); 83} 84 85// If we'll be displaying either alt text or an image, add some padding. 86static const unsigned short paddingWidth = 4; 87static 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. 91static const float maxAltTextWidth = 1024; 92static const int maxAltTextHeight = 256; 93 94IntSize RenderImage::imageSizeForError(CachedImage* newImage) const 95{ 96 ASSERT_ARG(newImage, newImage); 97 ASSERT_ARG(newImage, newImage->imageForRenderer(this)); 98 99 IntSize imageSize; 100 if (newImage->willPaintBrokenImage()) { 101 float deviceScaleFactor = WebCore::deviceScaleFactor(frame()); 102 pair<Image*, float> brokenImageAndImageScaleFactor = newImage->brokenImage(deviceScaleFactor); 103 imageSize = brokenImageAndImageScaleFactor.first->size(); 104 imageSize.scale(1 / brokenImageAndImageScaleFactor.second); 105 } else 106 imageSize = newImage->imageForRenderer(this)->size(); 107 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. 110 return IntSize(paddingWidth + imageSize.width() * style()->effectiveZoom(), paddingHeight + imageSize.height() * style()->effectiveZoom()); 111} 112 113// Sets the image height and width to fit the alt text. Returns true if the 114// image size changed. 115bool RenderImage::setImageSizeForAltText(CachedImage* newImage /* = 0 */) 116{ 117 IntSize imageSize; 118 if (newImage && newImage->imageForRenderer(this)) 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); 123 } 124 125 // we have an alt and the user meant it (its not a text we invented) 126 if (!m_altText.isEmpty()) { 127 FontCachePurgePreventer fontCachePurgePreventer; 128 129 const Font& font = style()->font(); 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); 132 } 133 134 if (imageSize == intrinsicSize()) 135 return false; 136 137 setIntrinsicSize(imageSize); 138 return true; 139} 140 141void 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 } 149#if ENABLE(CSS_IMAGE_RESOLUTION) 150 if (diff == StyleDifferenceLayout 151 && (oldStyle->imageResolution() != style()->imageResolution() 152 || oldStyle->imageResolutionSnap() != style()->imageResolutionSnap() 153 || oldStyle->imageResolutionSource() != style()->imageResolutionSource())) 154 imageDimensionsChanged(true /* imageSizeChanged */); 155#endif 156} 157 158void RenderImage::imageChanged(WrappedImagePtr newImage, const IntRect* rect) 159{ 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()) 164 return; 165 166 if (hasBoxDecorations() || hasMask()) 167 RenderReplaced::imageChanged(newImage, rect); 168 169 if (!m_imageResource) 170 return; 171 172 if (newImage != m_imageResource->imagePtr() || !newImage) 173 return; 174 175 if (!m_didIncrementVisuallyNonEmptyPixelCount) { 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))); 178 m_didIncrementVisuallyNonEmptyPixelCount = true; 179 } 180 181 bool imageSizeChanged = false; 182 183 // Set image dimensions, taking into account the size of the alt text. 184 if (m_imageResource->errorOccurred()) { 185 if (!m_altText.isEmpty() && document()->hasPendingStyleRecalc()) { 186 ASSERT(node()); 187 if (node()) { 188 m_needsToSetSizeForAltText = true; 189 node()->setNeedsStyleRecalc(SyntheticStyleChange); 190 } 191 return; 192 } 193 imageSizeChanged = setImageSizeForAltText(m_imageResource->cachedImage()); 194 } 195 196 imageDimensionsChanged(imageSizeChanged, rect); 197} 198 199bool RenderImage::updateIntrinsicSizeIfNeeded(const LayoutSize& newSize, bool imageSizeChanged) 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 209void RenderImage::imageDimensionsChanged(bool imageSizeChanged, const IntRect* rect) 210{ 211#if ENABLE(CSS_IMAGE_RESOLUTION) 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); 218#else 219 bool intrinsicSizeChanged = updateIntrinsicSizeIfNeeded(m_imageResource->imageSize(style()->effectiveZoom()), imageSizeChanged); 220#endif 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 229 bool shouldRepaint = true; 230 if (intrinsicSizeChanged) { 231 if (!preferredLogicalWidthsDirty()) 232 setPreferredLogicalWidthsDirty(true); 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 } 244 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 = 249 style()->logicalWidth().isPercent() 250 || style()->logicalMaxWidth().isPercent() 251 || style()->logicalMinWidth().isPercent(); 252 253 if (imageSizeChanged || hasOverrideSize || containingBlockNeedsToRecomputePreferredSize) { 254 shouldRepaint = false; 255 if (!selfNeedsLayout()) 256 setNeedsLayout(true); 257 } 258 } 259 260 if (shouldRepaint) { 261 LayoutRect repaintRect; 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. 265 repaintRect = enclosingIntRect(mapRect(*rect, FloatRect(FloatPoint(), m_imageResource->imageSize(1.0f)), contentBoxRect())); 266 // Guard against too-large changed rects. 267 repaintRect.intersect(contentBoxRect()); 268 } else 269 repaintRect = contentBoxRect(); 270 271 repaintRectangle(repaintRect); 272 273#if USE(ACCELERATED_COMPOSITING) 274 // Tell any potential compositing layers that the image needs updating. 275 contentChanged(ImageChanged); 276#endif 277 } 278} 279 280void RenderImage::notifyFinished(CachedResource* newImage) 281{ 282 if (!m_imageResource) 283 return; 284 285 if (documentBeingDestroyed()) 286 return; 287 288 invalidateBackgroundObscurationStatus(); 289 290#if USE(ACCELERATED_COMPOSITING) 291 if (newImage == m_imageResource->cachedImage()) { 292 // tell any potential compositing layers 293 // that the image is done and they can reference it directly. 294 contentChanged(ImageChanged); 295 } 296#else 297 UNUSED_PARAM(newImage); 298#endif 299} 300 301void RenderImage::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset) 302{ 303 LayoutUnit cWidth = contentWidth(); 304 LayoutUnit cHeight = contentHeight(); 305 LayoutUnit leftBorder = borderLeft(); 306 LayoutUnit topBorder = borderTop(); 307 LayoutUnit leftPad = paddingLeft(); 308 LayoutUnit topPad = paddingTop(); 309 310 GraphicsContext* context = paintInfo.context; 311 312 Page* page = 0; 313 if (Frame* frame = this->frame()) 314 page = frame->page(); 315 316 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred()) { 317 if (paintInfo.phase == PaintPhaseSelection) 318 return; 319 320 if (page && paintInfo.phase == PaintPhaseForeground) 321 page->addRelevantUnpaintedObject(this, visualOverflowRect()); 322 323 if (cWidth > 2 && cHeight > 2) { 324 const int borderWidth = 1; 325 326 // Draw an outline rect where the image should be. 327 context->setStrokeStyle(SolidStroke); 328 context->setStrokeColor(Color::lightGray, style()->colorSpace()); 329 context->setFillColor(Color::transparent, style()->colorSpace()); 330 context->drawRect(pixelSnappedIntRect(LayoutRect(paintOffset.x() + leftBorder + leftPad, paintOffset.y() + topBorder + topPad, cWidth, cHeight))); 331 332 bool errorPictureDrawn = false; 333 LayoutSize imageOffset; 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. 336 LayoutUnit usableWidth = cWidth - 2 * borderWidth; 337 LayoutUnit usableHeight = cHeight - 2 * borderWidth; 338 339 RefPtr<Image> image = m_imageResource->image(); 340 341 if (m_imageResource->errorOccurred() && !image->isNull() && usableWidth >= image->width() && usableHeight >= image->height()) { 342 float deviceScaleFactor = WebCore::deviceScaleFactor(frame()); 343 // Call brokenImage() explicitly to ensure we get the broken image icon at the appropriate resolution. 344 pair<Image*, float> brokenImageAndImageScaleFactor = m_imageResource->cachedImage()->brokenImage(deviceScaleFactor); 345 image = brokenImageAndImageScaleFactor.first; 346 IntSize imageSize = image->size(); 347 imageSize.scale(1 / brokenImageAndImageScaleFactor.second); 348 // Center the error image, accounting for border and padding. 349 LayoutUnit centerX = (usableWidth - imageSize.width()) / 2; 350 if (centerX < 0) 351 centerX = 0; 352 LayoutUnit centerY = (usableHeight - imageSize.height()) / 2; 353 if (centerY < 0) 354 centerY = 0; 355 imageOffset = LayoutSize(leftBorder + leftPad + centerX + borderWidth, topBorder + topPad + centerY + borderWidth); 356 context->drawImage(image.get(), style()->colorSpace(), pixelSnappedIntRect(LayoutRect(paintOffset + imageOffset, imageSize)), CompositeSourceOver, shouldRespectImageOrientation()); 357 errorPictureDrawn = true; 358 } 359 360 if (!m_altText.isEmpty()) { 361 String text = document()->displayStringModifiedByEncoding(m_altText); 362 context->setFillColor(style()->visitedDependentColor(CSSPropertyColor), style()->colorSpace()); 363 const Font& font = style()->font(); 364 const FontMetrics& fontMetrics = font.fontMetrics(); 365 LayoutUnit ascent = fontMetrics.ascent(); 366 LayoutPoint altTextOffset = paintOffset; 367 altTextOffset.move(leftBorder + leftPad + (paddingWidth / 2) - borderWidth, topBorder + topPad + ascent + (paddingHeight / 2) - borderWidth); 368 369 // Only draw the alt text if it'll fit within the content box, 370 // and only if it fits above the error image. 371 TextRun textRun = RenderBlock::constructTextRun(this, font, text, style()); 372 LayoutUnit textWidth = font.width(textRun); 373 if (errorPictureDrawn) { 374 if (usableWidth >= textWidth && fontMetrics.height() <= imageOffset.height()) 375 context->drawText(font, textRun, altTextOffset); 376 } else if (usableWidth >= textWidth && usableHeight >= fontMetrics.height()) 377 context->drawText(font, textRun, altTextOffset); 378 } 379 } 380 } else if (m_imageResource->hasImage() && cWidth > 0 && cHeight > 0) { 381 RefPtr<Image> img = m_imageResource->image(cWidth, cHeight); 382 if (!img || img->isNull()) { 383 if (page && paintInfo.phase == PaintPhaseForeground) 384 page->addRelevantUnpaintedObject(this, visualOverflowRect()); 385 return; 386 } 387 388#if PLATFORM(MAC) 389 if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled()) 390 paintCustomHighlight(toPoint(paintOffset - location()), style()->highlight(), true); 391#endif 392 393 LayoutSize contentSize(cWidth, cHeight); 394 LayoutPoint contentLocation = paintOffset; 395 contentLocation.move(leftBorder + leftPad, topBorder + topPad); 396 paintIntoRect(context, LayoutRect(contentLocation, contentSize)); 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 } 406 } 407} 408 409void RenderImage::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset) 410{ 411 RenderReplaced::paint(paintInfo, paintOffset); 412 413 if (paintInfo.phase == PaintPhaseOutline) 414 paintAreaElementFocusRing(paintInfo); 415} 416 417void RenderImage::paintAreaElementFocusRing(PaintInfo& paintInfo) 418{ 419 Document* document = this->document(); 420 421 if (document->printing() || !document->frame()->selection()->isFocusedAndActive()) 422 return; 423 424 if (paintInfo.context->paintingDisabled() && !paintInfo.context->updatingControlTints()) 425 return; 426 427 Element* focusedElement = document->focusedElement(); 428 if (!focusedElement || !focusedElement->hasTagName(areaTag)) 429 return; 430 431 HTMLAreaElement* areaElement = static_cast<HTMLAreaElement*>(focusedElement); 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 438 Path path = areaElement->computePath(this); 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(); 445 unsigned short outlineWidth = areaElementStyle->outlineWidth(); 446 if (!outlineWidth) 447 return; 448 449 paintInfo.context->drawFocusRing(path, outlineWidth, 450 areaElementStyle->outlineOffset(), 451 areaElementStyle->visitedDependentColor(CSSPropertyOutlineColor)); 452} 453 454void 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 464void RenderImage::paintIntoRect(GraphicsContext* context, const LayoutRect& rect) 465{ 466 IntRect alignedRect = pixelSnappedIntRect(rect); 467 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred() || alignedRect.width() <= 0 || alignedRect.height() <= 0) 468 return; 469 470 RefPtr<Image> img = m_imageResource->image(alignedRect.width(), alignedRect.height()); 471 if (!img || img->isNull()) 472 return; 473 474 HTMLImageElement* imageElt = (node() && node()->hasTagName(imgTag)) ? static_cast<HTMLImageElement*>(node()) : 0; 475 CompositeOperator compositeOperator = imageElt ? imageElt->compositeOperator() : CompositeSourceOver; 476 Image* image = m_imageResource->image().get(); 477 bool useLowQualityScaling = shouldPaintAtLowQuality(context, image, image, alignedRect.size()); 478 context->drawImage(m_imageResource->image(alignedRect.width(), alignedRect.height()).get(), style()->colorSpace(), alignedRect, compositeOperator, shouldRespectImageOrientation(), useLowQualityScaling); 479} 480 481bool RenderImage::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance bleedAvoidance, InlineFlowBox*) const 482{ 483 if (!RenderBoxModelObject::boxShadowShouldBeAppliedToBackground(bleedAvoidance)) 484 return false; 485 486 return !const_cast<RenderImage*>(this)->backgroundIsKnownToBeObscured(); 487} 488 489bool RenderImage::foregroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect, unsigned maxDepthToTest) const 490{ 491 UNUSED_PARAM(maxDepthToTest); 492 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred()) 493 return false; 494 if (m_imageResource->cachedImage() && !m_imageResource->cachedImage()->isLoaded()) 495 return false; 496 if (!contentBoxRect().contains(localRect)) 497 return false; 498 EFillBox backgroundClip = style()->backgroundClip(); 499 // Background paints under borders. 500 if (backgroundClip == BorderFillBox && style()->hasBorder() && !borderObscuresBackground()) 501 return false; 502 // Background shows in padding area. 503 if ((backgroundClip == BorderFillBox || backgroundClip == PaddingFillBox) && style()->hasPadding()) 504 return false; 505 // Check for image with alpha. 506 return m_imageResource->cachedImage() && m_imageResource->cachedImage()->currentFrameKnownToBeOpaque(this); 507} 508 509bool RenderImage::computeBackgroundIsKnownToBeObscured() 510{ 511 if (!hasBackground()) 512 return false; 513 514 LayoutRect paintedExtent; 515 if (!getBackgroundPaintedExtent(paintedExtent)) 516 return false; 517 return foregroundIsKnownToBeOpaqueInRect(paintedExtent, 0); 518} 519 520LayoutUnit RenderImage::minimumReplacedHeight() const 521{ 522 return m_imageResource->errorOccurred() ? intrinsicSize().height() : LayoutUnit(); 523} 524 525HTMLMapElement* RenderImage::imageMap() const 526{ 527 HTMLImageElement* i = node() && node()->hasTagName(imgTag) ? static_cast<HTMLImageElement*>(node()) : 0; 528 return i ? i->treeScope()->getImageMap(i->fastGetAttribute(usemapAttr)) : 0; 529} 530 531bool RenderImage::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction) 532{ 533 HitTestResult tempResult(result.hitTestLocation()); 534 bool inside = RenderReplaced::nodeAtPoint(request, tempResult, locationInContainer, accumulatedOffset, hitTestAction); 535 536 if (tempResult.innerNode() && node()) { 537 if (HTMLMapElement* map = imageMap()) { 538 LayoutRect contentBox = contentBoxRect(); 539 float scaleFactor = 1 / style()->effectiveZoom(); 540 LayoutPoint mapLocation = locationInContainer.point() - toLayoutSize(accumulatedOffset) - locationOffset() - toLayoutSize(contentBox.location()); 541 mapLocation.scale(scaleFactor, scaleFactor); 542 543 if (map->mapMouseEvent(mapLocation, contentBox.size(), tempResult)) 544 tempResult.setInnerNonSharedNode(node()); 545 } 546 } 547 548 if (!inside && result.isRectBasedTest()) 549 result.append(tempResult); 550 if (inside) 551 result = tempResult; 552 return inside; 553} 554 555void RenderImage::updateAltText() 556{ 557 if (!node()) 558 return; 559 560 if (node()->hasTagName(inputTag)) 561 m_altText = static_cast<HTMLInputElement*>(node())->altText(); 562 else if (node()->hasTagName(imgTag)) 563 m_altText = static_cast<HTMLImageElement*>(node())->altText(); 564} 565 566void RenderImage::layout() 567{ 568 StackStats::LayoutCheckPoint layoutCheckPoint; 569 RenderReplaced::layout(); 570 571 // Propagate container size to image resource. 572 IntSize containerSize(contentWidth(), contentHeight()); 573 if (!containerSize.isEmpty()) 574 m_imageResource->setContainerSizeForRenderer(containerSize); 575} 576 577void RenderImage::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const 578{ 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())) { 583 RenderObject* containingBlock = isOutOfFlowPositioned() ? container() : this->containingBlock(); 584 if (containingBlock->isBox()) { 585 RenderBox* box = toRenderBox(containingBlock); 586 intrinsicSize.setWidth(box->availableLogicalWidth()); 587 intrinsicSize.setHeight(box->availableLogicalHeight(IncludeMarginBorderPadding)); 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; 593 return; 594 } 595} 596 597bool RenderImage::needsPreferredWidthsRecalculation() const 598{ 599 if (RenderReplaced::needsPreferredWidthsRecalculation()) 600 return true; 601 return embeddedContentBox(); 602} 603 604RenderBox* RenderImage::embeddedContentBox() const 605{ 606 if (!m_imageResource) 607 return 0; 608 609#if ENABLE(SVG) 610 CachedImage* cachedImage = m_imageResource->cachedImage(); 611 if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage()) 612 return static_cast<SVGImage*>(cachedImage->image())->embeddedContentBox(); 613#endif 614 615 return 0; 616} 617 618} // namespace WebCore 619