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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//! Typed interaction queries over the existing layout and cursor implementation.
//!
//! Positions use Unicode scalar offsets, never UTF-16 code units or logical
//! inline-object slots. Body text and nested table cells are supported. A target
//! paragraph containing logical inline controls or character overlaps is rejected:
//! the legacy cursor contract mixes text and object slots, and this facade has no
//! object-side affinity. Text boxes, captions, notes and header/footer editing are
//! also outside this initial contract. Queries do not change the document/caret.

use super::DocumentCore;
use crate::error::HwpError;
use crate::model::{control::Control, paragraph::Paragraph};
use serde::{Deserialize, Serialize};
use std::fmt;

/// Maximum nested table address depth accepted by the native facade.
pub const NATIVE_CELL_PATH_MAX_DEPTH: usize = 32;

/// One table/cell/paragraph descent, relative to the preceding paragraph.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NativeCellPathEntry {
    pub control: u32,
    pub cell: u32,
    pub paragraph: u32,
}

/// `paragraph` always identifies a top-level body paragraph. For a nonempty
/// `cell_path`, its last entry identifies the leaf paragraph being addressed.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NativeTextPosition {
    pub section: u32,
    pub paragraph: u32,
    pub scalar_offset: u32,
    pub cell_path: Vec<NativeCellPathEntry>,
}

/// Anchor/focus order is preserved; geometry normalizes their document order.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NativeTextSelection {
    pub anchor: NativeTextPosition,
    pub focus: NativeTextPosition,
}

#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NativeInteractionBounds {
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
}

