fonts 0.2.0

High-performance font parsing and analysis library for Grida Canvas
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
use fonts::{CurrentTextStyle, UIFontParser};
use std::collections::HashMap;

/// Comprehensive verification tests for smart resolution behavior.
///
/// These tests explicitly verify the expected behavior of smart resolution,
/// not just that the tests pass. This is crucial for implementing get_normals()
/// which will need to work in reverse and maintain consistency.
///
/// Expected Behavior Documentation:
///
/// 1. PRIORITY RULE: Explicit properties (weight, width, slant) ALWAYS take priority over custom_axes
/// 2. FALLBACK RULE: Only use custom_axes when explicit property is None
/// 3. ITALIC SPECIAL: italic flag NEVER maps to custom_axes["ital"] - always returns None
/// 4. AXIS SEEDING: Only resolve to custom_axes if the target font actually has that axis
/// 5. NON-VF FALLBACK: For non-variable fonts, only explicit properties are used

#[test]
fn verify_priority_rule_explicit_properties_always_win() {
    let parser = UIFontParser::new();

    // Test case: Explicit weight=400, custom_axes["wght"]=500
    // EXPECTED: weight=400 should win (explicit property priority)
    let mut custom_axes = HashMap::new();
    custom_axes.insert("wght".to_string(), 500.0);

    let current_style = CurrentTextStyle {
        weight: Some(400), // This should ALWAYS win
        width: None,
        slant: None,
        custom_axes,
    };

    // VERIFICATION: Explicit weight should take priority
    assert_eq!(
        parser.get_current_axis_value(&current_style, "wght"),
        Some(400.0),
        "FAILURE: Explicit weight=400 should take priority over custom_axes[\"wght\"]=500"
    );

    // Test case: Explicit width=100, custom_axes["wdth"]=120
    // EXPECTED: width=100 should win
    let mut custom_axes = HashMap::new();
    custom_axes.insert("wdth".to_string(), 120.0);

    let current_style = CurrentTextStyle {
        weight: None,
        width: Some(100), // This should ALWAYS win
        slant: None,
        custom_axes,
    };

    // VERIFICATION: Explicit width should take priority
    assert_eq!(
        parser.get_current_axis_value(&current_style, "wdth"),
        Some(100.0),
        "FAILURE: Explicit width=100 should take priority over custom_axes[\"wdth\"]=120"
    );

    // Test case: Explicit slant=15.0, custom_axes["slnt"]=5.0
    // EXPECTED: slant=15.0 should win
    let mut custom_axes = HashMap::new();
    custom_axes.insert("slnt".to_string(), 5.0);

    let current_style = CurrentTextStyle {
        weight: None,
        width: None,
        slant: Some(15.0), // This should ALWAYS win
        custom_axes,
    };

    // VERIFICATION: Explicit slant should take priority
    assert_eq!(
        parser.get_current_axis_value(&current_style, "slnt"),
        Some(15.0),
        "FAILURE: Explicit slant=15.0 should take priority over custom_axes[\"slnt\"]=5.0"
    );
}

#[test]
fn verify_fallback_rule_custom_axes_when_explicit_is_none() {
    let parser = UIFontParser::new();

    // Test case: weight=None, custom_axes["wght"]=500
    // EXPECTED: Should fallback to custom_axes["wght"]=500
    let mut custom_axes = HashMap::new();
    custom_axes.insert("wght".to_string(), 500.0);

    let current_style = CurrentTextStyle {
        weight: None, // This should trigger fallback
        width: None,
        slant: None,
        custom_axes,
    };

    // VERIFICATION: Should fallback to custom_axes
    assert_eq!(
        parser.get_current_axis_value(&current_style, "wght"),
        Some(500.0),
        "FAILURE: Should fallback to custom_axes[\"wght\"]=500 when weight=None"
    );

    // Test case: width=None, custom_axes["wdth"]=120
    // EXPECTED: Should fallback to custom_axes["wdth"]=120
    let mut custom_axes = HashMap::new();
    custom_axes.insert("wdth".to_string(), 120.0);

    let current_style = CurrentTextStyle {
        weight: None,
        width: None, // This should trigger fallback
        slant: None,
        custom_axes,
    };

    // VERIFICATION: Should fallback to custom_axes
    assert_eq!(
        parser.get_current_axis_value(&current_style, "wdth"),
        Some(120.0),
        "FAILURE: Should fallback to custom_axes[\"wdth\"]=120 when width=None"
    );

    // Test case: slant=None, custom_axes["slnt"]=5.0
    // EXPECTED: Should fallback to custom_axes["slnt"]=5.0
    let mut custom_axes = HashMap::new();
    custom_axes.insert("slnt".to_string(), 5.0);

    let current_style = CurrentTextStyle {
        weight: None,
        width: None,
        slant: None, // This should trigger fallback
        custom_axes,
    };

    // VERIFICATION: Should fallback to custom_axes
    assert_eq!(
        parser.get_current_axis_value(&current_style, "slnt"),
        Some(5.0),
        "FAILURE: Should fallback to custom_axes[\"slnt\"]=5.0 when slant=None"
    );
}

