Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math font #377

Merged
merged 34 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 41 additions & 68 deletions src/engine/backends/math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ mod tests {
fn test_parse_style() {
use kime_engine_dict::math_symbol_key::*;

assert_eq!(crate::parse_style("sf"), STYLE_SF);
assert_eq!(crate::parse_style("bf"), STYLE_BF);
assert_eq!(crate::parse_style("it"), STYLE_IT);
assert_eq!(crate::parse_style("tt"), STYLE_TT);
assert_eq!(crate::parse_style("bb"), STYLE_BB);
assert_eq!(crate::parse_style("scr"), STYLE_SCR);
assert_eq!(crate::parse_style("cal"), STYLE_CAL);
assert_eq!(crate::parse_style("frak"), STYLE_FRAK);
assert_eq!(crate::parse_style("fruk"), STYLE_NONE);
assert_eq!(crate::parse_style("bfit"), STYLE_BF | STYLE_IT);
assert_eq!(crate::parse_style("bfsfit"), STYLE_SF | STYLE_BF | STYLE_IT);
assert_eq!(crate::parse_style("sf"), Style::SF);
assert_eq!(crate::parse_style("bf"), Style::BF);
assert_eq!(crate::parse_style("it"), Style::IT);
assert_eq!(crate::parse_style("tt"), Style::TT);
assert_eq!(crate::parse_style("bb"), Style::BB);
assert_eq!(crate::parse_style("scr"), Style::SCR);
assert_eq!(crate::parse_style("cal"), Style::CAL);
assert_eq!(crate::parse_style("frak"), Style::FRAK);
assert_eq!(crate::parse_style("fruk"), Style::NONE);
assert_eq!(crate::parse_style("bfit"), Style::BF | Style::IT);
assert_eq!(crate::parse_style("bfsfit"), Style::SF | Style::BF | Style::IT);
}
}

Expand All @@ -43,66 +43,39 @@ impl MathMode {
}
}

fn take_str(s: &str, n: usize) -> &str {
if s.len() >= n {
&s[0..n]
} else {
s
}
}

