webfont-generator 0.2.3

Generate webfonts (SVG, TTF, EOT, WOFF, WOFF2) from SVG icons
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
use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;
use std::sync::{Mutex, OnceLock};

#[cfg(feature = "napi")]
use napi::bindgen_prelude::Uint8Array;
#[cfg(feature = "napi")]
use napi_derive::napi;
use serde_json::{Map, Value};

use crate::templates::{
    SharedTemplateData, build_css_context, build_html_context, build_html_registry, make_src,
    render_css_with_hbs_context, render_css_with_src_mutate, render_default_html_with_styles,
    render_html_with_hbs_context,
};
use crate::util::to_io_err;

#[cfg_attr(feature = "napi", napi(string_enum = "lowercase"))]
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum FontType {
    Svg,
    Ttf,
    Eot,
    Woff,
    Woff2,
}

impl FontType {
    /// Returns the CSS `format()` value (e.g., "truetype", "woff2").
    #[inline]
    pub fn css_format(self) -> &'static str {
        match self {
            FontType::Svg => "svg",
            FontType::Ttf => "truetype",
            FontType::Eot => "embedded-opentype",
            FontType::Woff => "woff",
            FontType::Woff2 => "woff2",
        }
    }

    /// Returns the file extension (e.g., "svg", "ttf", "woff2").
    #[inline]
    pub fn as_extension(self) -> &'static str {
        match self {
            FontType::Svg => "svg",
            FontType::Ttf => "ttf",
            FontType::Eot => "eot",
            FontType::Woff => "woff",
            FontType::Woff2 => "woff2",
        }
    }
}

#[cfg_attr(feature = "napi", napi(object))]
#[derive(Clone, Default)]
pub struct SvgFormatOptions {
    pub center_vertically: Option<bool>,
    pub font_id: Option<String>,
    pub metadata: Option<String>,
    pub optimize_output: Option<bool>,
    pub preserve_aspect_ratio: Option<bool>,
}

#[cfg_attr(feature = "napi", napi(object))]
#[derive(Clone)]
pub struct TtfFormatOptions {
    pub copyright: Option<String>,
    pub description: Option<String>,
    pub ts: Option<i64>,
    pub url: Option<String>,
    pub version: Option<String>,
}

#[cfg_attr(feature = "napi", napi(object))]
#[derive(Clone)]
pub struct WoffFormatOptions {
    pub metadata: Option<String>,
}

#[cfg_attr(feature = "napi", napi(object))]
#[derive(Clone, Default)]
pub struct FormatOptions {
    pub svg: Option<SvgFormatOptions>,
    pub ttf: Option<TtfFormatOptions>,
    pub woff: Option<WoffFormatOptions>,
}

#[cfg_attr(feature = "napi", napi(object))]
#[derive(Clone, Default)]
pub struct GenerateWebfontsOptions {
    pub ascent: Option<f64>,
    pub center_horizontally: Option<bool>,
    pub center_vertically: Option<bool>,
    pub css: Option<bool>,
    pub css_dest: Option<String>,
    pub css_template: Option<String>,
    pub codepoints: Option<HashMap<String, u32>>,
    pub css_fonts_url: Option<String>,
    pub descent: Option<f64>,
    pub dest: String,
    pub files: Vec<String>,
    pub fixed_width: Option<bool>,
    pub format_options: Option<FormatOptions>,
    pub html: Option<bool>,
    pub html_dest: Option<String>,
    pub html_template: Option<String>,
    pub font_height: Option<f64>,
    pub font_name: Option<String>,
    pub font_style: Option<String>,
    pub font_weight: Option<String>,
    pub ligature: Option<bool>,
    pub normalize: Option<bool>,
    pub order: Option<Vec<FontType>>,
    pub optimize_output: Option<bool>,
    pub preserve_aspect_ratio: Option<bool>,
    pub round: Option<f64>,
    pub start_codepoint: Option<u32>,
    pub template_options: Option<Map<String, Value>>,
    pub types: Option<Vec<FontType>>,
    pub write_files: Option<bool>,
}

pub(crate) const DEFAULT_FONT_TYPES: [FontType; 3] =
    [FontType::Eot, FontType::Woff, FontType::Woff2];

