@leafer-draw/node 1.6.7 → 1.8.0

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/dist/node.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import { LeaferCanvasBase, Platform, canvasPatch, FileHelper, Creator, LeaferImage, defineKey, LeafList, DataHelper, RenderEvent, ChildEvent, WatchEvent, PropertyEvent, LeafHelper, BranchHelper, LeafBoundsHelper, Bounds, Debug, LeafLevelList, LayoutEvent, Run, ImageManager, ResizeEvent, BoundsHelper, MatrixHelper, MathHelper, AlignHelper, PointHelper, ImageEvent, AroundHelper, Direction4 } from '@leafer/core';
1
+ import { LeaferCanvasBase, Platform, canvasPatch, FileHelper, Creator, LeaferImage, defineKey, LeafList, DataHelper, RenderEvent, ChildEvent, WatchEvent, PropertyEvent, LeafHelper, BranchHelper, LeafBoundsHelper, Bounds, Debug, LeafLevelList, LayoutEvent, Run, ImageManager, ResizeEvent, BoundsHelper, getMatrixData, MatrixHelper, MathHelper, AlignHelper, PointHelper, ImageEvent, AroundHelper, Direction4 } from '@leafer/core';
2
2
  export * from '@leafer/core';
3
3
  export { LeaferImage } from '@leafer/core';
4
4
  import { writeFileSync } from 'fs';
@@ -711,9 +711,14 @@ function fills(fills, ui, canvas) {
711
711
  }
712
712
  }
713
713
  canvas.fillStyle = item.style;
