27 Commits

Author SHA1 Message Date
6970e59413 renamed get- to compute-columns-widths 2025-07-13 16:09:10 +02:00
57a3a371eb refactored pre-rendering 2025-07-13 16:06:02 +02:00
c1191de661 refactored setup process 2025-07-13 14:36:25 +02:00
5e70e5179f Merge pull request 'Add parameter to style participant line' (#11) from feat/10-participant-line-styling into dev
Reviewed-on: #11
2025-07-09 20:47:50 +00:00
000813ff3b bumped version to 0.2.2 2025-07-09 22:41:30 +02:00
f94c6f25a2 added documentation for line-stroke 2025-07-09 22:39:50 +02:00
d1a44d0b0f added line-stroke participant param 2025-07-09 22:35:37 +02:00
1b8c40d70d added uncommitted file
Forgot to add cetz.typ in 788c8999 (oops)
2025-07-09 22:23:54 +02:00
b9c19ac730 fixed link in manual 2025-07-09 21:55:32 +02:00
c46b345771 updated version in README 2025-03-12 14:17:32 +01:00
060e37e38e switched to justfile 2025-03-12 14:14:27 +01:00
788c8999b1 bumped to Typst 0.13.1 + cetz 0.3.4 2025-03-12 14:14:10 +01:00
3eadf63db3 Merge pull request 'Fix _ret in group' (#8) from fix/issue-6-alt-groups into dev
Reviewed-on: #8
2025-03-10 17:58:06 +00:00
e57e617bb9 fixed groups with _ret 2025-03-10 17:54:01 +00:00
dfb980e366 Merge pull request 'Adapt to typst v0.13.0' (#7) from johannesacco/chronos:main into dev
Reviewed-on: #7
2025-03-10 16:41:51 +00:00
3beaad03d4 Bump version to 0.2.1 2025-03-10 17:22:49 +01:00
5cb8f50f2a Update to cetz:0.3.3
This fixes a number of warnings introduced in typst v0.13.0.

Also increase typst version to v0.13.0 as that is what cetz:0.3.3
requires.
2025-03-10 17:22:42 +01:00
e8d737ff00 updated manual 2024-11-09 17:57:53 +01:00
19c60c5ecf Release 0.2.0
Reviewed-on: #5
2024-11-09 14:00:05 +00:00
df3a2ddf68 updated README 2024-11-09 14:54:59 +01:00
42e8e26aa7 updated manual and gallery 2024-11-09 14:50:55 +01:00
b33531bef5 added max-width param to _col 2024-11-09 14:42:11 +01:00
ab1386e721 added comment wrapping if width is restricted 2024-11-09 14:41:44 +01:00
1f155063a8 updated CeTZ to 0.3.1 2024-11-09 14:19:14 +01:00
d986284e4e updated typst to 0.12.0 + bumped version to 0.2.0 2024-11-09 14:05:41 +01:00
a037727edf removed font set rule 2024-11-04 08:18:34 +01:00
b0950f5e68 fixed sequences with both create- and enable-dst 2024-10-27 11:24:03 +01:00
29 changed files with 785 additions and 471 deletions

View File

@ -15,7 +15,7 @@ This package lets you render sequence diagrams directly in Typst. The following
<td>
```typst
#import "@preview/chronos:0.1.0"
#import "@preview/chronos:0.2.2"
#chronos.diagram({
import chronos: *
_par("Alice")

View File

@ -5,7 +5,7 @@
chronos: chronos
)
#let example(src, show-src: true, vertical: false, fill: true, wrap: true) = {
#let example(src, show-src: true, vertical: false, fill: false, wrap: true) = {
src = src.text
let full-src = example-preamble + src
let body = eval(full-src, scope: example-scope)
@ -16,15 +16,15 @@
box(
stroke: black + 1pt,
radius: .5em,
fill: if fill {color.white.darken(5%)} else {none},
fill: if fill {color.white.darken(2%)} else {none},
if show-src {
let src-block = align(left, raw(src, lang: "typc"))
let src-block = raw(src, block: true, lang: "typc")
table(
columns: if vertical {1} else {2},
inset: 1em,
inset: 5pt,
align: horizon + center,
stroke: none,
img,
table.cell(inset: 1em, img),
if vertical {table.hline()} else {table.vline()}, src-block
)
} else {

View File

@ -1,8 +1,23 @@
#import "example.typ": example
#let seq-return = example(```
_seq(
"Bob", "Alice",
comment: [hello],
enable-dst: true
)
_seq(
"Alice", "Alice",
comment: [some action]
)
_ret(comment: [bye])
```)
#let seq-comm-align = example(```
_par("p1", display-name: "Start participant")
_par("p2", display-name: "End participant")
_par("p1",
display-name: "Start participant")
_par("p2",
display-name: "End participant")
let alignments = (
"start", "end",
"left", "right",
@ -90,15 +105,15 @@ _par("a", display-name: "Alice")
_par("b", display-name: "Bob")
_loop("default loop", {
_seq("a", "b", comment: "Are you here ?")
_seq("a", "b", comment: "Are you here?")
})
_gap()
_loop("min loop", min: 1, {
_seq("a", "b", comment: "Are you here ?")
_seq("a", "b", comment: "Are you here?")
})
_gap()
_loop("min-max loop", min: 1, max: 5, {
_seq("a", "b", comment: "Are you still here ?")
_seq("a", "b", comment: "Are you still here?")
})
```)
@ -121,15 +136,58 @@ _sync({
})
```)
#let gaps-seps = example(```
_par("alice", display-name: "Alice")
_par("bob", display-name: "Bob")
#let gaps = example(```
_par("a", display-name: "Alice")
_par("b", display-name: "Bob")
_seq("alice", "bob", comment: "Hello")
_gap(size: 10)
_seq("bob", "alice", comment: "Hi")
_sep("Another day")
_seq("alice", "bob", comment: "Hello again")
_seq("a", "b", comment: [message 1])
_seq("b", "a", comment: [ok], dashed: true)
_gap()
_seq("a", "b", comment: [message 2])
_seq("b", "a", comment: [ok], dashed: true)
_gap(size: 40)
_seq("a", "b", comment: [message 3])
_seq("b", "a", comment: [ok], dashed: true)
```)
#let seps = example(```
_par("a", display-name: "Alice")
_par("b", display-name: "Bob")
_sep[Initialization]
_seq("a", "b", comment: [Request 1])
_seq(
"b", "a",
comment: [Response 1],
dashed: true
)
_sep[Repetition]
_seq("a", "b", comment: [Request 2])
_seq(
"b", "a",
comment: [Response 2],
dashed: true
)
```)
#let delays = example(```
_par("a", display-name: "Alice")
_par("b", display-name: "Bob")
_seq("a", "b", comment: [Auth Request])
_delay()
_seq(
"b", "a",
comment: [Auth Response],
dashed: true
)
_delay(name: [5 minutes later])
_seq(
"b", "a",
comment: [Good Bye !],
dashed: true
)
```)
#let notes-shapes = example(```

View File

@ -1,8 +1,15 @@
/// Creates a gap before the next element
/// - size (int): Size of the gap
#let _gap(size: 20) = {}
/// Creates a separator before the next element
/// #examples.gaps-seps
/// #examples.seps
/// - name (content): Name to display in the middle of the separator
#let _sep(name) = {}
#let _sep(name) = {}
/// Creates a delay before the next element
/// #examples.delays
/// - name (content, none): Name to display in the middle of the delay area
/// - size (int): Size of the delay
#let _delay(name: none, size: 30) = {}
/// Creates a gap before the next element
/// #examples.gaps
/// - size (int): Size of the gap
#let _gap(size: 20) = {}

View File

@ -34,6 +34,7 @@
/// - invisible (bool): If set to true, the participant will not be shown
/// - shape (str): The shape of the participant. Possible values in @@SHAPES
/// - color (color): The participant's color
/// - line-stroke (stroke): The participant's line style (defaults to a light gray dashed line)
/// - custom-image (none, image): If shape is 'custom', sets the custom image to display
/// - show-bottom (bool): Whether to display the bottom shape
/// - show-top (bool): Whether to display the top shape
@ -45,6 +46,11 @@
invisible: false,
shape: "participant",
color: rgb("#E2E2F0"),
line-stroke: (
dash: "dashed",
paint: gray.darken(40%),
thickness: .5pt
),
custom-image: none,
show-bottom: true,
show-top: true,
@ -58,10 +64,12 @@
/// - width (auto, int, float, length): Optional fixed width of the column\ If the column's content (e.g. sequence comments) is larger, it will overflow
/// - margin (int, float, length): Additional margin to add to the column\ This margin is not included in `width` and `min-width`, but rather added separately
/// - min-width (int, float, length): Minimum width of the column\ If set to a larger value than `width`, the latter will be overriden
/// - max-width (int, float, length, none): Maximum width of the column\ If set to a lower value than `width`, the latter will be overriden\ If set to `none`, no restriction is applied
#let _col(
p1,
p2,
width: auto,
margin: 0,
min-width: 0
min-width: 0,
max-width: none
) = {}

View File

@ -42,6 +42,17 @@
slant: none
) = {}
/// Creates a return sequence
/// #examples.seq-return
/// - comment (none, content): Optional comment to display along the arrow
#let _ret(comment: none) = {}
/// Accepted values for `comment-align` argument of @@_seq()
/// #examples.seq-comm-align
#let comment-align = (
"start", "end", "left", "center", "right"
)
/// Accepted values for `event` argument of @@_evt()
///
/// `EVENTS = ("create", "destroy", "enable", "disable")`
@ -51,10 +62,4 @@
/// #examples.seq-tips
#let tips = (
"", ">", ">>", "\\", "\\\\", "/", "//", "x", "o",
)
/// Accepted values for `comment-align` argument of @@_seq()
/// #examples.seq-comm-align
#let comment-align = (
"start", "end", "left", "center", "right"
)

