Building a Pure-TypeScript SVG-to-EPS Vector PostScript Converter (CMYK, Elliptical Arcs, Zero Server Uploads)

2026-12-02来源 dev.toEnglish

Every time a small business sends a logo to a U.S. screen printer, embroidery shop, sign maker, or promotional merchandise vendor, the artwork spec sheet says the same thing:

"Please upload your logo as a vector .EPS or .PDF file (fonts converted to outlines)."

If you try to build a browser-native SVG-to-EPS converter without shipping a 40 MB Ghostscript or Inkscape WASM binary—and without uploading customer logos to a backend server—you quickly discover why most web converters are slow cloud queues behind paywalls.

Here is how we built a pure-TypeScript EPSF-3.0 (%!PS-Adobe-3.0 EPSF-3.0) vector PostScript compiler in ~1,100 lines of zero-dependency code for LogoNib's free browser tool (logonib.com/tools/svg-to-eps) and our one-time vector brand packs.

1. The Coordinate Flip: SVG Top-Down vs. PostScript Bottom-Up

SVG places (0, 0) at the top-left corner with +Y pointing downward. Encapsulated PostScript (EPS) places (0, 0) at the bottom-left corner with +Y pointing upward.

Instead of manually flipping every y coordinate and negating every arc sweep or rotation angle inside each shape converter, you can apply a single global affine transform right after %%EndSetup inside balanced gsave / grestore blocks:

gsave [1 0 0 -1 0 400] concat % All subsequent SVG coordinates map 1:1 into PostScript space grestore 

If the SVG has a non-zero viewBox="minX minY width height", you compose the scale and translation into that root matrix so every shape lands cleanly inside %%BoundingBox: 0 0 <widthPt> <heightPt> and %%HiResBoundingBox.

2. Converting SVG Path Commands (Especially Quadratic Curves and Elliptical Arcs)

PostScript natively speaks moveto, lineto, curveto (cubic Bézier), and closepath. It does not have SVG's quadratic Bézier (Q/T) or elliptical arc (A/a) path commands.

Degree-Elevating Quadratic Béziers (Q/T) to Cubic (curveto)

Every quadratic Bézier from P0 through control point P1 to P2 converts exactly to a cubic Bézier (C1, C2, P2) with zero approximation loss:

const c1x = x0 + (2 / 3) * (qx - x0); const c1y = y0 + (2 / 3) * (qy - y0); const c2x = x2 + (2 / 3) * (qx - x2); const c2y = y2 + (2 / 3) * (qy - y2); ops.push(`${fmt(c1x)} ${fmt(c1y)} ${fmt(c2x)} ${fmt(c2y)} ${fmt(x2)} ${fmt(y2)} curveto`); 

Converting Elliptical Arcs (A/a) to Cubic Segments

For A rx ry xAxisRotation largeArcFlag sweepFlag x y, we implement the W3C SVG F.6.5/F.6.6 endpoint-to-center parameterization:

  1. Transform the midpoint into rotated ellipse space and scale up rx, ry if the radii are too small to reach the endpoint.
  2. Compute the ellipse center (cx, cy), start angle θ1, and sweep Δθ.
  3. Subdivide |Δθ| into segments of at most π/2 radians (90°) and emit one cubic Bézier per segment using α = (4/3) · tan(Δθ_seg / 4).

For <circle> and <ellipse>, four 90° cubic segments using κ ≈ 0.5522847498 give sub-pixel accuracy cleanly across Illustrator, CorelDRAW, Inkscape, and RIP software.

3. Print CMYK (setcmykcolor) & Alpha Compositing

PostScript Level 2/3 EPS does not support native alpha transparency (fill-opacity or rgba()). If you silently ignore opacity, a 20% gray accent turns solid black in print.

Our emitColorOp() helper composites any semi-transparent color over white (blendWithWhite) before emitting either:

  • Print CMYK mode: c m y k setcmykcolor (using deterministic RGB-to-CMYK conversion)
  • Screen RGB mode: r g b setrgbcolor

And for fill-rule="evenodd", we switch PostScript's paint operator from fill to eofill—preserving hollow ring cutouts and compound emblem geometry accurately.

4. Security Hardening: Treating Untrusted SVGs Safely

SVG is XML that can carry <script>, inline on* handlers, <foreignObject>, SMIL animation payloads, or maliciously nested transforms—and EPS headers themselves can be broken by newline injection inside %%Title: comments.

Before a single PostScript line is emitted:

  1. We pre-sanitize the SVG through our sanitizer and pre-flight auditor (optimizeSvg + auditSvgPreflight), stripping dangerous tags/attributes and surfacing warnings if live <text> nodes aren't converted to outlines.
  2. We cap nesting depth (64), shape count (10,000), and commands per path (25,000) to prevent browser freezes on pathological files.
  3. We strip control characters and non-ASCII bytes from %%Title comments (sanitizePsComment) so header lines cannot inject arbitrary PostScript operators.
  4. On the web UI, we never render user-uploaded SVGs with {@html} or same-origin image/svg+xml Blob URLs—previews are rasterized safely to PNG bytes first.

Try It Free (Zero Uploads, Zero Sign-Up)

You can test the converter on any SVG logo right in your browser (with both Print CMYK and Screen RGB modes plus a raw PostScript source inspector):

And if you are creating a new brand logo from scratch without recurring subscriptions, LogoNib (https://www.logonib.com) lets you generate and edit custom vector concepts free in your browser—then download your full print-ready package (outlined SVGs, CMYK & mono EPS files, 300-DPI Print Vector PDF, 4K PNGs, and commercial copyright certificate) for a single one-time payment starting at $29 ($49 for the Complete Brand Kit, $89 for Studio Multi-Concept), backed by a 30-day refund guarantee.

原文:https://www.a8q9.cn/en/article/building-a-pure-typescript-svg-to-eps-vector-postscript-converter-cmyk-elliptica-004fe2.html

相关文章