diff --git a/manimlib/mobject/svg/text_mobject.py b/manimlib/mobject/svg/text_mobject.py index 900a6fc5..ac9b7a7a 100644 --- a/manimlib/mobject/svg/text_mobject.py +++ b/manimlib/mobject/svg/text_mobject.py @@ -20,6 +20,7 @@ from manimlib.mobject.svg.string_mobject import StringMobject from manimlib.utils.cache import cache_on_disk from manimlib.utils.color import color_to_hex from manimlib.utils.color import int_to_hex +from manimlib.utils.simple_functions import hash_string from typing import TYPE_CHECKING @@ -74,34 +75,29 @@ def markup_to_svg( pango_width = line_width / FRAME_WIDTH * DEFAULT_PIXEL_WIDTH # Write the result to a temporary svg file, and return it's contents. - # TODO, better would be to have this not write to file at all - # - # To avoid CAIRO_STATUS_WRITE_ERROR: b'error while writing to - # output stream' on Windows, we need to pass 'delete=False'. - with tempfile.NamedTemporaryFile(suffix='.svg', mode='r+', delete=False) as tmp: - manimpango.MarkupUtils.text2svg( - text=markup_str, - font="", # Already handled - slant="NORMAL", # Already handled - weight="NORMAL", # Already handled - size=1, # Already handled - _=0, # Empty parameter - disable_liga=False, - file_name=tmp.name, - START_X=0, - START_Y=0, - width=DEFAULT_CANVAS_WIDTH, - height=DEFAULT_CANVAS_HEIGHT, - justify=justify, - indent=indent, - line_spacing=None, # Already handled - alignment=alignment, - pango_width=pango_width - ) - - # Read the contents - tmp.seek(0) - return tmp.read() + temp_file = Path(tempfile.gettempdir(), hash_string(markup_str)).with_suffix(".svg") + manimpango.MarkupUtils.text2svg( + text=markup_str, + font="", # Already handled + slant="NORMAL", # Already handled + weight="NORMAL", # Already handled + size=1, # Already handled + _=0, # Empty parameter + disable_liga=False, + file_name=str(temp_file), + START_X=0, + START_Y=0, + width=DEFAULT_CANVAS_WIDTH, + height=DEFAULT_CANVAS_HEIGHT, + justify=justify, + indent=indent, + line_spacing=None, # Already handled + alignment=alignment, + pango_width=pango_width + ) + result = temp_file.read_text() + os.remove(temp_file) + return result class MarkupText(StringMobject):