datacut 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. datacut/__init__.py +121 -0
  2. datacut/__main__.py +122 -0
  3. datacut/animation/__init__.py +0 -0
  4. datacut/animation/animation.py +336 -0
  5. datacut/animation/composition.py +258 -0
  6. datacut/animation/creation.py +315 -0
  7. datacut/animation/fading.py +328 -0
  8. datacut/animation/growing.py +90 -0
  9. datacut/animation/indication.py +608 -0
  10. datacut/animation/movement.py +165 -0
  11. datacut/animation/numbers.py +92 -0
  12. datacut/animation/physics.py +453 -0
  13. datacut/animation/rotation.py +95 -0
  14. datacut/animation/specialized.py +956 -0
  15. datacut/animation/transform.py +505 -0
  16. datacut/animation/transform_matching_parts.py +343 -0
  17. datacut/animation/update.py +95 -0
  18. datacut/camera/__init__.py +0 -0
  19. datacut/camera/camera.py +795 -0
  20. datacut/camera/camera_frame.py +320 -0
  21. datacut/camera/cinematography.py +1166 -0
  22. datacut/config.py +479 -0
  23. datacut/constants.py +160 -0
  24. datacut/css/__init__.py +189 -0
  25. datacut/css/easing.py +328 -0
  26. datacut/css/engine.py +1727 -0
  27. datacut/css/gradients.py +678 -0
  28. datacut/css/motion.py +1615 -0
  29. datacut/css/parser.py +741 -0
  30. datacut/css/presets.py +143 -0
  31. datacut/css/shader_fill.py +703 -0
  32. datacut/css/values.py +650 -0
  33. datacut/default_config.yml +224 -0
  34. datacut/event_handler/__init__.py +6 -0
  35. datacut/event_handler/event_dispatcher.py +161 -0
  36. datacut/event_handler/event_listner.py +31 -0
  37. datacut/event_handler/event_type.py +11 -0
  38. datacut/event_handler/picking.py +111 -0
  39. datacut/event_keys.py +53 -0
  40. datacut/extract_scene.py +200 -0
  41. datacut/layout/__init__.py +39 -0
  42. datacut/layout/components.py +425 -0
  43. datacut/layout/containers.py +1520 -0
  44. datacut/logger.py +13 -0
  45. datacut/mobject/__init__.py +0 -0
  46. datacut/mobject/boolean_ops.py +168 -0
  47. datacut/mobject/changing.py +209 -0
  48. datacut/mobject/coordinate_systems.py +1056 -0
  49. datacut/mobject/fractals.py +348 -0
  50. datacut/mobject/frame.py +80 -0
  51. datacut/mobject/functions.py +147 -0
  52. datacut/mobject/geometry.py +1834 -0
  53. datacut/mobject/gltf.py +243 -0
  54. datacut/mobject/interactive.py +906 -0
  55. datacut/mobject/lights.py +366 -0
  56. datacut/mobject/materials.py +207 -0
  57. datacut/mobject/matrix.py +570 -0
  58. datacut/mobject/mesh3d.py +2288 -0
  59. datacut/mobject/mobject.py +2885 -0
  60. datacut/mobject/mobject_update_utils.py +123 -0
  61. datacut/mobject/model.py +543 -0
  62. datacut/mobject/number_line.py +517 -0
  63. datacut/mobject/numbers.py +441 -0
  64. datacut/mobject/probability.py +536 -0
  65. datacut/mobject/shape_matchers.py +161 -0
  66. datacut/mobject/subtitles.py +260 -0
  67. datacut/mobject/svg/__init__.py +0 -0
  68. datacut/mobject/svg/brace.py +356 -0
  69. datacut/mobject/svg/drawings.py +1041 -0
  70. datacut/mobject/svg/old_tex_mobject.py +335 -0
  71. datacut/mobject/svg/path_cache.py +155 -0
  72. datacut/mobject/svg/special_tex.py +216 -0
  73. datacut/mobject/svg/string_mobject.py +622 -0
  74. datacut/mobject/svg/svg_mobject.py +657 -0
  75. datacut/mobject/svg/svg_parse.py +437 -0
  76. datacut/mobject/svg/tex_mobject.py +499 -0
  77. datacut/mobject/svg/text_mobject.py +702 -0
  78. datacut/mobject/svg/typst_mobject.py +864 -0
  79. datacut/mobject/three_dimensions.py +686 -0
  80. datacut/mobject/types/__init__.py +0 -0
  81. datacut/mobject/types/dot_cloud.py +220 -0
  82. datacut/mobject/types/image_mobject.py +130 -0
  83. datacut/mobject/types/point_cloud_mobject.py +138 -0
  84. datacut/mobject/types/shader_mobject.py +268 -0
  85. datacut/mobject/types/surface.py +848 -0
  86. datacut/mobject/types/vectorized_mobject.py +1749 -0
  87. datacut/mobject/types/video_mobject.py +3902 -0
  88. datacut/mobject/value_tracker.py +110 -0
  89. datacut/mobject/vector_field.py +585 -0
  90. datacut/module_loader.py +176 -0
  91. datacut/reactive/__init__.py +25 -0
  92. datacut/reactive/signals.py +610 -0
  93. datacut/renderer/__init__.py +4 -0
  94. datacut/renderer/drawing.py +1066 -0
  95. datacut/renderer/gpu.py +800 -0
  96. datacut/renderer/material.py +88 -0
  97. datacut/renderer/pipeline.py +179 -0
  98. datacut/renderer/post_process.py +577 -0
  99. datacut/renderer/renderer.py +1285 -0
  100. datacut/renderer/shader_source.py +146 -0
  101. datacut/renderer/shared_buffer.py +185 -0
  102. datacut/renderer/texture.py +217 -0
  103. datacut/renderer/uniform_block.py +118 -0
  104. datacut/scene/__init__.py +0 -0
  105. datacut/scene/incremental.py +233 -0
  106. datacut/scene/interactive_scene.py +758 -0
  107. datacut/scene/performance.py +226 -0
  108. datacut/scene/presentation_scene.py +783 -0
  109. datacut/scene/recording.py +541 -0
  110. datacut/scene/safe_zones.py +323 -0
  111. datacut/scene/scene.py +2849 -0
  112. datacut/scene/scene_embed.py +277 -0
  113. datacut/scene/scene_file_writer.py +1014 -0
  114. datacut/scene/scene_task.py +183 -0
  115. datacut/scene/sfx.py +164 -0
  116. datacut/scene/sound.py +702 -0
  117. datacut/scene/sound_clip.py +580 -0
  118. datacut/scene/timeline.py +1689 -0
  119. datacut/scene/transcript.py +503 -0
  120. datacut/shaders/fill.wgsl +107 -0
  121. datacut/shaders/fill_depth.wgsl +198 -0
  122. datacut/shaders/image.wgsl +25 -0
  123. datacut/shaders/inserts/NOTE.md +16 -0
  124. datacut/shaders/inserts/clip_test.wgsl +14 -0
  125. datacut/shaders/inserts/complex_functions.wgsl +20 -0
  126. datacut/shaders/inserts/fill_color.wgsl +10 -0
  127. datacut/shaders/inserts/finalize_color.wgsl +44 -0
  128. datacut/shaders/inserts/frame_uniforms.wgsl +37 -0
  129. datacut/shaders/inserts/frame_units.wgsl +18 -0
  130. datacut/shaders/inserts/image_quad.wgsl +28 -0
  131. datacut/shaders/inserts/mobject_uniforms.wgsl +11 -0
  132. datacut/shaders/inserts/project_point.wgsl +44 -0
  133. datacut/shaders/inserts/quad_corners.wgsl +12 -0
  134. datacut/shaders/inserts/read_data.wgsl +33 -0
  135. datacut/shaders/inserts/surface_mesh.wgsl +106 -0
  136. datacut/shaders/mandelbrot_fractal.wgsl +100 -0
  137. datacut/shaders/mesh.wgsl +474 -0
  138. datacut/shaders/newton_fractal.wgsl +200 -0
  139. datacut/shaders/present.wgsl +62 -0
  140. datacut/shaders/stroke.wgsl +1141 -0
  141. datacut/shaders/stroke_depth.wgsl +1136 -0
  142. datacut/shaders/surface.wgsl +40 -0
  143. datacut/shaders/textured_surface.wgsl +59 -0
  144. datacut/shaders/true_dot.wgsl +78 -0
  145. datacut/shaders/video.wgsl +42 -0
  146. datacut/tex_templates.yml +736 -0
  147. datacut/transcribe.py +32 -0
  148. datacut/typing.py +412 -0
  149. datacut/utils/__init__.py +0 -0
  150. datacut/utils/bezier.py +467 -0
  151. datacut/utils/browser_audio.py +423 -0
  152. datacut/utils/browser_code_transform.py +1293 -0
  153. datacut/utils/browser_completions.py +2255 -0
  154. datacut/utils/browser_fonts.py +143 -0
  155. datacut/utils/browser_pathops.py +47 -0
  156. datacut/utils/browser_scheduler.py +18 -0
  157. datacut/utils/browser_typst.py +82 -0
  158. datacut/utils/cache.py +34 -0
  159. datacut/utils/cinematic_vfx.py +779 -0
  160. datacut/utils/color.py +203 -0
  161. datacut/utils/debug.py +46 -0
  162. datacut/utils/dict_ops.py +22 -0
  163. datacut/utils/directories.py +55 -0
  164. datacut/utils/editor_signatures.py +1806 -0
  165. datacut/utils/editor_thumbnails.py +769 -0
  166. datacut/utils/family_ops.py +48 -0
  167. datacut/utils/file_ops.py +65 -0
  168. datacut/utils/images.py +52 -0
  169. datacut/utils/iterables.py +216 -0
  170. datacut/utils/native.py +280 -0
  171. datacut/utils/paths.py +83 -0
  172. datacut/utils/rate_functions.py +154 -0
  173. datacut/utils/render_geometry.py +186 -0
  174. datacut/utils/simple_functions.py +99 -0
  175. datacut/utils/sounds.py +43 -0
  176. datacut/utils/space_ops.py +460 -0
  177. datacut/utils/structured_array.py +256 -0
  178. datacut/utils/svg_export.py +102 -0
  179. datacut/utils/tex.py +139 -0
  180. datacut/utils/tex_file_writing.py +164 -0
  181. datacut/utils/tex_to_symbol_count.py +186 -0
  182. datacut/utils/typst_file_writing.py +205 -0
  183. datacut/utils/vfx_presets.py +4543 -0
  184. datacut/utils/visual_css.py +737 -0
  185. datacut/utils/visual_editor.py +4292 -0
  186. datacut/utils/window_audio.py +760 -0
  187. datacut/window.py +910 -0
  188. datacut-0.1.0.dist-info/METADATA +226 -0
  189. datacut-0.1.0.dist-info/RECORD +193 -0
  190. datacut-0.1.0.dist-info/WHEEL +5 -0
  191. datacut-0.1.0.dist-info/entry_points.txt +2 -0
  192. datacut-0.1.0.dist-info/licenses/LICENSE.md +21 -0
  193. datacut-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,499 @@
