This is your playground for exploring Typf's text rendering capabilities. Each example starts with raw text and shows how different shapers, renderers, and exporters transform it into final output. Start simple, then explore complex scripts, variable fonts, and vector graphics.
Run any example with a single command:
cargo run --example basicThe "Hello, World!" of Typf - see text transform into pixels through the complete pipeline.
cargo run --example basicWhat you'll learn:
- How the six-stage pipeline works in practice
- Create your first shaper/renderer/exporter chain
- Set colors, backgrounds, and padding
- Save rendered text as an image
Result: examples/output.ppm - Your first rendered text!
Shape and render once, export everywhere. Compare file sizes and quality across Typf's export formats.
cargo run --example formatsFormats demonstrated:
- PNG: Web-optimized, compressed with alpha channel
- SVG: Vector graphics with embedded bitmap data
- PPM: Portable Pixmap - uncompressed RGB color
- PGM: Portable Graymap - single-channel grayscale
- PBM: Portable Bitmap - pure black and white
Result: examples/output/test.{ppm,pgm,pbm,png,svg} - Same text, every format
Watch professional shaper HarfBuzz transform Arabic, Devanagari, and complex scripts with OpenType features.
cargo run --example harfbuzz --features shaping-hbTypography superpowers:
- Complex scripts: Arabic RTL, Hindi connecting forms, Thai tone marks
- OpenType features: Ligatures, kerning, small caps, number styles
- Language awareness: Script-specific shaping rules
- Performance boost: Built-in caching for repeated text
Output: examples/harfbuzz_output.ppm - Publication-quality text
Note: Requires shaping-hb feature - brings in professional-grade typography
Create reusable text processing pipelines that compose, configure, and execute complex rendering workflows.
cargo run --example pipelinePipeline patterns:
- Builder pattern: Clear, declarative stage composition
- Parameter control: Fine-tune shaping and rendering independently
- Reusability: One pipeline, unlimited texts
- Maintainability: Separation of concerns, testable components
Output: examples/pipeline_output.ppm - Clean architecture in action
All examples create output files in:
- Individual examples:
examples/*.ppm - Formats example:
examples/output/*
The examples/output/ directory is created automatically and is git-ignored.
Some examples require specific Cargo features to be enabled:
| Example | Required Features | Default? |
|---|---|---|
basic |
(none) | ✓ |
formats |
export-png, export-svg |
✓ |
harfbuzz |
shaping-hb |
✓ |
pipeline |
(none) | ✓ |
All examples work with the default feature set when building with cargo run --example <name>.
- basic: "Hello, Typf!"
- formats: "Format Test"
- harfbuzz: "Complex Text with لغة"
- pipeline: "Pipeline Example"
Most examples use mock/stub fonts for demonstration purposes. For real font rendering:
- Load a TrueType or OpenType font file
- Use
typf_fontdb::TypfFontFace::from_file(path) - Pass the font to the shaper
Example:
use typf_fontdb::TypfFontFace;
let font = TypfFontFace::from_file("path/to/font.ttf")?;The formats example creates the examples/output/ directory automatically. For other examples, ensure you're running from the project root.
If the harfbuzz example fails to compile, ensure HarfBuzz is installed:
# macOS
brew install harfbuzz
# Ubuntu/Debian
sudo apt-get install libharfbuzz-dev
# Fedora
sudo dnf install harfbuzz-develPNM files (PPM/PGM/PBM):
- macOS: Preview.app, GIMP
- Linux: GIMP, ImageMagick (
display file.ppm) - Windows: IrfanView, GIMP
PNG files:
- Any modern image viewer
SVG files:
- Any modern web browser
- Inkscape
- Adobe Illustrator
Experience variable fonts that morph between weights, widths, and optical sizes without loading multiple font files.
cargo run --example variable_fonts --features shaping-hbDesign freedom unlocked:
- Weight axis: Light to Black in real-time (100-900)
- Width axis: Compressed to Extended (75-125%)
- Optical size: Optimized for micro to macro displays
- Multi-axis control: Combine variations for exact typography
- Performance: One font file instead of dozens
Note: Load an actual variable font file to see the magic happen
Extract real glyph outlines from fonts and convert them to SVG paths - not just bitmas wrapped in XML.
cargo run --example svg_export_example --features shaping-hb,export-svgVector excellence:
- True paths: Actual Bézier curves from font outlines
- Infinite scaling: Zoom forever without quality loss
- Tiny files: ~30x smaller than bitmap-in-SVG alternatives
- Design friendly: Editable in Illustrator, Inkscape, Figma
- Web ready: Perfect scalable graphics for any screen
Output: output.svg - Professional vector typography
One text sample rendered through every format Typf offers - compare quality, file size, and use cases.
cargo run --example all_formats --features fullFormat showcase:
- PNG + Alpha: Web-optimized with transparency
- SVG Vector: Infinite scalability with path data
- PNM Family: RGB, grayscale, and bitmap variants
- JSON Debug: Insight into shaping algorithms
- Complex scripts: Arabic text challenges each exporter
Output: examples/output/* - Complete format comparison suite
Compare different shaping and rendering backends.
cargo run --example backend_comparison --features shaping-hbFeatures shown:
- Multiple backend configurations
- Performance comparison
- Quality comparison
Strategies for text exceeding bitmap width limits (~10,000 pixels).
cargo run --example long_text_handling --features shaping-hb,export-svgFeatures shown:
- Width estimation and limit checking
- SVG export for unlimited width
- Word-based line wrapping implementation
- Adaptive font sizing calculator
- Chunked rendering approach
Topics covered:
- Detecting when text is too long for bitmap rendering
- Using SVG export as an alternative (no width limits)
- Implementing simple line wrapping
- Adaptive font sizing to fit target width
- Chunked rendering for very long documents
See also:
bindings/python/examples/long_text_handling.py- Python equivalentdocs/PERFORMANCE.md- Performance optimization guideREADME.md- Known Limitations section
Python examples are located in bindings/python/examples/:
long_text_handling.py: Handling long text with Python bindings
To run Python examples:
cd bindings/python
python examples/long_text_handling.pyPrerequisites:
- Build Python bindings:
uvx maturin develop --release --features shaping-hb,export-svg - Install Python dependencies:
uv pip install fire pillow
For comprehensive benchmarking and testing:
cd typf-tester
# Test all backends
python typfme.py render --backend=harfbuzz --format=png
# Benchmark performance
python typfme.py bench --iterations=100
# Benchmark shaping only
python typfme.py bench-shaping --iterations=1000
# Benchmark rendering only
python typfme.py bench-rendering --iterations=100
# Test text length scaling
python typfme.py bench-scaling --iterations=50Outputs:
output/benchmark_report.json- Detailed JSON resultsoutput/benchmark_summary.md- Compact Markdown table
See: typf-tester/README.md for full testing documentation
After exploring these examples, check out:
- Architecture docs:
PLAN/00.md- Full system design - Performance guide:
docs/PERFORMANCE.md- Optimization strategies - API docs: Run
cargo doc --open - Tests: Run
cargo test --workspaceto see more usage patterns
Community project by FontLab - https://www.fontlab.org/