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
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
use std::fs;
use std::path::PathBuf;

use ttf_parser::{name_id, Face, Tag};

fn font_path(rel: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../fixtures/fonts")
        .join(rel)
}

fn main() {
    explore_roboto_flex_comprehensive();
    test_roboto_flex_italic_detection_scenarios();
    test_roboto_flex_name_table_analysis();
    explore_roboto_flex_gpos_features();
    explore_roboto_flex_feat_table();
}

fn explore_roboto_flex_comprehensive() {
    let path = font_path(
        "Roboto_Flex/RobotoFlex-VariableFont_GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght.ttf",
    );

    if !path.exists() {
        println!("Roboto Flex font not found, skipping exploration");
        return;
    }

    let data = fs::read(&path).unwrap();
    let face = Face::parse(&data, 0).unwrap();

    println!("=== ROBOTO FLEX COMPREHENSIVE EXPLORATION ===");
    println!();

    // === BASIC FONT PROPERTIES ===
    println!("📋 BASIC FONT PROPERTIES:");
    println!("  Units per EM: {}", face.units_per_em());
    println!("  Number of glyphs: {}", face.number_of_glyphs());
    println!("  Is variable: {}", face.is_variable());
    println!();

    // === STYLE & WEIGHT PROPERTIES ===
    println!("🎨 STYLE & WEIGHT PROPERTIES:");
    println!("  Style: {:?}", face.style());
    println!("  Is regular: {}", face.is_regular());
    println!("  Is italic: {}", face.is_italic());
    println!("  Is bold: {}", face.is_bold());
    println!("  Is oblique: {}", face.is_oblique());
    println!("  Is monospaced: {}", face.is_monospaced());
    println!("  Weight: {:?}", face.weight());
    println!("  Width: {:?}", face.width());
    println!("  Italic angle: {}", face.italic_angle());
    println!();

    // === NAME TABLE EXPLORATION ===
    println!("📝 NAME TABLE EXPLORATION:");
    let names = face.names();
    println!("  Total names: {}", names.len());

    for name in names {
        if name.is_unicode() {
            println!(
                "    NameID {}: '{}' (Platform: {:?}, Encoding: {:?})",
                name.name_id,
                name.to_string().unwrap_or_default(),
                name.platform_id,
                name.encoding_id
            );
        }
    }
    println!();

    // === VARIATION AXES DETAILED ===
    println!("⚙️ VARIATION AXES DETAILED:");
    if face.is_variable() {
        println!("  Variable font detected!");
        for axis in face.variation_axes() {
            println!("    Axis '{}':", axis.tag);
            println!("      Min: {}", axis.min_value);
            println!("      Default: {}", axis.def_value);
            println!("      Max: {}", axis.max_value);
            println!("      Name ID: {}", axis.name_id);
            println!("      Hidden: {}", axis.hidden);
        }
    } else {
        println!("  Not a variable font");
    }
    println!();

    // === OS/2 TABLE EXPLORATION ===
    println!("🖥️ OS/2 TABLE EXPLORATION:");
    if let Some(os2) = face.tables().os2 {
        println!("  Version: {}", os2.version);
        println!("  Weight: {:?}", os2.weight());
        println!("  Width: {:?}", os2.width());
        println!("  Style: {:?}", os2.style());
        println!("  Typographic ascender: {}", os2.typographic_ascender());
        println!("  Typographic descender: {}", os2.typographic_descender());
        println!("  Typographic line gap: {}", os2.typographic_line_gap());
        println!("  Windows ascender: {}", os2.windows_ascender());
        println!("  Windows descender: {}", os2.windows_descender());
        println!("  Unicode ranges: {:?}", os2.unicode_ranges());
        println!("  X height: {:?}", os2.x_height());
        println!("  Capital height: {:?}", os2.capital_height());
        println!("  Strikeout metrics: {:?}", os2.strikeout_metrics());
        println!("  Subscript metrics: {:?}", os2.subscript_metrics());
        println!("  Superscript metrics: {:?}", os2.superscript_metrics());
        println!("  Permissions: {:?}", os2.permissions());
    } else {
        println!("  No OS/2 table found");
    }
    println!();

    // === POST TABLE EXPLORATION ===
    println!("📮 POST TABLE EXPLORATION:");
    if let Some(post) = face.tables().post {
        println!("  Italic angle: {}", post.italic_angle);
        println!("  Underline position: {}", post.underline_metrics.position);
        println!(
            "  Underline thickness: {}",
            post.underline_metrics.thickness
        );
        println!("  Is fixed pitch: {}", post.is_monospaced);
    } else {
        println!("  No POST table found");
    }
    println!();

    // === STAT TABLE EXPLORATION ===
    println!("📊 STAT TABLE EXPLORATION:");
    if let Some(stat) = face.tables().stat {
        println!("  Fallback name ID: {:?}", stat.fallback_name_id);
        println!("  Axes count: {}", stat.axes.len());
        for (i, axis) in stat.axes.into_iter().enumerate() {
            println!("    Axis {}: {:?}", i, axis);
        }
    } else {
        println!("  No STAT table found");
    }
    println!();

    // === FVAR TABLE EXPLORATION (if available) ===
    println!("🔄 FVAR TABLE EXPLORATION:");
    if let Some(fvar) = face.tables().fvar {
        println!("  Axes count: {}", fvar.axes.len());
        for (i, axis) in fvar.axes.into_iter().enumerate() {
            println!("    Axis {}: '{}'", i, axis.tag);
            println!("      Min: {}", axis.min_value);
            println!("      Default: {}", axis.def_value);
            println!("      Max: {}", axis.max_value);
            println!("      Name ID: {}", axis.name_id);
            println!("      Hidden: {}", axis.hidden);
        }
    } else {
        println!("  No FVAR table found");
    }
    println!();

    // === METRICS EXPLORATION ===
    println!("📏 METRICS EXPLORATION:");
    println!("  Ascender: {}", face.ascender());
    println!("  Descender: {}", face.descender());
    println!("  Height: {}", face.height());
    println!("  Line gap: {}", face.line_gap());
    println!("  Typographic ascender: {:?}", face.typographic_ascender());
    println!(
        "  Typographic descender: {:?}",
        face.typographic_descender()
    );
    println!("  Typographic line gap: {:?}", face.typographic_line_gap());
    println!("  X height: {:?}", face.x_height());
    println!("  Capital height: {:?}", face.capital_height());
    println!("  Underline metrics: {:?}", face.underline_metrics());
    println!("  Strikeout metrics: {:?}", face.strikeout_metrics());
    println!("  Subscript metrics: {:?}", face.subscript_metrics());
    println!("  Superscript metrics: {:?}", face.superscript_metrics());
    println!();

    // === PERMISSIONS & EMBEDDING ===
    println!("🔒 PERMISSIONS & EMBEDDING:");
    println!("  Permissions: {:?}", face.permissions());
    println!("  Is subsetting allowed: {}", face.is_subsetting_allowed());
    println!(
        "  Is outline embedding allowed: {}",
        face.is_outline_embedding_allowed()
    );
    println!();

    // === UNICODE RANGES ===
    println!("🌍 UNICODE RANGES:");
    println!("  Unicode ranges: {:?}", face.unicode_ranges());
    println!();

    // === GLYPH EXPLORATION (first few glyphs) ===
    println!("🔤 GLYPH EXPLORATION (first 10 glyphs):");
    for i in 0..10.min(face.number_of_glyphs().into()) {
        let glyph_id = ttf_parser::GlyphId(i);
        if let Some(advance) = face.glyph_hor_advance(glyph_id) {
            println!("  Glyph {}: advance = {}", i, advance);
        }
        if let Some(bbox) = face.glyph_bounding_box(glyph_id) {
            println!("    Bounding box: {:?}", bbox);
        }
    }
    println!();

    // === VARIATION COORDINATES TESTING ===
    println!("🎛️ VARIATION COORDINATES TESTING:");
    if face.is_variable() {
        let mut test_face = face.clone();

        // Test setting slnt to -10 degrees
        if let Some(_) = test_face.set_variation(Tag::from_bytes(b"slnt"), -10.0) {
            println!("  Set slnt to -10°: SUCCESS");
            println!("    New italic angle: {}", test_face.italic_angle());
            println!("    New style: {:?}", test_face.style());
            println!("    New is_italic: {}", test_face.is_italic());
        } else {
            println!("  Set slnt to -10°: FAILED");
        }

        // Test setting wght to 700
        if let Some(_) = test_face.set_variation(Tag::from_bytes(b"wght"), 700.0) {
            println!("  Set wght to 700: SUCCESS");
            println!("    New weight: {:?}", test_face.weight());
            println!("    New is_bold: {}", test_face.is_bold());
        } else {
            println!("  Set wght to 700: FAILED");
        }

        // Test setting multiple axes
        let mut multi_face = face.clone();
        if let Some(_) = multi_face.set_variation(Tag::from_bytes(b"slnt"), -15.0) {
            if let Some(_) = multi_face.set_variation(Tag::from_bytes(b"wght"), 600.0) {
                println!("  Set slnt=-15°, wght=600: SUCCESS");
                println!("    New italic angle: {}", multi_face.italic_angle());
                println!("    New weight: {:?}", multi_face.weight());
                println!("    New style: {:?}", multi_face.style());
            }
        }
    }
    println!();

    // === RAW TABLE ACCESS ===
    println!("🗂️ RAW TABLE ACCESS:");
    let raw_face = face.raw_face();
    let table_tags = [
        "head", "hhea", "maxp", "name", "OS/2", "post", "fvar", "STAT", "cmap", "glyf", "CFF ",
    ];

    for tag_str in &table_tags {
        let tag = Tag::from_bytes_lossy(tag_str.as_bytes());
        if let Some(table_data) = raw_face.table(tag) {
            println!("  Table '{}': {} bytes", tag_str, table_data.len());
        } else {
            println!("  Table '{}': NOT FOUND", tag_str);
        }
    }
    println!();

    println!("=== EXPLORATION COMPLETE ===");
    println!();
    println!("🔍 KEY INSIGHTS:");
    println!("1. Roboto Flex has slnt axis (-10° to 0°) but ttf-parser doesn't consider it italic");
    println!("2. Rich name table with italic instances (NameID 282: 'Italic')");
    println!("3. 13 variation axes including slnt, wght, wdth, opsz");
    println!("4. STAT table with proper axis ordering");
    println!("5. Our Level 1 implementation correctly handles this via slnt axis detection");
    println!("6. Potential enhancement: Use STAT table and name table italic instances");
}

