refactor: refactor StringMobject

This commit is contained in:
YishiMichael 2022-08-22 21:52:48 +08:00
parent 8bd01d60e4
commit 4dfe8aff86
No known key found for this signature in database
GPG key ID: EC615C0C5A86BC80
7 changed files with 701 additions and 429 deletions

View file

@ -18,8 +18,6 @@ directories:
temporary_storage: "" temporary_storage: ""
universal_import_line: "from manimlib import *" universal_import_line: "from manimlib import *"
style: style:
# "latex" | "xelatex"
tex_compiler: "latex"
tex_template: "default" tex_template: "default"
font: "Consolas" font: "Consolas"
text_alignment: "LEFT" text_alignment: "LEFT"

View file

@ -179,13 +179,8 @@ class MTex(StringMobject):
if self.alignment: if self.alignment:
prefix_lines.append(self.alignment) prefix_lines.append(self.alignment)
if self.tex_environment: if self.tex_environment:
if isinstance(self.tex_environment, str): prefix_lines.append(f"\\begin{{{self.tex_environment}}}")
env_prefix = f"\\begin{{{self.tex_environment}}}" suffix_lines.append(f"\\end{{{self.tex_environment}}}")
env_suffix = f"\\end{{{self.tex_environment}}}"
else:
env_prefix, env_suffix = self.tex_environment
prefix_lines.append(env_prefix)
suffix_lines.append(env_suffix)
return ( return (
"".join([line + "\n" for line in prefix_lines]), "".join([line + "\n" for line in prefix_lines]),
"".join(["\n" + line for line in suffix_lines]) "".join(["\n" + line for line in suffix_lines])

View file

@ -506,13 +506,13 @@ class StringMobject(SVGMobject, ABC):
def build_parts_from_indices_lists( def build_parts_from_indices_lists(
self, indices_lists: list[list[int]] self, indices_lists: list[list[int]]
) -> VGroup: ) -> VGroup:
return VGroup(*[ return VGroup(*(
VGroup(*[ VGroup(*(
self.submobjects[submob_index] self.submobjects[submob_index]
for submob_index in indices_list for submob_index in indices_list
]) ))
for indices_list in indices_lists for indices_list in indices_lists
]) ))
def build_groups(self) -> VGroup: def build_groups(self) -> VGroup:
return self.build_parts_from_indices_lists([ return self.build_parts_from_indices_lists([

View file

@ -279,7 +279,7 @@ class MarkupText(StringMobject):
@staticmethod @staticmethod
def replace_for_matching(match_obj: re.Match) -> str: def replace_for_matching(match_obj: re.Match) -> str:
if match_obj.group("tag"): if match_obj.group("tag") or match_obj.group("passthrough"):
return "" return ""
if match_obj.group("entity"): if match_obj.group("entity"):
if match_obj.group("unicode"): if match_obj.group("unicode"):
@ -311,7 +311,7 @@ class MarkupText(StringMobject):
def get_configured_items(self) -> list[tuple[Span, dict[str, str]]]: def get_configured_items(self) -> list[tuple[Span, dict[str, str]]]:
return [ return [
*[ *(
(span, {key: val}) (span, {key: val})
for t2x_dict, key in ( for t2x_dict, key in (
(self.t2c, "foreground"), (self.t2c, "foreground"),
@ -321,12 +321,12 @@ class MarkupText(StringMobject):
) )
for selector, val in t2x_dict.items() for selector, val in t2x_dict.items()
for span in self.find_spans_by_selector(selector) for span in self.find_spans_by_selector(selector)
], ),
*[ *(
(span, local_config) (span, local_config)
for selector, local_config in self.local_configs.items() for selector, local_config in self.local_configs.items()
for span in self.find_spans_by_selector(selector) for span in self.find_spans_by_selector(selector)
] )
] ]
@staticmethod @staticmethod

File diff suppressed because it is too large Load diff

View file

@ -44,7 +44,6 @@ def init_customization() -> None:
}, },
"universal_import_line": "from manimlib import *", "universal_import_line": "from manimlib import *",
"style": { "style": {
"tex_compiler": "",
"tex_template": "", "tex_template": "",
"font": "Consolas", "font": "Consolas",
"background_color": "", "background_color": "",
@ -107,16 +106,11 @@ def init_customization() -> None:
console.print("[bold]Styles:[/bold]") console.print("[bold]Styles:[/bold]")
style_config = configuration["style"] style_config = configuration["style"]
compiler = Prompt.ask( tex_template = Prompt.ask(
" Select an executable program to use to compile a LaTeX source file", " Select a TeX template to compile a LaTeX source file",
choices=["latex", "xelatex"], default="default"
default="latex"
) )
style_config["tex_compiler"] = compiler style_config["tex_template"] = tex_template
if compiler == "latex":
style_config["tex_template"] = "default"
else:
style_config["tex_template"] = "ctex"
style_config["background_color"] = Prompt.ask( style_config["background_color"] = Prompt.ask(
" Which [bold]background color[/bold] do you want [italic](hex code)", " Which [bold]background color[/bold] do you want [italic](hex code)",
default="#333333" default="#333333"

View file

@ -15,7 +15,7 @@ from manimlib.utils.simple_functions import hash_string
SAVED_TEX_CONFIG = {} SAVED_TEX_CONFIG = {}
def get_tex_preamble(template_name: str) -> str: def get_tex_template_config(template_name: str) -> dict[str, str]:
name = template_name.replace(" ", "_").lower() name = template_name.replace(" ", "_").lower()
with open(os.path.join( with open(os.path.join(
get_manim_dir(), "manimlib", "tex_templates.yml" get_manim_dir(), "manimlib", "tex_templates.yml"
@ -27,28 +27,26 @@ def get_tex_preamble(template_name: str) -> str:
name name
) )
name = "default" name = "default"
result = templates_dict[name] return templates_dict[name]
if name not in ("default", "ctex", "basic", "ctex_basic", "blank"):
result = templates_dict["basic"] + "\n" + result
return result
def get_tex_config() -> dict[str, str]: def get_tex_config() -> dict[str, str]:
""" """
Returns a dict which should look something like this: Returns a dict which should look something like this:
{ {
"compiler": "latex",
"template": "default", "template": "default",
"compiler": "latex",
"preamble": "..." "preamble": "..."
} }
""" """
# Only load once, then save thereafter # Only load once, then save thereafter
if not SAVED_TEX_CONFIG: if not SAVED_TEX_CONFIG:
style_config = get_custom_config()["style"] template_name = get_custom_config()["style"]["tex_template"]
template_config = get_tex_template_config(template_name)
SAVED_TEX_CONFIG.update({ SAVED_TEX_CONFIG.update({
"compiler": style_config["tex_compiler"], "template": template_name,
"template": style_config["tex_template"], "compiler": template_config["compiler"],
"preamble": get_tex_preamble(style_config["tex_template"]) "preamble": template_config["preamble"]
}) })
return SAVED_TEX_CONFIG return SAVED_TEX_CONFIG
@ -58,9 +56,12 @@ def tex_content_to_svg_file(
) -> str: ) -> str:
tex_config = get_tex_config() tex_config = get_tex_config()
if not template or template == tex_config["template"]: if not template or template == tex_config["template"]:
compiler = tex_config["compiler"]
preamble = tex_config["preamble"] preamble = tex_config["preamble"]
else: else:
preamble = get_tex_preamble(template) config = get_tex_template_config(template)
compiler = config["compiler"]
preamble = config["preamble"]
if additional_preamble: if additional_preamble:
preamble += "\n" + additional_preamble preamble += "\n" + additional_preamble
@ -77,7 +78,7 @@ def tex_content_to_svg_file(
) )
if not os.path.exists(svg_file): if not os.path.exists(svg_file):
# If svg doesn't exist, create it # If svg doesn't exist, create it
create_tex_svg(full_tex, svg_file, tex_config["compiler"]) create_tex_svg(full_tex, svg_file, compiler)
return svg_file return svg_file