2022-02-14 22:55:41 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
from contextlib import contextmanager
|
2021-02-12 12:28:08 +05:30
|
|
|
import os
|
2022-03-28 17:55:50 +08:00
|
|
|
from pathlib import Path
|
2022-04-23 17:17:43 +08:00
|
|
|
import re
|
2022-02-14 22:55:41 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
import manimpango
|
2021-09-15 20:02:57 +08:00
|
|
|
import pygments
|
2022-02-26 20:31:26 +08:00
|
|
|
import pygments.formatters
|
2021-09-15 20:02:57 +08:00
|
|
|
import pygments.lexers
|
2021-06-20 01:08:17 +05:30
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
from manimlib.constants import DEFAULT_PIXEL_WIDTH, FRAME_WIDTH
|
|
|
|
from manimlib.constants import NORMAL
|
2022-01-26 19:55:47 +08:00
|
|
|
from manimlib.logger import log
|
2022-03-30 21:57:27 +08:00
|
|
|
from manimlib.mobject.svg.labelled_string import LabelledString
|
2019-08-12 09:35:05 +08:00
|
|
|
from manimlib.utils.config_ops import digest_config
|
2022-04-23 17:17:43 +08:00
|
|
|
from manimlib.utils.customization import get_customization
|
2022-02-26 20:31:26 +08:00
|
|
|
from manimlib.utils.directories import get_downloads_dir
|
|
|
|
from manimlib.utils.directories import get_text_dir
|
2022-04-23 17:17:43 +08:00
|
|
|
from manimlib.utils.tex_file_writing import tex_hash
|
2019-08-05 22:53:15 +08:00
|
|
|
|
2022-02-14 22:55:41 +08:00
|
|
|
from typing import TYPE_CHECKING
|
2022-02-16 21:08:25 +08:00
|
|
|
|
2022-02-14 22:55:41 +08:00
|
|
|
if TYPE_CHECKING:
|
2022-04-23 17:17:43 +08:00
|
|
|
from colour import Color
|
|
|
|
from typing import Iterable, Union
|
|
|
|
|
2022-04-06 22:38:33 +08:00
|
|
|
from manimlib.mobject.types.vectorized_mobject import VGroup
|
2022-04-23 17:17:43 +08:00
|
|
|
|
|
|
|
ManimColor = Union[str, Color]
|
2022-03-28 17:55:50 +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-02-14 22:55:41 +08:00
|
|
|
|
2022-04-06 22:38:33 +08:00
|
|
|
|
2021-06-19 19:33:53 +08:00
|
|
|
TEXT_MOB_SCALE_FACTOR = 0.0076
|
2021-10-01 12:32:38 -07:00
|
|
|
DEFAULT_LINE_SPACING_SCALE = 0.6
|
2022-04-23 17:17:43 +08:00
|
|
|
# Ensure the canvas is large enough to hold all glyphs.
|
|
|
|
DEFAULT_CANVAS_WIDTH = 16384
|
|
|
|
DEFAULT_CANVAS_HEIGHT = 16384
|
2020-05-07 16:21:43 +08:00
|
|
|
|
2021-09-15 20:02:57 +08:00
|
|
|
|
2022-03-28 17:55:50 +08:00
|
|
|
# See https://docs.gtk.org/Pango/pango_markup.html
|
2022-04-23 17:17:43 +08:00
|
|
|
MARKUP_COLOR_KEYS = (
|
|
|
|
"foreground", "fgcolor", "color",
|
|
|
|
"background", "bgcolor",
|
|
|
|
"underline_color",
|
|
|
|
"overline_color",
|
|
|
|
"strikethrough_color"
|
2022-03-28 17:55:50 +08:00
|
|
|
)
|
2022-04-23 17:17:43 +08:00
|
|
|
MARKUP_TAG_CONVERSION_DICT = {
|
2022-03-28 17:55:50 +08:00
|
|
|
"b": {"font_weight": "bold"},
|
|
|
|
"big": {"font_size": "larger"},
|
|
|
|
"i": {"font_style": "italic"},
|
|
|
|
"s": {"strikethrough": "true"},
|
|
|
|
"sub": {"baseline_shift": "subscript", "font_scale": "subscript"},
|
|
|
|
"sup": {"baseline_shift": "superscript", "font_scale": "superscript"},
|
|
|
|
"small": {"font_size": "smaller"},
|
|
|
|
"tt": {"font_family": "monospace"},
|
2022-04-23 17:17:43 +08:00
|
|
|
"u": {"underline": "single"}
|
2022-03-28 17:55:50 +08:00
|
|
|
}
|
2022-03-02 18:38:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Temporary handler
|
|
|
|
class _Alignment:
|
2022-03-23 12:21:40 +08:00
|
|
|
VAL_DICT = {
|
|
|
|
"LEFT": 0,
|
|
|
|
"CENTER": 1,
|
|
|
|
"RIGHT": 2
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, s: str):
|
|
|
|
self.value = _Alignment.VAL_DICT[s.upper()]
|
2022-03-02 18:38:24 +08:00
|
|
|
|
|
|
|
|
2022-03-28 17:55:50 +08:00
|
|
|
class MarkupText(LabelledString):
|
2019-08-05 22:53:15 +08:00
|
|
|
CONFIG = {
|
2022-03-28 17:55:50 +08:00
|
|
|
"is_markup": True,
|
2021-01-16 09:37:54 +08:00
|
|
|
"font_size": 48,
|
2022-02-26 20:31:26 +08:00
|
|
|
"lsh": None,
|
|
|
|
"justify": False,
|
|
|
|
"indent": 0,
|
|
|
|
"alignment": "LEFT",
|
2022-04-23 17:17:43 +08:00
|
|
|
"line_width": None,
|
2022-02-26 20:31:26 +08:00
|
|
|
"font": "",
|
2021-01-16 09:37:54 +08:00
|
|
|
"slant": NORMAL,
|
|
|
|
"weight": NORMAL,
|
2022-03-02 18:38:24 +08:00
|
|
|
"gradient": None,
|
2021-01-16 09:37:54 +08:00
|
|
|
"t2c": {},
|
|
|
|
"t2f": {},
|
|
|
|
"t2g": {},
|
|
|
|
"t2s": {},
|
|
|
|
"t2w": {},
|
2022-03-02 19:34:56 +08:00
|
|
|
"global_config": {},
|
|
|
|
"local_configs": {},
|
2019-08-05 22:53:15 +08:00
|
|
|
}
|
|
|
|
|
2022-03-23 12:21:40 +08:00
|
|
|
def __init__(self, text: str, **kwargs):
|
2021-09-15 20:02:57 +08:00
|
|
|
self.full2short(kwargs)
|
|
|
|
digest_config(self, kwargs)
|
2022-03-29 23:38:06 +08:00
|
|
|
|
2022-03-31 18:08:10 +08:00
|
|
|
if not self.font:
|
|
|
|
self.font = get_customization()["style"]["font"]
|
2022-03-29 23:38:06 +08:00
|
|
|
if self.is_markup:
|
2022-04-23 17:17:43 +08:00
|
|
|
self.validate_markup_string(text)
|
2022-03-28 17:55:50 +08:00
|
|
|
|
2021-01-16 09:35:13 +08:00
|
|
|
self.text = text
|
2022-03-28 17:55:50 +08:00
|
|
|
super().__init__(text, **kwargs)
|
2022-02-26 20:31:26 +08:00
|
|
|
|
2022-03-28 17:55:50 +08:00
|
|
|
if self.t2g:
|
|
|
|
log.warning(
|
|
|
|
"Manim currently cannot parse gradient from svg. "
|
|
|
|
"Please set gradient via `set_color_by_gradient`.",
|
|
|
|
)
|
2019-08-12 09:35:05 +08:00
|
|
|
if self.gradient:
|
|
|
|
self.set_color_by_gradient(*self.gradient)
|
2020-05-07 16:21:43 +08:00
|
|
|
if self.height is None:
|
2021-06-19 13:11:36 +08:00
|
|
|
self.scale(TEXT_MOB_SCALE_FACTOR)
|
2020-05-07 16:21:43 +08:00
|
|
|
|
2022-02-26 20:31:26 +08:00
|
|
|
@property
|
2022-03-23 12:21:40 +08:00
|
|
|
def hash_seed(self) -> tuple:
|
2022-02-26 20:31:26 +08:00
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
|
|
|
self.svg_default,
|
|
|
|
self.path_string_config,
|
2022-03-28 19:02:50 +08:00
|
|
|
self.base_color,
|
2022-03-29 23:38:06 +08:00
|
|
|
self.isolate,
|
2022-02-26 20:31:26 +08:00
|
|
|
self.text,
|
2022-03-02 18:38:24 +08:00
|
|
|
self.is_markup,
|
|
|
|
self.font_size,
|
2022-02-26 20:31:26 +08:00
|
|
|
self.lsh,
|
|
|
|
self.justify,
|
|
|
|
self.indent,
|
|
|
|
self.alignment,
|
2022-04-23 17:17:43 +08:00
|
|
|
self.line_width,
|
2022-02-26 20:31:26 +08:00
|
|
|
self.font,
|
|
|
|
self.slant,
|
|
|
|
self.weight,
|
|
|
|
self.t2c,
|
|
|
|
self.t2f,
|
|
|
|
self.t2s,
|
|
|
|
self.t2w,
|
2022-03-02 19:34:56 +08:00
|
|
|
self.global_config,
|
2022-03-29 23:38:06 +08:00
|
|
|
self.local_configs
|
2022-02-26 20:31:26 +08:00
|
|
|
)
|
2021-03-18 17:34:57 -07:00
|
|
|
|
2022-03-28 17:55:50 +08:00
|
|
|
def full2short(self, config: dict) -> None:
|
|
|
|
conversion_dict = {
|
|
|
|
"line_spacing_height": "lsh",
|
|
|
|
"text2color": "t2c",
|
|
|
|
"text2font": "t2f",
|
|
|
|
"text2gradient": "t2g",
|
|
|
|
"text2slant": "t2s",
|
|
|
|
"text2weight": "t2w"
|
|
|
|
}
|
|
|
|
for kwargs in [config, self.CONFIG]:
|
|
|
|
for long_name, short_name in conversion_dict.items():
|
|
|
|
if long_name in kwargs:
|
|
|
|
kwargs[short_name] = kwargs.pop(long_name)
|
|
|
|
|
|
|
|
def get_file_path_by_content(self, content: str) -> str:
|
2022-04-23 17:17:43 +08:00
|
|
|
hash_content = str((
|
|
|
|
content,
|
|
|
|
self.justify,
|
|
|
|
self.indent,
|
|
|
|
self.alignment,
|
|
|
|
self.line_width
|
|
|
|
))
|
2022-02-26 20:31:26 +08:00
|
|
|
svg_file = os.path.join(
|
2022-04-23 17:17:43 +08:00
|
|
|
get_text_dir(), tex_hash(hash_content) + ".svg"
|
2022-02-26 20:31:26 +08:00
|
|
|
)
|
|
|
|
if not os.path.exists(svg_file):
|
2022-03-28 17:55:50 +08:00
|
|
|
self.markup_to_svg(content, svg_file)
|
2022-02-26 20:31:26 +08:00
|
|
|
return svg_file
|
|
|
|
|
2022-03-23 12:21:40 +08:00
|
|
|
def markup_to_svg(self, markup_str: str, file_name: str) -> str:
|
2022-04-23 17:17:43 +08:00
|
|
|
self.validate_markup_string(markup_str)
|
|
|
|
|
2022-03-02 18:38:24 +08:00
|
|
|
# `manimpango` is under construction,
|
|
|
|
# so the following code is intended to suit its interface
|
|
|
|
alignment = _Alignment(self.alignment)
|
2022-04-23 17:17:43 +08:00
|
|
|
if self.line_width is None:
|
2022-03-02 18:38:24 +08:00
|
|
|
pango_width = -1
|
|
|
|
else:
|
2022-04-23 17:17:43 +08:00
|
|
|
pango_width = self.line_width / FRAME_WIDTH * DEFAULT_PIXEL_WIDTH
|
2022-03-02 18:38:24 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
return manimpango.MarkupUtils.text2svg(
|
2022-03-02 18:38:24 +08:00
|
|
|
text=markup_str,
|
|
|
|
font="", # Already handled
|
|
|
|
slant="NORMAL", # Already handled
|
|
|
|
weight="NORMAL", # Already handled
|
|
|
|
size=1, # Already handled
|
|
|
|
_=0, # Empty parameter
|
2022-03-28 18:54:43 +08:00
|
|
|
disable_liga=False,
|
2022-03-02 18:38:24 +08:00
|
|
|
file_name=file_name,
|
|
|
|
START_X=0,
|
|
|
|
START_Y=0,
|
2022-04-23 17:17:43 +08:00
|
|
|
width=DEFAULT_CANVAS_WIDTH,
|
|
|
|
height=DEFAULT_CANVAS_HEIGHT,
|
2022-03-02 18:38:24 +08:00
|
|
|
justify=self.justify,
|
|
|
|
indent=self.indent,
|
|
|
|
line_spacing=None, # Already handled
|
2022-02-26 20:31:26 +08:00
|
|
|
alignment=alignment,
|
2022-03-02 18:38:24 +08:00
|
|
|
pango_width=pango_width
|
2021-02-12 12:28:08 +05:30
|
|
|
)
|
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
@staticmethod
|
|
|
|
def validate_markup_string(markup_str: str) -> None:
|
|
|
|
validate_error = manimpango.MarkupUtils.validate(markup_str)
|
|
|
|
if not validate_error:
|
|
|
|
return
|
|
|
|
raise ValueError(
|
|
|
|
f"Invalid markup string \"{markup_str}\"\n"
|
|
|
|
f"{validate_error}"
|
|
|
|
)
|
|
|
|
|
|
|
|
def parse(self) -> None:
|
|
|
|
self.global_attr_dict = self.get_global_attr_dict()
|
|
|
|
self.tag_pairs_from_markup = self.get_tag_pairs_from_markup()
|
|
|
|
self.tag_spans = self.get_tag_spans()
|
|
|
|
self.items_from_markup = self.get_items_from_markup()
|
|
|
|
self.specified_items = self.get_specified_items()
|
|
|
|
super().parse()
|
2022-03-30 21:53:00 +08:00
|
|
|
|
2022-03-28 17:55:50 +08:00
|
|
|
# Toolkits
|
2021-03-18 17:34:57 -07:00
|
|
|
|
2022-03-28 17:55:50 +08:00
|
|
|
@staticmethod
|
|
|
|
def get_attr_dict_str(attr_dict: dict[str, str]) -> str:
|
|
|
|
return " ".join([
|
2022-03-31 18:08:10 +08:00
|
|
|
f"{key}='{val}'"
|
|
|
|
for key, val in attr_dict.items()
|
2022-03-28 17:55:50 +08:00
|
|
|
])
|
2021-09-15 20:02:57 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
# Parsing
|
2022-04-02 22:42:19 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def get_global_attr_dict(self) -> dict[str, str]:
|
|
|
|
result = {
|
|
|
|
"foreground": self.int_to_hex(self.base_color_int),
|
|
|
|
"font_family": self.font,
|
|
|
|
"font_style": self.slant,
|
|
|
|
"font_weight": self.weight,
|
|
|
|
"font_size": str(self.font_size * 1024),
|
|
|
|
}
|
|
|
|
# `line_height` attribute is supported since Pango 1.50.
|
|
|
|
pango_version = manimpango.pango_version()
|
|
|
|
if tuple(map(int, pango_version.split("."))) < (1, 50):
|
|
|
|
if self.lsh is not None:
|
|
|
|
log.warning(
|
|
|
|
f"Pango version {pango_version} found (< 1.50), "
|
|
|
|
"unable to set `line_height` attribute"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
line_spacing_scale = self.lsh or DEFAULT_LINE_SPACING_SCALE
|
|
|
|
result["line_height"] = str(((line_spacing_scale) + 1) * 0.6)
|
|
|
|
return result
|
2022-03-29 23:38:06 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def get_tag_pairs_from_markup(
|
2022-03-28 17:55:50 +08:00
|
|
|
self
|
|
|
|
) -> list[tuple[Span, Span, dict[str, str]]]:
|
|
|
|
if not self.is_markup:
|
|
|
|
return []
|
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
tag_pattern = r"""<(/?)(\w+)\s*((\w+\s*\=\s*(['"])[\s\S]*?\5\s*)*)>"""
|
|
|
|
attr_pattern = r"""(\w+)\s*\=\s*(['"])([\s\S]*?)\2"""
|
2022-03-28 17:55:50 +08:00
|
|
|
begin_match_obj_stack = []
|
|
|
|
match_obj_pairs = []
|
2022-04-23 17:17:43 +08:00
|
|
|
for match_obj in re.finditer(tag_pattern, self.string):
|
2022-03-28 17:55:50 +08:00
|
|
|
if not match_obj.group(1):
|
|
|
|
begin_match_obj_stack.append(match_obj)
|
|
|
|
else:
|
|
|
|
match_obj_pairs.append(
|
|
|
|
(begin_match_obj_stack.pop(), match_obj)
|
|
|
|
)
|
|
|
|
|
|
|
|
result = []
|
|
|
|
for begin_match_obj, end_match_obj in match_obj_pairs:
|
|
|
|
tag_name = begin_match_obj.group(2)
|
|
|
|
if tag_name == "span":
|
2022-03-29 23:38:06 +08:00
|
|
|
attr_dict = {
|
2022-03-31 18:08:10 +08:00
|
|
|
match.group(1): match.group(3)
|
2022-03-28 17:55:50 +08:00
|
|
|
for match in re.finditer(
|
|
|
|
attr_pattern, begin_match_obj.group(3)
|
|
|
|
)
|
2022-03-29 23:38:06 +08:00
|
|
|
}
|
2022-03-28 17:55:50 +08:00
|
|
|
else:
|
2022-04-23 17:17:43 +08:00
|
|
|
attr_dict = MARKUP_TAG_CONVERSION_DICT.get(tag_name, {})
|
2022-03-28 17:55:50 +08:00
|
|
|
|
|
|
|
result.append(
|
|
|
|
(begin_match_obj.span(), end_match_obj.span(), attr_dict)
|
|
|
|
)
|
|
|
|
return result
|
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def get_tag_spans(self) -> list[Span]:
|
2022-03-31 18:08:10 +08:00
|
|
|
return [
|
2022-04-23 17:17:43 +08:00
|
|
|
tag_span
|
|
|
|
for begin_tag, end_tag, _ in self.tag_pairs_from_markup
|
|
|
|
for tag_span in (begin_tag, end_tag)
|
2022-03-31 18:08:10 +08:00
|
|
|
]
|
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def get_items_from_markup(self) -> list[Span]:
|
2022-03-28 17:55:50 +08:00
|
|
|
return [
|
2022-04-23 17:17:43 +08:00
|
|
|
((begin_tag_span[1], end_tag_span[0]), attr_dict)
|
|
|
|
for begin_tag_span, end_tag_span, attr_dict
|
|
|
|
in self.tag_pairs_from_markup
|
|
|
|
if begin_tag_span[1] < end_tag_span[0]
|
2022-03-28 17:55:50 +08:00
|
|
|
]
|
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def get_specified_items(self) -> list[tuple[Span, dict[str, str]]]:
|
|
|
|
result = self.chain(
|
|
|
|
self.items_from_markup,
|
|
|
|
[
|
|
|
|
(span, {key: val})
|
|
|
|
for t2x_dict, key in (
|
|
|
|
(self.t2c, "foreground"),
|
|
|
|
(self.t2f, "font_family"),
|
|
|
|
(self.t2s, "font_style"),
|
|
|
|
(self.t2w, "font_weight")
|
|
|
|
)
|
|
|
|
for selector, val in t2x_dict.items()
|
|
|
|
for span in self.find_spans_by_selector(selector)
|
|
|
|
],
|
|
|
|
[
|
|
|
|
(span, local_config)
|
|
|
|
for selector, local_config in self.local_configs.items()
|
|
|
|
for span in self.find_spans_by_selector(selector)
|
|
|
|
],
|
|
|
|
[
|
|
|
|
(span, {})
|
|
|
|
for span in self.find_spans_by_selector(self.isolate)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
entity_spans = self.tag_spans.copy()
|
|
|
|
if self.is_markup:
|
|
|
|
entity_spans.extend(self.find_spans(r"&[\s\S]*?;"))
|
|
|
|
return [
|
|
|
|
(span, attr_dict)
|
|
|
|
for span, attr_dict in result
|
|
|
|
if not any([
|
|
|
|
entity_begin < index < entity_end
|
|
|
|
for index in span
|
|
|
|
for entity_begin, entity_end in entity_spans
|
|
|
|
])
|
|
|
|
]
|
2022-03-29 23:38:06 +08:00
|
|
|
|
2022-03-30 21:53:00 +08:00
|
|
|
def get_command_repl_items(self) -> list[tuple[Span, str]]:
|
|
|
|
result = [
|
2022-04-23 17:17:43 +08:00
|
|
|
(tag_span, "") for tag_span in self.tag_spans
|
2022-03-30 21:53:00 +08:00
|
|
|
]
|
|
|
|
if not self.is_markup:
|
2022-04-23 17:17:43 +08:00
|
|
|
result.extend([
|
2022-03-30 21:53:00 +08:00
|
|
|
(span, escaped)
|
|
|
|
for char, escaped in (
|
|
|
|
("&", "&"),
|
|
|
|
(">", ">"),
|
|
|
|
("<", "<")
|
|
|
|
)
|
2022-04-23 17:17:43 +08:00
|
|
|
for span in self.find_spans(re.escape(char))
|
|
|
|
])
|
2022-03-30 21:53:00 +08:00
|
|
|
return result
|
2022-03-28 17:55:50 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def get_specified_spans(self) -> list[Span]:
|
|
|
|
return self.remove_redundancies([
|
|
|
|
span for span, _ in self.specified_items
|
|
|
|
])
|
2022-03-31 16:15:58 +08:00
|
|
|
|
2022-03-30 21:53:00 +08:00
|
|
|
def get_label_span_list(self) -> list[Span]:
|
2022-04-23 17:17:43 +08:00
|
|
|
interval_spans = sorted(self.chain(
|
|
|
|
self.tag_spans,
|
|
|
|
[
|
|
|
|
(index, index)
|
|
|
|
for span in self.specified_spans
|
|
|
|
for index in span
|
2022-03-28 17:55:50 +08:00
|
|
|
]
|
2022-04-23 17:17:43 +08:00
|
|
|
))
|
|
|
|
text_spans = self.get_complement_spans(interval_spans, self.full_span)
|
|
|
|
if self.is_markup:
|
|
|
|
pattern = r"[0-9a-zA-Z]+|(?:&[\s\S]*?;|[^0-9a-zA-Z\s])+"
|
2022-03-31 16:15:58 +08:00
|
|
|
else:
|
2022-04-23 17:17:43 +08:00
|
|
|
pattern = r"[0-9a-zA-Z]+|[^0-9a-zA-Z\s]+"
|
|
|
|
return self.chain(*[
|
|
|
|
self.find_spans(pattern, pos=span_begin, endpos=span_end)
|
|
|
|
for span_begin, span_end in text_spans
|
|
|
|
])
|
|
|
|
|
|
|
|
def get_content(self, is_labelled: bool) -> str:
|
|
|
|
predefined_items = [
|
|
|
|
(self.full_span, self.global_attr_dict),
|
|
|
|
(self.full_span, self.global_config),
|
|
|
|
*self.specified_items
|
|
|
|
]
|
|
|
|
if is_labelled:
|
|
|
|
attr_dict_items = self.chain(
|
|
|
|
[
|
2022-04-11 23:44:33 +08:00
|
|
|
(span, {
|
2022-04-23 17:17:43 +08:00
|
|
|
key:
|
|
|
|
"black" if key.lower() in MARKUP_COLOR_KEYS else val
|
2022-04-11 23:44:33 +08:00
|
|
|
for key, val in attr_dict.items()
|
|
|
|
})
|
2022-04-23 17:17:43 +08:00
|
|
|
for span, attr_dict in predefined_items
|
2022-04-11 23:44:33 +08:00
|
|
|
],
|
2022-04-23 17:17:43 +08:00
|
|
|
[
|
2022-04-11 23:44:33 +08:00
|
|
|
(span, {"foreground": self.int_to_hex(label + 1)})
|
|
|
|
for label, span in enumerate(self.label_span_list)
|
|
|
|
]
|
2022-04-23 17:17:43 +08:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
attr_dict_items = self.chain(
|
|
|
|
predefined_items,
|
|
|
|
[
|
|
|
|
(span, {})
|
|
|
|
for span in self.label_span_list
|
|
|
|
]
|
|
|
|
)
|
2022-04-11 23:44:33 +08:00
|
|
|
inserted_string_pairs = [
|
2022-03-28 17:55:50 +08:00
|
|
|
(span, (
|
2022-04-11 23:44:33 +08:00
|
|
|
f"<span {self.get_attr_dict_str(attr_dict)}>",
|
|
|
|
"</span>"
|
2022-03-28 17:55:50 +08:00
|
|
|
))
|
2022-04-23 17:17:43 +08:00
|
|
|
for span, attr_dict in attr_dict_items if attr_dict
|
2022-03-28 17:55:50 +08:00
|
|
|
]
|
2022-04-23 17:17:43 +08:00
|
|
|
return self.get_replaced_string(
|
2022-04-11 23:44:33 +08:00
|
|
|
inserted_string_pairs, self.command_repl_items
|
|
|
|
)
|
2022-03-28 17:55:50 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
# Selector
|
|
|
|
|
|
|
|
def get_cleaned_substr(self, span: Span) -> str:
|
|
|
|
repl_items = list(filter(
|
|
|
|
lambda repl_item: self.span_contains(span, repl_item[0]),
|
|
|
|
self.command_repl_items
|
|
|
|
))
|
|
|
|
return self.get_replaced_substr(span, repl_items).strip()
|
2022-03-28 17:55:50 +08:00
|
|
|
|
|
|
|
# Method alias
|
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def get_parts_by_text(self, selector: Selector) -> VGroup:
|
|
|
|
return self.select_parts(selector)
|
2022-03-28 17:55:50 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def get_part_by_text(self, selector: Selector) -> VGroup:
|
|
|
|
return self.select_part(selector)
|
2022-03-28 17:55:50 +08:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def set_color_by_text(self, selector: Selector, color: ManimColor):
|
|
|
|
return self.set_parts_color(selector, color)
|
2022-03-28 17:55:50 +08:00
|
|
|
|
|
|
|
def set_color_by_text_to_color_map(
|
2022-04-23 17:17:43 +08:00
|
|
|
self, color_map: dict[Selector, ManimColor]
|
2022-03-28 17:55:50 +08:00
|
|
|
):
|
2022-04-23 17:17:43 +08:00
|
|
|
return self.set_parts_color_by_dict(color_map)
|
2022-03-28 17:55:50 +08:00
|
|
|
|
|
|
|
def get_text(self) -> str:
|
|
|
|
return self.get_string()
|
|
|
|
|
|
|
|
|
|
|
|
class Text(MarkupText):
|
2021-06-20 01:08:17 +05:30
|
|
|
CONFIG = {
|
2022-03-28 17:55:50 +08:00
|
|
|
"is_markup": False,
|
2021-06-20 01:08:17 +05:30
|
|
|
}
|
2021-09-15 20:02:57 +08:00
|
|
|
|
|
|
|
|
2022-03-02 18:38:24 +08:00
|
|
|
class Code(MarkupText):
|
2021-09-15 20:02:57 +08:00
|
|
|
CONFIG = {
|
|
|
|
"font": "Consolas",
|
|
|
|
"font_size": 24,
|
|
|
|
"lsh": 1.0,
|
|
|
|
"language": "python",
|
|
|
|
# Visit https://pygments.org/demo/ to have a preview of more styles.
|
|
|
|
"code_style": "monokai",
|
|
|
|
}
|
|
|
|
|
2022-03-23 12:21:40 +08:00
|
|
|
def __init__(self, code: str, **kwargs):
|
2021-09-15 20:02:57 +08:00
|
|
|
digest_config(self, kwargs)
|
2022-02-26 20:31:26 +08:00
|
|
|
self.code = code
|
2021-09-15 20:02:57 +08:00
|
|
|
lexer = pygments.lexers.get_lexer_by_name(self.language)
|
2022-03-28 17:55:50 +08:00
|
|
|
formatter = pygments.formatters.PangoMarkupFormatter(
|
|
|
|
style=self.code_style
|
|
|
|
)
|
2022-03-02 18:38:24 +08:00
|
|
|
markup = pygments.highlight(code, lexer, formatter)
|
2022-03-21 23:11:37 +08:00
|
|
|
markup = re.sub(r"</?tt>", "", markup)
|
2022-03-02 18:38:24 +08:00
|
|
|
super().__init__(markup, **kwargs)
|
2021-09-15 20:02:57 +08:00
|
|
|
|
|
|
|
|
2021-02-12 12:28:08 +05:30
|
|
|
@contextmanager
|
2022-02-14 22:55:41 +08:00
|
|
|
def register_font(font_file: str | Path):
|
2021-02-12 12:28:08 +05:30
|
|
|
"""Temporarily add a font file to Pango's search path.
|
|
|
|
This searches for the font_file at various places. The order it searches it described below.
|
|
|
|
1. Absolute path.
|
|
|
|
2. Downloads dir.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
font_file :
|
|
|
|
The font file to add.
|
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
Use ``with register_font(...)`` to add a font file to search
|
|
|
|
path.
|
|
|
|
.. code-block:: python
|
|
|
|
with register_font("path/to/font_file.ttf"):
|
|
|
|
a = Text("Hello", font="Custom Font Name")
|
|
|
|
Raises
|
|
|
|
------
|
|
|
|
FileNotFoundError:
|
|
|
|
If the font doesn't exists.
|
|
|
|
AttributeError:
|
|
|
|
If this method is used on macOS.
|
|
|
|
Notes
|
|
|
|
-----
|
|
|
|
This method of adding font files also works with :class:`CairoText`.
|
|
|
|
.. important ::
|
2021-02-19 00:49:50 +05:30
|
|
|
This method is available for macOS for ``ManimPango>=v0.2.3``. Using this
|
|
|
|
method with previous releases will raise an :class:`AttributeError` on macOS.
|
2021-02-12 12:28:08 +05:30
|
|
|
"""
|
|
|
|
|
|
|
|
input_folder = Path(get_downloads_dir()).parent.resolve()
|
|
|
|
possible_paths = [
|
|
|
|
Path(font_file),
|
|
|
|
input_folder / font_file,
|
|
|
|
]
|
|
|
|
for path in possible_paths:
|
|
|
|
path = path.resolve()
|
|
|
|
if path.exists():
|
|
|
|
file_path = path
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
error = f"Can't find {font_file}." f"Tried these : {possible_paths}"
|
|
|
|
raise FileNotFoundError(error)
|
|
|
|
|
|
|
|
try:
|
|
|
|
assert manimpango.register_font(str(file_path))
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
manimpango.unregister_font(str(file_path))
|