Draw ORCID author badges as native vector marks

This commit is contained in:
gloamxun committed 2026-10-19 19:22:16 +08:00
1 parent ae7e27116f
commit ec6826619b
4 files changed
+79 -9

No files matched your search

+27
View File
@@ -358,6 +358,10 @@ enum PositionedPageItem {
element: PositionedFigure, element: PositionedFigure,
y: i64, y: i64,
}, },
Badge {
x: i64,
y: i64,
},
FramedBox { FramedBox {
anchor: PageAnchor, anchor: PageAnchor,
element: PositionedFramedBox, element: PositionedFramedBox,
@@ -542,6 +546,7 @@ impl Element for PositionedPages {
PositionedPageItem::DisplayMath { .. } => "display math", PositionedPageItem::DisplayMath { .. } => "display math",
PositionedPageItem::Table { .. } => "table", PositionedPageItem::Table { .. } => "table",
PositionedPageItem::Image { .. } => "image", PositionedPageItem::Image { .. } => "image",
PositionedPageItem::Badge { .. } => "badge",
PositionedPageItem::FramedBox { .. } => "framed box", PositionedPageItem::FramedBox { .. } => "framed box",
PositionedPageItem::Centered { .. } => "centered block", PositionedPageItem::Centered { .. } => "centered block",
}; };
@@ -627,6 +632,28 @@ impl Element for PositionedPages {
y, y,
) )
} }
PositionedPageItem::Badge { x, y } => {
let x = scaled_units_to_mm(*x);
let y = scaled_units_to_mm(*y);
let mut badge_area = body_area.clone();
badge_area.add_offset(Position::new(x, y));
badge_area.set_width(Mm::from(4.0));
badge_area.set_height(Mm::from(4.0));
badge_area.fill_rect(
Position::default(),
Size::new(4.0, 4.0),
Style::new().with_color(genpdf::style::Color::Rgb(166, 206, 57)),
);
badge_area.print_str(
&context.font_cache,
Position::new(0.55, 0.6),
Style::new()
.with_font_size(4)
.with_color(genpdf::style::Color::Rgb(255, 255, 255)),
"iD",
)?;
(RenderResult::default(), y)
}
PositionedPageItem::FramedBox { element, y, .. } => { PositionedPageItem::FramedBox { element, y, .. } => {
let y = scaled_units_to_mm(*y); let y = scaled_units_to_mm(*y);
let mut box_area = body_area.clone(); let mut box_area = body_area.clone();
+34 -7
View File
@@ -1897,16 +1897,16 @@ pub(super) fn build_positioned_pages(
let column_count = blocks.len(); let column_count = blocks.len();
let column_width = content_width_mm / column_count as f64; let column_width = content_width_mm / column_count as f64;
let mut measured_blocks = Vec::with_capacity(column_count); let mut measured_blocks = Vec::with_capacity(column_count);
let mut badge_rows: Vec<Vec<bool>> = Vec::with_capacity(column_count);
for lines in &blocks { for lines in &blocks {
let mut measured_lines = Vec::with_capacity(lines.len()); let mut measured_lines = Vec::with_capacity(lines.len());
let mut block_badges = Vec::with_capacity(lines.len());
for (index, text) in lines.iter().enumerate() { for (index, text) in lines.iter().enumerate() {
let mut runs = Vec::new(); let mut runs = Vec::new();
let mut rest = text.as_str(); let mut rest = text.as_str();
let mut has_badge = false;
if let Some(after_badge) = text.strip_prefix("iD ") { if let Some(after_badge) = text.strip_prefix("iD ") {
let mut badge = Run::plain("iD ".to_string()); has_badge = true;
badge.bold = true;
badge.link = true;
runs.push(badge);
rest = after_badge; rest = after_badge;
} }
if !rest.is_empty() { if !rest.is_empty() {
@@ -1936,9 +1936,11 @@ pub(super) fn build_positioned_pages(
.next() .next()
{ {
measured_lines.push(line); measured_lines.push(line);
block_badges.push(has_badge);
} }
} }
measured_blocks.push(measured_lines); measured_blocks.push(measured_lines);
badge_rows.push(block_badges);
} }
let row_count = measured_blocks.iter().map(Vec::len).max().unwrap_or(0); let row_count = measured_blocks.iter().map(Vec::len).max().unwrap_or(0);
for row in 0..row_count { for row in 0..row_count {
@@ -1982,10 +1984,22 @@ pub(super) fn build_positioned_pages(
mono_family, mono_family,
sans_family, sans_family,
); );
let has_badge = badge_rows
.get(column)
.and_then(|rows| rows.get(row))
.copied()
.unwrap_or(false);
let column_start = column as f64 * column_width; let column_start = column as f64 * column_width;
let x = millimeters_to_scaled( let badge_size = 4.0_f64;
column_start + (column_width - natural_width).max(0.0) / 2.0, let badge_gap = 1.0_f64;
); let badge_space = if has_badge {
badge_size + badge_gap
} else {
0.0
};
let line_x = column_start
+ (column_width - natural_width - badge_space).max(0.0) / 2.0;
let x = millimeters_to_scaled(line_x + badge_space);
pages pages
.last_mut() .last_mut()
.expect("the page list starts with one page") .expect("the page list starts with one page")
@@ -1998,6 +2012,19 @@ pub(super) fn build_positioned_pages(
centered: false, centered: false,
ragged: false, ragged: false,
})); }));
if has_badge {
let badge_y = y + millimeters_to_scaled(
(scaled_units_to_mm(row_height) - badge_size).max(0.0) / 2.0,
);
pages
.last_mut()
.expect("the page list starts with one page")
.items
.push(PositionedPageItem::Badge {
x: millimeters_to_scaled(line_x),
y: badge_y,
});
}
} }
cursor = cursor.saturating_add(row_height); cursor = cursor.saturating_add(row_height);
} }
+12
View File
@@ -137,6 +137,7 @@ fn page_svg(
monospace_family, monospace_family,
sans_family, sans_family,
), ),
PositionedPageItem::Badge { x, y } => write_badge(&mut output, *x, *y),
PositionedPageItem::Image { element, y, .. } => write_figure( PositionedPageItem::Image { element, y, .. } => write_figure(
&mut output, &mut output,
element, element,
@@ -261,6 +262,17 @@ fn write_table_rule(
} }
} }
fn write_badge(output: &mut String, x: i64, y: i64) {
let _ = write!(
output,
"<rect x=\"{:.4}\" y=\"{:.4}\" width=\"4.0000\" height=\"4.0000\" fill=\"#A6CE39\" /><text x=\"{:.4}\" y=\"{:.4}\" font-size=\"2.6\" fill=\"#FFFFFF\" font-family=\"serif\">iD</text>",
scaled_units_to_mm(x),
scaled_units_to_mm(y),
scaled_units_to_mm(x) + 0.7,
scaled_units_to_mm(y) + 2.4,
);
}
fn write_figure( fn write_figure(
output: &mut String, output: &mut String,
figure: &PositionedFigure, figure: &PositionedFigure,
+6 -2
View File
@@ -154,8 +154,12 @@ fn arxiv_title_block_uses_rules_and_small_caps() {
"italic emph{{arxiv}} title run missing" "italic emph{{arxiv}} title run missing"
); );
assert!( assert!(
first_page.contains("fill=\"#008000\">iD</text>"), first_page.contains("fill=\"#A6CE39\""),
"ORCID link badge missing" "ORCID vector badge missing"
);
assert!(
first_page.contains(">iD</text>"),
"ORCID badge label missing"
); );
} }