Skip to main content

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