//! 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::update_text_layout_info`], which:24//!25//! 1. updates a [`Buffer`](cosmic_text::Buffer) from the [`TextSpan`]s, generating new [`FontAtlas`]es 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;4344use bevy_asset::AssetEventSystems;45pub use bounds::*;46pub use error::*;47pub use font::*;48pub use font_atlas::*;49pub use font_atlas_set::*;50pub use font_loader::*;51pub use glyph::*;52pub use pipeline::*;53pub use text::*;54pub use text_access::*;5556/// The text prelude.57///58/// This includes the most common types in this crate, re-exported for your convenience.59pub mod prelude {60#[doc(hidden)]61pub use crate::{62Font, FontHinting, FontSize, FontSmoothing, FontSource, FontStyle, FontWeight, FontWidth,63Justify, LineBreak, Strikethrough, StrikethroughColor, TextColor, TextError, TextFont,64TextLayout, TextSpan, Underline, UnderlineColor,65};66}6768use bevy_app::prelude::*;69use bevy_asset::AssetApp;70use bevy_ecs::prelude::*;7172/// The raw data for the default font used by `bevy_text`73#[cfg(feature = "default_font")]74pub const DEFAULT_FONT_DATA: &[u8] = include_bytes!("FiraMono-subset.ttf");7576/// Adds text rendering support to an app.77///78/// When the `bevy_text` feature is enabled with the `bevy` crate, this79/// plugin is included by default in the `DefaultPlugins`.80#[derive(Default)]81pub struct TextPlugin;8283/// System set in [`PostUpdate`] where all 2d text update systems are executed.84#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]85pub struct Text2dUpdateSystems;8687impl Plugin for TextPlugin {88fn build(&self, app: &mut App) {89app.init_asset::<Font>()90.init_asset_loader::<FontLoader>()91.init_resource::<FontAtlasSet>()92.init_resource::<TextPipeline>()93.init_resource::<CosmicFontSystem>()94.init_resource::<SwashCache>()95.init_resource::<TextIterScratch>()96.init_resource::<RemSize>()97.add_systems(98PostUpdate,99load_font_assets_into_fontdb_system.after(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