| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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 "app/gfx/canvas.h" | |
| 8 | |
| 9 #include "app/gfx/font.h" | |
| 10 #include "base/scoped_cftyperef.h" | |
| 11 #include "base/sys_string_conversions.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 *width = font.GetStringWidth(text); | |
| 32 *height = font.height(); | |
| 33 } | |
| 34 | |
| 35 void Canvas::DrawStringInt(const std::wstring& text, const gfx::Font& font, | |
| 36 const SkColor& color, int x, int y, int w, int h, | |
| 37 int flags) { | |
| 38 if (!IntersectsClipRectInt(x, y, w, h)) | |
| 39 return; | |
| 40 | |
| 41 CGContextRef context = beginPlatformPaint(); | |
| 42 CGContextSaveGState(context); | |
| 43 | |
| 44 NSColor* ns_color = [NSColor colorWithDeviceRed:SkColorGetR(color) / 255.0 | |
| 45 green:SkColorGetG(color) / 255.0 | |
| 46 blue:SkColorGetB(color) / 255.0 | |
| 47 alpha:1.0]; | |
| 48 NSMutableParagraphStyle *ns_style = | |
| 49 [[[NSParagraphStyle alloc] init] autorelease]; | |
| 50 if (flags & TEXT_ALIGN_CENTER) | |
| 51 [ns_style setAlignment:NSCenterTextAlignment]; | |
| 52 // TODO(awalker): Implement the rest of the Canvas text flags | |
| 53 | |
| 54 NSDictionary* attributes = | |
| 55 [NSDictionary dictionaryWithObjectsAndKeys: | |
| 56 font.nativeFont(), NSFontAttributeName, | |
| 57 ns_color, NSForegroundColorAttributeName, | |
| 58 ns_style, NSParagraphStyleAttributeName, | |
| 59 nil]; | |
| 60 | |
| 61 NSAttributedString* ns_string = | |
| 62 [[[NSAttributedString alloc] initWithString:base::SysWideToNSString(text) | |
| 63 attributes:attributes] autorelease]; | |
| 64 scoped_cftyperef<CTFramesetterRef> framesetter( | |
| 65 CTFramesetterCreateWithAttributedString(reinterpret_cast<CFAttributedStrin
gRef>(ns_string))); | |
| 66 | |
| 67 CGRect text_bounds = CGRectMake(x, y, w, h); | |
| 68 CGMutablePathRef path = CGPathCreateMutable(); | |
| 69 CGPathAddRect(path, NULL, text_bounds); | |
| 70 | |
| 71 scoped_cftyperef<CTFrameRef> frame( | |
| 72 CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL)); | |
| 73 CTFrameDraw(frame, context); | |
| 74 CGContextRestoreGState(context); | |
| 75 endPlatformPaint(); | |
| 76 } | |
| 77 | |
| 78 } // namespace gfx | |
| OLD | NEW |