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