Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Draw the checkbox border in its own function

Brandon Thomaseace4735dac3
diff --git a/crates/pdf/src/forms.rs b/crates/pdf/src/forms.rs
index 90234859d..e36478971 100644
--- a/crates/pdf/src/forms.rs
+++ b/crates/pdf/src/forms.rs
@@ -79,6 +79,16 @@ fn pdf_string(s: &str) -> String {
o
}
+/// A checkbox appearance's border: the item's stroke weight, inset by half of it so it stays
+/// inside the box; nothing for an unstroked item.
+fn checkbox_border(bw: f64, w: f64, h: f64) -> String {
+ if bw > 0.0 {
+ format!("0.45 G {bw:.2} w {i:.2} {i:.2} {:.2} {:.2} re S", (w - bw).max(0.0), (h - bw).max(0.0), i = bw / 2.0)
+ } else {
+ String::new()
+ }
+}
+
/// Add `fields` and `media` to `pdf` (page indices in output order). `None` when the file's
/// structure isn't the simple kind this understands.
pub(crate) fn add_fields(pdf: &[u8], fields: &[Placed], media: &[PlacedMedia]) -> Option<Vec<u8>> {
@@ -178,11 +188,7 @@ pub(crate) fn add_fields(pdf: &[u8], fields: &[Placed], media: &[PlacedMedia]) -
}
FieldKind::CheckBox => {
// Appearances: an empty box, and a box with a check mark.
- let border = if *bw > 0.0 {
- format!("0.45 G {bw:.2} w {i:.2} {i:.2} {:.2} {:.2} re S", (w - bw).max(0.0), (h - bw).max(0.0), i = bw / 2.0)
- } else {
- String::new()
- };
+ let border = checkbox_border(*bw, w, h);
let check = format!(
"{border} 0 g {:.2} w 1 J 1 j {:.2} {:.2} m {:.2} {:.2} l {:.2} {:.2} l S",
(w.min(h) * 0.12).max(0.8),