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/strings/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( | |
48 const WebKit::WebString& characters, | |
49 size_t before_index, | |
50 const WebKit::WebString& locale) { | |
51 DCHECK(locale.isEmpty() || locale.equals("en") || locale.equals("en_US") || | |
52 locale.equals("en_GB")); | |
53 if (!hyphen_dictionary_) | |
54 return 0; | |
55 | |
56 // Retrieve the positions where we can insert hyphens. This function assumes | |
57 // the input word is an English word so it can use the position returned by | |
58 // the hyphen library without conversion. | |
59 base::string16 word_utf16(characters); | |
60 if (!IsStringASCII(word_utf16)) | |
61 return 0; | |
62 std::string word = StringToLowerASCII(UTF16ToASCII(word_utf16)); | |
63 scoped_ptr<char[]> hyphens(new char[word.length() + 5]); | |
64 char** rep = NULL; | |
65 int* pos = NULL; | |
66 int* cut = NULL; | |
67 int error = hnj_hyphen_hyphenate2(hyphen_dictionary_, | |
68 word.data(), | |
69 static_cast<int>(word.length()), | |
70 hyphens.get(), | |
71 NULL, | |
72 &rep, | |
73 &pos, | |
74 &cut); | |
75 if (error) | |
76 return 0; | |
77 | |
78 // Release all resources allocated by the hyphen library now because they are | |
79 // not used when hyphenating English words. | |
80 if (rep) { | |
81 for (size_t i = 0; i < word.length(); ++i) { | |
82 if (rep[i]) | |
83 free(rep[i]); | |
84 } | |
85 free(rep); | |
86 } | |
87 if (pos) | |
88 free(pos); | |
89 if (cut) | |
90 free(cut); | |
91 | |
92 // Retrieve the last position where we can insert a hyphen before the given | |
93 // index. | |
94 if (before_index >= 2) { | |
95 for (size_t index = before_index - 2; index > 0; --index) { | |
96 if (hyphens[index] & 1) | |
97 return index + 1; | |
98 } | |
99 } | |
100 return 0; | |
101 } | |
102 | |
103 } // namespace webkit_glue | |
OLD | NEW |