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
//! Opt-in schema-v2 envelopes over the canonical schema-v1 paint projection.
//!
//! Reusing the v1 writer preserves source binding, explicit visual mirrors,
//! resource keys and output options. The public/default writer is unchanged.

use std::collections::{BTreeMap, BTreeSet};

use serde_json::{json, Value};

use crate::paint::{
    LayerNodeKind, PageLayerTree, PaintOp, TextV2CompatibilityProfile, TextV2Diagnostics,
    TextV2ValidationIssue, TextV2ValidationSeverity, TextVariantKind,
};
use crate::renderer::layer_renderer::{
    analyze_text_variant_selection, TextVariantSelectionOptions, TextVariantSelectionReport,
    VariantSelectionBackend,
};
use crate::schema_registry::TEXT_IR_V2_ENVELOPE_SCHEMA_VERSION;

impl PageLayerTree {
    /// One text envelope per anchored paint slot, including its TextRun fallback.
    pub fn to_json_v2_compat(&self) -> Result<String, Vec<TextV2ValidationIssue>> {
        self.write_text_v2_export(None)
    }

    /// Fallback-free outline envelopes. Every text slot must have a complete,
    /// resource-proven outline variant accepted by the shared replay contract.
    pub fn to_json_v2_strict_glyph_outline(&self) -> Result<String, Vec<TextV2ValidationIssue>> {
        self.write_text_v2_export(Some(TextVariantKind::GlyphOutline))
    }

    /// Fallback-free exact-font glyph envelopes. Unsupported font, geometry or
    /// style contracts fail the whole export instead of silently omitting text.
    pub fn to_json_v2_strict_glyph_run(&self) -> Result<String, Vec<TextV2ValidationIssue>> {
        self.write_text_v2_export(Some(TextVariantKind::GlyphRun))
    }

