Suzuri
Suzuri is a text rendering library written in Rust. It supports text rendering on both CPU and GPU (wgpu), providing font management, text layout, and rendering capabilities.
Suzuriは、Rust製のテキストレンダリングライブラリです。CPUおよびGPU (wgpu) でのテキスト描画をサポートしており、フォント管理、テキストレイアウト、レンダリング機能を提供します。
Features / 特徴
- Font Management: Loads system fonts and manages custom fonts. Internally uses fontdb.
- Text Layout: Calculates text placement including wrapping, alignment, line spacing, etc.
- Consistent Layout: Ensures layout consistency and reproducibility within a two-pass layout data flow. Unlike existing libraries such as
cosmic-textorglyphon, it prevents unstable behavior where reducing layout width alters line breaks even when sufficient whitespace remains. - Rendering:
- CPU Rendering: Drawing to image buffers.
- GPU Rendering: High-speed drawing using wgpu.
- フォント管理: システムフォントの読み込みや、カスタムフォントの管理を行います。内部で fontdb を使用しています。
- テキストレイアウト: 折り返し、整列、行間設定などを含むテキスト配置計算を行います。
- レイアウトの整合性:
cosmic-textやglyphonなどの既存ライブラリとは異なり、2パスレイアウトデータフロー内での整合性と再現性を確保しています。十分な余白がある状態でレイアウト幅を縮小しても、改行位置などの結果が不意に変わることのない安定した挙動を提供します。 - レンダリング:
- CPUレンダリング: 画像バッファへの描画。
- GPUレンダリング: wgpu を使用した高速な描画。
Installation / インストール
Add the following to your Cargo.toml.
Cargo.toml に以下を追加してください。
[]
= "0.2.0"
To use wgpu features, enable the wgpu feature.
wgpu機能を使用する場合は、wgpu featureを有効にしてください。
[]
= { = "0.2.0", = ["wgpu"] }
Usage / 使い方
1. Initialize FontSystem / FontSystemの初期化
FontSystem is the entry point for Suzuri. It handles font loading, layout, and renderer management.
FontSystem は Suzuri のエントリーポイントです。フォントの読み込み、レイアウト、レンダラーの管理を一括して行います。
use ;
let font_system = new;
font_system.load_system_fonts;
// Query a font / フォントの検索
let font_id = font_system
.query
.map
.expect;
For details on font queries, please refer to the fontdb documentation.
フォントクエリの詳細については、fontdbのドキュメントを参照してください。
2. Create Text Data / テキストデータの作成
Define the content and style of the text you want to render.
描画したいテキストの内容とスタイルを定義します。
use ;
// Color type is user-definable / 色の型はユーザー定義可能です
// For wgpu rendering, convert to [f32; 4] (Premultiplied Alpha)
// wgpuレンダリングのために [f32; 4] (Premultiplied Alpha) へ変換します
let mut data = new;
data.append;
3. Calculate Layout / レイアウトの計算
Configure layout settings with TextLayoutConfig and calculate the placement.
TextLayoutConfig でレイアウト設定を行い、配置を計算します。
use ;
let config = TextLayoutConfig ;
let layout = font_system.layout_text;
4. Rendering / レンダリング
GPU Rendering (wgpu) / GPUレンダリング (wgpu)
Initialize the renderer and draw inside a render pass.
レンダラーを初期化し、レンダーパス内で描画します。
use GpuCacheConfig;
use NonZeroUsize;
// Cache configuration / キャッシュ設定
let cache_configs = vec!;
// Initialize (one-time)
font_system.wgpu_init;
// Draw inside a render pass
font_system.wgpu_render;
CPU Rendering / CPUレンダリング
Use cpu_render to get pixel data in a callback.
cpu_render を使用して、コールバック内でピクセルデータを取得します。
use CpuCacheConfig;
use NonZeroUsize;
// Cache configuration / キャッシュ設定
let cache_configs = ;
// Initialize (one-time)
font_system.cpu_init;
// Render to buffer
let image_size = ;
font_system.cpu_render;
License / ライセンス
MIT OR Apache-2.0