Skip to content

Commit a842b2d

Browse files
authored
Support Typst v0.12 in 0.0.x (#136)
* add polyfills for old introspection stuff * add typst v0.12 to ci
1 parent b49f9b8 commit a842b2d

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

.github/workflows/tests-compile-ci.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ name: Tests Compile CI
88
on:
99
# Triggers the workflow on push or pull request events but only for the branches below
1010
push:
11-
branches: [ "main", "0.0.x", "0.1.0-dev" ]
11+
branches: ["main", "0.0.x", "0.1.0-dev"]
1212
pull_request:
13-
branches: [ "main", "0.0.x", "0.1.0-dev" ]
13+
branches: ["main", "0.0.x", "0.1.0-dev"]
1414

1515
# Allows one to run this workflow manually from the Actions tab
1616
workflow_dispatch:
@@ -25,8 +25,8 @@ jobs:
2525
matrix:
2626
# Test for the following Typst versions
2727
# 0.2.0 (earliest supported), 0.6.0 (first version with package management),
28-
# 0.10.0 (latest supported)
29-
typst-version: [v0.2.0, v0.6.0, v0.10.0]
28+
# 0.10.0, 0.11.1, 0.12.0 (latest supported)
29+
typst-version: [v0.2.0, v0.6.0, v0.10.0, v0.11.1, v0.12.0]
3030

3131
# Steps represent a sequence of tasks that will be executed as part of the job
3232
steps:
@@ -44,14 +44,14 @@ jobs:
4444
DISCLAIMER
4545
4646
- name: 📥 Setup Typst 0.2.0
47-
uses: yusancky/setup-typst@v1
47+
uses: typst-community/setup-typst@v1
4848
id: setup-typst-old
4949
if: ${{ matrix.typst-version == 'v0.2.0' }}
5050
with:
5151
version: ${{ matrix.typst-version }}
5252

5353
- name: 📥 Setup Typst (0.3.0+)
54-
uses: yusancky/setup-typst@v2
54+
uses: typst-community/setup-typst@v3
5555
id: setup-typst
5656
if: ${{ matrix.typst-version != 'v0.2.0' }}
5757
with:

tablex-test.typ

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#import "tablex.typ": *
22

3+
#let _style = if using-typst-v0110 { func => context(func(none)) } else { style }
4+
35
*Test*
46

57
test
@@ -767,17 +769,17 @@ Combining em and pt (with a stroke object):
767769
columns: 2,
768770
[A #box(line(length: 50pt)) B], [A #line(length: 50pt) B],
769771
[C], [D],
770-
style(styles => {
771-
measure(line(length: 40pt), styles)
772+
_style(styles => {
773+
_measure(line(length: 40pt), styles)
772774
}),
773775
[E]
774776
)
775777
#tablex(
776778
columns: 2,
777779
[A #box(line(length: 50pt)) B], [A #line(length: 50pt) B],
778780
[C], [D],
779-
style(styles => {
780-
measure(line(length: 40pt), styles)
781+
_style(styles => {
782+
_measure(line(length: 40pt), styles)
781783
}),
782784
[E]
783785
)
@@ -791,7 +793,7 @@ Combining em and pt (with a stroke object):
791793
frac-total: 10pt,
792794
) = {
793795
set text(size: 1pt) // Set 1em to 1pt
794-
style(styles => {
796+
_style(styles => {
795797
let actual = convert-length-to-pt(
796798
len,
797799
styles: styles,
@@ -864,7 +866,7 @@ Combining em and pt (with a stroke object):
864866
compare-repr: false,
865867
) = {
866868
set text(size: 1pt) // Set 1em to 1pt
867-
style(styles => {
869+
_style(styles => {
868870
let actual = stroke-len(
869871
value,
870872
styles: styles,

tablex.typ

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@
3535
// Attachments use "t" and "b" instead of "top" and "bottom" since v0.3.0.
3636
#let using-typst-v030-or-later = using-typst-v080-or-later or $a^b$.body.has("t")
3737

38+
#let using-typst-v090 = using-typst-v080-or-later and str(-1).codepoints().first() == "\u{2212}"
39+
#let using-typst-v0110 = using-typst-v090 and sys.version >= version(0, 11, 0)
40+
41+
// Polyfill for locate() when there is no context yet
42+
#let _locate = if using-typst-v0110 { func => context(func(here())) } else { locate }
43+
44+
// Polyfill for locate() when there is already context
45+
#let _locate_inherit = if using-typst-v0110 { func => func(here()) } else { locate }
46+
47+
// Polyfill for style() when there is already context
48+
#let _style_inherit = if using-typst-v0110 { func => func((:)) } else { style }
49+
50+
// Polyfill for measure(_, styles)
51+
#let _measure = if using-typst-v0110 { (value, _) => measure(value) } else { measure }
52+
3853
// This is true if types have fields in the current Typst version.
3954
// This means we can use stroke.thickness, length.em, and so on.
4055
#let typst-fields-supported = using-typst-v080-or-later
@@ -443,13 +458,13 @@
443458
let em = len.em
444459
// Measure with abs (and later multiply by the sign) so negative em works.
445460
// Otherwise it would return 0pt, and we would need to measure again with abs.
446-
let measured-em = calc-sign(em) * measure(box(width: calc.abs(em) * 1em), styles).width
461+
let measured-em = calc-sign(em) * _measure(box(width: calc.abs(em) * 1em), styles).width
447462

448463
return pt + measured-em
449464
}
450465

451466
// Fields not supported, so we have to measure twice when em can be negative.
452-
let measured-pt = measure(box(width: len), styles).width
467+
let measured-pt = _measure(box(width: len), styles).width
453468

454469
// If the measured length is positive, `len` must have overall been positive.
455470
// There's nothing else to be done, so return the measured length.
@@ -462,7 +477,7 @@
462477
// Hence, `len` must either be `0pt` or negative.
463478
// We multiply `len` by -1 to get a positive length, draw a line and measure it, then negate
464479
// the measured length. This nicely handles the `0pt` case as well.
465-
measured-pt = -measure(box(width: -len), styles).width
480+
measured-pt = -_measure(box(width: -len), styles).width
466481
return measured-pt
467482
}
468483

@@ -476,12 +491,6 @@
476491
return len
477492
}
478493

479-
// At this point, we will need to draw a line for measurement,
480-
// so we need the styles.
481-
if styles == none {
482-
panic("Cannot convert length to pt ('styles' not specified).")
483-
}
484-
485494
return measure-pt(len, styles)
486495
}
487496

@@ -543,12 +552,6 @@
543552
return convert-ratio-type-to-pt(len.ratio, page-size) + convert-length-type-to-pt(len.length, styles: styles)
544553
}
545554

546-
// We will need to draw a line for measurement later,
547-
// so we need the styles.
548-
if styles == none {
549-
panic("Cannot convert relative length to pt ('styles' not specified).")
550-
}
551-
552555
// Note on precision: the `repr` for em components is precise, unlike
553556
// other length components, which are rounded to a precision of 2.
554557
// This is true up to Typst 0.9.0 and possibly later versions.
@@ -1258,7 +1261,6 @@
12581261

12591262
// calculate the size of auto columns (based on the max width of their cells)
12601263
#let determine-auto-columns(grid: (), styles: none, columns: none, inset: none, align: auto, fit-spans: none, page-width: 0pt) = {
1261-
assert(styles != none, message: "Cannot measure auto columns without styles")
12621264
let total_auto_size = 0pt
12631265
let auto_sizes = ()
12641266
let new_columns = columns
@@ -1319,7 +1321,7 @@
13191321
inset: cell_inset, align_default: auto
13201322
)
13211323

1322-
let width = measure(cell-box, styles).width// + 2*cell_inset // the box already considers inset
1324+
let width = _measure(cell-box, styles).width// + 2*cell_inset // the box already considers inset
13231325

13241326
// here, we are excluding from the width of this cell
13251327
// at this column all width that was already covered by
@@ -1498,7 +1500,6 @@
14981500

14991501
// calculate the size of auto rows (based on the max height of their cells)
15001502
#let determine-auto-rows(grid: (), styles: none, columns: none, rows: none, align: auto, inset: none, fit-spans: none) = {
1501-
assert(styles != none, message: "Cannot measure auto rows without styles")
15021503
let total_auto_size = 0pt
15031504
let auto_sizes = ()
15041505
let new_rows = rows
@@ -1543,7 +1544,7 @@
15431544
// measure the cell's actual height,
15441545
// with its calculated width
15451546
// and with other constraints
1546-
let height = measure(cell-box, styles).height// + 2*cell_inset (box already considers inset)
1547+
let height = _measure(cell-box, styles).height// + 2*cell_inset (box already considers inset)
15471548

15481549
// here, we are excluding from the height of this cell
15491550
// at this row all height that was already covered by
@@ -2027,11 +2028,11 @@
20272028
// NOTE: This function cannot differentiate between the actual page
20282029
// and a possible box or block where the component using this function
20292030
// could be contained in.
2030-
#let get-page-dim-writer() = locate(w_loc => {
2031+
#let get-page-dim-writer() = _locate(w_loc => {
20312032
let table_id = _tablex-table-counter.at(w_loc)
20322033
let page_dim_state = get-page-dim-state(table_id)
20332034

2034-
place(top + left, locate(loc => {
2035+
place(top + left, _locate(loc => {
20352036
page_dim_state.update(s => {
20362037
if s.top_left != none {
20372038
s
@@ -2044,7 +2045,7 @@
20442045
})
20452046
}))
20462047

2047-
place(bottom + right, locate(loc => {
2048+
place(bottom + right, _locate(loc => {
20482049
page_dim_state.update(s => {
20492050
if s.bottom_right != none {
20502051
s
@@ -2088,7 +2089,7 @@
20882089
let vlines = row-group.vlines
20892090
let (start-y, end-y) = row-group.y_span
20902091

2091-
locate(loc => {
2092+
_locate(loc => {
20922093
// let old_page = latest-page-state.at(loc)
20932094
// let this_page = loc.page()
20942095

@@ -2114,7 +2115,7 @@
21142115
if repeat-header != false {
21152116
header-pages-state.update(l => l + (page,))
21162117
if (repeat-header == true) or (type(repeat-header) == _int-type and rel_page <= repeat-header) or (type(repeat-header) == _array-type and rel_page in repeat-header) {
2117-
let measures = measure(first-row-group.content, styles)
2118+
let measures = _measure(first-row-group.content, styles)
21182119
place(top+left, first-row-group.content) // add header
21192120
added_header_height = measures.height
21202121
}
@@ -2163,7 +2164,7 @@
21632164
dy: height-between(start: first_y, end: y) + added_header_height,
21642165
cell_box.box)
21652166

2166-
// let box_h = measure(cell_box.box, styles).height
2167+
// let box_h = _measure(cell_box.box, styles).height
21672168
// tallest_box_h = calc.max(tallest_box_h, box_h)
21682169
}
21692170
first_row = false
@@ -2838,10 +2839,10 @@
28382839
let map-cols = validate-map-func(map-cols)
28392840
let fit-spans = validate-fit-spans(fit-spans, default: (x: false, y: false))
28402841

2841-
layout(size => locate(t_loc => style(styles => {
2842+
layout(size => _locate_inherit(t_loc => _style_inherit(styles => {
28422843
let table_id = _tablex-table-counter.at(t_loc)
28432844
let page_dimensions = get-page-dim-state(table_id)
2844-
let page_dim_at = page_dimensions.final(t_loc)
2845+
let page_dim_at = if using-typst-v0110 { page_dimensions.final() } else { page_dimensions.final(t_loc) }
28452846
let t_pos = t_loc.position()
28462847

28472848
// Subtract the max width/height from current width/height to disregard margin/etc.

0 commit comments

Comments
 (0)