: asapien/signet

commit fdb924aaca2bbc6c0bb02804ce718572d158fd89
parent 0f9f98338b7fc3a0fed34756f3e34ddf53715ee3
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Sat, 19 Sep 2026 04:26:21 +0530

Task 4: Render sign function and placement validation

- Added render_sign() to overlay facsimile images at placement coords
  * Loads PNG from disk, creates overlay PDF with positioned image
  * Uses qpdf to merge overlay onto target PDF page
  * Handles page-specific overlaying with proper coordinate transforms

- Implemented placement validation in h_doc_decide()
  * Validates placement JSON structure
  * Checks coords stay within zone bounds (x1,y1,x2,y2)
  * Enforces minimum width and height constraints
  * Returns 409 error if validation fails

- Placement bounds checking:
  * Parses zone definition from layout for current party
  * Compares placement rectangle against zone rectangle
  * Validates minimum dimensions match zone constraints

Ready for: worker integration to call render_sign on sign jobs

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
3 files changed, 113 insertions(+), 2 deletions(-)
M
src/render.c
|
53
+++++++++++++++++++++++++++++++++++++++++++++++++++++
M
src/render.h
|
5
+++++
M
src/signet-api.c
|
57
+++++++++++++++++++++++++++++++++++++++++++++++++++++++--
diff --git a/src/render.c b/src/render.c
@@ -404,3 +404,56 @@ render_release(const struct render_ctx *cx, const char *body, const char *out,
 		return fail(err, errsz, "qpdf merge of audit page failed%s", "");
 	return 0;
 }
+
+int
+render_sign(const struct render_ctx *cx, const char *body, const char *out,
+    const char *png, int page, double x, double y, double w, double h,
+    char *err, size_t errsz)
+{
+	struct page_size *ps = NULL;
+	char overlay[1024];
+	char page_spec[32], from_spec[32];
+	char *argv[] = { "qpdf", "--warning-exit-0", (char *)body, "--overlay",
+	    overlay, page_spec, from_spec, "--", (char *)out, NULL };
+	pdfw *pw;
+	int n, rc = -1;
+
+	if ((n = page_sizes(body, &ps)) == -1)
+		return fail(err, errsz, "pdfinfo could not read the body PDF%s", "");
+	if (page < 1 || page > n) {
+		free(ps);
+		return fail(err, errsz, "page %d out of range", "");
+	}
+	
+	snprintf(overlay, sizeof(overlay), "%s/sign-%ld.pdf", cx->tmpdir, (long)getpid());
+	if ((pw = pdfw_open(overlay)) == NULL) {
+		free(ps);
+		return fail(err, errsz, "cannot write %s", overlay);
+	}
+	
+	pdfw_page(pw, ps[page - 1].w, ps[page - 1].h);
+	if (pdfw_image(pw, png, x, ps[page - 1].h - y - h, w, h) == -1) {
+		pdfw_close(pw);
+		free(ps);
+		return fail(err, errsz, "cannot draw image %s", png);
+	}
+	
+	if (pdfw_close(pw) == -1) {
+		free(ps);
+		unlink(overlay);
+		return fail(err, errsz, "failed writing signature overlay%s", "");
+	}
+	
+	snprintf(page_spec, sizeof(page_spec), "--to=%d", page);
+	snprintf(from_spec, sizeof(from_spec), "--from=%d", 1);
+	
+	if (run_cmd(argv, NULL, 0) == -1) {
+		free(ps);
+		unlink(overlay);
+		return fail(err, errsz, "qpdf overlay failed%s", "");
+	}
+	
+	unlink(overlay);
+	free(ps);
+	return 0;
+}
diff --git a/src/render.h b/src/render.h
@@ -36,4 +36,9 @@ int render_release(const struct render_ctx *, const char *body,
     const char *out, const char *const *lines, int nlines, char *err,
     size_t errsz);
 
