Skip to content

Commit

Permalink
Refactor & Improve Marks (#610)
Browse files Browse the repository at this point in the history
* mark: Refactor mark drawing

* mark: Add recursion guard + panic

* mark: Fix typo

* mark: Fixe reversed marks

* tests: Update

* mark: Fix mark filling

* tests: Update

* mark: Fix reversal + miter calculation

* mark: Support user marks

* mark: Fix user mnemonic

* tests: Add tests for custom marks

* Update docstring to new format

* Update changelog

* Minor fixes

* Docs add example

* Review stuff
  • Loading branch information
johannes-wolf committed Sep 6, 2024
1 parent 8f92067 commit 0cd4394
Show file tree
Hide file tree
Showing 50 changed files with 473 additions and 280 deletions.
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.
///
/// ```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

0 comments on commit 0cd4394

Please sign in to comment.