
# Objective
- Introduce a stable alternative to
[`std::any::type_name`](https://doc.rust-lang.org/std/any/fn.type_name.html).
- Rewrite of #5805 with heavy inspiration in design.
- On the path to #5830.
- Part of solving #3327.
## Solution
- Add a `TypePath` trait for static stable type path/name information.
- Add a `TypePath` derive macro.
- Add a `impl_type_path` macro for implementing internal and foreign
types in `bevy_reflect`.
---
## Changelog
- Added `TypePath` trait.
- Added `DynamicTypePath` trait and `get_type_path` method to `Reflect`.
- Added a `TypePath` derive macro.
- Added a `bevy_reflect::impl_type_path` for implementing `TypePath` on
internal and foreign types in `bevy_reflect`.
- Changed `bevy_reflect::utility::(Non)GenericTypeInfoCell` to
`(Non)GenericTypedCell<T>` which allows us to be generic over both
`TypeInfo` and `TypePath`.
- `TypePath` is now a supertrait of `Asset`, `Material` and
`Material2d`.
- `impl_reflect_struct` needs a `#[type_path = "..."]` attribute to be
specified.
- `impl_reflect_value` needs to either specify path starting with a
double colon (`::core::option::Option`) or an `in my_crate::foo`
declaration.
- Added `bevy_reflect_derive::ReflectTypePath`.
- Most uses of `Ident` in `bevy_reflect_derive` changed to use
`ReflectTypePath`.
## Migration Guide
- Implementors of `Asset`, `Material` and `Material2d` now also need to
derive `TypePath`.
- Manual implementors of `Reflect` will need to implement the new
`get_type_path` method.
## Open Questions
- [x] ~This PR currently does not migrate any usages of
`std::any::type_name` to use `bevy_reflect::TypePath` to ease the review
process. Should it?~ Migration will be left to a follow-up PR.
- [ ] This PR adds a lot of `#[derive(TypePath)]` and `T: TypePath` to
satisfy new bounds, mostly when deriving `TypeUuid`. Should we make
`TypePath` a supertrait of `TypeUuid`? [Should we remove `TypeUuid` in
favour of
`TypePath`?](2afbd85532 (r961067892)
)
46 lines
1.4 KiB
Rust
46 lines
1.4 KiB
Rust
use ab_glyph::{FontArc, FontVec, InvalidFont, OutlinedGlyph};
|
|
use bevy_reflect::{TypePath, TypeUuid};
|
|
use bevy_render::{
|
|
render_resource::{Extent3d, TextureDimension, TextureFormat},
|
|
texture::Image,
|
|
};
|
|
|
|
#[derive(Debug, TypeUuid, TypePath, Clone)]
|
|
#[uuid = "97059ac6-c9ba-4da9-95b6-bed82c3ce198"]
|
|
pub struct Font {
|
|
pub font: FontArc,
|
|
}
|
|
|
|
impl Font {
|
|
pub fn try_from_bytes(font_data: Vec<u8>) -> Result<Self, InvalidFont> {
|
|
let font = FontVec::try_from_vec(font_data)?;
|
|
let font = FontArc::new(font);
|
|
Ok(Font { font })
|
|
}
|
|
|
|
pub fn get_outlined_glyph_texture(outlined_glyph: OutlinedGlyph) -> Image {
|
|
let bounds = outlined_glyph.px_bounds();
|
|
let width = bounds.width() as usize;
|
|
let height = bounds.height() as usize;
|
|
let mut alpha = vec![0.0; width * height];
|
|
outlined_glyph.draw(|x, y, v| {
|
|
alpha[y as usize * width + x as usize] = v;
|
|
});
|
|
|
|
// TODO: make this texture grayscale
|
|
Image::new(
|
|
Extent3d {
|
|
width: width as u32,
|
|
height: height as u32,
|
|
depth_or_array_layers: 1,
|
|
},
|
|
TextureDimension::D2,
|
|
alpha
|
|
.iter()
|
|
.flat_map(|a| vec![255, 255, 255, (*a * 255.0) as u8])
|
|
.collect::<Vec<u8>>(),
|
|
TextureFormat::Rgba8UnormSrgb,
|
|
)
|
|
}
|
|
}
|