text

文字物件的结构

文字物件的结构比较复杂,Text 包含若干个 TextLine,每个 TextLine 包含若干个 TextChar

比如,对于

txt = Text('The first line.\nThe second line.\nThe third line.')

那么下图便说明了它的子物件情况, txt[0]txt[1]txt[2] 都是 TextLine

../../_images/text_children.png

对于每个 TextLine 而言,比如 txt[0],其子物件情况是如下图这样的

../../_images/textline_children.png

提示

其中 txt[0][3]txt[0][9] 是空格

也就是说这是一个 TextTextLineTextChar 的嵌套结构

对于这种复杂的嵌套结构,如果你想要取子物件列表的切片,手动数数可能有点繁琐(比如上面例子的首行中,"first" 对应的切片是 [4:9]

为了解决这个问题,你可以参考预览界面的 子物件选择 功能

字符的标记属性

TextChar 有四个标记属性,比如对于 “Ggf” 中的 “g” 字符而言:

../../_images/char_mark.png

备注

TextLine 也有类似的结构,但是只有 origrightup,没有 advance

有关更多与基线原点有关的内容,请参考 BasepointVItem

富文本

可以使用起始标记和结束标记(像 HTML 那样的)应用富文本格式:

具体写法是: <格式名 参数>被应用对应格式的文本</格式名>

比如,要想让文字的一部分变为蓝色,可以这样书写:

Text('Hello <c BLUE>JAnim</c>!', format=Text.Format.RichText)

format=Text.Format.RichText 也可以简写为 format='rich'

Text('Hello <c BLUE>JAnim</c>!', format='rich')

备注

这里的 ccolor 的简写

重要

Text 使用富文本需要传入 format=Text.Format.RichText 或者 format='rich',否则默认情况下视作普通文本

以下列出了可用的格式:

名称

缩写

作用

参数

示例

备注

color

c

颜色

颜色名称

<c BLUE>JAnim</c>

十六进制值

<c #00ff00>JAnim</c>

r g b

<c 0 1.0 0.5>JAnim</c>

r g b a

<c 0 1.0 0.5 0.5>JAnim</c>

描边也被设置为半透明

stroke_color

sc

描边颜色

同上

fill_color

fc

填充颜色

同上

alpha

a

透明度

一个数

<a 0.5>JAnim</a>

描边也被设置为半透明

stroke_alpha

sa

描边透明度

同上

fill_alpha

fa

填充透明度

同上

stroke

s

描边半径

一个数

<s 0.01>JAnim</s>

font_scale

fs

缩放倍数

一个数

Hello <fs 1.2>JAnim</fs>

小技巧

如果你想直接输入 < 符号,不希望其被解析为富文本标记,可以使用 << 来表示一个 < 符号,例如:

Text('if x << 10 <c RED>and</c> x > 2:', format='rich')

参考文档

class janim.items.text.Cmpt_Mark_TextCharImpl(*args, **kwargs)

基类:Cmpt_Mark, Generic

names: list[str] = ['orig', 'right', 'up', 'advance']
class janim.items.text.Cmpt_Mark_TextLineImpl(*args, **kwargs)

基类:Cmpt_Mark, Generic

names: list[str] = ['orig', 'right', 'up']
class janim.items.text.ProjType(*values)

基类:StrEnum

Horizontal = 'horizontal'
Vertical = 'vertial'
H = 'h'
V = 'v'
class janim.items.text.BasepointVItem(*args, **kwargs)

基类:MarkedItem, VItem

offset_to(other: BasepointVItem, proj: ProjType | Literal['horizontal', 'vertical', 'h', 'v'] | Vect | None = None) ndarray

计算从 selfother 的偏移量,如果指定了 proj 则只计算在该方向上的投影量

例如当我们想要让一段文字和另一段文字的基线对齐,可以使用 .offset_to(other, 'v') 计算基线垂直方向的偏移量,从而根据该量移动来对齐基线。

matrix_of_marks() matrix

得到标记点的坐标标架构成的矩阵

class janim.items.text.TextChar(char: str, fonts: list[Font], font_size: float, fill_alpha=None, **kwargs)

基类:BasepointVItem

字符物件,作为 TextLine 的子物件,在创建 TextLine 时产生

mark
static get_font_for_render(unicode: str, fonts: list[Font]) Font

从字体列表中找到支持显示 unicode 的字体,如果找不到只好选用第一个

get_mark_orig() ndarray
get_mark_right() ndarray
get_mark_up() ndarray
get_mark_advance() ndarray
get_advance_length() float
apply_act_list(act_params_map: dict[str, ActParamsStack]) None

应用富文本样式,由 Text.apply_rich_text() 调用

class janim.items.text.TextLine(text: str, fonts: list[Font], font_size: float, char_kwargs={}, fill_alpha=None, **kwargs)

基类:BasepointVItem, Group[TextChar]

单行文字物件,作为 Text 的子物件,在创建 Text 时产生

mark
get_mark_orig() ndarray
get_mark_right() ndarray
get_mark_up() ndarray
arrange_in_line(buff: float = 0) Self

根据 advance 的标记信息排列该行

match_to(target: Text | TextLine) Self

将该行文本与目标文本或文本行对齐

参数:

target -- 目标文本或文本行

class janim.items.text.Text(text: str, font: str | Iterable[str] = [], font_size: float = 24, weight: int | Weight | WeightName = 400, style: Style | StyleName = Style.Normal, force_full_name: bool = False, format: Format | Literal['plain', 'rich'] = Format.PlainText, line_kwargs: dict = {}, stroke_alpha: float = 0, fill_alpha: float = 1, stroke_background: bool = True, center: bool = True, **kwargs)

基类:VItem, Group[TextLine]

文字物件,支持富文本等功能

如果对换行排版等有较高的需求可以考虑使用 TypstDoc 以及 TypstText

示例:

Text('Hello World!')
Text('Hello <c RED>World</c>!', format='rich')
class Format(*values)

基类:StrEnum

PlainText = 'plain'
RichText = 'rich'
is_null() bool
idx_to_row_col(idx: int) tuple[int, int]

由字符索引得到 行数、列数 索引

select_parts(pattern: str | Pattern, group: int = 0)

根据 pattern 正则表达式 获得文字中的部分

参数:
  • pattern -- 用于匹配的正则表达式

  • group -- 对于正则表达式,指定使用第几个分组进行匹配,默认 0 表示整个匹配片段,其余数字表示对应的分组

提示:如果不希望使用正则表达式,可以使用 re.escape 进行转义,例如 re.escape('a[i]') 来正确匹配字符串中的 a[i]

示例:

txt = Text('Hello World!')
txt.select_parts('World').set(color=RED)

上面这个示例会选取出 Hello World! 中的 World 部分,并将其颜色设置为红色

txt = Text('for i in range(100) if i % 3 == 0 or i % 5 == 0')
txt.select_parts(r'[^f](or)', 1).set(color=BLUE)

上面这个示例会选取出其中的 or 部分,并且避免选取 for 中的 or

arrange_in_lines(buff: float = 0, base_buff: float = 0.85) Self
参数:
  • buff -- 每行之间的额外间距

  • base_buff -- 每行之间的基本间距,默认值 0.85 用于将两行上下排列,如果是 0 则会让两行完全重合,大部分时候不需要传入该值

match_to(target: Text | TextLine, *, self_lineno: int = 0) Self

将该文本与目标文本或文本行对齐

参数:
  • target -- 目标文本或文本行

  • self_lineno -- 将自身的哪一行与目标对齐,默认为首行(即 self_lineno=0

apply_rich_text() None

应用富文本效果

class janim.items.text.Title(text: str, font: str | Iterable[str] = [], font_size: float = 24, include_underline: bool = True, underline_width: float | None = None, underline_buff: float = 0.25, match_underline_width_to_text: bool = False, depth: float | None = None, **kwargs)

基类:Group

标题

  • include_underline=True 会添加下划线(默认添加)

  • underline_width 下划线的长度(默认 屏幕宽 - 2个单位

  • match_underline_width_to_text=True 时将下划线的长度和文字匹配(默认为 False

class janim.items.text.SourceDisplayer(obj, font_size=12, color='#888888', **kwargs)

基类:Text

显示 obj 的源代码