pub(crate) const DEFAULT_FONT_ORDER: [FontType; 5] = [
    FontType::Eot,
    FontType::Woff2,
    FontType::Woff,
    FontType::Ttf,
    FontType::Svg,
];

pub(crate) fn resolved_font_types(options: &GenerateWebfontsOptions) -> Vec<FontType> {
    match &options.types {
        Some(types) => types.clone(),
        None => DEFAULT_FONT_TYPES.to_vec(),
    }
}

pub(crate) struct ResolvedGenerateWebfontsOptions {
    pub ascent: Option<f64>,
    pub center_horizontally: Option<bool>,
    pub center_vertically: Option<bool>,
    pub css: bool,
    pub css_dest: String,
    pub css_template: Option<String>,
    pub codepoints: BTreeMap<String, u32>,
    pub css_fonts_url: Option<String>,
    pub descent: Option<f64>,
    pub dest: String,
    pub files: Vec<String>,
    pub fixed_width: Option<bool>,
    pub format_options: Option<FormatOptions>,
    pub html: bool,
    pub html_dest: String,
    pub html_template: Option<String>,
    pub font_height: Option<f64>,
    pub font_name: String,
    pub font_style: Option<String>,
    pub font_weight: Option<String>,
    pub ligature: bool,
    pub normalize: bool,
    pub order: Vec<FontType>,
    pub optimize_output: Option<bool>,
    pub preserve_aspect_ratio: Option<bool>,
    pub round: Option<f64>,
    pub start_codepoint: u32,
    pub template_options: Option<Map<String, Value>>,
    pub types: Vec<FontType>,
    pub write_files: bool,
}

pub(crate) struct LoadedSvgFile {
    pub contents: String,
    pub glyph_name: String,
    pub path: String,
}

/// Caches the last rendered CSS/HTML result for repeated calls with the same urls.
#[derive(Default)]
pub(crate) struct RenderCache {
    /// Result of generateCss() with no urls (computed once).
    css_no_urls: Option<String>,
    /// Last generateCss(urls) result.
    css_last_urls: Option<HashMap<FontType, String>>,
    css_last_result: Option<String>,
    /// Result of generateHtml() with no urls (computed once).
    html_no_urls: Option<String>,
    /// Last generateHtml(urls) result.
    html_last_urls: Option<HashMap<FontType, String>>,
    html_last_result: Option<String>,
}

pub(crate) struct CachedTemplateData {
    pub shared: SharedTemplateData,
    pub css_context: Map<String, Value>,
    pub css_hbs_context: Mutex<handlebars::Context>,
    pub html_context: Map<String, Value>,
    pub html_hbs_context: Mutex<handlebars::Context>,
    pub html_registry: Option<handlebars::Handlebars<'static>>,
    pub(crate) render_cache: Mutex<RenderCache>,
}

#[cfg_attr(feature = "napi", napi)]
pub struct GenerateWebfontsResult {
    pub(crate) css_context: Option<Map<String, Value>>,
    pub(crate) eot_font: Option<Arc<Vec<u8>>>,
    pub(crate) html_context: Option<Map<String, Value>>,
    pub(crate) options: ResolvedGenerateWebfontsOptions,
    pub(crate) source_files: Vec<LoadedSvgFile>,
    pub(crate) svg_font: Option<Arc<String>>,
    pub(crate) ttf_font: Option<Arc<Vec<u8>>>,
    pub(crate) woff2_font: Option<Arc<Vec<u8>>>,
    pub(crate) woff_font: Option<Arc<Vec<u8>>>,
    pub(crate) cached: OnceLock<Result<CachedTemplateData, String>>,
}

// Pure Rust getters (always available)
impl GenerateWebfontsResult {
    /// Returns the EOT font bytes, if generated.
    pub fn eot_bytes(&self) -> Option<&[u8]> {
        self.eot_font.as_ref().map(|v| v.as_ref().as_slice())
    }

    /// Returns the SVG font string, if generated.
    pub fn svg_string(&self) -> Option<&str> {
        self.svg_font.as_ref().map(|v| v.as_ref().as_str())
    }

