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