OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "platform/text/Hyphenation.h" | 5 #include "platform/text/Hyphenation.h" |
6 | 6 |
| 7 #include "platform/fonts/Font.h" |
7 #include "wtf/text/StringView.h" | 8 #include "wtf/text/StringView.h" |
8 | 9 |
9 namespace blink { | 10 namespace blink { |
10 | 11 |
11 Hyphenation::HyphenationMap& Hyphenation::getHyphenationMap() | |
12 { | |
13 DEFINE_STATIC_LOCAL(HyphenationMap, hyphenationMap, ()); | |
14 return hyphenationMap; | |
15 } | |
16 | |
17 Hyphenation* Hyphenation::get(const AtomicString& locale) | |
18 { | |
19 DCHECK(!locale.isNull()); | |
20 Hyphenation::HyphenationMap& hyphenationMap = getHyphenationMap(); | |
21 const auto& it = hyphenationMap.find(locale); | |
22 if (it != hyphenationMap.end()) | |
23 return it->value.get(); | |
24 | |
25 return hyphenationMap.add(locale, platformGetHyphenation(locale)) | |
26 .storedValue->value.get(); | |
27 } | |
28 | |
29 void Hyphenation::setForTesting(const AtomicString& locale, PassRefPtr<Hyphenati
on> hyphenation) | |
30 { | |
31 getHyphenationMap().set(locale, hyphenation); | |
32 } | |
33 | |
34 void Hyphenation::clearForTesting() | |
35 { | |
36 getHyphenationMap().clear(); | |
37 } | |
38 | |
39 Vector<size_t, 8> Hyphenation::hyphenLocations(const StringView& text) const | 12 Vector<size_t, 8> Hyphenation::hyphenLocations(const StringView& text) const |
40 { | 13 { |
41 Vector<size_t, 8> hyphenLocations; | 14 Vector<size_t, 8> hyphenLocations; |
42 size_t hyphenLocation = text.length(); | 15 size_t hyphenLocation = text.length(); |
43 if (hyphenLocation <= minimumSuffixLength) | 16 if (hyphenLocation <= minimumSuffixLength) |
44 return hyphenLocations; | 17 return hyphenLocations; |
45 hyphenLocation -= minimumSuffixLength; | 18 hyphenLocation -= minimumSuffixLength; |
46 | 19 |
47 while ((hyphenLocation = lastHyphenLocation(text, hyphenLocation)) >= minimu
mPrefixLength) | 20 while ((hyphenLocation = lastHyphenLocation(text, hyphenLocation)) >= minimu
mPrefixLength) |
48 hyphenLocations.append(hyphenLocation); | 21 hyphenLocations.append(hyphenLocation); |
49 | 22 |
50 return hyphenLocations; | 23 return hyphenLocations; |
51 } | 24 } |
52 | 25 |
| 26 int Hyphenation::minimumPrefixWidth(const Font& font) |
| 27 { |
| 28 // If the maximum width available for the prefix before the hyphen is small,
then it is very unlikely |
| 29 // that an hyphenation opportunity exists, so do not bother to look for it. |
| 30 // These are heuristic numbers for performance added in http://wkb.ug/45606 |
| 31 const int minimumPrefixWidthNumerator = 5; |
| 32 const int minimumPrefixWidthDenominator = 4; |
| 33 return font.getFontDescription().computedPixelSize() |
| 34 * minimumPrefixWidthNumerator / minimumPrefixWidthDenominator; |
| 35 } |
| 36 |
53 } // namespace blink | 37 } // namespace blink |
OLD | NEW |