From cf3eafb75cc5163b368006329606beed1a0d75dd Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Fri, 7 Apr 2023 15:26:26 +0200 Subject: [PATCH] refactor(geom): update asSVG() bleed handling BREAKING CHANGE: rename `bleed` attrib to `__bleed` - for consistency, keep all control attribs prefixed as `__xxx` - add asSvg() support for `__prec` - update docs --- packages/geom/src/as-svg.ts | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/packages/geom/src/as-svg.ts b/packages/geom/src/as-svg.ts index 1e18a09496..eedf1cfcb7 100644 --- a/packages/geom/src/as-svg.ts +++ b/packages/geom/src/as-svg.ts @@ -10,7 +10,7 @@ import { __collBounds } from "./internal/bounds.js"; /** * Can be overridden via {@link setSvgDefaultAttribs}. */ -let DEFAULT_ATTRIBS: Attribs = { fill: "none", stroke: "#000" }; +export let DEFAULT_ATTRIBS: Attribs = { fill: "none", stroke: "#000" }; /** * Sets the SVG root element attribs used by default by {@link svgDoc}. @@ -30,15 +30,28 @@ export const asSvg = (...args: any[]) => args.map((x) => serialize(convertTree(x))).join(""); /** - * Creates a hiccup SVG doc element for given {@link IShape}s and attribs - * (merged with default attribs). If the attribs do not include a `viewBox`, it - * will be computed automatically. Furthermore (and only for the case a viewbox - * needs to be computed), a `bleed` attrib can be provided to include a - * bleed/margin for the viewbox. + * Creates a hiccup SVG doc element container for given {@link IShape}s and + * attribs (merged with {@link DEFAULT_ATTRIBS}). If the attribs do not include + * a `viewBox`, it will be computed automatically. Furthermore (and only for the + * case a viewbox needs to be computed), a `__bleed` attrib can be provided to + * include a bleed/margin for the viewbox (in world space units). * * @remarks * Use {@link asSvg} to serialize the resulting doc to an SVG string. * + * The actual serialization is performed via the + * [thi.ng/hiccup](https://thi.ng/hiccup) and + * [thi.ng/hiccup-svg](https://thi.ng/hiccup-svg) packages. Floating point + * precision for various point coordinates can be controlled via the `__prec` + * attribute (number of fractional digits), either for the entire doc or on a + * per-shape basis. If omitted, the currently configured precision will be used + * (default: 3). + * + * Also see + * [`convertTree()`](https://docs.thi.ng/umbrella/hiccup-svg/functions/convertTree.html) + * and + * [`setPrecision()`](https://docs.thi.ng/umbrella/hiccup-svg/functions/setPrecision.html). + * * @param attribs * @param xs */ @@ -49,7 +62,7 @@ export const svgDoc = (attribs: Attribs, ...xs: IShape[]) => { const cbounds = __collBounds(xs, bounds); if (cbounds) { const [[x, y], [w, h]] = cbounds; - const bleed = attribs.bleed || 0; + const bleed = attribs.__bleed || 0; const bleed2 = 2 * bleed; const width = ff(w + bleed2); const height = ff(h + bleed2); @@ -59,7 +72,7 @@ export const svgDoc = (attribs: Attribs, ...xs: IShape[]) => { viewBox: `${ff(x - bleed)} ${ff( y - bleed )} ${width} ${height}`, - ...withoutKeysObj(attribs, ["bleed"]), + ...withoutKeysObj(attribs, ["__bleed"]), }; } }