// Test removed: italic field no longer exists in CurrentTextStyle

#[test]
fn verify_custom_axes_only_for_other_axes() {
    let parser = UIFontParser::new();

    // Test case: custom_axes["opsz"]=14.0 (no explicit property exists)
    // EXPECTED: Should return custom_axes["opsz"]=14.0
    let mut custom_axes = HashMap::new();
    custom_axes.insert("opsz".to_string(), 14.0);

    let current_style = CurrentTextStyle {
        weight: None,
        width: None,
        slant: None,
        custom_axes,
    };

    // VERIFICATION: Should use custom_axes for non-explicit axes
    assert_eq!(
        parser.get_current_axis_value(&current_style, "opsz"),
        Some(14.0),
        "FAILURE: Should use custom_axes[\"opsz\"]=14.0 for axes without explicit properties"
    );

    // Test case: custom_axes["GRAD"]=0.5 (no explicit property exists)
    // EXPECTED: Should return custom_axes["GRAD"]=0.5
    let mut custom_axes = HashMap::new();
    custom_axes.insert("GRAD".to_string(), 0.5);

    let current_style = CurrentTextStyle {
        weight: None,
        width: None,
        slant: None,
        custom_axes,
    };

    // VERIFICATION: Should use custom_axes for non-explicit axes
    assert_eq!(
        parser.get_current_axis_value(&current_style, "GRAD"),
        Some(0.5),
        "FAILURE: Should use custom_axes[\"GRAD\"]=0.5 for axes without explicit properties"
    );
}

#[test]
fn verify_none_values_return_none() {
    let parser = UIFontParser::new();

    // Test case: All values are None or empty
    // EXPECTED: All should return None
    let current_style = CurrentTextStyle {
        weight: None,
        width: None,
        slant: None,
        custom_axes: HashMap::new(),
    };

    // VERIFICATION: All should return None
    assert_eq!(
        parser.get_current_axis_value(&current_style, "wght"),
        None,
        "FAILURE: Should return None when weight=None and no custom_axes[\"wght\"]"
    );

    assert_eq!(
        parser.get_current_axis_value(&current_style, "wdth"),
        None,
        "FAILURE: Should return None when width=None and no custom_axes[\"wdth\"]"
    );

    assert_eq!(
        parser.get_current_axis_value(&current_style, "slnt"),
        None,
        "FAILURE: Should return None when slant=None and no custom_axes[\"slnt\"]"
    );

    assert_eq!(
        parser.get_current_axis_value(&current_style, "ital"),
        None,
        "FAILURE: Should return None for italic axis (special handling)"
    );

    assert_eq!(
        parser.get_current_axis_value(&current_style, "opsz"),
        None,
        "FAILURE: Should return None when no custom_axes[\"opsz\"]"
    );
}

#[test]
fn verify_mixed_scenarios_priority_and_fallback() {
    let parser = UIFontParser::new();

    // Test case: Mixed scenario with some explicit, some fallback, some custom-only
    // EXPECTED: Each should follow its respective rule
    let mut custom_axes = HashMap::new();
    custom_axes.insert("wght".to_string(), 500.0); // Should be overridden by weight=400
    custom_axes.insert("wdth".to_string(), 120.0); // Should be used (width=None)
    custom_axes.insert("slnt".to_string(), 5.0); // Should be overridden by slant=15.0
    custom_axes.insert("opsz".to_string(), 14.0); // Should be used (no explicit property)

    let current_style = CurrentTextStyle {
        weight: Some(400), // Should take priority over custom_axes["wght"]
        width: None,       // Should fallback to custom_axes["wdth"]
        slant: Some(15.0), // Should take priority over custom_axes["slnt"]
        custom_axes,
    };

    // VERIFICATION: Each axis should follow its respective rule
    assert_eq!(
        parser.get_current_axis_value(&current_style, "wght"),
        Some(400.0),
        "FAILURE: weight=400 should take priority over custom_axes[\"wght\"]=500"
    );

    assert_eq!(
        parser.get_current_axis_value(&current_style, "wdth"),
        Some(120.0),
        "FAILURE: width=None should fallback to custom_axes[\"wdth\"]=120"
    );

    assert_eq!(
        parser.get_current_axis_value(&current_style, "slnt"),
        Some(15.0),
        "FAILURE: slant=15.0 should take priority over custom_axes[\"slnt\"]=5.0"
    );

    assert_eq!(
        parser.get_current_axis_value(&current_style, "opsz"),
        Some(14.0),
        "FAILURE: Should use custom_axes[\"opsz\"]=14.0 for axes without explicit properties"
    );
}

