26 Commits

Author SHA1 Message Date
6bf98ebcb8 Release 0.2.1
Reviewed-on: #9
2025-03-12 13:20:26 +00: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
eb09a23fc1 added delay function 2024-10-08 11:44:53 +02:00
c4f09a0a3e added _ret for return sequences 2024-10-06 22:27:49 +02:00
9275169e8c added auto participant creation for notes 2024-10-06 15:14:34 +02:00
a198708743 fixed o sequence symbol not filled 2024-10-06 15:13:59 +02:00
b66634d44f added support for auto color with _note and _par 2024-10-06 15:12:50 +02:00
3bc103b9d7 updated manual with new group types 2024-10-05 01:52:31 +02:00
feff030510 added alt/else, loops and other group shortcuts 2024-10-04 19:19:17 +02:00
30 changed files with 521 additions and 140 deletions

View File

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

View File

@ -23,6 +23,6 @@
- [ ] PlantUML parser - [ ] PlantUML parser
- [ ] (Message numbering) - [ ] (Message numbering)
- [ ] Mainframes - [ ] Mainframes
- [ ] Different types of groups (alt/loop/etc.) - [x] Different types of groups (alt/loop/etc.)
- [ ] Delays - [ ] Delays
- [ ] Auto-fit in parent - [ ] Auto-fit in parent

View File

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

View File

