Skip to content

Commit

Permalink
decoration: Allow dynamic amplitudes
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Nov 24, 2024
1 parent d717502 commit 0c5a842
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/lib/decorations/path.typ
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
/// Length of a single segments
segment-length: none,

/// Amplitude of a segment in the direction of the segments normal
/// Amplitude of a segment in the direction of the segments normal.
/// The following types are supported:
/// - float
/// - function ratio -> float (the segment ratio is given as argument)
/// - array of floats (the rounded segment ratio is used as index)
amplitude: 1,
/// Decoration start
start: 0%,
Expand Down Expand Up @@ -59,6 +63,16 @@
factor: 150%,
)

#let resolve-amplitude(amplitude, segment, num-segments) = {
return if type(amplitude) == function {
(amplitude)(segment / (num-segments - 1) * 100%)
} else if type(amplitude) == array {
amplitude.at(int(calc.max(0, calc.round(segment / (num-segments - 1) * (amplitude.len() - 1)))), default: 0)
} else {
amplitude
}
}

#let resolve-style(ctx, segments, style) = {
assert(not (style.segments == none and style.segment-length == none),
message: "Only one of segments or segment-length must be set, while the other must be auto")
Expand Down Expand Up @@ -221,10 +235,9 @@
// list of points for the line-strip.
let fn(i, a, b, norm) = {
let ab = vector.sub(b, a)

let f = .25 - (50% - style.factor) / 50% * .25
let q-dir = vector.scale(ab, f)
let up = vector.scale(norm, style.amplitude / 2)
let up = vector.scale(norm, resolve-amplitude(style.amplitude, i, num-segments) / 2)
let down = vector.scale(up, -1)

let m1 = vector.add(vector.add(a, q-dir), up)
Expand Down Expand Up @@ -301,7 +314,8 @@
//
let fn(i, a, b, norm) = {
let ab = vector.sub(b, a)
let up = vector.scale(norm, style.amplitude / 2)
let amplitude = resolve-amplitude(style.amplitude, i, num-segments)
let up = vector.scale(norm, amplitude / 2)
let dist = vector.dist(a, b)

let d = vector.norm(ab)
Expand Down Expand Up @@ -381,7 +395,7 @@
//
let fn(i, a, b, norm) = {
let ab = vector.sub(b, a)
let up = vector.scale(norm, style.amplitude / 2)
let up = vector.scale(norm, resolve-amplitude(style.amplitude, i, num-segments) / 2)
let down = vector.scale(
up, -1)

Expand Down
Binary file modified tests/decorations/path/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.
6 changes: 6 additions & 0 deletions tests/decorations/path/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@

zigzag(line((0,0), (4,0)))
zigzag(line((0,1), (4,1)), amplitude: .5)
zigzag(line((0,2), (4,2)), amplitude: t => { 1 - .5 * t / 50% })
zigzag(line((0,3), (4,3)), amplitude: (0, 1, 0, 1, 0))
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *

wave(line((0,0), (4,0)))
wave(line((0,1), (4,1)), amplitude: .5)
wave(line((0,2), (4,2)), amplitude: t => { 1 - .5 * t / 50% })
wave(line((0,3), (4,3)), amplitude: (0, 1, 0, 1, 0))
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *

coil(line((0,0), (4,0)))
coil(line((0,1), (4,1)), amplitude: .5)
coil(line((0,2), (4,2)), amplitude: t => { 1 - .5 * t / 50% })
coil(line((0,3), (4,3)), amplitude: (0, 1, 0, 1, 0))
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
Expand Down

0 comments on commit 0c5a842

Please sign in to comment.