Skip to content

Commit 91dd180

Browse files
committed
coordinate: Add register function
1 parent 7f8f456 commit 91dd180

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

src/coordinate.typ

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,10 @@
322322
/// - update (bool): Update the context's last position
323323
/// -> array
324324
#let resolve(ctx, ..coordinates, update: true) = {
325-
let resolver = ()
326-
if type(ctx.resolve-coordinate) == array {
327-
resolver += ctx.resolve-coordinate
328-
} else if type(ctx.resolve-coordinate) == function {
329-
resolver.push(ctx.resolve-coordinate)
325+
let resolver = if type(ctx.resolve-coordinate) == array {
326+
ctx.resolve-coordinate
327+
} else {
328+
()
330329
}
331330

332331
let result = ()

src/draw.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
#import "draw/styling.typ": set-style, fill, stroke, register-mark
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
6+
#import "draw/util.typ": assert-version, register-coordinate-resolver

src/draw/util.typ

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,41 @@
1818
return (ctx: ctx)
1919
},)
2020
}
21+
22+
/// Push a custom coordinate resolve function to the list of coordinate
23+
/// resolvers. This resolver is scoped to the current context scope!
24+
///
25+
/// A coordinate resolver must be a function of the format `(context, coordinate) => coordinate`. And must _always_ return a valid coordinate or panic, in case of an error.
26+
///
27+
/// If multiple resolvers are registered, coordinates get passed through all
28+
/// resolvers in reverse registering order. All coordinates get paased to cetz'
29+
/// default coordinate resolvers.
30+
///
31+
/// ```typc example
32+
/// register-coordinate-resolver((ctx, c) => {
33+
/// if type(c) == dictionary and "log" in c {
34+
/// c = c.log.map(n => calc.log(n, base: 10))
35+
/// }
36+
/// return c
37+
/// })
38+
///
39+
/// circle((log: (10, 0)), radius: .25)
40+
/// circle((log: (100, 0)), radius: .25)
41+
/// circle((log: (1000, 0)), radius: .25)
42+
/// ```
43+
///
44+
/// - resolver (function): The resolver function, taking a context and a single coordinate and returning a single coordinate
45+
#let register-coordinate-resolver(resolver) = {
46+
assert.eq(type(resolver), function,
47+
message: "Coordinate resolver must be of type function (ctx, coordinate) => coordinate.")
48+
49+
return (ctx => {
50+
if type(ctx.resolve-coordinate) == array {
51+
ctx.resolve-coordinate.push(resolver)
52+
} else {
53+
ctx.resolve-coordinate = (resolver,)
54+
}
55+
56+
return (ctx: ctx)
57+
},)
58+
}

tests/coordinate/custom/test.typ

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
return coordinate
1616
}
1717

18-
set-ctx(ctx => {
19-
ctx.resolve-coordinate = log-resolver
20-
return ctx
21-
})
18+
register-coordinate-resolver(log-resolver)
2219

2320
set-style(circle: (radius: .1))
2421
for i in (.1, 1, 10, 100, 1000, 10000) {

0 commit comments

Comments
 (0)