Measure table cells with Computer Modern metrics
This commit is contained in:
1 parent
39f02b83c0
commit
d89b71bfbc
5 files changed
+91
-18
No files matched your search
@@ -548,14 +548,15 @@ impl Halign {
|
||||
}
|
||||
}
|
||||
|
||||
/// Natural width of a run of text at the illustrative 10pt metrics.
|
||||
/// Natural width of a run of text in the default 10pt Roman face.
|
||||
pub(crate) fn text_width(text: &str) -> i64 {
|
||||
let metrics = crate::cmr10::Cmr10Metrics::Roman;
|
||||
text.chars()
|
||||
.map(|character| {
|
||||
if character.is_whitespace() {
|
||||
4 * 65_536
|
||||
metrics.space()
|
||||
} else {
|
||||
10 * 65_536
|
||||
metrics.width(character)
|
||||
}
|
||||
})
|
||||
.sum()
|
||||
@@ -6190,9 +6191,16 @@ mod tests {
|
||||
let VerticalItem::Halign(halign) = &unit.items[0] else {
|
||||
panic!("expected a halign item");
|
||||
};
|
||||
assert_eq!(halign.cell_box(0, 0), Some((0, 5242880)));
|
||||
assert_eq!(halign.cell_box(1, 1), Some((6553600, 655360)));
|
||||
assert_eq!(halign.cell_box(2, 0), Some((1966080, 7208960)));
|
||||
let first = crate::text_width("longword").max(crate::text_width("A"));
|
||||
let second = crate::text_width("C").max(crate::text_width("B"));
|
||||
let gap = 20 * 65_536;
|
||||
assert_eq!(halign.cell_box(0, 0), Some((0, first)));
|
||||
assert_eq!(halign.cell_box(1, 1), Some((first + gap, second)));
|
||||
let total = crate::text_width("TOTAL");
|
||||
assert_eq!(
|
||||
halign.cell_box(2, 0),
|
||||
Some(((first + gap + second - total) / 2, first + gap + second))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -7714,8 +7722,10 @@ mod tests {
|
||||
let VerticalItem::Halign(halign) = &unit.items[0] else {
|
||||
panic!("expected a halign item");
|
||||
};
|
||||
assert_eq!(halign.column_widths, vec![5242880, 655360]);
|
||||
assert_eq!(halign.width, 7208960);
|
||||
let first = crate::text_width("longword").max(crate::text_width("A"));
|
||||
let second = crate::text_width("C").max(crate::text_width("B"));
|
||||
assert_eq!(halign.column_widths, vec![first, second]);
|
||||
assert_eq!(halign.width, first + second + 20 * 65_536);
|
||||
assert_eq!(halign.height, 1_599_078);
|
||||
}
|
||||
|
||||
@@ -7851,23 +7861,20 @@ second row & right\n",
|
||||
);
|
||||
assert_eq!(halign.column_widths[0], wrap);
|
||||
let lines = halign.cell_lines(0, 0);
|
||||
assert!(lines.len() >= 3, "lines: {lines:?}");
|
||||
assert!(lines.len() >= 2, "lines: {lines:?}");
|
||||
assert!(lines.iter().all(|line| crate::text_width(line) <= wrap));
|
||||
assert_eq!(
|
||||
halign.row_height(0),
|
||||
halign.line_height * lines.len() as i64
|
||||
);
|
||||
assert_eq!(
|
||||
halign.cell_lines(1, 0),
|
||||
vec!["second".to_owned(), "row".to_owned()]
|
||||
);
|
||||
assert_eq!(halign.cell_lines(1, 0).join(" "), "second row");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paragraph_columns_paint_wrapped_lines_and_split_rows() {
|
||||
let unit = Unit::halign_from_source_with_template(
|
||||
1,
|
||||
"p{20mm}l",
|
||||
"p{10mm}l",
|
||||
"alpha beta gamma delta & x \\\\
|
||||
second row text & y\n",
|
||||
0,
|
||||
|
||||
Reference in new issue
Block a user