Skip to main content

cryoglyph/
lib.rs

1//! Glyphon provides a simple way to render 2D text with [wgpu], [cosmic-text] and [etagere].
2//!
3//! [wpgu]: https://github.com/gfx-rs/wgpu
4//! [cosmic-text]: https://github.com/pop-os/cosmic-text
5//! [etagere]: https://github.com/nical/etagere
6
7mod cache;
8mod error;
9mod text_atlas;
10mod text_render;
11mod viewport;
12
13pub use cache::Cache;
14pub use error::{PrepareError, RenderError};
15pub use text_atlas::{ColorMode, TextAtlas};
16pub use text_render::TextRenderer;
17pub use viewport::Viewport;
18
19use text_render::ContentType;
20
21// Re-export all top-level types from `cosmic-text` for convenience.
22#[doc(no_inline)]
23pub use cosmic_text::{
24    self, Action, Affinity, Attrs, AttrsList, AttrsOwned, Buffer, BufferLine, CacheKey, Color,
25    Command, Cursor, Edit, Editor, Family, FamilyOwned, Font, FontSystem, LayoutCursor,
26    LayoutGlyph, LayoutLine, LayoutRun, LayoutRunIter, Metrics, ShapeGlyph, ShapeLine, ShapeSpan,
27    ShapeWord, Shaping, Stretch, Style, SubpixelBin, SwashCache, SwashContent, SwashImage, Weight,
28    Wrap, fontdb,
29};
30
31use etagere::AllocId;
32
33pub(crate) enum GpuCacheStatus {
34    InAtlas {
35        x: u16,
36        y: u16,
37        content_type: ContentType,
38    },
39    SkipRasterization,
40}
41
42pub(crate) struct GlyphDetails {
43    width: u16,
44    height: u16,
45    gpu_cache: GpuCacheStatus,
46    atlas_id: Option<AllocId>,
47    top: i16,
48    left: i16,
49}
50
51#[repr(C)]
52#[derive(Clone, Copy, Debug)]
53pub(crate) struct GlyphToRender {
54    pos: [i32; 2],
55    dim: [u16; 2],
56    uv: [u16; 2],
57    color: u32,
58    content_type_with_srgb: [u16; 2],
59    depth: f32,
60}
61
62/// The screen resolution to use when rendering text.
63#[repr(C)]
64#[derive(Clone, Copy, Debug, Eq, PartialEq)]
65pub struct Resolution {
66    /// The width of the screen in pixels.
67    pub width: u32,
68    /// The height of the screen in pixels.
69    pub height: u32,
70}
71
72#[repr(C)]
73#[derive(Clone, Copy, Debug, Eq, PartialEq)]
74pub(crate) struct Params {
75    screen_resolution: Resolution,
76    _pad: [u32; 2],
77}
78
79/// Controls the visible area of the text. Any text outside of the visible area will be clipped.
80#[derive(Clone, Copy, Debug, Eq, PartialEq)]
81pub struct TextBounds {
82    /// The position of the left edge of the visible area.
83    pub left: i32,
84    /// The position of the top edge of the visible area.
85    pub top: i32,
86    /// The position of the right edge of the visible area.
87    pub right: i32,
88    /// The position of the bottom edge of the visible area.
89    pub bottom: i32,
90}
91
92/// The default visible area doesn't clip any text.
93impl Default for TextBounds {
94    fn default() -> Self {
95        Self {
96            left: i32::MIN,
97            top: i32::MIN,
98            right: i32::MAX,
99            bottom: i32::MAX,
100        }
101    }
102}
103
104/// A text area containing text to be rendered along with its overflow behavior.
105#[derive(Clone)]
106pub struct TextArea<'a> {
107    /// The buffer containing the text to be rendered.
108    pub buffer: &'a Buffer,
109    /// The left edge of the buffer.
110    pub left: f32,
111    /// The top edge of the buffer.
112    pub top: f32,
113    /// The scaling to apply to the buffer.
114    pub scale: f32,
115    /// The visible bounds of the text area. This is used to clip the text and doesn't have to
116    /// match the `left` and `top` values.
117    pub bounds: TextBounds,
118    // The default color of the text area.
119    pub default_color: Color,
120}