Freeze savebox metrics and expand wd/ht/dp
\sbox/\savebox now measure their content at definition with the Roman metrics
at the class base size and store width/height/depth. \usebox carries that size
into the no-break segment, which scales the box back to its definition size
inside a differently sized paragraph, and \wd/\ht/\dp\boxname expand to the
frozen metrics before paragraph parsing.
\mbox/\hbox/\makebox keep following the surrounding size (their marker carries
size 0). A core test checks that a box frozen at 20pt is twice as wide as the
10pt one, and a route-D test checks the measured dimensions and a
\hspace{\wd\box} expansion. Image-only boxes still measure as empty text.
Three savebox tests strip the internal markers (now with their size prefix)
before comparing visible text.
This commit is contained in:
1 parent
95a6a1291a
commit
5a1a04d029
4 files changed
+251
-26
No files matched your search
@@ -58,7 +58,9 @@ pub enum InlineSegment {
|
||||
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),
|
||||
/// A positive size freezes the box at the size it was measured at; zero
|
||||
/// follows the surrounding paragraph size.
|
||||
NoBreak(ParagraphFont, i64, String),
|
||||
Link {
|
||||
font: ParagraphFont,
|
||||
url: String,
|
||||
@@ -580,6 +582,43 @@ fn scale_font_size(value: i64, size: i64) -> i64 {
|
||||
value.saturating_mul(size) / BASE_FONT_SIZE
|
||||
}
|
||||
|
||||
/// Scale a shaped run by an arbitrary rational factor (used to freeze a
|
||||
/// savebox measured at another base size).
|
||||
fn scale_items_ratio(items: &mut [HorizontalItem], numerator: i64, denominator: i64) {
|
||||
if numerator == denominator || denominator == 0 {
|
||||
return;
|
||||
}
|
||||
let scale = |value: i64| value.saturating_mul(numerator) / denominator;
|
||||
for item in items {
|
||||
match item {
|
||||
HorizontalItem::Box(box_) => {
|
||||
box_.width = scale(box_.width);
|
||||
box_.height = scale(box_.height);
|
||||
box_.depth = scale(box_.depth);
|
||||
box_.italic = scale(box_.italic);
|
||||
}
|
||||
HorizontalItem::Glue {
|
||||
width,
|
||||
stretch,
|
||||
shrink,
|
||||
..
|
||||
} => {
|
||||
*width = scale(*width);
|
||||
*stretch = scale(*stretch);
|
||||
*shrink = scale(*shrink);
|
||||
}
|
||||
HorizontalItem::Penalty { width, .. } => *width = scale(*width),
|
||||
HorizontalItem::Discretionary {
|
||||
pre_break_width,
|
||||
no_break_width,
|
||||
} => {
|
||||
*pre_break_width = scale(*pre_break_width);
|
||||
*no_break_width = scale(*no_break_width);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Scale every glyph and glue in a shaped item stream for the paragraph's
|
||||
/// base font size. Scaling after shaping is linear, so it matches shaping with
|
||||
/// scaled TFM metrics.
|
||||
@@ -1877,7 +1916,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
|
||||
previous = Some((*font, text.clone()));
|
||||
seen_content = true;
|
||||
}
|
||||
InlineSegment::NoBreak(font, text) => {
|
||||
InlineSegment::NoBreak(font, block_size, text) => {
|
||||
if text.is_empty() {
|
||||
continue;
|
||||
}
|
||||
@@ -1904,11 +1943,20 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
|
||||
});
|
||||
}
|
||||
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(*font);
|
||||
let freeze_ratio = if *block_size > 0 && *block_size != style.font_size {
|
||||
Some((*block_size, style.font_size.max(1)))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let space_width = match freeze_ratio {
|
||||
Some((numerator, denominator)) => metrics.space() * numerator / denominator,
|
||||
None => metrics.space(),
|
||||
};
|
||||
let mut first_word = true;
|
||||
for word in text.split_whitespace() {
|
||||
if !first_word {
|
||||
items.push(HorizontalItem::Box(HorizontalBox {
|
||||
width: metrics.space(),
|
||||
width: space_width,
|
||||
height: 0,
|
||||
depth: 0,
|
||||
character: None,
|
||||
@@ -1926,6 +1974,9 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
|
||||
first_word = false;
|
||||
let mut run = text_items_with_font(word, style, &metrics);
|
||||
run.retain(|item| matches!(item, HorizontalItem::Box(_)));
|
||||
if let Some((numerator, denominator)) = freeze_ratio {
|
||||
scale_items_ratio(&mut run, numerator, denominator);
|
||||
}
|
||||
for item in &mut run {
|
||||
if let HorizontalItem::Box(box_) = item {
|
||||
box_.font = Some(*font);
|
||||
@@ -2396,6 +2447,7 @@ mod tests {
|
||||
};
|
||||
let segments = vec![InlineSegment::NoBreak(
|
||||
ParagraphFont::Roman,
|
||||
0,
|
||||
"alpha beta gamma".to_string(),
|
||||
)];
|
||||
let paragraph = layout_paragraph_segments(&segments, &style);
|
||||
@@ -2417,6 +2469,42 @@ mod tests {
|
||||
assert_eq!(characters, "alphabetagamma");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frozen_no_break_box_keeps_its_definition_size() {
|
||||
let style = ParagraphStyle {
|
||||
line_width: 200 * 65_536,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let width = |paragraph: &Paragraph| -> i64 {
|
||||
paragraph.lines[0]
|
||||
.iter()
|
||||
.map(|item| match item {
|
||||
HorizontalItem::Box(box_) => box_.width,
|
||||
HorizontalItem::Glue { width, .. } => *width,
|
||||
_ => 0,
|
||||
})
|
||||
.sum()
|
||||
};
|
||||
let normal = layout_paragraph_segments(
|
||||
&[InlineSegment::NoBreak(
|
||||
ParagraphFont::Roman,
|
||||
0,
|
||||
"ab".to_string(),
|
||||
)],
|
||||
&style,
|
||||
);
|
||||
let frozen = layout_paragraph_segments(
|
||||
&[InlineSegment::NoBreak(
|
||||
ParagraphFont::Roman,
|
||||
2 * 655_360,
|
||||
"ab".to_string(),
|
||||
)],
|
||||
&style,
|
||||
);
|
||||
let ratio = width(&frozen) as f64 / width(&normal) as f64;
|
||||
assert!((ratio - 2.0).abs() < 0.05, "frozen ratio {ratio}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sized_segments_keep_their_own_glyph_size() {
|
||||
let style = ParagraphStyle {
|
||||
|
||||
Reference in new issue
Block a user