| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) | 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) |
| 4 * (C) 2000 Dirk Mueller (mueller@kde.org) | 4 * (C) 2000 Dirk Mueller (mueller@kde.org) |
| 5 * (C) 2006 Allan Sandfeld Jensen (kde@carewolf.com) | 5 * (C) 2006 Allan Sandfeld Jensen (kde@carewolf.com) |
| 6 * (C) 2006 Samuel Weinig (sam.weinig@gmail.com) | 6 * (C) 2006 Samuel Weinig (sam.weinig@gmail.com) |
| 7 * Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010, 2011 Apple Inc. All r
ights reserved. | 7 * Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010, 2011 Apple Inc. All r
ights reserved. |
| 8 * Copyright (C) 2010 Google 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. | 9 * Copyright (C) Research In Motion Limited 2011-2012. All rights reserved. |
| 10 * | 10 * |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 float deviceScaleFactor(LocalFrame*); | 53 float deviceScaleFactor(LocalFrame*); |
| 54 | 54 |
| 55 using namespace HTMLNames; | 55 using namespace HTMLNames; |
| 56 | 56 |
| 57 RenderImage::RenderImage(Element* element) | 57 RenderImage::RenderImage(Element* element) |
| 58 : RenderReplaced(element, IntSize()) | 58 : RenderReplaced(element, IntSize()) |
| 59 , m_didIncrementVisuallyNonEmptyPixelCount(false) | 59 , m_didIncrementVisuallyNonEmptyPixelCount(false) |
| 60 , m_isGeneratedContent(false) | 60 , m_isGeneratedContent(false) |
| 61 , m_imageDevicePixelRatio(1.0f) | 61 , m_imageDevicePixelRatio(1.0f) |
| 62 { | 62 { |
| 63 updateAltText(); |
| 63 ResourceLoadPriorityOptimizer::resourceLoadPriorityOptimizer()->addRenderObj
ect(this); | 64 ResourceLoadPriorityOptimizer::resourceLoadPriorityOptimizer()->addRenderObj
ect(this); |
| 64 } | 65 } |
| 65 | 66 |
| 66 RenderImage* RenderImage::createAnonymous(Document* document) | 67 RenderImage* RenderImage::createAnonymous(Document* document) |
| 67 { | 68 { |
| 68 RenderImage* image = new RenderImage(0); | 69 RenderImage* image = new RenderImage(0); |
| 69 image->setDocumentForAnonymous(document); | 70 image->setDocumentForAnonymous(document); |
| 70 return image; | 71 return image; |
| 71 } | 72 } |
| 72 | 73 |
| 73 RenderImage::~RenderImage() | 74 RenderImage::~RenderImage() |
| 74 { | 75 { |
| 75 } | 76 } |
| 76 | 77 |
| 77 void RenderImage::destroy() | 78 void RenderImage::destroy() |
| 78 { | 79 { |
| 79 ASSERT(m_imageResource); | 80 ASSERT(m_imageResource); |
| 80 m_imageResource->shutdown(); | 81 m_imageResource->shutdown(); |
| 81 RenderReplaced::destroy(); | 82 RenderReplaced::destroy(); |
| 82 } | 83 } |
| 83 | 84 |
| 84 void RenderImage::setImageResource(PassOwnPtr<RenderImageResource> imageResource
) | 85 void RenderImage::setImageResource(PassOwnPtr<RenderImageResource> imageResource
) |
| 85 { | 86 { |
| 86 ASSERT(!m_imageResource); | 87 ASSERT(!m_imageResource); |
| 87 m_imageResource = imageResource; | 88 m_imageResource = imageResource; |
| 88 m_imageResource->initialize(this); | 89 m_imageResource->initialize(this); |
| 89 } | 90 } |
| 90 | 91 |
| 92 // Alt text is restricted to this maximum size, in pixels. These are |
| 93 // signed integers because they are compared with other signed values. |
| 94 static const float maxAltTextWidth = 1024; |
| 95 static const int maxAltTextHeight = 256; |
| 96 |
| 97 IntSize RenderImage::imageSizeForError(ImageResource* newImage) const |
| 98 { |
| 99 ASSERT_ARG(newImage, newImage); |
| 100 ASSERT_ARG(newImage, newImage->imageForRenderer(this)); |
| 101 |
| 102 IntSize imageSize; |
| 103 if (newImage->willPaintBrokenImage()) { |
| 104 float deviceScaleFactor = blink::deviceScaleFactor(frame()); |
| 105 pair<Image*, float> brokenImageAndImageScaleFactor = ImageResource::brok
enImage(deviceScaleFactor); |
| 106 imageSize = brokenImageAndImageScaleFactor.first->size(); |
| 107 imageSize.scale(1 / brokenImageAndImageScaleFactor.second); |
| 108 } else |
| 109 imageSize = newImage->imageForRenderer(this)->size(); |
| 110 |
| 111 // imageSize() returns 0 for the error image. We need the true size of the |
| 112 // error image, so we have to get it by grabbing image() directly. |
| 113 return IntSize(paddingWidth + imageSize.width() * style()->effectiveZoom(),
paddingHeight + imageSize.height() * style()->effectiveZoom()); |
| 114 } |
| 115 |
| 116 // Sets the image height and width to fit the alt text. Returns true if the |
| 117 // image size changed. |
| 118 bool RenderImage::setImageSizeForAltText(ImageResource* newImage /* = 0 */) |
| 119 { |
| 120 IntSize imageSize; |
| 121 if (newImage && newImage->imageForRenderer(this)) |
| 122 imageSize = imageSizeForError(newImage); |
| 123 else if (!m_altText.isEmpty() || newImage) { |
| 124 // If we'll be displaying either text or an image, add a little padding. |
| 125 imageSize = IntSize(paddingWidth, paddingHeight); |
| 126 } |
| 127 |
| 128 // we have an alt and the user meant it (its not a text we invented) |
| 129 if (!m_altText.isEmpty()) { |
| 130 FontCachePurgePreventer fontCachePurgePreventer; |
| 131 |
| 132 const Font& font = style()->font(); |
| 133 IntSize paddedTextSize(paddingWidth + std::min(ceilf(font.width(construc
tTextRun(this, font, m_altText, style()))), maxAltTextWidth), paddingHeight + st
d::min(font.fontMetrics().height(), maxAltTextHeight)); |
| 134 imageSize = imageSize.expandedTo(paddedTextSize); |
| 135 } |
| 136 |
| 137 if (imageSize == intrinsicSize()) |
| 138 return false; |
| 139 |
| 140 setIntrinsicSize(imageSize); |
| 141 return true; |
| 142 } |
| 143 |
| 91 void RenderImage::imageChanged(WrappedImagePtr newImage, const IntRect* rect) | 144 void RenderImage::imageChanged(WrappedImagePtr newImage, const IntRect* rect) |
| 92 { | 145 { |
| 93 if (documentBeingDestroyed()) | 146 if (documentBeingDestroyed()) |
| 94 return; | 147 return; |
| 95 | 148 |
| 96 if (hasBoxDecorationBackground() || hasMask() || hasShapeOutside()) | 149 if (hasBoxDecorationBackground() || hasMask() || hasShapeOutside()) |
| 97 RenderReplaced::imageChanged(newImage, rect); | 150 RenderReplaced::imageChanged(newImage, rect); |
| 98 | 151 |
| 99 if (!m_imageResource) | 152 if (!m_imageResource) |
| 100 return; | 153 return; |
| 101 | 154 |
| 102 if (newImage != m_imageResource->imagePtr()) | 155 if (newImage != m_imageResource->imagePtr()) |
| 103 return; | 156 return; |
| 104 | 157 |
| 105 // Per the spec, we let the server-sent header override srcset/other sources
of dpr. | 158 // Per the spec, we let the server-sent header override srcset/other sources
of dpr. |
| 106 // https://github.com/igrigorik/http-client-hints/blob/master/draft-grigorik
-http-client-hints-01.txt#L255 | 159 // https://github.com/igrigorik/http-client-hints/blob/master/draft-grigorik
-http-client-hints-01.txt#L255 |
| 107 if (m_imageResource->cachedImage() && m_imageResource->cachedImage()->hasDev
icePixelRatioHeaderValue()) | 160 if (m_imageResource->cachedImage() && m_imageResource->cachedImage()->hasDev
icePixelRatioHeaderValue()) |
| 108 m_imageDevicePixelRatio = 1 / m_imageResource->cachedImage()->devicePixe
lRatioHeaderValue(); | 161 m_imageDevicePixelRatio = 1 / m_imageResource->cachedImage()->devicePixe
lRatioHeaderValue(); |
| 109 | 162 |
| 110 if (!m_didIncrementVisuallyNonEmptyPixelCount) { | 163 if (!m_didIncrementVisuallyNonEmptyPixelCount) { |
| 111 // At a zoom level of 1 the image is guaranteed to have an integer size. | 164 // At a zoom level of 1 the image is guaranteed to have an integer size. |
| 112 view()->frameView()->incrementVisuallyNonEmptyPixelCount(flooredIntSize(
m_imageResource->imageSize(1.0f))); | 165 view()->frameView()->incrementVisuallyNonEmptyPixelCount(flooredIntSize(
m_imageResource->imageSize(1.0f))); |
| 113 m_didIncrementVisuallyNonEmptyPixelCount = true; | 166 m_didIncrementVisuallyNonEmptyPixelCount = true; |
| 114 } | 167 } |
| 115 | 168 |
| 116 repaintOrMarkForLayout(rect); | 169 bool imageSizeChanged = false; |
| 170 |
| 171 // Set image dimensions, taking into account the size of the alt text. |
| 172 if (m_imageResource->errorOccurred() || !newImage) |
| 173 imageSizeChanged = setImageSizeForAltText(m_imageResource->cachedImage()
); |
| 174 |
| 175 paintInvalidationOrMarkForLayout(imageSizeChanged, rect); |
| 117 } | 176 } |
| 118 | 177 |
| 119 void RenderImage::updateIntrinsicSizeIfNeeded(const LayoutSize& newSize) | 178 void RenderImage::updateIntrinsicSizeIfNeeded(const LayoutSize& newSize) |
| 120 { | 179 { |
| 121 if (m_imageResource->errorOccurred() || !m_imageResource->hasImage()) | 180 if (m_imageResource->errorOccurred() || !m_imageResource->hasImage()) |
| 122 return; | 181 return; |
| 123 setIntrinsicSize(newSize); | 182 setIntrinsicSize(newSize); |
| 124 } | 183 } |
| 125 | 184 |
| 126 void RenderImage::updateInnerContentRect() | 185 void RenderImage::updateInnerContentRect() |
| 127 { | 186 { |
| 128 // Propagate container size to the image resource. | 187 // Propagate container size to the image resource. |
| 129 LayoutRect containerRect = replacedContentRect(); | 188 LayoutRect containerRect = replacedContentRect(); |
| 130 IntSize containerSize(containerRect.width(), containerRect.height()); | 189 IntSize containerSize(containerRect.width(), containerRect.height()); |
| 131 if (!containerSize.isEmpty()) | 190 if (!containerSize.isEmpty()) |
| 132 m_imageResource->setContainerSizeForRenderer(containerSize); | 191 m_imageResource->setContainerSizeForRenderer(containerSize); |
| 133 } | 192 } |
| 134 | 193 |
| 135 void RenderImage::repaintOrMarkForLayout(const IntRect* rect) | 194 void RenderImage::paintInvalidationOrMarkForLayout(bool imageSizeChangedToAccomo
dateAltText, const IntRect* rect) |
| 136 { | 195 { |
| 137 LayoutSize oldIntrinsicSize = intrinsicSize(); | 196 LayoutSize oldIntrinsicSize = intrinsicSize(); |
| 138 LayoutSize newIntrinsicSize = m_imageResource->intrinsicSize(style()->effect
iveZoom()); | 197 LayoutSize newIntrinsicSize = m_imageResource->intrinsicSize(style()->effect
iveZoom()); |
| 139 updateIntrinsicSizeIfNeeded(newIntrinsicSize); | 198 updateIntrinsicSizeIfNeeded(newIntrinsicSize); |
| 140 | 199 |
| 141 // In the case of generated image content using :before/:after/content, we m
ight not be | 200 // In the case of generated image content using :before/:after/content, we m
ight not be |
| 142 // in the render tree yet. In that case, we just need to update our intrinsi
c size. | 201 // in the render tree yet. In that case, we just need to update our intrinsi
c size. |
| 143 // layout() will be called after we are inserted in the tree which will take
care of | 202 // layout() will be called after we are inserted in the tree which will take
care of |
| 144 // what we are doing here. | 203 // what we are doing here. |
| 145 if (!containingBlock()) | 204 if (!containingBlock()) |
| 146 return; | 205 return; |
| 147 | 206 |
| 148 bool imageSourceHasChangedSize = oldIntrinsicSize != newIntrinsicSize; | 207 bool imageSourceHasChangedSize = oldIntrinsicSize != newIntrinsicSize || ima
geSizeChangedToAccomodateAltText; |
| 149 if (imageSourceHasChangedSize) | 208 if (imageSourceHasChangedSize) |
| 150 setPreferredLogicalWidthsDirty(); | 209 setPreferredLogicalWidthsDirty(); |
| 151 | 210 |
| 152 // If the actual area occupied by the image has changed and it is not constr
ained by style then a layout is required. | 211 // If the actual area occupied by the image has changed and it is not constr
ained by style then a layout is required. |
| 153 bool imageSizeIsConstrained = style()->logicalWidth().isSpecified() && style
()->logicalHeight().isSpecified(); | 212 bool imageSizeIsConstrained = style()->logicalWidth().isSpecified() && style
()->logicalHeight().isSpecified(); |
| 154 | 213 |
| 155 // FIXME: We only need to recompute the containing block's preferred size if
the containing block's size | 214 // FIXME: We only need to recompute the containing block's preferred size if
the containing block's size |
| 156 // depends on the image's size (i.e., the container uses shrink-to-fit sizin
g). | 215 // depends on the image's size (i.e., the container uses shrink-to-fit sizin
g). |
| 157 // There's no easy way to detect that shrink-to-fit is needed, always force
a layout. | 216 // There's no easy way to detect that shrink-to-fit is needed, always force
a layout. |
| 158 bool containingBlockNeedsToRecomputePreferredSize = style()->logicalWidth().
isPercent() || style()->logicalMaxWidth().isPercent() || style()->logicalMinWid
th().isPercent(); | 217 bool containingBlockNeedsToRecomputePreferredSize = style()->logicalWidth().
isPercent() || style()->logicalMaxWidth().isPercent() || style()->logicalMinWid
th().isPercent(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 if (path.isEmpty()) | 288 if (path.isEmpty()) |
| 230 return; | 289 return; |
| 231 | 290 |
| 232 RenderStyle* areaElementStyle = areaElement->computedStyle(); | 291 RenderStyle* areaElementStyle = areaElement->computedStyle(); |
| 233 unsigned short outlineWidth = areaElementStyle->outlineWidth(); | 292 unsigned short outlineWidth = areaElementStyle->outlineWidth(); |
| 234 | 293 |
| 235 IntRect paintInvalidationRect = enclosingIntRect(path.boundingRect()); | 294 IntRect paintInvalidationRect = enclosingIntRect(path.boundingRect()); |
| 236 paintInvalidationRect.moveBy(-absoluteContentBox().location()); | 295 paintInvalidationRect.moveBy(-absoluteContentBox().location()); |
| 237 paintInvalidationRect.inflate(outlineWidth); | 296 paintInvalidationRect.inflate(outlineWidth); |
| 238 | 297 |
| 239 repaintOrMarkForLayout(&paintInvalidationRect); | 298 paintInvalidationOrMarkForLayout(false, &paintInvalidationRect); |
| 240 } | 299 } |
| 241 | 300 |
| 242 bool RenderImage::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance
bleedAvoidance, InlineFlowBox*) const | 301 bool RenderImage::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance
bleedAvoidance, InlineFlowBox*) const |
| 243 { | 302 { |
| 244 if (!RenderBoxModelObject::boxShadowShouldBeAppliedToBackground(bleedAvoidan
ce)) | 303 if (!RenderBoxModelObject::boxShadowShouldBeAppliedToBackground(bleedAvoidan
ce)) |
| 245 return false; | 304 return false; |
| 246 | 305 |
| 247 return !const_cast<RenderImage*>(this)->boxDecorationBackgroundIsKnownToBeOb
scured(); | 306 return !const_cast<RenderImage*>(this)->boxDecorationBackgroundIsKnownToBeOb
scured(); |
| 248 } | 307 } |
| 249 | 308 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 } | 371 } |
| 313 } | 372 } |
| 314 | 373 |
| 315 if (!inside && result.isRectBasedTest()) | 374 if (!inside && result.isRectBasedTest()) |
| 316 result.append(tempResult); | 375 result.append(tempResult); |
| 317 if (inside) | 376 if (inside) |
| 318 result = tempResult; | 377 result = tempResult; |
| 319 return inside; | 378 return inside; |
| 320 } | 379 } |
| 321 | 380 |
| 381 void RenderImage::updateAltText() |
| 382 { |
| 383 if (!node()) |
| 384 return; |
| 385 |
| 386 if (isHTMLInputElement(*node())) |
| 387 m_altText = toHTMLInputElement(node())->altText(); |
| 388 else if (isHTMLImageElement(*node())) |
| 389 m_altText = toHTMLImageElement(node())->altText(); |
| 390 } |
| 391 |
| 322 void RenderImage::layout() | 392 void RenderImage::layout() |
| 323 { | 393 { |
| 324 RenderReplaced::layout(); | 394 RenderReplaced::layout(); |
| 325 updateInnerContentRect(); | 395 updateInnerContentRect(); |
| 326 } | 396 } |
| 327 | 397 |
| 328 bool RenderImage::updateImageLoadingPriorities() | 398 bool RenderImage::updateImageLoadingPriorities() |
| 329 { | 399 { |
| 330 if (!m_imageResource || !m_imageResource->cachedImage() || m_imageResource->
cachedImage()->isLoaded()) | 400 if (!m_imageResource || !m_imageResource->cachedImage() || m_imageResource->
cachedImage()->isLoaded()) |
| 331 return false; | 401 return false; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 return 0; | 459 return 0; |
| 390 | 460 |
| 391 ImageResource* cachedImage = m_imageResource->cachedImage(); | 461 ImageResource* cachedImage = m_imageResource->cachedImage(); |
| 392 if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage(
)) | 462 if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage(
)) |
| 393 return toSVGImage(cachedImage->image())->embeddedContentBox(); | 463 return toSVGImage(cachedImage->image())->embeddedContentBox(); |
| 394 | 464 |
| 395 return 0; | 465 return 0; |
| 396 } | 466 } |
| 397 | 467 |
| 398 } // namespace blink | 468 } // namespace blink |
| OLD | NEW |