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