File size: 10,412 Bytes
224e773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! Multi-format Renderer - SVG, Canvas, WebGL, PNG, PDF, GIF, WebM
use crate::scene_graph::{SceneGraph, NodeGeometry};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RenderFormat {
    SVG,     // Vector graphics (scalable)
    Canvas,  // Canvas 2D JSON specification
    WebGL,   // WebGL 3D specification
    PNG,     // Raster image (requires external renderer)
    PDF,     // PDF document
    GIF,     // Animated GIF
    WebM,    // Video format
}

impl RenderFormat {
    pub fn mime_type(&self) -> &'static str {
        match self {
            Self::SVG => "image/svg+xml",
            Self::Canvas => "application/json",
            Self::WebGL => "application/json",
            Self::PNG => "image/png",
            Self::PDF => "application/pdf",
            Self::GIF => "image/gif",
            Self::WebM => "video/webm",
        }
    }

    pub fn extension(&self) -> &'static str {
        match self {
            Self::SVG => "svg",
            Self::Canvas => "json",
            Self::WebGL => "json",
            Self::PNG => "png",
            Self::PDF => "pdf",
            Self::GIF => "gif",
            Self::WebM => "webm",
        }
    }
}

pub struct Renderer {
    format: RenderFormat,
}

impl Renderer {
    pub fn new(format: RenderFormat) -> Self {
        Self { format }
    }

    pub fn render(&self, scene: &SceneGraph) -> Result<Vec<u8>, String> {
        match self.format {
            RenderFormat::SVG => self.render_svg(scene),
            RenderFormat::Canvas => self.render_canvas(scene),
            RenderFormat::WebGL => self.render_webgl(scene),
            RenderFormat::PNG => self.render_png_spec(scene),
            RenderFormat::PDF => self.render_pdf_spec(scene),
            RenderFormat::GIF => self.render_gif_spec(scene),
            RenderFormat::WebM => self.render_webm_spec(scene),
        }
    }

    fn render_svg(&self, scene: &SceneGraph) -> Result<Vec<u8>, String> {
        let (x_min, y_min, x_max, y_max) = scene.bounds;
        let width = (x_max - x_min) as i32;
        let height = (y_max - y_min) as i32;

        let mut svg = format!(
            r#"<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="{}" height="{}" viewBox="{} {} {} {}">
  <title>{}</title>
  <rect width="{}" height="{}" fill="white"/>
"#,
            width, height, x_min as i32, y_min as i32, width, height, scene.name, width, height
        );

        self.render_node_svg(&scene.root, &mut svg, x_min, y_min)?;

        svg.push_str("</svg>\n");
        Ok(svg.into_bytes())
    }

    fn render_node_svg(&self, node: &crate::scene_graph::Node, svg: &mut String, x_offset: f64, y_offset: f64) -> Result<(), String> {
        let x = node.transform.x - x_offset;
        let y = node.transform.y - y_offset;

        match &node.geometry {
            NodeGeometry::Sphere { radius } => {
                svg.push_str(&format!(
                    r#"  <circle cx="{}" cy="{}" r="{}" fill="rgb({}, {}, {})" opacity="{}" stroke="black" stroke-width="1"/>
"#,
                    x as i32,
                    y as i32,
                    *radius as i32,
                    node.color.r,
                    node.color.g,
                    node.color.b,
                    node.color.a as f64 / 255.0
                ));
            }
            NodeGeometry::Cube { size } => {
                let half = size / 2.0;
                svg.push_str(&format!(
                    r#"  <rect x="{}" y="{}" width="{}" height="{}" fill="rgb({}, {}, {})" opacity="{}" stroke="black" stroke-width="1"/>
"#,
                    (x - half) as i32,
                    (y - half) as i32,
                    *size as i32,
                    *size as i32,
                    node.color.r,
                    node.color.g,
                    node.color.b,
                    node.color.a as f64 / 255.0
                ));
            }
            NodeGeometry::Point { radius } => {
                svg.push_str(&format!(
                    r#"  <circle cx="{}" cy="{}" r="{}" fill="rgb({}, {}, {})" opacity="{}"/>
"#,
                    x as i32,
                    y as i32,
                    *radius as i32,
                    node.color.r,
                    node.color.g,
                    node.color.b,
                    node.color.a as f64 / 255.0
                ));
            }
            NodeGeometry::Plane { width, height } => {
                svg.push_str(&format!(
                    r#"  <rect x="{}" y="{}" width="{}" height="{}" fill="rgb({}, {}, {})" opacity="{}" stroke="black" stroke-width="1"/>
"#,
                    (x - width / 2.0) as i32,
                    (y - height / 2.0) as i32,
                    *width as i32,
                    *height as i32,
                    node.color.r,
                    node.color.g,
                    node.color.b,
                    node.color.a as f64 / 255.0
                ));
            }
            NodeGeometry::Line { x1, y1, x2, y2 } => {
                svg.push_str(&format!(
                    r#"  <line x1="{}" y1="{}" x2="{}" y2="{}" stroke="rgb({}, {}, {})" stroke-width="2"/>
"#,
                    *x1 as i32, *y1 as i32, *x2 as i32, *y2 as i32, node.color.r, node.color.g, node.color.b
                ));
            }
            NodeGeometry::Text { content, font_size } => {
                svg.push_str(&format!(
                    r#"  <text x="{}" y="{}" font-size="{}" fill="rgb({}, {}, {})">{}</text>
"#,
                    x as i32, y as i32, *font_size as i32, node.color.r, node.color.g, node.color.b, content
                ));
            }
            NodeGeometry::Group => {
                // Groups don't render directly
            }
        }

        if let Some(label) = &node.label {
            svg.push_str(&format!(
                r#"  <text x="{}" y="{}" font-size="12" fill="black">{}</text>
"#,
                (x - 10.0) as i32,
                (y - 20.0) as i32,
                label
            ));
        }

        for child in &node.children {
            self.render_node_svg(child, svg, x_offset, y_offset)?;
        }

        Ok(())
    }

