Skip to content

Commit

Permalink
Reversed Mark Anchors (#724)
Browse files Browse the repository at this point in the history
* mark: Fix mark anchor positioning

* tests: Update ref images

* mark: Add reversed mark anchor test

* Fix triangle mark tip anchor

* PR Fixups
  • Loading branch information
johannes-wolf authored Oct 19, 2024
1 parent 1661c17 commit bfdc0e7
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/mark-shapes.typ
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@

import "/src/draw.typ": *
if style.reverse {
anchor("tip", tip)
// Since tip and base are now "swapped", we add the stroke thickness to the triangle
// base. To get smooth looking connections between the triangle tip and a connecting line,
// we do not add the tip-offset.
anchor("tip", vector.add(tip, vector.scale(dir, style.stroke.thickness / 2)))
anchor("base", base)
} else {
anchor("tip", vector.add(tip, vector.scale(dir, _calculate-tip-offset(style))))
anchor("base", vector.sub(base, vector.scale(dir, style.stroke.thickness / 2)))
}
anchor("base", vector.sub(base, vector.scale(dir, style.stroke.thickness / 2)))
}

#let create-diamond-tip-and-base-anchor(style, tip, base, center: none, ratio: 50%) = {
Expand Down
39 changes: 24 additions & 15 deletions src/mark.typ
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@
let base = mark.base
let origin = mark.at(style.anchor)

// Mirror anchors on mark center
if reverse {
(tip, base) = (base, tip)
origin = vector.sub(mark.center, vector.sub(origin, mark.center))
}

mark.offset = vector.dist(origin, tip)

let t = (
Expand All @@ -122,16 +128,17 @@
matrix.transform-rotate-dir(dir, up),
matrix.transform-rotate-z(-90deg),

// Apply mark transformations
if reverse {
matrix.transform-translate(-mark.length, 0, 0)
},
if slant not in (none, 0%) {
if type(slant) == ratio {
slant /= 100%
}
matrix.transform-shear-x(slant)
},
// Rotate mark to have base->tip on the x-axis
matrix.transform-rotate-z(if reverse {
vector.angle2(tip, base)
} else {
vector.angle2(base, tip)
}),

// Translate mark to have its anchor (tip, base, center) at (0,0)
matrix.transform-translate(..vector.scale(origin, if reverse {1} else {-1})),

// Mirror on x and/or y axis
if flip or reverse {
matrix.transform-scale({
if flip {
Expand All @@ -143,11 +150,13 @@
})
},

/* Rotate mark to have base->tip on the x-axis */
matrix.transform-rotate-z(vector.angle2(base, tip)),

/* Translate mark to have its anchor (tip, base, center) at (0,0) */
matrix.transform-translate(..vector.scale(origin, -1)),
// Slant on x axis
if slant not in (none, 0%) {
if type(slant) == ratio {
slant /= 100%
}
matrix.transform-shear-x(slant)
},
)

mark.drawables = drawable.apply-transform(
Expand Down
Binary file modified tests/arrows/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/bezier-through/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/mark/anchor/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions tests/mark/anchor/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,21 @@
(symbol: "x"),
(symbol: ">"),
))

#test-case({
import draw: *

anchor("a", (-1,0))
anchor("b", (+1,0))

line("a", "b")
mark("a", "b", symbol: "<", fill: black, anchor: "center")
mark("b", "a", symbol: "<", fill: black, anchor: "center")

anchor("c", (-1,-1))
anchor("d", (+1,-1))

line("c", "d")
mark("c", "d", symbol: ">", fill: black, anchor: "center")
mark("d", "c", symbol: ">", fill: black, anchor: "center")
})
Binary file modified tests/mark/auto-offset/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bfdc0e7

Please sign in to comment.