{"version":3,"file":"FabricObjectSVGExportMixin.min.mjs","sources":["../../../../src/shapes/Object/FabricObjectSVGExportMixin.ts"],"sourcesContent":["import type { TSVGReviver } from '../../typedefs';\nimport { uid } from '../../util/internals/uid';\nimport { colorPropToSVG } from '../../util/misc/svgParsing';\nimport { FILL, NONE, STROKE } from '../../constants';\nimport type { FabricObject } from './FabricObject';\nimport { isFiller } from '../../util/typeAssertions';\nimport { matrixToSVG } from '../../util/misc/svgExport';\n\nexport class FabricObjectSVGExportMixin {\n  /**\n   * When an object is being exported as SVG as a clippath, a reference inside the SVG is needed.\n   * This reference is a UID in the fabric namespace and is temporary stored here.\n   * @type {String}\n   */\n  declare clipPathId?: string;\n\n  /**\n   * Returns styles-string for svg-export\n   * @param {Boolean} skipShadow a boolean to skip shadow filter output\n   * @return {String}\n   */\n  getSvgStyles(\n    this: FabricObjectSVGExportMixin & FabricObject,\n    skipShadow?: boolean,\n  ) {\n    const fillRule = this.fillRule ? this.fillRule : 'nonzero',\n      strokeWidth = this.strokeWidth ? this.strokeWidth : '0',\n      strokeDashArray = this.strokeDashArray\n        ? this.strokeDashArray.join(' ')\n        : NONE,\n      strokeDashOffset = this.strokeDashOffset ? this.strokeDashOffset : '0',\n      strokeLineCap = this.strokeLineCap ? this.strokeLineCap : 'butt',\n      strokeLineJoin = this.strokeLineJoin ? this.strokeLineJoin : 'miter',\n      strokeMiterLimit = this.strokeMiterLimit ? this.strokeMiterLimit : '4',\n      opacity = typeof this.opacity !== 'undefined' ? this.opacity : '1',\n      visibility = this.visible ? '' : ' visibility: hidden;',\n      filter = skipShadow ? '' : this.getSvgFilter(),\n      fill = colorPropToSVG(FILL, this.fill),\n      stroke = colorPropToSVG(STROKE, this.stroke);\n\n    return [\n      stroke,\n      'stroke-width: ',\n      strokeWidth,\n      '; ',\n      'stroke-dasharray: ',\n      strokeDashArray,\n      '; ',\n      'stroke-linecap: ',\n      strokeLineCap,\n      '; ',\n      'stroke-dashoffset: ',\n      strokeDashOffset,\n      '; ',\n      'stroke-linejoin: ',\n      strokeLineJoin,\n      '; ',\n      'stroke-miterlimit: ',\n      strokeMiterLimit,\n      '; ',\n      fill,\n      'fill-rule: ',\n      fillRule,\n      '; ',\n      'opacity: ',\n      opacity,\n      ';',\n      filter,\n      visibility,\n    ].join('');\n  }\n\n  /**\n   * Returns filter for svg shadow\n   * @return {String}\n   */\n  getSvgFilter(this: FabricObjectSVGExportMixin & FabricObject) {\n    return this.shadow ? `filter: url(#SVGID_${this.shadow.id});` : '';\n  }\n\n  /**\n   * Returns id attribute for svg output\n   * @return {String}\n   */\n  getSvgCommons(\n    this: FabricObjectSVGExportMixin & FabricObject & { id?: string },\n  ) {\n    return [\n      this.id ? `id=\"${this.id}\" ` : '',\n      this.clipPath\n        ? `clip-path=\"url(#${\n            (this.clipPath as FabricObjectSVGExportMixin & FabricObject)\n              .clipPathId\n          })\" `\n        : '',\n    ].join('');\n  }\n\n  /**\n   * Returns transform-string for svg-export\n   * @param {Boolean} use the full transform or the single object one.\n   * @return {String}\n   */\n  getSvgTransform(\n    this: FabricObjectSVGExportMixin & FabricObject,\n    full?: boolean,\n    additionalTransform = '',\n  ) {\n    const transform = full ? this.calcTransformMatrix() : this.calcOwnMatrix(),\n      svgTransform = `transform=\"${matrixToSVG(transform)}`;\n    return `${svgTransform}${additionalTransform}\" `;\n  }\n\n  /**\n   * Returns svg representation of an instance\n   * This function is implemented in each subclass\n   * This is just because typescript otherwise cryies all the time\n   * @return {Array} an array of strings with the specific svg representation\n   * of the instance\n   */\n  _toSVG(_reviver?: TSVGReviver): string[] {\n    return [''];\n  }\n\n  /**\n   * Returns svg representation of an instance\n   * @param {TSVGReviver} [reviver] Method for further parsing of svg representation.\n   * @return {String} svg representation of an instance\n   */\n  toSVG(\n    this: FabricObjectSVGExportMixin & FabricObject,\n    reviver?: TSVGReviver,\n  ) {\n    return this._createBaseSVGMarkup(this._toSVG(reviver), {\n      reviver,\n    });\n  }\n\n  /**\n   * Returns svg clipPath representation of an instance\n   * @param {TSVGReviver} [reviver] Method for further parsing of svg representation.\n   * @return {String} svg representation of an instance\n   */\n  toClipPathSVG(\n    this: FabricObjectSVGExportMixin & FabricObject,\n    reviver?: TSVGReviver,\n  ) {\n    return (\n      '\\t' +\n      this._createBaseClipPathSVGMarkup(this._toSVG(reviver), {\n        reviver,\n      })\n    );\n  }\n\n  /**\n   * @private\n   */\n  _createBaseClipPathSVGMarkup(\n    this: FabricObjectSVGExportMixin & FabricObject,\n    objectMarkup: string[],\n    {\n      reviver,\n      additionalTransform = '',\n    }: { reviver?: TSVGReviver; additionalTransform?: string } = {},\n  ) {\n    const commonPieces = [\n        this.getSvgTransform(true, additionalTransform),\n        this.getSvgCommons(),\n      ].join(''),\n      // insert commons in the markup, style and svgCommons\n      index = objectMarkup.indexOf('COMMON_PARTS');\n    objectMarkup[index] = commonPieces;\n    return reviver ? reviver(objectMarkup.join('')) : objectMarkup.join('');\n  }\n\n  /**\n   * @private\n   */\n  _createBaseSVGMarkup(\n    this: FabricObjectSVGExportMixin & FabricObject,\n    objectMarkup: string[],\n    {\n      noStyle,\n      reviver,\n      withShadow,\n      additionalTransform,\n    }: {\n      noStyle?: boolean;\n      reviver?: TSVGReviver;\n      withShadow?: boolean;\n      additionalTransform?: string;\n    } = {},\n  ): string {\n    const styleInfo = noStyle ? '' : `style=\"${this.getSvgStyles()}\" `,\n      shadowInfo = withShadow ? `style=\"${this.getSvgFilter()}\" ` : '',\n      clipPath = this.clipPath as FabricObjectSVGExportMixin & FabricObject,\n      vectorEffect = this.strokeUniform\n        ? 'vector-effect=\"non-scaling-stroke\" '\n        : '',\n      absoluteClipPath = clipPath && clipPath.absolutePositioned,\n      stroke = this.stroke,\n      fill = this.fill,\n      shadow = this.shadow,\n      markup = [],\n      // insert commons in the markup, style and svgCommons\n      index = objectMarkup.indexOf('COMMON_PARTS');\n    let clipPathMarkup;\n    if (clipPath) {\n      clipPath.clipPathId = `CLIPPATH_${uid()}`;\n      clipPathMarkup = `<clipPath id=\"${\n        clipPath.clipPathId\n      }\" >\\n${clipPath.toClipPathSVG(reviver)}</clipPath>\\n`;\n    }\n    if (absoluteClipPath) {\n      markup.push('<g ', shadowInfo, this.getSvgCommons(), ' >\\n');\n    }\n    markup.push(\n      '<g ',\n      this.getSvgTransform(false),\n      !absoluteClipPath ? shadowInfo + this.getSvgCommons() : '',\n      ' >\\n',\n    );\n    const commonPieces = [\n      styleInfo,\n      vectorEffect,\n      noStyle ? '' : this.addPaintOrder(),\n      ' ',\n      additionalTransform ? `transform=\"${additionalTransform}\" ` : '',\n    ].join('');\n    objectMarkup[index] = commonPieces;\n    if (isFiller(fill)) {\n      markup.push(fill.toSVG(this));\n    }\n    if (isFiller(stroke)) {\n      markup.push(stroke.toSVG(this));\n    }\n    if (shadow) {\n      markup.push(shadow.toSVG(this));\n    }\n    if (clipPath) {\n      markup.push(clipPathMarkup);\n    }\n    markup.push(objectMarkup.join(''));\n    markup.push('</g>\\n');\n    absoluteClipPath && markup.push('</g>\\n');\n    return reviver ? reviver(markup.join('')) : markup.join('');\n  }\n\n  addPaintOrder(this: FabricObjectSVGExportMixin & FabricObject) {\n    return this.paintFirst !== FILL ? ` paint-order=\"${this.paintFirst}\" ` : '';\n  }\n}\n"],"names":["FabricObjectSVGExportMixin","getSvgStyles","skipShadow","fillRule","this","strokeWidth","strokeDashArray","join","NONE","strokeDashOffset","strokeLineCap","strokeLineJoin","strokeMiterLimit","opacity","visibility","visible","filter","getSvgFilter","fill","colorPropToSVG","FILL","STROKE","stroke","shadow","id","getSvgCommons","clipPath","clipPathId","getSvgTransform","full","additionalTransform","arguments","length","undefined","transform","calcTransformMatrix","calcOwnMatrix","matrixToSVG","_toSVG","_reviver","toSVG","reviver","_createBaseSVGMarkup","toClipPathSVG","_createBaseClipPathSVGMarkup","objectMarkup","commonPieces","index","indexOf","noStyle","withShadow","styleInfo","shadowInfo","vectorEffect","strokeUniform","absoluteClipPath","absolutePositioned","markup","clipPathMarkup","uid","push","addPaintOrder","isFi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