typg-core 5.0.17

Core search/discovery engine for typg (made by FontLab https://www.fontlab.com/)
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
//! LMDB-backed font index using Roaring Bitmaps for O(K) tag intersection queries.
//!
//! Stores font metadata and an inverted tag index in an LMDB environment,
//! enabling fast filtered lookups across large font collections without
//! re-reading font files on every query.
//!
//! Made by FontLab https://www.fontlab.com/
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::SystemTime;

use anyhow::{Context, Result};
use bytemuck::{Pod, Zeroable};
use heed::types::{Bytes, U64};
use heed::{Database, Env, EnvOpenOptions, RoTxn, RwTxn};
use read_fonts::types::Tag;
use roaring::RoaringBitmap;
use serde::{Deserialize, Serialize};

use crate::query::{FamilyClassFilter, Query};
use crate::search::{TypgFontFaceMatch, TypgFontFaceMeta};

/// Numeric identifier assigned to each indexed font face.
pub type FontID = u64;

/// Maximum LMDB environment size (10 GB, sufficient for over 1 million fonts).
const MAX_DB_SIZE: usize = 10 * 1024 * 1024 * 1024;

/// Maximum number of named LMDB databases.
const MAX_DBS: u32 = 10;

/// Metadata stored in the index for a single font face.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct IndexedFontMeta {
    /// Filesystem path to the font file.
    pub path: String,
    /// Face index within a TTC/OTC collection, if applicable.
    pub ttc_index: Option<u32>,
    /// Name strings extracted from the font's name table.
    pub names: Vec<String>,
    /// Whether the font contains a variable-font axis (`fvar` table).
    pub is_variable: bool,
    /// OS/2 `usWeightClass` value (100–900 scale).
    pub weight_class: Option<u16>,
    /// OS/2 `usWidthClass` value (1–9 condensed-to-expanded scale).
    pub width_class: Option<u16>,
    /// OS/2 family class: `(sFamilyClass >> 8, sFamilyClass & 0xFF)`.
    pub family_class: Option<(u8, u8)>,
    /// Serialized Roaring Bitmap of Unicode codepoints covered by the font.
    pub cmap_bitmap: Vec<u8>,
    /// PostScript Name (ID 6)
    #[serde(default)]
    pub psname: Option<String>,
    /// Typographic Family Name (ID 16)
    #[serde(default)]
    pub tfname: Option<String>,
    /// Legacy Family Name (ID 1)
    #[serde(default)]
    pub lfname: Option<String>,
    /// Typographic Subfamily Name (ID 17)
    #[serde(default)]
    pub tsname: Option<String>,
    /// Legacy Subfamily Name (ID 2)
    #[serde(default)]
    pub lsname: Option<String>,
}

/// Stored record mapping a path hash to its font ID and last-modified time.
///
/// Used for incremental index updates: if the mtime matches, the entry is current.
#[derive(Copy, Clone, Pod, Zeroable)]
#[repr(C)]
struct PathEntry {
    font_id: u64,
    mtime_secs: u64,
}

/// LMDB-backed index for fast font queries using Roaring Bitmap tag intersection.
///
/// Maintains three named databases:
/// - `metadata`: `FontID` → serialized `IndexedFontMeta`
/// - `inverted`: tag (u32) → serialized `RoaringBitmap` of font IDs
/// - `path_to_id`: path hash (u64) → `PathEntry` (font ID + mtime)
pub struct FontIndex {
    env: Env,
    /// `metadata` database: `FontID` → serialized font metadata.
    db_metadata: Database<U64<byteorder::NativeEndian>, Bytes>,
    /// `inverted` database: tag bytes → serialized Roaring Bitmap of font IDs.
    db_inverted: Database<Bytes, Bytes>,
    /// `path_to_id` database: path hash → `PathEntry` for incremental updates.
    db_path_to_id: Database<U64<byteorder::NativeEndian>, Bytes>,
    /// Monotonically increasing counter for allocating font IDs.
    next_id: AtomicU64,
}

