2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "platform/DragImage.h"
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"
58 const float kDragLabelBorderX = 4;
59 // Keep border_y in synch with DragController::LinkDragBorderInset.
60 const float kDragLabelBorderY = 2;
61 const float kLabelBorderYOffset = 2;
63 const float kMaxDragLabelWidth = 300;
64 const float kMaxDragLabelStringWidth = (kMaxDragLabelWidth - 2 * kDragLabelBorderX);
66 const float kDragLinkLabelFontSize = 11;
67 const float kDragLinkUrlFontSize = 10;
69 PassOwnPtr<DragImage> DragImage::create(Image* image, RespectImageOrientationEnum shouldRespectImageOrientation, float deviceScaleFactor)
74 RefPtr<NativeImageSkia> bitmap = image->nativeImageForCurrentFrame();
78 if (image->isBitmapImage()) {
79 ImageOrientation orientation = DefaultImageOrientation;
80 BitmapImage* bitmapImage = toBitmapImage(image);
81 IntSize sizeRespectingOrientation = bitmapImage->sizeRespectingOrientation();
83 if (shouldRespectImageOrientation == RespectImageOrientation)
84 orientation = bitmapImage->currentFrameOrientation();
86 if (orientation != DefaultImageOrientation) {
87 FloatRect destRect(FloatPoint(), sizeRespectingOrientation);
88 if (orientation.usesWidthAsHeight())
89 destRect = destRect.transposedRect();
93 SkBitmap::kARGB_8888_Config, sizeRespectingOrientation.width(), sizeRespectingOrientation.height());
94 if (!skBitmap.allocPixels())
97 SkCanvas canvas(skBitmap);
98 canvas.concat(affineTransformToSkMatrix(orientation.transformFromDefault(sizeRespectingOrientation)));
99 canvas.drawBitmapRect(bitmap->bitmap(), 0, destRect);
101 return adoptPtr(new DragImage(skBitmap, deviceScaleFactor));
106 if (!bitmap->bitmap().copyTo(&skBitmap, SkBitmap::kARGB_8888_Config))
108 return adoptPtr(new DragImage(skBitmap, deviceScaleFactor));
111 static Font deriveDragLabelFont(int size, FontWeight fontWeight, const FontDescription& systemFont)
113 FontDescription description = systemFont;
114 description.setWeight(fontWeight);
115 description.setSpecifiedSize(size);
116 description.setComputedSize(size);
117 Font result(description);
122 PassOwnPtr<DragImage> DragImage::create(const KURL& url, const String& inLabel, const FontDescription& systemFont, float deviceScaleFactor)
124 const Font labelFont = deriveDragLabelFont(kDragLinkLabelFontSize, FontWeightBold, systemFont);
125 const Font urlFont = deriveDragLabelFont(kDragLinkUrlFontSize, FontWeightNormal, systemFont);
126 FontCachePurgePreventer fontCachePurgePreventer;
128 bool drawURLString = true;
129 bool clipURLString = false;
130 bool clipLabelString = false;
132 String urlString = url.string();
133 String label = inLabel.stripWhiteSpace();
134 if (label.isEmpty()) {
135 drawURLString = false;
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());
144 if (labelSize.width() > kMaxDragLabelStringWidth) {
145 labelSize.setWidth(kMaxDragLabelStringWidth);
146 clipLabelString = true;
149 IntSize urlStringSize;
150 IntSize imageSize(labelSize.width() + kDragLabelBorderX * 2, labelSize.height() + kDragLabelBorderY * 2);
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;
160 imageSize.setWidth(std::max(labelSize.width(), urlStringSize.width()) + kDragLabelBorderX * 2);
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));
170 buffer->context()->scale(FloatSize(deviceScaleFactor, deviceScaleFactor));
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);
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);
188 label = StringTruncator::rightTruncate(label, imageSize.width() - (kDragLabelBorderX * 2.0f), labelFont, StringTruncator::EnableRoundingHacks);
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));
198 buffer->context()->drawBidiText(urlFont, TextRunPaintInfo(textRun), textPos);
200 RefPtr<Image> image = buffer->copyImage();
201 return DragImage::create(image.get(), DoNotRespectImageOrientation, deviceScaleFactor);
204 DragImage::DragImage(const SkBitmap& bitmap, float resolutionScale)
206 , m_resolutionScale(resolutionScale)
210 DragImage::~DragImage()
214 void DragImage::fitToMaxSize(const IntSize& srcSize, const IntSize& maxSize)
216 float heightResizeRatio = 0.0f;
217 float widthResizeRatio = 0.0f;
218 float resizeRatio = -1.0f;
219 IntSize originalSize = size();
221 if (srcSize.width() > maxSize.width()) {
222 widthResizeRatio = maxSize.width() / static_cast<float>(srcSize.width());
223 resizeRatio = widthResizeRatio;
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;
232 if (srcSize == originalSize) {
233 if (resizeRatio > 0.0f)
234 scale(resizeRatio, resizeRatio);
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;
246 scale(scaleX, scaleY);
249 void DragImage::scale(float scaleX, float scaleY)
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);
257 void DragImage::dissolveToFraction(float fraction)
259 m_bitmap.setAlphaType(kPremul_SkAlphaType);
260 SkAutoLockPixels lock(m_bitmap);
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,
269 SkColorGetB(*pixel));
274 } // namespace WebCore