3b1b / manim

Animation engine for explanatory math videos
MIT License
70.96k stars 6.24k forks source link

Text Error On non-English languages #1793

Open HumbleHumbert opened 2 years ago

HumbleHumbert commented 2 years ago

Describe the error

I recently installed manimgl on an env of Annaconda using python3.9. And met with this problem when trying to relize the last example in #680

Code and Error

Code:

from manimlib import *

script = '''
Hello
你好
こんにちは
안녕하세요
'''

class Demo(Scene):
    def construct(self):
        text = Text(script, font='Source Han Sans')
        self.play(Write(text))

is what the .py

then manimgl ***.py Demo

Error:

ManimGL v1.6.1
[20:23:00] WARNING  You may be using Windows platform and have not specified the path of                  config.py:232
                    `temporary_storage`, which may cause OSError. So it is recommended to specify the
                    `temporary_storage` in the config file (.yml)
Traceback (most recent call last):
  File "D:\Programming\Anaconda3\envs\manim\Scripts\manimgl-script.py", line 33, in <module>
    sys.exit(load_entry_point('manimgl', 'console_scripts', 'manimgl')())
  File "c:\users\unrul\manim\manimlib\__main__.py", line 25, in main
    scene.run()
  File "c:\users\unrul\manim\manimlib\scene\scene.py", line 91, in run
    self.construct()
  File "ppw.py", line 35, in construct
    text = Text(script, font='Source Han Sans')
  File "c:\users\unrul\manim\manimlib\mobject\svg\text_mobject.py", line 149, in __init__
    super().__init__(text, **kwargs)
  File "c:\users\unrul\manim\manimlib\mobject\svg\labelled_string.py", line 65, in __init__
    self.post_parse()
  File "c:\users\unrul\manim\manimlib\mobject\svg\labelled_string.py", line 122, in post_parse
    self.group_items = self.get_group_items()
  File "c:\users\unrul\manim\manimlib\mobject\svg\labelled_string.py", line 414, in get_group_items
    ordered_spans = [
  File "c:\users\unrul\manim\manimlib\mobject\svg\labelled_string.py", line 415, in <listcomp>
    self.label_span_list[label] if label != -1 else self.full_span
IndexError: list index out of range

Environment

win 10: manim 1.6.1: master python 3.9:

And actually, after I tried, it happens every time when I try to put any non-English languages in the Text, and all ended up the same IndexError: list index out of range

Can I get any help?

HumbleHumbert commented 2 years ago

right, I tried both MikTex and Texlive, the latter worked few years ago. But neither now.

TonyCrane commented 2 years ago

I can run your code without any error:

image

And notice that Text doesn't use LaTeX to generate svg file, it uses pango. Tex and TexText use LaTeX, and if you want to use it generate non-English text, you may have to modify the config file and using xelatex as tex executable instead of latex.

HumbleHumbert commented 2 years ago

Yes, I have seen your solution somewhere, and I went to the file C:\Users\unrul\manim\manimlib\default_config.yml . And did as

tex:
  executable: "xelatex -no-pdf"
  template_file: "ctex_template.tex"
  intermediate_filetype: "xdv"
  text_to_replace: "[tex_expression]"
  # For ctex, use the following configuration
  # executable: "xelatex -no-pdf"
  # template_file: "ctex_template.tex"
  # intermediate_filetype: "xdv"

only to get the result

(process:2736): Pango-WARNING **: 11:51:39.163: couldn't load font "Source Han Sans Not-Rotated 48", falling back to "Sans Not-Rotated 48", expect ugly output.
c:\users\unrul\manim\manimlib\utils\space_ops.py:155: RuntimeWarning: invalid value encountered in double_scalars
  cos_angle = np.dot(v1, v2) / np.float64(n1 * n2)
Traceback (most recent call last):
  File "D:\Programming\Anaconda3\envs\manim\Scripts\manimgl-script.py", line 33, in <module>
    sys.exit(load_entry_point('manimgl', 'console_scripts', 'manimgl')())
  File "c:\users\unrul\manim\manimlib\__main__.py", line 25, in main
    scene.run()
  File "c:\users\unrul\manim\manimlib\scene\scene.py", line 91, in run
    self.construct()
  File "ppw.py", line 13, in construct
    text = Text(script, font='Source Han Sans')
  File "c:\users\unrul\manim\manimlib\mobject\svg\text_mobject.py", line 149, in __init__
    super().__init__(text, **kwargs)
  File "c:\users\unrul\manim\manimlib\mobject\svg\labelled_string.py", line 65, in __init__
    self.post_parse()
  File "c:\users\unrul\manim\manimlib\mobject\svg\labelled_string.py", line 122, in post_parse
    self.group_items = self.get_group_items()
  File "c:\users\unrul\manim\manimlib\mobject\svg\labelled_string.py", line 414, in get_group_items
    ordered_spans = [
  File "c:\users\unrul\manim\manimlib\mobject\svg\labelled_string.py", line 415, in <listcomp>
    self.label_span_list[label] if label != -1 else self.full_span
IndexError: list index out of range

ending with the same IndexError. Looks like something to do with the Pango? And btw I enabled the UTF-8 Beta for Chinese if that matters.

NolanWY commented 2 years ago

Have you installed the font Source Han Sans ?

yusancky commented 2 years ago

I got a same problem too.

yusancky commented 2 years ago

@NolanWY : Have you installed the font Source Han Sans ?

I think it is not the reason. I tried with another font which I had, but the problem appeared again.

However, I found that it worked well if I deleted the Chinese characters. That means the following #ffeBe9 code didn't work but the #dafbe1 code worked well.

- script = 'Hello 你好'
+ script = 'Hello'
text = Text(script,font = "LXGWWenKai")
ghost commented 2 years ago

I had the same problem. But when I specified the font to be used in the code, it became normal.

+ text = Text("我是一个 Text", font = "Source Han Sans"
- text = Text("我是一个 Text")

I think it would be helpful to specify a different font.

yusancky commented 2 years ago

After I chose another font and replaced the name of it as the font in the code, the problem seems to be resolved for me.

Get the name of the font
class Demo(Scene):
    def construct(self):
        script = 'Hello 你好'
-       text = Text(scrtpt,font = 'LXGWWenKai')
+       text = Text(scrtpt,font = 'HarmonyOS Sans SC Light')
        self.play(Write(text))
        self.wait()

https://user-images.githubusercontent.com/83524927/193445681-5dad32c8-f7c7-42a2-ae98-92d34fb97ffb.mp4


In addition, It's inconvenient to use a font with non-English name. manimpango.list_fonts() will help you.