2022-03-31 16:15:58 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from manimlib.mobject.svg.labelled_string import LabelledString
|
|
|
|
from manimlib.utils.tex_file_writing import display_during_execution
|
2022-04-23 17:17:43 +08:00
|
|
|
from manimlib.utils.tex_file_writing import get_tex_config
|
|
|
|
from manimlib.utils.tex_file_writing import tex_to_svg_file
|
2022-03-31 16:15:58 +08:00
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2022-04-23 17:17:43 +08:00
|
|
|
from colour import Color
|
2022-05-02 22:40:06 +08:00
|
|
|
import re
|
2022-04-23 17:17:43 +08:00
|
|
|
from typing import Iterable, Union
|
|
|
|
|
2022-03-31 16:15:58 +08:00
|
|
|
from manimlib.mobject.types.vectorized_mobject import VGroup
|
2022-04-23 17:17:43 +08:00
|
|
|
|
|
|
|
ManimColor = Union[str, Color]
|
2022-03-31 16:15:58 +08:00
|
|
|
Span = tuple[int, int]
|
2022-04-23 17:17:43 +08:00
|
|
|
Selector = Union[
|
|
|
|
str,
|
|
|
|
re.Pattern,
|
|
|
|
tuple[Union[int, None], Union[int, None]],
|
|
|
|
Iterable[Union[
|
|
|
|
str,
|
|
|
|
re.Pattern,
|
|
|
|
tuple[Union[int, None], Union[int, None]]
|
|
|
|
]]
|
|
|
|
]
|
2022-03-31 16:15:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
SCALE_FACTOR_PER_FONT_POINT = 0.001
|
|
|
|
|
|
|
|
|
|
|
|
class MTex(LabelledString):
|
|
|
|
CONFIG = {
|
|
|
|
"font_size": 48,
|
|
|
|
"alignment": "\\centering",
|
|
|
|
"tex_environment": "align*",
|
|
|
|
"tex_to_color_map": {},
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, tex_string: str, **kwargs):
|
|
|
|
# Prevent from passing an empty string.
|
2022-04-23 17:17:43 +08:00
|
|
|
if not tex_string.strip():
|
2022-04-11 23:44:33 +08:00
|
|
|
tex_string = "\\\\"
|
2022-03-31 16:15:58 +08:00
|
|
|
self.tex_string = tex_string
|
|
|
|
super().__init__(tex_string, **kwargs)
|
|
|
|
|
2022-05-03 23:39:37 +08:00
|
|
|
self.set_color_by_tex_to_color_map(self.tex_to_color_map)
|
2022-03-31 16:15:58 +08:00
|
|
|
self.scale(SCALE_FACTOR_PER_FONT_POINT * self.font_size)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def hash_seed(self) -> tuple:
|
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
|
|
|
self.svg_default,
|
|
|
|
self.path_string_config,
|
|
|
|
self.base_color,
|
|
|
|
self.isolate,
|
|
|
|
self.tex_string,
|
|
|
|
self.alignment,
|
|
|
|
self.tex_environment,
|
|
|
|
self.tex_to_color_map
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_file_path_by_content(self, content: str) -> str:
|
|
|
|
tex_config = get_tex_config()
|
2022-04-11 23:44:33 +08:00
|
|
|
full_tex = tex_config["tex_body"].replace(
|
2022-03-31 16:15:58 +08:00
|
|
|
tex_config["text_to_replace"],
|
|
|
|
content
|
|
|
|
)
|
2022-04-23 17:17:43 +08:00
|
|
|
with display_during_execution(f"Writing \"{self.string}\""):
|
2022-04-11 23:44:33 +08:00
|
|
|
file_path = tex_to_svg_file(full_tex)
|
|
|
|
return file_path
|
2022-03-31 16:15:58 +08:00
|
|
|
|
2022-05-02 22:40:06 +08:00
|
|
|
#@property
|
|
|
|
#def sort_labelled_submobs(self) -> bool:
|
|
|
|
# return False
|
2022-03-31 16:15:58 +08:00
|
|
|
|
|
|
|
# Toolkits
|
|
|
|
|
|
|
|
@staticmethod
|
2022-05-02 22:40:06 +08:00
|
|
|
def get_color_command_str(rgb_hex: str) -> str:
|
|
|
|
rgb = MTex.hex_to_int(rgb_hex)
|
|
|
|
rg, b = divmod(rgb, 256)
|
2022-04-23 17:17:43 +08:00
|
|
|
r, g = divmod(rg, 256)
|
|
|
|
return f"\\color[RGB]{{{r}, {g}, {b}}}"
|
2022-03-31 16:15:58 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
@staticmethod
|
2022-05-03 23:39:37 +08:00
|
|
|
def get_tag_string_pair(
|
|
|
|
attr_dict: dict[str, str], label_hex: str | None
|
|
|
|
) -> tuple[str, str]:
|
|
|
|
if label_hex is None:
|
|
|
|
return ("", "")
|
|
|
|
return ("{{" + MTex.get_color_command_str(label_hex), "}}")
|
2022-05-02 22:40:06 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
# Parsing
|
2022-03-31 16:15:58 +08:00
|
|
|
|
2022-05-04 21:56:13 +08:00
|
|
|
def get_command_spans(self) -> tuple[list[Span], list[Span], list[Span]]:
|
|
|
|
cmd_spans = self.find_spans(r"\\(?:[a-zA-Z]+|\s|\S)")
|
|
|
|
begin_cmd_spans = [
|
|
|
|
span
|
|
|
|
for span in self.find_spans("{")
|
|
|
|
if (span[0] - 1, span[1]) not in cmd_spans
|
|
|
|
]
|
|
|
|
end_cmd_spans = [
|
|
|
|
span
|
|
|
|
for span in self.find_spans("}")
|
|
|
|
if (span[0] - 1, span[1]) not in cmd_spans
|
|
|
|
]
|
|
|
|
return begin_cmd_spans, end_cmd_spans, cmd_spans
|
|
|
|
|
|
|
|
def get_specified_items(
|
|
|
|
self, cmd_span_pairs: list[tuple[Span, Span]]
|
|
|
|
) -> list[tuple[Span, dict[str, str]]]:
|
|
|
|
cmd_content_spans = [
|
2022-05-02 22:40:06 +08:00
|
|
|
(span_begin, span_end)
|
2022-05-04 21:56:13 +08:00
|
|
|
for (_, span_begin), (span_end, _) in cmd_span_pairs
|
2022-04-23 17:17:43 +08:00
|
|
|
]
|
2022-05-04 21:56:13 +08:00
|
|
|
specified_spans = self.chain(
|
|
|
|
[
|
|
|
|
cmd_content_spans[range_begin]
|
|
|
|
for _, (range_begin, range_end) in self.compress_neighbours([
|
|
|
|
(span_begin + index, span_end - index)
|
|
|
|
for index, (span_begin, span_end) in enumerate(
|
|
|
|
cmd_content_spans
|
|
|
|
)
|
|
|
|
])
|
|
|
|
if range_end - range_begin >= 2
|
|
|
|
],
|
|
|
|
[
|
|
|
|
span
|
|
|
|
for selector in self.tex_to_color_map
|
|
|
|
for span in self.find_spans_by_selector(selector)
|
|
|
|
],
|
|
|
|
self.find_spans_by_selector(self.isolate)
|
|
|
|
)
|
|
|
|
return [(span, {}) for span in specified_spans]
|
2022-03-31 16:15:58 +08:00
|
|
|
|
2022-05-04 21:56:13 +08:00
|
|
|
def get_replaced_substr(self, substr: str, flag: int) -> str:
|
2022-05-04 22:18:19 +08:00
|
|
|
return substr
|
2022-05-04 21:56:13 +08:00
|
|
|
|
2022-05-03 23:39:37 +08:00
|
|
|
def get_full_content_string(self, content_string: str, is_labelled: bool) -> str:
|
|
|
|
result = content_string
|
2022-03-31 16:15:58 +08:00
|
|
|
|
2022-04-11 23:44:33 +08:00
|
|
|
if self.tex_environment:
|
2022-04-23 17:17:43 +08:00
|
|
|
if isinstance(self.tex_environment, str):
|
|
|
|
prefix = f"\\begin{{{self.tex_environment}}}"
|
|
|
|
suffix = f"\\end{{{self.tex_environment}}}"
|
|
|
|
else:
|
|
|
|
prefix, suffix = self.tex_environment
|
|
|
|
result = "\n".join([prefix, result, suffix])
|
2022-04-11 23:44:33 +08:00
|
|
|
if self.alignment:
|
|
|
|
result = "\n".join([self.alignment, result])
|
2022-05-02 22:40:06 +08:00
|
|
|
|
2022-05-03 23:39:37 +08:00
|
|
|
if not is_labelled:
|
2022-04-11 23:44:33 +08:00
|
|
|
result = "\n".join([
|
2022-05-02 22:40:06 +08:00
|
|
|
self.get_color_command_str(self.base_color_hex),
|
2022-04-11 23:44:33 +08:00
|
|
|
result
|
|
|
|
])
|
|
|
|
return result
|
2022-03-31 16:15:58 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
# Selector
|
2022-03-31 16:15:58 +08:00
|
|
|
|
|
|
|
def get_cleaned_substr(self, span: Span) -> str:
|
2022-05-04 21:56:13 +08:00
|
|
|
backslash_indices = [
|
|
|
|
index for index, _ in self.find_spans(r"\\[\s\S]")
|
|
|
|
]
|
|
|
|
ignored_indices = [
|
|
|
|
index
|
|
|
|
for index, _ in self.find_spans(r"[\s_^{}]")
|
|
|
|
if index - 1 not in backslash_indices
|
|
|
|
]
|
|
|
|
span_begin, span_end = span
|
|
|
|
while span_begin in ignored_indices:
|
|
|
|
span_begin += 1
|
|
|
|
while span_end - 1 in ignored_indices:
|
|
|
|
span_end -= 1
|
|
|
|
shrinked_span = (span_begin, span_end)
|
|
|
|
|
|
|
|
whitespace_repl_items = []
|
|
|
|
for whitespace_span in self.find_spans(r"\s+"):
|
|
|
|
if not self.span_contains(shrinked_span, whitespace_span):
|
|
|
|
continue
|
|
|
|
if whitespace_span[0] - 1 in backslash_indices:
|
|
|
|
whitespace_span = (whitespace_span[0] + 1, whitespace_span[1])
|
|
|
|
if all(
|
|
|
|
self.get_substr((index, index + 1)).isalpha()
|
|
|
|
for index in (whitespace_span[0] - 1, whitespace_span[1])
|
|
|
|
):
|
|
|
|
replaced_substr = " "
|
|
|
|
else:
|
|
|
|
replaced_substr = ""
|
|
|
|
whitespace_repl_items.append((whitespace_span, replaced_substr))
|
|
|
|
|
2022-05-04 22:18:19 +08:00
|
|
|
_, unclosed_right_braces, unclosed_left_braces = self.split_span_by_levels(shrinked_span)
|
2022-05-04 21:56:13 +08:00
|
|
|
return "".join([
|
|
|
|
unclosed_right_braces * "{",
|
|
|
|
self.replace_string(shrinked_span, whitespace_repl_items),
|
|
|
|
unclosed_left_braces * "}"
|
|
|
|
])
|
|
|
|
|
2022-03-31 16:15:58 +08:00
|
|
|
# Method alias
|
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def get_parts_by_tex(self, selector: Selector) -> VGroup:
|
|
|
|
return self.select_parts(selector)
|
2022-03-31 16:15:58 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def get_part_by_tex(self, selector: Selector) -> VGroup:
|
|
|
|
return self.select_part(selector)
|
2022-03-31 16:15:58 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def set_color_by_tex(self, selector: Selector, color: ManimColor):
|
|
|
|
return self.set_parts_color(selector, color)
|
2022-03-31 16:15:58 +08:00
|
|
|
|
|
|
|
def set_color_by_tex_to_color_map(
|
2022-04-23 17:17:43 +08:00
|
|
|
self, color_map: dict[Selector, ManimColor]
|
2022-03-31 16:15:58 +08:00
|
|
|
):
|
2022-04-23 17:17:43 +08:00
|
|
|
return self.set_parts_color_by_dict(color_map)
|
2022-03-31 16:15:58 +08:00
|
|
|
|
|
|
|
def get_tex(self) -> str:
|
|
|
|
return self.get_string()
|
|
|
|
|
|
|
|
|
|
|
|
class MTexText(MTex):
|
|
|
|
CONFIG = {
|
|
|
|
"tex_environment": None,
|
|
|
|
}
|