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

Side by Side Diff: Source/core/paint/ImagePainter.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, 2 months 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 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "core/paint/ImagePainter.h" 6 #include "core/paint/ImagePainter.h"
7 7
8 #include "core/dom/Document.h" 8 #include "core/dom/Document.h"
9 #include "core/dom/Element.h" 9 #include "core/dom/Element.h"
10 #include "core/editing/FrameSelection.h" 10 #include "core/editing/FrameSelection.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 m_renderImage.resolveColor(areaElementStyle, CSSPropertyOutlineColor)); 68 m_renderImage.resolveColor(areaElementStyle, CSSPropertyOutlineColor));
69 } 69 }
70 70
71 void ImagePainter::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintO ffset) 71 void ImagePainter::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintO ffset)
72 { 72 {
73 LayoutUnit cWidth = m_renderImage.contentWidth(); 73 LayoutUnit cWidth = m_renderImage.contentWidth();
74 LayoutUnit cHeight = m_renderImage.contentHeight(); 74 LayoutUnit cHeight = m_renderImage.contentHeight();
75 75
76 GraphicsContext* context = paintInfo.context; 76 GraphicsContext* context = paintInfo.context;
77 77
78 if (!m_renderImage.imageResource()->hasImage() || m_renderImage.imageResourc e()->errorOccurred()) { 78 if (!m_renderImage.imageResource()->hasImage()) {
79 if (paintInfo.phase == PaintPhaseSelection) 79 if (paintInfo.phase == PaintPhaseSelection)
80 return; 80 return;
81 81
82 if (cWidth > 2 && cHeight > 2) { 82 if (cWidth > 2 && cHeight > 2) {
83 const int borderWidth = 1;
84
85 LayoutUnit leftBorder = m_renderImage.borderLeft();
86 LayoutUnit topBorder = m_renderImage.borderTop();
87 LayoutUnit leftPad = m_renderImage.paddingLeft();
88 LayoutUnit topPad = m_renderImage.paddingTop();
89
90 // Draw an outline rect where the image should be. 83 // Draw an outline rect where the image should be.
91 context->setStrokeStyle(SolidStroke); 84 context->setStrokeStyle(SolidStroke);
92 context->setStrokeColor(Color::lightGray); 85 context->setStrokeColor(Color::lightGray);
93 context->setFillColor(Color::transparent); 86 context->setFillColor(Color::transparent);
94 context->drawRect(pixelSnappedIntRect(LayoutRect(paintOffset.x() + l eftBorder + leftPad, paintOffset.y() + topBorder + topPad, cWidth, cHeight))); 87 context->drawRect(pixelSnappedIntRect(LayoutRect(paintOffset.x() + m _renderImage.borderLeft() + m_renderImage.paddingLeft(), paintOffset.y() + m_ren derImage.borderTop() + m_renderImage.paddingTop(), cWidth, cHeight)));
95
96 bool errorPictureDrawn = false;
97 LayoutSize imageOffset;
98 // When calculating the usable dimensions, exclude the pixels of
99 // the ouline rect so the error image/alt text doesn't draw on it.
100 LayoutUnit usableWidth = cWidth - 2 * borderWidth;
101 LayoutUnit usableHeight = cHeight - 2 * borderWidth;
102
103 RefPtr<Image> image = m_renderImage.imageResource()->image();
104
105 if (m_renderImage.imageResource()->errorOccurred() && !image->isNull () && usableWidth >= image->width() && usableHeight >= image->height()) {
106 float deviceScaleFactor = blink::deviceScaleFactor(m_renderImage .frame());
107 // Call brokenImage() explicitly to ensure we get the broken ima ge icon at the appropriate resolution.
108 pair<Image*, float> brokenImageAndImageScaleFactor = ImageResour ce::brokenImage(deviceScaleFactor);
109 image = brokenImageAndImageScaleFactor.first;
110 IntSize imageSize = image->size();
111 imageSize.scale(1 / brokenImageAndImageScaleFactor.second);
112 // Center the error image, accounting for border and padding.
113 LayoutUnit centerX = (usableWidth - imageSize.width()) / 2;
114 if (centerX < 0)
115 centerX = 0;
116 LayoutUnit centerY = (usableHeight - imageSize.height()) / 2;
117 if (centerY < 0)
118 centerY = 0;
119 imageOffset = LayoutSize(leftBorder + leftPad + centerX + border Width, topBorder + topPad + centerY + borderWidth);
120 context->drawImage(image.get(), pixelSnappedIntRect(LayoutRect(p aintOffset + imageOffset, imageSize)), CompositeSourceOver, m_renderImage.should RespectImageOrientation());
121 errorPictureDrawn = true;
122 }
123
124 if (!m_renderImage.altText().isEmpty()) {
125 const Font& font = m_renderImage.style()->font();
126 const FontMetrics& fontMetrics = font.fontMetrics();
127 LayoutUnit ascent = fontMetrics.ascent();
128 LayoutPoint textRectOrigin = paintOffset;
129 textRectOrigin.move(leftBorder + leftPad + (RenderImage::padding Width / 2) - borderWidth, topBorder + topPad + (RenderImage::paddingHeight / 2) - borderWidth);
130 LayoutPoint textOrigin(textRectOrigin.x(), textRectOrigin.y() + ascent);
131
132 // Only draw the alt text if it'll fit within the content box,
133 // and only if it fits above the error image.
134 TextRun textRun = constructTextRun(&m_renderImage, font, m_rende rImage.altText(), m_renderImage.style(), TextRun::AllowTrailingExpansion | TextR un::ForbidLeadingExpansion, DefaultTextRunFlags | RespectDirection);
135 float textWidth = font.width(textRun);
136 TextRunPaintInfo textRunPaintInfo(textRun);
137 textRunPaintInfo.bounds = FloatRect(textRectOrigin, FloatSize(te xtWidth, fontMetrics.height()));
138 context->setFillColor(m_renderImage.resolveColor(CSSPropertyColo r));
139 if (textRun.direction() == RTL) {
140 int availableWidth = cWidth - static_cast<int>(RenderImage:: paddingWidth);
141 textOrigin.move(availableWidth - ceilf(textWidth), 0);
142 }
143 if (errorPictureDrawn) {
144 if (usableWidth >= textWidth && fontMetrics.height() <= imag eOffset.height())
145 context->drawBidiText(font, textRunPaintInfo, textOrigin );
146 } else if (usableWidth >= textWidth && usableHeight >= fontMetri cs.height()) {
147 context->drawBidiText(font, textRunPaintInfo, textOrigin);
148 }
149 }
150 } 88 }
151 } else if (m_renderImage.imageResource()->hasImage() && cWidth > 0 && cHeigh t > 0) { 89 } else if (cWidth > 0 && cHeight > 0) {
152 LayoutRect contentRect = m_renderImage.contentBoxRect(); 90 LayoutRect contentRect = m_renderImage.contentBoxRect();
153 contentRect.moveBy(paintOffset); 91 contentRect.moveBy(paintOffset);
154 LayoutRect paintRect = m_renderImage.replacedContentRect(); 92 LayoutRect paintRect = m_renderImage.replacedContentRect();
155 paintRect.moveBy(paintOffset); 93 paintRect.moveBy(paintOffset);
156 bool clip = !contentRect.contains(paintRect); 94 bool clip = !contentRect.contains(paintRect);
157 if (clip) { 95 if (clip) {
158 context->save(); 96 context->save();
159 context->clip(contentRect); 97 context->clip(contentRect);
160 } 98 }
161 99
(...skipping 23 matching lines...) Expand all
185 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeli ne migrates to tracing. 123 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeli ne migrates to tracing.
186 InspectorInstrumentation::willPaintImage(&m_renderImage); 124 InspectorInstrumentation::willPaintImage(&m_renderImage);
187 InterpolationQuality previousInterpolationQuality = context->imageInterpolat ionQuality(); 125 InterpolationQuality previousInterpolationQuality = context->imageInterpolat ionQuality();
188 context->setImageInterpolationQuality(interpolationQuality); 126 context->setImageInterpolationQuality(interpolationQuality);
189 context->drawImage(image, alignedRect, compositeOperator, m_renderImage.shou ldRespectImageOrientation()); 127 context->drawImage(image, alignedRect, compositeOperator, m_renderImage.shou ldRespectImageOrientation());
190 context->setImageInterpolationQuality(previousInterpolationQuality); 128 context->setImageInterpolationQuality(previousInterpolationQuality);
191 InspectorInstrumentation::didPaintImage(&m_renderImage); 129 InspectorInstrumentation::didPaintImage(&m_renderImage);
192 } 130 }
193 131
194 } // namespace blink 132 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698