Skip to content

Commit

Permalink
Merge pull request #6 from cetz-package/forward-style-arguments
Browse files Browse the repository at this point in the history
venn: Forward style arguments & support padding
  • Loading branch information
johannes-wolf authored Sep 10, 2024
2 parents 2943059 + ae9e48a commit 3e873ba
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 15 deletions.
Binary file modified manual.pdf
Binary file not shown.
4 changes: 3 additions & 1 deletion manual.typ
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ cetz.canvas({
All diagrams use the style root `venn` and accept the following style keys:
#doc-style.show-parameter-block("fill", "fill", default: "white", [The default fill for all elements])
#doc-style.show-parameter-block("stroke", "stroke", default: auto, [The default stroke for all elements])
#doc-style.show-parameter-block("padding", "number", default: 2em, [Padding of the outer (rect) element])
#doc-style.show-parameter-block("padding", ("number", "dictionary"), default: 2em, [
Padding of the outer (rect) element. Per side padding can be specified by passing a dicitonary with one or more
of the following keys: `top`, `bottom`, `left`, `right` and `rest`, where `rest` acts as a fallback for unset values.])

= Functions

Expand Down
30 changes: 18 additions & 12 deletions src/venn.typ
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,19 @@
/// - `ab`
/// - `not-ab`
///
/// - ..args (any): Set attributes
/// - ..args (any): Set and style attributes
/// - name (none, string): Element name
#let venn2(..args, name: none) = {
import cetz.draw: *

let distance = 1.25

group(name: name, ctx => {
let style = cetz.styles.resolve(ctx.style, base: default-style, merge: (:), root: "venn")
let padding = cetz.util.resolve-number(ctx, style.padding)
let style = cetz.styles.resolve(ctx.style, base: default-style, root: "venn", merge: args.named())
let padding = cetz.util.as-padding-dict(style.padding)
for (k, v) in padding {
padding.insert(k, cetz.util.resolve-number(ctx, v))
}

let args = venn-prepare-args(2, args.named(), style)

Expand All @@ -82,8 +85,8 @@
})

on-layer(args.not-ab-layer,
rect((rel: (-1 - padding, -1 - padding), to: pos-a),
(rel: (+1 + padding, +1 + padding), to: pos-b),
rect((rel: (-1 - padding.left, -1 - padding.bottom), to: pos-a),
(rel: (+1 + padding.right, +1 + padding.top), to: pos-b),
fill: args.not-ab-fill, stroke: args.not-ab-stroke, name: "frame"))

on-layer(args.ab-layer,
Expand All @@ -107,7 +110,7 @@
anchor("a", (rel: (-1 + distance / 2, 0), to: pos-a))
anchor("b", (rel: (+1 - distance / 2, 0), to: pos-b))
anchor("ab", (pos-a, 50%, pos-b))
anchor("not-ab", (rel: (padding / 2, padding / 2), to: "frame.south-west"))
anchor("not-ab", (rel: (padding.left / 2, padding.bottom / 2), to: "frame.south-west"))
})
}

Expand All @@ -133,14 +136,17 @@
///
/// - ..args (any): Set attributes
/// - name (none, string): Element name
#let venn3(..args, padding: .5, name: none) = {
#let venn3(..args, name: none) = {
import cetz.draw: *

let distance = .75

group(name: name, ctx => {
let style = cetz.styles.resolve(ctx.style, base: default-style, root: "venn")
let padding = cetz.util.resolve-number(ctx, style.padding)
let style = cetz.styles.resolve(ctx.style, base: default-style, root: "venn", merge: args.named())
let padding = cetz.util.as-padding-dict(style.padding)
for (k, v) in padding {
padding.insert(k, cetz.util.resolve-number(ctx, v))
}

let args = venn-prepare-args(3, args.named(), style)

Expand Down Expand Up @@ -171,8 +177,8 @@
let i-bc-1 = cetz.vector.add(m-bc, cetz.vector.rotate-z((-calc.sqrt(1 - calc.pow(d-bc / 2, 2)), 0), angle-bc + 90deg))

on-layer(args.not-abc-layer,
rect((rel: (-1 - padding, +1 + padding), to: pos-a),
(rel: (+1 + padding, -1 - padding), to: (pos-b.at(0), pos-c.at(1))),
rect((rel: (-1 - padding.left, +1 + padding.top), to: pos-a),
(rel: (+1 + padding.right, -1 - padding.bottom), to: (pos-b.at(0), pos-c.at(1))),
fill: args.not-abc-fill, stroke: args.not-abc-stroke, name: "frame"))

for (name, angle) in (("ab", 0deg), ("ac", 360deg / 3), ("bc", 2 * 360deg / 3)) {
Expand Down Expand Up @@ -217,7 +223,7 @@
anchor("bc", cetz.vector.lerp(a-b, a-c, .5))
anchor("ac", cetz.vector.lerp(a-a, a-c, .5))
anchor("abc", (0,0))
anchor("not-abc", (rel: (padding / 2, padding / 2), to: "frame.south-west"))
anchor("not-abc", (rel: (padding.left / 2, padding.bottom / 2), to: "frame.south-west"))
})
}

Binary file modified tests/venn2/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.
2 changes: 1 addition & 1 deletion tests/venn2/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
})

#test-case({
set-style(venn: (stroke: blue, fill: gray))
set-style(venn: (stroke: blue, fill: gray, padding: (top: 1, bottom: .5, rest: .25)))
venn2(name: "v", a-stroke: black, b-fill: green)
})
Binary file modified tests/venn3/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.
2 changes: 1 addition & 1 deletion tests/venn3/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@
})

#test-case({
set-style(venn: (stroke: blue, fill: gray))
set-style(venn: (stroke: blue, fill: gray, padding: (top: 1, bottom: .5, rest: .25)))
venn3(name: "v", a-stroke: black, ab-fill: green)
})

0 comments on commit 3e873ba

Please sign in to comment.