Draw mathcal letters from cmsy10
This commit is contained in:
1 parent
66ceabad0e
commit
e229809581
7 files changed
+114
-8
No files matched your search
@@ -15,6 +15,8 @@ pub enum Cmr10Metrics {
|
||||
BoldItalic,
|
||||
Mono,
|
||||
Sans,
|
||||
/// Calligraphic capitals from cmsy10 (`\mathcal`).
|
||||
MathCalligraphic,
|
||||
/// Math face: italic letters from cmmi10, big operators from cmex10.
|
||||
Math,
|
||||
}
|
||||
@@ -100,6 +102,7 @@ fn metrics_for(font: Cmr10Metrics) -> &'static TfmMetrics {
|
||||
Cmr10Metrics::Mono => CMTT10_METRICS.get_or_init(parse_mono),
|
||||
Cmr10Metrics::Sans => CMSS10_METRICS.get_or_init(parse_sans),
|
||||
Cmr10Metrics::Math => CMR10_METRICS.get_or_init(parse_roman),
|
||||
Cmr10Metrics::MathCalligraphic => CMSY10_METRICS.get_or_init(parse_math_symbol),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,6 +142,12 @@ impl Cmr10Metrics {
|
||||
}
|
||||
|
||||
pub fn width(&self, character: char) -> i64 {
|
||||
if *self == Self::MathCalligraphic {
|
||||
if character.is_ascii_uppercase() {
|
||||
return self.metrics().width(character);
|
||||
}
|
||||
return Self::Math.width(character);
|
||||
}
|
||||
if self.is_math() && character.is_ascii_alphabetic() {
|
||||
let width = math_italic_metrics().width(character);
|
||||
if width > 0 {
|
||||
@@ -164,6 +173,12 @@ impl Cmr10Metrics {
|
||||
}
|
||||
|
||||
pub fn height(&self, character: char) -> i64 {
|
||||
if *self == Self::MathCalligraphic {
|
||||
if character.is_ascii_uppercase() {
|
||||
return self.metrics().height(character);
|
||||
}
|
||||
return Self::Math.height(character);
|
||||
}
|
||||
if self.is_math() && character.is_ascii_alphabetic() {
|
||||
let height = math_italic_metrics().height(character);
|
||||
if height > 0 {
|
||||
@@ -182,6 +197,12 @@ impl Cmr10Metrics {
|
||||
}
|
||||
|
||||
pub fn depth(&self, character: char) -> i64 {
|
||||
if *self == Self::MathCalligraphic {
|
||||
if character.is_ascii_uppercase() {
|
||||
return self.metrics().depth(character);
|
||||
}
|
||||
return Self::Math.depth(character);
|
||||
}
|
||||
if self.is_math() && character.is_ascii_alphabetic() {
|
||||
let depth = math_italic_metrics().depth(character);
|
||||
if depth > 0 {
|
||||
@@ -200,6 +221,12 @@ impl Cmr10Metrics {
|
||||
}
|
||||
|
||||
pub fn italic(&self, character: char) -> i64 {
|
||||
if *self == Self::MathCalligraphic {
|
||||
if character.is_ascii_uppercase() {
|
||||
return self.metrics().italic(character);
|
||||
}
|
||||
return Self::Math.italic(character);
|
||||
}
|
||||
if self.is_math() && character.is_ascii_alphabetic() {
|
||||
return math_italic_metrics().italic(character);
|
||||
}
|
||||
@@ -298,6 +325,26 @@ pub fn math_extension_slot(character: char) -> Option<u8> {
|
||||
Some(slot)
|
||||
}
|
||||
|
||||
/// Advance width of a `cmsy10` glyph addressed by a Unicode math character.
|
||||
pub fn math_symbol_width(character: char) -> Option<i64> {
|
||||
if character.is_ascii_uppercase() {
|
||||
let width = math_symbol_metrics().width(character);
|
||||
if width > 0 {
|
||||
return Some(width);
|
||||
}
|
||||
}
|
||||
math_symbol_slot(character)
|
||||
.map(|slot| math_symbol_metrics().width(char::from(slot)))
|
||||
.filter(|width| *width > 0)
|
||||
}
|
||||
|
||||
/// Advance width of a `cmex10` glyph addressed by a Unicode n-ary operator.
|
||||
pub fn math_extension_width(character: char) -> Option<i64> {
|
||||
math_extension_slot(character)
|
||||
.map(|slot| math_extension_metrics().width(char::from(slot)))
|
||||
.filter(|width| *width > 0)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -4189,6 +4189,29 @@ mod tests {
|
||||
assert_eq!(faces, vec![FontFace::Bold]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mathcal_paints_with_the_cmsy_face() {
|
||||
use crate::render::{DisplayItem, FontFace};
|
||||
let document = Document::new(
|
||||
1_000_000,
|
||||
vec![Unit::math(
|
||||
1,
|
||||
crate::math::parse_math("\\mathcal{A}"),
|
||||
&Cmr10Metrics::Math,
|
||||
)],
|
||||
);
|
||||
let engine = Engine::layout(&document);
|
||||
let list = engine.pages()[0].display_list();
|
||||
assert!(list.items.iter().any(|item| matches!(
|
||||
item,
|
||||
DisplayItem::Text {
|
||||
text,
|
||||
font: FontFace::MathSymbol,
|
||||
..
|
||||
} if text == "A"
|
||||
)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edit_on_second_page_only_rebuilds_second_page() {
|
||||
let mut document = document();
|
||||
|
||||
@@ -13,6 +13,7 @@ pub enum MathAlphabet {
|
||||
Italic,
|
||||
Sans,
|
||||
Mono,
|
||||
Calligraphic,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
@@ -305,6 +306,7 @@ pub(crate) fn alphabet_metrics(font: Cmr10Metrics, alphabet: MathAlphabet) -> Cm
|
||||
MathAlphabet::Mono => {
|
||||
Cmr10Metrics::for_paragraph_font(crate::paragraph::ParagraphFont::Mono)
|
||||
}
|
||||
MathAlphabet::Calligraphic => Cmr10Metrics::MathCalligraphic,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1344,6 +1346,7 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
||||
"mathit" => Some(MathAlphabet::Italic),
|
||||
"mathsf" => Some(MathAlphabet::Sans),
|
||||
"mathtt" => Some(MathAlphabet::Mono),
|
||||
"mathcal" => Some(MathAlphabet::Calligraphic),
|
||||
_ => None,
|
||||
};
|
||||
match alphabet {
|
||||
@@ -1557,12 +1560,15 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mathcal_keeps_character_widths() {
|
||||
fn mathcal_uses_calligraphic_widths() {
|
||||
let font = Cmr10Metrics::default();
|
||||
let calligraphic = layout_math(&parse_math("\\mathcal{N}"), &font);
|
||||
let upright = layout_math(&parse_math("\\mathrm{N}"), &font);
|
||||
assert_eq!(calligraphic.width, upright.width);
|
||||
assert_eq!(calligraphic.height, upright.height);
|
||||
assert_ne!(calligraphic.width, upright.width);
|
||||
assert_eq!(
|
||||
calligraphic.width,
|
||||
crate::cmr10::math_symbol_width('N').expect("cmsy has calligraphic capitals")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -516,7 +516,9 @@ fn write_pdf(
|
||||
.iter()
|
||||
.filter_map(|character| {
|
||||
let code = if slot == FontFace::MathSymbol as usize {
|
||||
crate::cmr10::math_symbol_slot(*character)
|
||||
crate::cmr10::math_symbol_slot(*character).or_else(|| {
|
||||
character.is_ascii_uppercase().then_some(*character as u8)
|
||||
})
|
||||
} else {
|
||||
crate::cmr10::math_extension_slot(*character)
|
||||
};
|
||||
@@ -568,7 +570,18 @@ fn write_pdf(
|
||||
let mut next_code = first;
|
||||
for code in first..=last {
|
||||
let width = by_code.get(&code).map_or(0, |character| {
|
||||
(metrics.width(*character) * 100 / 65_536) as i64
|
||||
let width = match slot {
|
||||
slot if slot == FontFace::MathSymbol as usize => {
|
||||
crate::cmr10::math_symbol_width(*character)
|
||||
.unwrap_or_else(|| metrics.width(*character))
|
||||
}
|
||||
slot if slot == FontFace::MathExtension as usize => {
|
||||
crate::cmr10::math_extension_width(*character)
|
||||
.unwrap_or_else(|| metrics.width(*character))
|
||||
}
|
||||
_ => metrics.width(*character),
|
||||
};
|
||||
(width * 100 / 65_536) as i64
|
||||
});
|
||||
widths.push(width.to_string());
|
||||
let glyph = if slot == FontFace::MathSymbol as usize
|
||||
@@ -924,6 +937,22 @@ mod type1_pdf_tests {
|
||||
assert!(!text.contains("/BaseFont /CMR10"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mathcal_letters_embed_cmsy10() {
|
||||
let mut list = hello_list();
|
||||
list.items[0] = DisplayItem::Text {
|
||||
x: 0,
|
||||
y: 800_000,
|
||||
size: 655_360,
|
||||
text: "A".to_string(),
|
||||
font: FontFace::MathSymbol,
|
||||
};
|
||||
let pdf = write(&[list]);
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("/BaseFont /CMSY10"), "{text}");
|
||||
assert!(text.contains("(\\101)"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn math_symbols_embed_cmsy10() {
|
||||
let mut list = hello_list();
|
||||
|
||||
@@ -696,6 +696,7 @@ impl Page {
|
||||
math::MathAlphabet::Italic => FontFace::MathItalic,
|
||||
math::MathAlphabet::Sans => FontFace::Sans,
|
||||
math::MathAlphabet::Mono => FontFace::Mono,
|
||||
math::MathAlphabet::Calligraphic => FontFace::MathSymbol,
|
||||
};
|
||||
let mut cursor = x;
|
||||
for character in text.chars() {
|
||||
|
||||
Reference in new issue
Block a user