impl FontIndex {
    /// Open the index at `index_dir`, creating it if it does not exist.
    ///
    /// Initializes the LMDB environment and the three named databases.
    /// Scans existing metadata entries to determine the next available font ID.
    pub fn open(index_dir: &Path) -> Result<Self> {
        fs::create_dir_all(index_dir)
            .with_context(|| format!("creating index directory {}", index_dir.display()))?;

        let env = unsafe {
            EnvOpenOptions::new()
                .map_size(MAX_DB_SIZE)
                .max_dbs(MAX_DBS)
                .open(index_dir)
                .with_context(|| format!("opening LMDB at {}", index_dir.display()))?
        };

        // Create or open the named databases.
        let mut wtxn = env.write_txn()?;
        let db_metadata = env.create_database(&mut wtxn, Some("metadata"))?;
        let db_inverted = env.create_database(&mut wtxn, Some("inverted"))?;
        let db_path_to_id = env.create_database(&mut wtxn, Some("path_to_id"))?;
        wtxn.commit()?;

        // Determine the next FontID by scanning existing entries.
        let rtxn = env.read_txn()?;
        let mut max_id: u64 = 0;
        for result in db_metadata.iter(&rtxn)? {
            let (id, _) = result?;
            if id > max_id {
                max_id = id;
            }
        }
        drop(rtxn);

        Ok(Self {
            env,
            db_metadata,
            db_inverted,
            db_path_to_id,
            next_id: AtomicU64::new(max_id + 1),
        })
    }

    /// Return the number of font faces currently stored in the index.
    pub fn count(&self) -> Result<usize> {
        let rtxn = self.env.read_txn()?;
        Ok(self.db_metadata.len(&rtxn)? as usize)
    }

    /// Open a write transaction and return an [`IndexWriter`] for adding or updating fonts.
    pub fn writer(&self) -> Result<IndexWriter<'_>> {
        let wtxn = self.env.write_txn()?;
        Ok(IndexWriter {
            index: self,
            wtxn,
            modified_tags: HashSet::new(),
        })
    }

    /// Open a read transaction and return an [`IndexReader`] for querying the index.
    pub fn reader(&self) -> Result<IndexReader<'_>> {
        let rtxn = self.env.read_txn()?;
        Ok(IndexReader { index: self, rtxn })
    }

    /// Allocate and return the next available font ID atomically.
    fn alloc_id(&self) -> FontID {
        self.next_id.fetch_add(1, Ordering::Relaxed)
    }
}

/// Writer handle for atomic index ingestion.
pub struct IndexWriter<'a> {
    index: &'a FontIndex,
    wtxn: RwTxn<'a>,
    modified_tags: HashSet<u32>,
}