@ -1,8 +1,23 @@
#import "example.typ": example #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(``` #let seq-comm-align = example(```
_par("p1", display-name: "Start participant") _par("p1",
_par("p2", display-name: "End participant") display-name: "Start participant")
_par("p2",
display-name: "End participant")
let alignments = ( let alignments = (
"start", "end", "start", "end",
"left", "right", "left", "right",
@ -63,6 +78,45 @@ _grp("Group 1", desc: "Description", {
}) })
```) ```)
#let alt = example(```
_par("a", display-name: "Alice")
_par("b", display-name: "Bob")
_alt(
"first encounter", {
_seq("a", "b", comment: "Who are you ?")
_seq("b", "a", comment: "I'm Bob")
},
"know eachother", {
_seq("a", "b", comment: "Hello Bob")
_seq("b", "a", comment: "Hello Alice")
},
"best friends", {
_seq("a", "b", comment: "Hi !")
_seq("b", "a", comment: "Hi !")
}
)
```)
#let loop = example(```
_par("a", display-name: "Alice")
_par("b", display-name: "Bob")
_loop("default loop", {
_seq("a", "b", comment: "Are you here?")
})
_gap()
_loop("min loop", min: 1, {
_seq("a", "b", comment: "Are you here?")
})
_gap()
_loop("min-max loop", min: 1, max: 5, {
_seq("a", "b", comment: "Are you still here?")
})
```)
#let sync = example(``` #let sync = example(```
_par("alice", display-name: "Alice") _par("alice", display-name: "Alice")
_par("bob", display-name: "Bob") _par("bob", display-name: "Bob")
@ -82,15 +136,58 @@ _sync({
}) })
```) ```)
#let gaps-seps = example(``` #let gaps = example(```
_par("alice", display-name: "Alice") _par("a", display-name: "Alice")
_par("bob", display-name: "Bob") _par("b", display-name: "Bob")
_seq("alice", "bob", comment: "Hello") _seq("a", "b", comment: [message 1])
_gap(size: 10) _seq("b", "a", comment: [ok], dashed: true)
_seq("bob", "alice", comment: "Hi") _gap()
_sep("Another day") _seq("a", "b", comment: [message 2])
_seq("alice", "bob", comment: "Hello again") _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(``` #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 /// Creates a separator before the next element
/// #examples.gaps-seps /// #examples.seps
/// - name (content): Name to display in the middle of the separator /// - 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

@ -2,7 +2,7 @@
/// #examples.grp /// #examples.grp
/// - name (content): The group's name /// - name (content): The group's name
/// - desc (none, content): Optional description /// - desc (none, content): Optional description
/// - type (str): The groups's type (unused for the moment) /// - type (str): The groups's type (should only be set through other functions like @@_alt() or @@_loop() )
/// - elmts (array): Elements inside the group (can be sequences, other groups, notes, etc.) /// - elmts (array): Elements inside the group (can be sequences, other groups, notes, etc.)
#let _grp( #let _grp(
name, name,
@ -11,9 +11,48 @@
elmts elmts
) = {} ) = {}
/// Synchronizes multiple sequences /// Creates an alt-else group of sequences
///
/// It contains at least one section but can have as many as needed
/// #examples.alt
/// - desc (content): The alt's label
/// - elmts (array): Elements inside the alt's first section
/// - ..args (content, array): Complementary "else" sections.\ You can add as many else sections as you need by passing a content (else section label) followed by an array of elements (see example)
#let _alt(
desc,
elmts,
..args
)
/// Creates a looped group of sequences
/// #examples.loop
/// - desc (content): Loop description
/// - min (none, number): Optional lower bound of the loop
/// - max (auto, number): Upper bound of the loop. If left as `auto` and `min` is set, it will be infinity (`'*'`)
/// - elmts (array): Elements inside the group
#let _loop(
desc,
min: none,
max: auto,
elmts
) = {}
/// Synchronizes multiple sequences\
/// All elements inside a synchronized group will start at the same time
/// #examples.sync /// #examples.sync
/// - elmts (array): Synchronized elements (generally sequences or notes) /// - elmts (array): Synchronized elements (generally sequences or notes)
#let _sync( #let _sync(
elmts elmts
) )
/// Creates an optional group\
/// This is a simple wrapper around @@_grp()
/// - desc (content): Group description
/// - elmts (array): Elements inside the group
#let _opt(desc, elmts) = {}
/// Creates a break group\
/// This is a simple wrapper around @@_grp()
/// - desc (content): Group description
/// - elmts (array): Elements inside the group
#let _break(desc, elmts) = {}

View File

@ -58,10 +58,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 /// - 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 /// - 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 /// - 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( #let _col(
p1, p1,
p2, p2,
width: auto, width: auto,
margin: 0, margin: 0,
min-width: 0 min-width: 0,
max-width: none
) = {} ) = {}

View File

@ -42,6 +42,17 @@
slant: none 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() /// Accepted values for `event` argument of @@_evt()
/// ///
/// `EVENTS = ("create", "destroy", "enable", "disable")` /// `EVENTS = ("create", "destroy", "enable", "disable")`
@ -52,9 +63,3 @@
#let tips = ( #let tips = (
"", ">", ">>", "\\", "\\\\", "/", "//", "x", "o", "", ">", ">>", "\\", "\\\\", "/", "//", "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.

View File

@ -33,15 +33,38 @@ Alice <-- Bob: Another authentication Response
#chronos.diagram({ #chronos.diagram({
import chronos: * import chronos: *
_seq("Alice", "Bob", comment: "Authentication Request") _seq("Alice", "Bob", comment: "Authentication Request")
_seq("Bob", "Alice", comment: "Authentication Failure")
_grp("My own label", desc: "My own label2", { _alt(
_seq("Alice", "Log", comment: "Log attack start") "successful case", {
_grp("loop", desc: "1000 times", { _seq("Bob", "Alice", comment: "Authentication Accepted")
_seq("Alice", "Bob", comment: "DNS Attack") },
}) "some kind of failure", {
_seq("Alice", "Bob", comment: "Log attack end") _seq("Bob", "Alice", comment: "Authentication Failure")
_grp("My own label", desc: "My own label2", {
_seq("Alice", "Log", comment: "Log attack start")
_loop("1000 times", {
_seq("Alice", "Bob", comment: "DNS Attack")
})
_seq("Alice", "Log", comment: "Log attack end")
})
},
"Another type of failure", {
_seq("Bob", "Alice", comment: "Please repeat")
}
)
})
#chronos.diagram({
import chronos: *
_par("a", display-name: box(width: 1.5em, height: .5em), show-bottom: false)
_par("b", display-name: box(width: 1.5em, height: .5em), show-bottom: false)
_col("a", "b", width: 2cm)
_loop("a<1", min: 1, {
_seq("a", "b", end-tip: ">>")
_seq("b", "a", end-tip: ">>")
}) })
_seq("a", "b", end-tip: ">>")
}) })
#chronos.diagram({ #chronos.diagram({
@ -55,6 +78,15 @@ Alice <-- Bob: Another authentication Response
_seq("Bob", "Alice", comment: "another authentication Response", dashed: true) _seq("Bob", "Alice", comment: "another authentication Response", dashed: true)
}) })
#chronos.diagram({
import chronos: *
_seq("Alice", "Bob", comment: "Authentication Request")
_delay()
_seq("Bob", "Alice", comment: "Authentication Response")
_delay(name: "5 minutes later")
_seq("Bob", "Alice", comment: "Good Bye !")
})
#chronos.diagram({ #chronos.diagram({
import chronos: * import chronos: *
_seq("Alice", "Bob", comment: "message 1") _seq("Alice", "Bob", comment: "message 1")

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/lib.typ" as chronos
#import "src/participant.typ" as mod-par #import "src/participant.typ" as mod-par
#import "docs/examples.typ" #import "docs/examples.typ"
@ -6,32 +8,48 @@
#let TYPST = image("gallery/typst.png", width: 1.5cm, height: 1.5cm, fit: "contain") #let TYPST = image("gallery/typst.png", width: 1.5cm, height: 1.5cm, fit: "contain")
#let doc-ref(target, full: false, var: false) = { #show: codly-init
let (module, func) = target.split(".") #codly(
let label-name = module + func languages: codly-languages
let display-name = func )
if full {
display-name = target #set text(font: "Source Sans 3")
}
if not var {
label-name += "()"
display-name += "()"
}
link(label(label-name))[#display-name]
}
#set heading(numbering: (..num) => if num.pos().len() < 4 { #set heading(numbering: (..num) => if num.pos().len() < 4 {
numbering("1.1", ..num) 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) #show link: set text(fill: blue)
#set page(numbering: "1/1", header: align(right)[chronos #sym.dash.em v#chronos.version]) #set page(numbering: "1/1", header: align(right)[chronos #sym.dash.em v#chronos.version])
#set page( #set page(
header: locate(loc => align(left)[chronos #sym.dash.em v#chronos.version]), header: align(left)[chronos #sym.dash.em v#chronos.version],
footer: locate(loc => align(center, counter(page).display("1/1", both: true))) footer: context align(center, counter(page).display("1/1", both: true))
) )
= Introduction = Introduction
@ -40,14 +58,16 @@ This package lets you create nice sequence diagrams using the CeTZ package.
= Usage = 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: Simply import #link("https://typst.app/universe/package/chronos/")[chronos] and call the `diagram` function:
#pad(left: 1em)[```typ #raw(block:true, lang: "typ", ```typ
#import "@preview/chronos:0.1.0" $import
#chronos.diagram({ #chronos.diagram({
import chronos: * import chronos: *
... ...
}) })
```] ```.text.replace("$import", import-stmt))
= Examples = Examples
@ -121,7 +141,11 @@ chronos.diagram({
== Custom images == Custom images
#example(``` #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 TYPST = load-img("../gallery/typst.png")
let FERRIS = load-img("../gallery/ferris.png") let FERRIS = load-img("../gallery/ferris.png")
let ME = load-img("../gallery/me.jpg") let ME = load-img("../gallery/me.jpg")
@ -148,11 +172,11 @@ chronos.diagram({
read("docs/participants.typ"), read("docs/participants.typ"),
name: "Participants", name: "Participants",
require-all-parameters: true, require-all-parameters: true,
old-syntax: true,
scope: ( scope: (
chronos: chronos, chronos: chronos,
mod-par: mod-par, mod-par: mod-par,
TYPST: TYPST, TYPST: TYPST
doc-ref: doc-ref
) )
) )
#tidy.show-module(par-docs, show-outline: false, sort-functions: none) #tidy.show-module(par-docs, show-outline: false, sort-functions: none)
@ -163,9 +187,9 @@ chronos.diagram({
read("docs/sequences.typ"), read("docs/sequences.typ"),
name: "Sequences", name: "Sequences",
require-all-parameters: true, require-all-parameters: true,
old-syntax: true,
scope: ( scope: (
chronos: chronos, chronos: chronos,
doc-ref: doc-ref,
examples: examples examples: examples
) )
) )
@ -177,13 +201,13 @@ chronos.diagram({
read("docs/groups.typ"), read("docs/groups.typ"),
name: "Groups", name: "Groups",
require-all-parameters: true, require-all-parameters: true,
old-syntax: true,
scope: ( scope: (
chronos: chronos, chronos: chronos,
doc-ref: doc-ref,
examples: examples examples: examples
) )
) )
#tidy.show-module(grp-docs, show-outline: false) #tidy.show-module(grp-docs, show-outline: false, sort-functions: none)
#pagebreak(weak: true) #pagebreak(weak: true)
@ -191,13 +215,13 @@ chronos.diagram({
read("docs/gaps_seps.typ"), read("docs/gaps_seps.typ"),
name: "Gaps and separators", name: "Gaps and separators",
require-all-parameters: true, require-all-parameters: true,
old-syntax: true,
scope: ( scope: (
chronos: chronos, chronos: chronos,
doc-ref: doc-ref,
examples: examples 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) #pagebreak(weak: true)
@ -205,9 +229,9 @@ chronos.diagram({
read("docs/notes.typ"), read("docs/notes.typ"),
name: "Notes", name: "Notes",
require-all-parameters: true, require-all-parameters: true,
old-syntax: true,
scope: ( scope: (
chronos: chronos, chronos: chronos,
doc-ref: doc-ref,
examples: examples examples: examples
) )
) )

View File

@ -1,6 +1,7 @@
#import "utils.typ": get-group-span, fit-canvas #import "utils.typ": get-group-span, fit-canvas
#import "renderer.typ": render #import "renderer.typ": render
#import "participant.typ" as participant: _par, PAR-SPECIALS #import "participant.typ" as participant: _par, PAR-SPECIALS
#import "sequence.typ": _seq
#let _gap(size: 20) = { #let _gap(size: 20) = {
return (( return ((
@ -18,14 +19,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 (( return ((
type: "col", type: "col",
p1: p1, p1: p1,
p2: p2, p2: p2,
width: width, width: width,
margin: margin, margin: margin,
min-width: min-width min-width: min-width,
max-width: max-width
),) ),)
} }
@ -38,11 +40,12 @@
let elmts = elements let elmts = elements
let i = 0 let i = 0
// Flatten groups let activation-history = ()
// Flatten groups + convert returns
while i < elmts.len() { while i < elmts.len() {
let elmt = elmts.at(i) let elmt = elmts.at(i)
if elmt.type == "grp" { if elmt.type == "grp" {
let grp-elmts = elmt.elmts
elmt.elmts = elmt.elmts.map(e => { elmt.elmts = elmt.elmts.map(e => {
if e.type == "seq" { if e.type == "seq" {
if e.p1 == "?" { if e.p1 == "?" {
@ -56,12 +59,41 @@
elmts.at(i) = elmt elmts.at(i) = elmt
elmts = ( elmts = (
elmts.slice(0, i + 1) + elmts.slice(0, i + 1) +
grp-elmts + elmt.elmts +
(( ((
type: "grp-end" type: "grp-end",
start-i: i
),) + ),) +
elmts.slice(i+1) elmts.slice(i+1)
) )
} else if elmt.type == "grp-end" {
// Put back elements in group because they might have changed
elmts.at(elmt.start-i).elmts = elmts.slice(elmt.start-i + 1, 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" {
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 i += 1
} }
@ -130,6 +162,21 @@
} else if elmt.side == "right" { } else if elmt.side == "right" {
linked.push("]") 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 = ( last-note = (
elmt: elmt, elmt: elmt,
i: i i: i
@ -185,7 +232,7 @@
// Compute groups spans (horizontal) // Compute groups spans (horizontal)
for (i, elmt) in elmts.enumerate() { for (i, elmt) in elmts.enumerate() {
if elmt.type == "grp" { if elmt.type == "grp" or elmt.type == "alt" {
let (min-i, max-i) = get-group-span(participants, elmt) let (min-i, max-i) = get-group-span(participants, elmt)
elmts.at(i).insert("min-i", min-i) elmts.at(i).insert("min-i", min-i)
elmts.at(i).insert("max-i", max-i) elmts.at(i).insert("max-i", max-i)
@ -198,7 +245,6 @@
} }
} }
set text(font: "Source Sans 3")
let canvas = render(participants, elmts) let canvas = render(participants, elmts)
fit-canvas(canvas, width: width) 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": * #import "consts.typ": *
#let _grp(name, desc: none, type: "default", elmts) = { #let _grp(name, desc: none, type: "default", elmts) = {
@ -11,6 +11,36 @@
),) ),)
} }
#let _alt(desc, elmts, ..args) = {
let all-elmts = ()
all-elmts += elmts
let args = args.pos()
for i in range(0, args.len(), step: 2) {
let else-desc = args.at(i)
let else-elmts = args.at(i + 1, default: ())
all-elmts.push((
type: "else",
desc: else-desc
))
all-elmts += else-elmts
}
return _grp("alt", desc: desc, type: "alt", all-elmts)
}
#let _loop(desc, min: none, max: auto, elmts) = {
let name = "loop"
if min != none {
if max == auto {
max = "*"
}
name += "(" + str(min) + "," + str(max) + ")"
}
_grp(name, desc: desc, type: "loop", elmts)
}
#let _opt(desc, elmts) = grp("opt", desc: desc, type: "opt", elmts)
#let _break(desc, elmts) = grp("break", desc: desc, type: "break", elmts)
#let render(x0, x1, y0, y1, group) = { #let render(x0, x1, y0, y1, group) = {
let shapes = () let shapes = ()
let name = text(group.name, weight: "bold") let name = text(group.name, weight: "bold")
@ -52,3 +82,18 @@
return shapes return shapes
} }
#let render-else(x0, x1, y, elmt) = {
let shapes = draw.line(
(x0, y),
(x1, y),
stroke: (dash: (2pt, 1pt), thickness: .5pt)
)
shapes += draw.content(
(x0, y),
text([\[#elmt.desc\]], weight: "bold", size: .8em),
anchor: "north-west",
padding: 3pt
)
return shapes
}

View File

@ -1,9 +1,9 @@
#let version = version(0, 1, 1) #let version = version(0, 2, 1)
#import "diagram.typ": diagram, from-plantuml, _gap, _evt, _col #import "diagram.typ": diagram, from-plantuml, _gap, _evt, _col
#import "sequence.typ": _seq #import "sequence.typ": _seq, _ret
#import "group.typ": _grp #import "group.typ": _grp, _loop, _alt, _opt, _break
#import "participant.typ": _par #import "participant.typ": _par
#import "separator.typ": _sep #import "separator.typ": _sep, _delay
#import "note.typ": _note #import "note.typ": _note
#import "sync.typ": _sync #import "sync.typ": _sync

View File

@ -1,4 +1,4 @@
#import "@preview/cetz:0.2.2": draw #import "/src/cetz.typ": draw
#import "consts.typ": * #import "consts.typ": *
#let SIDES = ( #let SIDES = (
@ -25,6 +25,9 @@
panic("Aligned notes can only be over a participant (got side '" + side + "')") panic("Aligned notes can only be over a participant (got side '" + side + "')")
} }
} }
if color == auto {
color = COL-NOTE
}
return (( return ((
type: "note", type: "note",
side: side, side: side,

View File

@ -1,4 +1,4 @@
#import "@preview/cetz:0.2.2": draw #import "/src/cetz.typ": draw
#import "consts.typ": * #import "consts.typ": *
#let PAR-SPECIALS = "?[]" #let PAR-SPECIALS = "?[]"
@ -13,6 +13,7 @@
"queue", "queue",
"custom" "custom"
) )
#let DEFAULT-COLOR = rgb("#E2E2F0")
#let _par( #let _par(
name, name,
@ -20,11 +21,14 @@
from-start: true, from-start: true,
invisible: false, invisible: false,
shape: "participant", shape: "participant",
color: rgb("#E2E2F0"), color: DEFAULT-COLOR,
custom-image: none, custom-image: none,
show-bottom: true, show-bottom: true,
show-top: true, show-top: true,
) = { ) = {
if color == auto {
color = DEFAULT-COLOR
}
return (( return ((
type: "par", type: "par",
name: name, name: name,

View File

@ -1,4 +1,4 @@
#import "@preview/cetz:0.2.2": canvas, draw #import "/src/cetz.typ": canvas, draw
#import "utils.typ": get-participants-i, get-style, normalize-units #import "utils.typ": get-participants-i, get-style, normalize-units
#import "group.typ" #import "group.typ"
#import "participant.typ" #import "participant.typ"
@ -218,10 +218,13 @@
if elmt.width != auto { if elmt.width != auto {
widths.at(i) = normalize-units(elmt.width) widths.at(i) = normalize-units(elmt.width)
} }
widths.at(i) = calc.max(
widths.at(i), let width = widths.at(i)
normalize-units(elmt.min-width) width = calc.max(width, normalize-units(elmt.min-width))
) + normalize-units(elmt.margin) if elmt.max-width != none {
width = calc.min(width, normalize-units(elmt.max-width))
}
widths.at(i) = width + normalize-units(elmt.margin)
} }
} }
@ -242,6 +245,7 @@
let draw-seq = sequence.render.with(pars-i, x-pos, participants) let draw-seq = sequence.render.with(pars-i, x-pos, participants)
let draw-group = group.render.with() let draw-group = group.render.with()
let draw-else = group.render-else.with()
let draw-sep = separator.render.with(x-pos) let draw-sep = separator.render.with(x-pos)
let draw-par = participant.render.with(x-pos) let draw-par = participant.render.with(x-pos)
let draw-note = note.render.with(pars-i, x-pos) let draw-note = note.render.with(pars-i, x-pos)
@ -283,6 +287,9 @@
if g.at(1).max-i == elmt.max-i { g.at(3) += 1 } if g.at(1).max-i == elmt.max-i { g.at(3) += 1 }
g g
}) })
if elmt.grp-type == "alt" {
elmt.insert("elses", ())
}
groups.push((y, elmt, 0, 0)) groups.push((y, elmt, 0, 0))
y -= m.height / 1pt y -= m.height / 1pt
@ -294,6 +301,21 @@
let x1 = x-pos.at(group.max-i) + end-lvl * 10 + 20 let x1 = x-pos.at(group.max-i) + end-lvl * 10 + 20
shapes += draw-group(x0, x1, start-y, y, group) shapes += draw-group(x0, x1, start-y, y, group)
if group.grp-type == "alt" {
for (else-y, else-elmt) in group.elses {
shapes += draw-else(x0, x1, else-y, else-elmt)
}
}
// Alt's elses -> reserve space for label + store position
} else if elmt.type == "else" {
y -= Y-SPACE
let m = measure(text([\[#elmt.desc\]], weight: "bold", size: .8em))
groups.last().at(1).elses.push((
y, elmt
))
y -= m.height / 1pt
// Separator // Separator
} else if elmt.type == "sep" { } else if elmt.type == "sep" {
let shps let shps
@ -304,6 +326,26 @@
} else if elmt.type == "gap" { } else if elmt.type == "gap" {
y -= elmt.size y -= elmt.size
// Delay
} else if elmt.type == "delay" {
let y0 = y
let y1 = y - elmt.size
for (i, line) in lifelines.enumerate() {
line.lines.push(("delay-start", y0))
line.lines.push(("delay-end", y1))
lifelines.at(i) = line
}
if elmt.name != none {
let x0 = x-pos.first()
let x1 = x-pos.last()
shapes += draw.content(
((x0 + x1) / 2, (y0 + y1) / 2),
anchor: "center",
elmt.name
)
}
y = y1
// Event // Event
} else if elmt.type == "evt" { } else if elmt.type == "evt" {
let par-name = elmt.participant let par-name = elmt.participant
@ -416,6 +458,28 @@
if event == "destroy" { if event == "destroy" {
destructions.push((x + lvl * LIFELINE-W / 2, line.at(1))) destructions.push((x + lvl * LIFELINE-W / 2, line.at(1)))
} }
} else if event == "delay-start" {
draw.line(
(x, last-y),
(x, line.at(1)),
stroke: (
dash: "dashed",
paint: gray.darken(40%),
thickness: .5pt
)
)
last-y = line.at(1)
} else if event == "delay-end" {
draw.line(
(x, last-y),
(x, line.at(1)),
stroke: (
dash: "loosely-dotted",
paint: gray.darken(40%),
thickness: .8pt
)
)
last-y = line.at(1)
} }
} }

View File

@ -1,4 +1,4 @@
#import "@preview/cetz:0.2.2": draw #import "/src/cetz.typ": draw
#import "consts.typ": * #import "consts.typ": *
#let _sep(name) = { #let _sep(name) = {
@ -8,6 +8,14 @@
),) ),)
} }
#let _delay(name: none, size: 30) = {
return ((
type: "delay",
name: name,
size: size
),)
}
#let render(x-pos, elmt, y) = { #let render(x-pos, elmt, y) = {
let shapes = () let shapes = ()
y -= Y-SPACE y -= Y-SPACE

