OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 uint16_t countGraphemesInCluster(const TextRun& run, uint16_t star tIndex, 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 TextRun subRun = run.subRun(startIndex, length); | |
221 | |
222 TextBreakIterator* cursorPosIterator = cursorMovementIterator(subRun.charact ers16(), length); | |
223 | |
224 int cursorPos = cursorPosIterator->current(); | |
225 uint16_t numGraphemes = -1; // Assumption | |
226 while (0 <= cursorPos) { | |
227 cursorPos = cursorPosIterator->next(); | |
228 numGraphemes++; | |
229 } | |
230 return numGraphemes; | |
231 } | |
232 | |
211 inline HarfBuzzShaper::HarfBuzzRun::HarfBuzzRun(const SimpleFontData* fontData, unsigned startIndex, unsigned numCharacters, TextDirection direction, hb_script_ t script) | 233 inline HarfBuzzShaper::HarfBuzzRun::HarfBuzzRun(const SimpleFontData* fontData, unsigned startIndex, unsigned numCharacters, TextDirection direction, hb_script_ t script) |
212 : m_fontData(fontData) | 234 : m_fontData(fontData) |
213 , m_startIndex(startIndex) | 235 , m_startIndex(startIndex) |
214 , m_numCharacters(numCharacters) | 236 , m_numCharacters(numCharacters) |
215 , m_numGlyphs(0) | 237 , m_numGlyphs(0) |
216 , m_direction(direction) | 238 , m_direction(direction) |
217 , m_script(script) | 239 , m_script(script) |
218 , m_width(0) | 240 , m_width(0) |
219 { | 241 { |
220 } | 242 } |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
342 // Don't normalize tabs as they are not treated as spaces for word-end. | 364 // Don't normalize tabs as they are not treated as spaces for word-end. |
343 if (Character::treatAsSpace(character) && character != '\t') | 365 if (Character::treatAsSpace(character) && character != '\t') |
344 character = ' '; | 366 character = ' '; |
345 else if (Character::treatAsZeroWidthSpaceInComplexScript(character)) | 367 else if (Character::treatAsZeroWidthSpaceInComplexScript(character)) |
346 character = zeroWidthSpace; | 368 character = zeroWidthSpace; |
347 U16_APPEND(destination, *destinationLength, length, character, error); | 369 U16_APPEND(destination, *destinationLength, length, character, error); |
348 ASSERT_UNUSED(error, !error); | 370 ASSERT_UNUSED(error, !error); |
349 } | 371 } |
350 } | 372 } |
351 | 373 |
352 HarfBuzzShaper::HarfBuzzShaper(const Font* font, const TextRun& run) | 374 HarfBuzzShaper::HarfBuzzShaper(const Font* font, const TextRun& run, bool forTex tEmphasis) |
353 : m_font(font) | 375 : m_font(font) |
354 , m_normalizedBufferLength(0) | 376 , m_normalizedBufferLength(0) |
355 , m_run(run) | 377 , m_run(run) |
356 , m_wordSpacingAdjustment(font->fontDescription().wordSpacing()) | 378 , m_wordSpacingAdjustment(font->fontDescription().wordSpacing()) |
357 , m_padding(0) | 379 , m_padding(0) |
358 , m_padPerWordBreak(0) | 380 , m_padPerWordBreak(0) |
359 , m_padError(0) | 381 , m_padError(0) |
360 , m_letterSpacing(font->fontDescription().letterSpacing()) | 382 , m_letterSpacing(font->fontDescription().letterSpacing()) |
361 , m_fromIndex(0) | 383 , m_fromIndex(0) |
362 , m_toIndex(m_run.length()) | 384 , m_toIndex(m_run.length()) |
385 , m_forTextEmphasis(forTextEmphasis) | |
363 { | 386 { |
364 m_normalizedBuffer = adoptArrayPtr(new UChar[m_run.length() + 1]); | 387 m_normalizedBuffer = adoptArrayPtr(new UChar[m_run.length() + 1]); |
365 normalizeCharacters(m_run, m_run.length(), m_normalizedBuffer.get(), &m_norm alizedBufferLength); | 388 normalizeCharacters(m_run, m_run.length(), m_normalizedBuffer.get(), &m_norm alizedBufferLength); |
366 setPadding(m_run.expansion()); | 389 setPadding(m_run.expansion()); |
367 setFontFeatures(); | 390 setFontFeatures(); |
368 } | 391 } |
369 | 392 |
370 static void normalizeSpacesAndMirrorChars(const UChar* source, unsigned length, UChar* destination, unsigned* destinationLength, HarfBuzzShaper::NormalizeMode n ormalizeMode) | 393 static void normalizeSpacesAndMirrorChars(const UChar* source, unsigned length, UChar* destination, unsigned* destinationLength, HarfBuzzShaper::NormalizeMode n ormalizeMode) |
371 { | 394 { |
372 unsigned position = 0; | 395 unsigned position = 0; |
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
948 m_totalWidth += currentRun->width(); | 971 m_totalWidth += currentRun->width(); |
949 } | 972 } |
950 | 973 |
951 void HarfBuzzShaper::fillGlyphBufferFromHarfBuzzRun(GlyphBuffer* glyphBuffer, Ha rfBuzzRun* currentRun, FloatPoint& firstOffsetOfNextRun) | 974 void HarfBuzzShaper::fillGlyphBufferFromHarfBuzzRun(GlyphBuffer* glyphBuffer, Ha rfBuzzRun* currentRun, FloatPoint& firstOffsetOfNextRun) |
952 { | 975 { |
953 FloatPoint* offsets = currentRun->offsets(); | 976 FloatPoint* offsets = currentRun->offsets(); |
954 uint16_t* glyphs = currentRun->glyphs(); | 977 uint16_t* glyphs = currentRun->glyphs(); |
955 float* advances = currentRun->advances(); | 978 float* advances = currentRun->advances(); |
956 unsigned numGlyphs = currentRun->numGlyphs(); | 979 unsigned numGlyphs = currentRun->numGlyphs(); |
957 uint16_t* glyphToCharacterIndexes = currentRun->glyphToCharacterIndexes(); | 980 uint16_t* glyphToCharacterIndexes = currentRun->glyphToCharacterIndexes(); |
958 | |
959 for (unsigned i = 0; i < numGlyphs; ++i) { | 981 for (unsigned i = 0; i < numGlyphs; ++i) { |
960 uint16_t currentCharacterIndex = currentRun->startIndex() + glyphToChara cterIndexes[i]; | 982 uint16_t currentCharacterIndex = currentRun->startIndex() + glyphToChara cterIndexes[i]; |
961 FloatPoint& currentOffset = offsets[i]; | 983 FloatPoint& currentOffset = offsets[i]; |
962 FloatPoint& nextOffset = (i == numGlyphs - 1) ? firstOffsetOfNextRun : o ffsets[i + 1]; | 984 FloatPoint& nextOffset = (i == numGlyphs - 1) ? firstOffsetOfNextRun : o ffsets[i + 1]; |
963 float glyphAdvanceX = advances[i] + nextOffset.x() - currentOffset.x(); | 985 float glyphAdvanceX = advances[i] + nextOffset.x() - currentOffset.x(); |
964 float glyphAdvanceY = nextOffset.y() - currentOffset.y(); | 986 float glyphAdvanceY = nextOffset.y() - currentOffset.y(); |
987 | |
965 if (m_run.rtl()) { | 988 if (m_run.rtl()) { |
966 if (currentCharacterIndex >= m_toIndex) | 989 if (currentCharacterIndex >= m_toIndex) |
967 m_startOffset.move(glyphAdvanceX, glyphAdvanceY); | 990 m_startOffset.move(glyphAdvanceX, glyphAdvanceY); |
968 else if (currentCharacterIndex >= m_fromIndex) | 991 else if (currentCharacterIndex >= m_fromIndex) |
969 glyphBuffer->add(glyphs[i], currentRun->fontData(), createGlyphB ufferAdvance(glyphAdvanceX, glyphAdvanceY)); | 992 glyphBuffer->add(glyphs[i], currentRun->fontData(), createGlyphB ufferAdvance(glyphAdvanceX, glyphAdvanceY)); |
970 } else { | 993 } else { |
971 if (currentCharacterIndex < m_fromIndex) | 994 if (currentCharacterIndex < m_fromIndex) |
972 m_startOffset.move(glyphAdvanceX, glyphAdvanceY); | 995 m_startOffset.move(glyphAdvanceX, glyphAdvanceY); |
973 else if (currentCharacterIndex < m_toIndex) | 996 else if (currentCharacterIndex < m_toIndex) |
974 glyphBuffer->add(glyphs[i], currentRun->fontData(), createGlyphB ufferAdvance(glyphAdvanceX, glyphAdvanceY)); | 997 glyphBuffer->add(glyphs[i], currentRun->fontData(), createGlyphB ufferAdvance(glyphAdvanceX, glyphAdvanceY)); |
975 } | 998 } |
976 } | 999 } |
977 } | 1000 } |
978 | 1001 |
1002 void HarfBuzzShaper::fillGlyphBufferForTextEmphasis(GlyphBuffer* glyphBuffer, Ha rfBuzzRun* currentRun) | |
1003 { | |
1004 // FIXME: Instead of generating a synthetic GlyphBuffer here which is then u sed by the | |
1005 // drawEmphasisMarks method of FontFastPath, we should roll our own emphasis mark drawing function. | |
1006 | |
1007 float* advances = currentRun->advances(); | |
1008 unsigned numGlyphs = currentRun->numGlyphs(); | |
1009 uint16_t* glyphToCharacterIndexes = currentRun->glyphToCharacterIndexes(); | |
1010 uint16_t graphemesInCluster = 1; | |
1011 float clusterAdvance = 0; | |
1012 uint16_t clusterStart; | |
1013 | |
1014 // A "cluster" in this context means a cluster as it is used by HarfBuzz: | |
1015 // The minimal group of characters and corresponding glyphs, that cannot be broken | |
1016 // down further from a text shaping point of view. | |
1017 // A cluster can contain multiple glyphs and grapheme clusters, with mutuall y | |
1018 // overlapping boundaries. Below we count grapheme clusters per HarfBuzz clu sters, | |
1019 // then linearly split the sum of corresponding glyph advances by the number of | |
1020 // grapheme clusters in order to find positions for emphasis mark drawing. | |
1021 | |
1022 if (m_run.rtl()) | |
1023 clusterStart = currentRun->startIndex() + currentRun->numCharacters(); | |
1024 else | |
1025 clusterStart = currentRun->startIndex() + glyphToCharacterIndexes[0]; | |
1026 | |
1027 for (unsigned i = 0; i < numGlyphs; ++i) { | |
1028 uint16_t currentCharacterIndex = currentRun->startIndex() + glyphToChara cterIndexes[i]; | |
1029 bool isRunEnd = (i + 1 == numGlyphs); | |
1030 bool isClusterEnd = isRunEnd || (currentRun->startIndex() + glyphToChar acterIndexes[i + 1] != currentCharacterIndex); | |
1031 clusterAdvance += advances[i]; | |
1032 | |
1033 if (isClusterEnd) { | |
1034 uint16_t clusterEnd; | |
1035 if (m_run.rtl()) | |
1036 clusterEnd = currentCharacterIndex; | |
1037 else | |
1038 clusterEnd = isRunEnd ? currentRun->startIndex() + currentRun->n umCharacters() : currentRun->startIndex() + glyphToCharacterIndexes[i + 1]; | |
1039 | |
1040 graphemesInCluster = countGraphemesInCluster(m_run, clusterStart, cl usterEnd); | |
behdad
2014/02/11 18:53:44
Do you handle the case of this returning -1 or 0?
behdad
2014/02/11 20:17:42
Ok, I see why you don't care about that right now.
| |
1041 float glyphAdvanceX = clusterAdvance / graphemesInCluster; | |
behdad
2014/02/11 20:17:42
To handle some of the cases stated above, I sugges
| |
1042 for (unsigned j = 0; j < graphemesInCluster; ++j) { | |
1043 // Do not put emphasis marks on space, separator, and control ch aracters. | |
1044 GlyphBufferGlyph glyphToAdd = Character::canReceiveTextEmphasis( m_run[currentCharacterIndex]) ? 1 : 0; | |
1045 glyphBuffer->add(glyphToAdd, currentRun->fontData(), createGlyph BufferAdvance(glyphAdvanceX, 0)); | |
1046 } | |
1047 clusterStart = clusterEnd; | |
1048 clusterAdvance = 0; | |
1049 } | |
1050 } | |
1051 } | |
1052 | |
979 bool HarfBuzzShaper::fillGlyphBuffer(GlyphBuffer* glyphBuffer) | 1053 bool HarfBuzzShaper::fillGlyphBuffer(GlyphBuffer* glyphBuffer) |
980 { | 1054 { |
981 unsigned numRuns = m_harfBuzzRuns.size(); | 1055 unsigned numRuns = m_harfBuzzRuns.size(); |
982 if (m_run.rtl()) { | 1056 if (m_run.rtl()) { |
983 m_startOffset = m_harfBuzzRuns.last()->offsets()[0]; | 1057 m_startOffset = m_harfBuzzRuns.last()->offsets()[0]; |
984 for (int runIndex = numRuns - 1; runIndex >= 0; --runIndex) { | 1058 for (int runIndex = numRuns - 1; runIndex >= 0; --runIndex) { |
985 HarfBuzzRun* currentRun = m_harfBuzzRuns[runIndex].get(); | 1059 HarfBuzzRun* currentRun = m_harfBuzzRuns[runIndex].get(); |
986 FloatPoint firstOffsetOfNextRun = !runIndex ? FloatPoint() : m_harfB uzzRuns[runIndex - 1]->offsets()[0]; | 1060 FloatPoint firstOffsetOfNextRun = !runIndex ? FloatPoint() : m_harfB uzzRuns[runIndex - 1]->offsets()[0]; |
987 fillGlyphBufferFromHarfBuzzRun(glyphBuffer, currentRun, firstOffsetO fNextRun); | 1061 if (m_forTextEmphasis) |
1062 fillGlyphBufferForTextEmphasis(glyphBuffer, currentRun); | |
1063 else | |
1064 fillGlyphBufferFromHarfBuzzRun(glyphBuffer, currentRun, firstOff setOfNextRun); | |
988 } | 1065 } |
989 } else { | 1066 } else { |
990 m_startOffset = m_harfBuzzRuns.first()->offsets()[0]; | 1067 m_startOffset = m_harfBuzzRuns.first()->offsets()[0]; |
991 for (unsigned runIndex = 0; runIndex < numRuns; ++runIndex) { | 1068 for (unsigned runIndex = 0; runIndex < numRuns; ++runIndex) { |
992 HarfBuzzRun* currentRun = m_harfBuzzRuns[runIndex].get(); | 1069 HarfBuzzRun* currentRun = m_harfBuzzRuns[runIndex].get(); |
993 FloatPoint firstOffsetOfNextRun = runIndex == numRuns - 1 ? FloatPoi nt() : m_harfBuzzRuns[runIndex + 1]->offsets()[0]; | 1070 FloatPoint firstOffsetOfNextRun = runIndex == numRuns - 1 ? FloatPoi nt() : m_harfBuzzRuns[runIndex + 1]->offsets()[0]; |
994 fillGlyphBufferFromHarfBuzzRun(glyphBuffer, currentRun, firstOffsetO fNextRun); | 1071 if (m_forTextEmphasis) |
1072 fillGlyphBufferForTextEmphasis(glyphBuffer, currentRun); | |
1073 else | |
1074 fillGlyphBufferFromHarfBuzzRun(glyphBuffer, currentRun, firstOff setOfNextRun); | |
995 } | 1075 } |
996 } | 1076 } |
997 return glyphBuffer->size(); | 1077 return glyphBuffer->size(); |
998 } | 1078 } |
999 | 1079 |
1000 int HarfBuzzShaper::offsetForPosition(float targetX) | 1080 int HarfBuzzShaper::offsetForPosition(float targetX) |
1001 { | 1081 { |
1002 int charactersSoFar = 0; | 1082 int charactersSoFar = 0; |
1003 float currentX = 0; | 1083 float currentX = 0; |
1004 | 1084 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1069 if (!foundToX) | 1149 if (!foundToX) |
1070 toX = m_run.rtl() ? 0 : m_totalWidth; | 1150 toX = m_run.rtl() ? 0 : m_totalWidth; |
1071 | 1151 |
1072 // Using floorf() and roundf() as the same as mac port. | 1152 // Using floorf() and roundf() as the same as mac port. |
1073 if (fromX < toX) | 1153 if (fromX < toX) |
1074 return FloatRect(floorf(point.x() + fromX), point.y(), roundf(toX - from X), height); | 1154 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); | 1155 return FloatRect(floorf(point.x() + toX), point.y(), roundf(fromX - toX), he ight); |
1076 } | 1156 } |
1077 | 1157 |
1078 } // namespace WebCore | 1158 } // namespace WebCore |
OLD | NEW |