OLD | NEW |
---|---|
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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 m_renderImage.resolveColor(areaElementStyle, CSSPropertyOutlineColor)); | 71 m_renderImage.resolveColor(areaElementStyle, CSSPropertyOutlineColor)); |
72 } | 72 } |
73 | 73 |
74 void ImagePainter::paintReplaced(const PaintInfo& paintInfo, const LayoutPoint& paintOffset) | 74 void ImagePainter::paintReplaced(const PaintInfo& paintInfo, const LayoutPoint& paintOffset) |
75 { | 75 { |
76 LayoutUnit cWidth = m_renderImage.contentWidth(); | 76 LayoutUnit cWidth = m_renderImage.contentWidth(); |
77 LayoutUnit cHeight = m_renderImage.contentHeight(); | 77 LayoutUnit cHeight = m_renderImage.contentHeight(); |
78 | 78 |
79 GraphicsContext* context = paintInfo.context; | 79 GraphicsContext* context = paintInfo.context; |
80 | 80 |
81 if (!m_renderImage.imageResource()->hasImage() || m_renderImage.imageResourc e()->errorOccurred()) { | 81 if (!m_renderImage.imageResource()->hasImage()) { |
Noel Gordon
2014/12/16 00:54:53
I may have missed a mention of this in the ChangeL
| |
82 if (paintInfo.phase == PaintPhaseSelection) | 82 if (paintInfo.phase == PaintPhaseSelection) |
83 return; | 83 return; |
84 | 84 |
85 if (cWidth > 2 && cHeight > 2) { | 85 if (cWidth > 2 && cHeight > 2) { |
86 const int borderWidth = 1; | |
87 | |
88 LayoutUnit leftBorder = m_renderImage.borderLeft(); | |
89 LayoutUnit topBorder = m_renderImage.borderTop(); | |
90 LayoutUnit leftPad = m_renderImage.paddingLeft(); | |
91 LayoutUnit topPad = m_renderImage.paddingTop(); | |
92 | |
93 // Draw an outline rect where the image should be. | 86 // Draw an outline rect where the image should be. |
94 IntRect paintRect = pixelSnappedIntRect(LayoutRect(paintOffset.x() + leftBorder + leftPad, paintOffset.y() + topBorder + topPad, cWidth, cHeight)); | 87 IntRect paintRect = pixelSnappedIntRect(LayoutRect(paintOffset.x() + m_renderImage.borderLeft() + m_renderImage.paddingLeft(), paintOffset.y() + m_r enderImage.borderTop() + m_renderImage.paddingTop(), cWidth, cHeight)); |
95 RenderDrawingRecorder recorder(context, &m_renderImage, paintInfo.ph ase, paintRect); | 88 RenderDrawingRecorder recorder(context, &m_renderImage, paintInfo.ph ase, paintRect); |
96 context->setStrokeStyle(SolidStroke); | 89 context->setStrokeStyle(SolidStroke); |
97 context->setStrokeColor(Color::lightGray); | 90 context->setStrokeColor(Color::lightGray); |
98 context->setFillColor(Color::transparent); | 91 context->setFillColor(Color::transparent); |
99 context->drawRect(paintRect); | 92 context->drawRect(paintRect); |
100 | |
101 bool errorPictureDrawn = false; | |
102 LayoutSize imageOffset; | |
103 // When calculating the usable dimensions, exclude the pixels of | |
104 // the ouline rect so the error image/alt text doesn't draw on it. | |
105 LayoutUnit usableWidth = cWidth - 2 * borderWidth; | |
106 LayoutUnit usableHeight = cHeight - 2 * borderWidth; | |
107 | |
108 RefPtr<Image> image = m_renderImage.imageResource()->image(); | |
109 | |
110 if (m_renderImage.imageResource()->errorOccurred() && !image->isNull () && usableWidth >= image->width() && usableHeight >= image->height()) { | |
111 float deviceScaleFactor = blink::deviceScaleFactor(m_renderImage .frame()); | |
112 // Call brokenImage() explicitly to ensure we get the broken ima ge icon at the appropriate resolution. | |
113 pair<Image*, float> brokenImageAndImageScaleFactor = ImageResour ce::brokenImage(deviceScaleFactor); | |
114 image = brokenImageAndImageScaleFactor.first; | |
115 IntSize imageSize = image->size(); | |
116 imageSize.scale(1 / brokenImageAndImageScaleFactor.second); | |
117 // Center the error image, accounting for border and padding. | |
118 LayoutUnit centerX = (usableWidth - imageSize.width()) / 2; | |
119 if (centerX < 0) | |
120 centerX = 0; | |
121 LayoutUnit centerY = (usableHeight - imageSize.height()) / 2; | |
122 if (centerY < 0) | |
123 centerY = 0; | |
124 imageOffset = LayoutSize(leftBorder + leftPad + centerX + border Width, topBorder + topPad + centerY + borderWidth); | |
125 context->drawImage(image.get(), pixelSnappedIntRect(LayoutRect(p aintOffset + imageOffset, LayoutSize(imageSize))), CompositeSourceOver, m_render Image.shouldRespectImageOrientation()); | |
126 errorPictureDrawn = true; | |
127 } | |
128 | |
129 if (!m_renderImage.altText().isEmpty()) { | |
130 const Font& font = m_renderImage.style()->font(); | |
131 const FontMetrics& fontMetrics = font.fontMetrics(); | |
132 LayoutUnit ascent = fontMetrics.ascent(); | |
133 LayoutPoint textRectOrigin = paintOffset; | |
134 textRectOrigin.move(leftBorder + leftPad + (RenderImage::padding Width / 2) - borderWidth, topBorder + topPad + (RenderImage::paddingHeight / 2) - borderWidth); | |
135 FloatPoint textOrigin(textRectOrigin.x().toFloat(), (textRectOri gin.y() + ascent).toFloat()); | |
136 | |
137 // Only draw the alt text if it'll fit within the content box, | |
138 // and only if it fits above the error image. | |
139 TextRun textRun = constructTextRun(&m_renderImage, font, m_rende rImage.altText(), m_renderImage.style(), TextRun::AllowTrailingExpansion | TextR un::ForbidLeadingExpansion, DefaultTextRunFlags | RespectDirection); | |
140 float textWidth = font.width(textRun); | |
141 TextRunPaintInfo textRunPaintInfo(textRun); | |
142 textRunPaintInfo.bounds = FloatRect(FloatPoint(textRectOrigin), FloatSize(textWidth, fontMetrics.height())); | |
143 context->setFillColor(m_renderImage.resolveColor(CSSPropertyColo r)); | |
144 if (textRun.direction() == RTL) { | |
145 int availableWidth = cWidth - static_cast<int>(RenderImage:: paddingWidth); | |
146 textOrigin.move(availableWidth - ceilf(textWidth), 0); | |
147 } | |
148 if (errorPictureDrawn) { | |
149 if (usableWidth >= textWidth && fontMetrics.height() <= imag eOffset.height()) | |
150 context->drawBidiText(font, textRunPaintInfo, textOrigin ); | |
151 } else if (usableWidth >= textWidth && usableHeight >= fontMetri cs.height()) { | |
152 context->drawBidiText(font, textRunPaintInfo, textOrigin); | |
153 } | |
154 } | |
155 } | 93 } |
156 } else if (m_renderImage.imageResource()->hasImage() && cWidth > 0 && cHeigh t > 0) { | 94 } else if (cWidth > 0 && cHeight > 0) { |
157 LayoutRect contentRect = m_renderImage.contentBoxRect(); | 95 LayoutRect contentRect = m_renderImage.contentBoxRect(); |
158 contentRect.moveBy(paintOffset); | 96 contentRect.moveBy(paintOffset); |
159 LayoutRect paintRect = m_renderImage.replacedContentRect(); | 97 LayoutRect paintRect = m_renderImage.replacedContentRect(); |
160 paintRect.moveBy(paintOffset); | 98 paintRect.moveBy(paintOffset); |
161 RenderDrawingRecorder recorder(context, &m_renderImage, paintInfo.phase, contentRect); | 99 RenderDrawingRecorder recorder(context, &m_renderImage, paintInfo.phase, contentRect); |
162 bool clip = !contentRect.contains(paintRect); | 100 bool clip = !contentRect.contains(paintRect); |
163 if (clip) { | 101 if (clip) { |
164 context->save(); | 102 context->save(); |
165 context->clip(contentRect); | 103 context->clip(contentRect); |
166 } | 104 } |
(...skipping 27 matching lines...) Expand all Loading... | |
194 | 132 |
195 InterpolationQuality previousInterpolationQuality = context->imageInterpolat ionQuality(); | 133 InterpolationQuality previousInterpolationQuality = context->imageInterpolat ionQuality(); |
196 context->setImageInterpolationQuality(interpolationQuality); | 134 context->setImageInterpolationQuality(interpolationQuality); |
197 context->drawImage(image.get(), alignedRect, CompositeSourceOver, m_renderIm age.shouldRespectImageOrientation()); | 135 context->drawImage(image.get(), alignedRect, CompositeSourceOver, m_renderIm age.shouldRespectImageOrientation()); |
198 context->setImageInterpolationQuality(previousInterpolationQuality); | 136 context->setImageInterpolationQuality(previousInterpolationQuality); |
199 | 137 |
200 InspectorInstrumentation::didPaintImage(&m_renderImage); | 138 InspectorInstrumentation::didPaintImage(&m_renderImage); |
201 } | 139 } |
202 | 140 |
203 } // namespace blink | 141 } // namespace blink |
OLD | NEW |