@@ -167,7 +167,7 @@ export class TaggedString {
167
167
/**
168
168
* Converts a grapheme cluster index to a UTF-16 code unit (JavaScript character index).
169
169
*/
170
- codeUnitIndex ( unicodeIndex : number ) : number {
170
+ toCodeUnitIndex ( unicodeIndex : number ) : number {
171
171
return splitByGraphemeCluster ( this . text ) . slice ( 0 , unicodeIndex ) . join ( '' ) . length ;
172
172
}
173
173
@@ -271,7 +271,7 @@ function shapeText(
271
271
// Bidi doesn't have to be style-aware
272
272
lines = [ ] ;
273
273
// ICU operates on code units.
274
- lineBreaks = lineBreaks . map ( index => logicalInput . codeUnitIndex ( index ) ) ;
274
+ lineBreaks = lineBreaks . map ( index => logicalInput . toCodeUnitIndex ( index ) ) ;
275
275
const untaggedLines =
276
276
processBidirectionalText ( logicalInput . toString ( ) , lineBreaks ) ;
277
277
for ( const line of untaggedLines ) {
@@ -289,9 +289,20 @@ function shapeText(
289
289
// with formatting
290
290
lines = [ ] ;
291
291
// ICU operates on code units.
292
- lineBreaks = lineBreaks . map ( index => logicalInput . codeUnitIndex ( index ) ) ;
292
+ lineBreaks = lineBreaks . map ( index => logicalInput . toCodeUnitIndex ( index ) ) ;
293
+
294
+ // Convert grapheme cluster–based section index to be based on code units.
295
+ let i = 0 ;
296
+ let elapsedCodeUnits = 0 ;
297
+ let sectionIndex = [ ] ;
298
+ for ( const grapheme of splitByGraphemeCluster ( logicalInput . text ) ) {
299
+ sectionIndex . push ( ...Array ( grapheme . length ) . fill ( logicalInput . sectionIndex [ i ] ) ) ;
300
+ i ++ ;
301
+ elapsedCodeUnits += grapheme . length ;
302
+ }
303
+
293
304
const processedLines =
294
- processStyledBidirectionalText ( logicalInput . text , logicalInput . sectionIndex , lineBreaks ) ;
305
+ processStyledBidirectionalText ( logicalInput . text , sectionIndex , lineBreaks ) ;
295
306
for ( const line of processedLines ) {
296
307
const taggedLine = new TaggedString ( ) ;
297
308
taggedLine . text = line [ 0 ] ;
0 commit comments