    /// Returns the TTF font bytes, if generated.
    pub fn ttf_bytes(&self) -> Option<&[u8]> {
        self.ttf_font.as_ref().map(|v| v.as_ref().as_slice())
    }

    /// Returns the WOFF font bytes, if generated.
    pub fn woff_bytes(&self) -> Option<&[u8]> {
        self.woff_font.as_ref().map(|v| v.as_ref().as_slice())
    }

    /// Returns the WOFF2 font bytes, if generated.
    pub fn woff2_bytes(&self) -> Option<&[u8]> {
        self.woff2_font.as_ref().map(|v| v.as_ref().as_slice())
    }

    pub(crate) fn get_cached_io(&self) -> std::io::Result<&CachedTemplateData> {
        self.cached
            .get_or_init(|| {
                let shared = SharedTemplateData::new(&self.options, &self.source_files)
                    .map_err(|e| e.to_string())?;
                let css_context = match &self.css_context {
                    Some(ctx) => ctx.clone(),
                    None => build_css_context(&self.options, &shared),
                };
                let html_context = match &self.html_context {
                    Some(ctx) => ctx.clone(),
                    None => build_html_context(&self.options, &shared, &self.source_files, None)
                        .map_err(|e| e.to_string())?,
                };
                let html_registry =
                    build_html_registry(&self.options).map_err(|e| e.to_string())?;
                let css_hbs_context =
                    handlebars::Context::wraps(&css_context).map_err(|e| e.to_string())?;
                let html_hbs_context =
                    handlebars::Context::wraps(&html_context).map_err(|e| e.to_string())?;
                Ok(CachedTemplateData {
                    shared,
                    css_context,
                    css_hbs_context: Mutex::new(css_hbs_context),
                    html_context,
                    html_hbs_context: Mutex::new(html_hbs_context),
                    html_registry,
                    render_cache: Mutex::new(RenderCache::default()),
                })
            })
            .as_ref()
            .map_err(to_io_err)
    }

    /// Generate a CSS string for this webfont result.
    ///
    /// Pass `urls` to override the default font URLs in the CSS output.
    pub fn generate_css_pure(
        &self,
        urls: Option<HashMap<FontType, String>>,
    ) -> std::io::Result<String> {
        let cached = self.get_cached_io()?;
        let mut rc = cached.render_cache.lock().unwrap();

        match &urls {
            None => {
                if let Some(result) = &rc.css_no_urls {
                    return Ok(result.clone());
                }
                let ctx = cached.css_hbs_context.lock().unwrap();
                let result =
                    render_css_with_hbs_context(&cached.shared, &ctx, &cached.css_context)?;
                rc.css_no_urls = Some(result.clone());
                Ok(result)
            }
            Some(urls) => {
                // If the template doesn't reference {{src}}, URLs don't affect output
                if !cached.shared.css_template_uses_src {
                    drop(rc);
                    return self.generate_css_pure(None);
                }
                if rc.css_last_urls.as_ref() == Some(urls)
                    && let Some(result) = &rc.css_last_result
                {
                    return Ok(result.clone());
                }
                let src = make_src(&self.options, urls);
                let mut ctx = cached.css_hbs_context.lock().unwrap();
                let result = render_css_with_src_mutate(
                    &cached.shared,
                    &mut ctx,
                    &cached.css_context,
                    &src,
                )?;
                rc.css_last_urls = Some(urls.clone());
                rc.css_last_result = Some(result.clone());
                Ok(result)
            }
        }
    }

