Agreements the client signs on the link (task 7.6)
Built into the app rather than a separate signing service: any number of agreements (the processing of the client's data first, legal services and consultations later), each in versions whose text never changes. - Staff (Agreements in the sidebar, src/web/agreements.rs): paste the lawyers' text (empty line = paragraph, "# " heading, "- " list item), preview it as the client reads it, publish version 1; a changed text is the next version, the same text again is refused, and the database refuses any change to a published one (migrations/0007, trigger). Switch an agreement off and links stop asking for it. - Client: every active agreement is the first step of every link. The client reads it, ticks that they accept, types their name and signs with a finger or the mouse (static/signature.js; a PNG of the pad). A text changed while they read it is refused. A new version is asked for again at the next link. - The signed copy (src/agreements/pdf.rs, Sarabun so Thai prints, long Thai words cut without parting marks from their letter): the text, the name, the record, the time, the drawing, the link, the forwarded address, the browser and the text's SHA-256; kept as the person's encrypted document of kind "agreement", all in one transaction with the signature and the audit rows (actor link N). - The person's page shows each agreement's state (signed, not signed, to sign again) and every signed copy. - Phones: the section row now scrolls sideways instead of wrapping, so it keeps one height with five sections and Sign out. - Demo data: an invented agreement with a Thai paragraph. Checks: fmt, clippy; 105 unit and 56 integration tests (tests/agreements.rs: versions that never change, signing with a drawing, address and browser recorded, the PDF, signing again after a change, a stale version refused, switched off); layout check light/dark desk/phone on the agreement pages, the client's agreement page, the person page and the people list: no problems; a drawn signature in Edge at 390x844 signed and printed.
This commit is contained in:
1 parent
43ed75d81b
commit
d0e13dffd2
28 files changed
+2469
-57
No files matched your search
@@ -0,0 +1,387 @@
|
||||
//! The signed copy of an agreement as a PDF: the title and version, the
|
||||
//! text as published, and a last part that says who signed, when, through
|
||||
//! which link and from where, with the drawn signature and the SHA-256 of
|
||||
//! the text. Sarabun writes it, so Thai text prints too.
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use krilla::color::rgb;
|
||||
use krilla::geom::{PathBuilder, Point, Size, Transform};
|
||||
use krilla::image::Image;
|
||||
use krilla::metadata::Metadata;
|
||||
use krilla::num::NormalizedF32;
|
||||
use krilla::page::PageSettings;
|
||||
use krilla::paint::{Fill, LineCap, Stroke};
|
||||
use krilla::surface::Surface;
|
||||
use krilla::text::TextDirection;
|
||||
use krilla::{Data, Document};
|
||||
|
||||
use super::Block;
|
||||
use crate::papers::{Pen, width};
|
||||
|
||||
/// A4 portrait, in points.
|
||||
const W: f32 = 595.0;
|
||||
const H: f32 = 842.0;
|
||||
const MARGIN: f32 = 56.0;
|
||||
const BOTTOM: f32 = H - 60.0;
|
||||
|
||||
const NAVY: (u8, u8, u8) = (7, 19, 35);
|
||||
const INK: (u8, u8, u8) = (34, 38, 46);
|
||||
const GREY: (u8, u8, u8) = (104, 112, 124);
|
||||
const GOLD: (u8, u8, u8) = (245, 192, 0);
|
||||
|
||||
/// Who signed, and the facts the signature page records.
|
||||
pub struct Signed<'a> {
|
||||
pub title: &'a str,
|
||||
pub version: i32,
|
||||
/// "Nov 20, 2026"
|
||||
pub published: &'a str,
|
||||
pub sha256: &'a str,
|
||||
/// The name typed as the signature.
|
||||
pub name: &'a str,
|
||||
/// The record: "MUSTERMANN, Erika, born Aug 12, 1984, passport C01X00T47 (Germany)"
|
||||
pub record: &'a str,
|
||||
/// "Nov 20, 2026, 14:05 (Chiang Mai time)"
|
||||
pub at: &'a str,
|
||||
/// "client link 12 at https://visa.buildfor.life"
|
||||
pub through: &'a str,
|
||||
pub ip: &'a str,
|
||||
pub user_agent: &'a str,
|
||||
/// The drawn signature, a PNG.
|
||||
pub drawing: Option<&'a [u8]>,
|
||||
}
|
||||
|
||||
enum Op {
|
||||
Text {
|
||||
x: f32,
|
||||
y: f32,
|
||||
size: f32,
|
||||
color: (u8, u8, u8),
|
||||
text: String,
|
||||
},
|
||||
Rule {
|
||||
y: f32,
|
||||
},
|
||||
Image {
|
||||
x: f32,
|
||||
y: f32,
|
||||
w: f32,
|
||||
h: f32,
|
||||
},
|
||||
}
|
||||
|
||||
/// Pages of drawing operations, with the place to write next.
|
||||
struct Layout<'p> {
|
||||
pen: &'p Pen<'p>,
|
||||
pages: Vec<Vec<Op>>,
|
||||
y: f32,
|
||||
}
|
||||
|
||||
impl Layout<'_> {
|
||||
fn room(&mut self, needed: f32) {
|
||||
if self.y + needed > BOTTOM {
|
||||
self.pages.push(Vec::new());
|
||||
self.y = 72.0;
|
||||
}
|
||||
}
|
||||
|
||||
fn op(&mut self, op: Op) {
|
||||
self.pages.last_mut().expect("a page").push(op);
|
||||
}
|
||||
|
||||
/// Text wrapped to the width at `x`, line by line.
|
||||
fn text(&mut self, x: f32, text: &str, size: f32, lead: f32, color: (u8, u8, u8)) {
|
||||
for line in wrap(self.pen, text, size, W - MARGIN - x) {
|
||||
self.room(lead);
|
||||
self.y += lead;
|
||||
self.op(Op::Text {
|
||||
x,
|
||||
y: self.y,
|
||||
size,
|
||||
color,
|
||||
text: line,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Thai vowels and tone marks written above or below the letter before
|
||||
/// them: a line never starts with one.
|
||||
fn combining(c: char) -> bool {
|
||||
matches!(c, '\u{0E31}' | '\u{0E34}'..='\u{0E3A}' | '\u{0E47}'..='\u{0E4E}')
|
||||
}
|
||||
|
||||
/// `text` in lines that fit `room`; a word wider than a line (Thai has no
|
||||
/// spaces between words) is cut where it must be.
|
||||
fn wrap(pen: &Pen<'_>, text: &str, size: f32, room: f32) -> Vec<String> {
|
||||
let mut lines = Vec::new();
|
||||
let mut line = String::new();
|
||||
for word in text.split_whitespace() {
|
||||
let tried = if line.is_empty() {
|
||||
word.to_string()
|
||||
} else {
|
||||
format!("{line} {word}")
|
||||
};
|
||||
if width(&pen.face, &tried, size) <= room {
|
||||
line = tried;
|
||||
continue;
|
||||
}
|
||||
if !line.is_empty() {
|
||||
lines.push(std::mem::take(&mut line));
|
||||
}
|
||||
if width(&pen.face, word, size) <= room {
|
||||
line = word.to_string();
|
||||
continue;
|
||||
}
|
||||
let mut piece = String::new();
|
||||
for c in word.chars() {
|
||||
let mut next = piece.clone();
|
||||
next.push(c);
|
||||
if !piece.is_empty() && !combining(c) && width(&pen.face, &next, size) > room {
|
||||
lines.push(std::mem::take(&mut piece));
|
||||
piece.push(c);
|
||||
} else {
|
||||
piece = next;
|
||||
}
|
||||
}
|
||||
line = piece;
|
||||
}
|
||||
if !line.is_empty() {
|
||||
lines.push(line);
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
fn fill(c: (u8, u8, u8)) -> Fill {
|
||||
Fill {
|
||||
paint: rgb::Color::new(c.0, c.1, c.2).into(),
|
||||
opacity: NormalizedF32::ONE,
|
||||
rule: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The signed copy. `blocks` is the version's text.
|
||||
pub fn render(s: &Signed<'_>, blocks: &[Block]) -> Result<Vec<u8>> {
|
||||
let pen = Pen::sarabun()?;
|
||||
let image = match s.drawing {
|
||||
Some(png) => Some(
|
||||
Image::from_png(Data::from(png.to_vec()), true)
|
||||
.map_err(|e| anyhow!("the drawn signature: {e}"))?,
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
let mut l = Layout {
|
||||
pen: &pen,
|
||||
pages: vec![Vec::new()],
|
||||
y: 40.0,
|
||||
};
|
||||
l.text(MARGIN, s.title, 18.0, 30.0, NAVY);
|
||||
l.text(
|
||||
MARGIN,
|
||||
&format!("Version {}, published {}", s.version, s.published),
|
||||
9.5,
|
||||
16.0,
|
||||
GREY,
|
||||
);
|
||||
l.y += 10.0;
|
||||
l.op(Op::Rule { y: l.y });
|
||||
l.y += 6.0;
|
||||
for b in blocks {
|
||||
match b {
|
||||
Block::Heading(t) => {
|
||||
l.room(40.0);
|
||||
l.y += 8.0;
|
||||
l.text(MARGIN, t, 12.5, 19.0, NAVY);
|
||||
}
|
||||
Block::Para(t) => {
|
||||
l.y += 5.0;
|
||||
l.text(MARGIN, t, 10.5, 15.0, INK);
|
||||
}
|
||||
Block::Item(t) => {
|
||||
l.room(15.0);
|
||||
l.y += 2.0;
|
||||
let top = l.y;
|
||||
l.text(MARGIN + 14.0, t, 10.5, 15.0, INK);
|
||||
// the bullet on the item's first line
|
||||
let first = top + 15.0;
|
||||
if let Some(page) = l.pages.last_mut()
|
||||
&& first <= l.y
|
||||
{
|
||||
page.push(Op::Text {
|
||||
x: MARGIN + 2.0,
|
||||
y: first,
|
||||
size: 10.5,
|
||||
color: INK,
|
||||
text: "•".into(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the signature, kept together on one page
|
||||
let drawing_h = if image.is_some() { 90.0 } else { 0.0 };
|
||||
l.y += 18.0;
|
||||
l.room(230.0 + drawing_h);
|
||||
l.op(Op::Rule { y: l.y });
|
||||
l.y += 4.0;
|
||||
l.text(MARGIN, "Signed", 13.0, 22.0, NAVY);
|
||||
let rows: [(&str, &str); 3] = [
|
||||
("Signed by", s.name),
|
||||
("Record", s.record),
|
||||
("Signed on", s.at),
|
||||
];
|
||||
for (k, v) in rows {
|
||||
l.text(MARGIN, &format!("{k}: {v}"), 10.5, 16.0, INK);
|
||||
}
|
||||
if let Some(img) = &image {
|
||||
let (pw, ph) = img.size();
|
||||
let (pw, ph) = (pw.max(1) as f32, ph.max(1) as f32);
|
||||
let scale = (220.0 / pw).min(80.0 / ph);
|
||||
let (w, h) = (pw * scale, ph * scale);
|
||||
l.y += 8.0;
|
||||
l.op(Op::Image {
|
||||
x: MARGIN,
|
||||
y: l.y,
|
||||
w,
|
||||
h,
|
||||
});
|
||||
l.y += h + 4.0;
|
||||
l.op(Op::Rule { y: l.y });
|
||||
}
|
||||
l.y += 6.0;
|
||||
let small = [
|
||||
format!("Through {}", s.through),
|
||||
format!(
|
||||
"From the address {}",
|
||||
if s.ip.is_empty() { "not known" } else { s.ip }
|
||||
),
|
||||
format!("Browser: {}", s.user_agent),
|
||||
format!("Text: version {}, SHA-256 {}", s.version, s.sha256),
|
||||
];
|
||||
for line in &small {
|
||||
l.text(MARGIN, line, 8.5, 12.5, GREY);
|
||||
}
|
||||
|
||||
let total = l.pages.len();
|
||||
let mut doc = Document::new();
|
||||
doc.set_metadata(
|
||||
Metadata::new()
|
||||
.title(format!("{}, version {}", s.title, s.version))
|
||||
.creator("visa.buildfor.life".to_string()),
|
||||
);
|
||||
for (i, ops) in l.pages.iter().enumerate() {
|
||||
let mut page = doc.start_page_with(PageSettings::from_wh(W, H).expect("A4"));
|
||||
let mut surface = page.surface();
|
||||
for op in ops {
|
||||
draw(&mut surface, &pen, op, image.as_ref());
|
||||
}
|
||||
draw(
|
||||
&mut surface,
|
||||
&pen,
|
||||
&Op::Text {
|
||||
x: MARGIN,
|
||||
y: H - 30.0,
|
||||
size: 8.0,
|
||||
color: GREY,
|
||||
text: format!(
|
||||
"{}, version {} · signed by {} · page {} of {total}",
|
||||
s.title,
|
||||
s.version,
|
||||
s.name,
|
||||
i + 1
|
||||
),
|
||||
},
|
||||
None,
|
||||
);
|
||||
surface.finish();
|
||||
page.finish();
|
||||
}
|
||||
doc.finish()
|
||||
.map_err(|e| anyhow!("writing the agreement: {e:?}"))
|
||||
}
|
||||
|
||||
fn draw(s: &mut Surface<'_>, pen: &Pen<'_>, op: &Op, image: Option<&Image>) {
|
||||
match op {
|
||||
Op::Text {
|
||||
x,
|
||||
y,
|
||||
size,
|
||||
color,
|
||||
text,
|
||||
} => {
|
||||
s.set_fill(Some(fill(*color)));
|
||||
s.draw_text(
|
||||
Point::from_xy(*x, *y),
|
||||
pen.font.clone(),
|
||||
*size,
|
||||
text,
|
||||
false,
|
||||
TextDirection::Auto,
|
||||
);
|
||||
}
|
||||
Op::Rule { y } => {
|
||||
let mut pb = PathBuilder::new();
|
||||
pb.move_to(MARGIN, *y);
|
||||
pb.line_to(W - MARGIN, *y);
|
||||
if let Some(path) = pb.finish() {
|
||||
s.set_fill(None);
|
||||
s.set_stroke(Some(Stroke {
|
||||
paint: rgb::Color::new(GOLD.0, GOLD.1, GOLD.2).into(),
|
||||
width: 1.2,
|
||||
line_cap: LineCap::Butt,
|
||||
opacity: NormalizedF32::ONE,
|
||||
..Default::default()
|
||||
}));
|
||||
s.draw_path(&path);
|
||||
s.set_stroke(None);
|
||||
}
|
||||
}
|
||||
Op::Image { x, y, w, h } => {
|
||||
let (Some(img), Some(size)) = (image, Size::from_wh(*w, *h)) else {
|
||||
return;
|
||||
};
|
||||
s.push_transform(&Transform::from_translate(*x, *y));
|
||||
s.draw_image(img.clone(), size);
|
||||
s.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn long_thai_words_are_cut_and_marks_stay_with_their_letter() {
|
||||
let pen = Pen::sarabun().unwrap();
|
||||
let thai = "ข้อมูลส่วนบุคคลของท่านจะถูกเก็บรักษาไว้อย่างปลอดภัยตามพระราชบัญญัติคุ้มครองข้อมูลส่วนบุคคล";
|
||||
let lines = wrap(&pen, thai, 10.5, 120.0);
|
||||
assert!(lines.len() > 2, "{lines:?}");
|
||||
assert_eq!(lines.concat(), thai);
|
||||
for l in &lines {
|
||||
assert!(!l.chars().next().is_some_and(combining), "{l:?}");
|
||||
assert!(width(&pen.face, l, 10.5) <= 125.0, "{l:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_signed_copy_renders() {
|
||||
let blocks =
|
||||
super::super::blocks("# Purpose\nB4L processes your data.\n\n- name\n- passport");
|
||||
let s = Signed {
|
||||
title: "Processing of your data",
|
||||
version: 1,
|
||||
published: "Nov 20, 2026",
|
||||
sha256: &"a".repeat(64),
|
||||
name: "Erika Mustermann",
|
||||
record: "MUSTERMANN, Erika",
|
||||
at: "Nov 20, 2026, 14:05",
|
||||
through: "client link 1",
|
||||
ip: "203.0.113.7",
|
||||
user_agent: "Test",
|
||||
drawing: None,
|
||||
};
|
||||
let pdf = render(&s, &blocks).unwrap();
|
||||
assert!(pdf.starts_with(b"%PDF-"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user