Path: blob/main/crates/bevy_text/src/parley_context.rs
30635 views
use crate::TextError;1use crate::{FontSmoothing, FontSource};2use bevy_derive::Deref;3use bevy_derive::DerefMut;4use bevy_ecs::resource::Resource;5use parley::LayoutContext;6use parley::{FontContext, GenericFamily};7use swash::scale::ScaleContext;89#[derive(Copy, Clone, PartialEq, Default, Debug)]10/// Per-section metadata attached to shaped text runs.11///12/// This brush is stored in Parley's [`Layout`](`parley::Layout`) so Bevy can identify which text section13/// a glyph run belongs to, and the antialiasing method that should be used during rendering.14pub struct TextBrush {15/// Index of the text section within its `ComputedTextBlock`.16pub section_index: u32,17/// Antialiasing method to use when rendering the text.18pub font_smoothing: FontSmoothing,19}2021impl TextBrush {22/// Creates a new brush for glyphs from the given text section.23pub fn new(section_index: u32, font_smoothing: FontSmoothing) -> Self {24TextBrush {25section_index,26font_smoothing,27}28}29}3031/// A font database and cache, used for font family resolution and text layout.32///33/// This resource is a wrapper around [`parley::FontContext`].34#[derive(Resource, Default, Deref, DerefMut)]35pub struct FontCx(pub FontContext);3637impl FontCx {38/// Get the family name associated with a [`FontSource`].39///40/// If the `FontSource` is a `Handle`, returns `None`. The family name can be found by using the handle to look41/// up the `Font` asset instead.42pub fn get_family<'a>(&'a mut self, source: &'a FontSource) -> Option<&'a str> {43let generic_family = match source {44FontSource::Handle(_) => return None,45FontSource::Family(family) => return Some(family.as_str()),46FontSource::Serif => GenericFamily::Serif,47FontSource::SansSerif => GenericFamily::SansSerif,48FontSource::Cursive => GenericFamily::Cursive,49FontSource::Fantasy => GenericFamily::Fantasy,50FontSource::Monospace => GenericFamily::Monospace,51FontSource::SystemUi => GenericFamily::SystemUi,52FontSource::UiSerif => GenericFamily::UiSerif,53FontSource::UiSansSerif => GenericFamily::UiSansSerif,54FontSource::UiMonospace => GenericFamily::UiMonospace,55FontSource::UiRounded => GenericFamily::UiRounded,56FontSource::Emoji => GenericFamily::Emoji,57FontSource::Math => GenericFamily::Math,58FontSource::FangSong => GenericFamily::FangSong,59};6061let family_id = self.0.collection.generic_families(generic_family).next();62family_id.and_then(|id| self.0.collection.family_name(id))63}6465/// Sets the fallback font for a given generic family.66///67/// In most cases, these methods do not need to called manually,68/// as [`parley::fontique`] will automatically select appropriate default fonts based on available system fonts.69///70/// Note that the `parley/system` feature must be enabled to allow automatic system font discovery.71///72/// These methods will return an error if the provided family name does not already exist in the font collection.73pub fn set_generic_family(74&mut self,75generic: GenericFamily,76family_name: &str,77) -> Result<(), TextError> {78self.collection79.family_id(family_name)80.ok_or(TextError::NoSuchFontFamily(family_name.to_string()))81.map(|id| {82self.collection83.set_generic_families(generic, core::iter::once(id));84})85}8687/// Sets the serif generic family mapping.88pub fn set_serif_family(&mut self, family_name: &str) -> Result<(), TextError> {89self.set_generic_family(GenericFamily::Serif, family_name)90}9192/// Sets the sans-serif generic family mapping.93pub fn set_sans_serif_family(&mut self, family_name: &str) -> Result<(), TextError> {94self.set_generic_family(GenericFamily::SansSerif, family_name)95}9697/// Sets the cursive generic family mapping.98pub fn set_cursive_family(&mut self, family_name: &str) -> Result<(), TextError> {99self.set_generic_family(GenericFamily::Cursive, family_name)100}101102/// Sets the fantasy generic family mapping.103pub fn set_fantasy_family(&mut self, family_name: &str) -> Result<(), TextError> {104self.set_generic_family(GenericFamily::Fantasy, family_name)105}106107/// Sets the monospace generic family mapping.108pub fn set_monospace_family(&mut self, family_name: &str) -> Result<(), TextError> {109self.set_generic_family(GenericFamily::Monospace, family_name)110}111112/// Sets the system-ui generic family mapping.113pub fn set_system_ui_family(&mut self, family_name: &str) -> Result<(), TextError> {114self.set_generic_family(GenericFamily::SystemUi, family_name)115}116117/// Sets the ui-serif generic family mapping.118pub fn set_ui_serif_family(&mut self, family_name: &str) -> Result<(), TextError> {119self.set_generic_family(GenericFamily::UiSerif, family_name)120}121122/// Sets the ui-sans-serif generic family mapping.123pub fn set_ui_sans_serif_family(&mut self, family_name: &str) -> Result<(), TextError> {124self.set_generic_family(GenericFamily::UiSansSerif, family_name)125}126127/// Sets the ui-monospace generic family mapping.128pub fn set_ui_monospace_family(&mut self, family_name: &str) -> Result<(), TextError> {129self.set_generic_family(GenericFamily::UiMonospace, family_name)130}131132/// Sets the ui-rounded generic family mapping.133pub fn set_ui_rounded_family(&mut self, family_name: &str) -> Result<(), TextError> {134self.set_generic_family(GenericFamily::UiRounded, family_name)135}136137/// Sets the emoji generic family mapping.138pub fn set_emoji_family(&mut self, family_name: &str) -> Result<(), TextError> {139self.set_generic_family(GenericFamily::Emoji, family_name)140}141142/// Sets the math generic family mapping.143pub fn set_math_family(&mut self, family_name: &str) -> Result<(), TextError> {144self.set_generic_family(GenericFamily::Math, family_name)145}146147/// Sets the fangsong generic family mapping.148pub fn set_fang_song_family(&mut self, family_name: &str) -> Result<(), TextError> {149self.set_generic_family(GenericFamily::FangSong, family_name)150}151}152153/// Text layout context154#[derive(Resource, Default, Deref, DerefMut)]155pub struct LayoutCx(pub LayoutContext<TextBrush>);156157/// Text scaler context158#[derive(Resource, Default, Deref, DerefMut)]159pub struct ScaleCx(pub ScaleContext);160161162