Colour href labels from hypersetup

This commit is contained in:
gloamxun committed 2026-10-26 02:38:41 +08:00
1 parent 8f429b892b
commit ea89bc1522
6 files changed
+58 -26

No files matched your search

+44 -21
View File
@@ -242,6 +242,8 @@ struct PageGeometry {
pdf_author: Option<String>,
pdf_subject: Option<String>,
pdf_keywords: Option<String>,
/// Colour applied to `\href` labels (`xcolor`'s `urlcolor`/`linkcolor`).
link_color: Option<String>,
/// `thebibliography` entries: label key and entry text, in order.
bibliography: Vec<(String, String)>,
/// `\newtheorem` definitions from the preamble.
@@ -1879,6 +1881,7 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
"pdfauthor" => geometry.pdf_author = Some(value),
"pdfsubject" => geometry.pdf_subject = Some(value),
"pdfkeywords" => geometry.pdf_keywords = Some(value),
"urlcolor" | "linkcolor" => geometry.link_color = Some(value),
_ => {}
}
}
@@ -5807,6 +5810,7 @@ fn inline_math_segments(
base: ParagraphFont,
style: &ParagraphStyle,
custom: &[(String, [u8; 3])],
link_color: Option<[u8; 3]>,
) -> Option<Vec<texrra_tex_core::paragraph::InlineSegment>> {
if style.font_size != 655_360 {
return None;
@@ -5869,6 +5873,7 @@ fn inline_math_segments(
font,
url: url.clone(),
text,
color: link_color,
});
}
}
@@ -7884,29 +7889,37 @@ fn build_document_with_labels(
xspace_skip: geometry.xspace_skip,
};
let plain = strip_hrefs(&text);
let mut unit =
match inline_math_segments(&text, paragraph.font, &style, &geometry.colors) {
Some(segments) => Unit::paragraph_with_style_and_segments(
let mut unit = match inline_math_segments(
&text,
paragraph.font,
&style,
&geometry.colors,
geometry
.link_color
.as_deref()
.and_then(|name| color_spec(name, &geometry.colors)),
) {
Some(segments) => Unit::paragraph_with_style_and_segments(
index as u64,
text.clone(),
style.clone(),
segments,
),
None => match inline_runs(&plain, paragraph.font) {
Some(runs) => Unit::paragraph_with_style_and_runs(
index as u64,
text.clone(),
style.clone(),
segments,
plain.clone(),
style,
runs,
),
None => match inline_runs(&plain, paragraph.font) {
Some(runs) => Unit::paragraph_with_style_and_runs(
index as u64,
plain.clone(),
style,
runs,
),
None => Unit::paragraph_with_style_and_face(
index as u64,
plain,
style,
paragraph.font,
),
},
};
None => Unit::paragraph_with_style_and_face(
index as u64,
plain,
style,
paragraph.font,
),
},
};
unit.space_before = paragraph.space_before;
unit.space_stretch = paragraph.space_stretch;
unit.space_shrink = paragraph.space_shrink;
@@ -11152,6 +11165,16 @@ A definition.
assert!(!json.contains("\\large"), "{json}");
}
#[test]
fn hypersetup_urlcolor_paints_links() {
let json = preview_json(
"\\hypersetup{urlcolor=red}\n\nVisit \\href{https://example.com}{Example}.",
)
.expect("preview");
assert!(json.contains(r##"fill=\"#ff0000\""##), "{json}");
assert!(json.contains(">E</text>"), "{json}");
}
#[test]
fn href_creates_link_boxes() {
let source = "Visit \\href{https://example.com}{the site} now.";