    /// Generate an HTML string for this webfont result.
    ///
    /// Pass `urls` to override the default font URLs in the HTML output.
    pub fn generate_html_pure(
        &self,
        urls: Option<HashMap<FontType, String>>,
    ) -> std::io::Result<String> {
        let cached = self.get_cached_io()?;
        let mut rc = cached.render_cache.lock().unwrap();

        match &urls {
            None => {
                if let Some(result) = &rc.html_no_urls {
                    return Ok(result.clone());
                }
                let ctx = cached.html_hbs_context.lock().unwrap();
                let result = render_html_with_hbs_context(
                    cached.html_registry.as_ref(),
                    &ctx,
                    &cached.html_context,
                )?;
                rc.html_no_urls = Some(result.clone());
                Ok(result)
            }
            Some(urls) => {
                // If the CSS template doesn't reference {{src}}, URLs don't affect output
                if !cached.shared.css_template_uses_src {
                    drop(rc);
                    return self.generate_html_pure(None);
                }
                if rc.html_last_urls.as_ref() == Some(urls)
                    && let Some(result) = &rc.html_last_result
                {
                    return Ok(result.clone());
                }
                // Render CSS with the custom URLs (in-place src mutate, no clone)
                let src = make_src(&self.options, urls);
                let styles = {
                    let mut css_ctx = cached.css_hbs_context.lock().unwrap();
                    render_css_with_src_mutate(
                        &cached.shared,
                        &mut css_ctx,
                        &cached.css_context,
                        &src,
                    )?
                };
                // Hot path: default HTML template -- inject styles directly, skip clone
                if self.options.html_template.is_none() {
                    let result = render_default_html_with_styles(&cached.html_context, &styles);
                    rc.html_last_urls = Some(urls.clone());
                    rc.html_last_result = Some(result.clone());
                    return Ok(result);
                }
                // Custom HTML template: in-place styles mutate, no clone
                let mut html_ctx = cached.html_hbs_context.lock().unwrap();
                let registry = cached
                    .html_registry
                    .as_ref()
                    .expect("HTML registry should exist for custom template");
                let result = crate::util::render_with_field_swap(
                    &mut html_ctx,
                    "styles",
                    serde_json::Value::String(styles),
                    |ctx| {
                        registry
                            .render_with_context("html", ctx)
                            .map_err(crate::util::to_io_err)
                    },
                )?;
                rc.html_last_urls = Some(urls.clone());
                rc.html_last_result = Some(result.clone());
                Ok(result)
            }
        }
    }
}

// NAPI getters and methods
#[cfg(feature = "napi")]
#[napi]
impl GenerateWebfontsResult {
    #[napi(getter)]
    pub fn eot(&self) -> Option<Uint8Array> {
        self.eot_font
            .as_ref()
            .map(|v| Uint8Array::from(v.as_ref().clone()))
    }

    #[napi(getter)]
    pub fn svg(&self) -> Option<String> {
        self.svg_font.as_ref().map(|v| v.as_ref().clone())
    }

    #[napi(getter)]
    pub fn ttf(&self) -> Option<Uint8Array> {
        self.ttf_font
            .as_ref()
            .map(|v| Uint8Array::from(v.as_ref().clone()))
    }

    #[napi(getter)]
    pub fn woff2(&self) -> Option<Uint8Array> {
        self.woff2_font
            .as_ref()
            .map(|v| Uint8Array::from(v.as_ref().clone()))
    }

    #[napi(getter)]
    pub fn woff(&self) -> Option<Uint8Array> {
        self.woff_font
            .as_ref()
            .map(|v| Uint8Array::from(v.as_ref().clone()))
    }

    #[napi(ts_args_type = "urls?: Partial<Record<FontType, string>>")]
    pub fn generate_css(&self, urls: Option<HashMap<String, String>>) -> napi::Result<String> {
        let urls = urls.map(parse_native_urls).transpose()?;
        self.generate_css_pure(urls)
            .map_err(crate::util::to_napi_err)
    }

    #[napi(ts_args_type = "urls?: Partial<Record<FontType, string>>")]
    pub fn generate_html(&self, urls: Option<HashMap<String, String>>) -> napi::Result<String> {
        let urls = urls.map(parse_native_urls).transpose()?;
        self.generate_html_pure(urls)
            .map_err(crate::util::to_napi_err)
    }
}

