Mastermind is a Typst package, built on CeTZ, to draw UML diagrams, both directly within Typst or by parsing source code files.
Features
- Generate diagrams from source files, supported languages are:
- Java
- C#
- PHP (it can also extract PHP classes from Livewire components!)
- Manual diagrams: Create UML class diagrams directly in your Typst documents, with class names, attributes, and methods.
- Layout: arrange classes in rows or columns and choose where relationships connect to keep diagrams readable.
- Beautiful themes: Pick a built-in visual theme or customize the colors, shapes, and lines to suit your document.
See the /docs directory for the manual and the documentation.
Install
To import the package into your project:
#import "@preview/mastermind:0.3.0"
Usage
Generate diagrams
See the source code
<?php
class Author {
public string $name;
public function __construct(string $name) {
$this->name = $name;
}
}
<?php
class Book {
public string $title;
public Author $author;
public function __construct(string $title, Author $author) {
$this->title = $title;
$this->author = $author;
}
}
<?php
use Livewire\Component;
return new class extends Component {
/** @var array<Book> */
public array $books = [];
public function add(Book $book): void {
$this->books[] = $book;
}
};
?>
<section>
<h2>Book shelf</h2>
@foreach ($books as $book)
<p>{{ $book->title }}</p>
@endforeach
</section>
#import "@preview/mastermind:0.3.0": *
#set page(width: auto, height: auto)
#let sources = (
source(path("php/Author.php"), center: (-9, 0)),
source(path("php/Book.php"), center: (0, 0)),
source(path("php/book-shelf.blade.php"), center: (9, 0)),
)
#draw-source-uml-diagram(sources, themes.blueprint, parsers.php)
Layout
When positioning the classes in the diagram, you can help yourself with:
- row
- column
- group
See the source code
#import "@preview/mastermind:0.3.0": *
#set page(width: auto, height: auto)
#set text(font: "JetBrains Mono")
#let custom-theme = theme(
body-fill: orange,
inset: 10pt,
radius: 20pt,
header-fill: yellow,
line-thickness: 2pt,
stroke: 1pt,
line: "bezier",
)
#let classes = (
class(
"Customer",
center: (0, 0),
attributes: ("+ id: String", "+ email: String"),
methods: ("+ placeOrder()",),
),
class(
"Payment",
center: (8, -6),
type: "abstract",
attributes: ("+ amount: Money",),
methods: ("+ authorize()", "+ capture()"),
),
class(
"Order",
center: (0, -6),
attributes: ("+ id: String", "+ status: OrderStatus"),
methods: ("+ total()", "+ submit()"),
),
class(
"CardPayment",
center: (8, -12),
methods: ("+ authorize()",),
),
class(
"GiftCardPayment",
center: (0, -12),
methods: ("+ authorize()",),
),
class(
"CreditPayment",
center: (16, -12),
methods: ("+ authorize()",),
),
)
#let relationships = (
aggregation("Customer", "Order"),
dependency("Order", "Payment"),
inheritance("Payment", "CardPayment"),
inheritance("Payment", "GiftCardPayment", from-side: "south", to-side: "north"),
inheritance("Payment", "CreditPayment", from-side: "south", to-side: "north"),
)
#draw-uml-diagram(classes, relationships, theme: custom-theme)
Theming
You can also build your own custom theme:
See the source code
#import "@preview/mastermind:0.3.0": *
#set page(width: auto, height: auto)
#set text(font: "JetBrains Mono")
#let custom-theme = theme(
body-fill: orange,
inset: 10pt,
radius: 20pt,
header-fill: yellow,
line-thickness: 2pt,
stroke: 1pt,
line: "bezier",
)
#let classes = (
class(
"Customer",
center: (0, 0),
attributes: ("+ id: String", "+ email: String"),
methods: ("+ placeOrder()",),
),
class(
"Payment",
center: (8, -6),
type: "abstract",
attributes: ("+ amount: Money",),
methods: ("+ authorize()", "+ capture()"),
),
class(
"Order",
center: (0, -6),
attributes: ("+ id: String", "+ status: OrderStatus"),
methods: ("+ total()", "+ submit()"),
),
class(
"CardPayment",
center: (8, -12),
methods: ("+ authorize()",),
),
class(
"GiftCardPayment",
center: (0, -12),
methods: ("+ authorize()",),
),
class(
"CreditPayment",
center: (16, -12),
methods: ("+ authorize()",),
),
)
#let relationships = (
aggregation("Customer", "Order"),
dependency("Order", "Payment"),
inheritance("Payment", "CardPayment"),
inheritance("Payment", "GiftCardPayment", from-side: "south", to-side: "north"),
inheritance("Payment", "CreditPayment", from-side: "south", to-side: "north"),
)
#draw-uml-diagram(classes, relationships, theme: custom-theme)