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

Refactor & Improve Marks #610

Merged
merged 16 commits into from
Sep 6, 2024
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package called `cetz-plot`.
- Element names are now checked to not contain a "." character.
- Fixed intersection bug for content with `anchor:` set.
- **BREAKING** The winding order of _all_ elements has been changed to CCW.
- You can now add custom marks via `register-mark`.
- Mark anchor (tip, base, center) calcuation got fixed.

## Draw
- Added `floating` function for drawing elements without affecting bounding boxes.
Expand Down
5 changes: 5 additions & 0 deletions src/canvas.typ
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
nodes: (:),
// group stack
groups: (),
// user defined marks
marks: (
mnemonics: (:),
marks: (:),
)
)

let (ctx, bounds, drawables) = process.many(ctx, body)
Expand Down
2 changes: 1 addition & 1 deletion src/draw.typ
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "draw/grouping.typ": intersections, group, scope, anchor, copy-anchors, set-ctx, get-ctx, for-each-anchor, on-layer, hide, floating
#import "draw/transformations.typ": set-transform, rotate, translate, scale, set-origin, move-to, set-viewport
#import "draw/styling.typ": set-style, fill, stroke
#import "draw/styling.typ": set-style, fill, stroke, register-mark
#import "draw/shapes.typ": circle, circle-through, arc, arc-through, mark, line, grid, content, rect, bezier, bezier-through, catmull, hobby, merge-path
#import "draw/projection.typ": ortho, on-xy, on-xz, on-yz
#import "draw/util.typ": assert-version
37 changes: 37 additions & 0 deletions src/draw/styling.typ
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,40 @@
///
/// - stroke (stroke): Stroke style
#let stroke(stroke) = set-style(stroke: stroke)

/// Register a custom mark to the canvas
///
/// The mark should contain both anchors called **tip** and **base** that are used to determine the marks orientation. If unset both default to `(0, 0)`.
/// An anchor named **center** is used as center of the mark, if present. Otherwise the mid between **tip** and **base** is used.
fenjalien marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```typc example
/// register-mark(":)", style => {
/// circle((0,0), radius: .5, fill: yellow)
/// arc((0,0), start: 180deg + 30deg, delta: 180deg - 60deg, anchor: "origin", radius: .3)
/// circle((-0.15, 0.15), radius: .1, fill: white)
/// circle((-0.10, 0.10), radius: .025, fill: black)
/// circle(( 0.15, 0.15), radius: .1, fill: white)
/// circle(( 0.20, 0.10), radius: .025, fill: black)
///
/// anchor("tip", ( 0.5, 0))
/// anchor("base", (-0.5, 0))
/// })
///
/// line((0,0), (3,0), mark: (end: ":)"))
/// ```
///
/// - symbol (str): Mark name
/// - mnemonic (none,str): Mark short name
/// - body (function): Mark drawing callback, receiving the mark style as argument and returning elements. Format `(styles) => elements`.
#let register-mark(symbol, body, mnemonic: none) = {
assert(type(symbol) == str)
assert(type(body) == function)

(ctx => {
ctx.marks.marks.insert(symbol, body)
if type(mnemonic) == str and mnemonic.len() > 0 {
ctx.marks.mnemonics.insert(mnemonic, symbol)
}
return (ctx: ctx)
},)
}
Loading
Loading