OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2015 Google Inc. All rights reserved. | 2 * Copyright (C) 2015 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 19 matching lines...) Expand all Loading... |
30 #include "platform/fonts/shaping/CachingWordShapeIterator.h" | 30 #include "platform/fonts/shaping/CachingWordShapeIterator.h" |
31 #include "platform/fonts/shaping/HarfBuzzShaper.h" | 31 #include "platform/fonts/shaping/HarfBuzzShaper.h" |
32 #include "platform/fonts/shaping/ShapeCache.h" | 32 #include "platform/fonts/shaping/ShapeCache.h" |
33 #include "wtf/text/CharacterNames.h" | 33 #include "wtf/text/CharacterNames.h" |
34 | 34 |
35 namespace blink { | 35 namespace blink { |
36 | 36 |
37 class CachingWordShapeIterator { | 37 class CachingWordShapeIterator { |
38 public: | 38 public: |
39 CachingWordShapeIterator(ShapeCache* cache, const TextRun& run, | 39 CachingWordShapeIterator(ShapeCache* cache, const TextRun& run, |
40 const Font* font, HashSet<const SimpleFontData*>* fallbackFonts) | 40 const Font* font) |
41 : m_shapeCache(cache), m_textRun(run), m_font(font) | 41 : m_shapeCache(cache), m_textRun(run), m_font(font), m_startIndex(0) |
42 , m_fallbackFonts(fallbackFonts), m_startIndex(0) | |
43 { | 42 { |
44 ASSERT(font); | 43 ASSERT(font); |
45 const FontDescription& fontDescription = font->fontDescription(); | 44 const FontDescription& fontDescription = font->fontDescription(); |
46 | 45 |
47 // Word and letter spacing can change the width of a word, as can tabs | 46 // Word and letter spacing can change the width of a word, as can tabs |
48 // as we segment solely based on on space characters. | 47 // as we segment solely based on on space characters. |
49 // If expansion is used (for justified text) the spacing between words | 48 // If expansion is used (for justified text) the spacing between words |
50 // change and thus we need to shape the entire run. | 49 // change and thus we need to shape the entire run. |
51 m_wordResultCachable = !fontDescription.wordSpacing() | 50 m_wordResultCachable = !fontDescription.wordSpacing() |
52 && !fontDescription.letterSpacing() && !run.allowTabs() | 51 && !fontDescription.letterSpacing() && !run.allowTabs() |
53 && m_textRun.expansion() == 0.0f; | 52 && m_textRun.expansion() == 0.0f; |
54 | 53 |
55 // Shaping word by word is faster as each word is cached. If we cannot | 54 // Shaping word by word is faster as each word is cached. If we cannot |
56 // use the cache or if the font doesn't support word by word shaping | 55 // use the cache or if the font doesn't support word by word shaping |
57 // fall back on shaping the entire run. | 56 // fall back on shaping the entire run. |
58 m_shapeByWord = m_wordResultCachable && m_font->canShapeWordByWord(); | 57 m_shapeByWord = m_wordResultCachable && m_font->canShapeWordByWord(); |
59 } | 58 } |
60 | 59 |
61 bool next(RefPtr<ShapeResult>* wordResult) | 60 bool next(RefPtr<ShapeResult>* wordResult) |
62 { | 61 { |
63 if (!m_shapeByWord) { | 62 if (!m_shapeByWord) { |
64 if (m_startIndex) | 63 if (m_startIndex) |
65 return false; | 64 return false; |
66 *wordResult = shapeWord(m_textRun, m_font, m_fallbackFonts); | 65 *wordResult = shapeWord(m_textRun, m_font); |
67 m_startIndex = 1; | 66 m_startIndex = 1; |
68 return *wordResult; | 67 return *wordResult; |
69 } | 68 } |
70 | 69 |
71 unsigned length = m_textRun.length(); | 70 unsigned length = m_textRun.length(); |
72 if (m_startIndex < length) { | 71 if (m_startIndex < length) { |
73 if (m_textRun[m_startIndex] == spaceCharacter) { | 72 if (m_textRun[m_startIndex] == spaceCharacter) { |
74 TextRun wordRun = m_textRun.subRun(m_startIndex, 1); | 73 TextRun wordRun = m_textRun.subRun(m_startIndex, 1); |
75 *wordResult = shapeWord(wordRun, m_font, m_fallbackFonts); | 74 *wordResult = shapeWord(wordRun, m_font); |
76 m_startIndex++; | 75 m_startIndex++; |
77 return true; | 76 return true; |
78 } | 77 } |
79 | 78 |
80 for (unsigned i = m_startIndex; ; i++) { | 79 for (unsigned i = m_startIndex; ; i++) { |
81 if (i == length || m_textRun[i] == spaceCharacter) { | 80 if (i == length || m_textRun[i] == spaceCharacter) { |
82 TextRun wordRun = m_textRun.subRun(m_startIndex, | 81 TextRun wordRun = m_textRun.subRun(m_startIndex, |
83 i - m_startIndex); | 82 i - m_startIndex); |
84 *wordResult = shapeWord(wordRun, m_font, m_fallbackFonts); | 83 *wordResult = shapeWord(wordRun, m_font); |
85 m_startIndex = i; | 84 m_startIndex = i; |
86 return true; | 85 return true; |
87 } | 86 } |
88 } | 87 } |
89 } | 88 } |
90 return false; | 89 return false; |
91 } | 90 } |
92 | 91 |
93 private: | 92 private: |
94 void setFallbackFonts(const ShapeResult* wordResult, | 93 PassRefPtr<ShapeResult> shapeWord(const TextRun& wordRun, const Font* font) |
95 HashSet<const SimpleFontData*>* fallbackFonts) | |
96 { | |
97 if (fallbackFonts) { | |
98 for (auto& fallbackFont : *wordResult->fallbackFonts()) | |
99 fallbackFonts->add(fallbackFont.get()); | |
100 } | |
101 } | |
102 | |
103 PassRefPtr<ShapeResult> shapeWord(const TextRun& wordRun, | |
104 const Font* font, HashSet<const SimpleFontData*>* fallbackFonts) | |
105 { | 94 { |
106 ShapeCacheEntry* cacheEntry = m_wordResultCachable | 95 ShapeCacheEntry* cacheEntry = m_wordResultCachable |
107 ? m_shapeCache->add(wordRun, ShapeCacheEntry()) | 96 ? m_shapeCache->add(wordRun, ShapeCacheEntry()) |
108 : nullptr; | 97 : nullptr; |
109 if (cacheEntry && cacheEntry->m_shapeResult) { | 98 if (cacheEntry && cacheEntry->m_shapeResult) |
110 setFallbackFonts(cacheEntry->m_shapeResult.get(), fallbackFonts); | |
111 return cacheEntry->m_shapeResult; | 99 return cacheEntry->m_shapeResult; |
112 } | |
113 | 100 |
114 HashSet<const SimpleFontData*> fallbackFontsForWord; | 101 HarfBuzzShaper shaper(font, wordRun); |
115 HarfBuzzShaper shaper(font, wordRun, &fallbackFontsForWord); | |
116 RefPtr<ShapeResult> shapeResult = shaper.shapeResult(); | 102 RefPtr<ShapeResult> shapeResult = shaper.shapeResult(); |
117 if (!shapeResult) | 103 if (!shapeResult) |
118 return nullptr; | 104 return nullptr; |
119 | 105 |
120 if (cacheEntry) | 106 if (cacheEntry) |
121 cacheEntry->m_shapeResult = shapeResult; | 107 cacheEntry->m_shapeResult = shapeResult; |
122 | 108 |
123 setFallbackFonts(shapeResult.get(), fallbackFonts); | |
124 return shapeResult.release(); | 109 return shapeResult.release(); |
125 } | 110 } |
126 | 111 |
127 ShapeCache* m_shapeCache; | 112 ShapeCache* m_shapeCache; |
128 const TextRun& m_textRun; | 113 const TextRun& m_textRun; |
129 const Font* m_font; | 114 const Font* m_font; |
130 HashSet<const SimpleFontData*>* m_fallbackFonts; | |
131 unsigned m_startIndex : 30; | 115 unsigned m_startIndex : 30; |
132 unsigned m_wordResultCachable : 1; | 116 unsigned m_wordResultCachable : 1; |
133 unsigned m_shapeByWord : 1; | 117 unsigned m_shapeByWord : 1; |
134 }; | 118 }; |
135 | 119 |
136 } // namespace blink | 120 } // namespace blink |
137 | 121 |
138 #endif // CachingWordShapeIterator_h | 122 #endif // CachingWordShapeIterator_h |
OLD | NEW |