Paint colorbox backgrounds

This commit is contained in:
gloamxun committed 2026-10-26 03:05:07 +08:00
1 parent eebe6deac5
commit d88e74cd29
8 files changed
+167 -25

No files matched your search

+18 -2
View File
@@ -29,6 +29,8 @@ pub struct HorizontalBox {
/// Explicit RGB fill when this glyph came from `\textcolor`.
#[serde(default)]
pub color: Option<[u8; 3]>,
/// Background colour painted behind a `\\colorbox` run.
pub background: Option<[u8; 3]>,
}
/// A run of paragraph material: text in one face or an inline formula.
@@ -41,6 +43,8 @@ pub enum InlineSegment {
Script(ParagraphFont, i64, i64, String),
/// 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),
Math(crate::math::MathAtom),
Link {
font: ParagraphFont,
@@ -647,6 +651,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
math: None,
raise: 0,
color: None,
background: None,
size: None,
link: None,
}));
@@ -1118,6 +1123,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
math: None,
raise: 0,
color: None,
background: None,
size: None,
link: None,
}));
@@ -1132,6 +1138,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
math: None,
raise: 0,
color: None,
background: None,
size: None,
link: None,
}));
@@ -1480,6 +1487,7 @@ pub fn text_items_with_font(
math: None,
raise: 0,
color: None,
background: None,
size: None,
link: None,
}));
@@ -1727,7 +1735,8 @@ 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::Color(font, size, raise, color, text)
| InlineSegment::ColorBox(font, size, raise, color, text) => {
if text.is_empty() {
continue;
}
@@ -1761,7 +1770,11 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
box_.font = Some(*font);
box_.size = Some(*size);
box_.raise = *raise;
box_.color = Some(*color);
if matches!(segment, InlineSegment::ColorBox(..)) {
box_.background = Some(*color);
} else {
box_.color = Some(*color);
}
}
}
items.extend(run);
@@ -1839,6 +1852,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
size: None,
raise: 0,
color: None,
background: None,
link: None,
}));
previous = None;
@@ -1853,6 +1867,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::Link { font, .. }) => *font,
_ => ParagraphFont::Roman,
};
@@ -2281,6 +2296,7 @@ mod tests {
math: None,
raise: 0,
color: None,
background: None,
size: None,
link: None,
})