-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathtablex.typ
2470 lines (2027 loc) · 77.9 KB
/
tablex.typ
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Welcome to tablex!
// Feel free to contribute with any features you think are missing.
// -- table counter --
#let _tablex-table-counter = counter("_tablex-table-counter")
// -- compat --
#let calc-mod(a, b) = {
calc.floor(a) - calc.floor(b * calc.floor(a / b))
}
// ------------
// -- types --
#let hlinex(
start: 0, end: auto, y: auto,
stroke: auto,
stop-pre-gutter: auto, gutter-restrict: none,
stroke-expand: true,
expand: none
) = (
tablex-dict-type: "hline",
start: start,
end: end,
y: y,
stroke: stroke,
stop-pre-gutter: stop-pre-gutter,
gutter-restrict: gutter-restrict,
stroke-expand: stroke-expand,
expand: expand,
parent: none, // if hline was broken into multiple
)
#let vlinex(
start: 0, end: auto, x: auto,
stroke: auto,
stop-pre-gutter: auto, gutter-restrict: none,
stroke-expand: true,
expand: none
) = (
tablex-dict-type: "vline",
start: start,
end: end,
x: x,
stroke: stroke,
stop-pre-gutter: stop-pre-gutter,
gutter-restrict: gutter-restrict,
stroke-expand: stroke-expand,
expand: expand,
parent: none,
)
#let cellx(content,
x: auto, y: auto,
rowspan: 1, colspan: 1,
fill: auto, align: auto,
inset: auto
) = (
tablex-dict-type: "cell",
content: content,
rowspan: rowspan,
colspan: colspan,
align: align,
fill: fill,
inset: inset,
x: x,
y: y,
)
#let occupied(x: 0, y: 0, parent_x: none, parent_y: none) = (
tablex-dict-type: "occupied",
x: x,
y: y,
parent_x: parent_x,
parent_y: parent_y
)
// -- end: types --
// -- type checks, transformers and validators --
// Is this a valid dict created by this library?
#let is-tablex-dict(x) = (
type(x) == "dictionary"
and "tablex-dict-type" in x
)
#let is-tablex-dict-type(x, ..dict_types) = (
is-tablex-dict(x)
and x.tablex-dict-type in dict_types.pos()
)
#let is-tablex-cell(x) = is-tablex-dict-type(x, "cell")
#let is-tablex-hline(x) = is-tablex-dict-type(x, "hline")
#let is-tablex-vline(x) = is-tablex-dict-type(x, "vline")
#let is-some-tablex-line(x) = is-tablex-dict-type(x, "hline", "vline")
#let is-tablex-occupied(x) = is-tablex-dict-type(x, "occupied")
#let table-item-convert(item, keep_empty: true) = {
if type(item) == "function" { // dynamic cell content
cellx(item)
} else if keep_empty and item == () {
item
} else if type(item) != "dictionary" or "tablex-dict-type" not in item {
cellx[#item]
} else {
item
}
}
#let rowspanx(length, content, ..cell_options) = {
if is-tablex-cell(content) {
(..content, rowspan: length, ..cell_options.named())
} else {
cellx(
content,
rowspan: length,
..cell_options.named())
}
}
#let colspanx(length, content, ..cell_options) = {
if is-tablex-cell(content) {
(..content, colspan: length, ..cell_options.named())
} else {
cellx(
content,
colspan: length,
..cell_options.named())
}
}
// Get expected amount of cell positions
// in the table (considering colspan and rowspan)
#let get-expected-grid-len(items, col_len: 0) = {
let len = 0
// maximum explicit 'y' specified
let max_explicit_y = items
.filter(c => c.y != auto)
.fold(0, (acc, cell) => {
if (is-tablex-cell(cell)
and type(cell.y) in ("integer", "float")
and cell.y > acc) {
cell.y
} else {
acc
}
})
for item in items {
if is-tablex-cell(item) and item.x == auto and item.y == auto {
// cell occupies (colspan * rowspan) spaces
len += item.colspan * item.rowspan
} else if type(item) == "content" {
len += 1
}
}
let rows(len) = calc.ceil(len / col_len)
while rows(len) < max_explicit_y {
len += col_len
}
len
}
#let validate-cols-rows(columns, rows, items: ()) = {
if type(columns) == "integer" {
assert(columns >= 0, message: "Error: Cannot have a negative amount of columns.")
columns = (auto,) * columns
}
if type(rows) == "integer" {
assert(rows >= 0, message: "Error: Cannot have a negative amount of rows.")
rows = (auto,) * rows
}
if type(columns) != "array" {
columns = (columns,)
}
if type(rows) != "array" {
rows = (rows,)
}
// default empty column to a single auto column
if columns.len() == 0 {
columns = (auto,)
}
// default empty row to a single auto row
if rows.len() == 0 {
rows = (auto,)
}
let col_row_is_valid(col_row) = (
col_row == auto or type(col_row) in (
"fraction", "length", "relative length", "ratio"
)
)
if not columns.all(col_row_is_valid) {
panic("Invalid column sizes (must all be 'auto' or a valid length specifier).")
}
if not rows.all(col_row_is_valid) {
panic("Invalid row sizes (must all be 'auto' or a valid length specifier).")
}
let col_len = columns.len()
let grid_len = get-expected-grid-len(items, col_len: col_len)
let expected_rows = calc.ceil(grid_len / col_len)
// more cells than expected => add rows
if rows.len() < expected_rows {
let missing_rows = expected_rows - rows.len()
rows += (rows.last(),) * missing_rows
}
let new_items = ()
let is_at_first_column(grid_len) = calc-mod(grid_len, col_len) == 0
while not is_at_first_column(get-expected-grid-len(items + new_items, col_len: col_len)) { // fix incomplete rows
new_items.push(cellx[])
}
(columns: columns, rows: rows, items: new_items)
}
// -- end: type checks and validators --
// -- utility functions --
// Which positions does a cell occupy
// (Usually just its own, but increases if colspan / rowspan
// is greater than 1)
#let positions-spanned-by(cell, x: 0, y: 0, x_limit: 0, y_limit: none) = {
let result = ()
let rowspan = if "rowspan" in cell { cell.rowspan } else { 1 }
let colspan = if "colspan" in cell { cell.colspan } else { 1 }
if rowspan < 1 {
panic("Cell rowspan must be 1 or greater (bad cell: " + repr((x, y)) + ")")
} else if colspan < 1 {
panic("Cell colspan must be 1 or greater (bad cell: " + repr((x, y)) + ")")
}
let max_x = x + colspan
let max_y = y + rowspan
if x_limit != none {
max_x = calc.min(x_limit, max_x)
}
if y_limit != none {
max_y = calc.min(y_limit, max_y)
}
for x in range(x, max_x) {
for y in range(y, max_y) {
result.push((x, y))
}
}
result
}
// initialize an array with a certain element or init function, repeated
#let init-array(amount, element: none, init_function: none) = {
let nones = ()
if init_function == none {
init_function = () => element
}
range(amount).map(i => init_function())
}
// Default 'x' to a certain value if it is equal to the forbidden value
// ('none' by default)
#let default-if-not(x, default, if_isnt: none) = {
if x == if_isnt {
default
} else {
x
}
}
// Default 'x' to a certain value if it is none
#let default-if-none(x, default) = default-if-not(x, default, if_isnt: none)
// Default 'x' to a certain value if it is auto
#let default-if-auto(x, default) = default-if-not(x, default, if_isnt: auto)
// Default 'x' to a certain value if it is auto or none
#let default-if-auto-or-none(x, default) = if x in (auto, none) {
default
} else {
x
}
// The max between a, b, or the other one if either is 'none'.
#let max-if-not-none(a, b) = if a in (none, auto) {
b
} else if b in (none, auto) {
a
} else {
calc.max(a, b)
}
// Backwards-compatible enumerate
#let enumerate(arr) = {
if type(arr) != "array" {
return arr
}
let new-arr = ()
let i = 0
for x in arr {
new-arr.push((i, x))
i += 1
}
new-arr
}
// Gets the topmost parent of a line.
#let get-top-parent(line) = {
let previous = none
let current = line
while current != none {
previous = current
current = previous.parent
}
previous
}
// Convert a certain (non-relative) length to pt
//
// styles: from style()
// page_size: equivalent to 100%
// frac_amount: amount of 'fr' specified
// frac_total: total space shared by fractions
#let convert-length-to-pt(
len,
styles: none, page_size: none, frac_amount: none, frac_total: none
) = {
page_size = 0pt + page_size
if type(len) == "length" {
if "em" in repr(len) {
if styles == none {
panic("Cannot convert length to pt ('styles' not specified).")
}
measure(line(length: len), styles).width + 0pt
} else {
len + 0pt // mm, in, pt
}
} else if type(len) == "ratio" {
if page_size == none {
panic("Cannot convert ratio to pt ('page_size' not specified).")
}
((len / 1%) / 100) * page_size + 0pt // e.g. 100% / 1% = 100; / 100 = 1; 1 * page_size
} else if type(len) == "fraction" {
if frac_amount == none {
panic("Cannot convert fraction to pt ('frac_amount' not specified).")
}
if frac_total == none {
panic("Cannot convert fraction to pt ('frac_total' not specified).")
}
if frac_amount <= 0 {
return 0pt
}
let len_per_frac = frac_total / frac_amount
(len_per_frac * (len / 1fr)) + 0pt
} else if type(len) == "relative length" {
if styles == none {
panic("Cannot convert relative length to pt ('styles' not specified).")
}
let ratio_regex = regex("^\\d+%")
let ratio = repr(len).find(ratio_regex)
if ratio == none { // 2em + 5pt (doesn't contain 100% or something)
measure(line(length: len), styles).width
} else { // 100% + 2em + 5pt --> extract the "100%" part
if page_size == none {
panic("Cannot convert relative length to pt ('page_size' not specified).")
}
// SAFETY: guaranteed to be a ratio by regex
let ratio_part = eval(ratio)
assert(type(ratio_part) == "ratio", message: "Eval didn't return a ratio")
let other_part = len - ratio_part // get the (2em + 5pt) part
let ratio_part_pt = ((ratio_part / 1%) / 100) * page_size
let other_part_pt = 0pt
if other_part < 0pt {
other_part_pt = -measure(line(length: -other_part), styles).width
} else {
other_part_pt = measure(line(length: other_part), styles).width
}
ratio_part_pt + other_part_pt + 0pt
}
} else {
panic("Cannot convert '" + type(len) + "' to length.")
}
}
// Convert a stroke to its thickness
#let stroke-len(stroke, stroke-auto: 1pt) = {
let stroke = default-if-auto(stroke, stroke-auto)
if type(stroke) in ("length", "relative length") {
stroke
} else if type(stroke) == "color" {
1pt
} else if type(stroke) == "stroke" { // 2em + blue
let r = regex("^\\d+(?:em|pt|cm|in|%)")
let s = repr(stroke).find(r)
if s == none {
1pt
} else {
eval(s)
}
} else if type(stroke) == "dictionary" and "thickness" in stroke {
stroke.thickness
} else {
1pt
}
}
// --- end: utility functions ---
// --- grid functions ---
#let create-grid(width, initial_height) = (
tablex-dict-type: "grid",
items: init-array(width * initial_height),
width: width
)
#let is-tablex-grid(value) = is-tablex-dict-type("grid")
// Gets the index of (x, y) in a grid's array.
#let grid-index-at(x, y, grid: none, width: none) = {
width = default-if-none(grid, (width: width)).width
width = calc.floor(width)
(y * width) + calc-mod(x, width)
}
// Gets the cell at the given grid x, y position.
// Width (amount of columns) per line must be known.
// E.g. grid-at(grid, 5, 2, width: 7) => 5th column, 2nd row (7 columns per row)
#let grid-at(grid, x, y) = {
let index = grid-index-at(x, y, width: grid.width)
if index < grid.items.len() {
grid.items.at(index)
} else {
none
}
}
// Returns 'true' if the cell at (x, y)
// exists in the grid.
#let grid-has-pos(grid, x, y) = (
grid-index-at(x, y, grid: grid) < grid.items.len()
)
// How many rows are in this grid? (Given its width)
#let grid-count-rows(grid) = (
calc.floor(grid.items.len() / grid.width)
)
// Converts a grid array index to (x, y)
#let grid-index-to-pos(grid, index) = (
(calc-mod(index, grid.width), calc.floor(index / grid.width))
)
// Fetches an entire row of cells (all positions with the given y).
#let grid-get-row(grid, y) = {
range(grid.width).map(x => grid-at(grid, x, y))
}
// Fetches an entire column of cells (all positions with the given x).
#let grid-get-column(grid, x) = {
range(grid-count-rows(grid)).map(y => grid-at(grid, x, y))
}
// Expand grid to the given coords (add the missing cells)
#let grid-expand-to(grid, x, y, fill_with: (grid) => none) = {
let rows = grid-count-rows(grid)
let rowws = rows
// quickly add missing rows
while rows < y {
grid.items += (fill_with(grid),) * grid.width
rows += 1
}
let now = grid-index-to-pos(grid, grid.items.len() - 1)
// now columns and/or last missing row
while not grid-has-pos(grid, x, y) {
grid.items.push(fill_with(grid))
}
let new = grid-index-to-pos(grid, grid.items.len() - 1)
grid
}
// if occupied (extension of a cell) => get the cell that generated it.
// if a normal cell => return it, untouched.
#let get-parent-cell(cell, grid: none) = {
if is-tablex-occupied(cell) {
grid-at(grid, cell.parent_x, cell.parent_y)
} else if is-tablex-cell(cell) {
cell
} else {
panic("Cannot get parent table cell of a non-cell object: " + repr(cell))
}
}
// Return the next position available on the grid
#let next-available-position(
grid, x: 0, y: 0, x_limit: 0, y_limit: 0
) = {
let cell = (x, y)
let there_is_next(cell_pos) = {
let grid_cell = grid-at(grid, ..cell_pos)
grid_cell != none
}
while there_is_next(cell) {
x += 1
if x >= x_limit {
x = 0
y += 1
}
cell = (x, y)
if y >= y_limit { // last row reached - stop
break
}
}
cell
}
// Organize cells in a grid from the given items,
// and also get all given lines
#let generate-grid(items, x_limit: 0, y_limit: 0, map-cells: c => c) = {
// init grid as a matrix
// y_limit x x_limit
let grid = create-grid(x_limit, y_limit)
let grid-index-at = grid-index-at.with(width: x_limit)
let hlines = ()
let vlines = ()
let prev_x = 0
let prev_y = 0
let x = 0
let y = 0
let first_cell_reached = false // if true, hline should always be placed after the current row
let row_wrapped = false // if true, a vline should be added to the end of a row
let range_of_items = range(items.len())
let new_empty_cell(grid, index: auto) = {
let empty_cell = cellx[]
let index = default-if-auto(index, grid.items.len())
let new_cell_pos = grid-index-to-pos(grid, index)
empty_cell.x = new_cell_pos.at(0)
empty_cell.y = new_cell_pos.at(1)
empty_cell
}
// go through all input
for i in range_of_items {
let item = items.at(i)
// allow specifying () to change vline position
if type(item) == "array" and item.len() == 0 {
if x == 0 and y == 0 { // increment vline's secondary counter
prev_x += 1
}
continue // ignore all '()'
}
let item = table-item-convert(item)
if is-some-tablex-line(item) { // detect lines' x, y
if is-tablex-hline(item) {
let this_y = if first_cell_reached {
prev_y + 1
} else {
prev_y
}
item.y = default-if-auto(item.y, this_y)
hlines.push(item)
} else if is-tablex-vline(item) {
if item.x == auto {
if x == 0 and y == 0 { // placed before any elements
item.x = prev_x
prev_x += 1 // use this as a 'secondary counter'
// in the meantime
if prev_x > x_limit + 1 {
panic("Error: Specified way too many vlines or empty () cells before the first row of the table. (Note that () is used to separate vline()s at the beginning of the table.) Please specify at most " + str(x_limit + 1) + " empty cells or vlines before the first cell of the table.")
}
} else if row_wrapped {
item.x = x_limit // allow v_line at the last column
row_wrapped = false
} else {
item.x = x
}
}
vlines.push(item)
} else {
panic("Invalid line received (must be hline or vline).")
}
items.at(i) = item // override item with the new x / y coord set
continue
}
let cell = item
assert(is-tablex-cell(cell), message: "All table items must be cells or lines.")
first_cell_reached = true
let this_x = default-if-auto(cell.x, x)
let this_y = default-if-auto(cell.y, y)
if cell.x == none or cell.y == none {
panic("Error: Received cell with 'none' as x or y.")
}
if this_x == none or this_y == none {
panic("Internal tablex error: Grid wasn't large enough to fit the given cells. (Previous position: " + repr((prev_x, prev_y)) + ", new cell: " + repr(cell) + ")")
}
cell.x = this_x
cell.y = this_y
cell = table-item-convert(map-cells(cell))
assert(is-tablex-cell(cell), message: "Tablex error: 'map-cells' returned something that isn't a valid cell.")
if row_wrapped {
row_wrapped = false
}
let content = cell.content
let content = if type(content) == "function" {
let res = content(this_x, this_y)
if is-tablex-cell(res) {
cell = res
this_x = cell.x
this_y = cell.y
[#res.content]
} else {
[#res]
}
} else {
[#content]
}
if this_x == none or this_y == none {
panic("Error: Cell with function as content returned another cell with 'none' as x or y!")
}
if type(this_x) != "integer" or type(this_y) != "integer" {
panic("Error: Cell coordinates must be integers. Invalid pair: " + repr((this_x, this_y)))
}
cell.content = content
// up to which 'y' does this cell go
let max_x = this_x + cell.colspan - 1
let max_y = this_y + cell.rowspan - 1
if this_x >= x_limit {
panic("Error: Cell at " + repr((this_x, this_y)) + " is placed at an inexistent column.")
}
if max_x >= x_limit {
panic("Error: Cell at " + repr((this_x, this_y)) + " has a colspan of " + repr(cell.colspan) + ", which would exceed the available columns.")
}
let cell_positions = positions-spanned-by(cell, x: this_x, y: this_y, x_limit: x_limit, y_limit: none)
for position in cell_positions {
let px = position.at(0)
let py = position.at(1)
let currently_there = grid-at(grid, px, py)
if currently_there != none {
let parent_cell = get-parent-cell(currently_there, grid: grid)
panic("Error: Multiple cells attempted to occupy the cell position at " + repr((px, py)) + ": one starting at " + repr((this_x, this_y)) + ", and one starting at " + repr((parent_cell.x, parent_cell.y)))
}
// initial position => assign it to the cell's x/y
if position == (this_x, this_y) {
cell.x = this_x
cell.y = this_y
// expand grid to allow placing this cell (including colspan / rowspan)
let grid_expand_res = grid-expand-to(grid, grid.width - 1, max_y)
grid = grid_expand_res
y_limit = grid-count-rows(grid)
let index = grid-index-at(this_x, this_y)
if index > grid.items.len() {
panic("Internal tablex error: Could not expand grid to include cell at " + repr((this_x, this_y)))
}
grid.items.at(index) = cell
items.at(i) = cell
// other secondary position (from colspan / rowspan)
} else {
let index = grid-index-at(px, py)
grid.items.at(index) = occupied(x: px, y: py, parent_x: this_x, parent_y: this_y) // indicate this position's parent cell (to join them later)
}
}
let next_pos = next-available-position(grid, x: this_x, y: this_y, x_limit: x_limit, y_limit: y_limit)
prev_x = this_x
prev_y = this_y
x = next_pos.at(0)
y = next_pos.at(1)
if prev_y != y {
row_wrapped = true // we changed rows!
}
}
// for missing cell positions: add empty cell
for index_item in enumerate(grid.items) {
let index = index_item.at(0)
let item = index_item.at(1)
if item == none {
grid.items.at(index) = new_empty_cell(grid, index: index)
}
}
// while there are incomplete rows for some reason, add empty cells
while calc-mod(grid.items.len(), grid.width) != 0 {
grid.items.push(new_empty_cell(grid))
}
(
grid: grid,
items: grid.items,
hlines: hlines,
vlines: vlines,
new_row_count: grid-count-rows(grid)
)
}
// -- end: grid functions --
// -- col/row size functions --
// Makes a cell's box, using the given options
// cell - The cell data (including content)
// width, height - The cell's dimensions
// inset - The table's inset
// align_default - The default alignment if the cell doesn't specify one
// fill_default - The default fill color / etc if the cell doesn't specify one
#let make-cell-box(
cell,
width: 0pt, height: 0pt, inset: 5pt,
align_default: left,
fill_default: none) = {
let align_default = if type(align_default) == "function" {
align_default(cell.x, cell.y) // column, row
} else {
align_default
}
let fill_default = if type(fill_default) == "function" {
fill_default(cell.x, cell.y) // row, column
} else {
fill_default
}
let content = cell.content
let inset = default-if-auto(cell.inset, inset)
// use default align (specified in
// table 'align:')
// when the cell align is 'auto'
let cell_align = default-if-auto(cell.align, align_default)
// same here for fill
let cell_fill = default-if-auto(cell.fill, fill_default)
if cell_align != auto and type(cell_align) not in ("alignment", "2d alignment") {
panic("Invalid alignment specified (must be either a function (row, column) -> alignment, an alignment value - such as 'left' or 'center + top' -, or 'auto').")
}
let aligned_cell_content = if cell_align == auto {
[#content]
} else {
align(cell_align)[#content]
}
box(
width: width, height: height,
inset: inset, fill: cell_fill,
// avoid #set problems
baseline: 0pt,
outset: 0pt, radius: 0pt, stroke: none,
aligned_cell_content)
}
// Sums the sizes of fixed-size tracks (cols/rows). Anything else
// (auto, 1fr, ...) is ignored.
#let sum-fixed-size-tracks(tracks) = {
tracks.fold(0pt, (acc, el) => {
if type(el) == "length" {
acc + el
} else {
acc
}
})
}
// Calculate the size of fraction tracks (cols/rows) (1fr, 2fr, ...),
// based on the remaining sizes (after fixed-size and auto columns)
#let determine-frac-tracks(tracks, remaining: 0pt, gutter: none) = {
let frac-tracks = enumerate(tracks).filter(t => type(t.at(1)) == "fraction")
let amount-frac = frac-tracks.fold(0, (acc, el) => acc + (el.at(1) / 1fr))
if type(gutter) == "fraction" {
amount-frac += (gutter / 1fr) * (tracks.len() - 1)
}
let frac-width = if amount-frac > 0 {
remaining / amount-frac
} else {
0pt
}
if type(gutter) == "fraction" {
gutter = frac-width * (gutter / 1fr)
}
for i_size in frac-tracks {
let i = i_size.at(0)
let size = i_size.at(1)
tracks.at(i) = frac-width * (size / 1fr)
}
(tracks: tracks, gutter: gutter)
}
// Gets the last (rightmost) auto column a cell is inserted in, for
// due expansion
#let get-colspan-last-auto-col(cell, columns: none) = {
let cell_cols = range(cell.x, cell.x + cell.colspan)
let last_auto_col = none
for i_col in enumerate(columns).filter(i_col => i_col.at(0) in cell_cols) {
let i = i_col.at(0)
let col = i_col.at(1)
if col == auto {
last_auto_col = max-if-not-none(last_auto_col, i)
}
}
last_auto_col
}
// Gets the last (bottom-most) auto row a cell is inserted in, for
// due expansion
#let get-rowspan-last-auto-row(cell, rows: none) = {
let cell_rows = range(cell.y, cell.y + cell.rowspan)
let last_auto_row = none
for i_row in enumerate(rows).filter(i_row => i_row.at(0) in cell_rows) {
let i = i_row.at(0)
let row = i_row.at(1)
if row == auto {
last_auto_row = max-if-not-none(last_auto_row, i)
}
}
last_auto_row
}
// Given a cell that may span one or more columns, sums the
// sizes of the columns it spans, when those columns have fixed sizes.
// Useful to subtract from the total width to find out how much more
// should an auto column extend to have that cell fit in the table.
#let get-colspan-fixed-size-covered(cell, columns: none) = {
let cell_cols = range(cell.x, cell.x + cell.colspan)
let size = 0pt
for i_col in enumerate(columns).filter(i_col => i_col.at(0) in cell_cols) {
let i = i_col.at(0)
let col = i_col.at(1)
if type(col) == "length" {
size += col
}
}
size
}
// Given a cell that may span one or more rows, sums the
// sizes of the rows it spans, when those rows have fixed sizes.
// Useful to subtract from the total height to find out how much more
// should an auto row extend to have that cell fit in the table.
#let get-rowspan-fixed-size-covered(cell, rows: none) = {
let cell_rows = range(cell.y, cell.y + cell.rowspan)
let size = 0pt
for i_row in enumerate(rows).filter(i_row => i_row.at(0) in cell_rows) {
let i = i_row.at(0)
let row = i_row.at(1)
if type(row) == "length" {
size += row
}
}
size
}
// calculate the size of auto columns (based on the max width of their cells)
#let determine-auto-columns(grid: (), styles: none, columns: none, inset: none) = {
assert(styles != none, message: "Cannot measure auto columns without styles")
let total_auto_size = 0pt
let auto_sizes = ()
let new_columns = columns
for i_col in enumerate(columns) {
let i = i_col.at(0)
let col = i_col.at(1)
if col == auto {
// max cell width
let col_size = grid-get-column(grid, i)
.fold(0pt, (max, cell) => {
if cell == none {
panic("Not enough cells specified for the given amount of rows and columns.")
}
let pcell = get-parent-cell(cell, grid: grid) // in case this is a colspan
let last_auto_col = get-colspan-last-auto-col(pcell, columns: columns)
// only expand the last auto column of a colspan,