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
sourceandpath.
- 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
matchstatement. - If the key is an
intorslice, it simply calls the superclass's__getitem__method to handle these basic cases. - If the key is a
listcontaining only integers, it maps each integer to a call to the superclass's__getitem__method and then groups the results into aGroupobject. - If the key is a
listcontaining only booleans, it uses the boolean values to select which elements to include in the output. - If the key is a
strrepresenting a pattern, it calls theslicesmethod with the pattern and0as arguments to find the slice corresponding to the first occurrence of the pattern. - If the key is a
tupleorlistwith astrfollowed by anint, it calls theslicesmethod with the pattern and the ordinal number to find the slice corresponding to the nth occurrence of the pattern. - If the key is a
tupleorlistwith astrfollowed by alistofints, it calls theslicesmethod 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 thegetmethod with the resulting slices. - If the key is a
tupleorlistof mixed types (patterns and ordinals), it calls themulti_slicesmethod to process each pattern and ordinal combination separately and returns aGroupof 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
patternsandordinalsto 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 ofItemobjects representing the initial state.target: An iterable collection ofItemobjects 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 theTransformanimations.**kwargs: Additional keyword arguments passed to theAnimGroupconstructor.
-
Behavior:
- It processes the
trs_keywordsanddurationsto ensure they are in the correct format (iterables). - If
durationsis provided, it cycles through the durations and assigns them to thetrs_keywords. - It creates
Transformanimations for each pair of items insourceandtargetusing the processedtrs_keywords. - Finally, it initializes the
AnimGroupwith the created animations.
- It processes the
This method handles transformations starting from a moved state.
-
Parameters:
source: The initialItemobjects as a group.target: The finalItemobjects as a group.trs_keywords: Keyword arguments for theTransformanimations.move_keywords: Keyword arguments for theMoveanimations.joint_keywords: Keyword arguments for theSuccessionanimations.
-
Behavior:
- It checks that both
sourceandtargetare instances ofGroup. - It determines whether to perform a move or a direct transform based on the width of the bounding boxes of
sourceandtarget. - It creates
Successionanimations that combine a move and a transform for each pair of items insourceandtarget. - It returns an
AnimGroupwith the created animations.
- It checks that both
This method handles transformations between segments of items.
-
Parameters:
source: The initialItemobject.source_segments: An iterable of segment ranges or a single range for the source item.target: The finalItemobject orEllipsisTypeto 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, orEllipsisTypeto match the source segments.
-
Behavior:
- It processes the
source_segmentsandtarget_segmentsto ensure they are in the correct format (lists of tuples representing segment ranges). - It creates sub-items for the specified segments in
sourceandtarget. - It returns a new instance of
TransformInPartsinitialized with the segmentedsourceandtargetitems.
- It processes the
This method creates animations based on matching patterns between source and target.
-
Parameters:
source: A tuple containing the initialMyTypstobject and a list of patterns.target: A tuple containing the finalMyTypstobject 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
sourceandtargetthat match the patterns. - Depending on the value of
from_moved, it either createsTransformInPartsdirectly or calls thefrom_movedmethod.
This method creates animations based on transposed patterns between source and target.
-
Parameters:
source: The initialMyTypstobject.target: The finalMyTypstobject orEllipsisTypeto 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 orEllipsisTypeto match the source patterns.source_indices: An iterable of indices for the source patterns orEllipsisTypeto indicate cyclic behavior.target_indices: An iterable of indices for the target patterns orEllipsisTypeto 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_patternsmethod 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.