Compose nested inline faces relative to the surrounding run

This commit is contained in:
gloamxun committed 2026-10-24 02:15:08 +08:00
1 parent 0e8999671b
commit 4cf64a4d55
6 files changed
+156 -38

No files matched your search

+1
View File
@@ -114,6 +114,7 @@ impl Cmr10Metrics {
ParagraphFont::Roman => Self::Roman,
ParagraphFont::Bold => Self::Bold,
ParagraphFont::Italic => Self::Italic,
ParagraphFont::BoldItalic => Self::BoldItalic,
ParagraphFont::Mono => Self::Mono,
ParagraphFont::Sans => Self::Sans,
}
+58
View File
@@ -290,10 +290,68 @@ pub enum ParagraphFont {
Roman,
Bold,
Italic,
BoldItalic,
Mono,
Sans,
}
impl ParagraphFont {
/// `\emph`: toggles italic, combining with an inherited bold face.
pub const fn emphasized(self) -> Self {
match self {
Self::Roman => Self::Italic,
Self::Italic => Self::Roman,
Self::Bold => Self::BoldItalic,
Self::BoldItalic => Self::Bold,
Self::Mono | Self::Sans => self,
}
}
/// `\textbf`.
pub const fn bolded(self) -> Self {
match self {
Self::Roman | Self::Bold => Self::Bold,
Self::Italic | Self::BoldItalic => Self::BoldItalic,
Self::Mono | Self::Sans => self,
}
}
/// `\mdseries`.
pub const fn medium(self) -> Self {
match self {
Self::Bold | Self::Roman => Self::Roman,
Self::BoldItalic | Self::Italic => Self::Italic,
Self::Mono | Self::Sans => self,
}
}
/// `\textit` / `\textsl`.
pub const fn italicized(self) -> Self {
match self {
Self::Roman | Self::Italic => Self::Italic,
Self::Bold | Self::BoldItalic => Self::BoldItalic,
Self::Mono | Self::Sans => self,
}
}
/// `\textup`.
pub const fn upright(self) -> Self {
match self {
Self::Roman | Self::Italic => Self::Roman,
Self::Bold | Self::BoldItalic => Self::Bold,
Self::Mono | Self::Sans => self,
}
}
/// `\textrm`.
pub const fn roman_family(self) -> Self {
match self {
Self::Mono | Self::Sans => Self::Roman,
other => other,
}
}
}
impl Default for ParagraphFont {
fn default() -> Self {
Self::Roman
+1
View File
@@ -255,6 +255,7 @@ fn paragraph_face(font: ParagraphFont) -> FontFace {
ParagraphFont::Roman => FontFace::Roman,
ParagraphFont::Bold => FontFace::Bold,
ParagraphFont::Italic => FontFace::Italic,
ParagraphFont::BoldItalic => FontFace::BoldItalic,
ParagraphFont::Mono => FontFace::Mono,
ParagraphFont::Sans => FontFace::Sans,
}
+1 -1
View File
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
PDF opt-in with `?engine=route-d`). 522 workspace tests are green and no oracle
PDF opt-in with `?engine=route-d`). 525 workspace tests are green and no oracle
fixture is ignored.
Status after G1 (P0-P2): the paragraph model reproduces TeX's passes,
+2 -2
View File
@@ -353,7 +353,7 @@ M3 painter: `Page::display_list()` and `Halign::display_list()` produce absolute
PDF writer: `crates/texrra-tex-core/src/pdf.rs` writes multi-page PDF 1.4 with five embedded Liberation Type0/CID faces (Identity-H + ToUnicode + `/W` arrays), filled rectangles, diagonal rules, raw-RGB `/XObject` images, `/Annots` URI links and arbitrary `/special` operators. Ligature substitution and line-end italic correction are implemented; full NFSS family switching remains open.
M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic,Mono}`. Roman/Bold/Italic/BoldItalic/Mono text are painted with embedded Liberation Type0/CID fonts, each with a ToUnicode CMap and `/W` width array. `cmr10.tfm` is parsed at runtime for widths, heights/depths, kern pairs and ff/fi/fl/ffi/ffl ligatures; line-end and math-script italic corrections and monospace/sans paragraph faces are applied; paragraph-level NFSS declarations (`\rmfamily`, `\sffamily`, `\ttfamily`, `\bfseries`, `\itshape`, `\texttt`, `\textsf`) select those faces and the `\fontfamily`/`\fontseries`/`\fontshape`/`\fontencoding`/`\selectfont` command forms plus legacy `\rm`/`\sf`/`\tt`/`\bf`/`\it`/`\sl`/`\up`/`\normalfont` declarations are parsed; inline `\textbf`/`\emph`/`\textit`/`\texttt`/`\textsf` groups and `{\bfseries ...}` declaration groups (including nesting) split a paragraph into styled runs, each shaped by its own TFM and painted with its own face; true family/series/shape switching and Type1/CM outlines remain open. `Unit::paragraph` now builds its item stream with `layout_paragraph_with_font`, so CMR10 advances, kerns and ligatures drive paragraph widths; each glyph box also carries the TFM height/depth, and the painter emits 10pt glyphs and the Type0 `/W` array reports CMR10 advances, and ligature boxes paint their component letters, so `pdftotext` keeps word boundaries instead of inserting spaces between letters.
M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic,Mono}`. Roman/Bold/Italic/BoldItalic/Mono text are painted with embedded Liberation Type0/CID fonts, each with a ToUnicode CMap and `/W` width array. `cmr10.tfm` is parsed at runtime for widths, heights/depths, kern pairs and ff/fi/fl/ffi/ffl ligatures; line-end and math-script italic corrections and monospace/sans paragraph faces are applied; paragraph-level NFSS declarations (`\rmfamily`, `\sffamily`, `\ttfamily`, `\bfseries`, `\itshape`, `\texttt`, `\textsf`) select those faces and the `\fontfamily`/`\fontseries`/`\fontshape`/`\fontencoding`/`\selectfont` command forms plus legacy `\rm`/`\sf`/`\tt`/`\bf`/`\it`/`\sl`/`\up`/`\normalfont` declarations are parsed; inline `\textbf`/`\emph`/`\textit`/`\texttt`/`\textsf` groups and `{\bfseries ...}` declaration groups (including nesting) split a paragraph into styled runs, each shaped by its own TFM and painted with its own face; nested faces compose relatively (`\emph` inside bold yields bold italic via cmbxti10, `\emph` inside italic returns upright); true family/series/shape switching and Type1/CM outlines remain open. `Unit::paragraph` now builds its item stream with `layout_paragraph_with_font`, so CMR10 advances, kerns and ligatures drive paragraph widths; each glyph box also carries the TFM height/depth, and the painter emits 10pt glyphs and the Type0 `/W` array reports CMR10 advances, and ligature boxes paint their component letters, so `pdftotext` keeps word boundaries instead of inserting spaces between letters.
M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments.
@@ -367,7 +367,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph.
The crate currently has 347 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 522 tests (347 route-D unit, 30 engine binary unit, 7 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
The crate currently has 347 unit tests plus seven route-D integration tests and thirty-five oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 525 tests (347 route-D unit, 33 engine binary unit, 7 route-D integration, 35 oracle, 71 v1 integration, 27 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
### M0 incremental demo
+93 -35
View File
@@ -964,10 +964,15 @@ fn nfss_declaration<'a>(text: &'a str, command: &str) -> Option<&'a str> {
Some(rest)
}
fn nfss_font(raw: &str) -> Option<(ParagraphFont, String)> {
let mut family = Family::Roman;
let mut bold = false;
let mut italic = false;
fn nfss_font(raw: &str, base: ParagraphFont) -> Option<(ParagraphFont, String)> {
let (mut family, mut bold, mut italic) = match base {
ParagraphFont::Roman => (Family::Roman, false, false),
ParagraphFont::Bold => (Family::Roman, true, false),
ParagraphFont::Italic => (Family::Roman, false, true),
ParagraphFont::BoldItalic => (Family::Roman, true, true),
ParagraphFont::Mono => (Family::Mono, false, false),
ParagraphFont::Sans => (Family::Sans, false, false),
};
let mut text = raw.trim_start();
let mut consumed = false;
loop {
@@ -1055,15 +1060,12 @@ fn nfss_font(raw: &str) -> Option<(ParagraphFont, String)> {
let font = match family {
Family::Mono => ParagraphFont::Mono,
Family::Sans => ParagraphFont::Sans,
Family::Roman => {
if bold {
ParagraphFont::Bold
} else if italic {
ParagraphFont::Italic
} else {
ParagraphFont::Roman
}
}
Family::Roman => match (bold, italic) {
(true, true) => ParagraphFont::BoldItalic,
(true, false) => ParagraphFont::Bold,
(false, true) => ParagraphFont::Italic,
(false, false) => ParagraphFont::Roman,
},
};
Some((font, text.to_string()))
}
@@ -1118,27 +1120,39 @@ fn braced_group(rest: &str) -> Option<(&str, usize)> {
None
}
const INLINE_COMMANDS: [(&str, Option<ParagraphFont>); 11] = [
("\\textbf", Some(ParagraphFont::Bold)),
("\\emph", Some(ParagraphFont::Italic)),
("\\textit", Some(ParagraphFont::Italic)),
("\\textsl", Some(ParagraphFont::Italic)),
("\\texttt", Some(ParagraphFont::Mono)),
("\\textsf", Some(ParagraphFont::Sans)),
("\\textmd", None),
("\\textnormal", None),
("\\textup", None),
("\\textrm", None),
("\\textsc", None),
const INLINE_COMMANDS: [&str; 11] = [
"\\textbf",
"\\emph",
"\\textit",
"\\textsl",
"\\texttt",
"\\textsf",
"\\textmd",
"\\textnormal",
"\\textup",
"\\textrm",
"\\textsc",
];
/// Match a styled wrapper command at the start of `rest`.
fn inline_wrapper(rest: &str) -> Option<(Option<ParagraphFont>, &str, usize)> {
for (command, font) in INLINE_COMMANDS {
/// Match a styled wrapper command at the start of `rest`, composing its face
/// with the surrounding one (`\\emph` toggles italic inside a bold run, etc.).
fn inline_wrapper(rest: &str, base: ParagraphFont) -> Option<(ParagraphFont, &str, usize)> {
for command in INLINE_COMMANDS {
let Some(after) = rest.strip_prefix(command) else {
continue;
};
if let Some((inner, length)) = braced_group(after) {
let font = match command {
"\\textbf" => base.bolded(),
"\\emph" => base.emphasized(),
"\\textit" | "\\textsl" => base.italicized(),
"\\texttt" => ParagraphFont::Mono,
"\\textsf" => ParagraphFont::Sans,
"\\textmd" => base.medium(),
"\\textup" => base.upright(),
"\\textrm" => base.roman_family(),
"\\textnormal" => ParagraphFont::Roman,
_ => base,
};
return Some((font, inner, command.len() + length));
}
}
@@ -1155,20 +1169,20 @@ fn parse_runs(
while index < text.len() {
let rest = &text[index..];
if let Some((inner, length)) = braced_group(rest) {
let (inner_font, inner_text) = match nfss_font(inner) {
let (inner_font, inner_text) = match nfss_font(inner, font) {
Some((declared, body)) => (declared, body),
None => (font, inner.to_string()),
};
if nfss_font(inner).is_some() {
if nfss_font(inner, font).is_some() {
*found = true;
}
parse_runs(&inner_text, inner_font, runs, found);
index += length;
continue;
}
if let Some((wrapper_font, inner, length)) = inline_wrapper(rest) {
if let Some((wrapper_font, inner, length)) = inline_wrapper(rest, font) {
*found = true;
parse_runs(inner, wrapper_font.unwrap_or(font), runs, found);
parse_runs(inner, wrapper_font, runs, found);
index += length;
continue;
}
@@ -1195,7 +1209,7 @@ fn parse_runs(
}
fn parse_paragraph(raw: &str) -> SourceParagraph {
if let Some((font, body)) = nfss_font(raw) {
if let Some((font, body)) = nfss_font(raw, ParagraphFont::Roman) {
return SourceParagraph {
text: body,
font,
@@ -2038,13 +2052,57 @@ mod inline_tests {
runs,
vec![
(ParagraphFont::Bold, "bold ".to_string()),
(ParagraphFont::Italic, "italic".to_string()),
(ParagraphFont::BoldItalic, "italic".to_string()),
(ParagraphFont::Bold, " bold".to_string()),
(ParagraphFont::Roman, " plain".to_string()),
]
);
}
#[test]
fn emph_inside_bold_is_bold_italic() {
let runs = inline_runs(
"{\\bfseries bold \\emph{bold italic} bold}",
ParagraphFont::Roman,
)
.expect("runs");
assert_eq!(
runs,
vec![
(ParagraphFont::Bold, "bold ".to_string()),
(ParagraphFont::BoldItalic, "bold italic".to_string()),
(ParagraphFont::Bold, " bold".to_string()),
]
);
}
#[test]
fn emph_inside_italic_returns_upright() {
let runs = inline_runs("{\\itshape it \\emph{up} it}", ParagraphFont::Roman).expect("runs");
assert_eq!(
runs,
vec![
(ParagraphFont::Italic, "it ".to_string()),
(ParagraphFont::Roman, "up".to_string()),
(ParagraphFont::Italic, " it".to_string()),
]
);
}
#[test]
fn textmd_inside_bold_drops_weight() {
let runs =
inline_runs("\\textbf{a \\textmd{plain} b}", ParagraphFont::Roman).expect("runs");
assert_eq!(
runs,
vec![
(ParagraphFont::Bold, "a ".to_string()),
(ParagraphFont::Roman, "plain".to_string()),
(ParagraphFont::Bold, " b".to_string()),
]
);
}
#[test]
fn wrapper_groups_keep_nested_braces() {
let runs = inline_runs("\\textbf{a {b} c}", ParagraphFont::Roman).expect("runs");