impl<'a> IndexWriter<'a> {
    /// Check if a font needs re-indexing based on path and mtime.
    pub fn needs_update(&self, path: &Path, mtime: SystemTime) -> Result<bool> {
        let path_hash = hash_path(path);
        let mtime_secs = mtime
            .duration_since(SystemTime::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        if let Some(bytes) = self.index.db_path_to_id.get(&self.wtxn, &path_hash)? {
            if bytes.len() == std::mem::size_of::<PathEntry>() {
                let entry: PathEntry = *bytemuck::from_bytes(bytes);
                return Ok(entry.mtime_secs != mtime_secs);
            }
        }
        Ok(true) // Not found, needs indexing
    }

    /// Add a font face to the index.
    pub fn add_font(
        &mut self,
        path: &Path,
        ttc_index: Option<u32>,
        mtime: SystemTime,
        meta: &TypgFontFaceMeta,
    ) -> Result<FontID> {
        let path_hash = hash_path(path);
        let mtime_secs = mtime
            .duration_since(SystemTime::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        // Check for existing entry and remove it first.
        if let Some(bytes) = self.index.db_path_to_id.get(&self.wtxn, &path_hash)? {
            if bytes.len() == std::mem::size_of::<PathEntry>() {
                let entry: PathEntry = *bytemuck::from_bytes(bytes);
                self.remove_font_by_id(entry.font_id)?;
            }
        }

        // Allocate new ID.
        let font_id = self.index.alloc_id();

        // Build Roaring Bitmap for cmap coverage.
        let cmap_bitmap = build_cmap_bitmap(&meta.codepoints);

        // Serialize metadata with bincode.
        let meta_to_store = IndexedFontMeta {
            path: path.display().to_string(),
            ttc_index,
            names: meta.names.clone(),
            is_variable: meta.is_variable,
            weight_class: meta.weight_class,
            width_class: meta.width_class,
            family_class: meta.family_class,
            cmap_bitmap,
            psname: meta.psname.clone(),
            tfname: meta.tfname.clone(),
            lfname: meta.lfname.clone(),
            tsname: meta.tsname.clone(),
            lsname: meta.lsname.clone(),
        };

        let meta_bytes = bincode::serialize(&meta_to_store)
            .map_err(|e| anyhow::anyhow!("bincode serialize: {e}"))?;
        self.index
            .db_metadata
            .put(&mut self.wtxn, &font_id, &meta_bytes)?;

        // Update path-to-ID mapping.
        let path_entry = PathEntry {
            font_id,
            mtime_secs,
        };
        self.index.db_path_to_id.put(
            &mut self.wtxn,
            &path_hash,
            bytemuck::bytes_of(&path_entry),
        )?;

        // Update inverted indices for all tags.
        for tag in meta
            .axis_tags
            .iter()
            .chain(&meta.feature_tags)
            .chain(&meta.script_tags)
            .chain(&meta.table_tags)
        {
            self.add_to_inverted_index(tag_to_u32(*tag), font_id)?;
        }

        // Add special markers for variable fonts.
        if meta.is_variable {
            self.add_to_inverted_index(tag_marker(b"_VAR"), font_id)?;
        }

        Ok(font_id)
    }

    /// Remove a font by its ID from all indices.
    fn remove_font_by_id(&mut self, font_id: FontID) -> Result<()> {
        self.index.db_metadata.delete(&mut self.wtxn, &font_id)?;
        Ok(())
    }

    /// Remove entries whose source files no longer exist on disk.
    /// Returns (before_count, after_count).
    pub fn prune_missing(&mut self) -> Result<(usize, usize)> {
        // Collect IDs of entries with missing paths.
        let mut to_remove = Vec::new();
        let before = self.index.db_metadata.len(&self.wtxn)? as usize;

        for result in self.index.db_metadata.iter(&self.wtxn)? {
            let (font_id, bytes) = result?;
            let meta = deserialize_meta(bytes)?;
            let path = Path::new(&meta.path);
            if !path.exists() {
                to_remove.push(font_id);
            }
        }

        // Remove missing entries.
        for font_id in &to_remove {
            self.index.db_metadata.delete(&mut self.wtxn, font_id)?;
        }

        // Also remove path-to-id mappings for missing files.
        // We need to scan the path_to_id database to clean up stale entries.
        let mut stale_hashes = Vec::new();
        for result in self.index.db_path_to_id.iter(&self.wtxn)? {
            let (hash, bytes) = result?;
            if bytes.len() == std::mem::size_of::<PathEntry>() {
                let entry: PathEntry = *bytemuck::from_bytes(bytes);
                if to_remove.contains(&entry.font_id) {
                    stale_hashes.push(hash);
                }
            }
        }

        for hash in stale_hashes {
            self.index.db_path_to_id.delete(&mut self.wtxn, &hash)?;
        }

        let after = self.index.db_metadata.len(&self.wtxn)? as usize;
        Ok((before, after))
    }

    /// Add a font ID to an inverted index bitmap.
    fn add_to_inverted_index(&mut self, tag: u32, font_id: FontID) -> Result<()> {
        let tag_bytes = tag.to_ne_bytes();
        let mut bitmap = if let Some(bytes) = self.index.db_inverted.get(&self.wtxn, &tag_bytes)? {
            RoaringBitmap::deserialize_from(bytes)?
        } else {
            RoaringBitmap::new()
        };

        bitmap.insert(font_id as u32);
        self.modified_tags.insert(tag);

        let mut buf = Vec::new();
        bitmap.serialize_into(&mut buf)?;
        self.index
            .db_inverted
            .put(&mut self.wtxn, &tag_bytes, &buf)?;

        Ok(())
    }

    /// Commit the transaction.
    pub fn commit(self) -> Result<()> {
        self.wtxn.commit()?;
        Ok(())
    }

    /// Abort the transaction without committing.
    pub fn abort(self) {
        self.wtxn.abort();
    }
}

/// Reader handle for index queries.
pub struct IndexReader<'a> {
    index: &'a FontIndex,
    rtxn: RoTxn<'a>,
}

impl<'a> IndexReader<'a> {
    /// Execute a query and return matching font faces.
    pub fn find(&self, query: &Query) -> Result<Vec<TypgFontFaceMatch>> {
        // Phase 1: Use inverted indices to get candidate bitmap.
        let candidates = self.get_candidate_bitmap(query)?;

        // Phase 2: Filter candidates and hydrate metadata.
        let mut matches = Vec::new();
        for font_id in candidates.iter() {
            if let Some(meta) = self.get_metadata(font_id as u64)? {
                if self.passes_filters(&meta, query)? {
                    matches.push(hydrate_match(&meta));
                }
            }
        }

        // Sort by path for deterministic output.
        matches.sort_by(|a, b| {
            a.source
                .path
                .cmp(&b.source.path)
                .then_with(|| a.source.ttc_index.cmp(&b.source.ttc_index))
        });

        Ok(matches)
    }

