Chromium Code Reviews| Index: Source/platform/fonts/harfbuzz/HarfBuzzShaper.cpp |
| diff --git a/Source/platform/fonts/harfbuzz/HarfBuzzShaper.cpp b/Source/platform/fonts/harfbuzz/HarfBuzzShaper.cpp |
| index d2bea119bbcbbe5c3dce1e3491c73e6a1e5effff..55a60268dd5a981ddea04082e2aa8ee01db377a5 100644 |
| --- a/Source/platform/fonts/harfbuzz/HarfBuzzShaper.cpp |
| +++ b/Source/platform/fonts/harfbuzz/HarfBuzzShaper.cpp |
| @@ -38,6 +38,7 @@ |
| #include "platform/fonts/Font.h" |
| #include "platform/fonts/harfbuzz/HarfBuzzFace.h" |
| #include "platform/text/SurrogatePairAwareTextIterator.h" |
| +#include "platform/text/TextBreakIterator.h" |
| #include "wtf/MathExtras.h" |
| #include "wtf/unicode/Unicode.h" |
| #include <unicode/normlzr.h> |
| @@ -208,6 +209,35 @@ static inline float harfBuzzPositionToFloat(hb_position_t value) |
| return static_cast<float>(value) / (1 << 16); |
| } |
| +static inline unsigned countGraphemesInCluster(const TextRun& run, uint16_t startIndex, uint16_t endIndex) |
| +{ |
| + if (startIndex > endIndex) { |
| + uint16_t tempIndex = startIndex; |
| + startIndex = endIndex; |
| + endIndex = tempIndex; |
| + } |
| + uint16_t length = endIndex - startIndex; |
| + TextRun subRun = run.subRun(startIndex, length); |
| + |
| + const UChar* source; |
| + String stringFor8BitRun; |
| + if (run.is8Bit()) { |
| + stringFor8BitRun = String::make16BitFrom8BitSource(subRun.characters8(), subRun.length()); |
|
eae
2014/02/14 11:15:35
I don't suppose we could have this method use m_no
|
| + source = stringFor8BitRun.characters16(); |
| + } else { |
| + source = subRun.characters16(); |
| + } |
| + TextBreakIterator* cursorPosIterator = cursorMovementIterator(source, subRun.length()); |
| + |
| + int cursorPos = cursorPosIterator->current(); |
| + int numGraphemes = -1; |
| + while (0 <= cursorPos) { |
| + cursorPos = cursorPosIterator->next(); |
| + numGraphemes++; |
| + } |
| + return numGraphemes < 0 ? 0 : numGraphemes; |
| +} |
| + |
| inline HarfBuzzShaper::HarfBuzzRun::HarfBuzzRun(const SimpleFontData* fontData, unsigned startIndex, unsigned numCharacters, TextDirection direction, hb_script_t script) |
| : m_fontData(fontData) |
| , m_startIndex(startIndex) |
| @@ -349,7 +379,7 @@ static void normalizeCharacters(const TextRun& run, unsigned length, UChar* dest |
| } |
| } |
| -HarfBuzzShaper::HarfBuzzShaper(const Font* font, const TextRun& run) |
| +HarfBuzzShaper::HarfBuzzShaper(const Font* font, const TextRun& run, ForTextEmphasisOrNot forTextEmphasis) |
| : m_font(font) |
| , m_normalizedBufferLength(0) |
| , m_run(run) |
| @@ -360,6 +390,7 @@ HarfBuzzShaper::HarfBuzzShaper(const Font* font, const TextRun& run) |
| , m_letterSpacing(font->fontDescription().letterSpacing()) |
| , m_fromIndex(0) |
| , m_toIndex(m_run.length()) |
| + , m_forTextEmphasis(forTextEmphasis) |
| { |
| m_normalizedBuffer = adoptArrayPtr(new UChar[m_run.length() + 1]); |
| normalizeCharacters(m_run, m_run.length(), m_normalizedBuffer.get(), &m_normalizedBufferLength); |
| @@ -955,7 +986,6 @@ void HarfBuzzShaper::fillGlyphBufferFromHarfBuzzRun(GlyphBuffer* glyphBuffer, Ha |
| float* advances = currentRun->advances(); |
| unsigned numGlyphs = currentRun->numGlyphs(); |
| uint16_t* glyphToCharacterIndexes = currentRun->glyphToCharacterIndexes(); |
| - |
| for (unsigned i = 0; i < numGlyphs; ++i) { |
| uint16_t currentCharacterIndex = currentRun->startIndex() + glyphToCharacterIndexes[i]; |
| FloatPoint& currentOffset = offsets[i]; |
| @@ -976,6 +1006,60 @@ void HarfBuzzShaper::fillGlyphBufferFromHarfBuzzRun(GlyphBuffer* glyphBuffer, Ha |
| } |
| } |
| +void HarfBuzzShaper::fillGlyphBufferForTextEmphasis(GlyphBuffer* glyphBuffer, HarfBuzzRun* currentRun) |
| +{ |
| + // FIXME: Instead of generating a synthetic GlyphBuffer here which is then used by the |
| + // drawEmphasisMarks method of FontFastPath, we should roll our own emphasis mark drawing function. |
| + |
| + float* advances = currentRun->advances(); |
| + unsigned numGlyphs = currentRun->numGlyphs(); |
| + uint16_t* glyphToCharacterIndexes = currentRun->glyphToCharacterIndexes(); |
| + unsigned graphemesInCluster = 1; |
| + float clusterAdvance = 0; |
| + uint16_t clusterStart; |
| + |
| + // A "cluster" in this context means a cluster as it is used by HarfBuzz: |
| + // The minimal group of characters and corresponding glyphs, that cannot be broken |
| + // down further from a text shaping point of view. |
| + // A cluster can contain multiple glyphs and grapheme clusters, with mutually |
| + // overlapping boundaries. Below we count grapheme clusters per HarfBuzz clusters, |
| + // then linearly split the sum of corresponding glyph advances by the number of |
| + // grapheme clusters in order to find positions for emphasis mark drawing. |
| + |
| + if (m_run.rtl()) |
| + clusterStart = currentRun->startIndex() + currentRun->numCharacters(); |
| + else |
| + clusterStart = currentRun->startIndex() + glyphToCharacterIndexes[0]; |
| + |
| + for (unsigned i = 0; i < numGlyphs; ++i) { |
| + uint16_t currentCharacterIndex = currentRun->startIndex() + glyphToCharacterIndexes[i]; |
| + bool isRunEnd = (i + 1 == numGlyphs); |
| + bool isClusterEnd = isRunEnd || (currentRun->startIndex() + glyphToCharacterIndexes[i + 1] != currentCharacterIndex); |
| + clusterAdvance += advances[i]; |
| + |
| + if (isClusterEnd) { |
| + uint16_t clusterEnd; |
| + if (m_run.rtl()) |
| + clusterEnd = currentCharacterIndex; |
| + else |
| + clusterEnd = isRunEnd ? currentRun->startIndex() + currentRun->numCharacters() : currentRun->startIndex() + glyphToCharacterIndexes[i + 1]; |
| + |
| + graphemesInCluster = countGraphemesInCluster(m_run, clusterStart, clusterEnd); |
| + if (!graphemesInCluster || !clusterAdvance) |
| + continue; |
| + |
| + float glyphAdvanceX = clusterAdvance / graphemesInCluster; |
| + for (unsigned j = 0; j < graphemesInCluster; ++j) { |
| + // Do not put emphasis marks on space, separator, and control characters. |
| + GlyphBufferGlyph glyphToAdd = Character::canReceiveTextEmphasis(m_run[currentCharacterIndex]) ? 1 : 0; |
| + glyphBuffer->add(glyphToAdd, currentRun->fontData(), createGlyphBufferAdvance(glyphAdvanceX, 0)); |
| + } |
| + clusterStart = clusterEnd; |
| + clusterAdvance = 0; |
| + } |
| + } |
| +} |
| + |
| bool HarfBuzzShaper::fillGlyphBuffer(GlyphBuffer* glyphBuffer) |
| { |
| unsigned numRuns = m_harfBuzzRuns.size(); |
| @@ -984,14 +1068,20 @@ bool HarfBuzzShaper::fillGlyphBuffer(GlyphBuffer* glyphBuffer) |
| for (int runIndex = numRuns - 1; runIndex >= 0; --runIndex) { |
| HarfBuzzRun* currentRun = m_harfBuzzRuns[runIndex].get(); |
| FloatPoint firstOffsetOfNextRun = !runIndex ? FloatPoint() : m_harfBuzzRuns[runIndex - 1]->offsets()[0]; |
| - fillGlyphBufferFromHarfBuzzRun(glyphBuffer, currentRun, firstOffsetOfNextRun); |
| + if (m_forTextEmphasis == ForTextEmphasis) |
| + fillGlyphBufferForTextEmphasis(glyphBuffer, currentRun); |
| + else |
| + fillGlyphBufferFromHarfBuzzRun(glyphBuffer, currentRun, firstOffsetOfNextRun); |
| } |
| } else { |
| m_startOffset = m_harfBuzzRuns.first()->offsets()[0]; |
| for (unsigned runIndex = 0; runIndex < numRuns; ++runIndex) { |
| HarfBuzzRun* currentRun = m_harfBuzzRuns[runIndex].get(); |
| FloatPoint firstOffsetOfNextRun = runIndex == numRuns - 1 ? FloatPoint() : m_harfBuzzRuns[runIndex + 1]->offsets()[0]; |
| - fillGlyphBufferFromHarfBuzzRun(glyphBuffer, currentRun, firstOffsetOfNextRun); |
| + if (m_forTextEmphasis == ForTextEmphasis) |
| + fillGlyphBufferForTextEmphasis(glyphBuffer, currentRun); |
| + else |
| + fillGlyphBufferFromHarfBuzzRun(glyphBuffer, currentRun, firstOffsetOfNextRun); |
| } |
| } |
| return glyphBuffer->size(); |