Distinguish slanted from italic and add italic typewriter
Add cmsl10/cmbxsl10/cmitt10 faces so \slshape and \textsl no longer reuse the italic cmti10 and \ttfamily\itshape reaches cmitt10. The NFSS state tracks italic, slanted and small caps separately, with last-shape-wins semantics for \em and the shape commands.
This commit is contained in:
1 parent
6df38ced25
commit
aa4d86d735
15 files changed
+328
-34
No files matched your search
@@ -19,6 +19,12 @@ pub enum Cmr10Metrics {
|
||||
SansItalic,
|
||||
/// Roman small caps (cmcsc10).
|
||||
SmallCaps,
|
||||
/// Roman slanted (cmsl10).
|
||||
Slanted,
|
||||
/// Bold extended slanted (cmbxsl10).
|
||||
BoldSlanted,
|
||||
/// Italic typewriter (cmitt10).
|
||||
MonoItalic,
|
||||
/// Calligraphic capitals from cmsy10 (`\mathcal`).
|
||||
MathCalligraphic,
|
||||
/// Blackboard-bold capitals from msbm10 (`\mathbb`).
|
||||
@@ -38,6 +44,9 @@ 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 CMCSC10_TFM: &[u8] = include_bytes!("../assets/cmcsc10.tfm");
|
||||
static CMSL10_TFM: &[u8] = include_bytes!("../assets/cmsl10.tfm");
|
||||
static CMBXSL10_TFM: &[u8] = include_bytes!("../assets/cmbxsl10.tfm");
|
||||
static CMITT10_TFM: &[u8] = include_bytes!("../assets/cmitt10.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");
|
||||
@@ -53,6 +62,9 @@ static CMSS10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMSSBX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMSSI10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMCSC10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMSL10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMBXSL10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMITT10_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();
|
||||
@@ -99,6 +111,18 @@ fn parse_small_caps() -> TfmMetrics {
|
||||
parse(CMCSC10_TFM)
|
||||
}
|
||||
|
||||
fn parse_slanted() -> TfmMetrics {
|
||||
parse(CMSL10_TFM)
|
||||
}
|
||||
|
||||
fn parse_bold_slanted() -> TfmMetrics {
|
||||
parse(CMBXSL10_TFM)
|
||||
}
|
||||
|
||||
fn parse_mono_italic() -> TfmMetrics {
|
||||
parse(CMITT10_TFM)
|
||||
}
|
||||
|
||||
fn parse_math_italic() -> TfmMetrics {
|
||||
parse(CMMI10_TFM)
|
||||
}
|
||||
@@ -150,6 +174,9 @@ fn metrics_for(font: Cmr10Metrics) -> &'static TfmMetrics {
|
||||
Cmr10Metrics::SansBold => CMSSBX10_METRICS.get_or_init(parse_sans_bold),
|
||||
Cmr10Metrics::SansItalic => CMSSI10_METRICS.get_or_init(parse_sans_italic),
|
||||
Cmr10Metrics::SmallCaps => CMCSC10_METRICS.get_or_init(parse_small_caps),
|
||||
Cmr10Metrics::Slanted => CMSL10_METRICS.get_or_init(parse_slanted),
|
||||
Cmr10Metrics::BoldSlanted => CMBXSL10_METRICS.get_or_init(parse_bold_slanted),
|
||||
Cmr10Metrics::MonoItalic => CMITT10_METRICS.get_or_init(parse_mono_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),
|
||||
@@ -174,6 +201,9 @@ impl Cmr10Metrics {
|
||||
ParagraphFont::SansBold => Self::SansBold,
|
||||
ParagraphFont::SansItalic => Self::SansItalic,
|
||||
ParagraphFont::SmallCaps => Self::SmallCaps,
|
||||
ParagraphFont::Slanted => Self::Slanted,
|
||||
ParagraphFont::BoldSlanted => Self::BoldSlanted,
|
||||
ParagraphFont::MonoItalic => Self::MonoItalic,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,6 +223,9 @@ impl Cmr10Metrics {
|
||||
11 => Self::SansBold,
|
||||
12 => Self::SansItalic,
|
||||
13 => Self::SmallCaps,
|
||||
14 => Self::Slanted,
|
||||
15 => Self::BoldSlanted,
|
||||
16 => Self::MonoItalic,
|
||||
_ => Self::Roman,
|
||||
}
|
||||
}
|
||||
@@ -708,5 +741,19 @@ mod cmex_delimiter_tests {
|
||||
let small_caps = Cmr10Metrics::SmallCaps.width('a');
|
||||
assert_eq!(small_caps, metrics_for(Cmr10Metrics::SmallCaps).width('a'));
|
||||
assert_ne!(small_caps, Cmr10Metrics::Roman.width('a'));
|
||||
assert_eq!(Cmr10Metrics::for_pdf_slot(14), Cmr10Metrics::Slanted);
|
||||
assert_eq!(Cmr10Metrics::for_pdf_slot(15), Cmr10Metrics::BoldSlanted);
|
||||
assert_eq!(Cmr10Metrics::for_pdf_slot(16), Cmr10Metrics::MonoItalic);
|
||||
let slanted = Cmr10Metrics::Slanted.width('a');
|
||||
assert_eq!(slanted, metrics_for(Cmr10Metrics::Slanted).width('a'));
|
||||
assert_ne!(slanted, Cmr10Metrics::Italic.width('a'));
|
||||
assert_eq!(
|
||||
Cmr10Metrics::BoldSlanted.width('a'),
|
||||
metrics_for(Cmr10Metrics::BoldSlanted).width('a')
|
||||
);
|
||||
assert_eq!(
|
||||
Cmr10Metrics::MonoItalic.width('a'),
|
||||
metrics_for(Cmr10Metrics::MonoItalic).width('a')
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -6283,6 +6283,29 @@ mod tests {
|
||||
assert!(svg.contains("font-family=\"serif\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slanted_paragraph_paints_with_the_slanted_face() {
|
||||
let unit = Unit::paragraph_with_face(1, "slanted text", ParagraphFont::Slanted);
|
||||
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::Slanted,
|
||||
..
|
||||
}
|
||||
)));
|
||||
let svg = list.to_svg();
|
||||
assert!(svg.contains("font-style=\"italic\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mono_paragraph_paints_with_a_mono_face() {
|
||||
let unit = Unit::paragraph_with_face(1, "code 123", ParagraphFont::Mono);
|
||||
|
||||
@@ -297,6 +297,12 @@ pub enum ParagraphFont {
|
||||
SansItalic,
|
||||
/// Roman small caps (`cmcsc10`, TeX `\scshape`).
|
||||
SmallCaps,
|
||||
/// Roman slanted (`cmsl10`, TeX `\slshape`).
|
||||
Slanted,
|
||||
/// Bold extended slanted (`cmbxsl10`).
|
||||
BoldSlanted,
|
||||
/// Italic typewriter (`cmitt10`, TeX `\ttfamily\itshape`).
|
||||
MonoItalic,
|
||||
}
|
||||
|
||||
impl ParagraphFont {
|
||||
@@ -312,6 +318,9 @@ impl ParagraphFont {
|
||||
Self::SansBold => Self::SansBold,
|
||||
Self::SansItalic => Self::Sans,
|
||||
Self::SmallCaps => Self::Italic,
|
||||
Self::Slanted => Self::Italic,
|
||||
Self::BoldSlanted => Self::BoldItalic,
|
||||
Self::MonoItalic => Self::Mono,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,6 +332,8 @@ impl ParagraphFont {
|
||||
Self::Mono => self,
|
||||
Self::Sans | Self::SansBold | Self::SansItalic => Self::SansBold,
|
||||
Self::SmallCaps => Self::Bold,
|
||||
Self::Slanted | Self::BoldSlanted => Self::BoldSlanted,
|
||||
Self::MonoItalic => Self::MonoItalic,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -335,19 +346,39 @@ impl ParagraphFont {
|
||||
Self::Sans | Self::SansItalic => self,
|
||||
Self::SansBold => Self::Sans,
|
||||
Self::SmallCaps => Self::SmallCaps,
|
||||
Self::Slanted | Self::BoldSlanted => Self::Slanted,
|
||||
Self::MonoItalic => Self::MonoItalic,
|
||||
}
|
||||
}
|
||||
|
||||
/// `\textit` / `\textsl`.
|
||||
/// `\textit`.
|
||||
pub const fn italicized(self) -> Self {
|
||||
match self {
|
||||
Self::Roman | Self::Italic => Self::Italic,
|
||||
Self::Bold | Self::BoldItalic => Self::BoldItalic,
|
||||
Self::Mono => self,
|
||||
Self::Mono => Self::MonoItalic,
|
||||
Self::MonoItalic => Self::MonoItalic,
|
||||
Self::Sans => Self::SansItalic,
|
||||
Self::SansBold => Self::SansBold,
|
||||
Self::SansItalic => Self::SansItalic,
|
||||
Self::SmallCaps => Self::Italic,
|
||||
Self::Slanted => Self::Italic,
|
||||
Self::BoldSlanted => Self::BoldItalic,
|
||||
}
|
||||
}
|
||||
|
||||
/// `\textsl`.
|
||||
pub const fn slanted(self) -> Self {
|
||||
match self {
|
||||
Self::Roman | Self::Slanted => Self::Slanted,
|
||||
Self::Bold | Self::BoldSlanted => Self::BoldSlanted,
|
||||
Self::Italic => Self::Slanted,
|
||||
Self::BoldItalic => Self::BoldSlanted,
|
||||
Self::Mono => Self::MonoItalic,
|
||||
Self::MonoItalic => Self::MonoItalic,
|
||||
Self::Sans | Self::SansItalic => Self::SansItalic,
|
||||
Self::SansBold => Self::SansBold,
|
||||
Self::SmallCaps => Self::Slanted,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -360,6 +391,8 @@ impl ParagraphFont {
|
||||
Self::Sans | Self::SansItalic => Self::Sans,
|
||||
Self::SansBold => Self::SansBold,
|
||||
Self::SmallCaps => Self::SmallCaps,
|
||||
Self::Slanted | Self::BoldSlanted => Self::Roman,
|
||||
Self::MonoItalic => Self::Mono,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,14 +415,17 @@ impl ParagraphFont {
|
||||
Self::SmallCaps => Self::SmallCaps,
|
||||
Self::Mono => Self::Mono,
|
||||
Self::Sans | Self::SansBold | Self::SansItalic => self,
|
||||
Self::Slanted => Self::SmallCaps,
|
||||
Self::BoldSlanted => Self::Bold,
|
||||
Self::MonoItalic => Self::Mono,
|
||||
}
|
||||
}
|
||||
|
||||
/// `\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::Bold | Self::BoldItalic | Self::BoldSlanted => Self::SansBold,
|
||||
Self::Italic | Self::Slanted | Self::MonoItalic => Self::SansItalic,
|
||||
_ => Self::Sans,
|
||||
}
|
||||
}
|
||||
@@ -2383,5 +2419,23 @@ mod font_tests {
|
||||
assert_eq!(ParagraphFont::Bold.small_caps(), ParagraphFont::Bold);
|
||||
assert_eq!(ParagraphFont::Italic.small_caps(), ParagraphFont::SmallCaps);
|
||||
assert_eq!(ParagraphFont::SmallCaps.upright(), ParagraphFont::SmallCaps);
|
||||
assert_eq!(ParagraphFont::Slanted.italicized(), ParagraphFont::Italic);
|
||||
assert_eq!(ParagraphFont::Italic.slanted(), ParagraphFont::Slanted);
|
||||
assert_eq!(ParagraphFont::Bold.slanted(), ParagraphFont::BoldSlanted);
|
||||
assert_eq!(ParagraphFont::BoldSlanted.medium(), ParagraphFont::Slanted);
|
||||
assert_eq!(ParagraphFont::Slanted.upright(), ParagraphFont::Roman);
|
||||
assert_eq!(ParagraphFont::Slanted.emphasized(), ParagraphFont::Italic);
|
||||
assert_eq!(ParagraphFont::Mono.italicized(), ParagraphFont::MonoItalic);
|
||||
assert_eq!(ParagraphFont::MonoItalic.upright(), ParagraphFont::Mono);
|
||||
assert_eq!(ParagraphFont::Mono.slanted(), ParagraphFont::MonoItalic);
|
||||
assert_eq!(
|
||||
ParagraphFont::Slanted.small_caps(),
|
||||
ParagraphFont::SmallCaps
|
||||
);
|
||||
assert_eq!(ParagraphFont::BoldSlanted.small_caps(), ParagraphFont::Bold);
|
||||
assert_eq!(
|
||||
ParagraphFont::Slanted.sans_family(),
|
||||
ParagraphFont::SansItalic
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ use crate::cmr10::Cmr10Metrics;
|
||||
use crate::render::{DisplayItem, DisplayList, FontFace};
|
||||
use crate::type1::Type1Font;
|
||||
|
||||
const FACE_COUNT: usize = 14;
|
||||
const FACE_COUNT: usize = 17;
|
||||
|
||||
fn deflate(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||
@@ -116,6 +116,24 @@ static FULL_FACES: [PdfFace; FACE_COUNT] = [
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset/LiberationSerif-Italic.ttf"),
|
||||
base: "LiberationSerif-Slanted",
|
||||
italic_angle: -12,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset/LiberationSerif-BoldItalic.ttf"),
|
||||
base: "LiberationSerif-BoldSlanted",
|
||||
italic_angle: -12,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset/LiberationMono-Italic.ttf"),
|
||||
base: "LiberationMono-Italic",
|
||||
italic_angle: -12,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
];
|
||||
|
||||
static MIN_FACES: [PdfFace; FACE_COUNT] = [
|
||||
@@ -203,6 +221,24 @@ static MIN_FACES: [PdfFace; FACE_COUNT] = [
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSerif-Italic.ttf"),
|
||||
base: "LiberationSerif-Slanted",
|
||||
italic_angle: -12,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSerif-BoldItalic.ttf"),
|
||||
base: "LiberationSerif-BoldSlanted",
|
||||
italic_angle: -12,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationMono-Italic.ttf"),
|
||||
base: "LiberationMono-Italic",
|
||||
italic_angle: -12,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
];
|
||||
|
||||
fn minimal_text_char(ch: char) -> bool {
|
||||
@@ -339,6 +375,9 @@ fn font_resource(font: FontFace) -> &'static str {
|
||||
FontFace::SansBold => "F12",
|
||||
FontFace::SansItalic => "F13",
|
||||
FontFace::SmallCaps => "F14",
|
||||
FontFace::Slanted => "F15",
|
||||
FontFace::BoldSlanted => "F16",
|
||||
FontFace::MonoItalic => "F17",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -368,6 +407,9 @@ fn type1_bytes(slot: usize) -> Option<&'static [u8]> {
|
||||
Some(include_bytes!("../../../assets/fonts/cmssbx10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmssi10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmcsc10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmsl10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmbxsl10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmitt10.pfb")),
|
||||
];
|
||||
PFBS[slot]
|
||||
}
|
||||
@@ -1018,6 +1060,51 @@ mod type1_pdf_tests {
|
||||
assert!(text.contains("/BaseFont /CMCSC10"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slanted_text_embeds_cmsl10() {
|
||||
let mut list = hello_list();
|
||||
list.items[0] = DisplayItem::Text {
|
||||
x: 0,
|
||||
y: 800_000,
|
||||
size: 655_360,
|
||||
text: "Slanted".to_string(),
|
||||
font: FontFace::Slanted,
|
||||
};
|
||||
let pdf = write(&[list]);
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("/BaseFont /CMSL10"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bold_slanted_text_embeds_cmbxsl10() {
|
||||
let mut list = hello_list();
|
||||
list.items[0] = DisplayItem::Text {
|
||||
x: 0,
|
||||
y: 800_000,
|
||||
size: 655_360,
|
||||
text: "Slanted".to_string(),
|
||||
font: FontFace::BoldSlanted,
|
||||
};
|
||||
let pdf = write(&[list]);
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("/BaseFont /CMBXSL10"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mono_italic_text_embeds_cmitt10() {
|
||||
let mut list = hello_list();
|
||||
list.items[0] = DisplayItem::Text {
|
||||
x: 0,
|
||||
y: 800_000,
|
||||
size: 655_360,
|
||||
text: "Mono".to_string(),
|
||||
font: FontFace::MonoItalic,
|
||||
};
|
||||
let pdf = write(&[list]);
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("/BaseFont /CMITT10"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn math_italic_text_embeds_cmmi10() {
|
||||
let mut list = hello_list();
|
||||
|
||||
@@ -35,6 +35,9 @@ pub enum FontFace {
|
||||
SansItalic,
|
||||
/// Computer Modern roman small caps (`cmcsc10`).
|
||||
SmallCaps,
|
||||
Slanted,
|
||||
BoldSlanted,
|
||||
MonoItalic,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
@@ -163,7 +166,7 @@ impl DisplayList {
|
||||
let x = *x as f64 / 65_536.0;
|
||||
let y = *y as f64 / 65_536.0;
|
||||
let size = *size as f64 / 65_536.0;
|
||||
let family = if matches!(font, FontFace::Mono) {
|
||||
let family = if matches!(font, FontFace::Mono | FontFace::MonoItalic) {
|
||||
"monospace"
|
||||
} else if matches!(
|
||||
font,
|
||||
@@ -175,7 +178,10 @@ impl DisplayList {
|
||||
};
|
||||
let weight = if matches!(
|
||||
font,
|
||||
FontFace::Bold | FontFace::BoldItalic | FontFace::SansBold
|
||||
FontFace::Bold
|
||||
| FontFace::BoldItalic
|
||||
| FontFace::SansBold
|
||||
| FontFace::BoldSlanted
|
||||
) {
|
||||
" font-weight=\"bold\""
|
||||
} else {
|
||||
@@ -187,6 +193,9 @@ impl DisplayList {
|
||||
| FontFace::BoldItalic
|
||||
| FontFace::MathItalic
|
||||
| FontFace::SansItalic
|
||||
| FontFace::Slanted
|
||||
| FontFace::BoldSlanted
|
||||
| FontFace::MonoItalic
|
||||
) {
|
||||
" font-style=\"italic\""
|
||||
} else {
|
||||
@@ -289,6 +298,9 @@ fn paragraph_face(font: ParagraphFont) -> FontFace {
|
||||
ParagraphFont::SansBold => FontFace::SansBold,
|
||||
ParagraphFont::SansItalic => FontFace::SansItalic,
|
||||
ParagraphFont::SmallCaps => FontFace::SmallCaps,
|
||||
ParagraphFont::Slanted => FontFace::Slanted,
|
||||
ParagraphFont::BoldSlanted => FontFace::BoldSlanted,
|
||||
ParagraphFont::MonoItalic => FontFace::MonoItalic,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user