| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "config.h" | 26 #include "config.h" |
| 27 #include "core/platform/DragImage.h" | 27 #include "core/platform/DragImage.h" |
| 28 | 28 |
| 29 #include "core/platform/graphics/BitmapImage.h" |
| 30 #include "core/platform/graphics/Color.h" |
| 31 #include "core/platform/graphics/FloatPoint.h" |
| 32 #include "core/platform/graphics/FloatRect.h" |
| 29 #include "core/platform/graphics/Font.h" | 33 #include "core/platform/graphics/Font.h" |
| 30 #include "core/platform/graphics/FontCache.h" | 34 #include "core/platform/graphics/FontCache.h" |
| 31 #include "core/platform/graphics/FontDescription.h" | 35 #include "core/platform/graphics/FontDescription.h" |
| 32 #include "core/platform/graphics/FontSelector.h" | 36 #include "core/platform/graphics/FontMetrics.h" |
| 33 #include "core/platform/graphics/GraphicsContext.h" | 37 #include "core/platform/graphics/GraphicsContext.h" |
| 38 #include "core/platform/graphics/Image.h" |
| 34 #include "core/platform/graphics/ImageBuffer.h" | 39 #include "core/platform/graphics/ImageBuffer.h" |
| 40 #include "core/platform/graphics/IntPoint.h" |
| 41 #include "core/platform/graphics/IntSize.h" |
| 35 #include "core/platform/graphics/StringTruncator.h" | 42 #include "core/platform/graphics/StringTruncator.h" |
| 36 #include "core/platform/graphics/TextRun.h" | 43 #include "core/platform/graphics/TextRun.h" |
| 44 #include "core/platform/graphics/skia/NativeImageSkia.h" |
| 45 #include "core/platform/graphics/transforms/AffineTransform.h" |
| 46 #include "skia/ext/image_operations.h" |
| 47 #include "third_party/skia/include/core/SkCanvas.h" |
| 48 #include "third_party/skia/include/core/SkMatrix.h" |
| 37 #include "weborigin/KURL.h" | 49 #include "weborigin/KURL.h" |
| 50 #include "wtf/PassOwnPtr.h" |
| 51 #include "wtf/RefPtr.h" |
| 52 #include "wtf/text/WTFString.h" |
| 53 |
| 54 #include <algorithm> |
| 38 | 55 |
| 39 namespace WebCore { | 56 namespace WebCore { |
| 40 | 57 |
| 41 const float kDragLabelBorderX = 4; | 58 const float kDragLabelBorderX = 4; |
| 42 // Keep border_y in synch with DragController::LinkDragBorderInset. | 59 // Keep border_y in synch with DragController::LinkDragBorderInset. |
| 43 const float kDragLabelBorderY = 2; | 60 const float kDragLabelBorderY = 2; |
| 44 const float kDragLabelRadius = 5; | 61 const float kDragLabelRadius = 5; |
| 45 const float kLabelBorderYOffset = 2; | 62 const float kLabelBorderYOffset = 2; |
| 46 | 63 |
| 47 const float kMinDragLabelWidthBeforeClip = 120; | 64 const float kMinDragLabelWidthBeforeClip = 120; |
| 48 const float kMaxDragLabelWidth = 300; | 65 const float kMaxDragLabelWidth = 300; |
| 49 const float kMaxDragLabelStringWidth = (kMaxDragLabelWidth - 2 * kDragLabelBorde
rX); | 66 const float kMaxDragLabelStringWidth = (kMaxDragLabelWidth - 2 * kDragLabelBorde
rX); |
| 50 | 67 |
| 51 const float kDragLinkLabelFontSize = 11; | 68 const float kDragLinkLabelFontSize = 11; |
| 52 const float kDragLinkUrlFontSize = 10; | 69 const float kDragLinkUrlFontSize = 10; |
| 53 | 70 |
| 71 PassOwnPtr<DragImage> DragImage::create(Image* image, RespectImageOrientationEnu
m shouldRespectImageOrientation) |
| 72 { |
| 73 if (!image) |
| 74 return nullptr; |
| 75 |
| 76 RefPtr<NativeImageSkia> bitmap = image->nativeImageForCurrentFrame(); |
| 77 if (!bitmap) |
| 78 return nullptr; |
| 79 |
| 80 if (image->isBitmapImage()) { |
| 81 ImageOrientation orientation = DefaultImageOrientation; |
| 82 BitmapImage* bitmapImage = static_cast<BitmapImage*>(image); |
| 83 IntSize sizeRespectingOrientation = bitmapImage->sizeRespectingOrientati
on(); |
| 84 |
| 85 if (shouldRespectImageOrientation == RespectImageOrientation) |
| 86 orientation = bitmapImage->currentFrameOrientation(); |
| 87 |
| 88 if (orientation != DefaultImageOrientation) { |
| 89 FloatRect destRect(FloatPoint(), sizeRespectingOrientation); |
| 90 if (orientation.usesWidthAsHeight()) |
| 91 destRect = destRect.transposedRect(); |
| 92 |
| 93 SkBitmap skBitmap; |
| 94 skBitmap.setConfig( |
| 95 SkBitmap::kARGB_8888_Config, sizeRespectingOrientation.width(),
sizeRespectingOrientation.height()); |
| 96 if (!skBitmap.allocPixels()) |
| 97 return nullptr; |
| 98 |
| 99 SkCanvas canvas(skBitmap); |
| 100 canvas.concat(orientation.transformFromDefault(sizeRespectingOrienta
tion)); |
| 101 canvas.drawBitmapRect(bitmap->bitmap(), 0, destRect); |
| 102 |
| 103 return adoptPtr(new DragImage(skBitmap, bitmap->resolutionScale())); |
| 104 } |
| 105 } |
| 106 |
| 107 SkBitmap skBitmap; |
| 108 if (!bitmap->bitmap().copyTo(&skBitmap, SkBitmap::kARGB_8888_Config)) |
| 109 return nullptr; |
| 110 return adoptPtr(new DragImage(skBitmap, bitmap->resolutionScale())); |
| 111 } |
| 112 |
| 54 static Font deriveDragLabelFont(int size, FontWeight fontWeight, const FontDescr
iption& systemFont) | 113 static Font deriveDragLabelFont(int size, FontWeight fontWeight, const FontDescr
iption& systemFont) |
| 55 { | 114 { |
| 56 FontDescription description = systemFont; | 115 FontDescription description = systemFont; |
| 57 description.setWeight(fontWeight); | 116 description.setWeight(fontWeight); |
| 58 | |
| 59 description.setSpecifiedSize(size); | 117 description.setSpecifiedSize(size); |
| 60 description.setComputedSize(size); | 118 description.setComputedSize(size); |
| 61 Font result(description, 0, 0); | 119 Font result(description, 0, 0); |
| 62 result.update(0); | 120 result.update(0); |
| 63 return result; | 121 return result; |
| 64 } | 122 } |
| 65 | 123 |
| 66 DragImageRef fitDragImageToMaxSize(DragImageRef image, const IntSize& srcSize, c
onst IntSize& size) | 124 PassOwnPtr<DragImage> DragImage::create(const KURL& url, const String& inLabel,
const FontDescription& systemFont, float deviceScaleFactor) |
| 67 { | |
| 68 float heightResizeRatio = 0.0f; | |
| 69 float widthResizeRatio = 0.0f; | |
| 70 float resizeRatio = -1.0f; | |
| 71 IntSize originalSize = dragImageSize(image); | |
| 72 | |
| 73 if (srcSize.width() > size.width()) { | |
| 74 widthResizeRatio = size.width() / (float)srcSize.width(); | |
| 75 resizeRatio = widthResizeRatio; | |
| 76 } | |
| 77 | |
| 78 if (srcSize.height() > size.height()) { | |
| 79 heightResizeRatio = size.height() / (float)srcSize.height(); | |
| 80 if ((resizeRatio < 0.0f) || (resizeRatio > heightResizeRatio)) | |
| 81 resizeRatio = heightResizeRatio; | |
| 82 } | |
| 83 | |
| 84 if (srcSize == originalSize) | |
| 85 return resizeRatio > 0.0f ? scaleDragImage(image, FloatSize(resizeRatio,
resizeRatio)) : image; | |
| 86 | |
| 87 // The image was scaled in the webpage so at minimum we must account for tha
t scaling | |
| 88 float scalex = srcSize.width() / (float)originalSize.width(); | |
| 89 float scaley = srcSize.height() / (float)originalSize.height(); | |
| 90 if (resizeRatio > 0.0f) { | |
| 91 scalex *= resizeRatio; | |
| 92 scaley *= resizeRatio; | |
| 93 } | |
| 94 | |
| 95 return scaleDragImage(image, FloatSize(scalex, scaley)); | |
| 96 } | |
| 97 | |
| 98 DragImageRef createDragImageForSelection(DragImageRef image, float dragImageAlph
a) | |
| 99 { | |
| 100 if (image) | |
| 101 image = dissolveDragImageToFraction(image, dragImageAlpha); | |
| 102 return image; | |
| 103 } | |
| 104 | |
| 105 DragImageRef createDragImageForLink(const KURL& url, const String& inLabel, cons
t FontDescription& systemFont, float deviceScaleFactor) | |
| 106 { | 125 { |
| 107 const Font labelFont = deriveDragLabelFont(kDragLinkLabelFontSize, FontWeigh
tBold, systemFont); | 126 const Font labelFont = deriveDragLabelFont(kDragLinkLabelFontSize, FontWeigh
tBold, systemFont); |
| 108 const Font urlFont = deriveDragLabelFont(kDragLinkUrlFontSize, FontWeightNor
mal, systemFont); | 127 const Font urlFont = deriveDragLabelFont(kDragLinkUrlFontSize, FontWeightNor
mal, systemFont); |
| 109 FontCachePurgePreventer fontCachePurgePreventer; | 128 FontCachePurgePreventer fontCachePurgePreventer; |
| 110 | 129 |
| 111 bool drawURLString = true; | 130 bool drawURLString = true; |
| 112 bool clipURLString = false; | 131 bool clipURLString = false; |
| 113 bool clipLabelString = false; | 132 bool clipLabelString = false; |
| 114 | 133 |
| 115 String urlString = url.string(); | 134 String urlString = url.string(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 142 } else | 161 } else |
| 143 imageSize.setWidth(std::max(labelSize.width(), urlStringSize.width()
) + kDragLabelBorderX * 2); | 162 imageSize.setWidth(std::max(labelSize.width(), urlStringSize.width()
) + kDragLabelBorderX * 2); |
| 144 } | 163 } |
| 145 | 164 |
| 146 // We now know how big the image needs to be, so we create and | 165 // We now know how big the image needs to be, so we create and |
| 147 // fill the background | 166 // fill the background |
| 148 IntSize scaledImageSize = imageSize; | 167 IntSize scaledImageSize = imageSize; |
| 149 scaledImageSize.scale(deviceScaleFactor); | 168 scaledImageSize.scale(deviceScaleFactor); |
| 150 OwnPtr<ImageBuffer> buffer(ImageBuffer::create(scaledImageSize, deviceScaleF
actor)); | 169 OwnPtr<ImageBuffer> buffer(ImageBuffer::create(scaledImageSize, deviceScaleF
actor)); |
| 151 if (!buffer) | 170 if (!buffer) |
| 152 return 0; | 171 return nullptr; |
| 153 | 172 |
| 154 const float DragLabelRadius = 5; | 173 const float DragLabelRadius = 5; |
| 155 const IntSize radii(DragLabelRadius, DragLabelRadius); | 174 const IntSize radii(DragLabelRadius, DragLabelRadius); |
| 156 IntRect rect(IntPoint(), imageSize); | 175 IntRect rect(IntPoint(), imageSize); |
| 157 const Color backgroundColor(140, 140, 140); | 176 const Color backgroundColor(140, 140, 140); |
| 158 buffer->context()->fillRoundedRect(rect, radii, radii, radii, radii, backgro
undColor); | 177 buffer->context()->fillRoundedRect(rect, radii, radii, radii, radii, backgro
undColor); |
| 159 | 178 |
| 160 // Draw the text | 179 // Draw the text |
| 161 if (drawURLString) { | 180 if (drawURLString) { |
| 162 if (clipURLString) | 181 if (clipURLString) |
| 163 urlString = StringTruncator::centerTruncate(urlString, imageSize.wid
th() - (kDragLabelBorderX * 2.0f), urlFont, StringTruncator::EnableRoundingHacks
); | 182 urlString = StringTruncator::centerTruncate(urlString, imageSize.wid
th() - (kDragLabelBorderX * 2.0f), urlFont, StringTruncator::EnableRoundingHacks
); |
| 164 IntPoint textPos(kDragLabelBorderX, imageSize.height() - (kLabelBorderYO
ffset + urlFont.fontMetrics().descent())); | 183 IntPoint textPos(kDragLabelBorderX, imageSize.height() - (kLabelBorderYO
ffset + urlFont.fontMetrics().descent())); |
| 165 TextRun textRun(urlString); | 184 TextRun textRun(urlString); |
| 166 buffer->context()->drawText(urlFont, TextRunPaintInfo(textRun), textPos)
; | 185 buffer->context()->drawText(urlFont, TextRunPaintInfo(textRun), textPos)
; |
| 167 } | 186 } |
| 168 | 187 |
| 169 if (clipLabelString) | 188 if (clipLabelString) |
| 170 label = StringTruncator::rightTruncate(label, imageSize.width() - (kDrag
LabelBorderX * 2.0f), labelFont, StringTruncator::EnableRoundingHacks); | 189 label = StringTruncator::rightTruncate(label, imageSize.width() - (kDrag
LabelBorderX * 2.0f), labelFont, StringTruncator::EnableRoundingHacks); |
| 171 | 190 |
| 172 IntPoint textPos(kDragLabelBorderX, kDragLabelBorderY + labelFont.pixelSize(
)); | 191 IntPoint textPos(kDragLabelBorderX, kDragLabelBorderY + labelFont.pixelSize(
)); |
| 173 TextRun textRun(label); | 192 TextRun textRun(label); |
| 174 buffer->context()->drawText(urlFont, TextRunPaintInfo(textRun), textPos); | 193 buffer->context()->drawText(urlFont, TextRunPaintInfo(textRun), textPos); |
| 175 | 194 |
| 176 RefPtr<Image> image = buffer->copyImage(); | 195 RefPtr<Image> image = buffer->copyImage(); |
| 177 return createDragImageFromImage(image.get()); | 196 return DragImage::create(image.get()); |
| 197 } |
| 198 |
| 199 DragImage::DragImage(const SkBitmap& bitmap, float resolutionScale) |
| 200 : m_bitmap(bitmap) |
| 201 , m_resolutionScale(resolutionScale) |
| 202 { |
| 203 } |
| 204 |
| 205 DragImage::~DragImage() |
| 206 { |
| 207 } |
| 208 |
| 209 void DragImage::fitToMaxSize(const IntSize& srcSize, const IntSize& maxSize) |
| 210 { |
| 211 float heightResizeRatio = 0.0f; |
| 212 float widthResizeRatio = 0.0f; |
| 213 float resizeRatio = -1.0f; |
| 214 IntSize originalSize = size(); |
| 215 |
| 216 if (srcSize.width() > maxSize.width()) { |
| 217 widthResizeRatio = maxSize.width() / static_cast<float>(srcSize.width())
; |
| 218 resizeRatio = widthResizeRatio; |
| 219 } |
| 220 |
| 221 if (srcSize.height() > maxSize.height()) { |
| 222 heightResizeRatio = maxSize.height() / static_cast<float>(srcSize.height
()); |
| 223 if ((resizeRatio < 0.0f) || (resizeRatio > heightResizeRatio)) |
| 224 resizeRatio = heightResizeRatio; |
| 225 } |
| 226 |
| 227 if (srcSize == originalSize) { |
| 228 if (resizeRatio > 0.0f) |
| 229 scale(resizeRatio, resizeRatio); |
| 230 return; |
| 231 } |
| 232 |
| 233 // The image was scaled in the webpage so at minimum we must account for tha
t scaling |
| 234 float scaleX = srcSize.width() / static_cast<float>(originalSize.width()); |
| 235 float scaleY = srcSize.height() / static_cast<float>(originalSize.height()); |
| 236 if (resizeRatio > 0.0f) { |
| 237 scaleX *= resizeRatio; |
| 238 scaleY *= resizeRatio; |
| 239 } |
| 240 |
| 241 scale(scaleX, scaleY); |
| 242 } |
| 243 |
| 244 void DragImage::scale(float scaleX, float scaleY) |
| 245 { |
| 246 int imageWidth = scaleX * m_bitmap.width(); |
| 247 int imageHeight = scaleY * m_bitmap.height(); |
| 248 m_bitmap = skia::ImageOperations::Resize( |
| 249 m_bitmap, skia::ImageOperations::RESIZE_LANCZOS3, imageWidth, imageHeigh
t); |
| 250 } |
| 251 |
| 252 void DragImage::dissolveToFraction(float fraction) |
| 253 { |
| 254 m_bitmap.setIsOpaque(false); |
| 255 SkAutoLockPixels lock(m_bitmap); |
| 256 |
| 257 for (int row = 0; row < m_bitmap.height(); ++row) { |
| 258 for (int column = 0; column < m_bitmap.width(); ++column) { |
| 259 uint32_t* pixel = m_bitmap.getAddr32(column, row); |
| 260 *pixel = SkPreMultiplyARGB( |
| 261 SkColorGetA(*pixel) * fraction, |
| 262 SkColorGetR(*pixel), |
| 263 SkColorGetG(*pixel), |
| 264 SkColorGetB(*pixel)); |
| 265 } |
| 266 } |
| 178 } | 267 } |
| 179 | 268 |
| 180 } // namespace WebCore | 269 } // namespace WebCore |
| 181 | |
| OLD | NEW |