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

Rework style (again) #377

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 doc/style.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "example.typ": example
#import "/src/lib.typ"

#import "@preview/tidy:0.1.0"
#import "@preview/t4t:0.3.2": is
Expand Down Expand Up @@ -95,7 +96,8 @@
read(path),
scope: (
example: example,
show-parameter-block: show-parameter-block
show-parameter-block: show-parameter-block,
cetz: lib
)
),
show-outline: false,
Expand Down
Binary file modified manual.pdf
Binary file not shown.
8 changes: 4 additions & 4 deletions manual.typ
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,6 @@ Supported charts are:
#doc-style.parse-show-module("/src/lib/chart/columnchart.typ")

=== Examples -- Bar Chart <barchart-examples>

```example-vertical
import cetz.chart
// Left - Basic
Expand Down Expand Up @@ -806,7 +805,7 @@ let my-star(center, name: none, ..style) = {
let def-style = (n: 5, inner-radius: .5, radius: 1)

// Resolve the current style ("star")
let style = cetz.styles.resolve(ctx.style, style.named(),
let style = cetz.styles.resolve(ctx.style, merge: style.named(),
base: def-style, root: "star")

// Compute the corner coordinates
Expand All @@ -830,6 +829,7 @@ set-style(star: (fill: yellow)) // set-style works, too!
my-star((0,6), inner-radius: .3)
```


= Internals
== Context