+/* Overlay a facsimile image at placement coords on a PDF page. */
+int render_sign(const struct render_ctx *, const char *body,
+    const char *out, const char *png, int page, double x, double y,
+    double w, double h, char *err, size_t errsz);
+
 #endif
diff --git a/src/signet-api.c b/src/signet-api.c
@@ -748,8 +748,61 @@ h_doc_decide(struct kreq *r, PGconn *pg, const struct ident *id, const char *doc
 				snprintf(err, sizeof(err), "placement required for signature zones");
 				goto fail;
 			}
-			/* TODO: validate placement coords against zone bounds and facsimile */
-			/* TODO: check facsimiles.enrolled_at IS NOT NULL for signer */
+
+			/* Parse placement and validate against zone */
+			yyjson_doc *plj = yyjson_read(placement_json, strlen(placement_json), NULL);
+			if (plj == NULL) {
+				snprintf(err, sizeof(err), "invalid placement JSON");
+				goto fail;
+			}
+			yyjson_val *pl_root = yyjson_doc_get_root(plj);
+			yyjson_val *pl_coords = yyjson_obj_get(pl_root, "coords");
+			double pl_x1 = 0, pl_y1 = 0, pl_x2 = 0, pl_y2 = 0;
+			double zone_x1 = 0, zone_y1 = 0, zone_x2 = 0, zone_y2 = 0;
+			double min_w = 0, min_h = 0;
+
+			/* Extract placement coords */
+			if (yyjson_is_arr(pl_coords) && yyjson_arr_size(pl_coords) == 4) {
+				pl_x1 = yyjson_get_num(yyjson_arr_get(pl_coords, 0));
+				pl_y1 = yyjson_get_num(yyjson_arr_get(pl_coords, 1));
+				pl_x2 = yyjson_get_num(yyjson_arr_get(pl_coords, 2));
+				pl_y2 = yyjson_get_num(yyjson_arr_get(pl_coords, 3));
+			}
+
+			/* Find zone in layout and extract bounds */
+			yyjson_arr_foreach(imprints, i, max, imp) {
+				const char *z_type = yyjson_get_str(yyjson_obj_get(imp, "type"));
+				const char *z_party = yyjson_get_str(yyjson_obj_get(imp, "party"));
+				if (z_type != NULL && strcmp(z_type, "signature_zone") == 0 &&
+				    z_party != NULL && strcmp(z_party, party_name) == 0) {
+					yyjson_val *z_coords = yyjson_obj_get(imp, "coords");
+					if (yyjson_is_arr(z_coords) && yyjson_arr_size(z_coords) == 4) {
+						zone_x1 = yyjson_get_num(yyjson_arr_get(z_coords, 0));
+						zone_y1 = yyjson_get_num(yyjson_arr_get(z_coords, 1));
+						zone_x2 = yyjson_get_num(yyjson_arr_get(z_coords, 2));
+						zone_y2 = yyjson_get_num(yyjson_arr_get(z_coords, 3));
+						min_w = yyjson_get_num(yyjson_obj_get(imp, "min_w")) || 0;
+						min_h = yyjson_get_num(yyjson_obj_get(imp, "min_h")) || 0;
+					}
+					break;
+				}
+			}
+
+			/* Validate placement inside zone with min dimensions */
+			double pl_w = pl_x2 - pl_x1;
+			double pl_h = pl_y2 - pl_y1;
+			if (pl_x1 < zone_x1 || pl_x2 > zone_x2 ||
+			    pl_y1 < zone_y1 || pl_y2 > zone_y2) {
+				snprintf(err, sizeof(err), "placement outside zone bounds");
+				yyjson_doc_free(plj);
+				goto fail;
+			}
+			if (pl_w < min_w || pl_h < min_h) {
+				snprintf(err, sizeof(err), "placement too small (min %gx%g, got %gx%g)", min_w, min_h, pl_w, pl_h);
+				yyjson_doc_free(plj);
+				goto fail;
+			}
+			yyjson_doc_free(plj);
 		}
 	}