Paint framed colorbox backgrounds
This commit is contained in:
1 parent
d88e74cd29
commit
2018e5f8b8
8 files changed
+240
-62
No files matched your search
+84
-49
@@ -6381,82 +6381,104 @@ fn named_color(name: &str, custom: &[(String, [u8; 3])]) -> Option<[u8; 3]> {
|
||||
}
|
||||
|
||||
/// Splits `\textcolor{name}{text}` runs out of a paragraph piece.
|
||||
fn take_braced_group(text: &str) -> Option<(String, &str)> {
|
||||
let text = text.trim_start();
|
||||
let body = text.strip_prefix('{')?;
|
||||
let mut depth = 1_usize;
|
||||
for (offset, character) in body.char_indices() {
|
||||
match character {
|
||||
'{' => depth += 1,
|
||||
'}' => {
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
return Some((body[..offset].to_string(), &body[offset + 1..]));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn split_color_runs(
|
||||
text: &str,
|
||||
custom: &[(String, [u8; 3])],
|
||||
) -> Vec<(Option<[u8; 3]>, bool, String)> {
|
||||
) -> Vec<(Option<[u8; 3]>, bool, Option<[u8; 3]>, String)> {
|
||||
let mut out = Vec::new();
|
||||
let mut rest = text;
|
||||
let mut plain = String::new();
|
||||
loop {
|
||||
let next = rest
|
||||
.find("\\textcolor")
|
||||
.map(|index| (index, "\\textcolor", false))
|
||||
.map(|index| (index, "\\textcolor"))
|
||||
.into_iter()
|
||||
.chain(
|
||||
rest.find("\\colorbox")
|
||||
.map(|index| (index, "\\colorbox", true)),
|
||||
)
|
||||
.min_by_key(|(index, _, _)| *index);
|
||||
let Some((index, command, background)) = next else {
|
||||
.chain(rest.find("\\colorbox").map(|index| (index, "\\colorbox")))
|
||||
.chain(rest.find("\\fcolorbox").map(|index| (index, "\\fcolorbox")))
|
||||
.min_by_key(|(index, _)| *index);
|
||||
let Some((index, command)) = next else {
|
||||
break;
|
||||
};
|
||||
plain.push_str(&rest[..index]);
|
||||
let after = rest[index + command.len()..].trim_start();
|
||||
let Some(after) = after.strip_prefix('{') else {
|
||||
let after_command = &rest[index + command.len()..];
|
||||
let Some((first, after_first)) = take_braced_group(after_command) else {
|
||||
plain.push_str(&rest[index..]);
|
||||
rest = "";
|
||||
break;
|
||||
};
|
||||
let Some(close) = after.find('}') else {
|
||||
plain.push_str(&rest[index..]);
|
||||
rest = "";
|
||||
break;
|
||||
};
|
||||
let name = &after[..close];
|
||||
let after_name = after[close + 1..].trim_start();
|
||||
let Some(body) = after_name.strip_prefix('{') else {
|
||||
plain.push_str(&rest[index..]);
|
||||
rest = "";
|
||||
break;
|
||||
};
|
||||
let mut depth = 1_usize;
|
||||
let mut end = None;
|
||||
for (offset, character) in body.char_indices() {
|
||||
match character {
|
||||
'{' => depth += 1,
|
||||
'}' => {
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
end = Some(offset);
|
||||
break;
|
||||
}
|
||||
let (color, background, frame, body_source) = match command {
|
||||
"\\textcolor" => match color_spec(&first, custom) {
|
||||
Some(color) => (Some(color), false, None, after_first),
|
||||
None => {
|
||||
plain.push_str(&rest[index..=index]);
|
||||
rest = &rest[index + 1..];
|
||||
continue;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
"\\colorbox" => match color_spec(&first, custom) {
|
||||
Some(color) => (Some(color), true, None, after_first),
|
||||
None => {
|
||||
plain.push_str(&rest[index..=index]);
|
||||
rest = &rest[index + 1..];
|
||||
continue;
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
let Some(frame) = color_spec(&first, custom) else {
|
||||
plain.push_str(&rest[index..=index]);
|
||||
rest = &rest[index + 1..];
|
||||
continue;
|
||||
};
|
||||
let Some((background_name, after_background)) = take_braced_group(after_first)
|
||||
else {
|
||||
plain.push_str(&rest[index..]);
|
||||
rest = "";
|
||||
break;
|
||||
};
|
||||
let Some(background) = color_spec(&background_name, custom) else {
|
||||
plain.push_str(&rest[index..=index]);
|
||||
rest = &rest[index + 1..];
|
||||
continue;
|
||||
};
|
||||
(Some(background), true, Some(frame), after_background)
|
||||
}
|
||||
}
|
||||
let Some(end) = end else {
|
||||
};
|
||||
let Some((body, after_body)) = take_braced_group(body_source) else {
|
||||
plain.push_str(&rest[index..]);
|
||||
rest = "";
|
||||
break;
|
||||
};
|
||||
match color_spec(name, custom) {
|
||||
Some(color) => {
|
||||
if !plain.is_empty() {
|
||||
out.push((None, false, std::mem::take(&mut plain)));
|
||||
}
|
||||
out.push((Some(color), background, body[..end].to_string()));
|
||||
}
|
||||
None => plain.push_str(&rest[index..index + 1]),
|
||||
if !plain.is_empty() {
|
||||
out.push((None, false, None, std::mem::take(&mut plain)));
|
||||
}
|
||||
rest = &body[end + 1..];
|
||||
out.push((color, background, frame, body));
|
||||
rest = after_body;
|
||||
}
|
||||
plain.push_str(rest);
|
||||
if !plain.is_empty() {
|
||||
out.push((None, false, plain));
|
||||
out.push((None, false, None, plain));
|
||||
}
|
||||
if out.is_empty() {
|
||||
out.push((None, false, text.to_string()));
|
||||
out.push((None, false, None, text.to_string()));
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -6479,7 +6501,7 @@ fn inline_math_segments(
|
||||
for part in split_hrefs(text) {
|
||||
match part {
|
||||
HrefPart::Plain(piece) => {
|
||||
for (color, background, chunk) in split_color_runs(&piece, custom) {
|
||||
for (color, background, frame, 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 {
|
||||
@@ -6495,7 +6517,7 @@ fn inline_math_segments(
|
||||
if background {
|
||||
segments.push(
|
||||
texrra_tex_core::paragraph::InlineSegment::ColorBox(
|
||||
font, size, raise, rgb, piece,
|
||||
font, size, raise, rgb, frame, piece,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
@@ -12118,6 +12140,19 @@ A definition.
|
||||
assert!(text.contains("1.000 1.000 0.000 rg"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fcolorbox_paints_a_frame_and_background() {
|
||||
let source = "Body \\fcolorbox{red}{lime}{Framed} word.";
|
||||
let json = preview_json(source).expect("preview");
|
||||
assert!(json.contains(r##"stroke=\"#ff0000\""##), "{json}");
|
||||
assert!(json.contains(r##"fill=\"#00ff00\""##), "{json}");
|
||||
assert!(!json.contains("fcolorbox"), "{json}");
|
||||
let pdf = pdf_bytes(source).expect("pdf");
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("1.000 0.000 0.000 RG"), "{text}");
|
||||
assert!(text.contains("0.000 1.000 0.000 rg"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn colorbox_paints_a_background_rectangle() {
|
||||
let source = "Body \\colorbox{lime}{Boxed} word.";
|
||||
|
||||
Reference in new issue
Block a user