Crate cosmic_text

source ·
Expand description


This library provides advanced text handling in a generic way. It provides abstractions for shaping, font discovery, font fallback, layout, rasterization, and editing. Shaping utilizes rustybuzz, font discovery utilizes fontdb, and the rasterization is optional and utilizes swash. The other features are developed internal to this library.

It is recommended that you start by creating a FontSystem, after which you can create a Buffer, provide it with some text, and then inspect the layout it produces. At this point, you can use the SwashCache to rasterize glyphs into either images or pixels.

use cosmic_text::{Attrs, Color, FontSystem, SwashCache, Buffer, Metrics};

// A FontSystem provides access to detected system fonts, create one per application
let font_system = FontSystem::new();

// A SwashCache stores rasterized glyphs, create one per application
let mut swash_cache = SwashCache::new(&font_system);

// Text metrics indicate the font size and line height of a buffer
let metrics = Metrics::new(14, 20);

// A Buffer provides shaping and layout for a UTF-8 string, create one per text widget
let mut buffer = Buffer::new(&font_system, metrics);

// Set a size for the text buffer, in pixels
buffer.set_size(80, 25);

// Attributes indicate what font to choose
let attrs = Attrs::new();

// Add some text!
buffer.set_text("Hello, Rust! 🦀\n", attrs);

// Perform shaping as desired

// Inspect the output runs
for run in buffer.layout_runs() {
    for glyph in run.glyphs.iter() {
        println!("{:#?}", glyph);

// Create a default text color
let text_color = Color::rgb(0xFF, 0xFF, 0xFF);

// Draw the buffer (for performance, instead use SwashCache directly)
buffer.draw(&mut swash_cache, text_color, |x, y, w, h, color| {
    // Fill in your code here for drawing rectangles


pub use fontdb;


Text attributes
List of text attributes to apply to a line
An owned version of Attrs
A buffer of text that is shaped and laid out
A line (or paragraph) of text that is shaped and laid out
Key for building a glyph cache
Text color
Current cursor location
A wrapper of Buffer for easy editing
A font
Fonts that match a pattern
Access system fonts
The position of a cursor within a Buffer.
A laid out glyph
A line of laid out glyphs
A line of visible text for rendering
An iterator of visible text lines, see LayoutRun
Metrics of text
A shaped glyph
A shaped line (or paragraph)
A shaped span (for bidirectional processing)
A shaped word (for word wrapping)
Cache for rasterizing with the swash scaler
Scaled glyph image.
Specifies the weight of glyphs in the font, their degree of blackness or stroke thickness.


An action to perform on an Editor
Whether to associate cursors placed at a boundary between runs with the run before or after it.
Path command.
An owned version of Family
A face width.
Allows italic or oblique faces to be selected.
Binning of subpixel position for cache optimization
Content of a scaled glyph image.
Wrapping mode


A trait to allow easy replacements of Editor, like SyntaxEditor