Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: Source/platform/fonts/harfbuzz/HarfBuzzShaper.cpp

Issue 130433006: Implement CSS Emphasis Marks for complex text (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: countGraphemesInCluster based on m_normalizedBuffer (win build fix) Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/platform/fonts/harfbuzz/HarfBuzzShaper.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 Google Inc. All rights reserved. 2 * Copyright (c) 2012 Google Inc. All rights reserved.
3 * Copyright (C) 2013 BlackBerry Limited. All rights reserved. 3 * Copyright (C) 2013 BlackBerry Limited. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 6 * modification, are permitted provided that the following conditions are
7 * met: 7 * met:
8 * 8 *
9 * * Redistributions of source code must retain the above copyright 9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 20 matching lines...) Expand all
31 31
32 #include "config.h" 32 #include "config.h"
33 #include "platform/fonts/harfbuzz/HarfBuzzShaper.h" 33 #include "platform/fonts/harfbuzz/HarfBuzzShaper.h"
34 34
35 #include "RuntimeEnabledFeatures.h" 35 #include "RuntimeEnabledFeatures.h"
36 #include "hb-icu.h" 36 #include "hb-icu.h"
37 #include "platform/fonts/Character.h" 37 #include "platform/fonts/Character.h"
38 #include "platform/fonts/Font.h" 38 #include "platform/fonts/Font.h"
39 #include "platform/fonts/harfbuzz/HarfBuzzFace.h" 39 #include "platform/fonts/harfbuzz/HarfBuzzFace.h"
40 #include "platform/text/SurrogatePairAwareTextIterator.h" 40 #include "platform/text/SurrogatePairAwareTextIterator.h"
41 #include "platform/text/TextBreakIterator.h"
41 #include "wtf/MathExtras.h" 42 #include "wtf/MathExtras.h"
42 #include "wtf/unicode/Unicode.h" 43 #include "wtf/unicode/Unicode.h"
43 #include <unicode/normlzr.h> 44 #include <unicode/normlzr.h>
44 #include <unicode/uchar.h> 45 #include <unicode/uchar.h>
45 46
46 #include <list> 47 #include <list>
47 #include <map> 48 #include <map>
48 #include <string> 49 #include <string>
49 50
50 namespace WebCore { 51 namespace WebCore {
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 { 202 {
202 DEFINE_STATIC_LOCAL(HarfBuzzRunCache, globalHarfBuzzRunCache, ()); 203 DEFINE_STATIC_LOCAL(HarfBuzzRunCache, globalHarfBuzzRunCache, ());
203 return globalHarfBuzzRunCache; 204 return globalHarfBuzzRunCache;
204 } 205 }
205 206
206 static inline float harfBuzzPositionToFloat(hb_position_t value) 207 static inline float harfBuzzPositionToFloat(hb_position_t value)
207 { 208 {
208 return static_cast<float>(value) / (1 << 16); 209 return static_cast<float>(value) / (1 << 16);
209 } 210 }
210 211
212 static inline unsigned countGraphemesInCluster(const UChar* normalizedBuffer, un signed normalizedBufferLength, uint16_t startIndex, uint16_t endIndex)
213 {
214 if (startIndex > endIndex) {
215 uint16_t tempIndex = startIndex;
216 startIndex = endIndex;
217 endIndex = tempIndex;
218 }
219 uint16_t length = endIndex - startIndex;
220 ASSERT(static_cast<unsigned>(startIndex + length) <= normalizedBufferLength) ;
221 TextBreakIterator* cursorPosIterator = cursorMovementIterator(&normalizedBuf fer[startIndex], length);
222
223 int cursorPos = cursorPosIterator->current();
224 int numGraphemes = -1;
225 while (0 <= cursorPos) {
226 cursorPos = cursorPosIterator->next();
227 numGraphemes++;
228 }
229 return numGraphemes < 0 ? 0 : numGraphemes;
230 }
231
211 inline HarfBuzzShaper::HarfBuzzRun::HarfBuzzRun(const SimpleFontData* fontData, unsigned startIndex, unsigned numCharacters, TextDirection direction, hb_script_ t script) 232 inline HarfBuzzShaper::HarfBuzzRun::HarfBuzzRun(const SimpleFontData* fontData, unsigned startIndex, unsigned numCharacters, TextDirection direction, hb_script_ t script)
212 : m_fontData(fontData) 233 : m_fontData(fontData)
213 , m_startIndex(startIndex) 234 , m_startIndex(startIndex)
214 , m_numCharacters(numCharacters) 235 , m_numCharacters(numCharacters)
215 , m_numGlyphs(0) 236 , m_numGlyphs(0)
216 , m_direction(direction) 237 , m_direction(direction)
217 , m_script(script) 238 , m_script(script)
218 , m_width(0) 239 , m_width(0)
219 { 240 {
220 } 241 }
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 // Don't normalize tabs as they are not treated as spaces for word-end. 363 // Don't normalize tabs as they are not treated as spaces for word-end.
343 if (Character::treatAsSpace(character) && character != '\t') 364 if (Character::treatAsSpace(character) && character != '\t')
344 character = ' '; 365 character = ' ';
345 else if (Character::treatAsZeroWidthSpaceInComplexScript(character)) 366 else if (Character::treatAsZeroWidthSpaceInComplexScript(character))
346 character = zeroWidthSpace; 367 character = zeroWidthSpace;
347 U16_APPEND(destination, *destinationLength, length, character, error); 368 U16_APPEND(destination, *destinationLength, length, character, error);
348 ASSERT_UNUSED(error, !error); 369 ASSERT_UNUSED(error, !error);
349 } 370 }
350 } 371 }
351 372
352 HarfBuzzShaper::HarfBuzzShaper(const Font* font, const TextRun& run) 373 HarfBuzzShaper::HarfBuzzShaper(const Font* font, const TextRun& run, ForTextEmph asisOrNot forTextEmphasis)
353 : m_font(font) 374 : m_font(font)
354 , m_normalizedBufferLength(0) 375 , m_normalizedBufferLength(0)
355 , m_run(run) 376 , m_run(run)
356 , m_wordSpacingAdjustment(font->fontDescription().wordSpacing()) 377 , m_wordSpacingAdjustment(font->fontDescription().wordSpacing())
357 , m_padding(0) 378 , m_padding(0)
358 , m_padPerWordBreak(0) 379 , m_padPerWordBreak(0)
359 , m_padError(0) 380 , m_padError(0)
360 , m_letterSpacing(font->fontDescription().letterSpacing()) 381 , m_letterSpacing(font->fontDescription().letterSpacing())
361 , m_fromIndex(0) 382 , m_fromIndex(0)
362 , m_toIndex(m_run.length()) 383 , m_toIndex(m_run.length())
384 , m_forTextEmphasis(forTextEmphasis)
363 { 385 {
364 m_normalizedBuffer = adoptArrayPtr(new UChar[m_run.length() + 1]); 386 m_normalizedBuffer = adoptArrayPtr(new UChar[m_run.length() + 1]);
365 normalizeCharacters(m_run, m_run.length(), m_normalizedBuffer.get(), &m_norm alizedBufferLength); 387 normalizeCharacters(m_run, m_run.length(), m_normalizedBuffer.get(), &m_norm alizedBufferLength);
366 setPadding(m_run.expansion()); 388 setPadding(m_run.expansion());
367 setFontFeatures(); 389 setFontFeatures();
368 } 390 }
369 391
370 static void normalizeSpacesAndMirrorChars(const UChar* source, unsigned length, UChar* destination, unsigned* destinationLength, HarfBuzzShaper::NormalizeMode n ormalizeMode) 392 static void normalizeSpacesAndMirrorChars(const UChar* source, unsigned length, UChar* destination, unsigned* destinationLength, HarfBuzzShaper::NormalizeMode n ormalizeMode)
371 { 393 {
372 unsigned position = 0; 394 unsigned position = 0;
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after
948 m_totalWidth += currentRun->width(); 970 m_totalWidth += currentRun->width();
949 } 971 }
950 972
951 void HarfBuzzShaper::fillGlyphBufferFromHarfBuzzRun(GlyphBuffer* glyphBuffer, Ha rfBuzzRun* currentRun, FloatPoint& firstOffsetOfNextRun) 973 void HarfBuzzShaper::fillGlyphBufferFromHarfBuzzRun(GlyphBuffer* glyphBuffer, Ha rfBuzzRun* currentRun, FloatPoint& firstOffsetOfNextRun)
952 { 974 {
953 FloatPoint* offsets = currentRun->offsets(); 975 FloatPoint* offsets = currentRun->offsets();
954 uint16_t* glyphs = currentRun->glyphs(); 976 uint16_t* glyphs = currentRun->glyphs();
955 float* advances = currentRun->advances(); 977 float* advances = currentRun->advances();
956 unsigned numGlyphs = currentRun->numGlyphs(); 978 unsigned numGlyphs = currentRun->numGlyphs();
957 uint16_t* glyphToCharacterIndexes = currentRun->glyphToCharacterIndexes(); 979 uint16_t* glyphToCharacterIndexes = currentRun->glyphToCharacterIndexes();
958
959 for (unsigned i = 0; i < numGlyphs; ++i) { 980 for (unsigned i = 0; i < numGlyphs; ++i) {
960 uint16_t currentCharacterIndex = currentRun->startIndex() + glyphToChara cterIndexes[i]; 981 uint16_t currentCharacterIndex = currentRun->startIndex() + glyphToChara cterIndexes[i];
961 FloatPoint& currentOffset = offsets[i]; 982 FloatPoint& currentOffset = offsets[i];
962 FloatPoint& nextOffset = (i == numGlyphs - 1) ? firstOffsetOfNextRun : o ffsets[i + 1]; 983 FloatPoint& nextOffset = (i == numGlyphs - 1) ? firstOffsetOfNextRun : o ffsets[i + 1];
963 float glyphAdvanceX = advances[i] + nextOffset.x() - currentOffset.x(); 984 float glyphAdvanceX = advances[i] + nextOffset.x() - currentOffset.x();
964 float glyphAdvanceY = nextOffset.y() - currentOffset.y(); 985 float glyphAdvanceY = nextOffset.y() - currentOffset.y();
965 if (m_run.rtl()) { 986 if (m_run.rtl()) {
966 if (currentCharacterIndex >= m_toIndex) 987 if (currentCharacterIndex >= m_toIndex)
967 m_startOffset.move(glyphAdvanceX, glyphAdvanceY); 988 m_startOffset.move(glyphAdvanceX, glyphAdvanceY);
968 else if (currentCharacterIndex >= m_fromIndex) 989 else if (currentCharacterIndex >= m_fromIndex)
969 glyphBuffer->add(glyphs[i], currentRun->fontData(), createGlyphB ufferAdvance(glyphAdvanceX, glyphAdvanceY)); 990 glyphBuffer->add(glyphs[i], currentRun->fontData(), createGlyphB ufferAdvance(glyphAdvanceX, glyphAdvanceY));
970 } else { 991 } else {
971 if (currentCharacterIndex < m_fromIndex) 992 if (currentCharacterIndex < m_fromIndex)
972 m_startOffset.move(glyphAdvanceX, glyphAdvanceY); 993 m_startOffset.move(glyphAdvanceX, glyphAdvanceY);
973 else if (currentCharacterIndex < m_toIndex) 994 else if (currentCharacterIndex < m_toIndex)
974 glyphBuffer->add(glyphs[i], currentRun->fontData(), createGlyphB ufferAdvance(glyphAdvanceX, glyphAdvanceY)); 995 glyphBuffer->add(glyphs[i], currentRun->fontData(), createGlyphB ufferAdvance(glyphAdvanceX, glyphAdvanceY));
975 } 996 }
976 } 997 }
977 } 998 }
978 999
1000 void HarfBuzzShaper::fillGlyphBufferForTextEmphasis(GlyphBuffer* glyphBuffer, Ha rfBuzzRun* currentRun)
1001 {
1002 // FIXME: Instead of generating a synthetic GlyphBuffer here which is then u sed by the
1003 // drawEmphasisMarks method of FontFastPath, we should roll our own emphasis mark drawing function.
1004
1005 float* advances = currentRun->advances();
1006 unsigned numGlyphs = currentRun->numGlyphs();
1007 uint16_t* glyphToCharacterIndexes = currentRun->glyphToCharacterIndexes();
1008 unsigned graphemesInCluster = 1;
1009 float clusterAdvance = 0;
1010 uint16_t clusterStart;
1011
1012 // A "cluster" in this context means a cluster as it is used by HarfBuzz:
1013 // The minimal group of characters and corresponding glyphs, that cannot be broken
1014 // down further from a text shaping point of view.
1015 // A cluster can contain multiple glyphs and grapheme clusters, with mutuall y
1016 // overlapping boundaries. Below we count grapheme clusters per HarfBuzz clu sters,
1017 // then linearly split the sum of corresponding glyph advances by the number of
1018 // grapheme clusters in order to find positions for emphasis mark drawing.
1019
1020 if (m_run.rtl())
1021 clusterStart = currentRun->startIndex() + currentRun->numCharacters();
1022 else
1023 clusterStart = currentRun->startIndex() + glyphToCharacterIndexes[0];
1024
1025 for (unsigned i = 0; i < numGlyphs; ++i) {
1026 uint16_t currentCharacterIndex = currentRun->startIndex() + glyphToChara cterIndexes[i];
1027 bool isRunEnd = (i + 1 == numGlyphs);
1028 bool isClusterEnd = isRunEnd || (currentRun->startIndex() + glyphToChar acterIndexes[i + 1] != currentCharacterIndex);
1029 clusterAdvance += advances[i];
1030
1031 if (isClusterEnd) {
1032 uint16_t clusterEnd;
1033 if (m_run.rtl())
1034 clusterEnd = currentCharacterIndex;
1035 else
1036 clusterEnd = isRunEnd ? currentRun->startIndex() + currentRun->n umCharacters() : currentRun->startIndex() + glyphToCharacterIndexes[i + 1];
1037
1038 graphemesInCluster = countGraphemesInCluster(m_normalizedBuffer.get( ), m_normalizedBufferLength, clusterStart, clusterEnd);
1039 if (!graphemesInCluster || !clusterAdvance)
1040 continue;
1041
1042 float glyphAdvanceX = clusterAdvance / graphemesInCluster;
1043 for (unsigned j = 0; j < graphemesInCluster; ++j) {
1044 // Do not put emphasis marks on space, separator, and control ch aracters.
1045 GlyphBufferGlyph glyphToAdd = Character::canReceiveTextEmphasis( m_run[currentCharacterIndex]) ? 1 : 0;
1046 glyphBuffer->add(glyphToAdd, currentRun->fontData(), createGlyph BufferAdvance(glyphAdvanceX, 0));
1047 }
1048 clusterStart = clusterEnd;
1049 clusterAdvance = 0;
1050 }
1051 }
1052 }
1053
979 bool HarfBuzzShaper::fillGlyphBuffer(GlyphBuffer* glyphBuffer) 1054 bool HarfBuzzShaper::fillGlyphBuffer(GlyphBuffer* glyphBuffer)
980 { 1055 {
981 unsigned numRuns = m_harfBuzzRuns.size(); 1056 unsigned numRuns = m_harfBuzzRuns.size();
982 if (m_run.rtl()) { 1057 if (m_run.rtl()) {
983 m_startOffset = m_harfBuzzRuns.last()->offsets()[0]; 1058 m_startOffset = m_harfBuzzRuns.last()->offsets()[0];
984 for (int runIndex = numRuns - 1; runIndex >= 0; --runIndex) { 1059 for (int runIndex = numRuns - 1; runIndex >= 0; --runIndex) {
985 HarfBuzzRun* currentRun = m_harfBuzzRuns[runIndex].get(); 1060 HarfBuzzRun* currentRun = m_harfBuzzRuns[runIndex].get();
986 FloatPoint firstOffsetOfNextRun = !runIndex ? FloatPoint() : m_harfB uzzRuns[runIndex - 1]->offsets()[0]; 1061 FloatPoint firstOffsetOfNextRun = !runIndex ? FloatPoint() : m_harfB uzzRuns[runIndex - 1]->offsets()[0];
987 fillGlyphBufferFromHarfBuzzRun(glyphBuffer, currentRun, firstOffsetO fNextRun); 1062 if (m_forTextEmphasis == ForTextEmphasis)
1063 fillGlyphBufferForTextEmphasis(glyphBuffer, currentRun);
1064 else
1065 fillGlyphBufferFromHarfBuzzRun(glyphBuffer, currentRun, firstOff setOfNextRun);
988 } 1066 }
989 } else { 1067 } else {
990 m_startOffset = m_harfBuzzRuns.first()->offsets()[0]; 1068 m_startOffset = m_harfBuzzRuns.first()->offsets()[0];
991 for (unsigned runIndex = 0; runIndex < numRuns; ++runIndex) { 1069 for (unsigned runIndex = 0; runIndex < numRuns; ++runIndex) {
992 HarfBuzzRun* currentRun = m_harfBuzzRuns[runIndex].get(); 1070 HarfBuzzRun* currentRun = m_harfBuzzRuns[runIndex].get();
993 FloatPoint firstOffsetOfNextRun = runIndex == numRuns - 1 ? FloatPoi nt() : m_harfBuzzRuns[runIndex + 1]->offsets()[0]; 1071 FloatPoint firstOffsetOfNextRun = runIndex == numRuns - 1 ? FloatPoi nt() : m_harfBuzzRuns[runIndex + 1]->offsets()[0];
994 fillGlyphBufferFromHarfBuzzRun(glyphBuffer, currentRun, firstOffsetO fNextRun); 1072 if (m_forTextEmphasis == ForTextEmphasis)
1073 fillGlyphBufferForTextEmphasis(glyphBuffer, currentRun);
1074 else
1075 fillGlyphBufferFromHarfBuzzRun(glyphBuffer, currentRun, firstOff setOfNextRun);
995 } 1076 }
996 } 1077 }
997 return glyphBuffer->size(); 1078 return glyphBuffer->size();
998 } 1079 }
999 1080
1000 int HarfBuzzShaper::offsetForPosition(float targetX) 1081 int HarfBuzzShaper::offsetForPosition(float targetX)
1001 { 1082 {
1002 int charactersSoFar = 0; 1083 int charactersSoFar = 0;
1003 float currentX = 0; 1084 float currentX = 0;
1004 1085
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1069 if (!foundToX) 1150 if (!foundToX)
1070 toX = m_run.rtl() ? 0 : m_totalWidth; 1151 toX = m_run.rtl() ? 0 : m_totalWidth;
1071 1152
1072 // Using floorf() and roundf() as the same as mac port. 1153 // Using floorf() and roundf() as the same as mac port.
1073 if (fromX < toX) 1154 if (fromX < toX)
1074 return FloatRect(floorf(point.x() + fromX), point.y(), roundf(toX - from X), height); 1155 return FloatRect(floorf(point.x() + fromX), point.y(), roundf(toX - from X), height);
1075 return FloatRect(floorf(point.x() + toX), point.y(), roundf(fromX - toX), he ight); 1156 return FloatRect(floorf(point.x() + toX), point.y(), roundf(fromX - toX), he ight);
1076 } 1157 }
1077 1158
1078 } // namespace WebCore 1159 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/platform/fonts/harfbuzz/HarfBuzzShaper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698