    fn render_canvas(&self, scene: &SceneGraph) -> Result<Vec<u8>, String> {
        let spec = serde_json::json!({
            "type": "canvas_2d",
            "scene": scene.name,
            "format": "JSON",
            "bounds": {
                "x_min": scene.bounds.0,
                "y_min": scene.bounds.1,
                "x_max": scene.bounds.2,
                "y_max": scene.bounds.3,
            },
            "elements": self.scene_to_canvas_elements(&scene.root)?,
        });

        Ok(spec.to_string().into_bytes())
    }

    fn scene_to_canvas_elements(&self, node: &crate::scene_graph::Node) -> Result<serde_json::Value, String> {
        let mut elements = vec![];

        match &node.geometry {
            NodeGeometry::Sphere { radius } => {
                elements.push(serde_json::json!({
                    "type": "circle",
                    "x": node.transform.x,
                    "y": node.transform.y,
                    "radius": radius,
                    "fill": format!("rgb({}, {}, {})", node.color.r, node.color.g, node.color.b),
                }));
            }
            NodeGeometry::Cube { size } => {
                elements.push(serde_json::json!({
                    "type": "rect",
                    "x": node.transform.x - size / 2.0,
                    "y": node.transform.y - size / 2.0,
                    "width": size,
                    "height": size,
                    "fill": format!("rgb({}, {}, {})", node.color.r, node.color.g, node.color.b),
                }));
            }
            _ => {}
        }

        for child in &node.children {
            let child_elements = self.scene_to_canvas_elements(child)?;
            if let serde_json::Value::Array(arr) = child_elements {
                elements.extend(arr);
            }
        }

        Ok(serde_json::Value::Array(elements))
    }

    fn render_webgl(&self, _scene: &SceneGraph) -> Result<Vec<u8>, String> {
        let spec = serde_json::json!({
            "type": "webgl",
            "format": "GLSL + JSON",
            "capabilities": ["3D", "lighting", "texture_mapping"],
            "note": "Render as GLTF with Khronos materials"
        });

        Ok(spec.to_string().into_bytes())
    }

    fn render_png_spec(&self, _scene: &SceneGraph) -> Result<Vec<u8>, String> {
        let spec = serde_json::json!({
            "type": "png_specification",
            "note": "Use headless Chromium or Puppeteer to render SVG β†’ PNG",
            "pipeline": "SVG β†’ rasterize(96 DPI) β†’ PNG"
        });

        Ok(spec.to_string().into_bytes())
    }

    fn render_pdf_spec(&self, _scene: &SceneGraph) -> Result<Vec<u8>, String> {
        let spec = serde_json::json!({
            "type": "pdf_specification",
            "note": "Use wkhtmltopdf or similar to generate PDF from SVG",
            "pipeline": "SVG β†’ PDF renderer β†’ PDF"
        });

        Ok(spec.to_string().into_bytes())
    }

    fn render_gif_spec(&self, _scene: &SceneGraph) -> Result<Vec<u8>, String> {
        let spec = serde_json::json!({
            "type": "gif_specification",
            "note": "Render animation frames from scene graph transformations",
            "pipeline": "Scene β†’ interpolate keyframes β†’ render each β†’ GIF encode"
        });

        Ok(spec.to_string().into_bytes())
    }

    fn render_webm_spec(&self, _scene: &SceneGraph) -> Result<Vec<u8>, String> {
        let spec = serde_json::json!({
            "type": "webm_specification",
            "note": "Generate video from animated proof steps",
            "codec": "VP8 or VP9",
            "pipeline": "Scene β†’ render frames β†’ encode WebM"
        });

        Ok(spec.to_string().into_bytes())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn format_mime_types() {
        assert_eq!(RenderFormat::SVG.mime_type(), "image/svg+xml");
        assert_eq!(RenderFormat::PNG.mime_type(), "image/png");
    }

    #[test]
    fn format_extensions() {
        assert_eq!(RenderFormat::SVG.extension(), "svg");
        assert_eq!(RenderFormat::WebM.extension(), "webm");
    }
}