font_map_core/codegen/
glyph.rs1use proc_macro2::TokenStream;
2use quote::{format_ident, quote};
3
4use crate::font::Glyph;
5
6#[derive(Debug, Clone)]
8pub struct GlyphDesc {
9 identifier: String,
10 name: String,
11 codepoint: u32,
12 comments: Vec<String>,
13}
14impl GlyphDesc {
15 #[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",
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 #[must_use]
43 pub fn name(&self) -> &str {
44 &self.name
45 }
46
47 #[must_use]
49 pub fn codepoint(&self) -> u32 {
50 self.codepoint
51 }
52
53 #[must_use]
55 pub fn identifier(&self) -> &str {
56 &self.identifier
57 }
58
59 pub fn set_identifier(&mut self, identifier: String) {
61 self.identifier = identifier;
62 }
63
64 #[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}