Paint inline size changes at their own size
This commit is contained in:
1 parent
00f95fdd09
commit
ae69eba64d
6 files changed
+158
-28
No files matched your search
@@ -20,12 +20,17 @@ pub struct HorizontalBox {
|
||||
/// Link target when this glyph came from `\href`.
|
||||
#[serde(default)]
|
||||
pub link: Option<String>,
|
||||
/// Explicit glyph size in scaled points for a sized run.
|
||||
#[serde(default)]
|
||||
pub size: Option<i64>,
|
||||
}
|
||||
|
||||
/// A run of paragraph material: text in one face or an inline formula.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub enum InlineSegment {
|
||||
Text(ParagraphFont, String),
|
||||
/// A run carrying its own type size, such as `{\small ...}`.
|
||||
SizedText(ParagraphFont, i64, String),
|
||||
Math(crate::math::MathAtom),
|
||||
Link {
|
||||
font: ParagraphFont,
|
||||
@@ -629,6 +634,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: None,
|
||||
size: None,
|
||||
link: None,
|
||||
}));
|
||||
}
|
||||
@@ -1097,6 +1103,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: None,
|
||||
size: None,
|
||||
link: None,
|
||||
}));
|
||||
} else if *no_break_width > 0 {
|
||||
@@ -1108,6 +1115,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: None,
|
||||
size: None,
|
||||
link: None,
|
||||
}));
|
||||
}
|
||||
@@ -1453,6 +1461,7 @@ pub fn text_items_with_font(
|
||||
italic: font.italic(character),
|
||||
font: None,
|
||||
math: None,
|
||||
size: None,
|
||||
link: None,
|
||||
}));
|
||||
let boundary = character_index + 1;
|
||||
@@ -1620,6 +1629,45 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
|
||||
previous = Some((*font, text.clone()));
|
||||
seen_content = true;
|
||||
}
|
||||
InlineSegment::SizedText(font, size, 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: scale_font_size(metrics.space(), *size),
|
||||
stretch: scale_font_size(metrics.stretch(), *size),
|
||||
shrink: scale_font_size(metrics.shrink(), *size),
|
||||
order: 0,
|
||||
});
|
||||
}
|
||||
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(*font);
|
||||
let mut run = text_items_with_font(text, style, &metrics);
|
||||
scale_items_for_size(&mut run, *size);
|
||||
for item in &mut run {
|
||||
if let HorizontalItem::Box(box_) = item {
|
||||
box_.font = Some(*font);
|
||||
box_.size = Some(*size);
|
||||
}
|
||||
}
|
||||
items.extend(run);
|
||||
previous = Some((*font, text.clone()));
|
||||
seen_content = true;
|
||||
}
|
||||
InlineSegment::Link { font, url, text } => {
|
||||
if text.is_empty() {
|
||||
continue;
|
||||
@@ -1682,6 +1730,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: Some(Box::new(atom.clone())),
|
||||
size: None,
|
||||
link: None,
|
||||
}));
|
||||
previous = None;
|
||||
@@ -1692,7 +1741,9 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
|
||||
scale_items_for_size(&mut items, style.font_size);
|
||||
let mut paragraph = layout_items(&items, &sized_style(style));
|
||||
paragraph.font = match segments.first() {
|
||||
Some(InlineSegment::Text(font, _)) | Some(InlineSegment::Link { font, .. }) => *font,
|
||||
Some(InlineSegment::Text(font, _))
|
||||
| Some(InlineSegment::SizedText(font, _, _))
|
||||
| Some(InlineSegment::Link { font, .. }) => *font,
|
||||
_ => ParagraphFont::Roman,
|
||||
};
|
||||
paragraph.font_size = style.font_size;
|
||||
@@ -1978,6 +2029,32 @@ mod tests {
|
||||
assert!(linked.iter().all(|box_| box_.character.is_some()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sized_segments_keep_their_own_glyph_size() {
|
||||
let style = ParagraphStyle {
|
||||
line_width: 480 * 65_536,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let segments = vec![
|
||||
InlineSegment::Text(ParagraphFont::Roman, "x ".into()),
|
||||
InlineSegment::SizedText(ParagraphFont::Roman, 786_432, "x".into()),
|
||||
];
|
||||
let paragraph = layout_paragraph_segments(&segments, &style);
|
||||
let boxes: Vec<&HorizontalBox> = paragraph
|
||||
.lines
|
||||
.iter()
|
||||
.flatten()
|
||||
.filter_map(|item| match item {
|
||||
HorizontalItem::Box(box_) => Some(box_),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(boxes.len(), 2);
|
||||
assert_eq!(boxes[0].size, None);
|
||||
assert_eq!(boxes[1].size, Some(786_432));
|
||||
assert!(boxes[1].width > boxes[0].width);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_math_boxes_sit_between_words() {
|
||||
let style = ParagraphStyle {
|
||||
@@ -2031,6 +2108,7 @@ mod tests {
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: None,
|
||||
size: None,
|
||||
link: None,
|
||||
})
|
||||
};
|
||||
|
||||
@@ -550,6 +550,7 @@ impl Page {
|
||||
} else {
|
||||
flush_link(&mut link_run, &mut list.items);
|
||||
}
|
||||
let glyph_size = box_.size.unwrap_or(size);
|
||||
if let Some(math) = &box_.math {
|
||||
let math_font = Cmr10Metrics::default();
|
||||
Self::paint_math(
|
||||
@@ -557,7 +558,7 @@ impl Page {
|
||||
&math_font,
|
||||
x,
|
||||
baseline,
|
||||
size,
|
||||
glyph_size,
|
||||
&mut list.items,
|
||||
);
|
||||
}
|
||||
@@ -566,7 +567,7 @@ impl Page {
|
||||
font: box_.font.map_or(face, paragraph_face),
|
||||
x,
|
||||
y: baseline,
|
||||
size,
|
||||
size: glyph_size,
|
||||
text: character_text(character),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user