1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
use crate::{
flex::{DrawLayout, MeasureLayout},
utils::{max_optional_size, mm_to_pt, u32_to_color_and_alpha},
*,
};
/// Currently almost a copy of Row, with the difference that there is no self sized and there's
/// lines instead of gaps. The plan is to eventually replace this with a custom element instead of
/// a gap in Row.
pub struct TableRow<F: Fn(&mut RowContent)> {
pub line_style: LineStyle,
pub expand: bool,
pub content: F,
}
impl<F: Fn(&mut RowContent)> Element for TableRow<F> {
fn first_location_usage(&self, ctx: FirstLocationUsageCtx) -> FirstLocationUsage {
FirstLocationUsage::WillUse
}
fn measure(&self, mut ctx: MeasureCtx) -> ElementSize {
let mut measure_layout = MeasureLayout::new(ctx.width.max, self.line_style.thickness);
let mut max_height = None;
(self.content)(&mut RowContent {
text_pieces_cache: ctx.text_pieces_cache,
width: ctx.width,
first_height: ctx.first_height,
pass: Pass::MeasureNonExpanded {
layout: &mut measure_layout,
max_height: Some(&mut max_height),
breakable: ctx.breakable.as_mut(),
},
});
let mut width = measure_layout.no_expand_width();
let draw_layout = measure_layout.build();
(self.content)(&mut RowContent {
text_pieces_cache: ctx.text_pieces_cache,
width: ctx.width,
first_height: ctx.first_height,
pass: Pass::MeasureExpanded {
layout: &draw_layout,
max_height: &mut max_height,
width: if ctx.width.expand {
None
} else {
Some(&mut width)
},
gap: self.line_style.thickness,
breakable: ctx.breakable.as_mut(),
},
});
ElementSize {
width: if ctx.width.expand {
Some(ctx.width.max)
} else {
width
},
height: max_height,
}
}
fn draw(&self, mut ctx: DrawCtx) -> ElementSize {
let mut measure_layout = MeasureLayout::new(ctx.width.max, self.line_style.thickness);
let mut max_height = None;
let mut break_count = 0;
let mut extra_location_min_height = None;
(self.content)(&mut RowContent {
text_pieces_cache: ctx.text_pieces_cache,
width: ctx.width,
first_height: ctx.first_height,
pass: Pass::MeasureNonExpanded {
layout: &mut measure_layout,
max_height: if self.expand {
Some(&mut max_height)
} else {
None
},
breakable: ctx
.breakable
.as_ref()
.map(|b| BreakableMeasure {
full_height: b.full_height,
// in the non-expand case these will just be ignored
break_count: &mut break_count,
extra_location_min_height: &mut extra_location_min_height,
})
.as_mut(),
},
});
let draw_layout = measure_layout.build();
// If we want to expand all of the children to the same size we need an additional pass here
// to figure out the maximum height & break count of all of the children. This is part of
// the reason why expanding isn't just what Row always does.
if self.expand {
(self.content)(&mut RowContent {
text_pieces_cache: ctx.text_pieces_cache,
width: ctx.width,
first_height: ctx.first_height,
pass: Pass::MeasureExpanded {
layout: &draw_layout,
max_height: &mut max_height,
width: None, // We'll get that from draw. No point in getting it twice.
gap: self.line_style.thickness,
breakable: ctx
.breakable
.as_ref()
.map(|b| BreakableMeasure {
full_height: b.full_height,
// in the non-expand case these will just be ignored
break_count: &mut break_count,
extra_location_min_height: &mut extra_location_min_height,
})
.as_mut(),
},
});
if let Some(ref mut b) = ctx.breakable {
match break_count.cmp(&b.preferred_height_break_count) {
std::cmp::Ordering::Less => (),
std::cmp::Ordering::Equal => {
ctx.preferred_height = max_optional_size(ctx.preferred_height, max_height);
}
std::cmp::Ordering::Greater => {
b.preferred_height_break_count = break_count;
ctx.preferred_height = max_height;
}
}
} else {
ctx.preferred_height = max_optional_size(ctx.preferred_height, max_height);
}
}
let mut width = None;
let mut break_count = 0;
(self.content)(&mut RowContent {
text_pieces_cache: ctx.text_pieces_cache,
width: ctx.width,
first_height: ctx.first_height,
pass: Pass::Draw {
layout: &draw_layout,
max_height: &mut max_height,
width: &mut width,
gap: self.line_style.thickness,
pdf: ctx.pdf,
location: ctx.location.clone(),
preferred_height: ctx.preferred_height,
break_count: &mut break_count,
breakable: ctx.breakable.as_mut(),
},
});
if let Some(height) = max_height {
(self.content)(&mut RowContent {
text_pieces_cache: ctx.text_pieces_cache,
width: ctx.width,
first_height: ctx.first_height,
pass: Pass::DrawLines {
layout: &draw_layout,
width: None,
height,
line_style: self.line_style,
pdf: ctx.pdf,
location: ctx.location,
break_count,
breakable: ctx.breakable.as_mut(),
},
});
}
ElementSize {
width: if ctx.width.expand {
Some(ctx.width.max)
} else {
width
},
height: max_height,
}
}
}
pub struct RowContent<'a, 'b, 'c> {
text_pieces_cache: &'a TextPiecesCache,
width: WidthConstraint,
first_height: f32,
pass: Pass<'a, 'b, 'c>,
}
enum Pass<'a, 'b, 'c> {
MeasureNonExpanded {
layout: &'a mut MeasureLayout,
max_height: Option<&'a mut Option<f32>>,
breakable: Option<&'a mut BreakableMeasure<'b>>,
},
FirstLocationUsage {},
MeasureExpanded {
layout: &'a DrawLayout,
max_height: &'a mut Option<f32>,
width: Option<&'a mut Option<f32>>,
gap: f32,
breakable: Option<&'a mut BreakableMeasure<'b>>,
},
Draw {
layout: &'a DrawLayout,
max_height: &'a mut Option<f32>,
width: &'a mut Option<f32>,
gap: f32,
pdf: &'c mut Pdf,
location: Location,
preferred_height: Option<f32>,
break_count: &'a mut u32,
breakable: Option<&'a mut BreakableDraw<'b>>,
},
DrawLines {
layout: &'a DrawLayout,
height: f32,
width: Option<f32>,
break_count: u32,
line_style: LineStyle,
pdf: &'c mut Pdf,
location: Location,
breakable: Option<&'a mut BreakableDraw<'b>>,
},
}
#[derive(Copy, Clone, Serialize, Deserialize)]
pub enum Flex {
Expand(u8),
Fixed(f32),
}
fn add_height(
max_height: &mut Option<f32>,
breakable: Option<&mut BreakableMeasure>,
size: ElementSize,
break_count: u32,
extra_location_min_height: Option<f32>,
) {
if let Some(b) = breakable {
*b.extra_location_min_height =
max_optional_size(extra_location_min_height, *b.extra_location_min_height);
match break_count.cmp(b.break_count) {
std::cmp::Ordering::Less => (),
std::cmp::Ordering::Equal => {
*max_height = max_optional_size(*max_height, size.height);
}
std::cmp::Ordering::Greater => {
*b.break_count = break_count;
*max_height = size.height;
}
}
} else {
*max_height = max_optional_size(*max_height, size.height);
}
}
impl<'a, 'b, 'c> RowContent<'a, 'b, 'c> {
pub fn add<E: Element>(&mut self, element: &E, flex: Flex) {
match self.pass {
Pass::MeasureNonExpanded {
layout: &mut ref mut layout,
ref mut max_height,
ref mut breakable,
} => match flex {
Flex::Expand(fraction) => {
layout.add_expand(fraction);
}
Flex::Fixed(width) => {
layout.add_fixed(width);
if let Some(max_height) = max_height {
let mut break_count = 0;
let mut extra_location_min_height = None;
let size = element.measure(MeasureCtx {
text_pieces_cache: self.text_pieces_cache,
width: WidthConstraint {
max: width,
expand: true,
},
first_height: self.first_height,
breakable: breakable.as_mut().map(|b| BreakableMeasure {
full_height: b.full_height,
break_count: &mut break_count,
extra_location_min_height: &mut extra_location_min_height,
}),
});
add_height(
max_height,
breakable.as_deref_mut(),
size,
break_count,
extra_location_min_height,
);
}
}
},
Pass::MeasureExpanded {
layout,
max_height: &mut ref mut max_height,
ref mut width,
gap,
ref mut breakable,
} => match flex {
Flex::Expand(fraction) => {
let element_width = layout.expand_width(fraction);
let mut break_count = 0;
let mut extra_location_min_height = None;
let size = element.measure(MeasureCtx {
text_pieces_cache: self.text_pieces_cache,
width: WidthConstraint {
max: element_width,
expand: true,
},
first_height: self.first_height,
breakable: breakable.as_deref_mut().map(|b| BreakableMeasure {
full_height: b.full_height,
break_count: &mut break_count,
extra_location_min_height: &mut extra_location_min_height,
}),
});
add_height(
max_height,
breakable.as_deref_mut(),
size,
break_count,
extra_location_min_height,
);
if let &mut Some(&mut ref mut width) = width {
if let Some(w) = size.width {
if let Some(width) = width {
*width += gap + w;
} else {
*width = Some(w);
}
}
}
}
Flex::Fixed(_) => (),
},
Pass::Draw {
layout,
max_height: &mut ref mut max_height,
width: &mut ref mut width,
gap,
pdf: &mut ref mut pdf,
ref location,
preferred_height,
break_count: &mut ref mut break_count,
ref mut breakable,
} => {
let width_constraint = match flex {
Flex::Expand(fraction) => WidthConstraint {
max: layout.expand_width(fraction),
expand: true,
},
Flex::Fixed(width) => WidthConstraint {
max: width,
expand: true,
},
};
let mut element_break_count = 0;
let x_offset = if let &mut Some(width) = width {
width + gap
} else {
0.
};
let size = element.draw(DrawCtx {
pdf,
text_pieces_cache: self.text_pieces_cache,
location: Location {
pos: (location.pos.0 + x_offset, location.pos.1),
..location.clone()
},
width: width_constraint,
first_height: self.first_height,
preferred_height,
// some trickery to get rust to make a temporary option that owns the closure
breakable: breakable
.as_deref_mut()
.map(|b| {
(
b.full_height,
b.preferred_height_break_count,
|pdf: &mut Pdf, location_idx: u32, _| {
element_break_count = element_break_count.max(location_idx + 1);
let mut new_location = (b.do_break)(
pdf,
location_idx,
Some(if location_idx == 0 {
self.first_height
} else {
b.full_height
}),
);
new_location.pos.0 += x_offset;
new_location
},
)
})
.as_mut()
.map(
|&mut (
full_height,
preferred_height_break_count,
ref mut get_location,
)| {
BreakableDraw {
full_height,
preferred_height_break_count,
do_break: get_location,
}
},
),
});
if breakable.is_some() {
match element_break_count.cmp(break_count) {
std::cmp::Ordering::Less => (),
std::cmp::Ordering::Equal => {
*max_height = max_optional_size(*max_height, size.height);
}
std::cmp::Ordering::Greater => {
*break_count = element_break_count;
*max_height = size.height;
}
}
} else {
*max_height = max_optional_size(*max_height, size.height);
}
let mut width_add = |w| {
if let Some(width) = width {
*width += gap + w;
} else {
*width = Some(w);
}
};
width_add(width_constraint.max);
}
Pass::DrawLines {
layout,
height,
ref mut width,
line_style,
pdf: &mut ref mut pdf,
ref location,
break_count,
ref mut breakable,
} => {
let element_width = match flex {
Flex::Expand(fraction) => layout.expand_width(fraction),
Flex::Fixed(width) => width,
};
if let Some(width) = width {
let draw_line = |pdf: &mut Pdf, location: &Location, height: f32| {
let x = location.pos.0 + *width;
let y = location.pos.1;
let (color, _alpha) = u32_to_color_and_alpha(line_style.color);
let style = line_style;
let layer = location.layer(pdf);
layer
.save_state()
.set_line_width(mm_to_pt(style.thickness))
.set_stroke_rgb(color[0], color[1], color[2])
.set_line_cap(style.cap_style.into());
if let Some(pattern) = style.dash_pattern {
layer.set_dash_pattern(
pattern.dashes.map(f32::from),
pattern.offset as f32,
);
}
let line_x = x + line_style.thickness / 2.;
layer
.move_to(mm_to_pt(line_x), mm_to_pt(y))
.line_to(mm_to_pt(line_x), mm_to_pt(y - height))
.stroke()
.restore_state();
};
match breakable {
Some(breakable) if break_count > 0 => {
draw_line(pdf, location, self.first_height);
for i in 0..break_count {
let location = (breakable.do_break)(
pdf,
i,
Some(if i == 0 {
self.first_height
} else {
breakable.full_height
}),
);
draw_line(
pdf,
&location,
if i == break_count - 1 {
height
} else {
breakable.full_height
},
);
}
}
_ => {
draw_line(pdf, location, height);
}
}
*width += line_style.thickness + element_width;
} else {
*width = Some(element_width);
}
}
_ => todo!(),
}
}
}