webfont-generator 0.8.0

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
#[cfg(test)]
mod tests;

use std::collections::{BTreeMap, HashSet};
use std::io::{Error, ErrorKind};
use std::path::Path;

use super::files::LoadedSvgFile;
use crate::types::{
    FontType, FontVariant, FormatOptions, GenerateWebfontsOptions, MissingGlyphBehavior,
    MissingGlyphOptions,
};

#[derive(Clone)]
#[allow(
    dead_code,
    reason = "variant metadata is consumed by later generation phases"
)]
pub(crate) struct ResolvedVariants {
    pub variants: Vec<ResolvedFontVariant>,
    pub default_index: usize,
}

#[derive(Clone)]
#[allow(
    dead_code,
    reason = "variant metadata is consumed by later generation phases"
)]
pub(crate) struct ResolvedFontVariant {
    pub name: String,
    pub files: Vec<String>,
    pub weight: u16,
    pub class_name: String,
    pub selector: String,
}

#[derive(Clone)]
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>,
    /// Fully-resolved codepoints for the current glyph set (explicit + auto-assigned). Rebuilt
    /// by `finalize_generate_webfonts_options` from `explicit_codepoints` whenever the set changes.
    pub codepoints: BTreeMap<String, u32>,
    /// The user-supplied codepoints, kept as the stable base so re-resolving after an
    /// incremental add/remove assigns the same codepoints a fresh build would.
    pub explicit_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 incremental: 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 missing_glyphs: MissingGlyphOptions,
    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<serde_json::Map<String, serde_json::Value>>,
    pub types: Vec<FontType>,
    #[allow(
        dead_code,
        reason = "variant metadata is consumed by later generation phases"
    )]
    pub variants: Option<ResolvedVariants>,
    pub write_files: bool,
}

const DEFAULT_FONT_TYPES: [FontType; 3] = [FontType::Eot, FontType::Woff, FontType::Woff2];

const DEFAULT_FONT_ORDER: [FontType; 5] = [
    FontType::Eot,
    FontType::Woff2,
    FontType::Woff,
    FontType::Ttf,
    FontType::Svg,
];

pub(crate) fn validate_generate_webfonts_options(
    options: &GenerateWebfontsOptions,
) -> std::io::Result<()> {
    if options.dest.is_empty() {
        return Err(std::io::Error::new(
            ErrorKind::InvalidInput,
            "\"options.dest\" is empty.".to_owned(),
        ));
    }

    match options.variants.as_deref() {
        None => {
            if options.files.is_empty() {
                return Err(std::io::Error::new(
                    ErrorKind::InvalidInput,
                    "\"options.files\" is empty.".to_owned(),
                ));
            }
            if options.missing_glyphs.is_some() {
                return Err(std::io::Error::new(
                    ErrorKind::InvalidInput,
                    "\"options.missingGlyphs\" requires \"options.variants\".",
                ));
            }
            if options.variant_class_prefix.is_some() {
                return Err(std::io::Error::new(
                    ErrorKind::InvalidInput,
                    "\"options.variantClassPrefix\" requires \"options.variants\".",
                ));
            }
        }
        Some(variants) => validate_variants(options, variants)?,
    }

    if options.css.unwrap_or(true)
        && let Some(ref path) = options.css_template
        && !Path::new(path).exists()
    {
        return Err(std::io::Error::new(
            ErrorKind::InvalidInput,
            format!("\"options.cssTemplate\" file not found: {path}"),
        ));
    }

    if options.html.unwrap_or(false)
        && let Some(ref path) = options.html_template
        && !Path::new(path).exists()
    {
        return Err(std::io::Error::new(
            ErrorKind::InvalidInput,
            format!("\"options.htmlTemplate\" file not found: {path}"),
        ));
    }

    if let Some(quality) = options
        .format_options
        .as_ref()
        .and_then(|value| value.woff2.as_ref())
        .and_then(|value| value.compression_quality)
        && quality > 11
    {
        return Err(std::io::Error::new(
            ErrorKind::InvalidInput,
            format!(
                "\"options.formatOptions.woff2.compressionQuality\" must be between 0 and 11, got {quality}."
            ),
        ));
    }

    Ok(())
}

