Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 "webkit/mocks/mock_webhyphenator.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/scoped_handle.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "third_party/hyphen/hyphen.h" | |
| 12 | |
| 13 namespace webkit_glue { | |
| 14 | |
| 15 MockWebHyphenator::MockWebHyphenator() | |
| 16 : hyphen_dictionary_(NULL) { | |
| 17 } | |
| 18 | |
| 19 MockWebHyphenator::~MockWebHyphenator() { | |
| 20 if (hyphen_dictionary_) | |
| 21 hnj_hyphen_free(hyphen_dictionary_); | |
| 22 } | |
| 23 | |
| 24 void MockWebHyphenator::LoadDictionary(base::PlatformFile dict_file) { | |
| 25 CHECK(!hyphen_dictionary_); | |
| 26 // Initialize the hyphen library with a sample dictionary. To avoid test | |
| 27 // flakiness, this code synchronously loads the dictionary. | |
| 28 if (dict_file == base::kInvalidPlatformFileValue) { | |
| 29 NOTREACHED(); | |
| 30 return; | |
| 31 } | |
| 32 ScopedStdioHandle dict_handle(base::FdopenPlatformFile(dict_file, "r")); | |
| 33 if (!dict_handle.get()) { | |
| 34 NOTREACHED(); | |
| 35 base::ClosePlatformFile(dict_file); | |
| 36 return; | |
| 37 } | |
| 38 hyphen_dictionary_ = hnj_hyphen_load_file(dict_handle.get()); | |
| 39 DCHECK(hyphen_dictionary_); | |
| 40 } | |
| 41 | |
| 42 bool MockWebHyphenator::canHyphenate(const WebKit::WebString& locale) { | |
| 43 return locale.isEmpty() || locale.equals("en") || locale.equals("en_US") || | |
| 44 locale.equals("en_GB"); | |
| 45 } | |
| 46 | |
| 47 size_t MockWebHyphenator::computeLastHyphenLocation( | |
|
darin (slow to review)
2013/02/27 21:59:46
Is this really a "mock" hyphenator? It seems like
| |
| 48 const char16* characters, | |
| 49 size_t length, | |
| 50 size_t before_index, | |
| 51 const WebKit::WebString& locale) { | |
| 52 DCHECK(locale.isEmpty() || locale.equals("en") || locale.equals("en_US") || | |
| 53 locale.equals("en_GB")); | |
| 54 if (!hyphen_dictionary_) | |
| 55 return 0; | |
| 56 | |
| 57 // Retrieve the positions where we can insert hyphens. This function assumes | |
| 58 // the input word is an English word so it can use the position returned by | |
| 59 // the hyphen library without conversion. | |
| 60 string16 word_utf16(characters, length); | |
| 61 if (!IsStringASCII(word_utf16)) | |
| 62 return 0; | |
| 63 std::string word = StringToLowerASCII(UTF16ToASCII(word_utf16)); | |
| 64 scoped_array<char> hyphens(new char[word.length() + 5]); | |
| 65 char** rep = NULL; | |
| 66 int* pos = NULL; | |
| 67 int* cut = NULL; | |
| 68 int error = hnj_hyphen_hyphenate2(hyphen_dictionary_, | |
| 69 word.data(), | |
| 70 static_cast<int>(word.length()), | |
| 71 hyphens.get(), | |
| 72 NULL, | |
| 73 &rep, | |
| 74 &pos, | |
| 75 &cut); | |
| 76 if (error) | |
| 77 return 0; | |
| 78 | |
| 79 // Release all resources allocated by the hyphen library now because they are | |
| 80 // not used when hyphenating English words. | |
| 81 if (rep) { | |
| 82 for (size_t i = 0; i < word.length(); ++i) { | |
| 83 if (rep[i]) | |
| 84 free(rep[i]); | |
| 85 } | |
| 86 free(rep); | |
| 87 } | |
| 88 if (pos) | |
| 89 free(pos); | |
| 90 if (cut) | |
| 91 free(cut); | |
| 92 | |
| 93 // Retrieve the last position where we can insert a hyphen before the given | |
| 94 // index. | |
| 95 if (before_index >= 2) { | |
| 96 for (size_t index = before_index - 2; index > 0; --index) { | |
| 97 if (hyphens[index] & 1) | |
| 98 return index + 1; | |
| 99 } | |
| 100 } | |
| 101 return 0; | |
| 102 } | |
| 103 | |
| 104 } // namespace webkit_glue | |
| OLD | NEW |