fn parse_style(style_str: &str) -> Style {
let mut buf: &str = style_str;
let mut style = STYLE_NONE;
let mut style = Style::NONE;

loop {
let style_new = match take_str(buf, 2) {
"" => return style,
"sf" => {
buf = &buf[2..];
STYLE_SF
}
"bf" => {
buf = &buf[2..];
STYLE_BF
}
"it" => {
buf = &buf[2..];
STYLE_IT
}
"tt" => {
buf = &buf[2..];
STYLE_TT
}
"bb" => {
buf = &buf[2..];
STYLE_BB
}
"sc" => {
if let "r" = take_str(&buf[2..], 1) {
buf = &buf[3..];
STYLE_SCR
} else {
return STYLE_NONE;
}
}
"ca" => {
if let "l" = take_str(&buf[2..], 1) {
buf = &buf[3..];
STYLE_CAL
} else {
return STYLE_NONE;
}
}
"fr" => {
if let "ak" = take_str(&buf[2..], 2) {
buf = &buf[4..];
STYLE_FRAK
} else {
return STYLE_NONE;
}
}
_ => return STYLE_NONE,
let style_new = if buf == "" {
damhiya marked this conversation as resolved.
Show resolved Hide resolved
return style;
} else if let Some(_buf) = buf.strip_prefix("sf") {
buf = _buf;
Style::SF
} else if let Some(_buf) = buf.strip_prefix("bf") {
buf = _buf;
Style::BF
} else if let Some(_buf) = buf.strip_prefix("it") {
buf = _buf;
Style::IT
} else if let Some(_buf) = buf.strip_prefix("tt") {
buf = _buf;
Style::TT
} else if let Some(_buf) = buf.strip_prefix("bb") {
buf = _buf;
Style::BB
} else if let Some(_buf) = buf.strip_prefix("scr") {
buf = _buf;
Style::SCR
} else if let Some(_buf) = buf.strip_prefix("cal") {
buf = _buf;
Style::CAL
} else if let Some(_buf) = buf.strip_prefix("frak") {
buf = _buf;
Style::FRAK
} else {
return Style::NONE;
};

style |= style_new;
Expand Down Expand Up @@ -153,7 +126,7 @@ impl InputEngineMode for MathMode {
commit_buf.push_str(symbol);
}
} else {
if let Some(symbol) = kime_engine_dict::lookup_math_symbol(&first, STYLE_NONE) {
if let Some(symbol) = kime_engine_dict::lookup_math_symbol(&first, Style::NONE) {
commit_buf.push_str(symbol);
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/engine/dict/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ impl<'de> Deserialize<'de> for Style {
.into_iter()
.map(|s| {
Ok(match s {
"sf" => STYLE_SF,
"bf" => STYLE_BF,
"it" => STYLE_IT,
"tt" => STYLE_TT,
"bb" => STYLE_BB,
"scr" => STYLE_SCR,
"cal" => STYLE_CAL,
"frak" => STYLE_FRAK,
"sf" => Style::SF,
"bf" => Style::BF,
"it" => Style::IT,
"tt" => Style::TT,
"bb" => Style::BB,
"scr" => Style::SCR,
"cal" => Style::CAL,
"frak" => Style::FRAK,
_ => return Err(Error::custom("no matching style name")),
})
})
.fold(Ok(STYLE_NONE), |sty1, sty2| Ok(sty1? | sty2?));
.fold(Ok(Style::NONE), |sty1, sty2| Ok(sty1? | sty2?));
style
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/engine/dict/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ mod tests {
use crate::lookup_math_symbol;
use crate::math_symbol_key::*;

assert_eq!(lookup_math_symbol("alpha", STYLE_NONE), Some("α"));
assert_eq!(lookup_math_symbol("alpha", STYLE_BF), Some("𝛂"));
assert_eq!(lookup_math_symbol("alpha", STYLE_IT), Some("𝛼"));
assert_eq!(lookup_math_symbol("alpha", STYLE_BF | STYLE_IT), Some("𝜶"));
assert_eq!(lookup_math_symbol("alpha", Style::NONE), Some("α"));
assert_eq!(lookup_math_symbol("alpha", Style::BF), Some("𝛂"));
assert_eq!(lookup_math_symbol("alpha", Style::IT), Some("𝛼"));
assert_eq!(lookup_math_symbol("alpha", Style::BF | Style::IT), Some("𝜶"));

assert_eq!(
lookup_math_symbol("R", STYLE_SF | STYLE_BF | STYLE_IT),
lookup_math_symbol("R", Style::SF | Style::BF | Style::IT),
Some("𝙍")
);
assert_eq!(lookup_math_symbol("R", STYLE_TT), Some("𝚁"));
assert_eq!(lookup_math_symbol("R", STYLE_BB), Some("ℝ"));
assert_eq!(lookup_math_symbol("R", STYLE_SCR), Some("ℛ"));
assert_eq!(lookup_math_symbol("R", STYLE_CAL), Some("𝓡"));
assert_eq!(lookup_math_symbol("R", STYLE_FRAK), Some("ℜ"));
assert_eq!(lookup_math_symbol("R", Style::TT), Some("𝚁"));
assert_eq!(lookup_math_symbol("R", Style::BB), Some("ℝ"));
assert_eq!(lookup_math_symbol("R", Style::SCR), Some("ℛ"));
assert_eq!(lookup_math_symbol("R", Style::CAL), Some("𝓡"));
assert_eq!(lookup_math_symbol("R", Style::FRAK), Some("ℜ"));
}

#[test]
Expand Down
20 changes: 11 additions & 9 deletions src/engine/dict/src/math_symbol_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ impl BitOrAssign for Style {
}
}

pub const STYLE_NONE: Style = Style(0);
pub const STYLE_SF: Style = Style(1);
pub const STYLE_BF: Style = Style(2);
pub const STYLE_IT: Style = Style(4);
pub const STYLE_TT: Style = Style(8);
pub const STYLE_BB: Style = Style(16);
pub const STYLE_SCR: Style = Style(32);
pub const STYLE_CAL: Style = Style(64);
pub const STYLE_FRAK: Style = Style(128);
impl Style {
pub const NONE: Style = Style(0);
pub const SF: Style = Style(1<<0);
pub const BF: Style = Style(1<<1);
pub const IT: Style = Style(1<<2);
pub const TT: Style = Style(1<<3);
pub const BB: Style = Style(1<<4);
pub const SCR: Style = Style(1<<5);
pub const CAL: Style = Style(1<<6);
pub const FRAK: Style = Style(1<<7);
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct SymbolKey<'a>(pub &'a str, pub Style);