epaint/
lib.rs

1//! A simple 2D graphics library for turning simple 2D shapes and text into textured triangles.
2//!
3//! Made for [`egui`](https://github.com/emilk/egui/).
4//!
5//! Create some [`Shape`]:s and pass them to [`tessellate_shapes`] to generate [`Mesh`]:es
6//! that you can then paint using some graphics API of your choice (e.g. OpenGL).
7//!
8//! ## Coordinate system
9//! The left-top corner of the screen is `(0.0, 0.0)`,
10//! with X increasing to the right and Y increasing downwards.
11//!
12//! `epaint` uses logical _points_ as its coordinate system.
13//! Those related to physical _pixels_ by the `pixels_per_point` scale factor.
14//! For example, a high-dpi screen can have `pixels_per_point = 2.0`,
15//! meaning there are two physical screen pixels for each logical point.
16//!
17//! Angles are in radians, and are measured clockwise from the X-axis, which has angle=0.
18//!
19//! ## Feature flags
20#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
21//!
22
23#![allow(clippy::float_cmp)]
24#![allow(clippy::manual_range_contains)]
25
26mod bezier;
27pub mod color;
28pub mod image;
29mod margin;
30mod mesh;
31pub mod mutex;
32mod shadow;
33mod shape;
34pub mod shape_transform;
35pub mod stats;
36mod stroke;
37pub mod tessellator;
38pub mod text;
39mod texture_atlas;
40mod texture_handle;
41pub mod textures;
42pub mod util;
43
44pub use self::{
45    bezier::{CubicBezierShape, QuadraticBezierShape},
46    color::ColorMode,
47    image::{ColorImage, FontImage, ImageData, ImageDelta},
48    margin::Margin,
49    mesh::{Mesh, Mesh16, Vertex},
50    shadow::Shadow,
51    shape::{
52        CircleShape, EllipseShape, PaintCallback, PaintCallbackInfo, PathShape, RectShape,
53        Rounding, Shape, TextShape,
54    },
55    stats::PaintStats,
56    stroke::{PathStroke, Stroke},
57    tessellator::{TessellationOptions, Tessellator},
58    text::{FontFamily, FontId, Fonts, Galley},
59    texture_atlas::TextureAtlas,
60    texture_handle::TextureHandle,
61    textures::TextureManager,
62};
63
64#[allow(deprecated)]
65pub use tessellator::tessellate_shapes;
66
67pub use ecolor::{Color32, Hsva, HsvaGamma, Rgba};
68pub use emath::{pos2, vec2, Pos2, Rect, Vec2};
69
70pub use ahash;
71pub use ecolor;
72pub use emath;
73
74#[cfg(feature = "color-hex")]
75pub use ecolor::hex_color;
76
77/// The UV coordinate of a white region of the texture mesh.
78/// The default egui texture has the top-left corner pixel fully white.
79/// You need need use a clamping texture sampler for this to work
80/// (so it doesn't do bilinear blending with bottom right corner).
81pub const WHITE_UV: emath::Pos2 = emath::pos2(0.0, 0.0);
82
83/// What texture to use in a [`Mesh`] mesh.
84///
85/// If you don't want to use a texture, use `TextureId::Managed(0)` and the [`WHITE_UV`] for uv-coord.
86#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
87#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
88pub enum TextureId {
89    /// Textures allocated using [`TextureManager`].
90    ///
91    /// The first texture (`TextureId::Managed(0)`) is used for the font data.
92    Managed(u64),
93
94    /// Your own texture, defined in any which way you want.
95    /// The backend renderer will presumably use this to look up what texture to use.
96    User(u64),
97}
98
99impl Default for TextureId {
100    /// The epaint font texture.
101    fn default() -> Self {
102        Self::Managed(0)
103    }
104}
105
106/// A [`Shape`] within a clip rectangle.
107///
108/// Everything is using logical points.
109#[derive(Clone, Debug, PartialEq)]
110pub struct ClippedShape {
111    /// Clip / scissor rectangle.
112    /// Only show the part of the [`Shape`] that falls within this.
113    pub clip_rect: emath::Rect,
114
115    /// The shape
116    pub shape: Shape,
117}
118
119/// A [`Mesh`] or [`PaintCallback`] within a clip rectangle.
120///
121/// Everything is using logical points.
122#[derive(Clone, Debug)]
123pub struct ClippedPrimitive {
124    /// Clip / scissor rectangle.
125    /// Only show the part of the [`Mesh`] that falls within this.
126    pub clip_rect: emath::Rect,
127
128    /// What to paint - either a [`Mesh`] or a [`PaintCallback`].
129    pub primitive: Primitive,
130}
131
132/// A rendering primitive - either a [`Mesh`] or a [`PaintCallback`].
133#[derive(Clone, Debug)]
134pub enum Primitive {
135    Mesh(Mesh),
136    Callback(PaintCallback),
137}
138
139// ---------------------------------------------------------------------------
140
141/// Was epaint compiled with the `rayon` feature?
142pub const HAS_RAYON: bool = cfg!(feature = "rayon");
143
144// ---------------------------------------------------------------------------
145
146mod profiling_scopes {
147    #![allow(unused_macros)]
148    #![allow(unused_imports)]
149
150    /// Profiling macro for feature "puffin"
151    macro_rules! profile_function {
152        ($($arg: tt)*) => {
153            #[cfg(feature = "puffin")]
154            #[cfg(not(target_arch = "wasm32"))] // Disabled on web because of the coarse 1ms clock resolution there.
155            puffin::profile_function!($($arg)*);
156        };
157    }
158    pub(crate) use profile_function;
159
160    /// Profiling macro for feature "puffin"
161    macro_rules! profile_scope {
162        ($($arg: tt)*) => {
163            #[cfg(feature = "puffin")]
164            #[cfg(not(target_arch = "wasm32"))] // Disabled on web because of the coarse 1ms clock resolution there.
165            puffin::profile_scope!($($arg)*);
166        };
167    }
168    pub(crate) use profile_scope;
169}
170
171#[allow(unused_imports)]
172pub(crate) use profiling_scopes::*;