Skip to content

Commit e786eb6

Browse files
committed
Fix Write() stroke width to scale with font_size or object scale
1 parent 3c6a1ee commit e786eb6

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

manim/animation/creation.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def construct(self):
8585
from manim.mobject.text.text_mobject import Text
8686
from manim.scene.scene import Scene
8787

88-
from manim.constants import RIGHT, TAU
88+
from manim.constants import DEFAULT_FONT_SIZE, DEFAULT_STROKE_WIDTH, RIGHT, TAU
8989
from manim.mobject.opengl.opengl_surface import OpenGLSurface
9090
from manim.mobject.opengl.opengl_vectorized_mobject import OpenGLVMobject
9191
from manim.utils.color import ManimColor
@@ -94,6 +94,7 @@ def construct(self):
9494
from ..animation.animation import Animation
9595
from ..animation.composition import Succession
9696
from ..mobject.mobject import Group, Mobject
97+
from ..mobject.svg.svg_mobject import SVGMobject
9798
from ..mobject.types.vectorized_mobject import VMobject
9899
from ..utils.bezier import integer_interpolate
99100
from ..utils.rate_functions import double_smooth, linear
@@ -322,6 +323,7 @@ def construct(self):
322323
def __init__(
323324
self,
324325
vmobject: VMobject | OpenGLVMobject,
326+
stroke_width: float | None = None,
325327
rate_func: Callable[[float], float] = linear,
326328
reverse: bool = False,
327329
**kwargs,
@@ -333,11 +335,13 @@ def __init__(
333335
run_time,
334336
lag_ratio,
335337
)
338+
stroke_width = self._adjust_stroke_width_for_text(vmobject, stroke_width)
336339
self.reverse = reverse
337340
if "remover" not in kwargs:
338341
kwargs["remover"] = reverse
339342
super().__init__(
340343
vmobject,
344+
stroke_width=stroke_width,
341345
rate_func=rate_func,
342346
run_time=run_time,
343347
lag_ratio=lag_ratio,
@@ -358,6 +362,19 @@ def _set_default_config_from_length(
358362
lag_ratio = min(4.0 / max(1.0, length), 0.2)
359363
return run_time, lag_ratio
360364

365+
def _adjust_stroke_width_for_text(
366+
self,
367+
vmobject: VMobject | OpenGLVMobject,
368+
stroke_width: float | None,
369+
scale_factor: float = 0.25,
370+
) -> float:
371+
if stroke_width is not None:
372+
return stroke_width
373+
if not isinstance(vmobject, SVGMobject):
374+
return 2.0 # default in DrawBorderThenFill
375+
font_size = getattr(vmobject, "font_size", DEFAULT_FONT_SIZE)
376+
return (font_size / DEFAULT_FONT_SIZE) * DEFAULT_STROKE_WIDTH * scale_factor
377+
361378
def reverse_submobjects(self) -> None:
362379
self.mobject.invert(recursive=True)
363380

0 commit comments

Comments
 (0)