#[cfg(target_family = "wasm")] use base64::Engine; #[cfg(target_family = "wasm")] use canvas_utils::{Canvas, CanvasHandle}; #[cfg(target_family = "wasm")] use core_types::math::bbox::Bbox; use core_types::table::{Table, TableRow}; #[cfg(target_family = "wasm")] use core_types::transform::Footprint; #[cfg(target_family = "wasm")] use core_types::{ATTR_EDITOR_MERGED_LAYERS, ATTR_TRANSFORM, WasmNotSend}; use core_types::{Color, Ctx}; pub use graph_craft::application_io::*; pub use graph_craft::document::value::RenderOutputType; use graphene_application_io::ApplicationIo; #[cfg(target_family = "wasm")] pub use graphene_canvas_utils as canvas_utils; #[cfg(target_family = "wasm")] use graphic_types::Graphic; #[cfg(target_family = "wasm")] use graphic_types::IntoGraphicTable; #[cfg(target_family = "wasm")] use graphic_types::Vector; use graphic_types::raster_types::Image; use graphic_types::raster_types::{CPU, Raster}; #[cfg(target_family = "wasm")] use graphic_types::vector_types::gradient::GradientStops; #[cfg(target_family = "wasm")] use rendering::{Render, RenderParams, RenderSvgSegmentList, SvgRender}; use std::sync::Arc; fn parse_headers(headers: &str) -> reqwest::header::HeaderMap { use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; let mut header_map = HeaderMap::new(); for line in headers.lines() { if let Some((key, value)) = line.split_once(':') { let Ok(header_name) = HeaderName::from_bytes(key.trim().as_bytes()) else { continue }; let Ok(header_value) = HeaderValue::from_str(value.trim()) else { continue }; header_map.insert(header_name, header_value); } } header_map } /// Sends an HTTP GET request to a specified URL and optionally waits for the response (unless discarded) which is output as a string. #[node_macro::node(category("Web Request"))] async fn get_request( _: impl Ctx, _primary: (), /// The web address to send the GET request to. #[name("URL")] url: String, /// Makes the request run in the background without waiting on a response. This is useful for triggering webhooks without blocking the continued execution of the graph. discard_result: bool, #[widget(ParsedWidgetOverride::Custom = "text_area")] headers: String, ) -> String { let header_map = parse_headers(&headers); let request = reqwest::Client::new().get(url).headers(header_map); if discard_result { #[cfg(target_family = "wasm")] wasm_bindgen_futures::spawn_local(async move { let _ = request.send().await; }); #[cfg(all(not(target_family = "wasm"), feature = "tokio"))] tokio::spawn(async move { let _ = request.send().await; }); return String::new(); } let Ok(response) = request.send().await else { return String::new(); }; response.text().await.ok().unwrap_or_default() } /// Sends an HTTP POST request to a specified URL with the provided binary data and optionally waits for the response (unless discarded) which is output as a string. #[node_macro::node(category("Web Request"))] async fn post_request( _: impl Ctx, _primary: (), /// The web address to send the POST request to. #[name("URL")] url: String, /// The binary data to include in the body of the POST request. body: Table, /// Makes the request run in the background without waiting on a response. This is useful for triggering webhooks without blocking the continued execution of the graph. discard_result: bool, #[widget(ParsedWidgetOverride::Custom = "text_area")] headers: String, ) -> String { let mut header_map = parse_headers(&headers); header_map.insert("Content-Type", "application/octet-stream".parse().unwrap()); let body_bytes: Vec = body.iter_element_values().copied().collect(); let request = reqwest::Client::new().post(url).body(body_bytes).headers(header_map); if discard_result { #[cfg(target_family = "wasm")] wasm_bindgen_futures::spawn_local(async move { let _ = request.send().await; }); #[cfg(all(not(target_family = "wasm"), feature = "tokio"))] tokio::spawn(async move { let _ = request.send().await; }); return String::new(); } let Ok(response) = request.send().await else { return String::new(); }; response.text().await.ok().unwrap_or_default() } /// Converts a text string to raw binary data. Useful for transmission over HTTP or writing to files. #[node_macro::node(category("Web Request"), name("String to Bytes"))] fn string_to_bytes(_: impl Ctx, string: String) -> Table { string.into_bytes().into_iter().map(TableRow::new_from_element).collect() } /// Converts extracted raw RGBA pixel data from an input image. Each pixel becomes 4 sequential bytes. Useful for transmission over HTTP or writing to files. #[node_macro::node(category("Web Request"), name("Image to Bytes"))] fn image_to_bytes(_: impl Ctx, image: Table>) -> Table { let Some(image) = image.element(0) else { return Table::new() }; image.data.iter().flat_map(|color| color.to_rgba8_srgb()).map(TableRow::new_from_element).collect() } /// Loads binary from URLs and local asset paths. Returns a transparent placeholder if the resource fails to load, allowing rendering to continue. #[node_macro::node(category("Web Request"))] async fn load_resource<'a: 'n>(_: impl Ctx, _primary: (), #[scope("editor-api")] editor_resources: &'a PlatformEditorApi, #[name("URL")] url: String) -> Arc<[u8]> { let Some(api) = editor_resources.application_io.as_ref() else { return Arc::from(include_bytes!("../../../graph-craft/src/null.png").to_vec()); }; let Ok(data) = api.load_resource(url) else { return Arc::from(include_bytes!("../../../graph-craft/src/null.png").to_vec()); }; let Ok(data) = data.await else { return Arc::from(include_bytes!("../../../graph-craft/src/null.png").to_vec()); }; data } /// Converts raw binary data to a raster image. /// /// Works with standard image format (PNG, JPEG, WebP, etc.). Automatically converts the color space to linear sRGB for accurate compositing. #[node_macro::node(category("Web Request"))] fn decode_image(_: impl Ctx, data: Arc<[u8]>) -> Table> { let Some(image) = image::load_from_memory(data.as_ref()).ok() else { return Table::new(); }; let image = image.to_rgba32f(); let image = Image { data: image .chunks(4) .map(|pixel| Color::from_unassociated_alpha(pixel[0], pixel[1], pixel[2], pixel[3]).to_linear_srgb()) .collect(), width: image.width(), height: image.height(), ..Default::default() }; Table::new_from_element(Raster::new_cpu(image)) } #[cfg(target_family = "wasm")] #[node_macro::node(category(""))] async fn create_canvas(_: impl Ctx) -> CanvasHandle { CanvasHandle::new() } /// Renders a view of the input graphic within an area defined by the *Footprint*. #[cfg(target_family = "wasm")] #[node_macro::node(category(""))] async fn rasterize( _: impl Ctx, #[implementations( Table, Table>, Table, Table, Table, )] mut data: Table, footprint: Footprint, mut canvas: CanvasHandle, ) -> Table> where Table: Render + Clone + graphic_types::IntoGraphicTable, { use glam::{DAffine2, DVec2}; if footprint.transform.matrix2.determinant() == 0. { log::trace!("Invalid footprint received for rasterization"); return Table::new(); } // Snapshot the input as a Table so the renderer can recurse into the original child layers // when collecting metadata, exposing their click targets to editor tools (same mechanism as Boolean Operation). let upstream_graphic_table = data.clone().into_graphic_table(); let mut render = SvgRender::new(); let aabb = Bbox::from_transform(footprint.transform).to_axis_aligned_bbox(); let size = aabb.size(); let resolution = footprint.resolution; let render_params = RenderParams { footprint, for_export: true, ..Default::default() }; for transform in data.iter_attribute_values_mut_or_default::(ATTR_TRANSFORM) { *transform = DAffine2::from_translation(-aabb.start) * *transform; } data.render_svg(&mut render, &render_params); render.format_svg(DVec2::ZERO, size); let svg_string = render.svg.to_svg_string(); canvas.set_resolution(resolution); let context = canvas.context(); let preamble = "data:image/svg+xml;base64,"; let mut base64_string = String::with_capacity(preamble.len() + svg_string.len() * 4); base64_string.push_str(preamble); base64::engine::general_purpose::STANDARD.encode_string(svg_string, &mut base64_string); let image_data = web_sys::HtmlImageElement::new().unwrap(); image_data.set_src(base64_string.as_str()); wasm_bindgen_futures::JsFuture::from(image_data.decode()).await.unwrap(); context .draw_image_with_html_image_element_and_dw_and_dh(&image_data, 0., 0., resolution.x as f64, resolution.y as f64) .unwrap(); let rasterized = context.get_image_data(0., 0., resolution.x as f64, resolution.y as f64).unwrap(); let image = Image::from_image_data(&rasterized.data().0, resolution.x as u32, resolution.y as u32); Table::new_from_row( TableRow::new_from_element(Raster::new_cpu(image)) .with_attribute(ATTR_TRANSFORM, footprint.transform) .with_attribute(ATTR_EDITOR_MERGED_LAYERS, upstream_graphic_table), ) }