|
17 | 17 | from manimlib.constants import NORMAL |
18 | 18 | from manimlib.logger import log |
19 | 19 | from manimlib.mobject.svg.string_mobject import StringMobject |
| 20 | +from manimlib.mobject.svg.svg_mobject import get_svg_content_height |
20 | 21 | from manimlib.utils.cache import cache_on_disk |
21 | 22 | from manimlib.utils.color import color_to_hex |
22 | 23 | from manimlib.utils.color import int_to_hex |
|
31 | 32 | from manimlib.typing import ManimColor, Span, Selector |
32 | 33 |
|
33 | 34 |
|
34 | | -TEXT_MOB_SCALE_FACTOR = 0.0076 |
35 | 35 | DEFAULT_LINE_SPACING_SCALE = 0.6 |
36 | 36 | # Ensure the canvas is large enough to hold all glyphs. |
37 | 37 | DEFAULT_CANVAS_WIDTH = 16384 |
@@ -100,6 +100,18 @@ def markup_to_svg( |
100 | 100 | return result |
101 | 101 |
|
102 | 102 |
|
| 103 | +@lru_cache(maxsize=1) |
| 104 | +def get_text_mob_scale_factor() -> float: |
| 105 | + # Render a reference "0" and calibrate so that font_size_for_unit_height |
| 106 | + # gives a height of 1 manim unit. Compensates for platform DPI differences. |
| 107 | + ref_size = 48 |
| 108 | + font_size_for_unit_height = manim_config.text.font_size_for_unit_height |
| 109 | + pango_size = str(round(ref_size * 1024)) |
| 110 | + svg_string = markup_to_svg(f'<span font_size="{pango_size}">0</span>') |
| 111 | + svg_height = get_svg_content_height(svg_string) |
| 112 | + return ref_size / (font_size_for_unit_height * svg_height) |
| 113 | + |
| 114 | + |
103 | 115 | class MarkupText(StringMobject): |
104 | 116 | # See https://docs.gtk.org/Pango/pango_markup.html |
105 | 117 | MARKUP_TAGS = { |
@@ -188,7 +200,7 @@ def __init__( |
188 | 200 | if self.t2c: |
189 | 201 | self.set_color_by_text_to_color_map(self.t2c) |
190 | 202 | if height is None: |
191 | | - self.scale(TEXT_MOB_SCALE_FACTOR) |
| 203 | + self.scale(get_text_mob_scale_factor()) |
192 | 204 |
|
193 | 205 | def get_svg_string_by_content(self, content: str) -> str: |
194 | 206 | self.content = content |
|
0 commit comments