fn test_roboto_flex_italic_detection_scenarios() {
    let path = font_path(
        "Roboto_Flex/RobotoFlex-VariableFont_GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght.ttf",
    );

    if !path.exists() {
        println!("Roboto Flex font not found, skipping italic detection scenarios");
        return;
    }

    let data = fs::read(&path).unwrap();
    let face = Face::parse(&data, 0).unwrap();

    println!("=== ROBOTO FLEX ITALIC DETECTION SCENARIOS ===");
    println!();

    // Test different variation coordinates
    let test_scenarios = vec![
        ("Default", vec![]),
        ("Slant -5°", vec![(Tag::from_bytes(b"slnt"), -5.0)]),
        ("Slant -10°", vec![(Tag::from_bytes(b"slnt"), -10.0)]),
        ("Slant -15°", vec![(Tag::from_bytes(b"slnt"), -15.0)]),
        ("Weight 700", vec![(Tag::from_bytes(b"wght"), 700.0)]),
        (
            "Slant -10° + Weight 700",
            vec![
                (Tag::from_bytes(b"slnt"), -10.0),
                (Tag::from_bytes(b"wght"), 700.0),
            ],
        ),
        (
            "Slant -10° + Weight 300",
            vec![
                (Tag::from_bytes(b"slnt"), -10.0),
                (Tag::from_bytes(b"wght"), 300.0),
            ],
        ),
    ];

    for (scenario_name, variations) in test_scenarios {
        let mut test_face = face.clone();

        // Apply variations
        for (tag, value) in &variations {
            test_face.set_variation(*tag, *value);
        }

        println!("📋 Scenario: {}", scenario_name);
        println!("  Style: {:?}", test_face.style());
        println!("  Is italic: {}", test_face.is_italic());
        println!("  Is oblique: {}", test_face.is_oblique());
        println!("  Italic angle: {}", test_face.italic_angle());
        println!("  Weight: {:?}", test_face.weight());
        println!("  Is bold: {}", test_face.is_bold());
        println!();
    }
}

