Paint framed colorbox backgrounds

This commit is contained in:
gloamxun committed 2026-10-26 03:08:37 +08:00
1 parent d88e74cd29
commit 2018e5f8b8
8 files changed
+240 -62

No files matched your search

+54 -9
View File
@@ -31,6 +31,9 @@ pub struct HorizontalBox {
pub color: Option<[u8; 3]>,
/// Background colour painted behind a `\\colorbox` run.
pub background: Option<[u8; 3]>,
/// A frame colour and line width drawn around the box, like `\fcolorbox`.
#[serde(default)]
pub frame: Option<([u8; 3], i64)>,
}
/// A run of paragraph material: text in one face or an inline formula.
@@ -44,7 +47,7 @@ pub enum InlineSegment {
/// A run painted with an explicit RGB colour.
Color(ParagraphFont, i64, i64, [u8; 3], String),
/// A run painted over an RGB background, like `\\colorbox`.
ColorBox(ParagraphFont, i64, i64, [u8; 3], String),
ColorBox(ParagraphFont, i64, i64, [u8; 3], Option<[u8; 3]>, String),
Math(crate::math::MathAtom),
Link {
font: ParagraphFont,
@@ -652,6 +655,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
raise: 0,
color: None,
background: None,
frame: None,
size: None,
link: None,
}));
@@ -1124,6 +1128,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
raise: 0,
color: None,
background: None,
frame: None,
size: None,
link: None,
}));
@@ -1139,6 +1144,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
raise: 0,
color: None,
background: None,
frame: None,
size: None,
link: None,
}));
@@ -1488,6 +1494,7 @@ pub fn text_items_with_font(
raise: 0,
color: None,
background: None,
frame: None,
size: None,
link: None,
}));
@@ -1735,8 +1742,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
previous = Some((*font, text.clone()));
seen_content = true;
}
InlineSegment::Color(font, size, raise, color, text)
| InlineSegment::ColorBox(font, size, raise, color, text) => {
InlineSegment::Color(font, size, raise, color, text) => {
if text.is_empty() {
continue;
}
@@ -1770,11 +1776,48 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
box_.font = Some(*font);
box_.size = Some(*size);
box_.raise = *raise;
if matches!(segment, InlineSegment::ColorBox(..)) {
box_.background = Some(*color);
} else {
box_.color = Some(*color);
}
box_.color = Some(*color);
}
}
items.extend(run);
previous = Some((*font, text.clone()));
}
InlineSegment::ColorBox(font, size, raise, color, frame, 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);
box_.raise = *raise;
box_.background = Some(*color);
box_.frame = frame.map(|frame| (frame, 26_214));
}
}
items.extend(run);
@@ -1853,6 +1896,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
raise: 0,
color: None,
background: None,
frame: None,
link: None,
}));
previous = None;
@@ -1867,7 +1911,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
| Some(InlineSegment::SizedText(font, _, _))
| Some(InlineSegment::Script(font, _, _, _))
| Some(InlineSegment::Color(font, _, _, _, _))
| Some(InlineSegment::ColorBox(font, _, _, _, _))
| Some(InlineSegment::ColorBox(font, _, _, _, _, _))
| Some(InlineSegment::Link { font, .. }) => *font,
_ => ParagraphFont::Roman,
};
@@ -2297,6 +2341,7 @@ mod tests {
raise: 0,
color: None,
background: None,
frame: None,
size: None,
link: None,
})