DEV Community

Cover image for Four rendering bugs we hit building a browser rubber stamp maker
Xuxuan Zhou
Xuxuan Zhou

Posted on

Four rendering bugs we hit building a browser rubber stamp maker

Disclosure: I build and run StampZen, the browser-based stamp and seal maker discussed below.

When we started building StampZen on Next.js 16 and @resvg/resvg-js, the rendering spec looked small: draw a circle or rectangle, wrap text around an arc, drop an icon in the middle, and output PNG, SVG, and PDF. Four concrete bugs forced us to rethink how font loading, SVG arcs, client previews, and Word exports actually behave in production.

1. @resvg/resvg-js silently ignores bold and italic without static font files

We load custom fonts with loadSystemFonts: false so server exports look identical on macOS dev machines and Vercel serverless functions. When we added Bold and Italic toggles to the editor, I expected font-weight="700" and font-style="italic" to work on our bundled variable TTFs (Oswald and Archivo) or synthesize obliquing on static Regular files (PT Serif and Courier Prime).

They didn't. Running a pixel count on a 100x60 probe render of "AB" in PT Serif returned 394 ink pixels for regular, 394 for font-weight="700", and 394 for font-style="italic". Resvg did not throw or warn. It just drew Regular again.

Two changes fixed it:

  1. We bundled static Bold, Italic, and BoldItalic TTF cuts for the fonts that support them (and added Cinzel, Roboto Slab, and Playfair Display, bringing the set to eight fonts). After adding the static files, the same "AB" probe jumped from 394 to 532 ink pixels for PT Serif Bold and shifted to 380 for Italic.
  2. Because Oswald, Cinzel, Roboto Slab, and Noto Sans CJK SC have no true italic cut in our bundle, we store supportsBold and supportsItalic in a shared client-safe metadata table and disable the I button when a font lacks an italic TTF. Otherwise the browser fakes oblique glyphs in the preview while the paid export prints upright letters.

We also keep a module-load assertion (assertFontsLoadable()) that renders "Ag" with each registered font and throws at startup if zero dark pixels appear. That has already caught one missing file path before deployment.

2. Bottom-ring SVG <textPath> renders upside down unless you reverse the arc

Circular seals almost always have one line arched over the top (YOUR COMPANY NAME) and a second line along the bottom (• EST. 2026 •). We draw each ring as a 180-degree arc centered on its apex (-90° for top, +90° for bottom) with startOffset="50%" and text-anchor="middle".

If you generate both arcs the same way (from apex - 90° to apex + 90° with sweep=1), top text reads cleanly while bottom text renders upside down. SVG keeps a glyph's "up" vector 90 degrees to the left of the path's travel direction. Walking a bottom arc clockwise flips the tangent inward.

Reversing only the bottom arc (apex + 90° to apex - 90° with sweep=0) fixes both problems at once. The glyphs stand upright facing the viewer, and the string still reads left to right without reversing the characters.

function ringPathD(cx: number, cy: number, rx: number, ry: number, apexAngle: number, position: RingPosition): string {
  const a0 = apexAngle - 90;
  const a1 = apexAngle + 90;
  const [start, end, sweep] = position === "bottom" ? [a1, a0, 0] : [a0, a1, 1];
  const p0 = polarEllipse(cx, cy, rx, ry, start);
  const p1 = polarEllipse(cx, cy, rx, ry, end);
  return `M ${fmt(p0.x)} ${fmt(p0.y)} A ${fmt(rx)} ${fmt(ry)} 0 0 ${sweep} ${fmt(p1.x)} ${fmt(p1.y)}`;
}
Enter fullscreen mode Exit fullscreen mode

Switching to rx and ry also let the same function handle oval stamps with no separate layout path.

3. Moving preview to inline SVG killed our paywall

Our first version rendered every preview on the server via POST /api/preview after a 300ms debounce. It protected the paid vector export, since the browser only ever received a 480px watermarked PNG. It also felt sluggish: dragging a size slider took 500 to 800ms per update.

So I extracted the SVG generator and rendered the stamp as an inline <svg> in the editor. Editing became instant, and a code review immediately caught the obvious flaw: the watermark was just <g id="main-stampzen-watermark">. Anyone could open DevTools, delete one node, and copy a clean vector SVG for free.

We threw away the inline <svg> approach and built a separate 480px Canvas 2D preview renderer instead. It draws borders, curved text, icons, logos, rotation, and a deterministic mulberry32 noise mask onto an offscreen canvas, then paints the diagonal watermark directly into the raster pixels. The SVG renderer (svg-core.ts) stays strictly server-side and never enters the client bundle.

One side effect we caught during the same review: adding five extra next/font/google declarations to the root layout bumped font preloads on static pages like /terms from 6 to 16 (~1 MB of unused fonts). Adding preload: false to the five editor-only fonts and calling document.fonts.load() lazily inside the canvas component brought /terms right back down to 6 preloads.

4. Triangular stamps broke our center-text box math, and a 95-line ZIP writer solved .docx

Once we added triangular stamps alongside round, oval, square, and rectangular layouts for the online seal maker, visual testing exposed a bad overlap. Our center-text sizing assumed a generous fraction of the outer bounding box (baseHalf * 0.56 * 1.5). Inside an equilateral triangle with a star-ring border, the actual inscribed circle is much smaller than the outer box. Center text like APPROVED / 2026 ran straight across the circular ring text and out through the triangle's inner edges.

Bounding the center box's half-diagonal by the smaller of innermostBorderInnerEdge and the innermost ring glyph clearance fixed both ringed triangles and ringless text-only triangles in both the server renderer and the canvas preview.

The other surprise came from talking to small-office buyers: many don't want an SVG at all. They want to paste a stamp into a Microsoft Word contract at the right physical size without manually resizing an image. Instead of pulling in a heavy document library, we wrote a 95-line zero-dependency PKZIP builder (STORE mode plus IEEE 802.3 CRC-32) and packed the five minimal OOXML parts for a .docx file. DrawingML uses 36,000 EMUs per millimeter, so a 38 mm seal gets cx="1368000" cy="1368000" and opens at exact scale in Word, Google Docs, and Apple QuickLook.

If you work with scanned paperwork rather than building from scratch, we also shipped a client-side utility to extract a stamp from a scanned image onto a transparent PNG using pure Canvas pixel filtering.

Happy to share the exact mulberry32 aging mask or OOXML template details in the comments if anyone is building browser graphics tools on Next.js.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

The 394-to-532 ink-pixel probe is the kind of test that should be in every font pipeline — resvg failing silently is the worst kind of bug, because the output looks fine until someone compares it side by side with the real Bold cut. The paywall lesson is the one I would steal: an inline SVG preview hands the vector away by design, and painting the watermark into raster pixels with a seeded noise mask is the honest fix, not an obfuscation layer. And the 36,000 EMUs per millimeter detail for the .docx export is exactly the sort of unit conversion that silently breaks at 38 mm scale if you guess. Was the 95-line ZIP writer more reliable than pulling in a library, or was the real win that you control the EMU math yourself?