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,
y: i64,
},
Badge {
x: i64,
y: i64,
},
FramedBox {
anchor: PageAnchor,
element: PositionedFramedBox,
@@ -542,6 +546,7 @@ impl Element for PositionedPages {
PositionedPageItem::DisplayMath { .. } => "display math",
PositionedPageItem::Table { .. } => "table",
PositionedPageItem::Image { .. } => "image",
PositionedPageItem::Badge { .. } => "badge",
PositionedPageItem::FramedBox { .. } => "framed box",
PositionedPageItem::Centered { .. } => "centered block",
};
@@ -627,6 +632,28 @@ impl Element for PositionedPages {
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, .. } => {
let y = scaled_units_to_mm(*y);
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_width = content_width_mm / column_count as f64;
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 {
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() {
let mut runs = Vec::new();
let mut rest = text.as_str();
let mut has_badge = false;
if let Some(after_badge) = text.strip_prefix("iD ") {
let mut badge = Run::plain("iD ".to_string());
badge.bold = true;
badge.link = true;
runs.push(badge);
has_badge = true;
rest = after_badge;
}
if !rest.is_empty() {
@@ -1936,9 +1936,11 @@ pub(super) fn build_positioned_pages(
.next()
{
measured_lines.push(line);
block_badges.push(has_badge);
}
}
measured_blocks.push(measured_lines);
badge_rows.push(block_badges);
}
let row_count = measured_blocks.iter().map(Vec::len).max().unwrap_or(0);
for row in 0..row_count {
@@ -1982,10 +1984,22 @@ pub(super) fn build_positioned_pages(
mono_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 x = millimeters_to_scaled(
column_start + (column_width - natural_width).max(0.0) / 2.0,
);
let badge_size = 4.0_f64;
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
.last_mut()
.expect("the page list starts with one page")
@@ -1998,6 +2012,19 @@ pub(super) fn build_positioned_pages(
centered: 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);
}
+12
View File
@@ -137,6 +137,7 @@ fn page_svg(
monospace_family,
sans_family,
),
PositionedPageItem::Badge { x, y } => write_badge(&mut output, *x, *y),
PositionedPageItem::Image { element, y, .. } => write_figure(
&mut output,
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(
output: &mut String,
figure: &PositionedFigure,