Expand Down Expand Up @@ -880,13 +880,13 @@ import cetz.vector
let my-star(center, ..style) = {
(ctx => {
// Define a default style
let def-style = (n: 5, inner-radius: .5, radius: 1)
let def-style = (n: 5, inner-radius: .5, radius: 1, stroke: auto, fill: auto)

// Resolve center to a vector
let (ctx, center) = cetz.coordinate.resolve(ctx, center)

// Resolve the current style ("star")
let style = cetz.styles.resolve(ctx.style, style.named(),
let style = cetz.styles.resolve(ctx.style, merge: style.named(),
base: def-style, root: "star")

// Compute the corner coordinates
Expand Down
2 changes: 1 addition & 1 deletion src/canvas.typ
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
// Previous element position & bbox
prev: (pt: (0, 0, 0)),
em-size: measure(box(width: 1em, height: 1em), st),
style: (:),
style: styles.default,
// Current transform
transform: matrix.mul-mat(
matrix.transform-shear-z(.5),
Expand Down
4 changes: 2 additions & 2 deletions src/draw/grouping.typ
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
message: "Unexpected positional arguments: " + repr(style.pos()),
)
(ctx => {
let style = styles.resolve(ctx, style.named(), root: "group")
let style = styles.resolve(ctx.style, merge: style.named(), root: "group")

let bounds = none
let drawables = ()
Expand Down Expand Up @@ -447,7 +447,7 @@
end: path-util.point-on-path(path.segments, 1)
)

let style = styles.resolve(ctx.style, style, root: "mark")
let style = styles.resolve(ctx.style, merge: style, root: "mark")

for mark in marks {
let (pos, dir) = path-util.direction(path.segments, mark.pos)
Expand Down
47 changes: 25 additions & 22 deletions src/draw/shapes.typ
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

(ctx => {
let (ctx, pos) = coordinate.resolve(ctx, position)
let style = styles.resolve(ctx.style, style, root: "circle")
let style = styles.resolve(ctx.style, merge: style, root: "circle")
let (rx, ry) = util.resolve-radius(style.radius).map(util.resolve-number.with(ctx))
let (cx, cy, cz) = pos
let (ox, oy) = (calc.cos(45deg) * rx, calc.sin(45deg) * ry)
Expand Down Expand Up @@ -143,7 +143,7 @@

let center = util.calculate-circle-center-3pt(a, b, c)

let style = styles.resolve(ctx.style, style, root: "circle")
let style = styles.resolve(ctx.style, merge: style, root: "circle")
let (cx, cy, cz) = center
let r = vector.dist(a, (cx, cy))
let (ox, oy) = (calc.cos(45deg) * r, calc.sin(45deg) * r)
Expand Down Expand Up @@ -266,7 +266,7 @@
assert.ne(start-angle, stop-angle, message: "Angle must be greater than 0deg")

return (ctx => {
let style = styles.resolve(ctx.style, style, root: "arc")
let style = styles.resolve(ctx.style, merge: style, root: "arc")
assert(style.mode in ("OPEN", "PIE", "CLOSE"))

let (ctx, arc-start) = coordinate.resolve(ctx, position)
Expand Down Expand Up @@ -540,7 +540,7 @@

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

return (ctx: ctx, drawables: drawable.mark(
..pts,
Expand Down Expand Up @@ -585,7 +585,7 @@

return (ctx => {
let (ctx, ..pts) = coordinate.resolve(ctx, ..pts)
let style = styles.resolve(ctx.style, style, root: "line")
let style = styles.resolve(ctx.style, merge: style, root: "line")
let (transform, anchors) = anchor_.setup(
(anchor) => {
(
Expand Down Expand Up @@ -642,16 +642,17 @@
///
/// = Styling
/// *Root:* `grid`
/// / step: TODO
/// / help-lines: TODO
///
/// = Anchors
/// Supports compass anchors.
///
/// - from (coordinate): The top left of the grid
/// - to (coordinate): The bottom right of the grid
/// - step (number): Grid spacing.
/// - name (none,string):
/// - ..style (style):
#let grid(from, to, step: 1, name: none, help-lines: false, ..style) = {
#let grid(from, to, name: none, ..style) = {
(from, to).map(coordinate.resolve-system)

assert.eq(style.pos(), (), message: "Unexpected positional arguments: " + repr(style.pos()))
Expand All @@ -665,17 +666,21 @@
(calc.max(from.at(0), to.at(0)), calc.max(from.at(1), to.at(1)))
)

let style = styles.resolve(ctx.style, style)
if help-lines {
let style = styles.resolve(ctx.style, merge: style, root: "grid", base: (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not having this in the default style dictionary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't really matter where the default of an element's style is. Really styles.default can just have stroke, fill, radius, padding, shorten and mark keys in it. Then use base for each element and it'll work just the same...

step: 1,
stroke: auto,
help-lines: false,
))
if style.help-lines {
style.stroke = 0.2pt + gray
}

let (x-step, y-step) = if type(step) == dictionary {
(step.at("x", default: 1), step.at("y", default: 1))
} else if type(step) == array {
step
let (x-step, y-step) = if type(style.step) == dictionary {
(style.step.at("x", default: 1), style.step.at("y", default: 1))
} else if type(style.step) == array {
style.step
} else {
(step, step)
(style.step, style.step)
}.map(util.resolve-number.with(ctx))

let drawables = {
Expand All @@ -685,7 +690,6 @@
x += from.at(0)
drawable.path(
path-util.line-segment(((x, from.at(1)), (x, to.at(1)))),
fill: style.fill,
stroke: style.stroke
)
})
Expand All @@ -698,7 +702,6 @@
y += from.at(1)
drawable.path(
path-util.line-segment(((from.at(0), y), (to.at(1), y))),
fill: style.fill,
stroke: style.stroke
)
})
Expand Down Expand Up @@ -816,7 +819,7 @@
}

return (ctx => {
let style = styles.resolve(ctx.style, style, root: "content")
let style = styles.resolve(ctx.style, merge: style, root: "content")
let padding = util.as-padding-dict(style.padding)
for (k, v) in padding {
padding.insert(k, util.resolve-number(ctx, v))
Expand Down Expand Up @@ -1032,7 +1035,7 @@
transform: ctx.transform
)

let style = styles.resolve(ctx.style, style, root: "rect")
let style = styles.resolve(ctx.style, merge: style, root: "rect")
let (x1, y1, z1) = a
let (x2, y2, z2) = b
let drawables = drawable.path(
Expand Down Expand Up @@ -1118,7 +1121,7 @@
transform: ctx.transform
)

let style = styles.resolve(ctx.style, style, root: "bezier")
let style = styles.resolve(ctx.style, merge: style, root: "bezier")

let curve = (start, end, ..ctrl)
let (marks, curve) = if style.mark != none {
Expand Down Expand Up @@ -1231,7 +1234,7 @@
)
}

let style = styles.resolve(ctx.style, style, root: "catmull")
let style = styles.resolve(ctx.style, merge: style, root: "catmull")
let curves = bezier_.catmull-to-cubic(
pts,
style.tension,
Expand Down Expand Up @@ -1320,7 +1323,7 @@
)
}

let style = styles.resolve(ctx.style, style, root: "hobby")
let style = styles.resolve(ctx.style, merge: style, root: "hobby")
let curves = hobby_.hobby-to-cubic(
pts,
ta: ta,
Expand Down Expand Up @@ -1407,7 +1410,7 @@
}
}

let style = styles.resolve(ctx.style, style)
let style = styles.resolve(ctx.style, merge: style)

let (transform, anchors) = anchor_.setup(
anchor => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/angle.typ
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
stroke: auto,
radius: .5,
label-radius: 50%,
mark: styles.default.mark,
mark: auto,
)

/// Draw an angle between `a` and `b` through origin `origin`
Expand Down Expand Up @@ -62,7 +62,7 @@
name: none,
..style
) = draw.group(name: name, ctx => {
let style = styles.resolve(ctx.style, style.named(), base: default-style, root: "angle")
let style = styles.resolve(ctx.style, merge: style.named(), base: default-style, root: "angle")
let (ctx, origin, a, b) = coordinate.resolve(ctx, origin, a, b)

assert(origin.at(2) == a.at(2) and a.at(2) == b.at(2),
Expand Down
24 changes: 16 additions & 8 deletions src/lib/axes.typ
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
stroke: (paint: gray, dash: "dotted"),
fill: none
),
x: (
fill: auto,
stroke: auto,
mark: auto,
tick: auto
),
y: (
fill: auto,
stroke: auto,
mark: auto,
tick: auto
)
)

#let default-style-schoolbook = util.merge-dictionary(default-style, (
Expand Down Expand Up @@ -313,7 +325,7 @@
anchor("data-top-right", (w, h))

let style = style.named()
style = styles.resolve(ctx.style, style, root: "axes",
style = styles.resolve(ctx.style, merge: style, root: "axes",
base: default-style)

let padding = (
Expand Down Expand Up @@ -467,7 +479,7 @@
let style = style.named()
style = styles.resolve(
ctx.style,
style,
merge: style,
root: "axes",
base: default-style-schoolbook
)
Expand All @@ -492,9 +504,7 @@
(y-axis, "east", (y-x, auto), (1, 0), "y"),
)

line((-padding.left, x-y), (w + padding.right, x-y),
..util.merge-dictionary(style, style.at("x", default: (:))),
name: "x-axis")
line((-padding.left, x-y), (w + padding.right, x-y), ..style.x, name: "x-axis")
if "label" in x-axis and x-axis.label != none {
let anchor = style.label.anchor
if style.label.anchor == auto {
Expand All @@ -504,9 +514,7 @@
anchor: anchor, par(justify: false, x-axis.label))
}

line((y-x, -padding.bottom), (y-x, h + padding.top),
..util.merge-dictionary(style, style.at("y", default: (:))),
name: "y-axis")
line((y-x, -padding.bottom), (y-x, h + padding.top), ..style.y, name: "y-axis")
if "label" in y-axis and y-axis.label != none {
let anchor = style.label.anchor
if style.label.anchor == auto {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/chart/barchart.typ
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@
)

group(ctx => {
let style = util.merge-dictionary(barchart-default-style,
styles.resolve(ctx.style, (:), root: "barchart"))
let style = styles.resolve(ctx.style, root: "barchart", base: barchart-default-style)

axes.scientific(
size: size,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/chart/columnchart.typ
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@
)

group(ctx => {
let style = util.merge-dictionary(columnchart-default-style,
styles.resolve(ctx.style, (:), root: "columnchart"))
let style = styles.resolve(ctx.style, root: "barchart", base: columnchart-default-style)


axes.scientific(size: size,
left: y,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/decorations.typ
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

group(name: name, ctx => {
// Get styles and validate types and values
let style = styles.resolve(ctx.style, style.named(),
let style = styles.resolve(ctx.style, merge: style.named(),
root: "brace", base: brace-default-style)

let amplitude = style.amplitude
Expand Down Expand Up @@ -259,7 +259,7 @@

group(name: name, ctx => {
// Get styles and validate their types and values
let style = styles.resolve(ctx.style, style.named(),
let style = styles.resolve(ctx.style, merge: style.named(),
root: "flat-brace", base: flat-brace-default-style)

let amplitude = style.amplitude
Expand Down
8 changes: 6 additions & 2 deletions src/lib/plot.typ
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,12 @@
if type(data.at(i).mark-style) == function {
data.at(i).mark-style = (data.at(i).mark-style)(i)
}
data.at(i).mark-style = util.merge-dictionary(
mark-style-base, data.at(i).mark-style)
if type(data.at(i).mark-style) == dictionary {
data.at(i).mark-style = util.merge-dictionary(
mark-style-base,
data.at(i).mark-style
)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/plot/legend.typ
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
// Draw a legend box at position relative to anchor of plot-element
#let draw-legend(ctx, style, items, size, plot, position, anchor) = {
let style = styles.resolve(
ctx.style, style, base: default-style, root: "legend")
ctx.style, merge: style, base: default-style, root: "legend")
assert(style.orientation in (ttb, ltr),
message: "Unsupported legend orientation.")

Expand Down
Loading