text-typeset 1.6.0

Turns rich text documents into GPU-ready glyph quads
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
mod helpers;
use helpers::{NOTO_SANS, Typesetter, make_typesetter};

use text_typeset::font::resolve::resolve_font;
use text_typeset::shaping::shaper::{font_metrics_px, shape_text, shape_text_with_buffer};

#[test]
fn shape_hello_produces_glyphs() {
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    let run = shape_text(ts.font_registry(), &resolved, "Hello", 0).unwrap();

    // "Hello" has 5 characters and should produce at least 5 glyphs
    // (could be fewer with ligatures, but Noto Sans doesn't ligate H-e-l-l-o)
    assert_eq!(run.glyphs.len(), 5);
    assert!(run.advance_width > 0.0);
}

#[test]
fn shape_empty_string_produces_no_glyphs() {
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    let run = shape_text(ts.font_registry(), &resolved, "", 0).unwrap();
    assert_eq!(run.glyphs.len(), 0);
    assert!((run.advance_width - 0.0).abs() < f32::EPSILON);
}

#[test]
fn glyph_advances_are_positive() {
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    let run = shape_text(ts.font_registry(), &resolved, "Test text", 0).unwrap();

    for glyph in &run.glyphs {
        // Space may have zero y_advance, but x_advance should be positive
        // (for LTR text, all glyphs advance right)
        assert!(
            glyph.x_advance >= 0.0,
            "glyph_id {} has negative x_advance: {}",
            glyph.glyph_id,
            glyph.x_advance
        );
    }
}

#[test]
fn cluster_values_map_to_byte_offsets() {
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    let text = "AB";
    let run = shape_text(ts.font_registry(), &resolved, text, 0).unwrap();

    assert_eq!(run.glyphs.len(), 2);
    // First glyph's cluster should be byte offset 0 (for 'A')
    assert_eq!(run.glyphs[0].cluster, 0);
    // Second glyph's cluster should be byte offset 1 (for 'B')
    assert_eq!(run.glyphs[1].cluster, 1);
}

#[test]
fn multibyte_utf8_clusters_are_correct() {
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    // 'é' is 2 bytes in UTF-8 (0xC3, 0xA9)
    let text = "";
    let run = shape_text(ts.font_registry(), &resolved, text, 0).unwrap();

    assert!(run.glyphs.len() >= 2);
    assert_eq!(run.glyphs[0].cluster, 0); // 'A' at byte 0
    assert_eq!(run.glyphs[1].cluster, 1); // 'é' at byte 1
}

#[test]
fn text_offset_is_stored_in_run() {
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    let run = shape_text(ts.font_registry(), &resolved, "Hi", 42).unwrap();
    assert_eq!(run.text_range, 42..44);
}

#[test]
fn larger_font_size_produces_larger_advances() {
    let ts = make_typesetter();
    let small = resolve_font(
        ts.font_registry(),
        None,
        None,
        None,
        None,
        Some(12),
        1.0,
        1.0,
    )
    .unwrap();
    let large = resolve_font(
        ts.font_registry(),
        None,
        None,
        None,
        None,
        Some(48),
        1.0,
        1.0,
    )
    .unwrap();

    let run_small = shape_text(ts.font_registry(), &small, "W", 0).unwrap();
    let run_large = shape_text(ts.font_registry(), &large, "W", 0).unwrap();

    assert!(
        run_large.advance_width > run_small.advance_width,
        "48px advance ({}) should be greater than 12px advance ({})",
        run_large.advance_width,
        run_small.advance_width
    );
}

#[test]
fn space_has_nonzero_advance() {
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    let run = shape_text(ts.font_registry(), &resolved, " ", 0).unwrap();

    assert_eq!(run.glyphs.len(), 1);
    assert!(
        run.glyphs[0].x_advance > 0.0,
        "space advance should be positive"
    );
}

