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