#[test]
fn verify_edge_case_zero_values() {
    let parser = UIFontParser::new();

    // Test case: Zero values in explicit properties
    // EXPECTED: Zero values should be treated as valid (not None)
    let mut custom_axes = HashMap::new();
    custom_axes.insert("wght".to_string(), 500.0);
    custom_axes.insert("wdth".to_string(), 120.0);
    custom_axes.insert("slnt".to_string(), 5.0);

    let current_style = CurrentTextStyle {
        weight: Some(0),  // Zero should be treated as valid explicit value
        width: Some(0),   // Zero should be treated as valid explicit value
        slant: Some(0.0), // Zero should be treated as valid explicit value
        // False should be treated as valid explicit value
        custom_axes,
    };

    // VERIFICATION: Zero values should take priority over custom_axes
    assert_eq!(
        parser.get_current_axis_value(&current_style, "wght"),
        Some(0.0),
        "FAILURE: weight=0 should take priority over custom_axes[\"wght\"]=500"
    );

    assert_eq!(
        parser.get_current_axis_value(&current_style, "wdth"),
        Some(0.0),
        "FAILURE: width=0 should take priority over custom_axes[\"wdth\"]=120"
    );

    assert_eq!(
        parser.get_current_axis_value(&current_style, "slnt"),
        Some(0.0),
        "FAILURE: slant=0.0 should take priority over custom_axes[\"slnt\"]=5.0"
    );
}

#[test]
fn verify_edge_case_negative_values() {
    let parser = UIFontParser::new();

    // Test case: Negative values in explicit properties
    // EXPECTED: Negative values should be treated as valid (not None)
    let mut custom_axes = HashMap::new();
    custom_axes.insert("slnt".to_string(), 5.0);

    let current_style = CurrentTextStyle {
        weight: None,
        width: None,
        slant: Some(-15.0), // Negative should be treated as valid explicit value
        custom_axes,
    };

    // VERIFICATION: Negative values should take priority over custom_axes
    assert_eq!(
        parser.get_current_axis_value(&current_style, "slnt"),
        Some(-15.0),
        "FAILURE: slant=-15.0 should take priority over custom_axes[\"slnt\"]=5.0"
    );
}

#[test]
fn verify_consistency_for_round_trip_testing() {
    let parser = UIFontParser::new();

    // This test verifies that the resolution behavior is consistent and predictable
    // for round-trip testing (get_italics -> get_normals -> get_italics)

    // Test case: Create a style with known values
    let mut custom_axes = HashMap::new();
    custom_axes.insert("wght".to_string(), 500.0);
    custom_axes.insert("wdth".to_string(), 120.0);
    custom_axes.insert("slnt".to_string(), 5.0);
    custom_axes.insert("opsz".to_string(), 14.0);

    let current_style = CurrentTextStyle {
        weight: Some(400), // Explicit
        width: None,       // Fallback
        slant: Some(15.0), // Explicit
        // Special
        custom_axes,
    };

    // VERIFICATION: Resolution should be consistent
    let resolved_values = [
        (
            "wght",
            parser.get_current_axis_value(&current_style, "wght"),
        ),
        (
            "wdth",
            parser.get_current_axis_value(&current_style, "wdth"),
        ),
        (
            "slnt",
            parser.get_current_axis_value(&current_style, "slnt"),
        ),
        (
            "opsz",
            parser.get_current_axis_value(&current_style, "opsz"),
        ),
        (
            "ital",
            parser.get_current_axis_value(&current_style, "ital"),
        ),
    ];

    // Expected resolved values
    let expected_values = [
        ("wght", Some(400.0)), // Explicit weight wins
        ("wdth", Some(120.0)), // Fallback to custom_axes
        ("slnt", Some(15.0)),  // Explicit slant wins
        ("opsz", Some(14.0)),  // Custom axis only
        ("ital", None),        // Special handling
    ];

    // VERIFICATION: All values should match expected
    for ((axis, actual), (expected_axis, expected)) in
        resolved_values.iter().zip(expected_values.iter())
    {
        assert_eq!(
            axis, expected_axis,
            "FAILURE: Axis mismatch in consistency test"
        );
        assert_eq!(
            actual, expected,
            "FAILURE: {} resolution inconsistent: got {:?}, expected {:?}",
            axis, actual, expected
        );
    }
}