#[test]
fn font_metrics_are_reasonable() {
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    let metrics = font_metrics_px(ts.font_registry(), &resolved).unwrap();

    // Ascent should be positive (above baseline)
    assert!(metrics.ascent > 0.0, "ascent should be positive");
    // Descent should be positive in swash (distance below baseline)
    assert!(metrics.descent > 0.0, "descent should be positive");
    // Line height (ascent + descent + leading) should be reasonable for 16px
    let line_height = metrics.ascent + metrics.descent + metrics.leading;
    assert!(
        line_height > 10.0 && line_height < 40.0,
        "line height {} is out of reasonable range for 16px",
        line_height
    );
}

#[test]
fn total_advance_matches_sum_of_glyphs() {
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    let run = shape_text(ts.font_registry(), &resolved, "Hello world", 0).unwrap();

    let sum: f32 = run.glyphs.iter().map(|g| g.x_advance).sum();
    assert!(
        (run.advance_width - sum).abs() < 0.01,
        "total advance {} should match glyph sum {}",
        run.advance_width,
        sum
    );
}

#[test]
fn no_notdef_glyphs_for_basic_latin() {
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    let run = shape_text(
        ts.font_registry(),
        &resolved,
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
        0,
    )
    .unwrap();

    for glyph in &run.glyphs {
        assert_ne!(
            glyph.glyph_id, 0,
            ".notdef glyph found — font missing a basic Latin character"
        );
    }
}

#[test]
fn shape_with_buffer_recycling() {
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();

    let buffer = harfrust::UnicodeBuffer::new();
    let (run1, buffer) =
        shape_text_with_buffer(ts.font_registry(), &resolved, "Hello", 0, buffer, &[]).unwrap();
    assert_eq!(run1.glyphs.len(), 5);

    // Reuse the recycled buffer for a second shaping call
    let (run2, _buffer) =
        shape_text_with_buffer(ts.font_registry(), &resolved, "World", 0, buffer, &[]).unwrap();
    assert_eq!(run2.glyphs.len(), 5);

    // Both runs should have identical advance structure (same font, same length)
    assert!(run1.advance_width > 0.0);
    assert!(run2.advance_width > 0.0);
}

// ── BiDi tests ──────────────────────────────────────────────────

#[test]
fn bidi_pure_ltr_produces_single_run() {
    use text_typeset::shaping::shaper::bidi_runs;

    let runs = bidi_runs("Hello world");
    assert_eq!(runs.len(), 1);
    assert_eq!(
        runs[0].direction,
        text_typeset::shaping::shaper::TextDirection::LeftToRight
    );
}

#[test]
fn bidi_empty_text_produces_no_runs() {
    use text_typeset::shaping::shaper::bidi_runs;

    // Empty input has nothing to shape; the layout path early-exits on
    // `text.is_empty()`, so an empty slice of runs is the honest answer.
    let runs = bidi_runs("");
    assert!(runs.is_empty());
}

#[test]
fn bidi_mixed_produces_multiple_runs() {
    use text_typeset::shaping::shaper::bidi_runs;

    // Hebrew text mixed with English
    let runs = bidi_runs("Hello שלום world");
    assert!(
        runs.len() >= 2,
        "mixed LTR+RTL text should produce at least 2 bidi runs, got {}",
        runs.len()
    );

    // At least one run should be RTL
    let has_rtl = runs
        .iter()
        .any(|r| r.direction == text_typeset::shaping::shaper::TextDirection::RightToLeft);
    assert!(has_rtl, "mixed text should contain an RTL run");
}

#[test]
fn shape_rtl_text_produces_glyphs() {
    use text_typeset::shaping::shaper::{TextDirection, shape_text_directed};

    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();

    let run = shape_text_directed(
        ts.font_registry(),
        &resolved,
        "שלום",
        0,
        TextDirection::RightToLeft,
        &[],
    );
    assert!(run.is_some(), "shaping RTL text should succeed");
    let run = run.unwrap();
    assert!(!run.glyphs.is_empty(), "RTL text should produce glyphs");
    assert!(run.advance_width > 0.0);
}

