Renderer¶
Renderer 是编译、测量、静态渲染、SVG 和动画的主要入口。
Renderer(cache_max_bytes=...) 控制每个渲染器的资源缓存,font_cache_max_bytes 则限制它
通过 cache_font 持有的字体。字形缓存是进程级的,需要在首次使用之前单独配置。
takumi_py.renderer.set_glyph_cache_max_bytes ¶
Set the process-wide glyph cache budget before the first render.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_bytes
|
int
|
Maximum retained glyph-cache size in bytes. Use |
required |
The budget only applies to a glyph cache that has not been used yet, so later calls do not resize it.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
takumi_py.renderer.Renderer ¶
Compile, measure, and render Takumi nodes, HTML, SVG, and animations.
__init__ ¶
__init__(
*,
load_default_fonts: bool = True,
fonts: Sequence[FontResourceInput] | None = None,
cache_max_bytes: int | None = None,
font_cache_max_bytes: int | None = None,
) -> None
Create a renderer with optional font and cache configuration.
cache_max_bytes controls the renderer-local resource cache for decoded
images, SVG rasters, and related render resources. Pass 0 to disable
retention or None to use Takumi's default budget.
font_cache_max_bytes bounds the decoded fonts that cache_font() keeps.
Past it, the least recently used of them are released; fonts registered with
register_font() or fonts are never released for space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
load_default_fonts
|
bool
|
Load Takumi's bundled default fonts. |
True
|
fonts
|
Sequence[FontResourceInput] | None
|
Font resources to register during construction. |
None
|
cache_max_bytes
|
int | None
|
Maximum renderer-local resource-cache size in bytes. |
None
|
font_cache_max_bytes
|
int | None
|
Maximum decoded size of cached fonts in bytes, or
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TakumiError
|
If native renderer initialization fails. |
compile_node ¶
Compile a Python node mapping for repeated rendering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
NodeInput
|
Node mapping accepted by Takumi. |
required |
validate
|
bool
|
Validate and normalize the mapping in Python before passing it to the native decoder. |
False
|
Returns:
| Type | Description |
|---|---|
CompiledNode
|
An opaque compiled node owned by the native binding. |
Raises:
| Type | Description |
|---|---|
NodeValidationError
|
If Python-side validation is enabled and fails. |
NodeDecodeError
|
If the native decoder rejects the node. |
compile_stylesheet ¶
Compile CSS and fail if it cannot be parsed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
css
|
str
|
CSS source text. |
required |
Returns:
| Type | Description |
|---|---|
CompiledStyleSheet
|
An opaque compiled stylesheet suitable for repeated rendering. |
Raises:
| Type | Description |
|---|---|
StyleSheetError
|
If the stylesheet is invalid. |
compile_stylesheet_lossy ¶
Compile CSS using Takumi's error-tolerant HTML stylesheet parser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
css
|
str
|
CSS source text, potentially containing declarations that Takumi cannot parse. |
required |
Returns:
| Type | Description |
|---|---|
CompiledStyleSheet
|
A compiled stylesheet containing the declarations Takumi accepted. |
compile_keyframes ¶
Compile structured keyframe rules into a stylesheet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keyframes
|
KeyframesInput
|
Keyframe mappings or rule objects accepted by Takumi. |
required |
Returns:
| Type | Description |
|---|---|
CompiledStyleSheet
|
A compiled stylesheet containing the animation rules. |
Raises:
| Type | Description |
|---|---|
StyleSheetError
|
If a keyframe rule cannot be compiled. |
register_font ¶
Register one font resource with this renderer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
font
|
FontResourceInput
|
Raw font bytes or a |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
The font-family names discovered or assigned during registration. |
Raises:
| Type | Description |
|---|---|
FontError
|
If Takumi cannot decode or register the font. |
register_fonts ¶
Register multiple font resources with this renderer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fonts
|
Sequence[FontResourceInput]
|
Raw fonts or |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
All font-family names discovered or assigned during registration. |
Raises:
| Type | Description |
|---|---|
FontError
|
If Takumi cannot decode or register a font. |
cache_font ¶
Register a font under key that the renderer may release for space.
Use it for fonts that renders load on demand. Past font_cache_max_bytes,
the least recently used cached families are released, other than the ones
this call registered; a render marks the families in its font_families
used. Check cached_fonts(key) before loading a font again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
font
|
FontResourceInput
|
Raw font bytes or a |
required |
key
|
str
|
Identifies the font, such as the URL it was downloaded from. Caching another font under the same key replaces the record. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
The font-family names discovered or assigned during registration. |
Raises:
| Type | Description |
|---|---|
FontError
|
If Takumi cannot decode or register the font. |
cached_fonts ¶
Return the families a cache_font() call under key registered.
A hit marks the families used, so they are the last to be released.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key passed to |
required |
pin
|
bool
|
Keep the families from ever being released for space. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[str, ...] | None
|
The family names, or |
tuple[str, ...] | None
|
of its families has since been released or unregistered. |
unregister_font ¶
Remove every face registered under a font-family name.
All unicode_range subsets registered under name are removed
together, so later renders treat the family as unknown and fall back, and
cached_fonts() no longer reports the keys that registered it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Font-family name returned by |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
clear_fonts ¶
Remove every registered font, including fonts passed to __init__.
Only the bundled default font remains, and only when the renderer was
created with load_default_fonts=True.
render_compiled ¶
render_compiled(
node: CompiledNode,
*,
stylesheets: Sequence[CompiledStyleSheet] | None = None,
keyframes: KeyframesInput | UnsetType | None = UNSET,
options: RenderOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
format: ImageOutputFormat | UnsetType = UNSET,
quality: int | UnsetType | None = UNSET,
lossless: bool | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
device_pixel_ratio: float | UnsetType = UNSET,
draw_debug_border: bool | UnsetType = UNSET,
time_ms: int | UnsetType = UNSET,
dithering: DitheringAlgorithm | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
) -> bytes
Render a compiled node to an encoded image or raw RGBA bytes.
Explicit keyword arguments override matching fields in options;
parameters left as UNSET inherit the supplied or default option.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
CompiledNode
|
Previously compiled node. |
required |
stylesheets
|
Sequence[CompiledStyleSheet] | None
|
Previously compiled stylesheets applied in order. |
None
|
keyframes
|
KeyframesInput | UnsetType | None
|
Structured animation rules, |
UNSET
|
options
|
RenderOptions | None
|
Base render configuration. |
None
|
width
|
int | UnsetType | None
|
Output viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
Output viewport height, or |
UNSET
|
format
|
ImageOutputFormat | UnsetType
|
Static output format. |
UNSET
|
quality
|
int | UnsetType | None
|
Lossy encoder quality from 0 through 100 where supported. |
UNSET
|
lossless
|
bool | UnsetType | None
|
Whether to use lossless WebP encoding. |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
device_pixel_ratio
|
float | UnsetType
|
Scale factor applied to raster output dimensions. |
UNSET
|
draw_debug_border
|
bool | UnsetType
|
Draw Takumi's layout debug borders. |
UNSET
|
time_ms
|
int | UnsetType
|
Animation sampling time in milliseconds. |
UNSET
|
dithering
|
DitheringAlgorithm | UnsetType
|
Color-dithering algorithm for formats that use it. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
Returns:
| Type | Description |
|---|---|
bytes
|
Encoded image bytes, or raw RGBA pixels when |
Raises:
| Type | Description |
|---|---|
RenderError
|
If layout or rendering fails. |
ResourceError
|
If an image resource cannot be decoded. |
UnsupportedFormatError
|
If the requested output cannot be encoded. |
ValueError
|
If an output option combination is invalid. |
measure_compiled ¶
measure_compiled(
node: CompiledNode,
*,
stylesheets: Sequence[CompiledStyleSheet] | None = None,
keyframes: KeyframesInput | UnsetType | None = UNSET,
options: RenderOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
device_pixel_ratio: float | UnsetType = UNSET,
draw_debug_border: bool | UnsetType = UNSET,
time_ms: int | UnsetType = UNSET,
dithering: DitheringAlgorithm | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
) -> MeasuredNode
Measure a compiled node without encoding an image.
Explicit keyword arguments override matching fields in options;
parameters left as UNSET inherit the supplied or default option.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
CompiledNode
|
Previously compiled node. |
required |
stylesheets
|
Sequence[CompiledStyleSheet] | None
|
Previously compiled stylesheets applied in order. |
None
|
keyframes
|
KeyframesInput | UnsetType | None
|
Structured animation rules, |
UNSET
|
options
|
RenderOptions | None
|
Base render configuration. |
None
|
width
|
int | UnsetType | None
|
Layout viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
Layout viewport height, or |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
device_pixel_ratio
|
float | UnsetType
|
Scale factor used during layout. |
UNSET
|
draw_debug_border
|
bool | UnsetType
|
Enable Takumi's debug-border layout mode. |
UNSET
|
time_ms
|
int | UnsetType
|
Animation sampling time in milliseconds. |
UNSET
|
dithering
|
DitheringAlgorithm | UnsetType
|
Dithering option passed to the native measurement context. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
Returns:
| Type | Description |
|---|---|
MeasuredNode
|
An immutable measurement tree with geometry and shaped text runs. |
Raises:
| Type | Description |
|---|---|
RenderError
|
If layout or measurement fails. |
ResourceError
|
If an image resource cannot be decoded. |
NodeDecodeError
|
If Takumi returns an invalid measurement payload. |
render_node ¶
render_node(
node: NodeInput,
*,
stylesheets: Sequence[str] | None = None,
keyframes: KeyframesInput | None = None,
options: RenderOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
format: ImageOutputFormat | UnsetType = UNSET,
quality: int | UnsetType | None = UNSET,
lossless: bool | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
device_pixel_ratio: float | UnsetType = UNSET,
draw_debug_border: bool | UnsetType = UNSET,
time_ms: int | UnsetType = UNSET,
dithering: DitheringAlgorithm | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
validate: bool = False,
) -> bytes
Compile and render a Python node mapping.
Explicit render keywords override matching fields in options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
NodeInput
|
Node mapping accepted by Takumi. |
required |
stylesheets
|
Sequence[str] | None
|
CSS source strings compiled with strict parsing. |
None
|
keyframes
|
KeyframesInput | None
|
Structured animation rules appended after |
None
|
options
|
RenderOptions | None
|
Base render configuration. |
None
|
width
|
int | UnsetType | None
|
Output viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
Output viewport height, or |
UNSET
|
format
|
ImageOutputFormat | UnsetType
|
Static output format. |
UNSET
|
quality
|
int | UnsetType | None
|
Lossy encoder quality from 0 through 100 where supported. |
UNSET
|
lossless
|
bool | UnsetType | None
|
Whether to use lossless WebP encoding. |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
device_pixel_ratio
|
float | UnsetType
|
Scale factor applied to raster output dimensions. |
UNSET
|
draw_debug_border
|
bool | UnsetType
|
Draw Takumi's layout debug borders. |
UNSET
|
time_ms
|
int | UnsetType
|
Animation sampling time in milliseconds. |
UNSET
|
dithering
|
DitheringAlgorithm | UnsetType
|
Color-dithering algorithm for formats that use it. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
validate
|
bool
|
Validate and normalize |
False
|
Returns:
| Type | Description |
|---|---|
bytes
|
Encoded image bytes, or raw RGBA pixels when |
Raises:
| Type | Description |
|---|---|
NodeValidationError
|
If Python-side validation is enabled and fails. |
NodeDecodeError
|
If the native decoder rejects the node. |
StyleSheetError
|
If a stylesheet or keyframe rule is invalid. |
RenderError
|
If layout or rendering fails. |
UnsupportedFormatError
|
If the requested output cannot be encoded. |
measure_node ¶
measure_node(
node: NodeInput,
*,
stylesheets: Sequence[str] | None = None,
keyframes: KeyframesInput | None = None,
options: RenderOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
device_pixel_ratio: float | UnsetType = UNSET,
draw_debug_border: bool | UnsetType = UNSET,
time_ms: int | UnsetType = UNSET,
dithering: DitheringAlgorithm | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
validate: bool = False,
) -> MeasuredNode
Compile and measure a Python node mapping without encoding an image.
Explicit render keywords override matching fields in options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
NodeInput
|
Node mapping accepted by Takumi. |
required |
stylesheets
|
Sequence[str] | None
|
CSS source strings compiled with strict parsing. |
None
|
keyframes
|
KeyframesInput | None
|
Structured animation rules appended after |
None
|
options
|
RenderOptions | None
|
Base render configuration. |
None
|
width
|
int | UnsetType | None
|
Layout viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
Layout viewport height, or |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
device_pixel_ratio
|
float | UnsetType
|
Scale factor used during layout. |
UNSET
|
draw_debug_border
|
bool | UnsetType
|
Enable Takumi's debug-border layout mode. |
UNSET
|
time_ms
|
int | UnsetType
|
Animation sampling time in milliseconds. |
UNSET
|
dithering
|
DitheringAlgorithm | UnsetType
|
Dithering option passed to the native measurement context. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
validate
|
bool
|
Validate and normalize |
False
|
Returns:
| Type | Description |
|---|---|
MeasuredNode
|
An immutable measurement tree with geometry and shaped text runs. |
Raises:
| Type | Description |
|---|---|
NodeValidationError
|
If Python-side validation is enabled and fails. |
NodeDecodeError
|
If node decoding or measurement conversion fails. |
StyleSheetError
|
If a stylesheet or keyframe rule is invalid. |
RenderError
|
If layout or measurement fails. |
compile_html ¶
Parse HTML into a compiled Takumi node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html
|
str
|
HTML fragment or document source. |
required |
html_options
|
HtmlOptions | None
|
Parser presets, Tailwind attribute name, and maximum
element depth. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
CompiledHtml
|
The compiled root node and a stylesheet bundle. The current parser |
CompiledHtml
|
returns an empty bundle because |
Raises:
| Type | Description |
|---|---|
HtmlParseError
|
If the HTML cannot be parsed or exceeds configured limits. |
render_html ¶
render_html(
html: str,
*,
stylesheets: Sequence[str] | None = None,
keyframes: KeyframesInput | None = None,
html_options: HtmlOptions | None = None,
options: RenderOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
format: ImageOutputFormat | UnsetType = UNSET,
quality: int | UnsetType | None = UNSET,
lossless: bool | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
device_pixel_ratio: float | UnsetType = UNSET,
draw_debug_border: bool | UnsetType = UNSET,
time_ms: int | UnsetType = UNSET,
dithering: DitheringAlgorithm | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
) -> bytes
Parse HTML and render it to an encoded image or raw RGBA bytes.
External CSS uses the lossy parser appropriate for browser-authored
stylesheets. Explicit render keywords override matching fields in
options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html
|
str
|
HTML fragment or document source. |
required |
stylesheets
|
Sequence[str] | None
|
Additional CSS source strings applied in order. |
None
|
keyframes
|
KeyframesInput | None
|
Structured animation rules appended after |
None
|
html_options
|
HtmlOptions | None
|
HTML parsing configuration. |
None
|
options
|
RenderOptions | None
|
Base render configuration. |
None
|
width
|
int | UnsetType | None
|
Output viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
Output viewport height, or |
UNSET
|
format
|
ImageOutputFormat | UnsetType
|
Static output format. |
UNSET
|
quality
|
int | UnsetType | None
|
Lossy encoder quality from 0 through 100 where supported. |
UNSET
|
lossless
|
bool | UnsetType | None
|
Whether to use lossless WebP encoding. |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
device_pixel_ratio
|
float | UnsetType
|
Scale factor applied to raster output dimensions. |
UNSET
|
draw_debug_border
|
bool | UnsetType
|
Draw Takumi's layout debug borders. |
UNSET
|
time_ms
|
int | UnsetType
|
Animation sampling time in milliseconds. |
UNSET
|
dithering
|
DitheringAlgorithm | UnsetType
|
Color-dithering algorithm for formats that use it. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
Returns:
| Type | Description |
|---|---|
bytes
|
Encoded image bytes, or raw RGBA pixels when |
Raises:
| Type | Description |
|---|---|
HtmlParseError
|
If the HTML cannot be parsed. |
StyleSheetError
|
If structured keyframes cannot be compiled. |
RenderError
|
If layout or rendering fails. |
UnsupportedFormatError
|
If the requested output cannot be encoded. |
measure_html ¶
measure_html(
html: str,
*,
stylesheets: Sequence[str] | None = None,
keyframes: KeyframesInput | None = None,
html_options: HtmlOptions | None = None,
options: RenderOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
device_pixel_ratio: float | UnsetType = UNSET,
draw_debug_border: bool | UnsetType = UNSET,
time_ms: int | UnsetType = UNSET,
dithering: DitheringAlgorithm | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
) -> MeasuredNode
Parse and measure HTML without encoding an image.
External CSS uses the lossy parser appropriate for browser-authored
stylesheets. Explicit render keywords override matching fields in
options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html
|
str
|
HTML fragment or document source. |
required |
stylesheets
|
Sequence[str] | None
|
Additional CSS source strings applied in order. |
None
|
keyframes
|
KeyframesInput | None
|
Structured animation rules appended after |
None
|
html_options
|
HtmlOptions | None
|
HTML parsing configuration. |
None
|
options
|
RenderOptions | None
|
Base render configuration. |
None
|
width
|
int | UnsetType | None
|
Layout viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
Layout viewport height, or |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
device_pixel_ratio
|
float | UnsetType
|
Scale factor used during layout. |
UNSET
|
draw_debug_border
|
bool | UnsetType
|
Enable Takumi's debug-border layout mode. |
UNSET
|
time_ms
|
int | UnsetType
|
Animation sampling time in milliseconds. |
UNSET
|
dithering
|
DitheringAlgorithm | UnsetType
|
Dithering option passed to the native measurement context. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
Returns:
| Type | Description |
|---|---|
MeasuredNode
|
An immutable measurement tree with geometry and shaped text runs. |
Raises:
| Type | Description |
|---|---|
HtmlParseError
|
If the HTML cannot be parsed. |
StyleSheetError
|
If structured keyframes cannot be compiled. |
RenderError
|
If layout or measurement fails. |
NodeDecodeError
|
If Takumi returns an invalid measurement payload. |
render_template ¶
render_template(
template_name: str,
context: Mapping[str, object],
*,
template_dir: str | Path = ".",
stylesheets: Sequence[str] | None = None,
keyframes: KeyframesInput | None = None,
html_options: HtmlOptions | None = None,
options: RenderOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
format: ImageOutputFormat | UnsetType = UNSET,
quality: int | UnsetType | None = UNSET,
lossless: bool | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
device_pixel_ratio: float | UnsetType = UNSET,
draw_debug_border: bool | UnsetType = UNSET,
time_ms: int | UnsetType = UNSET,
dithering: DitheringAlgorithm | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
) -> bytes
Render a Jinja template through the HTML pipeline.
Templates are loaded from template_dir using Jinja's standard
filesystem loader and autoescaping rules. Explicit render keywords override
matching fields in options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template_name
|
str
|
Template path relative to |
required |
context
|
Mapping[str, object]
|
Values exposed to the Jinja template. |
required |
template_dir
|
str | Path
|
Root directory used to load templates. |
'.'
|
stylesheets
|
Sequence[str] | None
|
Additional CSS source strings applied in order. |
None
|
keyframes
|
KeyframesInput | None
|
Structured animation rules appended after |
None
|
html_options
|
HtmlOptions | None
|
HTML parsing configuration. |
None
|
options
|
RenderOptions | None
|
Base render configuration. |
None
|
width
|
int | UnsetType | None
|
Output viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
Output viewport height, or |
UNSET
|
format
|
ImageOutputFormat | UnsetType
|
Static output format. |
UNSET
|
quality
|
int | UnsetType | None
|
Lossy encoder quality from 0 through 100 where supported. |
UNSET
|
lossless
|
bool | UnsetType | None
|
Whether to use lossless WebP encoding. |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
device_pixel_ratio
|
float | UnsetType
|
Scale factor applied to raster output dimensions. |
UNSET
|
draw_debug_border
|
bool | UnsetType
|
Draw Takumi's layout debug borders. |
UNSET
|
time_ms
|
int | UnsetType
|
Animation sampling time in milliseconds. |
UNSET
|
dithering
|
DitheringAlgorithm | UnsetType
|
Color-dithering algorithm for formats that use it. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
Returns:
| Type | Description |
|---|---|
bytes
|
Encoded image bytes, or raw RGBA pixels when |
Raises:
| Type | Description |
|---|---|
TemplateError
|
If Jinja cannot load or render the template. |
HtmlParseError
|
If the rendered HTML cannot be parsed. |
RenderError
|
If layout or rendering fails. |
UnsupportedFormatError
|
If the requested output cannot be encoded. |
render_svg_compiled ¶
render_svg_compiled(
node: CompiledNode,
*,
stylesheets: Sequence[CompiledStyleSheet] | None = None,
keyframes: KeyframesInput | UnsetType | None = UNSET,
options: RenderOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
time_ms: int | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
) -> str
Render a compiled node as an SVG document.
Explicit keyword arguments override matching fields in options;
raster-only options in RenderOptions are ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
CompiledNode
|
Previously compiled node. |
required |
stylesheets
|
Sequence[CompiledStyleSheet] | None
|
Previously compiled stylesheets applied in order. |
None
|
keyframes
|
KeyframesInput | UnsetType | None
|
Structured animation rules, |
UNSET
|
options
|
RenderOptions | None
|
Base render configuration. |
None
|
width
|
int | UnsetType | None
|
SVG viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
SVG viewport height, or |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
time_ms
|
int | UnsetType
|
Animation sampling time in milliseconds. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
Returns:
| Type | Description |
|---|---|
str
|
A complete SVG document as Unicode text. |
Raises:
| Type | Description |
|---|---|
RenderError
|
If layout or SVG generation fails. |
ResourceError
|
If an image resource cannot be decoded. |
render_svg_node ¶
render_svg_node(
node: NodeInput,
*,
stylesheets: Sequence[str] | None = None,
keyframes: KeyframesInput | None = None,
options: RenderOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
time_ms: int | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
validate: bool = False,
) -> str
Compile a Python node mapping and render it as SVG.
Explicit render keywords override matching fields in options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
NodeInput
|
Node mapping accepted by Takumi. |
required |
stylesheets
|
Sequence[str] | None
|
CSS source strings compiled with strict parsing. |
None
|
keyframes
|
KeyframesInput | None
|
Structured animation rules appended after |
None
|
options
|
RenderOptions | None
|
Base render configuration. |
None
|
width
|
int | UnsetType | None
|
SVG viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
SVG viewport height, or |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
time_ms
|
int | UnsetType
|
Animation sampling time in milliseconds. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
validate
|
bool
|
Validate and normalize |
False
|
Returns:
| Type | Description |
|---|---|
str
|
A complete SVG document as Unicode text. |
Raises:
| Type | Description |
|---|---|
NodeValidationError
|
If Python-side validation is enabled and fails. |
NodeDecodeError
|
If the native decoder rejects the node. |
StyleSheetError
|
If a stylesheet or keyframe rule is invalid. |
RenderError
|
If layout or SVG generation fails. |
render_svg_html ¶
render_svg_html(
html: str,
*,
stylesheets: Sequence[str] | None = None,
keyframes: KeyframesInput | None = None,
html_options: HtmlOptions | None = None,
options: RenderOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
time_ms: int | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
) -> str
Parse HTML and render it as an SVG document.
External CSS uses the lossy parser appropriate for browser-authored
stylesheets. Explicit render keywords override matching fields in
options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html
|
str
|
HTML fragment or document source. |
required |
stylesheets
|
Sequence[str] | None
|
Additional CSS source strings applied in order. |
None
|
keyframes
|
KeyframesInput | None
|
Structured animation rules appended after |
None
|
html_options
|
HtmlOptions | None
|
HTML parsing configuration. |
None
|
options
|
RenderOptions | None
|
Base render configuration. |
None
|
width
|
int | UnsetType | None
|
SVG viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
SVG viewport height, or |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
time_ms
|
int | UnsetType
|
Animation sampling time in milliseconds. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
Returns:
| Type | Description |
|---|---|
str
|
A complete SVG document as Unicode text. |
Raises:
| Type | Description |
|---|---|
HtmlParseError
|
If the HTML cannot be parsed. |
StyleSheetError
|
If structured keyframes cannot be compiled. |
RenderError
|
If layout or SVG generation fails. |
render_svg_template ¶
render_svg_template(
template_name: str,
context: Mapping[str, object],
*,
template_dir: str | Path = ".",
stylesheets: Sequence[str] | None = None,
keyframes: KeyframesInput | None = None,
html_options: HtmlOptions | None = None,
options: RenderOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
time_ms: int | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
) -> str
Render a Jinja template as an SVG document through the HTML pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template_name
|
str
|
Template path relative to |
required |
context
|
Mapping[str, object]
|
Values exposed to the Jinja template. |
required |
template_dir
|
str | Path
|
Root directory used to load templates. |
'.'
|
stylesheets
|
Sequence[str] | None
|
Additional CSS source strings applied in order. |
None
|
keyframes
|
KeyframesInput | None
|
Structured animation rules appended after |
None
|
html_options
|
HtmlOptions | None
|
HTML parsing configuration. |
None
|
options
|
RenderOptions | None
|
Base render configuration. |
None
|
width
|
int | UnsetType | None
|
SVG viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
SVG viewport height, or |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
time_ms
|
int | UnsetType
|
Animation sampling time in milliseconds. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
Returns:
| Type | Description |
|---|---|
str
|
A complete SVG document as Unicode text. |
Raises:
| Type | Description |
|---|---|
TemplateError
|
If Jinja cannot load or render the template. |
HtmlParseError
|
If the rendered HTML cannot be parsed. |
RenderError
|
If layout or SVG generation fails. |
render_sequence_at_time ¶
render_sequence_at_time(
scenes: Sequence[AnimationScene],
time_ms: int,
*,
stylesheets: Sequence[str] | None = None,
keyframes: KeyframesInput | None = None,
options: RenderOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
format: ImageOutputFormat | UnsetType = UNSET,
quality: int | UnsetType | None = UNSET,
lossless: bool | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
device_pixel_ratio: float | UnsetType = UNSET,
draw_debug_border: bool | UnsetType = UNSET,
dithering: DitheringAlgorithm | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
validate: bool = False,
) -> bytes
Render the active scene in a timed sequence at one instant.
Scene durations define the sequence timeline. Explicit render keywords
override matching fields in options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scenes
|
Sequence[AnimationScene]
|
Ordered nodes and their durations in milliseconds. Nodes may already be compiled. |
required |
time_ms
|
int
|
Sequence sampling time in milliseconds. |
required |
stylesheets
|
Sequence[str] | None
|
CSS source strings compiled with strict parsing. |
None
|
keyframes
|
KeyframesInput | None
|
Structured animation rules appended after |
None
|
options
|
RenderOptions | None
|
Base render configuration. |
None
|
width
|
int | UnsetType | None
|
Output viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
Output viewport height, or |
UNSET
|
format
|
ImageOutputFormat | UnsetType
|
Static output format. |
UNSET
|
quality
|
int | UnsetType | None
|
Lossy encoder quality from 0 through 100 where supported. |
UNSET
|
lossless
|
bool | UnsetType | None
|
Whether to use lossless WebP encoding. |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
device_pixel_ratio
|
float | UnsetType
|
Scale factor applied to raster output dimensions. |
UNSET
|
draw_debug_border
|
bool | UnsetType
|
Draw Takumi's layout debug borders. |
UNSET
|
dithering
|
DitheringAlgorithm | UnsetType
|
Color-dithering algorithm for formats that use it. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
validate
|
bool
|
Validate and normalize uncompiled scene nodes in Python. |
False
|
Returns:
| Type | Description |
|---|---|
bytes
|
Encoded image bytes, or raw RGBA pixels when |
Raises:
| Type | Description |
|---|---|
AnimationError
|
If the sequence is empty or its timeline is invalid. |
NodeValidationError
|
If Python-side validation is enabled and fails. |
NodeDecodeError
|
If a native node decoder rejects a scene. |
RenderError
|
If layout or rendering fails. |
UnsupportedFormatError
|
If the requested output cannot be encoded. |
render_animation ¶
render_animation(
scenes: Sequence[AnimationScene],
*,
stylesheets: Sequence[str] | None = None,
keyframes: KeyframesInput | None = None,
options: RenderOptions | None = None,
encode_options: AnimationEncodeOptions | None = None,
width: int | UnsetType | None = UNSET,
height: int | UnsetType | None = UNSET,
font_size: float | UnsetType = UNSET,
device_pixel_ratio: float | UnsetType = UNSET,
draw_debug_border: bool | UnsetType = UNSET,
dithering: DitheringAlgorithm | UnsetType = UNSET,
images: Sequence[ImageResourceInput]
| UnsetType
| None = UNSET,
font_families: Sequence[str] | UnsetType | None = UNSET,
lang: str | UnsetType | None = UNSET,
fps: int = 30,
format: AnimationOutputFormat | UnsetType = UNSET,
quality: int | UnsetType | None = UNSET,
lossless: bool | UnsetType | None = UNSET,
loop_count: int | UnsetType | None = UNSET,
webp_blend: bool | UnsetType = UNSET,
webp_dispose: bool | UnsetType = UNSET,
webp_speed: int | UnsetType | None = UNSET,
validate: bool = False,
) -> bytes
Render a timed scene sequence and encode it as an animation.
Render keywords override fields in options. Encoder keywords override
fields in encode_options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scenes
|
Sequence[AnimationScene]
|
Ordered nodes and their durations in milliseconds. Nodes may already be compiled. |
required |
stylesheets
|
Sequence[str] | None
|
CSS source strings compiled with strict parsing. |
None
|
keyframes
|
KeyframesInput | None
|
Structured animation rules appended after |
None
|
options
|
RenderOptions | None
|
Base render configuration shared by every frame. |
None
|
encode_options
|
AnimationEncodeOptions | None
|
Base animation encoder configuration. |
None
|
width
|
int | UnsetType | None
|
Output viewport width, or |
UNSET
|
height
|
int | UnsetType | None
|
Output viewport height, or |
UNSET
|
font_size
|
float | UnsetType
|
Root font size in CSS pixels. |
UNSET
|
device_pixel_ratio
|
float | UnsetType
|
Scale factor applied to frame dimensions. |
UNSET
|
draw_debug_border
|
bool | UnsetType
|
Draw Takumi's layout debug borders. |
UNSET
|
dithering
|
DitheringAlgorithm | UnsetType
|
Color-dithering algorithm for formats that use it. |
UNSET
|
images
|
Sequence[ImageResourceInput] | UnsetType | None
|
Per-render image resources addressable by their |
UNSET
|
font_families
|
Sequence[str] | UnsetType | None
|
Preferred fallback font-family order. |
UNSET
|
lang
|
str | UnsetType | None
|
Document language used for text shaping and font selection. |
UNSET
|
fps
|
int
|
Number of frames rendered per second: at most 90 for WebP and APNG and at most 50 for GIF, whose decoders clamp shorter frame delays. |
30
|
format
|
AnimationOutputFormat | UnsetType
|
Animation container format. |
UNSET
|
quality
|
int | UnsetType | None
|
Lossy encoder quality from 0 through 100 where supported. |
UNSET
|
lossless
|
bool | UnsetType | None
|
Whether to use lossless WebP encoding. |
UNSET
|
loop_count
|
int | UnsetType | None
|
Number of animation repetitions, or |
UNSET
|
webp_blend
|
bool | UnsetType
|
Blend each WebP frame with the previous canvas. |
UNSET
|
webp_dispose
|
bool | UnsetType
|
Dispose each WebP frame before drawing the next one. |
UNSET
|
webp_speed
|
int | UnsetType | None
|
WebP encoder speed/effort setting supported by Takumi. |
UNSET
|
validate
|
bool
|
Validate and normalize uncompiled scene nodes in Python. |
False
|
Returns:
| Type | Description |
|---|---|
bytes
|
Encoded WebP, APNG, or GIF animation bytes. |
Raises:
| Type | Description |
|---|---|
AnimationError
|
If the timeline or encoder configuration is invalid. |
NodeValidationError
|
If Python-side validation is enabled and fails. |
NodeDecodeError
|
If a native node decoder rejects a scene. |
RenderError
|
If layout or frame rendering fails. |
UnsupportedFormatError
|
If the animation cannot be encoded. |
ValueError
|
If |
encode_frames ¶
encode_frames(
frames: Sequence[RawAnimationFrame],
*,
encode_options: AnimationEncodeOptions | None = None,
format: AnimationOutputFormat | UnsetType = UNSET,
quality: int | UnsetType | None = UNSET,
lossless: bool | UnsetType | None = UNSET,
loop_count: int | UnsetType | None = UNSET,
webp_blend: bool | UnsetType = UNSET,
webp_dispose: bool | UnsetType = UNSET,
webp_speed: int | UnsetType | None = UNSET,
) -> bytes
Encode pre-rendered raw RGBA frames as an animation.
Explicit encoder keywords override matching fields in encode_options.
Each frame's byte length must equal width * height * 4.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frames
|
Sequence[RawAnimationFrame]
|
Ordered raw RGBA frames with dimensions and durations. |
required |
encode_options
|
AnimationEncodeOptions | None
|
Base animation encoder configuration. |
None
|
format
|
AnimationOutputFormat | UnsetType
|
Animation container format. |
UNSET
|
quality
|
int | UnsetType | None
|
Lossy encoder quality from 0 through 100 where supported. |
UNSET
|
lossless
|
bool | UnsetType | None
|
Whether to use lossless WebP encoding. |
UNSET
|
loop_count
|
int | UnsetType | None
|
Number of animation repetitions, or |
UNSET
|
webp_blend
|
bool | UnsetType
|
Blend each WebP frame with the previous canvas. |
UNSET
|
webp_dispose
|
bool | UnsetType
|
Dispose each WebP frame before drawing the next one. |
UNSET
|
webp_speed
|
int | UnsetType | None
|
WebP encoder speed/effort setting supported by Takumi. |
UNSET
|
Returns:
| Type | Description |
|---|---|
bytes
|
Encoded WebP, APNG, or GIF animation bytes. |
Raises:
| Type | Description |
|---|---|
AnimationError
|
If the frames have invalid dimensions or byte lengths, or the encoder configuration is invalid. |
UnsupportedFormatError
|
If the animation cannot be encoded. |
CompiledHtml¶
takumi_py.renderer.CompiledHtml
dataclass
¶
A compiled HTML node together with its compiled stylesheets.
The current parser ignores <style> elements, so stylesheets is
empty. Compile document-level CSS explicitly before passing it to a
compiled rendering method.
Attributes:
| Name | Type | Description |
|---|---|---|
node |
CompiledNode
|
The compiled root node. |
stylesheets |
tuple[CompiledStyleSheet, ...]
|
Compiled stylesheets returned by the parser. |