Some advances of formula supplements base on Janim.
The MyTypst
class extends the functionality of the TypstDoc
class (presumably from the typst
library) by adding methods for advanced indexing and searching capabilities. This class is particularly useful for accessing and manipulating specific parts of a document based on patterns, positions, and other criteria.
This method initializes the MyTypst
object by calling the superclass constructor with the given source
and path
.
-
Parameters:
source
: The source of the document (e.g., file name or content).path
: The path to the source file.**kwargs
: Additional keyword arguments.
-
Behavior:
- Calls the superclass constructor with the provided
source
andpath
.
- Calls the superclass constructor with the provided
This method overloads the indexing operator []
to provide flexible access to different parts of the document.
-
Parameters:
key
: The index or key to access the document.
-
Behavior:
- The method starts by checking the type of the key using a
match
statement. - If the key is an
int
orslice
, it simply calls the superclass's__getitem__
method to handle these basic cases. - If the key is a
list
containing only integers, it maps each integer to a call to the superclass's__getitem__
method and then groups the results into aGroup
object. - If the key is a
list
containing only booleans, it uses the boolean values to select which elements to include in the output. - If the key is a
str
representing a pattern, it calls theslices
method with the pattern and0
as arguments to find the slice corresponding to the first occurrence of the pattern. - If the key is a
tuple
orlist
with astr
followed by anint
, it calls theslices
method with the pattern and the ordinal number to find the slice corresponding to the nth occurrence of the pattern. - If the key is a
tuple
orlist
with astr
followed by alist
ofint
s, it calls theslices
method with the pattern and the list of ordinal numbers to find the slices corresponding to those occurrences of the pattern. - If the key is a
dict
, it flattens the dictionary's values and calls theget
method with the resulting slices. - If the key is a
tuple
orlist
of mixed types (patterns and ordinals), it calls themulti_slices
method to process each pattern and ordinal combination separately and returns aGroup
of the results. - There is special handling for keys that represent "empty" values (like
None
,[]
,()
,""
, etc.), where the entire document is returned unchanged.
- The method starts by checking the type of the key using a
This method finds the slices corresponding to multiple occurrences of a pattern or patterns.
-
Parameters:
patterns
: An iterable of patterns.ordinals
: An iterable of ordinal numbers indicating the nth occurrence of each pattern.
-
Behavior:
- It iterates over the
patterns
andordinals
to find the slices for each pattern and ordinal combination.
- It iterates over the
This method finds the slices corresponding to the occurrence of a pattern.
-
Parameters:
pattern
: The pattern to search for.ordinal
: The ordinal number indicating the nth occurrence of the pattern.
-
Behavior:
- First, check if the pattern is an empty pattern; if so, return a slice of the entire data.
- If pattern is a string, convert it into a MyTypst object.
- Use self.indices(pat) to find all positions where the pattern matches.
- Depending on the type of ordinal, decide how to generate slices:
- If ordinal is a valid integer less than the number of match positions, return a single slice at the specified position.
- If ordinal is unspecified or represents an empty pattern and there is only one match position, return a slice at that position; if there are multiple match positions, return a list of slices for each match.
- If ordinal is a list, generate a slice for each position in the list.
- First, check if the pattern is an empty pattern; if so, return a slice of the entire data.
This method extracts the specified part of the document.
-
Parameters:
slice
: The slice indicating the part of the document to extract.
-
Behavior:
- It uses the provided slice to extract the corresponding part of the document.
- If gapless is False:
- If slices is a slice object, the method returns the data sliced according to this single slice.
- If slices is a sequence of multiple slices, the method iterates over each slice, calling self.get(i) recursively to get the corresponding fragment, and appends these fragments to the fragments list.
- If gapless is True:
- Initialize a set indices with the starting index 0 and the length of the data.
- Iterate over the flattened slices, ensuring each element is a slice type, and update the indices set with the start and stop indices of each slice.
- Sort the indices and create pairs of adjacent indices to form slices.
- Return a list of fragments based on these slices.
The MyTypst
class provides a rich set of features for accessing and manipulating documents. It supports various forms of indexing, including integer indices, slices, boolean masks, and pattern-based indexing. The __getitem__
method is the core of this functionality, allowing users to easily access specific parts of the document or search for patterns within the document. The additional methods (multi_slices
, slices
, get
, and _with_empty
) support the functionality provided by __getitem__
and enable more complex operations on the document.
The class is designed to be user-friendly and versatile, making it suitable for a wide range of applications where precise control over document content is necessary.
This code defines a class TransformInParts
that inherits from AnimGroup
. The class is designed to create animations that transform items between two states, potentially involving intermediate steps like moving or transforming segments of the items. Let's break down the methods and their functionality:
This is the constructor method for TransformInParts
.
-
Parameters:
source
: An iterable collection ofItem
objects representing the initial state.target
: An iterable collection ofItem
objects representing the final state.durations
: An iterable of integers specifying the duration of each animation, or a single integer to be used cyclically.trs_keywords
: An iterable of dictionaries containing keyword arguments for theTransform
animations.**kwargs
: Additional keyword arguments passed to theAnimGroup
constructor.
-
Behavior:
- It processes the
trs_keywords
anddurations
to ensure they are in the correct format (iterables). - If
durations
is provided, it cycles through the durations and assigns them to thetrs_keywords
. - It creates
Transform
animations for each pair of items insource
andtarget
using the processedtrs_keywords
. - Finally, it initializes the
AnimGroup
with the created animations.
- It processes the
This method handles transformations starting from a moved state.
-
Parameters:
source
: The initialItem
objects as a group.target
: The finalItem
objects as a group.trs_keywords
: Keyword arguments for theTransform
animations.move_keywords
: Keyword arguments for theMove
animations.joint_keywords
: Keyword arguments for theSuccession
animations.
-
Behavior:
- It checks that both
source
andtarget
are instances ofGroup
. - It determines whether to perform a move or a direct transform based on the width of the bounding boxes of
source
andtarget
. - It creates
Succession
animations that combine a move and a transform for each pair of items insource
andtarget
. - It returns an
AnimGroup
with the created animations.
- It checks that both
This method handles transformations between segments of items.
-
Parameters:
source
: The initialItem
object.source_segments
: An iterable of segment ranges or a single range for the source item.target
: The finalItem
object orEllipsisType
to indicate that the target segments should match the source segments.target_segments
: An iterable of segment ranges or a single range for the target item, orEllipsisType
to match the source segments.
-
Behavior:
- It processes the
source_segments
andtarget_segments
to ensure they are in the correct format (lists of tuples representing segment ranges). - It creates sub-items for the specified segments in
source
andtarget
. - It returns a new instance of
TransformInParts
initialized with the segmentedsource
andtarget
items.
- It processes the
This method creates animations based on matching patterns between source
and target
.
-
Parameters:
source
: A tuple containing the initialMyTypst
object and a list of patterns.target
: A tuple containing the finalMyTypst
object and a list of patterns.gapless
: A boolean indicating whether the transformations should be gapless.from_moved
: A boolean indicating whether the transformations should start from a moved state.**kwargs
: Additional keyword arguments.
-
Behavior:
- It processes the patterns to ensure they are in the correct format.
- It retrieves the parts of
source
andtarget
that match the patterns. - Depending on the value of
from_moved
, it either createsTransformInParts
directly or calls thefrom_moved
method.
This method creates animations based on transposed patterns between source
and target
.
-
Parameters:
source
: The initialMyTypst
object.target
: The finalMyTypst
object orEllipsisType
to indicate that the target patterns should match the source patterns.source_patterns
: An iterable of patterns for the source.target_patterns
: An iterable of patterns for the target orEllipsisType
to match the source patterns.source_indices
: An iterable of indices for the source patterns orEllipsisType
to indicate cyclic behavior.target_indices
: An iterable of indices for the target patterns orEllipsisType
to indicate cyclic behavior.**anim_kwargs
: Additional keyword arguments for animations.
-
Behavior:
- It processes the input patterns and indices to ensure they are in the correct format.
- It calls the
matching_patterns
method with the processed patterns and indices.
The TransformInParts
class provides a flexible way to create animations that involve transforming items from a source state to a target state, potentially including intermediate steps like moving. The class supports various types of inputs, including patterns and segment ranges, and allows for customization through keyword arguments.