Repository: 3b1b/manim
File: manimlib/mobject/svg/text_mobject.py (Python)

Code before:
            self.set_color_by_t2g()

        # anti-aliasing
        if self.height is None and not self.debug:
            self.load_scale_factor()
            self.scale(self.scale_factor * self.font_size)
    
    def load_scale_factor(self):
        factor_file = os.path.join(get_temp_dir(), "text_scale_factor.txt")
        if os.path.exists(factor_file):
            with open(factor_file, 'r') as f:
                self.scale_factor = float(f.read())
        else:
            tex_height = TexText("b", font_size=1).get_height()
            text_height = Text("b", font_size=1, debug=True).get_height()
            self.scale_factor = tex_height / text_height
            with open(factor_file, "w") as f:
                f.write(str(self.scale_factor))

    def remove_empty_path(self, file_name):
        with open(file_name, 'r') as fpr:
            content = fpr.read()
        content = re.sub(r'<path .*?d=""/>', '', content)
        with open(file_name, 'w') as fpw:
            fpw.write(content)

    def apply_space_chars(self):
        submobs = self.submobjects.copy()
        for char_index in range(len(self.text)):
            if self.text[char_index] in [" ", "\t", "\n"]:
                space = Dot(radius=0, fill_opacity=0, stroke_opacity=0)
                space.move_to(submobs[max(char_index - 1, 0)].get_center())
                submobs.insert(char_index, space)
        self.set_submobjects(submobs)

    def find_indexes(self, word):
        m = re.match(r'\[([0-9\-]{0,}):([0-9\-]{0,})\]', word)
        if m:
            start = int(m.group(1)) if m.group(1) != '' else 0
            end = int(m.group(2)) if m.group(2) != '' else len(self.text)
            start = len(self.text) + start if start < 0 else start
            end = len(self.text) + end if end < 0 else end
            return [(start, end)]

        indexes = []
        index = self.text.find(word)
        while index != -1:
            indexes.append((index, index + len(word)))
            index = self.text.find(word, index + len(word))
        return indexes


Review comment (suggestion):
I worry this would mean that for rendering text LaTeX would be a must. Also, there should a better solution than this. Both the size needn't need to match but they should be made to fit into the screen.

Code after:
        file_name = self.text2svg()
        PangoUtils.remove_last_M(file_name)
        self.remove_empty_path(file_name)
        SVGMobject.__init__(self, file_name, **config)
        self.text = text
        if self.disable_ligatures:
            self.apply_space_chars()
        if self.t2c:
            self.set_color_by_t2c()
        if self.gradient:
            self.set_color_by_gradient(*self.gradient)
        if self.t2g:
            self.set_color_by_t2g()

        # anti-aliasing
        if self.height is None and not self.debug:
            self.load_scale_factor()
            self.scale(self.scale_factor * self.font_size)
    
    def load_scale_factor(self):
        factor_file = os.path.join(get_temp_dir(), "text_scale_factor.txt")
        if os.path.exists(factor_file):
            with open(factor_file, 'r') as f:
                self.scale_factor = float(f.read())
        else:
            tex_height = TexText("b", font_size=1).get_height()
            text_height = Text("b", font_size=1, debug=True).get_height()
            self.scale_factor = tex_height / text_height
            with open(factor_file, "w") as f:
                f.write(str(self.scale_factor))

    def remove_empty_path(self, file_name):
        with open(file_name, 'r') as fpr:
            content = fpr.read()
        content = re.sub(r'<path .*?d=""/>', '', content)
        with open(file_name, 'w') as fpw:
            fpw.write(content)

    def apply_space_chars(self):
        submobs = self.submobjects.copy()
        for char_index in range(len(self.text)):
            if self.text[char_index] in [" ", "\t", "\n"]:
                space = Dot(radius=0, fill_opacity=0, stroke_opacity=0)
                space.move_to(submobs[max(char_index - 1, 0)].get_center())
                submobs.insert(char_index, space)
        self.set_submobjects(submobs)

    def find_indexes(self, word):
        m = re.match(r'\[([0-9\-]{0,}):([0-9\-]{0,})\]', word)
        if m:
            start = int(m.group(1)) if m.group(1) != '' else 0