@leafer-ui/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, Plugin, 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, Plugin, 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';
@@ -869,9 +869,14 @@ function fills(fills, ui, canvas) {
869
869
  }
870
870
  }
871
871
  canvas.fillStyle = item.style;
872
- if (item.transform) {
872
+ if (item.transform || item.scaleFixed) {
873
873
  canvas.save();
874
- canvas.transform(item.transform);
874
+ if (item.transform)
875
+ canvas.transform(item.transform);
876
+ if (item.scaleFixed) {
877
+ const { scaleX, scaleY } = ui.getRenderScaleData(true);
878
+ canvas.scale(1 / scaleX, 1 / scaleY);
879
+ }
875
880
  if (item.blendMode)
876
881
  canvas.blendMode = item.blendMode;
877
882
  fillPathOrText(ui, canvas);
@@ -907,8 +912,13 @@ function strokeText(stroke, ui, canvas) {
907
912
  }
908
913
  function drawCenter$1(stroke, strokeWidthScale, ui, canvas) {
909
914
  const data = ui.__;
910
- canvas.setStroke(!data.__isStrokes && stroke, data.strokeWidth * strokeWidthScale, data);
911
- data.__isStrokes ? drawStrokesStyle(stroke, true, ui, canvas) : drawTextStroke(ui, canvas);
915
+ if (typeof stroke === 'object') {
916
+ drawStrokesStyle(stroke, strokeWidthScale, true, ui, canvas);
917
+ }
918
+ else {
919
+ canvas.setStroke(stroke, data.__strokeWidth * strokeWidthScale, data);
920
+ drawTextStroke(ui, canvas);
921
+ }
912
922
  }
913
923
  function drawAlign(stroke, align, ui, canvas) {
914
924
  const out = canvas.getSameCanvas(true, true);
@@ -917,15 +927,9 @@ function drawAlign(stroke, align, ui, canvas) {
917
927
  out.blendMode = align === 'outside' ? 'destination-out' : 'destination-in';
918
928
  fillText(ui, out);
919
929
  out.blendMode = 'normal';
920
- copyWorld(canvas, out, ui);
930
+ LeafHelper.copyCanvasByWorld(ui, canvas, out);
921
931
  out.recycle(ui.__nowWorld);
922
932
  }
923
- function copyWorld(canvas, out, ui) {
924
- if (ui.__worldFlipped || Platform.fullImageShadow)
925
- canvas.copyWorldByReset(out, ui.__nowWorld);
926
- else
927
- canvas.copyWorldToInner(out, ui.__nowWorld, ui.__layout.renderBounds);
928
- }
929
933
  function drawTextStroke(ui, canvas) {
930
934
  let row, data = ui.__.__textDrawData;
931
935
  const { rows, decorationY } = data;
@@ -941,14 +945,21 @@ function drawTextStroke(ui, canvas) {
941
945
  rows.forEach(row => decorationY.forEach(value => canvas.strokeRect(row.x, row.y + value, row.width, decorationHeight)));
942
946
  }
943
947
  }
944
- function drawStrokesStyle(strokes, isText, ui, canvas) {
948
+ function drawStrokesStyle(strokes, strokeWidthScale, isText, ui, canvas) {
945
949
  let item;
950
+ const data = ui.__, { __hasMultiStrokeStyle } = data;
951
+ __hasMultiStrokeStyle || canvas.setStroke(undefined, data.__strokeWidth * strokeWidthScale, data);
946
952
  for (let i = 0, len = strokes.length; i < len; i++) {
947
953
  item = strokes[i];
948
954
  if (item.image && PaintImage.checkImage(ui, canvas, item, false))
949
955
  continue;
950
956
  if (item.style) {
951
- canvas.strokeStyle = item.style;
957
+ if (__hasMultiStrokeStyle) {
958
+ const { strokeStyle } = item;
959
+ strokeStyle ? canvas.setStroke(item.style, data.__getRealStrokeWidth(strokeStyle) * strokeWidthScale, data, strokeStyle) : canvas.setStroke(item.style, data.__strokeWidth * strokeWidthScale, data);
960
+ }
961
+ else
962
+ canvas.strokeStyle = item.style;
952
963
  if (item.blendMode) {
953
964
  canvas.saveBlendMode(item.blendMode);
954
965
  isText ? drawTextStroke(ui, canvas) : canvas.stroke();
@@ -987,8 +998,13 @@ function strokes(strokes, ui, canvas) {
987
998
  }
988
999
  function drawCenter(stroke, strokeWidthScale, ui, canvas) {
989
1000
  const data = ui.__;
990
- canvas.setStroke(!data.__isStrokes && stroke, data.__strokeWidth * strokeWidthScale, data);
991
- data.__isStrokes ? drawStrokesStyle(stroke, false, ui, canvas) : canvas.stroke();
1001
+ if (typeof stroke === 'object') {
1002
+ drawStrokesStyle(stroke, strokeWidthScale, false, ui, canvas);
1003
+ }
1004
+ else {
1005
+ canvas.setStroke(stroke, data.__strokeWidth * strokeWidthScale, data);
1006
+ canvas.stroke();
1007
+ }
992
1008
  if (data.__useArrow)
993
1009
  Paint.strokeArrow(stroke, ui, canvas);
994
1010
  }
@@ -1010,7 +1026,7 @@ function drawOutside(stroke, ui, canvas) {
1010
1026
  drawCenter(stroke, 2, ui, out);
1011
1027
  out.clipUI(data);
1012
1028
  out.clearWorld(renderBounds);
1013
- copyWorld(canvas, out, ui);
1029
+ LeafHelper.copyCanvasByWorld(ui, canvas, out);
1014
1030
  out.recycle(ui.__nowWorld);
1015
1031
  }
1016
1032
  }
@@ -1065,8 +1081,16 @@ function compute(attrName, ui) {
1065
1081
  if (!(paints instanceof Array))
1066
1082
  paints = [paints];
1067
1083
  recycleMap = PaintImage.recycleImage(attrName, data);
1084
+ let maxChildStrokeWidth;
1068
1085
  for (let i = 0, len = paints.length, item; i < len; i++) {
1069
- (item = getLeafPaint(attrName, paints[i], ui)) && leafPaints.push(item);
1086
+ if (item = getLeafPaint(attrName, paints[i], ui)) {
1087
+ leafPaints.push(item);
1088
+ if (item.strokeStyle) {
1089
+ maxChildStrokeWidth || (maxChildStrokeWidth = 1);
1090
+ if (item.strokeStyle.strokeWidth)
1091
+ maxChildStrokeWidth = Math.max(maxChildStrokeWidth, item.strokeStyle.strokeWidth);
1092
+ }
1093
+ }
1070
1094
  }
1071
1095
  data['_' + attrName] = leafPaints.length ? leafPaints : undefined;
1072
1096
  if (leafPaints.length) {
@@ -1083,6 +1107,7 @@ function compute(attrName, ui) {
1083
1107
  else {
1084
1108
  stintSet(data, '__isAlphaPixelStroke', isAlphaPixel);
1085
1109
  stintSet(data, '__isTransparentStroke', isTransparent);
1110
+ stintSet(data, '__hasMultiStrokeStyle', maxChildStrokeWidth);
1086
1111
  }
1087
1112
  }
1088
1113
  function getLeafPaint(attrName, paint, ui) {
@@ -1114,6 +1139,11 @@ function getLeafPaint(attrName, paint, ui) {
1114
1139
  if (data) {
1115
1140
  if (typeof data.style === 'string' && hasTransparent$1(data.style))
1116
1141
  data.isTransparent = true;
1142
+ if (paint.style) {
1143
+ if (paint.style.strokeWidth === 0)
1144
+ return undefined;
1145
+ data.strokeStyle = paint.style;
1146
+ }
1117
1147
  if (paint.blendMode)
1118
1148
  data.blendMode = paint.blendMode;
1119
1149
  }
@@ -1133,8 +1163,8 @@ const PaintModule = {
1133
1163
  shape
1134
1164
  };
1135
1165
 
1136
- let origin = {};
1137
- const { get: get$3, rotateOfOuter: rotateOfOuter$1, translate: translate$1, scaleOfOuter: scaleOfOuter$1, scale: scaleHelper, rotate } = MatrixHelper;
1166
+ let origin = {}, tempMatrix = getMatrixData();
1167
+ const { get: get$3, rotateOfOuter: rotateOfOuter$1, translate: translate$1, scaleOfOuter: scaleOfOuter$1, multiplyParent, scale: scaleHelper, rotate, skew: skewHelper } = MatrixHelper;
1138
1168
  function fillOrFitMode(data, box, x, y, scaleX, scaleY, rotation) {
1139
1169
  const transform = get$3();
1140
1170
  translate$1(transform, box.x + x, box.y + y);
@@ -1143,13 +1173,19 @@ function fillOrFitMode(data, box, x, y, scaleX, scaleY, rotation) {
1143
1173
  rotateOfOuter$1(transform, { x: box.x + box.width / 2, y: box.y + box.height / 2 }, rotation);
1144
1174
  data.transform = transform;
1145
1175
  }
1146
- function clipMode(data, box, x, y, scaleX, scaleY, rotation) {
1176
+ function clipMode(data, box, x, y, scaleX, scaleY, rotation, skew, clipSize) {
1147
1177
  const transform = get$3();
1148
- translate$1(transform, box.x + x, box.y + y);
1149
- if (scaleX)
1150
- scaleHelper(transform, scaleX, scaleY);
1151
1178
  if (rotation)
1152
1179
  rotate(transform, rotation);
1180
+ if (skew)
1181
+ skewHelper(transform, skew.x, skew.y);
1182
+ if (scaleX)
1183
+ scaleHelper(transform, scaleX, scaleY);
1184
+ translate$1(transform, box.x + x, box.y + y);
1185
+ if (clipSize) {
1186
+ tempMatrix.a = box.width / clipSize.width, tempMatrix.d = box.height / clipSize.height;
1187
+ multiplyParent(transform, tempMatrix);
1188
+ }
1153
1189
  data.transform = transform;
1154
1190
  }
1155
1191
  function repeatMode(data, box, width, height, x, y, scaleX, scaleY, rotation, align) {
@@ -1186,11 +1222,15 @@ const tempBox = new Bounds();
1186
1222
  const tempScaleData = {};
1187
1223
  const tempImage = {};
1188
1224
  function createData(leafPaint, image, paint, box) {
1189
- const { changeful, sync } = paint;
1225
+ const { changeful, sync, editing, scaleFixed } = paint;
1190
1226
  if (changeful)
1191
1227
  leafPaint.changeful = changeful;
1192
1228
  if (sync)
1193
1229
  leafPaint.sync = sync;
1230
+ if (editing)
1231
+ leafPaint.editing = editing;
1232
+ if (scaleFixed)
1233
+ leafPaint.scaleFixed = scaleFixed;
1194
1234
  leafPaint.data = getPatternData(paint, box, image);
1195
1235
  }
1196
1236
  function getPatternData(paint, box, image) {
@@ -1199,7 +1239,7 @@ function getPatternData(paint, box, image) {
1199
1239
  if (paint.mode === 'strench')
1200
1240
  paint.mode = 'stretch';
1201
1241
  let { width, height } = image;
1202
- const { opacity, mode, align, offset, scale, size, rotation, repeat, filters } = paint;
1242
+ const { opacity, mode, align, offset, scale, size, rotation, skew, clipSize, repeat, filters } = paint;
1203
1243
  const sameBox = box.width === width && box.height === height;
1204
1244
  const data = { mode };
1205
1245
  const swapSize = align !== 'center' && (rotation || 0) % 180 === 90;
@@ -1233,8 +1273,8 @@ function getPatternData(paint, box, image) {
1233
1273
  break;
1234
1274
  case 'normal':
1235
1275
  case 'clip':
1236
- if (tempImage.x || tempImage.y || scaleX || rotation)
1237
- clipMode(data, box, tempImage.x, tempImage.y, scaleX, scaleY, rotation);
1276
+ if (tempImage.x || tempImage.y || scaleX || clipSize || rotation || skew)
1277
+ clipMode(data, box, tempImage.x, tempImage.y, scaleX, scaleY, rotation, skew, paint.clipSize);
1238
1278
  break;
1239
1279
  case 'repeat':
1240
1280
  if (!sameBox || scaleX || rotation)
@@ -1311,11 +1351,11 @@ function image(ui, attrName, paint, boxBounds, firstUse) {
1311
1351
  }
1312
1352
  onLoadSuccess(ui, event);
1313
1353
  }
1314
- leafPaint.loadId = null;
1354
+ leafPaint.loadId = undefined;
1315
1355
  }, (error) => {
1316
1356
  ignoreRender(ui, false);
1317
1357
  onLoadError(ui, event, error);
1318
- leafPaint.loadId = null;
1358
+ leafPaint.loadId = undefined;
1319
1359
  });
1320
1360
  if (ui.placeholderColor) {
1321
1361
  if (!ui.placeholderDelay)
@@ -1371,16 +1411,16 @@ function ignoreRender(ui, value) {
1371
1411
  }
1372
1412
 
1373
1413
  const { get: get$1, scale, copy: copy$1 } = MatrixHelper;
1374
- const { ceil, abs: abs$1 } = Math;
1414
+ const { ceil, abs } = Math;
1375
1415
  function createPattern(ui, paint, pixelRatio) {
1376
- let { scaleX, scaleY } = ImageManager.patternLocked ? ui.__world : ui.__nowWorld;
1416
+ let { scaleX, scaleY } = ui.getRenderScaleData(true, paint.scaleFixed);
1377
1417
  const id = scaleX + '-' + scaleY + '-' + pixelRatio;
1378
1418
  if (paint.patternId !== id && !ui.destroyed) {
1379
- scaleX = abs$1(scaleX);
1380
- scaleY = abs$1(scaleY);
1381
1419
  const { image, data } = paint;
1382
1420
  let imageScale, imageMatrix, { width, height, scaleX: sx, scaleY: sy, transform, repeat } = data;
1383
1421
  if (sx) {
1422
+ sx = abs(sx);
1423
+ sy = abs(sy);
1384
1424
  imageMatrix = get$1();
1385
1425
  copy$1(imageMatrix, transform);
1386
1426
  scale(imageMatrix, 1 / sx, 1 / sy);
@@ -1433,9 +1473,8 @@ function createPattern(ui, paint, pixelRatio) {
1433
1473
  }
1434
1474
  }
1435
1475
 
1436
- const { abs } = Math;
1437
1476
  function checkImage(ui, canvas, paint, allowDraw) {
1438
- const { scaleX, scaleY } = ImageManager.patternLocked ? ui.__world : ui.__nowWorld;
1477
+ const { scaleX, scaleY } = ui.getRenderScaleData(true, paint.scaleFixed);
1439
1478
  const { pixelRatio } = canvas, { data } = paint;
1440
1479
  if (!data || (paint.patternId === scaleX + '-' + scaleY + '-' + pixelRatio && !Export.running)) {
1441
1480
  return false;
@@ -1448,8 +1487,8 @@ function checkImage(ui, canvas, paint, allowDraw) {
1448
1487
  else {
1449
1488
  if (!(paint.changeful || ResizeEvent.isResizing(ui) || Export.running)) {
1450
1489
  let { width, height } = data;
1451
- width *= abs(scaleX) * pixelRatio;
1452
- height *= abs(scaleY) * pixelRatio;
1490
+ width *= scaleX * pixelRatio;
1491
+ height *= scaleY * pixelRatio;
1453
1492
  if (data.scaleX) {
1454
1493
  width *= data.scaleX;
1455
1494
  height *= data.scaleY;
@@ -1459,6 +1498,10 @@ function checkImage(ui, canvas, paint, allowDraw) {
1459
1498
  }
1460
1499
  }
1461
1500
  if (allowDraw) {
1501
+ if (ui.__.__isFastShadow) {
1502
+ canvas.fillStyle = paint.style || '#000';
1503
+ canvas.fill();
1504
+ }
1462
1505
  drawImage(ui, canvas, paint, data);
1463
1506
  return true;
1464
1507
  }
@@ -1647,10 +1690,7 @@ function shadow(ui, current, shape) {
1647
1690
  }
1648
1691
  worldCanvas ? other.copyWorld(worldCanvas, nowWorld, nowWorld, 'destination-out') : other.copyWorld(shape.canvas, shapeBounds, bounds, 'destination-out');
1649
1692
  }
1650
- if (ui.__worldFlipped)
1651
- current.copyWorldByReset(other, copyBounds, nowWorld, item.blendMode);
1652
- else
1653
- current.copyWorldToInner(other, copyBounds, __layout.renderBounds, item.blendMode);
1693
+ LeafHelper.copyCanvasByWorld(ui, current, other, copyBounds, item.blendMode);
1654
1694
  if (end && index < end)
1655
1695
  other.clearWorld(copyBounds, true);
1656
1696
  });
@@ -1709,10 +1749,7 @@ function innerShadow(ui, current, shape) {
1709
1749
  copyBounds = bounds;
1710
1750
  }
1711
1751
  other.fillWorld(copyBounds, ColorConvert.string(item.color), 'source-in');
1712
- if (ui.__worldFlipped)
1713
- current.copyWorldByReset(other, copyBounds, nowWorld, item.blendMode);
1714
- else
1715
- current.copyWorldToInner(other, copyBounds, __layout.renderBounds, item.blendMode);
1752
+ LeafHelper.copyCanvasByWorld(ui, current, other, copyBounds, item.blendMode);
1716
1753
  if (end && index < end)
1717
1754
  other.clearWorld(copyBounds, true);
1718
1755
  });
@@ -1768,12 +1805,11 @@ Group.prototype.__renderMask = function (canvas, options) {
1768
1805
  contentCanvas = getCanvas(canvas);
1769
1806
  child.__render(maskCanvas, options);
1770
1807
  }
1771
- if (!(mask === 'clipping' || mask === 'clipping-path'))
1772
- continue;
1773
- }
1774
- if (excludeRenderBounds(child, options))
1808
+ if (mask === 'clipping' || mask === 'clipping-path')
1809
+ excludeRenderBounds(child, options) || child.__render(canvas, options);
1775
1810
  continue;
1776
- child.__render(contentCanvas || canvas, options);
1811
+ }
1812
+ excludeRenderBounds(child, options) || child.__render(contentCanvas || canvas, options);
1777
1813
  }
1778
1814
  maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity);
1779
1815
  };
@@ -2367,123 +2403,131 @@ function getTrimBounds(canvas) {
2367
2403
  index++;
2368
2404
  }
2369
2405
  const bounds = new Bounds$1();
2370
- toBounds(pointBounds, bounds);
2371
- return bounds.scale(1 / canvas.pixelRatio).ceil();
2406
+ if (pointBounds) {
2407
+ toBounds(pointBounds, bounds);
2408
+ bounds.scale(1 / canvas.pixelRatio).ceil();
2409
+ }
2410
+ return bounds;
2372
2411
  }
2373
2412
 
2374
2413
  const ExportModule = {
2375
2414
  syncExport(leaf, filename, options) {
2376
- this.running = true;
2415
+ Export.running = true;
2377
2416
  let result;
2378
- const fileType = FileHelper$1.fileType(filename);
2379
- const isDownload = filename.includes('.');
2380
- options = FileHelper$1.getExportOptions(options);
2381
- const { toURL } = Platform$1;
2382
- const { download } = Platform$1.origin;
2383
- if (fileType === 'json') {
2384
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2385
- result = { data: isDownload ? true : leaf.toJSON(options.json) };
2386
- }
2387
- else if (fileType === 'svg') {
2388
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2389
- result = { data: isDownload ? true : leaf.toSVG() };
2390
- }
2391
- else {
2392
- let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2393
- const { worldTransform, isLeafer, leafer, isFrame } = leaf;
2394
- const { slice, clip, trim, screenshot, padding, onCanvas } = options;
2395
- const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
2396
- const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
2397
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2398
- const needFill = FileHelper$1.isOpaqueImage(filename) || fill, matrix = new Matrix();
2399
- if (screenshot) {
2400
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2401
- }
2402
- else {
2403
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
2404
- scaleX = worldTransform.scaleX;
2405
- scaleY = worldTransform.scaleY;
2406
- switch (relative) {
2407
- case 'inner':
2408
- matrix.set(worldTransform);
2409
- break;
2410
- case 'local':
2411
- matrix.set(worldTransform).divide(leaf.localTransform);
2412
- scaleX /= leaf.scaleX;
2413
- scaleY /= leaf.scaleY;
2414
- break;
2415
- case 'world':
2416
- scaleX = 1;
2417
- scaleY = 1;
2418
- break;
2419
- case 'page':
2420
- relative = leafer || leaf;
2421
- default:
2422
- matrix.set(worldTransform).divide(leaf.getTransform(relative));
2423
- const l = relative.worldTransform;
2424
- scaleX /= scaleX / l.scaleX;
2425
- scaleY /= scaleY / l.scaleY;
2426
- }
2427
- renderBounds = leaf.getBounds('render', relative);
2417
+ try {
2418
+ const fileType = FileHelper$1.fileType(filename);
2419
+ const isDownload = filename.includes('.');
2420
+ options = FileHelper$1.getExportOptions(options);
2421
+ const { toURL } = Platform$1;
2422
+ const { download } = Platform$1.origin;
2423
+ if (fileType === 'json') {
2424
+ isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2425
+ result = { data: isDownload ? true : leaf.toJSON(options.json) };
2428
2426
  }
2429
- const scaleData = { scaleX: 1, scaleY: 1 };
2430
- MathHelper$1.getScaleData(options.scale, options.size, renderBounds, scaleData);
2431
- let pixelRatio = options.pixelRatio || 1;
2432
- let { x, y, width, height } = new Bounds$1(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2433
- if (clip)
2434
- x += clip.x, y += clip.y, width = clip.width, height = clip.height;
2435
- 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) };
2436
- let canvas = Creator$1.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
2437
- let sliceLeaf;
2438
- if (slice) {
2439
- sliceLeaf = leaf;
2440
- sliceLeaf.__worldOpacity = 0;
2441
- leaf = leafer || leaf;
2442
- renderOptions.bounds = canvas.bounds;
2443
- }
2444
- canvas.save();
2445
- if (isFrame && fill !== undefined) {
2446
- const oldFill = leaf.get('fill');
2447
- leaf.fill = '';
2448
- leaf.__render(canvas, renderOptions);
2449
- leaf.fill = oldFill;
2427
+ else if (fileType === 'svg') {
2428
+ isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2429
+ result = { data: isDownload ? true : leaf.toSVG() };
2450
2430
  }
2451
2431
  else {
2452
- leaf.__render(canvas, renderOptions);
2453
- }
2454
- canvas.restore();
2455
- if (sliceLeaf)
2456
- sliceLeaf.__updateWorldOpacity();
2457
- if (trim) {
2458
- trimBounds = getTrimBounds(canvas);
2459
- const old = canvas, { width, height } = trimBounds;
2460
- const config = { x: 0, y: 0, width, height, pixelRatio };
2461
- canvas = Creator$1.canvas(config);
2462
- canvas.copyWorld(old, trimBounds, config);
2463
- }
2464
- if (padding) {
2465
- const [top, right, bottom, left] = MathHelper$1.fourNumber(padding);
2466
- const old = canvas, { width, height } = old;
2467
- canvas = Creator$1.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
2468
- canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
2432
+ let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2433
+ const { worldTransform, isLeafer, leafer, isFrame } = leaf;
2434
+ const { slice, clip, trim, screenshot, padding, onCanvas } = options;
2435
+ const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
2436
+ const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
2437
+ const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2438
+ const needFill = FileHelper$1.isOpaqueImage(filename) || fill, matrix = new Matrix();
2439
+ if (screenshot) {
2440
+ renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2441
+ }
2442
+ else {
2443
+ let relative = options.relative || (isLeafer ? 'inner' : 'local');
2444
+ scaleX = worldTransform.scaleX;
2445
+ scaleY = worldTransform.scaleY;
2446
+ switch (relative) {
2447
+ case 'inner':
2448
+ matrix.set(worldTransform);
2449
+ break;
2450
+ case 'local':
2451
+ matrix.set(worldTransform).divide(leaf.localTransform);
2452
+ scaleX /= leaf.scaleX;
2453
+ scaleY /= leaf.scaleY;
2454
+ break;
2455
+ case 'world':
2456
+ scaleX = 1;
2457
+ scaleY = 1;
2458
+ break;
2459
+ case 'page':
2460
+ relative = leafer || leaf;
2461
+ default:
2462
+ matrix.set(worldTransform).divide(leaf.getTransform(relative));
2463
+ const l = relative.worldTransform;
2464
+ scaleX /= scaleX / l.scaleX;
2465
+ scaleY /= scaleY / l.scaleY;
2466
+ }
2467
+ renderBounds = leaf.getBounds('render', relative);
2468
+ }
2469
+ const scaleData = { scaleX: 1, scaleY: 1 };
2470
+ MathHelper$1.getScaleData(options.scale, options.size, renderBounds, scaleData);
2471
+ let pixelRatio = options.pixelRatio || 1;
2472
+ let { x, y, width, height } = new Bounds$1(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2473
+ if (clip)
2474
+ x += clip.x, y += clip.y, width = clip.width, height = clip.height;
2475
+ 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) };
2476
+ let canvas = Creator$1.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
2477
+ let sliceLeaf;
2478
+ if (slice) {
2479
+ sliceLeaf = leaf;
2480
+ sliceLeaf.__worldOpacity = 0;
2481
+ leaf = leafer || leaf;
2482
+ renderOptions.bounds = canvas.bounds;
2483
+ }
2484
+ canvas.save();
2485
+ if (isFrame && fill !== undefined) {
2486
+ const oldFill = leaf.get('fill');
2487
+ leaf.fill = '';
2488
+ leaf.__render(canvas, renderOptions);
2489
+ leaf.fill = oldFill;
2490
+ }
2491
+ else {
2492
+ leaf.__render(canvas, renderOptions);
2493
+ }
2494
+ canvas.restore();
2495
+ if (sliceLeaf)
2496
+ sliceLeaf.__updateWorldOpacity();
2497
+ if (trim) {
2498
+ trimBounds = getTrimBounds(canvas);
2499
+ const old = canvas, { width, height } = trimBounds;
2500
+ const config = { x: 0, y: 0, width, height, pixelRatio };
2501
+ canvas = Creator$1.canvas(config);
2502
+ canvas.copyWorld(old, trimBounds, config);
2503
+ }
2504
+ if (padding) {
2505
+ const [top, right, bottom, left] = MathHelper$1.fourNumber(padding);
2506
+ const old = canvas, { width, height } = old;
2507
+ canvas = Creator$1.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
2508
+ canvas.copyWorld(old, old.bounds, { x: left, y: top, width, height });
2509
+ }
2510
+ if (needFill)
2511
+ canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2512
+ if (onCanvas)
2513
+ onCanvas(canvas);
2514
+ const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
2515
+ result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
2469
2516
  }
2470
- if (needFill)
2471
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2472
- if (onCanvas)
2473
- onCanvas(canvas);
2474
- const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
2475
- result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
2476
2517
  }
2477
- this.running = false;
2518
+ catch (error) {
2519
+ result = { data: '', error };
2520
+ }
2521
+ Export.running = false;
2478
2522
  return result;
2479
2523
  },
2480
2524
  export(leaf, filename, options) {
2481
- this.running = true;
2525
+ Export.running = true;
2482
2526
  return addTask((success) => new Promise((resolve) => {
2483
2527
  const getResult = () => __awaiter(this, void 0, void 0, function* () {
2484
2528
  if (!Resource.isComplete)
2485
2529
  return Platform$1.requestRender(getResult);
2486
- const result = ExportModule.syncExport(leaf, filename, options);
2530
+ const result = Export.syncExport(leaf, filename, options);
2487
2531
  if (result.data instanceof Promise)
2488
2532
  result.data = yield result.data;
2489
2533
  success(result);