    fn write_text_v2_export(
        &self,
        strict_kind: Option<TextVariantKind>,
    ) -> Result<String, Vec<TextV2ValidationIssue>> {
        // Public PaintOp construction can violate metadata invariants. Reject
        // that before the shared selector reaches its internal exhaustive kind
        // match (which assumes validated producer output).
        let mut nodes = vec![&self.root];
        while let Some(node) = nodes.pop() {
            match &node.kind {
                LayerNodeKind::Group { children, .. } => nodes.extend(children),
                LayerNodeKind::ClipRect { child, .. } | LayerNodeKind::ClipPath { child, .. } => {
                    nodes.push(child)
                }
                LayerNodeKind::Leaf { ops } => {
                    for op in ops {
                        let (variant, expected) = match op {
                            PaintOp::GlyphRun { run, .. } => {
                                (&run.variant, TextVariantKind::GlyphRun)
                            }
                            PaintOp::GlyphOutline { outline, .. } => {
                                (&outline.variant, TextVariantKind::GlyphOutline)
                            }
                            _ => continue,
                        };
                        if variant.variant_kind != expected
                            || variant.is_default_fallback
                            || variant.variant_id.is_empty()
                            || variant.equivalence_group.is_empty()
                        {
                            return Err(vec![issue(
                                "variantKindMismatch",
                                Some(variant.equivalence_group.clone()),
                                "root",
                                "glyph sidecar metadata does not describe its payload kind"
                                    .to_string(),
                            )]);
                        }
                    }
                }
            }
        }
        let diagnostics = TextV2Diagnostics::from_layer_tree_with_profile(
            self,
            TextV2CompatibilityProfile::V2Compat,
        );
        if diagnostics.has_errors() {
            return Err(diagnostics.validation_issues);
        }
        // Selection uses the same portable resource/geometry guards as browser
        // replay, not the optimistic diagnostics-only availability bit. This
        // includes bounded TTC/variable/vertical capabilities when implemented.
        let reports = strict_kind
            .map(|kind| {
                analyze_text_variant_selection(
                    self,
                    TextVariantSelectionOptions {
                        backend: VariantSelectionBackend::CanvasKitBrowser,
                        prefer_strict_outline: kind == TextVariantKind::GlyphOutline,
                        allow_colrv0_color_layers: true,
                        allow_colrv1_stage1_color_graph: true,
                        allow_bitmap_glyph: true,
                        allow_svg_glyph: true,
                        ..TextVariantSelectionOptions::canvaskit()
                    },
                )
            })
            .unwrap_or_default();
        let reports = reports
            .into_iter()
            .map(|report| (report.equivalence_group.clone(), report))
            .collect();
        let mut value: Value = serde_json::from_str(&self.to_json()).map_err(|error| {
            vec![issue(
                "invalidPaintSerialization",
                None,
                "root",
                format!("canonical paint JSON is not finite/valid: {error}"),
            )]
        })?;
        let mut issues = Vec::new();
        let mut used_variants = BTreeSet::new();
        let mut required_features = BTreeSet::from([
            "text.variants".to_string(),
            "text.paintOrderSlot".to_string(),
        ]);
        if value["requiredFeatures"]
            .as_array()
            .is_some_and(|v| v.iter().any(|v| v == "paint.textClipPath"))
        {
            required_features.insert("paint.textClipPath".into());
        }
        envelope_node(
            &mut value["root"],
            "root",
            strict_kind,
            &reports,
            &mut used_variants,
            &mut required_features,
            &mut issues,
        );
        if !issues.is_empty() {
            return Err(issues);
        }

        value["schemaVersion"] = json!(TEXT_IR_V2_ENVELOPE_SCHEMA_VERSION);
        value["schemaMinorVersion"] = json!(0);
        value["schema"] = json!({ "major": 2, "minor": 0 });
        value["layout"] = json!({ "profile": "hwpCompat", "measurementAuthority": "legacyHwpPositions", "shapedMeasurement": "diagnosticsOnly" });
        let strict = strict_kind.is_some();
        if strict {
            required_features.insert("text.strictVisualFallbackFree".to_string());
        }
        for name in [
            "text.variants",
            "text.paintOrderSlot",
            "text.strictVisualFallbackFree",
        ] {
            append_feature(&mut value["knownFeatures"], name);
        }
        for feature in &required_features {
            append_feature(&mut value["usedFeatures"], feature);
        }
        value["requiredFeatures"] = json!(required_features);
        value["text"]["defaultVariant"] =
            json!(strict_kind.map_or("textRun", TextVariantKind::as_str));
        value["text"]["variants"] = json!(used_variants);
        value["text"]["fallbackRequired"] = json!(!strict);
        let text_v2 = &mut value["textV2"];
        text_v2["profile"] = json!(if strict {
            "strictVisual"
        } else {
            "compatibility"
        });
        text_v2["canonicalOp"] = json!("text");
        text_v2["compatibilityProfile"] = json!(if strict {
            "fallbackFreeStrict"
        } else {
            "v2Compat"
        });
        text_v2["fallbackPolicy"] = json!(if strict { "none" } else { "required" });
        text_v2["fallbackRequired"] = json!(!strict);
        text_v2["strictVisualFallbackFree"] = json!(strict);
        text_v2["paintOrderSlots"] = json!("required");
        // A strict export no longer contains the compatibility fallback. Keep
        // diagnostics as evidence without claiming that v1 downgrade is lossless.
        if strict {
            text_v2["downgradePath"] = json!("requiresTextRunFallbackReconstruction");
            if let Some(slots) = text_v2["slotDiagnostics"].as_array_mut() {
                for slot in slots {
                    slot["fallbackPresent"] = json!(false);
                    slot["strictVariantAvailable"] = json!(true);
                    slot["fallbackReason"] = Value::Null;
                }
            }
        }
        serde_json::to_string(&value).map_err(|error| {
            vec![issue(
                "invalidPaintSerialization",
                None,
                "root",
                error.to_string(),
            )]
        })
    }
}

fn append_feature(features: &mut Value, feature: &str) {
    if let Some(features) = features.as_array_mut() {
        if !features.iter().any(|value| value == feature) {
            features.push(json!(feature));
        }
    }
}

fn issue(
    code: &'static str,
    slot_id: Option<String>,
    leaf_path: &str,
    message: String,
) -> TextV2ValidationIssue {
    TextV2ValidationIssue {
        severity: TextV2ValidationSeverity::Error,
        code,
        slot_id,
        leaf_path: Some(leaf_path.to_string()),
        message,
    }
}

