{"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 { Pattern } from '../../Pattern';\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   * James added, 增加clipPathPattern属性，用于导出svg时 Pattern转Image对象\n   */\n  declare clipPathPattern?: FabricObject | null;\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      // James modified, 非skipShadow 时候，fill和stroke都设置为白色\n      fill = skipShadow ? colorPropToSVG(FILL, this.fill) : 'fill: white;',\n      stroke = skipShadow\n        ? colorPropToSVG(STROKE, this.stroke)\n        : 'stroke:white;';\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   * James added\n   * Returns pattern svg attributes string\n   * @return {String}\n   */\n  getPatternSvgCommons(\n    this: FabricObjectSVGExportMixin & FabricObject & { id?: string },\n  ) {\n    return [\n      this.id ? 'id=\"' + this.id + '_clip\" ' : '',\n      this.clipPathPattern\n        ? 'clip-path=\"url(#' + this.clipPathPattern.clipPathId + ')\" '\n        : '',\n    ].join('');\n  }\n\n  /**\n   * James added\n   * Returns id attribute for svg clippath output\n   * @return {String}\n   */\n  getSvgCommonsClipPath(\n    this: FabricObjectSVGExportMixin & FabricObject & { id?: string },\n  ) {\n    if (this.clipPathPattern) {\n      return [this.id ? 'id=\"' + this.id + '\" ' : ''].join('');\n    } else {\n      return this.getSvgCommons();\n    }\n  }\n\n  /**\n   * James added\n   * 自定义clipPath的svg输出transform\n   * @param {*} full\n   * @param {*} additionalTransform\n   */\n  getSvgTransformClipPath(\n    this: FabricObjectSVGExportMixin & FabricObject,\n    full: boolean,\n    additionalTransform = '',\n  ) {\n    // 如果是pattern，就不需要transform\n    if (this.clipPathPattern) {\n      return additionalTransform\n        ? 'transform=\"' + additionalTransform + '\" '\n        : '';\n    } else {\n      return this.getSvgTransform(full, additionalTransform);\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.getSvgTransformClipPath(true, additionalTransform),\n        this.getSvgCommonsClipPath(),\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    var skipShadow = true;\n    let clipPath = this.clipPath as FabricObjectSVGExportMixin & FabricObject;\n    const styleInfo = noStyle\n        ? ''\n        : `style=\"${this.getSvgStyles(skipShadow)}\" `,\n      shadowInfo = withShadow ? `style=\"${this.getSvgFilter()}\" ` : '',\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    // James added\n    let commonPieces = '',\n      clipPathMarkup = '';\n\n    // 如果是pattern，就生成clipppath\n    // 使用 clipPathPattern区别是否是pattern\n    this.clipPathPattern = null;\n    if (fill instanceof Pattern) {\n      this.clipPathPattern = clipPath = this;\n    }\n    // James added end\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    commonPieces = [\n      styleInfo,\n      vectorEffect,\n      noStyle ? '' : this.addPaintOrder(),\n      ' ',\n      additionalTransform ? `transform=\"${additionalTransform}\" ` : '',\n    ].join('');\n\n    // James added shadow 放在上面\n    // 文本等调用方已经通过 withShadow 挂载 filter 时，避免再额外复制一份可见对象，\n    // 但仍然需要输出 filter 定义，否则普通 SVG 会丢失阴影。\n    if (shadow) {\n      markup.push(shadow.toSVG(this));\n    }\n    if (shadow && !withShadow) {\n      const styleInfoWithShadow = 'style=\"' + this.getSvgStyles(false) + '\" ';\n      const commonPiecesWithShadow = [\n        styleInfoWithShadow,\n        vectorEffect,\n        noStyle ? '' : this.addPaintOrder(),\n        ' ',\n        additionalTransform ? 'transform=\"' + additionalTransform + '\" ' : '',\n      ].join('');\n      const objectMarkupCopy = JSON.parse(JSON.stringify(objectMarkup));\n      objectMarkupCopy[index] = commonPiecesWithShadow;\n\n      markup.push(objectMarkupCopy.join(''));\n    }\n    // objectMarkup中是导出主对象(如path)的svg，index下标是style，放在commonPieces\n    objectMarkup[index] = commonPieces;\n    if (isFiller(fill)) {\n      // James added clipPattern apply pattern clip path just for fill.\n      if (this.clipPathPattern) {\n        markup.push('<g ', shadowInfo, this.getPatternSvgCommons(), ' >\\n');\n      }\n      markup.push(fill.toSVG(this));\n      // James added clipPattern\n      if (this.clipPathPattern) {\n        markup.push('</g>\\n');\n      }\n    }\n    if (isFiller(stroke)) {\n      markup.push(stroke.toSVG(this));\n    }\n    if (clipPath) {\n      markup.push(clipPathMarkup);\n    }\n    // if use pattern, ignore objectMarkup, because pattern clipPath has been applied in pattern's svg, and objectMarkup is not needed anymore\n    if (!this.clipPathPattern) {\n      markup.push(objectMarkup.join(''));\n    }\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","getPatternSvgCommons","clipPathPattern","getSvgCommonsClipPath","getSvgTransformClipPath","objectMarkup","commonPieces","index","indexOf","noStyle","withShadow","styleInfo","shadowInfo","vectorEffect","strokeUniform","absoluteClipPath","absolutePositioned","markup","clipPathMarkup","Pattern","uid","push","addPaintOrder","commonPiecesWithShadow","objectMarkupCopy","JSON","parse","stringify","isFiller","paintFirst"],"mappings":"qXASO,MAAMA,EAkBXC,YAAAA,CAEEC,GAEA,MAAMC,EAAWC,KAAKD,SAAWC,KAAKD,SAAW,UAC/CE,EAAcD,KAAKC,YAAcD,KAAKC,YAAc,IACpDC,EAAkBF,KAAKE,gBACnBF,KAAKE,gBAAgBC,KAAK,KAC1BC,EACJC,EAAmBL,KAAKK,iBAAmBL,KAAKK,iBAAmB,IACnEC,EAAgBN,KAAKM,cAAgBN,KAAKM,cAAgB,OAC1DC,EAAiBP,KAAKO,eAAiBP,KAAKO,eAAiB,QAC7DC,EAAmBR,KAAKQ,iBAAmBR,KAAKQ,iBAAmB,IACnEC,OAAkC,IAAjBT,KAAKS,QAA0BT,KAAKS,QAAU,IAC/DC,EAAaV,KAAKW,QAAU,GAAK,uBACjCC,EAASd,EAAa,GAAKE,K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