Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: ui/views/examples/multiline_example.cc

Issue 152473008: More or less implement RenderTextHarfBuzz (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nits Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/gfx/switches.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 #include "ui/views/examples/multiline_example.h" 5 #include "ui/views/examples/multiline_example.h"
6 6
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/events/event.h" 8 #include "ui/events/event.h"
9 #include "ui/gfx/render_text.h" 9 #include "ui/gfx/render_text.h"
10 #include "ui/views/background.h" 10 #include "ui/views/background.h"
11 #include "ui/views/border.h" 11 #include "ui/views/border.h"
12 #include "ui/views/controls/button/checkbox.h" 12 #include "ui/views/controls/button/checkbox.h"
13 #include "ui/views/controls/label.h" 13 #include "ui/views/controls/label.h"
14 #include "ui/views/controls/textfield/textfield.h" 14 #include "ui/views/controls/textfield/textfield.h"
15 #include "ui/views/layout/grid_layout.h" 15 #include "ui/views/layout/grid_layout.h"
16 #include "ui/views/view.h" 16 #include "ui/views/view.h"
17 17
18 using base::ASCIIToUTF16; 18 using base::ASCIIToUTF16;
19 19
20 namespace views { 20 namespace views {
21 namespace examples { 21 namespace examples {
22 22
23 namespace {
24
25 gfx::Range ClampRange(gfx::Range range, size_t max) {
sky 2014/05/21 16:16:38 const gfx::Range&
26 range.set_start(std::min(range.start(), max));
27 range.set_end(std::min(range.end(), max));
28 return range;
29 }
30
31 } // namespace
32
23 // A simple View that hosts a RenderText object. 33 // A simple View that hosts a RenderText object.
24 class MultilineExample::RenderTextView : public View { 34 class MultilineExample::RenderTextView : public View {
25 public: 35 public:
26 RenderTextView() : render_text_(gfx::RenderText::CreateInstance()) { 36 RenderTextView() : render_text_(gfx::RenderText::CreateInstance()) {
27 render_text_->SetHorizontalAlignment(gfx::ALIGN_CENTER); 37 render_text_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
28 render_text_->SetColor(SK_ColorBLACK); 38 render_text_->SetColor(SK_ColorBLACK);
29 render_text_->SetMultiline(true); 39 render_text_->SetMultiline(true);
30 SetBorder(Border::CreateSolidBorder(2, SK_ColorGRAY)); 40 SetBorder(Border::CreateSolidBorder(2, SK_ColorGRAY));
31 } 41 }
32 42
(...skipping 21 matching lines...) Expand all
54 gfx::Rect rect = old_rect; 64 gfx::Rect rect = old_rect;
55 rect.set_width(w - GetInsets().width()); 65 rect.set_width(w - GetInsets().width());
56 render_text_->SetDisplayRect(rect); 66 render_text_->SetDisplayRect(rect);
57 int height = render_text_->GetStringSize().height() + GetInsets().height(); 67 int height = render_text_->GetStringSize().height() + GetInsets().height();
58 render_text_->SetDisplayRect(old_rect); 68 render_text_->SetDisplayRect(old_rect);
59 return height; 69 return height;
60 } 70 }
61 71
62 void SetText(const base::string16& new_contents) { 72 void SetText(const base::string16& new_contents) {
63 // Color and style the text inside |test_range| to test colors and styles. 73 // Color and style the text inside |test_range| to test colors and styles.
64 gfx::Range test_range(1, 21); 74 const size_t range_max = new_contents.length();
65 test_range.set_start(std::min(test_range.start(), new_contents.length())); 75 gfx::Range color_range = ClampRange(gfx::Range(1, 21), range_max);
66 test_range.set_end(std::min(test_range.end(), new_contents.length())); 76 gfx::Range bold_range = ClampRange(gfx::Range(4, 10), range_max);
77 gfx::Range italic_range = ClampRange(gfx::Range(7, 13), range_max);
67 78
68 render_text_->SetText(new_contents); 79 render_text_->SetText(new_contents);
69 render_text_->SetColor(SK_ColorBLACK); 80 render_text_->SetColor(SK_ColorBLACK);
70 render_text_->ApplyColor(0xFFFF0000, test_range); 81 render_text_->ApplyColor(0xFFFF0000, color_range);
71 render_text_->SetStyle(gfx::DIAGONAL_STRIKE, false); 82 render_text_->SetStyle(gfx::DIAGONAL_STRIKE, false);
72 render_text_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, test_range); 83 render_text_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, color_range);
73 render_text_->SetStyle(gfx::UNDERLINE, false); 84 render_text_->SetStyle(gfx::UNDERLINE, false);
74 render_text_->ApplyStyle(gfx::UNDERLINE, true, test_range); 85 render_text_->ApplyStyle(gfx::UNDERLINE, true, color_range);
86 render_text_->ApplyStyle(gfx::BOLD, true, bold_range);
87 render_text_->ApplyStyle(gfx::ITALIC, true, italic_range);
75 InvalidateLayout(); 88 InvalidateLayout();
76 } 89 }
77 90
78 private: 91 private:
79 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE { 92 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE {
80 gfx::Rect bounds = GetLocalBounds(); 93 gfx::Rect bounds = GetLocalBounds();
81 bounds.Inset(GetInsets()); 94 bounds.Inset(GetInsets());
82 render_text_->SetDisplayRect(bounds); 95 render_text_->SetDisplayRect(bounds);
83 } 96 }
84 97
85 scoped_ptr<gfx::RenderText> render_text_; 98 scoped_ptr<gfx::RenderText> render_text_;
86 99
87 DISALLOW_COPY_AND_ASSIGN(RenderTextView); 100 DISALLOW_COPY_AND_ASSIGN(RenderTextView);
88 }; 101 };
89 102
90 MultilineExample::MultilineExample() 103 MultilineExample::MultilineExample()
91 : ExampleBase("Multiline RenderText"), 104 : ExampleBase("Multiline RenderText"),
92 render_text_view_(NULL), 105 render_text_view_(NULL),
93 label_(NULL), 106 label_(NULL),
94 textfield_(NULL), 107 textfield_(NULL),
95 label_checkbox_(NULL) { 108 label_checkbox_(NULL) {
96 } 109 }
97 110
98 MultilineExample::~MultilineExample() { 111 MultilineExample::~MultilineExample() {
99 } 112 }
100 113
101 void MultilineExample::CreateExampleView(View* container) { 114 void MultilineExample::CreateExampleView(View* container) {
102 const char kTestString[] = "test string asdf 1234 test string asdf 1234 " 115 const base::string16 kTestString = base::WideToUTF16(L"qwerty"
103 "test string asdf 1234 test string asdf 1234"; 116 L"\x627\x644\x631\x626\x64A\x633\x64A\x629"
117 L"asdfgh");
104 118
105 render_text_view_ = new RenderTextView(); 119 render_text_view_ = new RenderTextView();
106 render_text_view_->SetText(ASCIIToUTF16(kTestString)); 120 render_text_view_->SetText(kTestString);
107 121
108 label_ = new Label(); 122 label_ = new Label();
109 label_->SetText(ASCIIToUTF16(kTestString)); 123 label_->SetText(kTestString);
110 label_->SetMultiLine(true); 124 label_->SetMultiLine(true);
111 label_->SetBorder(Border::CreateSolidBorder(2, SK_ColorCYAN)); 125 label_->SetBorder(Border::CreateSolidBorder(2, SK_ColorCYAN));
112 126
113 label_checkbox_ = new Checkbox(ASCIIToUTF16("views::Label:")); 127 label_checkbox_ = new Checkbox(ASCIIToUTF16("views::Label:"));
114 label_checkbox_->SetChecked(true); 128 label_checkbox_->SetChecked(true);
115 label_checkbox_->set_listener(this); 129 label_checkbox_->set_listener(this);
116 label_checkbox_->set_request_focus_on_press(false); 130 label_checkbox_->set_request_focus_on_press(false);
117 131
118 textfield_ = new Textfield(); 132 textfield_ = new Textfield();
119 textfield_->set_controller(this); 133 textfield_->set_controller(this);
120 textfield_->SetText(ASCIIToUTF16(kTestString)); 134 textfield_->SetText(kTestString);
121 135
122 GridLayout* layout = new GridLayout(container); 136 GridLayout* layout = new GridLayout(container);
123 container->SetLayoutManager(layout); 137 container->SetLayoutManager(layout);
124 138
125 ColumnSet* column_set = layout->AddColumnSet(0); 139 ColumnSet* column_set = layout->AddColumnSet(0);
126 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 140 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER,
127 0.0f, GridLayout::USE_PREF, 0, 0); 141 0.0f, GridLayout::USE_PREF, 0, 0);
128 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 142 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
129 1.0f, GridLayout::FIXED, 0, 0); 143 1.0f, GridLayout::FIXED, 0, 0);
130 144
(...skipping 27 matching lines...) Expand all
158 void MultilineExample::ButtonPressed(Button* sender, const ui::Event& event) { 172 void MultilineExample::ButtonPressed(Button* sender, const ui::Event& event) {
159 DCHECK_EQ(sender, label_checkbox_); 173 DCHECK_EQ(sender, label_checkbox_);
160 label_->SetText(label_checkbox_->checked() ? textfield_->text() : 174 label_->SetText(label_checkbox_->checked() ? textfield_->text() :
161 base::string16()); 175 base::string16());
162 container()->Layout(); 176 container()->Layout();
163 container()->SchedulePaint(); 177 container()->SchedulePaint();
164 } 178 }
165 179
166 } // namespace examples 180 } // namespace examples
167 } // namespace views 181 } // namespace views
OLDNEW
« no previous file with comments | « ui/gfx/switches.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698