excalidraw-render 0.1.0

Render Excalidraw drawings to SVG, PNG and PDF in pure Rust, without a browser
Documentation
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use std::error::Error;
use std::fmt;

use crate::scene::{ElementKind, SourceField};
use crate::{
    CompatibilityDiagnostic, ErrorCode, ErrorDescriptor, FrameRenderRequest, RenderError,
    RenderOutcome, SceneDocument,
};

/// Kind of live frame occurrence discovered in a scene document.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum FrameKind {
    /// An ordinary Excalidraw frame.
    Frame,
    /// An Excalidraw magic frame.
    MagicFrame,
}

impl FrameKind {
    /// Returns the stable Excalidraw element-kind spelling.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Frame => "frame",
            Self::MagicFrame => "magicframe",
        }
    }
}

impl fmt::Display for FrameKind {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// Borrowed identity and display metadata for one live frame occurrence.
#[derive(Clone, Copy)]
pub struct FrameRef<'scene> {
    pub(crate) scene: &'scene SceneDocument,
    pub(crate) source_index: usize,
    ordinal: u32,
    kind: FrameKind,
    id: Option<&'scene str>,
    name: Option<&'scene str>,
}

impl FrameRef<'_> {
    /// Returns the one-based source-order position among live frame occurrences.
    pub const fn ordinal(self) -> u32 {
        self.ordinal
    }

    /// Returns whether this is an ordinary or magic frame.
    pub const fn kind(self) -> FrameKind {
        self.kind
    }

    pub(crate) fn belongs_to(self, scene: &SceneDocument) -> bool {
        self.scene.same_identity(scene)
    }
}

impl<'scene> FrameRef<'scene> {
    /// Returns the source element ID when it is a non-empty string.
    ///
    /// Excalidraw gives every element a unique ID, but a hand-edited or
    /// damaged scene can omit or repeat one; the renderer repairs such IDs
    /// when it renders, and the [ordinal](Self::ordinal) always identifies
    /// the occurrence.
    pub const fn id(self) -> Option<&'scene str> {
        self.id
    }

    /// Returns the source display name when it was a string.
    pub const fn name(self) -> Option<&'scene str> {
        self.name
    }
}

/// Two references are equal when they name the same frame occurrence of the
/// same admitted scene. A clone of a [`SceneDocument`] is the same scene; a
/// second parse of the same bytes is not.
impl PartialEq for FrameRef<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.belongs_to(other.scene) && self.source_index == other.source_index
    }
}

impl Eq for FrameRef<'_> {}

impl std::hash::Hash for FrameRef<'_> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.scene.hash_identity(state);
        self.source_index.hash(state);
    }
}

impl fmt::Debug for FrameRef<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("FrameRef")
            .field("ordinal", &self.ordinal)
            .field("kind", &self.kind)
            .field("id", &self.id)
            .field("name", &self.name)
            .finish_non_exhaustive()
    }
}

/// Iterator over live ordinary and magic frame occurrences.
#[derive(Clone, Debug)]
pub struct FrameRefs<'scene> {
    scene: &'scene SceneDocument,
    source_index: usize,
    ordinal: u32,
}

impl<'scene> FrameRefs<'scene> {
    pub(crate) const fn new(scene: &'scene SceneDocument) -> Self {
        Self {
            scene,
            source_index: 0,
            ordinal: 0,
        }
    }
}

impl<'scene> Iterator for FrameRefs<'scene> {
    type Item = FrameRef<'scene>;

    fn next(&mut self) -> Option<Self::Item> {
        while self.source_index < self.scene.element_occurrence_count() {
            let source_index = self.source_index;
            self.source_index += 1;
            if self.scene.element_is_silent(source_index) {
                continue;
            }
            let Some(source) = self.scene.element_at(source_index) else {
                continue;
            };
            let kind = match source.kind {
                ElementKind::Frame => FrameKind::Frame,
                ElementKind::MagicFrame => FrameKind::MagicFrame,
                _ => continue,
            };
            self.ordinal = self.ordinal.saturating_add(1);
            let id = match source.id {
                SourceField::Value(id) if !id.is_empty() => Some(id),
                SourceField::Value(_) | SourceField::Missing | SourceField::Invalid => None,
            };
            let name = match source.name {
                SourceField::Value(name) => name,
                SourceField::Missing | SourceField::Invalid => None,
            };
            return Some(FrameRef {
                scene: self.scene,
                source_index,
                ordinal: self.ordinal,
                kind,
                id,
                name,
            });
        }
        None
    }
}

