1 /* 2 * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org) 3 * (C) 1999 Antti Koivisto (koivisto (at) kde.org) 4 * (C) 2000 Dirk Mueller (mueller (at) kde.org) 5 * (C) 2006 Allan Sandfeld Jensen (kde (at) carewolf.com) 6 * (C) 2006 Samuel Weinig (sam.weinig (at) 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 "core/rendering/RenderImage.h" 30 31 #include "HTMLNames.h" 32 #include "core/editing/FrameSelection.h" 33 #include "core/html/HTMLAreaElement.h" 34 #include "core/html/HTMLImageElement.h" 35 #include "core/html/HTMLInputElement.h" 36 #include "core/html/HTMLMapElement.h" 37 #include "core/loader/cache/ImageResource.h" 38 #include "core/page/Frame.h" 39 #include "core/page/Page.h" 40 #include "core/platform/graphics/Font.h" 41 #include "core/platform/graphics/FontCache.h" 42 #include "core/platform/graphics/GraphicsContext.h" 43 #include "core/platform/graphics/GraphicsContextStateSaver.h" 44 #include "core/rendering/HitTestResult.h" 45 #include "core/rendering/PaintInfo.h" 46 #include "core/rendering/RenderView.h" 47 #include "core/svg/graphics/SVGImage.h" 48 #include "wtf/UnusedParam.h" 49 50 using namespace std; 51 52 namespace WebCore { 53 54 using namespace HTMLNames; 55 56 RenderImage::RenderImage(Element* element) 57 : RenderReplaced(element, IntSize()) 58 , m_needsToSetSizeForAltText(false) 59 , m_didIncrementVisuallyNonEmptyPixelCount(false) 60 , m_isGeneratedContent(false) 61 { 62 updateAltText(); 63 } 64 65 RenderImage* RenderImage::createAnonymous(Document* document) 66 { 67 RenderImage* image = new RenderImage(0); 68 image->setDocumentForAnonymous(document); 69 return image; 70 } 71 72 RenderImage::~RenderImage() 73 { 74 ASSERT(m_imageResource); 75 m_imageResource->shutdown(); 76 } 77 78 void 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. 86 static const unsigned short paddingWidth = 4; 87 static const unsigned short paddingHeight = 4; 88 89 // Alt text is restricted to this maximum size, in pixels. These are 90 // signed integers because they are compared with other signed values. 91 static const float maxAltTextWidth = 1024; 92 static const int maxAltTextHeight = 256; 93 94 IntSize RenderImage::imageSizeForError(ImageResource* 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. 115 bool RenderImage::setImageSizeForAltText(ImageResource* 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 141 void RenderImage::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle) 142 { 143 RenderReplaced::styleDidChange(diff, oldStyle); 144 if (m_needsToSetSizeForAltText) { 145 if (!m_altText.isEmpty() && setImageSizeForAltText(m_imageResource->cachedImage())) 146 imageDimensionsChanged(true /* imageSizeChanged */); 147 m_needsToSetSizeForAltText = false; 148 } 149 } 150 151 void RenderImage::imageChanged(WrappedImagePtr newImage, const IntRect* rect) 152 { 153 if (documentBeingDestroyed()) 154 return; 155 156 if (hasBoxDecorations() || hasMask()) 157 RenderReplaced::imageChanged(newImage, rect); 158 159 if (!m_imageResource) 160 return; 161 162 if (newImage != m_imageResource->imagePtr()) 163 return; 164 165 if (!m_didIncrementVisuallyNonEmptyPixelCount) { 166 // At a zoom level of 1 the image is guaranteed to have an integer size. 167 view()->frameView()->incrementVisuallyNonEmptyPixelCount(flooredIntSize(m_imageResource->imageSize(1.0f))); 168 m_didIncrementVisuallyNonEmptyPixelCount = true; 169 } 170 171 bool imageSizeChanged = false; 172 173 // Set image dimensions, taking into account the size of the alt text. 174 if (m_imageResource->errorOccurred() || !newImage) { 175 if (!m_altText.isEmpty() && document()->hasPendingStyleRecalc()) { 176 ASSERT(node()); 177 if (node()) { 178 m_needsToSetSizeForAltText = true; 179 node()->setNeedsStyleRecalc(LocalStyleChange, StyleChangeFromRenderer); 180 } 181 return; 182 } 183 imageSizeChanged = setImageSizeForAltText(m_imageResource->cachedImage()); 184 } 185 186 imageDimensionsChanged(imageSizeChanged, rect); 187 } 188 189 bool RenderImage::updateIntrinsicSizeIfNeeded(const LayoutSize& newSize, bool imageSizeChanged) 190 { 191 if (newSize == intrinsicSize() && !imageSizeChanged) 192 return false; 193 if (m_imageResource->errorOccurred() || !m_imageResource->hasImage()) 194 return imageSizeChanged; 195 setIntrinsicSize(newSize); 196 return true; 197 } 198 199 void RenderImage::imageDimensionsChanged(bool imageSizeChanged, const IntRect* rect) 200 { 201 bool intrinsicSizeChanged = updateIntrinsicSizeIfNeeded(m_imageResource->imageSize(style()->effectiveZoom()), imageSizeChanged); 202 203 // In the case of generated image content using :before/:after/content, we might not be 204 // in the render tree yet. In that case, we just need to update our intrinsic size. 205 // layout() will be called after we are inserted in the tree which will take care of 206 // what we are doing here. 207 if (!containingBlock()) 208 return; 209 210 bool shouldRepaint = true; 211 if (intrinsicSizeChanged) { 212 if (!preferredLogicalWidthsDirty()) 213 setPreferredLogicalWidthsDirty(true); 214 215 bool hasOverrideSize = hasOverrideHeight() || hasOverrideWidth(); 216 if (!hasOverrideSize && !imageSizeChanged) { 217 LogicalExtentComputedValues computedValues; 218 computeLogicalWidthInRegion(computedValues); 219 LayoutUnit newWidth = computedValues.m_extent; 220 computeLogicalHeight(height(), 0, computedValues); 221 LayoutUnit newHeight = computedValues.m_extent; 222 223 imageSizeChanged = width() != newWidth || height() != newHeight; 224 } 225 226 // FIXME: We only need to recompute the containing block's preferred size 227 // if the containing block's size depends on the image's size (i.e., the container uses shrink-to-fit sizing). 228 // There's no easy way to detect that shrink-to-fit is needed, always force a layout. 229 bool containingBlockNeedsToRecomputePreferredSize = 230 style()->logicalWidth().isPercent() 231 || style()->logicalMaxWidth().isPercent() 232 || style()->logicalMinWidth().isPercent(); 233 234 if (imageSizeChanged || hasOverrideSize || containingBlockNeedsToRecomputePreferredSize) { 235 shouldRepaint = false; 236 if (!selfNeedsLayout()) 237 setNeedsLayout(); 238 } 239 } 240 241 if (shouldRepaint) { 242 LayoutRect repaintRect; 243 if (rect) { 244 // The image changed rect is in source image coordinates (pre-zooming), 245 // so map from the bounds of the image to the contentsBox. 246 repaintRect = enclosingIntRect(mapRect(*rect, FloatRect(FloatPoint(), m_imageResource->imageSize(1.0f)), contentBoxRect())); 247 // Guard against too-large changed rects. 248 repaintRect.intersect(contentBoxRect()); 249 } else 250 repaintRect = contentBoxRect(); 251 252 repaintRectangle(repaintRect); 253 254 // Tell any potential compositing layers that the image needs updating. 255 contentChanged(ImageChanged); 256 } 257 } 258 259 void RenderImage::notifyFinished(Resource* newImage) 260 { 261 if (!m_imageResource) 262 return; 263 264 if (documentBeingDestroyed()) 265 return; 266 267 invalidateBackgroundObscurationStatus(); 268 269 if (newImage == m_imageResource->cachedImage()) { 270 // tell any potential compositing layers 271 // that the image is done and they can reference it directly. 272 contentChanged(ImageChanged); 273 } 274 } 275 276 void RenderImage::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset) 277 { 278 LayoutUnit cWidth = contentWidth(); 279 LayoutUnit cHeight = contentHeight(); 280 LayoutUnit leftBorder = borderLeft(); 281 LayoutUnit topBorder = borderTop(); 282 LayoutUnit leftPad = paddingLeft(); 283 LayoutUnit topPad = paddingTop(); 284 285 GraphicsContext* context = paintInfo.context; 286 287 Page* page = 0; 288 if (Frame* frame = this->frame()) 289 page = frame->page(); 290 291 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred()) { 292 if (paintInfo.phase == PaintPhaseSelection) 293 return; 294 295 if (page && paintInfo.phase == PaintPhaseForeground) 296 page->addRelevantUnpaintedObject(this, visualOverflowRect()); 297 298 if (cWidth > 2 && cHeight > 2) { 299 const int borderWidth = 1; 300 301 // Draw an outline rect where the image should be. 302 context->setStrokeStyle(SolidStroke); 303 context->setStrokeColor(Color::lightGray); 304 context->setFillColor(Color::transparent); 305 context->drawRect(pixelSnappedIntRect(LayoutRect(paintOffset.x() + leftBorder + leftPad, paintOffset.y() + topBorder + topPad, cWidth, cHeight))); 306 307 bool errorPictureDrawn = false; 308 LayoutSize imageOffset; 309 // When calculating the usable dimensions, exclude the pixels of 310 // the ouline rect so the error image/alt text doesn't draw on it. 311 LayoutUnit usableWidth = cWidth - 2 * borderWidth; 312 LayoutUnit usableHeight = cHeight - 2 * borderWidth; 313 314 RefPtr<Image> image = m_imageResource->image(); 315 316 if (m_imageResource->errorOccurred() && !image->isNull() && usableWidth >= image->width() && usableHeight >= image->height()) { 317 float deviceScaleFactor = WebCore::deviceScaleFactor(frame()); 318 // Call brokenImage() explicitly to ensure we get the broken image icon at the appropriate resolution. 319 pair<Image*, float> brokenImageAndImageScaleFactor = m_imageResource->cachedImage()->brokenImage(deviceScaleFactor); 320 image = brokenImageAndImageScaleFactor.first; 321 IntSize imageSize = image->size(); 322 imageSize.scale(1 / brokenImageAndImageScaleFactor.second); 323 // Center the error image, accounting for border and padding. 324 LayoutUnit centerX = (usableWidth - imageSize.width()) / 2; 325 if (centerX < 0) 326 centerX = 0; 327 LayoutUnit centerY = (usableHeight - imageSize.height()) / 2; 328 if (centerY < 0) 329 centerY = 0; 330 imageOffset = LayoutSize(leftBorder + leftPad + centerX + borderWidth, topBorder + topPad + centerY + borderWidth); 331 context->drawImage(image.get(), pixelSnappedIntRect(LayoutRect(paintOffset + imageOffset, imageSize)), CompositeSourceOver, shouldRespectImageOrientation()); 332 errorPictureDrawn = true; 333 } 334 335 if (!m_altText.isEmpty()) { 336 String text = document()->displayStringModifiedByEncoding(m_altText); 337 const Font& font = style()->font(); 338 const FontMetrics& fontMetrics = font.fontMetrics(); 339 LayoutUnit ascent = fontMetrics.ascent(); 340 LayoutPoint textRectOrigin = paintOffset; 341 textRectOrigin.move(leftBorder + leftPad + (paddingWidth / 2) - borderWidth, topBorder + topPad + (paddingHeight / 2) - borderWidth); 342 LayoutPoint textOrigin(textRectOrigin.x(), textRectOrigin.y() + ascent); 343 344 // Only draw the alt text if it'll fit within the content box, 345 // and only if it fits above the error image. 346 TextRun textRun = RenderBlock::constructTextRun(this, font, text, style()); 347 LayoutUnit textWidth = font.width(textRun); 348 TextRunPaintInfo textRunPaintInfo(textRun); 349 textRunPaintInfo.bounds = FloatRect(textRectOrigin, FloatSize(textWidth, fontMetrics.height())); 350 context->setFillColor(resolveColor(CSSPropertyColor)); 351 if (errorPictureDrawn) { 352 if (usableWidth >= textWidth && fontMetrics.height() <= imageOffset.height()) 353 context->drawText(font, textRunPaintInfo, textOrigin); 354 } else if (usableWidth >= textWidth && usableHeight >= fontMetrics.height()) 355 context->drawText(font, textRunPaintInfo, textOrigin); 356 } 357 } 358 } else if (m_imageResource->hasImage() && cWidth > 0 && cHeight > 0) { 359 RefPtr<Image> img = m_imageResource->image(cWidth, cHeight); 360 if (!img || img->isNull()) { 361 if (page && paintInfo.phase == PaintPhaseForeground) 362 page->addRelevantUnpaintedObject(this, visualOverflowRect()); 363 return; 364 } 365 366 LayoutSize contentSize(cWidth, cHeight); 367 LayoutPoint contentLocation = paintOffset; 368 contentLocation.move(leftBorder + leftPad, topBorder + topPad); 369 paintIntoRect(context, LayoutRect(contentLocation, contentSize)); 370 371 if (cachedImage() && page && paintInfo.phase == PaintPhaseForeground) { 372 // For now, count images as unpainted if they are still progressively loading. We may want 373 // to refine this in the future to account for the portion of the image that has painted. 374 if (cachedImage()->isLoading()) 375 page->addRelevantUnpaintedObject(this, LayoutRect(contentLocation, contentSize)); 376 else 377 page->addRelevantRepaintedObject(this, LayoutRect(contentLocation, contentSize)); 378 } 379 } 380 } 381 382 void RenderImage::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset) 383 { 384 RenderReplaced::paint(paintInfo, paintOffset); 385 386 if (paintInfo.phase == PaintPhaseOutline) 387 paintAreaElementFocusRing(paintInfo); 388 } 389 390 void RenderImage::paintAreaElementFocusRing(PaintInfo& paintInfo) 391 { 392 Document* document = this->document(); 393 394 if (document->printing() || !document->frame()->selection()->isFocusedAndActive()) 395 return; 396 397 if (paintInfo.context->paintingDisabled() && !paintInfo.context->updatingControlTints()) 398 return; 399 400 Element* focusedElement = document->focusedElement(); 401 if (!focusedElement || !isHTMLAreaElement(focusedElement)) 402 return; 403 404 HTMLAreaElement* areaElement = toHTMLAreaElement(focusedElement); 405 if (areaElement->imageElement() != node()) 406 return; 407 408 // Even if the theme handles focus ring drawing for entire elements, it won't do it for 409 // an area within an image, so we don't call RenderTheme::supportsFocusRing here. 410 411 Path path = areaElement->computePath(this); 412 if (path.isEmpty()) 413 return; 414 415 RenderStyle* areaElementStyle = areaElement->computedStyle(); 416 unsigned short outlineWidth = areaElementStyle->outlineWidth(); 417 if (!outlineWidth) 418 return; 419 420 // FIXME: Clip path instead of context when Skia pathops is ready. 421 // https://crbug.com/251206 422 GraphicsContextStateSaver savedContext(*paintInfo.context); 423 paintInfo.context->clip(absoluteContentBox()); 424 paintInfo.context->drawFocusRing(path, outlineWidth, 425 areaElementStyle->outlineOffset(), 426 resolveColor(areaElementStyle, CSSPropertyOutlineColor)); 427 } 428 429 void RenderImage::areaElementFocusChanged(HTMLAreaElement* areaElement) 430 { 431 ASSERT(areaElement->imageElement() == node()); 432 433 Path path = areaElement->computePath(this); 434 if (path.isEmpty()) 435 return; 436 437 RenderStyle* areaElementStyle = areaElement->computedStyle(); 438 unsigned short outlineWidth = areaElementStyle->outlineWidth(); 439 440 IntRect repaintRect = enclosingIntRect(path.boundingRect()); 441 repaintRect.moveBy(-absoluteContentBox().location()); 442 repaintRect.inflate(outlineWidth); 443 444 repaintRectangle(repaintRect); 445 } 446 447 void RenderImage::paintIntoRect(GraphicsContext* context, const LayoutRect& rect) 448 { 449 IntRect alignedRect = pixelSnappedIntRect(rect); 450 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred() || alignedRect.width() <= 0 || alignedRect.height() <= 0) 451 return; 452 453 RefPtr<Image> img = m_imageResource->image(alignedRect.width(), alignedRect.height()); 454 if (!img || img->isNull()) 455 return; 456 457 HTMLImageElement* imageElt = (node() && node()->hasTagName(imgTag)) ? toHTMLImageElement(node()) : 0; 458 CompositeOperator compositeOperator = imageElt ? imageElt->compositeOperator() : CompositeSourceOver; 459 Image* image = m_imageResource->image().get(); 460 bool useLowQualityScaling = shouldPaintAtLowQuality(context, image, image, alignedRect.size()); 461 context->drawImage(m_imageResource->image(alignedRect.width(), alignedRect.height()).get(), alignedRect, compositeOperator, shouldRespectImageOrientation(), useLowQualityScaling); 462 } 463 464 bool RenderImage::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance bleedAvoidance, InlineFlowBox*) const 465 { 466 if (!RenderBoxModelObject::boxShadowShouldBeAppliedToBackground(bleedAvoidance)) 467 return false; 468 469 return !const_cast<RenderImage*>(this)->backgroundIsKnownToBeObscured(); 470 } 471 472 bool RenderImage::foregroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect, unsigned maxDepthToTest) const 473 { 474 UNUSED_PARAM(maxDepthToTest); 475 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred()) 476 return false; 477 if (m_imageResource->cachedImage() && !m_imageResource->cachedImage()->isLoaded()) 478 return false; 479 if (!contentBoxRect().contains(localRect)) 480 return false; 481 EFillBox backgroundClip = style()->backgroundClip(); 482 // Background paints under borders. 483 if (backgroundClip == BorderFillBox && style()->hasBorder() && !borderObscuresBackground()) 484 return false; 485 // Background shows in padding area. 486 if ((backgroundClip == BorderFillBox || backgroundClip == PaddingFillBox) && style()->hasPadding()) 487 return false; 488 // Check for image with alpha. 489 return m_imageResource->cachedImage() && m_imageResource->cachedImage()->currentFrameKnownToBeOpaque(this); 490 } 491 492 bool RenderImage::computeBackgroundIsKnownToBeObscured() 493 { 494 if (!hasBackground()) 495 return false; 496 return foregroundIsKnownToBeOpaqueInRect(backgroundPaintedExtent(), 0); 497 } 498 499 LayoutUnit RenderImage::minimumReplacedHeight() const 500 { 501 return m_imageResource->errorOccurred() ? intrinsicSize().height() : LayoutUnit(); 502 } 503 504 HTMLMapElement* RenderImage::imageMap() const 505 { 506 HTMLImageElement* i = node() && node()->hasTagName(imgTag) ? toHTMLImageElement(node()) : 0; 507 return i ? i->treeScope()->getImageMap(i->fastGetAttribute(usemapAttr)) : 0; 508 } 509 510 bool RenderImage::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction) 511 { 512 HitTestResult tempResult(result.hitTestLocation()); 513 bool inside = RenderReplaced::nodeAtPoint(request, tempResult, locationInContainer, accumulatedOffset, hitTestAction); 514 515 if (tempResult.innerNode() && node()) { 516 if (HTMLMapElement* map = imageMap()) { 517 LayoutRect contentBox = contentBoxRect(); 518 float scaleFactor = 1 / style()->effectiveZoom(); 519 LayoutPoint mapLocation = locationInContainer.point() - toLayoutSize(accumulatedOffset) - locationOffset() - toLayoutSize(contentBox.location()); 520 mapLocation.scale(scaleFactor, scaleFactor); 521 522 if (map->mapMouseEvent(mapLocation, contentBox.size(), tempResult)) 523 tempResult.setInnerNonSharedNode(node()); 524 } 525 } 526 527 if (!inside && result.isRectBasedTest()) 528 result.append(tempResult); 529 if (inside) 530 result = tempResult; 531 return inside; 532 } 533 534 void RenderImage::updateAltText() 535 { 536 if (!node()) 537 return; 538 539 if (node()->hasTagName(inputTag)) 540 m_altText = toHTMLInputElement(node())->altText(); 541 else if (node()->hasTagName(imgTag)) 542 m_altText = toHTMLImageElement(node())->altText(); 543 } 544 545 void RenderImage::layout() 546 { 547 StackStats::LayoutCheckPoint layoutCheckPoint; 548 RenderReplaced::layout(); 549 550 // Propagate container size to image resource. 551 IntSize containerSize(contentWidth(), contentHeight()); 552 if (!containerSize.isEmpty()) 553 m_imageResource->setContainerSizeForRenderer(containerSize); 554 } 555 556 void RenderImage::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const 557 { 558 RenderReplaced::computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio, isPercentageIntrinsicSize); 559 560 // Our intrinsicSize is empty if we're rendering generated images with relative width/height. Figure out the right intrinsic size to use. 561 if (intrinsicSize.isEmpty() && (m_imageResource->imageHasRelativeWidth() || m_imageResource->imageHasRelativeHeight())) { 562 RenderObject* containingBlock = isOutOfFlowPositioned() ? container() : this->containingBlock(); 563 if (containingBlock->isBox()) { 564 RenderBox* box = toRenderBox(containingBlock); 565 intrinsicSize.setWidth(box->availableLogicalWidth()); 566 intrinsicSize.setHeight(box->availableLogicalHeight(IncludeMarginBorderPadding)); 567 } 568 } 569 // Don't compute an intrinsic ratio to preserve historical WebKit behavior if we're painting alt text and/or a broken image. 570 if (m_imageResource && m_imageResource->errorOccurred()) { 571 intrinsicRatio = 1; 572 return; 573 } 574 } 575 576 bool RenderImage::needsPreferredWidthsRecalculation() const 577 { 578 if (RenderReplaced::needsPreferredWidthsRecalculation()) 579 return true; 580 return embeddedContentBox(); 581 } 582 583 RenderBox* RenderImage::embeddedContentBox() const 584 { 585 if (!m_imageResource) 586 return 0; 587 588 ImageResource* cachedImage = m_imageResource->cachedImage(); 589 if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage()) 590 return static_cast<SVGImage*>(cachedImage->image())->embeddedContentBox(); 591 592 return 0; 593 } 594 595 } // namespace WebCore 596