Merge pull request #1553 from naveen521kk/fix-text

Scale Text Correctly
This commit is contained in:
Grant Sanderson 2021-06-18 12:14:32 -07:00 committed by GitHub
commit b4f23e8d8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@ import hashlib
import os import os
import re import re
import typing import typing
import warnings
from contextlib import contextmanager from contextlib import contextmanager
from pathlib import Path from pathlib import Path
@ -17,8 +18,8 @@ from manimlib.utils.directories import get_downloads_dir, get_text_dir
from manimpango import PangoUtils from manimpango import PangoUtils
from manimpango import TextSetting from manimpango import TextSetting
TEXT_MOB_SCALE_FACTOR = 0.001048 TEXT_MOB_SCALE_FACTOR = 1/100
DEFAULT_LINE_SPACING_SCALE = 0.3
class Text(SVGMobject): class Text(SVGMobject):
CONFIG = { CONFIG = {
@ -30,7 +31,7 @@ class Text(SVGMobject):
"font": '', "font": '',
"gradient": None, "gradient": None,
"lsh": -1, "lsh": -1,
"size": 1, "size": None,
"font_size": 48, "font_size": 48,
"tab_width": 4, "tab_width": 4,
"slant": NORMAL, "slant": NORMAL,
@ -46,7 +47,17 @@ class Text(SVGMobject):
def __init__(self, text, **config): def __init__(self, text, **config):
self.full2short(config) self.full2short(config)
digest_config(self, config) digest_config(self, config)
self.lsh = self.size if self.lsh == -1 else self.lsh if self.size:
warnings.warn(
"self.size has been deprecated and will "
"be removed in future.",
DeprecationWarning
)
self.font_size = self.size
if self.lsh == -1:
self.lsh = self.font_size + self.font_size * DEFAULT_LINE_SPACING_SCALE
else:
self.lsh = self.font_size + self.font_size * self.lsh
text_without_tabs = text text_without_tabs = text
if text.find('\t') != -1: if text.find('\t') != -1:
text_without_tabs = text.replace('\t', ' ' * self.tab_width) text_without_tabs = text.replace('\t', ' ' * self.tab_width)
@ -67,7 +78,7 @@ class Text(SVGMobject):
# anti-aliasing # anti-aliasing
if self.height is None: if self.height is None:
self.scale(TEXT_MOB_SCALE_FACTOR * self.font_size) self.scale(TEXT_MOB_SCALE_FACTOR)
def remove_empty_path(self, file_name): def remove_empty_path(self, file_name):
with open(file_name, 'r') as fpr: with open(file_name, 'r') as fpr:
@ -144,7 +155,7 @@ class Text(SVGMobject):
def text2hash(self): def text2hash(self):
settings = self.font + self.slant + self.weight settings = self.font + self.slant + self.weight
settings += str(self.t2f) + str(self.t2s) + str(self.t2w) settings += str(self.t2f) + str(self.t2s) + str(self.t2w)
settings += str(self.lsh) + str(self.size) settings += str(self.lsh) + str(self.font_size)
id_str = self.text + settings id_str = self.text + settings
hasher = hashlib.sha256() hasher = hashlib.sha256()
hasher.update(id_str.encode()) hasher.update(id_str.encode())
@ -198,8 +209,8 @@ class Text(SVGMobject):
def text2svg(self): def text2svg(self):
# anti-aliasing # anti-aliasing
size = self.size * 10 size = self.font_size
lsh = self.lsh * 10 lsh = self.lsh
if self.font == '': if self.font == '':
self.font = get_customization()['style']['font'] self.font = get_customization()['style']['font']
@ -210,8 +221,8 @@ class Text(SVGMobject):
if os.path.exists(file_name): if os.path.exists(file_name):
return file_name return file_name
settings = self.text2settings() settings = self.text2settings()
width = 600 width = DEFAULT_PIXEL_WIDTH
height = 400 height = DEFAULT_PIXEL_HEIGHT
disable_liga = self.disable_ligatures disable_liga = self.disable_ligatures
return manimpango.text2svg( return manimpango.text2svg(
settings, settings,