OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "services/keyboard_native/text_update_key.h" |
| 8 #include "third_party/skia/include/core/SkCanvas.h" |
| 9 #include "third_party/skia/include/core/SkPaint.h" |
| 10 #include "third_party/skia/include/core/SkScalar.h" |
| 11 #include "ui/gfx/geometry/rect_f.h" |
| 12 |
| 13 namespace keyboard { |
| 14 |
| 15 TextUpdateKey::TextUpdateKey( |
| 16 std::string text, |
| 17 base::Callback<void(const TextUpdateKey&)> touch_up_callback) |
| 18 : text_(text), touch_up_callback_(touch_up_callback) { |
| 19 } |
| 20 |
| 21 TextUpdateKey::~TextUpdateKey() { |
| 22 } |
| 23 |
| 24 // Key implementation. |
| 25 void TextUpdateKey::Draw(SkCanvas* canvas, |
| 26 const SkPaint& paint, |
| 27 const gfx::RectF& rect) { |
| 28 std::string text_to_fit = text_; |
| 29 SkRect bounds; |
| 30 SkPaint paint_copy = paint; |
| 31 paint_copy.measureText((const void*)(text_.c_str()), strlen(text_.c_str()), |
| 32 &bounds); |
| 33 bool text_need_scale = false; |
| 34 if (bounds.width() > rect.width() * 0.8) { |
| 35 text_need_scale = true; |
| 36 |
| 37 paint_copy.setTextScaleX(SkFloatToScalar(0.6)); |
| 38 paint_copy.measureText((const void*)(text_.c_str()), strlen(text_.c_str()), |
| 39 &bounds); |
| 40 paint_copy.setTextScaleX(SkIntToScalar(1)); |
| 41 if (bounds.width() > rect.width() * 0.8) { |
| 42 int dot_count = SkScalarTruncToInt((SkScalarToFloat(bounds.width()) - |
| 43 SkScalarToFloat(rect.width()) * 0.8) / |
| 44 SkScalarToFloat(bounds.width()) * |
| 45 strlen(text_.c_str())) + |
| 46 1; |
| 47 int dot_count_in_text = dot_count < 3 ? dot_count : 3; |
| 48 std::string dots(dot_count_in_text, '.'); |
| 49 text_to_fit = |
| 50 dots + |
| 51 text_to_fit.substr(dot_count, text_to_fit.length() - dot_count); |
| 52 } |
| 53 } |
| 54 |
| 55 float text_baseline_offset = rect.height() / 5.0f; |
| 56 if (text_need_scale) { |
| 57 paint_copy.setTextScaleX(SkFloatToScalar(0.6)); |
| 58 } |
| 59 canvas->drawText(text_to_fit.c_str(), strlen(text_to_fit.c_str()), |
| 60 rect.x() + (rect.width() / 2.0f), |
| 61 rect.y() + rect.height() - text_baseline_offset, paint_copy); |
| 62 paint_copy.setTextScaleX(SkIntToScalar(1)); |
| 63 } |
| 64 |
| 65 const char* TextUpdateKey::ToText() const { |
| 66 const char* text_char = text_.c_str(); |
| 67 return text_char; |
| 68 } |
| 69 |
| 70 void TextUpdateKey::OnTouchUp() { |
| 71 touch_up_callback_.Run(*this); |
| 72 } |
| 73 |
| 74 void TextUpdateKey::ChangeText(std::string new_text) { |
| 75 text_ = new_text; |
| 76 } |
| 77 } // namespace keyboard |
OLD | NEW |