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

富文本

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

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

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

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

备注

这里的 ccolor 的简写

重要

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

以下列出了可用的格式:

名称

缩写

作用

参数

示例

备注

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>

参考文档

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

基类:VItem

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

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() 调用

cmpt_init_datas = {'color': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'depth': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.item.Item'>), 'fill': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'glow': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'points': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.points.Points'>), 'radius': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'stroke': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>)}
class janim.items.text.text.TextLine(text: str, fonts: list[Font], font_size: float, char_kwargs={}, fill_alpha=None, **kwargs)

基类:VItem, Group[TextChar]

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

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

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

cmpt_init_datas = {'color': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'depth': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.item.Item'>), 'fill': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'glow': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'points': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.points.Points'>), 'radius': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'stroke': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>)}
class janim.items.text.text.Text(text: str, font: str | Iterable[str] = [], font_size: float = 24, format: Format = Format.PlainText, line_kwargs: dict = {}, stroke_alpha: float = 0, fill_alpha: float = 1, **kwargs)

基类:VItem, Group[TextLine]

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

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

class Format(*values)

基类:Enum

PlainText = 0
RichText = 1
is_null() bool
idx_to_row_col(idx: int) tuple[int, int]

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

select_parts(pattern)

根据 pattern 获得文字中的部分

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

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

apply_rich_text() None

应用富文本效果

cmpt_init_datas = {'color': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'depth': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.item.Item'>), 'fill': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'glow': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'points': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.points.Points'>), 'radius': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'stroke': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>)}
class janim.items.text.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

cmpt_init_datas = {'depth': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.item.Item'>), 'points': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.points.Points'>)}
class janim.items.text.text.SourceDisplayer(obj, font_size=12, color='#888888', **kwargs)

基类:Text

显示 obj 的源代码

cmpt_init_datas = {'color': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'depth': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.item.Item'>), 'fill': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'glow': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'points': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.points.Points'>), 'radius': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>), 'stroke': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.vitem.VItem'>)}