grafo/
lib.rs

1//! # Grafo
2//!
3//! [![Grafo crate](https://img.shields.io/crates/v/grafo.svg)](https://crates.io/crates/grafo)
4//! [![Grafo documentation](https://docs.rs/grafo/badge.svg)](https://docs.rs/grafo)
5//! [![Build and test](https://github.com/antouhou/grafo/actions/workflows/rust.yml/badge.svg?branch=main)](https://github.com/antouhou/grafo/actions)
6//!
7//! Grafo is a GPU-accelerated rendering library for Rust. It is a one-stop solution in case
8//! you need a quick and simple way to render shapes, images, and text in your application. It
9//! supports features such as masking, clipping, and font loading and rendering.
10//!
11//! The library is designed for flexibility and ease of use, making it suitable for a wide
12//! range of applications, from simple graphical interfaces to complex rendering engines.
13//!
14//! ## Features
15//!
16//! * Shape Rendering: Create and render complex vector shapes.
17//! * Image Rendering: Render images with support for clipping to shapes.
18//! * Text Rendering: Load fonts and render text with customizable layout, alignment, and styling using the
19//!    [glyphon](https://github.com/grovesNL/glyphon) crate.
20//! * Stencil Operations: Advanced stencil operations for clipping and masking.
21//!
22//! Grafo [available on crates.io](https://crates.io/crates/grafo), and
23//! [API Documentation is available on docs.rs](https://docs.rs/grafo/).
24//!
25//! ## Getting Started
26//!
27//! Add the following to your `Cargo.toml`:
28//!
29//! ```toml
30//! [dependencies]
31//! grafo = "0.1.0"
32//! winit = "0.27"   # For window creation and event handling
33//! image = "0.24"   # For image processing
34//! env_logger = "0.10" # For logging
35//! log = "0.4"      # For logging
36//! ```
37//!
38//! ### Basic Usage
39//!
40//! Below is a simple example demonstrating how to initialize the `Renderer`, add shapes and text,
41//! and render a frame using `winit`. For a more comprehensive example, refer to the
42//! [examples](https://github.com/antouhou/grafo/tree/main/examples) folder in the repository.
43//!
44//! ```rust,no_run
45//! use futures::executor::block_on;
46//! use grafo::{BorderRadii, Shape};
47//! use grafo::{Color, Stroke};
48//! use std::sync::Arc;
49//! use winit::event::{Event, WindowEvent};
50//! use winit::event_loop::EventLoop;
51//! use winit::window::WindowBuilder;
52//!
53//!     env_logger::init();
54//!     let event_loop = EventLoop::new().expect("to start an event loop");
55//!     let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap());
56//!
57//!     let window_size = window.inner_size();
58//!     let scale_factor = window.scale_factor();
59//!     let physical_size = (window_size.width, window_size.height);
60//!
61//!     // Initialize the renderer
62//!     let mut renderer = block_on(grafo::Renderer::new(
63//!         window.clone(),
64//!         physical_size,
65//!         scale_factor,
66//!     ));
67//!
68//!     // Define a simple rectangle shape
69//!     let rect = Shape::rect(
70//!         [(0.0, 0.0), (200.0, 100.0)],
71//!         Color::rgb(0, 128, 255), // Blue fill
72//!         Stroke::new(2.0, Color::BLACK), // Black stroke with width 2.0
73//!     );
74//!     renderer.add_shape(rect, None, (100.0, 100.0), None);
75//!
76//!     // Start the event loop
77//!     event_loop.run(move |event, event_loop_window_target| match event {
78//!         Event::WindowEvent {
79//!             ref event,
80//!             window_id,
81//!         } if window_id == window.id() => match event {
82//!             WindowEvent::CloseRequested => event_loop_window_target.exit(),
83//!             WindowEvent::Resized(physical_size) => {
84//!                 let new_size = (physical_size.width, physical_size.height);
85//!                 renderer.resize(new_size);
86//!                 window.request_redraw();
87//!             }
88//!             WindowEvent::RedrawRequested => {
89//!                 match renderer.render() {
90//!                     Ok(_) => {
91//!                         renderer.clear_draw_queue();
92//!                     }
93//!                     Err(wgpu::SurfaceError::Lost) => renderer.resize(renderer.size()),
94//!                     Err(wgpu::SurfaceError::OutOfMemory) => event_loop_window_target.exit(),
95//!                     Err(e) => eprintln!("{:?}", e),
96//!                 }
97//!             }
98//!             _ => {}
99//!         },
100//!         _ => {}
101//!     });
102//! ```
103//!
104//! ## Examples
105//!
106//! For a detailed example showcasing advanced features like hierarchical clipping,
107//! image rendering, and text rendering, please refer to the
108//! [examples](https://github.com/antouhou/grafo/tree/main/examples) directory in the repository.
109
110pub use glyphon;
111pub use glyphon::fontdb;
112pub use glyphon::fontdb::Family as FontFamily;
113pub use glyphon::fontdb::Source as FontSource;
114pub use lyon;
115pub use wgpu;
116
117mod color;
118mod debug_tools;
119mod id;
120mod image_draw_data;
121mod pipeline;
122mod renderer;
123mod stroke;
124mod util;
125mod vertex;
126
127mod cache;
128mod shape;
129mod text;
130mod texture_manager;
131
132pub use color::Color;
133pub use renderer::MathRect;
134pub use renderer::Renderer;
135pub use shape::*;
136pub use stroke::Stroke;
137pub use text::*;
138pub use texture_manager::TextureManager;