Add sans-serif bold and italic faces
Map \sffamily with \bfseries or \itshape onto the bundled cmssbx10 and cmssi10 metrics, with their Type1 outlines embedded alongside the other Computer Modern faces. Bold italic sans substitutes cmssbx10, matching the OT1 fallback in TeX.
This commit is contained in:
1 parent
03925e3116
commit
9140990a7d
13 files changed
+223
-19
No files matched your search
@@ -15,6 +15,8 @@ pub enum Cmr10Metrics {
|
||||
BoldItalic,
|
||||
Mono,
|
||||
Sans,
|
||||
SansBold,
|
||||
SansItalic,
|
||||
/// Calligraphic capitals from cmsy10 (`\mathcal`).
|
||||
MathCalligraphic,
|
||||
/// Blackboard-bold capitals from msbm10 (`\mathbb`).
|
||||
@@ -31,6 +33,8 @@ static CMTI10_TFM: &[u8] = include_bytes!("../assets/cmti10.tfm");
|
||||
static CMBXTI10_TFM: &[u8] = include_bytes!("../assets/cmbxti10.tfm");
|
||||
static CMTT10_TFM: &[u8] = include_bytes!("../assets/cmtt10.tfm");
|
||||
static CMSS10_TFM: &[u8] = include_bytes!("../assets/cmss10.tfm");
|
||||
static CMSSBX10_TFM: &[u8] = include_bytes!("../assets/cmssbx10.tfm");
|
||||
static CMSSI10_TFM: &[u8] = include_bytes!("../assets/cmssi10.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");
|
||||
@@ -43,6 +47,8 @@ static CMTI10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMBXTI10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMTT10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMSS10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMSSBX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMSSI10_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();
|
||||
@@ -77,6 +83,14 @@ fn parse_sans() -> TfmMetrics {
|
||||
parse(CMSS10_TFM)
|
||||
}
|
||||
|
||||
fn parse_sans_bold() -> TfmMetrics {
|
||||
parse(CMSSBX10_TFM)
|
||||
}
|
||||
|
||||
fn parse_sans_italic() -> TfmMetrics {
|
||||
parse(CMSSI10_TFM)
|
||||
}
|
||||
|
||||
fn parse_math_italic() -> TfmMetrics {
|
||||
parse(CMMI10_TFM)
|
||||
}
|
||||
@@ -125,6 +139,8 @@ fn metrics_for(font: Cmr10Metrics) -> &'static TfmMetrics {
|
||||
Cmr10Metrics::BoldItalic => CMBXTI10_METRICS.get_or_init(parse_bold_italic),
|
||||
Cmr10Metrics::Mono => CMTT10_METRICS.get_or_init(parse_mono),
|
||||
Cmr10Metrics::Sans => CMSS10_METRICS.get_or_init(parse_sans),
|
||||
Cmr10Metrics::SansBold => CMSSBX10_METRICS.get_or_init(parse_sans_bold),
|
||||
Cmr10Metrics::SansItalic => CMSSI10_METRICS.get_or_init(parse_sans_italic),
|
||||
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),
|
||||
@@ -146,6 +162,8 @@ impl Cmr10Metrics {
|
||||
ParagraphFont::BoldItalic => Self::BoldItalic,
|
||||
ParagraphFont::Mono => Self::Mono,
|
||||
ParagraphFont::Sans => Self::Sans,
|
||||
ParagraphFont::SansBold => Self::SansBold,
|
||||
ParagraphFont::SansItalic => Self::SansItalic,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,6 +180,8 @@ impl Cmr10Metrics {
|
||||
8 => Self::Math,
|
||||
9 => Self::MathBlackboard,
|
||||
10 => Self::MathFraktur,
|
||||
11 => Self::SansBold,
|
||||
12 => Self::SansItalic,
|
||||
_ => Self::Roman,
|
||||
}
|
||||
}
|
||||
@@ -660,4 +680,18 @@ mod cmex_delimiter_tests {
|
||||
assert!(cmex_delimiter('.', 1).is_none());
|
||||
assert!(cmex_delimiter('x', 1).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sans_bold_and_italic_use_their_own_metrics() {
|
||||
let regular = Cmr10Metrics::Sans.width('A');
|
||||
let bold = Cmr10Metrics::SansBold.width('A');
|
||||
let italic = Cmr10Metrics::SansItalic.width('A');
|
||||
assert_eq!(bold, metrics_for(Cmr10Metrics::SansBold).width('A'));
|
||||
assert_eq!(italic, metrics_for(Cmr10Metrics::SansItalic).width('A'));
|
||||
assert_ne!(regular, bold);
|
||||
assert_ne!(Cmr10Metrics::Sans.space(), Cmr10Metrics::SansBold.space());
|
||||
assert!(italic > 0);
|
||||
assert_eq!(Cmr10Metrics::for_pdf_slot(11), Cmr10Metrics::SansBold);
|
||||
assert_eq!(Cmr10Metrics::for_pdf_slot(12), Cmr10Metrics::SansItalic);
|
||||
}
|
||||
}
|
||||
@@ -6236,6 +6236,30 @@ mod tests {
|
||||
assert!(svg.contains("font-family=\"sans-serif\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sans_bold_paragraph_paints_with_the_sans_bold_face() {
|
||||
let unit = Unit::paragraph_with_face(1, "sans bold", ParagraphFont::SansBold);
|
||||
let page = Page {
|
||||
items: unit.items,
|
||||
insertions: Vec::new(),
|
||||
marks: Vec::new(),
|
||||
floats: Vec::new(),
|
||||
sources: Vec::new(),
|
||||
hash: 0,
|
||||
};
|
||||
let list = page.display_list();
|
||||
assert!(list.items.iter().any(|item| matches!(
|
||||
item,
|
||||
crate::render::DisplayItem::Text {
|
||||
font: crate::render::FontFace::SansBold,
|
||||
..
|
||||
}
|
||||
)));
|
||||
let svg = list.to_svg();
|
||||
assert!(svg.contains("font-family=\"sans-serif\""));
|
||||
assert!(svg.contains("font-weight=\"bold\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mono_paragraph_paints_with_a_mono_face() {
|
||||
let unit = Unit::paragraph_with_face(1, "code 123", ParagraphFont::Mono);
|
||||
|
||||
@@ -293,6 +293,8 @@ pub enum ParagraphFont {
|
||||
BoldItalic,
|
||||
Mono,
|
||||
Sans,
|
||||
SansBold,
|
||||
SansItalic,
|
||||
}
|
||||
|
||||
impl ParagraphFont {
|
||||
@@ -303,7 +305,10 @@ impl ParagraphFont {
|
||||
Self::Italic => Self::Roman,
|
||||
Self::Bold => Self::BoldItalic,
|
||||
Self::BoldItalic => Self::Bold,
|
||||
Self::Mono | Self::Sans => self,
|
||||
Self::Mono => self,
|
||||
Self::Sans => Self::SansItalic,
|
||||
Self::SansBold => Self::SansBold,
|
||||
Self::SansItalic => Self::Sans,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -312,7 +317,8 @@ impl ParagraphFont {
|
||||
match self {
|
||||
Self::Roman | Self::Bold => Self::Bold,
|
||||
Self::Italic | Self::BoldItalic => Self::BoldItalic,
|
||||
Self::Mono | Self::Sans => self,
|
||||
Self::Mono => self,
|
||||
Self::Sans | Self::SansBold | Self::SansItalic => Self::SansBold,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,7 +327,9 @@ impl ParagraphFont {
|
||||
match self {
|
||||
Self::Bold | Self::Roman => Self::Roman,
|
||||
Self::BoldItalic | Self::Italic => Self::Italic,
|
||||
Self::Mono | Self::Sans => self,
|
||||
Self::Mono => self,
|
||||
Self::Sans | Self::SansItalic => self,
|
||||
Self::SansBold => Self::Sans,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,7 +338,10 @@ impl ParagraphFont {
|
||||
match self {
|
||||
Self::Roman | Self::Italic => Self::Italic,
|
||||
Self::Bold | Self::BoldItalic => Self::BoldItalic,
|
||||
Self::Mono | Self::Sans => self,
|
||||
Self::Mono => self,
|
||||
Self::Sans => Self::SansItalic,
|
||||
Self::SansBold => Self::SansBold,
|
||||
Self::SansItalic => Self::SansItalic,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,7 +350,9 @@ impl ParagraphFont {
|
||||
match self {
|
||||
Self::Roman | Self::Italic => Self::Roman,
|
||||
Self::Bold | Self::BoldItalic => Self::Bold,
|
||||
Self::Mono | Self::Sans => self,
|
||||
Self::Mono => self,
|
||||
Self::Sans | Self::SansItalic => Self::Sans,
|
||||
Self::SansBold => Self::SansBold,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -347,9 +360,20 @@ impl ParagraphFont {
|
||||
pub const fn roman_family(self) -> Self {
|
||||
match self {
|
||||
Self::Mono | Self::Sans => Self::Roman,
|
||||
Self::SansBold => Self::Bold,
|
||||
Self::SansItalic => Self::Italic,
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
/// `\textsf`: change family, keeping the inherited weight and shape.
|
||||
pub const fn sans_family(self) -> Self {
|
||||
match self {
|
||||
Self::Bold | Self::BoldItalic => Self::SansBold,
|
||||
Self::Italic => Self::SansItalic,
|
||||
_ => Self::Sans,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ParagraphFont {
|
||||
@@ -2309,3 +2333,32 @@ mod tests {
|
||||
assert_eq!(measured(1), 6, "looser paragraph");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod font_tests {
|
||||
use super::ParagraphFont;
|
||||
|
||||
#[test]
|
||||
fn sans_faces_compose_with_weight_and_shape() {
|
||||
assert_eq!(ParagraphFont::Sans.bolded(), ParagraphFont::SansBold);
|
||||
assert_eq!(ParagraphFont::Sans.emphasized(), ParagraphFont::SansItalic);
|
||||
assert_eq!(ParagraphFont::SansItalic.emphasized(), ParagraphFont::Sans);
|
||||
assert_eq!(
|
||||
ParagraphFont::SansBold.emphasized(),
|
||||
ParagraphFont::SansBold
|
||||
);
|
||||
assert_eq!(ParagraphFont::SansBold.medium(), ParagraphFont::Sans);
|
||||
assert_eq!(ParagraphFont::SansItalic.upright(), ParagraphFont::Sans);
|
||||
assert_eq!(ParagraphFont::SansBold.roman_family(), ParagraphFont::Bold);
|
||||
assert_eq!(
|
||||
ParagraphFont::SansItalic.roman_family(),
|
||||
ParagraphFont::Italic
|
||||
);
|
||||
assert_eq!(ParagraphFont::Bold.sans_family(), ParagraphFont::SansBold);
|
||||
assert_eq!(
|
||||
ParagraphFont::Italic.sans_family(),
|
||||
ParagraphFont::SansItalic
|
||||
);
|
||||
assert_eq!(ParagraphFont::Mono.sans_family(), ParagraphFont::Sans);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ use crate::cmr10::Cmr10Metrics;
|
||||
use crate::render::{DisplayItem, DisplayList, FontFace};
|
||||
use crate::type1::Type1Font;
|
||||
|
||||
const FACE_COUNT: usize = 11;
|
||||
const FACE_COUNT: usize = 13;
|
||||
|
||||
fn deflate(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||
@@ -98,6 +98,18 @@ static FULL_FACES: [PdfFace; FACE_COUNT] = [
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset/LiberationSans-Bold.ttf"),
|
||||
base: "LiberationSans-Bold",
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset/LiberationSans-Italic.ttf"),
|
||||
base: "LiberationSans-Italic",
|
||||
italic_angle: -12,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
];
|
||||
|
||||
static MIN_FACES: [PdfFace; FACE_COUNT] = [
|
||||
@@ -167,6 +179,18 @@ static MIN_FACES: [PdfFace; FACE_COUNT] = [
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSans-Bold.ttf"),
|
||||
base: "LiberationSans-Bold",
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSans-Italic.ttf"),
|
||||
base: "LiberationSans-Italic",
|
||||
italic_angle: -12,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
];
|
||||
|
||||
fn minimal_text_char(ch: char) -> bool {
|
||||
@@ -300,6 +324,8 @@ fn font_resource(font: FontFace) -> &'static str {
|
||||
FontFace::MathExtension => "F9",
|
||||
FontFace::MathBlackboard => "F10",
|
||||
FontFace::MathFraktur => "F11",
|
||||
FontFace::SansBold => "F12",
|
||||
FontFace::SansItalic => "F13",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,6 +352,8 @@ fn type1_bytes(slot: usize) -> Option<&'static [u8]> {
|
||||
Some(include_bytes!("../../../assets/fonts/cmex10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/msbm10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/eufm10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmssbx10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmssi10.pfb")),
|
||||
];
|
||||
PFBS[slot]
|
||||
}
|
||||
@@ -931,6 +959,36 @@ mod type1_pdf_tests {
|
||||
assert!(!text.contains("/BaseFont /CMR10"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sans_bold_text_embeds_cmssbx10() {
|
||||
let mut list = hello_list();
|
||||
list.items[0] = DisplayItem::Text {
|
||||
x: 0,
|
||||
y: 800_000,
|
||||
size: 655_360,
|
||||
text: "Sans".to_string(),
|
||||
font: FontFace::SansBold,
|
||||
};
|
||||
let pdf = write(&[list]);
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("/BaseFont /CMSSBX10"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sans_italic_text_embeds_cmssi10() {
|
||||
let mut list = hello_list();
|
||||
list.items[0] = DisplayItem::Text {
|
||||
x: 0,
|
||||
y: 800_000,
|
||||
size: 655_360,
|
||||
text: "Sans".to_string(),
|
||||
font: FontFace::SansItalic,
|
||||
};
|
||||
let pdf = write(&[list]);
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("/BaseFont /CMSSI10"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn math_italic_text_embeds_cmmi10() {
|
||||
let mut list = hello_list();
|
||||
|
||||
@@ -29,6 +29,10 @@ pub enum FontFace {
|
||||
MathBlackboard,
|
||||
/// Euler Fraktur (`eufm10`).
|
||||
MathFraktur,
|
||||
/// Computer Modern sans-serif bold (`cmssbx10`).
|
||||
SansBold,
|
||||
/// Computer Modern sans-serif italic (`cmssi10`).
|
||||
SansItalic,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
@@ -159,19 +163,28 @@ impl DisplayList {
|
||||
let size = *size as f64 / 65_536.0;
|
||||
let family = if matches!(font, FontFace::Mono) {
|
||||
"monospace"
|
||||
} else if matches!(font, FontFace::Sans) {
|
||||
} else if matches!(
|
||||
font,
|
||||
FontFace::Sans | FontFace::SansBold | FontFace::SansItalic
|
||||
) {
|
||||
"sans-serif"
|
||||
} else {
|
||||
"serif"
|
||||
};
|
||||
let weight = if matches!(font, FontFace::Bold | FontFace::BoldItalic) {
|
||||
let weight = if matches!(
|
||||
font,
|
||||
FontFace::Bold | FontFace::BoldItalic | FontFace::SansBold
|
||||
) {
|
||||
" font-weight=\"bold\""
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let style = if matches!(
|
||||
font,
|
||||
FontFace::Italic | FontFace::BoldItalic | FontFace::MathItalic
|
||||
FontFace::Italic
|
||||
| FontFace::BoldItalic
|
||||
| FontFace::MathItalic
|
||||
| FontFace::SansItalic
|
||||
) {
|
||||
" font-style=\"italic\""
|
||||
} else {
|
||||
@@ -271,6 +284,8 @@ fn paragraph_face(font: ParagraphFont) -> FontFace {
|
||||
ParagraphFont::BoldItalic => FontFace::BoldItalic,
|
||||
ParagraphFont::Mono => FontFace::Mono,
|
||||
ParagraphFont::Sans => FontFace::Sans,
|
||||
ParagraphFont::SansBold => FontFace::SansBold,
|
||||
ParagraphFont::SansItalic => FontFace::SansItalic,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user