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()) { | 81 if (!m_renderImage.imageResource()->hasImage() || m_renderImage.imageResourc
e()->errorOccurred()) { |
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 |
86 // Draw an outline rect where the image should be. | 93 // Draw an outline rect where the image should be. |
87 IntRect paintRect = pixelSnappedIntRect(LayoutRect(paintOffset.x() +
m_renderImage.borderLeft() + m_renderImage.paddingLeft(), paintOffset.y() + m_r
enderImage.borderTop() + m_renderImage.paddingTop(), cWidth, cHeight)); | 94 IntRect paintRect = pixelSnappedIntRect(LayoutRect(paintOffset.x() +
leftBorder + leftPad, paintOffset.y() + topBorder + topPad, cWidth, cHeight)); |
88 DrawingRecorder recorder(context, &m_renderImage, paintInfo.phase, p
aintRect); | 95 DrawingRecorder recorder(context, &m_renderImage, paintInfo.phase, p
aintRect); |
89 context->setStrokeStyle(SolidStroke); | 96 context->setStrokeStyle(SolidStroke); |
90 context->setStrokeColor(Color::lightGray); | 97 context->setStrokeColor(Color::lightGray); |
91 context->setFillColor(Color::transparent); | 98 context->setFillColor(Color::transparent); |
92 context->drawRect(paintRect); | 99 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, imageSize)), CompositeSourceOver, m_renderImage.should
RespectImageOrientation()); |
| 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 } |
93 } | 155 } |
94 } else if (cWidth > 0 && cHeight > 0) { | 156 } else if (m_renderImage.imageResource()->hasImage() && cWidth > 0 && cHeigh
t > 0) { |
95 LayoutRect contentRect = m_renderImage.contentBoxRect(); | 157 LayoutRect contentRect = m_renderImage.contentBoxRect(); |
96 contentRect.moveBy(paintOffset); | 158 contentRect.moveBy(paintOffset); |
97 LayoutRect paintRect = m_renderImage.replacedContentRect(); | 159 LayoutRect paintRect = m_renderImage.replacedContentRect(); |
98 paintRect.moveBy(paintOffset); | 160 paintRect.moveBy(paintOffset); |
99 DrawingRecorder recorder(context, &m_renderImage, paintInfo.phase, conte
ntRect); | 161 DrawingRecorder recorder(context, &m_renderImage, paintInfo.phase, conte
ntRect); |
100 bool clip = !contentRect.contains(paintRect); | 162 bool clip = !contentRect.contains(paintRect); |
101 if (clip) { | 163 if (clip) { |
102 context->save(); | 164 context->save(); |
103 context->clip(contentRect); | 165 context->clip(contentRect); |
104 } | 166 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 bool shouldAntialias = !m_renderImage.isRenderImage() || blink::deviceScaleF
actor(m_renderImage.frame()) < 3.4; | 209 bool shouldAntialias = !m_renderImage.isRenderImage() || blink::deviceScaleF
actor(m_renderImage.frame()) < 3.4; |
148 | 210 |
149 bool oldShouldAntialias = paintInfo.context->shouldAntialias(); | 211 bool oldShouldAntialias = paintInfo.context->shouldAntialias(); |
150 paintInfo.context->setShouldAntialias(shouldAntialias); | 212 paintInfo.context->setShouldAntialias(shouldAntialias); |
151 BoxPainter(m_renderImage).paintBoxDecorationBackground(paintInfo, paintOffse
t); | 213 BoxPainter(m_renderImage).paintBoxDecorationBackground(paintInfo, paintOffse
t); |
152 | 214 |
153 paintInfo.context->setShouldAntialias(oldShouldAntialias); | 215 paintInfo.context->setShouldAntialias(oldShouldAntialias); |
154 } | 216 } |
155 | 217 |
156 } // namespace blink | 218 } // namespace blink |
OLD | NEW |