Skip to content

Commit 11c9895

Browse files
committed
error: Backtrace POC
1 parent 91dd180 commit 11c9895

File tree

7 files changed

+50
-25
lines changed

7 files changed

+50
-25
lines changed

src/anchor.typ

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#import deps.oxifmt: strfmt
33

44
#import "util.typ"
5-
#import util: typst-length
6-
75
#import "intersection.typ"
86
#import "drawable.typ"
97
#import "path-util.typ"

src/canvas.typ

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#import "process.typ"
88
#import "version.typ"
99

10-
#import util: typst-length
11-
1210
/// Sets up a canvas for drawing on.
1311
///
1412
/// - length (length,ratio): Used to specify what 1 coordinate unit is. If given a ratio, that ratio is relative to the containing elements width!
@@ -25,7 +23,7 @@
2523
message: "Incorrect type for body: " + repr(type(body)),
2624
)
2725

28-
assert(type(length) in (typst-length, ratio), message: "Expected `length` to be of type length or ratio, got " + repr(length))
26+
assert(type(length) in (std.length, ratio), message: "Expected `length` to be of type length or ratio, got " + repr(length))
2927
let length = if type(length) == ratio {
3028
length * ly.width
3129
} else {
@@ -38,6 +36,8 @@
3836
version: version.version,
3937
length: length,
4038
debug: debug,
39+
// Backtrace info (list of strings)
40+
backtrace: (),
4141
// Previous element position & bbox
4242
prev: (pt: (0, 0, 0)),
4343
style: styles.default,
@@ -117,7 +117,7 @@
117117
vertices += pts
118118
}
119119
}
120-
if type(drawable.stroke) == dictionary and "thickness" in drawable.stroke and type(drawable.stroke.thickness) != typst-length {
120+
if type(drawable.stroke) == dictionary and "thickness" in drawable.stroke and type(drawable.stroke.thickness) != std.length {
121121
drawable.stroke.thickness *= length
122122
}
123123
path(
@@ -127,7 +127,7 @@
127127
..vertices,
128128
)
129129
} else if drawable.type == "content" {
130-
let (width, height) = util.typst-measure(drawable.body)
130+
let (width, height) = std.measure(drawable.body)
131131
move(
132132
dx: (drawable.pos.at(0) - bounds.low.at(0)) * length - width / 2,
133133
dy: (drawable.pos.at(1) - bounds.low.at(1)) * length - height / 2,

src/coordinate.typ

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import "vector.typ"
22
#import "util.typ"
3+
#import "error.typ"
34
#import "deps.typ"
45
#import deps.oxifmt: strfmt
56

@@ -68,7 +69,7 @@
6869
}
6970

7071
// Check if node is known
71-
assert(name in ctx.nodes,
72+
error.assert(ctx, name in ctx.nodes,
7273
message: "Unknown element '" + name + "' in elements " + repr(ctx.nodes.keys()))
7374

7475
// Resolve length anchors
@@ -137,7 +138,7 @@
137138
// Distance between C and P
138139
let pc = vector.len(D)
139140
if pc < r {
140-
panic("No tangent solution for element " + c.element + " and point " + repr(c.point))
141+
error.panic(ctx, "No tangent solution for element " + c.element + " and point " + repr(c.point))
141142
}
142143
// Distance between P and X0
143144
let d = r*r / pc
@@ -300,7 +301,7 @@
300301
}
301302

302303
if t == none {
303-
panic("Failed to resolve coordinate: " + repr(c))
304+
error.panic(ctx, "Failed to resolve coordinate: " + repr(c))
304305
}
305306
return t
306307
}
@@ -357,7 +358,7 @@
357358
} else if t == "function" {
358359
resolve-function(resolve, ctx, c)
359360
} else {
360-
panic("Failed to resolve coordinate of format: " + repr(c))
361+
error.panic(ctx, "Failed to resolve coordinate of format: " + repr(c))
361362
}.map(util.resolve-number.with(ctx))
362363

