@leafer-draw/worker 2.0.0 → 2.0.2

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/worker.cjs CHANGED
@@ -13,6 +13,39 @@ exports.PathNodeHandleType = void 0;
13
13
  PathNodeHandleType[PathNodeHandleType["mirror"] = 4] = "mirror";
14
14
  })(exports.PathNodeHandleType || (exports.PathNodeHandleType = {}));
15
15
 
16
+ function __awaiter(thisArg, _arguments, P, generator) {
17
+ function adopt(value) {
18
+ return value instanceof P ? value : new P(function(resolve) {
19
+ resolve(value);
20
+ });
21
+ }
22
+ return new (P || (P = Promise))(function(resolve, reject) {
23
+ function fulfilled(value) {
24
+ try {
25
+ step(generator.next(value));
26
+ } catch (e) {
27
+ reject(e);
28
+ }
29
+ }
30
+ function rejected(value) {
31
+ try {
32
+ step(generator["throw"](value));
33
+ } catch (e) {
34
+ reject(e);
35
+ }
36
+ }
37
+ function step(result) {
38
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
39
+ }
40
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
41
+ });
42
+ }
43
+
44
+ typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
45
+ var e = new Error(message);
46
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
47
+ };
48
+
16
49
  class LeaferCanvas extends core.LeaferCanvasBase {
17
50
  get allowBackgroundColor() {
18
51
  return true;
@@ -37,8 +70,6 @@ core.canvasPatch(OffscreenCanvasRenderingContext2D.prototype);
37
70
 
38
71
  core.canvasPatch(Path2D.prototype);
39
72
 
40
- const {mineType: mineType} = core.FileHelper;
41
-
42
73
  Object.assign(core.Creator, {
43
74
  canvas: (options, manager) => new LeaferCanvas(options, manager),
44
75
  image: options => new core.LeaferImage(options)
@@ -49,7 +80,7 @@ function useCanvas(_canvasType, _power) {
49
80
  createCanvas: (width, height) => new OffscreenCanvas(width, height),
50
81
  canvasToDataURL: (canvas, type, quality) => new Promise((resolve, reject) => {
51
82
  canvas.convertToBlob({
52
- type: mineType(type),
83
+ type: core.FileHelper.mimeType(type),
53
84
  quality: quality
54
85
  }).then(blob => {
55
86
  var reader = new FileReader;
@@ -61,14 +92,14 @@ function useCanvas(_canvasType, _power) {
61
92
  });
62
93
  }),
63
94
  canvasToBolb: (canvas, type, quality) => canvas.convertToBlob({
64
- type: mineType(type),
95
+ type: core.FileHelper.mimeType(type),
65
96
  quality: quality
66
97
  }),
67
98
  canvasSaveAs: (_canvas, _filename, _quality) => new Promise(resolve => resolve()),
68
99
  download(_url, _filename) {
69
100
  return undefined;
70
101
  },
71
- loadImage(src) {
102
+ loadImage(src, _crossOrigin, _leaferImage) {
72
103
  return new Promise((resolve, reject) => {
73
104
  let req = new XMLHttpRequest;
74
105
  req.open("GET", core.Platform.image.getRealURL(src), true);
@@ -83,6 +114,13 @@ function useCanvas(_canvasType, _power) {
83
114
  req.onerror = e => reject(e);
84
115
  req.send();
85
116
  });
117
+ },
118
+ loadContent(url_1) {
119
+ return __awaiter(this, arguments, void 0, function*(url, responseType = "text") {
120
+ const response = yield fetch(url);
121
+ if (!response.ok) throw new Error(`${response.status}`);
122
+ return yield response[responseType]();
123
+ });
86
124
  }
87
125
  };
88
126
  core.Platform.canvas = core.Creator.canvas();
@@ -582,7 +620,7 @@ class Renderer {
582
620
  getCellList() {
583
621
  return undefined;
584
622
  }
585
- addBlock(block) {
623
+ addBlock(block, _leafList) {
586
624
  if (!this.updateBlocks) this.updateBlocks = [];
587
625
  this.updateBlocks.push(block);
588
626
  }
@@ -630,7 +668,8 @@ class Renderer {
630
668
  __onLayoutEnd(event) {
631
669
  if (event.data) event.data.map(item => {
632
670
  let empty;
633
- if (item.updatedList) item.updatedList.list.some(leaf => {
671
+ const {updatedList: updatedList} = item;
672
+ if (updatedList) updatedList.list.some(leaf => {
634
673
  empty = !leaf.__world.width || !leaf.__world.height;
635
674
  if (empty) {
636
675
  if (!leaf.isLeafer) debug.tip(leaf.innerName, ": empty");
@@ -638,7 +677,7 @@ class Renderer {
638
677
  }
639
678
  return empty;
640
679
  });
641
- this.addBlock(empty ? this.canvas.bounds : item.updatedBounds);
680
+ this.addBlock(empty ? this.canvas.bounds : item.updatedBounds, updatedList);
642
681
  });
643
682
  }
644
683
  emitRender(type, bounds, options) {
@@ -978,11 +1017,14 @@ function compute(attrName, ui) {
978
1017
  function getLeafPaint(attrName, paint, ui) {
979
1018
  if (!core.isObject(paint) || paint.visible === false || paint.opacity === 0) return undefined;
980
1019
  let leafPaint;
981
- const {boxBounds: boxBounds} = ui.__layout;
982
- switch (paint.type) {
1020
+ const {boxBounds: boxBounds} = ui.__layout, {type: type} = paint;
1021
+ switch (type) {
983
1022
  case "image":
1023
+ case "film":
1024
+ case "video":
984
1025
  if (!paint.url) return undefined;
985
1026
  leafPaint = draw.PaintImage.image(ui, attrName, paint, boxBounds, !recycleMap || !recycleMap[paint.url]);
1027
+ if (type !== "image") draw.PaintImage[type](leafPaint);
986
1028
  break;
987
1029
 
988
1030
  case "linear":
@@ -998,7 +1040,7 @@ function getLeafPaint(attrName, paint, ui) {
998
1040
  break;
999
1041
 
1000
1042
  case "solid":
1001
- const {type: type, color: color, opacity: opacity} = paint;
1043
+ const {color: color, opacity: opacity} = paint;
1002
1044
  leafPaint = {
1003
1045
  type: type,
1004
1046
  style: draw.ColorConvert.string(color, opacity)
@@ -1042,7 +1084,7 @@ const {isSame: isSame} = core.BoundsHelper;
1042
1084
 
1043
1085
  function image(ui, attrName, paint, boxBounds, firstUse) {
1044
1086
  let leafPaint, event;
1045
- const image = core.ImageManager.get(paint);
1087
+ const image = core.ImageManager.get(paint, paint.type);
1046
1088
  if (cache && paint === cache.paint && isSame(boxBounds, cache.boxBounds)) {
1047
1089
  leafPaint = cache.leafPaint;
1048
1090
  } else {
@@ -1103,8 +1145,8 @@ function image(ui, attrName, paint, boxBounds, firstUse) {
1103
1145
  }
1104
1146
 
1105
1147
  function checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds) {
1106
- if (attrName === "fill" && !ui.__.__naturalWidth) {
1107
- const data = ui.__;
1148
+ const data = ui.__;
1149
+ if (attrName === "fill" && !data.__naturalWidth) {
1108
1150
  data.__naturalWidth = image.width / data.pixelRatio;
1109
1151
  data.__naturalHeight = image.height / data.pixelRatio;
1110
1152
  if (data.__autoSide) {
@@ -1116,7 +1158,12 @@ function checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds
1116
1158
  return false;
1117
1159
  }
1118
1160
  }
1119
- if (!leafPaint.data) draw.PaintImage.createData(leafPaint, image, paint, boxBounds);
1161
+ if (!leafPaint.data) {
1162
+ draw.PaintImage.createData(leafPaint, image, paint, boxBounds);
1163
+ const {transform: transform} = leafPaint.data, {opacity: opacity, blendMode: blendMode} = paint;
1164
+ const clip = transform && !transform.onlyScale || data.path || data.cornerRadius;
1165
+ if (clip || opacity && opacity < 1 || blendMode) leafPaint.complex = clip ? 2 : true;
1166
+ }
1120
1167
  return true;
1121
1168
  }
1122
1169
 
@@ -1159,7 +1206,7 @@ function getPatternData(paint, box, image) {
1159
1206
  if (paint.padding) box = tempBox.set(box).shrink(paint.padding);
1160
1207
  if (paint.mode === "strench") paint.mode = "stretch";
1161
1208
  const {width: width, height: height} = image;
1162
- const {opacity: opacity, mode: mode, align: align, offset: offset, scale: scale, size: size, rotation: rotation, skew: skew, clipSize: clipSize, repeat: repeat, gap: gap, filters: filters, interlace: interlace} = paint;
1209
+ const {mode: mode, align: align, offset: offset, scale: scale, size: size, rotation: rotation, skew: skew, clipSize: clipSize, repeat: repeat, gap: gap, interlace: interlace} = paint;
1163
1210
  const sameBox = box.width === width && box.height === height;
1164
1211
  const data = {
1165
1212
  mode: mode
@@ -1222,8 +1269,6 @@ function getPatternData(paint, box, image) {
1222
1269
  data.scaleX = scaleX;
1223
1270
  data.scaleY = scaleY;
1224
1271
  }
1225
- if (opacity && opacity < 1) data.opacity = opacity;
1226
- if (filters) data.filters = filters;
1227
1272
  if (repeat) data.repeat = core.isString(repeat) ? repeat === "x" ? "repeat-x" : "repeat-y" : "repeat";
1228
1273
  if (interlace) data.interlace = core.isNumber(interlace) || interlace.type === "percent" ? {
1229
1274
  type: "x",
@@ -1254,7 +1299,7 @@ const {get: get$2, set: set, rotateOfOuter: rotateOfOuter$1, translate: translat
1254
1299
 
1255
1300
  function stretchMode(data, box, scaleX, scaleY) {
1256
1301
  const transform = get$2(), {x: x, y: y} = box;
1257
- if (x || y) translate(transform, x, y); else transform.onlyScale = true;
1302
+ if (x || y) translate(transform, x, y); else if (scaleX > 0 && scaleY > 0) transform.onlyScale = true;
1258
1303
  scaleHelper(transform, scaleX, scaleY);
1259
1304
  data.transform = transform;
1260
1305
  }
@@ -1326,39 +1371,6 @@ function layout(transform, box, x, y, scaleX, scaleY, rotation, skew) {
1326
1371
  translate(transform, box.x + x, box.y + y);
1327
1372
  }
1328
1373
 
1329
- function __awaiter(thisArg, _arguments, P, generator) {
1330
- function adopt(value) {
1331
- return value instanceof P ? value : new P(function(resolve) {
1332
- resolve(value);
1333
- });
1334
- }
1335
- return new (P || (P = Promise))(function(resolve, reject) {
1336
- function fulfilled(value) {
1337
- try {
1338
- step(generator.next(value));
1339
- } catch (e) {
1340
- reject(e);
1341
- }
1342
- }
1343
- function rejected(value) {
1344
- try {
1345
- step(generator["throw"](value));
1346
- } catch (e) {
1347
- reject(e);
1348
- }
1349
- }
1350
- function step(result) {
1351
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
1352
- }
1353
- step((generator = generator.apply(thisArg, _arguments || [])).next());
1354
- });
1355
- }
1356
-
1357
- typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
1358
- var e = new Error(message);
1359
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1360
- };
1361
-
1362
1374
  const {get: get$1, scale: scale, copy: copy$1} = core.MatrixHelper;
1363
1375
 
1364
1376
  const {getFloorScale: getFloorScale} = core.MathHelper, {abs: abs$1} = Math;
@@ -1376,10 +1388,10 @@ function createPatternTask(paint, ui, canvas, renderOptions) {
1376
1388
  }
1377
1389
 
1378
1390
  function createPattern(paint, ui, canvas, renderOptions) {
1379
- let {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = scaleX + "-" + scaleY;
1391
+ let {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = paint.film ? paint.nowIndex : scaleX + "-" + scaleY;
1380
1392
  if (paint.patternId !== id && !ui.destroyed) {
1381
1393
  if (!(core.Platform.image.isLarge(paint.image, scaleX, scaleY) && !paint.data.repeat)) {
1382
- const {image: image, data: data} = paint, {transform: transform, gap: gap} = data, fixScale = draw.PaintImage.getPatternFixScale(paint, scaleX, scaleY);
1394
+ const {image: image, data: data} = paint, {opacity: opacity, filters: filters} = paint.originPaint, {transform: transform, gap: gap} = data, fixScale = draw.PaintImage.getPatternFixScale(paint, scaleX, scaleY);
1383
1395
  let imageMatrix, xGap, yGap, {width: width, height: height} = image;
1384
1396
  if (fixScale) scaleX *= fixScale, scaleY *= fixScale;
1385
1397
  width *= scaleX;
@@ -1395,7 +1407,7 @@ function createPattern(paint, ui, canvas, renderOptions) {
1395
1407
  if (transform) copy$1(imageMatrix, transform);
1396
1408
  scale(imageMatrix, 1 / scaleX, 1 / scaleY);
1397
1409
  }
1398
- const imageCanvas = image.getCanvas(width, height, data.opacity, data.filters, xGap, yGap, ui.leafer && ui.leafer.config.smooth, data.interlace);
1410
+ const imageCanvas = image.getCanvas(width, height, opacity, filters, xGap, yGap, ui.leafer && ui.leafer.config.smooth, data.interlace);
1399
1411
  const pattern = image.getPattern(imageCanvas, data.repeat || (core.Platform.origin.noRepeat || "no-repeat"), imageMatrix, paint);
1400
1412
  paint.style = pattern;
1401
1413
  paint.patternId = id;
@@ -1416,15 +1428,15 @@ function getPatternFixScale(paint, imageScaleX, imageScaleY) {
1416
1428
  }
1417
1429
 
1418
1430
  function checkImage(paint, drawImage, ui, canvas, renderOptions) {
1419
- const {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions);
1431
+ const {scaleX: scaleX, scaleY: scaleY} = draw.PaintImage.getImageRenderScaleData(paint, ui, canvas, renderOptions), id = paint.film ? paint.nowIndex : scaleX + "-" + scaleY;
1420
1432
  const {image: image, data: data, originPaint: originPaint} = paint, {exporting: exporting, snapshot: snapshot} = renderOptions;
1421
- if (!data || paint.patternId === scaleX + "-" + scaleY && !exporting || snapshot) {
1433
+ if (!data || paint.patternId === id && !exporting || snapshot) {
1422
1434
  return false;
1423
1435
  } else {
1424
1436
  if (drawImage) {
1425
1437
  if (data.repeat) {
1426
1438
  drawImage = false;
1427
- } else if (!(originPaint.changeful || core.Platform.name === "miniapp" && core.ResizeEvent.isResizing(ui) || exporting)) {
1439
+ } else if (!(originPaint.changeful || paint.film || core.Platform.name === "miniapp" && core.ResizeEvent.isResizing(ui) || exporting)) {
1428
1440
  drawImage = core.Platform.image.isLarge(image, scaleX, scaleY) || image.width * scaleX > 8096 || image.height * scaleY > 8096;
1429
1441
  }
1430
1442
  }
@@ -1442,20 +1454,21 @@ function checkImage(paint, drawImage, ui, canvas, renderOptions) {
1442
1454
  }
1443
1455
  }
1444
1456
 
1445
- function drawImage(paint, _imageScaleX, _imageScaleY, ui, canvas, _renderOptions) {
1446
- const {data: data, image: image} = paint, {blendMode: blendMode} = paint.originPaint, {opacity: opacity, transform: transform} = data, view = image.getFull(data.filters), u = ui.__;
1447
- let {width: width, height: height} = image, clipUI;
1448
- if ((clipUI = transform && !transform.onlyScale || u.path || u.cornerRadius) || opacity || blendMode) {
1457
+ function drawImage(paint, imageScaleX, imageScaleY, ui, canvas, _renderOptions) {
1458
+ const {data: data, image: image, complex: complex} = paint;
1459
+ let {width: width, height: height} = image;
1460
+ if (complex) {
1461
+ const {blendMode: blendMode, opacity: opacity} = paint.originPaint, {transform: transform} = data;
1449
1462
  canvas.save();
1450
- clipUI && canvas.clipUI(ui);
1463
+ complex === 2 && canvas.clipUI(ui);
1451
1464
  blendMode && (canvas.blendMode = blendMode);
1452
1465
  opacity && (canvas.opacity *= opacity);
1453
1466
  transform && canvas.transform(transform);
1454
- canvas.drawImage(view, 0, 0, width, height);
1467
+ image.render(canvas, 0, 0, width, height, ui, paint, imageScaleX, imageScaleY);
1455
1468
  canvas.restore();
1456
1469
  } else {
1457
1470
  if (data.scaleX) width *= data.scaleX, height *= data.scaleY;
1458
- canvas.drawImage(view, 0, 0, width, height);
1471
+ image.render(canvas, 0, 0, width, height, ui, paint, imageScaleX, imageScaleY);
1459
1472
  }
1460
1473
  }
1461
1474
 
@@ -2175,7 +2188,7 @@ function layoutText(drawData, style) {
2175
2188
  let {x: x, y: y, width: width, height: height} = bounds, realHeight = __lineHeight * countRows + (paraSpacing ? paraSpacing * (drawData.paraNumber - 1) : 0);
2176
2189
  let starY = __baseLine;
2177
2190
  if (__clipText && realHeight > height) {
2178
- realHeight = Math.max(height, __lineHeight);
2191
+ realHeight = Math.max(style.__autoHeight ? realHeight : height, __lineHeight);
2179
2192
  if (countRows > 1) drawData.overflow = countRows;
2180
2193
  } else if (height || autoSizeAlign) {
2181
2194
  switch (verticalAlign) {
@@ -2232,10 +2245,10 @@ function layoutText(drawData, style) {
2232
2245
  }
2233
2246
 
2234
2247
  function clipText(drawData, style, x, width) {
2235
- if (!width) return;
2236
2248
  const {rows: rows, overflow: overflow} = drawData;
2237
2249
  let {textOverflow: textOverflow} = style;
2238
- rows.splice(overflow);
2250
+ if (overflow) rows.splice(overflow);
2251
+ if (!width) return;
2239
2252
  if (textOverflow && textOverflow !== "show") {
2240
2253
  if (textOverflow === "hide") textOverflow = ""; else if (textOverflow === "ellipsis") textOverflow = "...";
2241
2254
  let char, charRight;
@@ -2394,6 +2407,13 @@ Object.assign(draw.Effect, EffectModule);
2394
2407
 
2395
2408
  useCanvas();
2396
2409
 
2410
+ Object.defineProperty(exports, "LeaferFilm", {
2411
+ enumerable: true,
2412
+ get: function() {
2413
+ return core.LeaferFilm;
2414
+ }
2415
+ });
2416
+
2397
2417
  Object.defineProperty(exports, "LeaferImage", {
2398
2418
  enumerable: true,
2399
2419
  get: function() {
@@ -2401,6 +2421,13 @@ Object.defineProperty(exports, "LeaferImage", {
2401
2421
  }
2402
2422
  });
2403
2423
 
2424
+ Object.defineProperty(exports, "LeaferVideo", {
2425
+ enumerable: true,
2426
+ get: function() {
2427
+ return core.LeaferVideo;
2428
+ }
2429
+ });
2430
+
2404
2431
  exports.Layouter = Layouter;
2405
2432
 
2406
2433
  exports.LeaferCanvas = LeaferCanvas;