Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_text/src/lib.rs
9349 views
1
//! This crate provides the tools for positioning and rendering text in Bevy.
2
//!
3
//! # `Font`
4
//!
5
//! Fonts contain information for drawing glyphs, which are shapes that typically represent a single character,
6
//! but in some cases part of a "character" (grapheme clusters) or more than one character (ligatures).
7
//!
8
//! A font *face* is part of a font family,
9
//! and is distinguished by its style (e.g. italic), its weight (e.g. bold) and its stretch (e.g. condensed).
10
//!
11
//! In Bevy, [`Font`]s are loaded by the [`FontLoader`] as [assets](bevy_asset::AssetPlugin).
12
//!
13
//! # `TextPipeline`
14
//!
15
//! The [`TextPipeline`] resource does all of the heavy lifting for rendering text.
16
//!
17
//! UI `Text` is first measured by creating a [`TextMeasureInfo`] in [`TextPipeline::create_text_measure`],
18
//! which is called by the `measure_text_system` system of `bevy_ui`.
19
//!
20
//! Note that text measurement is only relevant in a UI context.
21
//!
22
//! With the actual text bounds defined, the `bevy_ui::widget::text::text_system` system (in a UI context)
23
//! or `bevy_sprite::text2d::update_text2d_layout` system (in a 2d world space context)
24
//! passes it into [`TextPipeline::update_text_layout_info`], which:
25
//!
26
//! 1. updates a [`Buffer`](cosmic_text::Buffer) from the [`TextSpan`]s, generating new [`FontAtlas`]es if necessary.
27
//! 2. iterates over each glyph in the [`Buffer`](cosmic_text::Buffer) to create a [`PositionedGlyph`],
28
//! retrieving glyphs from the cache, or rasterizing to a [`FontAtlas`] if necessary.
29
//! 3. [`PositionedGlyph`]s are stored in a [`TextLayoutInfo`],
30
//! which contains all the information that downstream systems need for rendering.
31
32
extern crate alloc;
33
34
mod bounds;
35
mod error;
36
mod font;
37
mod font_atlas;
38
mod font_atlas_set;
39
mod font_loader;
40
mod glyph;
41
mod pipeline;
42
mod text;
43
mod text_access;
44
45
use bevy_asset::AssetEventSystems;
46
pub use bounds::*;
47
pub use error::*;
48
pub use font::*;
49
pub use font_atlas::*;
50
pub use font_atlas_set::*;
51
pub use font_loader::*;
52
pub use glyph::*;
53
pub use pipeline::*;
54
pub use text::*;
55
pub use text_access::*;
56
57
/// The text prelude.
58
///
59
/// This includes the most common types in this crate, re-exported for your convenience.
60
pub mod prelude {
61
#[doc(hidden)]
62
pub use crate::{
63
Font, FontHinting, FontSize, FontSmoothing, FontSource, FontStyle, FontWeight, FontWidth,
64
Justify, LineBreak, Strikethrough, StrikethroughColor, TextColor, TextError, TextFont,
65
TextLayout, TextSpan, Underline, UnderlineColor,
66
};
67
}
68
69
use bevy_app::prelude::*;
70
use bevy_asset::AssetApp;
71
use bevy_ecs::prelude::*;
72
73
/// The raw data for the default font used by `bevy_text`
74
#[cfg(feature = "default_font")]
75
pub const DEFAULT_FONT_DATA: &[u8] = include_bytes!("FiraMono-subset.ttf");
76
77
/// Adds text rendering support to an app.
78
///
79
/// When the `bevy_text` feature is enabled with the `bevy` crate, this
80
/// plugin is included by default in the `DefaultPlugins`.
81
#[derive(Default)]
82
pub struct TextPlugin;
83
84
/// System set in [`PostUpdate`] where all 2d text update systems are executed.
85
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
86
pub struct Text2dUpdateSystems;
87
88
impl Plugin for TextPlugin {
89
fn build(&self, app: &mut App) {
90
app.init_asset::<Font>()
91
.init_asset_loader::<FontLoader>()
92
.init_resource::<FontAtlasSet>()
93
.init_resource::<TextPipeline>()
94
.init_resource::<CosmicFontSystem>()
95
.init_resource::<SwashCache>()
96
.init_resource::<TextIterScratch>()
97
.init_resource::<RemSize>()
98
.add_systems(
99
PostUpdate,
100
load_font_assets_into_fontdb_system.after(AssetEventSystems),
101
)
102
.add_systems(Last, trim_cosmic_cache);
103
104
#[cfg(feature = "default_font")]
105
{
106
use bevy_asset::{AssetId, Assets};
107
let mut assets = app.world_mut().resource_mut::<Assets<_>>();
108
let asset = Font::try_from_bytes(DEFAULT_FONT_DATA.to_vec()).unwrap();
109
assets.insert(AssetId::default(), asset).unwrap();
110
};
111
}
112
}
113
114