typst

class janim.items.svg.typst.TypstDoc(text: str, *, vars: dict[str, TypstVar] | None = None, vars_size_unit: Literal['pt', 'mm', 'cm', 'in'] | None = None, sys_inputs: dict[str, str] = {}, scale: float = 1.0, shared_preamble: str | None = None, additional_preamble: str | None = None, **kwargs)

Bases: SVGItem

Typst document

group_key: str | None = 'data-typst-label'
move_into_position() None
classmethod typstify(obj: TypstPattern) TypstDoc

Convert a string to a Typst object, if it already is one, return it directly

static vars_str(vars: dict[str, TypstVar], unit_or_scale: str | float) tuple[str, dict[str, Points]]
static var_str(var: TypstVar, label: str, unit_or_scale: str | float, mapping: dict[str, Points]) str
static length_str(length: float, unit_or_scale: str | float) str
get_label(name: str) Group[SVGElemItem]
match_pattern(target: TypstDoc, pattern: TypstPattern, ordinal: int = 0, target_ordinal: int | None = None) Self

Match and transform so that the matched parts coincide

For example

t1 = TypstMath('x^2 + y^2')
t2 = TypstMath('x + y')
t2.points.match_pattern(t1, '+')

Will transform t2 so that the plus signs of both coincide

patterns(*patterns: SingleMatchPattern) Group[VItem | BasepointVItem]
patterns(*patterns: MultiMatchPattern) Group[Group[VItem | BasepointVItem]]
patterns(*patterns: SingleMatchPattern | MultiMatchPattern) Group[VItem | BasepointVItem] | Group[Group[VItem | BasepointVItem]]

Get multiple pattern match results at once, returning a Group

e.g. typ.patterns(A, B, ...) is equivalent to Group(typ[A], typ[B], ...)

get(slices, gapless: bool = False)

Get sliced sub-items based on slices

By default, gapless=False:

  • Means directly slicing sub-items through the given slices, for example

    item.get(slice(1, 3)) == item[1:3]

  • Supports using lists to get a batch of sub-items, for example

    item.get([slice(1, 3), slice(4, 7)]) == [item[1:3], item[4:7]]

  • Lists support nesting, and results maintain the original nested structure, for example

    item.get([slice(1, 3), [slice(4, 6), slice(10, 12)]]) == [item[1:3], [item[4:6], item[10:12]]]

If gapless=True:

  • Means splitting all sub-items at all start and end positions of the given slices and returning them together, for example

    item.get(slice(1, 3), gapless=True) == [item[:1], item[1:3], item[3:]]

  • Also supports lists and nested lists, for example

    item.get([slice(1, 3), slice(5, 7)], gapless=True) == [item[:1], item[1:3], item[3:5], item[5:7], item[7:]]

  • Note: In this case, all nested structures will be flattened first before processing

MultiMatchPattern = MultiMatchPattern
SingleMatchPattern = SingleMatchPattern
slice(pattern: TypstPattern, ordinal: int) slice
slice(pattern: TypstPattern, ordinal: Iterable[int] | EllipsisType) list[slice]

Get the slice where the specified pattern matches in shape in this item

  • By default returns the first match (i.e., ordinal=0)

  • Passing other indices to ordinal can get specific parts of subsequent matches

  • Passing an index list to ordinal can get multiple specific parts of matches

  • Passing ellipsis ... to ordinal can get all matching parts

indices(pattern: TypstPattern) list[int]

Find all positions where pattern appears in this formula

  • pattern supports using strings or Typst objects

lps_map: dict[str, list[int]] = {}
property lps: list[int]

Partial match table used in the KMP algorithm

class janim.items.svg.typst.TypstText(text: str, *, shared_preamble: str | None = None, preamble: str | None = None, use_math_environment: bool = False, **kwargs)

Bases: TypstDoc

Typst text

move_into_position() None
class janim.items.svg.typst.TypstMath(text: str, *, shared_preamble: str | None = None, preamble: str | None = None, use_math_environment: bool = True, **kwargs)

Bases: TypstText

Typst math

Equivalent to TypstText with use_math_environment=True