@@ -91,8 +91,13 @@ pub(crate) fn build<E: Encoder>(mut flow: &mut Flow, spans: &[TextSpan<E>], node
91
91
match * node {
92
92
Node :: Final { ref indices } => {
93
93
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
+
96
101
let class = classify ( node_spans. clone ( ) ) ;
97
102
let mut text = String :: new ( ) ;
98
103
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
111
116
NodeTag :: Line => {
112
117
let mut indices = vec ! [ ] ;
113
118
node. indices ( & mut indices) ;
119
+
114
120
let line_spans = indices. iter ( ) . flat_map ( |& i| spans. get ( i) ) ;
115
121
let bbox: RectF = line_spans. clone ( ) . map ( |s| s. rect ) . reduce ( |a, b| a. union_rect ( b) ) . unwrap ( ) . into ( ) ;
116
122
117
- let mut text = String :: new ( ) ;
118
- let words = concat_text ( & mut text, line_spans. clone ( ) ) ;
119
123
let class = classify ( line_spans. clone ( ) ) ;
124
+ let mut text = String :: new ( ) ;
125
+ let words = concat_text ( & mut text, line_spans) ;
120
126
121
127
let t = match class {
122
128
Class :: Header => RunType :: Header ,
123
129
_ => RunType :: Paragraph ,
124
130
} ;
125
-
126
131
127
132
flow. add_line ( words, t) ;
128
133
}
129
134
NodeTag :: Paragraph => {
130
- assert_eq ! ( x. len( ) , 0 ) ;
135
+ assert_eq ! ( x. len( ) , 0 , "For a paragraph x gaps should be empty" ) ;
131
136
let mut lines: Vec < ( RectF , usize ) > = vec ! [ ] ;
132
137
let mut indices = vec ! [ ] ;
138
+
133
139
for n in cells {
134
140
let start = indices. len ( ) ;
135
141
n. indices ( & mut indices) ;
@@ -142,8 +148,10 @@ pub(crate) fn build<E: Encoder>(mut flow: &mut Flow, spans: &[TextSpan<E>], node
142
148
143
149
let para_spans = indices. iter ( ) . flat_map ( |& i| spans. get ( i) ) ;
144
150
let class = classify ( para_spans. clone ( ) ) ;
151
+ // the bounding box the paragraph
145
152
let bbox = lines. iter ( ) . map ( |t| t. 0 ) . reduce ( |a, b| a. union_rect ( b) ) . unwrap ( ) ;
146
153
let line_height = avg ( para_spans. map ( |s| s. rect . height ( ) ) ) . unwrap ( ) ;
154
+
147
155
// classify the lines by this vertical line
148
156
let left_margin = bbox. min_x ( ) + 0.5 * line_height;
149
157
@@ -158,9 +166,10 @@ pub(crate) fn build<E: Encoder>(mut flow: &mut Flow, spans: &[TextSpan<E>], node
158
166
left += 1 ;
159
167
}
160
168
}
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.
161
171
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.
164
173
let indent = left > right;
165
174
166
175
let mut para_start = 0 ;
@@ -180,9 +189,8 @@ pub(crate) fn build<E: Encoder>(mut flow: &mut Flow, spans: &[TextSpan<E>], node
180
189
}
181
190
} ) ;
182
191
para_start = line_start;
183
- } else {
184
- text. push ( '\n' ) ;
185
- }
192
+ }
193
+ text. push ( '\n' ) ;
186
194
}
187
195
if end > line_start {
188
196
let words = concat_text ( & mut text, indices[ line_start..end] . iter ( ) . flat_map ( |& i| spans. get ( i) ) ) ;
0 commit comments