Chromium Code Reviews| Index: Source/platform/fonts/shaping/CachingWordShapeIterator.h |
| diff --git a/Source/platform/fonts/shaping/CachingWordShapeIterator.h b/Source/platform/fonts/shaping/CachingWordShapeIterator.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..46e9839881b5cf4082382b50aa202ca603739d86 |
| --- /dev/null |
| +++ b/Source/platform/fonts/shaping/CachingWordShapeIterator.h |
| @@ -0,0 +1,132 @@ |
| +/* |
| + * Copyright (C) 2015 Google Inc. All rights reserved. |
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions |
| + * are met: |
| + * 1. Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * 2. Redistributions in binary form must reproduce the above copyright |
| + * notice, this list of conditions and the following disclaimer in the |
| + * documentation and/or other materials provided with the distribution. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY |
| + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#ifndef CachingWordShapeIterator_h |
| +#define CachingWordShapeIterator_h |
| + |
| +#include "platform/fonts/SimpleFontData.h" |
| +#include "platform/fonts/shaping/CachingWordShapeIterator.h" |
| +#include "platform/fonts/shaping/HarfBuzzShaper.h" |
| +#include "platform/fonts/shaping/ShapeCache.h" |
| +#include "wtf/unicode/CharacterNames.h" |
| + |
| +namespace blink { |
| + |
| +class CachingWordShapeIterator { |
| +public: |
| + CachingWordShapeIterator(ShapeCache* cache, const TextRun& run, |
| + const Font* font, HashSet<const SimpleFontData*>* fallbackFonts) |
| + : m_shapeCache(cache), m_textRun(run), m_font(font) |
| + , m_fallbackFonts(fallbackFonts), m_startIndex(0) |
| + { |
| + ASSERT(font); |
| + const FontDescription& fontDescription = font->fontDescription(); |
| + bool hasWordSpacingOrLetterSpacing = fontDescription.wordSpacing() |
| + || fontDescription.letterSpacing(); |
| + // Word spacing and letter spacing can change the width of a word. |
| + // If a tab occurs inside a word, the width of the word varies based on |
| + // its position on the line. |
| + // If expansion is used (for justified text) the spacing between words |
| + // change and thus we cannot use the word cache. |
| + // FIXME: Move expansion handling from shaping to ShapeResult and add |
| + // support for expansion to shapeWord. |
| + m_isCacheable = !hasWordSpacingOrLetterSpacing && !run.allowTabs() |
| + && m_textRun.expansion() == 0.0f; |
| + |
| + // Shaping word by word is faster as each word is cached. If we cannot |
| + // use the cache or if the font doesn't support word by word shaping |
| + // fall back on shaping the entire run. |
| + m_shapeByWord = m_font->canShapeWordByWord() && m_isCacheable; |
| + } |
| + |
| + bool next(RefPtr<ShapeResult>* wordResult) |
| + { |
| + if (!m_shapeByWord) { |
| + if (m_startIndex) |
| + return false; |
| + *wordResult = shapeWord(m_textRun, m_font, m_fallbackFonts); |
| + m_startIndex = 1; |
| + return *wordResult; |
| + } |
| + |
| + unsigned length = m_textRun.length(); |
| + if (m_startIndex < length) { |
| + if (m_textRun[m_startIndex] == spaceCharacter) { |
| + if (!m_spaceShape) { |
| + TextRun wordRun = m_textRun.subRun(m_startIndex, 1); |
| + m_spaceShape = shapeWord(wordRun, m_font, m_fallbackFonts); |
| + } |
| + *wordResult = m_spaceShape; |
| + m_startIndex++; |
| + return true; |
| + } |
| + |
| + for (unsigned i = m_startIndex; ; i++) { |
| + if (i == length || m_textRun[i] == spaceCharacter) { |
| + TextRun wordRun = m_textRun.subRun(m_startIndex, |
| + i - m_startIndex); |
| + *wordResult = shapeWord(wordRun, m_font, m_fallbackFonts); |
| + m_startIndex = i; |
| + return true; |
| + } |
| + } |
| + } |
| + return false; |
| + } |
| + |
| +private: |
| + PassRefPtr<ShapeResult> shapeWord(const TextRun& wordRun, |
| + const Font* font, HashSet<const SimpleFontData*>* fallbackFonts) |
| + { |
| + ShapeCacheEntry* cacheEntry = m_isCacheable |
| + ? m_shapeCache->add(wordRun, ShapeCacheEntry()) |
| + : nullptr; |
| + if (cacheEntry && cacheEntry->m_shapeResult) |
| + return cacheEntry->m_shapeResult; |
| + |
| + HarfBuzzShaper shaper(font, wordRun, fallbackFonts); |
| + RefPtr<ShapeResult> shapeResult = shaper.shapeResult(); |
| + if (!shapeResult) |
| + return nullptr; |
| + |
| + if (cacheEntry && (!fallbackFonts || fallbackFonts->isEmpty())) |
|
drott
2015/06/23 12:45:51
Could you add a similar FIXME + crbug for the fact
|
| + cacheEntry->m_shapeResult = shapeResult; |
| + |
| + return shapeResult.release(); |
| + } |
| + |
| + ShapeCache* m_shapeCache; |
| + const TextRun& m_textRun; |
| + const Font* m_font; |
| + HashSet<const SimpleFontData*>* m_fallbackFonts; |
| + unsigned m_startIndex : 30; |
| + unsigned m_isCacheable : 1; |
|
drott
2015/06/23 12:45:51
When reviewing, I struggled to understand what thi
|
| + unsigned m_shapeByWord : 1; |
| + RefPtr<ShapeResult> m_spaceShape; |
| +}; |
| + |
| +} // namespace blink |
| + |
| +#endif // CachingWordShapeIterator_h |