
# Objective - Fixes #6370 - Closes #6581 ## Solution - Added the following lints to the workspace: - `std_instead_of_core` - `std_instead_of_alloc` - `alloc_instead_of_core` - Used `cargo +nightly fmt` with [item level use formatting](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#Item%5C%3A) to split all `use` statements into single items. - Used `cargo clippy --workspace --all-targets --all-features --fix --allow-dirty` to _attempt_ to resolve the new linting issues, and intervened where the lint was unable to resolve the issue automatically (usually due to needing an `extern crate alloc;` statement in a crate root). - Manually removed certain uses of `std` where negative feature gating prevented `--all-features` from finding the offending uses. - Used `cargo +nightly fmt` with [crate level use formatting](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#Crate%5C%3A) to re-merge all `use` statements matching Bevy's previous styling. - Manually fixed cases where the `fmt` tool could not re-merge `use` statements due to conditional compilation attributes. ## Testing - Ran CI locally ## Migration Guide The MSRV is now 1.81. Please update to this version or higher. ## Notes - This is a _massive_ change to try and push through, which is why I've outlined the semi-automatic steps I used to create this PR, in case this fails and someone else tries again in the future. - Making this change has no impact on user code, but does mean Bevy contributors will be warned to use `core` and `alloc` instead of `std` where possible. - This lint is a critical first step towards investigating `no_std` options for Bevy. --------- Co-authored-by: François Mockers <francois.mockers@vleue.com>
129 lines
5.2 KiB
Rust
129 lines
5.2 KiB
Rust
use bevy_asset::{Assets, Handle};
|
|
use bevy_math::{IVec2, UVec2};
|
|
use bevy_render::{
|
|
render_asset::RenderAssetUsages,
|
|
render_resource::{Extent3d, TextureDimension, TextureFormat},
|
|
texture::{Image, ImageSampler},
|
|
};
|
|
use bevy_sprite::{DynamicTextureAtlasBuilder, TextureAtlasLayout};
|
|
use bevy_utils::HashMap;
|
|
|
|
use crate::{FontSmoothing, GlyphAtlasLocation, TextError};
|
|
|
|
/// Rasterized glyphs are cached, stored in, and retrieved from, a `FontAtlas`.
|
|
///
|
|
/// A `FontAtlas` contains one or more textures, each of which contains one or more glyphs packed into them.
|
|
///
|
|
/// A [`FontAtlasSet`](crate::FontAtlasSet) contains a `FontAtlas` for each font size in the same font face.
|
|
///
|
|
/// For the same font face and font size, a glyph will be rasterized differently for different subpixel offsets.
|
|
/// In practice, ranges of subpixel offsets are grouped into subpixel bins to limit the number of rasterized glyphs,
|
|
/// providing a trade-off between visual quality and performance.
|
|
///
|
|
/// A [`CacheKey`](cosmic_text::CacheKey) encodes all of the information of a subpixel-offset glyph and is used to
|
|
/// find that glyphs raster in a [`TextureAtlas`](bevy_sprite::TextureAtlas) through its corresponding [`GlyphAtlasLocation`].
|
|
pub struct FontAtlas {
|
|
/// Used to update the [`TextureAtlasLayout`].
|
|
pub dynamic_texture_atlas_builder: DynamicTextureAtlasBuilder,
|
|
/// A mapping between subpixel-offset glyphs and their [`GlyphAtlasLocation`].
|
|
pub glyph_to_atlas_index: HashMap<cosmic_text::CacheKey, GlyphAtlasLocation>,
|
|
/// The handle to the [`TextureAtlasLayout`] that holds the rasterized glyphs.
|
|
pub texture_atlas: Handle<TextureAtlasLayout>,
|
|
/// The texture where this font atlas is located
|
|
pub texture: Handle<Image>,
|
|
}
|
|
|
|
impl FontAtlas {
|
|
/// Create a new [`FontAtlas`] with the given size, adding it to the appropriate asset collections.
|
|
pub fn new(
|
|
textures: &mut Assets<Image>,
|
|
texture_atlases_layout: &mut Assets<TextureAtlasLayout>,
|
|
size: UVec2,
|
|
font_smoothing: FontSmoothing,
|
|
) -> FontAtlas {
|
|
let mut image = Image::new_fill(
|
|
Extent3d {
|
|
width: size.x,
|
|
height: size.y,
|
|
depth_or_array_layers: 1,
|
|
},
|
|
TextureDimension::D2,
|
|
&[0, 0, 0, 0],
|
|
TextureFormat::Rgba8UnormSrgb,
|
|
// Need to keep this image CPU persistent in order to add additional glyphs later on
|
|
RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
|
|
);
|
|
if font_smoothing == FontSmoothing::None {
|
|
image.sampler = ImageSampler::nearest();
|
|
}
|
|
let texture = textures.add(image);
|
|
let texture_atlas = texture_atlases_layout.add(TextureAtlasLayout::new_empty(size));
|
|
Self {
|
|
texture_atlas,
|
|
glyph_to_atlas_index: HashMap::default(),
|
|
dynamic_texture_atlas_builder: DynamicTextureAtlasBuilder::new(size, 1),
|
|
texture,
|
|
}
|
|
}
|
|
|
|
/// Get the [`GlyphAtlasLocation`] for a subpixel-offset glyph.
|
|
pub fn get_glyph_index(&self, cache_key: cosmic_text::CacheKey) -> Option<GlyphAtlasLocation> {
|
|
self.glyph_to_atlas_index.get(&cache_key).copied()
|
|
}
|
|
|
|
/// Checks if the given subpixel-offset glyph is contained in this [`FontAtlas`].
|
|
pub fn has_glyph(&self, cache_key: cosmic_text::CacheKey) -> bool {
|
|
self.glyph_to_atlas_index.contains_key(&cache_key)
|
|
}
|
|
|
|
/// Add a glyph to the atlas, updating both its texture and layout.
|
|
///
|
|
/// The glyph is represented by `glyph`, and its image content is `glyph_texture`.
|
|
/// This content is copied into the atlas texture, and the atlas layout is updated
|
|
/// to store the location of that glyph into the atlas.
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// Returns `()` if the glyph is successfully added, or [`TextError::FailedToAddGlyph`] otherwise.
|
|
/// In that case, neither the atlas texture nor the atlas layout are
|
|
/// modified.
|
|
pub fn add_glyph(
|
|
&mut self,
|
|
textures: &mut Assets<Image>,
|
|
atlas_layouts: &mut Assets<TextureAtlasLayout>,
|
|
cache_key: cosmic_text::CacheKey,
|
|
texture: &Image,
|
|
offset: IVec2,
|
|
) -> Result<(), TextError> {
|
|
let atlas_layout = atlas_layouts.get_mut(&self.texture_atlas).unwrap();
|
|
let atlas_texture = textures.get_mut(&self.texture).unwrap();
|
|
|
|
if let Some(glyph_index) =
|
|
self.dynamic_texture_atlas_builder
|
|
.add_texture(atlas_layout, texture, atlas_texture)
|
|
{
|
|
self.glyph_to_atlas_index.insert(
|
|
cache_key,
|
|
GlyphAtlasLocation {
|
|
glyph_index,
|
|
offset,
|
|
},
|
|
);
|
|
Ok(())
|
|
} else {
|
|
Err(TextError::FailedToAddGlyph(cache_key.glyph_id))
|
|
}
|
|
}
|
|
}
|
|
|
|
impl core::fmt::Debug for FontAtlas {
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
f.debug_struct("FontAtlas")
|
|
.field("glyph_to_atlas_index", &self.glyph_to_atlas_index)
|
|
.field("texture_atlas", &self.texture_atlas)
|
|
.field("texture", &self.texture)
|
|
.field("dynamic_texture_atlas_builder", &"[...]")
|
|
.finish()
|
|
}
|
|
}
|