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