k2f_layout 0.3.2

K2F deterministic layout compiler
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
use crate::style::{apply_patch, apply_script_geometry, modifier_precedence, patch_for_modifier};
use crate::Theme;
use k2f_core::Modifier;

use super::types::{push_or_merge_run, TextRun};

/// Split `text` into deterministic styled runs, based on range modifiers.
///
/// - Ranges are **byte offsets** into UTF-8 text (validated elsewhere to be on char boundaries).
/// - Style is computed as: base(role) + all patches whose range covers the run.
/// - Overlaps are resolved deterministically by:
///   1) modifier type precedence (`Theme.modifiers.precedence` or default),
///   2) modifier type name (lexicographic) for unknown types not in precedence list,
///   3) larger ranges apply before smaller ranges (more specific ranges win),
///   4) range start/end, then intent (lexicographic).
pub(crate) fn split_runs(
    text: &str,
    role: &str,
    variant: Option<&str>,
    modifiers: &[Modifier],
    theme: &Theme,
) -> Result<Vec<TextRun>, String> {
    if modifiers.is_empty() {
        let style = crate::resolved_style::resolve_text_style(role, variant, theme);
        return Ok(vec![TextRun {
            start: 0,
            end: text.len(),
            style,
            text: text.to_string(),
            math_tex: None,
        }]);
    }

    // Defensive validation (callers typically run `validate_semantic_tree` before layout).
    let len = text.len();
    for m in modifiers {
        let [s, e] = m.range;
        if s >= e || e > len {
            return Err(format!(
                "Invalid modifier range {:?} for text length {}",
                m.range, len
            ));
        }
        if !text.is_char_boundary(s) || !text.is_char_boundary(e) {
            return Err(format!(
                "Modifier range {:?} is not on UTF-8 char boundary",
                m.range
            ));
        }
    }

    // Collect deterministic breakpoints.
    let mut points: Vec<usize> = Vec::with_capacity(2 + modifiers.len() * 2);
    points.push(0);
    points.push(len);
    for m in modifiers {
        points.push(m.range[0]);
        points.push(m.range[1]);
    }
    points.sort_unstable();
    points.dedup();

    let precedence = modifier_precedence(theme);
    let base_style = crate::resolved_style::resolve_text_style(role, variant, theme);

    let mut out: Vec<TextRun> = Vec::new();
    for w in points.windows(2) {
        let start = w[0];
        let end = w[1];
        if start == end {
            continue;
        }

        let mut applicable: Vec<&Modifier> = modifiers
            .iter()
            .filter(|m| m.range[0] <= start && m.range[1] >= end)
            .collect();

        applicable.sort_by(|a, b| modifier_cmp(a, b, &precedence));

        let mut style = base_style.clone();
        // Highest-precedence script type wins when both somehow overlap.
        let mut script_kind: Option<&str> = None;
        for m in &applicable {
            let mut patch = patch_for_modifier(m, theme)?;
            if m.mod_type == "superscript" || m.mod_type == "subscript" {
                // Engine owns script size; ignore theme font_size on these types.
                patch.font_size = None;
                script_kind = Some(m.mod_type.as_str());
            }
            style = apply_patch(style, &patch);
        }
        if let Some(kind) = script_kind {
            apply_script_geometry(&mut style, kind);
        }

        let run_text = text[start..end].to_string();
        let math_tex = applicable
            .iter()
            .find(|m| m.mod_type == "math")
            .map(|m| m.intent.clone());
        push_or_merge_run(
            &mut out,
            TextRun {
                start,
                end,
                style,
                text: run_text,
                math_tex,
            },
        );
    }

    // Ensure we always return at least one run (should be true given points include 0/len).
    if out.is_empty() {
        let style = crate::resolved_style::resolve_text_style(role, variant, theme);
        return Ok(vec![TextRun {
            start: 0,
            end: text.len(),
            style,
            text: text.to_string(),
            math_tex: None,
        }]);
    }

    Ok(out)
}

fn precedence_index(mod_type: &str, precedence: &[String]) -> usize {
    precedence
        .iter()
        .position(|t| t == mod_type)
        .unwrap_or(precedence.len())
}

