# `GigTypst`
[🔗](https://github.com/giggleton/gig_typst/blob/v0.1.0/lib/gig_typst.ex#L1)

Typst text and maths as labels for Giggly: a plot's title, an axis's or a legend's, set by
[Typst](https://typst.app) and drawn into the plot's SVG.

`text/2` and `math/2` each return a `GigTypst.Content`, which goes wherever Giggly takes a
label another library lays out: any `Giggly.Plot.labs/2` title, a scale's or a secondary axis's
`name:`, a guide's `title:`, a text or label annotation's `label:`, a scale's `labels:`, which
`label_math/2` writes from each break, a facet's strips, through a `labeller:` function such
as `&GigTypst.math/1`, and a text or label layer's rows, through `label_with:`.

    Giggly.Plot.new(table, %{x: :x, y: :y})
    |> Giggly.Plot.geom_point()
    |> Giggly.Plot.labs(
      title: GigTypst.text("Growth of *every* sample"),
      y: GigTypst.math("sigma^2 / n")
    )
    |> Giggly.to_svg()

The theme still decides the title's size, colour, opacity, font family and face, and where it
goes and how it turns; Typst lays out what is inside it. A label's box is its glyphs' own
bounds, so a title can take a fraction of a point more or less room than the same text as a
string; its lines break where its markup breaks them, and it is drawn as Typst's outlines.

## Fonts

The theme's family names a font:

  * `""` and `"sans"` — Liberation Sans, which this package ships, so a label measures as
    Giggly's own text does;
  * `"serif"` — New Computer Modern, and `"mono"` — DejaVu Sans Mono, both shipped with Typst;
  * any other — the font of that name, from the directories `font_dirs:` lists or the machine's
    installed fonts.

Maths is set in New Computer Modern Math unless `math_family:` names another. A family no font
is found for raises, naming it, where Typst would draw in another without a word.

# `label_math`

```elixir
@spec label_math((String.t() -&gt; String.t()), keyword()) :: ([term()] -&gt;
                                                        [
                                                          GigTypst.Content.t()
                                                          | nil
                                                        ])
```

A scale's labels function setting each break as Typst maths: `formula` is handed each break
written as text and returns the formula, which `math/2` lays out.
`labels: GigTypst.label_math(fn text -> "10^" <> text end)` labels the breaks 1, 2 and 3 as
10¹, 10² and 10³.

## Options

  * `:format` — a function handed every break at once, returning each one's text, or `nil` for
    one with no label, as `Scales.Labels`' functions do: `&Scales.Labels.scientific/1`, or
    `fn breaks -> breaks |> Enum.map(&:math.log10/1) |> Scales.Labels.plain() end` to label a
    log axis's breaks by their powers. By default numbers are written by
    `Scales.Labels.plain/1`, as a Giggly axis writes its own, and a discrete scale's strings as
    they are.
  * `:inline`, `:font_dirs` and `:math_family` — as `math/2` takes them.

A missing break, or one `:format` gives no text, has no label, and `formula` is not called
for it.

## Examples

    iex> labeller = GigTypst.label_math(fn text -> "x_" <> text end)
    iex> labeller.([1.0, 2.0]) |> Enum.map(fn content -> content.source end)
    ["x_1", "x_2"]

    iex> labeller = GigTypst.label_math(fn text -> text end)
    iex> labeller.(["alpha", nil]) |> Enum.map(fn content -> content && content.source end)
    ["alpha", nil]

# `math`

```elixir
@spec math(String.t(), keyword()) :: GigTypst.Content.t()
```

A label of Typst maths, as written between `$` signs: `"sigma^2 / n"`, `"sum_(i=1)^n x_i"`.

## Options

  * `:inline` — `true` to set it as maths within a line, smaller and flatter, `false` to set it
    as a formula standing on its own; `false` unless given.
  * `:font_dirs` and `:math_family` — as `text/2` takes them.

## Examples

    iex> GigTypst.math("sigma^2 / n", inline: true)
    %GigTypst.Content{
      source: "sigma^2 / n",
      mode: :inline_math,
      font_dirs: [],
      math_family: "New Computer Modern Math"
    }

# `text`

```elixir
@spec text(String.t(), keyword()) :: GigTypst.Content.t()
```

A label of Typst markup: text, `*strong*` and `_emphasised_`, `$x^2$` maths among it, a line
break with `\`.

## Options

  * `:font_dirs` — directories of font files to look for a family in, beside the machine's
    installed fonts; `[]` unless given. Pass the same list to every label: Typst keeps the
    fonts it scanned for one list at a time.
  * `:math_family` — the font any maths in it is set in, `"New Computer Modern Math"` unless
    given.

## Examples

    iex> GigTypst.text("Growth of *every* sample")
    %GigTypst.Content{
      source: "Growth of *every* sample",
      mode: :markup,
      font_dirs: [],
      math_family: "New Computer Modern Math"
    }

---

*Consult [api-reference.md](api-reference.md) for complete listing*