fn validate_variants(
    options: &GenerateWebfontsOptions,
    variants: &[crate::types::FontVariant],
) -> std::io::Result<()> {
    if !options.files.is_empty() {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            "\"options.files\" must be empty when \"options.variants\" is provided.",
        ));
    }
    if variants.len() < 2 {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            "\"options.variants\" must contain at least two variants.",
        ));
    }
    if options
        .types
        .as_ref()
        .is_some_and(|types| types.contains(&FontType::Svg) || types.contains(&FontType::Eot))
    {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            "\"options.types\" cannot include \"svg\" or \"eot\" with \"options.variants\".",
        ));
    }
    if options.font_weight.is_some() {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            "\"options.fontWeight\" cannot be used with \"options.variants\".",
        ));
    }
    if options
        .template_options
        .as_ref()
        .is_some_and(|template| template.contains_key("variantClassPrefix"))
    {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            "\"options.templateOptions.variantClassPrefix\" cannot be used with \"options.variants\"; use \"options.variantClassPrefix\".",
        ));
    }

    let class_prefix = options.variant_class_prefix.as_deref().unwrap_or("icon--");
    if class_prefix.is_empty()
        || class_prefix.contains(char::is_whitespace)
        || class_prefix.contains('\0')
    {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            "\"options.variantClassPrefix\" must be non-empty and contain neither whitespace nor NUL.",
        ));
    }

    let mut names = HashSet::with_capacity(variants.len());
    let mut default_count = 0;
    for (index, variant) in variants.iter().enumerate() {
        let path = format!("options.variants[{index}]");
        if variant.files.is_empty() {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                format!("\"{path}.files\" is empty."),
            ));
        }
        if variant.name.is_empty() {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                format!("\"{path}.name\" is empty."),
            ));
        }
        if variant.name.contains(char::is_whitespace) {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                format!("\"{path}.name\" contains whitespace."),
            ));
        }
        if variant.name.contains('\0') {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                format!("\"{path}.name\" contains NUL."),
            ));
        }
        if !names.insert(&variant.name) {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                format!(
                    "\"{path}.name\" duplicates variant name \"{}\".",
                    variant.name
                ),
            ));
        }
        if let Some(weight) = variant.weight
            && !(1..=1000).contains(&weight)
        {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                format!("\"{path}.weight\" must be between 1 and 1000, got {weight}."),
            ));
        }
        default_count += usize::from(variant.default == Some(true));
    }

    if default_count != 1 {
        return Err(Error::new(
            ErrorKind::InvalidInput,
            format!(
                "\"options.variants\" must contain exactly one default variant, found {default_count}."
            ),
        ));
    }

    if variants.iter().all(|variant| variant.weight.is_some()) {
        for (index, pair) in variants.windows(2).enumerate() {
            if pair[0].weight >= pair[1].weight {
                return Err(Error::new(
                    ErrorKind::InvalidInput,
                    format!(
                        "\"options.variants[{}].weight\" must be greater than the preceding explicit weight.",
                        index + 1,
                    ),
                ));
            }
        }
    }

    if let Some(missing) = &options.missing_glyphs {
        match missing.behavior {
            MissingGlyphBehavior::Fallback => {
                let Some(fallback) = missing.variant.as_deref() else {
                    return Err(Error::new(
                        ErrorKind::InvalidInput,
                        "\"options.missingGlyphs.variant\" is required when behavior is \"fallback\".",
                    ));
                };
                if !variants.iter().any(|variant| variant.name == fallback) {
                    return Err(Error::new(
                        ErrorKind::InvalidInput,
                        format!(
                            "\"options.missingGlyphs.variant\" does not name a configured variant: \"{fallback}\"."
                        ),
                    ));
                }
            }
            MissingGlyphBehavior::Blank | MissingGlyphBehavior::Error
                if missing.variant.is_some() =>
            {
                return Err(Error::new(
                    ErrorKind::InvalidInput,
                    "\"options.missingGlyphs.variant\" is only valid when behavior is \"fallback\".",
                ));
            }
            MissingGlyphBehavior::Blank | MissingGlyphBehavior::Error => {}
        }
    }

    Ok(())
}

fn serialize_css_identifier(value: &str) -> String {
    let characters = value.chars().collect::<Vec<_>>();
    let mut escaped = String::with_capacity(value.len());
    for (index, character) in characters.iter().copied().enumerate() {
        let codepoint = character as u32;
        if character == '\0' {
            escaped.push('\u{fffd}');
        } else if (1..=0x1f).contains(&codepoint)
            || codepoint == 0x7f
            || (index == 0 && character.is_ascii_digit())
            || (index == 1 && character.is_ascii_digit() && characters.first() == Some(&'-'))
        {
            escaped.push('\\');
            escaped.push_str(&format!("{codepoint:x} "));
        } else if index == 0 && character == '-' && characters.len() == 1 {
            escaped.push_str("\\-");
        } else if codepoint >= 0x80
            || character == '-'
            || character == '_'
            || character.is_ascii_alphanumeric()
        {
            escaped.push(character);
        } else {
            escaped.push('\\');
            escaped.push(character);
        }
    }
    escaped
}

