hephae_text/
lib.rs

1#![allow(internal_features)]
2#![cfg_attr(any(docsrs, docsrs_dep), feature(rustdoc_internals))]
3#![doc = include_str!("../README.md")]
4#![cfg_attr(doc, deny(missing_docs))]
5
6pub use cosmic_text;
7
8pub mod atlas;
9pub mod def;
10pub mod layout;
11
12use bevy_ecs::prelude::*;
13
14/// Common imports for [`hephae_text`](crate).
15pub mod prelude {
16    pub use crate::{
17        atlas::ExtractedFontAtlases,
18        def::{Font, Text, TextAlign, TextFont, TextGlyph, TextGlyphs, TextSpan, TextStructure, TextWrap},
19        layout::FontLayout,
20    };
21}
22
23/// App plugins for [`hephae_text`](crate).
24pub mod plugin {
25    use std::sync::Mutex;
26
27    use bevy_app::{PluginGroupBuilder, prelude::*};
28    use bevy_asset::AssetApp;
29    use bevy_ecs::prelude::IntoSystemConfigs;
30    use bevy_render::{ExtractSchedule, RenderApp};
31
32    use crate::{
33        HephaeTextSystems,
34        atlas::{ExtractedFontAtlases, FontAtlas, extract_font_atlases},
35        def::{Font, FontLoader, Text, TextAlign, TextFont, TextSpan, TextWrap, compute_structure, notify_structure},
36        layout::{FontLayout, FontLayoutInner, load_fonts_to_database},
37    };
38
39    /// Provides text-rendering functionality into the app.
40    pub fn text() -> impl PluginGroup {
41        struct TextGroup;
42        impl PluginGroup for TextGroup {
43            fn build(self) -> PluginGroupBuilder {
44                #[allow(unused_mut)]
45                let mut builder = PluginGroupBuilder::start::<Self>().add(|app: &mut App| {
46                    let (sender, receiver) = async_channel::bounded(4);
47                    app.init_asset::<Font>()
48                        .init_asset::<FontAtlas>()
49                        .register_asset_loader(FontLoader { add_to_database: sender })
50                        .insert_resource(FontLayout(Mutex::new(FontLayoutInner::new(receiver))))
51                        .register_type::<Text>()
52                        .register_type::<TextWrap>()
53                        .register_type::<TextAlign>()
54                        .register_type::<TextFont>()
55                        .register_type::<TextSpan>()
56                        .configure_sets(Update, HephaeTextSystems::LoadFontsToDatabase)
57                        .configure_sets(PostUpdate, HephaeTextSystems::ComputeStructure)
58                        .add_systems(Update, load_fonts_to_database.in_set(HephaeTextSystems::LoadFontsToDatabase))
59                        .add_systems(
60                            PostUpdate,
61                            (compute_structure, notify_structure)
62                                .chain()
63                                .in_set(HephaeTextSystems::ComputeStructure),
64                        );
65
66                    if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
67                        render_app
68                            .init_resource::<ExtractedFontAtlases>()
69                            .configure_sets(ExtractSchedule, HephaeTextSystems::ExtractFontAtlases)
70                            .add_systems(
71                                ExtractSchedule,
72                                extract_font_atlases.in_set(HephaeTextSystems::ExtractFontAtlases),
73                            );
74                    }
75                });
76
77                #[cfg(feature = "locale")]
78                {
79                    builder = builder
80                        .add(hephae_locale::plugin::locale_target::<Text>())
81                        .add(hephae_locale::plugin::locale_target::<TextSpan>());
82                }
83
84                builder
85            }
86        }
87
88        TextGroup
89    }
90}
91
92/// Labels for systems added by Hephae Text.
93#[derive(SystemSet, Debug, Copy, Clone, PartialEq, Eq, Hash)]
94pub enum HephaeTextSystems {
95    /// System in [`ExtractSchedule`](bevy_render::ExtractSchedule) that extracts font atlases into
96    /// the render world.
97    ExtractFontAtlases,
98    /// System in [`Update`](bevy_app::Update) that loads bytes sent from
99    /// [`FontLoader`](def::FontLoader) into a [`Font`](def::Font) and adds them to the database.
100    LoadFontsToDatabase,
101    /// System in [`PostUpdate`](bevy_app::PostUpdate) that computes and marks
102    /// [`TextStructure`](def::TextStructure) as changed as necessary, for convenience of systems
103    /// wishing to listen for change-detection.
104    ComputeStructure,
105}