1
+ from __future__ import annotations
2
+
3
+ # What the Visual Editor files this module's classes under
4
+ __editor_category__ = 'Text & Math'
5
+
6
+ import re
7
+ import sys
8
+
9
+ from functools import lru_cache
10
+
11
+ from datacut.config import manim_config
12
+ from datacut.mobject.svg.string_mobject import StringMobject
13
+ from datacut.mobject.svg.svg_mobject import SVGMobject, get_svg_content_height
14
+ from datacut.mobject.types.vectorized_mobject import VGroup
15
+ from datacut.mobject.types.vectorized_mobject import VMobject
16
+ from datacut.utils.color import color_to_hex
17
+ from datacut.utils.color import hex_to_int
18
+ from datacut.utils.typst_file_writing import typst_to_svg
19
+ from datacut.utils.tex import num_tex_symbols, parse_tex_submob_map
20
+ from datacut.logger import log
21
+
22
+ from typing import TYPE_CHECKING
23
+ from datacut.typing import OneOf
24
+
25
+ if TYPE_CHECKING:
26
+ from datacut.typing import Annotated, Example
27
+ from datacut.typing import CompilableText, FontName, FontSize
28
+ from datacut.typing import ManimColor, Span, Selector, Self
29
+
30
+
31
+ @lru_cache(maxsize=8)
32
+ def get_tex_mob_scale_factor(text_mode: bool = False, font: str = "New Computer Modern") -> float:
33
+ # Render a reference "0" and calibrate so that font_size_for_unit_height
34
+ # gives a height of 1 manim unit.
35
+ font_size_for_unit_height = manim_config.tex.font_size_for_unit_height
36
+ svg_string = typst_to_svg(Tex._make_mitex_source("0", text_mode=text_mode, font=font))
37
+ svg_height = get_svg_content_height(svg_string)
38
+ return 1.0 / (font_size_for_unit_height * svg_height)
39
+
40
+
41
+ class Tex(StringMobject):
42
+ # Drawn 24px wide in the editor's shelf: what reads at that size
43
+ editor_thumbnail_args = (r'"e^{i\pi}"',)
44
+
45
+ tex_environment: str = "align*"
46
+
47
+ def __init__(
48
+ self,
49
+ *tex_strings: Annotated[CompilableText, Example('r"E = mc^2"')],
50
+ font_size: FontSize = 48,
51
+ font: FontName = "New Computer Modern",
52
+ alignment: str = R"\centering",
53
+ template: str = "",
54
+ additional_preamble: str = "",
55
+ tex_to_color_map: Annotated[dict, OneOf("color_map")] = dict(),
56
+ t2c: Annotated[dict, OneOf("color_map")] = dict(),
57
+ isolate: Selector = [],
58
+ use_labelled_svg: bool = True,
59
+ **kwargs
60
+ ):
61
+ # Combine multi-string arg, but mark them to isolate
62
+ if len(tex_strings) > 1:
63
+ if isinstance(isolate, (str, re.Pattern, tuple)):
64
+ isolate = [isolate]
65
+ isolate = [*isolate, *tex_strings]
66
+
67
+ tex_string = (" ".join(tex_strings)).strip()
68
+
69
+ # Prevent from passing an empty string.
70
+ if not tex_string.strip():
71
+ tex_string = R"\\"
72
+
73
+ self.tex_string = tex_string
74
+ self.font = font
75
+ self.alignment = alignment
76
+ self.template = template
77
+ self.additional_preamble = additional_preamble
78
+ self.tex_to_color_map = dict(**t2c, **tex_to_color_map)
79
+
80
+ text_mode = not bool(self.tex_environment)
81
+
82
+ super().__init__(
83
+ tex_string,
84
+ use_labelled_svg=use_labelled_svg,
85
+ isolate=isolate,
86
+ height=None,
87
+ stroke_width=None,
88
+ **kwargs
89
+ )
90
+
91
+ self.set_color_by_tex_to_color_map(self.tex_to_color_map)
92
+ # The browser renders TeX through MiTeX inside typst-wasm. Its SVG is
93
+ # asynchronous, so browser scaling is applied by create() after rendering.
94
+ if sys.platform != "emscripten":
95
+ scale_factor = get_tex_mob_scale_factor(text_mode=text_mode, font=self.font) * font_size
96
+ self.scale(scale_factor)
97
+ self.scale_stroke_widths(scale_factor)
98
+
99
+ self.font_size = font_size # Important for this to go after the scale call
100
+
101
+ @classmethod
102
+ def create(cls, *tex_strings: str, **kwargs):
103
+ """Create Tex/TexText, using MiTeX."""
104
+ if sys.platform != "emscripten":
105
+ return cls(*tex_strings, **kwargs)
106
+ return cls._create_browser(*tex_strings, **kwargs)
107
+
108
+ @staticmethod
109
+ def _make_mitex_source(
110
+ content: str,
111
+ *,
112
+ text_mode: bool = False,
113
+ font: str = "New Computer Modern",
114
+ is_labelled: bool = False,
115
+ ) -> str:
116
+ fill_col = "#000000" if is_labelled else "#ffffff"
117
+ font_rule = f'#set text(font: "{font}")\n' if font else ""
118
+ doc_head = (
119
+ f'#set page(width: auto, height: auto, margin: 0pt, fill: none)\n'
120
+ f'#set text(fill: rgb("{fill_col}"), size: 10pt, ligatures: false)\n'
121
+ f'{font_rule}'
122
+ f'#show math.equation: set text(ligatures: false)\n'
123
+ f'#set line(stroke: rgb("{fill_col}"))\n'
124
+ f'#let _mc(c, it) = if c != "" {{ text(fill: rgb(c), it) }} else {{ it }}\n'
125
+ ).strip()
126
+
127
+ delimiter = "`"
128
+ command = "mitext" if text_mode else "mitex"
129
+ return f"#import \"@preview/mitex:0.2.7\": *\n{doc_head}\n" + f'#{command}({delimiter}\n{content}\n{delimiter})\n'
130
+
131
+ @classmethod
132
+ async def _create_browser(cls, *tex_strings: str, **kwargs):
133
+ from datacut.utils.browser_typst import typst_to_svg_async
134
+
135
+ use_labelled_svg = kwargs.get("use_labelled_svg", True)
136
+ font_size = kwargs.get("font_size", 48)
137
+ text_mode = not bool(cls.tex_environment)
138
+
139
+ # Build a probe with an empty SVG. This preserves Tex's native parsing,
140
+ # isolation spans, color-label insertion, and options without invoking
141
+ # the native LaTeX executable on the browser.
142
+ probe_kwargs = dict(kwargs)
143
+ for private_key in ("_svg_override", "_labelled_svg_override"):
144
+ probe_kwargs.pop(private_key, None)
145
+ probe_kwargs["use_labelled_svg"] = True
146
+ probe_kwargs["_svg_override"] = (
147
+ '<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1" '
148
+ 'viewBox="0 0 1 1"/>'
149
+ )
150
+ probe = cls(*tex_strings, **probe_kwargs)
151
+
152
+ async def render(is_labelled: bool) -> str:
153
+ content = probe.get_content(is_labelled=is_labelled)
154
+ # \centering is a layout command rather than equation content and
155
+ # is not needed in Typst's auto-sized SVG page.
156
+ if probe.alignment:
157
+ content = content.replace(probe.alignment, "")
158
+ if probe.additional_preamble:
159
+ content = probe.additional_preamble + "\n" + content
160
+ svg = await typst_to_svg_async(
161
+ cls._make_mitex_source(
162
+ content,
163
+ text_mode=text_mode,
164
+ font=probe.font,
165
+ is_labelled=is_labelled,
166
+ )
167
+ )
168
+ return svg
169
+
170
+ svg = await render(is_labelled=use_labelled_svg)
171
+ labelled_svg = None if use_labelled_svg else await render(is_labelled=True)
172
+
173
+ final_kwargs = dict(kwargs)
174
+ final_kwargs.pop("_svg_override", None)
175
+ final_kwargs.pop("_labelled_svg_override", None)
176
+ final_kwargs["_svg_override"] = svg
177
+ final_kwargs["use_labelled_svg"] = use_labelled_svg
178
+ if labelled_svg is not None:
179
+ final_kwargs["_labelled_svg_override"] = labelled_svg
180
+
181
+ obj = cls(*tex_strings, **final_kwargs)
182
+
183
+ # Calibrate independently for math and text-mode MiTeX output.
184
+ scale_key = (text_mode, cls.tex_environment)
185
+ scale_cache = getattr(Tex, "_browser_scale_cache", None)
186
+ if scale_cache is None:
187
+ scale_cache = {}
188
+ Tex._browser_scale_cache = scale_cache
189
+ if scale_key not in scale_cache:
190
+ reference_svg = await typst_to_svg_async(
191
+ cls._make_mitex_source("0", text_mode=text_mode, font=probe.font)
192
+ )
193
+ svg_height = get_svg_content_height(reference_svg)
194
+ scale_cache[scale_key] = (
195
+ 1.0 / (manim_config.tex.font_size_for_unit_height * svg_height)
196
+ )
197
+ obj.scale(scale_cache[scale_key] * font_size)
198
+ obj.scale_stroke_widths(scale_cache[scale_key] * font_size)
199
+ # Native Tex scales before assigning font_size, so retain that behavior.
200
+ obj.font_size = font_size
201
+ return obj
202
+
203
+ def get_svg_string_by_content(self, content: str) -> str:
204
+ text_mode = not bool(self.tex_environment)
205
+ is_labelled = "\\color[RGB]{" in content
206
+ if self.alignment and self.alignment in content:
207
+ content = content.replace(self.alignment, "")
208
+ if self.additional_preamble:
209
+ content = self.additional_preamble + "\n" + content
210
+ mitex_src = self._make_mitex_source(
211
+ content,
212
+ text_mode=text_mode,
213
+ font=getattr(self, "font", "New Computer Modern"),
214
+ is_labelled=is_labelled,
215
+ )
216
+ return typst_to_svg(mitex_src)
217
+
218
+ def _handle_scale_side_effects(self, scale_factor: float) -> Self:
219
+ if hasattr(self, "font_size"):
220
+ self.font_size *= scale_factor
221
+ return self
222
+
223
+ # Parsing
224
+
225
+ @staticmethod
226
+ def get_command_matches(string: str) -> list[re.Match]:
227
+ # Lump together adjacent brace pairs
228
+ pattern = re.compile(r"""
229
+ (?P<command>\\(?:[a-zA-Z]+|.))
230
+ |(?P<open>{+)
231
+ |(?P<close>}+)
232
+ """, flags=re.X | re.S)
233
+ result = []
234
+ open_stack = []
235
+ for match_obj in pattern.finditer(string):
236
+ if match_obj.group("open"):
237
+ open_stack.append((match_obj.span(), len(result)))
238
+ elif match_obj.group("close"):
239
+ close_start, close_end = match_obj.span()
240
+ while True:
241
+ if not open_stack:
242
+ raise ValueError("Missing '{' inserted")
243
+ (open_start, open_end), index = open_stack.pop()
244
+ n = min(open_end - open_start, close_end - close_start)
245
+ result.insert(index, pattern.fullmatch(
246
+ string, pos=open_end - n, endpos=open_end
247
+ ))
248
+ result.append(pattern.fullmatch(
249
+ string, pos=close_start, endpos=close_start + n
250
+ ))
251
+ close_start += n
252
+ if close_start < close_end:
253
+ continue
254
+ open_end -= n
255
+ if open_start < open_end:
256
+ open_stack.append(((open_start, open_end), index))
257
+ break
258
+ else:
259
+ result.append(match_obj)
260
+ if open_stack:
261
+ raise ValueError("Missing '}' inserted")
262
+ return result
263
+
264
+ @staticmethod
265
+ def get_command_flag(match_obj: re.Match) -> int:
266
+ if match_obj.group("open"):
267
+ return 1
268
+ if match_obj.group("close"):
269
+ return -1
270
+ return 0
271
+
272
+ @staticmethod
273
+ def replace_for_content(match_obj: re.Match) -> str:
274
+ return match_obj.group()
275
+
276
+ @staticmethod
277
+ def replace_for_matching(match_obj: re.Match) -> str:
278
+ if match_obj.group("command"):
279
+ return match_obj.group()
280
+ return ""
281
+
282
+ @staticmethod
283
+ def get_attr_dict_from_command_pair(
284
+ open_command: re.Match, close_command: re.Match
285
+ ) -> dict[str, str] | None:
286
+ if len(open_command.group()) >= 2:
287
+ return {}
288
+ return None
289
+
290
+ def get_configured_items(self) -> list[tuple[Span, dict[str, str]]]:
291
+ return [
292
+ (span, {})
293
+ for selector in self.tex_to_color_map
294
+ for span in self.find_spans_by_selector(selector)
295
+ ]
296
+
297
+ @staticmethod
298
+ def get_color_command(rgb_hex: str) -> str:
299
+ rgb = hex_to_int(rgb_hex)
300
+ rg, b = divmod(rgb, 256)
301
+ r, g = divmod(rg, 256)
302
+ return f"\\color[RGB]{{{r}, {g}, {b}}}"
303
+
304
+ @staticmethod
305
+ def get_command_string(
306
+ attr_dict: dict[str, str], is_end: bool, label_hex: str | None
307
+ ) -> str:
308
+ if label_hex is None:
309
+ return ""
310
+ if is_end:
311
+ return "}}"
312
+ return "{{" + Tex.get_color_command(label_hex)
313
+
314
+ def get_content_prefix_and_suffix(
315
+ self, is_labelled: bool
316
+ ) -> tuple[str, str]:
317
+ prefix_lines = []
318
+ suffix_lines = []
319
+ if not is_labelled:
320
+ prefix_lines.append(self.get_color_command(
321
+ color_to_hex(self.base_color)
322
+ ))
323
+ if self.alignment:
324
+ prefix_lines.append(self.alignment)
325
+ if self.tex_environment:
326
+ prefix_lines.append(f"\\begin{{{self.tex_environment}}}")
327
+ suffix_lines.append(f"\\end{{{self.tex_environment}}}")
328
+ return (
329
+ "".join([line + "\n" for line in prefix_lines]),
330
+ "".join(["\n" + line for line in suffix_lines])
331
+ )
332
+
333
+ # Method alias
334
+
335
+ def get_parts_by_tex(self, selector: Selector) -> VGroup:
336
+ return self.select_parts(selector)
337
+
338
+ def get_part_by_tex(self, selector: Selector, index: int = 0) -> VMobject:
339
+ return self.select_part(selector, index)
340
+
341
+ def set_color_by_tex(self, selector: Selector, color: ManimColor):
342
+ return self.set_parts_color(selector, color)
343
+
344
+ def set_color_by_tex_to_color_map(
345
+ self, color_map: dict[Selector, ManimColor]
346
+ ):
347
+ return self.set_parts_color_by_dict(color_map)
348
+
349
+ def get_tex(self) -> str:
350
+ return self.get_string()
351
+
352
+ # Specific to Tex
353
+ def _get_char_to_submob_indices(self) -> list[list[int]]:
354
+ if not hasattr(self, "_char_to_submob_map"):
355
+ _, self._token_spans, self._char_to_submob_map = parse_tex_submob_map(self.string)
356
+ return self._char_to_submob_map
357
+
358
+ def get_token_submob_map(self) -> list[tuple[Span, list[int], str]]:
359
+ if not hasattr(self, "_token_spans"):
360
+ _, self._token_spans, self._char_to_submob_map = parse_tex_submob_map(self.string)
361
+ return sorted(
362
+ [(span, idxs, "/" if tok == "frac_bar" else tok) for span, idxs, tok in self._token_spans if idxs],
363
+ key=lambda item: item[0][0]
364
+ )
365
+
366
+ def get_submob_indices_for_span(self, span: Span) -> list[int]:
367
+ mapping = self._get_char_to_submob_indices()
368
+ indices = []
369
+ for c in range(span[0], min(span[1], len(mapping))):
370
+ for idx in mapping[c]:
371
+ if idx not in indices and idx < len(self.submobjects):
372
+ indices.append(idx)
373
+ return sorted(indices)
374
+
375
+ def select_unisolated_substring(self, pattern: str | re.Pattern) -> VGroup:
376
+ spans = self.find_spans_by_selector(pattern)
377
+ indices_list = [self.get_submob_indices_for_span(span) for span in spans]
378
+ indices_list = [idx for idx in indices_list if idx]
379
+ return self.build_parts_from_indices_lists(indices_list)
380
+
381
+ def get_submob_indices_list_by_span(self, arbitrary_span: Span) -> list[int]:
382
+ result = super().get_submob_indices_list_by_span(arbitrary_span)
383
+ if not result:
384
+ return self.get_submob_indices_for_span(arbitrary_span)
385
+ return result
386
+
387
+ def substr_to_path_count(self, substr: str) -> int:
388
+ return num_tex_symbols(substr)
389
+
390
+ def get_symbol_substrings(self):
391
+ pattern = "|".join((
392
+ # Tex commands
393
+ r"\\[a-zA-Z]+",
394
+ # And most single characters, with these exceptions
395
+ r"[^\^\{\}\s\_\$\\\&]",
396
+ ))
397
+ return re.findall(pattern, self.string)
398
+
399
+ def make_number_changeable(
400
+ self,
401
+ value: float | int | str,
402
+ index: int = 0,
403
+ replace_all: bool = False,
404
+ **config,
405
+ ) -> VMobject:
406
+ substr = str(value)
407
+ parts = self.select_parts(substr)
408
+ if len(parts) == 0:
409
+ log.warning(f"{value} not found in Tex.make_number_changeable call")
410
+ return VMobject()
411
+ if index > len(parts) - 1:
412
+ log.warning(f"Requested {index}th occurance of {value}, but only {len(parts)} exist")
413
+ return VMobject()
414
+ if not replace_all:
415
+ parts = [parts[index]]
416
+
417
+ from datacut.mobject.numbers import DecimalNumber
418
+
419
+ decimal_mobs = []
420
+ for part in parts:
421
+ if "num_decimal_places" not in config:
422
+ ndp = len(substr.split(".")[1]) if "." in substr else 0
423
+ config["num_decimal_places"] = ndp
424
+ decimal_mob = DecimalNumber(float(value), **config)
425
+ decimal_mob.replace(part)
426
+ decimal_mob.match_style(part)
427
+ if len(part) > 1:
428
+ self.remove(*part[1:])
429
+ self.replace_submobject(self.submobjects.index(part[0]), decimal_mob)
430
+ decimal_mobs.append(decimal_mob)
431
+
432
+ # Replace substr with something that looks like a tex command. This
433
+ # is to ensure Tex.substr_to_path_count counts it correctly.
434
+ self.string = self.string.replace(substr, R"\decimalmob", 1)
435
+
436
+ if replace_all:
437
+ return VGroup(*decimal_mobs)
438
+ return decimal_mobs[index]
439
+
440
+ async def make_number_changeable_async(
441
+ self,
442
+ value: float | int | str,
443
+ index: int = 0,
444
+ replace_all: bool = False,
445
+ **config,
446
+ ) -> VMobject:
447
+ substr = str(value)
448
+ parts = self.select_parts(substr)
449
+ if len(parts) == 0:
450
+ log.warning(f"{value} not found in Tex.make_number_changeable call")
451
+ return VMobject()
452
+ if index > len(parts) - 1:
453
+ log.warning(f"Requested {index}th occurance of {value}, but only {len(parts)} exist")
454
+ return VMobject()
455
+ if not replace_all:
456
+ parts = [parts[index]]
457
+
458
+ from datacut.mobject.numbers import DecimalNumber
459
+
460
+ decimal_mobs = []
461
+ for part in parts:
462
+ if "num_decimal_places" not in config:
463
+ ndp = len(substr.split(".")[1]) if "." in substr else 0
464
+ config["num_decimal_places"] = ndp
465
+ decimal_mob = await DecimalNumber.create(float(value), **config)
466
+ decimal_mob.replace(part)
467
+ decimal_mob.match_style(part)
468
+ if len(part) > 1:
469
+ self.remove(*part[1:])
470
+ self.replace_submobject(self.submobjects.index(part[0]), decimal_mob)
471
+ decimal_mobs.append(decimal_mob)
472
+
473
+ self.string = self.string.replace(substr, R"\decimalmob", 1)
474
+
475
+ if replace_all:
476
+ return VGroup(*decimal_mobs)
477
+ return decimal_mobs[index]
478
+
479
+ editor_thumbnail = (
480
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">'
481
+ '<path d="M3.4 16.8Q3.4 16.5 3.4 16.1Q3.5 15.7 3.6 15.1Q4.0 15.1 4.5 15.1Q5.3 15.1 6.0 15.0Q6.6 14.9 7.1 14.7Q7.5 14.6 7.8 14.3Q8.1 14.0 8.2 13.7Q8.3 13.5 8.4 13.3Q8.4 13.1 8.4 12.9Q8.4 12.2 7.8 11.8Q7.3 11.4 6.5 11.4Q6.0 11.4 5.5 11.5Q5.0 11.7 4.5 11.9Q3.4 12.5 2.7 13.6Q2.0 14.7 2.0 16.1Q2.0 17.5 2.7 18.4Q3.5 19.3 4.7 19.3Q5.4 19.3 6.0 19.1Q6.6 18.9 7.2 18.6Q7.6 18.3 8.0 18.1Q8.3 17.8 8.5 17.5Q8.6 17.4 8.6 17.3Q8.7 17.2 8.7 17.2Q8.7 17.0 8.6 17.0Q8.5 16.9 8.4 16.9Q8.3 16.9 8.2 16.9Q8.2 17.0 8.1 17.1Q7.7 17.7 7.1 18.0Q6.6 18.4 6.0 18.6Q5.6 18.7 5.3 18.7Q5.0 18.8 4.8 18.8Q4.1 18.8 3.7 18.2Q3.4 17.6 3.4 16.8ZM7.7 12.9Q7.7 13.8 6.9 14.2Q6.0 14.6 4.4 14.6Q4.1 14.6 3.8 14.6Q4.0 13.8 4.4 13.2Q4.7 12.6 5.2 12.4Q5.6 12.1 5.9 12.0Q6.3 11.9 6.5 11.9Q7.0 11.9 7.3 12.2Q7.7 12.5 7.7 12.9Z" fill="#FFFFFF" fill-opacity="1.00" fill-rule="evenodd" stroke-linejoin="round"/>'
482
+ '<path d="M13.2 5.2L12.7 4.7L12.0 5.4L12.5 5.9L13.2 5.2ZM11.6 7.4L10.8 7.6L10.3 8.3L9.9 9.3L9.9 9.4L10.3 9.4L10.4 9.3L11.6 7.8L11.8 7.9L11.8 8.2L11.5 9.4L10.7 11.3L10.6 11.8L11.0 12.6L12.5 12.7L13.1 12.0L13.4 11.4L13.5 10.9L13.3 10.8L13.0 11.0L13.0 11.0L12.5 12.1L11.8 12.5L11.6 12.4L11.6 11.8L11.8 11.3L12.8 8.7L12.8 8.5L11.6 7.4Z" fill="#FFFFFF" fill-opacity="1.00" fill-rule="evenodd" stroke-linejoin="round"/>'
483
+ '<path d="M19.6 12.9L20.0 12.7L20.2 12.3L20.2 12.2L20.1 12.0L19.8 11.1L19.7 10.0L19.7 9.2L19.9 8.5L21.3 8.5L21.8 8.3L22.0 7.9L21.9 7.6L21.4 7.6L16.9 7.6L16.2 7.7L15.5 8.2L15.3 8.4L15.0 8.8L14.8 9.1L14.7 9.2L14.8 9.4L15.1 9.4L15.2 9.3L15.9 8.7L16.8 8.5L17.5 8.5L16.9 10.1L16.0 12.1L15.9 12.2L15.9 12.3L15.9 12.4L15.9 12.5L16.0 12.8L16.3 12.9L16.7 12.7L17.0 12.2L17.1 11.9L17.3 11.3L18.0 8.5L19.3 8.5L19.0 10.0L18.9 11.2L19.6 12.9Z" fill="#FFFFFF" fill-opacity="1.00" fill-rule="evenodd" stroke-linejoin="round"/>'
484
+ '</svg>'
485
+ )
486
+
487
+
488
+ class TexText(Tex):
489
+ # Drawn 24px wide in the editor's shelf: what reads at that size
490
+ editor_thumbnail_args = ('"Aa"',)
491
+
492
+ tex_environment: str = ""
493
+
494
+ editor_thumbnail = (
495
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">'
496
+ '<path d="M13.3 17.3Q12.4 17.3 12.1 17.2Q11.8 17.1 11.7 16.8Q9.8 11.5 8.0 6.2Q8.0 6.1 7.8 6.0Q7.8 6.0 7.6 6.0Q7.6 6.0 7.5 6.0Q7.3 6.1 7.3 6.2Q5.5 11.3 3.7 16.4Q3.5 16.8 3.1 17.1Q2.7 17.3 2.1 17.3Q2.1 17.3 2.0 17.3Q2.0 17.6 2.0 17.8Q2.0 17.8 2.0 17.8Q2.8 17.8 3.5 17.8Q4.0 17.8 4.6 17.8Q5.1 17.8 5.6 17.8Q5.6 17.6 5.6 17.3Q5.0 17.3 4.6 17.0Q4.3 16.8 4.3 16.4Q4.3 16.3 4.4 16.0Q4.5 15.7 5.1 14.1Q7.2 14.1 9.2 14.1Q9.3 14.3 9.5 15.0Q10.1 16.7 10.1 16.8Q10.1 17.1 9.7 17.2Q9.4 17.3 8.9 17.3Q8.8 17.3 8.8 17.3Q8.8 17.6 8.8 17.8Q9.2 17.8 9.8 17.8Q10.4 17.8 11.1 17.8Q11.6 17.8 12.5 17.8Q13.1 17.8 13.3 17.8Q13.3 17.6 13.3 17.3ZM9.0 13.5Q7.2 13.5 5.3 13.5Q5.8 12.1 6.8 9.3Q7.0 8.5 7.2 8.2Q7.2 8.2 7.2 8.2Q7.2 8.2 7.3 8.6Q8.0 10.4 9.0 13.5Z" fill="#FFFFFF" fill-opacity="1.00" fill-rule="evenodd" stroke-linejoin="round"/>'
497
+ '<path d="M20.4 13.2Q20.4 12.6 20.3 12.2Q20.2 11.7 19.8 11.4Q19.4 10.9 18.8 10.7Q18.2 10.4 17.4 10.4Q16.6 10.4 15.8 10.9Q15.0 11.4 15.0 12.3Q15.0 12.7 15.2 12.9Q15.4 13.1 15.8 13.1Q16.1 13.1 16.3 12.9Q16.6 12.6 16.6 12.3Q16.6 12.0 16.3 11.8Q16.1 11.5 15.7 11.5Q16.0 11.2 16.5 11.0Q17.0 10.8 17.4 10.8Q18.2 10.8 18.7 11.3Q19.2 11.9 19.2 12.8Q19.2 13.2 19.2 13.5Q16.8 13.6 15.5 14.5Q14.6 15.2 14.6 16.2Q14.6 17.2 15.5 17.6Q16.2 18.0 17.2 18.0Q17.9 18.0 18.5 17.6Q19.1 17.3 19.4 16.6Q19.4 17.0 19.6 17.4Q20.0 17.9 20.7 17.9Q21.1 17.9 21.5 17.6Q22.0 17.2 22.0 16.4Q22.0 15.9 22.0 15.4Q21.8 15.4 21.6 15.4Q21.6 15.9 21.6 16.4Q21.6 16.9 21.4 17.2Q21.2 17.4 21.0 17.4Q20.7 17.4 20.6 17.2Q20.4 16.9 20.4 16.3Q20.4 14.7 20.4 13.2ZM19.2 15.5Q19.2 16.5 18.6 17.1Q18.0 17.6 17.3 17.6Q16.6 17.6 16.2 17.2Q15.8 16.8 15.8 16.2Q15.8 15.4 16.5 14.8Q17.4 13.9 19.2 13.9Q19.2 14.7 19.2 15.5Z" fill="#FFFFFF" fill-opacity="1.00" fill-rule="evenodd" stroke-linejoin="round"/>'
498
+ '</svg>'
499
+ )