fn resolve_variant_weights(
    variants: &[FontVariant],
    default_index: usize,
) -> std::io::Result<Vec<u16>> {
    let mut weights = variants
        .iter()
        .map(|variant| variant.weight)
        .collect::<Vec<_>>();
    weights[default_index].get_or_insert(400);

    let mut anchors = Vec::with_capacity(variants.len() + 2);
    anchors.push((None, 0_u32));
    anchors.extend(
        weights
            .iter()
            .enumerate()
            .filter_map(|(index, weight)| weight.map(|weight| (Some(index), u32::from(weight)))),
    );
    anchors.push((None, 1001));

    for pair in anchors.windows(2) {
        let (left_index, left_weight) = pair[0];
        let (right_index, right_weight) = pair[1];
        if left_weight >= right_weight {
            let index = right_index.unwrap_or(variants.len() - 1);
            return Err(Error::new(
                ErrorKind::InvalidInput,
                format!(
                    "\"options.variants[{index}].weight\" must be greater than the preceding resolved weight {left_weight}."
                ),
            ));
        }

        let start = left_index.map_or(0, |index| index + 1);
        let end = right_index.unwrap_or(variants.len());
        let count = end - start;
        if count == 0 {
            continue;
        }
        if right_weight - left_weight - 1 < count as u32 {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                format!(
                    "\"options.variants[{start}..{}].weight\" cannot fit {count} automatic weights between {left_weight} and {right_weight}.",
                    end - 1,
                ),
            ));
        }

        let before_default = right_index.is_some_and(|index| index <= default_index);
        let step_weights = (0..count)
            .map(|offset| {
                if before_default {
                    i64::from(right_weight) - 100 * (count - offset) as i64
                } else {
                    i64::from(left_weight) + 100 * (offset + 1) as i64
                }
            })
            .collect::<Vec<_>>();
        let step_weights_fit = step_weights
            .iter()
            .all(|weight| *weight > i64::from(left_weight) && *weight < i64::from(right_weight));

        for offset in 0..count {
            let weight = if step_weights_fit {
                step_weights[offset] as u32
            } else {
                left_weight
                    + (right_weight - left_weight) * (offset as u32 + 1) / (count as u32 + 1)
            };
            weights[start + offset] = Some(weight as u16);
        }
    }

    Ok(weights.into_iter().map(Option::unwrap).collect())
}

fn resolve_variants(
    variants: &[FontVariant],
    class_prefix: &str,
) -> std::io::Result<ResolvedVariants> {
    let default_index = variants
        .iter()
        .position(|variant| variant.default == Some(true))
        .ok_or_else(|| {
            Error::new(
                ErrorKind::InvalidInput,
                "\"options.variants\" must contain a default variant.",
            )
        })?;
    let weights = resolve_variant_weights(variants, default_index)?;
    let mut resolved = Vec::with_capacity(variants.len());

    for (variant, weight) in variants.iter().zip(weights) {
        let class_name = format!("{class_prefix}{}", variant.name);
        let selector = serialize_css_identifier(&class_name);
        resolved.push(ResolvedFontVariant {
            name: variant.name.clone(),
            files: variant.files.clone(),
            weight,
            class_name,
            selector,
        });
    }

    Ok(ResolvedVariants {
        variants: resolved,
        default_index,
    })
}

