@@ -60,18 +60,27 @@ pub enum MathStyle {
60
60
Stretched ,
61
61
}
62
62
63
- /// Returns an iterator that yields the styled equivalent of a `char`.
64
- ///
65
- /// This `struct` is created by the [`to_style`] function. See its
66
- /// documentation for more.
67
- #[ derive( Debug , Clone ) ]
68
- pub struct ToStyle ( core:: array:: IntoIter < char , 2 > ) ;
69
-
70
- impl ToStyle {
71
- fn new ( c : char , style : MathStyle ) -> ToStyle {
63
+ impl MathStyle {
64
+ /// Applies the style to a given `char`.
65
+ ///
66
+ /// Note that some styles will convert a character into multiple
67
+ /// characters, hence this function always returns an array of two
68
+ /// characters.
69
+ ///
70
+ /// # Examples
71
+ ///
72
+ /// ```
73
+ /// use codex::styling::MathStyle;
74
+ ///
75
+ /// assert_eq!(['𝑴', '\0'], MathStyle::SerifItalicBold.apply('M'));
76
+ /// assert_eq!(['𞸪', '\0'], MathStyle::Initial.apply('ك'));
77
+ /// assert_eq!(['ⅈ', '\0'], MathStyle::DoubleStruckItalic.apply('i'));
78
+ /// assert_eq!(['𝓴', '\u{fe01}'], MathStyle::RoundhandBold.apply('k'));
79
+ /// ```
80
+ pub fn apply ( self , c : char ) -> [ char ; 2 ] {
72
81
use mappings:: * ;
73
82
use MathStyle :: * ;
74
- let chars = match style {
83
+ match self {
75
84
Serif => [ to_serif ( c) , '\0' ] ,
76
85
SerifBold => [ to_serif_bold ( c) , '\0' ] ,
77
86
SerifItalic => [ to_serif_italic ( c) , '\0' ] ,
@@ -95,8 +104,20 @@ impl ToStyle {
95
104
Tailed => [ to_tailed ( c) , '\0' ] ,
96
105
Looped => [ to_looped ( c) , '\0' ] ,
97
106
Stretched => [ to_stretched ( c) , '\0' ] ,
98
- } ;
107
+ }
108
+ }
109
+ }
99
110
111
+ /// Returns an iterator that yields the styled equivalent of a `char`.
112
+ ///
113
+ /// This `struct` is created by the [`to_style`] function. See its
114
+ /// documentation for more.
115
+ #[ derive( Debug , Clone ) ]
116
+ pub struct ToStyle ( core:: array:: IntoIter < char , 2 > ) ;
117
+
118
+ impl ToStyle {
119
+ #[ inline]
120
+ fn new ( chars : [ char ; 2 ] ) -> ToStyle {
100
121
let mut iter = chars. into_iter ( ) ;
101
122
if chars[ 1 ] == '\0' {
102
123
iter. next_back ( ) ;
@@ -171,14 +192,18 @@ impl fmt::Display for ToStyle {
171
192
/// ```
172
193
/// use codex::styling::{to_style, MathStyle};
173
194
///
174
- /// assert_eq!("𝑴", to_style('M', MathStyle::SerifItalicBold).to_string());
175
- /// assert_eq!("𞸪", to_style('ك', MathStyle::Initial).to_string());
176
- /// assert_eq!("ⅈ", to_style('i', MathStyle::DoubleStruckItalic).to_string());
177
- /// assert_eq!("𝓴\u{fe01}", to_style('k', MathStyle::RoundhandBold).to_string());
195
+ /// assert_eq!("𞺚", to_style('ظ', MathStyle::Looped).to_string());
196
+ /// assert_eq!("𝒬\u{fe00}", to_style('Q', MathStyle::Chancery).to_string());
197
+ ///
198
+ /// let s = "xγΩAذح1∑س"
199
+ /// .chars()
200
+ /// .flat_map(|c| to_style(c, MathStyle::DoubleStruck))
201
+ /// .collect::<String>();
202
+ /// assert_eq!("𝕩ℽΩ𝔸𞺸𞺧𝟙⅀𞺮", s);
178
203
/// ```
179
204
#[ inline]
180
205
pub fn to_style ( c : char , style : MathStyle ) -> ToStyle {
181
- ToStyle :: new ( c , style)
206
+ ToStyle :: new ( style. apply ( c ) )
182
207
}
183
208
184
209
/// Functions which map a `char` to its specified styled form.
0 commit comments