: asapien/signet

commit c7b2ef8f8a6f424f4b66a041689be1233c024e54
parent c0fc0f91a8e3479c35b0e466e7bf8f4149f6dcde
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Sat, 19 Sep 2026 04:48:04 +0530

Task 8: Implement circular seal clipping

Add PDF clipping path support to pdfw:
- pdfw_clip_circle(cx, cy, r): start circular clipping with Bezier approximation
- pdfw_clip_end(): end clipping context

Apply circular clipping in render pipeline:
- Detect shape="circle" on pim_image/dam_image imprints
- Calculate center and radius from bounding box
- Apply clipping before image draw, end after

Uses 4-Bezier circle approximation (k=0.5522847498) for smooth PDF rendering.
Completes Task 8 per spec ยง08.
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
3 files changed, 47 insertions(+), 2 deletions(-)
M
src/pdfw.c
|
28
++++++++++++++++++++++++++++
M
src/pdfw.h
|
3
+++
M
src/render.c
|
18
++++++++++++++++--
diff --git a/src/pdfw.c b/src/pdfw.c
@@ -460,6 +460,34 @@ pdfw_image(pdfw *p, const char *path, double x, double y, double w, double h)
 	return 0;
 }
 
+void
+pdfw_clip_circle(pdfw *p, double cx, double cy, double r)
+{
+	if (!p->in_page)
+		return;
+	/* Draw circle using four Bezier curves and set as clipping path */
+	double k = 0.5522847498;  /* magic constant for circle approximation */
+	cprintf(p, "q\n");
+	cprintf(p, "%.3f %.3f m\n", cx + r, cy);
+	cprintf(p, "%.3f %.3f %.3f %.3f %.3f %.3f c\n",
+	    cx + r, cy + k * r, cx + k * r, cy + r, cx, cy + r);
+	cprintf(p, "%.3f %.3f %.3f %.3f %.3f %.3f c\n",
+	    cx - k * r, cy + r, cx - r, cy + k * r, cx - r, cy);
+	cprintf(p, "%.3f %.3f %.3f %.3f %.3f %.3f c\n",
+	    cx - r, cy - k * r, cx - k * r, cy - r, cx, cy - r);
+	cprintf(p, "%.3f %.3f %.3f %.3f %.3f %.3f c\n",
+	    cx + k * r, cy - r, cx + r, cy - k * r, cx + r, cy);
+	cprintf(p, "h\nW n\n");  /* close path and set clipping */
+}
+
+void
+pdfw_clip_end(pdfw *p)
+{
+	if (!p->in_page)
+		return;
+	cprintf(p, "Q\n");
+}
+
 /* --- trailer --------------------------------------------------------------- */
 
 static void
diff --git a/src/pdfw.h b/src/pdfw.h
@@ -23,6 +23,9 @@ void pdfw_text(pdfw *, int font, double size, double x, double baseline,
 int pdfw_image_size(pdfw *, const char *path, int *w, int *h);
 int pdfw_image(pdfw *, const char *path, double x, double y, double w,
     double h);
+/* Clipping: pdfw_clip_circle begins a circular clip path, pdfw_clip_end ends it */
+void pdfw_clip_circle(pdfw *, double cx, double cy, double r);
+void pdfw_clip_end(pdfw *);
 /* returns 0 only if every write succeeded */
 int pdfw_close(pdfw *);
 
diff --git a/src/render.c b/src/render.c
@@ -296,13 +296,27 @@ draw_imprint(pdfw *pw, const struct render_ctx *cx, yyjson_val *layout,
 			return fail(err, errsz, "bad date format: %s", fmt);
 		snprintf(text, sizeof(text), "%s%s", prefix, d);
 		draw_text(pw, cx, imp, text, x, top, w, h, pg->h);
-	} else if (strcmp(type, "dam_image") == 0) {
+	} else if (strcmp(type, "pim_image") == 0 || strcmp(type, "dam_image") == 0) {
+		const char *shape = yyjson_get_str(yyjson_obj_get(imp, "shape"));
 		if (!is_safe_relpath(ref))
 			return fail(err, errsz, "bad DAM path: %s", ref ? ref : "?");
 		snprintf(path, sizeof(path), "%s/%s",
 		    conf_get(cx->cf, "dam_dir", "/var/www/dam"), ref);
+
+		/* Apply circular clipping if shape is "circle" */
+		if (shape != NULL && strcmp(shape, "circle") == 0) {
+			double cx = x + w / 2;
+			double cy = pg->h - top - h / 2;
+			double r = (w < h ? w : h) / 2;
+			pdfw_clip_circle(pw, cx, cy, r);
+		}
+
 		if (draw_image(pw, path, x, top, w, h, pg->h) == -1)
-			return fail(err, errsz, "cannot embed DAM image: %s", ref);
+			return fail(err, errsz, "cannot embed image: %s", ref);
+
+		/* End clipping if it was applied */
+		if (shape != NULL && strcmp(shape, "circle") == 0)
+			pdfw_clip_end(pw);
 	} else if (strcmp(type, "signature") == 0) {
 		if (!cx->with_signatures)
 			return 0;