pub(crate) fn resolve_generate_webfonts_options(
    options: GenerateWebfontsOptions,
) -> std::io::Result<ResolvedGenerateWebfontsOptions> {
    let types = resolved_font_types(&options);
    validate_font_type_order(&options, &types)?;
    let order = resolve_font_type_order(&options, &types);
    let css = options.css.unwrap_or(true);
    let html = options.html.unwrap_or(false);
    let font_name = options.font_name.unwrap_or_else(|| "iconfont".to_owned());
    let css_dest = options
        .css_dest
        .unwrap_or_else(|| default_output_dest(&options.dest, &font_name, "css"));
    let html_dest = options
        .html_dest
        .unwrap_or_else(|| default_output_dest(&options.dest, &font_name, "html"));
    let write_files = options.write_files.unwrap_or(true);
    let explicit_codepoints: BTreeMap<String, u32> =
        options.codepoints.unwrap_or_default().into_iter().collect();
    let variants = options
        .variants
        .as_deref()
        .map(|variants| {
            resolve_variants(
                variants,
                options.variant_class_prefix.as_deref().unwrap_or("icon--"),
            )
        })
        .transpose()?;

    let svg_format = options
        .format_options
        .as_ref()
        .and_then(|fo| fo.svg.as_ref());
    let center_vertically = svg_format
        .and_then(|s| s.center_vertically)
        .or(options.center_vertically);
    let optimize_output = svg_format
        .and_then(|s| s.optimize_output)
        .or(options.optimize_output);
    let preserve_aspect_ratio = svg_format
        .and_then(|s| s.preserve_aspect_ratio)
        .or(options.preserve_aspect_ratio);

    Ok(ResolvedGenerateWebfontsOptions {
        ascent: options.ascent,
        center_horizontally: options.center_horizontally,
        center_vertically,
        css,
        css_dest,
        css_template: match options.css_template {
            Some(ref t) if t.is_empty() => {
                return Err(std::io::Error::new(
                    ErrorKind::InvalidInput,
                    "\"options.cssTemplate\" must not be empty.".to_owned(),
                ));
            }
            other => other,
        },
        codepoints: explicit_codepoints.clone(),
        explicit_codepoints,
        css_fonts_url: options.css_fonts_url,
        descent: options.descent,
        dest: options.dest,
        files: options.files,
        fixed_width: options.fixed_width,
        format_options: options.format_options,
        html,
        html_dest,
        html_template: match options.html_template {
            Some(ref t) if t.is_empty() => {
                return Err(std::io::Error::new(
                    ErrorKind::InvalidInput,
                    "\"options.htmlTemplate\" must not be empty.".to_owned(),
                ));
            }
            other => other,
        },
        incremental: options.incremental.unwrap_or(false),
        font_height: options.font_height,
        font_name,
        font_style: options.font_style,
        font_weight: options.font_weight,
        ligature: options.ligature.unwrap_or(true),
        missing_glyphs: options.missing_glyphs.unwrap_or(MissingGlyphOptions {
            behavior: MissingGlyphBehavior::Blank,
            variant: None,
        }),
        normalize: options.normalize.unwrap_or(true),
        order,
        optimize_output,
        preserve_aspect_ratio,
        round: options.round,
        start_codepoint: options.start_codepoint.unwrap_or(0xF101),
        template_options: options.template_options,
        types,
        variants,
        write_files,
    })
}

pub(crate) fn finalize_generate_webfonts_options(
    options: &mut ResolvedGenerateWebfontsOptions,
    source_files: &[LoadedSvgFile],
) -> std::io::Result<()> {
    options.codepoints = resolve_codepoints(
        source_files
            .iter()
            .map(|source_file| source_file.glyph_name.as_str()),
        &options.explicit_codepoints,
        options.start_codepoint,
    )?;

    Ok(())
}

fn resolved_font_types(options: &GenerateWebfontsOptions) -> Vec<FontType> {
    match &options.types {
        Some(types) => types.clone(),
        None if options.variants.is_some() => vec![FontType::Woff, FontType::Woff2],
        None => DEFAULT_FONT_TYPES.to_vec(),
    }
}

fn resolve_font_type_order(options: &GenerateWebfontsOptions, types: &[FontType]) -> Vec<FontType> {
    match &options.order {
        Some(order) => order.clone(),
        None => DEFAULT_FONT_ORDER
            .iter()
            .copied()
            .filter(|font_type| types.contains(font_type))
            .collect(),
    }
}

pub(crate) fn default_output_dest(dest: &str, font_name: &str, extension: &str) -> String {
    Path::new(dest)
        .join(format!("{font_name}.{extension}"))
        .to_string_lossy()
        .into_owned()
}

fn validate_font_type_order(
    options: &GenerateWebfontsOptions,
    requested_types: &[FontType],
) -> std::io::Result<()> {
    if let Some(order) = &options.order
        && let Some(invalid_type) = order
            .iter()
            .copied()
            .find(|font_type| !requested_types.contains(font_type))
    {
        return Err(std::io::Error::new(
            ErrorKind::InvalidInput,
            format!(
                "Invalid font type order: '{}' is not present in 'types'.",
                invalid_type.as_extension()
            ),
        ));
    }

    Ok(())
}

pub(super) fn resolve_codepoints<'a>(
    names: impl IntoIterator<Item = &'a str>,
    codepoints: &BTreeMap<String, u32>,
    start_codepoint: u32,
) -> Result<BTreeMap<String, u32>, Error> {
    let mut resolved_codepoints = codepoints.clone();
    let mut used_codepoints: HashSet<u32> = resolved_codepoints.values().copied().collect();
    let mut next_codepoint = start_codepoint;

    for name in names {
        if resolved_codepoints.contains_key(name) {
            continue;
        }

        while used_codepoints.contains(&next_codepoint) {
            next_codepoint = next_codepoint.checked_add(1).ok_or_else(|| {
                Error::new(
                    ErrorKind::InvalidInput,
                    "Unable to assign another glyph codepoint: the u32 range is exhausted.",
                )
            })?;
        }

        resolved_codepoints.insert(name.to_owned(), next_codepoint);
        used_codepoints.insert(next_codepoint);
        next_codepoint = next_codepoint.saturating_add(1);
    }

    Ok(resolved_codepoints)
}