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 CHROME_RENDERER_HYPHENATOR_HYPHENATOR_H_ |
| 6 #define CHROME_RENDERER_HYPHENATOR_HYPHENATOR_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <vector> |
| 10 |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/platform_file.h" |
| 13 #include "base/string16.h" |
| 14 |
| 15 namespace file_util { |
| 16 class MemoryMappedFile; |
| 17 } |
| 18 |
| 19 typedef struct _HyphenDict HyphenDict; |
| 20 |
| 21 // A class that hyphenates a word. This class encapsulates the hyphen library |
| 22 // and manages resources used by the library. When this class uses a huge |
| 23 // dictionary, it takes lots of memory. A renderer should create this object |
| 24 // only when it renders a page that needs hyphenation and deletes it when it |
| 25 // moves to a page that does not need hyphenation as listed in the following |
| 26 // snippet. |
| 27 // |
| 28 // base::PlatformFile file("hyphen.us"); |
| 29 // scoped_ptr<Hyphenator> hyphenator(new Hyphenator(file)); |
| 30 // size_t pos = hyphenator->ComputeLastHyphenLocation("organize", 8); |
| 31 // ... |
| 32 // hyphenator.reset(); |
| 33 // |
| 34 class Hyphenator { |
| 35 public: |
| 36 explicit Hyphenator(base::PlatformFile file); |
| 37 ~Hyphenator(); |
| 38 |
| 39 // Initializes the hyphen library and allocates resources needed for |
| 40 // hyphenation. |
| 41 bool Initialize(); |
| 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 private: |
| 49 // The dictionary used by the hyphen library. |
| 50 HyphenDict* dictionary_; |
| 51 |
| 52 // The dictionary file and its memory-mapping object. (Our copy of the hyphen |
| 53 // library uses a memory-mapped file opened by a browser so renderers can use |
| 54 // it without opening the file.) |
| 55 base::PlatformFile rule_file_; |
| 56 scoped_ptr<file_util::MemoryMappedFile> rule_map_; |
| 57 |
| 58 // A cached result. WebKit often calls ComputeLastHyphenLocation with the same |
| 59 // word multiple times to find the best hyphenation point when it finds a line |
| 60 // break. On the other hand, the hyphen library returns all hyphenation points |
| 61 // for a word. This class caches the hyphenation points returned by the hyphen |
| 62 // library to avoid calling the library multiple times. |
| 63 string16 word_; |
| 64 bool result_; |
| 65 std::vector<int> hyphens_; |
| 66 }; |
| 67 |
| 68 #endif // CHROME_RENDERER_HYPHENATOR_HYPHENATOR_H_ |
OLD | NEW |