fn test_roboto_flex_name_table_analysis() {
    let path = font_path(
        "Roboto_Flex/RobotoFlex-VariableFont_GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght.ttf",
    );

    if !path.exists() {
        println!("Roboto Flex font not found, skipping name table analysis");
        return;
    }

    let data = fs::read(&path).unwrap();
    let face = Face::parse(&data, 0).unwrap();

    println!("=== ROBOTO FLEX NAME TABLE ANALYSIS ===");
    println!();

    let names = face.names();

    // Group names by ID
    let mut names_by_id: std::collections::HashMap<u16, Vec<_>> = std::collections::HashMap::new();
    for name in names {
        if name.is_unicode() {
            names_by_id.entry(name.name_id).or_default().push(name);
        }
    }

    // Print names by ID
    for (name_id, name_list) in &names_by_id {
        println!("📝 NameID {} ({} entries):", name_id, name_list.len());
        for name in name_list {
            println!(
                "  '{}' (Platform: {:?}, Language: {})",
                name.to_string().unwrap_or_default(),
                name.platform_id,
                name.language_id
            );
        }
        println!();
    }

    // Check for specific name IDs that might indicate italic
    let italic_indicators = vec![
        name_id::FAMILY,
        name_id::SUBFAMILY,
        name_id::FULL_NAME,
        name_id::POST_SCRIPT_NAME,
        name_id::TYPOGRAPHIC_FAMILY,
        name_id::TYPOGRAPHIC_SUBFAMILY,
    ];

    println!("🔍 ITALIC INDICATOR ANALYSIS:");
    for name_id in italic_indicators {
        if let Some(names) = names_by_id.get(&name_id) {
            for name in names {
                let text = name.to_string().unwrap_or_default().to_lowercase();
                let has_italic = text.contains("italic") || text.contains("oblique");
                println!(
                    "  NameID {}: '{}' -> Has italic indicator: {}",
                    name_id,
                    name.to_string().unwrap_or_default(),
                    has_italic
                );
            }
        }
    }
}

