@@ -91,8 +91,13 @@ pub(crate) fn build<E: Encoder>(mut flow: &mut Flow, spans: &[TextSpan<E>], node
9191 match * node {
9292 Node :: Final { ref indices } => {
9393 if indices. len ( ) > 0 {
94- let node_spans = indices. iter ( ) . flat_map ( |& i| spans. get ( i) ) ;
95- let bbox = node_spans. clone ( ) . map ( |s| s. rect ) . reduce ( |a, b| a. union_rect ( b) ) . unwrap ( ) ;
94+ let node_spans = indices. iter ( )
95+ . flat_map ( |& i| spans. get ( i) ) ;
96+ let bbox = node_spans. clone ( )
97+ . map ( |s| s. rect )
98+ . reduce ( |a, b| a. union_rect ( b) )
99+ . unwrap ( ) ;
100+
96101 let class = classify ( node_spans. clone ( ) ) ;
97102 let mut text = String :: new ( ) ;
98103 let words = concat_text ( & mut text, node_spans) ;
@@ -111,25 +116,26 @@ pub(crate) fn build<E: Encoder>(mut flow: &mut Flow, spans: &[TextSpan<E>], node
111116 NodeTag :: Line => {
112117 let mut indices = vec ! [ ] ;
113118 node. indices ( & mut indices) ;
119+
114120 let line_spans = indices. iter ( ) . flat_map ( |& i| spans. get ( i) ) ;
115121 let bbox: RectF = line_spans. clone ( ) . map ( |s| s. rect ) . reduce ( |a, b| a. union_rect ( b) ) . unwrap ( ) . into ( ) ;
116122
117- let mut text = String :: new ( ) ;
118- let words = concat_text ( & mut text, line_spans. clone ( ) ) ;
119123 let class = classify ( line_spans. clone ( ) ) ;
124+ let mut text = String :: new ( ) ;
125+ let words = concat_text ( & mut text, line_spans) ;
120126
121127 let t = match class {
122128 Class :: Header => RunType :: Header ,
123129 _ => RunType :: Paragraph ,
124130 } ;
125-
126131
127132 flow. add_line ( words, t) ;
128133 }
129134 NodeTag :: Paragraph => {
130- assert_eq ! ( x. len( ) , 0 ) ;
135+ assert_eq ! ( x. len( ) , 0 , "For a paragraph x gaps should be empty" ) ;
131136 let mut lines: Vec < ( RectF , usize ) > = vec ! [ ] ;
132137 let mut indices = vec ! [ ] ;
138+
133139 for n in cells {
134140 let start = indices. len ( ) ;
135141 n. indices ( & mut indices) ;
@@ -142,8 +148,10 @@ pub(crate) fn build<E: Encoder>(mut flow: &mut Flow, spans: &[TextSpan<E>], node
142148
143149 let para_spans = indices. iter ( ) . flat_map ( |& i| spans. get ( i) ) ;
144150 let class = classify ( para_spans. clone ( ) ) ;
151+ // the bounding box the paragraph
145152 let bbox = lines. iter ( ) . map ( |t| t. 0 ) . reduce ( |a, b| a. union_rect ( b) ) . unwrap ( ) ;
146153 let line_height = avg ( para_spans. map ( |s| s. rect . height ( ) ) ) . unwrap ( ) ;
154+
147155 // classify the lines by this vertical line
148156 let left_margin = bbox. min_x ( ) + 0.5 * line_height;
149157
@@ -158,9 +166,10 @@ pub(crate) fn build<E: Encoder>(mut flow: &mut Flow, spans: &[TextSpan<E>], node
158166 left += 1 ;
159167 }
160168 }
169+ //typically paragraphs are indented to the right and longer than 2 lines.
170+ //then there will be a higher left count than right count.
161171
162- // typically paragraphs are indented to the right and longer than 2 lines.
163- // then there will be a higher left count than right count.
172+ //TODO: What if a paragraph with two lines starts at the same x? It will result in left = right.
164173 let indent = left > right;
165174
166175 let mut para_start = 0 ;
@@ -180,9 +189,8 @@ pub(crate) fn build<E: Encoder>(mut flow: &mut Flow, spans: &[TextSpan<E>], node
180189 }
181190 } ) ;
182191 para_start = line_start;
183- } else {
184- text. push ( '\n' ) ;
185- }
192+ }
193+ text. push ( '\n' ) ;
186194 }
187195 if end > line_start {
188196 let words = concat_text ( & mut text, indices[ line_start..end] . iter ( ) . flat_map ( |& i| spans. get ( i) ) ) ;
0 commit comments