diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-23 23:04:23 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-23 23:04:23 +0000 |
commit | 3024338797b31cd0e0357878bcd5b084e4a8af69 (patch) | |
tree | cf20d1d15f3907bb625a7cd1e465bbc47118821e /gfx/canvas_skia_mac.mm | |
parent | 218f45da1c19e775cf72f46cc72f1c624927fd93 (diff) | |
download | chromium_src-3024338797b31cd0e0357878bcd5b084e4a8af69.zip chromium_src-3024338797b31cd0e0357878bcd5b084e4a8af69.tar.gz chromium_src-3024338797b31cd0e0357878bcd5b084e4a8af69.tar.bz2 |
Canvas refactoring part 2.
- Rename Canvas to CanvasSkia.
- Create a subclass Canvas that inherits from CanvasSkia for compatibility.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/2862025
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50664 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gfx/canvas_skia_mac.mm')
-rw-r--r-- | gfx/canvas_skia_mac.mm | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/gfx/canvas_skia_mac.mm b/gfx/canvas_skia_mac.mm new file mode 100644 index 0000000..29102fd --- /dev/null +++ b/gfx/canvas_skia_mac.mm @@ -0,0 +1,84 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import <Cocoa/Cocoa.h> + +#include "gfx/canvas.h" + +#include "base/scoped_cftyperef.h" +#include "base/sys_string_conversions.h" +#include "gfx/font.h" +#include "gfx/rect.h" +#include "third_party/skia/include/core/SkShader.h" + +namespace gfx { + +CanvasSkia::CanvasSkia(int width, int height, bool is_opaque) + : skia::PlatformCanvas(width, height, is_opaque) { +} + +CanvasSkia::CanvasSkia() : skia::PlatformCanvas() { +} + +CanvasSkia::~CanvasSkia() { +} + +// static +void CanvasSkia::SizeStringInt(const std::wstring& text, + const gfx::Font& font, + int *width, int *height, int flags) { + NSFont* native_font = font.nativeFont(); + NSString* ns_string = base::SysWideToNSString(text); + NSDictionary* attributes = + [NSDictionary dictionaryWithObject:native_font + forKey:NSFontAttributeName]; + NSSize string_size = [ns_string sizeWithAttributes:attributes]; + *width = string_size.width; + *height = font.height(); +} + +void CanvasSkia::DrawStringInt(const std::wstring& text, const gfx::Font& font, + const SkColor& color, int x, int y, int w, int h, + int flags) { + if (!IntersectsClipRectInt(x, y, w, h)) + return; + + CGContextRef context = beginPlatformPaint(); + CGContextSaveGState(context); + + NSColor* ns_color = [NSColor colorWithDeviceRed:SkColorGetR(color) / 255.0 + green:SkColorGetG(color) / 255.0 + blue:SkColorGetB(color) / 255.0 + alpha:SkColorGetA(color) / 255.0]; + NSMutableParagraphStyle *ns_style = + [[[NSParagraphStyle alloc] init] autorelease]; + if (flags & TEXT_ALIGN_CENTER) + [ns_style setAlignment:NSCenterTextAlignment]; + // TODO(awalker): Implement the rest of the Canvas text flags + + NSDictionary* attributes = + [NSDictionary dictionaryWithObjectsAndKeys: + font.nativeFont(), NSFontAttributeName, + ns_color, NSForegroundColorAttributeName, + ns_style, NSParagraphStyleAttributeName, + nil]; + + NSAttributedString* ns_string = + [[[NSAttributedString alloc] initWithString:base::SysWideToNSString(text) + attributes:attributes] autorelease]; + scoped_cftyperef<CTFramesetterRef> framesetter( + CTFramesetterCreateWithAttributedString(reinterpret_cast<CFAttributedStringRef>(ns_string))); + + CGRect text_bounds = CGRectMake(x, y, w, h); + CGMutablePathRef path = CGPathCreateMutable(); + CGPathAddRect(path, NULL, text_bounds); + + scoped_cftyperef<CTFrameRef> frame( + CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL)); + CTFrameDraw(frame, context); + CGContextRestoreGState(context); + endPlatformPaint(); +} + +} // namespace gfx |