font_map_core/codegen/
glyph.rs

1use proc_macro2::TokenStream;
2use quote::{format_ident, quote};
3
4use crate::font::Glyph;
5
6/// Describes a glyph within a font
7#[derive(Debug, Clone)]
8pub struct GlyphDesc {
9    identifier: String,
10    name: String,
11    codepoint: u32,
12    comments: Vec<String>,
13}
14impl GlyphDesc {
15    /// Create a new glyph description from an identifier and a glyph
16    #[must_use]
17    pub fn new(identifier: &str, glyph: &Glyph) -> Self {
18        let identifier = identifier.to_string();
19        let name = glyph.name().to_string();
20        let codepoint = glyph.codepoint();
21        let uni_range = glyph.unicode_range();
22
23        let comments = vec![
24            format!("`{name} (U+{codepoint:04X})`  "),
25            format!("Unicode range: {uni_range}"),
26            #[cfg(feature = "extended-svg")]
27            format!(
28                "\n\n![Preview Glyph]({})",
29                glyph.svg_dataimage_url().unwrap_or_default()
30            ),
31        ];
32
33        Self {
34            identifier,
35            name,
36            codepoint,
37            comments,
38        }
39    }
40
41    /// Get the name of the glyph
42    #[must_use]
43    pub fn name(&self) -> &str {
44        &self.name
45    }
46
47    /// Get the codepoint of the glyph
48    #[must_use]
49    pub fn codepoint(&self) -> u32 {
50        self.codepoint
51    }
52
53    /// Get the identifier of the glyph
54    #[must_use]
55    pub fn identifier(&self) -> &str {
56        &self.identifier
57    }
58
59    /// Set the identifier of the glyph
60    pub fn set_identifier(&mut self, identifier: String) {
61        self.identifier = identifier;
62    }
63
64    /// Generate code for the glyph
65    #[must_use]
66    pub fn codegen(&self) -> TokenStream {
67        let identifier = format_ident!("{}", &self.identifier);
68        let comments = &self.comments;
69        let codepoint = self.codepoint;
70
71        quote! {
72            #( #[doc = #comments] )*
73            #identifier = #codepoint,
74        }
75    }
76}
77
78impl Eq for GlyphDesc {}
79impl PartialEq for GlyphDesc {
80    fn eq(&self, other: &Self) -> bool {
81        self.identifier == other.identifier
82    }
83}
84
85impl Ord for GlyphDesc {
86    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
87        self.identifier.cmp(&other.identifier)
88    }
89}
90
91impl PartialOrd for GlyphDesc {
92    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
93        Some(self.cmp(other))
94    }
95}