bevy_text/
glyph.rs

1//! This module exports types related to rendering glyphs.
2
3use bevy_asset::AssetId;
4use bevy_image::prelude::*;
5use bevy_math::{IVec2, Vec2};
6use bevy_reflect::Reflect;
7
8/// A glyph of a font, typically representing a single character, positioned in screen space.
9///
10/// Contains information about how and where to render a glyph.
11///
12/// Used in [`TextPipeline::queue_text`](crate::TextPipeline::queue_text) and [`crate::TextLayoutInfo`] for rendering glyphs.
13#[derive(Debug, Clone, Reflect)]
14#[reflect(Clone)]
15pub struct PositionedGlyph {
16    /// The position of the glyph in the text block's bounding box.
17    pub position: Vec2,
18    /// The width and height of the glyph in logical pixels.
19    pub size: Vec2,
20    /// Information about the glyph's atlas.
21    pub atlas_info: GlyphAtlasInfo,
22    /// The index of the glyph in the [`ComputedTextBlock`](crate::ComputedTextBlock)'s tracked spans.
23    pub span_index: usize,
24    /// The index of the glyph's line.
25    pub line_index: usize,
26    /// The byte index of the glyph in its line.
27    pub byte_index: usize,
28    /// The byte length of the glyph.
29    pub byte_length: usize,
30}
31
32/// Information about a glyph in an atlas.
33///
34/// Rasterized glyphs are stored as rectangles
35/// in one or more [`FontAtlas`](crate::FontAtlas)es.
36///
37/// Used in [`PositionedGlyph`] and [`FontAtlasSet`](crate::FontAtlasSet).
38#[derive(Debug, Clone, Reflect)]
39#[reflect(Clone)]
40pub struct GlyphAtlasInfo {
41    /// An asset ID to the [`Image`] data for the texture atlas this glyph was placed in.
42    ///
43    /// An asset ID of the handle held by the [`FontAtlas`](crate::FontAtlas).
44    pub texture: AssetId<Image>,
45    /// An asset ID to the [`TextureAtlasLayout`] map for the texture atlas this glyph was placed
46    /// in.
47    ///
48    /// An asset ID of the handle held by the [`FontAtlas`](crate::FontAtlas).
49    pub texture_atlas: AssetId<TextureAtlasLayout>,
50    /// Location and offset of a glyph within the texture atlas.
51    pub location: GlyphAtlasLocation,
52}
53
54/// The location of a glyph in an atlas,
55/// and how it should be positioned when placed.
56///
57/// Used in [`GlyphAtlasInfo`] and [`FontAtlas`](crate::FontAtlas).
58#[derive(Debug, Clone, Copy, Reflect)]
59#[reflect(Clone)]
60pub struct GlyphAtlasLocation {
61    /// The index of the glyph in the atlas
62    pub glyph_index: usize,
63    /// The required offset (relative positioning) when placed
64    pub offset: IVec2,
65}