OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_RENDERER_HYPHENATOR_HYPHENATOR_H_ | |
6 #define CONTENT_RENDERER_HYPHENATOR_HYPHENATOR_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/memory/scoped_handle.h" | |
11 #include "base/platform_file.h" | |
12 #include "base/strings/string16.h" | |
13 #include "content/common/content_export.h" | |
14 #include "content/public/renderer/render_process_observer.h" | |
15 #include "ipc/ipc_platform_file.h" | |
16 | |
17 typedef struct _HyphenDict HyphenDict; | |
18 | |
19 namespace content { | |
20 class RenderThread; | |
21 | |
22 // A class that hyphenates a word. This class encapsulates the hyphen library | |
23 // and manages resources used by the library. When this class uses a huge | |
24 // dictionary, it takes lots of memory (~1.3MB for English). A renderer should | |
25 // create this object only when it renders a page that needs hyphenation and | |
26 // deletes it when it moves to a page that does not need hyphenation. | |
27 class CONTENT_EXPORT Hyphenator : public RenderProcessObserver { | |
28 public: | |
29 explicit Hyphenator(base::PlatformFile file); | |
30 virtual ~Hyphenator(); | |
31 | |
32 // Initializes the hyphen library and allocates resources needed for | |
33 // hyphenation. | |
34 bool Initialize(); | |
35 | |
36 bool Attach(RenderThread* thread, const string16& locale); | |
37 | |
38 // Returns whether this object can hyphenate words. When this object does not | |
39 // have a dictionary file attached, this function sends an IPC request to open | |
40 // the file. | |
41 bool CanHyphenate(const string16& locale); | |
42 | |
43 // Returns the last hyphenation point, the position where we can insert a | |
44 // hyphen, before the given position. If there are not any hyphenation points, | |
45 // this function returns 0. | |
46 size_t ComputeLastHyphenLocation(const string16& word, size_t before_index); | |
47 | |
48 // RenderProcessObserver implementation. | |
49 virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE; | |
50 | |
51 private: | |
52 void OnSetDictionary(IPC::PlatformFileForTransit rule_file); | |
53 | |
54 // The dictionary used by the hyphen library. | |
55 HyphenDict* dictionary_; | |
56 | |
57 string16 locale_; | |
58 ScopedStdioHandle dictionary_file_; | |
59 | |
60 // A cached result. WebKit often calls ComputeLastHyphenLocation with the same | |
61 // word multiple times to find the best hyphenation point when it finds a line | |
62 // break. On the other hand, the hyphen library returns all hyphenation points | |
63 // for a word. This class caches the hyphenation points returned by the hyphen | |
64 // library to avoid calling the library multiple times. | |
65 string16 word_; | |
66 bool result_; | |
67 std::vector<int> hyphen_offsets_; | |
68 | |
69 DISALLOW_COPY_AND_ASSIGN(Hyphenator); | |
70 }; | |
71 | |
72 } // namespace content | |
73 | |
74 #endif // CONTENT_RENDERER_HYPHENATOR_HYPHENATOR_H_ | |
OLD | NEW |