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
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user