Skip to content

Commit

Permalink
feat: add touying-reducer for cetz and fletcher animation (#5)
Browse files Browse the repository at this point in the history
* feat: add touying-reducer

* example: update example
  • Loading branch information
OrangeX4 authored Feb 13, 2024
1 parent 7c65111 commit 593ec0d
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 2 deletions.
49 changes: 48 additions & 1 deletion examples/example.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#import "../lib.typ": s, pause, meanwhile, touying-equation, utils, states, pdfpc, themes
#import "../lib.typ": s, pause, meanwhile, touying-equation, touying-reducer, utils, states, pdfpc, themes
#import "@preview/cetz:0.2.0"
#import "@preview/fletcher:0.4.1" as fletcher: node, edge

#let cetz-canvas = touying-reducer.with(reduce: cetz.canvas, cover: cetz.draw.hide)
#let fletcher-diagram = touying-reducer.with(reduce: (arr, ..args) => fletcher.diagram(..args, ..arr))

// You can comment out the theme registration below and it can still work normally
#let s = themes.metropolis.register(s, aspect-ratio: "16-9", footer: self => self.info.institution)
Expand Down Expand Up @@ -79,6 +84,48 @@
Touying equation is very simple.
]

// cetz animation
#slide[
Cetz in Touying:

#cetz-canvas({
import cetz.draw: *

rect((0,0), (5,5))

(pause,)

rect((0,0), (1,1))
rect((1,1), (2,2))
rect((2,2), (3,3))

(pause,)

line((0,0), (2.5, 2.5), name: "line")
})
]

// fletcher animation
#slide[
Fletcher in Touying:

#fletcher-diagram(
node-stroke: .1em,
node-fill: gradient.radial(blue.lighten(80%), blue, center: (30%, 20%), radius: 80%),
spacing: 4em,
edge((-1,0), "r", "-|>", `open(path)`, label-pos: 0, label-side: center),
node((0,0), `reading`, radius: 2em),
edge((0,0), (0,0), `read()`, "--|>", bend: 130deg),
pause,
edge(`read()`, "-|>"),
node((1,0), `eof`, radius: 2em),
pause,
edge(`close()`, "-|>"),
node((2,0), `closed`, radius: 2em, extrude: (-2.5, 0)),
edge((0,0), (2,0), `close()`, "-|>", bend: -40deg),
)
]

// multiple pages for one slide
#slide[
#lorem(200)
Expand Down
2 changes: 1 addition & 1 deletion lib.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "slide.typ": s, pause, meanwhile, slides-end, touying-equation
#import "slide.typ": s, pause, meanwhile, slides-end, touying-equation, touying-reducer
#import "utils/utils.typ"
#import "utils/states.typ"
#import "utils/pdfpc.typ"
Expand Down
82 changes: 82 additions & 0 deletions slide.typ
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
},
))
}
// touying reducer mark
#let touying-reducer(reduce: arr => arr.sum(), cover: arr => none, ..args) = {
metadata((
kind: "touying-reducer",
reduce: reduce,
cover: cover,
kwargs: args.named(),
args: args.pos(),
))
}

// parse touying equation, and get the repetitions
#let _parse-touying-equation(self: utils.empty-object, need-cover: true, base: 1, index: 1, eqt) = {
Expand Down Expand Up @@ -103,6 +113,66 @@
return (result-arr, max-repetitions)
}

// parse touying reducer, and get the repetitions
#let _parse-touying-reducer(self: utils.empty-object, base: 1, index: 1, reducer) = {
let result-arr = ()
// repetitions
let repetitions = base
let max-repetitions = repetitions
// get cover function from self
let cover = reducer.cover
// parse the content
let result = ()
let cover-arr = ()
for child in reducer.args.flatten() {
if type(child) == content and child.func() == metadata {
let kind = child.value.at("kind", default: none)
if kind == "touying-pause" {
repetitions += 1
} else if kind == "touying-meanwhile" {
// clear the cover-arr when encounter #meanwhile
if cover-arr.len() != 0 {
result.push(cover(cover-arr.sum()))
cover-arr = ()
}
// then reset the repetitions
max-repetitions = calc.max(max-repetitions, repetitions)
repetitions = 1
} else {
if repetitions <= index {
result.push(child)
} else {
cover-arr.push(child)
}
}
} else {
if repetitions <= index {
result.push(child)
} else {
cover-arr.push(child)
}
}
}
// clear the cover-arr when end
if cover-arr.len() != 0 {
let r = cover(cover-arr)
if type(r) == array {
result += r
} else {
result.push(r)
}
cover-arr = ()
}
result-arr.push(
(reducer.reduce)(
..reducer.kwargs,
result,
)
)
max-repetitions = calc.max(max-repetitions, repetitions)
return (result-arr, max-repetitions)
}

// parse a sequence into content, and get the repetitions
#let _parse-content(self: utils.empty-object, need-cover: true, base: 1, index: 1, ..bodies) = {
let bodies = bodies.pos()
Expand Down Expand Up @@ -149,6 +219,18 @@
cover-arr.push(cont)
}
repetitions = nextrepetitions
} else if kind == "touying-reducer" {
// handle touying-reducer
let (conts, nextrepetitions) = _parse-touying-reducer(
self: self, base: repetitions, index: index, child.value
)
let cont = conts.first()
if repetitions <= index or not need-cover {
result.push(cont)
} else {
cover-arr.push(cont)
}
repetitions = nextrepetitions
} else {
if repetitions <= index or not need-cover {
result.push(child)
Expand Down

0 comments on commit 593ec0d

Please sign in to comment.