Paint colorbox backgrounds
This commit is contained in:
1 parent
eebe6deac5
commit
d88e74cd29
8 files changed
+167
-25
No files matched your search
@@ -6607,6 +6607,41 @@ mod tests {
|
||||
assert!(svg.contains(">s</text>"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn color_box_segments_paint_a_background_rect() {
|
||||
let segments = vec![crate::paragraph::InlineSegment::ColorBox(
|
||||
ParagraphFont::Roman,
|
||||
65_536 * 10,
|
||||
0,
|
||||
[0, 255, 0],
|
||||
"Boxed".into(),
|
||||
)];
|
||||
let style = crate::paragraph::ParagraphStyle {
|
||||
line_width: 480 * 65_536,
|
||||
..crate::paragraph::ParagraphStyle::default()
|
||||
};
|
||||
let unit = Unit::paragraph_with_style_and_segments(1, "Boxed", 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 rect = list
|
||||
.items
|
||||
.iter()
|
||||
.find_map(|item| match item {
|
||||
crate::render::DisplayItem::Rect { color, width, .. } => Some((*color, *width)),
|
||||
_ => None,
|
||||
})
|
||||
.expect("background rect");
|
||||
assert_eq!(rect.0, [0, 255, 0]);
|
||||
assert!(rect.1 > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sized_paragraphs_paint_at_their_font_size() {
|
||||
let style = crate::paragraph::ParagraphStyle {
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
@@ -573,6 +573,24 @@ fn page_content(
|
||||
));
|
||||
}
|
||||
DisplayItem::Background(_) => {}
|
||||
DisplayItem::Rect {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
color,
|
||||
} => {
|
||||
content.push_str(&format!(
|
||||
"{:.3} {:.3} {:.3} rg\n{} {} {} {} re f\n",
|
||||
color[0] as f64 / 255.0,
|
||||
color[1] as f64 / 255.0,
|
||||
color[2] as f64 / 255.0,
|
||||
pt(*x),
|
||||
pt(height - *y - *height),
|
||||
pt(*width),
|
||||
pt(*height)
|
||||
));
|
||||
}
|
||||
DisplayItem::Rule {
|
||||
x,
|
||||
y,
|
||||
|
||||
@@ -69,6 +69,13 @@ pub enum DisplayItem {
|
||||
height: i64,
|
||||
index: usize,
|
||||
},
|
||||
Rect {
|
||||
x: i64,
|
||||
y: i64,
|
||||
width: i64,
|
||||
height: i64,
|
||||
color: [u8; 3],
|
||||
},
|
||||
Link {
|
||||
x: i64,
|
||||
y: i64,
|
||||
@@ -162,7 +169,9 @@ impl DisplayList {
|
||||
*x2 += dx;
|
||||
*y2 += dy;
|
||||
}
|
||||
DisplayItem::Image { x, y, .. } | DisplayItem::Link { x, y, .. } => {
|
||||
DisplayItem::Rect { x, y, .. }
|
||||
| DisplayItem::Image { x, y, .. }
|
||||
| DisplayItem::Link { x, y, .. } => {
|
||||
*x += dx;
|
||||
*y += dy;
|
||||
}
|
||||
@@ -246,6 +255,24 @@ impl DisplayList {
|
||||
escape(text)
|
||||
));
|
||||
}
|
||||
DisplayItem::Rect {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
color,
|
||||
} => {
|
||||
out.push_str(&format!(
|
||||
"<rect x=\"{:.3}\" y=\"{:.3}\" width=\"{:.3}\" height=\"{:.3}\" fill=\"#{:02x}{:02x}{:02x}\"/>\n",
|
||||
*x as f64 / 65_536.0,
|
||||
*y as f64 / 65_536.0,
|
||||
*width as f64 / 65_536.0,
|
||||
*height as f64 / 65_536.0,
|
||||
color[0],
|
||||
color[1],
|
||||
color[2]
|
||||
));
|
||||
}
|
||||
DisplayItem::Rule {
|
||||
x,
|
||||
y,
|
||||
@@ -616,6 +643,17 @@ impl Page {
|
||||
} else {
|
||||
flush_link(&mut link_run, &mut list.items);
|
||||
}
|
||||
if let Some(background) = box_.background {
|
||||
list.items.push(DisplayItem::Rect {
|
||||
x,
|
||||
y: baseline - box_.height,
|
||||
width: box_.width,
|
||||
height: box_.height + box_.depth,
|
||||
color: background,
|
||||
});
|
||||
list.items.push(DisplayItem::Fill([0, 0, 0]));
|
||||
current_color = None;
|
||||
}
|
||||
if box_.color != current_color {
|
||||
let rgb = box_.color.unwrap_or([0, 0, 0]);
|
||||
list.items.push(DisplayItem::Fill(rgb));
|
||||
|
||||
Reference in new issue
Block a user