Index: examples/keyboard_client/keyboard_client.cc |
diff --git a/examples/keyboard_client/keyboard_client.cc b/examples/keyboard_client/keyboard_client.cc |
index a9ef5e1412fbc6063abef7e286ac09cf4d7dff57..933966bacfc1a311c10676b1615f6ba910f73744 100644 |
--- a/examples/keyboard_client/keyboard_client.cc |
+++ b/examples/keyboard_client/keyboard_client.cc |
@@ -219,16 +219,7 @@ class KeyboardDelegate : public mojo::ApplicationDelegate, |
void CommitText(const mojo::String& text, |
int32_t new_cursor_position) override { |
- std::string combined(text_[1]); |
- combined.append(text); |
- SkRect bounds; |
- text_paint_.measureText(combined.data(), combined.size(), &bounds); |
- if (bounds.width() > text_view_->bounds().width) { |
- text_[0] = text_[1]; |
- text_[1] = text; |
- } else { |
- text_[1].append(text); |
- } |
+ text_.append(text); |
DrawText(); |
} |
@@ -236,10 +227,10 @@ class KeyboardDelegate : public mojo::ApplicationDelegate, |
int32_t after_length) override { |
// treat negative and zero |before_length| values as no-op. |
if (before_length > 0) { |
- if (before_length > static_cast<int32_t>(text_[1].size())) { |
- before_length = text_[1].size(); |
+ if (before_length > static_cast<int32_t>(text_.size())) { |
+ before_length = text_.size(); |
} |
- text_[1].erase(text_[1].end() - before_length, text_[1].end()); |
+ text_.erase(text_.end() - before_length, text_.end()); |
} |
DrawText(); |
} |
@@ -316,15 +307,46 @@ class KeyboardDelegate : public mojo::ApplicationDelegate, |
float row_height = text_view_height_ / 2.0f; |
float text_baseline_offset = row_height / 5.0f; |
- |
- if (!text_[0].empty()) { |
- canvas->drawText(text_[0].data(), text_[0].size(), 0.0f, |
- row_height - text_baseline_offset, text_paint_); |
- } |
- |
- if (!text_[1].empty()) { |
- canvas->drawText(text_[1].data(), text_[1].size(), 0.0f, |
- (2.0f * row_height) - text_baseline_offset, text_paint_); |
+ if (!text_.empty()) { |
+ SkRect sk_rect; |
+ text_paint_.measureText((const void*)(text_.c_str()), |
+ strlen(text_.c_str()), &sk_rect); |
+ |
+ if (sk_rect.width() > text_view_->bounds().width) { |
+ char reverse_text[text_.length() + 1]; |
+ for (unsigned int i = 0; i < text_.length(); i++) { |
+ reverse_text[i] = text_[text_.length() - i - 1]; |
APW
2015/07/31 22:36:43
use either a reverse iterator or create a copy of
riajiang
2015/08/01 01:23:08
Done.
|
+ } |
+ reverse_text[text_.length()] = '\0'; |
+ |
+ size_t processed1 = text_paint_.breakText((const void*)(reverse_text), |
+ strlen(reverse_text), |
+ text_view_->bounds().width); |
+ size_t processed2 = text_paint_.breakText( |
+ (const void*)(reverse_text + processed1), |
+ strlen(reverse_text) - processed1, text_view_->bounds().width); |
+ if (processed1 + processed2 < text_.length()) { |
+ canvas->drawText( |
+ text_.data() + (text_.length() - processed1), processed1, 0.0f, |
+ (2.0f * row_height) - text_baseline_offset, text_paint_); |
+ canvas->drawText( |
+ text_.data() + (text_.length() - processed1 - processed2), |
+ processed2, 0.0f, row_height - text_baseline_offset, text_paint_); |
+ } else { |
+ size_t processed3 = text_paint_.breakText( |
+ (const void*)(text_.c_str()), strlen(text_.c_str()), |
+ text_view_->bounds().width); |
+ canvas->drawText(text_.data(), processed3, 0.0f, |
+ row_height - text_baseline_offset, text_paint_); |
+ canvas->drawText( |
+ text_.data() + processed3, strlen(text_.c_str()) - processed3, |
+ 0.0f, (2.0f * row_height) - text_baseline_offset, text_paint_); |
+ } |
+ } else { |
+ canvas->drawText(text_.data(), text_.size(), 0.0f, |
APW
2015/07/31 22:36:43
just to make things a bit easier to understand, ca
riajiang
2015/08/01 01:23:08
Done.
|
+ (2.0f * row_height) - text_baseline_offset, |
+ text_paint_); |
+ } |
} |
canvas->flush(); |
@@ -352,7 +374,7 @@ class KeyboardDelegate : public mojo::ApplicationDelegate, |
scoped_ptr<ViewTextureUploader> text_view_texture_uploader_; |
scoped_ptr<ViewTextureUploader> root_view_texture_uploader_; |
int text_view_height_; |
- std::string text_[2]; |
+ std::string text_; |
SkPaint text_paint_; |
base::WeakPtrFactory<KeyboardDelegate> weak_factory_; |