Expand description
§COSMIC Text
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, Shaping};
// A FontSystem provides access to detected system fonts, create one per application
let mut font_system = FontSystem::new();
// A SwashCache stores rasterized glyphs, create one per application
let mut swash_cache = SwashCache::new();
// Text metrics indicate the font size and line height of a buffer
let metrics = Metrics::new(14.0, 20.0);
// A Buffer provides shaping and layout for a UTF-8 string, create one per text widget
let mut buffer = Buffer::new(&mut font_system, metrics);
// Borrow buffer together with the font system for more convenient method calls
let mut buffer = buffer.borrow_with(&mut font_system);
// Set a size for the text buffer, in pixels
buffer.set_size(Some(80.0), Some(25.0));
// Attributes indicate what font to choose
let attrs = Attrs::new();
// Add some text!
buffer.set_text("Hello, Rust! 🦀\n", attrs, Shaping::Advanced);
// Perform shaping as desired
buffer.shape_until_scroll(true);
// 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
});
Re-exports§
pub use ttf_parser;
pub use fontdb;
pub use rustybuzz;
Structs§
- Angle
- Represents an angle in degrees or radians.
- Attrs
- Text attributes
- Attrs
List - List of text attributes to apply to a line
- Attrs
Owned - An owned version of
Attrs
- Bidi
Paragraphs - An iterator over the paragraphs in the input text.
It is equivalent to
core::str::Lines
but followsunicode-bidi
behaviour. - Borrowed
With Font System - A value borrowed together with an
FontSystem
- Buffer
- A buffer of text that is shaped and laid out
- Buffer
Line - A line (or paragraph) of text that is shaped and laid out
- Cache
Key - Key for building a glyph cache
- Cache
KeyFlags - Flags that change rendering
- Cache
Metrics - Metrics, but implementing Eq and Hash using u32 representation of f32
- Change
- A set of change items grouped into one logical change
- Change
Item - A unique change to an editor
- Color
- Text color
- Cursor
- Current cursor location
- Editor
- A wrapper of [
Buffer
] for easy editing - Font
- A font
- Font
Match Attrs - Font-specific part of
Attrs
to be used for matching - Font
Match Key - Font
System - Access to the system fonts.
- Layout
Cursor - The position of a cursor within a [
Buffer
]. - Layout
Glyph - A laid out glyph
- Layout
Line - A line of laid out glyphs
- Layout
Run - A line of visible text for rendering
- Layout
RunIter - An iterator of visible text lines, see
LayoutRun
- Line
Iter - Iterator over lines terminated by
LineEnding
- Metrics
- Metrics of text
- Physical
Glyph - Placement
- Describes the offset and dimensions of a rendered mask.
- Scroll
- Scroll position in [
Buffer
] - Shape
Buffer - A set of buffers containing allocations for shaped text.
- Shape
Glyph - A shaped glyph
- Shape
Line - A shaped line (or paragraph)
- Shape
RunCache - A helper structure for caching shape runs.
- Shape
RunKey - Key for caching shape runs.
- Shape
Span - A shaped span (for bidirectional processing)
- Shape
Word - A shaped word (for word wrapping)
- Swash
Cache - Cache for rasterizing with the swash scaler
- Swash
Image - Scaled glyph image.
- Syntax
Editor - A wrapper of
Editor
with syntax highlighting provided bySyntaxSystem
- Syntax
System - Syntax
Theme - A theme parsed from a
.tmTheme
file. - Transform
- Two dimensional transformation matrix.
- ViEditor
- ViParser
- Weight
- Specifies the weight of glyphs in the font, their degree of blackness or stroke thickness.
Enums§
- Action
- An action to perform on an
Editor
- Affinity
- Whether to associate cursors placed at a boundary between runs with the run before or after it.
- Align
- Align or justify
- Buffer
Ref - Command
- Path command.
- Family
- A font family.
- Family
Owned - An owned version of
Family
- Line
Ending - Line ending
- Motion
- A motion to perform on a
Cursor
- Selection
- Selection mode
- Shaping
- The shaping strategy of some text.
- Stretch
- A face width.
- Style
- Allows italic or oblique faces to be selected.
- Subpixel
Bin - Binning of subpixel position for cache optimization
- Swash
Content - Content of a scaled glyph image.
- ViMode
- Wrap
- Wrapping mode