Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: Source/core/rendering/RenderImage.cpp

Issue 481753002: Use Shadow DOM to display fallback content for images (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Updated Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 if (path.isEmpty()) 234 if (path.isEmpty())
294 return; 235 return;
295 236
296 RenderStyle* areaElementStyle = areaElement->computedStyle(); 237 RenderStyle* areaElementStyle = areaElement->computedStyle();
297 unsigned short outlineWidth = areaElementStyle->outlineWidth(); 238 unsigned short outlineWidth = areaElementStyle->outlineWidth();
298 239
299 IntRect paintInvalidationRect = enclosingIntRect(path.boundingRect()); 240 IntRect paintInvalidationRect = enclosingIntRect(path.boundingRect());
300 paintInvalidationRect.moveBy(-absoluteContentBox().location()); 241 paintInvalidationRect.moveBy(-absoluteContentBox().location());
301 paintInvalidationRect.inflate(outlineWidth); 242 paintInvalidationRect.inflate(outlineWidth);
302 243
303 paintInvalidationOrMarkForLayout(false, &paintInvalidationRect); 244 repaintOrMarkForLayout(&paintInvalidationRect);
304 } 245 }
305 246
306 bool RenderImage::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance bleedAvoidance, InlineFlowBox*) const 247 bool RenderImage::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance bleedAvoidance, InlineFlowBox*) const
307 { 248 {
308 if (!RenderBoxModelObject::boxShadowShouldBeAppliedToBackground(bleedAvoidan ce)) 249 if (!RenderBoxModelObject::boxShadowShouldBeAppliedToBackground(bleedAvoidan ce))
309 return false; 250 return false;
310 251
311 return !const_cast<RenderImage*>(this)->boxDecorationBackgroundIsKnownToBeOb scured(); 252 return !const_cast<RenderImage*>(this)->boxDecorationBackgroundIsKnownToBeOb scured();
312 } 253 }
313 254
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 } 317 }
377 } 318 }
378 319
379 if (!inside && result.isRectBasedTest()) 320 if (!inside && result.isRectBasedTest())
380 result.append(tempResult); 321 result.append(tempResult);
381 if (inside) 322 if (inside)
382 result = tempResult; 323 result = tempResult;
383 return inside; 324 return inside;
384 } 325 }
385 326
386 void RenderImage::updateAltText()
387 {
388 if (!node())
389 return;
390
391 if (isHTMLInputElement(*node()))
392 m_altText = toHTMLInputElement(node())->altText();
393 else if (isHTMLImageElement(*node()))
394 m_altText = toHTMLImageElement(node())->altText();
395 }
396
397 void RenderImage::layout() 327 void RenderImage::layout()
398 { 328 {
399 RenderReplaced::layout(); 329 RenderReplaced::layout();
400 updateInnerContentRect(); 330 updateInnerContentRect();
401 } 331 }
402 332
403 bool RenderImage::updateImageLoadingPriorities() 333 bool RenderImage::updateImageLoadingPriorities()
404 { 334 {
405 if (!m_imageResource || !m_imageResource->cachedImage() || m_imageResource-> cachedImage()->isLoaded()) 335 if (!m_imageResource || !m_imageResource->cachedImage() || m_imageResource-> cachedImage()->isLoaded())
406 return false; 336 return false;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 return 0; 394 return 0;
465 395
466 ImageResource* cachedImage = m_imageResource->cachedImage(); 396 ImageResource* cachedImage = m_imageResource->cachedImage();
467 if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage( )) 397 if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage( ))
468 return toSVGImage(cachedImage->image())->embeddedContentBox(); 398 return toSVGImage(cachedImage->image())->embeddedContentBox();
469 399
470 return 0; 400 return 0;
471 } 401 }
472 402
473 } // namespace blink 403 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698