Keep mbox and savebox content on one line
Add InlineSegment::NoBreak: it shapes each word and replaces the interword
glue with fixed-width boxes, so the run has no breakpoints and behaves like
TeX's unbreakable hbox rather than reflowing at the use site.
Route-D wraps \usebox bodies and \mbox/\hbox/\makebox arguments in private-use
markers; the paragraph text builder splits on them and emits NoBreak runs,
leaving ordinary text and styling on the existing path. \underline keeps its
old inline behaviour.
A 60pt measure now keeps \mbox{alpha beta gamma} and a stored
\sbox{alpha beta gamma} on one line, covered by a core segment test and a
route-D test. Savebox dimensions are still measured at the use site instead of
being frozen at definition, and \wd/\ht/\dp accessors remain open.
This commit is contained in:
1 parent
56faec6046
commit
95a6a1291a
4 files changed
+213
-13
No files matched your search
@@ -56,6 +56,9 @@ pub enum InlineSegment {
|
||||
String,
|
||||
),
|
||||
Math(crate::math::MathAtom),
|
||||
/// Material that never breaks internally, like an `\mbox` or a measured
|
||||
/// savebox: interword spaces become fixed-width boxes instead of glue.
|
||||
NoBreak(ParagraphFont, String),
|
||||
Link {
|
||||
font: ParagraphFont,
|
||||
url: String,
|
||||
@@ -1874,6 +1877,65 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
|
||||
previous = Some((*font, text.clone()));
|
||||
seen_content = true;
|
||||
}
|
||||
InlineSegment::NoBreak(font, text) => {
|
||||
if text.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let starts_with_space = text.chars().next().is_some_and(char::is_whitespace);
|
||||
let boundary_space = match &previous {
|
||||
Some((_previous_font, previous_text)) => {
|
||||
previous_text
|
||||
.chars()
|
||||
.next_back()
|
||||
.is_some_and(char::is_whitespace)
|
||||
|| starts_with_space
|
||||
}
|
||||
None => seen_content && starts_with_space,
|
||||
};
|
||||
if boundary_space {
|
||||
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(
|
||||
previous.as_ref().map_or(*font, |(font, _)| *font),
|
||||
);
|
||||
items.push(HorizontalItem::Glue {
|
||||
width: metrics.space(),
|
||||
stretch: metrics.stretch(),
|
||||
shrink: metrics.shrink(),
|
||||
order: 0,
|
||||
});
|
||||
}
|
||||
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(*font);
|
||||
let mut first_word = true;
|
||||
for word in text.split_whitespace() {
|
||||
if !first_word {
|
||||
items.push(HorizontalItem::Box(HorizontalBox {
|
||||
width: metrics.space(),
|
||||
height: 0,
|
||||
depth: 0,
|
||||
character: None,
|
||||
italic: 0,
|
||||
font: Some(*font),
|
||||
math: None,
|
||||
raise: 0,
|
||||
color: None,
|
||||
background: None,
|
||||
frame: None,
|
||||
size: None,
|
||||
link: None,
|
||||
}));
|
||||
}
|
||||
first_word = false;
|
||||
let mut run = text_items_with_font(word, style, &metrics);
|
||||
run.retain(|item| matches!(item, HorizontalItem::Box(_)));
|
||||
for item in &mut run {
|
||||
if let HorizontalItem::Box(box_) = item {
|
||||
box_.font = Some(*font);
|
||||
}
|
||||
}
|
||||
items.extend(run);
|
||||
}
|
||||
previous = Some((*font, text.clone()));
|
||||
seen_content = true;
|
||||
}
|
||||
InlineSegment::Link {
|
||||
font,
|
||||
url,
|
||||
@@ -2326,6 +2388,35 @@ mod tests {
|
||||
assert!(elapsed < std::time::Duration::from_secs(10), "{elapsed:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_break_segments_replace_spaces_with_fixed_boxes() {
|
||||
let style = ParagraphStyle {
|
||||
line_width: 40 * 65_536,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let segments = vec![InlineSegment::NoBreak(
|
||||
ParagraphFont::Roman,
|
||||
"alpha beta gamma".to_string(),
|
||||
)];
|
||||
let paragraph = layout_paragraph_segments(&segments, &style);
|
||||
assert_eq!(paragraph.lines.len(), 1, "the box must not wrap");
|
||||
let line = ¶graph.lines[0];
|
||||
assert!(
|
||||
!line
|
||||
.iter()
|
||||
.any(|item| matches!(item, HorizontalItem::Glue { .. })),
|
||||
"spaces inside a no-break box must be fixed boxes"
|
||||
);
|
||||
let characters: String = line
|
||||
.iter()
|
||||
.filter_map(|item| match item {
|
||||
HorizontalItem::Box(box_) => box_.character,
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(characters, "alphabetagamma");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sized_segments_keep_their_own_glyph_size() {
|
||||
let style = ParagraphStyle {
|
||||
|
||||
Reference in new issue
Block a user