diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-01 07:48:07 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-01 07:48:07 +0000 |
commit | ea315d056841c6ea0f30f17b2e61ce930c1c5ccd (patch) | |
tree | 1c44dc5e0e5810cd949a0c276bab437ab3add79a /webkit/mocks | |
parent | 9b2608479f2cca81e90ab4ab91ddae2fe9a0723c (diff) | |
download | chromium_src-ea315d056841c6ea0f30f17b2e61ce930c1c5ccd.zip chromium_src-ea315d056841c6ea0f30f17b2e61ce930c1c5ccd.tar.gz chromium_src-ea315d056841c6ea0f30f17b2e61ce930c1c5ccd.tar.bz2 |
Switch from using individual methods for hyphenation to using the WebHyphantor interface
Also allow the content embedder to override the hyphenator used.
BUG=178693
Review URL: https://codereview.chromium.org/12335128
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@185486 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/mocks')
-rw-r--r-- | webkit/mocks/mock_webhyphenator.cc | 104 | ||||
-rw-r--r-- | webkit/mocks/mock_webhyphenator.h | 45 |
2 files changed, 149 insertions, 0 deletions
diff --git a/webkit/mocks/mock_webhyphenator.cc b/webkit/mocks/mock_webhyphenator.cc new file mode 100644 index 0000000..903197b --- /dev/null +++ b/webkit/mocks/mock_webhyphenator.cc @@ -0,0 +1,104 @@ +// Copyright (c) 2013 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 "webkit/mocks/mock_webhyphenator.h" + +#include "base/logging.h" +#include "base/memory/scoped_handle.h" +#include "base/memory/scoped_ptr.h" +#include "base/string_util.h" +#include "third_party/hyphen/hyphen.h" + +namespace webkit_glue { + +MockWebHyphenator::MockWebHyphenator() + : hyphen_dictionary_(NULL) { +} + +MockWebHyphenator::~MockWebHyphenator() { + if (hyphen_dictionary_) + hnj_hyphen_free(hyphen_dictionary_); +} + +void MockWebHyphenator::LoadDictionary(base::PlatformFile dict_file) { + CHECK(!hyphen_dictionary_); + // Initialize the hyphen library with a sample dictionary. To avoid test + // flakiness, this code synchronously loads the dictionary. + if (dict_file == base::kInvalidPlatformFileValue) { + NOTREACHED(); + return; + } + ScopedStdioHandle dict_handle(base::FdopenPlatformFile(dict_file, "r")); + if (!dict_handle.get()) { + NOTREACHED(); + base::ClosePlatformFile(dict_file); + return; + } + hyphen_dictionary_ = hnj_hyphen_load_file(dict_handle.get()); + DCHECK(hyphen_dictionary_); +} + +bool MockWebHyphenator::canHyphenate(const WebKit::WebString& locale) { + return locale.isEmpty() || locale.equals("en") || locale.equals("en_US") || + locale.equals("en_GB"); +} + +size_t MockWebHyphenator::computeLastHyphenLocation( + const char16* characters, + size_t length, + size_t before_index, + const WebKit::WebString& locale) { + DCHECK(locale.isEmpty() || locale.equals("en") || locale.equals("en_US") || + locale.equals("en_GB")); + 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. + string16 word_utf16(characters, length); + if (!IsStringASCII(word_utf16)) + return 0; + std::string word = StringToLowerASCII(UTF16ToASCII(word_utf16)); + 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; +} + +} // namespace webkit_glue diff --git a/webkit/mocks/mock_webhyphenator.h b/webkit/mocks/mock_webhyphenator.h new file mode 100644 index 0000000..8615e16 --- /dev/null +++ b/webkit/mocks/mock_webhyphenator.h @@ -0,0 +1,45 @@ +// Copyright (c) 2013 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 WEBKIT_MOCKS_MOCK_WEBHYPHENATOR_H_ +#define WEBKIT_MOCKS_MOCK_WEBHYPHENATOR_H_ + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "base/platform_file.h" +#include "third_party/WebKit/Source/Platform/chromium/public/WebHyphenator.h" + +typedef struct _HyphenDict HyphenDict; + +namespace webkit_glue { + +// Implements a simple WebHyphenator that only supports en-US. It is used for +// layout tests that expect that hyphenator to be available synchronously. +// Therefore, this class supports synchronous loading of the dictionary as well. +class MockWebHyphenator : public WebKit::WebHyphenator { + public: + MockWebHyphenator(); + virtual ~MockWebHyphenator(); + + // Loads the hyphenation dictionary. |dict_file| should be an open fd to + // third_party/hyphen/hyph_en_US.dic. + void LoadDictionary(base::PlatformFile dict_file); + + // WebHyphenator implementation. + 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: + HyphenDict* hyphen_dictionary_; + + DISALLOW_COPY_AND_ASSIGN(MockWebHyphenator); +}; + +} // namespace webkit_glue + +#endif // WEBKIT_MOCKS_MOCK_WEBHYPHENATOR_H_ |