View File

@ -1,27 +0,0 @@
#!/bin/bash
echo
echo "Generating gallery PDFs"
set -- ./gallery/*.typ
cnt="$#"
i=1
for f
do
f2="${f/typ/pdf}"
echo "($i/$cnt) $f -> $f2"
typst c --root ./ "$f" "$f2"
i=$((i+1))
done
set -- ./gallery/readme/*.typ
cnt="$#"
i=1
for f
do
f2="${f/typ/png}"
echo "($i/$cnt) $f -> $f2"
typst c --root ./ "$f" "$f2"
i=$((i+1))
done

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 96 KiB

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 22 KiB

12
justfile Normal file
View File

@ -0,0 +1,12 @@
# Local Variables:
# mode: makefile
# End:
gallery_dir := "./gallery"
set shell := ["bash", "-uc"]
manual:
typst c manual.typ manual.pdf
gallery:
for f in "{{gallery_dir}}"/*.typ; do typst c --root . "$f" "${f%typ}pdf"; done
for f in "{{gallery_dir}}"/readme/*.typ; do typst c --root . "$f" "${f%typ}png"; done

Binary file not shown.

View File

@ -1,4 +1,6 @@
#import "@preview/tidy:0.3.0"
#import "@preview/tidy:0.4.2"
#import "@preview/codly:1.2.0": codly-init, codly
#import "@preview/codly-languages:0.1.8": codly-languages
#import "src/lib.typ" as chronos
#import "src/participant.typ" as mod-par
#import "docs/examples.typ"
@ -6,32 +8,48 @@
#let TYPST = image("gallery/typst.png", width: 1.5cm, height: 1.5cm, fit: "contain")
#let doc-ref(target, full: false, var: false) = {
let (module, func) = target.split(".")
let label-name = module + func
let display-name = func
if full {
display-name = target
}
if not var {
label-name += "()"
display-name += "()"
}
link(label(label-name))[#display-name]
}
#show: codly-init
#codly(
languages: codly-languages
)
#set text(font: "Source Sans 3")
#set heading(numbering: (..num) => if num.pos().len() < 4 {
numbering("1.1", ..num)
})
#align(center)[
#v(2cm)
#text(size: 2em)[*Chronos*]
_v#chronos.version;_
#v(1cm)
#chronos.diagram({
import chronos: *
_par("u", display-name: [User], shape: "actor")
_par("wa", display-name: [Web App])
_par("tu", display-name: [Typst Universe], shape: "database")
_seq("u", "wa", comment: [Compile document], enable-dst: true)
_seq("wa", "tu", comment: [Fetch Chronos])
_seq("tu", "wa", dashed: true, slant: 10)
_seq("wa", "wa", comment: [Render])
_ret(comment: [Nice sequence diagram])
})
]
#pagebreak()
#{
outline(indent: true, depth: 3)
outline(indent: auto, depth: 3)
}
#show link: set text(fill: blue)
#set page(numbering: "1/1", header: align(right)[chronos #sym.dash.em v#chronos.version])
#set page(
header: locate(loc => align(left)[chronos #sym.dash.em v#chronos.version]),
footer: locate(loc => align(center, counter(page).display("1/1", both: true)))
header: align(left)[chronos #sym.dash.em v#chronos.version],
footer: context align(center, counter(page).display("1/1", both: true))
)
= Introduction
@ -40,18 +58,20 @@ This package lets you create nice sequence diagrams using the CeTZ package.
= Usage
#let import-stmt = "#import \"@preview/chronos:" + str(chronos.version) + "\""
Simply import #link("https://typst.app/universe/package/chronos/")[chronos] and call the `diagram` function:
#pad(left: 1em)[```typ
#import "@preview/chronos:0.1.0"
#raw(block:true, lang: "typ", ```typ
$import
#chronos.diagram({
import chronos: *
...
})
```]
```.text.replace("$import", import-stmt))
= Examples
You can find the following examples and more in the #link("https://git.kb28.ch/HEL/circuiteria/src/branch/main/gallery")[gallery] directory
You can find the following examples and more in the #link("https://git.kb28.ch/HEL/chronos/src/branch/main/gallery")[gallery] directory
== Some groups and sequences
@ -121,7 +141,11 @@ chronos.diagram({
== Custom images
#example(```
let load-img(path) = image(path, width: 1.5cm, height: 1.5cm, fit:"contain")
let load-img(path) = image(
path,
width: 1.5cm, height: 1.5cm,
fit:"contain"
)
let TYPST = load-img("../gallery/typst.png")
let FERRIS = load-img("../gallery/ferris.png")
let ME = load-img("../gallery/me.jpg")
@ -148,11 +172,11 @@ chronos.diagram({
read("docs/participants.typ"),
name: "Participants",
require-all-parameters: true,
old-syntax: true,
scope: (
chronos: chronos,
mod-par: mod-par,
TYPST: TYPST,
doc-ref: doc-ref
TYPST: TYPST
)
)
#tidy.show-module(par-docs, show-outline: false, sort-functions: none)
@ -163,9 +187,9 @@ chronos.diagram({
read("docs/sequences.typ"),
name: "Sequences",
require-all-parameters: true,
old-syntax: true,
scope: (
chronos: chronos,
doc-ref: doc-ref,
examples: examples
)
)
@ -177,9 +201,9 @@ chronos.diagram({
read("docs/groups.typ"),
name: "Groups",
require-all-parameters: true,
old-syntax: true,
scope: (
chronos: chronos,
doc-ref: doc-ref,
examples: examples
)
)
@ -191,13 +215,13 @@ chronos.diagram({
read("docs/gaps_seps.typ"),
name: "Gaps and separators",
require-all-parameters: true,
old-syntax: true,
scope: (
chronos: chronos,
doc-ref: doc-ref,
examples: examples
)
)
#tidy.show-module(gap-sep-docs, show-outline: false)
#tidy.show-module(gap-sep-docs, show-outline: false, sort-functions: none)
#pagebreak(weak: true)
@ -205,9 +229,9 @@ chronos.diagram({
read("docs/notes.typ"),
name: "Notes",
require-all-parameters: true,
old-syntax: true,
scope: (
chronos: chronos,
doc-ref: doc-ref,
examples: examples
)
)

1
src/cetz.typ Normal file
View File

@ -0,0 +1 @@
#import "@preview/cetz:0.3.4": *

View File

@ -1,119 +1,156 @@
#import "@preview/cetz:0.2.2": canvas, draw
#import "utils.typ": get-participants-i, get-style, normalize-units
#import "group.typ"
#import "participant.typ"
#import "/src/cetz.typ" as cetz: canvas, draw
#import "utils.typ": get-participants-i, get-style, normalize-units, is-elmt
#import "../group.typ"
#import "../participant.typ"
#import participant: PAR-SPECIALS
#import "sequence.typ"
#import "separator.typ"
#import "sync.typ"
#import "consts.typ": *
#import "note.typ" as note: get-note-box
#import "../sequence.typ"
#import "../separator.typ"
#import "../sync.typ"
#import "../consts.typ": *
#import "../note.typ" as note: get-note-box
#let DEBUG-INVISIBLE = false
#let get-columns-width(participants, elements) = {
participants = participants.map(p => {
#let init-lifelines(participants) = {
return participants.map(p => {
p.insert("lifeline-lvl", 0)
p.insert("max-lifelines", 0)
p
})
let pars-i = get-participants-i(participants)
let cells = ()
}
// Unwrap syncs
#let unwrap-syncs(elements) = {
let i = 0
while i < elements.len() {
let elmt = elements.at(i)
if elmt.type == "sync" {
elements = elements.slice(0, i + 1) + elmt.elmts + elements.slice(i + 1)
elements = (
elements.slice(0, i + 1) +
elmt.elmts +
elements.slice(i + 1)
)
}
i += 1
}
return elements
}
// Compute max lifeline levels
#let seq-update-lifelines(participants, pars-i, seq) = {
let participants = participants
let com = if seq.comment == none {""} else {seq.comment}
let i1 = pars-i.at(seq.p1)
let i2 = pars-i.at(seq.p2)
let cell = (
elmt: seq,
i1: calc.min(i1, i2),
i2: calc.max(i1, i2),
cell: box(com, inset: 3pt)
)
if seq.disable-src or seq.destroy-src {
let p = participants.at(i1)
p.lifeline-lvl -= 1
participants.at(i1) = p
}
if seq.disable-dst {
let p = participants.at(i2)
p.lifeline-lvl -= 1
participants.at(i2) = p
}
if seq.enable-dst {
let p = participants.at(i2)
p.lifeline-lvl += 1
p.max-lifelines = calc.max(p.max-lifelines, p.lifeline-lvl)
participants.at(i2) = p
}
return (participants, cell)
}
#let evt-update-lifelines(participants, pars-i, evt) = {
let par-name = evt.participant
let i = pars-i.at(par-name)
let par = participants.at(i)
if evt.event == "disable" or evt.event == "destroy" {
par.lifeline-lvl -= 1
} else if evt.event == "enable" {
par.lifeline-lvl += 1
par.max-lifelines = calc.max(par.max-lifelines, par.lifeline-lvl)
}
participants.at(i) = par
return participants
}
#let note-get-cell(note) = {
let (p1, p2) = (none, none)
let cell = none
if note.side == "left" {
p1 = "["
p2 = note.pos
cell = get-note-box(note)
} else if note.side == "right" {
p1 = note.pos
p2 = "]"
cell = get-note-box(note)
} else if note.side == "over" and note.aligned-with != none {
let box1 = get-note-box(note)
let box2 = get-note-box(note.aligned-with)
let m1 = measure(box1)
let m2 = measure(box2)
cell = box(
width: (m1.width + m2.width) / 2,
height: calc.max(m1.height, m2.height)
)
p1 = note.pos
p2 = note.aligned-with.pos
} else {
return none
}
let i1 = pars-i.at(p1)
let i2 = pars-i.at(p2)
cell = (
elmt: note,
i1: calc.min(i1, i2),
i2: calc.max(i1, i2),
cell: cell
)
return cell
}
#let compute-max-lifeline-levels(participants, elements, pars-i) = {
let cells = ()
for elmt in elements {
if elmt.type == "seq" {
let com = if elmt.comment == none {""} else {elmt.comment}
let i1 = pars-i.at(elmt.p1)
let i2 = pars-i.at(elmt.p2)
cells.push(
(
elmt: elmt,
i1: calc.min(i1, i2),
i2: calc.max(i1, i2),
cell: box(com, inset: 3pt)
)
let cell
(participants, cell) = seq-update-lifelines(
participants,
pars-i,
elmt
)
if elmt.disable-src or elmt.destroy-src {
let p = participants.at(i1)
p.lifeline-lvl -= 1
participants.at(i1) = p
}
if elmt.disable-dst {
let p = participants.at(i2)
p.lifeline-lvl -= 1
participants.at(i2) = p
}
if elmt.enable-dst {
let p = participants.at(i2)
p.lifeline-lvl += 1
p.max-lifelines = calc.max(p.max-lifelines, p.lifeline-lvl)
participants.at(i2) = p
}
cells.push(cell)
} else if elmt.type == "evt" {
let par-name = elmt.participant
let i = pars-i.at(par-name)
let par = participants.at(i)
if elmt.event == "disable" or elmt.event == "destroy" {
par.lifeline-lvl -= 1
} else if elmt.event == "enable" {
par.lifeline-lvl += 1
par.max-lifelines = calc.max(par.max-lifelines, par.lifeline-lvl)
}
participants.at(i) = par
participants = evt-update-lifelines(
participants,
pars-i,
elmt
)
} else if elmt.type == "note" {
let (p1, p2) = (none, none)
let cell = none
if elmt.side == "left" {
p1 = "["
p2 = elmt.pos
cell = get-note-box(elmt)
} else if elmt.side == "right" {
p1 = elmt.pos
p2 = "]"
cell = get-note-box(elmt)
} else if elmt.side == "over" {
if elmt.aligned-with != none {
let box1 = get-note-box(elmt)
let box2 = get-note-box(elmt.aligned-with)
let m1 = measure(box1)
let m2 = measure(box2)
cell = box(width: (m1.width + m2.width) / 2, height: calc.max(m1.height, m2.height))
p1 = elmt.pos
p2 = elmt.aligned-with.pos
}
}
if p1 != none and p2 != none and cell != none {
let i1 = pars-i.at(p1)
let i2 = pars-i.at(p2)
cells.push(
(
elmt: elmt,
i1: calc.min(i1, i2),
i2: calc.max(i1, i2),
cell: cell
)
)
let cell = note-get-cell(elmt)
if cell != none {
cells.push(cell)
}
}
}
// Compute column widths
// Compute minimum widths for participant names and shapes
return (participants, elements, cells)
}
/// Compute minimum widths for participant names and shapes
#let participants-min-col-widths(participants) = {
let widths = ()
for i in range(participants.len() - 1) {
let p1 = participants.at(i)
@ -124,11 +161,15 @@
let w2 = m2.width
widths.push(w1 / 2pt + w2 / 2pt + PAR-SPACE)
}
return widths
}
// Compute minimum width for over notes
for n in elements.filter(e => (e.type == "note" and
e.side == "over" and
type(e.pos) == str)) {
/// Compute minimum width for over notes
#let notes-min-col-widths(elements, widths, pars-i) = {
let widths = widths
let notes = elements.filter(e => e.type == "note")
for n in notes.filter(e => (e.side == "over" and
type(e.pos) == str)) {
let m = note.get-size(n)
let i = pars-i.at(n.pos)
@ -146,8 +187,12 @@
)
}
}
return widths
}
// Compute minimum width for simple sequences (spanning 1 column)
/// Compute minimum width for simple sequences (spanning 1 column)
#let simple-seq-min-col-widths(cells, widths) = {
let widths = widths
for cell in cells.filter(c => c.i2 - c.i1 == 1) {
let m = measure(cell.cell)
widths.at(cell.i1) = calc.max(
@ -155,9 +200,14 @@
m.width / 1pt + COMMENT-PAD
)
}
return widths
}
// Compute minimum width for self sequences
for cell in cells.filter(c => c.elmt.type == "seq" and c.i1 == c.i2) {
/// Compute minimum width for self sequences
#let self-seq-min-col-widths(cells, widths) = {
let widths = widths
for cell in cells.filter(c => (c.elmt.type == "seq" and
c.i1 == c.i2)) {
let m = measure(cell.cell)
let i = cell.i1
if cell.elmt.flip {
@ -170,69 +220,112 @@
)
}
}
return widths
}
// Compute remaining widths for longer sequences (spanning multiple columns)
/// Compute remaining widths for longer sequences (spanning multiple columns)
#let long-seq-min-col-widths(cells, widths) = {
let widths = widths
let multicol-cells = cells.filter(c => c.i2 - c.i1 > 1)
multicol-cells = multicol-cells.sorted(key: c => {
c.i1 * 1000 + c.i2
})
for cell in multicol-cells {
let m = measure(cell.cell)
let width = (
m.width / 1pt +
COMMENT-PAD -
widths.slice(cell.i1, cell.i2 - 1).sum()
)
widths.at(cell.i2 - 1) = calc.max(
widths.at(cell.i2 - 1),
m.width / 1pt + COMMENT-PAD - widths.slice(cell.i1, cell.i2 - 1).sum()
widths.at(cell.i2 - 1), width
)
}
return widths
}
// Add lifeline widths
for (i, w) in widths.enumerate() {
/// Add lifeline widths
#let col-widths-add-lifelines(participants, widths) = {
return widths.enumerate().map(((i, w)) => {
let p1 = participants.at(i)
let p2 = participants.at(i + 1)
let w = w + p1.max-lifelines * LIFELINE-W / 2
w += p1.max-lifelines * LIFELINE-W / 2
if p2.max-lifelines != 0 {
w += LIFELINE-W / 2
}
widths.at(i) = w
}
return w
})
}
for elmt in elements {
if elmt.type == "col" {
let i1 = pars-i.at(elmt.p1)
let i2 = pars-i.at(elmt.p2)
if calc.abs(i1 - i2) != 1 {
let i-min = calc.min(i1, i2)
let i-max = calc.max(i1, i2)
let others = pars-i.pairs()
.sorted(key: p => p.last())
.slice(i-min + 1, i-max)
.map(p => "'" + p.first() + "'")
.join(", ")
panic(
"Column participants must be consecutive (participants (" +
others +
") are in between)"
)
}
let i = calc.min(i1, i2)
if elmt.width != auto {
widths.at(i) = normalize-units(elmt.width)
}
widths.at(i) = calc.max(
widths.at(i),
normalize-units(elmt.min-width)
) + normalize-units(elmt.margin)
#let process-col-elements(elements, widths, pars-i) = {
let widths = widths
let cols = elements.filter(e => e.type == "col")
for col in cols {
let i1 = pars-i.at(col.p1)
let i2 = pars-i.at(col.p2)
if calc.abs(i1 - i2) != 1 {
let i-min = calc.min(i1, i2)
let i-max = calc.max(i1, i2)
let others = pars-i.pairs()
.sorted(key: p => p.last())
.slice(i-min + 1, i-max)
.map(p => "'" + p.first() + "'")
.join(", ")
panic(
"Column participants must be consecutive (participants (" +
others +
") are in between)"
)
}
}
let i = calc.min(i1, i2)
let width = widths.at(i)
if col.width != auto {
width = normalize-units(col.width)
}
width = calc.max(
width,
normalize-units(col.min-width)
)
if col.max-width != none {
width = calc.min(
width,
normalize-units(col.max-width)
)
}
widths.at(i) = width + normalize-units(col.margin)
}
return widths
}
#let compute-columns-width(participants, elements, pars-i) = {
elements = elements.filter(is-elmt)
elements = unwrap-syncs(elements)
let cells
(participants, elements, cells) = compute-max-lifeline-levels(participants, elements, pars-i)
let widths = participants-min-col-widths(participants)
widths = notes-min-col-widths(elements, widths, pars-i)
widths = simple-seq-min-col-widths(cells, widths)
widths = self-seq-min-col-widths(cells, widths)
widths = long-seq-min-col-widths(cells, widths)
widths = col-widths-add-lifelines(participants, widths)
widths = process-col-elements(elements, widths, pars-i)
return widths
}
#let render(participants, elements) = context canvas(length: 1pt, {
let participants = participants
let elements = elements
let shapes = ()
participants = init-lifelines(participants)
let pars-i = get-participants-i(participants)
let widths = get-columns-width(participants, elements)
let widths = compute-columns-width(participants, elements, pars-i)
// Compute each column's X position
let x-pos = (0,)
@ -264,8 +357,11 @@
// Draw elemnts
for elmt in elements {
if not is-elmt(elmt) {
shapes.push(elmt)
// Sequences
if elmt.type == "seq" {
} else if elmt.type == "seq" {
let shps
(y, lifelines, shps) = draw-seq(elmt, y, lifelines)
shapes += shps
@ -429,11 +525,7 @@
draw.line(
(x, last-y),
(x, line.at(1)),
stroke: (
dash: "dashed",
paint: gray.darken(40%),
thickness: .5pt
)
stroke: p.line-stroke
)
}
lines.push(line)
@ -459,11 +551,7 @@
draw.line(
(x, last-y),
(x, line.at(1)),
stroke: (
dash: "dashed",
paint: gray.darken(40%),
thickness: .5pt
)
stroke: p.line-stroke
)
last-y = line.at(1)
} else if event == "delay-end" {
@ -483,11 +571,7 @@
draw.line(
(x, last-y),
(x, y),
stroke: (
dash: "dashed",
paint: gray.darken(40%),
thickness: .5pt
)
stroke: p.line-stroke
)
// Draw lifeline rectangles (reverse for bottom to top)

317
src/core/setup.typ Normal file
View File

@ -0,0 +1,317 @@
#import "utils.typ": is-elmt, get-group-span
#import "../participant.typ": _exists as par-exists, _par
#let flatten-group(elmts, i) = {
let group = elmts.at(i)
elmts.at(i) = group
return (
elmts.slice(0, i + 1) +
group.elmts +
((
type: "grp-end",
start-i: i
),) +
elmts.slice(i+1)
)
}
#let update-group-children(elmts, i) = {
let elmts = elmts
let group-end = elmts.at(i)
elmts.at(group-end.start-i).elmts = elmts.slice(group-end.start-i + 1, i)
return elmts
}
#let convert-return(elmts, i, activation-history) = {
if activation-history.len() == 0 {
panic("Cannot return if no lifeline is activated")
}
let elmts = elmts
let activation-history = activation-history
let ret = elmts.at(i)
let seq = activation-history.pop()
elmts.at(i) = _seq(
seq.p2, seq.p1,
comment: ret.comment,
disable-src: true,
dashed: true
).first()
return (elmts, activation-history)
}
#let unwrap-containers(elmts) = {
let elmts = elmts
let i = 0
let activation-history = ()
// Flatten groups + convert returns
while i < elmts.len() {
let elmt = elmts.at(i)
if not is-elmt(elmt) {
i += 1
continue
}
if elmt.type == "grp" {
elmts = flatten-group(elmts, i)
} else if elmt.type == "seq" {
if elmt.enable-dst {
activation-history.push(elmt)
}
} else if elmt.type == "evt" {
if elmt.event == "enable" {
for elmt2 in elmts.slice(0, i).rev() {
if elmt2.type == "seq" {
activation-history.push(elmt2)
break
}
}
}
} else if elmt.type == "ret" {
(elmts, activation-history) = convert-return(elmts, i, activation-history)
}
i += 1
}
return (elmts, activation-history)
}
#let prepare-seq-participants(ctx, seq) = {
let ctx = ctx
if not par-exists(ctx.participants, seq.p1) {
ctx.participants.push(_par(seq.p1).first())
}
if not par-exists(ctx.participants, seq.p2) {
ctx.participants.push(_par(
seq.p2,
from-start: not seq.create-dst
).first())
} else if seq.create-dst {
let i = ctx.participants.position(p => p.name == seq.p2)
ctx.participants.at(i).from-start = false
}
let p1 = seq.p1
let p2 = seq.p2
if seq.p1 == "?" {
p1 = "?" + seq.p2
}
if seq.p2 == "?" {
p2 = seq.p1 + "?"
}
ctx.linked.push(p1)
ctx.linked.push(p2)
ctx.last-seq = (
elmt: seq,
i: ctx.i,
p1: p1,
p2: p2
)
return ctx
}
#let prepare-note-participants(ctx, note) = {
let ctx = ctx
let note = note
note.insert(
"linked",
note.pos == none and note.side != "across"
)
if note.pos == none and note.side != "across" {
let names = ctx.participants.map(p => p.name)
let i1 = names.position(n => n == ctx.last-seq.p1)
let i2 = names.position(n => n == ctx.last-seq.p2)
let pars = (
(i1, ctx.last-seq.p1),
(i2, ctx.last-seq.p2)
).sorted(key: p => p.first())
if note.side == "left" {
note.pos = pars.first().last()
} else if note.side == "right" {
note.pos = pars.last().last()
}
let seq = last-seq.note
seq.insert("linked-note", note)
ctx.elmts.at(last-seq.i) = seq
}
if note.aligned {
let n = last-note.note
n.aligned-with = note
ctx.elmts.at(last-note.i) = n
}
if note.side == "left" {
ctx.linked.push("[")
} else if note.side == "right" {
ctx.linked.push("]")
}
let pars = none
if type(note.pos) == str {
pars = (note.pos,)
} else if type(note.pos) == array {
pars = note.pos
}
if pars != none {
for par in pars {
if not par-exists(ctx.participants, par) {
participants.push(_par(par).first())
}
}
}
ctx.elmts.at(ctx.i) = note
ctx.last-note = (
elmt: note,
i: ctx.i
)
return ctx
}
#let prepare-evt-participants(ctx, evt) = {
let par = evt.participant
if not par-exists(ctx.participants, par) {
let p = _par(
par,
from-start: evt.event != "create"
).first()
ctx.participants.push(p)
} else if evt.event == "create" {
let i = ctx.participants.position(p => p.name == par)
ctx.participants.at(i).from-start = false
}
return ctx
}
#let normalize-special-participants(elmt) = {
if elmt.p1 == "?" {
elmt.p1 = "?" + elmt.p2
} else if elmt.p2 == "?" {
elmt.p2 = elmt.p1 + "?"
}
return elmt
}
#let prepare-participants(elmts) = {
let ctx = (
linked: (),
last-seq: none,
last-note: none,
participants: (),
elmts: elmts,
i: 0
)
for (i, elmt) in ctx.elmts.enumerate() {
ctx.i = i
if not is-elmt(elmt) {
continue
}
if elmt.type == "par" {
ctx.participants.push(elmt)
} else if elmt.type == "seq" {
ctx = prepare-seq-participants(ctx, elmt)
} else if elmt.type == "note" {
ctx = prepare-note-participants(ctx, elmt)
} else if elmt.type == "evt" {
ctx = prepare-evt-participants(ctx, elmt)
}
}
ctx.linked = ctx.linked.dedup()
let pars = ctx.participants
let participants = ()
if "[" in ctx.linked {
participants.push(_par("[", invisible: true).first())
}
for (i, p) in pars.enumerate() {
let before = _par(
"?" + p.name,
invisible: true
).first()
let after = _par(
p.name + "?",
invisible: true
).first()
if before.name in ctx.linked {
if participants.len() == 0 or not participants.last().name.ends-with("?") {
participants.push(before)
} else {
participants.insert(-1, before)
}
}
participants.push(p)
if after.name in ctx.linked {
participants.push(after)
}
}
if "]" in ctx.linked {
participants.push(_par(
"]",
invisible: true
).first())
}
return (ctx.elmts, participants)
}
#let finalize-setup(elmts, participants) = {
for (i, p) in participants.enumerate() {
p.insert("i", i)
participants.at(i) = p
}
let containers = ()
for (i, elmt) in elmts.enumerate() {
if not is-elmt(elmt) {
continue
}
if elmt.type == "seq" {
elmts.at(i) = normalize-special-participants(elmt)
} else if elmt.type == "grp-end" {
// Put back elements in group because they might have changed
elmts = update-group-children(elmts, i)
} else if elmt.type in ("grp", "alt") {
containers.push(i)
}
}
// Compute groups spans (horizontal)
for i in containers {
let elmt = elmts.at(i)
let (min-i, max-i) = get-group-span(participants, elmt)
elmts.at(i).insert("min-i", min-i)
elmts.at(i).insert("max-i", max-i)
}
return (elmts, participants)
}
#let setup(elements) = {
let (elmts, activation-history) = unwrap-containers(elements)
let participants
(elmts, participants) = prepare-participants(elmts)
return finalize-setup(elmts, participants)
}

View File

@ -1,3 +1,13 @@
#let is-elmt(elmt) = {
if type(elmt) != dictionary {
return false
}
if "type" not in elmt {
return false
}
return true
}
#let normalize-units(value) = {
if type(value) == int or type(value) == float {
return value
@ -36,6 +46,9 @@
max-i = calc.max(max-i, i1)
}
}
if max-i < min-i {
(min-i, max-i) = (max-i, min-i)
}
return (min-i, max-i)
}

View File

@ -1,8 +1,10 @@
#import "utils.typ": get-group-span, fit-canvas
#import "renderer.typ": render
#import "core/utils.typ": fit-canvas
#import "core/renderer.typ": render
#import "participant.typ" as participant: _par, PAR-SPECIALS
#import "sequence.typ": _seq
#import "core/setup.typ": setup
#let _gap(size: 20) = {
return ((
type: "gap",
@ -19,14 +21,15 @@
),)
}
#let _col(p1, p2, width: auto, margin: 0, min-width: 0) = {
#let _col(p1, p2, width: auto, margin: 0, min-width: 0, max-width: none) = {
return ((
type: "col",
p1: p1,
p2: p2,
width: width,
margin: margin,
min-width: min-width
min-width: min-width,
max-width: max-width
),)
}
@ -35,212 +38,8 @@
return
}
let participants = ()
let elmts = elements
let i = 0
let (elmts, participants) = setup(elements)
let activation-history = ()
// Flatten groups + convert returns
while i < elmts.len() {
let elmt = elmts.at(i)
if elmt.type == "grp" {
let grp-elmts = elmt.elmts
elmt.elmts = elmt.elmts.map(e => {
if e.type == "seq" {
if e.p1 == "?" {
e.p1 = "?" + e.p2
} else if e.p2 == "?" {
e.p2 = e.p1 + "?"
}
}
e
})
elmts.at(i) = elmt
elmts = (
elmts.slice(0, i + 1) +
grp-elmts +
((
type: "grp-end"
),) +
elmts.slice(i+1)
)
} else if elmt.type == "seq" {
if elmt.enable-dst {
activation-history.push(elmt)
}
} else if elmt.type == "evt" {
if elmt.event == "enable" {
for elmt2 in elmts.slice(0, i).rev() {
if elmt2.type == "seq" {
activation-history.push(elmt2)
break
}
}
}
} else if elmt.type == "ret" {
if activation-history.len() == 0 {
panic("Cannot return if no lifeline is activated")
}
let seq = activation-history.pop()
elmts.at(i) = _seq(
seq.p2, seq.p1,
comment: elmt.comment,
disable-src: true,
dashed: true
).first()
}
i += 1
}
// List participants
let linked = ()
let last-seq = none
let last-note = none
for (i, elmt) in elmts.enumerate() {
if elmt.type == "par" {
participants.push(elmt)
} else if elmt.type == "seq" {
if not participant._exists(participants, elmt.p1) {
participants.push(_par(elmt.p1).first())
}
if not participant._exists(participants, elmt.p2) {
let par = _par(elmt.p2, from-start: not elmt.create-dst).first()
participants.push(par)
} else if elmt.create-dst {
let i = participants.position(p => p.name == elmt.p2)
participants.at(i).from-start = false
}
let p1 = elmt.p1
let p2 = elmt.p2
if elmt.p1 == "?" {
p1 = "?" + elmt.p2
}
if elmt.p2 == "?" {
p2 = elmt.p1 + "?"
}
linked.push(p1)
linked.push(p2)
last-seq = (
elmt: elmt,
i: i,
p1: p1,
p2: p2
)
} else if elmt.type == "note" {
elmt.insert("linked", elmt.pos == none and elmt.side != "across")
if elmt.pos == none and elmt.side != "across" {
let names = participants.map(p => p.name)
let i1 = names.position(n => n == last-seq.p1)
let i2 = names.position(n => n == last-seq.p2)
let pars = ((i1, last-seq.p1), (i2, last-seq.p2)).sorted(key: p => p.first())
if elmt.side == "left" {
elmt.pos = pars.first().last()
} else if elmt.side == "right" {
elmt.pos = pars.last().last()
}
let seq = last-seq.elmt
seq.insert("linked-note", elmt)
elmts.at(last-seq.i) = seq
}
if elmt.aligned {
let n = last-note.elmt
n.aligned-with = elmt
elmts.at(last-note.i) = n
}
elmts.at(i) = elmt
if elmt.side == "left" {
linked.push("[")
} else if elmt.side == "right" {
linked.push("]")
}
let pars = none
if type(elmt.pos) == str {
pars = (elmt.pos,)
} else if type(elmt.pos) == array {
pars = elmt.pos
}
if pars != none {
for par in pars {
if not participant._exists(participants, par) {
participants.push(_par(par).first())
}
}
}
last-note = (
elmt: elmt,
i: i
)
} else if elmt.type == "evt" {
let par = elmt.participant
if not participant._exists(participants, par) {
let p = _par(par, from-start: elmt.event != "create").first()
participants.push(p)
} else if elmt.event == "create" {
let i = participants.position(p => p.name == par)
participants.at(i).from-start = false
}
}
}
linked = linked.dedup()
let pars = participants
participants = ()
if "[" in linked {
participants.push(_par("[", invisible: true).first())
}
for (i, p) in pars.enumerate() {
let before = _par("?" + p.name, invisible: true).first()
let after = _par(p.name + "?", invisible: true).first()
if before.name in linked {
if participants.len() == 0 or not participants.last().name.ends-with("?") {
participants.push(before)
} else {
participants.insert(-1, before)
}
}
participants.push(p)
if after.name in linked {
participants.push(after)
}
}
if "]" in linked {
participants.push(_par("]", invisible: true).first())
}
// Add index to participant
for (i, p) in participants.enumerate() {
p.insert("i", i)
participants.at(i) = p
}
// Compute groups spans (horizontal)
for (i, elmt) in elmts.enumerate() {
if elmt.type == "grp" or elmt.type == "alt" {
let (min-i, max-i) = get-group-span(participants, elmt)
elmts.at(i).insert("min-i", min-i)
elmts.at(i).insert("max-i", max-i)
} else if elmt.type == "seq" {
if elmt.p1 == "?" {
elmts.at(i).p1 = "?" + elmt.p2
} else if elmt.p2 == "?" {
elmts.at(i).p2 = elmt.p1 + "?"
}
}
}
set text(font: "Source Sans 3")
let canvas = render(participants, elmts)
fit-canvas(canvas, width: width)
}

View File

@ -1,4 +1,4 @@
#import "@preview/cetz:0.2.2": draw
#import "/src/cetz.typ": draw
#import "consts.typ": *
#let _grp(name, desc: none, type: "default", elmts) = {

View File

@ -1,4 +1,4 @@
#let version = version(0, 1, 1)
#let version = version(0, 2, 2)
#import "diagram.typ": diagram, from-plantuml, _gap, _evt, _col
#import "sequence.typ": _seq, _ret

View File

@ -1,4 +1,4 @@
#import "@preview/cetz:0.2.2": draw
#import "/src/cetz.typ": draw
#import "consts.typ": *
#let SIDES = (

View File

@ -1,4 +1,4 @@
#import "@preview/cetz:0.2.2": draw
#import "/src/cetz.typ": draw
#import "consts.typ": *
#let PAR-SPECIALS = "?[]"
@ -22,6 +22,11 @@
invisible: false,
shape: "participant",
color: DEFAULT-COLOR,
line-stroke: (
dash: "dashed",
paint: gray.darken(40%),
thickness: .5pt
),
custom-image: none,
show-bottom: true,
show-top: true,
@ -37,6 +42,7 @@
invisible: invisible,
shape: shape,
color: color,
line-stroke: line-stroke,
custom-image: custom-image,
show-bottom: show-bottom,
show-top: show-top

View File

@ -1,4 +1,4 @@
#import "@preview/cetz:0.2.2": draw
#import "/src/cetz.typ": draw
#import "consts.typ": *
#let _sep(name) = {

View File

@ -1,4 +1,4 @@
#import "@preview/cetz:0.2.2": draw, vector
#import "/src/cetz.typ": draw, vector
#import "consts.typ": *
#import "participant.typ"
#import "note.typ"
@ -100,19 +100,27 @@
y -= Y-SPACE
let i1 = pars-i.at(elmt.p1)
let i2 = pars-i.at(elmt.p2)
let width = calc.abs(x-pos.at(i1) - x-pos.at(i2))
let h = 0
let comment = if elmt.comment == none {none} else {
let w = calc.min(width * 1pt, measure(elmt.comment).width)
box(
width: if i1 == i2 {auto} else {w},
elmt.comment
)
}
// Reserve space for comment
if elmt.comment != none {
h = calc.max(h, measure(box(elmt.comment)).height / 1pt + 6)
if comment != none {
h = calc.max(h, measure(comment).height / 1pt + 6)
}
if "linked-note" in elmt {
h = calc.max(h, note.get-size(elmt.linked-note).height / 2)
}
y -= h
let i1 = pars-i.at(elmt.p1)
let i2 = pars-i.at(elmt.p2)
let start-info = (
i: i1,
x: x-pos.at(i1),
@ -169,7 +177,7 @@
let m = measure(box(par.display-name))
let f = if i1 > i2 {-1} else {1}
end-info.x -= (m.width + PAR-PAD.last() * 2) / 2pt * f
shapes += participant.render(x-pos, par, y: end-info.y - CREATE-OFFSET)
shapes += participant.render(x-pos, par, y: end-info.y)
}
end-info.ll-lvl = lifelines.at(i2).level * LIFELINE-W / 2
@ -244,7 +252,7 @@
(x2, end-info.y)
)
if elmt.comment != none {
if comment != none {
comment-anchor = (
start: if x-mid < x1 {"south-east"} else {"south-west"},
end: if x-mid < x1 {"south-west"} else {"south-east"},
@ -268,7 +276,7 @@
(x2, end-info.y)
)
if elmt.comment != none {
if comment != none {
let start-pt = pts.first()
let end-pt = pts.last()
if elmt.start-tip != "" {
@ -348,27 +356,26 @@
shapes += draw.line(..pts, ..style)
if elmt.comment != none {
if comment != none {
shapes += draw.content(
comment-pt,
elmt.comment,
comment,
anchor: comment-anchor,
angle: comment-angle,
padding: 3pt
)
}
if elmt.create-dst {
let dst-line = lifelines.at(i2)
dst-line.lines.push(("create", end-info.y))
lifelines.at(i2) = dst-line
}
if elmt.enable-dst {
let dst-line = lifelines.at(i2)
dst-line.lines.push(("enable", end-info.y, elmt.lifeline-style))
lifelines.at(i2) = dst-line
}
if elmt.create-dst {
end-info.y -= CREATE-OFFSET
let dst-line = lifelines.at(i2)
dst-line.lines.push(("create", end-info.y))
lifelines.at(i2) = dst-line
}
if "linked-note" in elmt {
let m = note.get-size(elmt.linked-note)

View File

@ -1,7 +1,7 @@
[package]
name = "chronos"
version = "0.1.1"
compiler = "0.11.0"
version = "0.2.2"
compiler = "0.13.1"
repository = "https://git.kb28.ch/HEL/chronos"
entrypoint = "src/lib.typ"
authors = [
@ -11,4 +11,4 @@ categories = ["visualization"]
license = "Apache-2.0"
description = "A package to draw sequence diagrams with CeTZ"
keywords = ["sequence", "diagram", "plantuml"]
exclude = [ "gallery", "gallery.bash", "docs" ]
exclude = [ "gallery", "justfile", "docs" ]