OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/hyphenator/hyphenator.h" | 5 #include "content/renderer/hyphenator/hyphenator.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "content/common/hyphenator_messages.h" |
| 13 #include "content/public/renderer/render_thread.h" |
12 #include "third_party/hyphen/hyphen.h" | 14 #include "third_party/hyphen/hyphen.h" |
13 #include "unicode/uscript.h" | 15 #include "unicode/uscript.h" |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 // A class that converts a sequence of UTF-8 characters to UTF-16 ones and holds | 19 // A class that converts a sequence of UTF-8 characters to UTF-16 ones and holds |
18 // only the length of converted UTF-16 characters. This class is used for | 20 // only the length of converted UTF-16 characters. This class is used for |
19 // creating a mapping from the position of a UTF-8 string to a position of a | 21 // creating a mapping from the position of a UTF-8 string to a position of a |
20 // UTF-16 string without unnecessary conversions. Even though the following | 22 // UTF-16 string without unnecessary conversions. Even though the following |
21 // snippet produces the same mapping, it needs to convert same characters many | 23 // snippet produces the same mapping, it needs to convert same characters many |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 184 |
183 Hyphenator::Hyphenator(base::PlatformFile file) | 185 Hyphenator::Hyphenator(base::PlatformFile file) |
184 : dictionary_(NULL), | 186 : dictionary_(NULL), |
185 rule_file_(file), | 187 rule_file_(file), |
186 result_(0) { | 188 result_(0) { |
187 } | 189 } |
188 | 190 |
189 Hyphenator::~Hyphenator() { | 191 Hyphenator::~Hyphenator() { |
190 if (dictionary_) | 192 if (dictionary_) |
191 hnj_hyphen_free(dictionary_); | 193 hnj_hyphen_free(dictionary_); |
| 194 if (rule_file_ != base::kInvalidPlatformFileValue) |
| 195 base::ClosePlatformFile(rule_file_); |
192 } | 196 } |
193 | 197 |
194 bool Hyphenator::Initialize() { | 198 bool Hyphenator::Initialize() { |
195 if (dictionary_) | 199 if (dictionary_) |
196 return true; | 200 return true; |
197 | 201 |
| 202 // Attach the dictionary file to the MemoryMappedFile object. When it |
| 203 // succeeds, this class does not have to close this file because it is closed |
| 204 // by the MemoryMappedFile class. To prevent this class from closing this |
| 205 // file, we reset its handle. |
198 rule_map_.reset(new file_util::MemoryMappedFile); | 206 rule_map_.reset(new file_util::MemoryMappedFile); |
199 if (!rule_map_->Initialize(rule_file_)) | 207 if (!rule_map_->Initialize(rule_file_)) |
200 return false; | 208 return false; |
| 209 rule_file_ = base::kInvalidPlatformFileValue; |
201 | 210 |
202 dictionary_ = hnj_hyphen_load(rule_map_->data(), rule_map_->length()); | 211 dictionary_ = hnj_hyphen_load(rule_map_->data(), rule_map_->length()); |
203 return !!dictionary_; | 212 return !!dictionary_; |
204 } | 213 } |
205 | 214 |
| 215 bool Hyphenator::Attach(content::RenderThread* thread, const string16& locale) { |
| 216 if (!thread) |
| 217 return false; |
| 218 locale_.assign(locale); |
| 219 thread->AddObserver(this); |
| 220 return thread->Send(new HyphenatorHostMsg_OpenDictionary(locale)); |
| 221 } |
| 222 |
| 223 bool Hyphenator::CanHyphenate(const string16& locale) { |
| 224 return !locale_.compare(locale); |
| 225 } |
| 226 |
206 size_t Hyphenator::ComputeLastHyphenLocation(const string16& word, | 227 size_t Hyphenator::ComputeLastHyphenLocation(const string16& word, |
207 size_t before_index) { | 228 size_t before_index) { |
208 if (!dictionary_ || word.empty()) | 229 if (!Initialize() || word.empty()) |
209 return 0; | 230 return 0; |
210 | 231 |
211 // Call the hyphen library to get all hyphenation points, i.e. positions where | 232 // Call the hyphen library to get all hyphenation points, i.e. positions where |
212 // we can insert hyphens. When WebKit finds a line-break, it calls this | 233 // we can insert hyphens. When WebKit finds a line-break, it calls this |
213 // function twice or more with the same word to find the best hyphenation | 234 // function twice or more with the same word to find the best hyphenation |
214 // point. To avoid calling the hyphen library twice or more with the same | 235 // point. To avoid calling the hyphen library twice or more with the same |
215 // word, we cache the last query. | 236 // word, we cache the last query. |
216 if (word_ != word) { | 237 if (word_ != word) { |
217 word_ = word; | 238 word_ = word; |
218 Query query(word); | 239 Query query(word); |
219 result_ = query.Hyphenate(dictionary_, &hyphen_offsets_); | 240 result_ = query.Hyphenate(dictionary_, &hyphen_offsets_); |
220 } | 241 } |
221 if (!result_) | 242 if (!result_) |
222 return 0; | 243 return 0; |
223 for (std::vector<int>::reverse_iterator it = hyphen_offsets_.rbegin(); | 244 for (std::vector<int>::reverse_iterator it = hyphen_offsets_.rbegin(); |
224 it != hyphen_offsets_.rend(); ++it) { | 245 it != hyphen_offsets_.rend(); ++it) { |
225 if (static_cast<size_t>(*it) < before_index) | 246 if (static_cast<size_t>(*it) < before_index) |
226 return *it; | 247 return *it; |
227 } | 248 } |
228 return 0; | 249 return 0; |
229 } | 250 } |
230 | 251 |
| 252 bool Hyphenator::OnControlMessageReceived(const IPC::Message& message) { |
| 253 bool handled = true; |
| 254 IPC_BEGIN_MESSAGE_MAP(Hyphenator, message) |
| 255 IPC_MESSAGE_HANDLER(HyphenatorMsg_SetDictionary, OnSetDictionary) |
| 256 IPC_MESSAGE_UNHANDLED(handled = false) |
| 257 IPC_END_MESSAGE_MAP() |
| 258 return handled; |
| 259 } |
| 260 |
| 261 void Hyphenator::OnSetDictionary(IPC::PlatformFileForTransit file) { |
| 262 base::PlatformFile rule_file = |
| 263 IPC::PlatformFileForTransitToPlatformFile(file); |
| 264 if (rule_file == base::kInvalidPlatformFileValue) |
| 265 return; |
| 266 // Delete the current dictionary and save the given file to this object. We |
| 267 // initialize the hyphen library the first time when WebKit actually |
| 268 // hyphenates a word, i.e. when WebKit calls the ComputeLastHyphenLocation |
| 269 // function. (WebKit does not always hyphenate words even when it calls the |
| 270 // CanHyphenate function, e.g. WebKit does not have to hyphenate words when it |
| 271 // does not have to break text into lines.) |
| 272 if (dictionary_) { |
| 273 hnj_hyphen_free(dictionary_); |
| 274 dictionary_ = NULL; |
| 275 } |
| 276 rule_map_.reset(); |
| 277 rule_file_ = rule_file; |
| 278 } |
| 279 |
231 } // namespace content | 280 } // namespace content |
OLD | NEW |