fn explore_roboto_flex_gpos_features() {
    let path = font_path(
        "Roboto_Flex/RobotoFlex-VariableFont_GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght.ttf",
    );

    if !path.exists() {
        println!("Roboto Flex font not found, skipping GPOS exploration");
        return;
    }

    let data = fs::read(&path).unwrap();
    let face = Face::parse(&data, 0).unwrap();
    let tables = face.tables();

    println!("=== ROBOTO FLEX GPOS FEATURES EXPLORATION ===");
    println!();

    // Check available tables
    println!("📋 Available tables in Roboto Flex font:");
    println!("  - kern: {}", tables.kern.is_some());
    println!("  - GSUB: {}", tables.gsub.is_some());
    println!("  - GPOS: {}", tables.gpos.is_some());
    println!("  - fvar: {}", tables.fvar.is_some());
    println!("  - STAT: {}", tables.stat.is_some());
    println!();

    // Explore GPOS table structure
    if let Some(gpos) = tables.gpos {
        println!("🎯 GPOS table found!");
        println!("  - Scripts: {}", gpos.scripts.len());
        println!("  - Features: {}", gpos.features.len());
        println!("  - Lookups: {}", gpos.lookups.len());
        println!();

        // List GPOS features
        println!("📝 GPOS features:");
        for feature in gpos.features {
            println!(
                "  - {}: {} lookups",
                feature.tag,
                feature.lookup_indices.len()
            );
        }
        println!();

        // Explore lookups to see what gpos module provides
        println!("🔍 GPOS lookup exploration:");
        for lookup_index in 0..gpos.lookups.len() {
            if let Some(lookup) = gpos.lookups.get(lookup_index) {
                println!(
                    "  Lookup {}: flags {:?}, subtables: {}",
                    lookup_index,
                    lookup.flags,
                    lookup.subtables.len()
                );

                // Try to parse the first subtable using the gpos module
                if let Some(pos_subtable) = lookup
                    .subtables
                    .get::<ttf_parser::gpos::PositioningSubtable>(0)
                {
                    println!("      PositioningSubtable parsed successfully!");
                    println!("        Coverage: {:?}", pos_subtable.coverage());

                    // Now we can use the gpos module types
                    match pos_subtable {
                        ttf_parser::gpos::PositioningSubtable::Single(single_adj) => {
                            println!("        Single adjustment: {:?}", single_adj);
                        }
                        ttf_parser::gpos::PositioningSubtable::Pair(pair_adj) => {
                            println!("        Pair adjustment: {:?}", pair_adj);
                        }
                        ttf_parser::gpos::PositioningSubtable::Cursive(cursive_adj) => {
                            println!("        Cursive adjustment: {:?}", cursive_adj);
                        }
                        ttf_parser::gpos::PositioningSubtable::MarkToBase(mark_to_base) => {
                            println!("        Mark to base: {:?}", mark_to_base);
                        }
                        ttf_parser::gpos::PositioningSubtable::MarkToLigature(mark_to_lig) => {
                            println!("        Mark to ligature: {:?}", mark_to_lig);
                        }
                        ttf_parser::gpos::PositioningSubtable::MarkToMark(mark_to_mark) => {
                            println!("        Mark to mark: {:?}", mark_to_mark);
                        }
                        ttf_parser::gpos::PositioningSubtable::Context(ctx) => {
                            println!("        Context: {:?}", ctx);
                        }
                        ttf_parser::gpos::PositioningSubtable::ChainContext(chain_ctx) => {
                            println!("        Chain context: {:?}", chain_ctx);
                        }
                    }
                } else {
                    println!("      Failed to parse PositioningSubtable");
                }
            }
        }
    } else {
        println!("❌ GPOS table NOT found in Roboto Flex");
    }
    println!();
}

fn explore_roboto_flex_feat_table() {
    let path = font_path(
        "Roboto_Flex/RobotoFlex-VariableFont_GRAD,XOPQ,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght.ttf",
    );

    if !path.exists() {
        println!("Roboto Flex font not found, skipping feat table exploration");
        return;
    }

    let data = fs::read(&path).unwrap();
    let face = Face::parse(&data, 0).unwrap();
    let tables = face.tables();

    println!("=== ROBOTO FLEX FEAT TABLE EXPLORATION ===");
    println!();

    // Check if we can access feat table using the feat module
    if let Some(feat_table) = tables.feat {
        println!("🎯 feat table found and already parsed!");
        println!("Feature names count: {}", feat_table.names.len());
        println!();

        // List feature names using the feat module
        println!("📝 Feature names (using feat module):");
        for feature_name in feat_table.names {
            println!(
                "  - Feature ID {}: name_index {}",
                feature_name.feature, feature_name.name_index
            );
            println!(
                "    Settings: {}, Exclusive: {}",
                feature_name.setting_names.len(),
                feature_name.exclusive
            );
        }
    } else {
        println!("❌ feat table not found in Roboto Flex");
    }
    println!();
}