Scale paragraphs by their declared base font size
This commit is contained in:
1 parent
aa4d86d735
commit
9876895a84
8 files changed
+249
-17
No files matched your search
@@ -6306,6 +6306,30 @@ mod tests {
|
||||
assert!(svg.contains("font-style=\"italic\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sized_paragraphs_paint_at_their_font_size() {
|
||||
let style = crate::paragraph::ParagraphStyle {
|
||||
font_size: 12 * 65_536,
|
||||
..crate::paragraph::ParagraphStyle::default()
|
||||
};
|
||||
let unit =
|
||||
Unit::paragraph_with_style_and_face(1, "large text", style, ParagraphFont::Roman);
|
||||
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 { size: 786_432, .. }))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mono_paragraph_paints_with_a_mono_face() {
|
||||
let unit = Unit::paragraph_with_face(1, "code 123", ParagraphFont::Mono);
|
||||
@@ -6859,6 +6883,7 @@ mod tests {
|
||||
width: 50,
|
||||
depth: 0,
|
||||
font: ParagraphFont::Roman,
|
||||
font_size: 10 * 65_536,
|
||||
left_skip: 0,
|
||||
line_indents: vec![0; 10],
|
||||
line_baselines: (0..10).map(|line| 8 + line * 10).collect(),
|
||||
@@ -8433,6 +8458,7 @@ b & 2 \\
|
||||
width: 5 * 65_536,
|
||||
depth: 2 * 65_536,
|
||||
font: ParagraphFont::Roman,
|
||||
font_size: 10 * 65_536,
|
||||
left_skip: 0,
|
||||
line_indents: vec![0],
|
||||
line_baselines: vec![8 * 65_536],
|
||||
|
||||
@@ -73,6 +73,13 @@ fn default_adj_demerits() -> i64 {
|
||||
10_000
|
||||
}
|
||||
|
||||
/// Base font size for unscaled paragraphs (10pt in scaled points).
|
||||
const BASE_FONT_SIZE: i64 = 655_360;
|
||||
|
||||
fn default_font_size() -> i64 {
|
||||
BASE_FONT_SIZE
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ParagraphStyle {
|
||||
pub line_width: i64,
|
||||
@@ -169,6 +176,10 @@ pub struct ParagraphStyle {
|
||||
#[serde(default)]
|
||||
pub hyphenation_exceptions: Vec<String>,
|
||||
pub line_height: i64,
|
||||
/// Base font size in scaled points (`\fontsize`, `\large`, ...). Glyph
|
||||
/// metrics and the default interline glue scale by this over 10pt.
|
||||
#[serde(default = "default_font_size")]
|
||||
pub font_size: i64,
|
||||
pub char_width: i64,
|
||||
pub space_width: i64,
|
||||
pub space_stretch: i64,
|
||||
@@ -274,6 +285,7 @@ impl Default for ParagraphStyle {
|
||||
adj_demerits: 10_000,
|
||||
hyphenation_exceptions: Vec::new(),
|
||||
line_height: 12,
|
||||
font_size: BASE_FONT_SIZE,
|
||||
char_width: 10,
|
||||
space_width: 4,
|
||||
space_stretch: 6,
|
||||
@@ -447,6 +459,9 @@ pub struct Paragraph {
|
||||
pub depth: i64,
|
||||
#[serde(default)]
|
||||
pub font: ParagraphFont,
|
||||
/// Base font size used when painting the glyphs of this paragraph.
|
||||
#[serde(default = "default_font_size")]
|
||||
pub font_size: i64,
|
||||
#[serde(default)]
|
||||
pub left_skip: i64,
|
||||
/// Left offset for each line, from `\hangindent`/`\parshape`.
|
||||
@@ -499,6 +514,7 @@ impl Paragraph {
|
||||
width: self.width,
|
||||
depth: tail_depth,
|
||||
font: self.font,
|
||||
font_size: self.font_size,
|
||||
left_skip: self.left_skip,
|
||||
line_indents: tail_indents,
|
||||
line_baselines: tail_baselines,
|
||||
@@ -508,6 +524,70 @@ impl Paragraph {
|
||||
}
|
||||
}
|
||||
|
||||
/// Scale a font-derived dimension by the paragraph's base font size.
|
||||
fn scale_font_size(value: i64, size: i64) -> i64 {
|
||||
value.saturating_mul(size) / BASE_FONT_SIZE
|
||||
}
|
||||
|
||||
/// 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.
|
||||
fn scale_items_for_size(items: &mut [HorizontalItem], size: i64) {
|
||||
if size == BASE_FONT_SIZE {
|
||||
return;
|
||||
}
|
||||
for item in items {
|
||||
match item {
|
||||
HorizontalItem::Box(box_) => {
|
||||
box_.width = scale_font_size(box_.width, size);
|
||||
box_.height = scale_font_size(box_.height, size);
|
||||
box_.depth = scale_font_size(box_.depth, size);
|
||||
box_.italic = scale_font_size(box_.italic, size);
|
||||
}
|
||||
HorizontalItem::Glue {
|
||||
width,
|
||||
stretch,
|
||||
shrink,
|
||||
..
|
||||
} => {
|
||||
*width = scale_font_size(*width, size);
|
||||
*stretch = scale_font_size(*stretch, size);
|
||||
*shrink = scale_font_size(*shrink, size);
|
||||
}
|
||||
HorizontalItem::Discretionary {
|
||||
pre_break_width,
|
||||
no_break_width,
|
||||
} => {
|
||||
*pre_break_width = scale_font_size(*pre_break_width, size);
|
||||
*no_break_width = scale_font_size(*no_break_width, size);
|
||||
}
|
||||
HorizontalItem::Penalty { .. } => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Clone a paragraph style with its font-derived defaults scaled to the
|
||||
/// paragraph's base size. Explicitly authored glue is left alone, and the
|
||||
/// measure, indentation and skips stay absolute as in TeX.
|
||||
fn sized_style(style: &ParagraphStyle) -> ParagraphStyle {
|
||||
if style.font_size == BASE_FONT_SIZE {
|
||||
return style.clone();
|
||||
}
|
||||
let mut sized = style.clone();
|
||||
if sized.baselineskip == default_baselineskip() {
|
||||
sized.baselineskip = scale_font_size(sized.baselineskip, style.font_size);
|
||||
}
|
||||
if sized.lineskip == default_lineskip() {
|
||||
sized.lineskip = scale_font_size(sized.lineskip, style.font_size);
|
||||
}
|
||||
sized.line_height = scale_font_size(sized.line_height, style.font_size);
|
||||
sized.char_width = scale_font_size(sized.char_width, style.font_size);
|
||||
sized.space_width = scale_font_size(sized.space_width, style.font_size);
|
||||
sized.space_stretch = scale_font_size(sized.space_stretch, style.font_size);
|
||||
sized.space_shrink = scale_font_size(sized.space_shrink, style.font_size);
|
||||
sized
|
||||
}
|
||||
|
||||
/// Convert text into a horizontal item stream: one box per character, one
|
||||
/// glue per space, and a final penalty.
|
||||
pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
|
||||
@@ -561,6 +641,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
width: 0,
|
||||
depth: 0,
|
||||
font: ParagraphFont::Roman,
|
||||
font_size: style.font_size,
|
||||
left_skip: style.left_skip,
|
||||
line_indents: Vec::new(),
|
||||
line_baselines: Vec::new(),
|
||||
@@ -1171,6 +1252,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
depth,
|
||||
lines,
|
||||
font: ParagraphFont::Roman,
|
||||
font_size: style.font_size,
|
||||
left_skip: style.left_skip,
|
||||
line_indents,
|
||||
line_baselines,
|
||||
@@ -1407,8 +1489,11 @@ pub fn layout_paragraph_with_font(
|
||||
style: &ParagraphStyle,
|
||||
font: &crate::cmr10::Cmr10Metrics,
|
||||
) -> Paragraph {
|
||||
let items = text_items_with_font(text, style, font);
|
||||
layout_items(&items, style)
|
||||
let mut items = text_items_with_font(text, style, font);
|
||||
scale_items_for_size(&mut items, style.font_size);
|
||||
let mut paragraph = layout_items(&items, &sized_style(style));
|
||||
paragraph.font_size = style.font_size;
|
||||
paragraph
|
||||
}
|
||||
|
||||
/// Lay out a paragraph whose text is split into styled runs.
|
||||
@@ -1452,14 +1537,19 @@ pub fn layout_paragraph_runs(
|
||||
items.extend(run);
|
||||
previous = Some((*font, text.clone()));
|
||||
}
|
||||
let mut paragraph = layout_items(&items, style);
|
||||
scale_items_for_size(&mut items, style.font_size);
|
||||
let mut paragraph = layout_items(&items, &sized_style(style));
|
||||
paragraph.font = runs.first().map(|run| run.0).unwrap_or_default();
|
||||
paragraph.font_size = style.font_size;
|
||||
paragraph
|
||||
}
|
||||
|
||||
pub fn layout_paragraph(text: &str, style: &ParagraphStyle) -> Paragraph {
|
||||
let items = text_items(text, style);
|
||||
layout_items(&items, style)
|
||||
let mut items = text_items(text, style);
|
||||
scale_items_for_size(&mut items, style.font_size);
|
||||
let mut paragraph = layout_items(&items, &sized_style(style));
|
||||
paragraph.font_size = style.font_size;
|
||||
paragraph
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -1478,6 +1568,33 @@ mod tests {
|
||||
.count()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn font_size_scales_glyphs_and_line_spacing() {
|
||||
let font = crate::cmr10::Cmr10Metrics::Roman;
|
||||
let base = ParagraphStyle {
|
||||
line_width: 150 * 65_536,
|
||||
par_indent: 0,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let ten = layout_paragraph_with_font("hello world", &base, &font);
|
||||
let large = ParagraphStyle {
|
||||
font_size: 12 * 65_536,
|
||||
..base.clone()
|
||||
};
|
||||
let twelve = layout_paragraph_with_font("hello world", &large, &font);
|
||||
assert!(twelve.width > ten.width);
|
||||
assert!(twelve.height > ten.height);
|
||||
let glyphs = |paragraph: &Paragraph| {
|
||||
paragraph
|
||||
.lines
|
||||
.iter()
|
||||
.flatten()
|
||||
.filter(|item| matches!(item, HorizontalItem::Box(_)))
|
||||
.count()
|
||||
};
|
||||
assert_eq!(glyphs(&ten), glyphs(&twelve));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overfull_lines_do_not_exceed_the_declared_shrink() {
|
||||
let font = crate::cmr10::Cmr10Metrics::Roman;
|
||||
|
||||
@@ -502,7 +502,7 @@ impl Page {
|
||||
let face = paragraph_face(paragraph.font);
|
||||
let lines = paragraph.lines.len().max(1) as i64;
|
||||
let line_height = (paragraph.height / lines).max(1);
|
||||
let size = 10 * 65_536;
|
||||
let size = paragraph.font_size.max(1);
|
||||
for (line_index, line) in paragraph.lines.iter().enumerate() {
|
||||
let indent = paragraph.line_indents.get(line_index).copied().unwrap_or(0);
|
||||
let baseline = y + paragraph
|
||||
|
||||
@@ -14,6 +14,7 @@ use texrra_tex_core::{
|
||||
fn body_style() -> ParagraphStyle {
|
||||
let font = Cmr10Metrics::default();
|
||||
ParagraphStyle {
|
||||
font_size: 10 * 65_536,
|
||||
line_width: 480 * 65_536,
|
||||
left_skip: 0,
|
||||
right_skip: 0,
|
||||
@@ -185,6 +186,7 @@ fn route_d_page_matches_pdftex_raster_geometry() {
|
||||
std::fs::create_dir_all(&root).expect("create raster dir");
|
||||
|
||||
let style = ParagraphStyle {
|
||||
font_size: 10 * 65_536,
|
||||
line_width: 480 * 65_536,
|
||||
left_skip: 0,
|
||||
right_skip: 0,
|
||||
@@ -365,6 +367,7 @@ fn route_d_two_pages_match_pdftex_raster_geometry() {
|
||||
std::fs::create_dir_all(&root).expect("create raster dir");
|
||||
|
||||
let style = ParagraphStyle {
|
||||
font_size: 10 * 65_536,
|
||||
line_width: 480 * 65_536,
|
||||
left_skip: 0,
|
||||
right_skip: 0,
|
||||
@@ -717,6 +720,7 @@ fn route_d_multiline_bands_track_pdftex() {
|
||||
let text = "alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu";
|
||||
|
||||
let style = ParagraphStyle {
|
||||
font_size: 10 * 65_536,
|
||||
line_width: 150 * 65_536,
|
||||
left_skip: 0,
|
||||
right_skip: 0,
|
||||
|
||||
Reference in new issue
Block a user