    /// List all indexed fonts.
    pub fn list_all(&self) -> Result<Vec<TypgFontFaceMatch>> {
        let mut matches = Vec::new();
        for result in self.index.db_metadata.iter(&self.rtxn)? {
            let (_, bytes) = result?;
            let meta = deserialize_meta(bytes)?;
            matches.push(hydrate_match(&meta));
        }

        matches.sort_by(|a, b| {
            a.source
                .path
                .cmp(&b.source.path)
                .then_with(|| a.source.ttc_index.cmp(&b.source.ttc_index))
        });

        Ok(matches)
    }

    /// Get the candidate bitmap by intersecting tag bitmaps.
    fn get_candidate_bitmap(&self, query: &Query) -> Result<RoaringBitmap> {
        let mut result: Option<RoaringBitmap> = None;

        // Intersect axis tag bitmaps.
        for tag in query.axes() {
            let bitmap = self.get_tag_bitmap(tag_to_u32(*tag))?;
            result = Some(intersect_optional(result, bitmap));
        }

        // Intersect feature tag bitmaps.
        for tag in query.features() {
            let bitmap = self.get_tag_bitmap(tag_to_u32(*tag))?;
            result = Some(intersect_optional(result, bitmap));
        }

        // Intersect script tag bitmaps.
        for tag in query.scripts() {
            let bitmap = self.get_tag_bitmap(tag_to_u32(*tag))?;
            result = Some(intersect_optional(result, bitmap));
        }

        // Intersect table tag bitmaps.
        for tag in query.tables() {
            let bitmap = self.get_tag_bitmap(tag_to_u32(*tag))?;
            result = Some(intersect_optional(result, bitmap));
        }

        // Require variable fonts if specified.
        if query.requires_variable() {
            let bitmap = self.get_tag_bitmap(tag_marker(b"_VAR"))?;
            result = Some(intersect_optional(result, bitmap));
        }

        // If no tag filters, return all fonts.
        match result {
            Some(bitmap) => Ok(bitmap),
            None => {
                let mut all = RoaringBitmap::new();
                for r in self.index.db_metadata.iter(&self.rtxn)? {
                    let (id, _) = r?;
                    all.insert(id as u32);
                }
                Ok(all)
            }
        }
    }

    /// Get the bitmap for a specific tag.
    fn get_tag_bitmap(&self, tag: u32) -> Result<RoaringBitmap> {
        let tag_bytes = tag.to_ne_bytes();
        if let Some(bytes) = self.index.db_inverted.get(&self.rtxn, &tag_bytes)? {
            Ok(RoaringBitmap::deserialize_from(bytes)?)
        } else {
            Ok(RoaringBitmap::new())
        }
    }

