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(); | |
64 ResourceLoadPriorityOptimizer::resourceLoadPriorityOptimizer()->addRenderObj
ect(this); | 63 ResourceLoadPriorityOptimizer::resourceLoadPriorityOptimizer()->addRenderObj
ect(this); |
65 } | 64 } |
66 | 65 |
67 RenderImage* RenderImage::createAnonymous(Document* document) | 66 RenderImage* RenderImage::createAnonymous(Document* document) |
68 { | 67 { |
69 RenderImage* image = new RenderImage(0); | 68 RenderImage* image = new RenderImage(0); |
70 image->setDocumentForAnonymous(document); | 69 image->setDocumentForAnonymous(document); |
71 return image; | 70 return image; |
72 } | 71 } |
73 | 72 |
74 RenderImage::~RenderImage() | 73 RenderImage::~RenderImage() |
75 { | 74 { |
76 } | 75 } |
77 | 76 |
78 void RenderImage::destroy() | 77 void RenderImage::destroy() |
79 { | 78 { |
80 ASSERT(m_imageResource); | 79 ASSERT(m_imageResource); |
81 m_imageResource->shutdown(); | 80 m_imageResource->shutdown(); |
82 RenderReplaced::destroy(); | 81 RenderReplaced::destroy(); |
83 } | 82 } |
84 | 83 |
85 void RenderImage::setImageResource(PassOwnPtr<RenderImageResource> imageResource
) | 84 void RenderImage::setImageResource(PassOwnPtr<RenderImageResource> imageResource
) |
86 { | 85 { |
87 ASSERT(!m_imageResource); | 86 ASSERT(!m_imageResource); |
88 m_imageResource = imageResource; | 87 m_imageResource = imageResource; |
89 m_imageResource->initialize(this); | 88 m_imageResource->initialize(this); |
90 } | 89 } |
91 | 90 |
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 | |
144 void RenderImage::imageChanged(WrappedImagePtr newImage, const IntRect* rect) | 91 void RenderImage::imageChanged(WrappedImagePtr newImage, const IntRect* rect) |
145 { | 92 { |
146 if (documentBeingDestroyed()) | 93 if (documentBeingDestroyed()) |
147 return; | 94 return; |
148 | 95 |
149 if (hasBoxDecorationBackground() || hasMask() || hasShapeOutside()) | 96 if (hasBoxDecorationBackground() || hasMask() || hasShapeOutside()) |
150 RenderReplaced::imageChanged(newImage, rect); | 97 RenderReplaced::imageChanged(newImage, rect); |
151 | 98 |
152 if (!m_imageResource) | 99 if (!m_imageResource) |
153 return; | 100 return; |
154 | 101 |
155 if (newImage != m_imageResource->imagePtr()) | 102 if (newImage != m_imageResource->imagePtr()) |
156 return; | 103 return; |
157 | 104 |
158 // Per the spec, we let the server-sent header override srcset/other sources
of dpr. | 105 // 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 | 106 // 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()) | 107 if (m_imageResource->cachedImage() && m_imageResource->cachedImage()->hasDev
icePixelRatioHeaderValue()) |
161 m_imageDevicePixelRatio = 1 / m_imageResource->cachedImage()->devicePixe
lRatioHeaderValue(); | 108 m_imageDevicePixelRatio = 1 / m_imageResource->cachedImage()->devicePixe
lRatioHeaderValue(); |
162 | 109 |
163 if (!m_didIncrementVisuallyNonEmptyPixelCount) { | 110 if (!m_didIncrementVisuallyNonEmptyPixelCount) { |
164 // At a zoom level of 1 the image is guaranteed to have an integer size. | 111 // 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))); | 112 view()->frameView()->incrementVisuallyNonEmptyPixelCount(flooredIntSize(
m_imageResource->imageSize(1.0f))); |
166 m_didIncrementVisuallyNonEmptyPixelCount = true; | 113 m_didIncrementVisuallyNonEmptyPixelCount = true; |
167 } | 114 } |
168 | 115 |
169 bool imageSizeChanged = false; | 116 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 } | 117 } |
177 | 118 |
178 void RenderImage::updateIntrinsicSizeIfNeeded(const LayoutSize& newSize) | 119 void RenderImage::updateIntrinsicSizeIfNeeded(const LayoutSize& newSize) |
179 { | 120 { |
180 if (m_imageResource->errorOccurred() || !m_imageResource->hasImage()) | 121 if (m_imageResource->errorOccurred() || !m_imageResource->hasImage()) |
181 return; | 122 return; |
182 setIntrinsicSize(newSize); | 123 setIntrinsicSize(newSize); |
183 } | 124 } |
184 | 125 |
185 void RenderImage::updateInnerContentRect() | 126 void RenderImage::updateInnerContentRect() |
186 { | 127 { |
187 // Propagate container size to the image resource. | 128 // Propagate container size to the image resource. |
188 LayoutRect containerRect = replacedContentRect(); | 129 LayoutRect containerRect = replacedContentRect(); |
189 IntSize containerSize(containerRect.width(), containerRect.height()); | 130 IntSize containerSize(containerRect.width(), containerRect.height()); |
190 if (!containerSize.isEmpty()) | 131 if (!containerSize.isEmpty()) |
191 m_imageResource->setContainerSizeForRenderer(containerSize); | 132 m_imageResource->setContainerSizeForRenderer(containerSize); |
192 } | 133 } |
193 | 134 |
194 void RenderImage::paintInvalidationOrMarkForLayout(bool imageSizeChangedToAccomo
dateAltText, const IntRect* rect) | 135 void RenderImage::repaintOrMarkForLayout(const IntRect* rect) |
195 { | 136 { |
196 LayoutSize oldIntrinsicSize = intrinsicSize(); | 137 LayoutSize oldIntrinsicSize = intrinsicSize(); |
197 LayoutSize newIntrinsicSize = m_imageResource->intrinsicSize(style()->effect
iveZoom()); | 138 LayoutSize newIntrinsicSize = m_imageResource->intrinsicSize(style()->effect
iveZoom()); |
198 updateIntrinsicSizeIfNeeded(newIntrinsicSize); | 139 updateIntrinsicSizeIfNeeded(newIntrinsicSize); |
199 | 140 |
200 // In the case of generated image content using :before/:after/content, we m
ight not be | 141 // 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. | 142 // 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 | 143 // layout() will be called after we are inserted in the tree which will take
care of |
203 // what we are doing here. | 144 // what we are doing here. |
204 if (!containingBlock()) | 145 if (!containingBlock()) |
205 return; | 146 return; |
206 | 147 |
207 bool imageSourceHasChangedSize = oldIntrinsicSize != newIntrinsicSize || ima
geSizeChangedToAccomodateAltText; | 148 bool imageSourceHasChangedSize = oldIntrinsicSize != newIntrinsicSize; |
208 if (imageSourceHasChangedSize) | 149 if (imageSourceHasChangedSize) |
209 setPreferredLogicalWidthsDirty(); | 150 setPreferredLogicalWidthsDirty(); |
210 | 151 |
211 // If the actual area occupied by the image has changed and it is not constr
ained by style then a layout is required. | 152 // 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(); | 153 bool imageSizeIsConstrained = style()->logicalWidth().isSpecified() && style
()->logicalHeight().isSpecified(); |
213 | 154 |
214 // FIXME: We only need to recompute the containing block's preferred size if
the containing block's size | 155 // 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). | 156 // 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. | 157 // 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(); | 158 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()) | 229 if (path.isEmpty()) |
289 return; | 230 return; |
290 | 231 |
291 RenderStyle* areaElementStyle = areaElement->computedStyle(); | 232 RenderStyle* areaElementStyle = areaElement->computedStyle(); |
292 unsigned short outlineWidth = areaElementStyle->outlineWidth(); | 233 unsigned short outlineWidth = areaElementStyle->outlineWidth(); |
293 | 234 |
294 IntRect paintInvalidationRect = enclosingIntRect(path.boundingRect()); | 235 IntRect paintInvalidationRect = enclosingIntRect(path.boundingRect()); |
295 paintInvalidationRect.moveBy(-absoluteContentBox().location()); | 236 paintInvalidationRect.moveBy(-absoluteContentBox().location()); |
296 paintInvalidationRect.inflate(outlineWidth); | 237 paintInvalidationRect.inflate(outlineWidth); |
297 | 238 |
298 paintInvalidationOrMarkForLayout(false, &paintInvalidationRect); | 239 repaintOrMarkForLayout(&paintInvalidationRect); |
299 } | 240 } |
300 | 241 |
301 bool RenderImage::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance
bleedAvoidance, InlineFlowBox*) const | 242 bool RenderImage::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance
bleedAvoidance, InlineFlowBox*) const |
302 { | 243 { |
303 if (!RenderBoxModelObject::boxShadowShouldBeAppliedToBackground(bleedAvoidan
ce)) | 244 if (!RenderBoxModelObject::boxShadowShouldBeAppliedToBackground(bleedAvoidan
ce)) |
304 return false; | 245 return false; |
305 | 246 |
306 return !const_cast<RenderImage*>(this)->boxDecorationBackgroundIsKnownToBeOb
scured(); | 247 return !const_cast<RenderImage*>(this)->boxDecorationBackgroundIsKnownToBeOb
scured(); |
307 } | 248 } |
308 | 249 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 } | 312 } |
372 } | 313 } |
373 | 314 |
374 if (!inside && result.isRectBasedTest()) | 315 if (!inside && result.isRectBasedTest()) |
375 result.append(tempResult); | 316 result.append(tempResult); |
376 if (inside) | 317 if (inside) |
377 result = tempResult; | 318 result = tempResult; |
378 return inside; | 319 return inside; |
379 } | 320 } |
380 | 321 |
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() | 322 void RenderImage::layout() |
393 { | 323 { |
394 RenderReplaced::layout(); | 324 RenderReplaced::layout(); |
395 updateInnerContentRect(); | 325 updateInnerContentRect(); |
396 } | 326 } |
397 | 327 |
398 bool RenderImage::updateImageLoadingPriorities() | 328 bool RenderImage::updateImageLoadingPriorities() |
399 { | 329 { |
400 if (!m_imageResource || !m_imageResource->cachedImage() || m_imageResource->
cachedImage()->isLoaded()) | 330 if (!m_imageResource || !m_imageResource->cachedImage() || m_imageResource->
cachedImage()->isLoaded()) |
401 return false; | 331 return false; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 return 0; | 389 return 0; |
460 | 390 |
461 ImageResource* cachedImage = m_imageResource->cachedImage(); | 391 ImageResource* cachedImage = m_imageResource->cachedImage(); |
462 if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage(
)) | 392 if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage(
)) |
463 return toSVGImage(cachedImage->image())->embeddedContentBox(); | 393 return toSVGImage(cachedImage->image())->embeddedContentBox(); |
464 | 394 |
465 return 0; | 395 return 0; |
466 } | 396 } |
467 | 397 |
468 } // namespace blink | 398 } // namespace blink |
OLD | NEW |