rhwp 0.9.1

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Request ownership is separate from the editable document's lifetime.
use super::DocumentCore;
use crate::error::HwpError;
use crate::parser::pdf::{PdfImportError, PdfImportOptions, PdfImportPolicy, PdfImportProgress};
use pdf_oxide::import_limits::{ImportBudget, ImportBudgetSnapshot};
use std::sync::Arc;

struct OwnerGuard<F>(F);
impl<F> std::fmt::Debug for OwnerGuard<F> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("OwnerGuard")
    }
}
impl<F: Fn() -> bool + Send + Sync> pdf_oxide::import_limits::ImportWorkGuard for OwnerGuard<F> {
    fn permits_work(&self) -> bool {
        (self.0)()
    }
}

#[derive(Debug)]
struct ReopenReservation {
    active: Arc<std::sync::atomic::AtomicBool>,
    inner: Arc<dyn crate::storage::Reservation>,
}
impl crate::storage::Reservation for ReopenReservation {
    fn hash_payload(&self, element_bytes: usize, capacity: usize) -> Result<(), String> {
        if self.active.load(std::sync::atomic::Ordering::Acquire) {
            self.inner.hash_payload(element_bytes, capacity)
        } else {
            Ok(())
        }
    }
    fn reject(&self, reason: &'static str) -> String {
        if self.active.load(std::sync::atomic::Ordering::Acquire) {
            self.inner.reject(reason)
        } else {
            reason.into()
        }
    }
}
struct ReopenLease(Arc<std::sync::atomic::AtomicBool>);
impl Drop for ReopenLease {
    fn drop(&mut self) {
        self.0.store(false, std::sync::atomic::Ordering::Release);
    }
}

/// One PDF initialization request with shared parser and Core storage accounting.
///
/// The session never becomes part of the returned document. Ordinary editing
/// therefore does not consume the initial import's cumulative counters. Hosts
/// still supervise process/worker memory and execution time; these counters do
/// not measure every renderer allocation or the process heap.
pub struct PdfImportSession {
    request: Arc<ImportBudget>,
    policy: PdfImportPolicy,
    started: bool,
    failed: bool,
}

impl PdfImportSession {
    pub fn new(policy: PdfImportPolicy) -> Result<Self, HwpError> {
        Self::new_inner(policy, None)
    }