    /// Get metadata for a font ID.
    fn get_metadata(&self, font_id: FontID) -> Result<Option<IndexedFontMeta>> {
        if let Some(bytes) = self.index.db_metadata.get(&self.rtxn, &font_id)? {
            Ok(Some(deserialize_meta(bytes)?))
        } else {
            Ok(None)
        }
    }

    /// Check if metadata passes query filters that can't use inverted indices.
    fn passes_filters(&self, meta: &IndexedFontMeta, query: &Query) -> Result<bool> {
        // Name pattern filter.
        if !query.name_patterns().is_empty() {
            let matches_any = meta
                .names
                .iter()
                .any(|name| query.name_patterns().iter().any(|p| p.is_match(name)));
            if !matches_any {
                return Ok(false);
            }
        }

        // Weight range filter.
        if let Some(range) = query.weight_range() {
            if let Some(weight) = meta.weight_class {
                if !range.contains(&weight) {
                    return Ok(false);
                }
            } else {
                return Ok(false);
            }
        }

        // Width range filter.
        if let Some(range) = query.width_range() {
            if let Some(width) = meta.width_class {
                if !range.contains(&width) {
                    return Ok(false);
                }
            } else {
                return Ok(false);
            }
        }

        // Family class filter.
        if let Some(filter) = query.family_class() {
            if let Some((major, sub)) = meta.family_class {
                if !matches_family_class(major, sub, filter) {
                    return Ok(false);
                }
            } else {
                return Ok(false);
            }
        }

        // Codepoint/text filter using cmap bitmap.
        if !query.codepoints().is_empty() && !meta.cmap_bitmap.is_empty() {
            if let Ok(cmap) = RoaringBitmap::deserialize_from(meta.cmap_bitmap.as_slice()) {
                for &cp in query.codepoints() {
                    if !cmap.contains(cp as u32) {
                        return Ok(false);
                    }
                }
            }
        }

        Ok(true)
    }
}

/// Deserialize metadata from bytes.
fn deserialize_meta(bytes: &[u8]) -> Result<IndexedFontMeta> {
    bincode::deserialize(bytes).map_err(|e| anyhow::anyhow!("bincode deserialize: {e}"))
}

/// Convert metadata to TypgFontFaceMatch.
fn hydrate_match(meta: &IndexedFontMeta) -> TypgFontFaceMatch {
    use crate::search::{TypgFontFaceMeta, TypgFontSource};

    TypgFontFaceMatch {
        source: TypgFontSource {
            path: PathBuf::from(&meta.path),
            ttc_index: meta.ttc_index,
        },
        metadata: TypgFontFaceMeta {
            names: meta.names.clone(),
            axis_tags: Vec::new(),    // Not stored in indexed form
            feature_tags: Vec::new(), // Not stored in indexed form
            script_tags: Vec::new(),  // Not stored in indexed form
            table_tags: Vec::new(),   // Not stored in indexed form
            codepoints: Vec::new(),   // Stored as bitmap
            is_variable: meta.is_variable,
            weight_class: meta.weight_class,
            width_class: meta.width_class,
            family_class: meta.family_class,
            creator_names: Vec::new(), // Not stored in indexed form
            license_names: Vec::new(), // Not stored in indexed form
            psname: meta.psname.clone(),
            tfname: meta.tfname.clone(),
            lfname: meta.lfname.clone(),
            tsname: meta.tsname.clone(),
            lsname: meta.lsname.clone(),
        },
    }
}

/// Hash a path for the path-to-ID lookup.
fn hash_path(path: &Path) -> u64 {
    use xxhash_rust::xxh3::xxh3_64;
    xxh3_64(path.to_string_lossy().as_bytes())
}

/// Convert Tag to u32.
fn tag_to_u32(tag: Tag) -> u32 {
    u32::from_be_bytes(tag.into_bytes())
}

/// Create a marker tag for special indices.
fn tag_marker(name: &[u8; 4]) -> u32 {
    u32::from_be_bytes(*name)
}

/// Build a Roaring Bitmap from codepoints for efficient coverage checks.
fn build_cmap_bitmap(codepoints: &[char]) -> Vec<u8> {
    if codepoints.is_empty() {
        return Vec::new();
    }

    let mut bitmap = RoaringBitmap::new();
    for &cp in codepoints {
        bitmap.insert(cp as u32);
    }

    let mut buf = Vec::new();
    bitmap.serialize_into(&mut buf).unwrap_or_default();
    buf
}

/// Intersect an optional bitmap with another bitmap.
fn intersect_optional(opt: Option<RoaringBitmap>, other: RoaringBitmap) -> RoaringBitmap {
    match opt {
        Some(mut bm) => {
            bm &= &other;
            bm
        }
        None => other,
    }
}

/// Check if family class matches the filter.
fn matches_family_class(major: u8, sub: u8, filter: &FamilyClassFilter) -> bool {
    if major != filter.major {
        return false;
    }
    match filter.subclass {
        Some(expected_sub) => sub == expected_sub,
        None => true,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn make_mock_meta(
        names: Vec<String>,
        feature_tags: &[Tag],
        script_tags: &[Tag],
        codepoints: &[char],
        is_variable: bool,
        weight_class: Option<u16>,
        width_class: Option<u16>,
        family_class: Option<(u8, u8)>,
    ) -> TypgFontFaceMeta {
        TypgFontFaceMeta {
            names,
            axis_tags: Vec::new(),
            feature_tags: feature_tags.to_vec(),
            script_tags: script_tags.to_vec(),
            table_tags: Vec::new(),
            codepoints: codepoints.to_vec(),
            is_variable,
            weight_class,
            width_class,
            family_class,
            creator_names: Vec::new(),
            license_names: Vec::new(),
            psname: None,
            tfname: None,
            lfname: None,
            tsname: None,
            lsname: None,
        }
    }

    #[test]
    fn test_index_open_and_close() {
        let dir = TempDir::new().unwrap();
        let index = FontIndex::open(dir.path()).unwrap();
        assert_eq!(index.count().unwrap(), 0);
    }

    #[test]
    fn test_add_and_query_font() {
        let dir = TempDir::new().unwrap();
        let index = FontIndex::open(dir.path()).unwrap();

        // Add a font.
        let mut writer = index.writer().unwrap();
        let path = Path::new("/test/font.ttf");
        let mtime = SystemTime::UNIX_EPOCH;

        let meta = make_mock_meta(
            vec!["Test Font".to_string()],
            &[Tag::new(b"smcp")],
            &[Tag::new(b"latn")],
            &['a', 'b', 'c'],
            false,
            Some(400),
            Some(5),
            Some((8, 1)),
        );

        writer.add_font(path, None, mtime, &meta).unwrap();
        writer.commit().unwrap();

        assert_eq!(index.count().unwrap(), 1);

        // Query with feature filter.
        let reader = index.reader().unwrap();
        let query = Query::new().with_features(vec![Tag::new(b"smcp")]);
        let matches = reader.find(&query).unwrap();
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].source.path, path);
    }

