OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/fonts/shaping/ShapeResultBuffer.h" | 5 #include "platform/fonts/shaping/ShapeResultBuffer.h" |
6 | 6 |
7 #include "platform/fonts/CharacterRange.h" | 7 #include "platform/fonts/CharacterRange.h" |
8 #include "platform/fonts/GlyphBuffer.h" | 8 #include "platform/fonts/GlyphBuffer.h" |
9 #include "platform/fonts/SimpleFontData.h" | 9 #include "platform/fonts/SimpleFontData.h" |
10 #include "platform/fonts/shaping/ShapeResultInlineHeaders.h" | 10 #include "platform/fonts/shaping/ShapeResultInlineHeaders.h" |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 unsigned to, | 99 unsigned to, |
100 unsigned runOffset) { | 100 unsigned runOffset) { |
101 if (!run) | 101 if (!run) |
102 return 0; | 102 return 0; |
103 float advanceSoFar = initialAdvance; | 103 float advanceSoFar = initialAdvance; |
104 const unsigned numGlyphs = run->m_glyphData.size(); | 104 const unsigned numGlyphs = run->m_glyphData.size(); |
105 for (unsigned i = 0; i < numGlyphs; ++i) { | 105 for (unsigned i = 0; i < numGlyphs; ++i) { |
106 const HarfBuzzRunGlyphData& glyphData = run->m_glyphData[i]; | 106 const HarfBuzzRunGlyphData& glyphData = run->m_glyphData[i]; |
107 uint16_t currentCharacterIndex = | 107 uint16_t currentCharacterIndex = |
108 run->m_startIndex + glyphData.characterIndex + runOffset; | 108 run->m_startIndex + glyphData.characterIndex + runOffset; |
109 if ((direction == TextDirection::Rtl && currentCharacterIndex >= to) || | 109 if ((direction == TextDirection::kRtl && currentCharacterIndex >= to) || |
110 (direction == TextDirection::Ltr && currentCharacterIndex < from)) { | 110 (direction == TextDirection::kLtr && currentCharacterIndex < from)) { |
111 advanceSoFar += glyphData.advance; | 111 advanceSoFar += glyphData.advance; |
112 } else if ((direction == TextDirection::Rtl && | 112 } else if ((direction == TextDirection::kRtl && |
113 currentCharacterIndex >= from) || | 113 currentCharacterIndex >= from) || |
114 (direction == TextDirection::Ltr && | 114 (direction == TextDirection::kLtr && |
115 currentCharacterIndex < to)) { | 115 currentCharacterIndex < to)) { |
116 addGlyphToBuffer(glyphBuffer, advanceSoFar, run->m_direction, | 116 addGlyphToBuffer(glyphBuffer, advanceSoFar, run->m_direction, |
117 run->m_fontData.get(), glyphData, textRun, | 117 run->m_fontData.get(), glyphData, textRun, |
118 currentCharacterIndex); | 118 currentCharacterIndex); |
119 advanceSoFar += glyphData.advance; | 119 advanceSoFar += glyphData.advance; |
120 } | 120 } |
121 } | 121 } |
122 return advanceSoFar - initialAdvance; | 122 return advanceSoFar - initialAdvance; |
123 } | 123 } |
124 | 124 |
(...skipping 18 matching lines...) Expand all Loading... |
143 TextDirection direction = textRun.direction(); | 143 TextDirection direction = textRun.direction(); |
144 | 144 |
145 // A "cluster" in this context means a cluster as it is used by HarfBuzz: | 145 // A "cluster" in this context means a cluster as it is used by HarfBuzz: |
146 // The minimal group of characters and corresponding glyphs, that cannot be | 146 // The minimal group of characters and corresponding glyphs, that cannot be |
147 // broken down further from a text shaping point of view. A cluster can | 147 // broken down further from a text shaping point of view. A cluster can |
148 // contain multiple glyphs and grapheme clusters, with mutually overlapping | 148 // contain multiple glyphs and grapheme clusters, with mutually overlapping |
149 // boundaries. Below we count grapheme clusters per HarfBuzz clusters, then | 149 // boundaries. Below we count grapheme clusters per HarfBuzz clusters, then |
150 // linearly split the sum of corresponding glyph advances by the number of | 150 // linearly split the sum of corresponding glyph advances by the number of |
151 // grapheme clusters in order to find positions for emphasis mark drawing. | 151 // grapheme clusters in order to find positions for emphasis mark drawing. |
152 uint16_t clusterStart = static_cast<uint16_t>( | 152 uint16_t clusterStart = static_cast<uint16_t>( |
153 direction == TextDirection::Rtl | 153 direction == TextDirection::kRtl |
154 ? run->m_startIndex + run->m_numCharacters + runOffset | 154 ? run->m_startIndex + run->m_numCharacters + runOffset |
155 : run->glyphToCharacterIndex(0) + runOffset); | 155 : run->glyphToCharacterIndex(0) + runOffset); |
156 | 156 |
157 float advanceSoFar = initialAdvance; | 157 float advanceSoFar = initialAdvance; |
158 const unsigned numGlyphs = run->m_glyphData.size(); | 158 const unsigned numGlyphs = run->m_glyphData.size(); |
159 for (unsigned i = 0; i < numGlyphs; ++i) { | 159 for (unsigned i = 0; i < numGlyphs; ++i) { |
160 const HarfBuzzRunGlyphData& glyphData = run->m_glyphData[i]; | 160 const HarfBuzzRunGlyphData& glyphData = run->m_glyphData[i]; |
161 uint16_t currentCharacterIndex = | 161 uint16_t currentCharacterIndex = |
162 run->m_startIndex + glyphData.characterIndex + runOffset; | 162 run->m_startIndex + glyphData.characterIndex + runOffset; |
163 bool isRunEnd = (i + 1 == numGlyphs); | 163 bool isRunEnd = (i + 1 == numGlyphs); |
164 bool isClusterEnd = | 164 bool isClusterEnd = |
165 isRunEnd || (run->glyphToCharacterIndex(i + 1) + runOffset != | 165 isRunEnd || (run->glyphToCharacterIndex(i + 1) + runOffset != |
166 currentCharacterIndex); | 166 currentCharacterIndex); |
167 | 167 |
168 if ((direction == TextDirection::Rtl && currentCharacterIndex >= to) || | 168 if ((direction == TextDirection::kRtl && currentCharacterIndex >= to) || |
169 (direction != TextDirection::Rtl && currentCharacterIndex < from)) { | 169 (direction != TextDirection::kRtl && currentCharacterIndex < from)) { |
170 advanceSoFar += glyphData.advance; | 170 advanceSoFar += glyphData.advance; |
171 direction == TextDirection::Rtl ? --clusterStart : ++clusterStart; | 171 direction == TextDirection::kRtl ? --clusterStart : ++clusterStart; |
172 continue; | 172 continue; |
173 } | 173 } |
174 | 174 |
175 clusterAdvance += glyphData.advance; | 175 clusterAdvance += glyphData.advance; |
176 | 176 |
177 if (textRun.is8Bit()) { | 177 if (textRun.is8Bit()) { |
178 float glyphAdvanceX = glyphData.advance; | 178 float glyphAdvanceX = glyphData.advance; |
179 if (Character::canReceiveTextEmphasis(textRun[currentCharacterIndex])) { | 179 if (Character::canReceiveTextEmphasis(textRun[currentCharacterIndex])) { |
180 addEmphasisMark(glyphBuffer, emphasisData, glyphCenter, | 180 addEmphasisMark(glyphBuffer, emphasisData, glyphCenter, |
181 advanceSoFar + glyphAdvanceX / 2); | 181 advanceSoFar + glyphAdvanceX / 2); |
182 } | 182 } |
183 advanceSoFar += glyphAdvanceX; | 183 advanceSoFar += glyphAdvanceX; |
184 } else if (isClusterEnd) { | 184 } else if (isClusterEnd) { |
185 uint16_t clusterEnd; | 185 uint16_t clusterEnd; |
186 if (direction == TextDirection::Rtl) | 186 if (direction == TextDirection::kRtl) |
187 clusterEnd = currentCharacterIndex; | 187 clusterEnd = currentCharacterIndex; |
188 else | 188 else |
189 clusterEnd = static_cast<uint16_t>( | 189 clusterEnd = static_cast<uint16_t>( |
190 isRunEnd ? run->m_startIndex + run->m_numCharacters + runOffset | 190 isRunEnd ? run->m_startIndex + run->m_numCharacters + runOffset |
191 : run->glyphToCharacterIndex(i + 1) + runOffset); | 191 : run->glyphToCharacterIndex(i + 1) + runOffset); |
192 | 192 |
193 graphemesInCluster = countGraphemesInCluster(textRun.characters16(), | 193 graphemesInCluster = countGraphemesInCluster(textRun.characters16(), |
194 textRun.charactersLength(), | 194 textRun.charactersLength(), |
195 clusterStart, clusterEnd); | 195 clusterStart, clusterEnd); |
196 if (!graphemesInCluster || !clusterAdvance) | 196 if (!graphemesInCluster || !clusterAdvance) |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 return fillFastHorizontalGlyphBuffer(glyphBuffer, textRun); | 260 return fillFastHorizontalGlyphBuffer(glyphBuffer, textRun); |
261 | 261 |
262 float advance = 0; | 262 float advance = 0; |
263 | 263 |
264 if (textRun.rtl()) { | 264 if (textRun.rtl()) { |
265 unsigned wordOffset = textRun.length(); | 265 unsigned wordOffset = textRun.length(); |
266 for (unsigned j = 0; j < m_results.size(); j++) { | 266 for (unsigned j = 0; j < m_results.size(); j++) { |
267 unsigned resolvedIndex = m_results.size() - 1 - j; | 267 unsigned resolvedIndex = m_results.size() - 1 - j; |
268 const RefPtr<const ShapeResult>& wordResult = m_results[resolvedIndex]; | 268 const RefPtr<const ShapeResult>& wordResult = m_results[resolvedIndex]; |
269 for (unsigned i = 0; i < wordResult->m_runs.size(); i++) { | 269 for (unsigned i = 0; i < wordResult->m_runs.size(); i++) { |
270 advance += fillGlyphBufferForRun<TextDirection::Rtl>( | 270 advance += fillGlyphBufferForRun<TextDirection::kRtl>( |
271 glyphBuffer, wordResult->m_runs[i].get(), textRun, advance, from, | 271 glyphBuffer, wordResult->m_runs[i].get(), textRun, advance, from, |
272 to, wordOffset - wordResult->numCharacters()); | 272 to, wordOffset - wordResult->numCharacters()); |
273 } | 273 } |
274 wordOffset -= wordResult->numCharacters(); | 274 wordOffset -= wordResult->numCharacters(); |
275 } | 275 } |
276 } else { | 276 } else { |
277 unsigned wordOffset = 0; | 277 unsigned wordOffset = 0; |
278 for (unsigned j = 0; j < m_results.size(); j++) { | 278 for (unsigned j = 0; j < m_results.size(); j++) { |
279 const RefPtr<const ShapeResult>& wordResult = m_results[j]; | 279 const RefPtr<const ShapeResult>& wordResult = m_results[j]; |
280 for (unsigned i = 0; i < wordResult->m_runs.size(); i++) { | 280 for (unsigned i = 0; i < wordResult->m_runs.size(); i++) { |
281 advance += fillGlyphBufferForRun<TextDirection::Ltr>( | 281 advance += fillGlyphBufferForRun<TextDirection::kLtr>( |
282 glyphBuffer, wordResult->m_runs[i].get(), textRun, advance, from, | 282 glyphBuffer, wordResult->m_runs[i].get(), textRun, advance, from, |
283 to, wordOffset); | 283 to, wordOffset); |
284 } | 284 } |
285 wordOffset += wordResult->numCharacters(); | 285 wordOffset += wordResult->numCharacters(); |
286 } | 286 } |
287 } | 287 } |
288 | 288 |
289 return advance; | 289 return advance; |
290 } | 290 } |
291 | 291 |
(...skipping 25 matching lines...) Expand all Loading... |
317 CharacterRange ShapeResultBuffer::getCharacterRange(TextDirection direction, | 317 CharacterRange ShapeResultBuffer::getCharacterRange(TextDirection direction, |
318 float totalWidth, | 318 float totalWidth, |
319 unsigned absoluteFrom, | 319 unsigned absoluteFrom, |
320 unsigned absoluteTo) const { | 320 unsigned absoluteTo) const { |
321 float currentX = 0; | 321 float currentX = 0; |
322 float fromX = 0; | 322 float fromX = 0; |
323 float toX = 0; | 323 float toX = 0; |
324 bool foundFromX = false; | 324 bool foundFromX = false; |
325 bool foundToX = false; | 325 bool foundToX = false; |
326 | 326 |
327 if (direction == TextDirection::Rtl) | 327 if (direction == TextDirection::kRtl) |
328 currentX = totalWidth; | 328 currentX = totalWidth; |
329 | 329 |
330 // The absoluteFrom and absoluteTo arguments represent the start/end offset | 330 // The absoluteFrom and absoluteTo arguments represent the start/end offset |
331 // for the entire run, from/to are continuously updated to be relative to | 331 // for the entire run, from/to are continuously updated to be relative to |
332 // the current word (ShapeResult instance). | 332 // the current word (ShapeResult instance). |
333 int from = absoluteFrom; | 333 int from = absoluteFrom; |
334 int to = absoluteTo; | 334 int to = absoluteTo; |
335 | 335 |
336 unsigned totalNumCharacters = 0; | 336 unsigned totalNumCharacters = 0; |
337 for (unsigned j = 0; j < m_results.size(); j++) { | 337 for (unsigned j = 0; j < m_results.size(); j++) { |
338 const RefPtr<const ShapeResult> result = m_results[j]; | 338 const RefPtr<const ShapeResult> result = m_results[j]; |
339 if (direction == TextDirection::Rtl) { | 339 if (direction == TextDirection::kRtl) { |
340 // Convert logical offsets to visual offsets, because results are in | 340 // Convert logical offsets to visual offsets, because results are in |
341 // logical order while runs are in visual order. | 341 // logical order while runs are in visual order. |
342 if (!foundFromX && from >= 0 && | 342 if (!foundFromX && from >= 0 && |
343 static_cast<unsigned>(from) < result->numCharacters()) | 343 static_cast<unsigned>(from) < result->numCharacters()) |
344 from = result->numCharacters() - from - 1; | 344 from = result->numCharacters() - from - 1; |
345 if (!foundToX && to >= 0 && | 345 if (!foundToX && to >= 0 && |
346 static_cast<unsigned>(to) < result->numCharacters()) | 346 static_cast<unsigned>(to) < result->numCharacters()) |
347 to = result->numCharacters() - to - 1; | 347 to = result->numCharacters() - to - 1; |
348 currentX -= result->width(); | 348 currentX -= result->width(); |
349 } | 349 } |
350 for (unsigned i = 0; i < result->m_runs.size(); i++) { | 350 for (unsigned i = 0; i < result->m_runs.size(); i++) { |
351 if (!result->m_runs[i]) | 351 if (!result->m_runs[i]) |
352 continue; | 352 continue; |
353 DCHECK_EQ(direction == TextDirection::Rtl, result->m_runs[i]->rtl()); | 353 DCHECK_EQ(direction == TextDirection::kRtl, result->m_runs[i]->rtl()); |
354 int numCharacters = result->m_runs[i]->m_numCharacters; | 354 int numCharacters = result->m_runs[i]->m_numCharacters; |
355 if (!foundFromX && from >= 0 && from < numCharacters) { | 355 if (!foundFromX && from >= 0 && from < numCharacters) { |
356 fromX = | 356 fromX = |
357 result->m_runs[i]->xPositionForVisualOffset(from, AdjustToStart) + | 357 result->m_runs[i]->xPositionForVisualOffset(from, AdjustToStart) + |
358 currentX; | 358 currentX; |
359 foundFromX = true; | 359 foundFromX = true; |
360 } else { | 360 } else { |
361 from -= numCharacters; | 361 from -= numCharacters; |
362 } | 362 } |
363 | 363 |
364 if (!foundToX && to >= 0 && to < numCharacters) { | 364 if (!foundToX && to >= 0 && to < numCharacters) { |
365 toX = result->m_runs[i]->xPositionForVisualOffset(to, AdjustToEnd) + | 365 toX = result->m_runs[i]->xPositionForVisualOffset(to, AdjustToEnd) + |
366 currentX; | 366 currentX; |
367 foundToX = true; | 367 foundToX = true; |
368 } else { | 368 } else { |
369 to -= numCharacters; | 369 to -= numCharacters; |
370 } | 370 } |
371 | 371 |
372 if (foundFromX && foundToX) | 372 if (foundFromX && foundToX) |
373 break; | 373 break; |
374 currentX += result->m_runs[i]->m_width; | 374 currentX += result->m_runs[i]->m_width; |
375 } | 375 } |
376 if (direction == TextDirection::Rtl) | 376 if (direction == TextDirection::kRtl) |
377 currentX -= result->width(); | 377 currentX -= result->width(); |
378 totalNumCharacters += result->numCharacters(); | 378 totalNumCharacters += result->numCharacters(); |
379 } | 379 } |
380 | 380 |
381 // The position in question might be just after the text. | 381 // The position in question might be just after the text. |
382 if (!foundFromX && absoluteFrom == totalNumCharacters) { | 382 if (!foundFromX && absoluteFrom == totalNumCharacters) { |
383 fromX = direction == TextDirection::Rtl ? 0 : totalWidth; | 383 fromX = direction == TextDirection::kRtl ? 0 : totalWidth; |
384 foundFromX = true; | 384 foundFromX = true; |
385 } | 385 } |
386 if (!foundToX && absoluteTo == totalNumCharacters) { | 386 if (!foundToX && absoluteTo == totalNumCharacters) { |
387 toX = direction == TextDirection::Rtl ? 0 : totalWidth; | 387 toX = direction == TextDirection::kRtl ? 0 : totalWidth; |
388 foundToX = true; | 388 foundToX = true; |
389 } | 389 } |
390 if (!foundFromX) | 390 if (!foundFromX) |
391 fromX = 0; | 391 fromX = 0; |
392 if (!foundToX) | 392 if (!foundToX) |
393 toX = direction == TextDirection::Rtl ? 0 : totalWidth; | 393 toX = direction == TextDirection::kRtl ? 0 : totalWidth; |
394 | 394 |
395 // None of our runs is part of the selection, possibly invalid arguments. | 395 // None of our runs is part of the selection, possibly invalid arguments. |
396 if (!foundToX && !foundFromX) | 396 if (!foundToX && !foundFromX) |
397 fromX = toX = 0; | 397 fromX = toX = 0; |
398 if (fromX < toX) | 398 if (fromX < toX) |
399 return CharacterRange(fromX, toX); | 399 return CharacterRange(fromX, toX); |
400 return CharacterRange(toX, fromX); | 400 return CharacterRange(toX, fromX); |
401 } | 401 } |
402 | 402 |
403 void ShapeResultBuffer::addRunInfoRanges(const ShapeResult::RunInfo& runInfo, | 403 void ShapeResultBuffer::addRunInfoRanges(const ShapeResult::RunInfo& runInfo, |
(...skipping 14 matching lines...) Expand all Loading... |
418 ranges.push_back(CharacterRange(end, start)); | 418 ranges.push_back(CharacterRange(end, start)); |
419 else | 419 else |
420 ranges.push_back(CharacterRange(start, end)); | 420 ranges.push_back(CharacterRange(start, end)); |
421 } | 421 } |
422 } | 422 } |
423 | 423 |
424 Vector<CharacterRange> ShapeResultBuffer::individualCharacterRanges( | 424 Vector<CharacterRange> ShapeResultBuffer::individualCharacterRanges( |
425 TextDirection direction, | 425 TextDirection direction, |
426 float totalWidth) const { | 426 float totalWidth) const { |
427 Vector<CharacterRange> ranges; | 427 Vector<CharacterRange> ranges; |
428 float currentX = direction == TextDirection::Rtl ? totalWidth : 0; | 428 float currentX = direction == TextDirection::kRtl ? totalWidth : 0; |
429 for (const RefPtr<const ShapeResult> result : m_results) { | 429 for (const RefPtr<const ShapeResult> result : m_results) { |
430 if (direction == TextDirection::Rtl) | 430 if (direction == TextDirection::kRtl) |
431 currentX -= result->width(); | 431 currentX -= result->width(); |
432 unsigned runCount = result->m_runs.size(); | 432 unsigned runCount = result->m_runs.size(); |
433 for (unsigned index = 0; index < runCount; index++) { | 433 for (unsigned index = 0; index < runCount; index++) { |
434 unsigned runIndex = | 434 unsigned runIndex = |
435 direction == TextDirection::Rtl ? runCount - 1 - index : index; | 435 direction == TextDirection::kRtl ? runCount - 1 - index : index; |
436 addRunInfoRanges(*result->m_runs[runIndex], currentX, ranges); | 436 addRunInfoRanges(*result->m_runs[runIndex], currentX, ranges); |
437 currentX += result->m_runs[runIndex]->m_width; | 437 currentX += result->m_runs[runIndex]->m_width; |
438 } | 438 } |
439 if (direction == TextDirection::Rtl) | 439 if (direction == TextDirection::kRtl) |
440 currentX -= result->width(); | 440 currentX -= result->width(); |
441 } | 441 } |
442 return ranges; | 442 return ranges; |
443 } | 443 } |
444 | 444 |
445 int ShapeResultBuffer::offsetForPosition(const TextRun& run, | 445 int ShapeResultBuffer::offsetForPosition(const TextRun& run, |
446 float targetX, | 446 float targetX, |
447 bool includePartialGlyphs) const { | 447 bool includePartialGlyphs) const { |
448 unsigned totalOffset; | 448 unsigned totalOffset; |
449 if (run.rtl()) { | 449 if (run.rtl()) { |
(...skipping 21 matching lines...) Expand all Loading... |
471 totalOffset += offsetForWord; | 471 totalOffset += offsetForWord; |
472 if (targetX >= 0 && targetX <= wordResult->width()) | 472 if (targetX >= 0 && targetX <= wordResult->width()) |
473 return totalOffset; | 473 return totalOffset; |
474 targetX -= wordResult->width(); | 474 targetX -= wordResult->width(); |
475 } | 475 } |
476 } | 476 } |
477 return totalOffset; | 477 return totalOffset; |
478 } | 478 } |
479 | 479 |
480 } // namespace blink | 480 } // namespace blink |
OLD | NEW |