Skip to content

Commit 6dc0028

Browse files
canvas: Add runtime version check for add-ons (#595)
* canvas: Add version number to ctx This allows other libs checking the cetz version number at runtime. * draw: Add assert-version function * draw: Default to cetz 0.2.0
1 parent ce7be43 commit 6dc0028

File tree

7 files changed

+31
-1
lines changed

7 files changed

+31
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CeTZ 0.3.0 requires Typst 0.11.0
44
The licence changed from Apache-2.0 to GPLv3.
55

66
## Canvas
7+
- Add runtime cetz version check support (see `assert-version`)
78
- Fixed a bug with `#set place(float: true)` affecting the canvas.
89
- Transformation matrices are now rounded
910
- The default coordinate system changed to a right-hand side system.

manual.typ

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ line(..c)
294294
== Grouping
295295
#doc-style.parse-show-module("/src/draw/grouping.typ")
296296

297+
#pagebreak()
298+
== Utility
299+
#doc-style.parse-show-module("/src/draw/util.typ")
300+
297301
#pagebreak()
298302
== Transformations
299303
All transformation functions push a transformation matrix onto the current transform stack. To apply transformations scoped use a `group(...)` object.

src/canvas.typ

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#import "aabb.typ"
66
#import "styles.typ"
77
#import "process.typ"
8+
#import "version.typ"
89

910
#import util: typst-length
1011

@@ -40,6 +41,7 @@
4041
message: "Canvas length must be != 0!")
4142

4243
let ctx = (
44+
version: version.version,
4345
length: length,
4446
debug: debug,
4547
// Previous element position & bbox

src/draw.typ

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
#import "draw/styling.typ": set-style, fill, stroke
44
#import "draw/shapes.typ": circle, circle-through, arc, arc-through, mark, line, grid, content, rect, bezier, bezier-through, catmull, hobby, merge-path
55
#import "draw/projection.typ": ortho, on-xy, on-xz, on-yz
6+
#import "draw/util.typ": assert-version

src/draw/util.typ

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// Assert that the cetz version of the canvas
2+
/// matches the given version (range).
3+
///
4+
/// min (version): Minimum version (current >= min)
5+
/// max (none, version): First unsupported version (current < max)
6+
/// hint (string): Name of the function/module this assert is called from
7+
#let assert-version(min, max: none, hint: "") = {
8+
if hint != "" { hint = " by " + hint }
9+
(ctx => {
10+
/* Default to 2.0.0, as this is the first version that had elements as single functions. */
11+
let v = ctx.at("version", default: version(0,2,0))
12+
assert(min <= v,
13+
message: "CeTZ canvas version is " + str(v) + ", but the minimum required version" + hint + " is " + str(min))
14+
if max != none {
15+
assert(max > v,
16+
message: "CeTZ canvas version is " + str(v) + ", but the maximum supported version" + hint + " is " + str(min))
17+
}
18+
19+
return (ctx: ctx)
20+
},)
21+
}

src/lib.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#let version = version((0,2,2))
1+
#import "version.typ": version
22

33
#import "canvas.typ": canvas
44
#import "draw.typ"

src/version.typ

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#let version = version(0,2,2)

0 commit comments

Comments
 (0)