    #[test]
    fn test_incremental_update() {
        let dir = TempDir::new().unwrap();
        let index = FontIndex::open(dir.path()).unwrap();

        let path = Path::new("/test/font.ttf");
        let mtime1 = SystemTime::UNIX_EPOCH;
        let mtime2 = SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1000);

        // First add.
        {
            let mut writer = index.writer().unwrap();
            assert!(writer.needs_update(path, mtime1).unwrap());
            let meta = make_mock_meta(
                vec!["V1".to_string()],
                &[],
                &[],
                &[],
                false,
                None,
                None,
                None,
            );
            writer.add_font(path, None, mtime1, &meta).unwrap();
            writer.commit().unwrap();
        }

        // Same mtime - no update needed.
        {
            let writer = index.writer().unwrap();
            assert!(!writer.needs_update(path, mtime1).unwrap());
            writer.abort();
        }

        // Different mtime - update needed.
        {
            let writer = index.writer().unwrap();
            assert!(writer.needs_update(path, mtime2).unwrap());
            writer.abort();
        }
    }

    #[test]
    fn test_bitmap_intersection() {
        let dir = TempDir::new().unwrap();
        let index = FontIndex::open(dir.path()).unwrap();

        // Add two fonts with different features.
        {
            let mut writer = index.writer().unwrap();
            let meta1 = make_mock_meta(
                vec!["Font1".to_string()],
                &[Tag::new(b"smcp"), Tag::new(b"liga")],
                &[],
                &[],
                false,
                None,
                None,
                None,
            );
            writer
                .add_font(
                    Path::new("/font1.ttf"),
                    None,
                    SystemTime::UNIX_EPOCH,
                    &meta1,
                )
                .unwrap();
            let meta2 = make_mock_meta(
                vec!["Font2".to_string()],
                &[Tag::new(b"smcp")],
                &[],
                &[],
                false,
                None,
                None,
                None,
            );
            writer
                .add_font(
                    Path::new("/font2.ttf"),
                    None,
                    SystemTime::UNIX_EPOCH,
                    &meta2,
                )
                .unwrap();
            writer.commit().unwrap();
        }

        let reader = index.reader().unwrap();

        // Query for smcp only - should find both.
        let q1 = Query::new().with_features(vec![Tag::new(b"smcp")]);
        assert_eq!(reader.find(&q1).unwrap().len(), 2);

        // Query for smcp AND liga - should find only font1.
        let q2 = Query::new().with_features(vec![Tag::new(b"smcp"), Tag::new(b"liga")]);
        let matches = reader.find(&q2).unwrap();
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].source.path, Path::new("/font1.ttf"));
    }

    #[test]
    fn test_cmap_bitmap() {
        let codepoints = vec!['a', 'b', 'c', 'ñ', ''];
        let bitmap_bytes = build_cmap_bitmap(&codepoints);
        let bitmap = RoaringBitmap::deserialize_from(bitmap_bytes.as_slice()).unwrap();

        // Should contain all original codepoints.
        for &cp in &codepoints {
            assert!(bitmap.contains(cp as u32));
        }

        // Should not contain a codepoint we didn't add.
        assert!(!bitmap.contains('z' as u32));
    }

    #[test]
    fn test_prune_missing() {
        let dir = TempDir::new().unwrap();
        let index = FontIndex::open(dir.path()).unwrap();

        // Add two fonts: one with a real path, one with a fake path.
        {
            let mut writer = index.writer().unwrap();
            // This path doesn't exist.
            let meta_missing = make_mock_meta(
                vec!["Missing".to_string()],
                &[],
                &[],
                &[],
                false,
                None,
                None,
                None,
            );
            writer
                .add_font(
                    Path::new("/nonexistent/font.ttf"),
                    None,
                    SystemTime::UNIX_EPOCH,
                    &meta_missing,
                )
                .unwrap();

            // Use an existing file (any test file that exists).
            let existing_path = Path::new(env!("CARGO_MANIFEST_DIR"));
            let meta_existing = make_mock_meta(
                vec!["Existing".to_string()],
                &[],
                &[],
                &[],
                false,
                None,
                None,
                None,
            );
            writer
                .add_font(existing_path, None, SystemTime::UNIX_EPOCH, &meta_existing)
                .unwrap();
            writer.commit().unwrap();
        }

        assert_eq!(index.count().unwrap(), 2);

        // Prune missing entries.
        {
            let mut writer = index.writer().unwrap();
            let (before, after) = writer.prune_missing().unwrap();
            writer.commit().unwrap();

            assert_eq!(before, 2);
            assert_eq!(after, 1);
        }

        // Verify only the existing entry remains.
        assert_eq!(index.count().unwrap(), 1);
        let reader = index.reader().unwrap();
        let entries = reader.list_all().unwrap();
        assert_eq!(entries.len(), 1);
        assert!(entries[0]
            .metadata
            .names
            .iter()
            .any(|n| n.contains("Existing")));
    }
}