-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Rewrite MathTex to make it more robust regarding splitting #4515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
henrikmidtiby
wants to merge
57
commits into
ManimCommunity:main
Choose a base branch
from
henrikmidtiby:MathTexFromScratch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+226
−140
Open
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
b88b358
Extracted the method get_mob_from_shape_element
henrikmidtiby e8e003a
Moved more functionality to get_mob_from_shape_element
henrikmidtiby f9f7138
More cleanup
henrikmidtiby 1a9212b
Parse the svg file while maintaining the group structure.
henrikmidtiby bd78c43
Make the svg groups available
henrikmidtiby 494708e
Merge branch 'main' into maintain_groups_from_svg
henrikmidtiby 9e364e3
Handle PERF401 issue
henrikmidtiby 9059479
[pre-commit.ci] pre-commit autoupdate (#4506)
pre-commit-ci[bot] 66f456e
Added an example of the issue
henrikmidtiby d6c6f5d
Experimenting with coloring elements from the latex equation
henrikmidtiby 96db1b0
...
henrikmidtiby fe411af
Regular expression can now match more than one object
henrikmidtiby 68395df
Process the string by applying the substrings in the order they match
henrikmidtiby 4499c73
Code refactoring and added type annotations
henrikmidtiby 751c718
...
henrikmidtiby 73a08e4
Added a lot of test cases
henrikmidtiby b9e313e
More examples
henrikmidtiby 63a4c19
More examples
henrikmidtiby a651d95
Use matched_strings_and_ids to simplify existing methods
henrikmidtiby b8c9123
Remove unused code
henrikmidtiby 86a1b46
Update get_part_by_tex to use matched_strings_and_ids
henrikmidtiby 1c2d113
This is required for test_MathTable to pass
henrikmidtiby d9bc03f
Ensure that self.texstring is set.
henrikmidtiby 64894a7
Merge branch 'main' into MathTexFromScratch
henrikmidtiby d12bde7
Added more examples from exising issues in the github repo
henrikmidtiby 63f786f
Ensure that latex groups are maintained by adding an additional pair …
henrikmidtiby d8a4b43
ExampleScene -> Scene
henrikmidtiby bb41f5c
Added comment
henrikmidtiby c9b6c52
_break_up_by_substrings
henrikmidtiby 0afc9b8
Refactor code
henrikmidtiby e2f3203
Added comment to example
henrikmidtiby f0a4bf9
Handle integer inputs well.
henrikmidtiby 3fd3adc
Expose the original tex_string
henrikmidtiby f06991e
Do not treat the content of substrings_to_isolate as regular expressi…
henrikmidtiby c11dcc3
Updated examples
henrikmidtiby c11799a
Update examples
henrikmidtiby ada86f1
Fix SVMobject caching issue.
henrikmidtiby 919bfee
Remove traces from brace_notation_split_occurred
henrikmidtiby b7d1c83
Simplify MathTex::_break_up_by_substrings
henrikmidtiby ca6f0c1
Fix small issue in tex that in some cases moved elements a tiny bit a…
henrikmidtiby d7ddee8
No use of regular expressions for locate substrings.
henrikmidtiby 654f877
Updated notes to the set of test cases
henrikmidtiby ee93305
Handle issues with the center environment.
henrikmidtiby 01e1f25
Add example
henrikmidtiby 084745d
Fix issue with rectangles (e.g. from sqrt)
henrikmidtiby e4d19cb
ConvertToOpenGL
henrikmidtiby 3b7da3b
Reduce the number of nesting levels.
henrikmidtiby d2ea3f9
Use the specified arg_seperator
henrikmidtiby e98c514
Deal with the double curly brace markup
henrikmidtiby 2d6bfee
Code cleanup
henrikmidtiby 3e99987
Code cleanup
henrikmidtiby ba58cd0
Rollback a few changes
henrikmidtiby 4025934
Code cleanup
henrikmidtiby 5b69c36
Merge branch 'main' into MathTexFromScratch
henrikmidtiby e928a0c
Adjust paths the generated artefacts in tests that rely on MathTex
henrikmidtiby bbdd0bb
Added a remark to the using text guide on enclosing snippets in curly…
henrikmidtiby 800cfa6
Added space around the numerator argument to frac to avoid having dou…
henrikmidtiby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,12 +21,12 @@ | |
| from ..geometry.line import Line | ||
| from ..geometry.polygram import Polygon, Rectangle, RoundedRectangle | ||
| from ..opengl.opengl_compatibility import ConvertToOpenGL | ||
| from ..types.vectorized_mobject import VMobject | ||
| from ..types.vectorized_mobject import VGroup, VMobject | ||
|
|
||
| __all__ = ["SVGMobject", "VMobjectFromSVGPath"] | ||
|
|
||
|
|
||
| SVG_HASH_TO_MOB_MAP: dict[int, VMobject] = {} | ||
| SVG_HASH_TO_MOB_MAP: dict[int, SVGMobject] = {} | ||
|
|
||
|
|
||
| def _convert_point_to_3d(x: float, y: float) -> np.ndarray: | ||
|
|
@@ -127,6 +127,7 @@ def __init__( | |
| self.stroke_color = stroke_color | ||
| self.stroke_opacity = stroke_opacity # type: ignore[assignment] | ||
| self.stroke_width = stroke_width # type: ignore[assignment] | ||
| self.id_to_vgroup_dict: dict[str, VGroup] = {} | ||
| if self.stroke_width is None: | ||
| self.stroke_width = 0 | ||
|
|
||
|
|
@@ -170,6 +171,7 @@ def init_svg_mobject(self, use_svg_cache: bool) -> None: | |
| if hash_val in SVG_HASH_TO_MOB_MAP: | ||
| mob = SVG_HASH_TO_MOB_MAP[hash_val].copy() | ||
| self.add(*mob) | ||
| self.id_to_vgroup_dict = mob.id_to_vgroup_dict | ||
| return | ||
|
|
||
| self.generate_mobject() | ||
|
|
@@ -203,8 +205,9 @@ def generate_mobject(self) -> None: | |
| svg = se.SVG.parse(modified_file_path) | ||
| modified_file_path.unlink() | ||
|
|
||
| mobjects = self.get_mobjects_from(svg) | ||
| mobjects, mobject_dict = self.get_mobjects_from(svg) | ||
| self.add(*mobjects) | ||
| self.id_to_vgroup_dict = mobject_dict | ||
| self.flip(RIGHT) # Flip y | ||
|
|
||
| def get_file_path(self) -> Path: | ||
|
|
@@ -258,7 +261,9 @@ def generate_config_style_dict(self) -> dict[str, str]: | |
| result[svg_key] = str(svg_default_dict[style_key]) | ||
| return result | ||
|
|
||
| def get_mobjects_from(self, svg: se.SVG) -> list[VMobject]: | ||
| def get_mobjects_from( | ||
| self, svg: se.SVG | ||
| ) -> tuple[list[VMobject], dict[str, VGroup]]: | ||
| """Convert the elements of the SVG to a list of mobjects. | ||
|
|
||
| Parameters | ||
|
|
@@ -267,36 +272,77 @@ def get_mobjects_from(self, svg: se.SVG) -> list[VMobject]: | |
| The parsed SVG file. | ||
| """ | ||
| result: list[VMobject] = [] | ||
| for shape in svg.elements(): | ||
| # can we combine the two continue cases into one? | ||
| if isinstance(shape, se.Group): # noqa: SIM114 | ||
| continue | ||
| elif isinstance(shape, se.Path): | ||
| mob: VMobject = self.path_to_mobject(shape) | ||
| elif isinstance(shape, se.SimpleLine): | ||
| mob = self.line_to_mobject(shape) | ||
| elif isinstance(shape, se.Rect): | ||
| mob = self.rect_to_mobject(shape) | ||
| elif isinstance(shape, (se.Circle, se.Ellipse)): | ||
| mob = self.ellipse_to_mobject(shape) | ||
| elif isinstance(shape, se.Polygon): | ||
| mob = self.polygon_to_mobject(shape) | ||
| elif isinstance(shape, se.Polyline): | ||
| mob = self.polyline_to_mobject(shape) | ||
| elif isinstance(shape, se.Text): | ||
| mob = self.text_to_mobject(shape) | ||
| elif isinstance(shape, se.Use) or type(shape) is se.SVGElement: | ||
| continue | ||
| else: | ||
| logger.warning(f"Unsupported element type: {type(shape)}") | ||
| continue | ||
| if mob is None or not mob.has_points(): | ||
| continue | ||
| self.apply_style_to_mobject(mob, shape) | ||
| if isinstance(shape, se.Transformable) and shape.apply: | ||
| self.handle_transform(mob, shape.transform) | ||
| result.append(mob) | ||
| return result | ||
| stack: list[tuple[se.SVGElement, int]] = [] | ||
| stack.append((svg, 1)) | ||
| group_id_number = 0 | ||
| vgroup_stack: list[str] = ["root"] | ||
| vgroup_names: list[str] = ["root"] | ||
| vgroups: dict[str, VGroup] = {"root": VGroup()} | ||
| while len(stack) > 0: | ||
| element, depth = stack.pop() | ||
| # Reduce stack heights | ||
| vgroup_stack = vgroup_stack[0:(depth)] | ||
| try: | ||
| group_name = str(element.values["id"]) | ||
| except Exception: | ||
| group_name = f"numbered_group_{group_id_number}" | ||
| group_id_number += 1 | ||
| vg = VGroup() | ||
| vgroup_names.append(group_name) | ||
| vgroup_stack.append(group_name) | ||
| vgroups[group_name] = vg | ||
|
|
||
| if isinstance(element, (se.Group, se.Use)): | ||
| stack.extend((subelement, depth + 1) for subelement in element[::-1]) | ||
| # Add element to the parent vgroup | ||
| try: | ||
| if isinstance( | ||
| element, | ||
| ( | ||
| se.Path, | ||
| se.SimpleLine, | ||
| se.Rect, | ||
| se.Circle, | ||
| se.Ellipse, | ||
| se.Polygon, | ||
| se.Polyline, | ||
| se.Text, | ||
| ), | ||
| ): | ||
| mob = self.get_mob_from_shape_element(element) | ||
| if mob is not None: | ||
| result.append(mob) | ||
| for parent_name in vgroup_stack[:-1]: | ||
| vgroups[parent_name].add(mob) | ||
| except Exception as e: | ||
| print(e) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logger.error with a bit more context? |
||
|
|
||
| return result, vgroups | ||
|
|
||
| def get_mob_from_shape_element(self, shape: se.SVGElement) -> VMobject | None: | ||
| if isinstance(shape, se.Path): | ||
| mob: VMobject | None = self.path_to_mobject(shape) | ||
| elif isinstance(shape, se.SimpleLine): | ||
| mob = self.line_to_mobject(shape) | ||
| elif isinstance(shape, se.Rect): | ||
| mob = self.rect_to_mobject(shape) | ||
| elif isinstance(shape, (se.Circle, se.Ellipse)): | ||
| mob = self.ellipse_to_mobject(shape) | ||
| elif isinstance(shape, se.Polygon): | ||
| mob = self.polygon_to_mobject(shape) | ||
| elif isinstance(shape, se.Polyline): | ||
| mob = self.polyline_to_mobject(shape) | ||
| elif isinstance(shape, se.Text): | ||
| mob = self.text_to_mobject(shape) | ||
| else: | ||
| logger.warning(f"Unsupported element type: {type(shape)}") | ||
| mob = None | ||
| if mob is None or not mob.has_points(): | ||
| return mob | ||
| self.apply_style_to_mobject(mob, shape) | ||
| if isinstance(shape, se.Transformable) and shape.apply: | ||
| self.handle_transform(mob, shape.transform) | ||
| return mob | ||
|
|
||
| @staticmethod | ||
| def handle_transform(mob: VMobject, matrix: se.Matrix) -> VMobject: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we attempt to strengthen the parser to make this not necessary? Perhaps not at first.