Skip to content

Commit

Permalink
Placeholder implementation of intrinsic plot sizing (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
seeM authored Aug 16, 2024
1 parent 95292fa commit 977fb70
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
60 changes: 53 additions & 7 deletions crates/amalthea/src/comm/plot_comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
use serde::Deserialize;
use serde::Serialize;

/// The intrinsic size of a plot, if known
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct IntrinsicSize {
/// The width of the plot
pub width: f64,

/// The height of the plot
pub height: f64,

/// The unit of measurement of the plot's dimensions
pub unit: PlotUnit,

/// The source of the intrinsic size e.g. 'Matplotlib'
pub source: String
}

/// A rendered plot
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct PlotResult {
Expand All @@ -21,6 +37,16 @@ pub struct PlotResult {
pub mime_type: String
}

/// The size of a plot
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct PlotSize {
/// The plot's height, in pixels
pub height: i64,

/// The plot's width, in pixels
pub width: i64
}

/// Possible values for Format in Render
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, strum_macros::Display)]
pub enum RenderFormat {
Expand All @@ -41,14 +67,24 @@ pub enum RenderFormat {
Pdf
}

/// Possible values for PlotUnit
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, strum_macros::Display)]
pub enum PlotUnit {
#[serde(rename = "pixels")]
#[strum(to_string = "pixels")]
Pixels,

#[serde(rename = "inches")]
#[strum(to_string = "inches")]
Inches
}

/// Parameters for the Render method.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct RenderParams {
/// The requested plot height, in pixels
pub height: i64,

/// The requested plot width, in pixels
pub width: i64,
/// The requested size of the plot. If not provided, the plot will be
/// rendered at its intrinsic size.
pub size: Option<PlotSize>,

/// The pixel ratio of the display device
pub pixel_ratio: f64,
Expand All @@ -63,10 +99,17 @@ pub struct RenderParams {
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(tag = "method", content = "params")]
pub enum PlotBackendRequest {
/// Get the intrinsic size of a plot, if known.
///
/// The intrinsic size of a plot is the size at which a plot would be if
/// no size constraints were applied by Positron.
#[serde(rename = "get_intrinsic_size")]
GetIntrinsicSize,

/// Render a plot
///
/// Requests a plot to be rendered at a given height and width. The plot
/// data is returned in a base64-encoded string.
/// Requests a plot to be rendered. The plot data is returned in a
/// base64-encoded string.
#[serde(rename = "render")]
Render(RenderParams),

Expand All @@ -78,6 +121,9 @@ pub enum PlotBackendRequest {
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(tag = "method", content = "result")]
pub enum PlotBackendReply {
/// The intrinsic size of a plot, if known
GetIntrinsicSizeReply(Option<IntrinsicSize>),

/// A rendered plot
RenderReply(PlotResult),

Expand Down
10 changes: 8 additions & 2 deletions crates/ark/src/plots/graphics_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,17 @@ impl DeviceContext {
plot_id: &String,
) -> anyhow::Result<PlotBackendReply> {
match message {
PlotBackendRequest::GetIntrinsicSize => {
Ok(PlotBackendReply::GetIntrinsicSizeReply(None))
},
PlotBackendRequest::Render(plot_meta) => {
let size = unwrap!(plot_meta.size, None => {
bail!("Intrinsically sized plots are not yet supported.");
});
let data = self.render_plot(
&plot_id,
plot_meta.width,
plot_meta.height,
size.width,
size.height,
plot_meta.pixel_ratio,
&plot_meta.format,
)?;
Expand Down

0 comments on commit 977fb70

Please sign in to comment.