Skip to content

Commit f95c1f2

Browse files
committed
mark: Fix (some) issues
1 parent 5373e91 commit f95c1f2

File tree

20 files changed

+39
-90
lines changed

20 files changed

+39
-90
lines changed

src/mark-shapes.typ

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
return calc.sin(angle/2) * (style.stroke.thickness / 2)
3535
}
3636

37-
#let create-tip-and-base-anchor(style, tip, base, center: none, respect-stroke-thickness: true) = {
38-
if base == tip { base = vector.add(tip, (1e-8, 0, 0)) }
37+
#let create-tip-and-base-anchor(style, tip, base, center: none, respect-stroke-thickness: false) = {
38+
if base == auto or base == tip {
39+
base = vector.add(tip, (1e-6, 0, 0))
40+
}
3941
let dir = vector.norm(vector.sub(tip, base))
4042

4143
let thickness = if respect-stroke-thickness {
@@ -52,19 +54,23 @@
5254
}
5355

5456
#let create-triangle-tip-and-base-anchor(style, tip, base, center: none) = {
55-
if base == tip { base = vector.add(tip, (1e-8, 0, 0)) }
57+
if base == auto or base == tip {
58+
base = vector.add(tip, (1e-6, 0, 0))
59+
}
5660
let dir = vector.norm(vector.sub(tip, base))
61+
let dist = vector.dist(tip, base)
62+
let thickness = calc.min(style.stroke.at("thickness", default: 0), dist) / 2
5763

5864
import "/src/draw.typ": anchor
5965
if style.reverse {
6066
// Since tip and base are now "swapped", we add the stroke thickness to the triangle
6167
// base. To get smooth looking connections between the triangle tip and a connecting line,
6268
// we do not add the tip-offset.
63-
anchor("tip", vector.add(tip, vector.scale(dir, style.stroke.thickness / 2)))
64-
anchor("base", base)
69+
anchor("tip", tip)
70+
anchor("base", vector.sub(base, vector.scale(dir, thickness)))
6571
} else {
6672
anchor("tip", vector.add(tip, vector.scale(dir, _calculate-tip-offset(style))))
67-
anchor("base", vector.sub(base, vector.scale(dir, style.stroke.thickness / 2)))
73+
anchor("base", base)
6874
}
6975
}
7076

@@ -96,9 +102,9 @@
96102
import "/src/draw.typ": *
97103

98104
if style.harpoon {
99-
line((0,0), (style.length, 0), (style.length, +style.width / 2), close: true)
105+
line((0,0), (style.length, 0), (style.length, style.width / 2), close: true)
100106
} else {
101-
line((0,0), (style.length, -style.width / 2), (style.length, +style.width / 2), close: true)
107+
line((0,0), (style.length, -style.width / 2), (style.length, style.width / 2), close: true)
102108
}
103109

104110
create-triangle-tip-and-base-anchor(style, (0, 0), (style.length, 0))
@@ -129,7 +135,7 @@
129135
}
130136

131137
let offset = style.stroke.thickness / 2
132-
create-tip-and-base-anchor(style, (offset, 0), (offset, 0), respect-stroke-thickness: false)
138+
create-tip-and-base-anchor(style, (-offset, 0), (offset, 0))
133139
anchor("center", (0, 0))
134140
},
135141
ellipse: (style) => {
@@ -143,7 +149,7 @@
143149
circle((0, 0), radius: r)
144150
}
145151

146-
create-tip-and-base-anchor(style, (r.at(0), 0), (-r.at(0), 0))
152+
create-tip-and-base-anchor(style, (r.at(0), 0), (-r.at(0), 0), respect-stroke-thickness: true)
147153
},
148154
circle: (style) => {
149155
import "/src/draw.typ": arc, circle
@@ -156,21 +162,20 @@
156162
circle((0, 0), radius: r)
157163
}
158164

159-
create-tip-and-base-anchor(style, (r, 0), (-r, 0))
165+
create-tip-and-base-anchor(style, (r, 0), (-r, 0), respect-stroke-thickness: true)
160166
},
161167
bracket: (style) => {
162168
import "/src/draw.typ": *
163169

164170
let (l, w, i) = (style.length, style.width, style.inset)
165171

166172
if style.harpoon {
167-
line((-l - i, w / 2), (0, w / 2), (0, 0), fill: none)
173+
line((l + i, w / 2), (0, w / 2), (0, 0), fill: none)
168174
} else {
169-
line((-l - i, w / 2), (0, w / 2), (0, -w / 2), (-l - i, -w / 2), fill: none)
175+
line((l + i, w / 2), (0, w / 2), (0, -w / 2), (l + i, -w / 2), fill: none)
170176
}
171177

172-
let offset = style.stroke.thickness / 2
173-
create-tip-and-base-anchor(style, (offset, 0), (offset + 1e-8, 0))
178+
create-tip-and-base-anchor(style, (0, 0), (0, 0), respect-stroke-thickness: true)
174179
},
175180
diamond: (style) => {
176181
import "/src/draw.typ": *
@@ -196,7 +201,7 @@
196201
rect((0, -w / 2), (-l, +w / 2))
197202
}
198203

199-
create-tip-and-base-anchor(style, (0, 0), (-l, 0))
204+
create-tip-and-base-anchor(style, (0, 0), (-l, 0), respect-stroke-thickness: true)
200205
},
201206
hook: (style) => {
202207
import "/src/draw.typ": *
@@ -213,9 +218,7 @@
213218
}
214219
}, fill: none)
215220

