@contrail/documents 1.17.0-alpha.7002786 → 1.17.0-alpha.f7593e7

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
@@ -9,8 +9,8 @@ Versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
9
9
 
10
10
  ### Added
11
11
 
12
- - `DocumentChange.skipUndo?: boolean` — optional marker a producer sets on a forward
13
- change definition to keep the action out of undo history.
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
+ - `@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
14
 
15
15
  ## [1.16.0] - 2026-08-28
16
16
 
package/lib/index.d.ts CHANGED
@@ -12,4 +12,5 @@ export * from './document-element-constants';
12
12
  export * from './util/curved-line/point';
13
13
  export * from './util/curved-line/curved-line';
14
14
  export * from './util/callout/callout';
15
+ export * from './util/mask';
15
16
  export * from './document-frame.service';
package/lib/index.js CHANGED
@@ -28,4 +28,5 @@ __exportStar(require("./document-element-constants"), exports);
28
28
  __exportStar(require("./util/curved-line/point"), exports);
29
29
  __exportStar(require("./util/curved-line/curved-line"), exports);
30
30
  __exportStar(require("./util/callout/callout"), exports);
31
+ __exportStar(require("./util/mask"), exports);
31
32
  __exportStar(require("./document-frame.service"), exports);
@@ -74,6 +74,11 @@ export interface LineDefinition {
74
74
  markerEnd?: string;
75
75
  lineType?: LineType;
76
76
  }
77
+ export type MaskShapeKey = 'circle' | 'square' | 'rounded' | 'triangle' | 'diamond' | 'parallelogram' | 'star' | 'heart' | 'cloud';
78
+ export interface MaskDefinition {
79
+ shape: MaskShapeKey;
80
+ rotation?: number;
81
+ }
77
82
  export interface CropDefinition {
78
83
  x1?: number;
79
84
  x2?: number;
@@ -17,5 +17,4 @@ export interface DocumentChange {
17
17
  documentGenerationConfig?: any;
18
18
  backingAssortmentItemData?: any;
19
19
  targetSortIndex?: number;
20
- skipUndo?: boolean;
21
20
  }
@@ -1,5 +1,5 @@
1
1
  import { PropertyLevel } from '@contrail/types';
2
- import { CropDefinition, LineDefinition, PositionDefinition, RotationDefinition, SizeDefinition } from './common';
2
+ import { CropDefinition, LineDefinition, MaskDefinition, PositionDefinition, RotationDefinition, SizeDefinition } from './common';
3
3
  import { Document } from './document';
4
4
  import { ScaleTransformation } from './element-transformation';
5
5
  export interface AlternateUrls {
@@ -32,6 +32,7 @@ export interface DocumentElement extends Document {
32
32
  annotations?: any;
33
33
  isLocked?: boolean;
34
34
  cropDefinition?: CropDefinition;
35
+ maskDefinition?: MaskDefinition;
35
36
  embedInfo?: any;
36
37
  isHidden?: boolean;
37
38
  elementIds?: Array<string>;
@@ -0,0 +1,4 @@
1
+ export * from './mask-shape-geometry';
2
+ export * from './rotation-math';
3
+ export * from './shape-mask-element';
4
+ export * from './mask-preset-geometry';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./mask-shape-geometry"), exports);
18
+ __exportStar(require("./rotation-math"), exports);
19
+ __exportStar(require("./shape-mask-element"), exports);
20
+ __exportStar(require("./mask-preset-geometry"), exports);
@@ -0,0 +1,3 @@
1
+ import { MaskShapeKey } from './mask-shape-geometry';
2
+ export declare const MASK_SHAPE_PRESET_GEOMETRY: Record<MaskShapeKey, string>;
3
+ export declare function getMaskPresetGeometry(shape: MaskShapeKey | null): string | null;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MASK_SHAPE_PRESET_GEOMETRY = void 0;
4
+ exports.getMaskPresetGeometry = getMaskPresetGeometry;
5
+ exports.MASK_SHAPE_PRESET_GEOMETRY = {
6
+ circle: 'ellipse',
7
+ square: 'rect',
8
+ rounded: 'roundRect',
9
+ triangle: 'triangle',
10
+ diamond: 'diamond',
11
+ parallelogram: 'parallelogram',
12
+ star: 'star5',
13
+ heart: 'heart',
14
+ cloud: 'cloud',
15
+ };
16
+ function getMaskPresetGeometry(shape) {
17
+ var _a;
18
+ return shape ? (_a = exports.MASK_SHAPE_PRESET_GEOMETRY[shape]) !== null && _a !== void 0 ? _a : null : null;
19
+ }
@@ -0,0 +1,26 @@
1
+ import { MaskShapeKey } from '../../types';
2
+ export { MaskShapeKey };
3
+ export declare const MASK_SHAPE_KEYS: readonly MaskShapeKey[];
4
+ export interface MaskRect {
5
+ x: number;
6
+ y: number;
7
+ width: number;
8
+ height: number;
9
+ }
10
+ export type PathSegment = {
11
+ op: 'move' | 'line';
12
+ x: number;
13
+ y: number;
14
+ } | {
15
+ op: 'cubic';
16
+ c1x: number;
17
+ c1y: number;
18
+ c2x: number;
19
+ c2y: number;
20
+ x: number;
21
+ y: number;
22
+ } | {
23
+ op: 'close';
24
+ };
25
+ export declare function getMaskShapeSegments(shape: MaskShapeKey, rect: MaskRect, rotationDeg?: number): PathSegment[];
26
+ export declare function rotatedRectAABB(rect: MaskRect, rotationDeg: number): MaskRect;
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MASK_SHAPE_KEYS = void 0;
4
+ exports.getMaskShapeSegments = getMaskShapeSegments;
5
+ exports.rotatedRectAABB = rotatedRectAABB;
6
+ const rotation_math_1 = require("./rotation-math");
7
+ exports.MASK_SHAPE_KEYS = [
8
+ 'circle',
9
+ 'square',
10
+ 'rounded',
11
+ 'triangle',
12
+ 'diamond',
13
+ 'parallelogram',
14
+ 'star',
15
+ 'heart',
16
+ 'cloud',
17
+ ];
18
+ const K = 0.5522847498;
19
+ function polygon(points) {
20
+ const [first, ...rest] = points;
21
+ return [
22
+ { op: 'move', x: first[0], y: first[1] },
23
+ ...rest.map(([x, y]) => ({ op: 'line', x, y })),
24
+ { op: 'close' },
25
+ ];
26
+ }
27
+ function ellipse(cx, cy, rx, ry) {
28
+ return [
29
+ { op: 'move', x: cx + rx, y: cy },
30
+ { op: 'cubic', c1x: cx + rx, c1y: cy + K * ry, c2x: cx + K * rx, c2y: cy + ry, x: cx, y: cy + ry },
31
+ { op: 'cubic', c1x: cx - K * rx, c1y: cy + ry, c2x: cx - rx, c2y: cy + K * ry, x: cx - rx, y: cy },
32
+ { op: 'cubic', c1x: cx - rx, c1y: cy - K * ry, c2x: cx - K * rx, c2y: cy - ry, x: cx, y: cy - ry },
33
+ { op: 'cubic', c1x: cx + K * rx, c1y: cy - ry, c2x: cx + rx, c2y: cy - K * ry, x: cx + rx, y: cy },
34
+ { op: 'close' },
35
+ ];
36
+ }
37
+ function roundedRect(x, y, w, h, r) {
38
+ const x2 = x + w;
39
+ const y2 = y + h;
40
+ return [
41
+ { op: 'move', x: x + r, y },
42
+ { op: 'line', x: x2 - r, y },
43
+ { op: 'cubic', c1x: x2 - r + K * r, c1y: y, c2x: x2, c2y: y + r - K * r, x: x2, y: y + r },
44
+ { op: 'line', x: x2, y: y2 - r },
45
+ { op: 'cubic', c1x: x2, c1y: y2 - r + K * r, c2x: x2 - r + K * r, c2y: y2, x: x2 - r, y: y2 },
46
+ { op: 'line', x: x + r, y: y2 },
47
+ { op: 'cubic', c1x: x + r - K * r, c1y: y2, c2x: x, c2y: y2 - r + K * r, x, y: y2 - r },
48
+ { op: 'line', x, y: y + r },
49
+ { op: 'cubic', c1x: x, c1y: y + r - K * r, c2x: x + r - K * r, c2y: y, x: x + r, y },
50
+ { op: 'close' },
51
+ ];
52
+ }
53
+ const UNIT_SEGMENTS = {
54
+ circle: ellipse(50, 50, 49, 49),
55
+ square: polygon([
56
+ [1, 1],
57
+ [99, 1],
58
+ [99, 99],
59
+ [1, 99],
60
+ ]),
61
+ rounded: roundedRect(1, 1, 98, 98, 16),
62
+ triangle: polygon([
63
+ [50, 3],
64
+ [97, 96],
65
+ [3, 96],
66
+ ]),
67
+ diamond: polygon([
68
+ [50, 2],
69
+ [98, 50],
70
+ [50, 98],
71
+ [2, 50],
72
+ ]),
73
+ parallelogram: polygon([
74
+ [27, 12],
75
+ [98, 12],
76
+ [73, 88],
77
+ [2, 88],
78
+ ]),
79
+ star: polygon([
80
+ [50, 3],
81
+ [61, 37],
82
+ [97, 37],
83
+ [68, 59],
84
+ [79, 95],
85
+ [50, 73],
86
+ [21, 95],
87
+ [32, 59],
88
+ [3, 37],
89
+ [39, 37],
90
+ ]),
91
+ heart: [
92
+ { op: 'move', x: 50, y: 90 },
93
+ { op: 'cubic', c1x: 6, c1y: 58, c2x: 10, c2y: 20, x: 32, y: 18 },
94
+ { op: 'cubic', c1x: 44, c1y: 17, c2x: 50, c2y: 27, x: 50, y: 34 },
95
+ { op: 'cubic', c1x: 50, c1y: 27, c2x: 56, c2y: 17, x: 68, y: 18 },
96
+ { op: 'cubic', c1x: 90, c1y: 20, c2x: 94, c2y: 58, x: 50, y: 90 },
97
+ { op: 'close' },
98
+ ],
99
+ cloud: [
100
+ { op: 'move', x: 30, y: 80 },
101
+ { op: 'cubic', c1x: 13, c1y: 80, c2x: 6, c2y: 66, x: 16, y: 56 },
102
+ { op: 'cubic', c1x: 11, c1y: 42, c2x: 27, c2y: 34, x: 37, y: 42 },
103
+ { op: 'cubic', c1x: 41, c1y: 24, c2x: 66, c2y: 24, x: 70, y: 42 },
104
+ { op: 'cubic', c1x: 86, c1y: 38, c2x: 96, c2y: 56, x: 84, y: 68 },
105
+ { op: 'cubic', c1x: 92, c1y: 78, c2x: 82, c2y: 88, x: 70, y: 84 },
106
+ { op: 'close' },
107
+ ],
108
+ };
109
+ function getMaskShapeSegments(shape, rect, rotationDeg = 0) {
110
+ const unit = UNIT_SEGMENTS[shape];
111
+ if (!unit) {
112
+ throw new Error(`Unknown mask shape: ${shape}`);
113
+ }
114
+ const sx = rect.width / 100;
115
+ const sy = rect.height / 100;
116
+ const tx = (x) => rect.x + x * sx;
117
+ const ty = (y) => rect.y + y * sy;
118
+ const cx = rect.x + rect.width * 0.5;
119
+ const cy = rect.y + rect.height * 0.5;
120
+ const rotate = (x, y) => rotationDeg ? (0, rotation_math_1.rotatePoint)({ x, y }, rotationDeg, { x: cx, y: cy }) : { x, y };
121
+ return unit.map((s) => {
122
+ if (s.op === 'close') {
123
+ return { op: 'close' };
124
+ }
125
+ if (s.op === 'cubic') {
126
+ const c1 = rotate(tx(s.c1x), ty(s.c1y));
127
+ const c2 = rotate(tx(s.c2x), ty(s.c2y));
128
+ const p = rotate(tx(s.x), ty(s.y));
129
+ return { op: 'cubic', c1x: c1.x, c1y: c1.y, c2x: c2.x, c2y: c2.y, x: p.x, y: p.y };
130
+ }
131
+ const p = rotate(tx(s.x), ty(s.y));
132
+ return { op: s.op, x: p.x, y: p.y };
133
+ });
134
+ }
135
+ function rotatedRectAABB(rect, rotationDeg) {
136
+ if (!rotationDeg) {
137
+ return Object.assign({}, rect);
138
+ }
139
+ const cx = rect.x + rect.width * 0.5;
140
+ const cy = rect.y + rect.height * 0.5;
141
+ const corners = [
142
+ { x: rect.x, y: rect.y },
143
+ { x: rect.x + rect.width, y: rect.y },
144
+ { x: rect.x + rect.width, y: rect.y + rect.height },
145
+ { 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);
149
+ const minX = Math.min(...xs);
150
+ const maxX = Math.max(...xs);
151
+ const minY = Math.min(...ys);
152
+ const maxY = Math.max(...ys);
153
+ return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
154
+ }
@@ -0,0 +1,2 @@
1
+ import { PositionDefinition } from '../../types';
2
+ export declare function rotatePoint(point: PositionDefinition, angleDeg: number, pivot?: PositionDefinition): PositionDefinition;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rotatePoint = rotatePoint;
4
+ function rotatePoint(point, angleDeg, pivot = { x: 0, y: 0 }) {
5
+ const theta = (angleDeg * Math.PI) / 180;
6
+ const cos = Math.cos(theta);
7
+ const sin = Math.sin(theta);
8
+ const dx = point.x - pivot.x;
9
+ const dy = point.y - pivot.y;
10
+ return {
11
+ x: dx * cos - dy * sin + pivot.x,
12
+ y: dx * sin + dy * cos + pivot.y,
13
+ };
14
+ }
@@ -0,0 +1,16 @@
1
+ import { CropDefinition, DocumentElement, SizeDefinition } from '../../types';
2
+ import { MaskRect, MaskShapeKey } from './mask-shape-geometry';
3
+ export declare function hasShapeMask(element: DocumentElement): boolean;
4
+ export declare function getShapeMaskKey(element: DocumentElement): MaskShapeKey | null;
5
+ export declare function getShapeMaskRotation(element: DocumentElement): number;
6
+ export declare function normalizeMaskRotation(rotation: number): number;
7
+ export declare function isShapeMaskable(element: DocumentElement): boolean;
8
+ export declare function getDefaultShapeMaskCrop(size: SizeDefinition): CropDefinition;
9
+ export declare function cropDefinitionToMaskRect(crop: CropDefinition, size: SizeDefinition): MaskRect;
10
+ export declare function maskRectToCropDefinition(rect: MaskRect, size: SizeDefinition): CropDefinition;
11
+ export declare const SHAPE_MASK_CLEAR_FIELDS: {
12
+ readonly maskDefinition: any;
13
+ readonly cropDefinition: any;
14
+ readonly imageSize: any;
15
+ readonly imagePosition: any;
16
+ };
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SHAPE_MASK_CLEAR_FIELDS = void 0;
4
+ exports.hasShapeMask = hasShapeMask;
5
+ exports.getShapeMaskKey = getShapeMaskKey;
6
+ exports.getShapeMaskRotation = getShapeMaskRotation;
7
+ exports.normalizeMaskRotation = normalizeMaskRotation;
8
+ exports.isShapeMaskable = isShapeMaskable;
9
+ exports.getDefaultShapeMaskCrop = getDefaultShapeMaskCrop;
10
+ exports.cropDefinitionToMaskRect = cropDefinitionToMaskRect;
11
+ exports.maskRectToCropDefinition = maskRectToCropDefinition;
12
+ const DEFAULT_MASK_FRACTION = 0.72;
13
+ function hasShapeMask(element) {
14
+ var _a, _b;
15
+ return (!!getShapeMaskKey(element) && !!((_a = element === null || element === void 0 ? void 0 : element.cropDefinition) === null || _a === void 0 ? void 0 : _a.widthPercent) && !!((_b = element === null || element === void 0 ? void 0 : element.cropDefinition) === null || _b === void 0 ? void 0 : _b.heightPercent));
16
+ }
17
+ function getShapeMaskKey(element) {
18
+ var _a, _b;
19
+ return (_b = (_a = element === null || element === void 0 ? void 0 : element.maskDefinition) === null || _a === void 0 ? void 0 : _a.shape) !== null && _b !== void 0 ? _b : null;
20
+ }
21
+ function getShapeMaskRotation(element) {
22
+ var _a, _b;
23
+ return (_b = (_a = element === null || element === void 0 ? void 0 : element.maskDefinition) === null || _a === void 0 ? void 0 : _a.rotation) !== null && _b !== void 0 ? _b : 0;
24
+ }
25
+ function normalizeMaskRotation(rotation) {
26
+ return ((rotation % 360) + 360) % 360;
27
+ }
28
+ function isShapeMaskable(element) {
29
+ return (element === null || element === void 0 ? void 0 : element.type) === 'image';
30
+ }
31
+ function getDefaultShapeMaskCrop(size) {
32
+ const side = Math.min(size.width, size.height) * DEFAULT_MASK_FRACTION;
33
+ return {
34
+ x1Percent: ((size.width - side) * 0.5) / size.width,
35
+ y1Percent: ((size.height - side) * 0.5) / size.height,
36
+ widthPercent: side / size.width,
37
+ heightPercent: side / size.height,
38
+ };
39
+ }
40
+ function cropDefinitionToMaskRect(crop, size) {
41
+ return {
42
+ x: crop.x1Percent * size.width,
43
+ y: crop.y1Percent * size.height,
44
+ width: crop.widthPercent * size.width,
45
+ height: crop.heightPercent * size.height,
46
+ };
47
+ }
48
+ function maskRectToCropDefinition(rect, size) {
49
+ return {
50
+ x1Percent: rect.x / size.width,
51
+ y1Percent: rect.y / size.height,
52
+ widthPercent: rect.width / size.width,
53
+ heightPercent: rect.height / size.height,
54
+ };
55
+ }
56
+ exports.SHAPE_MASK_CLEAR_FIELDS = {
57
+ maskDefinition: null,
58
+ cropDefinition: null,
59
+ imageSize: null,
60
+ imagePosition: null,
61
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/documents",
3
- "version": "1.17.0-alpha.7002786",
3
+ "version": "1.17.0-alpha.f7593e7",
4
4
  "description": "Documents library for contrail platform",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",