Draw rounded ORCID badges in PDFs

This commit is contained in:
gloamxun committed 2026-10-19 21:25:31 +08:00
1 parent 5d6d51536f
commit e4c5b0cb32
5 files changed
+56 -11

No files matched your search

+43
View File
@@ -365,6 +365,49 @@ impl<'a> Area<'a> {
self.draw_rectangle(position, size, style, false, true);
}
/// Draws a filled rectangle with rounded corners (polygonal arc approximation).
pub fn fill_rounded_rect(&self, position: Position, size: Size, radius: f64, style: Style) {
let px = position.x.0;
let py = position.y.0;
let width = size.width.0;
let height = size.height.0;
let r = radius.min(width / 2.0).min(height / 2.0);
let corners = [
(px + width - r, py + r, -90.0),
(px + width - r, py + height - r, 0.0),
(px + r, py + height - r, 90.0),
(px + r, py + r, 180.0),
];
let mut points = Vec::new();
for (cx, cy, start) in corners {
for step in 0..=6 {
let angle = (start + step as f64 * 15.0).to_radians();
points.push(Position::new(
Mm(cx + r * angle.cos()),
Mm(cy + r * angle.sin()),
));
}
}
let line_points: Vec<_> = points
.into_iter()
.map(|pos| (self.transform_position(pos).into(), false))
.collect();
let line = printpdf::Line {
points: line_points,
is_closed: true,
has_fill: true,
has_stroke: false,
is_clipping_path: false,
};
if let Some(color) = style.color() {
self.layer().set_fill_color(color.into());
}
self.layer().add_shape(line);
if style.color().is_some() {
self.layer().set_fill_color(Color::Rgb(0, 0, 0).into());
}
}
/// Draws a filled axis-aligned rectangle.
///
/// The position is relative to the upper left corner of the area.