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 // FIXME: Perhaps turn this into a classic character break iterator? | |
223 TextBreakIterator* cursorPosIterator = cursorMovementIterator(subRun.charact ers16(), length); | |
224 | |
225 int cursorPos = cursorPosIterator->current(); | |
226 uint16_t numGraphemes = -1; // Assumption | |
227 while (0 <= cursorPos) { | |
228 cursorPos = cursorPosIterator->next(); | |
229 numGraphemes++; | |
230 } | |
231 return numGraphemes; | |
232 } | |
233 | |
211 inline HarfBuzzShaper::HarfBuzzRun::HarfBuzzRun(const SimpleFontData* fontData, unsigned startIndex, unsigned numCharacters, TextDirection direction, hb_script_ t script) | 234 inline HarfBuzzShaper::HarfBuzzRun::HarfBuzzRun(const SimpleFontData* fontData, unsigned startIndex, unsigned numCharacters, TextDirection direction, hb_script_ t script) |
212 : m_fontData(fontData) | 235 : m_fontData(fontData) |
213 , m_startIndex(startIndex) | 236 , m_startIndex(startIndex) |
214 , m_numCharacters(numCharacters) | 237 , m_numCharacters(numCharacters) |
215 , m_numGlyphs(0) | 238 , m_numGlyphs(0) |
216 , m_direction(direction) | 239 , m_direction(direction) |
217 , m_script(script) | 240 , m_script(script) |
218 , m_width(0) | 241 , m_width(0) |
219 { | 242 { |
220 } | 243 } |
(...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. | 365 // Don't normalize tabs as they are not treated as spaces for word-end. |
343 if (Character::treatAsSpace(character) && character != '\t') | 366 if (Character::treatAsSpace(character) && character != '\t') |
344 character = ' '; | 367 character = ' '; |
345 else if (Character::treatAsZeroWidthSpaceInComplexScript(character)) | 368 else if (Character::treatAsZeroWidthSpaceInComplexScript(character)) |
346 character = zeroWidthSpace; | 369 character = zeroWidthSpace; |
347 U16_APPEND(destination, *destinationLength, length, character, error); | 370 U16_APPEND(destination, *destinationLength, length, character, error); |
348 ASSERT_UNUSED(error, !error); | 371 ASSERT_UNUSED(error, !error); |
349 } | 372 } |
350 } | 373 } |
351 | 374 |
352 HarfBuzzShaper::HarfBuzzShaper(const Font* font, const TextRun& run) | 375 HarfBuzzShaper::HarfBuzzShaper(const Font* font, const TextRun& run, bool forTex tEmphasis) |
353 : m_font(font) | 376 : m_font(font) |
354 , m_normalizedBufferLength(0) | 377 , m_normalizedBufferLength(0) |
355 , m_run(run) | 378 , m_run(run) |
356 , m_wordSpacingAdjustment(font->wordSpacing()) | 379 , m_wordSpacingAdjustment(font->wordSpacing()) |
357 , m_padding(0) | 380 , m_padding(0) |
358 , m_padPerWordBreak(0) | 381 , m_padPerWordBreak(0) |
359 , m_padError(0) | 382 , m_padError(0) |
360 , m_letterSpacing(font->letterSpacing()) | 383 , m_letterSpacing(font->letterSpacing()) |
361 , m_fromIndex(0) | 384 , m_fromIndex(0) |
362 , m_toIndex(m_run.length()) | 385 , m_toIndex(m_run.length()) |
386 , m_forTextEmphasis(forTextEmphasis) | |
363 { | 387 { |
364 m_normalizedBuffer = adoptArrayPtr(new UChar[m_run.length() + 1]); | 388 m_normalizedBuffer = adoptArrayPtr(new UChar[m_run.length() + 1]); |
365 normalizeCharacters(m_run, m_run.length(), m_normalizedBuffer.get(), &m_norm alizedBufferLength); | 389 normalizeCharacters(m_run, m_run.length(), m_normalizedBuffer.get(), &m_norm alizedBufferLength); |
366 setPadding(m_run.expansion()); | 390 setPadding(m_run.expansion()); |
367 setFontFeatures(); | 391 setFontFeatures(); |
368 } | 392 } |
369 | 393 |
370 static void normalizeSpacesAndMirrorChars(const UChar* source, unsigned length, UChar* destination, unsigned* destinationLength, HarfBuzzShaper::NormalizeMode n ormalizeMode) | 394 static void normalizeSpacesAndMirrorChars(const UChar* source, unsigned length, UChar* destination, unsigned* destinationLength, HarfBuzzShaper::NormalizeMode n ormalizeMode) |
371 { | 395 { |
372 unsigned position = 0; | 396 unsigned position = 0; |
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
909 } | 933 } |
910 | 934 |
911 void HarfBuzzShaper::fillGlyphBufferFromHarfBuzzRun(GlyphBuffer* glyphBuffer, Ha rfBuzzRun* currentRun, FloatPoint& firstOffsetOfNextRun) | 935 void HarfBuzzShaper::fillGlyphBufferFromHarfBuzzRun(GlyphBuffer* glyphBuffer, Ha rfBuzzRun* currentRun, FloatPoint& firstOffsetOfNextRun) |
912 { | 936 { |
913 FloatPoint* offsets = currentRun->offsets(); | 937 FloatPoint* offsets = currentRun->offsets(); |
914 uint16_t* glyphs = currentRun->glyphs(); | 938 uint16_t* glyphs = currentRun->glyphs(); |
915 float* advances = currentRun->advances(); | 939 float* advances = currentRun->advances(); |
916 unsigned numGlyphs = currentRun->numGlyphs(); | 940 unsigned numGlyphs = currentRun->numGlyphs(); |
917 uint16_t* glyphToCharacterIndexes = currentRun->glyphToCharacterIndexes(); | 941 uint16_t* glyphToCharacterIndexes = currentRun->glyphToCharacterIndexes(); |
918 | 942 |
919 for (unsigned i = 0; i < numGlyphs; ++i) { | 943 // FIXME: Instead of generating a synthetic GlyphBuffer here which is then u sed by the |
920 uint16_t currentCharacterIndex = currentRun->startIndex() + glyphToChara cterIndexes[i]; | 944 // drawEmphasisMarks method of FontFastPath, we should roll our own emphasis mark drawing function. |
921 FloatPoint& currentOffset = offsets[i]; | 945 if (m_forTextEmphasis) { |
922 FloatPoint& nextOffset = (i == numGlyphs - 1) ? firstOffsetOfNextRun : o ffsets[i + 1]; | 946 uint16_t graphemesInCluster = 1; |
923 float glyphAdvanceX = advances[i] + nextOffset.x() - currentOffset.x(); | 947 uint16_t clusterStart; |
924 float glyphAdvanceY = nextOffset.y() - currentOffset.y(); | 948 float clusterAdvance = 0; |
925 if (m_run.rtl()) { | 949 if (m_run.rtl()) |
926 if (currentCharacterIndex >= m_toIndex) | 950 clusterStart = currentRun->startIndex() + currentRun->numCharacters( ); |
927 m_startOffset.move(glyphAdvanceX, glyphAdvanceY); | 951 else |
928 else if (currentCharacterIndex >= m_fromIndex) | 952 clusterStart = currentRun->startIndex() + glyphToCharacterIndexes[0] ; |
929 glyphBuffer->add(glyphs[i], currentRun->fontData(), createGlyphB ufferAdvance(glyphAdvanceX, glyphAdvanceY)); | 953 |
930 } else { | 954 for (unsigned i = 0; i < numGlyphs; ++i) { |
leviw_travelin_and_unemployed
2014/02/10 00:10:38
This block of code is becoming more complicated th
| |
931 if (currentCharacterIndex < m_fromIndex) | 955 uint16_t currentCharacterIndex = currentRun->startIndex() + glyphToC haracterIndexes[i]; |
932 m_startOffset.move(glyphAdvanceX, glyphAdvanceY); | 956 bool runEnd = (i + 1 == numGlyphs); |
933 else if (currentCharacterIndex < m_toIndex) | 957 bool isClusterEnd = runEnd || (currentRun->startIndex() + glyphToCh aracterIndexes[i + 1] != currentCharacterIndex); |
934 glyphBuffer->add(glyphs[i], currentRun->fontData(), createGlyphB ufferAdvance(glyphAdvanceX, glyphAdvanceY)); | 958 clusterAdvance += advances[i]; |
959 | |
960 if (isClusterEnd) { | |
961 uint16_t clusterEnd; | |
962 if (m_run.rtl()) | |
963 clusterEnd = currentRun->startIndex() + glyphToCharacterInde xes[i]; | |
964 else | |
965 clusterEnd = runEnd ? currentRun->startIndex() + currentRun- >numCharacters() : currentRun->startIndex() + glyphToCharacterIndexes[i + 1]; | |
966 graphemesInCluster = countGraphemesInCluster(m_run, clusterStart , clusterEnd); | |
967 float glyphAdvanceX = clusterAdvance / graphemesInCluster; | |
968 for (unsigned j = 0; j < graphemesInCluster; ++j) { | |
969 GlyphBufferGlyph glyphToAdd = Character::canReceiveTextEmpha sis(m_run[currentCharacterIndex]) ? 1 : 0; | |
970 glyphBuffer->add(glyphToAdd, currentRun->fontData(), createG lyphBufferAdvance(glyphAdvanceX, 0)); | |
971 } | |
972 clusterStart = clusterEnd; | |
973 clusterAdvance = 0; | |
974 } | |
975 } | |
976 } else { | |
977 for (unsigned i = 0; i < numGlyphs; ++i) { | |
978 uint16_t currentCharacterIndex = currentRun->startIndex() + glyphToC haracterIndexes[i]; | |
979 FloatPoint& currentOffset = offsets[i]; | |
980 FloatPoint& nextOffset = (i == numGlyphs - 1) ? firstOffsetOfNextRun : offsets[i + 1]; | |
981 float glyphAdvanceX = advances[i] + nextOffset.x() - currentOffset.x (); | |
982 float glyphAdvanceY = nextOffset.y() - currentOffset.y(); | |
983 | |
984 if (m_run.rtl()) { | |
985 if (currentCharacterIndex >= m_toIndex) | |
986 m_startOffset.move(glyphAdvanceX, glyphAdvanceY); | |
987 else if (currentCharacterIndex >= m_fromIndex) | |
988 glyphBuffer->add(glyphs[i], currentRun->fontData(), createGl yphBufferAdvance(glyphAdvanceX, glyphAdvanceY)); | |
989 } else { | |
990 if (currentCharacterIndex < m_fromIndex) | |
991 m_startOffset.move(glyphAdvanceX, glyphAdvanceY); | |
992 else if (currentCharacterIndex < m_toIndex) | |
993 glyphBuffer->add(glyphs[i], currentRun->fontData(), createGl yphBufferAdvance(glyphAdvanceX, glyphAdvanceY)); | |
994 } | |
935 } | 995 } |
936 } | 996 } |
937 } | 997 } |
938 | 998 |
939 bool HarfBuzzShaper::fillGlyphBuffer(GlyphBuffer* glyphBuffer) | 999 bool HarfBuzzShaper::fillGlyphBuffer(GlyphBuffer* glyphBuffer) |
940 { | 1000 { |
941 unsigned numRuns = m_harfBuzzRuns.size(); | 1001 unsigned numRuns = m_harfBuzzRuns.size(); |
942 if (m_run.rtl()) { | 1002 if (m_run.rtl()) { |
943 m_startOffset = m_harfBuzzRuns.last()->offsets()[0]; | 1003 m_startOffset = m_harfBuzzRuns.last()->offsets()[0]; |
944 for (int runIndex = numRuns - 1; runIndex >= 0; --runIndex) { | 1004 for (int runIndex = numRuns - 1; runIndex >= 0; --runIndex) { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1029 if (!foundToX) | 1089 if (!foundToX) |
1030 toX = m_run.rtl() ? 0 : m_totalWidth; | 1090 toX = m_run.rtl() ? 0 : m_totalWidth; |
1031 | 1091 |
1032 // Using floorf() and roundf() as the same as mac port. | 1092 // Using floorf() and roundf() as the same as mac port. |
1033 if (fromX < toX) | 1093 if (fromX < toX) |
1034 return FloatRect(floorf(point.x() + fromX), point.y(), roundf(toX - from X), height); | 1094 return FloatRect(floorf(point.x() + fromX), point.y(), roundf(toX - from X), height); |
1035 return FloatRect(floorf(point.x() + toX), point.y(), roundf(fromX - toX), he ight); | 1095 return FloatRect(floorf(point.x() + toX), point.y(), roundf(fromX - toX), he ight); |
1036 } | 1096 } |
1037 | 1097 |
1038 } // namespace WebCore | 1098 } // namespace WebCore |
OLD | NEW |