Draw mathbb letters from msbm10
This commit is contained in:
1 parent
e229809581
commit
cb40cc4634
7 files changed
+122
-1
No files matched your search
Binary file not shown.
Binary file not shown.
@@ -17,6 +17,8 @@ pub enum Cmr10Metrics {
|
||||
Sans,
|
||||
/// Calligraphic capitals from cmsy10 (`\mathcal`).
|
||||
MathCalligraphic,
|
||||
/// Blackboard-bold capitals from msbm10 (`\mathbb`).
|
||||
MathBlackboard,
|
||||
/// Math face: italic letters from cmmi10, big operators from cmex10.
|
||||
Math,
|
||||
}
|
||||
@@ -30,6 +32,7 @@ static CMSS10_TFM: &[u8] = include_bytes!("../assets/cmss10.tfm");
|
||||
static CMMI10_TFM: &[u8] = include_bytes!("../assets/cmmi10.tfm");
|
||||
static CMEX10_TFM: &[u8] = include_bytes!("../assets/cmex10.tfm");
|
||||
static CMSY10_TFM: &[u8] = include_bytes!("../assets/cmsy10.tfm");
|
||||
static MSBM10_TFM: &[u8] = include_bytes!("../assets/msbm10.tfm");
|
||||
|
||||
static CMR10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMBX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
@@ -40,6 +43,7 @@ static CMSS10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMMI10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMEX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMSY10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static MSBM10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
|
||||
fn parse(bytes: &[u8]) -> TfmMetrics {
|
||||
TfmMetrics::parse(bytes).expect("embedded TFM must be valid")
|
||||
@@ -93,6 +97,14 @@ fn math_symbol_metrics() -> &'static TfmMetrics {
|
||||
CMSY10_METRICS.get_or_init(parse_math_symbol)
|
||||
}
|
||||
|
||||
fn parse_blackboard() -> TfmMetrics {
|
||||
parse(MSBM10_TFM)
|
||||
}
|
||||
|
||||
fn math_blackboard_metrics() -> &'static TfmMetrics {
|
||||
MSBM10_METRICS.get_or_init(parse_blackboard)
|
||||
}
|
||||
|
||||
fn metrics_for(font: Cmr10Metrics) -> &'static TfmMetrics {
|
||||
match font {
|
||||
Cmr10Metrics::Roman => CMR10_METRICS.get_or_init(parse_roman),
|
||||
@@ -103,6 +115,7 @@ fn metrics_for(font: Cmr10Metrics) -> &'static TfmMetrics {
|
||||
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),
|
||||
Cmr10Metrics::MathBlackboard => MSBM10_METRICS.get_or_init(parse_blackboard),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,6 +146,8 @@ impl Cmr10Metrics {
|
||||
5 => Self::Sans,
|
||||
6 => Self::Math,
|
||||
7 => Self::Math,
|
||||
8 => Self::Math,
|
||||
9 => Self::MathBlackboard,
|
||||
_ => Self::Roman,
|
||||
}
|
||||
}
|
||||
@@ -148,6 +163,12 @@ impl Cmr10Metrics {
|
||||
}
|
||||
return Self::Math.width(character);
|
||||
}
|
||||
if *self == Self::MathBlackboard {
|
||||
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 {
|
||||
@@ -179,6 +200,12 @@ impl Cmr10Metrics {
|
||||
}
|
||||
return Self::Math.height(character);
|
||||
}
|
||||
if *self == Self::MathBlackboard {
|
||||
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 {
|
||||
@@ -203,6 +230,12 @@ impl Cmr10Metrics {
|
||||
}
|
||||
return Self::Math.depth(character);
|
||||
}
|
||||
if *self == Self::MathBlackboard {
|
||||
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 {
|
||||
@@ -227,6 +260,12 @@ impl Cmr10Metrics {
|
||||
}
|
||||
return Self::Math.italic(character);
|
||||
}
|
||||
if *self == Self::MathBlackboard {
|
||||
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);
|
||||
}
|
||||
@@ -338,6 +377,17 @@ pub fn math_symbol_width(character: char) -> Option<i64> {
|
||||
.filter(|width| *width > 0)
|
||||
}
|
||||
|
||||
/// Advance width of a `msbm10` blackboard-bold capital.
|
||||
pub fn math_blackboard_width(character: char) -> Option<i64> {
|
||||
if character.is_ascii_uppercase() {
|
||||
let width = math_blackboard_metrics().width(character);
|
||||
if width > 0 {
|
||||
return Some(width);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// 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)
|
||||
|
||||
@@ -4212,6 +4212,29 @@ mod tests {
|
||||
)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mathbb_paints_with_the_msbm_face() {
|
||||
use crate::render::{DisplayItem, FontFace};
|
||||
let document = Document::new(
|
||||
1_000_000,
|
||||
vec![Unit::math(
|
||||
1,
|
||||
crate::math::parse_math("\\mathbb{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::MathBlackboard,
|
||||
..
|
||||
} if text == "A"
|
||||
)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edit_on_second_page_only_rebuilds_second_page() {
|
||||
let mut document = document();
|
||||
|
||||
@@ -14,6 +14,7 @@ pub enum MathAlphabet {
|
||||
Sans,
|
||||
Mono,
|
||||
Calligraphic,
|
||||
Blackboard,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
@@ -307,6 +308,7 @@ pub(crate) fn alphabet_metrics(font: Cmr10Metrics, alphabet: MathAlphabet) -> Cm
|
||||
Cmr10Metrics::for_paragraph_font(crate::paragraph::ParagraphFont::Mono)
|
||||
}
|
||||
MathAlphabet::Calligraphic => Cmr10Metrics::MathCalligraphic,
|
||||
MathAlphabet::Blackboard => Cmr10Metrics::MathBlackboard,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1347,6 +1349,7 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
||||
"mathsf" => Some(MathAlphabet::Sans),
|
||||
"mathtt" => Some(MathAlphabet::Mono),
|
||||
"mathcal" => Some(MathAlphabet::Calligraphic),
|
||||
"mathbb" => Some(MathAlphabet::Blackboard),
|
||||
_ => None,
|
||||
};
|
||||
match alphabet {
|
||||
@@ -1571,6 +1574,18 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mathbb_uses_blackboard_widths() {
|
||||
let font = Cmr10Metrics::default();
|
||||
let blackboard = layout_math(&parse_math("\\mathbb{A}"), &font);
|
||||
let upright = layout_math(&parse_math("\\mathrm{A}"), &font);
|
||||
assert_ne!(blackboard.width, upright.width);
|
||||
assert_eq!(
|
||||
blackboard.width,
|
||||
crate::cmr10::math_blackboard_width('A').expect("msbm has blackboard capitals")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smash_can_target_only_the_top_or_bottom() {
|
||||
let font = Cmr10Metrics::default();
|
||||
|
||||
@@ -16,7 +16,7 @@ use crate::cmr10::Cmr10Metrics;
|
||||
use crate::render::{DisplayItem, DisplayList, FontFace};
|
||||
use crate::type1::Type1Font;
|
||||
|
||||
const FACE_COUNT: usize = 9;
|
||||
const FACE_COUNT: usize = 10;
|
||||
|
||||
fn deflate(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||
@@ -86,6 +86,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-MathBlackboard",
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
];
|
||||
|
||||
static MIN_FACES: [PdfFace; FACE_COUNT] = [
|
||||
@@ -143,6 +149,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-MathBlackboard",
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
];
|
||||
|
||||
fn minimal_text_char(ch: char) -> bool {
|
||||
@@ -274,6 +286,7 @@ fn font_resource(font: FontFace) -> &'static str {
|
||||
FontFace::MathItalic => "F7",
|
||||
FontFace::MathSymbol => "F8",
|
||||
FontFace::MathExtension => "F9",
|
||||
FontFace::MathBlackboard => "F10",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,6 +311,7 @@ fn type1_bytes(slot: usize) -> Option<&'static [u8]> {
|
||||
Some(include_bytes!("../../../assets/fonts/cmmi10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmsy10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmex10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/msbm10.pfb")),
|
||||
];
|
||||
PFBS[slot]
|
||||
}
|
||||
@@ -937,6 +951,22 @@ mod type1_pdf_tests {
|
||||
assert!(!text.contains("/BaseFont /CMR10"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mathbb_letters_embed_msbm10() {
|
||||
let mut list = hello_list();
|
||||
list.items[0] = DisplayItem::Text {
|
||||
x: 0,
|
||||
y: 800_000,
|
||||
size: 655_360,
|
||||
text: "A".to_string(),
|
||||
font: FontFace::MathBlackboard,
|
||||
};
|
||||
let pdf = write(&[list]);
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("/BaseFont /MSBM10"), "{text}");
|
||||
assert!(text.contains("(\\101)"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mathcal_letters_embed_cmsy10() {
|
||||
let mut list = hello_list();
|
||||
|
||||
@@ -25,6 +25,8 @@ pub enum FontFace {
|
||||
MathSymbol,
|
||||
/// Computer Modern math extensions (`cmex10`), addressed by TeX slot.
|
||||
MathExtension,
|
||||
/// Computer Modern blackboard bold (`msbm10`).
|
||||
MathBlackboard,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
@@ -697,6 +699,7 @@ impl Page {
|
||||
math::MathAlphabet::Sans => FontFace::Sans,
|
||||
math::MathAlphabet::Mono => FontFace::Mono,
|
||||
math::MathAlphabet::Calligraphic => FontFace::MathSymbol,
|
||||
math::MathAlphabet::Blackboard => FontFace::MathBlackboard,
|
||||
};
|
||||
let mut cursor = x;
|
||||
for character in text.chars() {
|
||||
|
||||
Reference in new issue
Block a user