@@ -211,7 +211,7 @@ interface CraftRect {
211211}
212212
213213interface BlockTrack {
214- key : number ;
214+ blockKeys : Set < number > ;
215215 selector : string ;
216216 sourceFile : string ;
217217 /** Frame index → what the block showed there. */
@@ -258,20 +258,47 @@ interface Anchor {
258258}
259259
260260function trackBlocks ( frames : CraftFrame [ ] , readHeight : number ) : BlockTrack [ ] {
261- const tracks = new Map < number , BlockTrack > ( ) ;
261+ const tracks : BlockTrack [ ] = [ ] ;
262+ const byKey = new Map < number , BlockTrack > ( ) ;
262263 frames . forEach ( ( frame , index ) => {
263264 for ( const [ key , block ] of readableBlocks ( frame , readHeight ) ) {
264- const track = tracks . get ( key ) ?? {
265- key,
266- selector : block . selector ,
267- sourceFile : block . sourceFile ,
268- seen : new Map < number , BlockFrame > ( ) ,
269- } ;
265+ let track = byKey . get ( key ) ?? trackContinuedBy ( tracks , index , block ) ;
266+ if ( ! track ) {
267+ track = {
268+ blockKeys : new Set ( ) ,
269+ selector : block . selector ,
270+ sourceFile : block . sourceFile ,
271+ seen : new Map ( ) ,
272+ } ;
273+ tracks . push ( track ) ;
274+ }
275+ track . blockKeys . add ( key ) ;
276+ byKey . set ( key , track ) ;
270277 track . seen . set ( index , { text : block . text , rect : block . rect } ) ;
271- tracks . set ( key , track ) ;
272278 }
273279 } ) ;
274- return [ ...tracks . values ( ) ] ;
280+ return tracks ;
281+ }
282+
283+ function trackContinuedBy (
284+ tracks : BlockTrack [ ] ,
285+ index : number ,
286+ block : BlockFrame ,
287+ ) : BlockTrack | undefined {
288+ return tracks . find ( ( track ) => {
289+ const last = track . seen . get ( index ) ?? track . seen . get ( index - 1 ) ;
290+ return ! ! last && last . text === block . text && sameBox ( last . rect , block . rect ) ;
291+ } ) ;
292+ }
293+
294+ const SAME_BOX_OVERLAP = 0.9 ;
295+
296+ function sameBox ( a : CraftRect , b : CraftRect ) : boolean {
297+ const width = Math . min ( a . right , b . right ) - Math . max ( a . left , b . left ) ;
298+ const height = Math . min ( a . bottom , b . bottom ) - Math . max ( a . top , b . top ) ;
299+ if ( width <= 0 || height <= 0 ) return false ;
300+ const area = ( rect : CraftRect ) => ( rect . right - rect . left ) * ( rect . bottom - rect . top ) ;
301+ return ( width * height ) / Math . max ( area ( a ) , area ( b ) ) >= SAME_BOX_OVERLAP ;
275302}
276303
277304/** Maximal runs of consecutive frame indices. */
@@ -374,8 +401,10 @@ async function refineWindow(
374401 canvas : Canvas ,
375402) : Promise < LineWindow > {
376403 const readHeight = T . readHeightAt1080 * ( Math . min ( canvas . width , canvas . height ) / 1080 ) ;
377- const visibleAt = async ( time : number ) =>
378- readableBlocks ( await probe ( time ) , readHeight ) . has ( candidate . track . key ) ;
404+ const visibleAt = async ( time : number ) => {
405+ const blocks = readableBlocks ( await probe ( time ) , readHeight ) ;
406+ return [ ...candidate . track . blockKeys ] . some ( ( key ) => blocks . has ( key ) ) ;
407+ } ;
379408 const window = { ...candidate . window } ;
380409 for ( let step = 0 ; step < T . refineSteps ; step += 1 ) {
381410 if ( window . startIn > window . startOut ) {
@@ -445,7 +474,7 @@ function lineUnreadFinding(
445474 words,
446475 seconds : round ( held ) ,
447476 floorSeconds : round ( floor ) ,
448- measured : measuredAround ( frames , track . key , candidate . firstIndex , candidate . lastIndex ) ,
477+ measured : measuredAround ( frames , track . blockKeys , candidate . firstIndex , candidate . lastIndex ) ,
449478 } ;
450479}
451480
@@ -454,7 +483,7 @@ const MEASURED_SPAN = 3;
454483/** The samples at both ends of the line's run, as the sampler measured them. */
455484function measuredAround (
456485 frames : CraftFrame [ ] ,
457- key : number ,
486+ keys : Set < number > ,
458487 first : number ,
459488 last : number ,
460489) : NonNullable < CraftFinding [ "measured" ] > {
@@ -466,7 +495,7 @@ function measuredAround(
466495 for ( const index of [ ...indices ] . sort ( ( a , b ) => a - b ) ) {
467496 const frame = frames [ index ] ;
468497 if ( ! frame ) continue ;
469- const parts = frame . parts . filter ( ( part ) => part . block === key && ! part . captions ) ;
498+ const parts = frame . parts . filter ( ( part ) => keys . has ( part . block ) && ! part . captions ) ;
470499 if ( parts . length === 0 ) {
471500 out . push ( { time : round ( frame . time ) , opacity : null , heightPx : null } ) ;
472501 continue ;
0 commit comments