@contrail/documents 1.20.0-alpha.23f6f22 → 1.20.0-alpha.25ac790

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -11,6 +11,7 @@ Versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
11
11
 
12
12
  - `MaskDefinition` and `MaskShapeKey` on `DocumentElement` — `maskDefinition` records that an image is masked into one of nine preset shapes, with an optional clockwise `rotation`. Its presence changes how `cropDefinition`'s percents are read: element-space (the mask window within the element) instead of the source-image crop they otherwise mean. Anything deriving image geometry must check `hasShapeMask` before treating a crop as a source crop — reading one as the other samples the wrong region of the bitmap.
13
13
  - `@contrail/documents/util/mask` — the shared mask model, exported from the package root: the nine shapes as renderer-agnostic cubic path segments (`getMaskShapeSegments`), `rotatedRectAABB` for the bounds a tilted shape actually occupies, `rotatePoint`, the mask-rect/percent conversions, and `MASK_SHAPE_PRESET_GEOMETRY` mapping each shape to its OOXML preset so exporters and the canvas cannot drift apart. Segments are cubic-only because cubics survive affine maps exactly, letting each consumer render them however it draws — `Path2D` in a browser, preset geometry in pptx.
14
+ - `getMaskBakeGeometry`, `getMaskBakeLayout` and `maskShapeSegmentsToSvgPath` — the bake inputs a consumer needs to flatten a mask into a bitmap, and an SVG serializer for renderers that go through SVG rather than a canvas path. The layout scales to the source bitmap's own resolution rather than the element's on-board size, so a shrunk image does not bake at reduced detail, and re-bases a rotated mask onto its own bounding box. Only the rasterizer stays per-consumer: `Path2D` in a browser, an SVG composite in a service.
14
15
 
15
16
  ## [1.18.0] - 2026-09-10
16
17
 
@@ -2,3 +2,4 @@ export * from './mask-shape-geometry';
2
2
  export * from './rotation-math';
3
3
  export * from './shape-mask-element';
4
4
  export * from './mask-preset-geometry';
5
+ export * from './mask-bake-layout';
@@ -18,3 +18,4 @@ __exportStar(require("./mask-shape-geometry"), exports);
18
18
  __exportStar(require("./rotation-math"), exports);
19
19
  __exportStar(require("./shape-mask-element"), exports);
20
20
  __exportStar(require("./mask-preset-geometry"), exports);
21
+ __exportStar(require("./mask-bake-layout"), exports);
@@ -0,0 +1,17 @@
1
+ import { DocumentElement, SizeDefinition } from '../../types';
2
+ import { MaskRect, MaskShapeKey } from './mask-shape-geometry';
3
+ export declare const MASK_BAKE_MAX_SIDE = 4096;
4
+ export interface MaskBakeGeometry {
5
+ shape: MaskShapeKey;
6
+ rotation: number;
7
+ maskRect: MaskRect;
8
+ imageRect: MaskRect;
9
+ }
10
+ export interface MaskBakeLayout {
11
+ size: SizeDefinition;
12
+ scale: number;
13
+ maskRect: MaskRect;
14
+ imageRect: MaskRect;
15
+ }
16
+ export declare function getMaskBakeGeometry(element: DocumentElement): MaskBakeGeometry | null;
17
+ export declare function getMaskBakeLayout(geometry: MaskBakeGeometry, sourceImageSize: SizeDefinition): MaskBakeLayout | null;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MASK_BAKE_MAX_SIDE = void 0;
4
+ exports.getMaskBakeGeometry = getMaskBakeGeometry;
5
+ exports.getMaskBakeLayout = getMaskBakeLayout;
6
+ const mask_shape_geometry_1 = require("./mask-shape-geometry");
7
+ const shape_mask_element_1 = require("./shape-mask-element");
8
+ exports.MASK_BAKE_MAX_SIDE = 4096;
9
+ function getMaskBakeGeometry(element) {
10
+ var _a, _b;
11
+ const shape = (0, shape_mask_element_1.getShapeMaskKey)(element);
12
+ if (!shape || !(0, shape_mask_element_1.hasShapeMask)(element) || !((_a = element.size) === null || _a === void 0 ? void 0 : _a.width) || !((_b = element.size) === null || _b === void 0 ? void 0 : _b.height)) {
13
+ return null;
14
+ }
15
+ const imageRect = element.imageSize && element.imagePosition
16
+ ? {
17
+ x: element.imagePosition.x,
18
+ y: element.imagePosition.y,
19
+ width: element.imageSize.width,
20
+ height: element.imageSize.height,
21
+ }
22
+ : { x: 0, y: 0, width: element.size.width, height: element.size.height };
23
+ return {
24
+ shape,
25
+ rotation: (0, shape_mask_element_1.getShapeMaskRotation)(element),
26
+ maskRect: (0, shape_mask_element_1.cropDefinitionToMaskRect)(element.cropDefinition, element.size),
27
+ imageRect,
28
+ };
29
+ }
30
+ function scaleRect(rect, scale, originX, originY) {
31
+ return {
32
+ x: (rect.x - originX) * scale,
33
+ y: (rect.y - originY) * scale,
34
+ width: rect.width * scale,
35
+ height: rect.height * scale,
36
+ };
37
+ }
38
+ function getMaskBakeLayout(geometry, sourceImageSize) {
39
+ const { maskRect, imageRect, rotation } = geometry;
40
+ if (!imageRect.width || !imageRect.height || !(sourceImageSize === null || sourceImageSize === void 0 ? void 0 : sourceImageSize.width) || !maskRect.width || !maskRect.height) {
41
+ return null;
42
+ }
43
+ const extent = rotation ? (0, mask_shape_geometry_1.rotatedRectAABB)(maskRect, rotation) : Object.assign({}, maskRect);
44
+ const nativeScale = sourceImageSize.width / imageRect.width;
45
+ const longestSide = Math.max(extent.width, extent.height) * nativeScale;
46
+ const scale = longestSide > exports.MASK_BAKE_MAX_SIDE ? (exports.MASK_BAKE_MAX_SIDE / longestSide) * nativeScale : nativeScale;
47
+ return {
48
+ size: {
49
+ width: Math.ceil(extent.width * scale),
50
+ height: Math.ceil(extent.height * scale),
51
+ },
52
+ scale,
53
+ maskRect: scaleRect(maskRect, scale, extent.x, extent.y),
54
+ imageRect: scaleRect(imageRect, scale, extent.x, extent.y),
55
+ };
56
+ }
@@ -24,3 +24,4 @@ export type PathSegment = {
24
24
  };
