Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mark: Respect transform-shape style #635

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ CeTZ 0.3.0 requires Typst 0.11.0
The licence changed from Apache-2.0 to GPLv3.

## Canvas
- Add runtime cetz version check support (see `assert-version`)
- Marks drawn using the `mark` function now respect the `transform-shape` style.
- The second argument of `mark` is now optional and defaults to `0deg`.
- Add runtime cetz version check support (see `assert-version`).
- Fixed a bug with `#set place(float: true)` affecting the canvas.
- Transformation matrices are now rounded
- The default coordinate system changed to a right-hand side system.
Expand Down
38 changes: 22 additions & 16 deletions src/draw/shapes.typ
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@
/// Note: To place a mark centered at the first coodinate (`from`) use
/// the marks `anchor: "center"` style.
///
/// To place marks unaffected by the current transformation, set their
/// `transform-shape` style to `false`.
///
/// = parameters
///
/// = Styling
Expand All @@ -436,27 +439,26 @@
///
/// - from (coordinate): The position to place the mark.
/// - to (coordinate,angle): The position or angle the mark should point towards.
/// - ..style (style):
#let mark(from, to, ..style) = {
assert.eq(
style.pos(),
(),
message: "Unexpected positional arguments: " + repr(style.pos()),
)

let style = style.named()

if type(to) == angle {
// Construct a coordinate pointing (+1, 0) away from
// `from`, rotated by the angle given.
to = ((rel: (to, 1), to: from))
/// - ..args-style (coordinate,angle,style): An optional secondary position or angle
/// to rotate the mark by and the marks style key value pairs (including `symbol:` to set the mark shape).
#let mark(from, ..args-style) = {
assert(args-style.pos().len() <= 1,
message: "Unexpected positional arguments: " + repr(args-style.pos()))

let style = args-style.named()

let to-angle = args-style.pos().at(0, default: 0deg)
let to = if type(to-angle) == angle {
(rel: (to-angle, 1), to: from)
} else if to-angle != none {
to-angle
}

(from, to).map(coordinate.resolve-system)

return (ctx => {
let (ctx, ..pts) = coordinate.resolve(ctx, from, to)
let style = styles.resolve(ctx.style, merge: style, root: "mark")
let (ctx, ..pts) = coordinate.resolve(ctx, from, to)

if style.end == none {
style.end = style.symbol
Expand All @@ -471,7 +473,11 @@
drawables = mark_.place-marks-along-path(ctx, style, none, drawables, add-path: false)
return (
ctx: ctx,
drawables: drawable.apply-transform(ctx.transform, drawables)
drawables: if style.transform-shape {
drawable.apply-transform(ctx.transform, drawables)
} else {
drawables
}
)
},)
}
Expand Down
Binary file modified tests/mark/shape-transform/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.
11 changes: 11 additions & 0 deletions tests/mark/shape-transform/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@
rotate(45deg)
line((-1,-1), (1,-1), (1,1), mark: (start: "rect", end: "rect", scale: 3))
})

#test-case({
import cetz.draw: *

set-style(mark: (transform-shape: false))

rotate(30deg)
rect((-1,-1), (1,1))

mark((0,0), symbol: "x")
})
Binary file modified tests/mark/single/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.
7 changes: 7 additions & 0 deletions tests/mark/single/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@
mark((0,0), 180deg, symbol: ">", scale: 3, fill: red)
mark((0,0), 270deg, symbol: ">", scale: 3, fill: yellow)
})

// Default angle
#test-case({
import draw: *

mark((0,0), symbol: ">")
})
Loading