    /// The owner supplies a cooperative check, not preemption of opaque work.
    pub fn new_with_work_guard<F: Fn() -> bool + Send + Sync + 'static>(
        policy: PdfImportPolicy,
        guard: F,
    ) -> Result<Self, HwpError> {
        Self::new_inner(policy, Some(Arc::new(OwnerGuard(guard))))
    }

    fn new_inner(
        policy: PdfImportPolicy,
        guard: Option<Arc<dyn pdf_oxide::import_limits::ImportWorkGuard>>,
    ) -> Result<Self, HwpError> {
        let fail =
            |error| HwpError::InvalidFile(crate::parser::ParseError::PdfError(error).to_string());
        policy.validate().map_err(fail)?;
        let request = ImportBudget::new(policy.backend)
            .map_err(crate::parser::pdf::PdfImportError::backend)
            .map_err(fail)?;
        let request = if let Some(guard) = guard {
            request.with_work_guard(guard)
        } else {
            request
        };
        Ok(Self {
            request: Arc::new(request),
            policy,
            started: false,
            failed: false,
        })
    }

    /// Reserve a caller-owned transport copy before its backing is allocated.
    /// This counts logical payload work, not JavaScript or process live heap.
    pub fn reserve_transfer(&mut self, bytes: usize) -> Result<(), HwpError> {
        if self.failed {
            return Err(HwpError::InvalidFile(
                "PDF_IMPORT_SESSION: failed transfer request".into(),
            ));
        }
        let result = self.request.charge_decoded(bytes).map_err(|error| {
            self.storage_error(
                "transport backing",
                crate::parser::pdf::PdfImportError::backend(error),
            )
        });
        if result.is_err() {
            self.failed = true;
        }
        result
    }

    /// Reopen the generated HWPX handoff under the owner's remaining policy.
    /// Archive/decoded backing and shared Core initialization consume this
    /// request. The legacy XML parser's object allocations and process heap are
    /// not thereby certified. Lazy assets detach from the request on return.
    pub fn reopen_hwpx_export(
        &mut self,
        data: &[u8],
        maximum: usize,
    ) -> Result<DocumentCore, HwpError> {
        if self.failed || std::mem::replace(&mut self.started, true) {
            return Err(HwpError::InvalidFile(
                "PDF_IMPORT_SESSION: request already started".into(),
            ));
        }
        let output = (|| {
            if maximum == 0
                || maximum > crate::pdf_runtime::PdfTransferLimits::standard().output_bytes
                || data.len() > maximum
            {
                return Err(HwpError::InvalidFile(
                    "PDF_RESOURCE_LIMIT: editor input byte limit".into(),
                ));
            }
            self.request.check().map_err(|e| {
                self.storage_error(
                    "editor reopen",
                    crate::parser::pdf::PdfImportError::backend(e),
                )
            })?;
            if crate::parser::detect_format(data) != crate::parser::FileFormat::Hwpx {
                return Err(HwpError::InvalidFile(
                    "PDF_EDITOR_REOPEN: expected generated HWPX".into(),
                ));
            }
            self.request
                .charge_decoded(
                    std::mem::size_of::<ReopenReservation>()
                        + std::mem::size_of::<std::sync::atomic::AtomicBool>()
                        + 4 * std::mem::size_of::<usize>(),
                )
                .map_err(|e| {
                    self.storage_error(
                        "editor reopen",
                        crate::parser::pdf::PdfImportError::backend(e),
                    )
                })?;
            let active = Arc::new(std::sync::atomic::AtomicBool::new(true));
            let _lease = ReopenLease(Arc::clone(&active));
            let reservation = crate::parser::pdf::allocation::payload_reservation(&self.request)
                .map_err(|e| self.storage_error("editor input", e))?;
            let reservation: Arc<dyn crate::storage::Reservation> = Arc::new(ReopenReservation {
                active,
                inner: reservation,
            });
            let parsed = crate::parser::parse_pdf_editor_hwpx(data, reservation)
                .map_err(|e| HwpError::InvalidFile(e.to_string()))?;
            // A permissive legacy XML branch cannot hide a fatal reservation.
            self.request.check().map_err(|e| {
                self.storage_error(
                    "editor reopen",
                    crate::parser::pdf::PdfImportError::backend(e),
                )
            })?;
            let mut core = DocumentCore::from_parsed_document_with_storage(
                parsed,
                crate::parser::FileFormat::Hwpx,
                &crate::parser::pdf::allocation::Allocation(&self.request),
            )?;
            // HWPX is the worker's transfer container, not the imported source.
            // Keep its load semantics, but use PDF export adaptation thereafter:
            // HWPX-only picture-extra normalization would erase the PDF crop's
            // explicit image reference dimensions on the next HWP save (#8403).
            core.source_format = crate::parser::FileFormat::Pdf;
            self.request.check().map_err(|e| {
                self.storage_error(
                    "editor reopen",
                    crate::parser::pdf::PdfImportError::backend(e),
                )
            })?;
            Ok(core)
        })();
        if output.is_err() {
            self.failed = true;
        }
        output
    }

    /// A failed request cannot be reused. Create a fresh session for recovery.
    pub fn open(
        &mut self,
        bytes: &[u8],
        options: &PdfImportOptions,
    ) -> Result<DocumentCore, HwpError> {
        self.open_with_progress(bytes, options, &mut |_| Ok(()))
    }

    /// Reports actual selected-page conversion boundaries. Final Core creation
    /// and serialization follow the last page; these are not elapsed-time estimates.
    /// A callback error aborts and invalidates this request like a parser error.
    pub fn open_with_progress(
        &mut self,
        bytes: &[u8],
        options: &PdfImportOptions,
        progress: &mut dyn FnMut(PdfImportProgress) -> Result<(), PdfImportError>,
    ) -> Result<DocumentCore, HwpError> {
        if self.failed || std::mem::replace(&mut self.started, true) {
            return Err(HwpError::InvalidFile(
                "PDF_IMPORT_SESSION: request already started; create a fresh session".into(),
            ));
        }
        let result = self.open_inner(bytes, options, progress);
        if result.is_err() {
            self.failed = true;
        }
        result
    }

    fn open_inner(
        &self,
        bytes: &[u8],
        options: &PdfImportOptions,
        progress: &mut dyn FnMut(PdfImportProgress) -> Result<(), PdfImportError>,
    ) -> Result<DocumentCore, HwpError> {
        let parsed = crate::parser::parse_pdf_with_metadata_and_request(
            bytes,
            options,
            self.policy,
            Arc::clone(&self.request),
            progress,
        )
        .map_err(|error| HwpError::InvalidFile(error.to_string()))?;
        let mut core = DocumentCore::from_parsed_document_with_storage(
            parsed,
            crate::parser::FileFormat::Pdf,
            &crate::parser::pdf::allocation::Allocation(&self.request),
        )?;
        self.request
            .check()
            .map_err(|error| HwpError::InvalidFile(error.to_string()))?;
        if let Some(metadata) = core.pdf_metadata.as_mut() {
            metadata.resource_usage = self.resource_usage();
        }
        Ok(core)
    }

    pub(crate) fn prepare_hwp_snapshot(
        &self,
        core: &DocumentCore,
    ) -> Result<crate::document_core::commands::HwpExportSnapshot, HwpError> {
        if !self.started || self.failed {
            return Err(HwpError::InvalidFile(
                "PDF_IMPORT_SESSION: invalid or failed snapshot request".into(),
            ));
        }
        core.try_prepare_hwp_export_snapshot(&crate::parser::pdf::allocation::Allocation(
            &self.request,
        ))
        .map_err(|error| self.storage_error("HWP snapshot", error))
    }

    /// Serialize HWP using the remaining request budget and an output byte cap.
    /// Format adaptation and opaque scratch also require host memory/time supervision.
    pub fn export_hwp(
        &mut self,
        core: &mut DocumentCore,
        maximum: usize,
    ) -> Result<Vec<u8>, HwpError> {
        if !self.started || self.failed || maximum < 512 {
            self.failed = true;
            return Err(HwpError::InvalidFile(
                "PDF_IMPORT_SESSION: invalid or failed export request".into(),
            ));
        }
        let output = self.prepare_hwp_snapshot(core).and_then(|snapshot| {
            crate::serializer::cfb_writer::serialize_hwp_with_storage(
                snapshot.document(),
                &crate::parser::pdf::allocation::Allocation(&self.request),
                maximum,
            )
            .map_err(|error| HwpError::RenderError(error.to_string()))
        });
        match output {
            Ok(bytes) => {
                if let Some(metadata) = core.pdf_metadata.as_mut() {
                    metadata.resource_usage = self.resource_usage();
                }
                Ok(bytes)
            }
            Err(error) => {
                self.failed = true;
                Err(error)
            }
        }
    }

    /// Serialize HWPX with the same request and a bounded ZIP output backing.
    /// XML/context construction also requires host memory/time supervision.
    pub fn export_hwpx(
        &mut self,
        core: &mut DocumentCore,
        maximum: usize,
    ) -> Result<Vec<u8>, HwpError> {
        if !self.started || self.failed || maximum == 0 {
            self.failed = true;
            return Err(HwpError::InvalidFile(
                "PDF_IMPORT_SESSION: invalid or failed export request".into(),
            ));
        }
        let output = core.hwpx_document_for_export(|doc| {
            let reservation = crate::parser::pdf::allocation::shared_reservation(&self.request)
                .map_err(|error| {
                    crate::serializer::SerializeError::XmlError(format!("HWPX context: {error}"))
                })?;
            crate::serializer::hwpx::serialize_hwpx_with_storage(
                doc,
                &crate::parser::pdf::allocation::Allocation(&self.request),
                maximum,
                reservation,
            )
        });
        match output {
            Ok(bytes) => {
                if let Some(metadata) = core.pdf_metadata.as_mut() {
                    metadata.resource_usage = self.resource_usage();
                }
                Ok(bytes)
            }
            Err(error) => {
                self.failed = true;
                Err(error)
            }
        }
    }

    /// Encode the worker response before allocating its output backing. The
    /// metadata snapshot describes import/export before this transport encoding;
    /// `resource_usage()` also includes the diagnostic buffer reservation.
    pub fn diagnostics_json(
        &mut self,
        core: &DocumentCore,
        maximum: usize,
    ) -> Result<Vec<u8>, HwpError> {
        if !self.started || self.failed || maximum == 0 {
            self.failed = true;
            return Err(HwpError::InvalidFile(
                "PDF_IMPORT_SESSION: invalid or failed diagnostics request".into(),
            ));
        }
        #[derive(serde::Serialize)]
        #[serde(rename_all = "camelCase")]
        struct Response<'a> {
            page_count: u32,
            diagnostics: Option<&'a crate::parser::pdf::PdfImportMetadata>,
        }
        let response = Response {
            page_count: core.page_count(),
            diagnostics: core.pdf_metadata(),
        };
        let output = crate::parser::pdf::allocation::Allocation(&self.request)
            .json_exact(&response, maximum)
            .map_err(|error| self.storage_error("diagnostic encoding", error));
        if output.is_err() {
            self.failed = true;
        }
        output
    }

    pub fn resource_usage(&self) -> ImportBudgetSnapshot {
        self.request.snapshot()
    }

    /// The shared SVG serializer refuses final syntax backing before growth.
    /// Layer construction and renderer scratch still require host supervision.
    pub fn render_page_svg(
        &mut self,
        core: &DocumentCore,
        page: u32,
        maximum: usize,
    ) -> Result<String, HwpError> {
        if !self.started || self.failed || maximum == 0 {
            return Err(HwpError::InvalidFile(
                "PDF_IMPORT_SESSION: invalid SVG request".into(),
            ));
        }
        if page >= core.page_count() {
            return Err(HwpError::InvalidFile(
                "PDF_PAGE_SELECTION: output page is out of range".into(),
            ));
        }
        let output = crate::parser::pdf::allocation::payload_reservation(&self.request)
            .map_err(|error| self.storage_error("SVG backing", error))
            .and_then(|reservation| core.render_page_svg_bounded(page, maximum, reservation));
        if output.is_err() {
            self.failed = true;
        }
        output
    }

    fn storage_error(&self, stage: &str, error: crate::parser::pdf::PdfImportError) -> HwpError {
        let usage = self.resource_usage();
        let details = match &usage.failure {
            Some(failure) => format!(
                "used {:?}, requested {:?}, limit {:?}",
                failure.used, failure.requested, failure.limit,
            ),
            None => format!("decoded bytes {}", usage.decoded_bytes),
        };
        HwpError::InvalidFile(format!(
            "{} (during {stage}; {details})",
            crate::parser::ParseError::PdfError(error),
        ))
    }
}