Render hyperlinks with hyperref green colour

This commit is contained in:
gloamxun committed 2026-10-19 18:29:24 +08:00
1 parent a8b20563fb
commit 09781d40e9
12 files changed
+158 -11

No files matched your search

+1 -1
View File
@@ -74,7 +74,7 @@ const PRELUDE: &str = r#"
\def\nocite#1{}
\def\newblock{ }
\def\caption#1{\texrracaptionstart#1\texrracaptionend}
\def\href#1#2{#2}
\def\href#1#2{\texrrahref{#2}}
\def\hline{\texrratablerule}
\def\toprule{\texrratablerule}
\def\midrule{\texrratablerule}
+29
View File
@@ -1417,6 +1417,15 @@ fn boxworks_line_break(
);
}
}
if run.link {
horizontal.push(
ds::Char {
char: texrra_core::document::LINK_START_MARKER,
font: null_font_id,
}
.into(),
);
}
horizontal.push(
ds::Char {
char: glyph_character,
@@ -1424,6 +1433,15 @@ fn boxworks_line_break(
}
.into(),
);
if run.link {
horizontal.push(
ds::Char {
char: texrra_core::document::LINK_END_MARKER,
font: null_font_id,
}
.into(),
);
}
previous_character = Some(glyph_character);
previous_font_id = Some(font_id);
}
@@ -1516,6 +1534,7 @@ fn boxworks_line_break(
}
let mut line_items = Vec::new();
let mut leader_pending = false;
let mut link_state = false;
for item in &line_box.list {
match item {
ds::Horizontal::Char(character) => {
@@ -1523,6 +1542,14 @@ fn boxworks_line_break(
leader_pending = true;
continue;
}
if character.char == texrra_core::document::LINK_START_MARKER {
link_state = true;
continue;
}
if character.char == texrra_core::document::LINK_END_MARKER {
link_state = false;
continue;
}
if let Some(marker) = inline_markers.get(&character.char) {
match marker {
InlineMarker::RaiseStart(raise) => {
@@ -1563,6 +1590,7 @@ fn boxworks_line_break(
italic,
monospace,
sans,
link: link_state,
font_size: glyph_size,
y_offset,
});
@@ -2469,6 +2497,7 @@ pub(super) fn render_pdf(
sans: false,
small_caps: false,
link: false,
y_offset: None,
+2
View File
@@ -1039,6 +1039,7 @@ fn layout_framed_runs(
italic: run.italic,
monospace: run.monospace,
sans: run.sans,
link: run.link,
font_size: glyph_size,
y_offset: run.y_offset.unwrap_or_default(),
});
@@ -3326,6 +3327,7 @@ pub(super) fn text_line_metrics_scaled(
italic,
monospace,
sans,
link: _,
font_size: glyph_size,
y_offset,
} = item
+41
View File
@@ -3171,6 +3171,7 @@ fn url_primitive(
}
_ => argument.as_slice(),
};
write_event(input, token, FrontendEventKind::LinkStart);
write_event(
input,
token,
@@ -3202,6 +3203,7 @@ fn url_primitive(
token,
FrontendEventKind::TextStyleEnd(TextStyle::Monospace),
);
write_event(input, token, FrontendEventKind::LinkEnd);
Ok(())
}
@@ -3655,6 +3657,33 @@ fn vbox_end_primitive(
Ok(())
}
fn href_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
// The prelude macro consumes the URL argument; this primitive only sees the
// visible link text, which is wrapped in a link span for hyperref colour.
let content = read_unexpanded_argument(input, token, "expects link text")?;
let inner = strip_braced_tokens(&content).to_vec();
write_event(input, token, FrontendEventKind::LinkStart);
let trace_key = token.trace_key();
let mut expansion = inner;
expansion.push(control_sequence_token(input, "texrralinkend", trace_key));
input
.as_mut()
.expansions_mut()
.extend(expansion.into_iter().rev());
Ok(())
}
fn link_end_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
write_event(input, token, FrontendEventKind::LinkEnd);
Ok(())
}
fn fbox_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
@@ -7696,6 +7725,18 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
"includegraphics",
texlang::command::BuiltIn::new_execution(includegraphics_command_primitive),
);
built_ins.insert(
"href",
texlang::command::BuiltIn::new_execution(href_primitive),
);
built_ins.insert(
"texrrahref",
texlang::command::BuiltIn::new_execution(href_primitive),
);
built_ins.insert(
"texrralinkend",
texlang::command::BuiltIn::new_execution(link_end_primitive),
);
built_ins.insert(
"fam",
texlang::variable::Command::new_singleton(
+29 -5
View File
@@ -24,6 +24,7 @@ pub(super) enum LineItem {
italic: bool,
monospace: bool,
sans: bool,
link: bool,
font_size: u8,
y_offset: Scaled,
},
@@ -48,12 +49,14 @@ pub(super) enum LineItem {
struct TextSpan {
text: String,
style: Style,
link: bool,
y_offset: Mm,
}
pub(super) struct PositionedSpan {
pub(super) text: String,
pub(super) style: Style,
pub(super) link: bool,
pub(super) x: Mm,
pub(super) y_offset: Mm,
}
@@ -137,6 +140,7 @@ impl TextLine {
italic,
monospace,
sans,
link,
font_size: glyph_size,
y_offset,
} if is_cjk(*character) => {
@@ -161,7 +165,9 @@ impl TextLine {
monospace_family,
sans_family,
*glyph_size,
*link,
),
link: *link,
y_offset: scaled_to_mm(*y_offset),
}));
}
@@ -171,10 +177,19 @@ impl TextLine {
italic,
monospace,
sans,
link,
font_size: glyph_size,
y_offset,
} => {
let style_key = (*bold, *italic, *monospace, *sans, *glyph_size, *y_offset);
let style_key = (
*bold,
*italic,
*monospace,
*sans,
*link,
*glyph_size,
*y_offset,
);
if text_style != Some(style_key) {
flush_text(
&mut items,
@@ -396,6 +411,7 @@ impl TextLine {
spans.push(PositionedSpan {
text: span.text.clone(),
style: span.style,
link: span.link,
x,
y_offset: span.y_offset,
});
@@ -426,6 +442,7 @@ impl TextLine {
spans.push(PositionedSpan {
text: leader,
style,
link: false,
x,
y_offset: Mm::from(0.0),
});
@@ -504,10 +521,11 @@ impl TextLine {
};
let _ = write!(
output,
"<text x=\"{:.4}\" y=\"{:.4}\" xml:space=\"preserve\" font-family=\"{family}\" font-size=\"{:.4}\" font-weight=\"{weight}\" font-style=\"{slant}\">{}</text>",
"<text x=\"{:.4}\" y=\"{:.4}\" xml:space=\"preserve\" font-family=\"{family}\" font-size=\"{:.4}\" font-weight=\"{weight}\" font-style=\"{slant}\"{}>{}</text>",
f64::from(x + horizontal_offset + span.x),
f64::from(baseline - span.y_offset),
svg_font_size(span.style.font_size()),
if span.link { " fill=\"#008000\"" } else { "" },
escape_html(&span.text),
);
}
@@ -517,7 +535,7 @@ impl TextLine {
fn flush_text(
items: &mut Vec<TextLineItem>,
text: &mut String,
style_key: &mut Option<(bool, bool, bool, bool, u8, Scaled)>,
style_key: &mut Option<(bool, bool, bool, bool, bool, u8, Scaled)>,
cjk_family: Option<FontFamily<fonts::Font>>,
monospace_family: Option<FontFamily<fonts::Font>>,
sans_family: Option<FontFamily<fonts::Font>>,
@@ -527,10 +545,10 @@ fn flush_text(
*style_key = None;
return;
}
let (bold, italic, monospace, sans, glyph_size, y_offset) =
let (bold, italic, monospace, sans, link, glyph_size, y_offset) =
style_key
.take()
.unwrap_or((false, false, false, false, font_size, Scaled::ZERO));
.unwrap_or((false, false, false, false, false, font_size, Scaled::ZERO));
items.push(TextLineItem::Text(TextSpan {
text: std::mem::take(text),
style: text_style_for(
@@ -543,7 +561,9 @@ fn flush_text(
monospace_family,
sans_family,
glyph_size,
link,
),
link,
y_offset: scaled_to_mm(y_offset),
}));
}
@@ -558,8 +578,12 @@ fn text_style_for(
monospace_family: Option<FontFamily<fonts::Font>>,
sans_family: Option<FontFamily<fonts::Font>>,
font_size: u8,
link: bool,
) -> Style {
let mut style = Style::new().with_font_size(font_size);
if link {
style = style.with_color(genpdf::style::Color::Rgb(0, 128, 0));
}
if bold && !is_cjk {
style = style.bold();
}