diff options
author | hbono@chromium.org <hbono@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-04 01:53:01 +0000 |
---|---|---|
committer | hbono@chromium.org <hbono@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-04 01:53:01 +0000 |
commit | e7827c507b5f1df35daab46e37a7f582ce849894 (patch) | |
tree | baa84ad36727681d9d60469fa679f076dce61d1c | |
parent | f9c0fab46100708848f664fa16102995ee454595 (diff) | |
download | chromium_src-e7827c507b5f1df35daab46e37a7f582ce849894.zip chromium_src-e7827c507b5f1df35daab46e37a7f582ce849894.tar.gz chromium_src-e7827c507b5f1df35daab46e37a7f582ce849894.tar.bz2 |
Implement hyphenation for DumpRenderTree.
This change implements the platform functions added by my WebKit change r124434 <http://trac.webkit.org/changeset/124434> for DumpRenderTree. It is a simplified version of my r146964 <http://crrev.com/146964>.
BUG=47083
TEST=hyphen*.html
Review URL: https://chromiumcodereview.appspot.com/11014014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@160044 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | webkit/DEPS | 1 | ||||
-rw-r--r-- | webkit/support/test_webkit_platform_support.cc | 82 | ||||
-rw-r--r-- | webkit/support/test_webkit_platform_support.h | 9 | ||||
-rw-r--r-- | webkit/support/webkit_support.gypi | 1 |
4 files changed, 92 insertions, 1 deletions
diff --git a/webkit/DEPS b/webkit/DEPS index 0f66417..af9b6fc 100644 --- a/webkit/DEPS +++ b/webkit/DEPS @@ -12,6 +12,7 @@ include_rules = [ "+grit", # For generated headers "+skia", "+third_party/angle", + "+third_party/hyphen", "+third_party/leveldatabase", "+third_party/skia", "+third_party/sqlite", diff --git a/webkit/support/test_webkit_platform_support.cc b/webkit/support/test_webkit_platform_support.cc index f970523..800cc25 100644 --- a/webkit/support/test_webkit_platform_support.cc +++ b/webkit/support/test_webkit_platform_support.cc @@ -14,6 +14,7 @@ #include "net/cookies/cookie_monster.h" #include "net/http/http_cache.h" #include "net/test/test_server.h" +#include "third_party/hyphen/hyphen.h" #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebAudioDevice.h" #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebData.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h" @@ -68,7 +69,8 @@ using WebKit::WebScriptController; TestWebKitPlatformSupport::TestWebKitPlatformSupport(bool unit_test_mode, WebKit::Platform* shadow_platform_delegate) : unit_test_mode_(unit_test_mode), - shadow_platform_delegate_(shadow_platform_delegate) { + shadow_platform_delegate_(shadow_platform_delegate), + hyphen_dictionary_(NULL) { v8::V8::SetCounterFunction(base::StatsTable::FindLocation); WebKit::initialize(this); @@ -150,6 +152,8 @@ TestWebKitPlatformSupport::TestWebKitPlatformSupport(bool unit_test_mode, } TestWebKitPlatformSupport::~TestWebKitPlatformSupport() { + if (hyphen_dictionary_) + hnj_hyphen_free(hyphen_dictionary_); } WebKit::WebMimeRegistry* TestWebKitPlatformSupport::mimeRegistry() { @@ -496,3 +500,79 @@ TestWebKitPlatformSupport::createRTCPeerConnectionHandler( return webkit_glue::WebKitPlatformSupportImpl::createRTCPeerConnectionHandler( client); } + +bool TestWebKitPlatformSupport::canHyphenate(const WebKit::WebString& locale) { + return locale.isEmpty() || locale.equals("en_US"); +} + +size_t TestWebKitPlatformSupport::computeLastHyphenLocation( + const char16* characters, + size_t length, + size_t before_index, + const WebKit::WebString& locale) { + DCHECK(locale.isEmpty() || locale.equals("en_US")); + if (!hyphen_dictionary_) { + // Initialize the hyphen library with a sample dictionary. To avoid test + // flakiness, this code synchronously loads the dictionary. + FilePath path; + if (!PathService::Get(base::DIR_SOURCE_ROOT, &path)) + return 0; + path = path.AppendASCII("third_party"); + path = path.AppendASCII("hyphen"); + path = path.AppendASCII("hyph_en_US.dic"); + std::string dictionary; + if (!file_util::ReadFileToString(path, &dictionary)) + return 0; + hyphen_dictionary_ = hnj_hyphen_load( + reinterpret_cast<const unsigned char*>(dictionary.data()), + dictionary.length()); + if (!hyphen_dictionary_) + return 0; + } + // Retrieve the positions where we can insert hyphens. This function assumes + // the input word is an English word so it can use the position returned by + // the hyphen library without conversion. + while (length && !IsAsciiAlpha(characters[length - 1])) + --length; + if (!length) + return 0; + std::string word = UTF16ToASCII(string16(characters, length)); + scoped_array<char> hyphens(new char[word.length() + 5]); + char** rep = NULL; + int* pos = NULL; + int* cut = NULL; + int error = hnj_hyphen_hyphenate2(hyphen_dictionary_, + word.data(), + static_cast<int>(word.length()), + hyphens.get(), + NULL, + &rep, + &pos, + &cut); + if (error) + return 0; + + // Release all resources allocated by the hyphen library now because they are + // not used when hyphenating English words. + if (rep) { + for (size_t i = 0; i < word.length(); ++i) { + if (rep[i]) + free(rep[i]); + } + free(rep); + } + if (pos) + free(pos); + if (cut) + free(cut); + + // Retrieve the last position where we can insert a hyphen before the given + // index. + if (before_index >= 2) { + for (size_t index = before_index - 2; index > 0; --index) { + if (hyphens[index] & 1) + return index + 1; + } + } + return 0; +} diff --git a/webkit/support/test_webkit_platform_support.h b/webkit/support/test_webkit_platform_support.h index 77c1979..e78ebb6 100644 --- a/webkit/support/test_webkit_platform_support.h +++ b/webkit/support/test_webkit_platform_support.h @@ -25,6 +25,8 @@ namespace WebKit { class WebAudioDevice; } +typedef struct _HyphenDict HyphenDict; + // An implementation of WebKitPlatformSupport for tests. class TestWebKitPlatformSupport : public webkit_glue::WebKitPlatformSupportImpl { @@ -120,6 +122,12 @@ class TestWebKitPlatformSupport : WebKit::WebMediaStreamCenterClient* client) OVERRIDE; virtual WebKit::WebRTCPeerConnectionHandler* createRTCPeerConnectionHandler( WebKit::WebRTCPeerConnectionHandlerClient* client) OVERRIDE; + virtual bool canHyphenate(const WebKit::WebString& locale) OVERRIDE; + virtual size_t computeLastHyphenLocation( + const char16* characters, + size_t length, + size_t before_index, + const WebKit::WebString& locale) OVERRIDE; private: TestShellWebMimeRegistryImpl mime_registry_; @@ -137,6 +145,7 @@ class TestWebKitPlatformSupport : bool unit_test_mode_; WebKit::WebGamepads gamepad_data_; WebKit::Platform* shadow_platform_delegate_; + HyphenDict* hyphen_dictionary_; #if defined(OS_WIN) || defined(OS_MACOSX) WebKit::WebThemeEngine* active_theme_engine_; diff --git a/webkit/support/webkit_support.gypi b/webkit/support/webkit_support.gypi index c54f97a..59e9486 100644 --- a/webkit/support/webkit_support.gypi +++ b/webkit/support/webkit_support.gypi @@ -18,6 +18,7 @@ '<(DEPTH)/net/net.gyp:net', '<(DEPTH)/skia/skia.gyp:skia', '<(DEPTH)/testing/gtest.gyp:gtest', + '<(DEPTH)/third_party/hyphen/hyphen.gyp:hyphen', '<(DEPTH)/ui/ui.gyp:ui', 'appcache', 'blob', |