//! This crate provides the tools for positioning and rendering text in Bevy.1//!2//! # `Font`3//!4//! Fonts contain information for drawing glyphs, which are shapes that typically represent a single character,5//! but in some cases part of a "character" (grapheme clusters) or more than one character (ligatures).6//!7//! A font *face* is part of a font family,8//! and is distinguished by its style (e.g. italic), its weight (e.g. bold) and its stretch (e.g. condensed).9//!10//! In Bevy, [`Font`]s are loaded by the [`FontLoader`] as [assets](bevy_asset::AssetPlugin).11//!12//! # `TextPipeline`13//!14//! The [`TextPipeline`] resource does all of the heavy lifting for rendering text.15//!16//! UI `Text` is first measured by creating a [`TextMeasureInfo`] in [`TextPipeline::create_text_measure`],17//! which is called by the `measure_text_system` system of `bevy_ui`.18//!19//! Note that text measurement is only relevant in a UI context.20//!21//! With the actual text bounds defined, the `bevy_ui::widget::text::text_system` system (in a UI context)22//! or `bevy_sprite::text2d::update_text2d_layout` system (in a 2d world space context)23//! passes it into [`TextPipeline::queue_text`], which:24//!25//! 1. updates a [`Buffer`](cosmic_text::Buffer) from the [`TextSpan`]s, generating new [`FontAtlasSet`]s if necessary.26//! 2. iterates over each glyph in the [`Buffer`](cosmic_text::Buffer) to create a [`PositionedGlyph`],27//! retrieving glyphs from the cache, or rasterizing to a [`FontAtlas`] if necessary.28//! 3. [`PositionedGlyph`]s are stored in a [`TextLayoutInfo`],29//! which contains all the information that downstream systems need for rendering.3031extern crate alloc;3233mod bounds;34mod error;35mod font;36mod font_atlas;37mod font_atlas_set;38mod font_loader;39mod glyph;40mod pipeline;41mod text;42mod text_access;4344pub use bounds::*;45pub use error::*;46pub use font::*;47pub use font_atlas::*;48pub use font_atlas_set::*;49pub use font_loader::*;50pub use glyph::*;51pub use pipeline::*;52pub use text::*;53pub use text_access::*;5455/// The text prelude.56///57/// This includes the most common types in this crate, re-exported for your convenience.58pub mod prelude {59#[doc(hidden)]60pub use crate::{61Font, Justify, LineBreak, TextColor, TextError, TextFont, TextLayout, TextSpan,62};63}6465use bevy_app::prelude::*;66use bevy_asset::{AssetApp, AssetEventSystems};67use bevy_ecs::prelude::*;6869/// The raw data for the default font used by `bevy_text`70#[cfg(feature = "default_font")]71pub const DEFAULT_FONT_DATA: &[u8] = include_bytes!("FiraMono-subset.ttf");7273/// Adds text rendering support to an app.74///75/// When the `bevy_text` feature is enabled with the `bevy` crate, this76/// plugin is included by default in the `DefaultPlugins`.77#[derive(Default)]78pub struct TextPlugin;7980/// System set in [`PostUpdate`] where all 2d text update systems are executed.81#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]82pub struct Text2dUpdateSystems;8384/// Deprecated alias for [`Text2dUpdateSystems`].85#[deprecated(since = "0.17.0", note = "Renamed to `Text2dUpdateSystems`.")]86pub type Update2dText = Text2dUpdateSystems;8788impl Plugin for TextPlugin {89fn build(&self, app: &mut App) {90app.init_asset::<Font>()91.init_asset_loader::<FontLoader>()92.init_resource::<FontAtlasSets>()93.init_resource::<TextPipeline>()94.init_resource::<CosmicFontSystem>()95.init_resource::<SwashCache>()96.init_resource::<TextIterScratch>()97.add_systems(98PostUpdate,99remove_dropped_font_atlas_sets.before(AssetEventSystems),100)101.add_systems(Last, trim_cosmic_cache);102103#[cfg(feature = "default_font")]104{105use bevy_asset::{AssetId, Assets};106let mut assets = app.world_mut().resource_mut::<Assets<_>>();107let asset = Font::try_from_bytes(DEFAULT_FONT_DATA.to_vec()).unwrap();108assets.insert(AssetId::default(), asset).unwrap();109};110}111}112113114