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.1.0"
To use wgpu features, enable the wgpu feature.
wgpu機能を使用する場合は、wgpu featureを有効にしてください。
[]
= { = "0.1.0", = ["wgpu"] }
Usage / 使い方
1. Prepare Fonts / フォントの準備
Use FontStorage to load fonts. You can load system fonts or query specific fonts.
FontStorage を使用してフォントを読み込みます。システムフォントをロードしたり、特定のフォントをクエリすることができます。
use FontStorage;
use ;
let mut font_storage = new;
font_storage.load_system_fonts;
// Query a font / フォントの検索
let font_id = font_storage
.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 / 色の型はユーザー定義可能です
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 = data.layout;
4. Rendering / レンダリング
GPU Rendering (wgpu) / GPUレンダリング (wgpu)
Use WgpuRenderer to draw. You need to set up wgpu::Device and wgpu::Queue beforehand.
WgpuRenderer を使用して描画します。事前に wgpu::Device や wgpu::Queue のセットアップが必要です。
use WgpuRenderer;
use GpuCacheConfig;
use NonZeroUsize;
// Cache configuration / キャッシュ設定
let cache_configs = vec!;
let mut renderer = new;
// Draw inside a render pass / レンダーパス内での描画
renderer.render;
CPU Rendering / CPUレンダリング
Use CpuRenderer to get pixel data.
CpuRenderer を使用してピクセルデータを取得します。
use CpuRenderer;
let mut renderer = new;
let pixel_buffer = renderer.render;
License / ライセンス
MIT OR Apache-2.0