714
- if (item.transform) {
714
+ if (item.transform || item.scaleFixed) {
715
715
  canvas.save();
716
- canvas.transform(item.transform);
716
+ if (item.transform)
717
+ canvas.transform(item.transform);
718
+ if (item.scaleFixed) {
719
+ const { scaleX, scaleY } = ui.getRenderScaleData(true);
720
+ canvas.scale(1 / scaleX, 1 / scaleY);
721
+ }
717
722
  if (item.blendMode)
718
723
  canvas.blendMode = item.blendMode;
719
724
  fillPathOrText(ui, canvas);
@@ -749,8 +754,13 @@ function strokeText(stroke, ui, canvas) {
749
754
  }
750
755
  function drawCenter$1(stroke, strokeWidthScale, ui, canvas) {
751
756
  const data = ui.__;
752
- canvas.setStroke(!data.__isStrokes && stroke, data.strokeWidth * strokeWidthScale, data);
753
- data.__isStrokes ? drawStrokesStyle(stroke, true, ui, canvas) : drawTextStroke(ui, canvas);
757
+ if (typeof stroke === 'object') {
758
+ drawStrokesStyle(stroke, strokeWidthScale, true, ui, canvas);
759
+ }
760
+ else {
761
+ canvas.setStroke(stroke, data.__strokeWidth * strokeWidthScale, data);
762
+ drawTextStroke(ui, canvas);
763
+ }
754
764
  }
755
765
  function drawAlign(stroke, align, ui, canvas) {
756
766
  const out = canvas.getSameCanvas(true, true);
@@ -759,15 +769,9 @@ function drawAlign(stroke, align, ui, canvas) {
759
769
  out.blendMode = align === 'outside' ? 'destination-out' : 'destination-in';
760
770
  fillText(ui, out);
761
771
  out.blendMode = 'normal';
762
- copyWorld(canvas, out, ui);
772
+ LeafHelper.copyCanvasByWorld(ui, canvas, out);
763
773
  out.recycle(ui.__nowWorld);
764
774
  }
765
- function copyWorld(canvas, out, ui) {
766
- if (ui.__worldFlipped || Platform.fullImageShadow)
767
- canvas.copyWorldByReset(out, ui.__nowWorld);
768
- else
769
- canvas.copyWorldToInner(out, ui.__nowWorld, ui.__layout.renderBounds);
770
- }
771
775
  function drawTextStroke(ui, canvas) {
772
776
  let row, data = ui.__.__textDrawData;
773
777
  const { rows, decorationY } = data;
@@ -783,14 +787,21 @@ function drawTextStroke(ui, canvas) {
783
787
  rows.forEach(row => decorationY.forEach(value => canvas.strokeRect(row.x, row.y + value, row.width, decorationHeight)));
784
788
  }
785
789
  }
786
- function drawStrokesStyle(strokes, isText, ui, canvas) {
790
+ function drawStrokesStyle(strokes, strokeWidthScale, isText, ui, canvas) {
787
791
  let item;
792
+ const data = ui.__, { __hasMultiStrokeStyle } = data;
793
+ __hasMultiStrokeStyle || canvas.setStroke(undefined, data.__strokeWidth * strokeWidthScale, data);
788
794
  for (let i = 0, len = strokes.length; i < len; i++) {
789
795
  item = strokes[i];
790
796
  if (item.image && PaintImage.checkImage(ui, canvas, item, false))
791
797
  continue;
792
798
  if (item.style) {
793
- canvas.strokeStyle = item.style;
799
+ if (__hasMultiStrokeStyle) {
800
+ const { strokeStyle } = item;
801
+ strokeStyle ? canvas.setStroke(item.style, data.__getRealStrokeWidth(strokeStyle) * strokeWidthScale, data, strokeStyle) : canvas.setStroke(item.style, data.__strokeWidth * strokeWidthScale, data);
802
+ }
803
+ else
804
+ canvas.strokeStyle = item.style;
794
805
  if (item.blendMode) {
795
806
  canvas.saveBlendMode(item.blendMode);
796
807
  isText ? drawTextStroke(ui, canvas) : canvas.stroke();
@@ -829,8 +840,13 @@ function strokes(strokes, ui, canvas) {
829
840
  }
830
841
  function drawCenter(stroke, strokeWidthScale, ui, canvas) {
831
842
  const data = ui.__;
832
- canvas.setStroke(!data.__isStrokes && stroke, data.__strokeWidth * strokeWidthScale, data);
833
- data.__isStrokes ? drawStrokesStyle(stroke, false, ui, canvas) : canvas.stroke();
843
+ if (typeof stroke === 'object') {
844
+ drawStrokesStyle(stroke, strokeWidthScale, false, ui, canvas);
845
+ }
846
+ else {
847
+ canvas.setStroke(stroke, data.__strokeWidth * strokeWidthScale, data);
848
+ canvas.stroke();
849
+ }
834
850
  if (data.__useArrow)
835
851
  Paint.strokeArrow(stroke, ui, canvas);
836
852
  }
@@ -852,7 +868,7 @@ function drawOutside(stroke, ui, canvas) {
852
868
  drawCenter(stroke, 2, ui, out);
853
869
  out.clipUI(data);
854
870
  out.clearWorld(renderBounds);
855
- copyWorld(canvas, out, ui);
871
+ LeafHelper.copyCanvasByWorld(ui, canvas, out);
856
872
  out.recycle(ui.__nowWorld);
857
873
  }
858
874
  }
@@ -907,8 +923,16 @@ function compute(attrName, ui) {
907
923
  if (!(paints instanceof Array))
908
924
  paints = [paints];
909
925
  recycleMap = PaintImage.recycleImage(attrName, data);
926
+ let maxChildStrokeWidth;
910
927
  for (let i = 0, len = paints.length, item; i < len; i++) {
911
- (item = getLeafPaint(attrName, paints[i], ui)) && leafPaints.push(item);
928
+ if (item = getLeafPaint(attrName, paints[i], ui)) {
929
+ leafPaints.push(item);
930
+ if (item.strokeStyle) {
931
+ maxChildStrokeWidth || (maxChildStrokeWidth = 1);
932
+ if (item.strokeStyle.strokeWidth)
933
+ maxChildStrokeWidth = Math.max(maxChildStrokeWidth, item.strokeStyle.strokeWidth);
934
+ }
935
+ }
912
936
  }
913
937
  data['_' + attrName] = leafPaints.length ? leafPaints : undefined;
914
938
  if (leafPaints.length) {
@@ -925,6 +949,7 @@ function compute(attrName, ui) {
925
949
  else {
926
950
  stintSet(data, '__isAlphaPixelStroke', isAlphaPixel);
927
951
  stintSet(data, '__isTransparentStroke', isTransparent);
952
+ stintSet(data, '__hasMultiStrokeStyle', maxChildStrokeWidth);
928
953
  }
929
954
  }
930
955
  function getLeafPaint(attrName, paint, ui) {
@@ -956,6 +981,11 @@ function getLeafPaint(attrName, paint, ui) {
956
981
  if (data) {
957
982
  if (typeof data.style === 'string' && hasTransparent$1(data.style))
958
983
  data.isTransparent = true;
984
+ if (paint.style) {
985
+ if (paint.style.strokeWidth === 0)
986
+ return undefined;
987
+ data.strokeStyle = paint.style;
988
+ }
959
989
  if (paint.blendMode)
960
990
  data.blendMode = paint.blendMode;
961
991
  }
@@ -975,8 +1005,8 @@ const PaintModule = {
975
1005
  shape
976
1006
  };
977
1007
 
978
- let origin = {};
979
- const { get: get$3, rotateOfOuter: rotateOfOuter$1, translate: translate$1, scaleOfOuter: scaleOfOuter$1, scale: scaleHelper, rotate } = MatrixHelper;
1008
+ let origin = {}, tempMatrix = getMatrixData();
1009
+ const { get: get$3, rotateOfOuter: rotateOfOuter$1, translate: translate$1, scaleOfOuter: scaleOfOuter$1, multiplyParent, scale: scaleHelper, rotate, skew: skewHelper } = MatrixHelper;
980
1010
  function fillOrFitMode(data, box, x, y, scaleX, scaleY, rotation) {
981
1011
  const transform = get$3();
982
1012
  translate$1(transform, box.x + x, box.y + y);
@@ -985,13 +1015,19 @@ function fillOrFitMode(data, box, x, y, scaleX, scaleY, rotation) {
985
1015
  rotateOfOuter$1(transform, { x: box.x + box.width / 2, y: box.y + box.height / 2 }, rotation);
986
1016
  data.transform = transform;
987
1017
  }
988
- function clipMode(data, box, x, y, scaleX, scaleY, rotation) {
1018
+ function clipMode(data, box, x, y, scaleX, scaleY, rotation, skew, clipSize) {
989
1019
  const transform = get$3();
990
- translate$1(transform, box.x + x, box.y + y);
991
- if (scaleX)
992
- scaleHelper(transform, scaleX, scaleY);
993
1020
  if (rotation)
994
1021
  rotate(transform, rotation);
1022
+ if (skew)
1023
+ skewHelper(transform, skew.x, skew.y);
1024
+ if (scaleX)
1025
+ scaleHelper(transform, scaleX, scaleY);
1026
+ translate$1(transform, box.x + x, box.y + y);
1027
+ if (clipSize) {
1028
+ tempMatrix.a = box.width / clipSize.width, tempMatrix.d = box.height / clipSize.height;
1029
+ multiplyParent(transform, tempMatrix);
1030
+ }
995
1031
  data.transform = transform;
996
1032
  }
997
1033
  function repeatMode(data, box, width, height, x, y, scaleX, scaleY, rotation, align) {
@@ -1028,11 +1064,15 @@ const tempBox = new Bounds();
1028
1064
  const tempScaleData = {};
1029
1065
  const tempImage = {};
1030
1066
  function createData(leafPaint, image, paint, box) {
1031
- const { changeful, sync } = paint;
1067
+ const { changeful, sync, editing, scaleFixed } = paint;
1032
1068
  if (changeful)
1033
1069
  leafPaint.changeful = changeful;
1034
1070
  if (sync)
1035
1071
  leafPaint.sync = sync;
1072
+ if (editing)
1073
+ leafPaint.editing = editing;
1074
+ if (scaleFixed)
1075
+ leafPaint.scaleFixed = scaleFixed;
1036
1076
  leafPaint.data = getPatternData(paint, box, image);
1037
1077
  }
1038
1078
  function getPatternData(paint, box, image) {
@@ -1041,7 +1081,7 @@ function getPatternData(paint, box, image) {
1041
1081
  if (paint.mode === 'strench')
1042
1082
  paint.mode = 'stretch';
1043
1083
  let { width, height } = image;
1044
- const { opacity, mode, align, offset, scale, size, rotation, repeat, filters } = paint;
1084
+ const { opacity, mode, align, offset, scale, size, rotation, skew, clipSize, repeat, filters } = paint;
1045
1085
  const sameBox = box.width === width && box.height === height;
1046
1086
  const data = { mode };
1047
1087
  const swapSize = align !== 'center' && (rotation || 0) % 180 === 90;
@@ -1075,8 +1115,8 @@ function getPatternData(paint, box, image) {
1075
1115
  break;
1076
1116
  case 'normal':
1077
1117
  case 'clip':
1078
- if (tempImage.x || tempImage.y || scaleX || rotation)
1079
- clipMode(data, box, tempImage.x, tempImage.y, scaleX, scaleY, rotation);
1118
+ if (tempImage.x || tempImage.y || scaleX || clipSize || rotation || skew)
1119
+ clipMode(data, box, tempImage.x, tempImage.y, scaleX, scaleY, rotation, skew, paint.clipSize);
1080
1120
  break;
1081
1121
  case 'repeat':
1082
1122
  if (!sameBox || scaleX || rotation)
@@ -1153,11 +1193,11 @@ function image(ui, attrName, paint, boxBounds, firstUse) {
1153
1193
  }
1154
1194
  onLoadSuccess(ui, event);
1155
1195
  }
1156
- leafPaint.loadId = null;
1196
+ leafPaint.loadId = undefined;
1157
1197
  }, (error) => {
1158
1198
  ignoreRender(ui, false);
1159
1199
  onLoadError(ui, event, error);
1160
- leafPaint.loadId = null;
1200
+ leafPaint.loadId = undefined;
1161
1201
  });
1162
1202
  if (ui.placeholderColor) {
1163
1203
  if (!ui.placeholderDelay)
@@ -1213,16 +1253,16 @@ function ignoreRender(ui, value) {
1213
1253
  }
1214
1254
 
1215
1255
  const { get: get$1, scale, copy: copy$1 } = MatrixHelper;
1216
- const { ceil, abs: abs$1 } = Math;
1256
+ const { ceil, abs } = Math;
1217
1257
  function createPattern(ui, paint, pixelRatio) {
1218
- let { scaleX, scaleY } = ImageManager.patternLocked ? ui.__world : ui.__nowWorld;
1258
+ let { scaleX, scaleY } = ui.getRenderScaleData(true, paint.scaleFixed);
1219
1259
  const id = scaleX + '-' + scaleY + '-' + pixelRatio;
1220
1260
  if (paint.patternId !== id && !ui.destroyed) {
1221
- scaleX = abs$1(scaleX);
1222
- scaleY = abs$1(scaleY);
1223
1261
  const { image, data } = paint;
1224
1262
  let imageScale, imageMatrix, { width, height, scaleX: sx, scaleY: sy, transform, repeat } = data;
1225
1263
  if (sx) {
1264
+ sx = abs(sx);
1265
+ sy = abs(sy);
1226
1266
  imageMatrix = get$1();
1227
1267
  copy$1(imageMatrix, transform);
1228
1268
  scale(imageMatrix, 1 / sx, 1 / sy);
@@ -1275,9 +1315,8 @@ function createPattern(ui, paint, pixelRatio) {
1275
1315
  }
1276
1316
  }
1277
1317
 
1278
- const { abs } = Math;
1279
1318
  function checkImage(ui, canvas, paint, allowDraw) {
1280
- const { scaleX, scaleY } = ImageManager.patternLocked ? ui.__world : ui.__nowWorld;
1319
+ const { scaleX, scaleY } = ui.getRenderScaleData(true, paint.scaleFixed);
1281
1320
  const { pixelRatio } = canvas, { data } = paint;
1282
1321
  if (!data || (paint.patternId === scaleX + '-' + scaleY + '-' + pixelRatio && !Export.running)) {
1283
1322
  return false;
@@ -1290,8 +1329,8 @@ function checkImage(ui, canvas, paint, allowDraw) {
1290
1329
  else {
1291
1330
  if (!(paint.changeful || ResizeEvent.isResizing(ui) || Export.running)) {
1292
1331
  let { width, height } = data;
1293
- width *= abs(scaleX) * pixelRatio;
1294
- height *= abs(scaleY) * pixelRatio;
1332
+ width *= scaleX * pixelRatio;
1333
+ height *= scaleY * pixelRatio;
1295
1334
  if (data.scaleX) {
1296
1335
  width *= data.scaleX;
1297
1336
  height *= data.scaleY;
@@ -1301,6 +1340,10 @@ function checkImage(ui, canvas, paint, allowDraw) {
1301
1340
  }
1302
1341
  }
1303
1342
  if (allowDraw) {
1343
+ if (ui.__.__isFastShadow) {
1344
+ canvas.fillStyle = paint.style || '#000';
1345
+ canvas.fill();
1346
+ }
1304
1347
  drawImage(ui, canvas, paint, data);
1305
1348
  return true;
1306
1349
  }
@@ -1489,10 +1532,7 @@ function shadow(ui, current, shape) {
1489
1532
  }
1490
1533
  worldCanvas ? other.copyWorld(worldCanvas, nowWorld, nowWorld, 'destination-out') : other.copyWorld(shape.canvas, shapeBounds, bounds, 'destination-out');
1491
1534
  }
1492
- if (ui.__worldFlipped)
1493
- current.copyWorldByReset(other, copyBounds, nowWorld, item.blendMode);
1494
- else
1495
- current.copyWorldToInner(other, copyBounds, __layout.renderBounds, item.blendMode);
1535
+ LeafHelper.copyCanvasByWorld(ui, current, other, copyBounds, item.blendMode);
1496
1536
  if (end && index < end)
1497
1537
  other.clearWorld(copyBounds, true);
1498
1538
  });
@@ -1551,10 +1591,7 @@ function innerShadow(ui, current, shape) {
1551
1591
  copyBounds = bounds;
1552
1592
  }
1553
1593
  other.fillWorld(copyBounds, ColorConvert.string(item.color), 'source-in');
1554
- if (ui.__worldFlipped)
1555
- current.copyWorldByReset(other, copyBounds, nowWorld, item.blendMode);
1556
- else
1557
- current.copyWorldToInner(other, copyBounds, __layout.renderBounds, item.blendMode);
1594
+ LeafHelper.copyCanvasByWorld(ui, current, other, copyBounds, item.blendMode);
1558
1595
  if (end && index < end)
1559
1596
  other.clearWorld(copyBounds, true);
1560
1597
  });
@@ -1610,12 +1647,11 @@ Group.prototype.__renderMask = function (canvas, options) {
1610
1647
  contentCanvas = getCanvas(canvas);
1611
1648
  child.__render(maskCanvas, options);
1612
1649
  }
1613
- if (!(mask === 'clipping' || mask === 'clipping-path'))
1614
- continue;
1615
- }
1616
- if (excludeRenderBounds(child, options))
1650
+ if (mask === 'clipping' || mask === 'clipping-path')
1651
+ excludeRenderBounds(child, options) || child.__render(canvas, options);
1617
1652
  continue;
1618
- child.__render(contentCanvas || canvas, options);
1653
+ }
1654
+ excludeRenderBounds(child, options) || child.__render(contentCanvas || canvas, options);
1619
1655
  }
1620
1656
  maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity);
1621
1657
  };
@@ -2209,123 +2245,131 @@ function getTrimBounds(canvas) {
2209
2245
  index++;
2210
2246
  }
2211
2247
  const bounds = new Bounds$1();
2212
- toBounds(pointBounds, bounds);
2213
- return bounds.scale(1 / canvas.pixelRatio).ceil();
2248
+ if (pointBounds) {
2249
+ toBounds(pointBounds, bounds);
2250
+ bounds.scale(1 / canvas.pixelRatio).ceil();
2251
+ }
2252
+ return bounds;
2214
2253
  }
2215
2254
 
2216
2255
  const ExportModule = {
2217
2256
  syncExport(leaf, filename, options) {
2218
- this.running = true;
2257
+ Export.running = true;
2219
2258
  let result;
2220
- const fileType = FileHelper$1.fileType(filename);
2221
- const isDownload = filename.includes('.');
2222
- options = FileHelper$1.getExportOptions(options);
2223
- const { toURL } = Platform$1;
2224
- const { download } = Platform$1.origin;
2225
- if (fileType === 'json') {
2226
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2227
- result = { data: isDownload ? true : leaf.toJSON(options.json) };
2228
- }
2229
- else if (fileType === 'svg') {
2230
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2231
- result = { data: isDownload ? true : leaf.toSVG() };
2232
- }
2233
- else {
2234
- let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2235
- const { worldTransform, isLeafer, leafer, isFrame } = leaf;
2236
- const { slice, clip, trim, screenshot, padding, onCanvas } = options;
2237
- const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
2238
- const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
2239
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2240
- const needFill = FileHelper$1.isOpaqueImage(filename) || fill, matrix = new Matrix();
2241
- if (screenshot) {
2242
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2243
- }
2244
- else {
2245
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
2246
- scaleX = worldTransform.scaleX;
2247
- scaleY = worldTransform.scaleY;
2248
- switch (relative) {
2249
- case 'inner':
2250
- matrix.set(worldTransform);
2251
- break;
2252
- case 'local':
2253
- matrix.set(worldTransform).divide(leaf.localTransform);
2254
- scaleX /= leaf.scaleX;
2255
- scaleY /= leaf.scaleY;
2256
- break;
2257
- case 'world':
2258
- scaleX = 1;
2259
- scaleY = 1;
2260
- break;
2261
- case 'page':
2262
- relative = leafer || leaf;
2263
- default:
2264
- matrix.set(worldTransform).divide(leaf.getTransform(relative));
2265
- const l = relative.worldTransform;
2266
- scaleX /= scaleX / l.scaleX;
2267
- scaleY /= scaleY / l.scaleY;
2268
- }
2269
- renderBounds = leaf.getBounds('render', relative);
2259
+ try {
2260
+ const fileType = FileHelper$1.fileType(filename);
2261
+ const isDownload = filename.includes('.');
2262
+ options = FileHelper$1.getExportOptions(options);
2263
+ const { toURL } = Platform$1;
2264
+ const { download } = Platform$1.origin;
2265
+ if (fileType === 'json') {
2266
+ isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2267
+ result = { data: isDownload ? true : leaf.toJSON(options.json) };
2270
2268
  }
2271
- const scaleData = { scaleX: 1, scaleY: 1 };
2272
- MathHelper$1.getScaleData(options.scale, options.size, renderBounds, scaleData);
2273
- let pixelRatio = options.pixelRatio || 1;
2274
- let { x, y, width, height } = new Bounds$1(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2275
- if (clip)
2276
- x += clip.x, y += clip.y, width = clip.width, height = clip.height;
2277
- const renderOptions = { exporting: true, matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
2278
- let canvas = Creator$1.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
2279
- let sliceLeaf;
2280
- if (slice) {
2281
- sliceLeaf = leaf;
2282
- sliceLeaf.__worldOpacity = 0;
2283
- leaf = leafer || leaf;
2284
- renderOptions.bounds = canvas.bounds;
2285
- }
2286
- canvas.save();
2287
- if (isFrame && fill !== undefined) {
2288
- const oldFill = leaf.get('fill');
2289
- leaf.fill = '';
2290
- leaf.__render(canvas, renderOptions);
2291
- leaf.fill = oldFill;
2269
+ else if (fileType === 'svg') {
2270
+ isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2271
+ result = { data: isDownload ? true : leaf.toSVG() };
2292
2272
  }
2293
2273
  else {
2294
- leaf.__render(canvas, renderOptions);
2295
- }
2296
- canvas.restore();
2297
- if (sliceLeaf)
2298
- sliceLeaf.__updateWorldOpacity();
2299
- if (trim) {
2300
- trimBounds = getTrimBounds(canvas);
2301
- const old = canvas, { width, height } = trimBounds;
2302
- const config = { x: 0, y: 0, width, height, pixelRatio };
2303
- canvas = Creator$1.canvas(config);
2304
- canvas.copyWorld(old, trimBounds, config);
2305
- }
2306
- if (padding) {
2307
- const [top, right, bottom, left] = MathHelper$1.fourNumber(padding);
2308
- const old = canvas, { width, height } = old;
2309
- canvas = Creator$1.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
2310
- canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
2274
+ let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2275
+ const { worldTransform, isLeafer, leafer, isFrame } = leaf;
2276
+ const { slice, clip, trim, screenshot, padding, onCanvas } = options;
2277
+ const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
2278
+ const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
2279
+ const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2280
+ const needFill = FileHelper$1.isOpaqueImage(filename) || fill, matrix = new Matrix();
2281
+ if (screenshot) {
2282
+ renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2283
+ }
2284
+ else {
2285
+ let relative = options.relative || (isLeafer ? 'inner' : 'local');
2286
+ scaleX = worldTransform.scaleX;
2287
+ scaleY = worldTransform.scaleY;
2288
+ switch (relative) {
2289
+ case 'inner':
2290
+ matrix.set(worldTransform);
2291
+ break;
2292
+ case 'local':
2293
+ matrix.set(worldTransform).divide(leaf.localTransform);
2294
+ scaleX /= leaf.scaleX;
2295
+ scaleY /= leaf.scaleY;
2296
+ break;
2297
+ case 'world':
2298
+ scaleX = 1;
2299
+ scaleY = 1;
2300
+ break;
2301
+ case 'page':
2302
+ relative = leafer || leaf;
2303
+ default:
2304
+ matrix.set(worldTransform).divide(leaf.getTransform(relative));
2305
+ const l = relative.worldTransform;
2306
+ scaleX /= scaleX / l.scaleX;
2307
+ scaleY /= scaleY / l.scaleY;
2308
+ }
2309
+ renderBounds = leaf.getBounds('render', relative);
2310
+ }
2311
+ const scaleData = { scaleX: 1, scaleY: 1 };
2312
+ MathHelper$1.getScaleData(options.scale, options.size, renderBounds, scaleData);
2313
+ let pixelRatio = options.pixelRatio || 1;
2314
+ let { x, y, width, height } = new Bounds$1(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2315
+ if (clip)
2316
+ x += clip.x, y += clip.y, width = clip.width, height = clip.height;
2317
+ const renderOptions = { exporting: true, matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
2318
+ let canvas = Creator$1.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
2319
+ let sliceLeaf;
2320
+ if (slice) {
2321
+ sliceLeaf = leaf;
2322
+ sliceLeaf.__worldOpacity = 0;
2323
+ leaf = leafer || leaf;
2324
+ renderOptions.bounds = canvas.bounds;
2325
+ }
2326
+ canvas.save();
2327
+ if (isFrame && fill !== undefined) {
2328
+ const oldFill = leaf.get('fill');
2329
+ leaf.fill = '';
2330
+ leaf.__render(canvas, renderOptions);
2331
+ leaf.fill = oldFill;
2332
+ }
2333
+ else {
2334
+ leaf.__render(canvas, renderOptions);
2335
+ }
2336
+ canvas.restore();
2337
+ if (sliceLeaf)
2338
+ sliceLeaf.__updateWorldOpacity();
2339
+ if (trim) {
2340
+ trimBounds = getTrimBounds(canvas);
2341
+ const old = canvas, { width, height } = trimBounds;
2342
+ const config = { x: 0, y: 0, width, height, pixelRatio };
2343
+ canvas = Creator$1.canvas(config);
2344
+ canvas.copyWorld(old, trimBounds, config);
2345
+ }
2346
+ if (padding) {
2347
+ const [top, right, bottom, left] = MathHelper$1.fourNumber(padding);
2348
+ const old = canvas, { width, height } = old;
2349
+ canvas = Creator$1.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
2350
+ canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
2351
+ }
2352
+ if (needFill)
2353
+ canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2354
+ if (onCanvas)
2355
+ onCanvas(canvas);
2356
+ const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
2357
+ result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
2311
2358
  }
2312
- if (needFill)
2313
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2314
- if (onCanvas)
2315
- onCanvas(canvas);
2316
- const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
2317
- result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
2318
2359
  }
2319
- this.running = false;
2360
+ catch (error) {
2361
+ result = { data: '', error };
2362
+ }
2363
+ Export.running = false;
2320
2364
  return result;
2321
2365
  },
2322
2366
  export(leaf, filename, options) {
2323
- this.running = true;
2367
+ Export.running = true;
2324
2368
  return addTask((success) => new Promise((resolve) => {
2325
2369
  const getResult = () => __awaiter(this, void 0, void 0, function* () {
2326
2370
  if (!Resource.isComplete)
2327
2371
  return Platform$1.requestRender(getResult);
2328
- const result = ExportModule.syncExport(leaf, filename, options);
2372
+ const result = Export.syncExport(leaf, filename, options);
2329
2373
  if (result.data instanceof Promise)
2330
2374
  result.data = yield result.data;
2331
2375
  success(result);