#[test]
fn cached_shaper_data_yields_identical_glyphs() {
    // ShaperData is built once per face and cached on the FontEntry.
    // Re-shaping the same face must produce byte-identical glyph output
    // (the cache is a pure speedup, not a behavior change).
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();

    let first = shape_text(ts.font_registry(), &resolved, "Cached shaping", 0).unwrap();
    // Second call hits the cached ShaperData on the same FontEntry.
    let second = shape_text(ts.font_registry(), &resolved, "Cached shaping", 0).unwrap();

    assert_eq!(first.glyphs.len(), second.glyphs.len());
    for (a, b) in first.glyphs.iter().zip(second.glyphs.iter()) {
        assert_eq!(a.glyph_id, b.glyph_id);
        assert_eq!(a.cluster, b.cluster);
        assert!((a.x_advance - b.x_advance).abs() < f32::EPSILON);
    }
    assert!((first.advance_width - second.advance_width).abs() < f32::EPSILON);
}

// ── OpenType feature tests ──────────────────────────────────────

#[test]
fn opentype_features_reach_the_shaper() {
    use text_typeset::FontFeature;
    use text_typeset::shaping::shaper::{TextDirection, shape_text_directed, to_harfrust_features};

    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();

    // Standard ligatures off vs on. NotoSans ligates "fi"; with `liga`
    // disabled it stays two glyphs, with it enabled it collapses to one.
    let off = to_harfrust_features(&[FontFeature::off(*b"liga")]);
    let on = to_harfrust_features(&[FontFeature::on(*b"liga")]);

    let run_off = shape_text_directed(
        ts.font_registry(),
        &resolved,
        "fi",
        0,
        TextDirection::LeftToRight,
        &off,
    )
    .unwrap();
    let run_on = shape_text_directed(
        ts.font_registry(),
        &resolved,
        "fi",
        0,
        TextDirection::LeftToRight,
        &on,
    )
    .unwrap();

    assert_eq!(
        run_off.glyphs.len(),
        2,
        "liga=0 should keep 'fi' as two glyphs"
    );
    assert_eq!(
        run_on.glyphs.len(),
        1,
        "liga=1 should ligate 'fi' into one glyph"
    );
}

// ── Glyph fallback tests ────────────────────────────────────────

#[test]
fn shape_text_with_fallback_no_notdef_for_basic_latin() {
    // With one font that covers Latin, shape_text should produce no .notdef
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    let run = shape_text(ts.font_registry(), &resolved, "Hello", 0).unwrap();
    assert!(
        run.glyphs.iter().all(|g| g.glyph_id != 0),
        "Latin text with Noto Sans should have no .notdef glyphs"
    );
}

#[test]
fn shape_text_fallback_is_attempted_for_missing_glyphs() {
    // Register two copies of the same font. Shape a character that exists
    // in the font — should work without fallback. The test verifies the
    // fallback code path doesn't break normal operation.
    let mut ts = Typesetter::new();
    let face1 = ts.register_font(NOTO_SANS);
    let _face2 = ts.register_font(NOTO_SANS); // second registration for fallback pool
    ts.set_default_font(face1, 16.0);

    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    let run = shape_text(ts.font_registry(), &resolved, "Test", 0).unwrap();

    assert_eq!(run.glyphs.len(), 4);
    assert!(run.advance_width > 0.0);
    // All glyphs should be resolved (no .notdef)
    assert!(
        run.glyphs.iter().all(|g| g.glyph_id != 0),
        "all glyphs should be resolved"
    );
}

#[test]
fn shape_text_advance_recomputed_after_fallback() {
    // Even if fallback is attempted (even if no .notdef present),
    // advance_width should match the sum of glyph advances.
    let ts = make_typesetter();
    let resolved =
        resolve_font(ts.font_registry(), None, None, None, None, None, 1.0, 1.0).unwrap();
    let run = shape_text(ts.font_registry(), &resolved, "Hello world", 0).unwrap();

    let sum: f32 = run.glyphs.iter().map(|g| g.x_advance).sum();
    assert!(
        (run.advance_width - sum).abs() < 0.01,
        "advance_width ({}) should match glyph sum ({})",
        run.advance_width,
        sum
    );
}