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