glyph_names/lib.rs
1#![deny(missing_docs)]
2#![no_std]
3
4//! Map a character to a glyph name according to the
5//! [Adobe Glyph List Specification](https://github.com/adobe-type-tools/agl-specification).
6//!
7//! ### Example
8//! ```
9//! use std::borrow::Cow;
10//! use glyph_names::glyph_name;
11//!
12//! assert_eq!(glyph_name('a' as u32), Some(Cow::from("a")));
13//! assert_eq!(glyph_name('%' as u32), Some(Cow::from("percent")));
14//! assert_eq!(glyph_name('☺' as u32), Some(Cow::from("smileface")));
15//! assert_eq!(glyph_name('↣' as u32), Some(Cow::from("uni21A3")));
16//! assert_eq!(glyph_name('🕴' as u32), Some(Cow::from("u1F574")));
17//! assert_eq!(glyph_name(0x110000), None);
18//! ```
19
20#[macro_use(format)]
21extern crate alloc;
22
23use alloc::borrow::Cow;
24use alloc::string::String;
25use core::convert::TryFrom;
26
27/// Adobe Glyph List For New Fonts
28mod aglfn;
29mod aglfn_names;
30pub use aglfn_names::GLYPH_NAME_PAIRS;
31
32/// Look up a glyph name for the supplied char code.
33pub fn glyph_name(ch: u32) -> Option<Cow<'static, str>> {
34 char::try_from(ch).ok().map(|ch| {
35 aglfn::glyph_name(ch)
36 .map(Cow::from)
37 .unwrap_or_else(|| Cow::from(unicode_glyph_name(ch)))
38 })
39}
40
41// It is recommended to specify names by using the ‘uni’ prefix for characters in the Basic
42// Multilingual Plane (BMP), and the shorter ‘u’ prefix for characters in the 16 Supplemental
43// Planes
44// https://github.com/adobe-type-tools/agl-specification#6-assigning-glyph-names-in-new-fonts
45fn unicode_glyph_name(ch: char) -> String {
46 let ch = ch as u32;
47 if ch <= 0xFFFF {
48 // Basic Multilingual Plane
49 format!("uni{:04X}", ch)
50 } else {
51 format!("u{:04X}", ch)
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_unicode_glyph_name() {
61 assert_eq!(&unicode_glyph_name('a'), "uni0061");
62 assert_eq!(&unicode_glyph_name('↣'), "uni21A3");
63 assert_eq!(&unicode_glyph_name('🕴'), "u1F574");
64 }
65}