@@ -6,7 +6,7 @@ import {Box, FormattingBox} from './layout-box.ts';
66import { binarySearchOf } from './util.ts' ;
77
88import type { InlineLevel , BlockLevel } from './layout-flow.ts' ;
9- import type { InlineFragment } from './layout-text.ts' ;
9+ import type { InlineFragment , Run } from './layout-text.ts' ;
1010import type { Color } from './style.ts' ;
1111import type { LoadedFontFace } from './text-font.ts' ;
1212
@@ -70,7 +70,7 @@ function getTextOffsetsForUncollapsedGlyphs(item: ShapedItem) {
7070
7171function drawText (
7272 item : ShapedItem ,
73- colors : [ Color , number ] [ ] ,
73+ run : Run ,
7474 textStart : number ,
7575 textEnd : number ,
7676 b : PaintBackend
@@ -81,8 +81,6 @@ function drawText(
8181 // Sadly this seems to only work in Firefox and only when the font doesn't do
8282 // any normalizination, so I could probably stop trying to support it
8383 // https://github.com/w3c/csswg-drafts/issues/699
84- const colorEnd = item . colorsEnd ( colors ) ;
85- let colorIndex = item . colorsStart ( colors ) ;
8684 let tx = item . x ;
8785 const collapsed = getTextOffsetsForUncollapsedGlyphs ( item ) ;
8886 textStart = Math . max ( textStart , collapsed . textStart ) ;
@@ -95,32 +93,23 @@ function drawText(
9593 tx += item . measure ( textStart , 1 , state ) . advance ;
9694 }
9795
98- while ( colorIndex !== colorEnd ) {
99- const [ color , offset ] = colors [ colorIndex ] ;
100- const colorStart = offset ;
101- const colorEnd = colorIndex + 1 < colors . length ? colors [ colorIndex + 1 ] [ 1 ] : textEnd ;
102- const start = Math . max ( colorStart , textStart ) ;
103- const end = Math . min ( colorEnd , textEnd ) ;
96+ if ( textStart < textEnd ) {
97+ // TODO: should really have isStartColorBoundary, isEndColorBoundary
98+ const isColorBoundary = textStart !== item . offset && textStart === run . start
99+ || textEnd !== item . end ( ) && textEnd === run . end ;
100+ const ax = item . measure ( textEnd , 1 , state ) . advance ;
104101
105- if ( start < end ) {
106- // TODO: should really have isStartColorBoundary, isEndColorBoundary
107- const isColorBoundary = start !== item . offset && start === colorStart
108- || end !== item . end ( ) && end === colorEnd ;
109- const ax = item . measure ( end , 1 , state ) . advance ;
102+ if ( item . attrs . level & 1 ) tx -= ax ;
110103
111- if ( item . attrs . level & 1 ) tx -= ax ;
104+ b . fillColor = run . style . color ;
105+ b . fontSize = style . fontSize ;
106+ b . font = item . face ;
107+ b . direction = item . attrs . level & 1 ? 'rtl' : 'ltr' ;
108+ b . text ( tx , item . y , item , textStart , textEnd , isColorBoundary ) ;
112109
113- b . fillColor = color ;
114- b . fontSize = style . fontSize ;
115- b . font = item . face ;
116- b . direction = item . attrs . level & 1 ? 'rtl' : 'ltr' ;
117- b . text ( tx , item . y , item , start , end , isColorBoundary ) ;
118-
119- if ( ! ( item . attrs . level & 1 ) ) tx += ax ;
120- }
121-
122- colorIndex += 1 ;
110+ if ( ! ( item . attrs . level & 1 ) ) tx += ax ;
123111 }
112+
124113}
125114
126115/**
@@ -321,11 +310,11 @@ function paintInline(
321310 paragraph : Paragraph ,
322311 b : PaintBackend
323312) {
324- const colors = paragraph . getColors ( ) ;
325313 const items = paragraph . items ;
326314 const stack : InlineLevel [ ] = [ inlineRoot ] ;
327315 let lastMark = inlineRoot . start ;
328316 let inlineMark = inlineRoot . start ;
317+ let run : Run | undefined = undefined ;
329318 let mark = inlineRoot . start ;
330319 let itemIndex = 0 ; // common case, adjusted below if necessary
331320 let itemEnd = items . length ; // common case, adjusted below
@@ -341,7 +330,7 @@ function paintInline(
341330 while ( itemIndex < itemEnd || stack . length ) {
342331 // paint lastMark..mark
343332 if ( itemIndex < itemEnd ) {
344- if ( lastMark < mark ) drawText ( items [ itemIndex ] , colors , lastMark , mark , b ) ;
333+ if ( lastMark < mark ) drawText ( items [ itemIndex ] , run ! , lastMark , mark , b ) ;
345334 if ( mark === items [ itemIndex ] . end ( ) ) itemIndex ++ ;
346335 }
347336
@@ -375,6 +364,7 @@ function paintInline(
375364 }
376365 }
377366 } else if ( box . isRun ( ) ) {
367+ run = box ;
378368 inlineMark = box . end ;
379369 }
380370 }
0 commit comments