/// Stable category of a lazy frame-batch construction failure.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum FrameBatchErrorKind {
    /// A frame reference belongs to another scene document.
    ForeignTarget,
    /// The same frame occurrence was requested more than once.
    DuplicateTarget,
    /// Shared target-neutral preparation could not be completed.
    SharedPreparationFailed,
}

/// Failure to construct a lazy multi-frame render operation.
///
/// [`Error::source`] is always `None`: a shared-preparation failure is
/// flattened into its kind and subject.
#[derive(Clone, Debug)]
pub struct FrameBatchError {
    kind: FrameBatchErrorKind,
    ordinal: Option<u32>,
    preparation_subject: Option<&'static str>,
}

impl FrameBatchError {
    pub(crate) const fn new(kind: FrameBatchErrorKind, ordinal: u32) -> Self {
        Self {
            kind,
            ordinal: Some(ordinal),
            preparation_subject: None,
        }
    }

    pub(crate) const fn preparation(error: &crate::render::FramePreparationError) -> Self {
        Self::from_preparation_subject(error.subject())
    }

    const fn from_preparation_subject(subject: &'static str) -> Self {
        Self {
            kind: FrameBatchErrorKind::SharedPreparationFailed,
            ordinal: None,
            preparation_subject: Some(subject),
        }
    }

    /// Returns the stable batch-construction failure category.
    pub const fn kind(&self) -> FrameBatchErrorKind {
        self.kind
    }

    /// Returns the implicated frame ordinal for a target-selection failure.
    pub const fn ordinal(&self) -> Option<u32> {
        self.ordinal
    }

    /// Returns the stable machine-readable error code.
    pub const fn code(&self) -> ErrorCode {
        match self.kind {
            FrameBatchErrorKind::ForeignTarget => ErrorCode::FRAME_BATCH_FOREIGN_TARGET,
            FrameBatchErrorKind::DuplicateTarget => ErrorCode::FRAME_BATCH_DUPLICATE_TARGET,
            FrameBatchErrorKind::SharedPreparationFailed => {
                ErrorCode::FRAME_BATCH_SHARED_PREPARATION_FAILED
            }
        }
    }

    /// Returns this failure's stable serializable descriptor.
    pub fn descriptor(&self) -> ErrorDescriptor {
        ErrorDescriptor::frame_batch(self.code(), self.to_string())
    }
}

impl fmt::Display for FrameBatchError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.kind {
            FrameBatchErrorKind::ForeignTarget => {
                write!(
                    formatter,
                    "frame {} belongs to another scene",
                    self.ordinal.unwrap_or(0)
                )
            }
            FrameBatchErrorKind::DuplicateTarget => {
                write!(
                    formatter,
                    "frame {} was selected more than once",
                    self.ordinal.unwrap_or(0)
                )
            }
            FrameBatchErrorKind::SharedPreparationFailed => write!(
                formatter,
                "shared frame preparation failed for {}",
                self.preparation_subject.unwrap_or("unknown_subject")
            ),
        }
    }
}

impl Error for FrameBatchError {}

/// One target result yielded by a lazy multi-frame render operation.
#[derive(Debug)]
pub struct FrameRenderEntry<'scene> {
    frame: FrameRef<'scene>,
    result: Result<RenderOutcome, RenderError>,
    target_diagnostics: Vec<CompatibilityDiagnostic>,
}