fn modifier_cmp(a: &Modifier, b: &Modifier, precedence: &[String]) -> std::cmp::Ordering {
    use std::cmp::Reverse;

    let a_prec = precedence_index(&a.mod_type, precedence);
    let b_prec = precedence_index(&b.mod_type, precedence);

    // Larger ranges apply first (so smaller, more specific ranges override later).
    let a_len = a.range[1] - a.range[0];
    let b_len = b.range[1] - b.range[0];

    (
        a_prec,
        a.mod_type.as_str(),
        Reverse(a_len),
        a.range[0],
        Reverse(a.range[1]),
        a.intent.as_str(),
    )
        .cmp(&(
            b_prec,
            b.mod_type.as_str(),
            Reverse(b_len),
            b.range[0],
            Reverse(b.range[1]),
            b.intent.as_str(),
        ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::style::StylePatch;
    use k2f_core::Pt;
    use std::collections::HashMap;

    fn color_emphasis_theme() -> Theme {
        let mut theme = Theme::default();
        let mut by_intent = HashMap::new();
        by_intent.insert(
            "red".to_string(),
            StylePatch {
                color: Some("red".to_string()),
                ..StylePatch::default()
            },
        );
        by_intent.insert(
            "blue".to_string(),
            StylePatch {
                color: Some("blue".to_string()),
                ..StylePatch::default()
            },
        );
        theme
            .modifiers
            .styles
            .insert("emphasis".to_string(), by_intent);
        theme
    }

    #[test]
    fn adjacent_modifiers_create_adjacent_runs() {
        let theme = color_emphasis_theme();
        let text = "HelloWorld";
        let modifiers = vec![
            Modifier {
                range: [0, 5],
                mod_type: "emphasis".to_string(),
                intent: "red".to_string(),
            },
            Modifier {
                range: [5, 10],
                mod_type: "emphasis".to_string(),
                intent: "blue".to_string(),
            },
        ];

        let runs = split_runs(text, "body", None, &modifiers, &theme).unwrap();
        assert_eq!(runs.len(), 2);
        assert_eq!(runs[0].start, 0);
        assert_eq!(runs[0].end, 5);
        assert_eq!(runs[0].text, "Hello");
        assert_eq!(runs[0].style.color, "red");
        assert_eq!(runs[1].start, 5);
        assert_eq!(runs[1].end, 10);
        assert_eq!(runs[1].text, "World");
        assert_eq!(runs[1].style.color, "blue");
    }

    #[test]
    fn overlapping_same_type_more_specific_range_wins() {
        let theme = color_emphasis_theme();
        let text = "abcdef";
        let modifiers = vec![
            Modifier {
                range: [0, 6],
                mod_type: "emphasis".to_string(),
                intent: "red".to_string(),
            },
            Modifier {
                range: [1, 3],
                mod_type: "emphasis".to_string(),
                intent: "blue".to_string(),
            },
        ];

        let runs = split_runs(text, "body", None, &modifiers, &theme).unwrap();
        assert_eq!(runs.len(), 3);
        assert_eq!(
            (
                runs[0].start,
                runs[0].end,
                runs[0].text.as_str(),
                runs[0].style.color.as_str()
            ),
            (0, 1, "a", "red")
        );
        assert_eq!(
            (
                runs[1].start,
                runs[1].end,
                runs[1].text.as_str(),
                runs[1].style.color.as_str()
            ),
            (1, 3, "bc", "blue")
        );
        assert_eq!(
            (
                runs[2].start,
                runs[2].end,
                runs[2].text.as_str(),
                runs[2].style.color.as_str()
            ),
            (3, 6, "def", "red")
        );
    }

    #[test]
    fn overlapping_different_types_follow_precedence_deterministically() {
        let mut theme = Theme::default();

        // Set explicit precedence: "a" then "b" (so "b" applies later and wins).
        theme.modifiers.precedence = vec!["emphasis".to_string(), "underline".to_string()];

        // Provide theme styles for both types, both writing `color`.
        let mut styles: HashMap<String, HashMap<String, StylePatch>> = HashMap::new();
        styles.insert(
            "emphasis".to_string(),
            HashMap::from([(
                "x".to_string(),
                StylePatch {
                    color: Some("red".to_string()),
                    ..StylePatch::default()
                },
            )]),
        );
        styles.insert(
            "underline".to_string(),
            HashMap::from([(
                "x".to_string(),
                StylePatch {
                    color: Some("blue".to_string()),
                    ..StylePatch::default()
                },
            )]),
        );
        theme.modifiers.styles = styles;

        let text = "abc";
        // Provide modifiers in "winning-last" *reverse* order to ensure we don't depend on input ordering.
        let modifiers = vec![
            Modifier {
                range: [0, 3],
                mod_type: "underline".to_string(),
                intent: "x".to_string(),
            },
            Modifier {
                range: [0, 3],
                mod_type: "emphasis".to_string(),
                intent: "x".to_string(),
            },
        ];

        let runs = split_runs(text, "body", None, &modifiers, &theme).unwrap();
        assert_eq!(runs.len(), 1);
        assert_eq!(runs[0].text, "abc");
        assert_eq!(runs[0].style.color, "blue");
    }

    #[test]
    fn utf8_byte_ranges_split_on_char_boundaries() {
        let theme = color_emphasis_theme();
        let text = "a\u{1F642}b"; // U+1F642 is 4 bytes in UTF-8

        // Byte layout: "a" [0..1], "🙂" [1..5], "b" [5..6]
        assert_eq!(text.len(), 6);
        assert!(text.is_char_boundary(0));
        assert!(text.is_char_boundary(1));
        assert!(text.is_char_boundary(5));
        assert!(text.is_char_boundary(6));

        let modifiers = vec![Modifier {
            range: [1, 5],
            mod_type: "emphasis".to_string(),
            intent: "red".to_string(),
        }];

        let runs = split_runs(text, "body", None, &modifiers, &theme).unwrap();
        assert_eq!(runs.len(), 3);

        assert_eq!(
            (runs[0].start, runs[0].end, runs[0].text.as_str()),
            (0, 1, "a")
        );
        assert_eq!(runs[0].style.color, "black"); // base style default

        assert_eq!(
            (runs[1].start, runs[1].end, runs[1].text.as_str()),
            (1, 5, "\u{1F642}")
        );
        assert_eq!(runs[1].style.color, "red");

        assert_eq!(
            (runs[2].start, runs[2].end, runs[2].text.as_str()),
            (5, 6, "b")
        );
        assert_eq!(runs[2].style.color, "black"); // base style default
    }

    #[test]
    fn superscript_shrinks_and_raises() {
        let theme = Theme::default();
        let text = "x2";
        let modifiers = vec![Modifier {
            range: [1, 2],
            mod_type: "superscript".to_string(),
            intent: "default".to_string(),
        }];
        let runs = split_runs(text, "body", None, &modifiers, &theme).unwrap();
        assert_eq!(runs.len(), 2);
        let parent = runs[0].style.font_size;
        assert_eq!(runs[1].style.font_size, Pt(parent.0 * 7 / 10));
        assert_eq!(runs[1].style.baseline_shift, Pt(parent.0 * 450 / 1000));
        assert_eq!(runs[0].style.baseline_shift, Pt::ZERO);
    }

    #[test]
    fn subscript_shrinks_and_lowers() {
        let theme = Theme::default();
        let text = "H2O";
        let modifiers = vec![Modifier {
            range: [1, 2],
            mod_type: "subscript".to_string(),
            intent: "default".to_string(),
        }];
        let runs = split_runs(text, "body", None, &modifiers, &theme).unwrap();
        assert_eq!(runs.len(), 3);
        let parent = runs[0].style.font_size;
        assert_eq!(runs[1].text, "2");
        assert_eq!(runs[1].style.font_size, Pt(parent.0 * 7 / 10));
        assert_eq!(runs[1].style.baseline_shift, Pt(-(parent.0 * 250 / 1000)));
    }

    #[test]
    fn script_theme_font_size_is_ignored() {
        use crate::style::StylePatch;
        use std::collections::HashMap;

        let mut theme = Theme::default();
        let mut by_intent = HashMap::new();
        by_intent.insert(
            "default".to_string(),
            StylePatch {
                font_size: Some(Pt(99_000)),
                color: Some("red".to_string()),
                ..StylePatch::default()
            },
        );
        theme
            .modifiers
            .styles
            .insert("superscript".to_string(), by_intent);

        let text = "a1";
        let modifiers = vec![Modifier {
            range: [1, 2],
            mod_type: "superscript".to_string(),
            intent: "default".to_string(),
        }];
        let runs = split_runs(text, "body", None, &modifiers, &theme).unwrap();
        let parent = runs[0].style.font_size;
        assert_eq!(runs[1].style.font_size, Pt(parent.0 * 7 / 10));
        assert_ne!(runs[1].style.font_size, Pt(99_000));
        assert_eq!(runs[1].style.color, "red");
    }

    #[test]
    fn utf8_non_boundary_range_is_rejected() {
        let theme = Theme::default();
        let text = "a\u{1F642}b";

        // [1,2] falls inside the multi-byte emoji and is not a char boundary.
        let modifiers = vec![Modifier {
            range: [1, 2],
            mod_type: "emphasis".to_string(),
            intent: "red".to_string(),
        }];

        let err = split_runs(text, "body", None, &modifiers, &theme).unwrap_err();
        assert!(err.contains("not on UTF-8 char boundary"));
    }
}