Skip to content

Commit ff20c5e

Browse files
committed
Add style resolving logic
1 parent 35497ce commit ff20c5e

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/styling.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,60 @@ pub fn to_style(c: char, style: MathStyle) -> ToStyle {
290290
ToStyle::new(styled)
291291
}
292292

293+
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
294+
pub enum MathVariant {
295+
Plain,
296+
Script,
297+
Fraktur,
298+
SansSerif,
299+
Monospace,
300+
DoubleStruck,
301+
}
302+
303+
pub fn resolve_style(
304+
c: char,
305+
variant: Option<MathVariant>,
306+
bold: bool,
307+
italic: Option<bool>,
308+
) -> MathStyle {
309+
use conversions::*;
310+
use MathVariant::*;
311+
match (variant.unwrap_or(Plain), bold, italic) {
312+
(Plain, false, Some(true)) if is_latin(c) | is_greek(c) => MathStyle::Italic,
313+
(Plain, true, Some(false)) if is_latin(c) | is_greek(c) => MathStyle::Bold,
314+
(Plain, true, Some(true)) if is_latin(c) | is_greek(c) => MathStyle::BoldItalic,
315+
(Plain, true, _) if is_digit(c) | matches!(c, 'Ϝ' | 'ϝ') => MathStyle::Bold,
316+
(Plain, _, Some(true)) if matches!(c, 'ı' | 'ȷ' | 'ħ') => MathStyle::Italic,
317+
(Plain, _, None) => resolve_style(c, variant, bold, Some(is_italic(c))),
318+
(Plain, _, _) => MathStyle::Plain,
319+
320+
(SansSerif, false, Some(false)) if is_latin(c) => MathStyle::SansSerif,
321+
(SansSerif, false, Some(true)) if is_latin(c) => MathStyle::SansSerifItalic,
322+
(SansSerif, true, Some(false)) if is_latin(c) => MathStyle::SansSerifBold,
323+
(SansSerif, true, Some(true)) if is_latin(c) => MathStyle::SansSerifBoldItalic,
324+
(SansSerif, false, _) if is_digit(c) => MathStyle::SansSerif,
325+
(SansSerif, true, _) if is_digit(c) => MathStyle::SansSerifBold,
326+
(SansSerif, _, Some(false)) if is_greek(c) => MathStyle::SansSerifBold,
327+
(SansSerif, _, Some(true)) if is_greek(c) => MathStyle::SansSerifBoldItalic,
328+
(SansSerif, _, None) => resolve_style(c, variant, bold, Some(is_italic(c))),
329+
330+
(Script, false, _) if is_latin(c) => MathStyle::Script,
331+
(Script, true, _) if is_latin(c) => MathStyle::BoldScript,
332+
(Fraktur, false, _) if is_latin(c) => MathStyle::Fraktur,
333+
(Fraktur, true, _) if is_latin(c) => MathStyle::BoldFraktur,
334+
(Monospace, _, _) if is_digit(c) | is_latin(c) => MathStyle::Monospace,
335+
(DoubleStruck, _, Some(true)) if matches!(c, 'D' | 'd' | 'e' | 'i' | 'j') => {
336+
MathStyle::DoubleStruckItalic
337+
}
338+
(DoubleStruck, _, _)
339+
if is_digit(c) | is_latin(c) | matches!(c, '∑' | 'Γ' | 'Π' | 'γ' | 'π') =>
340+
{
341+
MathStyle::DoubleStruck
342+
}
343+
(_, _, _) => resolve_style(c, None, bold, italic),
344+
}
345+
}
346+
293347
/// Functions which convert a `char` to its specified styled form.
294348
///
295349
/// Sourced from:
@@ -306,6 +360,26 @@ mod conversions {
306360
const VARIATION_SELECTOR_1: char = '\u{FE00}';
307361
const VARIATION_SELECTOR_2: char = '\u{FE01}';
308362

363+
#[inline]
364+
pub fn is_italic(c: char) -> bool {
365+
matches!(c, 'a'..='z' | 'ħ' | 'ı' | 'ȷ' | 'A'..='Z' | 'α'..='ω' | '∂' | 'ϵ' | 'ϑ' | 'ϰ' | 'ϕ' | 'ϱ' | 'ϖ')
366+
}
367+
368+
#[inline]
369+
pub fn is_digit(c: char) -> bool {
370+
c.is_ascii_digit()
371+
}
372+
373+
#[inline]
374+
pub fn is_latin(c: char) -> bool {
375+
c.is_ascii_alphabetic()
376+
}
377+
378+
#[inline]
379+
pub fn is_greek(c: char) -> bool {
380+
matches!(c, 'Α'..='Ω' | '∇' | 'ϴ' | 'α'..='ω' | '∂' | 'ϵ' | 'ϑ' | 'ϰ' | 'ϕ' | 'ϱ' | 'ϖ')
381+
}
382+
309383
/// The character given by adding `delta` to the codepoint of `c`.
310384
#[inline]
311385
fn apply_delta(c: char, delta: u32) -> char {

0 commit comments

Comments
 (0)