View File

@ -1,9 +1,12 @@
#import "@preview/cetz:0.2.2": draw, vector #import "/src/cetz.typ": draw, vector
#import "consts.typ": * #import "consts.typ": *
#import "participant.typ" #import "participant.typ"
#import "note.typ" #import "note.typ"
#let get-arrow-marks(sym, color) = { #let get-arrow-marks(sym, color) = {
if sym == none {
return none
}
if type(sym) == array { if type(sym) == array {
return sym.map(s => get-arrow-marks(s, color)) return sym.map(s => get-arrow-marks(s, color))
} }
@ -85,24 +88,39 @@
),) ),)
} }
#let _ret(comment: none) = {
return ((
type: "ret",
comment: comment
),)
}
#let render(pars-i, x-pos, participants, elmt, y, lifelines) = { #let render(pars-i, x-pos, participants, elmt, y, lifelines) = {
let shapes = () let shapes = ()
y -= Y-SPACE 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 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 // Reserve space for comment
if elmt.comment != none { if comment != none {
h = calc.max(h, measure(box(elmt.comment)).height / 1pt + 6) h = calc.max(h, measure(comment).height / 1pt + 6)
} }
if "linked-note" in elmt { if "linked-note" in elmt {
h = calc.max(h, note.get-size(elmt.linked-note).height / 2) h = calc.max(h, note.get-size(elmt.linked-note).height / 2)
} }
y -= h y -= h
let i1 = pars-i.at(elmt.p1)
let i2 = pars-i.at(elmt.p2)
let start-info = ( let start-info = (
i: i1, i: i1,
x: x-pos.at(i1), x: x-pos.at(i1),
@ -159,7 +177,7 @@
let m = measure(box(par.display-name)) let m = measure(box(par.display-name))
let f = if i1 > i2 {-1} else {1} let f = if i1 > i2 {-1} else {1}
end-info.x -= (m.width + PAR-PAD.last() * 2) / 2pt * f 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 end-info.ll-lvl = lifelines.at(i2).level * LIFELINE-W / 2
@ -234,7 +252,7 @@
(x2, end-info.y) (x2, end-info.y)
) )
if elmt.comment != none { if comment != none {
comment-anchor = ( comment-anchor = (
start: if x-mid < x1 {"south-east"} else {"south-west"}, start: if x-mid < x1 {"south-east"} else {"south-west"},
end: if x-mid < x1 {"south-west"} else {"south-east"}, end: if x-mid < x1 {"south-west"} else {"south-east"},
@ -258,7 +276,7 @@
(x2, end-info.y) (x2, end-info.y)
) )
if elmt.comment != none { if comment != none {
let start-pt = pts.first() let start-pt = pts.first()
let end-pt = pts.last() let end-pt = pts.last()
if elmt.start-tip != "" { if elmt.start-tip != "" {
@ -294,7 +312,7 @@
// Start circle tip // Start circle tip
if is-circle-tip(elmt.start-tip) { if is-circle-tip(elmt.start-tip) {
shapes += draw.circle(pts.first(), radius: CIRCLE-TIP-RADIUS, stroke: elmt.color, fill: none, name: "_circle-start-tip") shapes += draw.circle(pts.first(), radius: CIRCLE-TIP-RADIUS, stroke: none, fill: elmt.color, name: "_circle-start-tip")
pts.at(0) = "_circle-start-tip" pts.at(0) = "_circle-start-tip"
// Start cross tip // Start cross tip
@ -316,7 +334,7 @@
// End circle tip // End circle tip
if is-circle-tip(elmt.end-tip) { if is-circle-tip(elmt.end-tip) {
shapes += draw.circle(pts.last(), radius: 3, stroke: elmt.color, fill: none, name: "_circle-end-tip") shapes += draw.circle(pts.last(), radius: 3, stroke: none, fill: elmt.color, name: "_circle-end-tip")
pts.at(pts.len() - 1) = "_circle-end-tip" pts.at(pts.len() - 1) = "_circle-end-tip"
// End cross tip // End cross tip
@ -338,27 +356,26 @@
shapes += draw.line(..pts, ..style) shapes += draw.line(..pts, ..style)
if elmt.comment != none { if comment != none {
shapes += draw.content( shapes += draw.content(
comment-pt, comment-pt,
elmt.comment, comment,
anchor: comment-anchor, anchor: comment-anchor,
angle: comment-angle, angle: comment-angle,
padding: 3pt 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 { if elmt.enable-dst {
let dst-line = lifelines.at(i2) let dst-line = lifelines.at(i2)
dst-line.lines.push(("enable", end-info.y, elmt.lifeline-style)) dst-line.lines.push(("enable", end-info.y, elmt.lifeline-style))
lifelines.at(i2) = dst-line 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 { if "linked-note" in elmt {
let m = note.get-size(elmt.linked-note) let m = note.get-size(elmt.linked-note)

View File

@ -36,6 +36,9 @@
max-i = calc.max(max-i, i1) 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) return (min-i, max-i)
} }

View File

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