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

+53 -18
View File
@@ -1901,9 +1901,9 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
if let Some(rest) = trimmed.strip_prefix("\\DeclareMathOperator") {
let mut rest = rest.trim_start();
let mut starred = false;
if let Some(after) = rest.strip_prefix('*') {
if rest.starts_with('*') {
starred = true;
rest = after.trim_start();
rest = rest[1..].trim_start();
}
if let Some((command, tail)) = braced(rest) {
if let Some((body, _)) = braced(tail.trim_start()) {
@@ -2592,7 +2592,7 @@ impl<'a> NumericParser<'a> {
fn factor(&mut self) -> Option<i64> {
self.skip_space();
let rest = &self.text[self.position..];
if let Some(after) = rest.strip_prefix('(') {
if rest.starts_with('(') {
self.position += 1;
let value = self.expression()?;
self.skip_space();
@@ -2600,7 +2600,7 @@ impl<'a> NumericParser<'a> {
self.position += 1;
return Some(value);
}
if let Some(after) = rest.strip_prefix('-') {
if rest.starts_with('-') {
self.position += 1;
return Some(self.factor()?.saturating_neg());
}
@@ -2632,7 +2632,7 @@ impl<'a> NumericParser<'a> {
loop {
self.skip_space();
let rest = &self.text[self.position..];
if let Some(after) = rest.strip_prefix('*') {
if rest.starts_with('*') {
self.position += 1;
value = value.saturating_mul(self.factor()?);
} else if rest.starts_with('/') {
@@ -6381,13 +6381,28 @@ fn named_color(name: &str, custom: &[(String, [u8; 3])]) -> Option<[u8; 3]> {
}
/// Splits `\textcolor{name}{text}` runs out of a paragraph piece.
fn split_textcolor(text: &str, custom: &[(String, [u8; 3])]) -> Vec<(Option<[u8; 3]>, String)> {
fn split_color_runs(
text: &str,
custom: &[(String, [u8; 3])],
) -> Vec<(Option<[u8; 3]>, bool, String)> {
let mut out = Vec::new();
let mut rest = text;
let mut plain = String::new();
while let Some(index) = rest.find("\\textcolor") {
loop {
let next = rest
.find("\\textcolor")
.map(|index| (index, "\\textcolor", false))
.into_iter()
.chain(
rest.find("\\colorbox")
.map(|index| (index, "\\colorbox", true)),
)
.min_by_key(|(index, _, _)| *index);
let Some((index, command, background)) = next else {
break;
};
plain.push_str(&rest[..index]);
let after = rest[index + "\\textcolor".len()..].trim_start();
let after = rest[index + command.len()..].trim_start();
let Some(after) = after.strip_prefix('{') else {
plain.push_str(&rest[index..]);
rest = "";
@@ -6428,9 +6443,9 @@ fn split_textcolor(text: &str, custom: &[(String, [u8; 3])]) -> Vec<(Option<[u8;
match color_spec(name, custom) {
Some(color) => {
if !plain.is_empty() {
out.push((None, std::mem::take(&mut plain)));
out.push((None, false, std::mem::take(&mut plain)));
}
out.push((Some(color), body[..end].to_string()));
out.push((Some(color), background, body[..end].to_string()));
}
None => plain.push_str(&rest[index..index + 1]),
}
@@ -6438,10 +6453,10 @@ fn split_textcolor(text: &str, custom: &[(String, [u8; 3])]) -> Vec<(Option<[u8;
}
plain.push_str(rest);
if !plain.is_empty() {
out.push((None, plain));
out.push((None, false, plain));
}
if out.is_empty() {
out.push((None, text.to_string()));
out.push((None, false, text.to_string()));
}
out
}
@@ -6464,7 +6479,7 @@ fn inline_math_segments(
for part in split_hrefs(text) {
match part {
HrefPart::Plain(piece) => {
for (color, chunk) in split_textcolor(&piece, custom) {
for (color, background, chunk) in split_color_runs(&piece, custom) {
let runs = inline_styled_runs(&chunk, base)
.unwrap_or_else(|| vec![(base, style.font_size, 0, chunk.clone())]);
for (font, size, raise, run_text) in runs {
@@ -6477,11 +6492,19 @@ fn inline_math_segments(
} else if !piece.is_empty() {
if let Some(rgb) = color {
found_color = true;
segments.push(
texrra_tex_core::paragraph::InlineSegment::Color(
font, size, raise, rgb, piece,
),
);
if background {
segments.push(
texrra_tex_core::paragraph::InlineSegment::ColorBox(
font, size, raise, rgb, piece,
),
);
} else {
segments.push(
texrra_tex_core::paragraph::InlineSegment::Color(
font, size, raise, rgb, piece,
),
);
}
} else if raise != 0 {
found_size = true;
segments.push(
@@ -12095,6 +12118,18 @@ A definition.
assert!(text.contains("1.000 1.000 0.000 rg"), "{text}");
}
#[test]
fn colorbox_paints_a_background_rectangle() {
let source = "Body \\colorbox{lime}{Boxed} word.";
let json = preview_json(source).expect("preview");
assert!(json.contains(r##"fill=\"#00ff00\""##), "{json}");
assert!(json.contains(r##"<rect "##), "{json}");
assert!(!json.contains("colorbox"));
let pdf = pdf_bytes(source).expect("pdf");
let text = String::from_utf8_lossy(&pdf);
assert!(text.contains("0.000 1.000 0.000 rg"), "{text}");
}
#[test]
fn textcolor_paints_red_glyphs() {
let source = "Body \\textcolor{red}{red} word.";