impl<'scene> FrameRenderEntry<'scene> {
    /// Returns the target frame identity.
    pub const fn frame(&self) -> FrameRef<'scene> {
        self.frame
    }

    /// Returns the complete target render result.
    pub const fn result(&self) -> &Result<RenderOutcome, RenderError> {
        &self.result
    }

    /// Returns diagnostics attributed specifically to this target.
    pub fn target_diagnostics(&self) -> &[CompatibilityDiagnostic] {
        &self.target_diagnostics
    }

    /// Consumes the entry and returns its complete target result.
    ///
    /// # Errors
    ///
    /// Returns this frame's [`RenderError`] when it could not be rendered,
    /// as [`Renderer::render_frame`](crate::Renderer::render_frame) would.
    pub fn into_result(self) -> Result<RenderOutcome, RenderError> {
        self.result
    }
}

/// Lazy source-ordered rendering of selected frames from one scene document.
pub struct FrameRenderCursor<'scene, 'request> {
    catalog: &'static crate::catalog::RevisionCatalog,
    frames: Vec<FrameRef<'scene>>,
    request: &'request FrameRenderRequest,
    prepared: Option<crate::render::PreparedFrameRender>,
    next_index: usize,
    global_diagnostics: Vec<CompatibilityDiagnostic>,
    global_diagnostics_initialized: bool,
}

impl fmt::Debug for FrameRenderCursor<'_, '_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("FrameRenderCursor")
            .field("frame_count", &self.frames.len())
            .field("next_index", &self.next_index)
            .field("prepared", &self.prepared.is_some())
            .finish_non_exhaustive()
    }
}

impl<'scene, 'request> FrameRenderCursor<'scene, 'request> {
    pub(crate) fn new(
        catalog: &'static crate::catalog::RevisionCatalog,
        scene: &'scene SceneDocument,
        frames: impl IntoIterator<Item = FrameRef<'scene>>,
        request: &'request FrameRenderRequest,
    ) -> Result<Self, FrameBatchError> {
        let mut selected = Vec::new();
        for frame in frames {
            selected
                .try_reserve(1)
                .map_err(|_| FrameBatchError::from_preparation_subject("target_selection"))?;
            selected.push(frame);
        }
        let mut frames = selected;
        for frame in &frames {
            if !frame.belongs_to(scene) {
                return Err(FrameBatchError::new(
                    FrameBatchErrorKind::ForeignTarget,
                    frame.ordinal(),
                ));
            }
        }
        frames.sort_unstable_by_key(|frame| frame.ordinal());
        if let Some(duplicate) = frames
            .windows(2)
            .find(|frames| frames[0].source_index == frames[1].source_index)
        {
            return Err(FrameBatchError::new(
                FrameBatchErrorKind::DuplicateTarget,
                duplicate[0].ordinal(),
            ));
        }
        let prepared = if frames.is_empty() {
            None
        } else {
            Some(
                crate::render::PreparedFrameRender::new(
                    catalog.frame_profile(),
                    catalog.scene_surface(),
                    scene,
                    request.as_render_request(),
                )
                .map_err(|error| FrameBatchError::preparation(&error))?,
            )
        };
        Ok(Self {
            catalog,
            frames,
            request,
            prepared,
            next_index: 0,
            global_diagnostics: Vec::new(),
            global_diagnostics_initialized: false,
        })
    }

    /// Returns scene-global diagnostics discovered by targets rendered so far.
    pub fn global_diagnostics(&self) -> &[CompatibilityDiagnostic] {
        &self.global_diagnostics
    }
}

impl<'scene> Iterator for FrameRenderCursor<'scene, '_> {
    type Item = FrameRenderEntry<'scene>;

    fn next(&mut self) -> Option<Self::Item> {
        let frame = *self.frames.get(self.next_index)?;
        self.next_index += 1;
        let (result, global_diagnostics, target_diagnostics) = crate::render::render_frame_scoped(
            self.catalog,
            frame,
            self.request,
            self.prepared.as_ref(),
        );
        if !self.global_diagnostics_initialized {
            self.global_diagnostics = global_diagnostics;
            self.global_diagnostics_initialized = true;
        }
        Some(FrameRenderEntry {
            frame,
            result,
            target_diagnostics,
        })
    }
}