AshTypst.Context (ash_typst v0.2.0)

Copy Markdown View Source

Persistent Typst rendering context.

A context wraps a Rust-side SystemWorld that keeps fonts, virtual files, and the compiled document in memory. The typical lifecycle is:

  1. new/1 — create a context (scans fonts, sets root path)
  2. set_markup/2 — load a Typst template
  3. Optionally inject data via set_virtual_file/3, stream_virtual_file/4, or set_inputs/2
  4. compile/1 — compile the markup into a paged document
  5. render_svg/2 or export_pdf/2 — render output from the compiled document

Steps 2-5 can be repeated without re-creating the context. Fonts and virtual files persist until explicitly changed.

Thread safety

Each function acquires internal locks, so a single context can be shared across processes. However, concurrent compile and set_markup calls on the same context will serialize — design your pipeline accordingly.

Summary

Functions

Append a chunk to a virtual file (creates it if new).

Remove a virtual file. Invalidates the compiled document.

Compile the current markup.

Export the document as HTML.

Export the compiled document as a PDF binary.

List font families available in this context.

Create a new context.

Render a page of the compiled document as SVG.

Set a single sys.inputs key/value pair.

Replace all sys.inputs with the given map of string keys/values.

Set the main Typst markup. Invalidates any compiled document.

Set (or overwrite) a virtual file with text content. Invalidates the compiled document.

Set (or overwrite) a virtual file with raw binary content. Invalidates the compiled document.

Stream an Elixir enumerable into a virtual file as a Typst array.

Types

t()

@type t() :: reference()

Functions

append_virtual_file(ctx, path, chunk)

@spec append_virtual_file(t(), String.t(), String.t()) :: :ok

Append a chunk to a virtual file (creates it if new).

Does not invalidate the compiled document — call compile/1 after streaming is complete.

clear_virtual_file(ctx, path)

@spec clear_virtual_file(t(), String.t()) :: :ok

Remove a virtual file. Invalidates the compiled document.

compile(ctx)

@spec compile(t()) ::
  {:ok, AshTypst.CompileResult.t()} | {:error, AshTypst.CompileError.t()}

Compile the current markup.

Returns {:ok, %CompileResult{}} with the page count and warnings, or {:error, %CompileError{}} with diagnostics.

export_html(ctx)

@spec export_html(t()) :: {:ok, String.t()} | {:error, AshTypst.CompileError.t()}

Export the document as HTML.

Performs its own compilation (separate from compile/1).

export_pdf(ctx, opts \\ [])

@spec export_pdf(t(), keyword() | AshTypst.PDFOptions.t()) ::
  {:ok, binary()} | {:error, AshTypst.CompileError.t()}

Export the compiled document as a PDF binary.

Options

  • :pages — page range string like "1-3,5,7-9" (1-indexed)
  • :pdf_standards — list of standards, e.g. [:pdf_a_2b]
  • :document_id — stable identifier for caching

font_families(ctx)

@spec font_families(t()) :: [String.t()]

List font families available in this context.

new(opts \\ [])

@spec new(keyword() | AshTypst.Context.Options.t()) :: {:ok, t()}

Create a new context.

Fonts are scanned once during creation and reused across all operations.

Options

  • :root — root path for template resolution (default ".")
  • :font_paths — additional font directories to search
  • :ignore_system_fonts — skip system fonts (default false)

render_svg(ctx, opts \\ [])

@spec render_svg(
  t(),
  keyword()
) :: {:ok, String.t()} | {:error, AshTypst.CompileError.t()}

Render a page of the compiled document as SVG.

Options

  • :page — zero-indexed page number (default 0)

set_input(ctx, key, value)

@spec set_input(t(), String.t(), String.t()) :: :ok

Set a single sys.inputs key/value pair.

set_inputs(ctx, inputs)

@spec set_inputs(t(), %{required(String.t()) => String.t()}) :: :ok

Replace all sys.inputs with the given map of string keys/values.

set_markup(ctx, markup)

@spec set_markup(t(), String.t()) :: :ok

Set the main Typst markup. Invalidates any compiled document.

set_virtual_file(ctx, path, content)

@spec set_virtual_file(t(), String.t(), String.t()) :: :ok

Set (or overwrite) a virtual file with text content. Invalidates the compiled document.

set_virtual_file_binary(ctx, path, content)

@spec set_virtual_file_binary(t(), String.t(), binary()) :: :ok

Set (or overwrite) a virtual file with raw binary content. Invalidates the compiled document.

Use this for non-text files like images (PNG, SVG) that Typst reads via #image(read("name", encoding: none)).

stream_virtual_file(ctx, path, stream, opts \\ [])

@spec stream_virtual_file(t(), String.t(), Enumerable.t(), keyword()) :: :ok

Stream an Elixir enumerable into a virtual file as a Typst array.

Each element is encoded via AshTypst.Code.encode/2 and batched to Rust for memory efficiency.

Options

  • :variable_name — the #let binding name (default "data")
  • :context — encoding context passed to AshTypst.Code.encode/2
  • :batch_size — records per NIF call (default 100)