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
603
//! Bounded, atomic UTF-16 IME deltas over one plain body paragraph.
//!
//! Context text includes the active native preview. Qt adapters translate their
//! preview-excluding offsets using `preedit`; Flutter deltas use this text as
//! their oldText. Offset conversion never clamps a surrogate or context boundary.

use std::ops::Range;

use serde::{Deserialize, Serialize};

use super::{Composition, HistoryEntry, NativeEditOutcome, NativeEditor};
use crate::document_core::native_interaction::{NativeTextPosition, NativeTextSelection};
use crate::native_host::accessibility::{native_scalar_to_utf16, native_utf16_to_scalar};
use crate::native_host::commands::{NativeImeContext, NATIVE_IME_CONTEXT_SCALARS};
use crate::native_host::NativeHostError;
use crate::DocumentCore;

pub const MAX_IME_EDIT_BYTES: usize = 16 * 1024;
pub const MAX_IME_EDIT_SCALARS: usize = NATIVE_IME_CONTEXT_SCALARS;

/// Half-open UTF-16 offsets in the context specified by the containing request.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NativeImeUtf16Range {
    pub start: u32,
    pub end: u32,
}

/// Direction-preserving UTF-16 selection; offsets refer to the resulting text.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NativeImeUtf16Selection {
    pub anchor: u32,
    pub focus: u32,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NativeImeEditContext {
    pub revision: u64,
    pub font_generation: u64,
    pub context: NativeImeContext,
    pub selection: NativeTextSelection,
    pub composition_id: Option<u64>,
    /// The entire active preview inside `context.text`, including an unchanged
    /// original selection when beginComposition has not received a preview yet.
    pub preedit: Option<NativeImeUtf16Range>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NativeImeReplacement {
    pub range: NativeImeUtf16Range,
    pub text: String,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NativeImePreedit {
    /// Range in the committed result, before this new preview is inserted.
    pub replacement: NativeImeReplacement,
    /// Selection in the final result; both ends must lie inside the new preview.
    pub selection: Option<NativeImeUtf16Selection>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "camelCase",
    rename_all_fields = "camelCase",
    deny_unknown_fields
)]
pub enum NativeImeEdit {
    /// Replace only within the active preview, preserving its original undo and
    /// cancel baseline. Selection refers to the resulting context text.
    Continue {
        replacement: NativeImeReplacement,
        selection: Option<NativeImeUtf16Selection>,
    },
    /// Commit a context replacement and any existing composition as one undo
    /// entry. `selection` is in the committed result. An optional new preedit
    /// has its own cancellation baseline after that commit.
    Commit {
        replacement: NativeImeReplacement,
        selection: Option<NativeImeUtf16Selection>,
        preedit: Option<NativeImePreedit>,
    },
}

struct Replacement {
    range: NativeTextSelection,
    scalars: Range<usize>,
    result: String,
    end: usize,
    changed: bool,
}

struct PlannedPreedit {
    range: NativeTextSelection,
    text: String,
    original: String,
    selection: NativeTextSelection,
    changed: bool,
}

impl NativeEditor {
    pub fn ime_edit_context(
        &self,
        expected_revision: u64,
    ) -> Result<NativeImeEditContext, NativeHostError> {
        self.session.check_revision(expected_revision)?;
        Self::validate_selection(&self.session.core, &self.selection)?;
        same_paragraph(&self.selection)?;
        Self::check_body_text_span(&self.session.core, &self.selection)?;
        let context = self.ime_context(expected_revision)?;
        if context.anchor_scalar.is_none() || context.selection_truncated {
            return Err(limit("IME selection is outside the bounded context"));
        }
        let preedit = if let Some(composition) = &self.composition {
            same_paragraph(&composition.replacement_range)?;
            check_text_bound(&composition.original_text)?;
            let text = composition
                .preview_text
                .as_deref()
                .unwrap_or(&composition.original_text);
            check_text_bound(text)?;
            let start = Self::ordered(&composition.replacement_range).0;
            if (start.section, start.paragraph)
                != (context.position.section, context.position.paragraph)
            {
                return Err(invalid("composition is outside the IME paragraph"));
            }
            let local = start
                .scalar_offset
                .checked_sub(context.absolute_start_scalar)
                .ok_or_else(|| limit("active preedit is outside the bounded context"))?;
            let end = local
                .checked_add(text.chars().count() as u32)
                .ok_or_else(|| limit("active preedit exceeds scalar addresses"))?;
            let range = NativeImeUtf16Range {
                start: native_scalar_to_utf16(&context.text, local)?,
                end: native_scalar_to_utf16(&context.text, end)?,
            };
            let scalars = scalar_range(&context.text, range)?;
            if scalar_text(&context.text, scalars) != text {
                return Err(invalid("active preview and IME context disagree"));
            }
            Some(range)
        } else {
            None
        };
        Ok(NativeImeEditContext {
            revision: self.session.revision(),
            font_generation: self.session.font_generation(),
            context,
            selection: self.selection.clone(),
            composition_id: self.composition_id(),
            preedit,
        })
    }

    pub fn apply_ime_edit(
        &mut self,
        expected_revision: u64,
        expected: &NativeImeEditContext,
        edit: NativeImeEdit,
    ) -> Result<NativeEditOutcome, NativeHostError> {
        self.session.check_revision(expected_revision)?;
        // Reject large untrusted strings before counting scalars or comparing a
        // caller's entire context. The exact current snapshot remains authority.
        check_text_bound(&expected.context.text)?;
        check_text_bound(&expected.context.selected_text)?;
        Self::validate_selection(&self.session.core, &expected.selection)?;
        if expected != &self.ime_edit_context(expected_revision)? {
            return Err(invalid(
                "stale IME context, selection or composition identity",
            ));
        }
        match edit {
            NativeImeEdit::Continue {
                replacement,
                selection,
            } => self.continue_ime_edit(expected, replacement, selection),
            NativeImeEdit::Commit {
                replacement,
                selection,
                preedit,
            } => self.commit_ime_edit(expected, replacement, selection, preedit),
        }
    }

    fn continue_ime_edit(
        &mut self,
        expected: &NativeImeEditContext,
        replacement: NativeImeReplacement,
        selection: Option<NativeImeUtf16Selection>,
    ) -> Result<NativeEditOutcome, NativeHostError> {
        check_input(&replacement.text, None)?;
        let preview_range = expected
            .preedit
            .ok_or_else(|| invalid("no active IME composition"))?;
        let preview_scalars = scalar_range(&expected.context.text, preview_range)?;
        let replacement = plan_replacement(expected, &expected.context.text, &replacement)?;
        if replacement.scalars.start < preview_scalars.start
            || replacement.scalars.end > preview_scalars.end
        {
            return Err(invalid(
                "continuing IME delta must stay inside its active preedit",
            ));
        }
        let removed = replacement.scalars.end - replacement.scalars.start;
        let inserted = replacement.end - replacement.scalars.start;
        let preview_end = preview_scalars.end - removed + inserted;
        let preview = scalar_text(&replacement.result, preview_scalars.start..preview_end);
        let selected = plan_selection(expected, &replacement.result, selection, preview_end)?;
        check_preview_selection(expected, &selected, preview_scalars.start..preview_end)?;
        self.check_next_ime_window(
            expected,
            &replacement.result,
            &selected,
            Some(preview_scalars.start..preview_end),
        )?;
        let composition = self
            .composition
            .as_ref()
            .expect("validated composition context");
        let baseline = composition.before;
        let range = composition.replacement_range.clone();
        let changed = preview != composition.original_text;
        let mutation = replacement.changed;
        let (_, rollback) = self.ime_transaction(usize::from(mutation), mutation, |core, _| {
            if mutation {
                core.restore_snapshot_native(baseline)?;
                if changed {
                    Self::apply_replacement(core, &range, &preview)?;
                }
            }
            Ok(())
        })?;
        if let Some(rollback) = rollback {
            self.session.core.discard_snapshot_native(rollback);
        }
        self.selection = selected;
        let composition = self
            .composition
            .as_mut()
            .expect("validated composition context");
        composition.preview_text = Some(preview);
        composition.changed = changed;
        Ok(self.outcome(mutation, false))
    }

    fn commit_ime_edit(
        &mut self,
        expected: &NativeImeEditContext,
        replacement: NativeImeReplacement,
        selection: Option<NativeImeUtf16Selection>,
        preedit: Option<NativeImePreedit>,
    ) -> Result<NativeEditOutcome, NativeHostError> {
        check_input(
            &replacement.text,
            preedit
                .as_ref()
                .map(|preview| preview.replacement.text.as_str()),
        )?;
        let committed = plan_replacement(expected, &expected.context.text, &replacement)?;
        let committed_selection =
            plan_selection(expected, &committed.result, selection, committed.end)?;
        self.check_next_ime_window(expected, &committed.result, &committed_selection, None)?;
        let baseline = if let Some(composition) = &self.composition {
            let range = scalar_range(
                &expected.context.text,
                expected.preedit.expect("composition context"),
            )?;
            replace_scalars(&expected.context.text, range, &composition.original_text)
        } else {
            expected.context.text.clone()
        };
        let history_changed = committed.result != baseline;
        let restore_baseline =
            !history_changed && self.composition.as_ref().is_some_and(|value| value.changed);
        let preview = preedit
            .map(|preview| {
                let replacement =
                    plan_replacement(expected, &committed.result, &preview.replacement)?;
                // The next context query validates this cancellation baseline
                // too. A short preview can replace a span larger than either
                // bounded input after the commit has expanded the context.
                let original = scalar_text(&committed.result, replacement.scalars.clone());
                check_text_bound(&original)?;
                let selected = plan_selection(
                    expected,
                    &replacement.result,
                    preview.selection,
                    replacement.end,
                )?;
                let span = replacement.scalars.start..replacement.end;
                check_preview_selection(expected, &selected, span.clone())?;
                self.check_next_ime_window(expected, &replacement.result, &selected, Some(span))?;
                Ok::<_, NativeHostError>(PlannedPreedit {
                    range: replacement.range,
                    original,
                    text: preview.replacement.text,
                    selection: selected,
                    changed: replacement.changed,
                })
            })
            .transpose()?;
        let next_id = if preview.is_some() {
            Some(
                self.next_composition_id
                    .checked_add(1)
                    .ok_or_else(|| limit("composition identifiers exhausted"))?,
            )
        } else {
            None
        };
        let mutation = committed.changed
            || restore_baseline
            || preview.as_ref().is_some_and(|value| value.changed);
        let slots =
            usize::from(mutation) + usize::from(history_changed) + usize::from(preview.is_some());
        let old_before = self.composition.as_ref().map(|value| value.before);
        let undo_selection = self
            .composition
            .as_ref()
            .map(|value| value.selection_before.clone())
            .unwrap_or_else(|| self.selection.clone());
        let ((after, preview_before), rollback) =
            self.ime_transaction(slots, mutation, |core, allocated| {
                if restore_baseline {
                    core.restore_snapshot_native(old_before.expect("restoring composition"))?;
                } else if committed.changed {
                    Self::apply_replacement(core, &committed.range, &replacement.text)?;
                }
                let after = history_changed.then(|| retain_snapshot(core, allocated));
                let preview_before = preview.as_ref().map(|_| retain_snapshot(core, allocated));
                if let Some(preview) = &preview {
                    if preview.changed {
                        Self::apply_replacement(core, &preview.range, &preview.text)?;
                    }
                }
                Ok((after, preview_before))
            })?;
        // Every fallible operation precedes publication of editor-owned state.
        let old = self.composition.take();
        if history_changed {
            self.record(HistoryEntry {
                before: old_before.or(rollback).expect("changing action baseline"),
                after: after.expect("changing action snapshot"),
                selection_before: undo_selection,
                selection_after: committed_selection.clone(),
            });
        } else if let Some(old) = &old {
            self.session.core.discard_snapshot_native(old.before);
        }
        if let Some(rollback) = rollback {
            if old_before.is_some() || !history_changed {
                self.session.core.discard_snapshot_native(rollback);
            }
        }
        self.selection = committed_selection.clone();
        if let Some(preview) = preview {
            self.composition = Some(Composition {
                id: self.next_composition_id,
                before: preview_before.expect("new composition baseline"),
                selection_before: committed_selection,
                replacement_range: preview.range,
                original_text: preview.original,
                preview_text: Some(preview.text),
                changed: preview.changed,
            });
            self.next_composition_id = next_id.expect("new composition identifier");
            self.selection = preview.selection;
        }
        Ok(self.outcome(mutation, history_changed))
    }

    fn check_next_ime_window(
        &self,
        expected: &NativeImeEditContext,
        result: &str,
        selection: &NativeTextSelection,
        preview: Option<Range<usize>>,
    ) -> Result<(), NativeHostError> {
        let position = &expected.context.position;
        let paragraph = &self.session.core.document().sections[position.section as usize]
            .paragraphs[position.paragraph as usize]
            .text;
        let length = paragraph
            .chars()
            .count()
            .checked_sub(expected.context.text.chars().count())
            .and_then(|value| value.checked_add(result.chars().count()))
            .ok_or_else(|| limit("IME paragraph length overflow"))?;
        if u32::try_from(length).is_err() {
            return Err(limit("IME paragraph exceeds native scalar addresses"));
        }
        let start =
            (selection.focus.scalar_offset as usize).saturating_sub(NATIVE_IME_CONTEXT_SCALARS / 2);
        let end = length.min(start.saturating_add(NATIVE_IME_CONTEXT_SCALARS));
        let contains = |offset: u32| (start..=end).contains(&(offset as usize));
        if !contains(selection.anchor.scalar_offset) || !contains(selection.focus.scalar_offset) {
            return Err(limit(
                "resulting IME selection exceeds the next bounded context",
            ));
        }
        if let Some(preview) = preview {
            if !contains(native_position(expected, preview.start)?.scalar_offset)
                || !contains(native_position(expected, preview.end)?.scalar_offset)
            {
                return Err(limit("resulting preedit exceeds the next bounded context"));
            }
        }
        Ok(())
    }

    /// The core event log remains append-only, matching edit_transaction. Only
    /// new snapshots are discarded on error; old composition/history/save state
    /// is untouched, and the native revision is advanced once after success.
    fn ime_transaction<T>(
        &mut self,
        snapshot_slots: usize,
        mutation: bool,
        edit: impl FnOnce(&mut DocumentCore, &mut Vec<u32>) -> Result<T, NativeHostError>,
    ) -> Result<(T, Option<u32>), NativeHostError> {
        self.check_snapshot_slots(snapshot_slots)?;
        if mutation {
            self.check_mutation_capacity()?;
        }
        let rollback = mutation.then(|| self.session.core.save_snapshot_native());
        let mut allocated = Vec::new();
        let result = edit(&mut self.session.core, &mut allocated).and_then(|result| {
            if mutation {
                self.session.did_mutate()?;
            }
            Ok(result)
        });
        match result {
            Ok(result) => Ok((result, rollback)),
            Err(error) => {
                for snapshot in allocated {
                    self.session.core.discard_snapshot_native(snapshot);
                }
                Err(match rollback {
                    Some(snapshot) => self.rollback_and_discard(snapshot, error),
                    None => error,
                })
            }
        }
    }
}

fn retain_snapshot(core: &mut DocumentCore, allocated: &mut Vec<u32>) -> u32 {
    let id = core.save_snapshot_native();
    allocated.push(id);
    id
}

fn same_paragraph(selection: &NativeTextSelection) -> Result<(), NativeHostError> {
    if selection.anchor.section != selection.focus.section
        || selection.anchor.paragraph != selection.focus.paragraph
        || !selection.anchor.cell_path.is_empty()
        || !selection.focus.cell_path.is_empty()
    {
        return Err(NativeHostError::Unsupported(
            "IME deltas require one plain body paragraph".into(),
        ));
    }
    Ok(())
}

fn check_text_bound(text: &str) -> Result<(), NativeHostError> {
    if text.len() > MAX_IME_EDIT_BYTES || text.chars().count() > MAX_IME_EDIT_SCALARS {
        return Err(limit("IME text exceeds the bounded input limit"));
    }
    Ok(())
}

fn check_input(text: &str, preview: Option<&str>) -> Result<(), NativeHostError> {
    if text.len().saturating_add(preview.map_or(0, str::len)) > MAX_IME_EDIT_BYTES {
        return Err(limit(
            "combined IME commit and preedit exceed the input limit",
        ));
    }
    check_text_bound(text)?;
    NativeEditor::validate_text(text)?;
    if let Some(preview) = preview {
        check_text_bound(preview)?;
        NativeEditor::validate_text(preview)?;
        if text.len() + preview.len() > MAX_IME_EDIT_BYTES
            || text.chars().count() + preview.chars().count() > MAX_IME_EDIT_SCALARS
        {
            return Err(limit(
                "combined IME commit and preedit exceed the input limit",
            ));
        }
    }
    Ok(())
}

fn scalar_range(text: &str, range: NativeImeUtf16Range) -> Result<Range<usize>, NativeHostError> {
    if range.start > range.end {
        return Err(invalid("IME replacement range is reversed"));
    }
    Ok(native_utf16_to_scalar(text, range.start)? as usize
        ..native_utf16_to_scalar(text, range.end)? as usize)
}

fn scalar_text(text: &str, range: Range<usize>) -> String {
    text.chars()
        .skip(range.start)
        .take(range.end - range.start)
        .collect()
}

fn replace_scalars(text: &str, range: Range<usize>, replacement: &str) -> String {
    let mut result: String = text.chars().take(range.start).collect();
    result.push_str(replacement);
    result.extend(text.chars().skip(range.end));
    result
}

fn native_position(
    expected: &NativeImeEditContext,
    local: usize,
) -> Result<NativeTextPosition, NativeHostError> {
    let mut position = expected.context.position.clone();
    position.scalar_offset = expected
        .context
        .absolute_start_scalar
        .checked_add(u32::try_from(local).map_err(|_| limit("IME scalar position overflow"))?)
        .ok_or_else(|| limit("IME scalar position overflow"))?;
    Ok(position)
}

fn plan_replacement(
    expected: &NativeImeEditContext,
    context: &str,
    replacement: &NativeImeReplacement,
) -> Result<Replacement, NativeHostError> {
    let scalars = scalar_range(context, replacement.range)?;
    let changed = scalar_text(context, scalars.clone()) != replacement.text;
    let result = replace_scalars(context, scalars.clone(), &replacement.text);
    let end = scalars.start + replacement.text.chars().count();
    Ok(Replacement {
        range: NativeTextSelection {
            anchor: native_position(expected, scalars.start)?,
            focus: native_position(expected, scalars.end)?,
        },
        scalars,
        result,
        end,
        changed,
    })
}

fn plan_selection(
    expected: &NativeImeEditContext,
    result: &str,
    selection: Option<NativeImeUtf16Selection>,
    default: usize,
) -> Result<NativeTextSelection, NativeHostError> {
    let (anchor, focus) = if let Some(selection) = selection {
        (
            native_utf16_to_scalar(result, selection.anchor)? as usize,
            native_utf16_to_scalar(result, selection.focus)? as usize,
        )
    } else {
        (default, default)
    };
    Ok(NativeTextSelection {
        anchor: native_position(expected, anchor)?,
        focus: native_position(expected, focus)?,
    })
}

fn check_preview_selection(
    expected: &NativeImeEditContext,
    selection: &NativeTextSelection,
    preview: Range<usize>,
) -> Result<(), NativeHostError> {
    let start = native_position(expected, preview.start)?.scalar_offset;
    let end = native_position(expected, preview.end)?.scalar_offset;
    if [
        selection.anchor.scalar_offset,
        selection.focus.scalar_offset,
    ]
    .into_iter()
    .any(|value| value < start || value > end)
    {
        return Err(invalid("IME selection must remain inside its preedit"));
    }
    Ok(())
}

fn invalid(message: &str) -> NativeHostError {
    NativeHostError::InvalidArgument(message.into())
}

fn limit(message: &str) -> NativeHostError {
    NativeHostError::LimitExceeded(message.into())
}