[email protected] | 7c1e647e | 2012-01-14 01:07:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 6bf1a81 | 2009-07-11 01:57:28 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | d7c91966 | 2012-01-12 20:15:58 | [diff] [blame] | 5 | #import <Cocoa/Cocoa.h> |
| 6 | |
[email protected] | 94a0d258 | 2012-03-09 00:30:59 | [diff] [blame] | 7 | #include "ui/gfx/canvas.h" |
[email protected] | 6bf1a81 | 2009-07-11 01:57:28 | [diff] [blame] | 8 | |
[email protected] | 7c1e647e | 2012-01-14 01:07:57 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | d7c91966 | 2012-01-12 20:15:58 | [diff] [blame] | 10 | #include "base/sys_string_conversions.h" |
[email protected] | 7c1e647e | 2012-01-14 01:07:57 | [diff] [blame] | 11 | #include "third_party/skia/include/core/SkTypeface.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 12 | #include "ui/gfx/font.h" |
[email protected] | 0834e1b1 | 2012-04-11 02:23:56 | [diff] [blame] | 13 | #include "ui/gfx/rect.h" |
[email protected] | 7c1e647e | 2012-01-14 01:07:57 | [diff] [blame] | 14 | |
| 15 | // Note: This is a temporary Skia-based implementation of the ui/gfx text |
| 16 | // rendering routines for views/aura. It replaces the stale Cocoa-based |
[email protected] | 62424a5 | 2012-03-18 03:09:50 | [diff] [blame] | 17 | // implementation. A future |canvas_skia.cc| implementation will supersede |
[email protected] | 7c1e647e | 2012-01-14 01:07:57 | [diff] [blame] | 18 | // this and the other platform-specific implmenentations. |
| 19 | // Most drawing options, such as alignment and multi-line, are not implemented |
| 20 | // here. |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | SkTypeface::Style FontTypefaceStyle(const gfx::Font& font) { |
| 25 | int style = 0; |
| 26 | if (font.GetStyle() & gfx::Font::BOLD) |
| 27 | style |= SkTypeface::kBold; |
| 28 | if (font.GetStyle() & gfx::Font::ITALIC) |
| 29 | style |= SkTypeface::kItalic; |
| 30 | |
| 31 | return static_cast<SkTypeface::Style>(style); |
| 32 | } |
| 33 | |
| 34 | } // namespace |
[email protected] | 6bf1a81 | 2009-07-11 01:57:28 | [diff] [blame] | 35 | |
| 36 | namespace gfx { |
| 37 | |
[email protected] | 6bf1a81 | 2009-07-11 01:57:28 | [diff] [blame] | 38 | // static |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 39 | void Canvas::SizeStringInt(const string16& text, |
| 40 | const gfx::Font& font, |
| 41 | int* width, |
| 42 | int* height, |
| 43 | int flags) { |
[email protected] | d7c91966 | 2012-01-12 20:15:58 | [diff] [blame] | 44 | NSFont* native_font = font.GetNativeFont(); |
| 45 | NSString* ns_string = base::SysUTF16ToNSString(text); |
| 46 | NSDictionary* attributes = |
| 47 | [NSDictionary dictionaryWithObject:native_font |
| 48 | forKey:NSFontAttributeName]; |
| 49 | NSSize string_size = [ns_string sizeWithAttributes:attributes]; |
| 50 | *width = string_size.width; |
| 51 | *height = font.GetHeight(); |
[email protected] | 6bf1a81 | 2009-07-11 01:57:28 | [diff] [blame] | 52 | } |
| 53 | |
[email protected] | 0834e1b1 | 2012-04-11 02:23:56 | [diff] [blame] | 54 | void Canvas::DrawStringWithShadows(const string16& text, |
| 55 | const gfx::Font& font, |
| 56 | SkColor color, |
| 57 | const gfx::Rect& text_bounds, |
| 58 | int flags, |
[email protected] | 2565068 | 2012-05-29 23:09:19 | [diff] [blame] | 59 | const ShadowValues& shadows) { |
[email protected] | 0834e1b1 | 2012-04-11 02:23:56 | [diff] [blame] | 60 | DLOG_IF(WARNING, !shadows.empty()) << "Text shadow not implemented."; |
| 61 | |
[email protected] | 7c1e647e | 2012-01-14 01:07:57 | [diff] [blame] | 62 | SkTypeface* typeface = SkTypeface::CreateFromName(font.GetFontName().c_str(), |
| 63 | FontTypefaceStyle(font)); |
| 64 | SkPaint paint; |
| 65 | paint.setTypeface(typeface); |
| 66 | typeface->unref(); |
| 67 | paint.setColor(color); |
| 68 | canvas_->drawText(text.c_str(), |
| 69 | text.size() * sizeof(string16::value_type), |
[email protected] | 0834e1b1 | 2012-04-11 02:23:56 | [diff] [blame] | 70 | text_bounds.x(), |
| 71 | text_bounds.bottom(), |
[email protected] | 7c1e647e | 2012-01-14 01:07:57 | [diff] [blame] | 72 | paint); |
| 73 | } |
[email protected] | 6bf1a81 | 2009-07-11 01:57:28 | [diff] [blame] | 74 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 75 | void Canvas::DrawStringWithHalo(const string16& text, |
| 76 | const gfx::Font& font, |
[email protected] | 1b81ff2 | 2012-03-24 22:04:42 | [diff] [blame] | 77 | SkColor text_color, |
| 78 | SkColor halo_color, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 79 | int x, int y, int w, int h, |
| 80 | int flags) { |
[email protected] | 6bf1a81 | 2009-07-11 01:57:28 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 6bf1a81 | 2009-07-11 01:57:28 | [diff] [blame] | 83 | } // namespace gfx |