fn slot_id(op: &Value) -> Option<String> {
    if !matches!(
        op["type"].as_str(),
        Some("textRun" | "glyphRun" | "glyphOutline")
    ) {
        return None;
    }
    op["variant"]["equivalenceGroup"]
        .as_str()
        .map(str::to_string)
        .or_else(|| op["source"]["id"].as_u64().map(|id| format!("text-{id}")))
}

fn envelope_node(
    node: &mut Value,
    path: &str,
    strict_kind: Option<TextVariantKind>,
    reports: &BTreeMap<String, TextVariantSelectionReport>,
    used_variants: &mut BTreeSet<String>,
    required_features: &mut BTreeSet<String>,
    issues: &mut Vec<TextV2ValidationIssue>,
) {
    match node["kind"].as_str() {
        Some("group") => {
            if let Some(children) = node["children"].as_array_mut() {
                for (index, child) in children.iter_mut().enumerate() {
                    envelope_node(
                        child,
                        &format!("{path}/group[{index}]"),
                        strict_kind,
                        reports,
                        used_variants,
                        required_features,
                        issues,
                    );
                }
            }
        }
        Some("clipRect") | Some("clipPath") => envelope_node(
            &mut node["child"],
            &format!("{path}/clip"),
            strict_kind,
            reports,
            used_variants,
            required_features,
            issues,
        ),
        Some("leaf") => {
            let ops = node["ops"]
                .as_array_mut()
                .map(std::mem::take)
                .unwrap_or_default();
            let mut by_slot = BTreeMap::<String, Vec<Value>>::new();
            for op in &ops {
                if let Some(id) = slot_id(op) {
                    by_slot.entry(id).or_default().push(op.clone());
                }
            }
            let mut emitted = Vec::with_capacity(ops.len());
            for op in ops {
                if op["type"] != "textRun" {
                    if !matches!(op["type"].as_str(), Some("glyphRun" | "glyphOutline")) {
                        emitted.push(op);
                    }
                    continue;
                }
                let Some(id) = slot_id(&op) else {
                    issues.push(issue(
                        "textSourceMissing",
                        None,
                        path,
                        "TextRun has no canonical source id".to_string(),
                    ));
                    continue;
                };
                let parts = by_slot.remove(&id).unwrap_or_default();
                let selected = if let Some(kind) = strict_kind {
                    match reports.get(&id) {
                        Some(report)
                            if report.selected_variant_kind == Some(kind)
                                && !report.fallback_required =>
                        {
                            report.selected_variant_id.as_deref()
                        }
                        _ => {
                            issues.push(issue("strictVisualVariantMissing", Some(id), path, format!("fallback-free {} export requires a complete, resource-proven variant for every text slot", kind.as_str())));
                            continue;
                        }
                    }
                } else {
                    None
                };
                let variants = envelope_variants(parts, selected, path, &id, issues);
                for variant in &variants {
                    if let Some(kind) = variant["kind"].as_str() {
                        used_variants.insert(kind.to_string());
                        if strict_kind.is_some() {
                            required_features.insert(
                                if kind == "glyphRun" {
                                    "text.glyphRun"
                                } else {
                                    "text.outlineGlyph"
                                }
                                .to_string(),
                            );
                            if kind == "glyphRun" {
                                required_features.insert("fontResources".to_string());
                            }
                        }
                    }
                    if strict_kind.is_some() {
                        if let Some(features) = variant["requiredFeatures"].as_array() {
                            required_features.extend(
                                features
                                    .iter()
                                    .filter_map(Value::as_str)
                                    .map(str::to_string),
                            );
                        }
                        for part in variant["parts"].as_array().into_iter().flatten() {
                            if let Some(payload_kind) = part["payload"]["payloadKind"].as_str() {
                                required_features
                                    .insert(format!("text.glyphOutline.{payload_kind}"));
                            }
                            let payload = &part["payload"];
                            if !payload["svgGlyph"]["fontEmBox"].is_null() {
                                required_features
                                    .insert("text.glyphOutline.svgGlyph.fontEmBox".to_string());
                            }
                            if let Some(format) = payload["colorLayers"]["colorFormat"].as_str() {
                                required_features
                                    .insert(format!("text.glyphOutline.colorLayers.{format}"));
                            }
                            for node in payload["colorLayers"]["paintGraph"]["nodes"]
                                .as_array()
                                .into_iter()
                                .flatten()
                            {
                                match node["kind"].as_str() {
                                    Some("composite") => {
                                        required_features.insert(
                                            "text.glyphOutline.colorLayers.colrV1.sourceOver"
                                                .to_string(),
                                        );
                                    }
                                    Some("clip") => {
                                        required_features.insert(
                                            "text.glyphOutline.colorLayers.colrV1.clip".to_string(),
                                        );
                                    }
                                    _ => {}
                                }
                            }
                        }
                    }
                }
                emitted.push(json!({
                    "id": id, "type": "text", "bbox": op["bbox"],
                    "paintOrderSlotId": id, "selectionPolicy": "exclusiveVariantSet",
                    "defaultVariantId": selected.unwrap_or("textRun"),
                    "fallbackPolicy": if strict_kind.is_some() { "none" } else { "required" },
                    "variants": variants,
                }));
            }
            // A second sidecar group may claim the same source. The canonical
            // v1 writer anchors only one group to that TextRun; silently dropping
            // the remaining group would make even a compat export lossy.
            for id in by_slot.into_keys() {
                issues.push(issue(
                    "unanchoredTextVariant",
                    Some(id),
                    path,
                    "text variant group has no exported TextRun slot".to_string(),
                ));
            }
            node["ops"] = Value::Array(emitted);
        }
        _ => {}
    }
}