363364
if update {

src/draw/shapes.typ

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#let typst-angle = angle
2-
#let typst-rotate = rotate
3-
41
#import "/src/coordinate.typ"
52
#import "/src/drawable.typ"
63
#import "/src/styles.typ"
@@ -16,6 +13,7 @@
1613
#import "/src/mark-shapes.typ" as mark-shapes_
1714
#import "/src/polygon.typ"
1815
#import "/src/aabb.typ"
16+
#import "/src/error.typ"
1917

2018
#import "transformations.typ": *
2119
#import "styling.typ": *
@@ -52,6 +50,8 @@
5250
let style = style.named()
5351

5452
(ctx => {
53+
ctx = error.add-element-backtrace(ctx, "circle", name)
54+
5555
let (ctx, pos) = coordinate.resolve(ctx, position)
5656
let style = styles.resolve(ctx.style, merge: style, root: "circle")
5757
let (rx, ry) = util.resolve-radius(style.radius).map(util.resolve-number.with(ctx))
@@ -773,7 +773,7 @@
773773
(ctx, b) = coordinate.resolve(ctx, b)
774774
}
775775

776-
let angle = if type(angle) != typst-angle {
776+
let angle = if type(angle) != std.angle {
777777
let c
778778
(ctx, c) = coordinate.resolve(ctx, angle)
779779
vector.angle2(a, c)
@@ -909,7 +909,7 @@
909909
aabb-width,
910910
aabb-height,
911911
border.segments,
912-
typst-rotate(angle,
912+
std.rotate(angle,
913913
reflow: true,
914914
origin: center + horizon,
915915
block(

src/error.typ

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// Add a backtrace entry for a single element
2+
///
3+
/// - ctx (Context): The current context
4+
/// - element (string): The elements type (e.g. circle)
5+
/// - name (none,string): The elements name
6+
/// -> context
7+
#let add-element-backtrace(ctx, element, name) = {
8+
let message = "element: " + element
9+
if name != none {
10+
message += ", name: " + name
11+
}
12+
13+
ctx.backtrace.push(message)
14+
return ctx
15+
}
16+
17+
#let _get-backtrace-string(ctx) = {
18+
if ctx != none and ctx.backtrace != () {
19+
return ". Backtrace: " + ctx.backtrace.rev().join("; ")
20+
}
21+
return ""
22+
}
23+
24+
/// Panic but with cetz backtrace
25+
#let panic(ctx, message) = {
26+
std.panic(message + _get-backtrace-string(ctx))
27+
}
28+
29+
/// Assert but with cetz backtrace
30+
#let assert(ctx, cond, message: "") = {
31+
std.assert(cond, message: message + _get-backtrace-string(ctx))
32+
}

src/mark.typ

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#import "mark-shapes.typ": get-mark
88
#import "process.typ"
99

10-
#import util: typst-length
11-
1210
/// Checks if a mark should be drawn according to the current style.
1311
/// - style (style): The current style.
1412
/// -> bool

src/util.typ

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
/// Constant to be used as float rounding error
1010
#let float-epsilon = 0.000001
1111

12-
#let typst-measure = measure
13-
#let typst-length = length
14-
15-
1612
/// Multiplies vectors by a transformation matrix. If multiple vectors are given they are returned as an array, if only one vector is given only one will be returned, if a dictionary is given they will be returned in the dictionary with the same keys.
1713
///
1814
/// - transform (matrix,function): The $4 \times 4$ transformation matrix or a function that accepts and returns a vector.
@@ -200,7 +196,7 @@
200196
/// - cnt (content): The content to measure.
201197
/// -> vector
202198
#let measure(ctx, cnt) = {
203-
let size = typst-measure(cnt)
199+
let size = std.measure(cnt)
204200
return (
205201
calc.abs(size.width / ctx.length),
206202
calc.abs(size.height / ctx.length)
@@ -275,11 +271,11 @@
275271
let east = radii.at("east", default: auto)
276272

277273
if north != auto or south != auto {
278-
assert(west == auto and east == auto,
274+
assert(ctx, west == auto and east == auto,
279275
message: "Corner radius north/south and west/east are mutually exclusive! Use per corner radii: north-west, .. instead.")
280276
}
281277
if west != auto or east != auto {
282-
assert(north == auto and south == auto,
278+
assert(ctx, north == auto and south == auto,
283279
message: "Corner radius north/south and west/east are mutually exclusive! Use per corner radii: north-west, .. instead.")
284280
}
285281

0 commit comments

Comments
 (0)