Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(870)

Side by Side Diff: Source/WebCore/platform/mac/DragImageMac.mm

Issue 13713003: Remove all of WebCore/platform/mac which is not mentioned in WebCore.gypi. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Added back a couple needed headers Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/WebCore/platform/mac/DragDataMac.mm ('k') | Source/WebCore/platform/mac/EventLoopMac.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2007, 2009, 2012 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 #import "config.h"
27 #import "DragImage.h"
28
29 #if ENABLE(DRAG_SUPPORT)
30 #import "BitmapImage.h"
31 #import "CachedImage.h"
32 #import "Font.h"
33 #import "FontCache.h"
34 #import "FontDescription.h"
35 #import "FontSelector.h"
36 #import "GraphicsContext.h"
37 #import "Image.h"
38 #import "KURL.h"
39 #import "ResourceResponse.h"
40 #import "Settings.h"
41 #import "StringTruncator.h"
42 #import "TextRun.h"
43
44 namespace WebCore {
45
46 IntSize dragImageSize(RetainPtr<NSImage> image)
47 {
48 return (IntSize)[image.get() size];
49 }
50
51 void deleteDragImage(RetainPtr<NSImage>)
52 {
53 // Since this is a RetainPtr, there's nothing additional we need to do to
54 // delete it. It will be released when it falls out of scope.
55 }
56
57 RetainPtr<NSImage> scaleDragImage(RetainPtr<NSImage> image, FloatSize scale)
58 {
59 NSSize originalSize = [image.get() size];
60 NSSize newSize = NSMakeSize((originalSize.width * scale.width()), (originalS ize.height * scale.height()));
61 newSize.width = roundf(newSize.width);
62 newSize.height = roundf(newSize.height);
63 [image.get() setScalesWhenResized:YES];
64 [image.get() setSize:newSize];
65 return image;
66 }
67
68 RetainPtr<NSImage> dissolveDragImageToFraction(RetainPtr<NSImage> image, float d elta)
69 {
70 RetainPtr<NSImage> dissolvedImage(AdoptNS, [[NSImage alloc] initWithSize:[im age.get() size]]);
71
72 [dissolvedImage.get() lockFocus];
73 [image.get() drawAtPoint:NSZeroPoint fromRect:NSMakeRect(0, 0, [image size]. width, [image size].height) operation:NSCompositeCopy fraction:delta];
74 [dissolvedImage.get() unlockFocus];
75
76 return dissolvedImage;
77 }
78
79 RetainPtr<NSImage> createDragImageFromImage(Image* image, RespectImageOrientatio nEnum shouldRespectImageOrientation)
80 {
81 IntSize size = image->size();
82
83 if (image->isBitmapImage()) {
84 ImageOrientation orientation = DefaultImageOrientation;
85 BitmapImage* bitmapImage = static_cast<BitmapImage *>(image);
86 IntSize sizeRespectingOrientation = bitmapImage->sizeRespectingOrientati on();
87
88 if (shouldRespectImageOrientation == RespectImageOrientation)
89 orientation = bitmapImage->currentFrameOrientation();
90
91 if (orientation != DefaultImageOrientation) {
92 // Construct a correctly-rotated copy of the image to use as the dra g image.
93 FloatRect destRect(FloatPoint(), sizeRespectingOrientation);
94
95 RetainPtr<NSImage> rotatedDragImage(AdoptNS, [[NSImage alloc] initWi thSize:(NSSize)(sizeRespectingOrientation)]);
96 [rotatedDragImage.get() lockFocus];
97
98 // ImageOrientation uses top-left coordinates, need to flip to botto m-left, apply...
99 CGAffineTransform transform = CGAffineTransformMakeTranslation(0, de stRect.height());
100 transform = CGAffineTransformScale(transform, 1, -1);
101 transform = CGAffineTransformConcat(orientation.transformFromDefault (sizeRespectingOrientation), transform);
102
103 if (orientation.usesWidthAsHeight())
104 destRect = FloatRect(destRect.x(), destRect.y(), destRect.height (), destRect.width());
105
106 // ...and flip back.
107 transform = CGAffineTransformTranslate(transform, 0, destRect.height ());
108 transform = CGAffineTransformScale(transform, 1, -1);
109
110 RetainPtr<NSAffineTransform> cocoaTransform(AdoptNS, [[NSAffineTrans form alloc] init]);
111 [cocoaTransform.get() setTransformStruct:*(NSAffineTransformStruct*) &transform];
112 [cocoaTransform.get() concat];
113
114 [image->getNSImage() drawInRect:destRect fromRect:NSMakeRect(0, 0, s ize.width(), size.height()) operation:NSCompositeSourceOver fraction:1.0];
115 [rotatedDragImage.get() unlockFocus];
116
117 return rotatedDragImage;
118 }
119 }
120
121 RetainPtr<NSImage> dragImage(AdoptNS, [image->getNSImage() copy]);
122 [dragImage.get() setSize:(NSSize)size];
123 return dragImage;
124 }
125
126 RetainPtr<NSImage> createDragImageIconForCachedImage(CachedImage* image)
127 {
128 const String& filename = image->response().suggestedFilename();
129 NSString *extension = nil;
130 size_t dotIndex = filename.reverseFind('.');
131
132 if (dotIndex != notFound && dotIndex < (filename.length() - 1)) // require t hat a . exists after the first character and before the last
133 extension = filename.substring(dotIndex + 1);
134 else {
135 // It might be worth doing a further lookup to pull the extension from t he MIME type.
136 extension = @"";
137 }
138
139 return [[NSWorkspace sharedWorkspace] iconForFileType:extension];
140 }
141
142
143 const float DragLabelBorderX = 4;
144 //Keep border_y in synch with DragController::LinkDragBorderInset
145 const float DragLabelBorderY = 2;
146 const float DragLabelRadius = 5;
147 const float LabelBorderYOffset = 2;
148
149 const float MinDragLabelWidthBeforeClip = 120;
150 const float MaxDragLabelWidth = 320;
151
152 const float DragLinkLabelFontsize = 11;
153 const float DragLinkUrlFontSize = 10;
154
155 // FIXME - we should move all the functionality of NSString extras to WebCore
156
157 static Font& fontFromNSFont(NSFont *font)
158 {
159 static NSFont *currentFont;
160 DEFINE_STATIC_LOCAL(Font, currentRenderer, ());
161
162 if ([font isEqual:currentFont])
163 return currentRenderer;
164 if (currentFont)
165 CFRelease(currentFont);
166 currentFont = font;
167 CFRetain(currentFont);
168 FontPlatformData f(font, [font pointSize]);
169 currentRenderer = Font(f, ![[NSGraphicsContext currentContext] isDrawingToSc reen]);
170 return currentRenderer;
171 }
172
173 static bool canUseFastRenderer(const UniChar* buffer, unsigned length)
174 {
175 unsigned i;
176 for (i = 0; i < length; i++) {
177 UCharDirection direction = u_charDirection(buffer[i]);
178 if (direction == U_RIGHT_TO_LEFT || direction > U_OTHER_NEUTRAL)
179 return false;
180 }
181 return true;
182 }
183
184 static float widthWithFont(NSString *string, NSFont *font)
185 {
186 unsigned length = [string length];
187 Vector<UniChar, 2048> buffer(length);
188
189 [string getCharacters:buffer.data()];
190
191 if (canUseFastRenderer(buffer.data(), length)) {
192 FontCachePurgePreventer fontCachePurgePreventer;
193
194 Font webCoreFont(FontPlatformData(font, [font pointSize]), ![[NSGraphics Context currentContext] isDrawingToScreen]);
195 TextRun run(buffer.data(), length);
196 run.disableRoundingHacks();
197 return webCoreFont.width(run);
198 }
199
200 return [string sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys :font, NSFontAttributeName, nil]].width;
201 }
202
203 static inline CGFloat webkit_CGCeiling(CGFloat value)
204 {
205 if (sizeof(value) == sizeof(float))
206 return ceilf(value);
207 return static_cast<CGFloat>(ceil(value));
208 }
209
210 static void drawAtPoint(NSString *string, NSPoint point, NSFont *font, NSColor * textColor)
211 {
212 unsigned length = [string length];
213 Vector<UniChar, 2048> buffer(length);
214
215 [string getCharacters:buffer.data()];
216
217 if (canUseFastRenderer(buffer.data(), length)) {
218 FontCachePurgePreventer fontCachePurgePreventer;
219
220 // The following is a half-assed attempt to match AppKit's rounding rule s for drawAtPoint.
221 // It's probably incorrect for high DPI.
222 // If you change this, be sure to test all the text drawn this way in Sa fari, including
223 // the status bar, bookmarks bar, tab bar, and activity window.
224 point.y = webkit_CGCeiling(point.y);
225
226 NSGraphicsContext *nsContext = [NSGraphicsContext currentContext];
227 CGContextRef cgContext = static_cast<CGContextRef>([nsContext graphicsPo rt]);
228 GraphicsContext graphicsContext(cgContext);
229
230 // Safari doesn't flip the NSGraphicsContext before calling WebKit, yet WebCore requires a flipped graphics context.
231 BOOL flipped = [nsContext isFlipped];
232 if (!flipped)
233 CGContextScaleCTM(cgContext, 1, -1);
234
235 Font webCoreFont(FontPlatformData(font, [font pointSize]), ![nsContext i sDrawingToScreen], Antialiased);
236 TextRun run(buffer.data(), length);
237 run.disableRoundingHacks();
238
239 CGFloat red;
240 CGFloat green;
241 CGFloat blue;
242 CGFloat alpha;
243 [[textColor colorUsingColorSpaceName:NSDeviceRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
244 graphicsContext.setFillColor(makeRGBA(red * 255, green * 255, blue * 255 , alpha * 255), ColorSpaceDeviceRGB);
245
246 webCoreFont.drawText(&graphicsContext, run, FloatPoint(point.x, (flipped ? point.y : (-1 * point.y))));
247
248 if (!flipped)
249 CGContextScaleCTM(cgContext, 1, -1);
250 } else {
251 // The given point is on the baseline.
252 if ([[NSView focusView] isFlipped])
253 point.y -= [font ascender];
254 else
255 point.y += [font descender];
256
257 [string drawAtPoint:point withAttributes:[NSDictionary dictionaryWithObj ectsAndKeys:font, NSFontAttributeName, textColor, NSForegroundColorAttributeName , nil]];
258 }
259 }
260
261 static void drawDoubledAtPoint(NSString *string, NSPoint textPoint, NSColor *top Color, NSColor *bottomColor, NSFont *font)
262 {
263 // turn off font smoothing so translucent text draws correctly (Radar 31 18455)
264 drawAtPoint(string, textPoint, font, bottomColor);
265
266 textPoint.y += 1;
267 drawAtPoint(string, textPoint, font, topColor);
268 }
269
270 DragImageRef createDragImageForLink(KURL& url, const String& title, Frame* frame )
271 {
272 if (!frame)
273 return nil;
274 NSString *label = nsStringNilIfEmpty(title);
275 NSURL *cocoaURL = url;
276 NSString *urlString = [cocoaURL absoluteString];
277
278 BOOL drawURLString = YES;
279 BOOL clipURLString = NO;
280 BOOL clipLabelString = NO;
281
282 if (!label) {
283 drawURLString = NO;
284 label = urlString;
285 }
286
287 NSFont *labelFont = [[NSFontManager sharedFontManager] convertFont:[NSFont s ystemFontOfSize:DragLinkLabelFontsize]
288 toHaveTrait:NSBoldFon tMask];
289 NSFont *urlFont = [NSFont systemFontOfSize:DragLinkUrlFontSize];
290 NSSize labelSize;
291 labelSize.width = widthWithFont(label, labelFont);
292 labelSize.height = [labelFont ascender] - [labelFont descender];
293 if (labelSize.width > MaxDragLabelWidth){
294 labelSize.width = MaxDragLabelWidth;
295 clipLabelString = YES;
296 }
297
298 NSSize imageSize;
299 imageSize.width = labelSize.width + DragLabelBorderX * 2;
300 imageSize.height = labelSize.height + DragLabelBorderY * 2;
301 if (drawURLString) {
302 NSSize urlStringSize;
303 urlStringSize.width = widthWithFont(urlString, urlFont);
304 urlStringSize.height = [urlFont ascender] - [urlFont descender];
305 imageSize.height += urlStringSize.height;
306 if (urlStringSize.width > MaxDragLabelWidth) {
307 imageSize.width = std::max(MaxDragLabelWidth + DragLabelBorderY * 2, MinDragLabelWidthBeforeClip);
308 clipURLString = YES;
309 } else
310 imageSize.width = std::max(labelSize.width + DragLabelBorderX * 2, u rlStringSize.width + DragLabelBorderX * 2);
311 }
312 NSImage *dragImage = [[[NSImage alloc] initWithSize: imageSize] autorelease] ;
313 [dragImage lockFocus];
314
315 [[NSColor colorWithDeviceRed: 0.7f green: 0.7f blue: 0.7f alpha: 0.8f] set];
316
317 // Drag a rectangle with rounded corners
318 NSBezierPath *path = [NSBezierPath bezierPath];
319 [path appendBezierPathWithOvalInRect: NSMakeRect(0, 0, DragLabelRadius * 2, DragLabelRadius * 2)];
320 [path appendBezierPathWithOvalInRect: NSMakeRect(0, imageSize.height - DragL abelRadius * 2, DragLabelRadius * 2, DragLabelRadius * 2)];
321 [path appendBezierPathWithOvalInRect: NSMakeRect(imageSize.width - DragLabel Radius * 2, imageSize.height - DragLabelRadius * 2, DragLabelRadius * 2, DragLab elRadius * 2)];
322 [path appendBezierPathWithOvalInRect: NSMakeRect(imageSize.width - DragLabel Radius * 2, 0, DragLabelRadius * 2, DragLabelRadius * 2)];
323
324 [path appendBezierPathWithRect: NSMakeRect(DragLabelRadius, 0, imageSize.wid th - DragLabelRadius * 2, imageSize.height)];
325 [path appendBezierPathWithRect: NSMakeRect(0, DragLabelRadius, DragLabelRadi us + 10, imageSize.height - 2 * DragLabelRadius)];
326 [path appendBezierPathWithRect: NSMakeRect(imageSize.width - DragLabelRadius - 20, DragLabelRadius, DragLabelRadius + 20, imageSize.height - 2 * DragLabelRa dius)];
327 [path fill];
328
329 NSColor *topColor = [NSColor colorWithDeviceWhite:0.0f alpha:0.75f];
330 NSColor *bottomColor = [NSColor colorWithDeviceWhite:1.0f alpha:0.5f];
331 if (drawURLString) {
332 if (clipURLString)
333 urlString = StringTruncator::centerTruncate(urlString, imageSize.wid th - (DragLabelBorderX * 2), fontFromNSFont(urlFont));
334
335 drawDoubledAtPoint(urlString, NSMakePoint(DragLabelBorderX, DragLabelBord erY - [urlFont descender]), topColor, bottomColor, urlFont);
336 }
337
338 if (clipLabelString)
339 label = StringTruncator::rightTruncate(label, imageSize.width - (DragLab elBorderX * 2), fontFromNSFont(labelFont));
340 drawDoubledAtPoint(label, NSMakePoint(DragLabelBorderX, imageSize.height - L abelBorderYOffset - [labelFont pointSize]), topColor, bottomColor, labelFont);
341
342 [dragImage unlockFocus];
343
344 return dragImage;
345 }
346
347 } // namespace WebCore
348
349 #endif // ENABLE(DRAG_SUPPORT)
OLDNEW
« no previous file with comments | « Source/WebCore/platform/mac/DragDataMac.mm ('k') | Source/WebCore/platform/mac/EventLoopMac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698