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_list.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. Most drawing options, | 18 // this and the other platform-specific implmenentations. Most drawing options, |
19 // such as alignment, multi-line, and line heights are not implemented here. | 19 // such as alignment, multi-line, and line heights are not implemented here. |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 SkTypeface::Style FontTypefaceStyle(const gfx::Font& font) { | 23 SkTypeface::Style FontTypefaceStyle(const gfx::Font& font) { |
24 int style = 0; | 24 int style = 0; |
25 if (font.GetStyle() & gfx::Font::BOLD) | 25 if (font.GetStyle() & gfx::Font::BOLD) |
26 style |= SkTypeface::kBold; | 26 style |= SkTypeface::kBold; |
27 if (font.GetStyle() & gfx::Font::ITALIC) | 27 if (font.GetStyle() & gfx::Font::ITALIC) |
28 style |= SkTypeface::kItalic; | 28 style |= SkTypeface::kItalic; |
29 | 29 |
30 return static_cast<SkTypeface::Style>(style); | 30 return static_cast<SkTypeface::Style>(style); |
31 } | 31 } |
32 | 32 |
33 } // namespace | 33 } // namespace |
34 | 34 |
35 namespace gfx { | 35 namespace gfx { |
36 | 36 |
37 // static | 37 // static |
38 void Canvas::SizeStringInt(const base::string16& text, | 38 void Canvas::SizeStringInt(const base::string16& text, |
39 const gfx::Font& font, | 39 const gfx::FontList& font_list, |
40 int* width, | 40 int* width, |
41 int* height, | 41 int* height, |
42 int line_height, | 42 int line_height, |
43 int flags) { | 43 int flags) { |
44 DLOG_IF(WARNING, line_height != 0) << "Line heights not implemented."; | 44 DLOG_IF(WARNING, line_height != 0) << "Line heights not implemented."; |
45 DLOG_IF(WARNING, flags & Canvas::MULTI_LINE) << "Multi-line not implemented."; | 45 DLOG_IF(WARNING, flags & Canvas::MULTI_LINE) << "Multi-line not implemented."; |
46 | 46 |
47 NSFont* native_font = font.GetNativeFont(); | 47 NSFont* native_font = font_list.GetPrimaryFont().GetNativeFont(); |
48 NSString* ns_string = base::SysUTF16ToNSString(text); | 48 NSString* ns_string = base::SysUTF16ToNSString(text); |
49 NSDictionary* attributes = | 49 NSDictionary* attributes = |
50 [NSDictionary dictionaryWithObject:native_font | 50 [NSDictionary dictionaryWithObject:native_font |
51 forKey:NSFontAttributeName]; | 51 forKey:NSFontAttributeName]; |
52 NSSize string_size = [ns_string sizeWithAttributes:attributes]; | 52 NSSize string_size = [ns_string sizeWithAttributes:attributes]; |
53 *width = string_size.width; | 53 *width = string_size.width; |
54 *height = font.GetHeight(); | 54 *height = font_list.GetHeight(); |
| 55 } |
| 56 |
| 57 // static |
| 58 void Canvas::SizeStringInt(const base::string16& text, |
| 59 const gfx::Font& font, |
| 60 int* width, |
| 61 int* height, |
| 62 int line_height, |
| 63 int flags) { |
| 64 SizeStringInt(text, FontList(font), width, height, line_height, flags); |
55 } | 65 } |
56 | 66 |
57 void Canvas::DrawStringWithShadows(const base::string16& text, | 67 void Canvas::DrawStringWithShadows(const base::string16& text, |
58 const gfx::Font& font, | 68 const gfx::FontList& font_list, |
59 SkColor color, | 69 SkColor color, |
60 const gfx::Rect& text_bounds, | 70 const gfx::Rect& text_bounds, |
61 int line_height, | 71 int line_height, |
62 int flags, | 72 int flags, |
63 const ShadowValues& shadows) { | 73 const ShadowValues& shadows) { |
64 DLOG_IF(WARNING, line_height != 0) << "Line heights not implemented."; | 74 DLOG_IF(WARNING, line_height != 0) << "Line heights not implemented."; |
65 DLOG_IF(WARNING, flags & Canvas::MULTI_LINE) << "Multi-line not implemented."; | 75 DLOG_IF(WARNING, flags & Canvas::MULTI_LINE) << "Multi-line not implemented."; |
66 DLOG_IF(WARNING, !shadows.empty()) << "Text shadows not implemented."; | 76 DLOG_IF(WARNING, !shadows.empty()) << "Text shadows not implemented."; |
67 | 77 |
| 78 const Font& font = font_list.GetPrimaryFont(); |
68 skia::RefPtr<SkTypeface> typeface = skia::AdoptRef( | 79 skia::RefPtr<SkTypeface> typeface = skia::AdoptRef( |
69 SkTypeface::CreateFromName( | 80 SkTypeface::CreateFromName( |
70 font.GetFontName().c_str(), FontTypefaceStyle(font))); | 81 font.GetFontName().c_str(), FontTypefaceStyle(font))); |
71 SkPaint paint; | 82 SkPaint paint; |
72 paint.setTypeface(typeface.get()); | 83 paint.setTypeface(typeface.get()); |
73 paint.setColor(color); | 84 paint.setColor(color); |
74 canvas_->drawText(text.c_str(), | 85 canvas_->drawText(text.c_str(), |
75 text.size() * sizeof(base::string16::value_type), | 86 text.size() * sizeof(base::string16::value_type), |
76 text_bounds.x(), | 87 text_bounds.x(), |
77 text_bounds.bottom(), | 88 text_bounds.bottom(), |
78 paint); | 89 paint); |
79 } | 90 } |
80 | 91 |
| 92 void Canvas::DrawStringWithShadows(const base::string16& text, |
| 93 const gfx::Font& font, |
| 94 SkColor color, |
| 95 const gfx::Rect& text_bounds, |
| 96 int line_height, |
| 97 int flags, |
| 98 const ShadowValues& shadows) { |
| 99 DrawStringWithShadows(text, FontList(font), color, text_bounds, line_height, |
| 100 flags, shadows); |
| 101 } |
| 102 |
| 103 void Canvas::DrawStringWithHalo(const base::string16& text, |
| 104 const gfx::FontList& font_list, |
| 105 SkColor text_color, |
| 106 SkColor halo_color, |
| 107 const Rect& display_rect, |
| 108 int flags) { |
| 109 } |
| 110 |
81 void Canvas::DrawStringWithHalo(const base::string16& text, | 111 void Canvas::DrawStringWithHalo(const base::string16& text, |
82 const gfx::Font& font, | 112 const gfx::Font& font, |
83 SkColor text_color, | 113 SkColor text_color, |
84 SkColor halo_color, | 114 SkColor halo_color, |
85 int x, int y, int w, int h, | 115 int x, int y, int w, int h, |
86 int flags) { | 116 int flags) { |
87 } | 117 } |
88 | 118 |
89 } // namespace gfx | 119 } // namespace gfx |
OLD | NEW |