diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-14 14:55:02 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-14 14:55:02 +0000 |
commit | ed24f7a5588e96f5f675d62161d1c290c53c6558 (patch) | |
tree | 0538d67bfb37bb64f1d3c72bd9bb94c693279db2 /ui | |
parent | fb9a5e0628d306ac4d8d4ccdc82bd43c34dd6b04 (diff) | |
download | chromium_src-ed24f7a5588e96f5f675d62161d1c290c53c6558.zip chromium_src-ed24f7a5588e96f5f675d62161d1c290c53c6558.tar.gz chromium_src-ed24f7a5588e96f5f675d62161d1c290c53c6558.tar.bz2 |
ui/gfx: Extract RemoveAcceleratorChar() out of skia_util.*
This function has nothing to do with Skia, better to extract it into its own
module.
TEST=ui_unittests
R=asvitkine@chromium.org
TBR=sky@chromium.org # for gyp changes
Review URL: https://codereview.chromium.org/11364217
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@167675 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/gfx/canvas_skia.cc | 2 | ||||
-rw-r--r-- | ui/gfx/pango_util.cc | 2 | ||||
-rw-r--r-- | ui/gfx/skia_util.cc | 39 | ||||
-rw-r--r-- | ui/gfx/skia_util.h | 11 | ||||
-rw-r--r-- | ui/gfx/text_utils.cc | 49 | ||||
-rw-r--r-- | ui/gfx/text_utils.h | 25 | ||||
-rw-r--r-- | ui/gfx/text_utils_unittest.cc (renamed from ui/gfx/skia_util_unittest.cc) | 20 | ||||
-rw-r--r-- | ui/ui.gyp | 2 | ||||
-rw-r--r-- | ui/ui_unittests.gypi | 2 |
9 files changed, 92 insertions, 60 deletions
diff --git a/ui/gfx/canvas_skia.cc b/ui/gfx/canvas_skia.cc index ece6e98..263819b 100644 --- a/ui/gfx/canvas_skia.cc +++ b/ui/gfx/canvas_skia.cc @@ -15,7 +15,7 @@ #include "ui/gfx/rect.h" #include "ui/gfx/render_text.h" #include "ui/gfx/shadow_value.h" -#include "ui/gfx/skia_util.h" +#include "ui/gfx/text_utils.h" namespace { diff --git a/ui/gfx/pango_util.cc b/ui/gfx/pango_util.cc index cd874c3..424eb45 100644 --- a/ui/gfx/pango_util.cc +++ b/ui/gfx/pango_util.cc @@ -21,7 +21,7 @@ #include "ui/gfx/font_render_params_linux.h" #include "ui/gfx/platform_font_pango.h" #include "ui/gfx/rect.h" -#include "ui/gfx/skia_util.h" +#include "ui/gfx/text_utils.h" #if defined(TOOLKIT_GTK) #include <gdk/gdk.h> diff --git a/ui/gfx/skia_util.cc b/ui/gfx/skia_util.cc index 5046b6a..caa35e7 100644 --- a/ui/gfx/skia_util.cc +++ b/ui/gfx/skia_util.cc @@ -4,7 +4,6 @@ #include "ui/gfx/skia_util.h" -#include "base/i18n/char_iterator.h" #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkColorFilter.h" #include "third_party/skia/include/core/SkColorPriv.h" @@ -142,44 +141,6 @@ bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) { return (size1 == size2) && (0 == memcmp(addr1, addr2, bitmap1.getSize())); } -string16 RemoveAcceleratorChar(const string16& s, - char16 accelerator_char, - int* accelerated_char_pos, - int* accelerated_char_span) { - bool escaped = false; - int last_char_pos = -1; - int last_char_span = 0; - base::i18n::UTF16CharIterator chars(&s); - string16 accelerator_removed; - - accelerator_removed.reserve(s.size()); - while (!chars.end()) { - int32 c = chars.get(); - int array_pos = chars.array_pos(); - chars.Advance(); - - if (c != accelerator_char || escaped) { - int span = chars.array_pos() - array_pos; - if (escaped && c != accelerator_char) { - last_char_pos = accelerator_removed.size(); - last_char_span = span; - } - for (int i = 0; i < span; i++) - accelerator_removed.push_back(s[array_pos + i]); - escaped = false; - } else { - escaped = true; - } - } - - if (accelerated_char_pos) - *accelerated_char_pos = last_char_pos; - if (accelerated_char_span) - *accelerated_char_span = last_char_span; - - return accelerator_removed; -} - void ConvertSkiaToRGBA(const unsigned char* skia, int pixel_width, unsigned char* rgba) { diff --git a/ui/gfx/skia_util.h b/ui/gfx/skia_util.h index 08eb2203..6682407 100644 --- a/ui/gfx/skia_util.h +++ b/ui/gfx/skia_util.h @@ -8,7 +8,6 @@ #include <string> #include <vector> -#include "base/string16.h" #include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkRect.h" #include "third_party/skia/include/core/SkShader.h" @@ -70,16 +69,6 @@ UI_EXPORT SkDrawLooper* CreateShadowDrawLooper( UI_EXPORT bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2); -// Strip the accelerator char (typically '&') from a menu string. A double -// accelerator char ('&&') will be converted to a single char. The out params -// |accelerated_char_pos| and |accelerated_char_span| will be set to the index -// and span of the last accelerated character, respectively, or -1 and 0 if -// there was none. -UI_EXPORT string16 RemoveAcceleratorChar(const string16& s, - char16 accelerator_char, - int* accelerated_char_pos, - int* accelerated_char_span); - // Converts Skia ARGB format pixels in |skia| to RGBA. UI_EXPORT void ConvertSkiaToRGBA(const unsigned char* skia, int pixel_width, diff --git a/ui/gfx/text_utils.cc b/ui/gfx/text_utils.cc new file mode 100644 index 0000000..a5a1b79 --- /dev/null +++ b/ui/gfx/text_utils.cc @@ -0,0 +1,49 @@ +// Copyright 2012 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 "ui/gfx/text_utils.h" + +#include "base/i18n/char_iterator.h" + +namespace gfx { + +string16 RemoveAcceleratorChar(const string16& s, + char16 accelerator_char, + int* accelerated_char_pos, + int* accelerated_char_span) { + bool escaped = false; + int last_char_pos = -1; + int last_char_span = 0; + base::i18n::UTF16CharIterator chars(&s); + string16 accelerator_removed; + + accelerator_removed.reserve(s.size()); + while (!chars.end()) { + int32 c = chars.get(); + int array_pos = chars.array_pos(); + chars.Advance(); + + if (c != accelerator_char || escaped) { + int span = chars.array_pos() - array_pos; + if (escaped && c != accelerator_char) { + last_char_pos = accelerator_removed.size(); + last_char_span = span; + } + for (int i = 0; i < span; i++) + accelerator_removed.push_back(s[array_pos + i]); + escaped = false; + } else { + escaped = true; + } + } + + if (accelerated_char_pos) + *accelerated_char_pos = last_char_pos; + if (accelerated_char_span) + *accelerated_char_span = last_char_span; + + return accelerator_removed; +} + +} // namespace gfx diff --git a/ui/gfx/text_utils.h b/ui/gfx/text_utils.h new file mode 100644 index 0000000..274664f --- /dev/null +++ b/ui/gfx/text_utils.h @@ -0,0 +1,25 @@ +// Copyright 2012 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. + +#ifndef UI_GFX_TEXT_UTILS_H_ +#define UI_GFX_TEXT_UTILS_H_ + +#include "base/string16.h" +#include "ui/base/ui_export.h" + +namespace gfx { + +// Strip the accelerator char (typically '&') from a menu string. A double +// accelerator char ('&&') will be converted to a single char. The out params +// |accelerated_char_pos| and |accelerated_char_span| will be set to the index +// and span of the last accelerated character, respectively, or -1 and 0 if +// there was none. +UI_EXPORT string16 RemoveAcceleratorChar(const string16& s, + char16 accelerator_char, + int* accelerated_char_pos, + int* accelerated_char_span); + +} // namespace gfx + +#endif // UI_GFX_TEXT_UTILS_H_ diff --git a/ui/gfx/skia_util_unittest.cc b/ui/gfx/text_utils_unittest.cc index c468ce1..0b4aaad 100644 --- a/ui/gfx/skia_util_unittest.cc +++ b/ui/gfx/text_utils_unittest.cc @@ -2,14 +2,17 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "ui/gfx/skia_util.h" +#include "ui/gfx/text_utils.h" #include "base/utf_string_conversions.h" #include "testing/gtest/include/gtest/gtest.h" -static const char16 kAcceleratorChar = '&'; +namespace gfx { +namespace { -TEST(SkiaUtilTest, RemoveAcceleratorChar) { +const char16 kAcceleratorChar = '&'; + +TEST(TextUtilsTest, RemoveAcceleratorChar) { struct TestData { const char* input; int accelerated_char_pos; @@ -45,12 +48,15 @@ TEST(SkiaUtilTest, RemoveAcceleratorChar) { for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { int accelerated_char_pos; int accelerated_char_span; - string16 result = gfx::RemoveAcceleratorChar(UTF8ToUTF16(cases[i].input), - kAcceleratorChar, - &accelerated_char_pos, - &accelerated_char_span); + string16 result = RemoveAcceleratorChar(UTF8ToUTF16(cases[i].input), + kAcceleratorChar, + &accelerated_char_pos, + &accelerated_char_span); EXPECT_EQ(result, UTF8ToUTF16(cases[i].output)); EXPECT_EQ(accelerated_char_pos, cases[i].accelerated_char_pos); EXPECT_EQ(accelerated_char_span, cases[i].accelerated_char_span); } } + +} // namespace +} // namespace gfx @@ -516,6 +516,8 @@ 'gfx/sys_color_change_listener.cc', 'gfx/sys_color_change_listener.h', 'gfx/text_constants.h', + 'gfx/text_utils.cc', + 'gfx/text_utils.h', 'gfx/transform.cc', 'gfx/transform.h', 'gfx/transform_util.cc', diff --git a/ui/ui_unittests.gypi b/ui/ui_unittests.gypi index 67816dd..abcf32d 100644 --- a/ui/ui_unittests.gypi +++ b/ui/ui_unittests.gypi @@ -93,7 +93,7 @@ 'gfx/shadow_value_unittest.cc', 'gfx/size_unittest.cc', 'gfx/skbitmap_operations_unittest.cc', - 'gfx/skia_util_unittest.cc', + 'gfx/text_utils_unittest.cc', 'gfx/vector2d_unittest.cc', 'gfx/vector3d_unittest.cc', 'test/run_all_unittests.cc', |