fn envelope_variants(
    parts: Vec<Value>,
    selected: Option<&str>,
    path: &str,
    slot: &str,
    issues: &mut Vec<TextV2ValidationIssue>,
) -> Vec<Value> {
    let mut variants = Vec::<Value>::new();
    for mut payload in parts {
        let kind = payload["type"].as_str().unwrap_or("textRun").to_string();
        let meta = payload["variant"].clone();
        let id = meta["variantId"].as_str().unwrap_or("textRun").to_string();
        if selected.is_some_and(|selected| selected != id) {
            continue;
        }
        let part_index = meta["partIndex"].as_u64().unwrap_or(0);
        let part_count = meta["partCount"].as_u64().unwrap_or(1);
        payload["id"] = json!(if kind == "textRun" {
            format!("op-{slot}")
        } else {
            format!("op-{slot}-{id}-{part_index}")
        });
        let mut part =
            json!({ "partIndex": part_index, "partCount": part_count, "payload": payload });
        if !meta["localPaintOrder"].is_null() {
            part["localPaintOrder"] = meta["localPaintOrder"].clone();
        }
        let index = if let Some(index) = variants
            .iter()
            .position(|variant| variant["variantId"] == id)
        {
            index
        } else {
            let mut variant =
                json!({ "variantId": id, "kind": kind, "requiredFeatures": [], "parts": [] });
            if !meta["quality"].is_null() {
                variant["quality"] = meta["quality"].clone();
            }
            variants.push(variant);
            variants.len() - 1
        };
        let variant = &mut variants[index];
        if variant["kind"] != kind {
            issues.push(issue(
                "variantKindMismatch",
                Some(slot.to_string()),
                path,
                format!("variant {id} mixes text payload kinds"),
            ));
        }
        if let Some(features) = meta["requires"].as_array() {
            for feature in features.iter().filter_map(Value::as_str) {
                append_feature(&mut variant["requiredFeatures"], feature);
            }
        }
        variant["parts"]
            .as_array_mut()
            .expect("constructed parts array")
            .push(part);
    }
    for variant in &variants {
        let parts = variant["parts"]
            .as_array()
            .expect("constructed parts array");
        let expected = parts
            .first()
            .and_then(|part| part["partCount"].as_u64())
            .unwrap_or(0);
        let indices = parts
            .iter()
            .filter_map(|part| part["partIndex"].as_u64())
            .collect::<BTreeSet<_>>();
        if expected == 0
            || expected != parts.len() as u64
            || indices.len() != parts.len()
            || parts
                .iter()
                .any(|part| part["partCount"].as_u64() != Some(expected))
            || indices
                .iter()
                .enumerate()
                .any(|(expected, actual)| *actual != expected as u64)
        {
            issues.push(issue(
                "variantPartsIncomplete",
                Some(slot.to_string()),
                path,
                "text variant parts must form exactly one complete set".to_string(),
            ));
        }
    }
    variants
}