代码

限定名称: manim.mobject.text.code\_mobject.Code

class Code(file_name=None, code=None, tab_width=3, line_spacing=0.3, font_size=24, font='Monospace', stroke_width=0, margin=0.3, indentation_chars='    ', background='rectangle', background_stroke_width=1, background_stroke_color=ManimColor('#FFFFFF'), corner_radius=0.2, insert_line_no=True, line_no_from=1, line_no_buff=0.4, style='vim', language=None, generate_html_file=False, warn_missing_font=True, **kwargs)[source]

基础类: VGroup

一个高亮的源代码列表。

一个对象 listing代码 是由三个对象组成的 VGroup

  • 背景,listing.background_mobject。这要么是一个Rectangle(如果列表已使用background="rectangle"初始化,这是默认选项),要么是一个类似于窗口的VGroup(如果传递了background="window")。

  • 行号,listing.line_numbers(一个段落对象)。

  • 高亮显示的代码本身,listing.code(一个段落对象)。

警告

在带有前导空格的文本(特别是在这种情况下:代码)上使用Transform可能会看起来奇怪。考虑使用remove_invisible_chars()来解决这个问题。

示例

正常用法:

listing = Code(
    "helloworldcpp.cpp",
    tab_width=4,
    background_stroke_width=1,
    background_stroke_color=WHITE,
    insert_line_no=True,
    style=Code.styles_list[15],
    background="window",
    language="cpp",
)

我们也可以渲染作为字符串传递的代码(但请注意,在这种情况下必须指定语言):

示例:CodeFromString

../_images/CodeFromString-1.png
from manim import *

class CodeFromString(Scene):
    def construct(self):
        code = '''from manim import Scene, Square

class FadeInSquare(Scene):
    def construct(self):
        s = Square()
        self.play(FadeIn(s))
        self.play(s.animate.scale(2))
        self.wait()
'''
        rendered_code = Code(code=code, tab_width=4, background="window",
                            language="Python", font="Monospace")
        self.add(rendered_code)
class CodeFromString(Scene):
    def construct(self):
        code = '''from manim import Scene, Square

class FadeInSquare(Scene):
    def construct(self):
        s = Square()
        self.play(FadeIn(s))
        self.play(s.animate.scale(2))
        self.wait()
'''
        rendered_code = Code(code=code, tab_width=4, background="window",
                            language="Python", font="Monospace")
        self.add(rendered_code)

Parameters:
  • file_name (str | os.PathLike | None) – 要显示的代码文件的名称。

  • code (str | None) – 如果未指定file_name,可以直接传递代码字符串。

  • tab_width (int) – 对应一个制表符的空格字符数。默认为3。

  • line_spacing (float) – 行与行之间的间距与字体大小的关系。默认为0.3,表示字体大小的30%。

  • font_size (float) – 一个用于缩放显示代码的数字。默认为24。

  • font (str) – 要使用的文本字体的名称。默认为 "Monospace"。 这可以是系统字体,也可以是使用 text.register_font() 加载的字体。请注意, 字体家族名称可能因操作系统而异。

  • stroke_width (float) – 文本的描边宽度。推荐值为0,也是默认值。

  • margin (float) – 文本与背景的内边距。默认为 0.3。

  • indentation_chars (str) – “缩进字符”指的是给定代码行开头的空格/制表符。默认为 "    "(空格)。

  • 背景 (str) – 定义背景的类型。目前仅支持 "rectangle"(默认)和 "window"

  • background_stroke_width (float) – 定义背景的描边宽度。默认为1。

  • background_stroke_color (str) – 定义背景的描边颜色。默认为 WHITE

  • corner_radius (float) – 定义背景的圆角半径。默认为0.2。

  • insert_line_no (bool) – 定义是否应在显示的代码中插入行号。默认为 True

  • line_no_from (int) – 定义行计数中第一行的编号。默认为1。

  • line_no_buff (float) – 定义行号与显示代码之间的间距。默认为0.4。

  • style (str) – 定义显示代码的样式类型。您可以在styles_list中查看可能的样式名称。默认为"vim"

  • 语言 (str | None) – 指定给定代码所使用的编程语言。如果为None (默认值),语言将自动检测。有关可能的选项列表,请访问https://pygments.org/docs/lexers/并查找 ‘别名或简称’。

  • generate_html_file (bool) – 定义是否生成高亮的html代码到文件夹 assets/codes/generated_html_files。默认为 False

  • warn_missing_font (bool) – 如果为True(默认值),当字体不存在于manimpango.list_fonts()返回的(区分大小写的)字体列表中时,Manim将发出警告。

background_mobject

代码列表的背景。

Type:

VGroup

line_numbers

代码列表的行号。如果指定了insert_line_no=False,则为空。

Type:

段落

code

高亮显示的代码。

Type:

段落

方法

属性

animate

用于动画化self的任何方法的应用。

animation_overrides

color

depth

mobject的深度。

fill_color

如果有多种颜色(用于渐变),则返回第一个颜色

height

mobject的高度。

n_points_per_curve

sheen_factor

stroke_color

styles_list

width

mobject的宽度。

_correct_non_span(line_str)[source]

函数将文本颜色应用于那些没有根据显示代码的background_color设置颜色的字符串。

Parameters:

line_str (str) – 接受一个HTML元素的字符串,根据显示代码的background_color为其添加颜色。

Returns:

生成的带有颜色属性的html元素的字符串。

Return type:

str

_ensure_valid_file()[source]

用于验证文件的函数。

_gen_code_json()[来源]

函数用于从html_string生成background_color、code_json和tab_spaces。 background_color只是显示代码的背景颜色。 code_json是一个二维数组,行作为行号, 列作为长度为2的数组,包含文本和文本的颜色值。 tab_spaces是一个二维数组,行作为行号, 列作为代码中该行前面的缩进字符的相应数量。

_gen_colored_lines()[source]

生成代码的函数。

Returns:

根据参数生成的代码。

Return type:

段落

_gen_html_string()[来源]

生成带有代码高亮的HTML字符串并将其存储在变量html_string中的函数。

_gen_line_numbers()[source]

生成行号的函数。

Returns:

根据参数生成的行号。

Return type:

段落

_original__init__(file_name=None, code=None, tab_width=3, line_spacing=0.3, font_size=24, font='Monospace', stroke_width=0, margin=0.3, indentation_chars='    ', background='rectangle', background_stroke_width=1, background_stroke_color=ManimColor('#FFFFFF'), corner_radius=0.2, insert_line_no=True, line_no_from=1, line_no_buff=0.4, style='vim', language=None, generate_html_file=False, warn_missing_font=True, **kwargs)

初始化自身。有关准确的签名,请参阅 help(type(self))。

Parameters:
  • file_name (str | PathLike | None)

  • code (str | None)

  • tab_width (int)

  • line_spacing (float)

  • font_size (float)

  • 字体 (字符串)

  • stroke_width (float)

  • margin (float)

  • indentation_chars (str)

  • 背景 (str)

  • background_stroke_width (float)

  • background_stroke_color (str)

  • corner_radius (float)

  • insert_line_no (bool)

  • line_no_from (int)

  • line_no_buff (float)

  • style (str)

  • 语言 (str | )

  • generate_html_file (bool)

  • warn_missing_font (bool)