Draw big operators with cmex10 outlines

This commit is contained in:
gloamxun committed 2026-10-24 03:46:50 +08:00
1 parent 6446de6b6b
commit 66ceabad0e
7 files changed
+167 -28

No files matched your search

+56 -16
View File
@@ -152,6 +152,12 @@ impl Cmr10Metrics {
return width;
}
}
if let Some(slot) = math_extension_slot(character) {
let width = math_extension_metrics().width(char::from(slot));
if width > 0 {
return width;
}
}
}
let width = self.metrics().width(character);
if width > 0 { width } else { 327_680 }
@@ -168,6 +174,9 @@ impl Cmr10Metrics {
if let Some(slot) = math_symbol_slot(character) {
return math_symbol_metrics().height(char::from(slot));
}
if let Some(slot) = math_extension_slot(character) {
return math_extension_metrics().height(char::from(slot));
}
}
self.metrics().height(character)
}
@@ -183,6 +192,9 @@ impl Cmr10Metrics {
if let Some(slot) = math_symbol_slot(character) {
return math_symbol_metrics().depth(char::from(slot));
}
if let Some(slot) = math_extension_slot(character) {
return math_extension_metrics().depth(char::from(slot));
}
}
self.metrics().depth(character)
}
@@ -195,6 +207,9 @@ impl Cmr10Metrics {
if let Some(slot) = math_symbol_slot(character) {
return math_symbol_metrics().italic(char::from(slot));
}
if let Some(slot) = math_extension_slot(character) {
return math_extension_metrics().italic(char::from(slot));
}
}
self.metrics().italic(character)
}
@@ -234,28 +249,53 @@ impl Cmr10Metrics {
/// Metrics for a named Computer Modern extension big operator (`\sum` and friends).
pub fn cmex_operator(name: &str) -> Option<(i64, i64, i64)> {
let slot = match name {
"sum" => 'P',
"prod" => 'Q',
"coprod" => 'R',
"int" => 'S',
"oint" => 'T',
"bigcap" => 'U',
"bigcup" => 'V',
"bigsqcup" => 'W',
"bigvee" => 'X',
"bigwedge" => 'Y',
"bigodot" => 'Z',
"bigotimes" => '[',
"bigoplus" => '\\',
"biguplus" => ']',
"sum" => 80,
"prod" => 81,
"coprod" => 96,
"int" => 82,
"oint" => 72,
"bigcap" => 84,
"bigcup" => 83,
"bigsqcup" => 85,
"bigvee" => 87,
"bigwedge" => 86,
"bigodot" => 74,
"bigotimes" => 78,
"bigoplus" => 76,
"biguplus" => 85,
_ => return None,
};
let metrics = math_extension_metrics();
let width = metrics.width(slot);
let width = metrics.width(char::from(slot));
if width == 0 {
return None;
}
Some((width, metrics.height(slot), metrics.depth(slot)))
Some((
width,
metrics.height(char::from(slot)),
metrics.depth(char::from(slot)),
))
}
/// Map a Unicode n-ary operator to its slot in the bundled cmex10 font.
pub fn math_extension_slot(character: char) -> Option<u8> {
let slot = match character {
'\u{2211}' => 80, // \sum
'\u{220F}' => 81, // \prod
'\u{2210}' => 96, // \coprod
'\u{222B}' => 82, // \int
'\u{222E}' => 72, // \oint
'\u{22C2}' => 84, // \bigcap
'\u{22C3}' => 83, // \bigcup
'\u{2A06}' => 85, // \bigsqcup
'\u{22C1}' => 87, // \bigvee
'\u{22C0}' => 86, // \bigwedge
'\u{2A00}' => 74, // \bigodot
'\u{2A02}' => 78, // \bigotimes
'\u{2A01}' => 76, // \bigoplus
_ => return None,
};
Some(slot)
}
#[cfg(test)]
+28
View File
@@ -6413,6 +6413,34 @@ mod tests {
assert!(svg.contains(">x</text>"));
}
#[test]
fn big_operators_paint_with_the_cmex_face() {
let atom = crate::math::parse_math("\\sum_{i=1}^{n}");
let document = Document::new(
400,
vec![Unit::math(1, atom, &crate::cmr10::Cmr10Metrics::new())],
);
let engine = Engine::layout(&document);
let list = engine.pages()[0].display_list();
let item = list
.items
.iter()
.find(|item| {
matches!(
item,
DisplayItem::Text {
font: crate::render::FontFace::MathExtension,
..
}
)
})
.expect("sum should use the CMEX face");
if let DisplayItem::Text { text, .. } = item {
assert_eq!(text, "\u{2211}");
}
assert!(list.to_svg().contains('\u{2211}'));
}
#[test]
fn bottom_float_content_is_painted_at_the_page_bottom() {
let mut document = Document::new(100, Vec::new());
+21
View File
@@ -835,6 +835,27 @@ fn parse_delimiter(chars: &[char], index: usize) -> (char, usize) {
}
}
/// Unicode n-ary operator for a `\sum`-style big operator name.
pub(crate) fn big_operator_character(name: &str) -> Option<char> {
let character = match name {
"sum" => '\u{2211}',
"prod" => '\u{220F}',
"coprod" => '\u{2210}',
"int" => '\u{222B}',
"oint" => '\u{222E}',
"bigcap" => '\u{22C2}',
"bigcup" => '\u{22C3}',
"bigsqcup" => '\u{2A06}',
"bigvee" => '\u{22C1}',
"bigwedge" => '\u{22C0}',
"bigodot" => '\u{2A00}',
"bigotimes" => '\u{2A02}',
"bigoplus" => '\u{2A01}',
_ => return None,
};
Some(character)
}
fn math_symbol_command(name: &str) -> Option<char> {
let character = match name {
"cdot" => '\u{00B7}',
+42 -4
View File
@@ -16,7 +16,7 @@ use crate::cmr10::Cmr10Metrics;
use crate::render::{DisplayItem, DisplayList, FontFace};
use crate::type1::Type1Font;
const FACE_COUNT: usize = 8;
const FACE_COUNT: usize = 9;
fn deflate(bytes: &[u8]) -> Vec<u8> {
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
@@ -80,6 +80,12 @@ static FULL_FACES: [PdfFace; FACE_COUNT] = [
italic_angle: 0,
font: OnceLock::new(),
},
PdfFace {
bytes: include_bytes!("../../../assets/fonts-subset/LiberationSerif-Regular.ttf"),
base: "LiberationSerif-MathExtension",
italic_angle: 0,
font: OnceLock::new(),
},
];
static MIN_FACES: [PdfFace; FACE_COUNT] = [
@@ -131,6 +137,12 @@ static MIN_FACES: [PdfFace; FACE_COUNT] = [
italic_angle: 0,
font: OnceLock::new(),
},
PdfFace {
bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSerif-Regular.ttf"),
base: "LiberationSerif-MathExtension",
italic_angle: 0,
font: OnceLock::new(),
},
];
fn minimal_text_char(ch: char) -> bool {
@@ -261,6 +273,7 @@ fn font_resource(font: FontFace) -> &'static str {
FontFace::Sans => "F6",
FontFace::MathItalic => "F7",
FontFace::MathSymbol => "F8",
FontFace::MathExtension => "F9",
}
}
@@ -284,6 +297,7 @@ fn type1_bytes(slot: usize) -> Option<&'static [u8]> {
Some(include_bytes!("../../../assets/fonts/cmss10.pfb")),
Some(include_bytes!("../../../assets/fonts/cmmi10.pfb")),
Some(include_bytes!("../../../assets/fonts/cmsy10.pfb")),
Some(include_bytes!("../../../assets/fonts/cmex10.pfb")),
];
PFBS[slot]
}
@@ -497,11 +511,16 @@ fn write_pdf(
return None;
}
let font = type1_fonts()[slot].as_ref()?;
if slot == FontFace::MathSymbol as usize {
if slot == FontFace::MathSymbol as usize || slot == FontFace::MathExtension as usize {
let codes = used_chars[slot]
.iter()
.filter_map(|character| {
crate::cmr10::math_symbol_slot(*character).map(|code| (*character, code))
let code = if slot == FontFace::MathSymbol as usize {
crate::cmr10::math_symbol_slot(*character)
} else {
crate::cmr10::math_extension_slot(*character)
};
code.map(|code| (*character, code))
})
.collect();
return Some((font, codes));
@@ -552,7 +571,9 @@ fn write_pdf(
(metrics.width(*character) * 100 / 65_536) as i64
});
widths.push(width.to_string());
let glyph = if slot == FontFace::MathSymbol as usize {
let glyph = if slot == FontFace::MathSymbol as usize
|| slot == FontFace::MathExtension as usize
{
font.name_for_code(code).map(|name| (code, name))
} else {
by_code
@@ -886,6 +907,23 @@ mod type1_pdf_tests {
assert!(!text.contains("/BaseFont /CMR10"), "{text}");
}
#[test]
fn math_operators_embed_cmex10() {
let mut list = hello_list();
list.items[0] = DisplayItem::Text {
x: 0,
y: 800_000,
size: 655_360,
text: "\u{2211}".to_string(),
font: FontFace::MathExtension,
};
let pdf = write(&[list]);
let text = String::from_utf8_lossy(&pdf);
assert!(text.contains("/BaseFont /CMEX10"), "{text}");
assert!(text.contains("/summationtext"), "{text}");
assert!(!text.contains("/BaseFont /CMR10"), "{text}");
}
#[test]
fn math_symbols_embed_cmsy10() {
let mut list = hello_list();
+15 -3
View File
@@ -23,6 +23,8 @@ pub enum FontFace {
MathItalic,
/// Computer Modern math symbols (`cmsy10`), addressed by TeX slot.
MathSymbol,
/// Computer Modern math extensions (`cmex10`), addressed by TeX slot.
MathExtension,
}
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -1069,14 +1071,24 @@ impl Page {
});
}
MathAtom::BigOperator { name } => {
for (index, character) in name.chars().enumerate() {
if let Some(character) = math::big_operator_character(name) {
list.push(DisplayItem::Text {
font: FontFace::Roman,
x: x + index as i64 * font.width(character),
font: FontFace::MathExtension,
x,
y: baseline,
size,
text: character.to_string(),
});
} else {
for (index, character) in name.chars().enumerate() {
list.push(DisplayItem::Text {
font: FontFace::Roman,
x: x + index as i64 * font.width(character),
y: baseline,
size,
text: character.to_string(),
});
}
}
}
MathAtom::SizedDelimiter { delimiter, .. } => {