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.h" | |
8 | |
9 #include "base/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 Canvas::Canvas(int width, int height, bool is_opaque) | |
18 : skia::PlatformCanvas(width, height, is_opaque) { | |
19 } | |
20 | |
21 Canvas::Canvas() : skia::PlatformCanvas() { | |
22 } | |
23 | |
24 Canvas::~Canvas() { | |
25 } | |
26 | |
27 // static | |
28 void Canvas::SizeStringInt(const std::wstring& text, | |
29 const gfx::Font& font, | |
30 int *width, int *height, int flags) { | |
31 NSFont* native_font = font.nativeFont(); | |
32 NSString* ns_string = base::SysWideToNSString(text); | |
33 NSDictionary* attributes = | |
34 [NSDictionary dictionaryWithObject:native_font | |
35 forKey:NSFontAttributeName]; | |
36 NSSize string_size = [ns_string sizeWithAttributes:attributes]; | |
37 *width = string_size.width; | |
38 *height = font.height(); | |
39 } | |
40 | |
41 void Canvas::DrawStringInt(const std::wstring& text, const gfx::Font& font, | |
42 const SkColor& color, int x, int y, int w, int h, | |
43 int flags) { | |
44 if (!IntersectsClipRectInt(x, y, w, h)) | |
45 return; | |
46 | |
47 CGContextRef context = beginPlatformPaint(); | |
48 CGContextSaveGState(context); | |
49 | |
50 NSColor* ns_color = [NSColor colorWithDeviceRed:SkColorGetR(color) / 255.0 | |
51 green:SkColorGetG(color) / 255.0 | |
52 blue:SkColorGetB(color) / 255.0 | |
53 alpha:SkColorGetA(color) / 255.0]; | |
54 NSMutableParagraphStyle *ns_style = | |
55 [[[NSParagraphStyle alloc] init] autorelease]; | |
56 if (flags & TEXT_ALIGN_CENTER) | |
57 [ns_style setAlignment:NSCenterTextAlignment]; | |
58 // TODO(awalker): Implement the rest of the Canvas text flags | |
59 | |
60 NSDictionary* attributes = | |
61 [NSDictionary dictionaryWithObjectsAndKeys: | |
62 font.nativeFont(), NSFontAttributeName, | |
63 ns_color, NSForegroundColorAttributeName, | |
64 ns_style, NSParagraphStyleAttributeName, | |
65 nil]; | |
66 | |
67 NSAttributedString* ns_string = | |
68 [[[NSAttributedString alloc] initWithString:base::SysWideToNSString(text) | |
69 attributes:attributes] autorelease]; | |
70 scoped_cftyperef<CTFramesetterRef> framesetter( | |
71 CTFramesetterCreateWithAttributedString(reinterpret_cast<CFAttributedStrin
gRef>(ns_string))); | |
72 | |
73 CGRect text_bounds = CGRectMake(x, y, w, h); | |
74 CGMutablePathRef path = CGPathCreateMutable(); | |
75 CGPathAddRect(path, NULL, text_bounds); | |
76 | |
77 scoped_cftyperef<CTFrameRef> frame( | |
78 CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL)); | |
79 CTFrameDraw(frame, context); | |
80 CGContextRestoreGState(context); | |
81 endPlatformPaint(); | |
82 } | |
83 | |
84 } // namespace gfx | |
OLD | NEW |