font_map_core/
codegen.rs

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
//! Code generation utilities for fonts
use proc_macro2::TokenStream;
use quote::format_ident;
use std::{collections::HashMap, vec};

use crate::font::{Font, StringKind};

mod docstring;
use docstring::DocstringExt;

mod to_ident;
use to_ident::{to_categories, to_identifiers, ToIdentExt};

mod category;
use category::FontCategoryDesc;

mod glyph;
pub use glyph::GlyphDesc;

#[cfg(feature = "codegen")]
#[cfg_attr(docsrs, doc(cfg(feature = "codegen")))]
pub use quote::quote;

/// Describes a font used for code generation
#[derive(Debug, Clone)]
pub struct FontDesc {
    identifier: String,
    family: Option<String>,
    comments: Vec<String>,
    categories: Vec<FontCategoryDesc>,
}
impl FontDesc {
    /// Describe the font from a `Font` instance, optionally skipping categories
    pub fn from_font(identifier: &str, font: &Font, skip_categories: bool) -> Self {
        let identifier = identifier.to_string();
        let family = font.string(StringKind::FontFamily).map(ToString::to_string);
        let mut comments = font.gen_docblock();

        //
        // Get initial categories
        let mut categories = if skip_categories {
            // If set, skip categorization all-together
            let glyphs = to_identifiers(font.glyphs());
            vec![FontCategoryDesc::new(&identifier, glyphs)]
        } else {
            // Otherwise, attempt a best-effort categorization
            let raw_categories = to_categories(font.glyphs());
            let mut categories = Vec::with_capacity(raw_categories.len());
            for (name, glyphs) in raw_categories {
                categories.push(FontCategoryDesc::new(&name, glyphs));
            }

            categories
        };

        //
        // If we have just one, fall-back to single-cat generation
        if categories.len() == 1 {
            let category = &mut categories[0];
            category.set_name(identifier.clone());
            category.set_comments(comments.drain(..));
            category.sort();

            return Self {
                identifier,
                family,
                comments,
                categories,
            };
        }

        //
        // Extract (or create) the `Other` category
        let mut other = categories
            .iter()
            .position(|c| c.name() == "Other")
            .map_or_else(
                || FontCategoryDesc::new("Other", HashMap::default()),
                |idx| categories.swap_remove(idx),
            );

        //
        // Extract all categories with < 3 glyphs and merge them with `Other`
        categories = categories
            .drain(..)
            .filter_map(|category| {
                if category.glyphs().len() > 2 {
                    return Some(category);
                }

                let (name, glyphs) = category.into_inner();
                for mut glyph in glyphs {
                    let identifier = name.merge_identifiers(glyph.identifier());
                    glyph.set_identifier(identifier);
                    other.insert(glyph);
                }
                None
            })
            .collect();

        //
        // Update/Add Other
        other.update_comments();
        categories.push(other);

        //
        // Sort the categories by name
        categories.sort_by(|a, b| a.name().cmp(b.name()));
        categories.iter_mut().for_each(FontCategoryDesc::sort);

        Self {
            identifier,
            family,
            comments,
            categories,
        }
    }

    /// Returns true if this font has only one category
    #[must_use]
    pub fn is_single_category(&self) -> bool {
        self.categories.len() == 1
    }

    /// Generate the code for the font
    ///
    /// Optionally, you can inject additional code into the generated font's impl
    #[allow(clippy::needless_pass_by_value)]
    #[must_use]
    pub fn codegen(&self, extra_impl: Option<TokenStream>) -> TokenStream {
        let identifier = format_ident!("{}", &self.identifier);
        let outer_comments = &self.comments;
        let font_family = self.family.iter();
        let injection = extra_impl.iter();

        if self.is_single_category() {
            let category = &self.categories[0];

            category.codegen(Some(quote! {
                #(
                    /// The family name for font
                    pub const FONT_FAMILY: &str = #font_family;
                )*
            }))
        } else {
            //
            // Categories in a module, generate an outer wrapper enum
            let mut categories = Vec::with_capacity(self.categories.len());
            for category in &self.categories {
                categories.push(category.codegen(None));
            }

            let mut variant_names = Vec::with_capacity(categories.len());
            let mut variants = Vec::with_capacity(categories.len());
            for category in &self.categories {
                let name = format_ident!("{}", category.name());
                let comments = category.comments();
                let variant = quote! {
                    #( #[doc = #comments] )*
                    #name(categories :: #name),
                };

                variant_names.push(name);
                variants.push(variant);
            }

            quote! {
                /// Contains a set of enums for each of the sub-categories in this font
                pub mod categories {
                    #( #categories )*
                }

                #[allow(rustdoc::bare_urls)]
                #( #[doc = #outer_comments] )*
                #[doc = ""]
                #[doc = "See the [`categories`] module for more information."]
                #[derive(Debug, Clone, Copy)]
                #[rustfmt::skip]
                pub enum #identifier {
                    #( #variants )*
                }

                #[rustfmt::skip]
                #[allow(dead_code)]
                impl #identifier {
                    #(
                        /// The family name for this glyph's font
                        pub const FONT_FAMILY: &str = #font_family;
                    )*

                    /// Returns the postscript name of the glyph
                    #[allow(clippy::too_many_lines)]
                    #[allow(clippy::match_same_arms)]
                    #[must_use]
                    pub fn name(&self) -> &'static str {
                        match self {
                            #( Self :: #variant_names(inner) => inner.name(), )*
                        }
                    }

                    #(
                        #injection
                    )*
                }

                #(
                    impl From<categories :: #variant_names> for #identifier {
                        fn from(value: categories :: #variant_names) -> Self {
                            Self :: #variant_names(value)
                        }
                    }
                )*

                impl From<#identifier> for char {
                    fn from(value: #identifier) -> Self {
                        match value {
                            #( #identifier :: #variant_names(inner) => char::from(inner), )*
                        }
                    }
                }

                impl From<&#identifier> for char {
                    fn from(value: &#identifier) -> Self {
                        (*value).into()
                    }
                }

                impl From<#identifier> for u32 {
                    fn from(value: #identifier) -> Self {
                        match value {
                            #( #identifier :: #variant_names(inner) => inner as u32, )*
                        }
                    }
                }

                impl From<&#identifier> for u32 {
                    fn from(value: &#identifier) -> Self {
                        (*value).into()
                    }
                }

                impl std::fmt::Display for #identifier {
                    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                        match self {
                            #( #identifier :: #variant_names(inner) => inner.fmt(f), )*
                        }
                    }
                }
            }
        }
    }
}

impl From<&FontDesc> for TokenStream {
    fn from(value: &FontDesc) -> Self {
        value.codegen(None)
    }
}