Skip to main content

wgame_gfx_typography/
lib.rs

1//! Glyphs test depth with `LessEqual` without writing depth, preserving smooth
2//! alpha edges. Draw translucent text after opaque content, back to front.
3//! GPU text rendering with shaping, alignment, and cached monochrome glyphs.
4//!
5//! Load a [`Font`] once, rasterize it for a pixel size, and reuse the [`FontTexture`]
6//! for labels. Cache unchanged [`Text`] objects outside the frame loop; recreate
7//! size-dependent rasters after resizing when appropriate.
8//!
9//! For HiDPI UI, rasterize at `logical_size * scale_factor` physical pixels and
10//! draw text scaled by `logical_size` through a logical-pixel camera. Refresh
11//! the raster and cached text when the scale factor changes; enlarging an old
12//! raster alone reduces sharpness.
13//!
14//! Text respects glyph placement offsets from [`wgame_typography`]; see that crate
15//! for rasterization and layout limitations.
16
17#![forbid(unsafe_code)]
18
19mod library;
20mod render;
21mod text;
22mod texture;
23
24pub use self::{
25    library::{TypographyLibrary, TypographyState},
26    text::{Text, TextAlign},
27    texture::FontTexture,
28};
29
30pub use wgame_typography::{Font as FontData, FontAtlas, RasterSettings, TextMetrics};
31
32/// A font for GPU text rendering.
33pub struct Font {
34    lib: TypographyLibrary,
35    data: FontData,
36}
37
38impl Font {
39    /// Creates a new font for GPU rendering.
40    pub fn new(lib: &TypographyLibrary, data: &FontData) -> Self {
41        Self {
42            lib: lib.clone(),
43            data: data.clone(),
44        }
45    }
46
47    /// Rasterizes the font with the given settings.
48    pub fn rasterize(&self, settings: impl Into<RasterSettings>) -> FontTexture {
49        self.lib.texture(&self.data, settings)
50    }
51}