2022-10-24 08:56:48 -06:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
|
2022-10-27 10:29:19 -06:00
|
|
|
//! # 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
|
2022-10-31 11:24:36 -06:00
|
|
|
//! [Buffer], provide it with some text, and then inspect the layout it produces. At this
|
2022-10-27 10:29:19 -06:00
|
|
|
//! point, you can use the `SwashCache` to rasterize glyphs into either images or pixels.
|
|
|
|
|
//!
|
|
|
|
|
//! ```
|
2022-10-31 11:24:36 -06:00
|
|
|
//! use cosmic_text::{Attrs, Color, FontSystem, SwashCache, Buffer, Metrics};
|
2022-10-27 10:29:19 -06:00
|
|
|
//!
|
|
|
|
|
//! // 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
|
2022-10-31 11:24:36 -06:00
|
|
|
//! let metrics = Metrics::new(14, 20);
|
2022-10-27 10:29:19 -06:00
|
|
|
//!
|
2022-10-31 11:24:36 -06:00
|
|
|
//! // A Buffer provides shaping and layout for a UTF-8 string, create one per text widget
|
|
|
|
|
//! let mut buffer = Buffer::new(&font_system, metrics);
|
2022-10-27 10:29:19 -06:00
|
|
|
//!
|
|
|
|
|
//! // Set a size for the text buffer, in pixels
|
2022-10-31 11:24:36 -06:00
|
|
|
//! buffer.set_size(80, 25);
|
2022-10-27 10:29:19 -06:00
|
|
|
//!
|
|
|
|
|
//! // Attributes indicate what font to choose
|
|
|
|
|
//! let attrs = Attrs::new();
|
|
|
|
|
//!
|
|
|
|
|
//! // Add some text!
|
2022-10-31 11:24:36 -06:00
|
|
|
//! buffer.set_text("Hello, Rust! 🦀\n", attrs);
|
2022-10-27 10:29:19 -06:00
|
|
|
//!
|
|
|
|
|
//! // Perform shaping as desired
|
2022-10-31 11:24:36 -06:00
|
|
|
//! buffer.shape_until_scroll();
|
2022-10-27 10:29:19 -06:00
|
|
|
//!
|
|
|
|
|
//! // Inspect the output runs
|
2022-10-31 11:24:36 -06:00
|
|
|
//! for run in buffer.layout_runs() {
|
2022-10-27 10:29:19 -06:00
|
|
|
//! for glyph in run.glyphs.iter() {
|
|
|
|
|
//! println!("{:#?}", glyph);
|
|
|
|
|
//! }
|
|
|
|
|
//! }
|
2022-10-27 11:02:56 -06:00
|
|
|
//!
|
|
|
|
|
//! // Create a default text color
|
|
|
|
|
//! let text_color = Color::rgb(0xFF, 0xFF, 0xFF);
|
|
|
|
|
//!
|
2022-11-04 23:46:15 -02:30
|
|
|
//! // Draw the buffer (for performance, instead use SwashCache directly)
|
2022-10-31 11:24:36 -06:00
|
|
|
//! buffer.draw(&mut swash_cache, text_color, |x, y, w, h, color| {
|
2022-10-27 11:02:56 -06:00
|
|
|
//! // Fill in your code here for drawing rectangles
|
|
|
|
|
//! });
|
2022-10-27 10:29:19 -06:00
|
|
|
//! ```
|
|
|
|
|
|
2022-10-25 14:14:23 -06:00
|
|
|
pub use self::attrs::*;
|
|
|
|
|
mod attrs;
|
|
|
|
|
|
2022-10-18 12:07:22 -06:00
|
|
|
pub use self::buffer::*;
|
|
|
|
|
mod buffer;
|
|
|
|
|
|
2022-10-27 14:51:46 -06:00
|
|
|
pub use self::buffer_line::*;
|
|
|
|
|
mod buffer_line;
|
|
|
|
|
|
2022-10-25 12:52:46 -06:00
|
|
|
pub use self::cache::*;
|
|
|
|
|
mod cache;
|
|
|
|
|
|
2022-10-31 11:24:36 -06:00
|
|
|
pub use self::editor::*;
|
|
|
|
|
mod editor;
|
|
|
|
|
|
2022-10-18 12:07:22 -06:00
|
|
|
pub use self::font::*;
|
|
|
|
|
mod font;
|
2022-10-25 10:55:24 -06:00
|
|
|
|
2022-10-25 12:52:46 -06:00
|
|
|
pub use self::layout::*;
|
|
|
|
|
mod layout;
|
|
|
|
|
|
2022-10-26 14:16:48 -06:00
|
|
|
pub use self::shape::*;
|
|
|
|
|
mod shape;
|
|
|
|
|
|
2022-10-25 10:55:24 -06:00
|
|
|
#[cfg(feature = "swash")]
|
|
|
|
|
pub use self::swash::*;
|
|
|
|
|
#[cfg(feature = "swash")]
|
|
|
|
|
mod swash;
|