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

Side by Side Diff: services/keyboard_native/text_update_key.cc

Issue 1247903003: Add spellcheck and word suggestion to the prediction service (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: format README and CHROMIUM.diff Created 5 years, 4 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
OLDNEW
(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
OLDNEW
« no previous file with comments | « services/keyboard_native/text_update_key.h ('k') | services/keyboard_native/view_observer_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698