@leafer-draw/node 1.7.0 → 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, skew: skewHelper } = 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,7 +1015,7 @@ 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, skew) {
1018
+ function clipMode(data, box, x, y, scaleX, scaleY, rotation, skew, clipSize) {
989
1019
  const transform = get$3();
990
1020
  if (rotation)
991
1021
  rotate(transform, rotation);
@@ -994,6 +1024,10 @@ function clipMode(data, box, x, y, scaleX, scaleY, rotation, skew) {
994
1024
  if (scaleX)
995
1025
  scaleHelper(transform, scaleX, scaleY);
996
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
+ }
997
1031
  data.transform = transform;
998
1032
  }
999
1033
  function repeatMode(data, box, width, height, x, y, scaleX, scaleY, rotation, align) {
@@ -1030,13 +1064,15 @@ const tempBox = new Bounds();
1030
1064
  const tempScaleData = {};
1031
1065
  const tempImage = {};
1032
1066
  function createData(leafPaint, image, paint, box) {
1033
- const { changeful, sync, editing } = paint;
1067
+ const { changeful, sync, editing, scaleFixed } = paint;
1034
1068
  if (changeful)
1035
1069
  leafPaint.changeful = changeful;
1036
1070
  if (sync)
1037
1071
  leafPaint.sync = sync;
1038
1072
  if (editing)
1039
1073
  leafPaint.editing = editing;
1074
+ if (scaleFixed)
1075
+ leafPaint.scaleFixed = scaleFixed;
1040
1076
  leafPaint.data = getPatternData(paint, box, image);
1041
1077
  }
1042
1078
  function getPatternData(paint, box, image) {
@@ -1045,7 +1081,7 @@ function getPatternData(paint, box, image) {
1045
1081
  if (paint.mode === 'strench')
1046
1082
  paint.mode = 'stretch';
1047
1083
  let { width, height } = image;
1048
- const { opacity, mode, align, offset, scale, size, rotation, skew, repeat, filters } = paint;
1084
+ const { opacity, mode, align, offset, scale, size, rotation, skew, clipSize, repeat, filters } = paint;
1049
1085
  const sameBox = box.width === width && box.height === height;
1050
1086
  const data = { mode };
1051
1087
  const swapSize = align !== 'center' && (rotation || 0) % 180 === 90;
@@ -1079,8 +1115,8 @@ function getPatternData(paint, box, image) {
1079
1115
  break;
1080
1116
  case 'normal':
1081
1117
  case 'clip':
1082
- if (tempImage.x || tempImage.y || scaleX || rotation || skew)
1083
- clipMode(data, box, tempImage.x, tempImage.y, scaleX, scaleY, rotation, skew);
1118
+ if (tempImage.x || tempImage.y || scaleX || clipSize || rotation || skew)
1119
+ clipMode(data, box, tempImage.x, tempImage.y, scaleX, scaleY, rotation, skew, paint.clipSize);
1084
1120
  break;
1085
1121
  case 'repeat':
1086
1122
  if (!sameBox || scaleX || rotation)
@@ -1217,18 +1253,16 @@ function ignoreRender(ui, value) {
1217
1253
  }
1218
1254
 
1219
1255
  const { get: get$1, scale, copy: copy$1 } = MatrixHelper;
1220
- const { ceil, abs: abs$1 } = Math;
1256
+ const { ceil, abs } = Math;
1221
1257
  function createPattern(ui, paint, pixelRatio) {
1222
- let { scaleX, scaleY } = ImageManager.patternLocked ? ui.__world : ui.__nowWorld;
1258
+ let { scaleX, scaleY } = ui.getRenderScaleData(true, paint.scaleFixed);
1223
1259
  const id = scaleX + '-' + scaleY + '-' + pixelRatio;
1224
1260
  if (paint.patternId !== id && !ui.destroyed) {
1225
- scaleX = abs$1(scaleX);
1226
- scaleY = abs$1(scaleY);
1227
1261
  const { image, data } = paint;
1228
1262
  let imageScale, imageMatrix, { width, height, scaleX: sx, scaleY: sy, transform, repeat } = data;
1229
1263
  if (sx) {
1230
- sx = abs$1(sx);
1231
- sy = abs$1(sy);
1264
+ sx = abs(sx);
1265
+ sy = abs(sy);
1232
1266
  imageMatrix = get$1();
1233
1267
  copy$1(imageMatrix, transform);
1234
1268
  scale(imageMatrix, 1 / sx, 1 / sy);
@@ -1281,9 +1315,8 @@ function createPattern(ui, paint, pixelRatio) {
1281
1315
  }
1282
1316
  }
1283
1317
 
1284
- const { abs } = Math;
1285
1318
  function checkImage(ui, canvas, paint, allowDraw) {
1286
- const { scaleX, scaleY } = ImageManager.patternLocked ? ui.__world : ui.__nowWorld;
1319
+ const { scaleX, scaleY } = ui.getRenderScaleData(true, paint.scaleFixed);
1287
1320
  const { pixelRatio } = canvas, { data } = paint;
1288
1321
  if (!data || (paint.patternId === scaleX + '-' + scaleY + '-' + pixelRatio && !Export.running)) {
1289
1322
  return false;
@@ -1296,8 +1329,8 @@ function checkImage(ui, canvas, paint, allowDraw) {
1296
1329
  else {
1297
1330
  if (!(paint.changeful || ResizeEvent.isResizing(ui) || Export.running)) {
1298
1331
  let { width, height } = data;
1299
- width *= abs(scaleX) * pixelRatio;
1300
- height *= abs(scaleY) * pixelRatio;
1332
+ width *= scaleX * pixelRatio;
1333
+ height *= scaleY * pixelRatio;
1301
1334
  if (data.scaleX) {
1302
1335
  width *= data.scaleX;
1303
1336
  height *= data.scaleY;
@@ -1307,6 +1340,10 @@ function checkImage(ui, canvas, paint, allowDraw) {
1307
1340
  }
1308
1341
  }
1309
1342
  if (allowDraw) {
1343
+ if (ui.__.__isFastShadow) {
1344
+ canvas.fillStyle = paint.style || '#000';
1345
+ canvas.fill();
1346
+ }
1310
1347
  drawImage(ui, canvas, paint, data);
1311
1348
  return true;
1312
1349
  }
@@ -1495,10 +1532,7 @@ function shadow(ui, current, shape) {
1495
1532
  }
1496
1533
  worldCanvas ? other.copyWorld(worldCanvas, nowWorld, nowWorld, 'destination-out') : other.copyWorld(shape.canvas, shapeBounds, bounds, 'destination-out');
1497
1534
  }
1498
- if (ui.__worldFlipped)
1499
- current.copyWorldByReset(other, copyBounds, nowWorld, item.blendMode);
1500
- else
1501
- current.copyWorldToInner(other, copyBounds, __layout.renderBounds, item.blendMode);
1535
+ LeafHelper.copyCanvasByWorld(ui, current, other, copyBounds, item.blendMode);
1502
1536
  if (end && index < end)
1503
1537
  other.clearWorld(copyBounds, true);
1504
1538
  });
@@ -1557,10 +1591,7 @@ function innerShadow(ui, current, shape) {
1557
1591
  copyBounds = bounds;
1558
1592
  }
1559
1593
  other.fillWorld(copyBounds, ColorConvert.string(item.color), 'source-in');
1560
- if (ui.__worldFlipped)
1561
- current.copyWorldByReset(other, copyBounds, nowWorld, item.blendMode);
1562
- else
1563
- current.copyWorldToInner(other, copyBounds, __layout.renderBounds, item.blendMode);
1594
+ LeafHelper.copyCanvasByWorld(ui, current, other, copyBounds, item.blendMode);
1564
1595
  if (end && index < end)
1565
1596
  other.clearWorld(copyBounds, true);
1566
1597
  });
@@ -1616,12 +1647,11 @@ Group.prototype.__renderMask = function (canvas, options) {
1616
1647
  contentCanvas = getCanvas(canvas);
1617
1648
  child.__render(maskCanvas, options);
1618
1649
  }
1619
- if (!(mask === 'clipping' || mask === 'clipping-path'))
1620
- continue;
1621
- }
1622
- if (excludeRenderBounds(child, options))
1650
+ if (mask === 'clipping' || mask === 'clipping-path')
1651
+ excludeRenderBounds(child, options) || child.__render(canvas, options);
1623
1652
  continue;
1624
- child.__render(contentCanvas || canvas, options);
1653
+ }
1654
+ excludeRenderBounds(child, options) || child.__render(contentCanvas || canvas, options);
1625
1655
  }
1626
1656
  maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity);
1627
1657
  };
@@ -2215,123 +2245,131 @@ function getTrimBounds(canvas) {
2215
2245
  index++;
2216
2246
  }
2217
2247
  const bounds = new Bounds$1();
2218
- toBounds(pointBounds, bounds);
2219
- 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;
2220
2253
  }
2221
2254
 
2222
2255
  const ExportModule = {
2223
2256
  syncExport(leaf, filename, options) {
2224
- this.running = true;
2257
+ Export.running = true;
2225
2258
  let result;
2226
- const fileType = FileHelper$1.fileType(filename);
2227
- const isDownload = filename.includes('.');
2228
- options = FileHelper$1.getExportOptions(options);
2229
- const { toURL } = Platform$1;
2230
- const { download } = Platform$1.origin;
2231
- if (fileType === 'json') {
2232
- isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2233
- result = { data: isDownload ? true : leaf.toJSON(options.json) };
2234
- }
2235
- else if (fileType === 'svg') {
2236
- isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2237
- result = { data: isDownload ? true : leaf.toSVG() };
2238
- }
2239
- else {
2240
- let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2241
- const { worldTransform, isLeafer, leafer, isFrame } = leaf;
2242
- const { slice, clip, trim, screenshot, padding, onCanvas } = options;
2243
- const smooth = options.smooth === undefined ? (leafer ? leafer.config.smooth : true) : options.smooth;
2244
- const contextSettings = options.contextSettings || (leafer ? leafer.config.contextSettings : undefined);
2245
- const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2246
- const needFill = FileHelper$1.isOpaqueImage(filename) || fill, matrix = new Matrix();
2247
- if (screenshot) {
2248
- renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2249
- }
2250
- else {
2251
- let relative = options.relative || (isLeafer ? 'inner' : 'local');
2252
- scaleX = worldTransform.scaleX;
2253
- scaleY = worldTransform.scaleY;
2254
- switch (relative) {
2255
- case 'inner':
2256
- matrix.set(worldTransform);
2257
- break;
2258
- case 'local':
2259
- matrix.set(worldTransform).divide(leaf.localTransform);
2260
- scaleX /= leaf.scaleX;
2261
- scaleY /= leaf.scaleY;
2262
- break;
2263
- case 'world':
2264
- scaleX = 1;
2265
- scaleY = 1;
2266
- break;
2267
- case 'page':
2268
- relative = leafer || leaf;
2269
- default:
2270
- matrix.set(worldTransform).divide(leaf.getTransform(relative));
2271
- const l = relative.worldTransform;
2272
- scaleX /= scaleX / l.scaleX;
2273
- scaleY /= scaleY / l.scaleY;
2274
- }
2275
- renderBounds = leaf.getBounds('render', relative);
2276
- }
2277
- const scaleData = { scaleX: 1, scaleY: 1 };
2278
- MathHelper$1.getScaleData(options.scale, options.size, renderBounds, scaleData);
2279
- let pixelRatio = options.pixelRatio || 1;
2280
- let { x, y, width, height } = new Bounds$1(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2281
- if (clip)
2282
- x += clip.x, y += clip.y, width = clip.width, height = clip.height;
2283
- 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) };
2284
- let canvas = Creator$1.canvas({ width: Math.floor(width), height: Math.floor(height), pixelRatio, smooth, contextSettings });
2285
- let sliceLeaf;
2286
- if (slice) {
2287
- sliceLeaf = leaf;
2288
- sliceLeaf.__worldOpacity = 0;
2289
- leaf = leafer || leaf;
2290
- renderOptions.bounds = canvas.bounds;
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) };
2291
2268
  }
2292
- canvas.save();
2293
- if (isFrame && fill !== undefined) {
2294
- const oldFill = leaf.get('fill');
2295
- leaf.fill = '';
2296
- leaf.__render(canvas, renderOptions);
2297
- leaf.fill = oldFill;
2269
+ else if (fileType === 'svg') {
2270
+ isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2271
+ result = { data: isDownload ? true : leaf.toSVG() };
2298
2272
  }
2299
2273
  else {
2300
- leaf.__render(canvas, renderOptions);
2301
- }
2302
- canvas.restore();
2303
- if (sliceLeaf)
2304
- sliceLeaf.__updateWorldOpacity();
2305
- if (trim) {
2306
- trimBounds = getTrimBounds(canvas);
2307
- const old = canvas, { width, height } = trimBounds;
2308
- const config = { x: 0, y: 0, width, height, pixelRatio };
2309
- canvas = Creator$1.canvas(config);
2310
- canvas.copyWorld(old, trimBounds, config);
2311
- }
2312
- if (padding) {
2313
- const [top, right, bottom, left] = MathHelper$1.fourNumber(padding);
2314
- const old = canvas, { width, height } = old;
2315
- canvas = Creator$1.canvas({ width: width + left + right, height: height + top + bottom, pixelRatio });
2316
- 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 };
2317
2358
  }
2318
- if (needFill)
2319
- canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2320
- if (onCanvas)
2321
- onCanvas(canvas);
2322
- const data = filename === 'canvas' ? canvas : canvas.export(filename, options);
2323
- result = { data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds };
2324
2359
  }
2325
- this.running = false;
2360
+ catch (error) {
2361
+ result = { data: '', error };
2362
+ }
2363
+ Export.running = false;
2326
2364
  return result;
2327
2365
  },
2328
2366
  export(leaf, filename, options) {
2329
- this.running = true;
2367
+ Export.running = true;
2330
2368
  return addTask((success) => new Promise((resolve) => {
2331
2369
  const getResult = () => __awaiter(this, void 0, void 0, function* () {
2332
2370
  if (!Resource.isComplete)
2333
2371
  return Platform$1.requestRender(getResult);
2334
- const result = ExportModule.syncExport(leaf, filename, options);
2372
+ const result = Export.syncExport(leaf, filename, options);
2335
2373
  if (result.data instanceof Promise)
2336
2374
  result.data = yield result.data;
2337
2375
  success(result);