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