Skip to content

Commit

Permalink
refactor(geom): update asSVG() bleed handling
Browse files Browse the repository at this point in the history
BREAKING CHANGE: rename `bleed` attrib to `__bleed`

- for consistency, keep all control attribs prefixed as `__xxx`
- add asSvg() support for `__prec`
- update docs
  • Loading branch information
postspectacular committed Apr 7, 2023
1 parent f81d0d8 commit cf3eafb
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions packages/geom/src/as-svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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
*/
Expand All @@ -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);
Expand All @@ -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"]),
};
}
}
Expand Down

0 comments on commit cf3eafb

Please sign in to comment.