216-
line((0, 0), (l - r, 0))
217-
218-
create-tip-and-base-anchor(style, (-r, 0), (l - r, 0), center: ((-r + i) / 2, 0), respect-stroke-thickness: false)
221+
create-tip-and-base-anchor(style, (-r, 0), (0, 0), center: ((-r + i) / 2, 0))
219222
},
220223
// An unfilled mark in the shape of an angle bracket (>).
221224
straight: (style) => {
@@ -230,7 +233,7 @@
230233
}
231234

232235
if style.harpoon {
233-
create-tip-and-base-anchor(style, (0, 0), (-1e-6, 0), respect-stroke-thickness: false)
236+
create-tip-and-base-anchor(style, (0, 0), (0, 0))
234237
} else {
235238
create-triangle-tip-and-base-anchor(style, (0, 0), (0, 0))
236239
}
@@ -254,33 +257,27 @@
254257
}, ..style)
255258

256259
let offset = style.stroke.thickness / 2
257-
create-tip-and-base-anchor(style, (-offset, 0), (-offset - 1e-6, 0), respect-stroke-thickness: false)
260+
create-tip-and-base-anchor(style, (-offset, 0), (2 * offset, 0))
258261
},
259262
plus: (style) => {
260263
import "/src/draw.typ": *
261264

262-
let style = style
263-
style.stroke.join = "round"
264-
265265
let (l, w) = (style.length, style.width)
266266

267267
line((-l / 2, 0), (+l / 2, 0))
268268
line((0, -w / 2), (0, +w / 2))
269269

270-
create-tip-and-base-anchor(style, (0, 0), (l / 2, 0), respect-stroke-thickness: false)
270+
create-tip-and-base-anchor(style, (0, 0), (0, 0))
271271
},
272272
x: (style) => {
273273
import "/src/draw.typ": *
274274

275-
let style = style
276-
style.stroke.join = "round"
277-
278275
let (l, w) = (style.length, style.width)
279276

280277
line((-l / 2, w / 2), (+l / 2, -w / 2))
281278
line((-l / 2, -w / 2), (+l / 2, +w / 2))
282279

283-
create-tip-and-base-anchor(style, (0, 0), (0, 0), respect-stroke-thickness: false)
280+
create-tip-and-base-anchor(style, (0, 0), (0, 0))
284281
},
285282
star: (style) => {
286283
import "/src/draw.typ": *
@@ -293,7 +290,7 @@
293290
line((0, 0), (calc.cos(a) * l / 2, calc.sin(a) * w / 2))
294291
}
295292

296-
create-tip-and-base-anchor(style, (0, 0), (l / 2, 0), respect-stroke-thickness: false)
293+
create-tip-and-base-anchor(style, (0, 0), (0, 0))
297294
},
298295
)
299296
#let names = marks.keys()

src/mark.typ

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@
111111

112112
assert(style.anchor in ("tip", "base", "center"))
113113
let tip = mark.tip
114-
let base = mark.base
115-
let origin = mark.at(style.anchor)
114+
let center = mark.at("center", default: tip)
115+
let base = mark.at("base", default: tip)
116116

117117
// Mirror anchors on mark center
118118
if reverse {
119119
(tip, base) = (base, tip)
120-
origin = vector.sub(mark.center, vector.sub(origin, mark.center))
121120
}
122121

122+
let origin = (tip: tip, base: base, center: center).at(style.anchor)
123123
mark.offset = vector.dist(origin, tip)
124124

125125
let t = (
@@ -271,11 +271,10 @@
271271
let mark = _eval-mark-shape-and-anchors(ctx, mark-fn(style), style)
272272
let offset = style.at("offset", default: 0)
273273
let inset = style.at("inset", default: 0)
274-
//inset = calc.min(inset, mark.length)
275274

276275
let mark-tip-info = path-util.point-at(
277276
segments, distance, reverse: is-end)
278-
let mark-base-info = if mark.length > 0 {
277+
let mark-base-info = if mark.length != 0 {
279278
path-util.point-at(
280279
segments, distance + mark.length - inset, reverse: is-end)
281280
} else {
@@ -386,7 +385,6 @@
386385
segments = path-util.shorten-to(
387386
segments,
388387
distance,
389-
mode: "CURVED",
390388
samples: style.position-samples,
391389
snap-to: snap-to)
392390
}

src/path-util.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@
224224
let pt = args.last()
225225
return (
226226
vector.lerp(origin, pt, calc.min(1, distance / vector.dist(origin, pt))),
227-
vector.norm(vector.sub(origin, pt)))
227+
vector.norm(vector.sub(pt, origin)))
228228
} else if kind == "c" {
229229
let (c1, c2, e) = args
230230
let t = bezier.cubic-t-for-distance(origin, e, c1, c2, distance, samples: samples)

tests/angle/ref/1.png

70 Bytes
Loading

tests/arrows/ref/1.png

-346 Bytes
Loading

tests/content-rtl/ref/1.png

-9 Bytes
Loading

tests/intersection/ref/1.png

13 Bytes
Loading

tests/mark-anchors/ref/1.png

1.91 KB
Loading

tests/mark-anchors/test.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#test-case(args => {
3131
import draw: *
3232

33-
set-style(mark: (stroke: red))
33+
set-style(mark: (stroke: blue))
3434
line((0,-1.2), (0,+1.2), stroke: green)
3535
mark((0, 0), (-1, 0), symbol: args.symbol, anchor: "tip")
3636
line((-1, 2), (3, 2), mark: (start: args.symbol, end: args.symbol, harpoon: true))

tests/mark-auto-offset/ref/1.png

1.37 KB
Loading

0 commit comments

Comments
 (0)