/// Page-local layout coordinates, in the same units as the core page tree.
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NativeCaretRect {
    pub page: u32,
    pub x: f64,
    pub y: f64,
    pub height: f64,
    pub cell_bounds: Option<NativeInteractionBounds>,
    pub cell_overflowed: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NativeSelectionRect {
    pub page: u32,
    pub bounds: NativeInteractionBounds,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NativeHitTest {
    pub position: NativeTextPosition,
    /// None when the existing hit-test returned a position without geometry.
    pub caret: Option<NativeCaretRect>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum NativeVerticalDirection {
    Up,
    Down,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NativeVerticalMove {
    pub position: NativeTextPosition,
    /// None represents the core's explicit `rectValid:false`, not a fake rect.
    pub caret: Option<NativeCaretRect>,
    pub preferred_x: f64,
}

#[derive(Debug)]
pub enum NativeInteractionError {
    InvalidInput(&'static str),
    InvalidPosition(String),
    Unsupported(&'static str),
    InvalidCoreResult(String),
    Core(HwpError),
}

impl fmt::Display for NativeInteractionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidInput(message) | Self::Unsupported(message) => f.write_str(message),
            Self::InvalidPosition(message) | Self::InvalidCoreResult(message) => {
                f.write_str(message)
            }
            Self::Core(error) => write!(f, "{error}"),
        }
    }
}

impl std::error::Error for NativeInteractionError {}

impl From<HwpError> for NativeInteractionError {
    fn from(error: HwpError) -> Self {
        Self::Core(error)
    }
}

impl DocumentCore {
    /// Validate a body/table address and its Unicode scalar boundary. This does
    /// not imply that its paragraph is supported for geometry or editing.
    pub fn validate_native_text_position(
        &self,
        position: &NativeTextPosition,
    ) -> Result<(), NativeInteractionError> {
        self.native_text_paragraph(position).map(|_| ())
    }

    /// Check the scalar-only interaction subset, also usable by native editors.
    pub fn check_native_interaction_support(
        &self,
        position: &NativeTextPosition,
    ) -> Result<(), NativeInteractionError> {
        let paragraph = self.native_text_paragraph(position)?;
        if paragraph.controls.iter().any(Control::is_logical_inline) {
            return Err(NativeInteractionError::Unsupported(
                "inline-object affinity is not supported by scalar interaction positions",
            ));
        }
        Ok(())
    }

    pub fn native_caret_rect(
        &self,
        position: &NativeTextPosition,
    ) -> Result<NativeCaretRect, NativeInteractionError> {
        self.check_native_interaction_support(position)?;
        let json = if position.cell_path.is_empty() {
            self.get_cursor_rect_native(
                position.section as usize,
                position.paragraph as usize,
                position.scalar_offset as usize,
            )?
        } else {
            self.get_cursor_rect_by_path_native(
                position.section as usize,
                position.paragraph as usize,
                &legacy_path_json(&position.cell_path),
                position.scalar_offset as usize,
            )?
        };
        self.native_decode_caret(decode(&json)?)
    }

    pub fn native_hit_test(
        &self,
        page: u32,
        x: f64,
        y: f64,
    ) -> Result<NativeHitTest, NativeInteractionError> {
        self.native_validate_page(page)?;
        if !x.is_finite() || !y.is_finite() {
            return Err(NativeInteractionError::InvalidInput(
                "hit coordinates must be finite",
            ));
        }
        let hit: LegacyHit = decode(&self.hit_test_native(page, x, y)?)?;
        Ok(NativeHitTest {
            position: self.native_decode_position(hit.position)?,
            caret: hit
                .caret
                .map(|rect| self.native_decode_caret(rect))
                .transpose()?,
        })
    }

    /// Selections must remain within one body section or one table-cell
    /// container. Different paragraphs in that container are supported.
    pub fn native_selection_rects(
        &self,
        selection: &NativeTextSelection,
    ) -> Result<Vec<NativeSelectionRect>, NativeInteractionError> {
        let mut start = &selection.anchor;
        let mut end = &selection.focus;
        self.check_native_interaction_support(start)?;
        self.check_native_interaction_support(end)?;
        if !same_container(start, end) {
            return Err(NativeInteractionError::Unsupported(
                "selection across sections or cell containers is not supported",
            ));
        }
        if position_order(start) > position_order(end) {
            std::mem::swap(&mut start, &mut end);
        }
        // Intermediate paragraphs also need scalar-only geometry. Checking only
        // the endpoints would silently reinterpret inline slots in the middle.
        for paragraph in position_order(start).0..=position_order(end).0 {
            let mut position = start.clone();
            position.scalar_offset = 0;
            if let Some(leaf) = position.cell_path.last_mut() {
                leaf.paragraph = paragraph;
            } else {
                position.paragraph = paragraph;
            }
            self.check_native_interaction_support(&position)?;
        }
        if start == end {
            return Ok(Vec::new());
        }
        let json = if start.cell_path.is_empty() {
            self.get_selection_rects_native(
                start.section as usize,
                start.paragraph as usize,
                start.scalar_offset as usize,
                end.paragraph as usize,
                end.scalar_offset as usize,
                None,
                None,
            )?
        } else {
            self.get_selection_rects_in_cell_by_path_native(
                start.section as usize,
                start.paragraph as usize,
                &legacy_path_json(&start.cell_path),
                position_order(start).0 as usize,
                start.scalar_offset as usize,
                position_order(end).0 as usize,
                end.scalar_offset as usize,
                None,
            )?
        };
        let rects: Vec<LegacySelectionRect> = decode(&json)?;
        rects
            .into_iter()
            .map(|rect| {
                self.native_validate_page(rect.page_index)?;
                let bounds = NativeInteractionBounds {
                    x: rect.x,
                    y: rect.y,
                    width: rect.width,
                    height: rect.height,
                };
                validate_bounds(bounds)?;
                Ok(NativeSelectionRect {
                    page: rect.page_index,
                    bounds,
                })
            })
            .collect()
    }

    /// Move one visual line through the existing navigation implementation.
    /// Nested cells preserve the complete path. Their existing table-boundary
    /// behavior is retained: navigation can stay at the boundary rather than exit.
    pub fn native_move_vertical(
        &self,
        position: &NativeTextPosition,
        direction: NativeVerticalDirection,
        preferred_x: Option<f64>,
    ) -> Result<NativeVerticalMove, NativeInteractionError> {
        self.check_native_interaction_support(position)?;
        if preferred_x.is_some_and(|x| !x.is_finite() || x < 0.0) {
            return Err(NativeInteractionError::InvalidInput(
                "preferred x must be finite and nonnegative",
            ));
        }
        let delta = match direction {
            NativeVerticalDirection::Up => -1,
            NativeVerticalDirection::Down => 1,
        };
        let preferred_x = preferred_x.unwrap_or(-1.0);
        let json = if position.cell_path.is_empty() {
            self.move_vertical_native(
                position.section as usize,
                position.paragraph as usize,
                position.scalar_offset as usize,
                delta,
                preferred_x,
                None,
            )?
        } else {
            self.move_vertical_by_path_native(
                position.section as usize,
                position.paragraph as usize,
                &legacy_path_json(&position.cell_path),
                position.scalar_offset as usize,
                delta,
                preferred_x,
            )?
        };
        let movement: LegacyMove = decode(&json)?;
        if !movement.preferred_x.is_finite() || movement.preferred_x < 0.0 {
            return Err(invalid_result("navigation returned an invalid preferred x"));
        }
        Ok(NativeVerticalMove {
            position: self.native_decode_position(movement.position)?,
            caret: if movement.rect_valid {
                Some(self.native_decode_caret(movement.caret)?)
            } else {
                None
            },
            preferred_x: movement.preferred_x,
        })
    }

    fn native_text_paragraph<'a>(
        &'a self,
        position: &NativeTextPosition,
    ) -> Result<&'a Paragraph, NativeInteractionError> {
        if position.cell_path.len() > NATIVE_CELL_PATH_MAX_DEPTH {
            return Err(NativeInteractionError::InvalidInput(
                "cell path exceeds depth limit",
            ));
        }
        let invalid = || {
            NativeInteractionError::InvalidPosition("text position is outside the document".into())
        };
        let mut paragraph = self
            .document
            .sections
            .get(position.section as usize)
            .and_then(|section| section.paragraphs.get(position.paragraph as usize))
            .ok_or_else(invalid)?;
        for entry in &position.cell_path {
            let control = paragraph
                .controls
                .get(entry.control as usize)
                .ok_or_else(invalid)?;
            let Control::Table(table) = control else {
                return Err(NativeInteractionError::Unsupported(
                    "only table-cell paths are supported",
                ));
            };
            paragraph = table
                .cells
                .get(entry.cell as usize)
                .and_then(|cell| cell.paragraphs.get(entry.paragraph as usize))
                .ok_or_else(invalid)?;
        }
        if position.scalar_offset as usize > paragraph.text.chars().count() {
            return Err(invalid());
        }
        Ok(paragraph)
    }

    fn native_validate_page(&self, page: u32) -> Result<(), NativeInteractionError> {
        if page >= self.page_count() {
            return Err(NativeInteractionError::InvalidInput(
                "page index is outside the document",
            ));
        }
        Ok(())
    }

    fn native_decode_position(
        &self,
        legacy: LegacyPosition,
    ) -> Result<NativeTextPosition, NativeInteractionError> {
        if legacy.is_text_box || legacy.inner_control_idx.is_some() {
            return Err(NativeInteractionError::Unsupported(
                "object/text-box hits are not supported",
            ));
        }
        let cell_path = match (
            legacy.parent_para_index,
            legacy.control_index,
            legacy.cell_index,
            legacy.cell_para_index,
        ) {
            (None, None, None, None) if legacy.cell_path.is_none() => Vec::new(),
            (Some(_), Some(control), Some(cell), Some(paragraph)) => {
                let path = legacy.cell_path.unwrap_or_else(|| {
                    vec![LegacyPathEntry {
                        control_index: control,
                        cell_index: cell,
                        cell_para_index: paragraph,
                    }]
                });
                let Some(first) = path.first() else {
                    return Err(invalid_result("empty cell path in core position"));
                };
                if first.control_index != control
                    || first.cell_index != cell
                    || first.cell_para_index != paragraph
                {
                    return Err(invalid_result("inconsistent cell path in core position"));
                }
                path.into_iter()
                    .map(|entry| NativeCellPathEntry {
                        control: entry.control_index,
                        cell: entry.cell_index,
                        paragraph: entry.cell_para_index,
                    })
                    .collect()
            }
            _ => return Err(invalid_result("incomplete cell context in core position")),
        };
        let position = NativeTextPosition {
            section: legacy.section_index,
            paragraph: legacy.parent_para_index.unwrap_or(legacy.paragraph_index),
            scalar_offset: legacy.char_offset,
            cell_path,
        };
        self.check_native_interaction_support(&position)?;
        Ok(position)
    }

    fn native_decode_caret(
        &self,
        legacy: LegacyCaret,
    ) -> Result<NativeCaretRect, NativeInteractionError> {
        self.native_validate_page(legacy.page_index)?;
        if !legacy.x.is_finite()
            || !legacy.y.is_finite()
            || !legacy.height.is_finite()
            || legacy.height <= 0.0
        {
            return Err(invalid_result("core returned invalid caret geometry"));
        }
        let cell_bounds = legacy
            .cell_bounds
            .map(|bounds| -> Result<_, NativeInteractionError> {
                let bounds = NativeInteractionBounds {
                    x: bounds.x,
                    y: bounds.y,
                    width: bounds.w,
                    height: bounds.h,
                };
                validate_bounds(bounds)?;
                Ok(bounds)
            })
            .transpose()?;
        Ok(NativeCaretRect {
            page: legacy.page_index,
            x: legacy.x,
            y: legacy.y,
            height: legacy.height,
            cell_bounds,
            cell_overflowed: legacy.cell_overflowed,
        })
    }
}

fn position_order(position: &NativeTextPosition) -> (u32, u32) {
    (
        position
            .cell_path
            .last()
            .map_or(position.paragraph, |entry| entry.paragraph),
        position.scalar_offset,
    )
}

fn same_container(a: &NativeTextPosition, b: &NativeTextPosition) -> bool {
    a.section == b.section
        && match (a.cell_path.split_last(), b.cell_path.split_last()) {
            (None, None) => true,
            (Some((a_leaf, a_prefix)), Some((b_leaf, b_prefix))) => {
                a.paragraph == b.paragraph
                    && a_prefix == b_prefix
                    && a_leaf.control == b_leaf.control
                    && a_leaf.cell == b_leaf.cell
            }
            _ => false,
        }
}

fn legacy_path_json(path: &[NativeCellPathEntry]) -> String {
    DocumentCore::format_path_json(
        &path
            .iter()
            .map(|entry| {
                (
                    entry.control as usize,
                    entry.cell as usize,
                    entry.paragraph as usize,
                )
            })
            .collect::<Vec<_>>(),
    )
}

fn validate_bounds(bounds: NativeInteractionBounds) -> Result<(), NativeInteractionError> {
    if ![bounds.x, bounds.y, bounds.width, bounds.height]
        .into_iter()
        .all(f64::is_finite)
        || bounds.width < 0.0
        || bounds.height < 0.0
    {
        return Err(invalid_result("core returned invalid rectangle geometry"));
    }
    Ok(())
}

fn invalid_result(message: &str) -> NativeInteractionError {
    NativeInteractionError::InvalidCoreResult(message.into())
}

fn decode<T: serde::de::DeserializeOwned>(json: &str) -> Result<T, NativeInteractionError> {
    serde_json::from_str(json)
        .map_err(|error| invalid_result(&format!("invalid core interaction result: {error}")))
}

// Required position/geometry fields have no defaults. Optional legacy metadata
// is deliberately tolerated; it is not used to invent missing coordinates.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct LegacyPosition {
    section_index: u32,
    paragraph_index: u32,
    char_offset: u32,
    parent_para_index: Option<u32>,
    control_index: Option<u32>,
    cell_index: Option<u32>,
    cell_para_index: Option<u32>,
    cell_path: Option<Vec<LegacyPathEntry>>,
    #[serde(default)]
    is_text_box: bool,
    inner_control_idx: Option<u32>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct LegacyPathEntry {
    control_index: u32,
    cell_index: u32,
    cell_para_index: u32,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct LegacyHit {
    #[serde(flatten)]
    position: LegacyPosition,
    #[serde(rename = "cursorRect")]
    caret: Option<LegacyCaret>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct LegacyCaret {
    page_index: u32,
    x: f64,
    y: f64,
    height: f64,
    cell_bounds: Option<LegacyBounds>,
    #[serde(default)]
    cell_overflowed: bool,
}

#[derive(Deserialize)]
struct LegacyBounds {
    x: f64,
    y: f64,
    w: f64,
    h: f64,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct LegacyMove {
    #[serde(flatten)]
    position: LegacyPosition,
    #[serde(flatten)]
    caret: LegacyCaret,
    preferred_x: f64,
    #[serde(default = "true_value")]
    rect_valid: bool,
}

fn true_value() -> bool {
    true
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct LegacySelectionRect {
    page_index: u32,
    x: f64,
    y: f64,
    width: f64,
    height: f64,
}