25
25
  export declare function getMaskShapeSegments(shape: MaskShapeKey, rect: MaskRect, rotationDeg?: number): PathSegment[];
26
26
  export declare function rotatedRectAABB(rect: MaskRect, rotationDeg: number): MaskRect;
27
+ export declare function maskShapeSegmentsToSvgPath(segments: PathSegment[]): string;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MASK_SHAPE_KEYS = void 0;
4
4
  exports.getMaskShapeSegments = getMaskShapeSegments;
5
5
  exports.rotatedRectAABB = rotatedRectAABB;
6
+ exports.maskShapeSegmentsToSvgPath = maskShapeSegmentsToSvgPath;
6
7
  const rotation_math_1 = require("./rotation-math");
7
8
  exports.MASK_SHAPE_KEYS = [
8
9
  'circle',
@@ -143,12 +144,28 @@ function rotatedRectAABB(rect, rotationDeg) {
143
144
  { x: rect.x + rect.width, y: rect.y },
144
145
  { x: rect.x + rect.width, y: rect.y + rect.height },
145
146
  { x: rect.x, y: rect.y + rect.height },
146
- ].map(p => (0, rotation_math_1.rotatePoint)(p, rotationDeg, { x: cx, y: cy }));
147
- const xs = corners.map(p => p.x);
148
- const ys = corners.map(p => p.y);
147
+ ].map((p) => (0, rotation_math_1.rotatePoint)(p, rotationDeg, { x: cx, y: cy }));
148
+ const xs = corners.map((p) => p.x);
149
+ const ys = corners.map((p) => p.y);
149
150
  const minX = Math.min(...xs);
150
151
  const maxX = Math.max(...xs);
151
152
  const minY = Math.min(...ys);
152
153
  const maxY = Math.max(...ys);
153
154
  return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
154
155
  }
156
+ function maskShapeSegmentsToSvgPath(segments) {
157
+ return segments
158
+ .map((segment) => {
159
+ if (segment.op === 'move') {
160
+ return `M ${segment.x} ${segment.y}`;
161
+ }
162
+ if (segment.op === 'line') {
163
+ return `L ${segment.x} ${segment.y}`;
164
+ }
165
+ if (segment.op === 'cubic') {
166
+ return `C ${segment.c1x} ${segment.c1y} ${segment.c2x} ${segment.c2y} ${segment.x} ${segment.y}`;
167
+ }
168
+ return 'Z';
169
+ })
170
+ .join(' ');
171
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/documents",
3
- "version": "1.20.0-alpha.23f6f22",
3
+ "version": "1.20.0-alpha.25ac790",
4
4
  "description": "Documents library for contrail platform",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",