@@ -117,7 +117,7 @@ func SplitByFontGlyphs(input Input, availableFaces []*font.Face) []Input {
117117// the return value of the [Fontmap.ResolveFace] call.
118118// The 'Face' field of 'input' is ignored: only 'availableFaces' is used to select the face.
119119func SplitByFace (input Input , availableFaces Fontmap ) []Input {
120- return splitByFace (input , availableFaces , nil )
120+ return splitByFace (input , availableFaces , nil , true )
121121}
122122
123123// Segmenter holds a state used to split input
@@ -395,22 +395,40 @@ func (seg *Segmenter) splitByVertOrientation() {
395395// assume [splitByScript] has been called
396396func (seg * Segmenter ) splitByFace (faces Fontmap ) {
397397 withScript , hasScriptSupport := faces .(FontmapScript )
398- for _ , input := range seg .input {
398+ lastRunWithoutFace := - 1
399+ for i , input := range seg .input {
399400 if hasScriptSupport {
400401 withScript .SetScript (input .Script )
401402 }
402- seg .output = splitByFace (input , faces , seg .output )
403+ isLast := i == len (seg .input )- 1
404+ L := len (seg .output )
405+ seg .output = splitByFace (input , faces , seg .output , isLast )
406+ if face := seg .output [L ].Face ; face != nil {
407+ if lastRunWithoutFace != - 1 {
408+ // apply it back
409+ for k := lastRunWithoutFace ; k < L ; k ++ {
410+ seg .output [k ].Face = face
411+ }
412+ // reset the marker
413+ lastRunWithoutFace = - 1
414+ }
415+ } else {
416+ // in this case, only one run has been added by splitByFace
417+ if lastRunWithoutFace == - 1 {
418+ lastRunWithoutFace = L
419+ }
420+ }
403421 }
404422}
405423
406- func splitByFace (input Input , availableFaces Fontmap , buffer []Input ) []Input {
424+ func splitByFace (input Input , availableFaces Fontmap , buffer []Input , isLast bool ) []Input {
407425 currentInput := input
408426 for i := input .RunStart ; i < input .RunEnd ; i ++ {
409427 r := input .Text [i ]
410428 // We can safely ignore characters if we have a face or if there is more text,
411429 // but we must force the choice of a face if we still don't have one and we reach
412430 // the final rune. Otherwise strings like all-whitespace are never assigned a face.
413- if ignoreFaceChange (r ) && (currentInput .Face != nil || i < input .RunEnd - 1 ) {
431+ if ignoreFaceChange (r ) && (currentInput .Face != nil || ! isLast || i < input .RunEnd - 1 ) {
414432 // add the rune to the current input
415433 continue
416434 }
0 commit comments