colorimetry_plot/rendable.rs
1// SPDX-License-Identifier: Apache-2.0 OR MIT
2// Copyright (c) 2025, Harbers Bik LLC
3
4//! # Rendable Trait Module
5//!
6//! This module defines the [`Rendable`] trait, which provides a unified interface for types that can be rendered as SVG elements with configurable view parameters.
7//!
8//! Types implementing [`Rendable`] can be composed into SVG documents, have their viewports adjusted, and be rendered to SVG output. The trait supports both required and optional methods, allowing for flexible and extensible rendering logic.
9//!
10//! ## Features
11//! - Abstracts rendering logic for SVG-compatible types
12//! - Supports view parameter configuration and adjustment
13//! - Enables composition of complex SVG documents from multiple renderable components
14//! - Provides optional methods for fine-grained control over view box position and size
15
16use svg::node::element::SVG;
17
18use crate::view::ViewParameters;
19
20/// A trait for types that can be rendered as SVG elements with configurable view parameters.
21///
22/// Types implementing `Rendable` can be composed into SVG documents, have their viewports adjusted,
23/// and be rendered to SVG output. This trait provides both required and optional methods for
24/// interacting with the object's view parameters and rendering logic.
25///
26/// # Required Methods
27/// - [`view_parameters`](Self::view_parameters): Returns the current view parameters for the object.
28/// - [`set_view_parameters`](Self::set_view_parameters): Sets the view parameters for the object.
29/// - [`render`](Self::render): Renders the object as an SVG element.
30///
31/// # Optional Methods
32/// - [`set_x`](Self::set_x): Sets the x position of the view box.
33/// - [`set_y`](Self::set_y): Sets the y position of the view box.
34/// - [`width`](Self::width): Returns the width of the view box.
35/// - [`height`](Self::height): Returns the height of the view box.
36///
37/// Implementors can override the optional methods for more efficient or specialized behavior.
38pub trait Rendable {
39 /// Returns the current view parameters for the object.
40 fn view_parameters(&self) -> ViewParameters;
41
42 /// Sets the view parameters for the object.
43 fn set_view_parameters(&mut self, view_box: ViewParameters);
44
45 /// Renders the object as an SVG element.
46 fn render(&self) -> SVG;
47
48 /// Sets the x position of the view box (optional).
49 fn set_x(&mut self, x: i32) {
50 let mut vb = self.view_parameters();
51 vb.set_x(x);
52 self.set_view_parameters(vb);
53 }
54
55 /// Sets the y position of the view box (optional).
56 fn set_y(&mut self, y: i32) {
57 let mut vb = self.view_parameters();
58 vb.set_y(y);
59 self.set_view_parameters(vb);
60 }
61
62 /// Returns the width of the view box (optional).
63 fn width(&self) -> u32 {
64 self.view_parameters().width()
65 }
66
67 /// Sets the width of the view box (optional).
68 fn set_width(&mut self, width: u32) {
69 let mut vb = self.view_parameters();
70 vb.set_width(width);
71 self.set_view_parameters(vb);
72 }
73
74 /// Returns the height of the view box (optional).
75 fn height(&self) -> u32 {
76 self.view_parameters().height()
77 }
78
79 /// Sets the height of the view box (optional).
80 fn set_height(&mut self, height: u32) {
81 let mut vb = self.view_parameters();
82 vb.set_height(height);
83 self.set_view_parameters(vb);
84 }
85
86 fn view_box(&mut self) -> (i32, i32, u32, u32) {
87 let vp = self.view_parameters();
88 vp.view_box()
89 }
90
91 fn set_view_box(&mut self, vx: i32, vy: i32, vw: u32, vh: u32) {
92 let mut vp = self.view_parameters();
93 vp.set_view_box(vx, vy, vw, vh);
94 self.set_view_parameters(vp);
95 }
96}