OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #import <Cocoa/Cocoa.h> | |
6 | |
7 #include "gfx/canvas_skia.h" | |
8 | |
9 #include "base/mac/scoped_cftyperef.h" | |
10 #include "base/sys_string_conversions.h" | |
11 #include "gfx/font.h" | |
12 #include "gfx/rect.h" | |
13 #include "third_party/skia/include/core/SkShader.h" | |
14 | |
15 namespace gfx { | |
16 | |
17 CanvasSkia::CanvasSkia(int width, int height, bool is_opaque) | |
18 : skia::PlatformCanvas(width, height, is_opaque) { | |
19 } | |
20 | |
21 CanvasSkia::CanvasSkia() : skia::PlatformCanvas() { | |
22 } | |
23 | |
24 CanvasSkia::~CanvasSkia() { | |
25 } | |
26 | |
27 // static | |
28 void CanvasSkia::SizeStringInt(const string16& text, | |
29 const gfx::Font& font, | |
30 int* width, int* height, | |
31 int flags) { | |
32 NSFont* native_font = font.GetNativeFont(); | |
33 NSString* ns_string = base::SysUTF16ToNSString(text); | |
34 NSDictionary* attributes = | |
35 [NSDictionary dictionaryWithObject:native_font | |
36 forKey:NSFontAttributeName]; | |
37 NSSize string_size = [ns_string sizeWithAttributes:attributes]; | |
38 *width = string_size.width; | |
39 *height = font.GetHeight(); | |
40 } | |
41 | |
42 void CanvasSkia::DrawStringInt(const string16& text, | |
43 const gfx::Font& font, | |
44 const SkColor& color, | |
45 int x, int y, int w, int h, | |
46 int flags) { | |
47 if (!IntersectsClipRectInt(x, y, w, h)) | |
48 return; | |
49 | |
50 CGContextRef context = beginPlatformPaint(); | |
51 CGContextSaveGState(context); | |
52 | |
53 NSColor* ns_color = [NSColor colorWithDeviceRed:SkColorGetR(color) / 255.0 | |
54 green:SkColorGetG(color) / 255.0 | |
55 blue:SkColorGetB(color) / 255.0 | |
56 alpha:SkColorGetA(color) / 255.0]; | |
57 NSMutableParagraphStyle *ns_style = | |
58 [[[NSParagraphStyle alloc] init] autorelease]; | |
59 if (flags & TEXT_ALIGN_CENTER) | |
60 [ns_style setAlignment:NSCenterTextAlignment]; | |
61 // TODO(awalker): Implement the rest of the Canvas text flags | |
62 | |
63 NSDictionary* attributes = | |
64 [NSDictionary dictionaryWithObjectsAndKeys: | |
65 font.GetNativeFont(), NSFontAttributeName, | |
66 ns_color, NSForegroundColorAttributeName, | |
67 ns_style, NSParagraphStyleAttributeName, | |
68 nil]; | |
69 | |
70 NSAttributedString* ns_string = | |
71 [[[NSAttributedString alloc] initWithString:base::SysUTF16ToNSString(text) | |
72 attributes:attributes] autorelease]; | |
73 base::mac::ScopedCFTypeRef<CTFramesetterRef> framesetter( | |
74 CTFramesetterCreateWithAttributedString( | |
75 reinterpret_cast<CFAttributedStringRef>(ns_string))); | |
76 | |
77 CGRect text_bounds = CGRectMake(x, y, w, h); | |
78 CGMutablePathRef path = CGPathCreateMutable(); | |
79 CGPathAddRect(path, NULL, text_bounds); | |
80 | |
81 base::mac::ScopedCFTypeRef<CTFrameRef> frame( | |
82 CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL)); | |
83 CTFrameDraw(frame, context); | |
84 CGContextRestoreGState(context); | |
85 endPlatformPaint(); | |
86 } | |
87 | |
88 } // namespace gfx | |
OLD | NEW |