@@ -813,6 +813,7 @@ impl GalleyCache {
813813 ) -> Galley {
814814 profiling:: function_scope!( ) ;
815815
816+ let mut current_section = 0 ;
816817 let mut start = 0 ;
817818 let mut max_rows_remaining = job. wrap . max_rows ;
818819 let mut galleys = Vec :: new ( ) ;
@@ -841,36 +842,49 @@ impl GalleyCache {
841842 round_output_to_gui : job. round_output_to_gui ,
842843 } ;
843844
844- // Keep all (previous) sections (so the correct `section_index` is outputted),
845- // but shift their byte ranges:
846- paragraph_job. sections = job
847- . sections
848- . iter ( )
849- . cloned ( )
850- . filter_map ( |section| {
851- let LayoutSection {
852- leading_space,
853- byte_range,
854- format,
855- } = section;
856- if end <= byte_range. start {
857- None
858- } else {
859- let byte_range = byte_range. start . saturating_sub ( start)
860- ..byte_range. end . saturating_sub ( start) . min ( end - start) ;
861- Some ( LayoutSection {
862- leading_space : if byte_range. end == 0 {
863- 0.0 // this section is behind us (unused in this paragraph)
864- } else {
865- leading_space
866- } ,
867- byte_range,
868- format,
869- } )
870- }
871- } )
872- . collect ( ) ;
845+ // Add overlapping sections:
846+ for section in & job. sections [ current_section..job. sections . len ( ) ] {
847+ let LayoutSection {
848+ leading_space,
849+ byte_range : section_range,
850+ format,
851+ } = section;
852+
853+ // dbg!(section_index, section_range);
854+
855+ // `start` and `end` are the byte range of the current paragraph.
856+ // How does the current section overlap with the paragraph range?
857+
858+ if section_range. end <= start {
859+ // The section is behind us
860+ current_section += 1 ;
861+ } else if end <= section_range. start {
862+ break ; // Haven't reached this one yet.
863+ } else {
864+ // Section range overlaps with paragraph range
865+ debug_assert ! (
866+ section_range. start < section_range. end,
867+ "Bad byte_range: {section_range:?}"
868+ ) ;
869+ let new_range = section_range. start . saturating_sub ( start)
870+ ..( section_range. end . at_most ( end) ) . saturating_sub ( start) ;
871+ debug_assert ! (
872+ new_range. start <= new_range. end,
873+ "Bad new section range: {new_range:?}"
874+ ) ;
875+ paragraph_job. sections . push ( LayoutSection {
876+ leading_space : if start <= new_range. start {
877+ * leading_space
878+ } else {
879+ 0.0
880+ } ,
881+ byte_range : new_range,
882+ format : format. clone ( ) ,
883+ } ) ;
884+ }
885+ }
873886
887+ // TODO(emilk): we could lay out each paragraph in parallel to get a nice speedup on multicore machines.
874888 let galley = self . layout ( fonts, paragraph_job, false ) ;
875889
876890 // This will prevent us from invalidating cache entries unnecessarily:
@@ -891,6 +905,7 @@ impl GalleyCache {
891905 start = end;
892906 }
893907
908+ // TODO: adjust section_index in the concatted galley
894909 Galley :: concat ( job, & galleys, fonts. pixels_per_point )
895910 }
896911
0 commit comments