orbtk_render/
lib.rs

1#![recursion_limit = "256"]
2
3use std::{any::Any, fmt};
4
5pub mod prelude;
6
7pub use orbtk_utils::prelude as utils;
8
9#[cfg(all(not(target_arch = "wasm32"), feature = "pfinder"))]
10#[path = "pathfinder/mod.rs"]
11pub mod platform;
12
13#[cfg(any(target_arch = "wasm32", feature = "pfinder"))]
14pub use self::platform::*;
15
16#[cfg(all(
17    not(target_arch = "wasm32"),
18    feature = "default",
19    not(feature = "pfinder")
20))]
21#[path = "raqote/mod.rs"]
22pub mod platform;
23
24#[cfg(all(
25    not(target_arch = "wasm32"),
26    feature = "default",
27    not(feature = "pfinder")
28))]
29pub mod concurrent;
30
31#[cfg(all(
32    not(target_arch = "wasm32"),
33    feature = "default",
34    not(feature = "pfinder")
35))]
36pub use self::concurrent::*;
37
38#[cfg(target_arch = "wasm32")]
39#[path = "web/mod.rs"]
40pub mod platform;
41
42#[cfg(target_arch = "wasm32")]
43pub use platform::RenderContext2D;
44
45pub use self::render_target::*;
46
47mod render_target;
48
49/// Defines the current configuration of the render ctx.
50#[derive(Debug, Clone)]
51pub struct RenderConfig {
52    pub fill_style: utils::Brush,
53    pub stroke_style: utils::Brush,
54    pub line_width: f64,
55    pub font_config: FontConfig,
56    pub alpha: f32,
57}
58
59impl Default for RenderConfig {
60    fn default() -> Self {
61        RenderConfig {
62            fill_style: utils::Brush::default(),
63            stroke_style: utils::Brush::default(),
64            line_width: 1.,
65            font_config: FontConfig::default(),
66            alpha: 1.,
67        }
68    }
69}
70
71/// The TextMetrics struct represents the dimension of a text.
72#[derive(Clone, Copy, Default, Debug)]
73pub struct TextMetrics {
74    pub width: f64,
75    pub height: f64,
76}
77
78// Internal font helper.
79#[derive(Default, Clone, PartialEq, Debug)]
80pub struct FontConfig {
81    pub family: String,
82    pub font_size: f64,
83}
84
85impl ToString for FontConfig {
86    fn to_string(&self) -> String {
87        format!("{}px {}", self.font_size, self.family)
88    }
89}
90
91pub trait RenderPipeline {
92    /// Draws the ctx of the pipeline.
93    fn draw(&self, image: &mut RenderTarget);
94}
95
96/// Used to implement a custom render pipeline.
97pub trait PipelineTrait: RenderPipeline + Any + Send {
98    /// Equality for two Pipeline objects.
99    fn box_eq(&self, other: &dyn Any) -> bool;
100
101    /// Converts self to an any reference.
102    fn as_any(&self) -> &dyn Any;
103
104    /// Clones self as box.
105    fn clone_box(&self) -> Box<dyn PipelineTrait>;
106
107    /// Draws the ctx of the pipeline.
108    fn draw_pipeline(&self, image: &mut RenderTarget) {
109        self.draw(image);
110    }
111}
112
113impl PartialEq for Box<dyn PipelineTrait> {
114    fn eq(&self, other: &Box<dyn PipelineTrait>) -> bool {
115        self.box_eq(other.as_any())
116    }
117}
118
119impl Clone for Box<dyn PipelineTrait> {
120    fn clone(&self) -> Self {
121        self.clone_box()
122    }
123}
124
125impl fmt::Debug for Box<dyn PipelineTrait> {
126    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127        write!(f, "Box<dyn PipelineTrait>")
128    }
129}