{
  "version": 3,
  "sources": ["../../src/shapes/TLDrawShape.ts"],
  "sourcesContent": ["import { T } from '@tldraw/validate'\nimport { b64Vecs } from '../misc/b64Vecs'\nimport { VecModel } from '../misc/geometry-types'\nimport { createShapePropsMigrationIds, createShapePropsMigrationSequence } from '../records/TLShape'\nimport { RecordProps } from '../recordsWithProps'\nimport { DefaultColorStyle, TLDefaultColorStyle } from '../styles/TLColorStyle'\nimport { DefaultDashStyle, TLDefaultDashStyle } from '../styles/TLDashStyle'\nimport { DefaultFillStyle, TLDefaultFillStyle } from '../styles/TLFillStyle'\nimport { DefaultSizeStyle, TLDefaultSizeStyle } from '../styles/TLSizeStyle'\nimport { TLBaseShape } from './TLBaseShape'\n\n/**\n * A segment of a draw shape representing either freehand drawing or straight line segments.\n *\n * @public\n */\nexport interface TLDrawShapeSegment {\n\t/** Type of drawing segment - 'free' for freehand curves, 'straight' for line segments */\n\ttype: 'free' | 'straight'\n\t/**\n\t * Delta-encoded base64 path data.\n\t * First point stored as Float32 (12 bytes) for precision, subsequent points as Float16 deltas (6 bytes each).\n\t */\n\tpath: string\n}\n\n/**\n * Validator for draw shape segments ensuring proper structure and data types.\n *\n * @public\n */\nexport const DrawShapeSegment: T.ObjectValidator<TLDrawShapeSegment> = T.object({\n\ttype: T.literalEnum('free', 'straight'),\n\tpath: T.string,\n})\n\n/**\n * Properties for the draw shape, which represents freehand drawing and sketching.\n *\n * @public\n */\nexport interface TLDrawShapeProps {\n\t/** Color style for the drawing stroke */\n\tcolor: TLDefaultColorStyle\n\t/** Fill style for closed drawing shapes */\n\tfill: TLDefaultFillStyle\n\t/** Dash pattern style for the stroke */\n\tdash: TLDefaultDashStyle\n\t/** Size/thickness of the drawing stroke */\n\tsize: TLDefaultSizeStyle\n\t/** Array of segments that make up the complete drawing path */\n\tsegments: TLDrawShapeSegment[]\n\t/** Whether the drawing is complete (user finished drawing) */\n\tisComplete: boolean\n\t/** Whether the drawing path forms a closed shape */\n\tisClosed: boolean\n\t/** Whether this drawing was created with a pen/stylus device */\n\tisPen: boolean\n\t/** Scale factor applied to the drawing */\n\tscale: number\n\t/** Horizontal scale factor for lazy resize */\n\tscaleX: number\n\t/** Vertical scale factor for lazy resize */\n\tscaleY: number\n}\n\n/**\n * A draw shape represents freehand drawing, sketching, and pen input on the canvas.\n * Draw shapes are composed of segments that can be either smooth curves or straight lines.\n *\n * @public\n * @example\n * ```ts\n * const drawShape: TLDrawShape = {\n *   id: createShapeId(),\n *   typeName: 'shape',\n *   type: 'draw',\n *   x: 50,\n *   y: 50,\n *   rotation: 0,\n *   index: 'a1',\n *   parentId: 'page:page1',\n *   isLocked: false,\n *   opacity: 1,\n *   props: {\n *     color: 'black',\n *     fill: 'none',\n *     dash: 'solid',\n *     size: 'm',\n *     segments: [{\n *       type: 'free',\n *       points: [{ x: 0, y: 0, z: 0.5 }, { x: 20, y: 15, z: 0.6 }]\n *     }],\n *     isComplete: true,\n *     isClosed: false,\n *     isPen: false,\n *     scale: 1\n *   },\n *   meta: {}\n * }\n * ```\n */\nexport type TLDrawShape = TLBaseShape<'draw', TLDrawShapeProps>\n\n/**\n * Validation schema for draw shape properties.\n *\n * @public\n * @example\n * ```ts\n * // Validate draw shape properties\n * const props = {\n *   color: 'red',\n *   fill: 'solid',\n *   segments: [{ type: 'free', points: [] }],\n *   isComplete: true\n * }\n * const isValid = drawShapeProps.color.isValid(props.color)\n * ```\n */\n/** @public */\nexport const drawShapeProps: RecordProps<TLDrawShape> = {\n\tcolor: DefaultColorStyle,\n\tfill: DefaultFillStyle,\n\tdash: DefaultDashStyle,\n\tsize: DefaultSizeStyle,\n\tsegments: T.arrayOf(DrawShapeSegment),\n\tisComplete: T.boolean,\n\tisClosed: T.boolean,\n\tisPen: T.boolean,\n\tscale: T.nonZeroNumber,\n\tscaleX: T.nonZeroFiniteNumber,\n\tscaleY: T.nonZeroFiniteNumber,\n}\n\nconst Versions = createShapePropsMigrationIds('draw', {\n\tAddInPen: 1,\n\tAddScale: 2,\n\tBase64: 3,\n\tLegacyPointsConversion: 4,\n})\n\n/**\n * Version identifiers for draw shape migrations.\n *\n * @public\n */\nexport { Versions as drawShapeVersions }\n\n/**\n * Migration sequence for draw shape properties across different schema versions.\n * Handles adding pen detection and scale properties to existing draw shapes.\n *\n * @public\n */\nexport const drawShapeMigrations = createShapePropsMigrationSequence({\n\tsequence: [\n\t\t{\n\t\t\tid: Versions.AddInPen,\n\t\t\tup: (props) => {\n\t\t\t\t// Rather than checking to see whether the shape is a pen at runtime,\n\t\t\t\t// from now on we're going to use the type of device reported to us\n\t\t\t\t// as well as the pressure data received; but for existing shapes we\n\t\t\t\t// need to check the pressure data to see if it's a pen or not.\n\n\t\t\t\tconst { points } = props.segments[0]\n\n\t\t\t\tif (points.length === 0) {\n\t\t\t\t\tprops.isPen = false\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tlet isPen = !(points[0].z === 0 || points[0].z === 0.5)\n\n\t\t\t\tif (points[1]) {\n\t\t\t\t\t// Double check if we have a second point (we probably should)\n\t\t\t\t\tisPen = isPen && !(points[1].z === 0 || points[1].z === 0.5)\n\t\t\t\t}\n\t\t\t\tprops.isPen = isPen\n\t\t\t},\n\t\t\tdown: 'retired',\n\t\t},\n\t\t{\n\t\t\tid: Versions.AddScale,\n\t\t\tup: (props) => {\n\t\t\t\tprops.scale = 1\n\t\t\t},\n\t\t\tdown: (props) => {\n\t\t\t\tdelete props.scale\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tid: Versions.Base64,\n\t\t\tup: (props) => {\n\t\t\t\t// Convert VecModel[] arrays directly to delta-encoded base64 in 'path'\n\t\t\t\tprops.segments = props.segments.map((segment: any) => {\n\t\t\t\t\tif (segment.path !== undefined) return segment\n\t\t\t\t\tconst { points, ...rest } = segment\n\t\t\t\t\tconst vecModels = Array.isArray(points) ? points : b64Vecs._legacyDecodePoints(points)\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...rest,\n\t\t\t\t\t\tpath: b64Vecs.encodePoints(vecModels),\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t\tprops.scaleX = props.scaleX ?? 1\n\t\t\t\tprops.scaleY = props.scaleY ?? 1\n\t\t\t},\n\t\t\tdown: (props) => {\n\t\t\t\t// Convert delta-encoded 'path' back to VecModel[] arrays in 'points'\n\t\t\t\tprops.segments = props.segments.map((segment: any) => {\n\t\t\t\t\tconst { path, ...rest } = segment\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...rest,\n\t\t\t\t\t\tpoints: b64Vecs.decodePoints(path),\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t\tdelete props.scaleX\n\t\t\t\tdelete props.scaleY\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tid: Versions.LegacyPointsConversion,\n\t\t\tup: (props) => {\n\t\t\t\t// Handle legacy data that was already migrated to v3 with absolute Float16 in 'points'\n\t\t\t\t// Convert 'points' to delta-encoded 'path'\n\t\t\t\tprops.segments = props.segments.map((segment: any) => {\n\t\t\t\t\t// If segment already has 'path', it's already in the new format\n\t\t\t\t\tif (segment.path !== undefined) return segment\n\n\t\t\t\t\tconst { points, ...rest } = segment\n\t\t\t\t\tconst vecModels = Array.isArray(points) ? points : b64Vecs._legacyDecodePoints(points)\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...rest,\n\t\t\t\t\t\tpath: b64Vecs.encodePoints(vecModels),\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t},\n\t\t\tdown: (_props) => {\n\t\t\t\t// handled by the previous down migration\n\t\t\t},\n\t\t},\n\t],\n})\n\n/**\n * Compress legacy draw shape segments by converting VecModel[] points to delta-encoded base64 format.\n * This function is useful for converting old draw shape data to the new compressed format.\n * Uses delta encoding for improved Float16 precision.\n *\n * @public\n */\nexport function compressLegacySegments(\n\tsegments: {\n\t\ttype: 'free' | 'straight'\n\t\tpoints: VecModel[]\n\t}[]\n): TLDrawShapeSegment[] {\n\treturn segments.map((segment) => ({\n\t\ttype: segment.type,\n\t\tpath: b64Vecs.encodeP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