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

+39
View File
@@ -6614,6 +6614,7 @@ mod tests {
65_536 * 10,
0,
[0, 255, 0],
None,
"Boxed".into(),
)];
let style = crate::paragraph::ParagraphStyle {
@@ -6642,6 +6643,44 @@ mod tests {
assert!(rect.1 > 0);
}
#[test]
fn framed_color_boxes_paint_a_frame() {
let segments = vec![crate::paragraph::InlineSegment::ColorBox(
ParagraphFont::Roman,
65_536 * 10,
0,
[0, 255, 0],
Some([255, 0, 0]),
"Framed".into(),
)];
let style = crate::paragraph::ParagraphStyle {
line_width: 480 * 65_536,
..crate::paragraph::ParagraphStyle::default()
};
let unit = Unit::paragraph_with_style_and_segments(1, "Framed", style, segments);
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();
let frame = list
.items
.iter()
.find_map(|item| match item {
crate::render::DisplayItem::Frame {
color, thickness, ..
} => Some((*color, *thickness)),
_ => None,
})
.expect("frame");
assert_eq!(frame.0, [255, 0, 0]);
assert!(frame.1 > 0);
}
#[test]
fn sized_paragraphs_paint_at_their_font_size() {
let style = crate::paragraph::ParagraphStyle {
+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,
})
+20
View File
@@ -638,6 +638,26 @@ fn page_content(
));
}
}
DisplayItem::Frame {
x,
y,
width,
height,
color,
thickness,
} => {
content.push_str(&format!(
"{:.3} {:.3} {:.3} RG\n{:.3} w\n{} {} {} {} re S\n0 0 0 RG\n1 w\n",
color[0] as f64 / 255.0,
color[1] as f64 / 255.0,
color[2] as f64 / 255.0,
*thickness as f64 / 65_536.0,
pt(*x),
pt(list.height - *y - *height),
pt(*width),
pt(*height)
));
}
DisplayItem::Link { .. } => {}
DisplayItem::Fill([r, g, b]) => {
content.push_str(&format!(
+39
View File
@@ -76,6 +76,14 @@ pub enum DisplayItem {
height: i64,
color: [u8; 3],
},
Frame {
x: i64,
y: i64,
width: i64,
height: i64,
color: [u8; 3],
thickness: i64,
},
Link {
x: i64,
y: i64,
@@ -170,6 +178,7 @@ impl DisplayList {
*y2 += dy;
}
DisplayItem::Rect { x, y, .. }
| DisplayItem::Frame { x, y, .. }
| DisplayItem::Image { x, y, .. }
| DisplayItem::Link { x, y, .. } => {
*x += dx;
@@ -288,6 +297,26 @@ impl DisplayList {
));
}
// Link annotations are PDF metadata; the SVG preview omits them.
DisplayItem::Frame {
x,
y,
width,
height,
color,
thickness,
} => {
out.push_str(&format!(
"<rect x=\"{:.3}\" y=\"{:.3}\" width=\"{:.3}\" height=\"{:.3}\" fill=\"none\" stroke=\"#{:02x}{:02x}{:02x}\" stroke-width=\"{:.3}\"/>\n",
*x as f64 / 65_536.0,
(*y - *height) as f64 / 65_536.0,
*width as f64 / 65_536.0,
*height as f64 / 65_536.0,
color[0],
color[1],
color[2],
*thickness as f64 / 65_536.0
));
}
DisplayItem::Link { .. } => {}
DisplayItem::Special(content) => {
out.push_str(&format!(
@@ -654,6 +683,16 @@ impl Page {
list.items.push(DisplayItem::Fill([0, 0, 0]));
current_color = None;
}
if let Some((frame_color, thickness)) = box_.frame {
list.items.push(DisplayItem::Frame {
x,
y: baseline - box_.height,
width: box_.width,
height: box_.height + box_.depth,
color: frame_color,
thickness,
});
}
if box_.color != current_color {
let rgb = box_.color.unwrap_or([0, 0, 0]);
list.items.push(DisplayItem::Fill(rgb));