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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: services/keyboard_native/text_update_key.cc
diff --git a/services/keyboard_native/text_update_key.cc b/services/keyboard_native/text_update_key.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d39e2fdbc6833de377f4e83b19d76051fcd01fe5
--- /dev/null
+++ b/services/keyboard_native/text_update_key.cc
@@ -0,0 +1,77 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <string>
+
+#include "services/keyboard_native/text_update_key.h"
+#include "third_party/skia/include/core/SkCanvas.h"
+#include "third_party/skia/include/core/SkPaint.h"
+#include "third_party/skia/include/core/SkScalar.h"
+#include "ui/gfx/geometry/rect_f.h"
+
+namespace keyboard {
+
+TextUpdateKey::TextUpdateKey(
+ std::string text,
+ base::Callback<void(const TextUpdateKey&)> touch_up_callback)
+ : text_(text), touch_up_callback_(touch_up_callback) {
+}
+
+TextUpdateKey::~TextUpdateKey() {
+}
+
+// Key implementation.
+void TextUpdateKey::Draw(SkCanvas* canvas,
+ const SkPaint& paint,
+ const gfx::RectF& rect) {
+ std::string text_to_fit = text_;
+ SkRect bounds;
+ SkPaint paint_copy = paint;
+ paint_copy.measureText((const void*)(text_.c_str()), strlen(text_.c_str()),
+ &bounds);
+ bool text_need_scale = false;
+ if (bounds.width() > rect.width() * 0.8) {
+ text_need_scale = true;
+
+ paint_copy.setTextScaleX(SkFloatToScalar(0.6));
+ paint_copy.measureText((const void*)(text_.c_str()), strlen(text_.c_str()),
+ &bounds);
+ paint_copy.setTextScaleX(SkIntToScalar(1));
+ if (bounds.width() > rect.width() * 0.8) {
+ int dot_count = SkScalarTruncToInt((SkScalarToFloat(bounds.width()) -
+ SkScalarToFloat(rect.width()) * 0.8) /
+ SkScalarToFloat(bounds.width()) *
+ strlen(text_.c_str())) +
+ 1;
+ int dot_count_in_text = dot_count < 3 ? dot_count : 3;
+ std::string dots(dot_count_in_text, '.');
+ text_to_fit =
+ dots +
+ text_to_fit.substr(dot_count, text_to_fit.length() - dot_count);
+ }
+ }
+
+ float text_baseline_offset = rect.height() / 5.0f;
+ if (text_need_scale) {
+ paint_copy.setTextScaleX(SkFloatToScalar(0.6));
+ }
+ canvas->drawText(text_to_fit.c_str(), strlen(text_to_fit.c_str()),
+ rect.x() + (rect.width() / 2.0f),
+ rect.y() + rect.height() - text_baseline_offset, paint_copy);
+ paint_copy.setTextScaleX(SkIntToScalar(1));
+}
+
+const char* TextUpdateKey::ToText() const {
+ const char* text_char = text_.c_str();
+ return text_char;
+}
+
+void TextUpdateKey::OnTouchUp() {
+ touch_up_callback_.Run(*this);
+}
+
+void TextUpdateKey::ChangeText(std::string new_text) {
+ text_ = new_text;
+}
+} // namespace keyboard
« 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