#[cfg(feature = "napi")]
fn parse_native_urls(urls: HashMap<String, String>) -> napi::Result<HashMap<FontType, String>> {
    urls.into_iter()
        .filter_map(|(font_type, url)| {
            let font_type = match font_type.as_str() {
                "svg" => Some(FontType::Svg),
                "ttf" => Some(FontType::Ttf),
                "eot" => Some(FontType::Eot),
                "woff" => Some(FontType::Woff),
                "woff2" => Some(FontType::Woff2),
                _ => None,
            }?;

            Some(Ok((font_type, url)))
        })
        .collect::<napi::Result<HashMap<FontType, String>>>()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{finalize_generate_webfonts_options, resolve_generate_webfonts_options};

    fn build_result(template: Option<&str>) -> GenerateWebfontsResult {
        let fixture = crate::test_helpers::webfont_fixture("add.svg");

        let mut css_template = None;
        let cleanup_dir;
        if let Some(content) = template {
            let tmp = std::env::temp_dir().join(format!(
                "render-cache-test-{}",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .as_nanos()
            ));
            std::fs::create_dir_all(&tmp).unwrap();
            let path = tmp.join("template.hbs");
            std::fs::write(&path, content).unwrap();
            css_template = Some(path.to_string_lossy().into_owned());
            cleanup_dir = Some(tmp);
        } else {
            cleanup_dir = None;
        }

        let options = GenerateWebfontsOptions {
            css: Some(true),
            css_template,
            codepoints: Some(HashMap::from([("add".to_owned(), 0xE001u32)])),
            dest: "artifacts".to_owned(),
            files: vec![fixture],
            html: Some(false),
            font_name: Some("iconfont".to_owned()),
            ligature: Some(false),
            order: Some(vec![FontType::Svg]),
            start_codepoint: Some(0xE001),
            types: Some(vec![FontType::Svg]),
            ..Default::default()
        };

        let mut resolved = resolve_generate_webfonts_options(options).unwrap();
        let source_files: Vec<LoadedSvgFile> = resolved
            .files
            .iter()
            .map(|path| LoadedSvgFile {
                contents: std::fs::read_to_string(path).unwrap(),
                glyph_name: std::path::Path::new(path)
                    .file_stem()
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .to_owned(),
                path: path.clone(),
            })
            .collect();
        finalize_generate_webfonts_options(&mut resolved, &source_files).unwrap();

        let result = GenerateWebfontsResult {
            cached: std::sync::OnceLock::new(),
            css_context: None,
            eot_font: None,
            html_context: None,
            options: resolved,
            source_files,
            svg_font: None,
            ttf_font: None,
            woff2_font: None,
            woff_font: None,
        };

        if let Some(dir) = cleanup_dir {
            // Don't clean up yet -- template file needed for lazy compilation
            std::mem::forget(dir);
        }

        result
    }

    #[test]
    fn generate_css_returns_cached_result_on_repeated_calls_without_urls() {
        let result = build_result(None);

        let first = result.generate_css_pure(None).unwrap();
        let second = result.generate_css_pure(None).unwrap();

        assert_eq!(first, second);
        assert!(!first.is_empty());
    }

    #[test]
    fn generate_css_returns_cached_result_on_repeated_calls_with_same_urls() {
        let result = build_result(None);
        let urls = HashMap::from([(FontType::Svg, "/a.svg".to_owned())]);

        let first = result.generate_css_pure(Some(urls.clone())).unwrap();
        let second = result.generate_css_pure(Some(urls)).unwrap();

        assert_eq!(first, second);
        assert!(first.contains("/a.svg"));
    }

    #[test]
    fn generate_css_returns_different_result_for_different_urls() {
        let result = build_result(None);
        let urls_a = HashMap::from([(FontType::Svg, "/a.svg".to_owned())]);
        let urls_b = HashMap::from([(FontType::Svg, "/b.svg".to_owned())]);

        let result_a = result.generate_css_pure(Some(urls_a)).unwrap();
        let result_b = result.generate_css_pure(Some(urls_b)).unwrap();

        assert_ne!(result_a, result_b);
        assert!(result_a.contains("/a.svg"));
        assert!(result_b.contains("/b.svg"));
    }

    #[test]
    fn generate_css_cache_updates_when_urls_change() {
        let result = build_result(None);
        let urls_a = HashMap::from([(FontType::Svg, "/a.svg".to_owned())]);
        let urls_b = HashMap::from([(FontType::Svg, "/b.svg".to_owned())]);

        let first_a = result.generate_css_pure(Some(urls_a.clone())).unwrap();
        let first_b = result.generate_css_pure(Some(urls_b)).unwrap();
        let second_a = result.generate_css_pure(Some(urls_a)).unwrap();

        assert_eq!(
            first_a, second_a,
            "returning to original urls should produce same result"
        );
        assert_ne!(first_a, first_b);
    }

    #[test]
    fn generate_css_cache_works_with_custom_template() {
        let result = build_result(Some("@font-face { src: {{{src}}}; }"));
        let urls = HashMap::from([(FontType::Svg, "/cached.svg".to_owned())]);

        let first = result.generate_css_pure(Some(urls.clone())).unwrap();
        let second = result.generate_css_pure(Some(urls)).unwrap();

        assert_eq!(first, second);
        assert!(first.contains("/cached.svg"));
    }

    #[test]
    fn generate_css_no_urls_and_with_urls_are_independent_caches() {
        let result = build_result(None);
        let urls = HashMap::from([(FontType::Svg, "/custom.svg".to_owned())]);

        let no_urls = result.generate_css_pure(None).unwrap();
        let with_urls = result.generate_css_pure(Some(urls)).unwrap();
        let no_urls_again = result.generate_css_pure(None).unwrap();

        assert_eq!(
            no_urls, no_urls_again,
            "no-urls cache should survive a with-urls call"
        );
        assert_ne!(no_urls, with_urls);
    }

    #[test]
    fn generate_css_with_urls_returns_no_urls_result_when_template_does_not_use_src() {
        let result = build_result(Some(".icon { font-family: {{fontName}}; }"));
        let urls = HashMap::from([(FontType::Svg, "/should-not-appear.svg".to_owned())]);

        let no_urls = result.generate_css_pure(None).unwrap();
        let with_urls = result.generate_css_pure(Some(urls)).unwrap();

        assert_eq!(
            no_urls, with_urls,
            "template without {{src}} should ignore urls"
        );
        assert!(!with_urls.contains("/should-not-appear.svg"));
        assert!(
            with_urls.contains("iconfont"),
            "should still render the template"
        );
    }

    #[test]
    fn generate_html_with_urls_returns_no_urls_result_when_css_template_does_not_use_src() {
        let result = build_result(Some(".icon { font-family: {{fontName}}; }"));
        let urls = HashMap::from([(FontType::Svg, "/should-not-appear.svg".to_owned())]);

        let no_urls = result.generate_html_pure(None).unwrap();
        let with_urls = result.generate_html_pure(Some(urls)).unwrap();

        assert_eq!(
            no_urls, with_urls,
            "CSS template without {{src}} means HTML is also unaffected by urls"
        );
    }

    #[test]
    fn generate_css_without_urls_produces_valid_css_using_css_fonts_url() {
        let result = build_result(None);

        let css = result.generate_css_pure(None).unwrap();

        assert!(
            css.contains("@font-face"),
            "should contain @font-face declaration"
        );
        assert!(css.contains("font-family:"), "should contain font-family");
        assert!(
            css.contains("iconfont.svg?"),
            "should use font name in URL with hash"
        );
        assert!(
            css.contains("format(\"svg\")"),
            "should contain format declaration"
        );
        assert!(
            css.contains("content:"),
            "should contain codepoint content rules"
        );
    }

    #[test]
    fn generate_css_with_urls_replaces_default_urls_in_src() {
        let result = build_result(None);
        let urls = HashMap::from([(FontType::Svg, "/cdn/icons.svg".to_owned())]);

        let css = result.generate_css_pure(Some(urls)).unwrap();

        assert!(
            css.contains("/cdn/icons.svg"),
            "custom URL should appear in output"
        );
        assert!(
            !css.contains("iconfont.svg?"),
            "default hash-based URL should not appear"
        );
        assert!(
            css.contains("format(\"svg\")"),
            "format should still be present"
        );
    }

    #[test]
    fn generate_html_without_urls_produces_valid_html() {
        let result = build_result(None);

        let html = result.generate_html_pure(None).unwrap();

        assert!(
            html.contains("<!DOCTYPE html>"),
            "should be a full HTML document"
        );
        assert!(html.contains("iconfont"), "should contain font name");
        assert!(html.contains("icon-add"), "should contain icon class name");
    }

    #[test]
    fn generate_html_with_urls_embeds_css_using_custom_urls() {
        let result = build_result(None);
        let urls = HashMap::from([(FontType::Svg, "/cdn/icons.svg".to_owned())]);

        let html = result.generate_html_pure(Some(urls)).unwrap();

        assert!(
            html.contains("/cdn/icons.svg"),
            "custom URL should appear in embedded CSS"
        );
        assert!(
            html.contains("icon-add"),
            "should still contain icon class name"
        );
    }

    #[test]
    fn generate_html_cache_returns_same_result_for_same_urls() {
        let result = build_result(None);
        let urls = HashMap::from([(FontType::Svg, "/cached.svg".to_owned())]);

        let first = result.generate_html_pure(Some(urls.clone())).unwrap();
        let second = result.generate_html_pure(Some(urls)).unwrap();

        assert_eq!(first, second);
        assert!(first.contains("/cached.svg"));
    }

    #[test]
    fn generate_html_cache_returns_different_result_for_different_urls() {
        let result = build_result(None);
        let urls_a = HashMap::from([(FontType::Svg, "/a.svg".to_owned())]);
        let urls_b = HashMap::from([(FontType::Svg, "/b.svg".to_owned())]);

        let result_a = result.generate_html_pure(Some(urls_a)).unwrap();
        let result_b = result.generate_html_pure(Some(urls_b)).unwrap();

        assert_ne!(result_a, result_b);
        assert!(result_a.contains("/a.svg"));
        assert!(result_b.contains("/b.svg"));
    }

    /// Build a result with multiple font types (svg + woff2) for testing partial URL overrides.
    fn build_multi_type_result() -> GenerateWebfontsResult {
        let fixture = crate::test_helpers::webfont_fixture("add.svg");
        let options = GenerateWebfontsOptions {
            css: Some(true),
            codepoints: Some(HashMap::from([("add".to_owned(), 0xE001u32)])),
            dest: "artifacts".to_owned(),
            files: vec![fixture],
            html: Some(true),
            font_name: Some("iconfont".to_owned()),
            ligature: Some(false),
            order: Some(vec![FontType::Woff2, FontType::Svg]),
            start_codepoint: Some(0xE001),
            types: Some(vec![FontType::Svg, FontType::Woff2]),
            ..Default::default()
        };

        let mut resolved = resolve_generate_webfonts_options(options).unwrap();
        let source_files: Vec<LoadedSvgFile> = resolved
            .files
            .iter()
            .map(|path| LoadedSvgFile {
                contents: std::fs::read_to_string(path).unwrap(),
                glyph_name: std::path::Path::new(path)
                    .file_stem()
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .to_owned(),
                path: path.clone(),
            })
            .collect();
        finalize_generate_webfonts_options(&mut resolved, &source_files).unwrap();

        GenerateWebfontsResult {
            cached: std::sync::OnceLock::new(),
            css_context: None,
            eot_font: None,
            html_context: None,
            options: resolved,
            source_files,
            svg_font: None,
            ttf_font: None,
            woff2_font: None,
            woff_font: None,
        }
    }

    #[test]
    fn generate_css_partial_urls_uses_empty_string_for_missing_types() {
        let result = build_multi_type_result();
        // Override only woff2, leave svg un-provided -- matches upstream behavior
        let urls = HashMap::from([(FontType::Woff2, "/cdn/font.woff2".to_owned())]);

        let css = result.generate_css_pure(Some(urls)).unwrap();

        assert!(
            css.contains("/cdn/font.woff2"),
            "overridden URL should appear"
        );
        assert!(
            !css.contains("iconfont.svg?"),
            "non-overridden type should not have default hash-based URL"
        );
        assert!(
            css.contains("url(\"#iconfont\")"),
            "non-overridden SVG type should produce empty base URL (upstream compat)"
        );
    }

    #[test]
    fn generate_html_partial_urls_uses_empty_string_for_missing_types() {
        let result = build_multi_type_result();
        let urls = HashMap::from([(FontType::Woff2, "/cdn/font.woff2".to_owned())]);

        let html = result.generate_html_pure(Some(urls)).unwrap();

        assert!(
            html.contains("/cdn/font.woff2"),
            "overridden URL should appear in HTML"
        );
        assert!(
            !html.contains("iconfont.svg?"),
            "non-overridden type should not have default hash-based URL in HTML"
        );
    }
}