Paint textcolor runs in named colours
This commit is contained in:
1 parent
f98964f6b6
commit
9ad404acee
7 files changed
+264
-38
No files matched your search
+147
-31
@@ -4559,6 +4559,97 @@ fn strip_hrefs(text: &str) -> String {
|
||||
output
|
||||
}
|
||||
|
||||
fn named_color(name: &str) -> Option<[u8; 3]> {
|
||||
match name.trim().to_ascii_lowercase().as_str() {
|
||||
"black" => Some([0, 0, 0]),
|
||||
"white" => Some([255, 255, 255]),
|
||||
"red" => Some([255, 0, 0]),
|
||||
"green" => Some([0, 128, 0]),
|
||||
"blue" => Some([0, 0, 255]),
|
||||
"cyan" => Some([0, 255, 255]),
|
||||
"magenta" => Some([255, 0, 255]),
|
||||
"yellow" => Some([255, 255, 0]),
|
||||
"gray" | "grey" => Some([128, 128, 128]),
|
||||
"darkgray" | "darkgrey" => Some([64, 64, 64]),
|
||||
"lightgray" | "lightgrey" => Some([192, 192, 192]),
|
||||
"brown" => Some([165, 42, 42]),
|
||||
"lime" => Some([0, 255, 0]),
|
||||
"olive" => Some([128, 128, 0]),
|
||||
"orange" => Some([255, 165, 0]),
|
||||
"pink" => Some([255, 192, 203]),
|
||||
"purple" => Some([128, 0, 128]),
|
||||
"teal" => Some([0, 128, 128]),
|
||||
"violet" => Some([238, 130, 238]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Splits `\textcolor{name}{text}` runs out of a paragraph piece.
|
||||
fn split_textcolor(text: &str) -> Vec<(Option<[u8; 3]>, String)> {
|
||||
let mut out = Vec::new();
|
||||
let mut rest = text;
|
||||
let mut plain = String::new();
|
||||
while let Some(index) = rest.find("\\textcolor") {
|
||||
plain.push_str(&rest[..index]);
|
||||
let after = rest[index + "\\textcolor".len()..].trim_start();
|
||||
let Some(after) = after.strip_prefix('{') 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 Some(end) = end else {
|
||||
plain.push_str(&rest[index..]);
|
||||
rest = "";
|
||||
break;
|
||||
};
|
||||
match named_color(name) {
|
||||
Some(color) => {
|
||||
if !plain.is_empty() {
|
||||
out.push((None, std::mem::take(&mut plain)));
|
||||
}
|
||||
out.push((Some(color), body[..end].to_string()));
|
||||
}
|
||||
None => plain.push_str(&rest[index..index + 1]),
|
||||
}
|
||||
rest = &body[end + 1..];
|
||||
}
|
||||
plain.push_str(rest);
|
||||
if !plain.is_empty() {
|
||||
out.push((None, plain));
|
||||
}
|
||||
if out.is_empty() {
|
||||
out.push((None, text.to_string()));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn inline_math_segments(
|
||||
text: &str,
|
||||
base: ParagraphFont,
|
||||
@@ -4571,35 +4662,47 @@ fn inline_math_segments(
|
||||
let mut found_math = false;
|
||||
let mut found_link = false;
|
||||
let mut found_size = false;
|
||||
let mut found_color = false;
|
||||
for part in split_hrefs(text) {
|
||||
match part {
|
||||
HrefPart::Plain(piece) => {
|
||||
let runs = inline_styled_runs(&piece, base)
|
||||
.unwrap_or_else(|| vec![(base, style.font_size, 0, piece.clone())]);
|
||||
for (font, size, raise, run_text) in runs {
|
||||
for (is_math, piece) in split_inline_math(&run_text) {
|
||||
if is_math {
|
||||
found_math = true;
|
||||
segments.push(texrra_tex_core::paragraph::InlineSegment::Math(
|
||||
parse_math(&piece),
|
||||
));
|
||||
} else if !piece.is_empty() {
|
||||
if raise != 0 {
|
||||
found_size = true;
|
||||
segments.push(texrra_tex_core::paragraph::InlineSegment::Script(
|
||||
font, size, raise, piece,
|
||||
for (color, chunk) in split_textcolor(&piece) {
|
||||
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 {
|
||||
for (is_math, piece) in split_inline_math(&run_text) {
|
||||
if is_math {
|
||||
found_math = true;
|
||||
segments.push(texrra_tex_core::paragraph::InlineSegment::Math(
|
||||
parse_math(&piece),
|
||||
));
|
||||
} else if size == 655_360 {
|
||||
segments.push(texrra_tex_core::paragraph::InlineSegment::Text(
|
||||
font, piece,
|
||||
));
|
||||
} else {
|
||||
found_size = true;
|
||||
segments.push(
|
||||
texrra_tex_core::paragraph::InlineSegment::SizedText(
|
||||
font, size, piece,
|
||||
),
|
||||
);
|
||||
} 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,
|
||||
),
|
||||
);
|
||||
} else if raise != 0 {
|
||||
found_size = true;
|
||||
segments.push(
|
||||
texrra_tex_core::paragraph::InlineSegment::Script(
|
||||
font, size, raise, piece,
|
||||
),
|
||||
);
|
||||
} else if size == 655_360 {
|
||||
segments.push(texrra_tex_core::paragraph::InlineSegment::Text(
|
||||
font, piece,
|
||||
));
|
||||
} else {
|
||||
found_size = true;
|
||||
segments.push(
|
||||
texrra_tex_core::paragraph::InlineSegment::SizedText(
|
||||
font, size, piece,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4618,7 +4721,7 @@ fn inline_math_segments(
|
||||
}
|
||||
}
|
||||
}
|
||||
(found_math || found_link || found_size).then_some(segments)
|
||||
(found_math || found_link || found_size || found_color).then_some(segments)
|
||||
}
|
||||
|
||||
fn inline_runs(text: &str, base: ParagraphFont) -> Option<Vec<(ParagraphFont, String)>> {
|
||||
@@ -7830,15 +7933,16 @@ a & b
|
||||
|
||||
#[test]
|
||||
fn preview_warnings_flag_leaked_commands() {
|
||||
let json = preview_json(r"See \textcolor{red}{red} and \unknownthing.").expect("preview");
|
||||
assert!(
|
||||
json.contains(r"unsupported command `\\textcolor`"),
|
||||
"{json}"
|
||||
);
|
||||
let json = preview_json(r"See \unknownthing and \textcolor{red}{red}.").expect("preview");
|
||||
assert!(
|
||||
json.contains(r"unsupported command `\\unknownthing`"),
|
||||
"{json}"
|
||||
);
|
||||
assert!(
|
||||
!json.contains(r"unsupported command `\\textcolor`"),
|
||||
"{json}"
|
||||
);
|
||||
assert!(json.contains("#ff0000"), "{json}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -9741,6 +9845,18 @@ A definition.
|
||||
assert!(boxes.iter().any(|box_| box_.character == Some('$')));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn textcolor_paints_red_glyphs() {
|
||||
let source = "Body \\textcolor{red}{red} word.";
|
||||
let json = preview_json(source).expect("preview");
|
||||
assert!(json.contains(r##"fill=\"#ff0000\""##), "{json}");
|
||||
assert!(!json.contains("textcolor"));
|
||||
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("textcolor"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_width_sets_the_paragraph_measure() {
|
||||
let long =
|
||||
|
||||
Reference in new issue
Block a user