@leafer-draw/miniapp 1.0.0 → 1.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.
@@ -0,0 +1,2530 @@
1
+ 'use strict';
2
+
3
+ var core = require('@leafer/core');
4
+ var draw = require('@leafer-ui/draw');
5
+
6
+ class LeaferCanvas extends core.LeaferCanvasBase {
7
+ get allowBackgroundColor() { return false; }
8
+ init() {
9
+ const { config } = this;
10
+ let view = config.view || config.canvas;
11
+ if (view) {
12
+ if (typeof view === 'string') {
13
+ if (view[0] !== '#')
14
+ view = '#' + view;
15
+ this.viewSelect = core.Platform.miniapp.select(view);
16
+ }
17
+ else if (view.fields) {
18
+ this.viewSelect = view;
19
+ }
20
+ else {
21
+ this.initView(view);
22
+ }
23
+ if (this.viewSelect)
24
+ core.Platform.miniapp.getSizeView(this.viewSelect).then(sizeView => {
25
+ this.initView(sizeView);
26
+ });
27
+ }
28
+ else {
29
+ this.initView();
30
+ }
31
+ }
32
+ initView(view) {
33
+ if (!view) {
34
+ view = {};
35
+ this.__createView();
36
+ }
37
+ else {
38
+ this.view = view.view || view;
39
+ }
40
+ this.view.getContext ? this.__createContext() : this.unrealCanvas();
41
+ const { width, height, pixelRatio } = this.config;
42
+ const size = { width: width || view.width, height: height || view.height, pixelRatio };
43
+ this.resize(size);
44
+ if (this.context) {
45
+ if (this.viewSelect)
46
+ core.Platform.renderCanvas = this;
47
+ if (this.context.roundRect) {
48
+ this.roundRect = function (x, y, width, height, radius) {
49
+ this.context.roundRect(x, y, width, height, typeof radius === 'number' ? [radius] : radius);
50
+ };
51
+ }
52
+ core.canvasPatch(this.context.__proto__);
53
+ }
54
+ }
55
+ __createView() {
56
+ this.view = core.Platform.origin.createCanvas(1, 1);
57
+ }
58
+ updateViewSize() {
59
+ const { width, height, pixelRatio } = this;
60
+ this.view.width = Math.ceil(width * pixelRatio);
61
+ this.view.height = Math.ceil(height * pixelRatio);
62
+ }
63
+ updateClientBounds(callback) {
64
+ if (this.viewSelect)
65
+ core.Platform.miniapp.getBounds(this.viewSelect).then(bounds => {
66
+ this.clientBounds = bounds;
67
+ if (callback)
68
+ callback();
69
+ });
70
+ }
71
+ startAutoLayout(autoBounds, listener) {
72
+ this.resizeListener = listener;
73
+ if (autoBounds) {
74
+ this.checkSize = this.checkSize.bind(this);
75
+ core.Platform.miniapp.onWindowResize(this.checkSize);
76
+ }
77
+ }
78
+ checkSize() {
79
+ if (this.viewSelect) {
80
+ setTimeout(() => {
81
+ this.updateClientBounds(() => {
82
+ const { width, height } = this.clientBounds;
83
+ const { pixelRatio } = this;
84
+ const size = { width, height, pixelRatio };
85
+ if (!this.isSameSize(size))
86
+ this.emitResize(size);
87
+ });
88
+ }, 500);
89
+ }
90
+ }
91
+ stopAutoLayout() {
92
+ this.autoLayout = false;
93
+ this.resizeListener = null;
94
+ core.Platform.miniapp.offWindowResize(this.checkSize);
95
+ }
96
+ unrealCanvas() {
97
+ this.unreal = true;
98
+ }
99
+ emitResize(size) {
100
+ const oldSize = {};
101
+ core.DataHelper.copyAttrs(oldSize, this, core.canvasSizeAttrs);
102
+ this.resize(size);
103
+ if (this.width !== undefined)
104
+ this.resizeListener(new core.ResizeEvent(size, oldSize));
105
+ }
106
+ }
107
+
108
+ const { mineType, fileType } = core.FileHelper;
109
+ Object.assign(core.Creator, {
110
+ canvas: (options, manager) => new LeaferCanvas(options, manager),
111
+ image: (options) => new core.LeaferImage(options)
112
+ });
113
+ function useCanvas(_canvasType, app) {
114
+ core.Platform.origin = {
115
+ createCanvas: (width, height, _format) => app.createOffscreenCanvas({ type: '2d', width, height }),
116
+ canvasToDataURL: (canvas, type, quality) => canvas.toDataURL(mineType(type), quality),
117
+ canvasToBolb: (canvas, type, quality) => canvas.toBuffer(type, { quality }),
118
+ canvasSaveAs: (canvas, filePath, quality) => {
119
+ let data = canvas.toDataURL(mineType(fileType(filePath)), quality);
120
+ data = data.substring(data.indexOf('64,') + 3);
121
+ return core.Platform.origin.download(data, filePath);
122
+ },
123
+ download(data, filePath) {
124
+ return new Promise((resolve, reject) => {
125
+ let toAlbum;
126
+ if (!filePath.includes('/')) {
127
+ filePath = `${app.env.USER_DATA_PATH}/` + filePath;
128
+ toAlbum = true;
129
+ }
130
+ const fs = app.getFileSystemManager();
131
+ fs.writeFile({
132
+ filePath,
133
+ data,
134
+ encoding: 'base64',
135
+ success() {
136
+ if (toAlbum) {
137
+ core.Platform.miniapp.saveToAlbum(filePath).then(() => {
138
+ fs.unlink({ filePath });
139
+ resolve();
140
+ });
141
+ }
142
+ else {
143
+ resolve();
144
+ }
145
+ },
146
+ fail(error) {
147
+ reject(error);
148
+ }
149
+ });
150
+ });
151
+ },
152
+ loadImage(src) {
153
+ return new Promise((resolve, reject) => {
154
+ const img = core.Platform.canvas.view.createImage();
155
+ img.onload = () => { resolve(img); };
156
+ img.onerror = (error) => { reject(error); };
157
+ img.src = core.Platform.image.getRealURL(src);
158
+ });
159
+ },
160
+ noRepeat: 'repeat-x'
161
+ };
162
+ core.Platform.miniapp = {
163
+ select(name) {
164
+ return app.createSelectorQuery().select(name);
165
+ },
166
+ getBounds(select) {
167
+ return new Promise((resolve) => {
168
+ select.boundingClientRect().exec((res) => {
169
+ const rect = res[1];
170
+ resolve({ x: rect.top, y: rect.left, width: rect.width, height: rect.height });
171
+ });
172
+ });
173
+ },
174
+ getSizeView(select) {
175
+ return new Promise((resolve) => {
176
+ select.fields({ node: true, size: true }).exec((res) => {
177
+ const data = res[0];
178
+ resolve({ view: data.node, width: data.width, height: data.height });
179
+ });
180
+ });
181
+ },
182
+ saveToAlbum(path) {
183
+ return new Promise((resolve) => {
184
+ app.getSetting({
185
+ success: (res) => {
186
+ if (res.authSetting['scope.writePhotosAlbum']) {
187
+ app.saveImageToPhotosAlbum({
188
+ filePath: path,
189
+ success() { resolve(true); }
190
+ });
191
+ }
192
+ else {
193
+ app.authorize({
194
+ scope: 'scope.writePhotosAlbum',
195
+ success: () => {
196
+ app.saveImageToPhotosAlbum({
197
+ filePath: path,
198
+ success() { resolve(true); }
199
+ });
200
+ },
201
+ fail: () => { }
202
+ });
203
+ }
204
+ }
205
+ });
206
+ });
207
+ },
208
+ onWindowResize(fun) {
209
+ app.onWindowResize(fun);
210
+ },
211
+ offWindowResize(fun) {
212
+ app.offWindowResize(fun);
213
+ }
214
+ };
215
+ core.Platform.event = {
216
+ stopDefault(_origin) { },
217
+ stopNow(_origin) { },
218
+ stop(_origin) { }
219
+ };
220
+ core.Platform.canvas = core.Creator.canvas();
221
+ core.Platform.conicGradientSupport = !!core.Platform.canvas.context.createConicGradient;
222
+ }
223
+ core.Platform.name = 'miniapp';
224
+ core.Platform.requestRender = function (render) {
225
+ const { view } = (core.Platform.renderCanvas || core.Platform.canvas);
226
+ view.requestAnimationFrame ? view.requestAnimationFrame(render) : setTimeout(render, 16);
227
+ };
228
+ core.defineKey(core.Platform, 'devicePixelRatio', { get() { return Math.max(1, wx.getSystemInfoSync().pixelRatio); } });
229
+
230
+ class Watcher {
231
+ get childrenChanged() { return this.hasAdd || this.hasRemove || this.hasVisible; }
232
+ get updatedList() {
233
+ if (this.hasRemove) {
234
+ const updatedList = new core.LeafList();
235
+ this.__updatedList.list.forEach(item => { if (item.leafer)
236
+ updatedList.add(item); });
237
+ return updatedList;
238
+ }
239
+ else {
240
+ return this.__updatedList;
241
+ }
242
+ }
243
+ constructor(target, userConfig) {
244
+ this.totalTimes = 0;
245
+ this.config = {};
246
+ this.__updatedList = new core.LeafList();
247
+ this.target = target;
248
+ if (userConfig)
249
+ this.config = core.DataHelper.default(userConfig, this.config);
250
+ this.__listenEvents();
251
+ }
252
+ start() {
253
+ if (this.disabled)
254
+ return;
255
+ this.running = true;
256
+ }
257
+ stop() {
258
+ this.running = false;
259
+ }
260
+ disable() {
261
+ this.stop();
262
+ this.__removeListenEvents();
263
+ this.disabled = true;
264
+ }
265
+ update() {
266
+ this.changed = true;
267
+ if (this.running)
268
+ this.target.emit(core.RenderEvent.REQUEST);
269
+ }
270
+ __onAttrChange(event) {
271
+ this.__updatedList.add(event.target);
272
+ this.update();
273
+ }
274
+ __onChildEvent(event) {
275
+ if (event.type === core.ChildEvent.ADD) {
276
+ this.hasAdd = true;
277
+ this.__pushChild(event.child);
278
+ }
279
+ else {
280
+ this.hasRemove = true;
281
+ this.__updatedList.add(event.parent);
282
+ }
283
+ this.update();
284
+ }
285
+ __pushChild(child) {
286
+ this.__updatedList.add(child);
287
+ if (child.isBranch)
288
+ this.__loopChildren(child);
289
+ }
290
+ __loopChildren(parent) {
291
+ const { children } = parent;
292
+ for (let i = 0, len = children.length; i < len; i++)
293
+ this.__pushChild(children[i]);
294
+ }
295
+ __onRquestData() {
296
+ this.target.emitEvent(new core.WatchEvent(core.WatchEvent.DATA, { updatedList: this.updatedList }));
297
+ this.__updatedList = new core.LeafList();
298
+ this.totalTimes++;
299
+ this.changed = false;
300
+ this.hasVisible = false;
301
+ this.hasRemove = false;
302
+ this.hasAdd = false;
303
+ }
304
+ __listenEvents() {
305
+ const { target } = this;
306
+ this.__eventIds = [
307
+ target.on_(core.PropertyEvent.CHANGE, this.__onAttrChange, this),
308
+ target.on_([core.ChildEvent.ADD, core.ChildEvent.REMOVE], this.__onChildEvent, this),
309
+ target.on_(core.WatchEvent.REQUEST, this.__onRquestData, this)
310
+ ];
311
+ }
312
+ __removeListenEvents() {
313
+ this.target.off_(this.__eventIds);
314
+ }
315
+ destroy() {
316
+ if (this.target) {
317
+ this.stop();
318
+ this.__removeListenEvents();
319
+ this.target = null;
320
+ this.__updatedList = null;
321
+ }
322
+ }
323
+ }
324
+
325
+ const { updateAllMatrix: updateAllMatrix$1, updateBounds: updateOneBounds, updateAllWorldOpacity } = core.LeafHelper;
326
+ const { pushAllChildBranch, pushAllParent } = core.BranchHelper;
327
+ function updateMatrix(updateList, levelList) {
328
+ let layout;
329
+ updateList.list.forEach(leaf => {
330
+ layout = leaf.__layout;
331
+ if (levelList.without(leaf) && !layout.proxyZoom) {
332
+ if (layout.matrixChanged) {
333
+ updateAllMatrix$1(leaf, true);
334
+ levelList.add(leaf);
335
+ if (leaf.isBranch)
336
+ pushAllChildBranch(leaf, levelList);
337
+ pushAllParent(leaf, levelList);
338
+ }
339
+ else if (layout.boundsChanged) {
340
+ levelList.add(leaf);
341
+ if (leaf.isBranch)
342
+ leaf.__tempNumber = 0;
343
+ pushAllParent(leaf, levelList);
344
+ }
345
+ }
346
+ });
347
+ }
348
+ function updateBounds(boundsList) {
349
+ let list, branch, children;
350
+ boundsList.sort(true);
351
+ boundsList.levels.forEach(level => {
352
+ list = boundsList.levelMap[level];
353
+ for (let i = 0, len = list.length; i < len; i++) {
354
+ branch = list[i];
355
+ if (branch.isBranch && branch.__tempNumber) {
356
+ children = branch.children;
357
+ for (let j = 0, jLen = children.length; j < jLen; j++) {
358
+ if (!children[j].isBranch) {
359
+ updateOneBounds(children[j]);
360
+ }
361
+ }
362
+ }
363
+ updateOneBounds(branch);
364
+ }
365
+ });
366
+ }
367
+ function updateChange(updateList) {
368
+ updateList.list.forEach(leaf => {
369
+ if (leaf.__layout.opacityChanged)
370
+ updateAllWorldOpacity(leaf);
371
+ leaf.__updateChange();
372
+ });
373
+ }
374
+
375
+ const { worldBounds } = core.LeafBoundsHelper;
376
+ const bigBounds = { x: 0, y: 0, width: 100000, height: 100000 };
377
+ class LayoutBlockData {
378
+ constructor(list) {
379
+ this.updatedBounds = new core.Bounds();
380
+ this.beforeBounds = new core.Bounds();
381
+ this.afterBounds = new core.Bounds();
382
+ if (list instanceof Array)
383
+ list = new core.LeafList(list);
384
+ this.updatedList = list;
385
+ }
386
+ setBefore() {
387
+ this.beforeBounds.setListWithFn(this.updatedList.list, worldBounds);
388
+ }
389
+ setAfter() {
390
+ const { list } = this.updatedList;
391
+ if (list.some(leaf => leaf.noBounds)) {
392
+ this.afterBounds.set(bigBounds);
393
+ }
394
+ else {
395
+ this.afterBounds.setListWithFn(list, worldBounds);
396
+ }
397
+ this.updatedBounds.setList([this.beforeBounds, this.afterBounds]);
398
+ }
399
+ merge(data) {
400
+ this.updatedList.addList(data.updatedList.list);
401
+ this.beforeBounds.add(data.beforeBounds);
402
+ this.afterBounds.add(data.afterBounds);
403
+ this.updatedBounds.add(data.updatedBounds);
404
+ }
405
+ destroy() {
406
+ this.updatedList = null;
407
+ }
408
+ }
409
+
410
+ const { updateAllMatrix, updateAllChange } = core.LeafHelper;
411
+ const debug$2 = core.Debug.get('Layouter');
412
+ class Layouter {
413
+ constructor(target, userConfig) {
414
+ this.totalTimes = 0;
415
+ this.config = {};
416
+ this.__levelList = new core.LeafLevelList();
417
+ this.target = target;
418
+ if (userConfig)
419
+ this.config = core.DataHelper.default(userConfig, this.config);
420
+ this.__listenEvents();
421
+ }
422
+ start() {
423
+ if (this.disabled)
424
+ return;
425
+ this.running = true;
426
+ }
427
+ stop() {
428
+ this.running = false;
429
+ }
430
+ disable() {
431
+ this.stop();
432
+ this.__removeListenEvents();
433
+ this.disabled = true;
434
+ }
435
+ layout() {
436
+ if (!this.running)
437
+ return;
438
+ const { target } = this;
439
+ this.times = 0;
440
+ try {
441
+ target.emit(core.LayoutEvent.START);
442
+ this.layoutOnce();
443
+ target.emitEvent(new core.LayoutEvent(core.LayoutEvent.END, this.layoutedBlocks, this.times));
444
+ }
445
+ catch (e) {
446
+ debug$2.error(e);
447
+ }
448
+ this.layoutedBlocks = null;
449
+ }
450
+ layoutAgain() {
451
+ if (this.layouting) {
452
+ this.waitAgain = true;
453
+ }
454
+ else {
455
+ this.layoutOnce();
456
+ }
457
+ }
458
+ layoutOnce() {
459
+ if (this.layouting)
460
+ return debug$2.warn('layouting');
461
+ if (this.times > 3)
462
+ return debug$2.warn('layout max times');
463
+ this.times++;
464
+ this.totalTimes++;
465
+ this.layouting = true;
466
+ this.target.emit(core.WatchEvent.REQUEST);
467
+ if (this.totalTimes > 1) {
468
+ this.partLayout();
469
+ }
470
+ else {
471
+ this.fullLayout();
472
+ }
473
+ this.layouting = false;
474
+ if (this.waitAgain) {
475
+ this.waitAgain = false;
476
+ this.layoutOnce();
477
+ }
478
+ }
479
+ partLayout() {
480
+ var _a;
481
+ if (!((_a = this.__updatedList) === null || _a === void 0 ? void 0 : _a.length))
482
+ return;
483
+ const t = core.Run.start('PartLayout');
484
+ const { target, __updatedList: updateList } = this;
485
+ const { BEFORE, LAYOUT, AFTER } = core.LayoutEvent;
486
+ const blocks = this.getBlocks(updateList);
487
+ blocks.forEach(item => item.setBefore());
488
+ target.emitEvent(new core.LayoutEvent(BEFORE, blocks, this.times));
489
+ this.extraBlock = null;
490
+ updateList.sort();
491
+ updateMatrix(updateList, this.__levelList);
492
+ updateBounds(this.__levelList);
493
+ updateChange(updateList);
494
+ if (this.extraBlock)
495
+ blocks.push(this.extraBlock);
496
+ blocks.forEach(item => item.setAfter());
497
+ target.emitEvent(new core.LayoutEvent(LAYOUT, blocks, this.times));
498
+ target.emitEvent(new core.LayoutEvent(AFTER, blocks, this.times));
499
+ this.addBlocks(blocks);
500
+ this.__levelList.reset();
501
+ this.__updatedList = null;
502
+ core.Run.end(t);
503
+ }
504
+ fullLayout() {
505
+ const t = core.Run.start('FullLayout');
506
+ const { target } = this;
507
+ const { BEFORE, LAYOUT, AFTER } = core.LayoutEvent;
508
+ const blocks = this.getBlocks(new core.LeafList(target));
509
+ target.emitEvent(new core.LayoutEvent(BEFORE, blocks, this.times));
510
+ Layouter.fullLayout(target);
511
+ blocks.forEach(item => { item.setAfter(); });
512
+ target.emitEvent(new core.LayoutEvent(LAYOUT, blocks, this.times));
513
+ target.emitEvent(new core.LayoutEvent(AFTER, blocks, this.times));
514
+ this.addBlocks(blocks);
515
+ core.Run.end(t);
516
+ }
517
+ static fullLayout(target) {
518
+ updateAllMatrix(target, true);
519
+ if (target.isBranch) {
520
+ core.BranchHelper.updateBounds(target);
521
+ }
522
+ else {
523
+ core.LeafHelper.updateBounds(target);
524
+ }
525
+ updateAllChange(target);
526
+ }
527
+ addExtra(leaf) {
528
+ if (!this.__updatedList.has(leaf)) {
529
+ const { updatedList, beforeBounds } = this.extraBlock || (this.extraBlock = new LayoutBlockData([]));
530
+ updatedList.length ? beforeBounds.add(leaf.__world) : beforeBounds.set(leaf.__world);
531
+ updatedList.add(leaf);
532
+ }
533
+ }
534
+ createBlock(data) {
535
+ return new LayoutBlockData(data);
536
+ }
537
+ getBlocks(list) {
538
+ return [this.createBlock(list)];
539
+ }
540
+ addBlocks(current) {
541
+ this.layoutedBlocks ? this.layoutedBlocks.push(...current) : this.layoutedBlocks = current;
542
+ }
543
+ __onReceiveWatchData(event) {
544
+ this.__updatedList = event.data.updatedList;
545
+ }
546
+ __listenEvents() {
547
+ const { target } = this;
548
+ this.__eventIds = [
549
+ target.on_(core.LayoutEvent.REQUEST, this.layout, this),
550
+ target.on_(core.LayoutEvent.AGAIN, this.layoutAgain, this),
551
+ target.on_(core.WatchEvent.DATA, this.__onReceiveWatchData, this)
552
+ ];
553
+ }
554
+ __removeListenEvents() {
555
+ this.target.off_(this.__eventIds);
556
+ }
557
+ destroy() {
558
+ if (this.target) {
559
+ this.stop();
560
+ this.__removeListenEvents();
561
+ this.target = this.config = null;
562
+ }
563
+ }
564
+ }
565
+
566
+ const debug$1 = core.Debug.get('Renderer');
567
+ class Renderer {
568
+ get needFill() { return !!(!this.canvas.allowBackgroundColor && this.config.fill); }
569
+ constructor(target, canvas, userConfig) {
570
+ this.FPS = 60;
571
+ this.totalTimes = 0;
572
+ this.times = 0;
573
+ this.config = {
574
+ usePartRender: true,
575
+ maxFPS: 60
576
+ };
577
+ this.target = target;
578
+ this.canvas = canvas;
579
+ if (userConfig)
580
+ this.config = core.DataHelper.default(userConfig, this.config);
581
+ this.__listenEvents();
582
+ this.__requestRender();
583
+ }
584
+ start() {
585
+ this.running = true;
586
+ }
587
+ stop() {
588
+ this.running = false;
589
+ }
590
+ update() {
591
+ this.changed = true;
592
+ }
593
+ requestLayout() {
594
+ this.target.emit(core.LayoutEvent.REQUEST);
595
+ }
596
+ render(callback) {
597
+ if (!(this.running && this.canvas.view)) {
598
+ this.changed = true;
599
+ return;
600
+ }
601
+ const { target } = this;
602
+ this.times = 0;
603
+ this.totalBounds = new core.Bounds();
604
+ debug$1.log(target.innerName, '--->');
605
+ try {
606
+ this.emitRender(core.RenderEvent.START);
607
+ this.renderOnce(callback);
608
+ this.emitRender(core.RenderEvent.END, this.totalBounds);
609
+ core.ImageManager.clearRecycled();
610
+ }
611
+ catch (e) {
612
+ this.rendering = false;
613
+ debug$1.error(e);
614
+ }
615
+ debug$1.log('-------------|');
616
+ }
617
+ renderAgain() {
618
+ if (this.rendering) {
619
+ this.waitAgain = true;
620
+ }
621
+ else {
622
+ this.renderOnce();
623
+ }
624
+ }
625
+ renderOnce(callback) {
626
+ if (this.rendering)
627
+ return debug$1.warn('rendering');
628
+ if (this.times > 3)
629
+ return debug$1.warn('render max times');
630
+ this.times++;
631
+ this.totalTimes++;
632
+ this.rendering = true;
633
+ this.changed = false;
634
+ this.renderBounds = new core.Bounds();
635
+ this.renderOptions = {};
636
+ if (callback) {
637
+ this.emitRender(core.RenderEvent.BEFORE);
638
+ callback();
639
+ }
640
+ else {
641
+ this.requestLayout();
642
+ if (this.ignore) {
643
+ this.ignore = this.rendering = false;
644
+ return;
645
+ }
646
+ this.emitRender(core.RenderEvent.BEFORE);
647
+ if (this.config.usePartRender && this.totalTimes > 1) {
648
+ this.partRender();
649
+ }
650
+ else {
651
+ this.fullRender();
652
+ }
653
+ }
654
+ this.emitRender(core.RenderEvent.RENDER, this.renderBounds, this.renderOptions);
655
+ this.emitRender(core.RenderEvent.AFTER, this.renderBounds, this.renderOptions);
656
+ this.updateBlocks = null;
657
+ this.rendering = false;
658
+ if (this.waitAgain) {
659
+ this.waitAgain = false;
660
+ this.renderOnce();
661
+ }
662
+ }
663
+ partRender() {
664
+ const { canvas, updateBlocks: list } = this;
665
+ if (!list)
666
+ return debug$1.warn('PartRender: need update attr');
667
+ this.mergeBlocks();
668
+ list.forEach(block => { if (canvas.bounds.hit(block) && !block.isEmpty())
669
+ this.clipRender(block); });
670
+ }
671
+ clipRender(block) {
672
+ const t = core.Run.start('PartRender');
673
+ const { canvas } = this;
674
+ const bounds = block.getIntersect(canvas.bounds);
675
+ const includes = block.includes(this.target.__world);
676
+ const realBounds = new core.Bounds(bounds);
677
+ canvas.save();
678
+ if (includes && !core.Debug.showRepaint) {
679
+ canvas.clear();
680
+ }
681
+ else {
682
+ bounds.spread(10 + 1 / this.canvas.pixelRatio).ceil();
683
+ canvas.clearWorld(bounds, true);
684
+ canvas.clipWorld(bounds, true);
685
+ }
686
+ this.__render(bounds, includes, realBounds);
687
+ canvas.restore();
688
+ core.Run.end(t);
689
+ }
690
+ fullRender() {
691
+ const t = core.Run.start('FullRender');
692
+ const { canvas } = this;
693
+ canvas.save();
694
+ canvas.clear();
695
+ this.__render(canvas.bounds, true);
696
+ canvas.restore();
697
+ core.Run.end(t);
698
+ }
699
+ __render(bounds, includes, realBounds) {
700
+ const options = bounds.includes(this.target.__world) ? { includes } : { bounds, includes };
701
+ if (this.needFill)
702
+ this.canvas.fillWorld(bounds, this.config.fill);
703
+ if (core.Debug.showRepaint)
704
+ this.canvas.strokeWorld(bounds, 'red');
705
+ this.target.__render(this.canvas, options);
706
+ this.renderBounds = realBounds = realBounds || bounds;
707
+ this.renderOptions = options;
708
+ this.totalBounds.isEmpty() ? this.totalBounds = realBounds : this.totalBounds.add(realBounds);
709
+ if (core.Debug.showHitView)
710
+ this.renderHitView(options);
711
+ if (core.Debug.showBoundsView)
712
+ this.renderBoundsView(options);
713
+ this.canvas.updateRender(realBounds);
714
+ }
715
+ renderHitView(_options) { }
716
+ renderBoundsView(_options) { }
717
+ addBlock(block) {
718
+ if (!this.updateBlocks)
719
+ this.updateBlocks = [];
720
+ this.updateBlocks.push(block);
721
+ }
722
+ mergeBlocks() {
723
+ const { updateBlocks: list } = this;
724
+ if (list) {
725
+ const bounds = new core.Bounds();
726
+ bounds.setList(list);
727
+ list.length = 0;
728
+ list.push(bounds);
729
+ }
730
+ }
731
+ __requestRender() {
732
+ const startTime = Date.now();
733
+ core.Platform.requestRender(() => {
734
+ this.FPS = Math.min(60, Math.ceil(1000 / (Date.now() - startTime)));
735
+ if (this.running) {
736
+ this.target.emit(core.AnimateEvent.FRAME);
737
+ if (this.changed && this.canvas.view)
738
+ this.render();
739
+ this.target.emit(core.RenderEvent.NEXT);
740
+ }
741
+ if (this.target)
742
+ this.__requestRender();
743
+ });
744
+ }
745
+ __onResize(e) {
746
+ if (this.canvas.unreal)
747
+ return;
748
+ if (e.bigger || !e.samePixelRatio) {
749
+ const { width, height } = e.old;
750
+ const bounds = new core.Bounds(0, 0, width, height);
751
+ if (!bounds.includes(this.target.__world) || this.needFill || !e.samePixelRatio) {
752
+ this.addBlock(this.canvas.bounds);
753
+ this.target.forceUpdate('surface');
754
+ return;
755
+ }
756
+ }
757
+ this.addBlock(new core.Bounds(0, 0, 1, 1));
758
+ this.changed = true;
759
+ }
760
+ __onLayoutEnd(event) {
761
+ if (event.data)
762
+ event.data.map(item => {
763
+ let empty;
764
+ if (item.updatedList)
765
+ item.updatedList.list.some(leaf => {
766
+ empty = (!leaf.__world.width || !leaf.__world.height);
767
+ if (empty) {
768
+ if (!leaf.isLeafer)
769
+ debug$1.tip(leaf.innerName, ': empty');
770
+ empty = (!leaf.isBranch || leaf.isBranchLeaf);
771
+ }
772
+ return empty;
773
+ });
774
+ this.addBlock(empty ? this.canvas.bounds : item.updatedBounds);
775
+ });
776
+ }
777
+ emitRender(type, bounds, options) {
778
+ this.target.emitEvent(new core.RenderEvent(type, this.times, bounds, options));
779
+ }
780
+ __listenEvents() {
781
+ const { target } = this;
782
+ this.__eventIds = [
783
+ target.on_(core.RenderEvent.REQUEST, this.update, this),
784
+ target.on_(core.LayoutEvent.END, this.__onLayoutEnd, this),
785
+ target.on_(core.RenderEvent.AGAIN, this.renderAgain, this),
786
+ target.on_(core.ResizeEvent.RESIZE, this.__onResize, this)
787
+ ];
788
+ }
789
+ __removeListenEvents() {
790
+ this.target.off_(this.__eventIds);
791
+ }
792
+ destroy() {
793
+ if (this.target) {
794
+ this.stop();
795
+ this.__removeListenEvents();
796
+ this.target = this.canvas = this.config = null;
797
+ }
798
+ }
799
+ }
800
+
801
+ Object.assign(core.Creator, {
802
+ watcher: (target, options) => new Watcher(target, options),
803
+ layouter: (target, options) => new Layouter(target, options),
804
+ renderer: (target, canvas, options) => new Renderer(target, canvas, options),
805
+ selector: (_target, _options) => undefined,
806
+ interaction: (_target, _canvas, _selector, _options) => undefined
807
+ });
808
+ core.Platform.layout = Layouter.fullLayout;
809
+
810
+ function fillText(ui, canvas) {
811
+ let row;
812
+ const { rows, decorationY, decorationHeight } = ui.__.__textDrawData;
813
+ for (let i = 0, len = rows.length; i < len; i++) {
814
+ row = rows[i];
815
+ if (row.text) {
816
+ canvas.fillText(row.text, row.x, row.y);
817
+ }
818
+ else if (row.data) {
819
+ row.data.forEach(charData => {
820
+ canvas.fillText(charData.char, charData.x, row.y);
821
+ });
822
+ }
823
+ if (decorationY)
824
+ canvas.fillRect(row.x, row.y + decorationY, row.width, decorationHeight);
825
+ }
826
+ }
827
+
828
+ function fill(fill, ui, canvas) {
829
+ canvas.fillStyle = fill;
830
+ ui.__.__font ? fillText(ui, canvas) : (ui.__.windingRule ? canvas.fill(ui.__.windingRule) : canvas.fill());
831
+ }
832
+ function fills(fills, ui, canvas) {
833
+ let item;
834
+ const { windingRule, __font } = ui.__;
835
+ for (let i = 0, len = fills.length; i < len; i++) {
836
+ item = fills[i];
837
+ if (item.image && draw.PaintImage.checkImage(ui, canvas, item, !__font))
838
+ continue;
839
+ if (item.style) {
840
+ canvas.fillStyle = item.style;
841
+ if (item.transform) {
842
+ canvas.save();
843
+ canvas.transform(item.transform);
844
+ if (item.blendMode)
845
+ canvas.blendMode = item.blendMode;
846
+ __font ? fillText(ui, canvas) : (windingRule ? canvas.fill(windingRule) : canvas.fill());
847
+ canvas.restore();
848
+ }
849
+ else {
850
+ if (item.blendMode) {
851
+ canvas.saveBlendMode(item.blendMode);
852
+ __font ? fillText(ui, canvas) : (windingRule ? canvas.fill(windingRule) : canvas.fill());
853
+ canvas.restoreBlendMode();
854
+ }
855
+ else {
856
+ __font ? fillText(ui, canvas) : (windingRule ? canvas.fill(windingRule) : canvas.fill());
857
+ }
858
+ }
859
+ }
860
+ }
861
+ }
862
+
863
+ function strokeText(stroke, ui, canvas) {
864
+ const { strokeAlign } = ui.__;
865
+ const isStrokes = typeof stroke !== 'string';
866
+ switch (strokeAlign) {
867
+ case 'center':
868
+ canvas.setStroke(isStrokes ? undefined : stroke, ui.__.strokeWidth, ui.__);
869
+ isStrokes ? drawStrokesStyle(stroke, true, ui, canvas) : drawTextStroke(ui, canvas);
870
+ break;
871
+ case 'inside':
872
+ drawAlignStroke('inside', stroke, isStrokes, ui, canvas);
873
+ break;
874
+ case 'outside':
875
+ drawAlignStroke('outside', stroke, isStrokes, ui, canvas);
876
+ break;
877
+ }
878
+ }
879
+ function drawAlignStroke(align, stroke, isStrokes, ui, canvas) {
880
+ const { __strokeWidth, __font } = ui.__;
881
+ const out = canvas.getSameCanvas(true, true);
882
+ out.setStroke(isStrokes ? undefined : stroke, __strokeWidth * 2, ui.__);
883
+ out.font = __font;
884
+ isStrokes ? drawStrokesStyle(stroke, true, ui, out) : drawTextStroke(ui, out);
885
+ out.blendMode = align === 'outside' ? 'destination-out' : 'destination-in';
886
+ fillText(ui, out);
887
+ out.blendMode = 'normal';
888
+ if (ui.__worldFlipped) {
889
+ canvas.copyWorldByReset(out, ui.__nowWorld);
890
+ }
891
+ else {
892
+ canvas.copyWorldToInner(out, ui.__nowWorld, ui.__layout.renderBounds);
893
+ }
894
+ out.recycle(ui.__nowWorld);
895
+ }
896
+ function drawTextStroke(ui, canvas) {
897
+ let row;
898
+ const { rows, decorationY, decorationHeight } = ui.__.__textDrawData;
899
+ for (let i = 0, len = rows.length; i < len; i++) {
900
+ row = rows[i];
901
+ if (row.text) {
902
+ canvas.strokeText(row.text, row.x, row.y);
903
+ }
904
+ else if (row.data) {
905
+ row.data.forEach(charData => {
906
+ canvas.strokeText(charData.char, charData.x, row.y);
907
+ });
908
+ }
909
+ if (decorationY)
910
+ canvas.strokeRect(row.x, row.y + decorationY, row.width, decorationHeight);
911
+ }
912
+ }
913
+ function drawStrokesStyle(strokes, isText, ui, canvas) {
914
+ let item;
915
+ for (let i = 0, len = strokes.length; i < len; i++) {
916
+ item = strokes[i];
917
+ if (item.image && draw.PaintImage.checkImage(ui, canvas, item, false))
918
+ continue;
919
+ if (item.style) {
920
+ canvas.strokeStyle = item.style;
921
+ if (item.blendMode) {
922
+ canvas.saveBlendMode(item.blendMode);
923
+ isText ? drawTextStroke(ui, canvas) : canvas.stroke();
924
+ canvas.restoreBlendMode();
925
+ }
926
+ else {
927
+ isText ? drawTextStroke(ui, canvas) : canvas.stroke();
928
+ }
929
+ }
930
+ }
931
+ }
932
+
933
+ function stroke(stroke, ui, canvas) {
934
+ const options = ui.__;
935
+ const { __strokeWidth, strokeAlign, __font } = options;
936
+ if (!__strokeWidth)
937
+ return;
938
+ if (__font) {
939
+ strokeText(stroke, ui, canvas);
940
+ }
941
+ else {
942
+ switch (strokeAlign) {
943
+ case 'center':
944
+ canvas.setStroke(stroke, __strokeWidth, options);
945
+ canvas.stroke();
946
+ break;
947
+ case 'inside':
948
+ canvas.save();
949
+ canvas.setStroke(stroke, __strokeWidth * 2, options);
950
+ options.windingRule ? canvas.clip(options.windingRule) : canvas.clip();
951
+ canvas.stroke();
952
+ canvas.restore();
953
+ break;
954
+ case 'outside':
955
+ const out = canvas.getSameCanvas(true, true);
956
+ out.setStroke(stroke, __strokeWidth * 2, options);
957
+ ui.__drawRenderPath(out);
958
+ out.stroke();
959
+ options.windingRule ? out.clip(options.windingRule) : out.clip();
960
+ out.clearWorld(ui.__layout.renderBounds);
961
+ if (ui.__worldFlipped) {
962
+ canvas.copyWorldByReset(out, ui.__nowWorld);
963
+ }
964
+ else {
965
+ canvas.copyWorldToInner(out, ui.__nowWorld, ui.__layout.renderBounds);
966
+ }
967
+ out.recycle(ui.__nowWorld);
968
+ break;
969
+ }
970
+ }
971
+ }
972
+ function strokes(strokes, ui, canvas) {
973
+ const options = ui.__;
974
+ const { __strokeWidth, strokeAlign, __font } = options;
975
+ if (!__strokeWidth)
976
+ return;
977
+ if (__font) {
978
+ strokeText(strokes, ui, canvas);
979
+ }
980
+ else {
981
+ switch (strokeAlign) {
982
+ case 'center':
983
+ canvas.setStroke(undefined, __strokeWidth, options);
984
+ drawStrokesStyle(strokes, false, ui, canvas);
985
+ break;
986
+ case 'inside':
987
+ canvas.save();
988
+ canvas.setStroke(undefined, __strokeWidth * 2, options);
989
+ options.windingRule ? canvas.clip(options.windingRule) : canvas.clip();
990
+ drawStrokesStyle(strokes, false, ui, canvas);
991
+ canvas.restore();
992
+ break;
993
+ case 'outside':
994
+ const { renderBounds } = ui.__layout;
995
+ const out = canvas.getSameCanvas(true, true);
996
+ ui.__drawRenderPath(out);
997
+ out.setStroke(undefined, __strokeWidth * 2, options);
998
+ drawStrokesStyle(strokes, false, ui, out);
999
+ options.windingRule ? out.clip(options.windingRule) : out.clip();
1000
+ out.clearWorld(renderBounds);
1001
+ if (ui.__worldFlipped) {
1002
+ canvas.copyWorldByReset(out, ui.__nowWorld);
1003
+ }
1004
+ else {
1005
+ canvas.copyWorldToInner(out, ui.__nowWorld, renderBounds);
1006
+ }
1007
+ out.recycle(ui.__nowWorld);
1008
+ break;
1009
+ }
1010
+ }
1011
+ }
1012
+
1013
+ const { getSpread, getOuterOf, getByMove, getIntersectData } = core.BoundsHelper;
1014
+ function shape(ui, current, options) {
1015
+ const canvas = current.getSameCanvas();
1016
+ const nowWorld = ui.__nowWorld;
1017
+ let bounds, fitMatrix, shapeBounds, worldCanvas;
1018
+ let { scaleX, scaleY } = nowWorld;
1019
+ if (scaleX < 0)
1020
+ scaleX = -scaleX;
1021
+ if (scaleY < 0)
1022
+ scaleY = -scaleY;
1023
+ if (current.bounds.includes(nowWorld)) {
1024
+ worldCanvas = canvas;
1025
+ bounds = shapeBounds = nowWorld;
1026
+ }
1027
+ else {
1028
+ const { renderShapeSpread: spread } = ui.__layout;
1029
+ const worldClipBounds = getIntersectData(spread ? getSpread(current.bounds, scaleX === scaleY ? spread * scaleX : [spread * scaleY, spread * scaleX]) : current.bounds, nowWorld);
1030
+ fitMatrix = current.bounds.getFitMatrix(worldClipBounds);
1031
+ let { a: fitScaleX, d: fitScaleY } = fitMatrix;
1032
+ if (fitMatrix.a < 1) {
1033
+ worldCanvas = current.getSameCanvas();
1034
+ ui.__renderShape(worldCanvas, options);
1035
+ scaleX *= fitScaleX;
1036
+ scaleY *= fitScaleY;
1037
+ }
1038
+ shapeBounds = getOuterOf(nowWorld, fitMatrix);
1039
+ bounds = getByMove(shapeBounds, -fitMatrix.e, -fitMatrix.f);
1040
+ if (options.matrix) {
1041
+ const { matrix } = options;
1042
+ fitMatrix.multiply(matrix);
1043
+ fitScaleX *= matrix.scaleX;
1044
+ fitScaleY *= matrix.scaleY;
1045
+ }
1046
+ options = Object.assign(Object.assign({}, options), { matrix: fitMatrix.withScale(fitScaleX, fitScaleY) });
1047
+ }
1048
+ ui.__renderShape(canvas, options);
1049
+ return {
1050
+ canvas, matrix: fitMatrix, bounds,
1051
+ worldCanvas, shapeBounds, scaleX, scaleY
1052
+ };
1053
+ }
1054
+
1055
+ let recycleMap;
1056
+ function compute(attrName, ui) {
1057
+ const data = ui.__, leafPaints = [];
1058
+ let paints = data.__input[attrName], hasOpacityPixel;
1059
+ if (!(paints instanceof Array))
1060
+ paints = [paints];
1061
+ recycleMap = draw.PaintImage.recycleImage(attrName, data);
1062
+ for (let i = 0, len = paints.length, item; i < len; i++) {
1063
+ item = getLeafPaint(attrName, paints[i], ui);
1064
+ if (item)
1065
+ leafPaints.push(item);
1066
+ }
1067
+ data['_' + attrName] = leafPaints.length ? leafPaints : undefined;
1068
+ if (leafPaints.length && leafPaints[0].image)
1069
+ hasOpacityPixel = leafPaints[0].image.hasOpacityPixel;
1070
+ if (attrName === 'fill') {
1071
+ data.__pixelFill = hasOpacityPixel;
1072
+ }
1073
+ else {
1074
+ data.__pixelStroke = hasOpacityPixel;
1075
+ }
1076
+ }
1077
+ function getLeafPaint(attrName, paint, ui) {
1078
+ if (typeof paint !== 'object' || paint.visible === false || paint.opacity === 0)
1079
+ return undefined;
1080
+ const { boxBounds } = ui.__layout;
1081
+ switch (paint.type) {
1082
+ case 'solid':
1083
+ let { type, blendMode, color, opacity } = paint;
1084
+ return { type, blendMode, style: draw.ColorConvert.string(color, opacity) };
1085
+ case 'image':
1086
+ return draw.PaintImage.image(ui, attrName, paint, boxBounds, !recycleMap || !recycleMap[paint.url]);
1087
+ case 'linear':
1088
+ return draw.PaintGradient.linearGradient(paint, boxBounds);
1089
+ case 'radial':
1090
+ return draw.PaintGradient.radialGradient(paint, boxBounds);
1091
+ case 'angular':
1092
+ return draw.PaintGradient.conicGradient(paint, boxBounds);
1093
+ default:
1094
+ return paint.r !== undefined ? { type: 'solid', style: draw.ColorConvert.string(paint) } : undefined;
1095
+ }
1096
+ }
1097
+
1098
+ const PaintModule = {
1099
+ compute,
1100
+ fill,
1101
+ fills,
1102
+ fillText,
1103
+ stroke,
1104
+ strokes,
1105
+ strokeText,
1106
+ drawTextStroke,
1107
+ shape
1108
+ };
1109
+
1110
+ let origin = {};
1111
+ const { get: get$3, rotateOfOuter: rotateOfOuter$1, translate: translate$1, scaleOfOuter: scaleOfOuter$1, scale: scaleHelper, rotate } = core.MatrixHelper;
1112
+ function fillOrFitMode(data, box, x, y, scaleX, scaleY, rotation) {
1113
+ const transform = get$3();
1114
+ translate$1(transform, box.x + x, box.y + y);
1115
+ scaleHelper(transform, scaleX, scaleY);
1116
+ if (rotation)
1117
+ rotateOfOuter$1(transform, { x: box.x + box.width / 2, y: box.y + box.height / 2 }, rotation);
1118
+ data.transform = transform;
1119
+ }
1120
+ function clipMode(data, box, x, y, scaleX, scaleY, rotation) {
1121
+ const transform = get$3();
1122
+ translate$1(transform, box.x + x, box.y + y);
1123
+ if (scaleX)
1124
+ scaleHelper(transform, scaleX, scaleY);
1125
+ if (rotation)
1126
+ rotate(transform, rotation);
1127
+ data.transform = transform;
1128
+ }
1129
+ function repeatMode(data, box, width, height, x, y, scaleX, scaleY, rotation, align) {
1130
+ const transform = get$3();
1131
+ if (rotation) {
1132
+ if (align === 'center') {
1133
+ rotateOfOuter$1(transform, { x: width / 2, y: height / 2 }, rotation);
1134
+ }
1135
+ else {
1136
+ rotate(transform, rotation);
1137
+ switch (rotation) {
1138
+ case 90:
1139
+ translate$1(transform, height, 0);
1140
+ break;
1141
+ case 180:
1142
+ translate$1(transform, width, height);
1143
+ break;
1144
+ case 270:
1145
+ translate$1(transform, 0, width);
1146
+ break;
1147
+ }
1148
+ }
1149
+ }
1150
+ origin.x = box.x + x;
1151
+ origin.y = box.y + y;
1152
+ translate$1(transform, origin.x, origin.y);
1153
+ if (scaleX)
1154
+ scaleOfOuter$1(transform, origin, scaleX, scaleY);
1155
+ data.transform = transform;
1156
+ }
1157
+
1158
+ const { get: get$2, translate } = core.MatrixHelper;
1159
+ const tempBox = new core.Bounds();
1160
+ const tempPoint = {};
1161
+ const tempScaleData = {};
1162
+ function createData(leafPaint, image, paint, box) {
1163
+ const { blendMode, sync } = paint;
1164
+ if (blendMode)
1165
+ leafPaint.blendMode = blendMode;
1166
+ if (sync)
1167
+ leafPaint.sync = sync;
1168
+ leafPaint.data = getPatternData(paint, box, image);
1169
+ }
1170
+ function getPatternData(paint, box, image) {
1171
+ let { width, height } = image;
1172
+ if (paint.padding)
1173
+ box = tempBox.set(box).shrink(paint.padding);
1174
+ const { opacity, mode, align, offset, scale, size, rotation, repeat } = paint;
1175
+ const sameBox = box.width === width && box.height === height;
1176
+ const data = { mode };
1177
+ const swapSize = align !== 'center' && (rotation || 0) % 180 === 90;
1178
+ const swapWidth = swapSize ? height : width, swapHeight = swapSize ? width : height;
1179
+ let x = 0, y = 0, scaleX, scaleY;
1180
+ if (!mode || mode === 'cover' || mode === 'fit') {
1181
+ if (!sameBox || rotation) {
1182
+ const sw = box.width / swapWidth, sh = box.height / swapHeight;
1183
+ scaleX = scaleY = mode === 'fit' ? Math.min(sw, sh) : Math.max(sw, sh);
1184
+ x += (box.width - width * scaleX) / 2, y += (box.height - height * scaleY) / 2;
1185
+ }
1186
+ }
1187
+ else if (scale || size) {
1188
+ core.MathHelper.getScaleData(scale, size, image, tempScaleData);
1189
+ scaleX = tempScaleData.scaleX;
1190
+ scaleY = tempScaleData.scaleY;
1191
+ }
1192
+ if (align) {
1193
+ const imageBounds = { x, y, width: swapWidth, height: swapHeight };
1194
+ if (scaleX)
1195
+ imageBounds.width *= scaleX, imageBounds.height *= scaleY;
1196
+ core.AlignHelper.toPoint(align, imageBounds, box, tempPoint, true);
1197
+ x += tempPoint.x, y += tempPoint.y;
1198
+ }
1199
+ if (offset)
1200
+ x += offset.x, y += offset.y;
1201
+ switch (mode) {
1202
+ case 'strench':
1203
+ if (!sameBox)
1204
+ width = box.width, height = box.height;
1205
+ break;
1206
+ case 'normal':
1207
+ case 'clip':
1208
+ if (x || y || scaleX || rotation)
1209
+ clipMode(data, box, x, y, scaleX, scaleY, rotation);
1210
+ break;
1211
+ case 'repeat':
1212
+ if (!sameBox || scaleX || rotation)
1213
+ repeatMode(data, box, width, height, x, y, scaleX, scaleY, rotation, align);
1214
+ if (!repeat)
1215
+ data.repeat = 'repeat';
1216
+ break;
1217
+ case 'fit':
1218
+ case 'cover':
1219
+ default:
1220
+ if (scaleX)
1221
+ fillOrFitMode(data, box, x, y, scaleX, scaleY, rotation);
1222
+ }
1223
+ if (!data.transform) {
1224
+ if (box.x || box.y) {
1225
+ data.transform = get$2();
1226
+ translate(data.transform, box.x, box.y);
1227
+ }
1228
+ }
1229
+ if (scaleX && mode !== 'strench') {
1230
+ data.scaleX = scaleX;
1231
+ data.scaleY = scaleY;
1232
+ }
1233
+ data.width = width;
1234
+ data.height = height;
1235
+ if (opacity)
1236
+ data.opacity = opacity;
1237
+ if (repeat)
1238
+ data.repeat = typeof repeat === 'string' ? (repeat === 'x' ? 'repeat-x' : 'repeat-y') : 'repeat';
1239
+ return data;
1240
+ }
1241
+
1242
+ let cache, box = new core.Bounds();
1243
+ const { isSame } = core.BoundsHelper;
1244
+ function image(ui, attrName, paint, boxBounds, firstUse) {
1245
+ let leafPaint, event;
1246
+ const image = core.ImageManager.get(paint);
1247
+ if (cache && paint === cache.paint && isSame(boxBounds, cache.boxBounds)) {
1248
+ leafPaint = cache.leafPaint;
1249
+ }
1250
+ else {
1251
+ leafPaint = { type: paint.type, image };
1252
+ cache = image.use > 1 ? { leafPaint, paint, boxBounds: box.set(boxBounds) } : null;
1253
+ }
1254
+ if (firstUse || image.loading)
1255
+ event = { image, attrName, attrValue: paint };
1256
+ if (image.ready) {
1257
+ checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds);
1258
+ if (firstUse) {
1259
+ onLoad(ui, event);
1260
+ onLoadSuccess(ui, event);
1261
+ }
1262
+ }
1263
+ else if (image.error) {
1264
+ if (firstUse)
1265
+ onLoadError(ui, event, image.error);
1266
+ }
1267
+ else {
1268
+ ignoreRender(ui, true);
1269
+ if (firstUse)
1270
+ onLoad(ui, event);
1271
+ leafPaint.loadId = image.load(() => {
1272
+ ignoreRender(ui, false);
1273
+ if (!ui.destroyed) {
1274
+ if (checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds)) {
1275
+ if (image.hasOpacityPixel)
1276
+ ui.__layout.hitCanvasChanged = true;
1277
+ ui.forceUpdate('surface');
1278
+ }
1279
+ onLoadSuccess(ui, event);
1280
+ }
1281
+ leafPaint.loadId = null;
1282
+ }, (error) => {
1283
+ ignoreRender(ui, false);
1284
+ onLoadError(ui, event, error);
1285
+ leafPaint.loadId = null;
1286
+ });
1287
+ }
1288
+ return leafPaint;
1289
+ }
1290
+ function checkSizeAndCreateData(ui, attrName, paint, image, leafPaint, boxBounds) {
1291
+ if (attrName === 'fill' && !ui.__.__naturalWidth) {
1292
+ const data = ui.__;
1293
+ data.__naturalWidth = image.width / data.pixelRatio;
1294
+ data.__naturalHeight = image.height / data.pixelRatio;
1295
+ if (data.__autoSide) {
1296
+ ui.forceUpdate('width');
1297
+ if (ui.__proxyData) {
1298
+ ui.setProxyAttr('width', data.width);
1299
+ ui.setProxyAttr('height', data.height);
1300
+ }
1301
+ return false;
1302
+ }
1303
+ }
1304
+ if (!leafPaint.data)
1305
+ createData(leafPaint, image, paint, boxBounds);
1306
+ return true;
1307
+ }
1308
+ function onLoad(ui, event) {
1309
+ emit(ui, core.ImageEvent.LOAD, event);
1310
+ }
1311
+ function onLoadSuccess(ui, event) {
1312
+ emit(ui, core.ImageEvent.LOADED, event);
1313
+ }
1314
+ function onLoadError(ui, event, error) {
1315
+ event.error = error;
1316
+ ui.forceUpdate('surface');
1317
+ emit(ui, core.ImageEvent.ERROR, event);
1318
+ }
1319
+ function emit(ui, type, data) {
1320
+ if (ui.hasEvent(type))
1321
+ ui.emitEvent(new core.ImageEvent(type, data));
1322
+ }
1323
+ function ignoreRender(ui, value) {
1324
+ const { leafer } = ui;
1325
+ if (leafer && leafer.viewReady)
1326
+ leafer.renderer.ignore = value;
1327
+ }
1328
+
1329
+ const { get: get$1, scale, copy: copy$1 } = core.MatrixHelper;
1330
+ const { ceil, abs: abs$1 } = Math;
1331
+ function createPattern(ui, paint, pixelRatio) {
1332
+ let { scaleX, scaleY } = core.ImageManager.patternLocked ? ui.__world : ui.__nowWorld;
1333
+ const id = scaleX + '-' + scaleY;
1334
+ if (paint.patternId !== id && !ui.destroyed) {
1335
+ scaleX = abs$1(scaleX);
1336
+ scaleY = abs$1(scaleY);
1337
+ const { image, data } = paint;
1338
+ let imageScale, imageMatrix, { width, height, scaleX: sx, scaleY: sy, opacity, transform, repeat } = data;
1339
+ if (sx) {
1340
+ imageMatrix = get$1();
1341
+ copy$1(imageMatrix, transform);
1342
+ scale(imageMatrix, 1 / sx, 1 / sy);
1343
+ scaleX *= sx;
1344
+ scaleY *= sy;
1345
+ }
1346
+ scaleX *= pixelRatio;
1347
+ scaleY *= pixelRatio;
1348
+ width *= scaleX;
1349
+ height *= scaleY;
1350
+ const size = width * height;
1351
+ if (!repeat) {
1352
+ if (size > core.Platform.image.maxCacheSize)
1353
+ return false;
1354
+ }
1355
+ let maxSize = core.Platform.image.maxPatternSize;
1356
+ if (!image.isSVG) {
1357
+ const imageSize = image.width * image.height;
1358
+ if (maxSize > imageSize)
1359
+ maxSize = imageSize;
1360
+ }
1361
+ if (size > maxSize)
1362
+ imageScale = Math.sqrt(size / maxSize);
1363
+ if (imageScale) {
1364
+ scaleX /= imageScale;
1365
+ scaleY /= imageScale;
1366
+ width /= imageScale;
1367
+ height /= imageScale;
1368
+ }
1369
+ if (sx) {
1370
+ scaleX /= sx;
1371
+ scaleY /= sy;
1372
+ }
1373
+ if (transform || scaleX !== 1 || scaleY !== 1) {
1374
+ if (!imageMatrix) {
1375
+ imageMatrix = get$1();
1376
+ if (transform)
1377
+ copy$1(imageMatrix, transform);
1378
+ }
1379
+ scale(imageMatrix, 1 / scaleX, 1 / scaleY);
1380
+ }
1381
+ const canvas = image.getCanvas(ceil(width) || 1, ceil(height) || 1, opacity);
1382
+ const pattern = image.getPattern(canvas, repeat || (core.Platform.origin.noRepeat || 'no-repeat'), imageMatrix, paint);
1383
+ paint.style = pattern;
1384
+ paint.patternId = id;
1385
+ return true;
1386
+ }
1387
+ else {
1388
+ return false;
1389
+ }
1390
+ }
1391
+
1392
+ /******************************************************************************
1393
+ Copyright (c) Microsoft Corporation.
1394
+
1395
+ Permission to use, copy, modify, and/or distribute this software for any
1396
+ purpose with or without fee is hereby granted.
1397
+
1398
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
1399
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1400
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
1401
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1402
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
1403
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1404
+ PERFORMANCE OF THIS SOFTWARE.
1405
+ ***************************************************************************** */
1406
+ /* global Reflect, Promise, SuppressedError, Symbol */
1407
+
1408
+
1409
+ function __awaiter(thisArg, _arguments, P, generator) {
1410
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1411
+ return new (P || (P = Promise))(function (resolve, reject) {
1412
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1413
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1414
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1415
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
1416
+ });
1417
+ }
1418
+
1419
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
1420
+ var e = new Error(message);
1421
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1422
+ };
1423
+
1424
+ const { abs } = Math;
1425
+ function checkImage(ui, canvas, paint, allowPaint) {
1426
+ const { scaleX, scaleY } = core.ImageManager.patternLocked ? ui.__world : ui.__nowWorld;
1427
+ if (!paint.data || (paint.patternId === scaleX + '-' + scaleY && !draw.Export.running)) {
1428
+ return false;
1429
+ }
1430
+ else {
1431
+ const { data } = paint;
1432
+ if (allowPaint) {
1433
+ if (!data.repeat) {
1434
+ let { width, height } = data;
1435
+ width *= abs(scaleX) * canvas.pixelRatio;
1436
+ height *= abs(scaleY) * canvas.pixelRatio;
1437
+ if (data.scaleX) {
1438
+ width *= data.scaleX;
1439
+ height *= data.scaleY;
1440
+ }
1441
+ allowPaint = (width * height > core.Platform.image.maxCacheSize) || draw.Export.running;
1442
+ }
1443
+ else {
1444
+ allowPaint = false;
1445
+ }
1446
+ }
1447
+ if (allowPaint) {
1448
+ canvas.save();
1449
+ canvas.clip();
1450
+ if (paint.blendMode)
1451
+ canvas.blendMode = paint.blendMode;
1452
+ if (data.opacity)
1453
+ canvas.opacity *= data.opacity;
1454
+ if (data.transform)
1455
+ canvas.transform(data.transform);
1456
+ canvas.drawImage(paint.image.view, 0, 0, data.width, data.height);
1457
+ canvas.restore();
1458
+ return true;
1459
+ }
1460
+ else {
1461
+ if (!paint.style || paint.sync || draw.Export.running) {
1462
+ createPattern(ui, paint, canvas.pixelRatio);
1463
+ }
1464
+ else {
1465
+ if (!paint.patternTask) {
1466
+ paint.patternTask = core.ImageManager.patternTasker.add(() => __awaiter(this, void 0, void 0, function* () {
1467
+ paint.patternTask = null;
1468
+ if (canvas.bounds.hit(ui.__nowWorld))
1469
+ createPattern(ui, paint, canvas.pixelRatio);
1470
+ ui.forceUpdate('surface');
1471
+ }), 300);
1472
+ }
1473
+ }
1474
+ return false;
1475
+ }
1476
+ }
1477
+ }
1478
+
1479
+ function recycleImage(attrName, data) {
1480
+ const paints = data['_' + attrName];
1481
+ if (paints instanceof Array) {
1482
+ let image, recycleMap, input, url;
1483
+ for (let i = 0, len = paints.length; i < len; i++) {
1484
+ image = paints[i].image;
1485
+ url = image && image.url;
1486
+ if (url) {
1487
+ if (!recycleMap)
1488
+ recycleMap = {};
1489
+ recycleMap[url] = true;
1490
+ core.ImageManager.recycle(image);
1491
+ if (image.loading) {
1492
+ if (!input) {
1493
+ input = (data.__input && data.__input[attrName]) || [];
1494
+ if (!(input instanceof Array))
1495
+ input = [input];
1496
+ }
1497
+ image.unload(paints[i].loadId, !input.some((item) => item.url === url));
1498
+ }
1499
+ }
1500
+ }
1501
+ return recycleMap;
1502
+ }
1503
+ return null;
1504
+ }
1505
+
1506
+ const PaintImageModule = {
1507
+ image,
1508
+ checkImage,
1509
+ createPattern,
1510
+ recycleImage,
1511
+ createData,
1512
+ getPatternData,
1513
+ fillOrFitMode,
1514
+ clipMode,
1515
+ repeatMode
1516
+ };
1517
+
1518
+ const { toPoint: toPoint$2 } = core.AroundHelper;
1519
+ const realFrom$2 = {};
1520
+ const realTo$2 = {};
1521
+ function linearGradient(paint, box) {
1522
+ let { from, to, type, blendMode, opacity } = paint;
1523
+ toPoint$2(from || 'top', box, realFrom$2);
1524
+ toPoint$2(to || 'bottom', box, realTo$2);
1525
+ const style = core.Platform.canvas.createLinearGradient(realFrom$2.x, realFrom$2.y, realTo$2.x, realTo$2.y);
1526
+ applyStops(style, paint.stops, opacity);
1527
+ const data = { type, style };
1528
+ if (blendMode)
1529
+ data.blendMode = blendMode;
1530
+ return data;
1531
+ }
1532
+ function applyStops(gradient, stops, opacity) {
1533
+ let stop;
1534
+ for (let i = 0, len = stops.length; i < len; i++) {
1535
+ stop = stops[i];
1536
+ if (typeof stop === 'string') {
1537
+ gradient.addColorStop(i / (len - 1), draw.ColorConvert.string(stop, opacity));
1538
+ }
1539
+ else {
1540
+ gradient.addColorStop(stop.offset, draw.ColorConvert.string(stop.color, opacity));
1541
+ }
1542
+ }
1543
+ }
1544
+
1545
+ const { getAngle, getDistance: getDistance$1 } = core.PointHelper;
1546
+ const { get, rotateOfOuter, scaleOfOuter } = core.MatrixHelper;
1547
+ const { toPoint: toPoint$1 } = core.AroundHelper;
1548
+ const realFrom$1 = {};
1549
+ const realTo$1 = {};
1550
+ function radialGradient(paint, box) {
1551
+ let { from, to, type, opacity, blendMode, stretch } = paint;
1552
+ toPoint$1(from || 'center', box, realFrom$1);
1553
+ toPoint$1(to || 'bottom', box, realTo$1);
1554
+ const style = core.Platform.canvas.createRadialGradient(realFrom$1.x, realFrom$1.y, 0, realFrom$1.x, realFrom$1.y, getDistance$1(realFrom$1, realTo$1));
1555
+ applyStops(style, paint.stops, opacity);
1556
+ const data = { type, style };
1557
+ const transform = getTransform(box, realFrom$1, realTo$1, stretch, true);
1558
+ if (transform)
1559
+ data.transform = transform;
1560
+ if (blendMode)
1561
+ data.blendMode = blendMode;
1562
+ return data;
1563
+ }
1564
+ function getTransform(box, from, to, stretch, rotate90) {
1565
+ let transform;
1566
+ const { width, height } = box;
1567
+ if (width !== height || stretch) {
1568
+ const angle = getAngle(from, to);
1569
+ transform = get();
1570
+ if (rotate90) {
1571
+ scaleOfOuter(transform, from, width / height * (stretch || 1), 1);
1572
+ rotateOfOuter(transform, from, angle + 90);
1573
+ }
1574
+ else {
1575
+ scaleOfOuter(transform, from, 1, width / height * (stretch || 1));
1576
+ rotateOfOuter(transform, from, angle);
1577
+ }
1578
+ }
1579
+ return transform;
1580
+ }
1581
+
1582
+ const { getDistance } = core.PointHelper;
1583
+ const { toPoint } = core.AroundHelper;
1584
+ const realFrom = {};
1585
+ const realTo = {};
1586
+ function conicGradient(paint, box) {
1587
+ let { from, to, type, opacity, blendMode, stretch } = paint;
1588
+ toPoint(from || 'center', box, realFrom);
1589
+ toPoint(to || 'bottom', box, realTo);
1590
+ const style = core.Platform.conicGradientSupport ? core.Platform.canvas.createConicGradient(0, realFrom.x, realFrom.y) : core.Platform.canvas.createRadialGradient(realFrom.x, realFrom.y, 0, realFrom.x, realFrom.y, getDistance(realFrom, realTo));
1591
+ applyStops(style, paint.stops, opacity);
1592
+ const data = { type, style };
1593
+ const transform = getTransform(box, realFrom, realTo, stretch || 1, core.Platform.conicGradientRotate90);
1594
+ if (transform)
1595
+ data.transform = transform;
1596
+ if (blendMode)
1597
+ data.blendMode = blendMode;
1598
+ return data;
1599
+ }
1600
+
1601
+ const PaintGradientModule = {
1602
+ linearGradient,
1603
+ radialGradient,
1604
+ conicGradient,
1605
+ getTransform
1606
+ };
1607
+
1608
+ const { copy, toOffsetOutBounds: toOffsetOutBounds$1 } = core.BoundsHelper;
1609
+ const tempBounds = {};
1610
+ const offsetOutBounds$1 = {};
1611
+ function shadow(ui, current, shape) {
1612
+ let copyBounds, spreadScale;
1613
+ const { __nowWorld: nowWorld, __layout } = ui;
1614
+ const { shadow } = ui.__;
1615
+ const { worldCanvas, bounds, shapeBounds, scaleX, scaleY } = shape;
1616
+ const other = current.getSameCanvas();
1617
+ const end = shadow.length - 1;
1618
+ toOffsetOutBounds$1(bounds, offsetOutBounds$1);
1619
+ shadow.forEach((item, index) => {
1620
+ other.setWorldShadow((offsetOutBounds$1.offsetX + item.x * scaleX), (offsetOutBounds$1.offsetY + item.y * scaleY), item.blur * scaleX, item.color);
1621
+ spreadScale = item.spread ? 1 + item.spread * 2 / (__layout.boxBounds.width + (__layout.strokeBoxSpread || 0) * 2) : 0;
1622
+ drawWorldShadow(other, offsetOutBounds$1, spreadScale, shape);
1623
+ copyBounds = bounds;
1624
+ if (item.box) {
1625
+ other.restore();
1626
+ other.save();
1627
+ if (worldCanvas) {
1628
+ other.copyWorld(other, bounds, nowWorld, 'copy');
1629
+ copyBounds = nowWorld;
1630
+ }
1631
+ worldCanvas ? other.copyWorld(worldCanvas, nowWorld, nowWorld, 'destination-out') : other.copyWorld(shape.canvas, shapeBounds, bounds, 'destination-out');
1632
+ }
1633
+ if (ui.__worldFlipped) {
1634
+ current.copyWorldByReset(other, copyBounds, nowWorld, item.blendMode);
1635
+ }
1636
+ else {
1637
+ current.copyWorldToInner(other, copyBounds, __layout.renderBounds, item.blendMode);
1638
+ }
1639
+ if (end && index < end)
1640
+ other.clearWorld(copyBounds, true);
1641
+ });
1642
+ other.recycle(copyBounds);
1643
+ }
1644
+ function drawWorldShadow(canvas, outBounds, spreadScale, shape) {
1645
+ const { bounds, shapeBounds } = shape;
1646
+ if (core.Platform.fullImageShadow) {
1647
+ copy(tempBounds, canvas.bounds);
1648
+ tempBounds.x += (outBounds.x - shapeBounds.x);
1649
+ tempBounds.y += (outBounds.y - shapeBounds.y);
1650
+ if (spreadScale) {
1651
+ const { matrix } = shape;
1652
+ tempBounds.x -= (bounds.x + (matrix ? matrix.e : 0) + bounds.width / 2) * (spreadScale - 1);
1653
+ tempBounds.y -= (bounds.y + (matrix ? matrix.f : 0) + bounds.height / 2) * (spreadScale - 1);
1654
+ tempBounds.width *= spreadScale;
1655
+ tempBounds.height *= spreadScale;
1656
+ }
1657
+ canvas.copyWorld(shape.canvas, canvas.bounds, tempBounds);
1658
+ }
1659
+ else {
1660
+ if (spreadScale) {
1661
+ copy(tempBounds, outBounds);
1662
+ tempBounds.x -= (outBounds.width / 2) * (spreadScale - 1);
1663
+ tempBounds.y -= (outBounds.height / 2) * (spreadScale - 1);
1664
+ tempBounds.width *= spreadScale;
1665
+ tempBounds.height *= spreadScale;
1666
+ }
1667
+ canvas.copyWorld(shape.canvas, shapeBounds, spreadScale ? tempBounds : outBounds);
1668
+ }
1669
+ }
1670
+
1671
+ const { toOffsetOutBounds } = core.BoundsHelper;
1672
+ const offsetOutBounds = {};
1673
+ function innerShadow(ui, current, shape) {
1674
+ let copyBounds, spreadScale;
1675
+ const { __nowWorld: nowWorld, __layout: __layout } = ui;
1676
+ const { innerShadow } = ui.__;
1677
+ const { worldCanvas, bounds, shapeBounds, scaleX, scaleY } = shape;
1678
+ const other = current.getSameCanvas();
1679
+ const end = innerShadow.length - 1;
1680
+ toOffsetOutBounds(bounds, offsetOutBounds);
1681
+ innerShadow.forEach((item, index) => {
1682
+ other.save();
1683
+ other.setWorldShadow((offsetOutBounds.offsetX + item.x * scaleX), (offsetOutBounds.offsetY + item.y * scaleY), item.blur * scaleX);
1684
+ spreadScale = item.spread ? 1 - item.spread * 2 / (__layout.boxBounds.width + (__layout.strokeBoxSpread || 0) * 2) : 0;
1685
+ drawWorldShadow(other, offsetOutBounds, spreadScale, shape);
1686
+ other.restore();
1687
+ if (worldCanvas) {
1688
+ other.copyWorld(other, bounds, nowWorld, 'copy');
1689
+ other.copyWorld(worldCanvas, nowWorld, nowWorld, 'source-out');
1690
+ copyBounds = nowWorld;
1691
+ }
1692
+ else {
1693
+ other.copyWorld(shape.canvas, shapeBounds, bounds, 'source-out');
1694
+ copyBounds = bounds;
1695
+ }
1696
+ other.fillWorld(copyBounds, item.color, 'source-in');
1697
+ if (ui.__worldFlipped) {
1698
+ current.copyWorldByReset(other, copyBounds, nowWorld, item.blendMode);
1699
+ }
1700
+ else {
1701
+ current.copyWorldToInner(other, copyBounds, __layout.renderBounds, item.blendMode);
1702
+ }
1703
+ if (end && index < end)
1704
+ other.clearWorld(copyBounds, true);
1705
+ });
1706
+ other.recycle(copyBounds);
1707
+ }
1708
+
1709
+ function blur(ui, current, origin) {
1710
+ const { blur } = ui.__;
1711
+ origin.setWorldBlur(blur * ui.__nowWorld.a);
1712
+ origin.copyWorldToInner(current, ui.__nowWorld, ui.__layout.renderBounds);
1713
+ origin.filter = 'none';
1714
+ }
1715
+
1716
+ function backgroundBlur(_ui, _current, _shape) {
1717
+ }
1718
+
1719
+ const EffectModule = {
1720
+ shadow,
1721
+ innerShadow,
1722
+ blur,
1723
+ backgroundBlur
1724
+ };
1725
+
1726
+ const { excludeRenderBounds } = core.LeafBoundsHelper;
1727
+ draw.Group.prototype.__renderMask = function (canvas, options) {
1728
+ let child, maskCanvas, contentCanvas, maskOpacity, currentMask;
1729
+ const { children } = this;
1730
+ for (let i = 0, len = children.length; i < len; i++) {
1731
+ child = children[i];
1732
+ if (child.__.mask) {
1733
+ if (currentMask) {
1734
+ maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity);
1735
+ maskCanvas = contentCanvas = null;
1736
+ }
1737
+ if (child.__.mask === 'path') {
1738
+ if (child.opacity < 1) {
1739
+ currentMask = 'opacity-path';
1740
+ maskOpacity = child.opacity;
1741
+ if (!contentCanvas)
1742
+ contentCanvas = getCanvas(canvas);
1743
+ }
1744
+ else {
1745
+ currentMask = 'path';
1746
+ canvas.save();
1747
+ }
1748
+ child.__clip(contentCanvas || canvas, options);
1749
+ }
1750
+ else {
1751
+ currentMask = 'alpha';
1752
+ if (!maskCanvas)
1753
+ maskCanvas = getCanvas(canvas);
1754
+ if (!contentCanvas)
1755
+ contentCanvas = getCanvas(canvas);
1756
+ child.__render(maskCanvas, options);
1757
+ }
1758
+ if (child.__.mask !== 'clipping')
1759
+ continue;
1760
+ }
1761
+ if (excludeRenderBounds(child, options))
1762
+ continue;
1763
+ child.__render(contentCanvas || canvas, options);
1764
+ }
1765
+ maskEnd(this, currentMask, canvas, contentCanvas, maskCanvas, maskOpacity);
1766
+ };
1767
+ function maskEnd(leaf, maskMode, canvas, contentCanvas, maskCanvas, maskOpacity) {
1768
+ switch (maskMode) {
1769
+ case 'alpha':
1770
+ usePixelMask(leaf, canvas, contentCanvas, maskCanvas);
1771
+ break;
1772
+ case 'opacity-path':
1773
+ copyContent(leaf, canvas, contentCanvas, maskOpacity);
1774
+ break;
1775
+ case 'path':
1776
+ canvas.restore();
1777
+ }
1778
+ }
1779
+ function getCanvas(canvas) {
1780
+ return canvas.getSameCanvas(false, true);
1781
+ }
1782
+ function usePixelMask(leaf, canvas, content, mask) {
1783
+ const realBounds = leaf.__nowWorld;
1784
+ content.resetTransform();
1785
+ content.opacity = 1;
1786
+ content.useMask(mask, realBounds);
1787
+ mask.recycle(realBounds);
1788
+ copyContent(leaf, canvas, content, 1);
1789
+ }
1790
+ function copyContent(leaf, canvas, content, maskOpacity) {
1791
+ const realBounds = leaf.__nowWorld;
1792
+ canvas.resetTransform();
1793
+ canvas.opacity = maskOpacity;
1794
+ canvas.copyWorld(content, realBounds);
1795
+ content.recycle(realBounds);
1796
+ }
1797
+
1798
+ const money = '¥¥$€££¢¢';
1799
+ const letter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
1800
+ const langBefore = '《(「〈『〖【〔{┌<‘“=' + money;
1801
+ const langAfter = '》)」〉』〗】〕}┐>’”!?,、。:;‰';
1802
+ const langSymbol = '≮≯≈≠=…';
1803
+ const langBreak$1 = '—/~|┆·';
1804
+ const beforeChar = '{[(<\'"' + langBefore;
1805
+ const afterChar = '>)]}%!?,.:;\'"' + langAfter;
1806
+ const symbolChar = afterChar + '_#~&*+\\=|' + langSymbol;
1807
+ const breakChar = '- ' + langBreak$1;
1808
+ const cjkRangeList = [
1809
+ [0x4E00, 0x9FFF],
1810
+ [0x3400, 0x4DBF],
1811
+ [0x20000, 0x2A6DF],
1812
+ [0x2A700, 0x2B73F],
1813
+ [0x2B740, 0x2B81F],
1814
+ [0x2B820, 0x2CEAF],
1815
+ [0x2CEB0, 0x2EBEF],
1816
+ [0x30000, 0x3134F],
1817
+ [0x31350, 0x323AF],
1818
+ [0x2E80, 0x2EFF],
1819
+ [0x2F00, 0x2FDF],
1820
+ [0x2FF0, 0x2FFF],
1821
+ [0x3000, 0x303F],
1822
+ [0x31C0, 0x31EF],
1823
+ [0x3200, 0x32FF],
1824
+ [0x3300, 0x33FF],
1825
+ [0xF900, 0xFAFF],
1826
+ [0xFE30, 0xFE4F],
1827
+ [0x1F200, 0x1F2FF],
1828
+ [0x2F800, 0x2FA1F],
1829
+ ];
1830
+ const cjkReg = new RegExp(cjkRangeList.map(([start, end]) => `[\\u${start.toString(16)}-\\u${end.toString(16)}]`).join('|'));
1831
+ function mapChar(str) {
1832
+ const map = {};
1833
+ str.split('').forEach(char => map[char] = true);
1834
+ return map;
1835
+ }
1836
+ const letterMap = mapChar(letter);
1837
+ const beforeMap = mapChar(beforeChar);
1838
+ const afterMap = mapChar(afterChar);
1839
+ const symbolMap = mapChar(symbolChar);
1840
+ const breakMap = mapChar(breakChar);
1841
+ var CharType;
1842
+ (function (CharType) {
1843
+ CharType[CharType["Letter"] = 0] = "Letter";
1844
+ CharType[CharType["Single"] = 1] = "Single";
1845
+ CharType[CharType["Before"] = 2] = "Before";
1846
+ CharType[CharType["After"] = 3] = "After";
1847
+ CharType[CharType["Symbol"] = 4] = "Symbol";
1848
+ CharType[CharType["Break"] = 5] = "Break";
1849
+ })(CharType || (CharType = {}));
1850
+ const { Letter: Letter$1, Single: Single$1, Before: Before$1, After: After$1, Symbol: Symbol$1, Break: Break$1 } = CharType;
1851
+ function getCharType(char) {
1852
+ if (letterMap[char]) {
1853
+ return Letter$1;
1854
+ }
1855
+ else if (breakMap[char]) {
1856
+ return Break$1;
1857
+ }
1858
+ else if (beforeMap[char]) {
1859
+ return Before$1;
1860
+ }
1861
+ else if (afterMap[char]) {
1862
+ return After$1;
1863
+ }
1864
+ else if (symbolMap[char]) {
1865
+ return Symbol$1;
1866
+ }
1867
+ else if (cjkReg.test(char)) {
1868
+ return Single$1;
1869
+ }
1870
+ else {
1871
+ return Letter$1;
1872
+ }
1873
+ }
1874
+
1875
+ const TextRowHelper = {
1876
+ trimRight(row) {
1877
+ const { words } = row;
1878
+ let trimRight = 0, len = words.length, char;
1879
+ for (let i = len - 1; i > -1; i--) {
1880
+ char = words[i].data[0];
1881
+ if (char.char === ' ') {
1882
+ trimRight++;
1883
+ row.width -= char.width;
1884
+ }
1885
+ else {
1886
+ break;
1887
+ }
1888
+ }
1889
+ if (trimRight)
1890
+ words.splice(len - trimRight, trimRight);
1891
+ }
1892
+ };
1893
+
1894
+ function getTextCase(char, textCase, firstChar) {
1895
+ switch (textCase) {
1896
+ case 'title':
1897
+ return firstChar ? char.toUpperCase() : char;
1898
+ case 'upper':
1899
+ return char.toUpperCase();
1900
+ case 'lower':
1901
+ return char.toLowerCase();
1902
+ default:
1903
+ return char;
1904
+ }
1905
+ }
1906
+
1907
+ const { trimRight } = TextRowHelper;
1908
+ const { Letter, Single, Before, After, Symbol, Break } = CharType;
1909
+ let word, row, wordWidth, rowWidth, realWidth;
1910
+ let char, charWidth, startCharSize, charSize, charType, lastCharType, langBreak, afterBreak, paraStart;
1911
+ let textDrawData, rows = [], bounds;
1912
+ function createRows(drawData, content, style) {
1913
+ textDrawData = drawData;
1914
+ rows = drawData.rows;
1915
+ bounds = drawData.bounds;
1916
+ const { __letterSpacing, paraIndent, textCase } = style;
1917
+ const { canvas } = core.Platform;
1918
+ const { width, height } = bounds;
1919
+ const charMode = width || height || __letterSpacing || (textCase !== 'none');
1920
+ if (charMode) {
1921
+ const wrap = style.textWrap !== 'none';
1922
+ const breakAll = style.textWrap === 'break';
1923
+ paraStart = true;
1924
+ lastCharType = null;
1925
+ startCharSize = charWidth = charSize = wordWidth = rowWidth = 0;
1926
+ word = { data: [] }, row = { words: [] };
1927
+ for (let i = 0, len = content.length; i < len; i++) {
1928
+ char = content[i];
1929
+ if (char === '\n') {
1930
+ if (wordWidth)
1931
+ addWord();
1932
+ row.paraEnd = true;
1933
+ addRow();
1934
+ paraStart = true;
1935
+ }
1936
+ else {
1937
+ charType = getCharType(char);
1938
+ if (charType === Letter && textCase !== 'none')
1939
+ char = getTextCase(char, textCase, !wordWidth);
1940
+ charWidth = canvas.measureText(char).width;
1941
+ if (__letterSpacing) {
1942
+ if (__letterSpacing < 0)
1943
+ charSize = charWidth;
1944
+ charWidth += __letterSpacing;
1945
+ }
1946
+ langBreak = (charType === Single && (lastCharType === Single || lastCharType === Letter)) || (lastCharType === Single && charType !== After);
1947
+ afterBreak = ((charType === Before || charType === Single) && (lastCharType === Symbol || lastCharType === After));
1948
+ realWidth = paraStart && paraIndent ? width - paraIndent : width;
1949
+ if (wrap && (width && rowWidth + wordWidth + charWidth > realWidth)) {
1950
+ if (breakAll) {
1951
+ if (wordWidth)
1952
+ addWord();
1953
+ if (rowWidth)
1954
+ addRow();
1955
+ }
1956
+ else {
1957
+ if (!afterBreak)
1958
+ afterBreak = charType === Letter && lastCharType == After;
1959
+ if (langBreak || afterBreak || charType === Break || charType === Before || charType === Single || (wordWidth + charWidth > realWidth)) {
1960
+ if (wordWidth)
1961
+ addWord();
1962
+ if (rowWidth)
1963
+ addRow();
1964
+ }
1965
+ else {
1966
+ if (rowWidth)
1967
+ addRow();
1968
+ }
1969
+ }
1970
+ }
1971
+ if (char === ' ' && paraStart !== true && (rowWidth + wordWidth) === 0) ;
1972
+ else {
1973
+ if (charType === Break) {
1974
+ if (char === ' ' && wordWidth)
1975
+ addWord();
1976
+ addChar(char, charWidth);
1977
+ addWord();
1978
+ }
1979
+ else if (langBreak || afterBreak) {
1980
+ if (wordWidth)
1981
+ addWord();
1982
+ addChar(char, charWidth);
1983
+ }
1984
+ else {
1985
+ addChar(char, charWidth);
1986
+ }
1987
+ }
1988
+ lastCharType = charType;
1989
+ }
1990
+ }
1991
+ if (wordWidth)
1992
+ addWord();
1993
+ if (rowWidth)
1994
+ addRow();
1995
+ rows.length > 0 && (rows[rows.length - 1].paraEnd = true);
1996
+ }
1997
+ else {
1998
+ content.split('\n').forEach(content => {
1999
+ textDrawData.paraNumber++;
2000
+ rows.push({ x: paraIndent || 0, text: content, width: canvas.measureText(content).width, paraStart: true });
2001
+ });
2002
+ }
2003
+ }
2004
+ function addChar(char, width) {
2005
+ if (charSize && !startCharSize)
2006
+ startCharSize = charSize;
2007
+ word.data.push({ char, width });
2008
+ wordWidth += width;
2009
+ }
2010
+ function addWord() {
2011
+ rowWidth += wordWidth;
2012
+ word.width = wordWidth;
2013
+ row.words.push(word);
2014
+ word = { data: [] };
2015
+ wordWidth = 0;
2016
+ }
2017
+ function addRow() {
2018
+ if (paraStart) {
2019
+ textDrawData.paraNumber++;
2020
+ row.paraStart = true;
2021
+ paraStart = false;
2022
+ }
2023
+ if (charSize) {
2024
+ row.startCharSize = startCharSize;
2025
+ row.endCharSize = charSize;
2026
+ startCharSize = 0;
2027
+ }
2028
+ row.width = rowWidth;
2029
+ if (bounds.width)
2030
+ trimRight(row);
2031
+ rows.push(row);
2032
+ row = { words: [] };
2033
+ rowWidth = 0;
2034
+ }
2035
+
2036
+ const CharMode = 0;
2037
+ const WordMode = 1;
2038
+ const TextMode = 2;
2039
+ function layoutChar(drawData, style, width, _height) {
2040
+ const { rows } = drawData;
2041
+ const { textAlign, paraIndent, letterSpacing } = style;
2042
+ let charX, addWordWidth, indentWidth, mode, wordChar;
2043
+ rows.forEach(row => {
2044
+ if (row.words) {
2045
+ indentWidth = paraIndent && row.paraStart ? paraIndent : 0;
2046
+ addWordWidth = (width && textAlign === 'justify' && row.words.length > 1) ? (width - row.width - indentWidth) / (row.words.length - 1) : 0;
2047
+ mode = (letterSpacing || row.isOverflow) ? CharMode : (addWordWidth > 0.01 ? WordMode : TextMode);
2048
+ if (row.isOverflow && !letterSpacing)
2049
+ row.textMode = true;
2050
+ if (mode === TextMode) {
2051
+ row.x += indentWidth;
2052
+ toTextChar$1(row);
2053
+ }
2054
+ else {
2055
+ row.x += indentWidth;
2056
+ charX = row.x;
2057
+ row.data = [];
2058
+ row.words.forEach(word => {
2059
+ if (mode === WordMode) {
2060
+ wordChar = { char: '', x: charX };
2061
+ charX = toWordChar(word.data, charX, wordChar);
2062
+ if (row.isOverflow || wordChar.char !== ' ')
2063
+ row.data.push(wordChar);
2064
+ }
2065
+ else {
2066
+ charX = toChar(word.data, charX, row.data, row.isOverflow);
2067
+ }
2068
+ if (!row.paraEnd && addWordWidth) {
2069
+ charX += addWordWidth;
2070
+ row.width += addWordWidth;
2071
+ }
2072
+ });
2073
+ }
2074
+ row.words = null;
2075
+ }
2076
+ });
2077
+ }
2078
+ function toTextChar$1(row) {
2079
+ row.text = '';
2080
+ row.words.forEach(word => {
2081
+ word.data.forEach(char => {
2082
+ row.text += char.char;
2083
+ });
2084
+ });
2085
+ }
2086
+ function toWordChar(data, charX, wordChar) {
2087
+ data.forEach(char => {
2088
+ wordChar.char += char.char;
2089
+ charX += char.width;
2090
+ });
2091
+ return charX;
2092
+ }
2093
+ function toChar(data, charX, rowData, isOverflow) {
2094
+ data.forEach(char => {
2095
+ if (isOverflow || char.char !== ' ') {
2096
+ char.x = charX;
2097
+ rowData.push(char);
2098
+ }
2099
+ charX += char.width;
2100
+ });
2101
+ return charX;
2102
+ }
2103
+
2104
+ function layoutText(drawData, style) {
2105
+ const { rows, bounds } = drawData;
2106
+ const { __lineHeight, __baseLine, __letterSpacing, __clipText, textAlign, verticalAlign, paraSpacing } = style;
2107
+ let { x, y, width, height } = bounds, realHeight = __lineHeight * rows.length + (paraSpacing ? paraSpacing * (drawData.paraNumber - 1) : 0);
2108
+ let starY = __baseLine;
2109
+ if (__clipText && realHeight > height) {
2110
+ realHeight = Math.max(height, __lineHeight);
2111
+ drawData.overflow = rows.length;
2112
+ }
2113
+ else {
2114
+ switch (verticalAlign) {
2115
+ case 'middle':
2116
+ y += (height - realHeight) / 2;
2117
+ break;
2118
+ case 'bottom':
2119
+ y += (height - realHeight);
2120
+ }
2121
+ }
2122
+ starY += y;
2123
+ let row, rowX, rowWidth;
2124
+ for (let i = 0, len = rows.length; i < len; i++) {
2125
+ row = rows[i];
2126
+ row.x = x;
2127
+ if (row.width < width || (row.width > width && !__clipText)) {
2128
+ switch (textAlign) {
2129
+ case 'center':
2130
+ row.x += (width - row.width) / 2;
2131
+ break;
2132
+ case 'right':
2133
+ row.x += width - row.width;
2134
+ }
2135
+ }
2136
+ if (row.paraStart && paraSpacing && i > 0)
2137
+ starY += paraSpacing;
2138
+ row.y = starY;
2139
+ starY += __lineHeight;
2140
+ if (drawData.overflow > i && starY > realHeight) {
2141
+ row.isOverflow = true;
2142
+ drawData.overflow = i + 1;
2143
+ }
2144
+ rowX = row.x;
2145
+ rowWidth = row.width;
2146
+ if (__letterSpacing < 0) {
2147
+ if (row.width < 0) {
2148
+ rowWidth = -row.width + style.fontSize + __letterSpacing;
2149
+ rowX -= rowWidth;
2150
+ rowWidth += style.fontSize;
2151
+ }
2152
+ else {
2153
+ rowWidth -= __letterSpacing;
2154
+ }
2155
+ }
2156
+ if (rowX < bounds.x)
2157
+ bounds.x = rowX;
2158
+ if (rowWidth > bounds.width)
2159
+ bounds.width = rowWidth;
2160
+ if (__clipText && width && width < rowWidth) {
2161
+ row.isOverflow = true;
2162
+ if (!drawData.overflow)
2163
+ drawData.overflow = rows.length;
2164
+ }
2165
+ }
2166
+ bounds.y = y;
2167
+ bounds.height = realHeight;
2168
+ }
2169
+
2170
+ function clipText(drawData, style, x, width) {
2171
+ if (!width)
2172
+ return;
2173
+ const { rows, overflow } = drawData;
2174
+ let { textOverflow } = style;
2175
+ rows.splice(overflow);
2176
+ if (textOverflow && textOverflow !== 'show') {
2177
+ if (textOverflow === 'hide')
2178
+ textOverflow = '';
2179
+ else if (textOverflow === 'ellipsis')
2180
+ textOverflow = '...';
2181
+ let char, charRight;
2182
+ const ellipsisWidth = textOverflow ? core.Platform.canvas.measureText(textOverflow).width : 0;
2183
+ const right = x + width - ellipsisWidth;
2184
+ const list = style.textWrap === 'none' ? rows : [rows[overflow - 1]];
2185
+ list.forEach(row => {
2186
+ if (row.isOverflow && row.data) {
2187
+ let end = row.data.length - 1;
2188
+ for (let i = end; i > -1; i--) {
2189
+ char = row.data[i];
2190
+ charRight = char.x + char.width;
2191
+ if (i === end && charRight < right) {
2192
+ break;
2193
+ }
2194
+ else if (charRight < right && char.char !== ' ') {
2195
+ row.data.splice(i + 1);
2196
+ row.width -= char.width;
2197
+ break;
2198
+ }
2199
+ row.width -= char.width;
2200
+ }
2201
+ row.width += ellipsisWidth;
2202
+ row.data.push({ char: textOverflow, x: charRight });
2203
+ if (row.textMode)
2204
+ toTextChar(row);
2205
+ }
2206
+ });
2207
+ }
2208
+ }
2209
+ function toTextChar(row) {
2210
+ row.text = '';
2211
+ row.data.forEach(char => {
2212
+ row.text += char.char;
2213
+ });
2214
+ row.data = null;
2215
+ }
2216
+
2217
+ function decorationText(drawData, style) {
2218
+ const { fontSize } = style;
2219
+ drawData.decorationHeight = fontSize / 11;
2220
+ switch (style.textDecoration) {
2221
+ case 'under':
2222
+ drawData.decorationY = fontSize * 0.15;
2223
+ break;
2224
+ case 'delete':
2225
+ drawData.decorationY = -fontSize * 0.35;
2226
+ }
2227
+ }
2228
+
2229
+ const { top, right, bottom, left } = core.Direction4;
2230
+ function getDrawData(content, style) {
2231
+ if (typeof content !== 'string')
2232
+ content = String(content);
2233
+ let x = 0, y = 0;
2234
+ let width = style.__getInput('width') || 0;
2235
+ let height = style.__getInput('height') || 0;
2236
+ const { textDecoration, __font, __padding: padding } = style;
2237
+ if (padding) {
2238
+ if (width) {
2239
+ x = padding[left];
2240
+ width -= (padding[right] + padding[left]);
2241
+ }
2242
+ if (height) {
2243
+ y = padding[top];
2244
+ height -= (padding[top] + padding[bottom]);
2245
+ }
2246
+ }
2247
+ const drawData = {
2248
+ bounds: { x, y, width, height },
2249
+ rows: [],
2250
+ paraNumber: 0,
2251
+ font: core.Platform.canvas.font = __font
2252
+ };
2253
+ createRows(drawData, content, style);
2254
+ if (padding)
2255
+ padAutoText(padding, drawData, style, width, height);
2256
+ layoutText(drawData, style);
2257
+ layoutChar(drawData, style, width);
2258
+ if (drawData.overflow)
2259
+ clipText(drawData, style, x, width);
2260
+ if (textDecoration !== 'none')
2261
+ decorationText(drawData, style);
2262
+ return drawData;
2263
+ }
2264
+ function padAutoText(padding, drawData, style, width, height) {
2265
+ if (!width) {
2266
+ switch (style.textAlign) {
2267
+ case 'left':
2268
+ offsetText(drawData, 'x', padding[left]);
2269
+ break;
2270
+ case 'right':
2271
+ offsetText(drawData, 'x', -padding[right]);
2272
+ }
2273
+ }
2274
+ if (!height) {
2275
+ switch (style.verticalAlign) {
2276
+ case 'top':
2277
+ offsetText(drawData, 'y', padding[top]);
2278
+ break;
2279
+ case 'bottom':
2280
+ offsetText(drawData, 'y', -padding[bottom]);
2281
+ }
2282
+ }
2283
+ }
2284
+ function offsetText(drawData, attrName, value) {
2285
+ const { bounds, rows } = drawData;
2286
+ bounds[attrName] += value;
2287
+ for (let i = 0; i < rows.length; i++)
2288
+ rows[i][attrName] += value;
2289
+ }
2290
+
2291
+ const TextConvertModule = {
2292
+ getDrawData
2293
+ };
2294
+
2295
+ function string(color, opacity) {
2296
+ if (typeof color === 'string')
2297
+ return color;
2298
+ let a = color.a === undefined ? 1 : color.a;
2299
+ if (opacity)
2300
+ a *= opacity;
2301
+ const rgb = color.r + ',' + color.g + ',' + color.b;
2302
+ return a === 1 ? 'rgb(' + rgb + ')' : 'rgba(' + rgb + ',' + a + ')';
2303
+ }
2304
+
2305
+ const ColorConvertModule = {
2306
+ string
2307
+ };
2308
+
2309
+ const { setPoint, addPoint, toBounds } = core.TwoPointBoundsHelper;
2310
+ function getTrimBounds(canvas) {
2311
+ const { width, height } = canvas.view;
2312
+ const { data } = canvas.context.getImageData(0, 0, width, height);
2313
+ let x, y, pointBounds, index = 0;
2314
+ for (let i = 0; i < data.length; i += 4) {
2315
+ if (data[i + 3] !== 0) {
2316
+ x = index % width;
2317
+ y = (index - x) / width;
2318
+ pointBounds ? addPoint(pointBounds, x, y) : setPoint(pointBounds = {}, x, y);
2319
+ }
2320
+ index++;
2321
+ }
2322
+ const bounds = new core.Bounds();
2323
+ toBounds(pointBounds, bounds);
2324
+ return bounds.scale(1 / canvas.pixelRatio).ceil();
2325
+ }
2326
+
2327
+ const ExportModule = {
2328
+ export(leaf, filename, options) {
2329
+ this.running = true;
2330
+ const fileType = core.FileHelper.fileType(filename);
2331
+ const isDownload = filename.includes('.');
2332
+ options = core.FileHelper.getExportOptions(options);
2333
+ return addTask((success) => new Promise((resolve) => {
2334
+ const over = (result) => {
2335
+ success(result);
2336
+ resolve();
2337
+ this.running = false;
2338
+ };
2339
+ const { toURL } = core.Platform;
2340
+ const { download } = core.Platform.origin;
2341
+ if (fileType === 'json') {
2342
+ isDownload && download(toURL(JSON.stringify(leaf.toJSON(options.json)), 'text'), filename);
2343
+ return over({ data: isDownload ? true : leaf.toJSON(options.json) });
2344
+ }
2345
+ if (fileType === 'svg') {
2346
+ isDownload && download(toURL(leaf.toSVG(), 'svg'), filename);
2347
+ return over({ data: isDownload ? true : leaf.toSVG() });
2348
+ }
2349
+ const { leafer } = leaf;
2350
+ if (leafer) {
2351
+ checkLazy(leaf);
2352
+ leafer.waitViewCompleted(() => __awaiter(this, void 0, void 0, function* () {
2353
+ let renderBounds, trimBounds, scaleX = 1, scaleY = 1;
2354
+ const { worldTransform, isLeafer, isFrame } = leaf;
2355
+ const { slice, trim, onCanvas } = options;
2356
+ const smooth = options.smooth === undefined ? leafer.config.smooth : options.smooth;
2357
+ const contextSettings = options.contextSettings || leafer.config.contextSettings;
2358
+ const screenshot = options.screenshot || leaf.isApp;
2359
+ const fill = (isLeafer && screenshot) ? (options.fill === undefined ? leaf.fill : options.fill) : options.fill;
2360
+ const needFill = core.FileHelper.isOpaqueImage(filename) || fill, matrix = new core.Matrix();
2361
+ if (screenshot) {
2362
+ renderBounds = screenshot === true ? (isLeafer ? leafer.canvas.bounds : leaf.worldRenderBounds) : screenshot;
2363
+ }
2364
+ else {
2365
+ let relative = options.relative || (isLeafer ? 'inner' : 'local');
2366
+ scaleX = worldTransform.scaleX;
2367
+ scaleY = worldTransform.scaleY;
2368
+ switch (relative) {
2369
+ case 'inner':
2370
+ matrix.set(worldTransform);
2371
+ break;
2372
+ case 'local':
2373
+ matrix.set(worldTransform).divide(leaf.localTransform);
2374
+ scaleX /= leaf.scaleX;
2375
+ scaleY /= leaf.scaleY;
2376
+ break;
2377
+ case 'world':
2378
+ scaleX = 1;
2379
+ scaleY = 1;
2380
+ break;
2381
+ case 'page':
2382
+ relative = leaf.leafer;
2383
+ default:
2384
+ matrix.set(worldTransform).divide(leaf.getTransform(relative));
2385
+ const l = relative.worldTransform;
2386
+ scaleX /= scaleX / l.scaleX;
2387
+ scaleY /= scaleY / l.scaleY;
2388
+ }
2389
+ renderBounds = leaf.getBounds('render', relative);
2390
+ }
2391
+ const scaleData = { scaleX: 1, scaleY: 1 };
2392
+ core.MathHelper.getScaleData(options.scale, options.size, renderBounds, scaleData);
2393
+ let pixelRatio = options.pixelRatio || 1;
2394
+ if (leaf.isApp) {
2395
+ scaleData.scaleX *= pixelRatio;
2396
+ scaleData.scaleY *= pixelRatio;
2397
+ pixelRatio = leaf.app.pixelRatio;
2398
+ }
2399
+ const { x, y, width, height } = new core.Bounds(renderBounds).scale(scaleData.scaleX, scaleData.scaleY);
2400
+ const renderOptions = { matrix: matrix.scale(1 / scaleData.scaleX, 1 / scaleData.scaleY).invert().translate(-x, -y).withScale(1 / scaleX * scaleData.scaleX, 1 / scaleY * scaleData.scaleY) };
2401
+ let canvas = core.Creator.canvas({ width: Math.round(width), height: Math.round(height), pixelRatio, smooth, contextSettings });
2402
+ let sliceLeaf;
2403
+ if (slice) {
2404
+ sliceLeaf = leaf;
2405
+ sliceLeaf.__worldOpacity = 0;
2406
+ leaf = leafer;
2407
+ renderOptions.bounds = canvas.bounds;
2408
+ }
2409
+ canvas.save();
2410
+ if (isFrame && fill !== undefined) {
2411
+ const oldFill = leaf.get('fill');
2412
+ leaf.fill = '';
2413
+ leaf.__render(canvas, renderOptions);
2414
+ leaf.fill = oldFill;
2415
+ }
2416
+ else {
2417
+ leaf.__render(canvas, renderOptions);
2418
+ }
2419
+ canvas.restore();
2420
+ if (sliceLeaf)
2421
+ sliceLeaf.__updateWorldOpacity();
2422
+ if (trim) {
2423
+ trimBounds = getTrimBounds(canvas);
2424
+ const old = canvas, { width, height } = trimBounds;
2425
+ const config = { x: 0, y: 0, width, height, pixelRatio };
2426
+ canvas = core.Creator.canvas(config);
2427
+ canvas.copyWorld(old, trimBounds, config);
2428
+ }
2429
+ if (needFill)
2430
+ canvas.fillWorld(canvas.bounds, fill || '#FFFFFF', 'destination-over');
2431
+ if (onCanvas)
2432
+ onCanvas(canvas);
2433
+ const data = filename === 'canvas' ? canvas : yield canvas.export(filename, options);
2434
+ over({ data, width: canvas.pixelWidth, height: canvas.pixelHeight, renderBounds, trimBounds });
2435
+ }));
2436
+ }
2437
+ else {
2438
+ over({ data: false });
2439
+ }
2440
+ }));
2441
+ }
2442
+ };
2443
+ let tasker;
2444
+ function addTask(task) {
2445
+ if (!tasker)
2446
+ tasker = new core.TaskProcessor();
2447
+ return new Promise((resolve) => {
2448
+ tasker.add(() => __awaiter(this, void 0, void 0, function* () { return yield task(resolve); }), { parallel: false });
2449
+ });
2450
+ }
2451
+ function checkLazy(leaf) {
2452
+ if (leaf.__.__needComputePaint)
2453
+ leaf.__.__computePaint();
2454
+ if (leaf.isBranch)
2455
+ leaf.children.forEach(child => checkLazy(child));
2456
+ }
2457
+
2458
+ const canvas = core.LeaferCanvasBase.prototype;
2459
+ const debug = core.Debug.get('@leafer-ui/export');
2460
+ canvas.export = function (filename, options) {
2461
+ const { quality, blob } = core.FileHelper.getExportOptions(options);
2462
+ if (filename.includes('.')) {
2463
+ return this.saveAs(filename, quality);
2464
+ }
2465
+ else if (blob) {
2466
+ return this.toBlob(filename, quality);
2467
+ }
2468
+ else {
2469
+ return this.toDataURL(filename, quality);
2470
+ }
2471
+ };
2472
+ canvas.toBlob = function (type, quality) {
2473
+ return new Promise((resolve) => {
2474
+ core.Platform.origin.canvasToBolb(this.view, type, quality).then((blob) => {
2475
+ resolve(blob);
2476
+ }).catch((e) => {
2477
+ debug.error(e);
2478
+ resolve(null);
2479
+ });
2480
+ });
2481
+ };
2482
+ canvas.toDataURL = function (type, quality) {
2483
+ return core.Platform.origin.canvasToDataURL(this.view, type, quality);
2484
+ };
2485
+ canvas.saveAs = function (filename, quality) {
2486
+ return new Promise((resolve) => {
2487
+ core.Platform.origin.canvasSaveAs(this.view, filename, quality).then(() => {
2488
+ resolve(true);
2489
+ }).catch((e) => {
2490
+ debug.error(e);
2491
+ resolve(false);
2492
+ });
2493
+ });
2494
+ };
2495
+
2496
+ Object.assign(draw.TextConvert, TextConvertModule);
2497
+ Object.assign(draw.ColorConvert, ColorConvertModule);
2498
+ Object.assign(draw.Paint, PaintModule);
2499
+ Object.assign(draw.PaintImage, PaintImageModule);
2500
+ Object.assign(draw.PaintGradient, PaintGradientModule);
2501
+ Object.assign(draw.Effect, EffectModule);
2502
+ Object.assign(draw.Export, ExportModule);
2503
+
2504
+ try {
2505
+ if (wx)
2506
+ useCanvas('miniapp', wx);
2507
+ }
2508
+ catch (_a) { }
2509
+
2510
+ Object.defineProperty(exports, "LeaferImage", {
2511
+ enumerable: true,
2512
+ get: function () { return core.LeaferImage; }
2513
+ });
2514
+ exports.Layouter = Layouter;
2515
+ exports.LeaferCanvas = LeaferCanvas;
2516
+ exports.Renderer = Renderer;
2517
+ exports.Watcher = Watcher;
2518
+ exports.useCanvas = useCanvas;
2519
+ Object.keys(core).forEach(function (k) {
2520
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
2521
+ enumerable: true,
2522
+ get: function () { return core[k]; }
2523
+ });
2524
+ });
2525
+ Object.keys(draw).forEach(function (k) {
2526
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
2527
+ enumerable: true,
2528
+ get: function () { return draw[k]; }
2529
+ });
2530
+ });